Merge branch 'vendor/OPENSSL'
[dragonfly.git] / gnu / lib / libregex / test / fileregex.c
1 #include <sys/types.h>
2 #include <stdio.h>
3 #include "regex.h"
4
5 #define BYTEWIDTH 8
6
7 /* Sorry, but this is just a test program.  */
8 #define LINE_MAX 500
9
10 int
11 main (argc, argv)
12     int argc;
13     char *argv[];
14 {
15   FILE *f;
16   char *filename;
17   char pat[500]; /* Sorry for that maximum size, too.  */
18   char line[LINE_MAX];
19   struct re_pattern_buffer buf;
20   char fastmap[(1 << BYTEWIDTH)];
21   const char *compile_ret;
22   unsigned lineno = 1;
23   unsigned nfound = 0;
24
25   /* Actually, it might be useful to allow the data file to be standard
26      input, and to specify the pattern on the command line.  */
27   if (argc != 2)
28     {
29       fprintf (stderr, "Usage: %s <filename>.\n", argv[0]);
30       exit (1);
31     }
32
33   filename = argv[1];
34   f = fopen (filename, "r");
35   if (f == NULL)
36     perror (filename);
37
38   buf.allocated = 0;
39   buf.buffer = NULL;
40   buf.fastmap = fastmap;
41
42   printf ("Pattern = ", pat);
43   gets (pat);
44
45   if (feof (stdin))
46     {
47       putchar ('\n');
48       exit (0);
49     }
50
51   compile_ret = re_compile_pattern (pat, strlen (pat), &buf);
52   if (compile_ret != NULL)
53     {
54       fprintf (stderr, "%s: %s\n", pat, compile_ret);
55       exit (1);
56     }
57
58   while (fgets (line, LINE_MAX, f) != NULL)
59     {
60       size_t len = strlen (line);
61       struct re_registers regs;
62       int search_ret
63         = re_search_2 (&buf, NULL, 0, line, len, 0, len, &regs, len);
64
65       if (search_ret == -2)
66         {
67           fprintf (stderr, "%s:%d: re_search failed.\n", filename, lineno);
68           exit (1);
69         }
70
71       nfound += search_ret != -1;
72       lineno++;
73     }
74
75   printf ("Matches found: %u (out of %u lines).\n", nfound, lineno - 1);
76   return 0;
77 }