Annotation of src/usr.bin/make/varmodifiers.c, Revision 1.29
1.28 espie 1: /* $OpenBSD: varmodifiers.c,v 1.27 2010/07/19 19:46:44 espie Exp $ */
1.1 espie 2: /* $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $ */
3:
4: /*
1.27 espie 5: * Copyright (c) 1999-2010 Marc Espie.
1.1 espie 6: *
7: * Extensive code changes for the OpenBSD project.
8: *
9: * Redistribution and use in source and binary forms, with or without
10: * modification, are permitted provided that the following conditions
11: * are met:
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: * 2. Redistributions in binary form must reproduce the above copyright
15: * notice, this list of conditions and the following disclaimer in the
16: * documentation and/or other materials provided with the distribution.
17: *
18: * THIS SOFTWARE IS PROVIDED BY THE OPENBSD PROJECT AND CONTRIBUTORS
19: * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OPENBSD
22: * PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29: */
30: /*
31: * Copyright (c) 1988, 1989, 1990, 1993
32: * The Regents of the University of California. All rights reserved.
33: * Copyright (c) 1989 by Berkeley Softworks
34: * All rights reserved.
35: *
36: * This code is derived from software contributed to Berkeley by
37: * Adam de Boor.
38: *
39: * Redistribution and use in source and binary forms, with or without
40: * modification, are permitted provided that the following conditions
41: * are met:
42: * 1. Redistributions of source code must retain the above copyright
43: * notice, this list of conditions and the following disclaimer.
44: * 2. Redistributions in binary form must reproduce the above copyright
45: * notice, this list of conditions and the following disclaimer in the
46: * documentation and/or other materials provided with the distribution.
1.11 millert 47: * 3. Neither the name of the University nor the names of its contributors
1.1 espie 48: * may be used to endorse or promote products derived from this software
49: * without specific prior written permission.
50: *
51: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
52: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
53: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
54: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
55: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
56: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
57: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
58: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
59: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
60: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
61: * SUCH DAMAGE.
62: */
63:
1.7 espie 64: /* VarModifiers_Apply is mostly a constituent function of Var_Parse, it
65: * is also called directly by Var_SubstVar. */
66:
67:
1.9 espie 68: #include <ctype.h>
69: #include <sys/types.h>
1.1 espie 70: #ifndef MAKE_BOOTSTRAP
1.9 espie 71: #include <regex.h>
1.1 espie 72: #endif
1.9 espie 73: #include <stddef.h>
74: #include <stdio.h>
75: #include <stdlib.h>
76: #include <string.h>
77: #include "config.h"
78: #include "defines.h"
1.1 espie 79: #include "buf.h"
1.9 espie 80: #include "var.h"
1.1 espie 81: #include "varmodifiers.h"
1.9 espie 82: #include "varname.h"
83: #include "targ.h"
84: #include "error.h"
85: #include "str.h"
86: #include "cmd_exec.h"
87: #include "memory.h"
88: #include "gnode.h"
89:
1.1 espie 90:
91: /* Var*Pattern flags */
92: #define VAR_SUB_GLOBAL 0x01 /* Apply substitution globally */
93: #define VAR_SUB_ONE 0x02 /* Apply substitution to one word */
1.7 espie 94: #define VAR_SUB_MATCHED 0x04 /* There was a match */
95: #define VAR_MATCH_START 0x08 /* Match at start of word */
1.1 espie 96: #define VAR_MATCH_END 0x10 /* Match at end of word */
97:
1.7 espie 98: /* Modifiers flags */
99: #define VAR_EQUAL 0x20
100: #define VAR_MAY_EQUAL 0x40
101: #define VAR_ADD_EQUAL 0x80
102: #define VAR_BANG_EQUAL 0x100
103:
1.1 espie 104: typedef struct {
1.22 espie 105: char *lbuffer; /* left string to free */
106: char *lhs; /* String to match */
107: size_t leftLen; /* Length of string */
108: char *rhs; /* Replacement string (w/ &'s removed) */
109: size_t rightLen; /* Length of replacement */
110: int flags;
1.1 espie 111: } VarPattern;
112:
1.7 espie 113: struct LoopStuff {
1.22 espie 114: struct LoopVar *var;
115: char *expand;
116: bool err;
1.7 espie 117: };
118:
1.9 espie 119: static bool VarHead(struct Name *, bool, Buffer, void *);
120: static bool VarTail(struct Name *, bool, Buffer, void *);
121: static bool VarSuffix(struct Name *, bool, Buffer, void *);
122: static bool VarRoot(struct Name *, bool, Buffer, void *);
123: static bool VarMatch(struct Name *, bool, Buffer, void *);
124: static bool VarSYSVMatch(struct Name *, bool, Buffer, void *);
125: static bool VarNoMatch(struct Name *, bool, Buffer, void *);
126: static bool VarUniq(struct Name *, bool, Buffer, void *);
127: static bool VarLoop(struct Name *, bool, Buffer, void *);
1.7 espie 128:
129:
1.1 espie 130: #ifndef MAKE_BOOTSTRAP
1.7 espie 131: static void VarREError(int, regex_t *, const char *);
1.9 espie 132: static bool VarRESubstitute(struct Name *, bool, Buffer, void *);
1.7 espie 133: static char *do_regex(const char *, const struct Name *, void *);
134:
1.1 espie 135: typedef struct {
1.22 espie 136: regex_t re;
137: int nsub;
138: regmatch_t *matches;
139: char *replace;
140: int flags;
1.1 espie 141: } VarREPattern;
142: #endif
143:
1.9 espie 144: static bool VarSubstitute(struct Name *, bool, Buffer, void *);
1.7 espie 145: static char *VarGetPattern(SymTable *, int, const char **, int, int,
146: size_t *, VarPattern *);
147: static char *VarQuote(const char *, const struct Name *, void *);
1.9 espie 148: static char *VarModify(char *, bool (*)(struct Name *, bool, Buffer, void *), void *);
1.7 espie 149:
1.9 espie 150: static void *check_empty(const char **, SymTable *, bool, int);
1.26 espie 151: static void *check_quote(const char **, SymTable *, bool, int);
1.7 espie 152: static char *do_upper(const char *, const struct Name *, void *);
153: static char *do_lower(const char *, const struct Name *, void *);
1.9 espie 154: static void *check_shcmd(const char **, SymTable *, bool, int);
1.7 espie 155: static char *do_shcmd(const char *, const struct Name *, void *);
156: static char *do_sort(const char *, const struct Name *, void *);
157: static char *finish_loop(const char *, const struct Name *, void *);
158: static int NameCompare(const void *, const void *);
159: static char *do_label(const char *, const struct Name *, void *);
160: static char *do_path(const char *, const struct Name *, void *);
161: static char *do_def(const char *, const struct Name *, void *);
162: static char *do_undef(const char *, const struct Name *, void *);
163: static char *do_assign(const char *, const struct Name *, void *);
164: static char *do_exec(const char *, const struct Name *, void *);
165:
1.9 espie 166: static void *assign_get_value(const char **, SymTable *, bool, int);
167: static void *get_cmd(const char **, SymTable *, bool, int);
168: static void *get_value(const char **, SymTable *, bool, int);
169: static void *get_stringarg(const char **, SymTable *, bool, int);
1.7 espie 170: static void free_stringarg(void *);
1.9 espie 171: static void *get_patternarg(const char **, SymTable *, bool, int);
172: static void *get_spatternarg(const char **, SymTable *, bool, int);
1.10 espie 173: static void *common_get_patternarg(const char **, SymTable *, bool, int, bool);
1.7 espie 174: static void free_patternarg(void *);
175: static void free_looparg(void *);
1.9 espie 176: static void *get_sysvpattern(const char **, SymTable *, bool, int);
177: static void *get_loop(const char **, SymTable *, bool, int);
1.7 espie 178: static char *LoopGrab(const char **);
179:
180: static struct Name dummy;
181: static struct Name *dummy_arg = &dummy;
182:
183: static struct modifier {
1.22 espie 184: bool atstart;
185: void * (*getarg)(const char **, SymTable *, bool, int);
186: char * (*apply)(const char *, const struct Name *, void *);
187: bool (*word_apply)(struct Name *, bool, Buffer, void *);
188: void (*freearg)(void *);
1.7 espie 189: } *choose_mod[256],
1.22 espie 190: match_mod = {false, get_stringarg, NULL, VarMatch, free_stringarg},
191: nomatch_mod = {false, get_stringarg, NULL, VarNoMatch, free_stringarg},
192: subst_mod = {false, get_spatternarg, NULL, VarSubstitute, free_patternarg},
1.1 espie 193: #ifndef MAKE_BOOTSTRAP
1.22 espie 194: resubst_mod = {false, get_patternarg, do_regex, NULL, free_patternarg},
1.1 espie 195: #endif
1.26 espie 196: quote_mod = {false, check_quote, VarQuote, NULL , free},
1.22 espie 197: tail_mod = {false, check_empty, NULL, VarTail, NULL},
198: head_mod = {false, check_empty, NULL, VarHead, NULL},
199: suffix_mod = {false, check_empty, NULL, VarSuffix, NULL},
200: root_mod = {false, check_empty, NULL, VarRoot, NULL},
201: upper_mod = {false, check_empty, do_upper, NULL, NULL},
202: lower_mod = {false, check_empty, do_lower, NULL, NULL},
203: shcmd_mod = {false, check_shcmd, do_shcmd, NULL, NULL},
204: sysv_mod = {false, get_sysvpattern, NULL, VarSYSVMatch, free_patternarg},
205: uniq_mod = {false, check_empty, NULL, VarUniq, NULL},
206: sort_mod = {false, check_empty, do_sort, NULL, NULL},
207: loop_mod = {false, get_loop, finish_loop, VarLoop, free_looparg},
208: undef_mod = {true, get_value, do_undef, NULL, NULL},
209: def_mod = {true, get_value, do_def, NULL, NULL},
210: label_mod = {true, check_empty, do_label, NULL, NULL},
211: path_mod = {true, check_empty, do_path, NULL, NULL},
212: assign_mod = {true, assign_get_value, do_assign, NULL, free_patternarg},
213: exec_mod = {true, get_cmd, do_exec, NULL, free_patternarg}
1.7 espie 214: ;
1.1 espie 215:
1.7 espie 216: void
217: VarModifiers_Init()
1.1 espie 218: {
1.22 espie 219: choose_mod['M'] = &match_mod;
220: choose_mod['N'] = &nomatch_mod;
221: choose_mod['S'] = &subst_mod;
1.7 espie 222: #ifndef MAKE_BOOTSTRAP
1.22 espie 223: choose_mod['C'] = &resubst_mod;
1.7 espie 224: #endif
1.22 espie 225: choose_mod['Q'] = "e_mod;
226: choose_mod['T'] = &tail_mod;
227: choose_mod['H'] = &head_mod;
228: choose_mod['E'] = &suffix_mod;
229: choose_mod['R'] = &root_mod;
230: if (FEATURES(FEATURE_UPPERLOWER)) {
231: choose_mod['U'] = &upper_mod;
232: choose_mod['L'] = &lower_mod;
233: }
234: if (FEATURES(FEATURE_SUNSHCMD))
235: choose_mod['s'] = &shcmd_mod;
236: if (FEATURES(FEATURE_UNIQ))
237: choose_mod['u'] = &uniq_mod;
238: if (FEATURES(FEATURE_SORT))
239: choose_mod['O'] = &sort_mod;
240: if (FEATURES(FEATURE_ODE)) {
241: choose_mod['@'] = &loop_mod;
242: choose_mod['D'] = &def_mod;
243: choose_mod['U'] = &undef_mod;
244: choose_mod['L'] = &label_mod;
1.24 espie 245: choose_mod['P'] = &path_mod;
1.22 espie 246: }
247: if (FEATURES(FEATURE_ASSIGN))
248: choose_mod[':'] = &assign_mod;
249: if (FEATURES(FEATURE_EXECMOD))
250: choose_mod['!'] = &exec_mod;
1.1 espie 251: }
252:
1.7 espie 253: /* All modifiers handle addSpace (need to add a space before placing the
254: * next word into the buffer) and propagate it when necessary.
1.1 espie 255: */
256:
257: /*-
258: *-----------------------------------------------------------------------
259: * VarHead --
1.7 espie 260: * Remove the tail of the given word and add the result to the given
1.1 espie 261: * buffer.
262: *-----------------------------------------------------------------------
263: */
1.9 espie 264: static bool
1.13 espie 265: VarHead(struct Name *word, bool addSpace, Buffer buf, void *dummy UNUSED)
1.1 espie 266: {
1.22 espie 267: const char *slash;
1.1 espie 268:
1.22 espie 269: slash = Str_rchri(word->s, word->e, '/');
270: if (slash != NULL) {
271: if (addSpace)
272: Buf_AddSpace(buf);
273: Buf_Addi(buf, word->s, slash);
274: } else {
275: /* If no directory part, give . (q.v. the POSIX standard). */
276: if (addSpace)
277: Buf_AddString(buf, " .");
278: else
279: Buf_AddChar(buf, '.');
280: }
281: return true;
1.1 espie 282: }
283:
284: /*-
285: *-----------------------------------------------------------------------
286: * VarTail --
1.7 espie 287: * Remove the head of the given word add the result to the given
1.1 espie 288: * buffer.
289: *-----------------------------------------------------------------------
290: */
1.9 espie 291: static bool
1.13 espie 292: VarTail(struct Name *word, bool addSpace, Buffer buf, void *dummy UNUSED)
1.1 espie 293: {
1.22 espie 294: const char *slash;
1.1 espie 295:
1.22 espie 296: if (addSpace)
297: Buf_AddSpace(buf);
298: slash = Str_rchri(word->s, word->e, '/');
299: if (slash != NULL)
300: Buf_Addi(buf, slash+1, word->e);
301: else
302: Buf_Addi(buf, word->s, word->e);
303: return true;
1.1 espie 304: }
305:
306: /*-
307: *-----------------------------------------------------------------------
308: * VarSuffix --
1.7 espie 309: * Add the suffix of the given word to the given buffer.
1.1 espie 310: *-----------------------------------------------------------------------
311: */
1.9 espie 312: static bool
1.13 espie 313: VarSuffix(struct Name *word, bool addSpace, Buffer buf, void *dummy UNUSED)
1.1 espie 314: {
1.22 espie 315: const char *dot;
1.1 espie 316:
1.22 espie 317: dot = Str_rchri(word->s, word->e, '.');
318: if (dot != NULL) {
319: if (addSpace)
320: Buf_AddSpace(buf);
321: Buf_Addi(buf, dot+1, word->e);
322: addSpace = true;
323: }
324: return addSpace;
1.1 espie 325: }
326:
327: /*-
328: *-----------------------------------------------------------------------
329: * VarRoot --
1.7 espie 330: * Remove the suffix of the given word and add the result to the
1.1 espie 331: * buffer.
332: *-----------------------------------------------------------------------
333: */
1.9 espie 334: static bool
1.13 espie 335: VarRoot(struct Name *word, bool addSpace, Buffer buf, void *dummy UNUSED)
1.1 espie 336: {
1.22 espie 337: const char *dot;
1.1 espie 338:
1.22 espie 339: if (addSpace)
340: Buf_AddSpace(buf);
341: dot = Str_rchri(word->s, word->e, '.');
342: if (dot != NULL)
343: Buf_Addi(buf, word->s, dot);
344: else
345: Buf_Addi(buf, word->s, word->e);
346: return true;
1.1 espie 347: }
348:
349: /*-
350: *-----------------------------------------------------------------------
351: * VarMatch --
1.7 espie 352: * Add the word to the buffer if it matches the given pattern.
1.1 espie 353: *-----------------------------------------------------------------------
354: */
1.9 espie 355: static bool
1.23 espie 356: VarMatch(struct Name *word, bool addSpace, Buffer buf,
1.13 espie 357: void *pattern) /* Pattern the word must match */
1.7 espie 358: {
1.22 espie 359: const char *pat = (const char *)pattern;
1.9 espie 360:
1.22 espie 361: if (Str_Matchi(word->s, word->e, pat, strchr(pat, '\0'))) {
362: if (addSpace)
363: Buf_AddSpace(buf);
364: Buf_Addi(buf, word->s, word->e);
365: return true;
366: } else
367: return addSpace;
1.7 espie 368: }
369:
370: /*-
371: *-----------------------------------------------------------------------
372: * VarNoMatch --
373: * Add the word to the buffer if it doesn't match the given pattern.
374: *-----------------------------------------------------------------------
375: */
1.9 espie 376: static bool
1.23 espie 377: VarNoMatch(struct Name *word, bool addSpace, Buffer buf,
1.13 espie 378: void *pattern) /* Pattern the word must not match */
1.7 espie 379: {
1.22 espie 380: const char *pat = (const char *)pattern;
1.9 espie 381:
1.22 espie 382: if (!Str_Matchi(word->s, word->e, pat, strchr(pat, '\0'))) {
383: if (addSpace)
384: Buf_AddSpace(buf);
385: Buf_Addi(buf, word->s, word->e);
386: return true;
387: } else
388: return addSpace;
1.7 espie 389: }
390:
1.9 espie 391: static bool
1.13 espie 392: VarUniq(struct Name *word, bool addSpace, Buffer buf, void *lastp)
1.1 espie 393: {
1.22 espie 394: struct Name *last = (struct Name *)lastp;
1.7 espie 395:
1.22 espie 396: /* does not match */
1.23 espie 397: if (last->s == NULL || last->e - last->s != word->e - word->s ||
1.22 espie 398: strncmp(word->s, last->s, word->e - word->s) != 0) {
399: if (addSpace)
400: Buf_AddSpace(buf);
401: Buf_Addi(buf, word->s, word->e);
402: addSpace = true;
403: }
404: last->s = word->s;
405: last->e = word->e;
406: return addSpace;
1.1 espie 407: }
408:
1.9 espie 409: static bool
1.13 espie 410: VarLoop(struct Name *word, bool addSpace, Buffer buf, void *vp)
1.7 espie 411: {
1.22 espie 412: struct LoopStuff *v = (struct LoopStuff *)vp;
1.7 espie 413:
1.22 espie 414: if (addSpace)
415: Buf_AddSpace(buf);
416: Var_SubstVar(buf, v->expand, v->var, word->s);
417: return true;
1.7 espie 418: }
419:
420: static char *
1.13 espie 421: finish_loop(const char *s, const struct Name *n UNUSED , void *p)
1.7 espie 422: {
423: struct LoopStuff *l = (struct LoopStuff *)p;
424:
1.15 espie 425: return Var_Subst(s, NULL, l->err);
1.7 espie 426: }
427:
428: static int
1.13 espie 429: NameCompare(const void *ap, const void *bp)
1.7 espie 430: {
431: struct Name *a, *b;
432: size_t n, m;
433: int c;
1.24 espie 434:
1.7 espie 435: a = (struct Name *)ap;
436: b = (struct Name *)bp;
437: n = a->e - a->s;
438: m = b->e - b->s;
439: if (n < m) {
440: c = strncmp(a->s, b->s, n);
441: if (c != 0)
442: return c;
443: else
444: return -1;
445: } else if (m < n) {
446: c = strncmp(a->s, b->s, m);
447: if (c != 0)
448: return c;
449: else
450: return 1;
451: } else
452: return strncmp(a->s, b->s, n);
453: }
454:
455: static char *
1.13 espie 456: do_sort(const char *s, const struct Name *dummy UNUSED, void *arg UNUSED)
1.7 espie 457: {
1.22 espie 458: struct Name *t;
459: unsigned long n, i, j;
460: const char *start, *end;
461:
462: n = 1024; /* start at 1024 words */
463: t = (struct Name *)emalloc(sizeof(struct Name) * n);
464: start = s;
465: end = start;
466:
467: for (i = 0;; i++) {
468: if (i == n) {
469: n *= 2;
470: t = (struct Name *)erealloc(t, sizeof(struct Name) * n);
471: }
472: start = iterate_words(&end);
473: if (start == NULL)
474: break;
475: t[i].s = start;
476: t[i].e = end;
477: }
478: if (i > 0) {
479: BUFFER buf;
1.7 espie 480:
1.22 espie 481: Buf_Init(&buf, end - s);
482: qsort(t, i, sizeof(struct Name), NameCompare);
483: Buf_Addi(&buf, t[0].s, t[0].e);
484: for (j = 1; j < i; j++) {
485: Buf_AddSpace(&buf);
486: Buf_Addi(&buf, t[j].s, t[j].e);
487: }
488: free(t);
489: return Buf_Retrieve(&buf);
490: } else {
491: free(t);
492: return "";
1.7 espie 493: }
494: }
495:
496: static char *
1.13 espie 497: do_label(const char *s UNUSED, const struct Name *n, void *arg UNUSED)
1.7 espie 498: {
1.22 espie 499: return Str_dupi(n->s, n->e);
1.7 espie 500: }
501:
502: static char *
1.13 espie 503: do_path(const char *s UNUSED, const struct Name *n, void *arg UNUSED)
1.7 espie 504: {
1.22 espie 505: GNode *gn;
1.7 espie 506:
1.22 espie 507: gn = Targ_FindNodei(n->s, n->e, TARG_NOCREATE);
508: if (gn == NULL)
509: return Str_dupi(n->s, n->e);
510: else
511: return strdup(gn->path);
1.7 espie 512: }
513:
514: static char *
1.13 espie 515: do_def(const char *s, const struct Name *n UNUSED, void *arg)
1.7 espie 516: {
1.22 espie 517: VarPattern *v = (VarPattern *)arg;
518: if (s == NULL) {
519: free_patternarg(v);
520: return NULL;
521: } else
522: return v->lbuffer;
1.7 espie 523: }
524:
525: static char *
1.13 espie 526: do_undef(const char *s, const struct Name *n UNUSED, void *arg)
1.7 espie 527: {
1.22 espie 528: VarPattern *v = (VarPattern *)arg;
529: if (s != NULL) {
530: free_patternarg(v);
531: return NULL;
532: } else
533: return v->lbuffer;
1.7 espie 534: }
535:
536: static char *
1.13 espie 537: do_assign(const char *s, const struct Name *n, void *arg)
1.7 espie 538: {
1.22 espie 539: VarPattern *v = (VarPattern *)arg;
540: char *msg;
541: char *result;
542:
543: switch (v->flags) {
544: case VAR_EQUAL:
545: Var_Seti(n->s, n->e, v->lbuffer);
546: break;
547: case VAR_MAY_EQUAL:
548: if (s == NULL)
549: Var_Seti(n->s, n->e, v->lbuffer);
550: break;
551: case VAR_ADD_EQUAL:
552: if (s == NULL)
553: Var_Seti(n->s, n->e, v->lbuffer);
554: else
555: Var_Appendi(n->s, n->e, v->lbuffer);
556: break;
557: case VAR_BANG_EQUAL:
558: result = Cmd_Exec(v->lbuffer, &msg);
559: if (result != NULL) {
560: Var_Seti(n->s, n->e, result);
561: free(result);
562: } else
563: Error(msg, v->lbuffer);
564: break;
1.7 espie 565:
1.22 espie 566: }
567: return NULL;
1.7 espie 568: }
569:
570: static char *
1.13 espie 571: do_exec(const char *s UNUSED, const struct Name *n UNUSED, void *arg)
1.7 espie 572: {
1.22 espie 573: VarPattern *v = (VarPattern *)arg;
574: char *msg;
575: char *result;
576:
577: result = Cmd_Exec(v->lbuffer, &msg);
578: if (result == NULL)
579: Error(msg, v->lbuffer);
580: return result;
1.7 espie 581: }
582:
1.1 espie 583: /*-
584: *-----------------------------------------------------------------------
585: * VarSYSVMatch --
1.7 espie 586: * Add the word to the buffer if it matches the given pattern.
587: * Used to implement the System V % modifiers.
1.1 espie 588: *-----------------------------------------------------------------------
589: */
1.9 espie 590: static bool
1.22 espie 591: VarSYSVMatch(struct Name *word, bool addSpace, Buffer buf, void *patp)
1.7 espie 592: {
1.22 espie 593: size_t len;
594: const char *ptr;
595: VarPattern *pat = (VarPattern *)patp;
1.1 espie 596:
1.22 espie 597: if (*word->s != '\0') {
598: if (addSpace)
599: Buf_AddSpace(buf);
600: if ((ptr = Str_SYSVMatch(word->s, pat->lhs, &len)) != NULL)
601: Str_SYSVSubst(buf, pat->rhs, ptr, len);
602: else
603: Buf_Addi(buf, word->s, word->e);
604: return true;
605: } else
606: return addSpace;
1.1 espie 607: }
608:
1.7 espie 609: void *
1.26 espie 610: get_sysvpattern(const char **p, SymTable *ctxt UNUSED, bool err, int endc)
1.1 espie 611: {
1.22 espie 612: VarPattern *pattern;
613: const char *cp, *cp2;
1.26 espie 614: BUFFER buf;
1.22 espie 615: int cnt = 0;
616: char startc = endc == ')' ? '(' : '{';
617: for (cp = *p;; cp++) {
618: if (*cp == '=' && cnt == 0)
619: break;
620: if (*cp == '\0')
621: return NULL;
622: if (*cp == startc)
623: cnt++;
624: else if (*cp == endc) {
625: cnt--;
626: if (cnt < 0)
627: return NULL;
628: }
1.7 espie 629: }
1.26 espie 630: Buf_Init(&buf, 0);
1.22 espie 631: for (cp2 = cp+1;; cp2++) {
1.29 ! espie 632: if ((*cp2 == ':' || *cp2 == endc) && cnt == 0)
1.22 espie 633: break;
1.26 espie 634: if (*cp2 == '\0') {
635: Buf_Destroy(&buf);
1.22 espie 636: return NULL;
1.26 espie 637: }
1.22 espie 638: if (*cp2 == startc)
639: cnt++;
640: else if (*cp2 == endc) {
641: cnt--;
1.26 espie 642: if (cnt < 0) {
643: Buf_Destroy(&buf);
1.22 espie 644: return NULL;
1.26 espie 645: }
646: } else if (*cp2 == '$') {
647: if (cp2[1] == '$')
648: cp2++;
649: else {
650: size_t len;
651: (void)Var_ParseBuffer(&buf, cp2, ctxt, err,
652: &len);
653: cp2 += len - 1;
654: continue;
655: }
1.22 espie 656: }
1.26 espie 657: Buf_AddChar(&buf, *cp2);
1.7 espie 658: }
1.23 espie 659:
1.22 espie 660: pattern = (VarPattern *)emalloc(sizeof(VarPattern));
661: pattern->lbuffer = pattern->lhs = Str_dupi(*p, cp);
662: pattern->leftLen = cp - *p;
1.26 espie 663: pattern->rhs = Buf_Retrieve(&buf);
664: pattern->rightLen = Buf_Size(&buf);
1.22 espie 665: pattern->flags = 0;
666: *p = cp2;
667: return pattern;
1.1 espie 668: }
669:
670:
671: /*-
672: *-----------------------------------------------------------------------
673: * VarSubstitute --
1.7 espie 674: * Perform a string-substitution on the given word, Adding the
675: * result to the given buffer.
1.1 espie 676: *-----------------------------------------------------------------------
677: */
1.9 espie 678: static bool
1.23 espie 679: VarSubstitute(struct Name *word, bool addSpace, Buffer buf,
1.13 espie 680: void *patternp) /* Pattern for substitution */
1.7 espie 681: {
682: size_t wordLen; /* Length of word */
683: const char *cp; /* General pointer */
684: VarPattern *pattern = (VarPattern *)patternp;
1.1 espie 685:
1.7 espie 686: wordLen = word->e - word->s;
1.1 espie 687: if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) !=
688: (VAR_SUB_ONE|VAR_SUB_MATCHED)) {
1.7 espie 689: /* Still substituting -- break it down into simple anchored cases
690: * and if none of them fits, perform the general substitution case. */
1.1 espie 691: if ((pattern->flags & VAR_MATCH_START) &&
1.7 espie 692: (strncmp(word->s, pattern->lhs, pattern->leftLen) == 0)) {
693: /* Anchored at start and beginning of word matches pattern. */
1.1 espie 694: if ((pattern->flags & VAR_MATCH_END) &&
695: (wordLen == pattern->leftLen)) {
1.7 espie 696: /* Also anchored at end and matches to the end (word
1.1 espie 697: * is same length as pattern) add space and rhs only
1.7 espie 698: * if rhs is non-null. */
1.1 espie 699: if (pattern->rightLen != 0) {
700: if (addSpace)
701: Buf_AddSpace(buf);
1.9 espie 702: addSpace = true;
1.7 espie 703: Buf_AddChars(buf, pattern->rightLen,
704: pattern->rhs);
1.1 espie 705: }
706: pattern->flags |= VAR_SUB_MATCHED;
707: } else if (pattern->flags & VAR_MATCH_END) {
1.7 espie 708: /* Doesn't match to end -- copy word wholesale. */
1.1 espie 709: goto nosub;
710: } else {
1.7 espie 711: /* Matches at start but need to copy in
712: * trailing characters. */
1.1 espie 713: if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){
714: if (addSpace)
715: Buf_AddSpace(buf);
1.9 espie 716: addSpace = true;
1.1 espie 717: }
718: Buf_AddChars(buf, pattern->rightLen, pattern->rhs);
719: Buf_AddChars(buf, wordLen - pattern->leftLen,
1.7 espie 720: word->s + pattern->leftLen);
1.1 espie 721: pattern->flags |= VAR_SUB_MATCHED;
722: }
723: } else if (pattern->flags & VAR_MATCH_START) {
1.7 espie 724: /* Had to match at start of word and didn't -- copy whole word. */
1.1 espie 725: goto nosub;
726: } else if (pattern->flags & VAR_MATCH_END) {
1.7 espie 727: /* Anchored at end, Find only place match could occur (leftLen
1.1 espie 728: * characters from the end of the word) and see if it does. Note
729: * that because the $ will be left at the end of the lhs, we have
1.7 espie 730: * to use strncmp. */
731: cp = word->s + (wordLen - pattern->leftLen);
732: if (cp >= word->s &&
733: strncmp(cp, pattern->lhs, pattern->leftLen) == 0) {
734: /* Match found. If we will place characters in the buffer,
1.1 espie 735: * add a space before hand as indicated by addSpace, then
736: * stuff in the initial, unmatched part of the word followed
1.7 espie 737: * by the right-hand-side. */
738: if (((cp - word->s) + pattern->rightLen) != 0) {
1.1 espie 739: if (addSpace)
740: Buf_AddSpace(buf);
1.9 espie 741: addSpace = true;
1.1 espie 742: }
1.9 espie 743: Buf_Addi(buf, word->s, cp);
1.1 espie 744: Buf_AddChars(buf, pattern->rightLen, pattern->rhs);
745: pattern->flags |= VAR_SUB_MATCHED;
746: } else {
1.7 espie 747: /* Had to match at end and didn't. Copy entire word. */
1.1 espie 748: goto nosub;
749: }
750: } else {
1.7 espie 751: /* Pattern is unanchored: search for the pattern in the word using
752: * strstr, copying unmatched portions and the
1.1 espie 753: * right-hand-side for each match found, handling non-global
754: * substitutions correctly, etc. When the loop is done, any
755: * remaining part of the word (word and wordLen are adjusted
756: * accordingly through the loop) is copied straight into the
757: * buffer.
1.9 espie 758: * addSpace is set to false as soon as a space is added to the
1.7 espie 759: * buffer. */
1.9 espie 760: bool done;
1.1 espie 761: size_t origSize;
762:
1.9 espie 763: done = false;
1.1 espie 764: origSize = Buf_Size(buf);
765: while (!done) {
1.7 espie 766: cp = strstr(word->s, pattern->lhs);
767: if (cp != NULL) {
768: if (addSpace && (cp - word->s) + pattern->rightLen != 0){
1.1 espie 769: Buf_AddSpace(buf);
1.9 espie 770: addSpace = false;
1.1 espie 771: }
1.9 espie 772: Buf_Addi(buf, word->s, cp);
1.1 espie 773: Buf_AddChars(buf, pattern->rightLen, pattern->rhs);
1.7 espie 774: wordLen -= (cp - word->s) + pattern->leftLen;
775: word->s = cp + pattern->leftLen;
776: if (wordLen == 0 || (pattern->flags & VAR_SUB_GLOBAL) == 0)
1.9 espie 777: done = true;
1.1 espie 778: pattern->flags |= VAR_SUB_MATCHED;
1.7 espie 779: } else
1.9 espie 780: done = true;
1.1 espie 781: }
782: if (wordLen != 0) {
783: if (addSpace)
784: Buf_AddSpace(buf);
1.7 espie 785: Buf_AddChars(buf, wordLen, word->s);
1.1 espie 786: }
1.7 espie 787: /* If added characters to the buffer, need to add a space
1.1 espie 788: * before we add any more. If we didn't add any, just return
1.7 espie 789: * the previous value of addSpace. */
790: return Buf_Size(buf) != origSize || addSpace;
1.1 espie 791: }
1.7 espie 792: return addSpace;
1.1 espie 793: }
794: nosub:
795: if (addSpace)
796: Buf_AddSpace(buf);
1.7 espie 797: Buf_AddChars(buf, wordLen, word->s);
1.9 espie 798: return true;
1.1 espie 799: }
800:
801: #ifndef MAKE_BOOTSTRAP
802: /*-
803: *-----------------------------------------------------------------------
804: * VarREError --
805: * Print the error caused by a regcomp or regexec call.
806: *-----------------------------------------------------------------------
807: */
808: static void
1.13 espie 809: VarREError(int err, regex_t *pat, const char *str)
1.1 espie 810: {
1.22 espie 811: char *errbuf;
812: int errlen;
1.1 espie 813:
1.22 espie 814: errlen = regerror(err, pat, 0, 0);
815: errbuf = emalloc(errlen);
816: regerror(err, pat, errbuf, errlen);
817: Error("%s: %s", str, errbuf);
818: free(errbuf);
1.1 espie 819: }
820:
821: /*-
822: *-----------------------------------------------------------------------
823: * VarRESubstitute --
824: * Perform a regex substitution on the given word, placing the
825: * result in the passed buffer.
826: *-----------------------------------------------------------------------
827: */
1.9 espie 828: static bool
1.13 espie 829: VarRESubstitute(struct Name *word, bool addSpace, Buffer buf, void *patternp)
1.7 espie 830: {
1.22 espie 831: VarREPattern *pat;
832: int xrv;
833: const char *wp;
834: char *rp;
835: int added;
1.1 espie 836:
837: #define MAYBE_ADD_SPACE() \
1.7 espie 838: if (addSpace && !added) \
1.22 espie 839: Buf_AddSpace(buf); \
1.1 espie 840: added = 1
841:
1.22 espie 842: added = 0;
843: wp = word->s;
844: pat = patternp;
845:
846: if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) ==
847: (VAR_SUB_ONE|VAR_SUB_MATCHED))
848: xrv = REG_NOMATCH;
849: else {
850: tryagain:
851: xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, 0);
1.1 espie 852: }
853:
1.22 espie 854: switch (xrv) {
855: case 0:
856: pat->flags |= VAR_SUB_MATCHED;
857: if (pat->matches[0].rm_so > 0) {
858: MAYBE_ADD_SPACE();
859: Buf_AddChars(buf, pat->matches[0].rm_so, wp);
1.1 espie 860: }
861:
1.22 espie 862: for (rp = pat->replace; *rp; rp++) {
863: if (*rp == '\\' && (rp[1] == '&' || rp[1] == '\\')) {
864: MAYBE_ADD_SPACE();
865: Buf_AddChar(buf,rp[1]);
866: rp++;
867: }
1.23 espie 868: else if (*rp == '&' ||
1.22 espie 869: (*rp == '\\' && isdigit(rp[1]))) {
870: int n;
871: const char *subbuf;
872: int sublen;
873: char errstr[3];
874:
875: if (*rp == '&') {
876: n = 0;
877: errstr[0] = '&';
878: errstr[1] = '\0';
879: } else {
880: n = rp[1] - '0';
881: errstr[0] = '\\';
882: errstr[1] = rp[1];
883: errstr[2] = '\0';
884: rp++;
885: }
886:
887: if (n > pat->nsub) {
1.23 espie 888: Error("No subexpression %s",
1.22 espie 889: &errstr[0]);
890: subbuf = "";
891: sublen = 0;
892: } else if (pat->matches[n].rm_so == -1 &&
893: pat->matches[n].rm_eo == -1) {
1.23 espie 894: Error("No match for subexpression %s",
1.22 espie 895: &errstr[0]);
896: subbuf = "";
897: sublen = 0;
898: } else {
899: subbuf = wp + pat->matches[n].rm_so;
1.23 espie 900: sublen = pat->matches[n].rm_eo -
1.22 espie 901: pat->matches[n].rm_so;
902: }
903:
904: if (sublen > 0) {
905: MAYBE_ADD_SPACE();
906: Buf_AddChars(buf, sublen, subbuf);
907: }
908: } else {
909: MAYBE_ADD_SPACE();
910: Buf_AddChar(buf, *rp);
911: }
912: }
913: wp += pat->matches[0].rm_eo;
914: if (pat->flags & VAR_SUB_GLOBAL)
915: goto tryagain;
916: if (*wp) {
917: MAYBE_ADD_SPACE();
918: Buf_AddString(buf, wp);
1.1 espie 919: }
1.22 espie 920: break;
921: default:
922: VarREError(xrv, &pat->re, "Unexpected regex error");
1.25 deraadt 923: /* FALLTHROUGH */
1.22 espie 924: case REG_NOMATCH:
925: if (*wp) {
926: MAYBE_ADD_SPACE();
927: Buf_AddString(buf, wp);
1.1 espie 928: }
1.22 espie 929: break;
1.1 espie 930: }
1.22 espie 931: return addSpace||added;
1.1 espie 932: }
933: #endif
934:
935: /*-
936: *-----------------------------------------------------------------------
937: * VarModify --
938: * Modify each of the words of the passed string using the given
939: * function. Used to implement all modifiers.
940: *
941: * Results:
942: * A string of all the words modified appropriately.
943: *-----------------------------------------------------------------------
944: */
945: static char *
1.13 espie 946: VarModify(char *str, /* String whose words should be trimmed */
1.7 espie 947: /* Function to use to modify them */
1.23 espie 948: bool (*modProc)(struct Name *, bool, Buffer, void *),
1.13 espie 949: void *datum) /* Datum to pass it */
1.1 espie 950: {
1.22 espie 951: BUFFER buf; /* Buffer for the new string */
952: bool addSpace; /* true if need to add a space to the
953: * buffer before adding the trimmed
954: * word */
955: struct Name word;
956:
957: Buf_Init(&buf, 0);
958: addSpace = false;
959:
960: word.e = str;
961:
962: while ((word.s = iterate_words(&word.e)) != NULL) {
963: char termc;
964:
965: termc = *word.e;
966: *((char *)(word.e)) = '\0';
967: addSpace = (*modProc)(&word, addSpace, &buf, datum);
968: *((char *)(word.e)) = termc;
969: }
970: return Buf_Retrieve(&buf);
1.1 espie 971: }
972:
973: /*-
974: *-----------------------------------------------------------------------
975: * VarGetPattern --
976: * Pass through the tstr looking for 1) escaped delimiters,
977: * '$'s and backslashes (place the escaped character in
978: * uninterpreted) and 2) unescaped $'s that aren't before
979: * the delimiter (expand the variable substitution).
980: * Return the expanded string or NULL if the delimiter was missing
981: * If pattern is specified, handle escaped ampersands, and replace
982: * unescaped ampersands with the lhs of the pattern.
983: *
984: * Results:
985: * A string of all the words modified appropriately.
986: * If length is specified, return the string length of the buffer
987: *-----------------------------------------------------------------------
988: */
989: static char *
1.23 espie 990: VarGetPattern(SymTable *ctxt, int err, const char **tstr, int delim1,
1.13 espie 991: int delim2, size_t *length, VarPattern *pattern)
1.1 espie 992: {
1.22 espie 993: const char *cp;
994: char *result;
995: BUFFER buf;
996: size_t junk;
997:
998: Buf_Init(&buf, 0);
999: if (length == NULL)
1000: length = &junk;
1.1 espie 1001:
1.7 espie 1002: #define IS_A_MATCH(cp, delim1, delim2) \
1.22 espie 1003: (cp[0] == '\\' && (cp[1] == delim1 || cp[1] == delim2 || \
1004: cp[1] == '\\' || cp[1] == '$' || (pattern && cp[1] == '&')))
1.1 espie 1005:
1.22 espie 1006: /*
1007: * Skim through until the matching delimiter is found;
1008: * pick up variable substitutions on the way. Also allow
1009: * backslashes to quote the delimiter, $, and \, but don't
1010: * touch other backslashes.
1011: */
1012: for (cp = *tstr; *cp != '\0' && *cp != delim1 && *cp != delim2; cp++) {
1013: if (IS_A_MATCH(cp, delim1, delim2)) {
1014: Buf_AddChar(&buf, cp[1]);
1015: cp++;
1016: } else if (*cp == '$') {
1017: /* Allowed at end of pattern */
1018: if (cp[1] == delim1 || cp[1] == delim2)
1019: Buf_AddChar(&buf, *cp);
1020: else {
1021: size_t len;
1022:
1023: /* If unescaped dollar sign not before the
1024: * delimiter, assume it's a variable
1025: * substitution and recurse. */
1.23 espie 1026: (void)Var_ParseBuffer(&buf, cp, ctxt, err,
1.22 espie 1027: &len);
1028: cp += len - 1;
1029: }
1030: } else if (pattern && *cp == '&')
1031: Buf_AddChars(&buf, pattern->leftLen, pattern->lhs);
1032: else
1033: Buf_AddChar(&buf, *cp);
1034: }
1.1 espie 1035:
1.22 espie 1036: *length = Buf_Size(&buf);
1037: result = Buf_Retrieve(&buf);
1.7 espie 1038:
1.22 espie 1039: if (*cp != delim1 && *cp != delim2) {
1040: *tstr = cp;
1041: *length = 0;
1042: free(result);
1043: return NULL;
1044: }
1045: else {
1046: *tstr = ++cp;
1047: return result;
1048: }
1.7 espie 1049: }
1050:
1051: /*-
1052: *-----------------------------------------------------------------------
1053: * VarQuote --
1054: * Quote shell meta-characters in the string
1055: *
1056: * Results:
1057: * The quoted string
1058: *-----------------------------------------------------------------------
1059: */
1060: static char *
1.26 espie 1061: VarQuote(const char *str, const struct Name *n UNUSED, void *islistp)
1.7 espie 1062: {
1.26 espie 1063: int islist = *((int *)islistp);
1.7 espie 1064:
1.22 espie 1065: BUFFER buf;
1066: /* This should cover most shells :-( */
1067: static char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
1.26 espie 1068: char *rep = meta;
1069: if (islist)
1070: rep += 3;
1.22 espie 1071:
1072: Buf_Init(&buf, MAKE_BSIZE);
1073: for (; *str; str++) {
1.26 espie 1074: if (strchr(rep, *str) != NULL)
1.22 espie 1075: Buf_AddChar(&buf, '\\');
1076: Buf_AddChar(&buf, *str);
1077: }
1078: return Buf_Retrieve(&buf);
1.7 espie 1079: }
1080:
1081: static void *
1.13 espie 1082: check_empty(const char **p, SymTable *ctxt UNUSED, bool b UNUSED, int endc)
1.7 espie 1083: {
1.22 espie 1084: dummy_arg->s = NULL;
1085: if ((*p)[1] == endc || (*p)[1] == ':') {
1086: (*p)++;
1087: return dummy_arg;
1.26 espie 1088: } else
1089: return NULL;
1090: }
1091:
1092: static void *
1093: check_quote(const char **p, SymTable *ctxt UNUSED, bool b UNUSED, int endc)
1094: {
1095: int *qargs = emalloc(sizeof(int));
1096: *qargs = 0;
1097: if ((*p)[1] == 'L') {
1098: *qargs = 1;
1099: (*p)++;
1100: }
1101: if ((*p)[1] == endc || (*p)[1] == ':') {
1102: (*p)++;
1103: return qargs;
1.22 espie 1104: } else
1105: return NULL;
1.7 espie 1106: }
1107:
1108: static void *
1.13 espie 1109: check_shcmd(const char **p, SymTable *ctxt UNUSED, bool b UNUSED, int endc)
1.7 espie 1110: {
1.22 espie 1111: if ((*p)[1] == 'h' && ((*p)[2] == endc || (*p)[2] == ':')) {
1112: (*p)+=2;
1113: return dummy_arg;
1114: } else
1115: return NULL;
1.7 espie 1116: }
1117:
1118:
1119: static char *
1.13 espie 1120: do_shcmd(const char *s, const struct Name *n UNUSED, void *arg UNUSED)
1.7 espie 1121: {
1.22 espie 1122: char *err;
1123: char *t;
1.7 espie 1124:
1.22 espie 1125: t = Cmd_Exec(s, &err);
1126: if (err)
1127: Error(err, s);
1128: return t;
1.7 espie 1129: }
1130:
1131: static void *
1.13 espie 1132: get_stringarg(const char **p, SymTable *ctxt UNUSED, bool b UNUSED, int endc)
1.7 espie 1133: {
1.22 espie 1134: const char *cp;
1135: char *s;
1.7 espie 1136:
1.22 espie 1137: for (cp = *p + 1; *cp != ':' && *cp != endc; cp++) {
1138: if (*cp == '\\') {
1139: if (cp[1] == ':' || cp[1] == endc || cp[1] == '\\')
1140: cp++;
1141: } else if (*cp == '\0')
1142: return NULL;
1143: }
1144: s = escape_dupi(*p+1, cp, ":)}");
1145: *p = cp;
1146: return s;
1.7 espie 1147: }
1148:
1149: static void
1.13 espie 1150: free_stringarg(void *arg)
1.7 espie 1151: {
1.22 espie 1152: free(arg);
1.7 espie 1153: }
1154:
1155: static char *
1.13 espie 1156: do_upper(const char *s, const struct Name *n UNUSED, void *arg UNUSED)
1.7 espie 1157: {
1.22 espie 1158: size_t len, i;
1159: char *t;
1.7 espie 1160:
1.22 espie 1161: len = strlen(s);
1162: t = emalloc(len+1);
1163: for (i = 0; i < len; i++)
1164: t[i] = toupper(s[i]);
1165: t[len] = '\0';
1166: return t;
1.7 espie 1167: }
1168:
1169: static char *
1.13 espie 1170: do_lower(const char *s, const struct Name *n UNUSED, void *arg UNUSED)
1.7 espie 1171: {
1.22 espie 1172: size_t len, i;
1173: char *t;
1.7 espie 1174:
1.22 espie 1175: len = strlen(s);
1176: t = emalloc(len+1);
1177: for (i = 0; i < len; i++)
1178: t[i] = tolower(s[i]);
1179: t[len] = '\0';
1180: return t;
1.1 espie 1181: }
1182:
1.10 espie 1183: static void *
1.13 espie 1184: get_patternarg(const char **p, SymTable *ctxt, bool err, int endc)
1.10 espie 1185: {
1.22 espie 1186: return common_get_patternarg(p, ctxt, err, endc, false);
1.10 espie 1187: }
1188:
1.7 espie 1189: /* Extract anchors */
1190: static void *
1.13 espie 1191: get_spatternarg(const char **p, SymTable *ctxt, bool err, int endc)
1.7 espie 1192: {
1.22 espie 1193: VarPattern *pattern;
1.7 espie 1194:
1.22 espie 1195: pattern = common_get_patternarg(p, ctxt, err, endc, true);
1196: if (pattern != NULL && pattern->leftLen > 0) {
1197: if (pattern->lhs[pattern->leftLen-1] == '$') {
1198: pattern->leftLen--;
1199: pattern->flags |= VAR_MATCH_END;
1.7 espie 1200: }
1.22 espie 1201: if (pattern->lhs[0] == '^') {
1202: pattern->lhs++;
1203: pattern->leftLen--;
1204: pattern->flags |= VAR_MATCH_START;
1205: }
1206: }
1207: return pattern;
1.7 espie 1208: }
1209:
1210: static void
1.13 espie 1211: free_looparg(void *arg)
1.1 espie 1212: {
1.22 espie 1213: struct LoopStuff *l = (struct LoopStuff *)arg;
1.1 espie 1214:
1.22 espie 1215: Var_DeleteLoopVar(l->var);
1216: free(l->expand);
1.7 espie 1217: }
1.1 espie 1218:
1.7 espie 1219: static char *
1.13 espie 1220: LoopGrab(const char **s)
1.7 espie 1221: {
1.22 espie 1222: const char *p, *start;
1.1 espie 1223:
1.22 espie 1224: start = *s;
1225: for (p = start; *p != '@'; p++) {
1226: if (*p == '\\')
1227: p++;
1228: if (*p == 0)
1229: return NULL;
1230: }
1231: *s = p+1;
1232: return escape_dupi(start, p, "@\\");
1.7 espie 1233: }
1.24 espie 1234:
1.7 espie 1235: static void *
1.21 espie 1236: get_loop(const char **p, SymTable *ctxt UNUSED, bool err, int endc)
1.7 espie 1237: {
1.22 espie 1238: static struct LoopStuff loop;
1239: const char *s;
1240: const char *var;
1241:
1242: s = *p +1;
1243:
1244: loop.var = NULL;
1245: loop.expand = NULL;
1246: loop.err = err;
1247: var = LoopGrab(&s);
1248: if (var != NULL) {
1249: loop.expand = LoopGrab(&s);
1250: if (*s == endc || *s == ':') {
1251: *p = s;
1252: loop.var = Var_NewLoopVar(var, NULL);
1253: return &loop;
1254: }
1.7 espie 1255: }
1.22 espie 1256: free_looparg(&loop);
1257: return NULL;
1.7 espie 1258: }
1.1 espie 1259:
1.7 espie 1260: static void *
1.23 espie 1261: common_get_patternarg(const char **p, SymTable *ctxt, bool err, int endc,
1.13 espie 1262: bool dosubst)
1.7 espie 1263: {
1.22 espie 1264: VarPattern *pattern;
1265: char delim;
1266: const char *s;
1267:
1268: pattern = (VarPattern *)emalloc(sizeof(VarPattern));
1269: pattern->flags = 0;
1270: s = *p;
1.1 espie 1271:
1.22 espie 1272: delim = s[1];
1273: if (delim == '\0')
1274: return NULL;
1275: s += 2;
1.1 espie 1276:
1.22 espie 1277: pattern->rhs = NULL;
1.23 espie 1278: pattern->lhs = VarGetPattern(ctxt, err, &s, delim, delim,
1.22 espie 1279: &pattern->leftLen, NULL);
1280: pattern->lbuffer = pattern->lhs;
1281: if (pattern->lhs != NULL) {
1282: pattern->rhs = VarGetPattern(ctxt, err, &s, delim, delim,
1283: &pattern->rightLen, dosubst ? pattern: NULL);
1284: if (pattern->rhs != NULL) {
1285: /* Check for global substitution. If 'g' after the
1286: * final delimiter, substitution is global and is
1287: * marked that way. */
1288: for (;; s++) {
1289: switch (*s) {
1290: case 'g':
1291: pattern->flags |= VAR_SUB_GLOBAL;
1292: continue;
1293: case '1':
1294: pattern->flags |= VAR_SUB_ONE;
1295: continue;
1296: }
1297: break;
1298: }
1299: if (*s == endc || *s == ':') {
1300: *p = s;
1301: return pattern;
1302: }
1.1 espie 1303: }
1.7 espie 1304: }
1.22 espie 1305: free_patternarg(pattern);
1306: return NULL;
1.7 espie 1307: }
1308:
1309: static void *
1.13 espie 1310: assign_get_value(const char **p, SymTable *ctxt, bool err, int endc)
1.7 espie 1311: {
1.22 espie 1312: const char *s;
1313: int flags;
1314: VarPattern *arg;
1315:
1316: s = *p + 1;
1317: if (s[0] == '=')
1318: flags = VAR_EQUAL;
1319: else if (s[0] == '?' && s[1] == '=')
1320: flags = VAR_MAY_EQUAL;
1321: else if (s[0] == '+' && s[1] == '=')
1322: flags = VAR_ADD_EQUAL;
1323: else if (s[0] == '!' && s[1] == '=')
1324: flags = VAR_BANG_EQUAL;
1325: else
1326: return NULL;
1327:
1328: arg = get_value(&s, ctxt, err, endc);
1329: if (arg != NULL) {
1330: *p = s;
1331: arg->flags = flags;
1.23 espie 1332: }
1.22 espie 1333: return arg;
1.7 espie 1334: }
1.1 espie 1335:
1.7 espie 1336: static void *
1.13 espie 1337: get_value(const char **p, SymTable *ctxt, bool err, int endc)
1.7 espie 1338: {
1.22 espie 1339: VarPattern *pattern;
1340: const char *s;
1.1 espie 1341:
1.22 espie 1342: pattern = (VarPattern *)emalloc(sizeof(VarPattern));
1343: s = *p + 1;
1344: pattern->rhs = NULL;
1345: pattern->lbuffer = VarGetPattern(ctxt, err, &s, ':', endc,
1346: &pattern->leftLen, NULL);
1347: if (s[-1] == endc || s[-1] == ':') {
1348: *p = s-1;
1349: return pattern;
1350: }
1351: free_patternarg(pattern);
1352: return NULL;
1.7 espie 1353: }
1.1 espie 1354:
1.7 espie 1355: static void *
1.13 espie 1356: get_cmd(const char **p, SymTable *ctxt, bool err, int endc UNUSED)
1.7 espie 1357: {
1.22 espie 1358: VarPattern *pattern;
1359: const char *s;
1.1 espie 1360:
1.22 espie 1361: pattern = (VarPattern *)emalloc(sizeof(VarPattern));
1362: s = *p + 1;
1363: pattern->rhs = NULL;
1364: pattern->lbuffer = VarGetPattern(ctxt, err, &s, '!', '!',
1365: &pattern->leftLen, NULL);
1366: if (s[-1] == '!') {
1367: *p = s-1;
1368: return pattern;
1369: }
1370: free_patternarg(pattern);
1371: return NULL;
1.7 espie 1372: }
1.1 espie 1373:
1.7 espie 1374: static void
1.13 espie 1375: free_patternarg(void *p)
1.7 espie 1376: {
1.22 espie 1377: VarPattern *vp = (VarPattern *)p;
1.8 espie 1378:
1.22 espie 1379: free(vp->lbuffer);
1380: free(vp->rhs);
1381: free(vp);
1.1 espie 1382: }
1383:
1.7 espie 1384: #ifndef MAKE_BOOTSTRAP
1.1 espie 1385: static char *
1.13 espie 1386: do_regex(const char *s, const struct Name *n UNUSED, void *arg)
1.7 espie 1387: {
1.22 espie 1388: VarREPattern p2;
1389: VarPattern *p = (VarPattern *)arg;
1390: int error;
1391: char *result;
1392:
1393: error = regcomp(&p2.re, p->lhs, REG_EXTENDED);
1394: if (error) {
1395: VarREError(error, &p2.re, "RE substitution error");
1396: return var_Error;
1397: }
1398: p2.nsub = p2.re.re_nsub + 1;
1399: p2.replace = p->rhs;
1400: p2.flags = p->flags;
1401: if (p2.nsub < 1)
1402: p2.nsub = 1;
1403: if (p2.nsub > 10)
1404: p2.nsub = 10;
1405: p2.matches = emalloc(p2.nsub * sizeof(regmatch_t));
1406: result = VarModify((char *)s, VarRESubstitute, &p2);
1407: regfree(&p2.re);
1408: free(p2.matches);
1409: return result;
1.7 espie 1410: }
1411: #endif
1412:
1413: char *
1.23 espie 1414: VarModifiers_Apply(char *str, const struct Name *name, SymTable *ctxt,
1.18 espie 1415: bool err, bool *freePtr, const char **pscan, int paren)
1.1 espie 1416: {
1.22 espie 1417: const char *tstr;
1418: bool atstart; /* Some ODE modifiers only make sense at start */
1419: char endc = paren == '(' ? ')' : '}';
1420: const char *start = *pscan;
1421:
1422: tstr = start;
1423: /*
1424: * Now we need to apply any modifiers the user wants applied.
1425: * These are:
1426: * :M<pattern> words which match the given <pattern>.
1427: * <pattern> is of the standard file
1428: * wildcarding form.
1429: * :S<d><pat1><d><pat2><d>[g]
1430: * Substitute <pat2> for <pat1> in the
1431: * value
1432: * :C<d><pat1><d><pat2><d>[g]
1433: * Substitute <pat2> for regex <pat1> in
1434: * the value
1435: * :H Substitute the head of each word
1436: * :T Substitute the tail of each word
1437: * :E Substitute the extension (minus '.') of
1438: * each word
1439: * :R Substitute the root of each word
1440: * (pathname minus the suffix).
1441: * :lhs=rhs Like :S, but the rhs goes to the end of
1442: * the invocation.
1443: */
1.24 espie 1444:
1.22 espie 1445: atstart = true;
1446: while (*tstr != endc && *tstr != '\0') {
1447: struct modifier *mod;
1448: void *arg;
1449: char *newStr;
1450:
1451: tstr++;
1452: if (DEBUG(VAR))
1453: printf("Applying :%c to \"%s\"\n", *tstr, str);
1454:
1455: mod = choose_mod[*tstr];
1456: arg = NULL;
1457:
1458: if (mod != NULL && (!mod->atstart || atstart))
1459: arg = mod->getarg(&tstr, ctxt, err, endc);
1460: if (FEATURES(FEATURE_SYSVVARSUB) && arg == NULL) {
1461: mod = &sysv_mod;
1462: arg = mod->getarg(&tstr, ctxt, err, endc);
1463: }
1464: atstart = false;
1465: if (arg != NULL) {
1466: if (str != NULL || (mod->atstart && name != NULL)) {
1467: if (mod->word_apply != NULL) {
1.23 espie 1468: newStr = VarModify(str,
1.22 espie 1469: mod->word_apply, arg);
1470: if (mod->apply != NULL) {
1471: char *newStr2;
1472:
1.23 espie 1473: newStr2 = mod->apply(newStr,
1.22 espie 1474: name, arg);
1475: free(newStr);
1476: newStr = newStr2;
1477: }
1478: } else
1479: newStr = mod->apply(str, name, arg);
1480: if (*freePtr)
1481: free(str);
1482: str = newStr;
1483: if (str != var_Error)
1484: *freePtr = true;
1485: else
1486: *freePtr = false;
1487: }
1488: if (mod->freearg != NULL)
1489: mod->freearg(arg);
1490: } else {
1491: Error("Bad modifier: %s\n", tstr);
1492: /* Try skipping to end of var... */
1493: for (tstr++; *tstr != endc && *tstr != '\0';)
1494: tstr++;
1495: if (str != NULL && *freePtr)
1496: free(str);
1497: str = var_Error;
1498: *freePtr = false;
1499: break;
1500: }
1501: if (DEBUG(VAR))
1502: printf("Result is \"%s\"\n", str);
1.7 espie 1503: }
1.22 espie 1504: if (*tstr == '\0')
1505: Error("Unclosed variable specification");
1506: else
1.7 espie 1507: tstr++;
1508:
1.22 espie 1509: *pscan = tstr;
1510: return str;
1.1 espie 1511: }
1.7 espie 1512:
1.1 espie 1513: char *
1.13 espie 1514: Var_GetHead(char *s)
1.1 espie 1515: {
1.22 espie 1516: return VarModify(s, VarHead, NULL);
1.1 espie 1517: }
1518:
1519: char *
1.13 espie 1520: Var_GetTail(char *s)
1.1 espie 1521: {
1.22 espie 1522: return VarModify(s, VarTail, NULL);
1.1 espie 1523: }