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