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