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