GCC Code Coverage Report | |||||||||||||||||||||
|
|||||||||||||||||||||
Line | Branch | Exec | Source |
1 |
/* $OpenBSD: compile.c,v 1.40 2015/10/26 22:24:44 jca Exp $ */ |
||
2 |
|||
3 |
/*- |
||
4 |
* Copyright (c) 1992 Diomidis Spinellis. |
||
5 |
* Copyright (c) 1992, 1993 |
||
6 |
* The Regents of the University of California. All rights reserved. |
||
7 |
* |
||
8 |
* This code is derived from software contributed to Berkeley by |
||
9 |
* Diomidis Spinellis of Imperial College, University of London. |
||
10 |
* |
||
11 |
* Redistribution and use in source and binary forms, with or without |
||
12 |
* modification, are permitted provided that the following conditions |
||
13 |
* are met: |
||
14 |
* 1. Redistributions of source code must retain the above copyright |
||
15 |
* notice, this list of conditions and the following disclaimer. |
||
16 |
* 2. Redistributions in binary form must reproduce the above copyright |
||
17 |
* notice, this list of conditions and the following disclaimer in the |
||
18 |
* documentation and/or other materials provided with the distribution. |
||
19 |
* 3. Neither the name of the University nor the names of its contributors |
||
20 |
* may be used to endorse or promote products derived from this software |
||
21 |
* without specific prior written permission. |
||
22 |
* |
||
23 |
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND |
||
24 |
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
||
25 |
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
||
26 |
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE |
||
27 |
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
||
28 |
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
||
29 |
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
||
30 |
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT |
||
31 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY |
||
32 |
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
||
33 |
* SUCH DAMAGE. |
||
34 |
*/ |
||
35 |
|||
36 |
#include <sys/types.h> |
||
37 |
#include <sys/stat.h> |
||
38 |
|||
39 |
#include <ctype.h> |
||
40 |
#include <errno.h> |
||
41 |
#include <fcntl.h> |
||
42 |
#include <limits.h> |
||
43 |
#include <regex.h> |
||
44 |
#include <stdio.h> |
||
45 |
#include <stdlib.h> |
||
46 |
#include <string.h> |
||
47 |
|||
48 |
#include "defs.h" |
||
49 |
#include "extern.h" |
||
50 |
|||
51 |
#define LHSZ 128 |
||
52 |
#define LHMASK (LHSZ - 1) |
||
53 |
static struct labhash { |
||
54 |
struct labhash *lh_next; |
||
55 |
u_int lh_hash; |
||
56 |
struct s_command *lh_cmd; |
||
57 |
int lh_ref; |
||
58 |
} *labels[LHSZ]; |
||
59 |
|||
60 |
static char *compile_addr(char *, struct s_addr *); |
||
61 |
static char *compile_ccl(char **, char *); |
||
62 |
static char *compile_delimited(char *, char *, int); |
||
63 |
static char *compile_flags(char *, struct s_subst *); |
||
64 |
static char *compile_re(char *, regex_t **); |
||
65 |
static char *compile_subst(char *, struct s_subst *); |
||
66 |
static char *compile_text(void); |
||
67 |
static char *compile_tr(char *, char **); |
||
68 |
static struct s_command |
||
69 |
**compile_stream(struct s_command **); |
||
70 |
static char *duptoeol(char *, char *, char **); |
||
71 |
static void enterlabel(struct s_command *); |
||
72 |
static struct s_command |
||
73 |
*findlabel(char *); |
||
74 |
static void fixuplabel(struct s_command *, struct s_command *); |
||
75 |
static void uselabel(void); |
||
76 |
|||
77 |
/* |
||
78 |
* Command specification. This is used to drive the command parser. |
||
79 |
*/ |
||
80 |
struct s_format { |
||
81 |
char code; /* Command code */ |
||
82 |
int naddr; /* Number of address args */ |
||
83 |
enum e_args args; /* Argument type */ |
||
84 |
}; |
||
85 |
|||
86 |
static struct s_format cmd_fmts[] = { |
||
87 |
{'{', 2, GROUP}, |
||
88 |
{'}', 0, ENDGROUP}, |
||
89 |
{'a', 1, TEXT}, |
||
90 |
{'b', 2, BRANCH}, |
||
91 |
{'c', 2, TEXT}, |
||
92 |
{'d', 2, EMPTY}, |
||
93 |
{'D', 2, EMPTY}, |
||
94 |
{'g', 2, EMPTY}, |
||
95 |
{'G', 2, EMPTY}, |
||
96 |
{'h', 2, EMPTY}, |
||
97 |
{'H', 2, EMPTY}, |
||
98 |
{'i', 1, TEXT}, |
||
99 |
{'l', 2, EMPTY}, |
||
100 |
{'n', 2, EMPTY}, |
||
101 |
{'N', 2, EMPTY}, |
||
102 |
{'p', 2, EMPTY}, |
||
103 |
{'P', 2, EMPTY}, |
||
104 |
{'q', 1, EMPTY}, |
||
105 |
{'r', 1, RFILE}, |
||
106 |
{'s', 2, SUBST}, |
||
107 |
{'t', 2, BRANCH}, |
||
108 |
{'w', 2, WFILE}, |
||
109 |
{'x', 2, EMPTY}, |
||
110 |
{'y', 2, TR}, |
||
111 |
{'!', 2, NONSEL}, |
||
112 |
{':', 0, LABEL}, |
||
113 |
{'#', 0, COMMENT}, |
||
114 |
{'=', 1, EMPTY}, |
||
115 |
{'\0', 0, COMMENT}, |
||
116 |
}; |
||
117 |
|||
118 |
/* The compiled program. */ |
||
119 |
struct s_command *prog; |
||
120 |
|||
121 |
/* |
||
122 |
* Compile the program into prog. |
||
123 |
* Initialise appends. |
||
124 |
*/ |
||
125 |
void |
||
126 |
compile(void) |
||
127 |
781 |
{ |
|
128 |
781 |
*compile_stream(&prog) = NULL; |
|
129 |
781 |
fixuplabel(prog, NULL); |
|
130 |
781 |
uselabel(); |
|
131 |
781 |
appends = xreallocarray(NULL, appendnum, sizeof(struct s_appends)); |
|
132 |
781 |
match = xreallocarray(NULL, maxnsub + 1, sizeof(regmatch_t)); |
|
133 |
781 |
} |
|
134 |
|||
135 |
#define EATSPACE() do { \ |
||
136 |
if (p) \ |
||
137 |
while (isascii((unsigned char)*p) && \ |
||
138 |
isspace((unsigned char)*p)) \ |
||
139 |
p++; \ |
||
140 |
} while (0) |
||
141 |
|||
142 |
static struct s_command ** |
||
143 |
compile_stream(struct s_command **link) |
||
144 |
781 |
{ |
|
145 |
char *p; |
||
146 |
static char *lbuf; /* To avoid excessive malloc calls */ |
||
147 |
static size_t bufsize; |
||
148 |
struct s_command *cmd, *cmd2, *stack; |
||
149 |
struct s_format *fp; |
||
150 |
int naddr; /* Number of addresses */ |
||
151 |
|||
152 |
781 |
stack = 0; |
|
153 |
for (;;) { |
||
154 |
✓✓ | 14716 |
if ((p = cu_fgets(&lbuf, &bufsize)) == NULL) { |
155 |
✗✓ | 781 |
if (stack != 0) |
156 |
error(COMPILE, "unexpected EOF (pending }'s)"); |
||
157 |
781 |
return (link); |
|
158 |
} |
||
159 |
|||
160 |
✓✗✓✗ ✓✓ |
29963 |
semicolon: EATSPACE(); |
161 |
✓✓ | 14732 |
if (*p == '#' || *p == '\0') |
162 |
201 |
continue; |
|
163 |
✗✓ | 14531 |
if (*p == ';') { |
164 |
p++; |
||
165 |
goto semicolon; |
||
166 |
} |
||
167 |
14531 |
*link = cmd = xmalloc(sizeof(struct s_command)); |
|
168 |
14531 |
link = &cmd->next; |
|
169 |
14531 |
cmd->nonsel = cmd->inrange = 0; |
|
170 |
/* First parse the addresses */ |
||
171 |
14531 |
naddr = 0; |
|
172 |
|||
173 |
/* Valid characters to start an address */ |
||
174 |
#define addrchar(c) (strchr("0123456789/\\$", (c))) |
||
175 |
✓✓ | 14531 |
if (addrchar(*p)) { |
176 |
1008 |
naddr++; |
|
177 |
1008 |
cmd->a1 = xmalloc(sizeof(struct s_addr)); |
|
178 |
1008 |
p = compile_addr(p, cmd->a1); |
|
179 |
✓✗✓✗ ✓✓ |
2062 |
EATSPACE(); /* EXTENSION */ |
180 |
✓✓ | 1008 |
if (*p == ',') { |
181 |
1 |
p++; |
|
182 |
✓✗✓✗ ✗✓ |
2 |
EATSPACE(); /* EXTENSION */ |
183 |
1 |
naddr++; |
|
184 |
1 |
cmd->a2 = xmalloc(sizeof(struct s_addr)); |
|
185 |
1 |
p = compile_addr(p, cmd->a2); |
|
186 |
✓✗✓✗ ✗✓ |
2 |
EATSPACE(); |
187 |
} else { |
||
188 |
1007 |
cmd->a2 = 0; |
|
189 |
} |
||
190 |
} else { |
||
191 |
13523 |
cmd->a1 = cmd->a2 = 0; |
|
192 |
} |
||
193 |
|||
194 |
14711 |
nonsel: /* Now parse the command */ |
|
195 |
✗✓ | 14711 |
if (!*p) |
196 |
error(COMPILE, "command expected"); |
||
197 |
14711 |
cmd->code = *p; |
|
198 |
✓✗ | 267627 |
for (fp = cmd_fmts; fp->code; fp++) |
199 |
✓✓ | 267627 |
if (fp->code == *p) |
200 |
14711 |
break; |
|
201 |
✗✓ | 14711 |
if (!fp->code) |
202 |
error(COMPILE, "invalid command code %c", *p); |
||
203 |
✗✓ | 14711 |
if (naddr > fp->naddr) |
204 |
error(COMPILE, |
||
205 |
"command %c expects up to %d address(es), found %d", |
||
206 |
*p, fp->naddr, naddr); |
||
207 |
✗✓✓✓ ✓✓✗✓ ✓✓✓✓ |
14711 |
switch (fp->args) { |
208 |
case NONSEL: /* ! */ |
||
209 |
180 |
p++; |
|
210 |
✓✗✓✗ ✗✓ |
360 |
EATSPACE(); |
211 |
180 |
cmd->nonsel = 1; |
|
212 |
180 |
goto nonsel; |
|
213 |
case GROUP: /* { */ |
||
214 |
498 |
p++; |
|
215 |
✓✗✓✗ ✓✓ |
1494 |
EATSPACE(); |
216 |
498 |
cmd->next = stack; |
|
217 |
498 |
stack = cmd; |
|
218 |
498 |
link = &cmd->u.c; |
|
219 |
✓✗ | 498 |
if (*p) |
220 |
goto semicolon; |
||
221 |
break; |
||
222 |
case ENDGROUP: |
||
223 |
/* |
||
224 |
* Short-circuit command processing, since end of |
||
225 |
* group is really just a noop. |
||
226 |
*/ |
||
227 |
498 |
cmd->nonsel = 1; |
|
228 |
✗✓ | 498 |
if (stack == 0) |
229 |
error(COMPILE, "unexpected }"); |
||
230 |
498 |
cmd2 = stack; |
|
231 |
498 |
stack = cmd2->next; |
|
232 |
498 |
cmd2->next = cmd; |
|
233 |
/*FALLTHROUGH*/ |
||
234 |
case EMPTY: /* d D g G h H l n N p P q x = \0 */ |
||
235 |
1348 |
p++; |
|
236 |
✓✗✓✗ ✓✓ |
3945 |
EATSPACE(); |
237 |
✓✓ | 1348 |
if (*p == ';') { |
238 |
11 |
p++; |
|
239 |
11 |
link = &cmd->next; |
|
240 |
11 |
goto semicolon; |
|
241 |
} |
||
242 |
✓✗ | 1337 |
if (*p) |
243 |
error(COMPILE, |
||
244 |
"extra characters at the end of %c command", cmd->code); |
||
245 |
break; |
||
246 |
case TEXT: /* a c i */ |
||
247 |
8 |
p++; |
|
248 |
✓✗✓✗ ✗✓ |
16 |
EATSPACE(); |
249 |
✗✓ | 8 |
if (*p != '\\') |
250 |
error(COMPILE, "command %c expects \\ followed by" |
||
251 |
" text", cmd->code); |
||
252 |
8 |
p++; |
|
253 |
✓✗✓✗ ✓✓ |
24 |
EATSPACE(); |
254 |
✗✓ | 8 |
if (*p) |
255 |
error(COMPILE, "extra characters after \\ at the" |
||
256 |
" end of %c command", cmd->code); |
||
257 |
8 |
cmd->t = compile_text(); |
|
258 |
8 |
break; |
|
259 |
case COMMENT: /* \0 # */ |
||
260 |
break; |
||
261 |
case WFILE: /* w */ |
||
262 |
p++; |
||
263 |
EATSPACE(); |
||
264 |
if (*p == '\0') |
||
265 |
error(COMPILE, "filename expected"); |
||
266 |
cmd->t = duptoeol(p, "w command", NULL); |
||
267 |
if (aflag) |
||
268 |
cmd->u.fd = -1; |
||
269 |
else if ((cmd->u.fd = open(p, |
||
270 |
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, |
||
271 |
DEFFILEMODE)) == -1) |
||
272 |
error(FATAL, "%s: %s", p, strerror(errno)); |
||
273 |
break; |
||
274 |
case RFILE: /* r */ |
||
275 |
405 |
p++; |
|
276 |
✓✗✓✗ ✓✓ |
1215 |
EATSPACE(); |
277 |
405 |
cmd->t = duptoeol(p, "read command", NULL); |
|
278 |
405 |
break; |
|
279 |
case BRANCH: /* b t */ |
||
280 |
677 |
p++; |
|
281 |
✓✗✓✗ ✓✓ |
2031 |
EATSPACE(); |
282 |
✓✓ | 677 |
if (*p == '\0') |
283 |
140 |
cmd->t = NULL; |
|
284 |
else |
||
285 |
537 |
cmd->t = duptoeol(p, "branch", &p); |
|
286 |
✓✗ | 677 |
if (*p == ';') { |
287 |
p++; |
||
288 |
goto semicolon; |
||
289 |
} |
||
290 |
break; |
||
291 |
case LABEL: /* : */ |
||
292 |
112 |
p++; |
|
293 |
✓✗✓✗ ✗✓ |
224 |
EATSPACE(); |
294 |
112 |
cmd->t = duptoeol(p, "label", &p); |
|
295 |
✗✓ | 112 |
if (strlen(cmd->t) == 0) |
296 |
error(COMPILE, "empty label"); |
||
297 |
112 |
enterlabel(cmd); |
|
298 |
✓✗ | 112 |
if (*p == ';') { |
299 |
p++; |
||
300 |
goto semicolon; |
||
301 |
} |
||
302 |
break; |
||
303 |
case SUBST: /* s */ |
||
304 |
11293 |
p++; |
|
305 |
✗✓ | 11293 |
if (*p == '\0' || *p == '\\') |
306 |
error(COMPILE, "substitute pattern can not be" |
||
307 |
" delimited by newline or backslash"); |
||
308 |
11293 |
cmd->u.s = xmalloc(sizeof(struct s_subst)); |
|
309 |
11293 |
p = compile_re(p, &cmd->u.s->re); |
|
310 |
✗✓ | 11293 |
if (p == NULL) |
311 |
error(COMPILE, "unterminated substitute pattern"); |
||
312 |
11293 |
--p; |
|
313 |
11293 |
p = compile_subst(p, cmd->u.s); |
|
314 |
11293 |
p = compile_flags(p, cmd->u.s); |
|
315 |
✓✗✓✗ ✗✓ |
22586 |
EATSPACE(); |
316 |
✓✓ | 11293 |
if (*p == ';') { |
317 |
596 |
p++; |
|
318 |
596 |
link = &cmd->next; |
|
319 |
596 |
goto semicolon; |
|
320 |
} |
||
321 |
break; |
||
322 |
case TR: /* y */ |
||
323 |
190 |
p++; |
|
324 |
190 |
p = compile_tr(p, (char **)&cmd->u.y); |
|
325 |
✓✗✓✗ ✗✓ |
380 |
EATSPACE(); |
326 |
✓✗ | 190 |
if (*p == ';') { |
327 |
190 |
p++; |
|
328 |
190 |
link = &cmd->next; |
|
329 |
190 |
goto semicolon; |
|
330 |
} |
||
331 |
if (*p) |
||
332 |
error(COMPILE, "extra text at the end of a" |
||
333 |
" transform command"); |
||
334 |
break; |
||
335 |
} |
||
336 |
} |
||
337 |
} |
||
338 |
|||
339 |
/* |
||
340 |
* Get a delimited string. P points to the delimeter of the string; d points |
||
341 |
* to a buffer area. Newline and delimiter escapes are processed; other |
||
342 |
* escapes are ignored. |
||
343 |
* |
||
344 |
* Returns a pointer to the first character after the final delimiter or NULL |
||
345 |
* in the case of a non-terminated string. The character array d is filled |
||
346 |
* with the processed string. |
||
347 |
*/ |
||
348 |
static char * |
||
349 |
compile_delimited(char *p, char *d, int is_tr) |
||
350 |
12637 |
{ |
|
351 |
char c; |
||
352 |
|||
353 |
12637 |
c = *p++; |
|
354 |
✗✓ | 12637 |
if (c == '\0') |
355 |
return (NULL); |
||
356 |
✗✓ | 12637 |
else if (c == '\\') |
357 |
error(COMPILE, "\\ can not be used as a string delimiter"); |
||
358 |
✗✓ | 12637 |
else if (c == '\n') |
359 |
error(COMPILE, "newline can not be used as a string delimiter"); |
||
360 |
✓✗ | 214448 |
while (*p) { |
361 |
✓✓✓✗ |
214448 |
if (*p == '[' && *p != c) { |
362 |
✗✓ | 2122 |
if ((d = compile_ccl(&p, d)) == NULL) |
363 |
error(COMPILE, "unbalanced brackets ([])"); |
||
364 |
continue; |
||
365 |
✓✓✗✓ |
212326 |
} else if (*p == '\\' && p[1] == '[') { |
366 |
*d++ = *p++; |
||
367 |
✓✓✗✓ |
212326 |
} else if (*p == '\\' && p[1] == c) { |
368 |
p++; |
||
369 |
✓✓✓✓ |
212326 |
} else if (*p == '\\' && p[1] == 'n') { |
370 |
5 |
*d++ = '\n'; |
|
371 |
5 |
p += 2; |
|
372 |
5 |
continue; |
|
373 |
✓✓✓✓ |
212321 |
} else if (*p == '\\' && p[1] == '\\') { |
374 |
✗✓ | 3 |
if (is_tr) |
375 |
p++; |
||
376 |
else |
||
377 |
3 |
*d++ = *p++; |
|
378 |
✓✓ | 212318 |
} else if (*p == c) { |
379 |
12637 |
*d = '\0'; |
|
380 |
12637 |
return (p + 1); |
|
381 |
} |
||
382 |
199684 |
*d++ = *p++; |
|
383 |
} |
||
384 |
return (NULL); |
||
385 |
} |
||
386 |
|||
387 |
|||
388 |
/* compile_ccl: expand a POSIX character class */ |
||
389 |
static char * |
||
390 |
compile_ccl(char **sp, char *t) |
||
391 |
2122 |
{ |
|
392 |
int c, d; |
||
393 |
2122 |
char *s = *sp; |
|
394 |
|||
395 |
2122 |
*t++ = *s++; |
|
396 |
✓✓ | 2122 |
if (*s == '^') |
397 |
485 |
*t++ = *s++; |
|
398 |
✗✓ | 2122 |
if (*s == ']') |
399 |
*t++ = *s++; |
||
400 |
✓✗✓✓ |
19560 |
for (; *s && (*t = *s) != ']'; s++, t++) |
401 |
✗✓✗✗ ✗✗ |
19560 |
if (*s == '[' && ((d = *(s+1)) == '.' || d == ':' || d == '=')) { |
402 |
*++t = *++s, t++, s++; |
||
403 |
for (c = *s; (*t = *s) != ']' || c != d; s++, t++) |
||
404 |
if ((c = *s) == '\0') |
||
405 |
return NULL; |
||
406 |
✓✓✗✓ |
19560 |
} else if (*s == '\\' && s[1] == 'n') { |
407 |
*t = '\n'; |
||
408 |
s++; |
||
409 |
} |
||
410 |
✓✗ | 2122 |
if (*s == ']') { |
411 |
2122 |
*sp = ++s; |
|
412 |
2122 |
return (++t); |
|
413 |
} else { |
||
414 |
return (NULL); |
||
415 |
} |
||
416 |
} |
||
417 |
|||
418 |
/* |
||
419 |
* Get a regular expression. P points to the delimiter of the regular |
||
420 |
* expression; repp points to the address of a regexp pointer. Newline |
||
421 |
* and delimiter escapes are processed; other escapes are ignored. |
||
422 |
* Returns a pointer to the first character after the final delimiter |
||
423 |
* or NULL in the case of a non terminated regular expression. The regexp |
||
424 |
* pointer is set to the compiled regular expression. |
||
425 |
* Cflags are passed to regcomp. |
||
426 |
*/ |
||
427 |
static char * |
||
428 |
compile_re(char *p, regex_t **repp) |
||
429 |
12257 |
{ |
|
430 |
int eval; |
||
431 |
char *re; |
||
432 |
|||
433 |
12257 |
re = xmalloc(strlen(p) + 1); /* strlen(re) <= strlen(p) */ |
|
434 |
12257 |
p = compile_delimited(p, re, 0); |
|
435 |
✓✗✓✓ |
12257 |
if (p && strlen(re) == 0) { |
436 |
1 |
*repp = NULL; |
|
437 |
1 |
free(re); |
|
438 |
1 |
return (p); |
|
439 |
} |
||
440 |
12256 |
*repp = xmalloc(sizeof(regex_t)); |
|
441 |
✓✗✗✓ |
12256 |
if (p && (eval = regcomp(*repp, re, Eflag ? REG_EXTENDED : 0)) != 0) |
442 |
error(COMPILE, "RE error: %s", strregerror(eval, *repp)); |
||
443 |
✓✓ | 12256 |
if (maxnsub < (*repp)->re_nsub) |
444 |
62 |
maxnsub = (*repp)->re_nsub; |
|
445 |
12256 |
free(re); |
|
446 |
12256 |
return (p); |
|
447 |
} |
||
448 |
|||
449 |
/* |
||
450 |
* Compile the substitution string of a regular expression and set res to |
||
451 |
* point to a saved copy of it. Nsub is the number of parenthesized regular |
||
452 |
* expressions. |
||
453 |
*/ |
||
454 |
static char * |
||
455 |
compile_subst(char *p, struct s_subst *s) |
||
456 |
11293 |
{ |
|
457 |
static char *lbuf; |
||
458 |
static size_t bufsize; |
||
459 |
int asize, ref, size; |
||
460 |
char c, *text, *op, *sp; |
||
461 |
11293 |
int sawesc = 0; |
|
462 |
|||
463 |
11293 |
c = *p++; /* Terminator character */ |
|
464 |
✗✓ | 11293 |
if (c == '\0') |
465 |
return (NULL); |
||
466 |
|||
467 |
11293 |
s->maxbref = 0; |
|
468 |
11293 |
s->linenum = linenum; |
|
469 |
11293 |
text = NULL; |
|
470 |
11293 |
asize = size = 0; |
|
471 |
do { |
||
472 |
11296 |
size_t len = ROUNDLEN(strlen(p) + 1); |
|
473 |
✓✗ | 11296 |
if (asize - size < len) { |
474 |
do { |
||
475 |
11296 |
asize += len; |
|
476 |
✗✓ | 11296 |
} while (asize - size < len); |
477 |
11296 |
text = xrealloc(text, asize); |
|
478 |
} |
||
479 |
11296 |
op = sp = text + size; |
|
480 |
✓✓ | 263180 |
for (; *p; p++) { |
481 |
✓✓ | 263177 |
if (*p == '\\' || sawesc) { |
482 |
/* |
||
483 |
* If this is a continuation from the last |
||
484 |
* buffer, we won't have a character to |
||
485 |
* skip over. |
||
486 |
*/ |
||
487 |
✗✓ | 366 |
if (sawesc) |
488 |
sawesc = 0; |
||
489 |
else |
||
490 |
366 |
p++; |
|
491 |
|||
492 |
✗✓ | 366 |
if (*p == '\0') { |
493 |
/* |
||
494 |
* This escaped character is continued |
||
495 |
* in the next part of the line. Note |
||
496 |
* this fact, then cause the loop to |
||
497 |
* exit w/ normal EOL case and reenter |
||
498 |
* above with the new buffer. |
||
499 |
*/ |
||
500 |
sawesc = 1; |
||
501 |
p--; |
||
502 |
continue; |
||
503 |
✓✓ | 366 |
} else if (strchr("123456789", *p) != NULL) { |
504 |
177 |
*sp++ = '\\'; |
|
505 |
177 |
ref = *p - '0'; |
|
506 |
✓✗✗✓ |
177 |
if (s->re != NULL && |
507 |
ref > s->re->re_nsub) |
||
508 |
error(COMPILE, |
||
509 |
"\\%c not defined in the RE", *p); |
||
510 |
✓✓ | 177 |
if (s->maxbref < ref) |
511 |
173 |
s->maxbref = ref; |
|
512 |
✓✓ | 189 |
} else if (*p == '&' || *p == '\\') |
513 |
51 |
*sp++ = '\\'; |
|
514 |
✓✓ | 262811 |
} else if (*p == c) { |
515 |
11293 |
p++; |
|
516 |
11293 |
*sp++ = '\0'; |
|
517 |
11293 |
size += sp - op; |
|
518 |
11293 |
s->new = xrealloc(text, size); |
|
519 |
11293 |
return (p); |
|
520 |
✗✓ | 251518 |
} else if (*p == '\n') { |
521 |
error(COMPILE, |
||
522 |
"unescaped newline inside substitute pattern"); |
||
523 |
/* NOTREACHED */ |
||
524 |
} |
||
525 |
251884 |
*sp++ = *p; |
|
526 |
} |
||
527 |
3 |
size += sp - op; |
|
528 |
✓✗ | 3 |
} while ((p = cu_fgets(&lbuf, &bufsize))); |
529 |
error(COMPILE, "unterminated substitute in regular expression"); |
||
530 |
/* NOTREACHED */ |
||
531 |
} |
||
532 |
|||
533 |
/* |
||
534 |
* Compile the flags of the s command |
||
535 |
*/ |
||
536 |
static char * |
||
537 |
compile_flags(char *p, struct s_subst *s) |
||
538 |
11293 |
{ |
|
539 |
int gn; /* True if we have seen g or n */ |
||
540 |
long l; |
||
541 |
char wfile[PATH_MAX], *q, *eq; |
||
542 |
|||
543 |
11293 |
s->n = 1; /* Default */ |
|
544 |
11293 |
s->p = 0; |
|
545 |
11293 |
s->wfile = NULL; |
|
546 |
11293 |
s->wfd = -1; |
|
547 |
11293 |
for (gn = 0;;) { |
|
548 |
✓✗✓✗ ✓✓ |
53176 |
EATSPACE(); /* EXTENSION */ |
549 |
✓✓✓✗ ✗✗ |
21522 |
switch (*p) { |
550 |
case 'g': |
||
551 |
✗✓ | 10210 |
if (gn) |
552 |
error(COMPILE, "more than one number or 'g' in" |
||
553 |
" substitute flags"); |
||
554 |
10210 |
gn = 1; |
|
555 |
10210 |
s->n = 0; |
|
556 |
10210 |
break; |
|
557 |
case '\0': |
||
558 |
case '\n': |
||
559 |
case ';': |
||
560 |
11293 |
return (p); |
|
561 |
case 'p': |
||
562 |
19 |
s->p = 1; |
|
563 |
19 |
break; |
|
564 |
case '1': case '2': case '3': |
||
565 |
case '4': case '5': case '6': |
||
566 |
case '7': case '8': case '9': |
||
567 |
if (gn) |
||
568 |
error(COMPILE, "more than one number or 'g' in" |
||
569 |
" substitute flags"); |
||
570 |
gn = 1; |
||
571 |
l = strtol(p, &p, 10); |
||
572 |
if (l <= 0 || l >= INT_MAX) |
||
573 |
error(COMPILE, |
||
574 |
"number in substitute flags out of range"); |
||
575 |
s->n = (int)l; |
||
576 |
continue; |
||
577 |
case 'w': |
||
578 |
p++; |
||
579 |
#ifdef HISTORIC_PRACTICE |
||
580 |
if (*p != ' ') { |
||
581 |
error(WARNING, "space missing before w wfile"); |
||
582 |
return (p); |
||
583 |
} |
||
584 |
#endif |
||
585 |
EATSPACE(); |
||
586 |
q = wfile; |
||
587 |
eq = wfile + sizeof(wfile) - 1; |
||
588 |
while (*p) { |
||
589 |
if (*p == '\n') |
||
590 |
break; |
||
591 |
if (q >= eq) |
||
592 |
error(COMPILE, "wfile too long"); |
||
593 |
*q++ = *p++; |
||
594 |
} |
||
595 |
*q = '\0'; |
||
596 |
if (q == wfile) |
||
597 |
error(COMPILE, "no wfile specified"); |
||
598 |
s->wfile = strdup(wfile); |
||
599 |
if (!aflag && (s->wfd = open(wfile, |
||
600 |
O_WRONLY|O_APPEND|O_CREAT|O_TRUNC, |
||
601 |
DEFFILEMODE)) == -1) |
||
602 |
error(FATAL, "%s: %s", wfile, strerror(errno)); |
||
603 |
return (p); |
||
604 |
default: |
||
605 |
error(COMPILE, |
||
606 |
"bad flag in substitute command: '%c'", *p); |
||
607 |
break; |
||
608 |
} |
||
609 |
10229 |
p++; |
|
610 |
} |
||
611 |
} |
||
612 |
|||
613 |
/* |
||
614 |
* Compile a translation set of strings into a lookup table. |
||
615 |
*/ |
||
616 |
static char * |
||
617 |
compile_tr(char *p, char **transtab) |
||
618 |
190 |
{ |
|
619 |
int i; |
||
620 |
char *lt, *op, *np; |
||
621 |
190 |
char *old = NULL, *new = NULL; |
|
622 |
|||
623 |
✗✓ | 190 |
if (*p == '\0' || *p == '\\') |
624 |
error(COMPILE, |
||
625 |
"transform pattern can not be delimited by newline or backslash"); |
||
626 |
190 |
old = xmalloc(strlen(p) + 1); |
|
627 |
190 |
p = compile_delimited(p, old, 1); |
|
628 |
✗✓ | 190 |
if (p == NULL) { |
629 |
error(COMPILE, "unterminated transform source string"); |
||
630 |
goto bad; |
||
631 |
} |
||
632 |
190 |
new = xmalloc(strlen(p) + 1); |
|
633 |
190 |
p = compile_delimited(--p, new, 1); |
|
634 |
✗✓ | 190 |
if (p == NULL) { |
635 |
error(COMPILE, "unterminated transform target string"); |
||
636 |
goto bad; |
||
637 |
} |
||
638 |
✓✗✓✗ ✗✓ |
380 |
EATSPACE(); |
639 |
✗✓ | 190 |
if (strlen(new) != strlen(old)) { |
640 |
error(COMPILE, "transform strings are not the same length"); |
||
641 |
goto bad; |
||
642 |
} |
||
643 |
/* We assume characters are 8 bits */ |
||
644 |
190 |
lt = xmalloc(UCHAR_MAX + 1); |
|
645 |
✓✓ | 48830 |
for (i = 0; i <= UCHAR_MAX; i++) |
646 |
48640 |
lt[i] = (char)i; |
|
647 |
✓✓ | 2845 |
for (op = old, np = new; *op; op++, np++) |
648 |
2655 |
lt[(u_char)*op] = *np; |
|
649 |
190 |
*transtab = lt; |
|
650 |
190 |
free(old); |
|
651 |
190 |
free(new); |
|
652 |
190 |
return (p); |
|
653 |
bad: |
||
654 |
free(old); |
||
655 |
free(new); |
||
656 |
return (NULL); |
||
657 |
} |
||
658 |
|||
659 |
/* |
||
660 |
* Compile the text following an a, c, or i command. |
||
661 |
*/ |
||
662 |
static char * |
||
663 |
compile_text(void) |
||
664 |
8 |
{ |
|
665 |
int asize, esc_nl, size; |
||
666 |
char *lbuf, *text, *p, *op, *s; |
||
667 |
size_t bufsize; |
||
668 |
|||
669 |
8 |
lbuf = text = NULL; |
|
670 |
8 |
asize = size = 0; |
|
671 |
✓✗ | 16 |
while ((p = cu_fgets(&lbuf, &bufsize))) { |
672 |
8 |
size_t len = ROUNDLEN(strlen(p) + 1); |
|
673 |
✓✗ | 8 |
if (asize - size < len) { |
674 |
do { |
||
675 |
8 |
asize += len; |
|
676 |
✗✓ | 8 |
} while (asize - size < len); |
677 |
8 |
text = xrealloc(text, asize); |
|
678 |
} |
||
679 |
8 |
op = s = text + size; |
|
680 |
✓✓ | 312 |
for (esc_nl = 0; *p != '\0'; p++) { |
681 |
✗✓✗✗ ✗✗ |
304 |
if (*p == '\\' && p[1] != '\0' && *++p == '\n') |
682 |
esc_nl = 1; |
||
683 |
304 |
*s++ = *p; |
|
684 |
} |
||
685 |
8 |
size += s - op; |
|
686 |
✓✗ | 8 |
if (!esc_nl) { |
687 |
8 |
*s = '\0'; |
|
688 |
8 |
break; |
|
689 |
} |
||
690 |
} |
||
691 |
8 |
free(lbuf); |
|
692 |
8 |
text = xrealloc(text, size + 1); |
|
693 |
8 |
text[size] = '\0'; |
|
694 |
8 |
return (text); |
|
695 |
} |
||
696 |
|||
697 |
/* |
||
698 |
* Get an address and return a pointer to the first character after |
||
699 |
* it. Fill the structure pointed to according to the address. |
||
700 |
*/ |
||
701 |
static char * |
||
702 |
compile_addr(char *p, struct s_addr *a) |
||
703 |
1009 |
{ |
|
704 |
char *end; |
||
705 |
|||
706 |
✗✓✗✓ ✗ |
1009 |
switch (*p) { |
707 |
case '\\': /* Context address */ |
||
708 |
++p; |
||
709 |
/* FALLTHROUGH */ |
||
710 |
case '/': /* Context address */ |
||
711 |
964 |
p = compile_re(p, &a->u.r); |
|
712 |
✗✓ | 964 |
if (p == NULL) |
713 |
error(COMPILE, "unterminated regular expression"); |
||
714 |
964 |
a->type = AT_RE; |
|
715 |
964 |
return (p); |
|
716 |
|||
717 |
case '$': /* Last line */ |
||
718 |
a->type = AT_LAST; |
||
719 |
return (p + 1); |
||
720 |
/* Line number */ |
||
721 |
case '0': case '1': case '2': case '3': case '4': |
||
722 |
case '5': case '6': case '7': case '8': case '9': |
||
723 |
45 |
a->type = AT_LINE; |
|
724 |
45 |
a->u.l = strtoul(p, &end, 10); |
|
725 |
45 |
return (end); |
|
726 |
default: |
||
727 |
error(COMPILE, "expected context address"); |
||
728 |
return (NULL); |
||
729 |
} |
||
730 |
} |
||
731 |
|||
732 |
/* |
||
733 |
* duptoeol -- |
||
734 |
* Return a copy of all the characters up to \n or \0. |
||
735 |
*/ |
||
736 |
static char * |
||
737 |
duptoeol(char *s, char *ctype, char **semi) |
||
738 |
1054 |
{ |
|
739 |
size_t len; |
||
740 |
int ws; |
||
741 |
char *start; |
||
742 |
|||
743 |
1054 |
ws = 0; |
|
744 |
✓✓ | 1054 |
if (semi) { |
745 |
✓✓✓✗ |
1603 |
for (start = s; *s != '\0' && *s != '\n' && *s != ';'; ++s) |
746 |
954 |
ws = isspace((unsigned char)*s); |
|
747 |
} else { |
||
748 |
✓✓ | 7830 |
for (start = s; *s != '\0' && *s != '\n'; ++s) |
749 |
7425 |
ws = isspace((unsigned char)*s); |
|
750 |
405 |
*s = '\0'; |
|
751 |
} |
||
752 |
✗✓ | 1054 |
if (ws) |
753 |
error(WARNING, "whitespace after %s", ctype); |
||
754 |
1054 |
len = s - start + 1; |
|
755 |
✓✓ | 1054 |
if (semi) |
756 |
649 |
*semi = s; |
|
757 |
1054 |
s = xmalloc(len); |
|
758 |
1054 |
strlcpy(s, start, len); |
|
759 |
1054 |
return (s); |
|
760 |
} |
||
761 |
|||
762 |
/* |
||
763 |
* Convert goto label names to addresses, and count a and r commands, in |
||
764 |
* the given subset of the script. Free the memory used by labels in b |
||
765 |
* and t commands (but not by :). |
||
766 |
* |
||
767 |
* TODO: Remove } nodes |
||
768 |
*/ |
||
769 |
static void |
||
770 |
fixuplabel(struct s_command *cp, struct s_command *end) |
||
771 |
1279 |
{ |
|
772 |
|||
773 |
✓✓ | 15810 |
for (; cp != end; cp = cp->next) |
774 |
✓✓✓✓ |
14531 |
switch (cp->code) { |
775 |
case 'a': |
||
776 |
case 'r': |
||
777 |
413 |
appendnum++; |
|
778 |
413 |
break; |
|
779 |
case 'b': |
||
780 |
case 't': |
||
781 |
/* Resolve branch target. */ |
||
782 |
✓✓ | 677 |
if (cp->t == NULL) { |
783 |
140 |
cp->u.c = NULL; |
|
784 |
140 |
break; |
|
785 |
} |
||
786 |
✗✓ | 537 |
if ((cp->u.c = findlabel(cp->t)) == NULL) |
787 |
error(COMPILE, "undefined label '%s'", cp->t); |
||
788 |
537 |
free(cp->t); |
|
789 |
537 |
break; |
|
790 |
case '{': |
||
791 |
/* Do interior commands. */ |
||
792 |
498 |
fixuplabel(cp->u.c, cp->next); |
|
793 |
break; |
||
794 |
} |
||
795 |
1279 |
} |
|
796 |
|||
797 |
/* |
||
798 |
* Associate the given command label for later lookup. |
||
799 |
*/ |
||
800 |
static void |
||
801 |
enterlabel(struct s_command *cp) |
||
802 |
112 |
{ |
|
803 |
struct labhash **lhp, *lh; |
||
804 |
u_char *p; |
||
805 |
u_int h, c; |
||
806 |
|||
807 |
✓✓ | 367 |
for (h = 0, p = (u_char *)cp->t; (c = *p) != 0; p++) |
808 |
255 |
h = (h << 5) + h + c; |
|
809 |
112 |
lhp = &labels[h & LHMASK]; |
|
810 |
✗✓ | 112 |
for (lh = *lhp; lh != NULL; lh = lh->lh_next) |
811 |
if (lh->lh_hash == h && strcmp(cp->t, lh->lh_cmd->t) == 0) |
||
812 |
error(COMPILE, "duplicate label '%s'", cp->t); |
||
813 |
112 |
lh = xmalloc(sizeof *lh); |
|
814 |
112 |
lh->lh_next = *lhp; |
|
815 |
112 |
lh->lh_hash = h; |
|
816 |
112 |
lh->lh_cmd = cp; |
|
817 |
112 |
lh->lh_ref = 0; |
|
818 |
112 |
*lhp = lh; |
|
819 |
112 |
} |
|
820 |
|||
821 |
/* |
||
822 |
* Find the label contained in the command l in the command linked |
||
823 |
* list cp. L is excluded from the search. Return NULL if not found. |
||
824 |
*/ |
||
825 |
static struct s_command * |
||
826 |
findlabel(char *name) |
||
827 |
537 |
{ |
|
828 |
struct labhash *lh; |
||
829 |
u_char *p; |
||
830 |
u_int h, c; |
||
831 |
|||
832 |
✓✓ | 1236 |
for (h = 0, p = (u_char *)name; (c = *p) != 0; p++) |
833 |
699 |
h = (h << 5) + h + c; |
|
834 |
✓✗ | 537 |
for (lh = labels[h & LHMASK]; lh != NULL; lh = lh->lh_next) { |
835 |
✓✗✓✗ |
537 |
if (lh->lh_hash == h && strcmp(name, lh->lh_cmd->t) == 0) { |
836 |
537 |
lh->lh_ref = 1; |
|
837 |
537 |
return (lh->lh_cmd); |
|
838 |
} |
||
839 |
} |
||
840 |
return (NULL); |
||
841 |
} |
||
842 |
|||
843 |
/* |
||
844 |
* Warn about any unused labels. As a side effect, release the label hash |
||
845 |
* table space. |
||
846 |
*/ |
||
847 |
static void |
||
848 |
uselabel(void) |
||
849 |
781 |
{ |
|
850 |
struct labhash *lh, *next; |
||
851 |
int i; |
||
852 |
|||
853 |
✓✓ | 100749 |
for (i = 0; i < LHSZ; i++) { |
854 |
✓✓ | 100080 |
for (lh = labels[i]; lh != NULL; lh = next) { |
855 |
112 |
next = lh->lh_next; |
|
856 |
✗✓ | 112 |
if (!lh->lh_ref) |
857 |
error(WARNING, "unused label '%s'", |
||
858 |
lh->lh_cmd->t); |
||
859 |
112 |
free(lh); |
|
860 |
} |
||
861 |
} |
||
862 |
781 |
} |
Generated by: GCOVR (Version 3.3) |