Debian Codegraph @

tbl-dctrl/tbl-dctrl.c

raw
1 /* dctrl-tools - Debian control file inspection tools
2 Copyright © 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012 Antti-Juhani Kaijanaho
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
17 */
18
19 #include <argp.h>
20 #include <locale.h>
21 #include <stdlib.h>
22 #include <wchar.h>
23 #include "misc.h"
24 #include "msg.h"
25 #include "i18n.h"
26 #include "ifile.h"
27 #include "para_bundle.h"
28 #include "sorter.h"
29
30 enum {
31 OPT_SILENT = 256,
32 OPT_MMAP
33 };
34
35 const char * argp_program_version = "tbl-dctrl (dctrl-tools) " VERSION;
36 const char * argp_program_bug_address = MAINTAINER;
37
38 static struct argp_option options[] = {
39 { "delimiter", 'd', N_("DELIM"), 0, N_("Specify a delimiter."), 0 },
40 { "no-heading", 'H', 0, 0, N_("Do not print a table heading"), 0 },
41 { "column", 'c', N_("SPEC"), 0, N_("Append the specified column."), 0 },
42 { "copying", 'C', 0, 0, N_("Print out the copyright license."), 0 },
43 { "errorlevel", 'l', N_("LEVEL"), 0, N_("Set debugging level to LEVEL."), 0 },
44 { "mmap", OPT_MMAP, 0, 0, N_("Attempt mmapping input files"), 0 },
45 { 0, 0, 0, 0, 0, 0 }
46 };
47
48 #define MAX_FNAMES 4096
49 #define MAX_COLUMNS 4096
50
51 struct arguments {
52 char *delim; // NULL if not specced
53 /* True iff some column lengths are unknown and need to be
54 * calculated. */
55 _Bool need_preprocessing;
56 /* True if no headings should be printed. */
57 _Bool no_heading;
58 /* Number of columns specified */
59 size_t num_columns;
60 /* Number of file names seen. */
61 size_t num_fnames;
62 /* Columns specified on the command line. */
63 struct column {
64 char *heading;
65 size_t field_inx;
66 size_t column_width;
67 } columns[MAX_COLUMNS];
68 /* File names seen on the command line. */
69 struct ifile fname[MAX_FNAMES];
70 };
71
72 size_t linewrap(char **res, char const *orig, size_t orig_len,
73 size_t col_width)
74 {
75 assert(col_width > 0);
76
77 // these are primarily manipulated by the INSERT macro below
78 size_t max = 16; // the capacity of rv
79 char *rv = malloc(max); // the prospective return value
80 size_t len = 0; // length of the string in rv
81
82 #define INSERT(c) do { \
83 if (len >= max) { \
84 max *= 2; \
85 rv = realloc(rv, max); \
86 if (rv == NULL) fatal_enomem(NULL); \
87 } \
88 rv[len++] = (c); \
89 } while (0)
90
91 size_t ll = 0; // the length of the current line, so far
92
93 // the following two record the location of the closest
94 // breakpoint so far
95 size_t bpo = 0; // indexes orig
96 size_t bpr = 0; // indexes rv
97
98 size_t num_lines = 1;
99
100 mbstate_t mbs;
101 memset(&mbs, 0, sizeof mbs);
102 for (size_t i = 0; i < orig_len; /**/) {
103 if (orig[i] == '\n') {
104 i++;
105 ll = 0;
106 INSERT('\n');
107 num_lines++;
108 continue;
109 }
110 if (ll == col_width) {
111 if (rv[bpr] == '\n') {
112 // no suitable breakpoint on this line,
113 // break here
114 } else {
115 assert(bpr < len);
116 len = bpr;
117 assert(bpo < i);
118 i = bpo + 1;
119 }
120 ll = 0;
121 INSERT('\n');
122 num_lines++;
123 continue;
124 }
125 if (orig[i] == ' ' || orig[i] == '\t') {
126 // record this breakpoint
127 bpo = i;
128 bpr = len;
129 }
130 int n = mbrlen(orig + i, orig_len - i, &mbs);
131 if (n <= 0) break;
132 for (int j = 0; j < n; j++) INSERT(orig[i+j]);
133 i += n;
134 ll++;
135 }
136 INSERT('\0');
137 *res = rv;
138 return num_lines;
139 #undef INSERT
140 }
141
142 void print_line(struct arguments *args,
143 struct fsaf_read_rv const columns[/*args->num_columns*/])
144 {
145 struct print_line_data {
146 char *wrapped;
147 size_t wrapped_len;
148 size_t inx;
149 } data[args->num_columns];
150 size_t num_lines = 0;
151 for (size_t i = 0; i < args->num_columns; i++) {
152 size_t n = linewrap(&data[i].wrapped,
153 columns[i].b, columns[i].len,
154 args->columns[i].column_width);
155 if (n > num_lines) num_lines = n;
156 data[i].inx = 0;
157 data[i].wrapped_len = strlen(data[i].wrapped);
158 }
159 for (size_t i = 0; i < num_lines; i++) {
160 for (size_t j = 0; j < args->num_columns; j++) {
161 if (args->delim == NULL) {
162 fputs("| ", stdout);
163 } else if (j > 0) {
164 fputs(args->delim, stdout);
165 }
166 mbstate_t mbs;
167 memset(&mbs, 0, sizeof mbs);
168 size_t k;
169 size_t m = 0;
170 for (k = data[j].inx;
171 k < data[j].wrapped_len;
172 /**/) {
173 if (data[j].wrapped[k] == '\n') {
174 k++;
175 break;
176 }
177 int n = mbrlen(data[j].wrapped + k,
178 data[j].wrapped_len - k,
179 &mbs);
180 for (int l = 0; l < n; l++) {
181 putchar(data[j].wrapped[k+l]);
182 }
183 if (n <= 0) n = 1;
184 k += n;
185 m++;
186 }
187
188 if (args->delim == NULL) {
189 while (m < args->columns[j].column_width) {
190 m++;
191 putchar(' ');
192 }
193 putchar(' ');
194 }
195 data[j].inx = k;
196 }
197 if (args->delim == NULL) fputs("|", stdout);
198 putchar('\n');
199 }
200 for (size_t i = 0; i < args->num_columns; i++) free(data[i].wrapped);
201 }
202
203 void print_head(struct arguments *args)
204 {
205 if (args->delim == NULL) {
206 for (size_t i = 0; i < args->num_columns; i++) {
207 putchar('+');
208 for (size_t j = 0;
209 j < args->columns[i].column_width + 2;
210 j++) {
211 putchar('=');
212 }
213 }
214 putchar('+');
215 putchar('\n');
216 }
217
218 if (!args->no_heading) {
219 struct fsaf_read_rv columns[args->num_columns];
220 for (size_t i = 0; i < args->num_columns; i++) {
221 columns[i].b = args->columns[i].heading;
222 columns[i].len = strlen(columns[i].b);
223 }
224 print_line(args, columns);
225 if (args->delim == NULL) {
226 for (size_t i = 0; i < args->num_columns; i++) {
227 putchar('+');
228 for (size_t j = 0;
229 j < args->columns[i].column_width + 2;
230 j++) {
231 putchar('-');
232 }
233 }
234 putchar('+');
235 putchar('\n');
236 }
237 }
238 }
239
240 void print_foot(struct arguments *args)
241 {
242 if (args->delim != NULL) return;
243 for (size_t i = 0; i < args->num_columns; i++) {
244 putchar('+');
245 for (size_t j = 0; j < args->columns[i].column_width + 2; j++) {
246 putchar('=');
247 }
248 }
249 putchar('+');
250 putchar('\n');
251 }
252
253 void handle_para(struct arguments *args, struct paragraph *para)
254 {
255 struct fsaf_read_rv columns[args->num_columns];
256 for (size_t i = 0; i < args->num_columns; i++) {
257 columns[i] = get_field(para, args->columns[i].field_inx, -1);
258 }
259 print_line(args, columns);
260 }
261
262 static error_t parse_opt (int key, char * arg, struct argp_state * state)
263 {
264 struct arguments * args = state->input;
265 switch (key) {
266 case 'C':
267 if (!to_stdout (COPYING)) fail();
268 exit(0);
269 case 'c': {
270 char *carg = strdup(arg);
271 if (carg == NULL) fatal_enomem(0);
272 char *lens = strrchr(carg, ':');
273 if (lens != NULL) *(lens++) = '\0'; else lens = "";
274 char *fn = strchr(carg, '=');
275 if (fn != NULL) *(fn++) = '\0';
276 struct column *col = &args->columns[args->num_columns];
277 col->heading = carg;
278 col->field_inx = fieldtrie_insert(fn == NULL ? carg : fn)->inx;
279 size_t n = 0;
280 _Bool err = 0;
281 for (char const *p = lens; *p != '\0'; p++) {
282 if (!('0' <= *p && *p <= '9')) {
283 err = 1;
284 continue;
285 }
286 n = n * 10 + (*p - '0');
287 }
288 if (err) message(L_IMPORTANT, 0, _("invalid column length"));
289 col->column_width = n > 0 ? n : (size_t)-1;
290 if (n == 0) args->need_preprocessing = 1;
291 args->num_columns++;
292 }
293 break;
294 case 'd':
295 args->delim = strdup(arg);
296 if (args->delim == NULL) fatal_enomem(NULL);
297 break;
298 case 'H':
299 args->no_heading = 1;
300 break;
301 case 'l': {
302 int ll = str2loglevel(arg);
303 if (ll < 0)
304 {
305 message(L_FATAL, 0, _("no such log level '%s'"), arg);
306 fail();
307 }
308 set_loglevel(ll);
309 debug_message("parse_opt: l", 0);
310 }
311 break;
312 case ARGP_KEY_ARG:
313 debug_message("parse_opt: argument", 0);
314 {
315 char const * s;
316 if (args->num_fnames >= MAX_FNAMES) {
317 message(L_FATAL, 0, _("too many file names"));
318 fail();
319 }
320 s = strdup(arg);
321 if (s == 0) fatal_enomem(0);
322 args->fname[args->num_fnames++] =
323 (struct ifile){ .mode = m_read, .s = s };
324 }
325 break;
326 case OPT_MMAP:
327 debug_message("parse_opt: mmap", 0);
328 /* ignore */
329 break;
330 case ARGP_KEY_END:
331 case ARGP_KEY_ARGS: case ARGP_KEY_INIT: case ARGP_KEY_SUCCESS:
332 case ARGP_KEY_ERROR: case ARGP_KEY_FINI: case ARGP_KEY_NO_ARGS:
333 debug_message("parse_opt: ignored", 0);
334 break;
335 default:
336 return ARGP_ERR_UNKNOWN;
337 }
338 return 0;
339 }
340
341 static char progdoc [] =
342 N_("tbl-dctrl -- tabularize Debian control files");
343
344 static struct argp argp = { options, parse_opt, 0, progdoc, 0, 0, 0 };
345
346 static size_t mbs_len(char const *mbs, size_t n, char const *fname)
347 {
348 if (n == (size_t)(-1)) n = strlen(mbs);
349 size_t mlen = 0;
350 size_t len = 0;
351 mbstate_t mbstate;
352 memset(&mbstate, 0, sizeof mbstate);
353 for (size_t k = 0; k < n;/**/) {
354 if (mbs[k] == '\n') {
355 if (len > mlen) mlen = len;
356 len = 0;
357 k++;
358 continue;
359 }
360 len++;
361 int delta = mbrlen(mbs + k, n - k, &mbstate);
362 if (delta <= 0) {
363 message(L_IMPORTANT, fname,
364 _("bad multibyte character"));
365 k++;
366 } else {
367 k += delta;
368 }
369 }
370 return len > mlen ? len : mlen;
371 }
372
373 int main(int argc, char * argv[])
374 {
375 setlocale(LC_ALL, "");
376 bindtextdomain (PACKAGE, LOCALEDIR);
377 textdomain(PACKAGE);
378
379 fieldtrie_init();
380
381 static struct arguments args;
382
383 //keys_init(&args.keys);
384 msg_set_progname(argv[0]);
385 argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, &args);
386
387 if (args.num_fnames == 0) {
388 args.fname[args.num_fnames++] = (struct ifile){
389 .mode = m_read, .s = "-"
390 };
391 }
392
393 if (args.num_columns == 0 || args.need_preprocessing) {
394 struct para_bundle pb;
395
396 bundle_init(&pb);
397
398 for (size_t i = 0; i < args.num_fnames; i++) {
399 bundle_slurp(&pb, args.fname[i],
400 args.num_columns == 0);
401 }
402
403 const size_t n = fieldtrie_count();
404
405 if (args.num_columns == 0) {
406 size_t m = n <= MAX_COLUMNS ? n : MAX_COLUMNS;
407 for (size_t i = 0; i < m; i++) {
408 struct field_attr *fa = fieldtrie_get(i);
409 args.columns[i].heading =
410 strndup(fa->name, fa->namelen);
411 if (args.columns[i].heading == NULL)
412 fatal_enomem(NULL);
413 args.columns[i].field_inx = fa->inx;
414 args.columns[i].column_width = -1;
415 }
416 args.num_columns = m;
417 }
418
419 size_t lens[n];
420 for (size_t i = 0; i < n; i++) lens[i] = 0;
421
422 for (size_t i = 0; i < pb.num_paras; i++) {
423 struct paragraph *p = pb.paras[i];
424 assert(p->nfields <= n);
425 for (size_t j = 0; j < p->nfields; j++) {
426 if (p->fields[j].first == NULL) continue;
427 struct fsaf_read_rv r = get_field(p, j, -1);
428 size_t len = mbs_len(r.b, r.len,
429 p->common->fp->fname);
430 if (len > lens[j]) lens[j] = len;
431 }
432 }
433 for (size_t i = 0; i < args.num_columns; i++) {
434 size_t width = args.columns[i].column_width;
435 size_t headw = mbs_len(args.columns[i].heading, -1, 0);
436 if (width == (size_t)(-1)) width = lens[i];
437 if (headw > width) width = headw;
438 args.columns[i].column_width = width;
439 }
440 print_head(&args);
441 for (size_t i = 0; i < pb.num_paras; i++) {
442 handle_para(&args, pb.paras[i]);
443 }
444 print_foot(&args);
445 } else {
446 print_head(&args);
447 for (size_t i = 0; i < args.num_fnames; ++i) {
448 int fd;
449 struct ifile fname = args.fname[i];
450
451 if (fname.mode == m_error) continue;
452
453 fd = open_ifile(fname);
454 if (fd == -1) break;
455
456 if (!chk_ifile(fname, fd)) break;
457
458 FSAF * fp = fsaf_fdopen(fd, fname.s);
459 para_parser_t pp;
460 para_parser_init(&pp, fp, true, false, false);
461 para_t para;
462 para_init(&pp, &para);
463 while (1) {
464 para_parse_next(&para);
465 if (para_eof(&pp)) break;
466 handle_para(&args, &para);
467 }
468
469 close_ifile(fname, fd);
470 }
471 print_foot(&args);
472 }
473
474 return 0;
475 }