1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
|
/* dctrl-tools - Debian control file inspection tools
Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004 Antti-Juhani Kaijanaho
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <argp.h>
#include <assert.h>
#include <fcntl.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <unistd.h>
#include "fnutil.h"
#include "fsaf.h"
#include "i18n.h"
#include "msg.h"
#include "paragraph.h"
#include "predicate.h"
#include "rc.h"
#include "util.h"
const char * argp_program_version = "grep-dctrl (dctrl-tools) " VERSION;
const char * argp_program_bug_address = MAINTAINER;
const char description [] = "Description";
size_t description_inx;
static char progdoc [] = N_("grep-dctrl -- grep Debian control files");
#define OPT_CONFIG 256
#define OPT_OPTPARSE 257
#define OPT_SILENT 258
#define OPT_EQ 259
#define OPT_LT 260
#define OPT_LE 261
#define OPT_GT 262
#define OPT_GE 263
#undef BANNER
#define COPYING "/usr/share/common-licenses/GPL"
/* Copy the file called fname to standard outuput stream. Return zero
iff problems were encountered. */
static int to_stdout (const char * fname)
{
FILE * f;
int c;
int rv = 1;
f = fopen (fname, "r");
if (f == 0) {
message (L_FATAL, strerror (errno), COPYING);
return 0;
}
while ( ( c = getc (f)) != EOF) putchar (c);
if (ferror (f)) {
message (L_FATAL, strerror (errno), COPYING);
rv = 0;
}
fclose (f);
return rv;
}
#ifdef BANNER
void banner(bool automatic)
{
char * fname = fnqualify_xalloc("~/.grep-dctrl-banner-shown");
struct stat st;
if (automatic) {
int r = stat(fname, &st);
if (r == 0) goto end;
}
FILE * fp = fopen("/dev/tty", "w");
if (fp == 0) {
perror("/dev/tty");
goto end;
}
fprintf(fp,
"==========================================================================\n"
" NOTE \n"
" grep-dctrl has been rewritten from scratch. Although this does add new \n"
" features, regressions are certainly possible. Please watch for them and \n"
" report them to the BTS. \n"
"==========================================================================\n"
"(The above annoying banner will not be shown to you again, unless you\n"
"request it with the -B switch. It will also be removed entirely soon.)\n");
int r = creat(fname, 0644);
if (r == -1) perror(fname);
if (!automatic) exit(0);
for (int i = 15; i > 0; i--) {
fprintf(fp, "%2d seconds until program is resumed...\r", i);
fflush(fp);
sleep(1);
}
fprintf(fp, " \r");
fflush(fp);
end:
free(fname);
}
#endif
static struct argp_option options[] = {
#ifdef BANNER
{ "banner", 'B', 0, 0, N_("Show the testing banner.") },
#endif
{ "errorlevel", 'l', N_("LEVEL"), 0, N_("Set debugging level to LEVEL.") },
{ "field", 'F', N_("FIELD,FIELD,..."), 0, N_("Restrict pattern matching to the FIELDs given.") },
{ 0, 'P', 0, 0, N_("This is a shorthand for -FPackage.") },
{ "show-field", 's', N_("FIELD,FIELD,..."), 0, N_("Show only the body of these fields from the matching paragraphs.") },
{ 0, 'd', 0, 0, N_("Show only the first line of the \"Description\" field from the matching paragraphs.") },
{ "no-field-names", 'n', 0, 0, N_("Suppress field names when showing specified fields.") },
{ "eregex", 'e', 0, 0, N_("Regard the pattern as an extended POSIX regular expression.") },
{ "regex", 'r', 0, 0, N_("The pattern is a standard POSIX regular expression.") },
{ "ignore-case", 'i', 0, 0, N_("Ignore case when looking for a match.") },
{ "invert-match", 'v', 0, 0, N_("Show only paragraphs that do not match.") },
{ "count", 'c', 0, 0, N_("Show only the count of matching paragraphs.") },
{ "config-file", OPT_CONFIG, N_("FNAME"),0, N_("Use FNAME as the config file.") },
{ "exact-match", 'X', 0, 0, N_("Do an exact match.") },
{ "copying", 'C', 0, 0, N_("Print out the copyright license.") },
{ "and", 'a', 0, 0, N_("Conjunct predicates.") },
{ "or", 'o', 0, 0, N_("Disjunct predicates.") },
{ "not", '!', 0, 0, N_("Negate the following predicate.") },
{ "eq", OPT_EQ, 0, 0, N_("Test for numerical equality.") },
{ "lt", OPT_LT, 0, 0, N_("Numerical test: <.") },
{ "le", OPT_LE, 0, 0, N_("Numerical test: <=.") },
{ "gt", OPT_GT, 0, 0, N_("Numerical test: >.") },
{ "ge", OPT_GE, 0, 0, N_("Numerical test: >=.") },
{ "debug-optparse", OPT_OPTPARSE, 0, 0, N_("Debug option parsing.") },
{ "quiet", 'q', 0, 0, N_("Do no output to stdout.") },
{ "silent", OPT_SILENT, 0, 0, N_("Do no output to stdout.") },
{ 0 }
};
enum state { STATE_ATOM, STATE_NEG, STATE_CONJ, STATE_DISJ,
STATE_PAREN, STATE_START, STATE_FINISHED };
#define MAX_FNAMES 4096
static int debug_optparse = 0;
struct arguments {
/* Parser state, used when parsing the predicate. */
enum state state;
/* Parser state flag: last token seen was ')' */
bool just_seen_cparen;
/* Top of the parser stack. */
size_t top;
/* Number of file names seen. */
size_t num_fnames;
/**/
size_t num_show_fields;
/**/
size_t num_search_fields;
/* A machine-readable representation of the predicate. */
struct predicate p;
/* Configuration file name */
char const * rcname;
/* Quiet operation? */
bool quiet;
/* Do show field names? */
bool show_field_name;
/* Do show (only) first line of Description? */
bool short_descr;
/* Does show_fields contain Description? */
bool description_selected;
/* Count matching paragraphs? */
bool count;
/* Invert match? */
bool invert_match;
/* Parser stack. */
struct stack_elem {
enum state state;
/* A linked list of instructions. */
struct insn_node {
int insn;
struct insn_node * next;
} * insns_first, * insns_last;
} stack[MAX_OPS];
/* File names seen on the command line. */
struct ifile fname[MAX_FNAMES];
/**/
struct show_fields {
char const * name;
size_t inx;
} show_fields[MAX_FIELDS];
/* Search field names seen during current atom. */
char * search_fields[MAX_FIELDS];
};
struct atom * clone_atom(struct arguments * args)
{
if (args->p.num_atoms >= MAX_ATOMS) {
message(L_FATAL, _("predicate is too complex"), 0);
fail();
}
struct atom * atom = get_current_atom(&args->p);
int push_insn = I_PUSH(args->p.num_atoms);
struct atom * rv = &args->p.atoms[args->p.num_atoms++];
rv->field_name = atom->field_name;
rv->field_inx = atom->field_inx;
rv->mode = atom->mode;
rv->ignore_case = atom->ignore_case;
rv->pat = atom->pat;
rv->patlen = atom->patlen;
assert(args->top > 0);
struct stack_elem * selem = &args->stack[args->top-1];
assert(selem->insns_first != 0);
assert(selem->insns_last != 0);
struct insn_node * node1 = malloc(sizeof *node1);
struct insn_node * node2 = malloc(sizeof *node2);
if (node1 == 0 || node2 == 0) fatal_enomem(0);
node1->insn = push_insn;
node2->insn = I_OR;
node1->next = node2;
node2->next = 0;
selem->insns_last->next = node1;
selem->insns_last = node2;
return rv;
}
static void finish_atom(struct arguments * args)
{
struct atom * atom = get_current_atom(&args->p);
if (atom->pat == 0) {
message(L_FATAL, _("A pattern is mandatory"), 0);
fail();
}
for (size_t i = 0; i < args->num_search_fields; i++) {
if (i > 0) atom = clone_atom(args);
atom->field_name = args->search_fields[i];
predicate_finish_atom(&args->p);
}
// If there are no fields, we have not yet run this...
// ... but it must be done (especially with -r/-e atoms)
if (args->num_search_fields == 0) predicate_finish_atom(&args->p);
args->num_search_fields = 0;
}
/* Pop off one stack state, inserting the associated instructions to
* the predicate program. If paren is true, current state must be
* STATE_PAREN, and if paren is false, it must not be STATE_PAREN. */
static void leave(struct arguments * args, int paren)
{
debug_message("leaving...", 0);
assert(paren == (args->state == STATE_PAREN));
if (args->state == STATE_ATOM) finish_atom(args);
assert(args->top > 0);
--args->top;
for (struct insn_node * it = args->stack[args->top].insns_first;
it != 0;) {
addinsn(&args->p, it->insn);
struct insn_node * next = it->next;
free(it);
it = next;
}
args->stack[args->top].insns_first = 0;
args->stack[args->top].insns_last = 0;
args->state = args->stack[args->top].state;
}
#define ENTER(state,insn) do { enter(args, (state), (insn)); } while (0)
static void prim_enter(struct arguments * args, const enum state state, const int insn)
{
if (args->top >= MAX_OPS) {
message(L_FATAL, _("predicate is too complex"), 0);
fail();
}
// args->stack[args->top].insn = insn;
struct insn_node * node = malloc(sizeof *node);
if (node == 0) fatal_enomem(0);
node->insn = insn;
node->next = 0;
args->stack[args->top].insns_first = node;
args->stack[args->top].insns_last = node;
args->stack[args->top].state = args->state;
++args->top;
args->state = state;
}
/* Push current state along with the given instruction to stack and
* enter the given state.
*/
static void enter(struct arguments * args, const enum state state, const int insn)
{
if (args->state == STATE_FINISHED) {
message(L_FATAL, _("syntax error in command line"), 0);
fail();
}
while (args->state < state || (state != STATE_NEG && args->state == state)) {
leave(args, 0);
}
prim_enter(args, state, insn);
debug_message("entering...", 0);
}
#define FINISH do { finish(args); } while (0)
/* Flush the state stack. */
static void finish(struct arguments * args)
{
while (args->top > 0) {
if (args->state == STATE_PAREN) {
message(L_FATAL, _("missing ')' in command line"), 0);
fail();
}
leave(args, 0);
}
assert(args->state == STATE_START);
args->state = STATE_FINISHED;
}
#define ENTER_ATOM (enter_atom((args),(just_seen_cparen)))
/* If necessary, enter STATE_ATOM and allocate a new atom, pushing
* along with the old state a PUSH instruction for the new atom to the
* parser stack. If we are already in STATE_ATOM, reuse the current
* atom. */
static struct atom * enter_atom(struct arguments * args, bool just_seen_cparen)
{
if (just_seen_cparen) {
message(L_FATAL, _("Unexpected atom in command line. "
"Did you forget to use a connective?"), 0);
fail();
}
struct atom * rv;
if (args->state == STATE_ATOM || args->state == STATE_FINISHED) {
assert(args->p.num_atoms > 0);
return &args->p.atoms[args->p.num_atoms-1];
}
if (args->p.num_atoms >= MAX_ATOMS) {
message(L_FATAL, _("predicate is too complex"), 0);
fail();
}
ENTER(STATE_ATOM, I_PUSH(args->p.num_atoms));
rv = &args->p.atoms[args->p.num_atoms++];
rv->field_name = 0;
rv->field_inx = -1;
rv->mode = M_SUBSTR;
rv->ignore_case = 0;
rv->pat = 0;
rv->patlen = 0;
return rv;
}
#define set_mode(nmode) do { \
atom = ENTER_ATOM; \
if (atom->mode != M_SUBSTR) { \
message(L_FATAL, _("inconsistent atom modifiers"), 0); \
fail(); \
} \
atom->mode = (nmode); \
} while (0)
static error_t parse_opt (int key, char * arg, struct argp_state * state)
{
struct arguments * args = state->input;
bool just_seen_cparen = args->just_seen_cparen;
args->just_seen_cparen = false;
struct atom * atom;
debug_message("parse_opt", 0);
switch (key) {
case 'C':
if (!to_stdout (COPYING)) fail();
exit(0);
#ifdef BANNER
case 'B':
banner(false);
#endif
case 'v':
args->invert_match = true;
break;
case 'c':
args->count = true;
break;
case 'q': case OPT_SILENT:
debug_message("parse_opt: q", 0);
args->quiet = true;
break;
case 'n':
debug_message("parse_opt: n", 0);
args->show_field_name = false;
break;
case 'd':
args->short_descr = true;
break;
case 's': {
char * carg = strdup(arg);
if (carg == 0) fatal_enomem(0);
for (char * s = strtok(carg, ","); s != 0; s = strtok(0, ",")){
struct show_fields * sf =
&args->show_fields[args->num_show_fields];
sf->name = strdup(s);
if (sf->name == 0) fatal_enomem(0);
struct field_attr fa = { .numeric = false };
sf->inx = fieldtrie_insert(&args->p.trie, s, fa);
if (sf->inx == description_inx) {
args->description_selected = true;
}
++args->num_show_fields;
}
free(carg);
}
break;
case 'l': {
int ll = str2loglevel(optarg);
if (ll < 0)
{
message(L_FATAL, _("no such log level"), optarg);
fail();
}
set_loglevel(ll);
debug_message("parse_opt: l", 0);
}
break;
case '!':
debug_message("parse_opt: !", 0);
ENTER(STATE_NEG, I_NEG);
break;
case 'a':
debug_message("parse_opt: a", 0);
ENTER(STATE_CONJ, I_AND);
break;
case 'o':
debug_message("parse_opt: o", 0);
ENTER(STATE_DISJ, I_OR);
break;
case 'P':
debug_message("parse_opt: P", 0);
arg = "Package";
/* pass through */
case 'F': {
atom = ENTER_ATOM;
char * carg = strdup(arg);
if (carg == 0) fatal_enomem(0);
for (char * s = strtok(carg, ","); s != 0; s = strtok(0, ",")){
char * tmp = strdup(s);
if (tmp == 0) fatal_enomem(0);
args->search_fields[args->num_search_fields++] = tmp;
}
free(carg);
}
break;
case 'X':
debug_message("parse_opt: X", 0);
set_mode(M_EXACT);
break;
case 'r':
debug_message("parse_opt: r", 0);
set_mode(M_REGEX);
break;
case 'e':
debug_message("parse_opt: e", 0);
set_mode(M_EREGEX);
break;
case OPT_EQ:
debug_message("parse_opt: eq", 0);
set_mode(M_NUM_EQ);
break;
case OPT_LT:
debug_message("parse_opt: lt", 0);
set_mode(M_NUM_LT);
break;
case OPT_LE:
debug_message("parse_opt: le", 0);
set_mode(M_NUM_LE);
break;
case OPT_GT:
debug_message("parse_opt: gt", 0);
set_mode(M_NUM_GT);
break;
case OPT_GE:
debug_message("parse_opt: ge", 0);
set_mode(M_NUM_GE);
break;
case 'i':
debug_message("parse_opt: i", 0);
atom = ENTER_ATOM;
atom->ignore_case = 1;
break;
case OPT_OPTPARSE:
debug_message("parse_opt: optparse", 0);
debug_optparse = 1;
break;
case ARGP_KEY_ARG:
debug_message("parse_opt: argument", 0);
redo:
debug_message("!!!", 0);
if (strcmp(arg, "!") == 0) {
debug_message("parse_opt: !", 0);
ENTER(STATE_NEG, I_NEG);
break;
}
if (strcmp(arg, "(") == 0) {
debug_message("parse_opt: (", 0);
prim_enter(args, STATE_PAREN, I_NOP);
break;
}
if (strcmp(arg, ")") == 0) {
debug_message("parse_opt: )", 0);
while (args->state != STATE_PAREN) {
if (args->top == 0) {
message(L_FATAL, _("unexpected ')' in command line"), 0);
fail();
}
leave(args, 0);
}
leave(args, 1);
args->just_seen_cparen = true;
break;
}
if (args->state == STATE_FINISHED) {
char const * s;
if (args->num_fnames >= MAX_FNAMES) {
message(L_FATAL, _("too many file names"), 0);
fail();
}
s = strdup(arg);
if (s == 0) fatal_enomem(0);
args->fname[args->num_fnames++] =
(struct ifile){ .mode = m_read, .s = s };
break;
}
if (just_seen_cparen || strcmp(arg, "--") == 0) { FINISH; break; }
atom = ENTER_ATOM;
if (atom->pat != 0) { FINISH; goto redo; }
atom->patlen = strlen(arg);
atom->pat = malloc(atom->patlen+1);
if (atom->pat == 0) fatal_enomem(0);
strcpy((char*)atom->pat, arg);
break;
case ARGP_KEY_END:
debug_message("parse_opt: end", 0);
if (args->state != STATE_FINISHED) FINISH;
break;
case ARGP_KEY_ARGS: case ARGP_KEY_INIT: case ARGP_KEY_SUCCESS:
case ARGP_KEY_ERROR: case ARGP_KEY_FINI: case ARGP_KEY_NO_ARGS:
debug_message("parse_opt: ignored", 0);
break;
case OPT_CONFIG:
debug_message("parse_opt: --config-file", 0);
args->rcname = strdup(arg);
break;
default:
return ARGP_ERR_UNKNOWN;
}
return 0;
}
static void dump_args(struct arguments * args)
{
size_t i;
assert(args->state == STATE_FINISHED);
assert(args->top == 0);
printf("num_atoms = %zi\n", args->p.num_atoms);
for (i = 0; i < args->p.num_atoms; i++) {
printf("atoms[%zi].field_name = %s\n", i, args->p.atoms[i].field_name);
printf("atoms[%zi].mode = %i\n", i, args->p.atoms[i].mode);
printf("atoms[%zi].ignore_case = %i\n", i, args->p.atoms[i].ignore_case);
printf("atoms[%zi].pat = %s\n", i, args->p.atoms[i].pat);
}
printf("proglen = %zi\n", args->p.proglen);
for (i = 0; i < args->p.proglen; i++) {
int op = args->p.program[i];
printf("program[%zi] = ", i);
switch (op) {
case I_NOP: puts("NOP"); break;
case I_NEG: puts("NEG"); break;
case I_AND: puts("AND"); break;
case I_OR: puts("OR"); break;
default:
printf("PUSH(%i)\n", op - I_PUSH(0));
}
}
printf("num_fnames = %zi\n", args->num_fnames);
for (i = 0; i < args->num_fnames; i++) {
printf("fname[%zi].mode = %s, fname[%zi].s = %s\n",
i, ifile_modes[args->fname[i].mode],
i, args->fname[i].s);
}
}
static void close_ifile(struct ifile f, int fd)
{
close(fd);
if (f.mode == m_exec) {
int status;
int r = wait(&status);
if (r == -1) {
errno_msg(L_IMPORTANT, f.s);
return;
}
if (WIFEXITED(status)) {
if (WEXITSTATUS(status) == 0) return;
fprintf(stderr, _("%s: command (%s) failed "
"(exit status %d)\n"),
get_progname(),
f.s,
WEXITSTATUS(status));
record_error();
return;
}
if (WIFSIGNALED(status)) {
fprintf(stderr, _("%s: command (%s) was killed "
"by signal %d\n"),
get_progname(),
f.s,
WTERMSIG(status));
record_error();
return;
}
}
}
static int open_pipe(char const * s)
{
int ps[2];
int r = pipe(ps);
if (r == -1) {
errno_msg(L_IMPORTANT, 0);
return -1;
}
pid_t pid = fork();
switch (pid) {
case -1:
errno_msg(L_IMPORTANT, 0);
close(ps[0]);
close(ps[1]);
return -1;
case 0:
// child
close(STDIN_FILENO);
r = dup2(ps[1], STDOUT_FILENO);
if (r == -1) {
fprintf(stderr, "%s (child): %s\n",
get_progname(), strerror(errno));
_exit(1);
}
close(ps[0]);
close(ps[1]);
execl("/bin/sh", "/bin/sh", "-c", s, 0);
fprintf(stderr, _("%s (child): failed to exec /bin/sh: %s\n"),
get_progname(), strerror(errno));
_exit(1);
}
// parent
close(ps[1]);
return ps[0];
}
static int open_ifile(struct ifile f)
{
switch (f.mode) {
case m_read:
if (strcmp(f.s, "-") == 0) {
return STDIN_FILENO;
}
{
int fd = open(f.s, O_RDONLY);
if (fd == -1) {
fprintf(stderr, "%s: %s: %s\n",
get_progname(), f.s, strerror(errno));
record_error();
return -1;
}
return fd;
}
case m_exec:
return open_pipe(f.s);
case m_error:
abort();
}
abort();
}
static struct argp argp = { options, parse_opt, 0, progdoc };
int main (int argc, char * argv[])
{
setlocale(LC_ALL, "");
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain(PACKAGE);
static struct arguments args;
args.state = STATE_START;
args.show_field_name = true;
msg_set_progname(argv[0]);
init_predicate(&args.p);
struct field_attr fa = { .numeric = false };
description_inx = fieldtrie_insert(&args.p.trie, description, fa);
argp_parse (&argp, argc, argv, ARGP_IN_ORDER, 0, &args);
#ifdef BANNER
banner(true);
#endif
if (debug_optparse) { dump_args(&args); return 0; }
if (args.p.num_atoms == 0) {
message(L_FATAL, _("a predicate is required"), 0);
fail();
}
if (!check_predicate(&args.p)) {
message(L_FATAL, _("malformed predicate"), 0);
fail();
}
if (args.short_descr && !args.description_selected) {
if (args.num_show_fields >= MAX_FIELDS) {
message(L_FATAL, _("too many output fields"), 0);
fail();
}
message(L_INFORMATIONAL,
_("Adding \"Description\" to selected output fields because of -d"),
0);
args.show_fields[args.num_show_fields].name = description;
args.show_fields[args.num_show_fields].inx = description_inx;
++args.num_show_fields;
}
if (!args.show_field_name && args.num_show_fields == 0) {
message(L_FATAL,
_("cannot suppress field names when showing whole paragraphs"),
0);
fail();
}
size_t count = 0;
bool found = false;
for (size_t i = 0; i < args.num_fnames || (i == 0 && args.num_fnames == 0); ++i) {
int fd;
struct ifile fname;
if (args.num_fnames == 0) {
// Hardcode grep-dctrl <-> "-" mapping so that
// Debian packages can genuinely depend on it.
char * argv0 = fnbase(argv[0]);
if (strcmp(argv0, "grep-dctrl") == 0) {
fname = (struct ifile){ .mode = m_read,
.s = "-" };
} else {
fname = find_ifile_by_exename(argv0, args.rcname);
}
} else {
fname = args.fname[i];
}
if (fname.mode == m_error) break;
fd = open_ifile(fname);
if (fd == -1) break;
{
struct stat stat;
int r = fstat(fd, &stat);
mode_t m = stat.st_mode;
if (r == -1) {
fprintf(stderr, _("%s: %s: cannot stat: %s\n"),
argv[0], fname.s, strerror(errno));
record_error();
close_ifile(fname, fd);
break;
}
if (!(S_ISREG(m) || S_ISCHR(m) || S_ISFIFO(m))) {
fprintf(stderr, "%s: %s: %s\n",
argv[0], fname.s,
S_ISDIR(m) ? _("is a directory, skipping") :
S_ISBLK(m) ? _("is a block device, skipping") :
S_ISLNK(m) ? _("internal error") :
S_ISSOCK(m) ? _("is a socket, skipping") :
_("unknown file type, skipping"));
record_error();
close_ifile(fname, fd);
break;
}
}
FSAF * fp = fsaf_fdopen(fd);
para_t para;
for (para_init(¶, fp, &args.p.trie);
!para_eof(¶);
para_parse_next(¶)) {
if ((args.invert_match || !does_para_satisfy(&args.p, ¶))
&& (!args.invert_match || does_para_satisfy(&args.p, ¶))) {
continue;
}
if (args.quiet) {
exit(0);
}
found = true;
if (args.count) {
++count;
continue;
}
if (args.num_show_fields == 0) {
struct fsaf_read_rv r = fsaf_read(fp, para.start, para.end - para.start);
fwrite(r.b, 1, r.len, stdout);
putchar('\n');
continue;
}
for (size_t j = 0; j < args.num_show_fields; j++) {
if (args.show_field_name) {
printf("%s: ", args.show_fields[j].name);
}
struct field_data * fd = ¶.fields[args.show_fields[j].inx];
struct fsaf_read_rv r = fsaf_read(fp, fd->start, fd->end - fd->start);
if (args.short_descr &&
args.show_fields[j].inx == description_inx) {
char * nl = memchr(r.b, '\n', r.len);
if (nl != 0) r.len = nl - r.b;
}
fwrite(r.b, 1, r.len, stdout);
puts("");
continue;
}
if (args.num_show_fields > 1) puts("");
}
close_ifile(fname, fd);
}
if (count) printf("%zi\n", count);
return errors_reported() ? 2 : found ? 0 : 1;
}
|