Merge branch 'vendor/OPENSSL' into HEAD
[dragonfly.git] / usr.bin / m4 / gnum4.c
1 /* $OpenBSD: gnum4.c,v 1.18 2002/04/26 16:15:16 espie Exp $ */
2
3 /*
4  * Copyright (c) 1999 Marc Espie
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
16  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25  * SUCH DAMAGE.
26  *
27  * $FreeBSD: src/usr.bin/m4/gnum4.c,v 1.9 2004/05/18 15:53:58 stefanf Exp $
28  * $DragonFly: src/usr.bin/m4/gnum4.c,v 1.3 2006/12/27 21:29:02 pavalos Exp $
29  */
30
31 /* 
32  * functions needed to support gnu-m4 extensions, including a fake freezing
33  */
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/wait.h>
38 #include <ctype.h>
39 #include <paths.h>
40 #include <regex.h>
41 #include <stddef.h>
42 #include <stdlib.h>
43 #include <stdio.h>
44 #include <string.h>
45 #include <err.h>
46 #include <errno.h>
47 #include <unistd.h>
48 #include "mdef.h"
49 #include "stdd.h"
50 #include "extern.h"
51
52
53 int mimic_gnu = 0;
54
55 /*
56  * Support for include path search
57  * First search in the the current directory.
58  * If not found, and the path is not absolute, include path kicks in.
59  * First, -I options, in the order found on the command line.
60  * Then M4PATH env variable
61  */
62
63 struct path_entry {
64         char *name;
65         struct path_entry *next;
66 } *first, *last;
67
68 static struct path_entry *new_path_entry(const char *);
69 static void ensure_m4path(void);
70 static struct input_file *dopath(struct input_file *, const char *);
71
72 static struct path_entry *
73 new_path_entry(const char *dirname)
74 {
75         struct path_entry *n;
76
77         n = malloc(sizeof(struct path_entry));
78         if (!n)
79                 errx(1, "out of memory");
80         n->name = strdup(dirname);
81         if (!n->name)
82                 errx(1, "out of memory");
83         n->next = 0;
84         return n;
85 }
86
87 void
88 addtoincludepath(const char *dirname)
89 {
90         struct path_entry *n;
91
92         n = new_path_entry(dirname);
93
94         if (last) {
95                 last->next = n;
96                 last = n;
97         }
98         else
99                 last = first = n;
100 }
101
102 static void
103 ensure_m4path(void)
104 {
105         static int envpathdone = 0;
106         char *envpath;
107         char *sweep;
108         char *path;
109
110         if (envpathdone)
111                 return;
112         envpathdone = TRUE;
113         envpath = getenv("M4PATH");
114         if (!envpath)
115                 return;
116         /* for portability: getenv result is read-only */
117         envpath = strdup(envpath);
118         if (!envpath)
119                 errx(1, "out of memory");
120         for (sweep = envpath;
121             (path = strsep(&sweep, ":")) != NULL;)
122             addtoincludepath(path);
123         free(envpath);
124 }
125
126 static
127 struct input_file *
128 dopath(struct input_file *i, const char *filename)
129 {
130         char path[MAXPATHLEN];
131         struct path_entry *pe;
132         FILE *f;
133
134         for (pe = first; pe; pe = pe->next) {
135                 snprintf(path, sizeof(path), "%s/%s", pe->name, filename);
136                 if ((f = fopen(path, "r")) != 0) {
137                         set_input(i, f, path);
138                         return i;
139                 }
140         }
141         return NULL;
142 }
143
144 struct input_file *
145 fopen_trypath(struct input_file *i, const char *filename)
146 {
147         FILE *f;
148
149         f = fopen(filename, "r");
150         if (f != NULL) {
151                 set_input(i, f, filename);
152                 return i;
153         }
154         if (filename[0] == '/')
155                 return NULL;
156
157         ensure_m4path();
158
159         return dopath(i, filename);
160 }
161
162 void
163 doindir(const char *argv[], int argc)
164 {
165         ndptr p;
166
167         p = lookup(argv[2]);
168         if (p == NULL)
169                 errx(1, "undefined macro %s", argv[2]);
170         argv[1] = p->defn;
171         eval(argv+1, argc-1, p->type);
172 }
173
174 void
175 dobuiltin(const char *argv[], int argc)
176 {
177         int n;
178         argv[1] = NULL;
179         n = builtin_type(argv[2]);
180         if (n != -1)
181                 eval(argv+1, argc-1, n);
182         else
183                 errx(1, "unknown builtin %s", argv[2]);
184 }
185
186
187 /* We need some temporary buffer space, as pb pushes BACK and substitution
188  * proceeds forward... */
189 static char *buffer;
190 static size_t bufsize = 0;
191 static size_t current = 0;
192
193 static void addchars(const char *, size_t);
194 static void addchar(int);
195 static char *twiddle(const char *);
196 static char *getstring(void);
197 static void exit_regerror(int, regex_t *);
198 static void do_subst(const char *, regex_t *, const char *, regmatch_t *);
199 static void do_regexpindex(const char *, regex_t *, regmatch_t *);
200 static void do_regexp(const char *, regex_t *, const char *, regmatch_t *);
201 static void add_sub(size_t, const char *, regex_t *, regmatch_t *);
202 static void add_replace(const char *, regex_t *, const char *, regmatch_t *);
203 #define addconstantstring(s) addchars((s), sizeof(s)-1)
204
205 static void
206 addchars(const char *c, size_t n)
207 {
208         if (n == 0)
209                 return;
210         while (current + n > bufsize) {
211                 if (bufsize == 0)
212                         bufsize = 1024;
213                 else
214                         bufsize *= 2;
215                 buffer = realloc(buffer, bufsize);
216                 if (buffer == NULL)
217                         errx(1, "out of memory");
218         }
219         memcpy(buffer+current, c, n);
220         current += n;
221 }
222
223 static void
224 addchar(int c)
225 {
226         if (current +1 > bufsize) {
227                 if (bufsize == 0)
228                         bufsize = 1024;
229                 else
230                         bufsize *= 2;
231                 buffer = realloc(buffer, bufsize);
232                 if (buffer == NULL)
233                         errx(1, "out of memory");
234         }
235         buffer[current++] = c;
236 }
237
238 static char *
239 getstring(void)
240 {
241         addchar('\0');
242         current = 0;
243         return buffer;
244 }
245
246
247 static void
248 exit_regerror(int er, regex_t *re)
249 {
250         size_t  errlen;
251         char    *errbuf;
252
253         errlen = regerror(er, re, NULL, 0);
254         errbuf = xalloc(errlen);
255         regerror(er, re, errbuf, errlen);
256         errx(1, "regular expression error: %s", errbuf);
257 }
258
259 static void
260 add_sub(size_t n, const char *string, regex_t *re, regmatch_t *pm)
261 {
262         if (n > re->re_nsub)
263                 warnx("No subexpression %zu", n);
264         /* Subexpressions that did not match are
265          * not an error.  */
266         else if (pm[n].rm_so != -1 &&
267             pm[n].rm_eo != -1) {
268                 addchars(string + pm[n].rm_so,
269                         pm[n].rm_eo - pm[n].rm_so);
270         }
271 }
272
273 /* Add replacement string to the output buffer, recognizing special
274  * constructs and replacing them with substrings of the original string.
275  */
276 static void
277 add_replace(const char *string, regex_t *re, const char *replace, regmatch_t *pm)
278 {
279         const char *p;
280
281         for (p = replace; *p != '\0'; p++) {
282                 if (*p == '&' && !mimic_gnu) {
283                         add_sub(0, string, re, pm);
284                         continue;
285                 }
286                 if (*p == '\\') {
287                         if (p[1] == '\\') {
288                                 addchar(p[1]);
289                                 p++;
290                                 continue;
291                         }
292                         if (p[1] == '&') {
293                                 if (mimic_gnu)
294                                         add_sub(0, string, re, pm);
295                                 else
296                                         addchar(p[1]);
297                                 p++;
298                                 continue;
299                         }
300                         if (isdigit(p[1])) {
301                                 add_sub(*(++p) - '0', string, re, pm);
302                                 continue;
303                         }
304                 }
305                 addchar(*p);
306         }
307 }
308
309 static void
310 do_subst(const char *string, regex_t *re, const char *replace, regmatch_t *pm)
311 {
312         int error;
313         int flags = 0;
314         const char *last_match = NULL;
315
316         while ((error = regexec(re, string, re->re_nsub+1, pm, flags)) == 0) {
317                 if (pm[0].rm_eo != 0) {
318                         if (string[pm[0].rm_eo-1] == '\n')
319                                 flags = 0;
320                         else
321                                 flags = REG_NOTBOL;
322                 }
323
324                 /* NULL length matches are special... We use the `vi-mode'
325                  * rule: don't allow a NULL-match at the last match
326                  * position.
327                  */
328                 if (pm[0].rm_so == pm[0].rm_eo &&
329                     string + pm[0].rm_so == last_match) {
330                         if (*string == '\0')
331                                 return;
332                         addchar(*string);
333                         if (*string++ == '\n')
334                                 flags = 0;
335                         else
336                                 flags = REG_NOTBOL;
337                         continue;
338                 }
339                 last_match = string + pm[0].rm_so;
340                 addchars(string, pm[0].rm_so);
341                 add_replace(string, re, replace, pm);
342                 string += pm[0].rm_eo;
343         }
344         if (error != REG_NOMATCH)
345                 exit_regerror(error, re);
346         pbstr(string);
347 }
348
349 static void
350 do_regexp(const char *string, regex_t *re, const char *replace, regmatch_t *pm)
351 {
352         int error;
353
354         switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
355         case 0:
356                 add_replace(string, re, replace, pm);
357                 pbstr(getstring());
358                 break;
359         case REG_NOMATCH:
360                 break;
361         default:
362                 exit_regerror(error, re);
363         }
364 }
365
366 static void
367 do_regexpindex(const char *string, regex_t *re, regmatch_t *pm)
368 {
369         int error;
370
371         switch(error = regexec(re, string, re->re_nsub+1, pm, 0)) {
372         case 0:
373                 pbunsigned(pm[0].rm_so);
374                 break;
375         case REG_NOMATCH:
376                 pbnum(-1);
377                 break;
378         default:
379                 exit_regerror(error, re);
380         }
381 }
382
383 /* In Gnu m4 mode, parentheses for backmatch don't work like POSIX 1003.2
384  * says. So we twiddle with the regexp before passing it to regcomp.
385  */
386 static char *
387 twiddle(const char *p)
388 {
389         /* This could use strcspn for speed... */
390         while (*p != '\0') {
391                 if (*p == '\\') {
392                         switch(p[1]) {
393                         case '(':
394                         case ')':
395                         case '|':
396                                 addchar(p[1]);
397                                 break;
398                         case 'w':
399                                 addconstantstring("[_a-zA-Z0-9]");
400                                 break;
401                         case 'W':
402                                 addconstantstring("[^_a-zA-Z0-9]");
403                                 break;
404                         case '<':
405                                 addconstantstring("[[:<:]]");
406                                 break;
407                         case '>':
408                                 addconstantstring("[[:>:]]");
409                                 break;
410                         default:
411                                 addchars(p, 2);
412                                 break;
413                         }
414                         p+=2;
415                         continue;
416                 }
417                 if (*p == '(' || *p == ')' || *p == '|')
418                         addchar('\\');
419
420                 addchar(*p);
421                 p++;
422         }
423         return getstring();
424 }
425
426 /* patsubst(string, regexp, opt replacement) */
427 /* argv[2]: string
428  * argv[3]: regexp
429  * argv[4]: opt rep
430  */
431 void
432 dopatsubst(const char *argv[], int argc)
433 {
434         int error;
435         regex_t re;
436         regmatch_t *pmatch;
437
438         if (argc <= 3) {
439                 warnx("Too few arguments to patsubst");
440                 return;
441         }
442         error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
443             REG_NEWLINE | REG_EXTENDED);
444         if (error != 0)
445                 exit_regerror(error, &re);
446
447         pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
448         do_subst(argv[2], &re,
449             argc != 4 && argv[4] != NULL ? argv[4] : "", pmatch);
450         pbstr(getstring());
451         free(pmatch);
452         regfree(&re);
453 }
454
455 void
456 doregexp(const char *argv[], int argc)
457 {
458         int error;
459         regex_t re;
460         regmatch_t *pmatch;
461
462         if (argc <= 3) {
463                 warnx("Too few arguments to regexp");
464                 return;
465         }
466         error = regcomp(&re, mimic_gnu ? twiddle(argv[3]) : argv[3],
467             REG_EXTENDED);
468         if (error != 0)
469                 exit_regerror(error, &re);
470
471         pmatch = xalloc(sizeof(regmatch_t) * (re.re_nsub+1));
472         if (argv[4] == NULL || argc == 4)
473                 do_regexpindex(argv[2], &re, pmatch);
474         else
475                 do_regexp(argv[2], &re, argv[4], pmatch);
476         free(pmatch);
477         regfree(&re);
478 }
479
480 void
481 doesyscmd(const char *cmd)
482 {
483         int p[2];
484         pid_t pid, cpid;
485         int cc;
486         int status;
487
488         /* Follow gnu m4 documentation: first flush buffers. */
489         fflush(NULL);
490
491         /* Just set up standard output, share stderr and stdin with m4 */
492         if (pipe(p) == -1)
493                 err(1, "bad pipe");
494         switch(cpid = fork()) {
495         case -1:
496                 err(1, "bad fork");
497                 /* NOTREACHED */
498         case 0:
499                 (void) close(p[0]);
500                 (void) dup2(p[1], 1);
501                 (void) close(p[1]);
502                 execl(_PATH_BSHELL, "sh", "-c", cmd, NULL);
503                 exit(1);
504         default:
505                 /* Read result in two stages, since m4's buffer is
506                  * pushback-only. */
507                 (void) close(p[1]);
508                 do {
509                         char result[BUFSIZE];
510                         cc = read(p[0], result, sizeof result);
511                         if (cc > 0)
512                                 addchars(result, cc);
513                 } while (cc > 0 || (cc == -1 && errno == EINTR));
514
515                 (void) close(p[0]);
516                 while ((pid = wait(&status)) != cpid && pid >= 0)
517                         continue;
518                 pbstr(getstring());
519         }
520 }