aboutsummaryrefslogtreecommitdiff
path: root/posix/tst-boost.c
blob: ccef2ab5f4aeeef84733bfcc7bf4ee431df3e51a (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
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
/* Regular expression tests.
   Copyright (C) 2003-2024 Free Software Foundation, Inc.
   This file is part of the GNU C Library.

   The GNU C Library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   The GNU C Library 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
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with the GNU C Library; if not, see
   <https://www.gnu.org/licenses/>.  */

#include <sys/types.h>
#include <mcheck.h>
#include <regex.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

void
frob_escapes (char *src, int pattern)
{
  char *dst;

  for (dst = src; *src != '\0'; dst++, src++)
    {
      if (*src == '\\')
	{
	  switch (src[1])
	    {
	    case 't':
	      src++;
	      *dst = '\t';
	      continue;
	    case 'n':
	      src++;
	      *dst = '\n';
	      continue;
	    case 'r':
	      src++;
	      *dst = '\r';
	      continue;
	    case '\\':
	    case '^':
	    case '{':
	    case '|':
	    case '}':
	      if (!pattern)
		{
		  src++;
		  *dst = *src;
		  continue;
		}
	      break;
	    }
	}
      if (src != dst)
	*dst = *src;
    }
  *dst = '\0';
}

int
main (int argc, char **argv)
{
  int ret = 0, n;
  char *line = NULL;
  size_t line_len = 0;
  ssize_t len;
  FILE *f;
  char *pattern, *string;
  int flags = REG_EXTENDED;
  int eflags = 0;
  regex_t re;
  regmatch_t rm[20];

  mtrace ();

  if (argc < 2)
    {
      fprintf (stderr, "Missing test filename\n");
      return 1;
    }

  f = fopen (argv[1], "r");
  if (f == NULL)
    {
      fprintf (stderr, "Couldn't open %s\n", argv[1]);
      return 1;
    }

  while ((len = getline (&line, &line_len, f)) > 0)
    {
      char *p, *q;
      int i;

      if (line[len - 1] == '\n')
	line[--len] = '\0';

      puts (line);

      if (line[0] == ';')
	continue;

      if (line[0] == '\0')
	continue;

      if (line[0] == '-')
	{
	  if (strstr (line, "REG_BASIC"))
	    flags = 0;
	  else
	    flags = REG_EXTENDED;
	  if (strstr (line, "REG_ICASE"))
	    flags |= REG_ICASE;
	  if (strstr (line, "REG_NEWLINE"))
	    flags |= REG_NEWLINE;
	  eflags = 0;
	  if (strstr (line, "REG_NOTBOL"))
	    eflags |= REG_NOTBOL;
	  if (strstr (line, "REG_NOTEOL"))
	    eflags |= REG_NOTEOL;
	  continue;
	}

      pattern = line + strspn (line, " \t");
      if (*pattern == '\0')
	continue;
      p = pattern + strcspn (pattern, " \t");
      if (*p == '\0')
	continue;
      *p++ = '\0';

      string = p + strspn (p, " \t");
      if (*string == '\0')
	continue;
      if (*string == '"')
	{
	  string++;
	  p = strchr (string, '"');
	  if (p == NULL)
	    continue;
	  *p++ = '\0';
	}
      else
	{
	  p = string + strcspn (string, " \t");
	  if (*string == '!')
	    string = NULL;
	  else if (*p == '\0')
	    continue;
	  else
	    *p++ = '\0';
	}

      frob_escapes (pattern, 1);
      if (string != NULL)
	frob_escapes (string, 0);

      n = regcomp (&re, pattern, flags);
      if (n != 0)
	{
	  if (string != NULL)
	    {
	      char buf[500];
	      regerror (n, &re, buf, sizeof (buf));
	      printf ("FAIL regcomp unexpectedly failed: %s\n",
		      buf);
	      ret = 1;
	    }
	  continue;
	}
      else if (string == NULL)
	{
	  regfree (&re);
	  puts ("FAIL regcomp unpexpectedly succeeded");
	  ret = 1;
	  continue;
	}

      if (regexec (&re, string, 20, rm, eflags))
	{
	  for (i = 0; i < 20; ++i)
	    {
	      rm[i].rm_so = -1;
	      rm[i].rm_eo = -1;
	    }
	}

      regfree (&re);

      for (i = 0; i < 20 && *p != '\0'; ++i)
	{
	  int rm_so, rm_eo;

	  rm_so = strtol (p, &q, 10);
	  if (p == q)
	    break;
	  p = q;

	  rm_eo = strtol (p, &q, 10);
	  if (p == q)
	    break;
	  p = q;

	  if (rm[i].rm_so != rm_so || rm[i].rm_eo != rm_eo)
	    {
	      printf ("FAIL rm[%d] %d..%d != expected %d..%d\n",
		      i, rm[i].rm_so, rm[i].rm_eo, rm_so, rm_eo);
	      ret = 1;
	      break;
	    }
	}
    }

  free (line);
  fclose (f);
  return ret;
}