Annotation of src/usr.bin/make/var.c, Revision 1.33
1.33 ! espie 1: /* $OpenBSD: var.c,v 1.32 2000/06/10 01:41:06 espie Exp $ */
1.6 millert 2: /* $NetBSD: var.c,v 1.18 1997/03/18 19:24:46 christos Exp $ */
1.1 deraadt 3:
4: /*
1.17 espie 5: * Copyright (c) 1999 Marc Espie.
6: *
7: * Extensive code modifications 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: /*
1.5 millert 32: * Copyright (c) 1988, 1989, 1990, 1993
33: * The Regents of the University of California. All rights reserved.
1.1 deraadt 34: * Copyright (c) 1989 by Berkeley Softworks
35: * All rights reserved.
36: *
37: * This code is derived from software contributed to Berkeley by
38: * Adam de Boor.
39: *
40: * Redistribution and use in source and binary forms, with or without
41: * modification, are permitted provided that the following conditions
42: * are met:
43: * 1. Redistributions of source code must retain the above copyright
44: * notice, this list of conditions and the following disclaimer.
45: * 2. Redistributions in binary form must reproduce the above copyright
46: * notice, this list of conditions and the following disclaimer in the
47: * documentation and/or other materials provided with the distribution.
48: * 3. All advertising materials mentioning features or use of this software
49: * must display the following acknowledgement:
50: * This product includes software developed by the University of
51: * California, Berkeley and its contributors.
52: * 4. Neither the name of the University nor the names of its contributors
53: * may be used to endorse or promote products derived from this software
54: * without specific prior written permission.
55: *
56: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
57: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
58: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
59: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
60: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
61: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
62: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
63: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
64: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
65: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
66: * SUCH DAMAGE.
67: */
68:
69: #ifndef lint
70: #if 0
1.5 millert 71: static char sccsid[] = "@(#)var.c 8.3 (Berkeley) 3/19/94";
1.1 deraadt 72: #else
1.33 ! espie 73: static char rcsid[] = "$OpenBSD: var.c,v 1.32 2000/06/10 01:41:06 espie Exp $";
1.1 deraadt 74: #endif
75: #endif /* not lint */
76:
77: /*-
78: * var.c --
79: * Variable-handling functions
80: *
81: * Interface:
82: * Var_Set Set the value of a variable in the given
83: * context. The variable is created if it doesn't
84: * yet exist. The value and variable name need not
85: * be preserved.
86: *
87: * Var_Append Append more characters to an existing variable
88: * in the given context. The variable needn't
89: * exist already -- it will be created if it doesn't.
90: * A space is placed between the old value and the
91: * new one.
92: *
93: * Var_Exists See if a variable exists.
94: *
95: * Var_Value Return the value of a variable in a context or
96: * NULL if the variable is undefined.
97: *
98: * Var_Subst Substitute named variable, or all variables if
99: * NULL in a string using
100: * the given context as the top-most one. If the
101: * third argument is non-zero, Parse_Error is
102: * called if any variables are undefined.
103: *
104: * Var_Parse Parse a variable expansion from a string and
105: * return the result and the number of characters
106: * consumed.
107: *
108: * Var_Delete Delete a variable in a context.
109: *
110: * Var_Init Initialize this module.
111: *
112: * Debugging:
113: * Var_Dump Print out all variables defined in the given
114: * context.
115: *
116: * XXX: There's a lot of duplication in these functions.
117: */
118:
119: #include <ctype.h>
1.6 millert 120: #ifndef MAKE_BOOTSTRAP
1.9 deraadt 121: #include <sys/types.h>
1.6 millert 122: #include <regex.h>
123: #endif
124: #include <stdlib.h>
1.1 deraadt 125: #include "make.h"
126: #include "buf.h"
127:
128: /*
129: * This is a harmless return value for Var_Parse that can be used by Var_Subst
130: * to determine if there was an error in parsing -- easier than returning
131: * a flag, as things outside this module don't give a hoot.
132: */
133: char var_Error[] = "";
134:
135: /*
136: * Similar to var_Error, but returned when the 'err' flag for Var_Parse is
137: * set false. Why not just use a constant? Well, gcc likes to condense
138: * identical string instances...
139: */
140: static char varNoError[] = "";
141:
142: /*
143: * Internally, variables are contained in four different contexts.
144: * 1) the environment. They may not be changed. If an environment
145: * variable is appended-to, the result is placed in the global
146: * context.
147: * 2) the global context. Variables set in the Makefile are located in
148: * the global context. It is the penultimate context searched when
149: * substituting.
150: * 3) the command-line context. All variables set on the command line
151: * are placed in this context. They are UNALTERABLE once placed here.
152: * 4) the local context. Each target has associated with it a context
153: * list. On this list are located the structures describing such
154: * local variables as $(@) and $(*)
155: * The four contexts are searched in the reverse order from which they are
156: * listed.
157: */
1.17 espie 158: GNode *VAR_GLOBAL; /* variables from the makefile */
159: GNode *VAR_CMD; /* variables defined on the command-line */
160: static GNode *VAR_ENV; /* variables read from env */
1.1 deraadt 161:
1.33 ! espie 162: static LIST allVars; /* List of all variables */
1.1 deraadt 163:
164: #define FIND_CMD 0x1 /* look in VAR_CMD when searching */
165: #define FIND_GLOBAL 0x2 /* look in VAR_GLOBAL as well */
166: #define FIND_ENV 0x4 /* look in the environment also */
167:
168: typedef struct Var {
169: char *name; /* the variable's name */
1.23 espie 170: BUFFER val; /* its value */
1.1 deraadt 171: int flags; /* miscellaneous status flags */
172: #define VAR_IN_USE 1 /* Variable's value currently being used.
173: * Used to avoid recursion */
174: #define VAR_JUNK 4 /* Variable is a junk variable that
175: * should be destroyed when done with
176: * it. Used by Var_Parse for undefined,
177: * modified variables */
178: } Var;
179:
1.6 millert 180: /* Var*Pattern flags */
181: #define VAR_SUB_GLOBAL 0x01 /* Apply substitution globally */
182: #define VAR_SUB_ONE 0x02 /* Apply substitution to one word */
183: #define VAR_SUB_MATCHED 0x04 /* There was a match */
184: #define VAR_MATCH_START 0x08 /* Match at start of word */
185: #define VAR_MATCH_END 0x10 /* Match at end of word */
186:
1.1 deraadt 187: typedef struct {
188: char *lhs; /* String to match */
1.16 espie 189: size_t leftLen; /* Length of string */
1.1 deraadt 190: char *rhs; /* Replacement string (w/ &'s removed) */
1.16 espie 191: size_t rightLen; /* Length of replacement */
1.1 deraadt 192: int flags;
193: } VarPattern;
194:
1.6 millert 195: #ifndef MAKE_BOOTSTRAP
196: typedef struct {
197: regex_t re;
198: int nsub;
199: regmatch_t *matches;
200: char *replace;
201: int flags;
202: } VarREPattern;
203: #endif
204:
1.23 espie 205: #define VarValue(v) Buf_Retrieve(&((v)->val))
1.32 espie 206: static int VarCmp __P((void *, void *));
1.1 deraadt 207: static Var *VarFind __P((char *, GNode *, int));
1.17 espie 208: static Var *VarAdd __P((char *, char *, GNode *));
1.32 espie 209: static void VarDelete __P((void *));
210: static Boolean VarHead __P((char *, Boolean, Buffer, void *));
211: static Boolean VarTail __P((char *, Boolean, Buffer, void *));
212: static Boolean VarSuffix __P((char *, Boolean, Buffer, void *));
213: static Boolean VarRoot __P((char *, Boolean, Buffer, void *));
214: static Boolean VarMatch __P((char *, Boolean, Buffer, void *));
1.4 briggs 215: #ifdef SYSVVARSUB
1.32 espie 216: static Boolean VarSYSVMatch __P((char *, Boolean, Buffer, void *));
1.4 briggs 217: #endif
1.32 espie 218: static Boolean VarNoMatch __P((char *, Boolean, Buffer, void *));
1.6 millert 219: #ifndef MAKE_BOOTSTRAP
220: static void VarREError __P((int, regex_t *, const char *));
1.32 espie 221: static Boolean VarRESubstitute __P((char *, Boolean, Buffer, void *));
1.6 millert 222: #endif
1.32 espie 223: static Boolean VarSubstitute __P((char *, Boolean, Buffer, void *));
1.16 espie 224: static char *VarGetPattern __P((GNode *, int, char **, int, int *, size_t *,
1.6 millert 225: VarPattern *));
226: static char *VarQuote __P((char *));
1.32 espie 227: static char *VarModify __P((char *, Boolean (*)(char *, Boolean, Buffer, void *), void *));
228: static void VarPrintVar __P((void *));
229: static Boolean VarUppercase __P((char *word, Boolean addSpace, Buffer buf, void *dummy));
230: static Boolean VarLowercase __P((char *word, Boolean addSpace, Buffer buf, void *dummy));
1.1 deraadt 231:
232: /*-
233: *-----------------------------------------------------------------------
234: * VarCmp --
235: * See if the given variable matches the named one. Called from
236: * Lst_Find when searching for a variable of a given name.
237: *
238: * Results:
239: * 0 if they match. non-zero otherwise.
240: *
241: * Side Effects:
242: * none
243: *-----------------------------------------------------------------------
244: */
245: static int
1.32 espie 246: VarCmp(v, name)
247: void *v; /* VAR structure to compare */
248: void *name; /* name to look for */
1.1 deraadt 249: {
1.32 espie 250: return strcmp((char *)name, ((Var *)v)->name);
1.1 deraadt 251: }
252:
253: /*-
254: *-----------------------------------------------------------------------
255: * VarFind --
256: * Find the given variable in the given context and any other contexts
257: * indicated.
258: *
259: * Results:
260: * A pointer to the structure describing the desired variable or
1.26 espie 261: * NULL if the variable does not exist.
1.1 deraadt 262: *
263: * Side Effects:
1.17 espie 264: * Caches env variables in the VAR_ENV context.
1.1 deraadt 265: *-----------------------------------------------------------------------
266: */
267: static Var *
268: VarFind (name, ctxt, flags)
269: char *name; /* name to find */
270: GNode *ctxt; /* context in which to find it */
271: int flags; /* FIND_GLOBAL set means to look in the
272: * VAR_GLOBAL context as well.
273: * FIND_CMD set means to look in the VAR_CMD
274: * context also.
275: * FIND_ENV set means to look in the
1.17 espie 276: * environment/VAR_ENV context. */
1.1 deraadt 277: {
278: LstNode var;
279: Var *v;
280:
281: /*
282: * If the variable name begins with a '.', it could very well be one of
283: * the local ones. We check the name against all the local variables
284: * and substitute the short version in for 'name' if it matches one of
285: * them.
286: */
287: if (*name == '.' && isupper((unsigned char) name[1]))
288: switch (name[1]) {
289: case 'A':
290: if (!strcmp(name, ".ALLSRC"))
291: name = ALLSRC;
292: if (!strcmp(name, ".ARCHIVE"))
293: name = ARCHIVE;
294: break;
295: case 'I':
296: if (!strcmp(name, ".IMPSRC"))
297: name = IMPSRC;
298: break;
299: case 'M':
300: if (!strcmp(name, ".MEMBER"))
301: name = MEMBER;
302: break;
303: case 'O':
304: if (!strcmp(name, ".OODATE"))
305: name = OODATE;
306: break;
307: case 'P':
308: if (!strcmp(name, ".PREFIX"))
309: name = PREFIX;
310: break;
311: case 'T':
312: if (!strcmp(name, ".TARGET"))
313: name = TARGET;
314: break;
315: }
316: /*
317: * First look for the variable in the given context. If it's not there,
318: * look for it in VAR_CMD, VAR_GLOBAL and the environment, in that order,
319: * depending on the FIND_* flags in 'flags'
320: */
1.33 ! espie 321: var = Lst_Find(&ctxt->context, VarCmp, name);
1.1 deraadt 322:
1.26 espie 323: if ((var == NULL) && (flags & FIND_CMD) && (ctxt != VAR_CMD))
1.33 ! espie 324: var = Lst_Find(&VAR_CMD->context, VarCmp, name);
1.26 espie 325: if (!checkEnvFirst && (var == NULL) && (flags & FIND_GLOBAL) &&
1.17 espie 326: (ctxt != VAR_GLOBAL)) {
1.33 ! espie 327: var = Lst_Find(&VAR_GLOBAL->context, VarCmp, name);
1.1 deraadt 328: }
1.26 espie 329: if ((var == NULL) && (flags & FIND_ENV)) {
1.33 ! espie 330: var = Lst_Find(&VAR_ENV->context, VarCmp, name);
1.26 espie 331: if (var == NULL) {
1.17 espie 332: char *env;
333:
334: if ((env = getenv(name)) != NULL)
335: return VarAdd(name, env, VAR_ENV);
1.1 deraadt 336: }
1.17 espie 337: }
1.26 espie 338: if (var == NULL && checkEnvFirst && (flags & FIND_GLOBAL) &&
1.17 espie 339: (ctxt != VAR_GLOBAL))
1.33 ! espie 340: var = Lst_Find(&VAR_GLOBAL->context, VarCmp, name);
1.26 espie 341: if (var == NULL)
342: return NULL;
1.17 espie 343: else
1.26 espie 344: return (Var *)Lst_Datum(var);
1.1 deraadt 345: }
346:
347: /*-
348: *-----------------------------------------------------------------------
349: * VarAdd --
350: * Add a new variable of name name and value val to the given context
351: *
352: * Results:
1.17 espie 353: * The added variable
1.1 deraadt 354: *
355: * Side Effects:
356: * The new variable is placed at the front of the given context
357: * The name and val arguments are duplicated so they may
358: * safely be freed.
359: *-----------------------------------------------------------------------
360: */
1.17 espie 361: static Var *
362: VarAdd(name, val, ctxt)
1.1 deraadt 363: char *name; /* name of variable to add */
364: char *val; /* value to set it to */
365: GNode *ctxt; /* context in which to set it */
366: {
367: register Var *v;
368: int len;
369:
370: v = (Var *) emalloc (sizeof (Var));
371:
1.4 briggs 372: v->name = estrdup (name);
1.1 deraadt 373:
374: len = val ? strlen(val) : 0;
1.23 espie 375: Buf_Init(&(v->val), len+1);
376: Buf_AddChars(&(v->val), len, val);
1.1 deraadt 377:
378: v->flags = 0;
379:
1.33 ! espie 380: Lst_AtFront(&ctxt->context, v);
! 381: Lst_AtEnd(&allVars, v);
1.1 deraadt 382: if (DEBUG(VAR)) {
383: printf("%s:%s = %s\n", ctxt->name, name, val);
384: }
1.17 espie 385: return v;
1.1 deraadt 386: }
387:
388:
389: /*-
390: *-----------------------------------------------------------------------
391: * VarDelete --
392: * Delete a variable and all the space associated with it.
393: *
394: * Results:
395: * None
396: *
397: * Side Effects:
398: * None
399: *-----------------------------------------------------------------------
400: */
401: static void
402: VarDelete(vp)
1.32 espie 403: void *vp;
1.1 deraadt 404: {
405: Var *v = (Var *) vp;
406: free(v->name);
1.23 espie 407: Buf_Destroy(&(v->val));
408: free(v);
1.1 deraadt 409: }
410:
411:
412:
413: /*-
414: *-----------------------------------------------------------------------
415: * Var_Delete --
416: * Remove a variable from a context.
417: *
418: * Results:
419: * None.
420: *
421: * Side Effects:
422: * The Var structure is removed and freed.
423: *
424: *-----------------------------------------------------------------------
425: */
426: void
427: Var_Delete(name, ctxt)
428: char *name;
429: GNode *ctxt;
430: {
431: LstNode ln;
432:
433: if (DEBUG(VAR)) {
434: printf("%s:delete %s\n", ctxt->name, name);
435: }
1.33 ! espie 436: ln = Lst_Find(&ctxt->context, VarCmp, name);
1.26 espie 437: if (ln != NULL) {
1.1 deraadt 438: register Var *v;
439:
440: v = (Var *)Lst_Datum(ln);
1.33 ! espie 441: Lst_Remove(&ctxt->context, ln);
! 442: ln = Lst_Member(&allVars, v);
! 443: Lst_Remove(&allVars, ln);
1.30 espie 444: VarDelete(v);
1.1 deraadt 445: }
446: }
447:
448: /*-
449: *-----------------------------------------------------------------------
450: * Var_Set --
451: * Set the variable name to the value val in the given context.
452: *
453: * Results:
454: * None.
455: *
456: * Side Effects:
457: * If the variable doesn't yet exist, a new record is created for it.
458: * Else the old value is freed and the new one stuck in its place
459: *
460: * Notes:
461: * The variable is searched for only in its context before being
462: * created in that context. I.e. if the context is VAR_GLOBAL,
463: * only VAR_GLOBAL->context is searched. Likewise if it is VAR_CMD, only
464: * VAR_CMD->context is searched. This is done to avoid the literally
465: * thousands of unnecessary strcmp's that used to be done to
466: * set, say, $(@) or $(<).
467: *-----------------------------------------------------------------------
468: */
469: void
470: Var_Set (name, val, ctxt)
471: char *name; /* name of variable to set */
472: char *val; /* value to give to the variable */
473: GNode *ctxt; /* context in which to set it */
474: {
475: register Var *v;
476:
477: /*
478: * We only look for a variable in the given context since anything set
479: * here will override anything in a lower context, so there's not much
480: * point in searching them all just to save a bit of memory...
481: */
482: v = VarFind (name, ctxt, 0);
1.26 espie 483: if (v == NULL) {
1.17 espie 484: (void)VarAdd(name, val, ctxt);
1.1 deraadt 485: } else {
1.23 espie 486: Buf_Reset(&(v->val));
487: Buf_AddString(&(v->val), val);
1.1 deraadt 488:
489: if (DEBUG(VAR)) {
490: printf("%s:%s = %s\n", ctxt->name, name, val);
491: }
492: }
493: /*
494: * Any variables given on the command line are automatically exported
1.17 espie 495: * to the environment (as per POSIX standard).
496: * We put them into the env cache directly.
497: * (Note that additions to VAR_CMD occur very early, so VAR_ENV is
498: * actually empty at this point).
1.1 deraadt 499: */
500: if (ctxt == VAR_CMD) {
501: setenv(name, val, 1);
1.17 espie 502: (void)VarAdd(name, val, VAR_ENV);
1.1 deraadt 503: }
504: }
505:
506: /*-
507: *-----------------------------------------------------------------------
508: * Var_Append --
509: * The variable of the given name has the given value appended to it in
510: * the given context.
511: *
512: * Results:
513: * None
514: *
515: * Side Effects:
516: * If the variable doesn't exist, it is created. Else the strings
517: * are concatenated (with a space in between).
518: *
519: * Notes:
520: * Only if the variable is being sought in the global context is the
521: * environment searched.
522: * XXX: Knows its calling circumstances in that if called with ctxt
523: * an actual target, it will only search that context since only
524: * a local variable could be being appended to. This is actually
525: * a big win and must be tolerated.
526: *-----------------------------------------------------------------------
527: */
528: void
529: Var_Append (name, val, ctxt)
530: char *name; /* Name of variable to modify */
531: char *val; /* String to append to it */
532: GNode *ctxt; /* Context in which this should occur */
533: {
534: register Var *v;
535:
536: v = VarFind (name, ctxt, (ctxt == VAR_GLOBAL) ? FIND_ENV : 0);
537:
1.26 espie 538: if (v == NULL) {
1.17 espie 539: (void)VarAdd(name, val, ctxt);
1.1 deraadt 540: } else {
1.23 espie 541: Buf_AddSpace(&(v->val));
542: Buf_AddString(&(v->val), val);
1.1 deraadt 543:
544: if (DEBUG(VAR)) {
1.23 espie 545: printf("%s:%s = %s\n", ctxt->name, name, VarValue(v));
1.1 deraadt 546: }
547:
548: }
549: }
550:
551: /*-
552: *-----------------------------------------------------------------------
553: * Var_Exists --
554: * See if the given variable exists.
555: *
556: * Results:
557: * TRUE if it does, FALSE if it doesn't
558: *
559: * Side Effects:
560: * None.
561: *
562: *-----------------------------------------------------------------------
563: */
564: Boolean
565: Var_Exists(name, ctxt)
566: char *name; /* Variable to find */
567: GNode *ctxt; /* Context in which to start search */
568: {
569: Var *v;
570:
571: v = VarFind(name, ctxt, FIND_CMD|FIND_GLOBAL|FIND_ENV);
572:
1.26 espie 573: if (v == NULL)
1.17 espie 574: return FALSE;
575: else
576: return TRUE;
1.1 deraadt 577: }
578:
579: /*-
580: *-----------------------------------------------------------------------
581: * Var_Value --
582: * Return the value of the named variable in the given context
583: *
584: * Results:
585: * The value if the variable exists, NULL if it doesn't
586: *
587: * Side Effects:
588: * None
589: *-----------------------------------------------------------------------
590: */
591: char *
1.18 espie 592: Var_Value(name, ctxt)
1.1 deraadt 593: char *name; /* name to find */
594: GNode *ctxt; /* context in which to search for it */
595: {
596: Var *v;
597:
1.21 espie 598: v = VarFind(name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1.26 espie 599: if (v != NULL)
1.23 espie 600: return VarValue(v);
1.21 espie 601: else
1.17 espie 602: return NULL;
1.1 deraadt 603: }
604:
605: /*-
606: *-----------------------------------------------------------------------
1.11 espie 607: * VarUppercase --
608: * Place the Upper cased word in the given buffer.
609: *
610: * Results:
611: * TRUE if characters were added to the buffer (a space needs to be
612: * added to the buffer before the next word).
613: *
614: * Side Effects:
615: * The word is added to the buffer.
616: *
617: *-----------------------------------------------------------------------
618: */
619: static Boolean
620: VarUppercase (word, addSpace, buf, dummy)
621: char *word; /* Word to Upper Case */
622: Boolean addSpace; /* True if need to add a space to the buffer
623: * before sticking in the head */
624: Buffer buf; /* Buffer in which to store it */
1.32 espie 625: void *dummy;
1.11 espie 626: {
627: size_t len = strlen(word);
628:
1.19 espie 629: if (addSpace)
630: Buf_AddSpace(buf);
631: while (len--)
1.16 espie 632: Buf_AddChar(buf, toupper(*word++));
1.11 espie 633: return (TRUE);
634: }
635:
636: /*-
637: *-----------------------------------------------------------------------
638: * VarLowercase --
639: * Place the Lower cased word in the given buffer.
640: *
641: * Results:
642: * TRUE if characters were added to the buffer (a space needs to be
643: * added to the buffer before the next word).
644: *
645: * Side Effects:
646: * The word is added to the buffer.
647: *
648: *-----------------------------------------------------------------------
649: */
650: static Boolean
651: VarLowercase (word, addSpace, buf, dummy)
652: char *word; /* Word to Lower Case */
653: Boolean addSpace; /* True if need to add a space to the buffer
654: * before sticking in the head */
655: Buffer buf; /* Buffer in which to store it */
1.32 espie 656: void *dummy;
1.11 espie 657: {
658: size_t len = strlen(word);
659:
1.19 espie 660: if (addSpace)
661: Buf_AddSpace(buf);
662: while (len--)
1.16 espie 663: Buf_AddChar(buf, tolower(*word++));
1.11 espie 664: return (TRUE);
665: }
666:
667: /*-
668: *-----------------------------------------------------------------------
1.1 deraadt 669: * VarHead --
670: * Remove the tail of the given word and place the result in the given
671: * buffer.
672: *
673: * Results:
674: * TRUE if characters were added to the buffer (a space needs to be
675: * added to the buffer before the next word).
676: *
677: * Side Effects:
678: * The trimmed word is added to the buffer.
679: *
680: *-----------------------------------------------------------------------
681: */
682: static Boolean
1.32 espie 683: VarHead(word, addSpace, buf, dummy)
1.1 deraadt 684: char *word; /* Word to trim */
685: Boolean addSpace; /* True if need to add a space to the buffer
686: * before sticking in the head */
687: Buffer buf; /* Buffer in which to store it */
1.32 espie 688: void *dummy;
1.1 deraadt 689: {
690: register char *slash;
691:
1.19 espie 692: slash = strrchr(word, '/');
693: if (slash != NULL) {
694: if (addSpace)
695: Buf_AddSpace(buf);
696: Buf_AddInterval(buf, word, slash);
1.1 deraadt 697: return (TRUE);
698: } else {
1.19 espie 699: /* If no directory part, give . (q.v. the POSIX standard) */
700: if (addSpace)
701: Buf_AddString(buf, " .");
702: else
1.16 espie 703: Buf_AddChar(buf, '.');
1.1 deraadt 704: }
705: return(dummy ? TRUE : TRUE);
706: }
707:
708: /*-
709: *-----------------------------------------------------------------------
710: * VarTail --
711: * Remove the head of the given word and place the result in the given
712: * buffer.
713: *
714: * Results:
715: * TRUE if characters were added to the buffer (a space needs to be
716: * added to the buffer before the next word).
717: *
718: * Side Effects:
719: * The trimmed word is added to the buffer.
720: *
721: *-----------------------------------------------------------------------
722: */
723: static Boolean
1.32 espie 724: VarTail(word, addSpace, buf, dummy)
1.1 deraadt 725: char *word; /* Word to trim */
726: Boolean addSpace; /* TRUE if need to stick a space in the
727: * buffer before adding the tail */
728: Buffer buf; /* Buffer in which to store it */
1.32 espie 729: void *dummy;
1.1 deraadt 730: {
731: register char *slash;
732:
1.19 espie 733: if (addSpace)
734: Buf_AddSpace(buf);
735: slash = strrchr(word, '/');
736: if (slash != NULL)
737: Buf_AddString(buf, slash+1);
738: else
739: Buf_AddString(buf, word);
1.1 deraadt 740: return (dummy ? TRUE : TRUE);
741: }
742:
743: /*-
744: *-----------------------------------------------------------------------
745: * VarSuffix --
746: * Place the suffix of the given word in the given buffer.
747: *
748: * Results:
749: * TRUE if characters were added to the buffer (a space needs to be
750: * added to the buffer before the next word).
751: *
752: * Side Effects:
753: * The suffix from the word is placed in the buffer.
754: *
755: *-----------------------------------------------------------------------
756: */
757: static Boolean
1.32 espie 758: VarSuffix(word, addSpace, buf, dummy)
1.1 deraadt 759: char *word; /* Word to trim */
760: Boolean addSpace; /* TRUE if need to add a space before placing
761: * the suffix in the buffer */
762: Buffer buf; /* Buffer in which to store it */
1.32 espie 763: void *dummy;
1.1 deraadt 764: {
1.19 espie 765: char *dot;
1.1 deraadt 766:
1.19 espie 767: dot = strrchr(word, '.');
768: if (dot != NULL) {
769: if (addSpace)
770: Buf_AddSpace(buf);
771: Buf_AddString(buf, dot+1);
1.1 deraadt 772: addSpace = TRUE;
773: }
774: return (dummy ? addSpace : addSpace);
775: }
776:
777: /*-
778: *-----------------------------------------------------------------------
779: * VarRoot --
780: * Remove the suffix of the given word and place the result in the
781: * buffer.
782: *
783: * Results:
784: * TRUE if characters were added to the buffer (a space needs to be
785: * added to the buffer before the next word).
786: *
787: * Side Effects:
788: * The trimmed word is added to the buffer.
789: *
790: *-----------------------------------------------------------------------
791: */
792: static Boolean
1.32 espie 793: VarRoot(word, addSpace, buf, dummy)
1.1 deraadt 794: char *word; /* Word to trim */
795: Boolean addSpace; /* TRUE if need to add a space to the buffer
796: * before placing the root in it */
797: Buffer buf; /* Buffer in which to store it */
1.32 espie 798: void *dummy;
1.1 deraadt 799: {
1.19 espie 800: char *dot;
1.1 deraadt 801:
1.19 espie 802: if (addSpace)
803: Buf_AddSpace(buf);
1.1 deraadt 804:
1.19 espie 805: dot = strrchr(word, '.');
806: if (dot != NULL)
807: Buf_AddInterval(buf, word, dot);
808: else
809: Buf_AddString(buf, word);
1.1 deraadt 810: return (dummy ? TRUE : TRUE);
811: }
812:
813: /*-
814: *-----------------------------------------------------------------------
815: * VarMatch --
816: * Place the word in the buffer if it matches the given pattern.
817: * Callback function for VarModify to implement the :M modifier.
1.5 millert 818: *
1.1 deraadt 819: * Results:
820: * TRUE if a space should be placed in the buffer before the next
821: * word.
822: *
823: * Side Effects:
824: * The word may be copied to the buffer.
825: *
826: *-----------------------------------------------------------------------
827: */
828: static Boolean
1.32 espie 829: VarMatch(word, addSpace, buf, pattern)
1.1 deraadt 830: char *word; /* Word to examine */
831: Boolean addSpace; /* TRUE if need to add a space to the
832: * buffer before adding the word, if it
833: * matches */
834: Buffer buf; /* Buffer in which to store it */
1.32 espie 835: void *pattern; /* Pattern the word must match */
1.1 deraadt 836: {
837: if (Str_Match(word, (char *) pattern)) {
1.19 espie 838: if (addSpace)
839: Buf_AddSpace(buf);
1.1 deraadt 840: addSpace = TRUE;
1.19 espie 841: Buf_AddString(buf, word);
1.1 deraadt 842: }
843: return(addSpace);
844: }
845:
1.4 briggs 846: #ifdef SYSVVARSUB
1.1 deraadt 847: /*-
848: *-----------------------------------------------------------------------
849: * VarSYSVMatch --
850: * Place the word in the buffer if it matches the given pattern.
851: * Callback function for VarModify to implement the System V %
852: * modifiers.
1.5 millert 853: *
1.1 deraadt 854: * Results:
855: * TRUE if a space should be placed in the buffer before the next
856: * word.
857: *
858: * Side Effects:
859: * The word may be copied to the buffer.
860: *
861: *-----------------------------------------------------------------------
862: */
863: static Boolean
1.32 espie 864: VarSYSVMatch(word, addSpace, buf, patp)
1.1 deraadt 865: char *word; /* Word to examine */
866: Boolean addSpace; /* TRUE if need to add a space to the
867: * buffer before adding the word, if it
868: * matches */
869: Buffer buf; /* Buffer in which to store it */
1.32 espie 870: void *patp; /* Pattern the word must match */
1.1 deraadt 871: {
872: int len;
873: char *ptr;
874: VarPattern *pat = (VarPattern *) patp;
875:
1.7 deraadt 876: if (*word) {
877: if (addSpace)
1.19 espie 878: Buf_AddSpace(buf);
1.7 deraadt 879:
880: addSpace = TRUE;
881:
882: if ((ptr = Str_SYSVMatch(word, pat->lhs, &len)) != NULL)
883: Str_SYSVSubst(buf, pat->rhs, ptr, len);
884: else
1.19 espie 885: Buf_AddString(buf, word);
1.7 deraadt 886: }
1.1 deraadt 887: return(addSpace);
888: }
1.4 briggs 889: #endif
1.1 deraadt 890:
891:
892: /*-
893: *-----------------------------------------------------------------------
894: * VarNoMatch --
895: * Place the word in the buffer if it doesn't match the given pattern.
896: * Callback function for VarModify to implement the :N modifier.
1.5 millert 897: *
1.1 deraadt 898: * Results:
899: * TRUE if a space should be placed in the buffer before the next
900: * word.
901: *
902: * Side Effects:
903: * The word may be copied to the buffer.
904: *
905: *-----------------------------------------------------------------------
906: */
907: static Boolean
1.32 espie 908: VarNoMatch(word, addSpace, buf, pattern)
1.1 deraadt 909: char *word; /* Word to examine */
910: Boolean addSpace; /* TRUE if need to add a space to the
911: * buffer before adding the word, if it
912: * matches */
913: Buffer buf; /* Buffer in which to store it */
1.32 espie 914: void *pattern; /* Pattern the word must match */
1.1 deraadt 915: {
916: if (!Str_Match(word, (char *) pattern)) {
1.19 espie 917: if (addSpace)
918: Buf_AddSpace(buf);
1.1 deraadt 919: addSpace = TRUE;
1.19 espie 920: Buf_AddString(buf, word);
1.1 deraadt 921: }
922: return(addSpace);
923: }
924:
925:
926: /*-
927: *-----------------------------------------------------------------------
928: * VarSubstitute --
929: * Perform a string-substitution on the given word, placing the
930: * result in the passed buffer.
931: *
932: * Results:
933: * TRUE if a space is needed before more characters are added.
934: *
935: * Side Effects:
936: * None.
937: *
938: *-----------------------------------------------------------------------
939: */
940: static Boolean
1.32 espie 941: VarSubstitute(word, addSpace, buf, patternp)
1.1 deraadt 942: char *word; /* Word to modify */
943: Boolean addSpace; /* True if space should be added before
944: * other characters */
945: Buffer buf; /* Buffer for result */
1.32 espie 946: void *patternp; /* Pattern for substitution */
1.1 deraadt 947: {
1.16 espie 948: register size_t wordLen; /* Length of word */
1.1 deraadt 949: register char *cp; /* General pointer */
950: VarPattern *pattern = (VarPattern *) patternp;
951:
952: wordLen = strlen(word);
1.6 millert 953: if ((pattern->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) !=
954: (VAR_SUB_ONE|VAR_SUB_MATCHED)) {
1.1 deraadt 955: /*
1.6 millert 956: * Still substituting -- break it down into simple anchored cases
1.1 deraadt 957: * and if none of them fits, perform the general substitution case.
958: */
959: if ((pattern->flags & VAR_MATCH_START) &&
960: (strncmp(word, pattern->lhs, pattern->leftLen) == 0)) {
961: /*
962: * Anchored at start and beginning of word matches pattern
963: */
964: if ((pattern->flags & VAR_MATCH_END) &&
965: (wordLen == pattern->leftLen)) {
966: /*
967: * Also anchored at end and matches to the end (word
968: * is same length as pattern) add space and rhs only
969: * if rhs is non-null.
970: */
971: if (pattern->rightLen != 0) {
1.19 espie 972: if (addSpace)
973: Buf_AddSpace(buf);
1.1 deraadt 974: addSpace = TRUE;
1.16 espie 975: Buf_AddChars(buf, pattern->rightLen, pattern->rhs);
1.1 deraadt 976: }
1.6 millert 977: pattern->flags |= VAR_SUB_MATCHED;
1.1 deraadt 978: } else if (pattern->flags & VAR_MATCH_END) {
979: /*
980: * Doesn't match to end -- copy word wholesale
981: */
982: goto nosub;
983: } else {
984: /*
985: * Matches at start but need to copy in trailing characters
986: */
987: if ((pattern->rightLen + wordLen - pattern->leftLen) != 0){
1.19 espie 988: if (addSpace)
989: Buf_AddSpace(buf);
1.1 deraadt 990: addSpace = TRUE;
991: }
1.16 espie 992: Buf_AddChars(buf, pattern->rightLen, pattern->rhs);
993: Buf_AddChars(buf, wordLen - pattern->leftLen,
994: word + pattern->leftLen);
1.6 millert 995: pattern->flags |= VAR_SUB_MATCHED;
1.1 deraadt 996: }
997: } else if (pattern->flags & VAR_MATCH_START) {
998: /*
999: * Had to match at start of word and didn't -- copy whole word.
1000: */
1001: goto nosub;
1002: } else if (pattern->flags & VAR_MATCH_END) {
1003: /*
1004: * Anchored at end, Find only place match could occur (leftLen
1005: * characters from the end of the word) and see if it does. Note
1006: * that because the $ will be left at the end of the lhs, we have
1007: * to use strncmp.
1008: */
1009: cp = word + (wordLen - pattern->leftLen);
1010: if ((cp >= word) &&
1011: (strncmp(cp, pattern->lhs, pattern->leftLen) == 0)) {
1012: /*
1013: * Match found. If we will place characters in the buffer,
1014: * add a space before hand as indicated by addSpace, then
1015: * stuff in the initial, unmatched part of the word followed
1016: * by the right-hand-side.
1017: */
1018: if (((cp - word) + pattern->rightLen) != 0) {
1.19 espie 1019: if (addSpace)
1020: Buf_AddSpace(buf);
1.1 deraadt 1021: addSpace = TRUE;
1022: }
1.19 espie 1023: Buf_AddInterval(buf, word, cp);
1.16 espie 1024: Buf_AddChars(buf, pattern->rightLen, pattern->rhs);
1.6 millert 1025: pattern->flags |= VAR_SUB_MATCHED;
1.1 deraadt 1026: } else {
1027: /*
1028: * Had to match at end and didn't. Copy entire word.
1029: */
1030: goto nosub;
1031: }
1032: } else {
1033: /*
1034: * Pattern is unanchored: search for the pattern in the word using
1035: * String_FindSubstring, copying unmatched portions and the
1036: * right-hand-side for each match found, handling non-global
1.5 millert 1037: * substitutions correctly, etc. When the loop is done, any
1.1 deraadt 1038: * remaining part of the word (word and wordLen are adjusted
1039: * accordingly through the loop) is copied straight into the
1040: * buffer.
1041: * addSpace is set FALSE as soon as a space is added to the
1042: * buffer.
1043: */
1044: register Boolean done;
1.16 espie 1045: size_t origSize;
1.1 deraadt 1046:
1047: done = FALSE;
1048: origSize = Buf_Size(buf);
1049: while (!done) {
1.15 espie 1050: cp = strstr(word, pattern->lhs);
1.1 deraadt 1051: if (cp != (char *)NULL) {
1052: if (addSpace && (((cp - word) + pattern->rightLen) != 0)){
1.19 espie 1053: Buf_AddSpace(buf);
1.1 deraadt 1054: addSpace = FALSE;
1055: }
1.19 espie 1056: Buf_AddInterval(buf, word, cp);
1.16 espie 1057: Buf_AddChars(buf, pattern->rightLen, pattern->rhs);
1.1 deraadt 1058: wordLen -= (cp - word) + pattern->leftLen;
1059: word = cp + pattern->leftLen;
1.5 millert 1060: if (wordLen == 0 || (pattern->flags & VAR_SUB_GLOBAL) == 0){
1.1 deraadt 1061: done = TRUE;
1062: }
1.6 millert 1063: pattern->flags |= VAR_SUB_MATCHED;
1.1 deraadt 1064: } else {
1065: done = TRUE;
1066: }
1067: }
1068: if (wordLen != 0) {
1.19 espie 1069: if (addSpace)
1070: Buf_AddSpace(buf);
1.16 espie 1071: Buf_AddChars(buf, wordLen, word);
1.1 deraadt 1072: }
1073: /*
1074: * If added characters to the buffer, need to add a space
1075: * before we add any more. If we didn't add any, just return
1076: * the previous value of addSpace.
1077: */
1.16 espie 1078: return (Buf_Size(buf) != origSize || addSpace);
1.1 deraadt 1079: }
1080: return (addSpace);
1081: }
1082: nosub:
1.19 espie 1083: if (addSpace)
1084: Buf_AddSpace(buf);
1.16 espie 1085: Buf_AddChars(buf, wordLen, word);
1.1 deraadt 1086: return(TRUE);
1087: }
1088:
1.6 millert 1089: #ifndef MAKE_BOOTSTRAP
1090: /*-
1091: *-----------------------------------------------------------------------
1092: * VarREError --
1093: * Print the error caused by a regcomp or regexec call.
1094: *
1095: * Results:
1096: * None.
1097: *
1098: * Side Effects:
1099: * An error gets printed.
1100: *
1101: *-----------------------------------------------------------------------
1102: */
1103: static void
1104: VarREError(err, pat, str)
1105: int err;
1106: regex_t *pat;
1107: const char *str;
1108: {
1109: char *errbuf;
1110: int errlen;
1111:
1112: errlen = regerror(err, pat, 0, 0);
1113: errbuf = emalloc(errlen);
1114: regerror(err, pat, errbuf, errlen);
1115: Error("%s: %s", str, errbuf);
1116: free(errbuf);
1117: }
1118:
1119: /*-
1120: *-----------------------------------------------------------------------
1121: * VarRESubstitute --
1122: * Perform a regex substitution on the given word, placing the
1123: * result in the passed buffer.
1124: *
1125: * Results:
1126: * TRUE if a space is needed before more characters are added.
1127: *
1128: * Side Effects:
1129: * None.
1130: *
1131: *-----------------------------------------------------------------------
1132: */
1133: static Boolean
1134: VarRESubstitute(word, addSpace, buf, patternp)
1135: char *word;
1136: Boolean addSpace;
1137: Buffer buf;
1.32 espie 1138: void *patternp;
1.6 millert 1139: {
1140: VarREPattern *pat;
1141: int xrv;
1142: char *wp;
1143: char *rp;
1144: int added;
1145:
1146: #define MAYBE_ADD_SPACE() \
1147: if (addSpace && !added) \
1.19 espie 1148: Buf_AddSpace(buf); \
1.6 millert 1149: added = 1
1150:
1151: added = 0;
1152: wp = word;
1153: pat = patternp;
1154:
1155: if ((pat->flags & (VAR_SUB_ONE|VAR_SUB_MATCHED)) ==
1156: (VAR_SUB_ONE|VAR_SUB_MATCHED))
1157: xrv = REG_NOMATCH;
1158: else {
1159: tryagain:
1160: xrv = regexec(&pat->re, wp, pat->nsub, pat->matches, 0);
1161: }
1162:
1163: switch (xrv) {
1164: case 0:
1165: pat->flags |= VAR_SUB_MATCHED;
1166: if (pat->matches[0].rm_so > 0) {
1167: MAYBE_ADD_SPACE();
1.16 espie 1168: Buf_AddChars(buf, pat->matches[0].rm_so, wp);
1.6 millert 1169: }
1170:
1171: for (rp = pat->replace; *rp; rp++) {
1172: if ((*rp == '\\') && ((rp[1] == '&') || (rp[1] == '\\'))) {
1173: MAYBE_ADD_SPACE();
1.16 espie 1174: Buf_AddChar(buf, rp[1]);
1.6 millert 1175: rp++;
1176: }
1177: else if ((*rp == '&') || ((*rp == '\\') && isdigit(rp[1]))) {
1178: int n;
1179: char *subbuf;
1180: int sublen;
1181: char errstr[3];
1182:
1183: if (*rp == '&') {
1184: n = 0;
1185: errstr[0] = '&';
1186: errstr[1] = '\0';
1187: } else {
1188: n = rp[1] - '0';
1189: errstr[0] = '\\';
1190: errstr[1] = rp[1];
1191: errstr[2] = '\0';
1192: rp++;
1193: }
1194:
1195: if (n > pat->nsub) {
1196: Error("No subexpression %s", &errstr[0]);
1197: subbuf = "";
1198: sublen = 0;
1199: } else if ((pat->matches[n].rm_so == -1) &&
1200: (pat->matches[n].rm_eo == -1)) {
1201: Error("No match for subexpression %s", &errstr[0]);
1202: subbuf = "";
1203: sublen = 0;
1204: } else {
1205: subbuf = wp + pat->matches[n].rm_so;
1206: sublen = pat->matches[n].rm_eo - pat->matches[n].rm_so;
1207: }
1208:
1209: if (sublen > 0) {
1210: MAYBE_ADD_SPACE();
1.16 espie 1211: Buf_AddChars(buf, sublen, subbuf);
1.6 millert 1212: }
1213: } else {
1214: MAYBE_ADD_SPACE();
1.16 espie 1215: Buf_AddChar(buf, *rp);
1.6 millert 1216: }
1217: }
1218: wp += pat->matches[0].rm_eo;
1219: if (pat->flags & VAR_SUB_GLOBAL)
1220: goto tryagain;
1221: if (*wp) {
1222: MAYBE_ADD_SPACE();
1.16 espie 1223: Buf_AddChars(buf, strlen(wp), wp);
1.6 millert 1224: }
1225: break;
1226: default:
1227: VarREError(xrv, &pat->re, "Unexpected regex error");
1228: /* fall through */
1229: case REG_NOMATCH:
1230: if (*wp) {
1231: MAYBE_ADD_SPACE();
1.16 espie 1232: Buf_AddChars(buf, strlen(wp), wp);
1.6 millert 1233: }
1234: break;
1235: }
1236: return(addSpace||added);
1237: }
1238: #endif
1239:
1.1 deraadt 1240: /*-
1241: *-----------------------------------------------------------------------
1242: * VarModify --
1243: * Modify each of the words of the passed string using the given
1244: * function. Used to implement all modifiers.
1245: *
1246: * Results:
1247: * A string of all the words modified appropriately.
1248: *
1249: * Side Effects:
1250: * None.
1251: *
1252: *-----------------------------------------------------------------------
1253: */
1254: static char *
1255: VarModify (str, modProc, datum)
1256: char *str; /* String whose words should be trimmed */
1257: /* Function to use to modify them */
1.32 espie 1258: Boolean (*modProc) __P((char *, Boolean, Buffer, void *));
1259: void *datum; /* Datum to pass it */
1.1 deraadt 1260: {
1.23 espie 1261: BUFFER buf; /* Buffer for the new string */
1.1 deraadt 1262: Boolean addSpace; /* TRUE if need to add a space to the
1263: * buffer before adding the trimmed
1264: * word */
1.10 espie 1265: char **av; /* word list */
1266: char *as; /* word list memory */
1.1 deraadt 1267: int ac, i;
1268:
1.23 espie 1269: Buf_Init(&buf, 0);
1.1 deraadt 1270: addSpace = FALSE;
1271:
1.10 espie 1272: av = brk_string(str, &ac, FALSE, &as);
1.1 deraadt 1273:
1.10 espie 1274: for (i = 0; i < ac; i++)
1.23 espie 1275: addSpace = (*modProc)(av[i], addSpace, &buf, datum);
1.5 millert 1276:
1.10 espie 1277: free(as);
1278: free(av);
1.23 espie 1279: return Buf_Retrieve(&buf);
1.1 deraadt 1280: }
1281:
1282: /*-
1283: *-----------------------------------------------------------------------
1.6 millert 1284: * VarGetPattern --
1285: * Pass through the tstr looking for 1) escaped delimiters,
1286: * '$'s and backslashes (place the escaped character in
1287: * uninterpreted) and 2) unescaped $'s that aren't before
1288: * the delimiter (expand the variable substitution).
1289: * Return the expanded string or NULL if the delimiter was missing
1.10 espie 1290: * If pattern is specified, handle escaped ampersands, and replace
1.6 millert 1291: * unescaped ampersands with the lhs of the pattern.
1292: *
1293: * Results:
1294: * A string of all the words modified appropriately.
1295: * If length is specified, return the string length of the buffer
1296: * If flags is specified and the last character of the pattern is a
1297: * $ set the VAR_MATCH_END bit of flags.
1298: *
1299: * Side Effects:
1300: * None.
1301: *-----------------------------------------------------------------------
1302: */
1303: static char *
1304: VarGetPattern(ctxt, err, tstr, delim, flags, length, pattern)
1305: GNode *ctxt;
1306: int err;
1307: char **tstr;
1308: int delim;
1309: int *flags;
1.16 espie 1310: size_t *length;
1.6 millert 1311: VarPattern *pattern;
1312: {
1313: char *cp;
1.23 espie 1314: BUFFER buf;
1.29 espie 1315: size_t junk;
1.23 espie 1316:
1317: Buf_Init(&buf, 0);
1.6 millert 1318: if (length == NULL)
1319: length = &junk;
1320:
1321: #define IS_A_MATCH(cp, delim) \
1322: ((cp[0] == '\\') && ((cp[1] == delim) || \
1323: (cp[1] == '\\') || (cp[1] == '$') || (pattern && (cp[1] == '&'))))
1324:
1325: /*
1326: * Skim through until the matching delimiter is found;
1327: * pick up variable substitutions on the way. Also allow
1328: * backslashes to quote the delimiter, $, and \, but don't
1329: * touch other backslashes.
1330: */
1331: for (cp = *tstr; *cp && (*cp != delim); cp++) {
1332: if (IS_A_MATCH(cp, delim)) {
1.23 espie 1333: Buf_AddChar(&buf, cp[1]);
1.6 millert 1334: cp++;
1335: } else if (*cp == '$') {
1336: if (cp[1] == delim) {
1337: if (flags == NULL)
1.23 espie 1338: Buf_AddChar(&buf, *cp);
1.6 millert 1339: else
1340: /*
1341: * Unescaped $ at end of pattern => anchor
1342: * pattern at end.
1343: */
1344: *flags |= VAR_MATCH_END;
1345: }
1346: else {
1347: char *cp2;
1.29 espie 1348: size_t len;
1.6 millert 1349: Boolean freeIt;
1350:
1351: /*
1352: * If unescaped dollar sign not before the
1353: * delimiter, assume it's a variable
1354: * substitution and recurse.
1355: */
1356: cp2 = Var_Parse(cp, ctxt, err, &len, &freeIt);
1.23 espie 1357: Buf_AddString(&buf, cp2);
1.6 millert 1358: if (freeIt)
1359: free(cp2);
1360: cp += len - 1;
1361: }
1362: }
1363: else if (pattern && *cp == '&')
1.23 espie 1364: Buf_AddChars(&buf, pattern->leftLen, pattern->lhs);
1.6 millert 1365: else
1.23 espie 1366: Buf_AddChar(&buf, *cp);
1.6 millert 1367: }
1368:
1369: if (*cp != delim) {
1370: *tstr = cp;
1371: *length = 0;
1372: return NULL;
1373: }
1374: else {
1375: *tstr = ++cp;
1.23 espie 1376: *length = Buf_Size(&buf);
1377: return Buf_Retrieve(&buf);
1.6 millert 1378: }
1379: }
1380:
1381: /*-
1382: *-----------------------------------------------------------------------
1383: * VarQuote --
1384: * Quote shell meta-characters in the string
1385: *
1386: * Results:
1387: * The quoted string
1388: *
1389: * Side Effects:
1390: * None.
1391: *
1392: *-----------------------------------------------------------------------
1393: */
1394: static char *
1395: VarQuote(str)
1396: char *str;
1397: {
1398:
1.23 espie 1399: BUFFER buf;
1.6 millert 1400: /* This should cover most shells :-( */
1401: static char meta[] = "\n \t'`\";&<>()|*?{}[]\\$!#^~";
1402:
1.23 espie 1403: Buf_Init(&buf, MAKE_BSIZE);
1.6 millert 1404: for (; *str; str++) {
1405: if (strchr(meta, *str) != NULL)
1.23 espie 1406: Buf_AddChar(&buf, '\\');
1407: Buf_AddChar(&buf, *str);
1.6 millert 1408: }
1.23 espie 1409: return Buf_Retrieve(&buf);
1.6 millert 1410: }
1411:
1412: /*-
1413: *-----------------------------------------------------------------------
1.1 deraadt 1414: * Var_Parse --
1415: * Given the start of a variable invocation, extract the variable
1416: * name and find its value, then modify it according to the
1417: * specification.
1418: *
1419: * Results:
1420: * The (possibly-modified) value of the variable or var_Error if the
1421: * specification is invalid. The length of the specification is
1422: * placed in *lengthPtr (for invalid specifications, this is just
1423: * 2...?).
1424: * A Boolean in *freePtr telling whether the returned string should
1425: * be freed by the caller.
1426: *
1427: * Side Effects:
1428: * None.
1429: *
1430: *-----------------------------------------------------------------------
1431: */
1432: char *
1433: Var_Parse (str, ctxt, err, lengthPtr, freePtr)
1434: char *str; /* The string to parse */
1435: GNode *ctxt; /* The context for the variable */
1436: Boolean err; /* TRUE if undefined variables are an error */
1.29 espie 1437: size_t *lengthPtr; /* OUT: The length of the specification */
1.1 deraadt 1438: Boolean *freePtr; /* OUT: TRUE if caller should free result */
1439: {
1440: register char *tstr; /* Pointer into str */
1441: Var *v; /* Variable in invocation */
1.6 millert 1442: char *cp; /* Secondary pointer into str (place marker
1.1 deraadt 1443: * for tstr) */
1444: Boolean haveModifier;/* TRUE if have modifiers for the variable */
1445: register char endc; /* Ending character when variable in parens
1446: * or braces */
1.2 deraadt 1447: register char startc=0; /* Starting character when variable in parens
1.1 deraadt 1448: * or braces */
1449: int cnt; /* Used to count brace pairs when variable in
1450: * in parens or braces */
1451: char *start;
1.6 millert 1452: char delim;
1.1 deraadt 1453: Boolean dynamic; /* TRUE if the variable is local and we're
1454: * expanding it in a non-local context. This
1455: * is done to support dynamic sources. The
1456: * result is just the invocation, unaltered */
1.5 millert 1457:
1.1 deraadt 1458: *freePtr = FALSE;
1459: dynamic = FALSE;
1460: start = str;
1.5 millert 1461:
1.1 deraadt 1462: if (str[1] != '(' && str[1] != '{') {
1463: /*
1464: * If it's not bounded by braces of some sort, life is much simpler.
1465: * We just need to check for the first character and return the
1466: * value if it exists.
1467: */
1468: char name[2];
1469:
1470: name[0] = str[1];
1471: name[1] = '\0';
1472:
1473: v = VarFind (name, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1.26 espie 1474: if (v == NULL) {
1.1 deraadt 1475: *lengthPtr = 2;
1.5 millert 1476:
1.1 deraadt 1477: if ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)) {
1478: /*
1479: * If substituting a local variable in a non-local context,
1480: * assume it's for dynamic source stuff. We have to handle
1481: * this specially and return the longhand for the variable
1482: * with the dollar sign escaped so it makes it back to the
1483: * caller. Only four of the local variables are treated
1484: * specially as they are the only four that will be set
1485: * when dynamic sources are expanded.
1486: */
1487: switch (str[1]) {
1488: case '@':
1489: return("$(.TARGET)");
1490: case '%':
1491: return("$(.ARCHIVE)");
1492: case '*':
1493: return("$(.PREFIX)");
1494: case '!':
1495: return("$(.MEMBER)");
1496: }
1497: }
1498: /*
1499: * Error
1500: */
1501: return (err ? var_Error : varNoError);
1502: } else {
1503: haveModifier = FALSE;
1504: tstr = &str[1];
1505: endc = str[1];
1506: }
1507: } else {
1508: startc = str[1];
1509: endc = startc == '(' ? ')' : '}';
1510:
1511: /*
1512: * Skip to the end character or a colon, whichever comes first.
1513: */
1514: for (tstr = str + 2;
1515: *tstr != '\0' && *tstr != endc && *tstr != ':';
1516: tstr++)
1517: {
1518: continue;
1519: }
1520: if (*tstr == ':') {
1521: haveModifier = TRUE;
1522: } else if (*tstr != '\0') {
1523: haveModifier = FALSE;
1524: } else {
1525: /*
1526: * If we never did find the end character, return NULL
1527: * right now, setting the length to be the distance to
1528: * the end of the string, since that's what make does.
1529: */
1530: *lengthPtr = tstr - str;
1531: return (var_Error);
1532: }
1533: *tstr = '\0';
1.5 millert 1534:
1.1 deraadt 1535: v = VarFind (str + 2, ctxt, FIND_ENV | FIND_GLOBAL | FIND_CMD);
1.26 espie 1536: if ((v == NULL) && (ctxt != VAR_CMD) && (ctxt != VAR_GLOBAL) &&
1.1 deraadt 1537: ((tstr-str) == 4) && (str[3] == 'F' || str[3] == 'D'))
1538: {
1539: /*
1540: * Check for bogus D and F forms of local variables since we're
1541: * in a local context and the name is the right length.
1542: */
1543: switch(str[2]) {
1544: case '@':
1545: case '%':
1546: case '*':
1547: case '!':
1548: case '>':
1549: case '<':
1550: {
1551: char vname[2];
1552: char *val;
1553:
1554: /*
1555: * Well, it's local -- go look for it.
1556: */
1557: vname[0] = str[2];
1558: vname[1] = '\0';
1559: v = VarFind(vname, ctxt, 0);
1.5 millert 1560:
1.26 espie 1561: if (v != NULL) {
1.1 deraadt 1562: /*
1563: * No need for nested expansion or anything, as we're
1564: * the only one who sets these things and we sure don't
1565: * but nested invocations in them...
1566: */
1.23 espie 1567: val = VarValue(v);
1.5 millert 1568:
1.1 deraadt 1569: if (str[3] == 'D') {
1.30 espie 1570: val = VarModify(val, VarHead, NULL);
1.1 deraadt 1571: } else {
1.30 espie 1572: val = VarModify(val, VarTail, NULL);
1.1 deraadt 1573: }
1574: /*
1575: * Resulting string is dynamically allocated, so
1576: * tell caller to free it.
1577: */
1578: *freePtr = TRUE;
1579: *lengthPtr = tstr-start+1;
1580: *tstr = endc;
1581: return(val);
1582: }
1583: break;
1584: }
1585: }
1586: }
1.5 millert 1587:
1.26 espie 1588: if (v == NULL) {
1.1 deraadt 1589: if ((((tstr-str) == 3) ||
1590: ((((tstr-str) == 4) && (str[3] == 'F' ||
1591: str[3] == 'D')))) &&
1592: ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
1593: {
1594: /*
1595: * If substituting a local variable in a non-local context,
1596: * assume it's for dynamic source stuff. We have to handle
1597: * this specially and return the longhand for the variable
1598: * with the dollar sign escaped so it makes it back to the
1599: * caller. Only four of the local variables are treated
1600: * specially as they are the only four that will be set
1601: * when dynamic sources are expanded.
1602: */
1603: switch (str[2]) {
1604: case '@':
1605: case '%':
1606: case '*':
1607: case '!':
1608: dynamic = TRUE;
1609: break;
1610: }
1611: } else if (((tstr-str) > 4) && (str[2] == '.') &&
1612: isupper((unsigned char) str[3]) &&
1613: ((ctxt == VAR_CMD) || (ctxt == VAR_GLOBAL)))
1614: {
1615: int len;
1.5 millert 1616:
1.1 deraadt 1617: len = (tstr-str) - 3;
1618: if ((strncmp(str+2, ".TARGET", len) == 0) ||
1619: (strncmp(str+2, ".ARCHIVE", len) == 0) ||
1620: (strncmp(str+2, ".PREFIX", len) == 0) ||
1621: (strncmp(str+2, ".MEMBER", len) == 0))
1622: {
1623: dynamic = TRUE;
1624: }
1625: }
1.5 millert 1626:
1.1 deraadt 1627: if (!haveModifier) {
1628: /*
1629: * No modifiers -- have specification length so we can return
1630: * now.
1631: */
1632: *lengthPtr = tstr - start + 1;
1633: *tstr = endc;
1634: if (dynamic) {
1635: str = emalloc(*lengthPtr + 1);
1636: strncpy(str, start, *lengthPtr);
1637: str[*lengthPtr] = '\0';
1638: *freePtr = TRUE;
1639: return(str);
1640: } else {
1641: return (err ? var_Error : varNoError);
1642: }
1643: } else {
1644: /*
1645: * Still need to get to the end of the variable specification,
1646: * so kludge up a Var structure for the modifications
1647: */
1648: v = (Var *) emalloc(sizeof(Var));
1649: v->name = &str[1];
1.23 espie 1650: Buf_Init(&(v->val), 1);
1.1 deraadt 1651: v->flags = VAR_JUNK;
1652: }
1653: }
1654: }
1655:
1656: if (v->flags & VAR_IN_USE) {
1657: Fatal("Variable %s is recursive.", v->name);
1658: /*NOTREACHED*/
1659: } else {
1660: v->flags |= VAR_IN_USE;
1661: }
1662: /*
1663: * Before doing any modification, we have to make sure the value
1664: * has been fully expanded. If it looks like recursion might be
1665: * necessary (there's a dollar sign somewhere in the variable's value)
1666: * we just call Var_Subst to do any other substitutions that are
1667: * necessary. Note that the value returned by Var_Subst will have
1668: * been dynamically-allocated, so it will need freeing when we
1669: * return.
1670: */
1.23 espie 1671: str = VarValue(v);
1.1 deraadt 1672: if (strchr (str, '$') != (char *)NULL) {
1.24 espie 1673: str = Var_Subst(str, ctxt, err);
1.1 deraadt 1674: *freePtr = TRUE;
1675: }
1.5 millert 1676:
1.1 deraadt 1677: v->flags &= ~VAR_IN_USE;
1.5 millert 1678:
1.1 deraadt 1679: /*
1680: * Now we need to apply any modifiers the user wants applied.
1681: * These are:
1682: * :M<pattern> words which match the given <pattern>.
1683: * <pattern> is of the standard file
1684: * wildcarding form.
1685: * :S<d><pat1><d><pat2><d>[g]
1686: * Substitute <pat2> for <pat1> in the value
1.6 millert 1687: * :C<d><pat1><d><pat2><d>[g]
1688: * Substitute <pat2> for regex <pat1> in the value
1.1 deraadt 1689: * :H Substitute the head of each word
1690: * :T Substitute the tail of each word
1691: * :E Substitute the extension (minus '.') of
1692: * each word
1693: * :R Substitute the root of each word
1694: * (pathname minus the suffix).
1695: * :lhs=rhs Like :S, but the rhs goes to the end of
1696: * the invocation.
1697: */
1698: if ((str != (char *)NULL) && haveModifier) {
1699: /*
1700: * Skip initial colon while putting it back.
1701: */
1702: *tstr++ = ':';
1703: while (*tstr != endc) {
1704: char *newStr; /* New value to return */
1705: char termc; /* Character which terminated scan */
1.5 millert 1706:
1.1 deraadt 1707: if (DEBUG(VAR)) {
1708: printf("Applying :%c to \"%s\"\n", *tstr, str);
1709: }
1710: switch (*tstr) {
1711: case 'N':
1712: case 'M':
1713: {
1714: char *pattern;
1715: char *cp2;
1716: Boolean copy;
1717:
1718: copy = FALSE;
1719: for (cp = tstr + 1;
1720: *cp != '\0' && *cp != ':' && *cp != endc;
1721: cp++)
1722: {
1723: if (*cp == '\\' && (cp[1] == ':' || cp[1] == endc)){
1724: copy = TRUE;
1725: cp++;
1726: }
1727: }
1728: termc = *cp;
1729: *cp = '\0';
1730: if (copy) {
1731: /*
1732: * Need to compress the \:'s out of the pattern, so
1733: * allocate enough room to hold the uncompressed
1734: * pattern (note that cp started at tstr+1, so
1735: * cp - tstr takes the null byte into account) and
1736: * compress the pattern into the space.
1737: */
1738: pattern = emalloc(cp - tstr);
1739: for (cp2 = pattern, cp = tstr + 1;
1740: *cp != '\0';
1741: cp++, cp2++)
1742: {
1743: if ((*cp == '\\') &&
1744: (cp[1] == ':' || cp[1] == endc)) {
1745: cp++;
1746: }
1747: *cp2 = *cp;
1748: }
1749: *cp2 = '\0';
1750: } else {
1751: pattern = &tstr[1];
1752: }
1753: if (*tstr == 'M' || *tstr == 'm') {
1.30 espie 1754: newStr = VarModify(str, VarMatch, pattern);
1.1 deraadt 1755: } else {
1.30 espie 1756: newStr = VarModify(str, VarNoMatch, pattern);
1.1 deraadt 1757: }
1758: if (copy) {
1759: free(pattern);
1760: }
1761: break;
1762: }
1763: case 'S':
1764: {
1765: VarPattern pattern;
1766:
1767: pattern.flags = 0;
1768: delim = tstr[1];
1769: tstr += 2;
1.6 millert 1770:
1.1 deraadt 1771: /*
1772: * If pattern begins with '^', it is anchored to the
1773: * start of the word -- skip over it and flag pattern.
1774: */
1775: if (*tstr == '^') {
1776: pattern.flags |= VAR_MATCH_START;
1777: tstr += 1;
1778: }
1779:
1.6 millert 1780: cp = tstr;
1781: if ((pattern.lhs = VarGetPattern(ctxt, err, &cp, delim,
1782: &pattern.flags, &pattern.leftLen, NULL)) == NULL)
1783: goto cleanup;
1784:
1785: if ((pattern.rhs = VarGetPattern(ctxt, err, &cp, delim,
1786: NULL, &pattern.rightLen, &pattern)) == NULL)
1787: goto cleanup;
1.5 millert 1788:
1.1 deraadt 1789: /*
1.6 millert 1790: * Check for global substitution. If 'g' after the final
1791: * delimiter, substitution is global and is marked that
1792: * way.
1.1 deraadt 1793: */
1.6 millert 1794: for (;; cp++) {
1795: switch (*cp) {
1796: case 'g':
1797: pattern.flags |= VAR_SUB_GLOBAL;
1798: continue;
1799: case '1':
1800: pattern.flags |= VAR_SUB_ONE;
1801: continue;
1.1 deraadt 1802: }
1.6 millert 1803: break;
1.1 deraadt 1804: }
1805:
1.6 millert 1806: termc = *cp;
1.30 espie 1807: newStr = VarModify(str, VarSubstitute, &pattern);
1.5 millert 1808:
1.1 deraadt 1809: /*
1.6 millert 1810: * Free the two strings.
1.1 deraadt 1811: */
1.6 millert 1812: free(pattern.lhs);
1813: free(pattern.rhs);
1814: break;
1815: }
1816: #ifndef MAKE_BOOTSTRAP
1817: case 'C':
1818: {
1819: VarREPattern pattern;
1820: char *re;
1821: int error;
1822:
1823: pattern.flags = 0;
1824: delim = tstr[1];
1825: tstr += 2;
1.1 deraadt 1826:
1.6 millert 1827: cp = tstr;
1.1 deraadt 1828:
1.6 millert 1829: if ((re = VarGetPattern(ctxt, err, &cp, delim, NULL,
1830: NULL, NULL)) == NULL)
1831: goto cleanup;
1832:
1833: if ((pattern.replace = VarGetPattern(ctxt, err, &cp,
1834: delim, NULL, NULL, NULL)) == NULL) {
1835: free(re);
1836: goto cleanup;
1837: }
1.5 millert 1838:
1.6 millert 1839: for (;; cp++) {
1840: switch (*cp) {
1841: case 'g':
1842: pattern.flags |= VAR_SUB_GLOBAL;
1843: continue;
1844: case '1':
1845: pattern.flags |= VAR_SUB_ONE;
1846: continue;
1.1 deraadt 1847: }
1.6 millert 1848: break;
1.1 deraadt 1849: }
1850:
1.6 millert 1851: termc = *cp;
1.5 millert 1852:
1.6 millert 1853: error = regcomp(&pattern.re, re, REG_EXTENDED);
1854: free(re);
1855: if (error) {
1.1 deraadt 1856: *lengthPtr = cp - start + 1;
1.6 millert 1857: VarREError(error, &pattern.re, "RE substitution error");
1858: free(pattern.replace);
1.1 deraadt 1859: return (var_Error);
1860: }
1861:
1.6 millert 1862: pattern.nsub = pattern.re.re_nsub + 1;
1863: if (pattern.nsub < 1)
1864: pattern.nsub = 1;
1865: if (pattern.nsub > 10)
1866: pattern.nsub = 10;
1867: pattern.matches = emalloc(pattern.nsub *
1868: sizeof(regmatch_t));
1.30 espie 1869: newStr = VarModify(str, VarRESubstitute, &pattern);
1.6 millert 1870: regfree(&pattern.re);
1871: free(pattern.replace);
1872: free(pattern.matches);
1.1 deraadt 1873: break;
1874: }
1.6 millert 1875: #endif
1876: case 'Q':
1877: if (tstr[1] == endc || tstr[1] == ':') {
1878: newStr = VarQuote (str);
1879: cp = tstr + 1;
1880: termc = *cp;
1881: break;
1882: }
1883: /*FALLTHRU*/
1.1 deraadt 1884: case 'T':
1885: if (tstr[1] == endc || tstr[1] == ':') {
1.30 espie 1886: newStr = VarModify(str, VarTail, NULL);
1.1 deraadt 1887: cp = tstr + 1;
1888: termc = *cp;
1889: break;
1890: }
1891: /*FALLTHRU*/
1892: case 'H':
1893: if (tstr[1] == endc || tstr[1] == ':') {
1.30 espie 1894: newStr = VarModify(str, VarHead, NULL);
1.1 deraadt 1895: cp = tstr + 1;
1896: termc = *cp;
1897: break;
1898: }
1899: /*FALLTHRU*/
1900: case 'E':
1901: if (tstr[1] == endc || tstr[1] == ':') {
1.30 espie 1902: newStr = VarModify(str, VarSuffix, NULL);
1.1 deraadt 1903: cp = tstr + 1;
1904: termc = *cp;
1905: break;
1906: }
1907: /*FALLTHRU*/
1908: case 'R':
1909: if (tstr[1] == endc || tstr[1] == ':') {
1.30 espie 1910: newStr = VarModify(str, VarRoot, NULL);
1.11 espie 1911: cp = tstr + 1;
1912: termc = *cp;
1913: break;
1914: }
1915: /*FALLTHRU*/
1916: case 'U':
1917: if (tstr[1] == endc || tstr[1] == ':') {
1.30 espie 1918: newStr = VarModify(str, VarUppercase, NULL);
1.11 espie 1919: cp = tstr + 1;
1920: termc = *cp;
1921: break;
1922: }
1923: /*FALLTHRU*/
1924: case 'L':
1925: if (tstr[1] == endc || tstr[1] == ':') {
1.30 espie 1926: newStr = VarModify(str, VarLowercase, NULL);
1.1 deraadt 1927: cp = tstr + 1;
1928: termc = *cp;
1929: break;
1930: }
1931: /*FALLTHRU*/
1.4 briggs 1932: #ifdef SUNSHCMD
1933: case 's':
1934: if (tstr[1] == 'h' && (tstr[2] == endc || tstr[2] == ':')) {
1935: char *err;
1936: newStr = Cmd_Exec (str, &err);
1937: if (err)
1938: Error (err, str);
1939: cp = tstr + 2;
1940: termc = *cp;
1941: break;
1942: }
1943: /*FALLTHRU*/
1944: #endif
1945: default:
1946: {
1947: #ifdef SYSVVARSUB
1.1 deraadt 1948: /*
1949: * This can either be a bogus modifier or a System-V
1950: * substitution command.
1951: */
1952: VarPattern pattern;
1953: Boolean eqFound;
1.5 millert 1954:
1.1 deraadt 1955: pattern.flags = 0;
1956: eqFound = FALSE;
1957: /*
1958: * First we make a pass through the string trying
1959: * to verify it is a SYSV-make-style translation:
1960: * it must be: <string1>=<string2>)
1961: */
1962: cp = tstr;
1963: cnt = 1;
1964: while (*cp != '\0' && cnt) {
1965: if (*cp == '=') {
1966: eqFound = TRUE;
1967: /* continue looking for endc */
1968: }
1969: else if (*cp == endc)
1970: cnt--;
1971: else if (*cp == startc)
1972: cnt++;
1973: if (cnt)
1974: cp++;
1975: }
1976: if (*cp == endc && eqFound) {
1.5 millert 1977:
1.1 deraadt 1978: /*
1979: * Now we break this sucker into the lhs and
1980: * rhs. We must null terminate them of course.
1981: */
1982: for (cp = tstr; *cp != '='; cp++)
1983: continue;
1984: pattern.lhs = tstr;
1985: pattern.leftLen = cp - tstr;
1986: *cp++ = '\0';
1.5 millert 1987:
1.1 deraadt 1988: pattern.rhs = cp;
1989: cnt = 1;
1990: while (cnt) {
1991: if (*cp == endc)
1992: cnt--;
1993: else if (*cp == startc)
1994: cnt++;
1995: if (cnt)
1996: cp++;
1997: }
1998: pattern.rightLen = cp - pattern.rhs;
1999: *cp = '\0';
1.5 millert 2000:
1.1 deraadt 2001: /*
2002: * SYSV modifications happen through the whole
2003: * string. Note the pattern is anchored at the end.
2004: */
1.30 espie 2005: newStr = VarModify(str, VarSYSVMatch, &pattern);
1.1 deraadt 2006:
2007: /*
2008: * Restore the nulled characters
2009: */
2010: pattern.lhs[pattern.leftLen] = '=';
2011: pattern.rhs[pattern.rightLen] = endc;
2012: termc = endc;
1.4 briggs 2013: } else
2014: #endif
2015: {
1.1 deraadt 2016: Error ("Unknown modifier '%c'\n", *tstr);
2017: for (cp = tstr+1;
2018: *cp != ':' && *cp != endc && *cp != '\0';
1.5 millert 2019: cp++)
1.1 deraadt 2020: continue;
2021: termc = *cp;
2022: newStr = var_Error;
2023: }
2024: }
2025: }
2026: if (DEBUG(VAR)) {
2027: printf("Result is \"%s\"\n", newStr);
2028: }
1.5 millert 2029:
1.1 deraadt 2030: if (*freePtr) {
2031: free (str);
2032: }
2033: str = newStr;
2034: if (str != var_Error) {
2035: *freePtr = TRUE;
2036: } else {
2037: *freePtr = FALSE;
2038: }
2039: if (termc == '\0') {
2040: Error("Unclosed variable specification for %s", v->name);
2041: } else if (termc == ':') {
2042: *cp++ = termc;
2043: } else {
2044: *cp = termc;
2045: }
2046: tstr = cp;
2047: }
2048: *lengthPtr = tstr - start + 1;
2049: } else {
2050: *lengthPtr = tstr - start + 1;
2051: *tstr = endc;
2052: }
1.5 millert 2053:
1.17 espie 2054: if (v->flags & VAR_JUNK) {
1.1 deraadt 2055: /*
2056: * Perform any free'ing needed and set *freePtr to FALSE so the caller
2057: * doesn't try to free a static pointer.
2058: */
2059: if (*freePtr) {
2060: free(str);
2061: }
2062: *freePtr = FALSE;
1.23 espie 2063: Buf_Destroy(&(v->val));
2064: free(v);
1.1 deraadt 2065: if (dynamic) {
2066: str = emalloc(*lengthPtr + 1);
2067: strncpy(str, start, *lengthPtr);
2068: str[*lengthPtr] = '\0';
2069: *freePtr = TRUE;
2070: } else {
1.12 espie 2071: str = err ? var_Error : varNoError;
1.1 deraadt 2072: }
2073: }
2074: return (str);
1.6 millert 2075:
2076: cleanup:
2077: *lengthPtr = cp - start + 1;
2078: if (*freePtr)
2079: free(str);
2080: Error("Unclosed substitution for %s (%c missing)",
2081: v->name, delim);
2082: return (var_Error);
1.1 deraadt 2083: }
2084:
2085: /*-
2086: *-----------------------------------------------------------------------
2087: * Var_Subst --
1.24 espie 2088: * Substitute for all variables in a string in a context
1.1 deraadt 2089: * If undefErr is TRUE, Parse_Error will be called when an undefined
2090: * variable is encountered.
2091: *
2092: * Results:
2093: * The resulting string.
2094: *
2095: * Side Effects:
2096: * None. The old string must be freed by the caller
2097: *-----------------------------------------------------------------------
2098: */
2099: char *
1.24 espie 2100: Var_Subst(str, ctxt, undefErr)
1.1 deraadt 2101: char *str; /* the string in which to substitute */
2102: GNode *ctxt; /* the context wherein to find variables */
2103: Boolean undefErr; /* TRUE if undefineds are an error */
2104: {
1.24 espie 2105: BUFFER buf; /* Buffer for forming things */
1.1 deraadt 2106: static Boolean errorReported; /* Set true if an error has already
2107: * been reported to prevent a plethora
2108: * of messages when recursing */
2109:
1.23 espie 2110: Buf_Init(&buf, MAKE_BSIZE);
1.1 deraadt 2111: errorReported = FALSE;
2112:
1.24 espie 2113: for (;;) {
2114: char *val; /* Value to substitute for a variable */
2115: size_t length; /* Length of the variable invocation */
2116: Boolean doFree; /* Set true if val should be freed */
2117: const char *cp;
2118:
2119: /* copy uninteresting stuff */
2120: for (cp = str; *str != '\0' && *str != '$'; str++)
2121: ;
2122: Buf_AddInterval(&buf, cp, str);
2123: if (*str == '\0')
2124: break;
2125: if (str[1] == '$') {
2126: /* A dollar sign may be escaped with another dollar sign. */
2127: Buf_AddChar(&buf, '$');
2128: str += 2;
2129: continue;
2130: }
2131: val = Var_Parse(str, ctxt, undefErr, &length, &doFree);
2132: /* When we come down here, val should either point to the
2133: * value of this variable, suitably modified, or be NULL.
2134: * Length should be the total length of the potential
2135: * variable invocation (from $ to end character...) */
2136: if (val == var_Error || val == varNoError) {
2137: /* If performing old-time variable substitution, skip over
2138: * the variable and continue with the substitution. Otherwise,
2139: * store the dollar sign and advance str so we continue with
2140: * the string... */
2141: if (oldVars) {
2142: str += length;
2143: } else if (undefErr) {
2144: /* If variable is undefined, complain and skip the
2145: * variable. The complaint will stop us from doing anything
2146: * when the file is parsed. */
2147: if (!errorReported) {
2148: Parse_Error(PARSE_FATAL,
2149: "Undefined variable \"%.*s\"",length,str);
2150: }
2151: str += length;
2152: errorReported = TRUE;
2153: } else {
2154: Buf_AddChar(&buf, *str);
2155: str += 1;
2156: }
2157: } else {
2158: /* We've now got a variable structure to store in. But first,
2159: * advance the string pointer. */
2160: str += length;
2161:
2162: /* Copy all the characters from the variable value straight
2163: * into the new string. */
2164: Buf_AddString(&buf, val);
2165: if (doFree)
2166: free(val);
2167: }
2168: }
2169: return Buf_Retrieve(&buf);
2170: }
1.1 deraadt 2171:
1.24 espie 2172: /*-
2173: *-----------------------------------------------------------------------
2174: * Var_SubstVar --
2175: * Substitute for one variable in the given string in the given context
2176: * If undefErr is TRUE, Parse_Error will be called when an undefined
2177: * variable is encountered.
2178: *
2179: * Side Effects:
2180: * Append the result to the buffer
2181: *-----------------------------------------------------------------------
2182: */
2183: void
2184: Var_SubstVar(buf, str, var, ctxt)
2185: Buffer buf; /* Where to store the result */
2186: char *str; /* The string in which to substitute */
2187: const char *var; /* Named variable */
2188: GNode *ctxt; /* The context wherein to find variables */
2189: {
2190: char *val; /* Value substituted for a variable */
2191: size_t length; /* Length of the variable invocation */
2192: Boolean doFree; /* Set true if val should be freed */
2193:
2194: for (;;) {
2195: const char *cp;
2196: /* copy uninteresting stuff */
2197: for (cp = str; *str != '\0' && *str != '$'; str++)
2198: ;
2199: Buf_AddInterval(buf, cp, str);
2200: if (*str == '\0')
2201: break;
2202: if (str[1] == '$') {
2203: Buf_AddString(buf, "$$");
2204: str += 2;
2205: continue;
2206: }
2207: if (str[1] != '(' && str[1] != '{') {
2208: if (str[1] != *var || var[1] != '\0') {
2209: Buf_AddChars(buf, 2, str);
2210: str += 2;
1.1 deraadt 2211: continue;
1.24 espie 2212: }
1.1 deraadt 2213: } else {
1.24 espie 2214: char *p;
2215: char endc;
1.1 deraadt 2216:
1.24 espie 2217: if (str[1] == '(')
2218: endc = ')';
2219: else if (str[1] == '{')
2220: endc = '}';
2221:
2222: /* Find the end of the variable specification. */
2223: p = str+2;
2224: while (*p != '\0' && *p != ':' && *p != endc && *p != '$')
2225: p++;
2226: /* A variable inside the variable. We don't know how to
2227: * expand the external variable at this point, so we try
2228: * again with the nested variable. */
2229: if (*p == '$') {
2230: Buf_AddInterval(buf, str, p);
2231: str = p;
2232: continue;
1.1 deraadt 2233: }
1.5 millert 2234:
1.24 espie 2235: if (strncmp(var, str + 2, p - str - 2) != 0 ||
2236: var[p - str - 2] != '\0') {
2237: /* Not the variable we want to expand. */
2238: Buf_AddInterval(buf, str, p);
2239: str = p;
2240: continue;
2241: }
1.1 deraadt 2242: }
1.24 espie 2243: /* okay, so we've found the variable we want to expand. */
2244: val = Var_Parse(str, ctxt, FALSE, &length, &doFree);
2245: /* We've now got a variable structure to store in. But first,
2246: * advance the string pointer. */
2247: str += length;
2248:
2249: /* Copy all the characters from the variable value straight
2250: * into the new string. */
2251: Buf_AddString(buf, val);
2252: if (doFree)
2253: free(val);
1.1 deraadt 2254: }
2255: }
2256:
2257: /*-
2258: *-----------------------------------------------------------------------
2259: * Var_GetTail --
2260: * Return the tail from each of a list of words. Used to set the
2261: * System V local variables.
2262: *
2263: * Results:
2264: * The resulting string.
2265: *
2266: * Side Effects:
2267: * None.
2268: *
2269: *-----------------------------------------------------------------------
2270: */
2271: char *
2272: Var_GetTail(file)
2273: char *file; /* Filename to modify */
2274: {
1.30 espie 2275: return VarModify(file, VarTail, NULL);
1.1 deraadt 2276: }
2277:
2278: /*-
2279: *-----------------------------------------------------------------------
2280: * Var_GetHead --
2281: * Find the leading components of a (list of) filename(s).
2282: * XXX: VarHead does not replace foo by ., as (sun) System V make
2283: * does.
2284: *
2285: * Results:
2286: * The leading components.
2287: *
2288: * Side Effects:
2289: * None.
2290: *
2291: *-----------------------------------------------------------------------
2292: */
2293: char *
2294: Var_GetHead(file)
2295: char *file; /* Filename to manipulate */
2296: {
1.30 espie 2297: return VarModify(file, VarHead, NULL);
1.1 deraadt 2298: }
2299:
2300: /*-
2301: *-----------------------------------------------------------------------
2302: * Var_Init --
2303: * Initialize the module
2304: *
2305: * Side Effects:
1.5 millert 2306: * The VAR_CMD and VAR_GLOBAL contexts are created
1.1 deraadt 2307: *-----------------------------------------------------------------------
2308: */
2309: void
1.33 ! espie 2310: Var_Init()
1.1 deraadt 2311: {
1.17 espie 2312: VAR_GLOBAL = Targ_NewGN("Global");
2313: VAR_CMD = Targ_NewGN("Command");
2314: VAR_ENV = Targ_NewGN("Environment");
1.33 ! espie 2315: Lst_Init(&allVars);
1.1 deraadt 2316:
2317: }
2318:
2319:
2320: void
1.33 ! espie 2321: Var_End()
1.1 deraadt 2322: {
1.33 ! espie 2323: Lst_Destroy(&allVars, VarDelete);
1.1 deraadt 2324: }
1.5 millert 2325:
1.1 deraadt 2326:
2327: /****************** PRINT DEBUGGING INFO *****************/
1.31 espie 2328: static void
2329: VarPrintVar(vp)
1.32 espie 2330: void *vp;
1.1 deraadt 2331: {
1.31 espie 2332: Var *v = (Var *)vp;
2333:
1.23 espie 2334: printf("%-16s = %s\n", v->name, VarValue(v));
1.1 deraadt 2335: }
2336:
2337: /*-
2338: *-----------------------------------------------------------------------
2339: * Var_Dump --
2340: * print all variables in a context
2341: *-----------------------------------------------------------------------
2342: */
2343: void
2344: Var_Dump (ctxt)
2345: GNode *ctxt;
2346: {
1.33 ! espie 2347: Lst_Every(&ctxt->context, VarPrintVar);
1.1 deraadt 2348: }