File: grep-dctrl.c

package info (click to toggle)
grep-dctrl 1.3a
  • links: PTS
  • area: main
  • in suites: potato
  • size: 660 kB
  • ctags: 403
  • sloc: ansic: 4,004; sh: 358; makefile: 226; sed: 93
file content (436 lines) | stat: -rw-r--r-- 12,877 bytes parent folder | download
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
/*   grep-dctrl - grep Debian control files
     Copyright (C) 1999, 2000  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; see the file COPYING.  If not, write to
     the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
     Boston, MA 02111-1307, USA.
  
     The author can be reached via mail at (ISO 8859-1 charset for the city)
        Antti-Juhani Kaijanaho
        Helvintie 2 e as 9
        FIN-40500 JYVSKYL
        FINLAND
        EUROPE
     and via electronic mail from
        gaia@iki.fi
     If you have a choice, use the email address; it is more likely to
     stay current.

*/

#include <errno.h>
#include <limits.h>
#include <locale.h>
#include <getopt.h>
#include <publib.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "i18n.h"
#include "matcher.h"
#include "msg.h"
#include "rc.h"
#include "strutil.h"

/* If you change these, update the manual page, too */
#define FIELD_MAXLEN 512  
#define SHOWFIELDS_MAXLEN 512
#define SHOWFIELD_MAXNUM 256

#define NO_REGEX -1
#define EXTENDED_REGEX 1
#define STD_REGEX 0

#define OPT_RCFILE UCHAR_MAX + 1

static void
print_version (const char * progname)
{
  printf ("grep-dctrl %s\n", VERSION);
  puts   ( _("This program is free software and comes WITHOUT ANY KIND "
             "OF WARRANTY."));
  printf (_("See the file %s for more information, or do a\n"), COPYING);
  printf (_("\"%s --copying\", piping the output to your favourite pager.\n"),
          progname);
}

static void
print_usage (const char * progname)
{
  printf (_("Usage: %s [ options ] PATTERN [ FILE ... ]\n"
            "       %s --version | --copying | --help | -V | -C | -h\n"),
          progname, progname);
  puts   (_("Possible options:"));
  puts   (_("       -l LEVEL, --errorlevel=LEVEL"));
  puts   (_("             Set debugging level to LEVEL (0..3,"));
  puts   (_("             biggest is most verbose)."));
  puts   (_("       -F FIELD, --field=FIELD"));
  puts   (_("             Restrict pattern matching to FIELD."));
  puts   (_("       -s FIELD,FIELD,...; --show-field=FIELD,FIELD,..."));
  puts   (_("             Show only the body of these fields"));
  puts   (_("             from the matching paragraphs."));
  puts   (_("       -n, --no-field-names"));
  puts   (_("             Print only the bodies of the fields to be shown"));
  puts   (_("       -e, --eregex"));
  puts   (_("             The pattern is an extended POSIX "
            "regular expression"));
  puts   (_("       -r, --regex"));
  puts   (_("             The pattern is a POSIX regular expression"));
  printf (_("       -i, --ignore-case\n"));
  puts   (_("             Ignore case when looking for a match."));
  puts   (_("       -v, --invert-match"));
  puts   (_("             Show those paragraphs that don't match."));
  puts   (_("       -c FNAME, --config-file=FNAME"));
  puts   (_("             Use FNAME as the config file."));
  printf (_("             (Defaults: %s/grep-dctrl.rc, ~/.grep-dctrlrc)\n"),
          SYSCONF);
  puts   (_("             NOTE: the short option \"-c\" is deprecated;"));
  puts   (_("             use the long option \"-config-file\" instead."));
  puts   (_("       -X, --exact-match"));
  puts   (_("             Require an exact match (not substring match)."));
  puts   (_("       -P    Shorthand for '-FPackage'."));
  puts   (_("       -V, --version"));
  puts   (_("             Print out version information."));
  puts   (_("       -C, --copying"));
  puts   (_("             Print out the copyright license. (long output)"));
  puts   (_("       -h, --help"));
  puts   (_("             Print out this help screen."));
  puts   ("");
  puts   (_("Please, report bugs to <gaia@iki.fi>."));
}

