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