summaryrefslogtreecommitdiffstats
path: root/gnu/lib/libregex/test/fileregex.c
blob: ba8106218c89fa7ed4777c0bf488322867f28458 (plain)
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
#include <sys/types.h>
#include <stdio.h>
#include "regex.h"

#define BYTEWIDTH 8

/* Sorry, but this is just a test program.  */
#define LINE_MAX 500

int
main (argc, argv)
    int argc;
    char *argv[];
{
  FILE *f;
  char *filename;
  char pat[500]; /* Sorry for that maximum size, too.  */
  char line[LINE_MAX];
  struct re_pattern_buffer buf;
  char fastmap[(1 << BYTEWIDTH)];
  const char *compile_ret;
  unsigned lineno = 1;
  unsigned nfound = 0;

  /* Actually, it might be useful to allow the data file to be standard
     input, and to specify the pattern on the command line.  */
  if (argc != 2)
    {
      fprintf (stderr, "Usage: %s <filename>.\n", argv[0]);
      exit (1);
    }

  filename = argv[1];
  f = fopen (filename, "r");
  if (f == NULL)
    perror (filename);

  buf.allocated = 0;
  buf.buffer = NULL;
  buf.fastmap = fastmap;

  printf ("Pattern = ", pat);
  gets (pat);

  if (feof (stdin))
    {
      putchar ('\n');
      exit (0);
    }

  compile_ret = re_compile_pattern (pat, strlen (pat), &buf);
  if (compile_ret != NULL)
    {
      fprintf (stderr, "%s: %s\n", pat, compile_ret);
      exit (1);
    }

  while (fgets (line, LINE_MAX, f) != NULL)
    {
      size_t len = strlen (line);
      struct re_registers regs;
      int search_ret
        = re_search_2 (&buf, NULL, 0, line, len, 0, len, &regs, len);

      if (search_ret == -2)
        {
          fprintf (stderr, "%s:%d: re_search failed.\n", filename, lineno);
          exit (1);
        }

      nfound += search_ret != -1;
      lineno++;
    }

  printf ("Matches found: %u (out of %u lines).\n", nfound, lineno - 1);
  return 0;
}
OpenPOWER on IntegriCloud