/* 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;
}

static void
set_match_field (struct matcher_t * matcher, const char * field)
{
  static char match_field [FIELD_MAXLEN] = "";

  if (match_field [0] != 0)
    {
      message (L_FATAL, _("don't try setting the search field twice"), 0);
      exit (EXIT_FAILURE);
    }

  if (strlen (field) >= FIELD_MAXLEN)
    {
      message (L_FATAL, _("field name exceeds maximum length"), 0);
      exit (EXIT_FAILURE);
    }
  strcpy (match_field, left_trimmed (field));
  trim_right (match_field);
  matcher->field = match_field;
}

int
main (int argc, char * argv [])
{
  int i;
  int regex_class = NO_REGEX;
  int rv = EXIT_SUCCESS;
  struct matcher_t matcher = { MATCH_FIXED, 1, 1 };
  char show_field_string [SHOWFIELDS_MAXLEN] = "";
  char * show_fields [SHOWFIELD_MAXNUM] = { 0 }; /* show these fields only */
  const char * rcname = 0;

  setlocale (LC_ALL, "");
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  msg_set_progname (argv[0]);

  while (1)
    {
      int c;
      static struct option long_options [] = {
        { "version",        no_argument,       0, 'V' },
        { "help",           no_argument,       0, 'h' },
        { "errorlevel",     required_argument, 0, 'l' },
        { "field",          required_argument, 0, 'F' },
        { "show-field",     required_argument, 0, 's' },
        { "eregex",         no_argument,       0, 'e' },
        { "regex",          no_argument,       0, 'r' },
        { "copying",        no_argument,       0, 'C' },
        { "ignore-case",    no_argument,       0, 'i' },
        { "invert-match",   no_argument,       0, 'v' },
        { "exact-match",    no_argument,       0, 'X' },
        { "config-file",    required_argument, 0, OPT_RCFILE },
        { "no-field-names", no_argument,       0, 'n' },
        { 0, 0, 0, 0 } };
      static const char * short_options = "c:CeF:hil:nPrs:vVX";

      c = getopt_long (argc, argv, short_options, long_options, 0);

      if (c == EOF)
        break;

      switch (c)
        {
        case 'V':
          print_version (argv [0]);
          exit (EXIT_SUCCESS);

        case 'h':
          print_usage (argv [0]);
          exit (EXIT_SUCCESS);

        case 'C':
          if (!to_stdout (COPYING))
            exit (EXIT_FAILURE);

          exit (EXIT_SUCCESS);

        case 'n':
          matcher.show_fieldnames = 0;
          break;

        case 'c':
          fprintf(stderr, "%s: warning: -c is deprecated; "
                  "use --config-file instead\n", argv[0]);
          /* fall through to OPT_RCFILE */

        case OPT_RCFILE:
          rcname = xstrdup (optarg);
          break;

        case 'l':
          {
            int ll = str2loglevel (optarg);
            if (ll < 0)
              {
                message (L_FATAL, _("no such log level"), optarg);
                exit (EXIT_FAILURE);
              }
            set_loglevel (ll);
          }
          break;

        case 'P':
          set_match_field (&matcher, "Package");
          break;

        case 'F':
          set_match_field (&matcher, optarg);
          break;

        case 'X':
          if (matcher.type == MATCH_REGEX)
            {
              message (L_FATAL,
                       _("don't know how to do an exact regex match"), 0);
              exit (EXIT_FAILURE);
            }

          matcher.type = MATCH_EXACT;
          debug_message (_("using exact matches"), 0);
          break;

        case 's':
          if (*show_field_string != 0)
            {
              message (L_FATAL, _("you can only use -s once"), 0);
              exit (EXIT_FAILURE);
            }
          if (strlen (optarg) >= SHOWFIELDS_MAXLEN)
            {
              message (L_FATAL, _("output field spec exceeds maximum length"), 0);
              exit (EXIT_FAILURE);
            }
          strcpy (show_field_string, optarg);

          /* Tokenize the field spec into field names. */
          {  
            int i = 0;
            show_fields [i] = strtok (show_field_string, ",");
            while (show_fields [i] != 0 && i < SHOWFIELD_MAXNUM)
              show_fields [++i] = strtok (0, ",");
            if (show_fields [i] != 0)
              {
                message (L_FATAL, _("too many output fields"), 0);
                exit (EXIT_FAILURE);
              }
          }
          break;
                
        case 'e':
          if (regex_class != NO_REGEX)
            {
              message (L_FATAL, _("do not use both -e and -r options"), 0);
              exit (EXIT_FAILURE);
            }
          if (matcher.type == MATCH_EXACT)
            {
              message (L_FATAL,
                       _("don't know how to do an exact regex match"), 0);
              exit (EXIT_FAILURE);
            }
          debug_message (_("using extended regular expressions"), 0);
          matcher.type = MATCH_REGEX;
          regex_class = EXTENDED_REGEX;
          break;

        case 'r':
          if (regex_class != NO_REGEX)
            {
              message (L_FATAL, _("do not use both -e and -r options"), 0);
              exit (EXIT_FAILURE);
            }
          if (matcher.type == MATCH_EXACT)
            {
              message (L_FATAL,
                       _("don't know how to do an exact regex match"), 0);
              exit (EXIT_FAILURE);
            }
          debug_message ("using conventional regular expressions", 0);
          matcher.type = MATCH_REGEX;
          regex_class = STD_REGEX;
          break;

        case 'i':
          debug_message (_("ignoring case"), 0);
          matcher.case_sensitive = 0;
          break;
 
        case 'v':
          debug_message(_("inverted match"), 0);
          matcher.inverse = 1;
          break;

        case '?': case ':':
          exit (EXIT_FAILURE);

        default:
          message (L_FATAL,
                   _("I'm broken - please report this to <gaia@iki.fi>"),
                   "main.c");
          abort ();
        }
          
    }

  if (show_fields [0] == 0 && !matcher.show_fieldnames)
    {
      message (L_FATAL,
               _("cannot suppress field names when showing whole paragraphs"),
               0);
      exit (EXIT_FAILURE);
    }

  if (argc == optind)
    {
      message (L_FATAL, _("a pattern is mandatory"), 0);
      exit (EXIT_FAILURE);
    }

  switch (matcher.type)
    {
      int rerr;
      static regex_t regex;

    case MATCH_FIXED: case MATCH_EXACT:
      matcher.pattern.fixed = argv [optind];
      break;

    case MATCH_REGEX:
      matcher.pattern.regex = &regex;
      rerr = regcomp (matcher.pattern.regex, argv [optind],
                      ( (regex_class == EXTENDED_REGEX ? REG_EXTENDED : 0)
                        | REG_NOSUB
                        | (matcher.case_sensitive ? 0 : REG_ICASE)));
      if (rerr != 0)
        {
          char * s;

          s = get_regerror (rerr, matcher.pattern.regex);
          if (s == 0)
            fatal_enomem (0);

          message (L_IMPORTANT, s, 0);
          free (s);
          exit (EXIT_FAILURE);
        }
      break;

    default:
      message (L_FATAL, _("I'm broken - please report this to <gaia@iki.fi>"),
               "main.c/regex");
      abort ();

    }

  for (i = ++optind; i < argc || (argc == optind && optind == i); i++)
    {
      FILE * f;
      const char * fname;

      if (optind == argc)
        fname = find_ifile_by_exename (fnbase (argv [0]), rcname);
      else
        fname = argv [i];

      if (fname == 0)
        {
          message (L_FATAL, _("need a file name to grep"), 0);
          exit (EXIT_FAILURE);
        }

      if (strcmp (fname, "-") == 0)
        {
          f = stdin;
          fname = "stdin";
        }
      else
        {
          f = fopen (fname, "r");
          if (f == NULL)
            {
              perror (argv[0]);
              break;
            }
        }
      rv = (grep_control (&matcher, f, show_fields, fname))
        ? rv : EXIT_FAILURE;
      if (f != stdin)
        fclose (f);
    }      
  return rv;
}