Annotation of src/usr.bin/make/parse.c, Revision 1.7
1.7 ! niklas 1: /* $OpenBSD$ */
! 2: /* $NetBSD: parse.c,v 1.22 1996/03/15 21:52:41 christos Exp $ */
1.1 deraadt 3:
4: /*
5: * Copyright (c) 1988, 1989, 1990 The Regents of the University of California.
6: * Copyright (c) 1988, 1989 by Adam de Boor
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
44: static char sccsid[] = "@(#)parse.c 5.18 (Berkeley) 2/19/91";
45: #else
1.7 ! niklas 46: static char rcsid[] = "$NetBSD: parse.c,v 1.22 1996/03/15 21:52:41 christos Exp $";
1.1 deraadt 47: #endif
48: #endif /* not lint */
49:
50: /*-
51: * parse.c --
52: * Functions to parse a makefile.
53: *
54: * One function, Parse_Init, must be called before any functions
55: * in this module are used. After that, the function Parse_File is the
56: * main entry point and controls most of the other functions in this
57: * module.
58: *
59: * Most important structures are kept in Lsts. Directories for
60: * the #include "..." function are kept in the 'parseIncPath' Lst, while
61: * those for the #include <...> are kept in the 'sysIncPath' Lst. The
62: * targets currently being defined are kept in the 'targets' Lst.
63: *
64: * The variables 'fname' and 'lineno' are used to track the name
65: * of the current file and the line number in that file so that error
66: * messages can be more meaningful.
67: *
68: * Interface:
69: * Parse_Init Initialization function which must be
70: * called before anything else in this module
71: * is used.
72: *
73: * Parse_End Cleanup the module
74: *
75: * Parse_File Function used to parse a makefile. It must
76: * be given the name of the file, which should
77: * already have been opened, and a function
78: * to call to read a character from the file.
79: *
80: * Parse_IsVar Returns TRUE if the given line is a
81: * variable assignment. Used by MainParseArgs
82: * to determine if an argument is a target
83: * or a variable assignment. Used internally
84: * for pretty much the same thing...
85: *
86: * Parse_Error Function called when an error occurs in
87: * parsing. Used by the variable and
88: * conditional modules.
89: * Parse_MainName Returns a Lst of the main target to create.
90: */
91:
92: #if __STDC__
93: #include <stdarg.h>
94: #else
95: #include <varargs.h>
96: #endif
97: #include <stdio.h>
98: #include <ctype.h>
99: #include <errno.h>
100: #include <sys/wait.h>
101: #include "make.h"
102: #include "hash.h"
103: #include "dir.h"
104: #include "job.h"
105: #include "buf.h"
106: #include "pathnames.h"
107:
108: /*
109: * These values are returned by ParseEOF to tell Parse_File whether to
110: * CONTINUE parsing, i.e. it had only reached the end of an include file,
111: * or if it's DONE.
112: */
113: #define CONTINUE 1
114: #define DONE 0
115: static Lst targets; /* targets we're working on */
116: static Lst targCmds; /* command lines for targets */
117: static Boolean inLine; /* true if currently in a dependency
118: * line or its commands */
119: typedef struct {
120: char *str;
121: char *ptr;
122: } PTR;
123:
124: static char *fname; /* name of current file (for errors) */
125: static int lineno; /* line number in current file */
126: static FILE *curFILE = NULL; /* current makefile */
127:
128: static PTR *curPTR = NULL; /* current makefile */
129:
130: static int fatals = 0;
131:
132: static GNode *mainNode; /* The main target to create. This is the
133: * first target on the first dependency
134: * line in the first makefile */
135: /*
136: * Definitions for handling #include specifications
137: */
138: typedef struct IFile {
139: char *fname; /* name of previous file */
140: int lineno; /* saved line number */
141: FILE * F; /* the open stream */
142: PTR * p; /* the char pointer */
143: } IFile;
144:
145: static Lst includes; /* stack of IFiles generated by
146: * #includes */
147: Lst parseIncPath; /* list of directories for "..." includes */
148: Lst sysIncPath; /* list of directories for <...> includes */
149:
150: /*-
151: * specType contains the SPECial TYPE of the current target. It is
152: * Not if the target is unspecial. If it *is* special, however, the children
153: * are linked as children of the parent but not vice versa. This variable is
154: * set in ParseDoDependency
155: */
156: typedef enum {
157: Begin, /* .BEGIN */
158: Default, /* .DEFAULT */
159: End, /* .END */
160: Ignore, /* .IGNORE */
161: Includes, /* .INCLUDES */
162: Interrupt, /* .INTERRUPT */
163: Libs, /* .LIBS */
164: MFlags, /* .MFLAGS or .MAKEFLAGS */
165: Main, /* .MAIN and we don't have anything user-specified to
166: * make */
167: NoExport, /* .NOEXPORT */
168: Not, /* Not special */
169: NotParallel, /* .NOTPARALELL */
170: Null, /* .NULL */
171: Order, /* .ORDER */
1.3 deraadt 172: Parallel, /* .PARALLEL */
1.1 deraadt 173: ExPath, /* .PATH */
1.7 ! niklas 174: Phony, /* .PHONY */
1.1 deraadt 175: Precious, /* .PRECIOUS */
176: ExShell, /* .SHELL */
177: Silent, /* .SILENT */
178: SingleShell, /* .SINGLESHELL */
179: Suffixes, /* .SUFFIXES */
1.3 deraadt 180: Wait, /* .WAIT */
1.1 deraadt 181: Attribute /* Generic attribute */
182: } ParseSpecial;
183:
184: static ParseSpecial specType;
1.3 deraadt 185: static int waiting;
1.1 deraadt 186:
187: /*
188: * Predecessor node for handling .ORDER. Initialized to NILGNODE when .ORDER
189: * seen, then set to each successive source on the line.
190: */
191: static GNode *predecessor;
192:
193: /*
194: * The parseKeywords table is searched using binary search when deciding
195: * if a target or source is special. The 'spec' field is the ParseSpecial
196: * type of the keyword ("Not" if the keyword isn't special as a target) while
197: * the 'op' field is the operator to apply to the list of targets if the
198: * keyword is used as a source ("0" if the keyword isn't special as a source)
199: */
200: static struct {
201: char *name; /* Name of keyword */
202: ParseSpecial spec; /* Type when used as a target */
203: int op; /* Operator when used as a source */
204: } parseKeywords[] = {
205: { ".BEGIN", Begin, 0 },
206: { ".DEFAULT", Default, 0 },
207: { ".END", End, 0 },
208: { ".EXEC", Attribute, OP_EXEC },
209: { ".IGNORE", Ignore, OP_IGNORE },
210: { ".INCLUDES", Includes, 0 },
211: { ".INTERRUPT", Interrupt, 0 },
212: { ".INVISIBLE", Attribute, OP_INVISIBLE },
213: { ".JOIN", Attribute, OP_JOIN },
214: { ".LIBS", Libs, 0 },
215: { ".MAIN", Main, 0 },
216: { ".MAKE", Attribute, OP_MAKE },
217: { ".MAKEFLAGS", MFlags, 0 },
218: { ".MFLAGS", MFlags, 0 },
219: { ".NOTMAIN", Attribute, OP_NOTMAIN },
220: { ".NOTPARALLEL", NotParallel, 0 },
1.3 deraadt 221: { ".NO_PARALLEL", NotParallel, 0 },
1.1 deraadt 222: { ".NULL", Null, 0 },
223: { ".OPTIONAL", Attribute, OP_OPTIONAL },
224: { ".ORDER", Order, 0 },
1.3 deraadt 225: { ".PARALLEL", Parallel, 0 },
1.1 deraadt 226: { ".PATH", ExPath, 0 },
1.7 ! niklas 227: { ".PHONY", Phony, OP_PHONY },
1.1 deraadt 228: { ".PRECIOUS", Precious, OP_PRECIOUS },
229: { ".RECURSIVE", Attribute, OP_MAKE },
230: { ".SHELL", ExShell, 0 },
231: { ".SILENT", Silent, OP_SILENT },
232: { ".SINGLESHELL", SingleShell, 0 },
233: { ".SUFFIXES", Suffixes, 0 },
234: { ".USE", Attribute, OP_USE },
1.3 deraadt 235: { ".WAIT", Wait, 0 },
1.1 deraadt 236: };
237:
238: static int ParseFindKeyword __P((char *));
239: static int ParseLinkSrc __P((ClientData, ClientData));
240: static int ParseDoOp __P((ClientData, ClientData));
1.3 deraadt 241: static int ParseAddDep __P((ClientData, ClientData));
242: static void ParseDoSrc __P((int, char *, Lst));
1.1 deraadt 243: static int ParseFindMain __P((ClientData, ClientData));
244: static int ParseAddDir __P((ClientData, ClientData));
245: static int ParseClearPath __P((ClientData, ClientData));
246: static void ParseDoDependency __P((char *));
247: static int ParseAddCmd __P((ClientData, ClientData));
248: static int ParseReadc __P((void));
249: static void ParseUnreadc __P((int));
250: static void ParseHasCommands __P((ClientData));
251: static void ParseDoInclude __P((char *));
252: #ifdef SYSVINCLUDE
253: static void ParseTraditionalInclude __P((char *));
254: #endif
255: static int ParseEOF __P((int));
256: static char *ParseReadLine __P((void));
257: static char *ParseSkipLine __P((int));
258: static void ParseFinishLine __P((void));
259:
260: /*-
261: *----------------------------------------------------------------------
262: * ParseFindKeyword --
263: * Look in the table of keywords for one matching the given string.
264: *
265: * Results:
266: * The index of the keyword, or -1 if it isn't there.
267: *
268: * Side Effects:
269: * None
270: *----------------------------------------------------------------------
271: */
272: static int
273: ParseFindKeyword (str)
274: char *str; /* String to find */
275: {
276: register int start,
277: end,
278: cur;
279: register int diff;
280:
281: start = 0;
282: end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
283:
284: do {
285: cur = start + ((end - start) / 2);
286: diff = strcmp (str, parseKeywords[cur].name);
287:
288: if (diff == 0) {
289: return (cur);
290: } else if (diff < 0) {
291: end = cur - 1;
292: } else {
293: start = cur + 1;
294: }
295: } while (start <= end);
296: return (-1);
297: }
298:
299: /*-
300: * Parse_Error --
301: * Error message abort function for parsing. Prints out the context
302: * of the error (line number and file) as well as the message with
303: * two optional arguments.
304: *
305: * Results:
306: * None
307: *
308: * Side Effects:
309: * "fatals" is incremented if the level is PARSE_FATAL.
310: */
311: /* VARARGS */
312: void
313: #if __STDC__
314: Parse_Error(int type, char *fmt, ...)
315: #else
316: Parse_Error(va_alist)
317: va_dcl
318: #endif
319: {
320: va_list ap;
321: #if __STDC__
322: va_start(ap, fmt);
323: #else
324: int type; /* Error type (PARSE_WARNING, PARSE_FATAL) */
325: char *fmt;
326:
327: va_start(ap);
328: type = va_arg(ap, int);
329: fmt = va_arg(ap, char *);
330: #endif
331:
332: (void)fprintf(stderr, "\"%s\", line %d: ", fname, lineno);
333: if (type == PARSE_WARNING)
334: (void)fprintf(stderr, "warning: ");
335: (void)vfprintf(stderr, fmt, ap);
336: va_end(ap);
337: (void)fprintf(stderr, "\n");
338: (void)fflush(stderr);
339: if (type == PARSE_FATAL)
340: fatals += 1;
341: }
342:
343: /*-
344: *---------------------------------------------------------------------
345: * ParseLinkSrc --
346: * Link the parent node to its new child. Used in a Lst_ForEach by
347: * ParseDoDependency. If the specType isn't 'Not', the parent
348: * isn't linked as a parent of the child.
349: *
350: * Results:
351: * Always = 0
352: *
353: * Side Effects:
354: * New elements are added to the parents list of cgn and the
355: * children list of cgn. the unmade field of pgn is updated
356: * to reflect the additional child.
357: *---------------------------------------------------------------------
358: */
359: static int
360: ParseLinkSrc (pgnp, cgnp)
361: ClientData pgnp; /* The parent node */
362: ClientData cgnp; /* The child node */
363: {
364: GNode *pgn = (GNode *) pgnp;
365: GNode *cgn = (GNode *) cgnp;
366: if (Lst_Member (pgn->children, (ClientData)cgn) == NILLNODE) {
367: (void)Lst_AtEnd (pgn->children, (ClientData)cgn);
368: if (specType == Not) {
369: (void)Lst_AtEnd (cgn->parents, (ClientData)pgn);
370: }
371: pgn->unmade += 1;
372: }
373: return (0);
374: }
375:
376: /*-
377: *---------------------------------------------------------------------
378: * ParseDoOp --
379: * Apply the parsed operator to the given target node. Used in a
380: * Lst_ForEach call by ParseDoDependency once all targets have
381: * been found and their operator parsed. If the previous and new
382: * operators are incompatible, a major error is taken.
383: *
384: * Results:
385: * Always 0
386: *
387: * Side Effects:
388: * The type field of the node is altered to reflect any new bits in
389: * the op.
390: *---------------------------------------------------------------------
391: */
392: static int
393: ParseDoOp (gnp, opp)
394: ClientData gnp; /* The node to which the operator is to be
395: * applied */
396: ClientData opp; /* The operator to apply */
397: {
398: GNode *gn = (GNode *) gnp;
399: int op = *(int *) opp;
400: /*
401: * If the dependency mask of the operator and the node don't match and
402: * the node has actually had an operator applied to it before, and
403: * the operator actually has some dependency information in it, complain.
404: */
405: if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
406: !OP_NOP(gn->type) && !OP_NOP(op))
407: {
408: Parse_Error (PARSE_FATAL, "Inconsistent operator for %s", gn->name);
409: return (1);
410: }
411:
412: if ((op == OP_DOUBLEDEP) && ((gn->type & OP_OPMASK) == OP_DOUBLEDEP)) {
413: /*
414: * If the node was the object of a :: operator, we need to create a
415: * new instance of it for the children and commands on this dependency
416: * line. The new instance is placed on the 'cohorts' list of the
417: * initial one (note the initial one is not on its own cohorts list)
418: * and the new instance is linked to all parents of the initial
419: * instance.
420: */
421: register GNode *cohort;
422: LstNode ln;
423:
424: cohort = Targ_NewGN(gn->name);
425: /*
426: * Duplicate links to parents so graph traversal is simple. Perhaps
427: * some type bits should be duplicated?
428: *
429: * Make the cohort invisible as well to avoid duplicating it into
430: * other variables. True, parents of this target won't tend to do
431: * anything with their local variables, but better safe than
432: * sorry.
433: */
434: Lst_ForEach(gn->parents, ParseLinkSrc, (ClientData)cohort);
435: cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
436: (void)Lst_AtEnd(gn->cohorts, (ClientData)cohort);
437:
438: /*
439: * Replace the node in the targets list with the new copy
440: */
441: ln = Lst_Member(targets, (ClientData)gn);
442: Lst_Replace(ln, (ClientData)cohort);
443: gn = cohort;
444: }
445: /*
446: * We don't want to nuke any previous flags (whatever they were) so we
447: * just OR the new operator into the old
448: */
449: gn->type |= op;
450:
451: return (0);
452: }
453:
1.3 deraadt 454: /*-
455: *---------------------------------------------------------------------
456: * ParseAddDep --
457: * Check if the pair of GNodes given needs to be synchronized.
458: * This has to be when two nodes are on different sides of a
459: * .WAIT directive.
460: *
461: * Results:
462: * Returns 1 if the two targets need to be ordered, 0 otherwise.
463: * If it returns 1, the search can stop
464: *
465: * Side Effects:
466: * A dependency can be added between the two nodes.
467: *
468: *---------------------------------------------------------------------
469: */
470: int
471: ParseAddDep(pp, sp)
472: ClientData pp;
473: ClientData sp;
474: {
475: GNode *p = (GNode *) pp;
476: GNode *s = (GNode *) sp;
477:
478: if (p->order < s->order) {
479: /*
480: * XXX: This can cause loops, and loops can cause unmade targets,
481: * but checking is tedious, and the debugging output can show the
482: * problem
483: */
484: (void)Lst_AtEnd(p->successors, (ClientData)s);
485: (void)Lst_AtEnd(s->preds, (ClientData)p);
486: return 0;
487: }
488: else
489: return 1;
490: }
491:
492:
1.1 deraadt 493: /*-
494: *---------------------------------------------------------------------
495: * ParseDoSrc --
496: * Given the name of a source, figure out if it is an attribute
497: * and apply it to the targets if it is. Else decide if there is
498: * some attribute which should be applied *to* the source because
499: * of some special target and apply it if so. Otherwise, make the
500: * source be a child of the targets in the list 'targets'
501: *
502: * Results:
503: * None
504: *
505: * Side Effects:
506: * Operator bits may be added to the list of targets or to the source.
507: * The targets may have a new source added to their lists of children.
508: *---------------------------------------------------------------------
509: */
510: static void
1.3 deraadt 511: ParseDoSrc (tOp, src, allsrc)
1.1 deraadt 512: int tOp; /* operator (if any) from special targets */
513: char *src; /* name of the source to handle */
1.3 deraadt 514: Lst allsrc; /* List of all sources to wait for */
515:
1.1 deraadt 516: {
1.3 deraadt 517: GNode *gn = NULL;
1.1 deraadt 518:
519: if (*src == '.' && isupper (src[1])) {
520: int keywd = ParseFindKeyword(src);
521: if (keywd != -1) {
1.3 deraadt 522: int op = parseKeywords[keywd].op;
523: if (op != 0) {
524: Lst_ForEach (targets, ParseDoOp, (ClientData)&op);
525: return;
526: }
527: if (parseKeywords[keywd].spec == Wait) {
528: waiting++;
529: return;
530: }
1.1 deraadt 531: }
532: }
1.3 deraadt 533:
534: switch (specType) {
535: case Main:
1.1 deraadt 536: /*
537: * If we have noted the existence of a .MAIN, it means we need
538: * to add the sources of said target to the list of things
539: * to create. The string 'src' is likely to be free, so we
540: * must make a new copy of it. Note that this will only be
541: * invoked if the user didn't specify a target on the command
542: * line. This is to allow #ifmake's to succeed, or something...
543: */
544: (void) Lst_AtEnd (create, (ClientData)strdup(src));
545: /*
546: * Add the name to the .TARGETS variable as well, so the user cna
547: * employ that, if desired.
548: */
549: Var_Append(".TARGETS", src, VAR_GLOBAL);
1.3 deraadt 550: return;
551:
552: case Order:
1.1 deraadt 553: /*
554: * Create proper predecessor/successor links between the previous
555: * source and the current one.
556: */
557: gn = Targ_FindNode(src, TARG_CREATE);
558: if (predecessor != NILGNODE) {
559: (void)Lst_AtEnd(predecessor->successors, (ClientData)gn);
560: (void)Lst_AtEnd(gn->preds, (ClientData)predecessor);
561: }
562: /*
563: * The current source now becomes the predecessor for the next one.
564: */
565: predecessor = gn;
1.3 deraadt 566: break;
567:
568: default:
1.1 deraadt 569: /*
570: * If the source is not an attribute, we need to find/create
571: * a node for it. After that we can apply any operator to it
572: * from a special target or link it to its parents, as
573: * appropriate.
574: *
575: * In the case of a source that was the object of a :: operator,
576: * the attribute is applied to all of its instances (as kept in
577: * the 'cohorts' list of the node) or all the cohorts are linked
578: * to all the targets.
579: */
580: gn = Targ_FindNode (src, TARG_CREATE);
581: if (tOp) {
582: gn->type |= tOp;
583: } else {
584: Lst_ForEach (targets, ParseLinkSrc, (ClientData)gn);
585: }
586: if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
587: register GNode *cohort;
588: register LstNode ln;
589:
590: for (ln=Lst_First(gn->cohorts); ln != NILLNODE; ln = Lst_Succ(ln)){
591: cohort = (GNode *)Lst_Datum(ln);
592: if (tOp) {
593: cohort->type |= tOp;
594: } else {
595: Lst_ForEach(targets, ParseLinkSrc, (ClientData)cohort);
596: }
597: }
598: }
1.3 deraadt 599: break;
600: }
601:
602: gn->order = waiting;
603: (void)Lst_AtEnd(allsrc, (ClientData)gn);
604: if (waiting) {
605: Lst_ForEach(allsrc, ParseAddDep, (ClientData)gn);
1.1 deraadt 606: }
607: }
608:
609: /*-
610: *-----------------------------------------------------------------------
611: * ParseFindMain --
612: * Find a real target in the list and set it to be the main one.
613: * Called by ParseDoDependency when a main target hasn't been found
614: * yet.
615: *
616: * Results:
617: * 0 if main not found yet, 1 if it is.
618: *
619: * Side Effects:
620: * mainNode is changed and Targ_SetMain is called.
621: *
622: *-----------------------------------------------------------------------
623: */
624: static int
625: ParseFindMain(gnp, dummy)
626: ClientData gnp; /* Node to examine */
627: ClientData dummy;
628: {
629: GNode *gn = (GNode *) gnp;
630: if ((gn->type & (OP_NOTMAIN|OP_USE|OP_EXEC|OP_TRANSFORM)) == 0) {
631: mainNode = gn;
632: Targ_SetMain(gn);
633: return (dummy ? 1 : 1);
634: } else {
635: return (dummy ? 0 : 0);
636: }
637: }
638:
639: /*-
640: *-----------------------------------------------------------------------
641: * ParseAddDir --
642: * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
643: *
644: * Results:
645: * === 0
646: *
647: * Side Effects:
648: * See Dir_AddDir.
649: *
650: *-----------------------------------------------------------------------
651: */
652: static int
653: ParseAddDir(path, name)
654: ClientData path;
655: ClientData name;
656: {
657: Dir_AddDir((Lst) path, (char *) name);
658: return(0);
659: }
660:
661: /*-
662: *-----------------------------------------------------------------------
663: * ParseClearPath --
664: * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
665: *
666: * Results:
667: * === 0
668: *
669: * Side Effects:
670: * See Dir_ClearPath
671: *
672: *-----------------------------------------------------------------------
673: */
674: static int
675: ParseClearPath(path, dummy)
676: ClientData path;
677: ClientData dummy;
678: {
679: Dir_ClearPath((Lst) path);
680: return(dummy ? 0 : 0);
681: }
682:
683: /*-
684: *---------------------------------------------------------------------
685: * ParseDoDependency --
686: * Parse the dependency line in line.
687: *
688: * Results:
689: * None
690: *
691: * Side Effects:
692: * The nodes of the sources are linked as children to the nodes of the
693: * targets. Some nodes may be created.
694: *
695: * We parse a dependency line by first extracting words from the line and
696: * finding nodes in the list of all targets with that name. This is done
697: * until a character is encountered which is an operator character. Currently
698: * these are only ! and :. At this point the operator is parsed and the
699: * pointer into the line advanced until the first source is encountered.
700: * The parsed operator is applied to each node in the 'targets' list,
701: * which is where the nodes found for the targets are kept, by means of
702: * the ParseDoOp function.
703: * The sources are read in much the same way as the targets were except
704: * that now they are expanded using the wildcarding scheme of the C-Shell
705: * and all instances of the resulting words in the list of all targets
706: * are found. Each of the resulting nodes is then linked to each of the
707: * targets as one of its children.
708: * Certain targets are handled specially. These are the ones detailed
709: * by the specType variable.
710: * The storing of transformation rules is also taken care of here.
711: * A target is recognized as a transformation rule by calling
712: * Suff_IsTransform. If it is a transformation rule, its node is gotten
713: * from the suffix module via Suff_AddTransform rather than the standard
714: * Targ_FindNode in the target module.
715: *---------------------------------------------------------------------
716: */
717: static void
718: ParseDoDependency (line)
719: char *line; /* the line to parse */
720: {
721: char *cp; /* our current position */
722: GNode *gn; /* a general purpose temporary node */
723: int op; /* the operator on the line */
724: char savec; /* a place to save a character */
725: Lst paths; /* List of search paths to alter when parsing
726: * a list of .PATH targets */
727: int tOp; /* operator from special target */
1.3 deraadt 728: Lst sources; /* list of archive source names after
729: * expansion */
1.1 deraadt 730: Lst curTargs; /* list of target names to be found and added
731: * to the targets list */
1.3 deraadt 732: Lst curSrcs; /* list of sources in order */
1.1 deraadt 733:
734: tOp = 0;
735:
736: specType = Not;
1.3 deraadt 737: waiting = 0;
1.1 deraadt 738: paths = (Lst)NULL;
739:
740: curTargs = Lst_Init(FALSE);
1.3 deraadt 741: curSrcs = Lst_Init(FALSE);
1.1 deraadt 742:
743: do {
744: for (cp = line;
745: *cp && !isspace (*cp) &&
746: (*cp != '!') && (*cp != ':') && (*cp != '(');
747: cp++)
748: {
749: if (*cp == '$') {
750: /*
751: * Must be a dynamic source (would have been expanded
752: * otherwise), so call the Var module to parse the puppy
753: * so we can safely advance beyond it...There should be
754: * no errors in this, as they would have been discovered
755: * in the initial Var_Subst and we wouldn't be here.
756: */
757: int length;
758: Boolean freeIt;
759: char *result;
760:
761: result=Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
762:
763: if (freeIt) {
764: free(result);
765: }
766: cp += length-1;
767: }
768: continue;
769: }
770: if (*cp == '(') {
771: /*
772: * Archives must be handled specially to make sure the OP_ARCHV
773: * flag is set in their 'type' field, for one thing, and because
774: * things like "archive(file1.o file2.o file3.o)" are permissible.
775: * Arch_ParseArchive will set 'line' to be the first non-blank
776: * after the archive-spec. It creates/finds nodes for the members
777: * and places them on the given list, returning SUCCESS if all
778: * went well and FAILURE if there was an error in the
779: * specification. On error, line should remain untouched.
780: */
781: if (Arch_ParseArchive (&line, targets, VAR_CMD) != SUCCESS) {
782: Parse_Error (PARSE_FATAL,
783: "Error in archive specification: \"%s\"", line);
784: return;
785: } else {
786: continue;
787: }
788: }
789: savec = *cp;
790:
791: if (!*cp) {
792: /*
793: * Ending a dependency line without an operator is a Bozo
794: * no-no
795: */
796: Parse_Error (PARSE_FATAL, "Need an operator");
797: return;
798: }
799: *cp = '\0';
800: /*
801: * Have a word in line. See if it's a special target and set
802: * specType to match it.
803: */
804: if (*line == '.' && isupper (line[1])) {
805: /*
806: * See if the target is a special target that must have it
807: * or its sources handled specially.
808: */
809: int keywd = ParseFindKeyword(line);
810: if (keywd != -1) {
811: if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
812: Parse_Error(PARSE_FATAL, "Mismatched special targets");
813: return;
814: }
815:
816: specType = parseKeywords[keywd].spec;
817: tOp = parseKeywords[keywd].op;
818:
819: /*
820: * Certain special targets have special semantics:
821: * .PATH Have to set the dirSearchPath
822: * variable too
823: * .MAIN Its sources are only used if
824: * nothing has been specified to
825: * create.
826: * .DEFAULT Need to create a node to hang
827: * commands on, but we don't want
828: * it in the graph, nor do we want
829: * it to be the Main Target, so we
830: * create it, set OP_NOTMAIN and
831: * add it to the list, setting
832: * DEFAULT to the new node for
833: * later use. We claim the node is
834: * A transformation rule to make
835: * life easier later, when we'll
836: * use Make_HandleUse to actually
837: * apply the .DEFAULT commands.
1.7 ! niklas 838: * .PHONY The list of targets
1.1 deraadt 839: * .BEGIN
840: * .END
841: * .INTERRUPT Are not to be considered the
842: * main target.
843: * .NOTPARALLEL Make only one target at a time.
844: * .SINGLESHELL Create a shell for each command.
845: * .ORDER Must set initial predecessor to NIL
846: */
847: switch (specType) {
848: case ExPath:
849: if (paths == NULL) {
850: paths = Lst_Init(FALSE);
851: }
852: (void)Lst_AtEnd(paths, (ClientData)dirSearchPath);
853: break;
854: case Main:
855: if (!Lst_IsEmpty(create)) {
856: specType = Not;
857: }
858: break;
859: case Begin:
860: case End:
861: case Interrupt:
862: gn = Targ_FindNode(line, TARG_CREATE);
863: gn->type |= OP_NOTMAIN;
864: (void)Lst_AtEnd(targets, (ClientData)gn);
865: break;
866: case Default:
867: gn = Targ_NewGN(".DEFAULT");
868: gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
869: (void)Lst_AtEnd(targets, (ClientData)gn);
870: DEFAULT = gn;
871: break;
872: case NotParallel:
873: {
874: extern int maxJobs;
875:
876: maxJobs = 1;
877: break;
878: }
879: case SingleShell:
880: compatMake = 1;
881: break;
882: case Order:
883: predecessor = NILGNODE;
884: break;
885: default:
886: break;
887: }
888: } else if (strncmp (line, ".PATH", 5) == 0) {
889: /*
890: * .PATH<suffix> has to be handled specially.
891: * Call on the suffix module to give us a path to
892: * modify.
893: */
894: Lst path;
895:
896: specType = ExPath;
897: path = Suff_GetPath (&line[5]);
898: if (path == NILLST) {
899: Parse_Error (PARSE_FATAL,
900: "Suffix '%s' not defined (yet)",
901: &line[5]);
902: return;
903: } else {
904: if (paths == (Lst)NULL) {
905: paths = Lst_Init(FALSE);
906: }
907: (void)Lst_AtEnd(paths, (ClientData)path);
908: }
909: }
910: }
911:
912: /*
913: * Have word in line. Get or create its node and stick it at
914: * the end of the targets list
915: */
916: if ((specType == Not) && (*line != '\0')) {
917: if (Dir_HasWildcards(line)) {
918: /*
919: * Targets are to be sought only in the current directory,
920: * so create an empty path for the thing. Note we need to
921: * use Dir_Destroy in the destruction of the path as the
922: * Dir module could have added a directory to the path...
923: */
924: Lst emptyPath = Lst_Init(FALSE);
925:
926: Dir_Expand(line, emptyPath, curTargs);
927:
928: Lst_Destroy(emptyPath, Dir_Destroy);
929: } else {
930: /*
931: * No wildcards, but we want to avoid code duplication,
932: * so create a list with the word on it.
933: */
934: (void)Lst_AtEnd(curTargs, (ClientData)line);
935: }
936:
937: while(!Lst_IsEmpty(curTargs)) {
938: char *targName = (char *)Lst_DeQueue(curTargs);
939:
940: if (!Suff_IsTransform (targName)) {
941: gn = Targ_FindNode (targName, TARG_CREATE);
942: } else {
943: gn = Suff_AddTransform (targName);
944: }
945:
946: (void)Lst_AtEnd (targets, (ClientData)gn);
947: }
948: } else if (specType == ExPath && *line != '.' && *line != '\0') {
949: Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
950: }
951:
952: *cp = savec;
953: /*
954: * If it is a special type and not .PATH, it's the only target we
955: * allow on this line...
956: */
957: if (specType != Not && specType != ExPath) {
958: Boolean warn = FALSE;
959:
960: while ((*cp != '!') && (*cp != ':') && *cp) {
961: if (*cp != ' ' && *cp != '\t') {
962: warn = TRUE;
963: }
964: cp++;
965: }
966: if (warn) {
967: Parse_Error(PARSE_WARNING, "Extra target ignored");
968: }
969: } else {
970: while (*cp && isspace (*cp)) {
971: cp++;
972: }
973: }
974: line = cp;
975: } while ((*line != '!') && (*line != ':') && *line);
976:
977: /*
978: * Don't need the list of target names anymore...
979: */
980: Lst_Destroy(curTargs, NOFREE);
981:
982: if (!Lst_IsEmpty(targets)) {
983: switch(specType) {
984: default:
985: Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
986: break;
987: case Default:
988: case Begin:
989: case End:
990: case Interrupt:
991: /*
992: * These four create nodes on which to hang commands, so
993: * targets shouldn't be empty...
994: */
995: case Not:
996: /*
997: * Nothing special here -- targets can be empty if it wants.
998: */
999: break;
1000: }
1001: }
1002:
1003: /*
1004: * Have now parsed all the target names. Must parse the operator next. The
1005: * result is left in op .
1006: */
1007: if (*cp == '!') {
1008: op = OP_FORCE;
1009: } else if (*cp == ':') {
1010: if (cp[1] == ':') {
1011: op = OP_DOUBLEDEP;
1012: cp++;
1013: } else {
1014: op = OP_DEPENDS;
1015: }
1016: } else {
1017: Parse_Error (PARSE_FATAL, "Missing dependency operator");
1018: return;
1019: }
1020:
1021: cp++; /* Advance beyond operator */
1022:
1023: Lst_ForEach (targets, ParseDoOp, (ClientData)&op);
1024:
1025: /*
1026: * Get to the first source
1027: */
1028: while (*cp && isspace (*cp)) {
1029: cp++;
1030: }
1031: line = cp;
1032:
1033: /*
1034: * Several special targets take different actions if present with no
1035: * sources:
1036: * a .SUFFIXES line with no sources clears out all old suffixes
1037: * a .PRECIOUS line makes all targets precious
1038: * a .IGNORE line ignores errors for all targets
1039: * a .SILENT line creates silence when making all targets
1040: * a .PATH removes all directories from the search path(s).
1041: */
1042: if (!*line) {
1043: switch (specType) {
1044: case Suffixes:
1045: Suff_ClearSuffixes ();
1046: break;
1047: case Precious:
1048: allPrecious = TRUE;
1049: break;
1050: case Ignore:
1051: ignoreErrors = TRUE;
1052: break;
1053: case Silent:
1054: beSilent = TRUE;
1055: break;
1056: case ExPath:
1057: Lst_ForEach(paths, ParseClearPath, (ClientData)NULL);
1058: break;
1059: default:
1060: break;
1061: }
1062: } else if (specType == MFlags) {
1063: /*
1064: * Call on functions in main.c to deal with these arguments and
1065: * set the initial character to a null-character so the loop to
1066: * get sources won't get anything
1067: */
1068: Main_ParseArgLine (line);
1069: *line = '\0';
1070: } else if (specType == ExShell) {
1071: if (Job_ParseShell (line) != SUCCESS) {
1072: Parse_Error (PARSE_FATAL, "improper shell specification");
1073: return;
1074: }
1075: *line = '\0';
1076: } else if ((specType == NotParallel) || (specType == SingleShell)) {
1077: *line = '\0';
1078: }
1079:
1080: /*
1081: * NOW GO FOR THE SOURCES
1082: */
1083: if ((specType == Suffixes) || (specType == ExPath) ||
1084: (specType == Includes) || (specType == Libs) ||
1085: (specType == Null))
1086: {
1087: while (*line) {
1088: /*
1089: * If the target was one that doesn't take files as its sources
1090: * but takes something like suffixes, we take each
1091: * space-separated word on the line as a something and deal
1092: * with it accordingly.
1093: *
1094: * If the target was .SUFFIXES, we take each source as a
1095: * suffix and add it to the list of suffixes maintained by the
1096: * Suff module.
1097: *
1098: * If the target was a .PATH, we add the source as a directory
1099: * to search on the search path.
1100: *
1101: * If it was .INCLUDES, the source is taken to be the suffix of
1102: * files which will be #included and whose search path should
1103: * be present in the .INCLUDES variable.
1104: *
1105: * If it was .LIBS, the source is taken to be the suffix of
1106: * files which are considered libraries and whose search path
1107: * should be present in the .LIBS variable.
1108: *
1109: * If it was .NULL, the source is the suffix to use when a file
1110: * has no valid suffix.
1111: */
1112: char savec;
1113: while (*cp && !isspace (*cp)) {
1114: cp++;
1115: }
1116: savec = *cp;
1117: *cp = '\0';
1118: switch (specType) {
1119: case Suffixes:
1120: Suff_AddSuffix (line);
1121: break;
1122: case ExPath:
1123: Lst_ForEach(paths, ParseAddDir, (ClientData)line);
1124: break;
1125: case Includes:
1126: Suff_AddInclude (line);
1127: break;
1128: case Libs:
1129: Suff_AddLib (line);
1130: break;
1131: case Null:
1132: Suff_SetNull (line);
1133: break;
1134: default:
1135: break;
1136: }
1137: *cp = savec;
1138: if (savec != '\0') {
1139: cp++;
1140: }
1141: while (*cp && isspace (*cp)) {
1142: cp++;
1143: }
1144: line = cp;
1145: }
1146: if (paths) {
1147: Lst_Destroy(paths, NOFREE);
1148: }
1149: } else {
1150: while (*line) {
1151: /*
1152: * The targets take real sources, so we must beware of archive
1153: * specifications (i.e. things with left parentheses in them)
1154: * and handle them accordingly.
1155: */
1156: while (*cp && !isspace (*cp)) {
1157: if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
1158: /*
1159: * Only stop for a left parenthesis if it isn't at the
1160: * start of a word (that'll be for variable changes
1161: * later) and isn't preceded by a dollar sign (a dynamic
1162: * source).
1163: */
1164: break;
1165: } else {
1166: cp++;
1167: }
1168: }
1169:
1170: if (*cp == '(') {
1171: GNode *gn;
1172:
1173: sources = Lst_Init (FALSE);
1174: if (Arch_ParseArchive (&line, sources, VAR_CMD) != SUCCESS) {
1175: Parse_Error (PARSE_FATAL,
1176: "Error in source archive spec \"%s\"", line);
1177: return;
1178: }
1179:
1180: while (!Lst_IsEmpty (sources)) {
1181: gn = (GNode *) Lst_DeQueue (sources);
1.3 deraadt 1182: ParseDoSrc (tOp, gn->name, curSrcs);
1.1 deraadt 1183: }
1184: Lst_Destroy (sources, NOFREE);
1185: cp = line;
1186: } else {
1187: if (*cp) {
1188: *cp = '\0';
1189: cp += 1;
1190: }
1191:
1.3 deraadt 1192: ParseDoSrc (tOp, line, curSrcs);
1.1 deraadt 1193: }
1194: while (*cp && isspace (*cp)) {
1195: cp++;
1196: }
1197: line = cp;
1198: }
1199: }
1200:
1201: if (mainNode == NILGNODE) {
1202: /*
1203: * If we have yet to decide on a main target to make, in the
1204: * absence of any user input, we want the first target on
1205: * the first dependency line that is actually a real target
1206: * (i.e. isn't a .USE or .EXEC rule) to be made.
1207: */
1208: Lst_ForEach (targets, ParseFindMain, (ClientData)0);
1209: }
1210:
1.3 deraadt 1211: /*
1212: * Finally, destroy the list of sources
1213: */
1214: Lst_Destroy(curSrcs, NOFREE);
1.1 deraadt 1215: }
1216:
1217: /*-
1218: *---------------------------------------------------------------------
1219: * Parse_IsVar --
1220: * Return TRUE if the passed line is a variable assignment. A variable
1221: * assignment consists of a single word followed by optional whitespace
1222: * followed by either a += or an = operator.
1223: * This function is used both by the Parse_File function and main when
1224: * parsing the command-line arguments.
1225: *
1226: * Results:
1227: * TRUE if it is. FALSE if it ain't
1228: *
1229: * Side Effects:
1230: * none
1231: *---------------------------------------------------------------------
1232: */
1233: Boolean
1234: Parse_IsVar (line)
1235: register char *line; /* the line to check */
1236: {
1237: register Boolean wasSpace = FALSE; /* set TRUE if found a space */
1238: register Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1239: int level = 0;
1240: #define ISEQOPERATOR(c) \
1241: (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1242:
1243: /*
1244: * Skip to variable name
1245: */
1246: for (;(*line == ' ') || (*line == '\t'); line++)
1247: continue;
1248:
1249: for (; *line != '=' || level != 0; line++)
1250: switch (*line) {
1251: case '\0':
1252: /*
1253: * end-of-line -- can't be a variable assignment.
1254: */
1255: return FALSE;
1256:
1257: case ' ':
1258: case '\t':
1259: /*
1260: * there can be as much white space as desired so long as there is
1261: * only one word before the operator
1262: */
1263: wasSpace = TRUE;
1264: break;
1265:
1266: case '(':
1267: case '{':
1268: level++;
1269: break;
1270:
1271: case '}':
1272: case ')':
1273: level--;
1274: break;
1275:
1276: default:
1277: if (wasSpace && haveName) {
1278: if (ISEQOPERATOR(*line)) {
1279: /*
1280: * When an = operator [+?!:] is found, the next
1281: * character * must be an = or it ain't a valid
1282: * assignment.
1283: */
1284: if (line[1] != '=' && level == 0)
1285: return FALSE;
1286: else
1287: return haveName;
1288: }
1289: else {
1290: /*
1291: * This is the start of another word, so not assignment.
1292: */
1293: return FALSE;
1294: }
1295: }
1296: else {
1297: haveName = TRUE;
1298: wasSpace = FALSE;
1299: }
1300: break;
1301: }
1302:
1303: return haveName;
1304: }
1305:
1306: /*-
1307: *---------------------------------------------------------------------
1308: * Parse_DoVar --
1309: * Take the variable assignment in the passed line and do it in the
1310: * global context.
1311: *
1312: * Note: There is a lexical ambiguity with assignment modifier characters
1313: * in variable names. This routine interprets the character before the =
1314: * as a modifier. Therefore, an assignment like
1315: * C++=/usr/bin/CC
1316: * is interpreted as "C+ +=" instead of "C++ =".
1317: *
1318: * Results:
1319: * none
1320: *
1321: * Side Effects:
1322: * the variable structure of the given variable name is altered in the
1323: * global context.
1324: *---------------------------------------------------------------------
1325: */
1326: void
1327: Parse_DoVar (line, ctxt)
1328: char *line; /* a line guaranteed to be a variable
1329: * assignment. This reduces error checks */
1330: GNode *ctxt; /* Context in which to do the assignment */
1331: {
1332: char *cp; /* pointer into line */
1333: enum {
1334: VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1335: } type; /* Type of assignment */
1336: char *opc; /* ptr to operator character to
1337: * null-terminate the variable name */
1338: /*
1339: * Avoid clobbered variable warnings by forcing the compiler
1340: * to ``unregister'' variables
1341: */
1342: #if __GNUC__
1343: (void) &cp;
1344: (void) &line;
1345: #endif
1346:
1347: /*
1348: * Skip to variable name
1349: */
1350: while ((*line == ' ') || (*line == '\t')) {
1351: line++;
1352: }
1353:
1354: /*
1355: * Skip to operator character, nulling out whitespace as we go
1356: */
1357: for (cp = line + 1; *cp != '='; cp++) {
1358: if (isspace (*cp)) {
1359: *cp = '\0';
1360: }
1361: }
1362: opc = cp-1; /* operator is the previous character */
1363: *cp++ = '\0'; /* nuke the = */
1364:
1365: /*
1366: * Check operator type
1367: */
1368: switch (*opc) {
1369: case '+':
1370: type = VAR_APPEND;
1371: *opc = '\0';
1372: break;
1373:
1374: case '?':
1375: /*
1376: * If the variable already has a value, we don't do anything.
1377: */
1378: *opc = '\0';
1379: if (Var_Exists(line, ctxt)) {
1380: return;
1381: } else {
1382: type = VAR_NORMAL;
1383: }
1384: break;
1385:
1386: case ':':
1387: type = VAR_SUBST;
1388: *opc = '\0';
1389: break;
1390:
1391: case '!':
1392: type = VAR_SHELL;
1393: *opc = '\0';
1394: break;
1395:
1396: default:
1397: type = VAR_NORMAL;
1398: break;
1399: }
1400:
1401: while (isspace (*cp)) {
1402: cp++;
1403: }
1404:
1405: if (type == VAR_APPEND) {
1406: Var_Append (line, cp, ctxt);
1407: } else if (type == VAR_SUBST) {
1408: /*
1409: * Allow variables in the old value to be undefined, but leave their
1410: * invocation alone -- this is done by forcing oldVars to be false.
1411: * XXX: This can cause recursive variables, but that's not hard to do,
1412: * and this allows someone to do something like
1413: *
1414: * CFLAGS = $(.INCLUDES)
1415: * CFLAGS := -I.. $(CFLAGS)
1416: *
1417: * And not get an error.
1418: */
1419: Boolean oldOldVars = oldVars;
1420:
1421: oldVars = FALSE;
1422: cp = Var_Subst(NULL, cp, ctxt, FALSE);
1423: oldVars = oldOldVars;
1424:
1425: Var_Set(line, cp, ctxt);
1426: free(cp);
1427: } else if (type == VAR_SHELL) {
1428: char *args[4]; /* Args for invoking the shell */
1429: int fds[2]; /* Pipe streams */
1430: int cpid; /* Child PID */
1431: int pid; /* PID from wait() */
1432: Boolean freeCmd; /* TRUE if the command needs to be freed, i.e.
1433: * if any variable expansion was performed */
1434:
1435: /*
1436: * Avoid clobbered variable warnings by forcing the compiler
1437: * to ``unregister'' variables
1438: */
1439: #if __GNUC__
1440: (void) &freeCmd;
1441: #endif
1442:
1443: /*
1444: * Set up arguments for shell
1445: */
1446: args[0] = "sh";
1447: args[1] = "-c";
1448: if (strchr(cp, '$') != (char *)NULL) {
1449: /*
1450: * There's a dollar sign in the command, so perform variable
1451: * expansion on the whole thing. The resulting string will need
1452: * freeing when we're done, so set freeCmd to TRUE.
1453: */
1454: args[2] = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1455: freeCmd = TRUE;
1456: } else {
1457: args[2] = cp;
1458: freeCmd = FALSE;
1459: }
1460: args[3] = (char *)NULL;
1461:
1462: /*
1463: * Open a pipe for fetching its output
1464: */
1465: pipe(fds);
1466:
1467: /*
1468: * Fork
1469: */
1470: cpid = vfork();
1471: if (cpid == 0) {
1472: /*
1473: * Close input side of pipe
1474: */
1475: close(fds[0]);
1476:
1477: /*
1478: * Duplicate the output stream to the shell's output, then
1479: * shut the extra thing down. Note we don't fetch the error
1480: * stream...why not? Why?
1481: */
1482: dup2(fds[1], 1);
1483: close(fds[1]);
1484:
1485: execv("/bin/sh", args);
1486: _exit(1);
1487: } else if (cpid < 0) {
1488: /*
1489: * Couldn't fork -- tell the user and make the variable null
1490: */
1491: Parse_Error(PARSE_WARNING, "Couldn't exec \"%s\"", cp);
1492: Var_Set(line, "", ctxt);
1493: } else {
1494: int status;
1495: int cc;
1496: Buffer buf;
1497: char *res;
1498:
1499: /*
1500: * No need for the writing half
1501: */
1502: close(fds[1]);
1503:
1504: buf = Buf_Init (MAKE_BSIZE);
1505:
1506: do {
1507: char result[BUFSIZ];
1508: cc = read(fds[0], result, sizeof(result));
1509: if (cc > 0)
1510: Buf_AddBytes(buf, cc, (Byte *) result);
1511: }
1512: while (cc > 0 || (cc == -1 && errno == EINTR));
1513:
1514: /*
1515: * Close the input side of the pipe.
1516: */
1517: close(fds[0]);
1518:
1519: /*
1520: * Wait for the process to exit.
1521: */
1522: while(((pid = wait(&status)) != cpid) && (pid >= 0))
1523: continue;
1524:
1525: res = (char *)Buf_GetAll (buf, &cc);
1526: Buf_Destroy (buf, FALSE);
1527:
1528: if (cc == 0) {
1529: /*
1530: * Couldn't read the child's output -- tell the user and
1531: * set the variable to null
1532: */
1533: Parse_Error(PARSE_WARNING, "Couldn't read shell's output");
1534: }
1535:
1536: if (status) {
1537: /*
1538: * Child returned an error -- tell the user but still use
1539: * the result.
1540: */
1541: Parse_Error(PARSE_WARNING, "\"%s\" returned non-zero", cp);
1542: }
1543:
1544: /*
1545: * Null-terminate the result, convert newlines to spaces and
1546: * install it in the variable.
1547: */
1548: res[cc] = '\0';
1549: cp = &res[cc] - 1;
1550:
1551: if (*cp == '\n') {
1552: /*
1553: * A final newline is just stripped
1554: */
1555: *cp-- = '\0';
1556: }
1557: while (cp >= res) {
1558: if (*cp == '\n') {
1559: *cp = ' ';
1560: }
1561: cp--;
1562: }
1563: Var_Set(line, res, ctxt);
1564: free(res);
1565:
1566: }
1567: if (freeCmd) {
1568: free(args[2]);
1569: }
1570: } else {
1571: /*
1572: * Normal assignment -- just do it.
1573: */
1574: Var_Set (line, cp, ctxt);
1575: }
1576: }
1577:
1578: /*-
1579: * ParseAddCmd --
1580: * Lst_ForEach function to add a command line to all targets
1581: *
1582: * Results:
1583: * Always 0
1584: *
1585: * Side Effects:
1586: * A new element is added to the commands list of the node.
1587: */
1588: static int
1589: ParseAddCmd(gnp, cmd)
1590: ClientData gnp; /* the node to which the command is to be added */
1591: ClientData cmd; /* the command to add */
1592: {
1593: GNode *gn = (GNode *) gnp;
1594: /* if target already supplied, ignore commands */
1595: if (!(gn->type & OP_HAS_COMMANDS))
1596: (void)Lst_AtEnd(gn->commands, cmd);
1597: return(0);
1598: }
1599:
1600: /*-
1601: *-----------------------------------------------------------------------
1602: * ParseHasCommands --
1603: * Callback procedure for Parse_File when destroying the list of
1604: * targets on the last dependency line. Marks a target as already
1605: * having commands if it does, to keep from having shell commands
1606: * on multiple dependency lines.
1607: *
1608: * Results:
1609: * None
1610: *
1611: * Side Effects:
1612: * OP_HAS_COMMANDS may be set for the target.
1613: *
1614: *-----------------------------------------------------------------------
1615: */
1616: static void
1617: ParseHasCommands(gnp)
1618: ClientData gnp; /* Node to examine */
1619: {
1620: GNode *gn = (GNode *) gnp;
1621: if (!Lst_IsEmpty(gn->commands)) {
1622: gn->type |= OP_HAS_COMMANDS;
1623: }
1624: }
1625:
1626: /*-
1627: *-----------------------------------------------------------------------
1628: * Parse_AddIncludeDir --
1629: * Add a directory to the path searched for included makefiles
1630: * bracketed by double-quotes. Used by functions in main.c
1631: *
1632: * Results:
1633: * None.
1634: *
1635: * Side Effects:
1636: * The directory is appended to the list.
1637: *
1638: *-----------------------------------------------------------------------
1639: */
1640: void
1641: Parse_AddIncludeDir (dir)
1642: char *dir; /* The name of the directory to add */
1643: {
1644: Dir_AddDir (parseIncPath, dir);
1645: }
1646:
1647: /*-
1648: *---------------------------------------------------------------------
1649: * ParseDoInclude --
1650: * Push to another file.
1651: *
1652: * The input is the line minus the #include. A file spec is a string
1653: * enclosed in <> or "". The former is looked for only in sysIncPath.
1654: * The latter in . and the directories specified by -I command line
1655: * options
1656: *
1657: * Results:
1658: * None
1659: *
1660: * Side Effects:
1661: * A structure is added to the includes Lst and readProc, lineno,
1662: * fname and curFILE are altered for the new file
1663: *---------------------------------------------------------------------
1664: */
1665: static void
1666: ParseDoInclude (file)
1667: char *file; /* file specification */
1668: {
1669: char *fullname; /* full pathname of file */
1670: IFile *oldFile; /* state associated with current file */
1671: char endc; /* the character which ends the file spec */
1672: char *cp; /* current position in file spec */
1673: Boolean isSystem; /* TRUE if makefile is a system makefile */
1674:
1675: /*
1676: * Skip to delimiter character so we know where to look
1677: */
1678: while ((*file == ' ') || (*file == '\t')) {
1679: file++;
1680: }
1681:
1682: if ((*file != '"') && (*file != '<')) {
1683: Parse_Error (PARSE_FATAL,
1684: ".include filename must be delimited by '\"' or '<'");
1685: return;
1686: }
1687:
1688: /*
1689: * Set the search path on which to find the include file based on the
1690: * characters which bracket its name. Angle-brackets imply it's
1691: * a system Makefile while double-quotes imply it's a user makefile
1692: */
1693: if (*file == '<') {
1694: isSystem = TRUE;
1695: endc = '>';
1696: } else {
1697: isSystem = FALSE;
1698: endc = '"';
1699: }
1700:
1701: /*
1702: * Skip to matching delimiter
1703: */
1704: for (cp = ++file; *cp && *cp != endc; cp++) {
1705: continue;
1706: }
1707:
1708: if (*cp != endc) {
1709: Parse_Error (PARSE_FATAL,
1710: "Unclosed %cinclude filename. '%c' expected",
1711: '.', endc);
1712: return;
1713: }
1714: *cp = '\0';
1715:
1716: /*
1717: * Substitute for any variables in the file name before trying to
1718: * find the thing.
1719: */
1720: file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1721:
1722: /*
1723: * Now we know the file's name and its search path, we attempt to
1724: * find the durn thing. A return of NULL indicates the file don't
1725: * exist.
1726: */
1727: if (!isSystem) {
1728: /*
1729: * Include files contained in double-quotes are first searched for
1730: * relative to the including file's location. We don't want to
1731: * cd there, of course, so we just tack on the old file's
1732: * leading path components and call Dir_FindFile to see if
1733: * we can locate the beast.
1734: */
1.4 niklas 1735: char *prefEnd, *Fname;
1.1 deraadt 1736:
1.4 niklas 1737: /* Make a temporary copy of this, to be safe. */
1738: Fname = strdup(fname);
1739:
1740: prefEnd = strrchr (Fname, '/');
1.1 deraadt 1741: if (prefEnd != (char *)NULL) {
1742: char *newName;
1743:
1744: *prefEnd = '\0';
1745: if (file[0] == '/')
1746: newName = strdup(file);
1747: else
1.4 niklas 1748: newName = str_concat (Fname, file, STR_ADDSLASH);
1.1 deraadt 1749: fullname = Dir_FindFile (newName, parseIncPath);
1750: if (fullname == (char *)NULL) {
1751: fullname = Dir_FindFile(newName, dirSearchPath);
1752: }
1753: free (newName);
1754: *prefEnd = '/';
1755: } else {
1756: fullname = (char *)NULL;
1757: }
1.4 niklas 1758: free (Fname);
1.1 deraadt 1759: } else {
1760: fullname = (char *)NULL;
1761: }
1762:
1763: if (fullname == (char *)NULL) {
1764: /*
1765: * System makefile or makefile wasn't found in same directory as
1766: * included makefile. Search for it first on the -I search path,
1767: * then on the .PATH search path, if not found in a -I directory.
1768: * XXX: Suffix specific?
1769: */
1770: fullname = Dir_FindFile (file, parseIncPath);
1771: if (fullname == (char *)NULL) {
1772: fullname = Dir_FindFile(file, dirSearchPath);
1773: }
1774: }
1775:
1776: if (fullname == (char *)NULL) {
1777: /*
1778: * Still haven't found the makefile. Look for it on the system
1779: * path as a last resort.
1780: */
1781: fullname = Dir_FindFile(file, sysIncPath);
1782: }
1783:
1784: if (fullname == (char *) NULL) {
1785: *cp = endc;
1786: Parse_Error (PARSE_FATAL, "Could not find %s", file);
1787: return;
1788: }
1789:
1790: free(file);
1791:
1792: /*
1793: * Once we find the absolute path to the file, we get to save all the
1794: * state from the current file before we can start reading this
1795: * include file. The state is stored in an IFile structure which
1796: * is placed on a list with other IFile structures. The list makes
1797: * a very nice stack to track how we got here...
1798: */
1799: oldFile = (IFile *) emalloc (sizeof (IFile));
1800: oldFile->fname = fname;
1801:
1802: oldFile->F = curFILE;
1803: oldFile->p = curPTR;
1804: oldFile->lineno = lineno;
1805:
1806: (void) Lst_AtFront (includes, (ClientData)oldFile);
1807:
1808: /*
1809: * Once the previous state has been saved, we can get down to reading
1810: * the new file. We set up the name of the file to be the absolute
1811: * name of the include file so error messages refer to the right
1812: * place. Naturally enough, we start reading at line number 0.
1813: */
1814: fname = fullname;
1815: lineno = 0;
1816:
1817: curFILE = fopen (fullname, "r");
1818: curPTR = NULL;
1819: if (curFILE == (FILE * ) NULL) {
1820: Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1821: /*
1822: * Pop to previous file
1823: */
1824: (void) ParseEOF(0);
1825: }
1826: }
1827:
1828:
1829: /*-
1830: *---------------------------------------------------------------------
1831: * Parse_FromString --
1832: * Start Parsing from the given string
1833: *
1834: * Results:
1835: * None
1836: *
1837: * Side Effects:
1838: * A structure is added to the includes Lst and readProc, lineno,
1839: * fname and curFILE are altered for the new file
1840: *---------------------------------------------------------------------
1841: */
1842: void
1843: Parse_FromString(str)
1844: char *str;
1845: {
1846: IFile *oldFile; /* state associated with this file */
1847:
1848: if (DEBUG(FOR))
1849: (void) fprintf(stderr, "%s\n----\n", str);
1850:
1851: oldFile = (IFile *) emalloc (sizeof (IFile));
1852: oldFile->lineno = lineno;
1853: oldFile->fname = fname;
1854: oldFile->F = curFILE;
1855: oldFile->p = curPTR;
1856:
1857: (void) Lst_AtFront (includes, (ClientData)oldFile);
1858:
1859: curFILE = NULL;
1860: curPTR = (PTR *) emalloc (sizeof (PTR));
1861: curPTR->str = curPTR->ptr = str;
1862: lineno = 0;
1863: fname = strdup(fname);
1864: }
1865:
1866:
1867: #ifdef SYSVINCLUDE
1868: /*-
1869: *---------------------------------------------------------------------
1870: * ParseTraditionalInclude --
1871: * Push to another file.
1872: *
1873: * The input is the line minus the "include". The file name is
1874: * the string following the "include".
1875: *
1876: * Results:
1877: * None
1878: *
1879: * Side Effects:
1880: * A structure is added to the includes Lst and readProc, lineno,
1881: * fname and curFILE are altered for the new file
1882: *---------------------------------------------------------------------
1883: */
1884: static void
1885: ParseTraditionalInclude (file)
1886: char *file; /* file specification */
1887: {
1888: char *fullname; /* full pathname of file */
1889: IFile *oldFile; /* state associated with current file */
1890: char *cp; /* current position in file spec */
1891: char *prefEnd;
1892:
1893: /*
1894: * Skip over whitespace
1895: */
1896: while ((*file == ' ') || (*file == '\t')) {
1897: file++;
1898: }
1899:
1900: if (*file == '\0') {
1901: Parse_Error (PARSE_FATAL,
1902: "Filename missing from \"include\"");
1903: return;
1904: }
1905:
1906: /*
1907: * Skip to end of line or next whitespace
1908: */
1909: for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1910: continue;
1911: }
1912:
1913: *cp = '\0';
1914:
1915: /*
1916: * Substitute for any variables in the file name before trying to
1917: * find the thing.
1918: */
1919: file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1920:
1921: /*
1922: * Now we know the file's name, we attempt to find the durn thing.
1923: * A return of NULL indicates the file don't exist.
1924: *
1925: * Include files are first searched for relative to the including
1926: * file's location. We don't want to cd there, of course, so we
1927: * just tack on the old file's leading path components and call
1928: * Dir_FindFile to see if we can locate the beast.
1929: * XXX - this *does* search in the current directory, right?
1930: */
1931:
1932: prefEnd = strrchr (fname, '/');
1933: if (prefEnd != (char *)NULL) {
1934: char *newName;
1935:
1936: *prefEnd = '\0';
1937: newName = str_concat (fname, file, STR_ADDSLASH);
1938: fullname = Dir_FindFile (newName, parseIncPath);
1939: if (fullname == (char *)NULL) {
1940: fullname = Dir_FindFile(newName, dirSearchPath);
1941: }
1942: free (newName);
1943: *prefEnd = '/';
1944: } else {
1945: fullname = (char *)NULL;
1946: }
1947:
1948: if (fullname == (char *)NULL) {
1949: /*
1950: * System makefile or makefile wasn't found in same directory as
1951: * included makefile. Search for it first on the -I search path,
1952: * then on the .PATH search path, if not found in a -I directory.
1953: * XXX: Suffix specific?
1954: */
1955: fullname = Dir_FindFile (file, parseIncPath);
1956: if (fullname == (char *)NULL) {
1957: fullname = Dir_FindFile(file, dirSearchPath);
1958: }
1959: }
1960:
1961: if (fullname == (char *)NULL) {
1962: /*
1963: * Still haven't found the makefile. Look for it on the system
1964: * path as a last resort.
1965: */
1966: fullname = Dir_FindFile(file, sysIncPath);
1967: }
1968:
1969: if (fullname == (char *) NULL) {
1970: Parse_Error (PARSE_FATAL, "Could not find %s", file);
1971: return;
1972: }
1973:
1974: /*
1975: * Once we find the absolute path to the file, we get to save all the
1976: * state from the current file before we can start reading this
1977: * include file. The state is stored in an IFile structure which
1978: * is placed on a list with other IFile structures. The list makes
1979: * a very nice stack to track how we got here...
1980: */
1981: oldFile = (IFile *) emalloc (sizeof (IFile));
1982: oldFile->fname = fname;
1983:
1984: oldFile->F = curFILE;
1985: oldFile->p = curPTR;
1986: oldFile->lineno = lineno;
1987:
1988: (void) Lst_AtFront (includes, (ClientData)oldFile);
1989:
1990: /*
1991: * Once the previous state has been saved, we can get down to reading
1992: * the new file. We set up the name of the file to be the absolute
1993: * name of the include file so error messages refer to the right
1994: * place. Naturally enough, we start reading at line number 0.
1995: */
1996: fname = fullname;
1997: lineno = 0;
1998:
1999: curFILE = fopen (fullname, "r");
2000: curPTR = NULL;
2001: if (curFILE == (FILE * ) NULL) {
2002: Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
2003: /*
2004: * Pop to previous file
2005: */
2006: (void) ParseEOF(1);
2007: }
2008: }
2009: #endif
2010:
2011: /*-
2012: *---------------------------------------------------------------------
2013: * ParseEOF --
2014: * Called when EOF is reached in the current file. If we were reading
2015: * an include file, the includes stack is popped and things set up
2016: * to go back to reading the previous file at the previous location.
2017: *
2018: * Results:
2019: * CONTINUE if there's more to do. DONE if not.
2020: *
2021: * Side Effects:
2022: * The old curFILE, is closed. The includes list is shortened.
2023: * lineno, curFILE, and fname are changed if CONTINUE is returned.
2024: *---------------------------------------------------------------------
2025: */
2026: static int
2027: ParseEOF (opened)
2028: int opened;
2029: {
2030: IFile *ifile; /* the state on the top of the includes stack */
2031:
2032: if (Lst_IsEmpty (includes)) {
2033: return (DONE);
2034: }
2035:
2036: ifile = (IFile *) Lst_DeQueue (includes);
2037: free ((Address) fname);
2038: fname = ifile->fname;
2039: lineno = ifile->lineno;
2040: if (opened && curFILE)
2041: (void) fclose (curFILE);
2042: if (curPTR) {
2043: free((Address) curPTR->str);
2044: free((Address) curPTR);
2045: }
2046: curFILE = ifile->F;
2047: curPTR = ifile->p;
2048: free ((Address)ifile);
2049: return (CONTINUE);
2050: }
2051:
2052: /*-
2053: *---------------------------------------------------------------------
2054: * ParseReadc --
2055: * Read a character from the current file
2056: *
2057: * Results:
2058: * The character that was read
2059: *
2060: * Side Effects:
2061: *---------------------------------------------------------------------
2062: */
2063: static int
2064: ParseReadc()
2065: {
2066: if (curFILE)
2067: return fgetc(curFILE);
2068:
2069: if (curPTR && *curPTR->ptr)
2070: return *curPTR->ptr++;
2071: return EOF;
2072: }
2073:
2074:
2075: /*-
2076: *---------------------------------------------------------------------
2077: * ParseUnreadc --
2078: * Put back a character to the current file
2079: *
2080: * Results:
2081: * None.
2082: *
2083: * Side Effects:
2084: *---------------------------------------------------------------------
2085: */
2086: static void
2087: ParseUnreadc(c)
2088: int c;
2089: {
2090: if (curFILE) {
2091: ungetc(c, curFILE);
2092: return;
2093: }
2094: if (curPTR) {
2095: *--(curPTR->ptr) = c;
2096: return;
2097: }
2098: }
2099:
2100:
2101: /* ParseSkipLine():
2102: * Grab the next line
2103: */
2104: static char *
2105: ParseSkipLine(skip)
2106: int skip; /* Skip lines that don't start with . */
2107: {
2108: char *line;
2109: int c, lastc = '\0', lineLength;
2110: Buffer buf;
2111:
2112: c = ParseReadc();
2113:
2114: if (skip) {
2115: /*
2116: * Skip lines until get to one that begins with a
2117: * special char.
2118: */
2119: while ((c != '.') && (c != EOF)) {
2120: while (((c != '\n') || (lastc == '\\')) && (c != EOF))
2121: {
2122: /*
2123: * Advance to next unescaped newline
2124: */
2125: if ((lastc = c) == '\n') {
2126: lineno++;
2127: }
2128: c = ParseReadc();
2129: }
2130: lineno++;
2131:
2132: lastc = c;
2133: c = ParseReadc ();
2134: }
2135: }
2136:
2137: if (c == EOF) {
2138: Parse_Error (PARSE_FATAL, "Unclosed conditional/for loop");
2139: return ((char *)NULL);
2140: }
2141:
2142: /*
2143: * Read the entire line into buf
2144: */
2145: buf = Buf_Init (MAKE_BSIZE);
2146: if (c != '\n') {
2147: do {
2148: Buf_AddByte (buf, (Byte)c);
2149: c = ParseReadc();
2150: } while ((c != '\n') && (c != EOF));
2151: }
2152: lineno++;
2153:
2154: Buf_AddByte (buf, (Byte)'\0');
2155: line = (char *)Buf_GetAll (buf, &lineLength);
2156: Buf_Destroy (buf, FALSE);
2157: return line;
2158: }
2159:
2160:
2161: /*-
2162: *---------------------------------------------------------------------
2163: * ParseReadLine --
2164: * Read an entire line from the input file. Called only by Parse_File.
2165: * To facilitate escaped newlines and what have you, a character is
2166: * buffered in 'lastc', which is '\0' when no characters have been
2167: * read. When we break out of the loop, c holds the terminating
2168: * character and lastc holds a character that should be added to
2169: * the line (unless we don't read anything but a terminator).
2170: *
2171: * Results:
2172: * A line w/o its newline
2173: *
2174: * Side Effects:
2175: * Only those associated with reading a character
2176: *---------------------------------------------------------------------
2177: */
2178: static char *
2179: ParseReadLine ()
2180: {
2181: Buffer buf; /* Buffer for current line */
2182: register int c; /* the current character */
2183: register int lastc; /* The most-recent character */
2184: Boolean semiNL; /* treat semi-colons as newlines */
2185: Boolean ignDepOp; /* TRUE if should ignore dependency operators
2186: * for the purposes of setting semiNL */
2187: Boolean ignComment; /* TRUE if should ignore comments (in a
2188: * shell command */
2189: char *line; /* Result */
2190: char *ep; /* to strip trailing blanks */
2191: int lineLength; /* Length of result */
2192:
2193: semiNL = FALSE;
2194: ignDepOp = FALSE;
2195: ignComment = FALSE;
2196:
2197: /*
2198: * Handle special-characters at the beginning of the line. Either a
2199: * leading tab (shell command) or pound-sign (possible conditional)
2200: * forces us to ignore comments and dependency operators and treat
2201: * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2202: * discards completely blank lines.
2203: */
2204: for (;;) {
2205: c = ParseReadc();
2206:
2207: if (c == '\t') {
2208: ignComment = ignDepOp = TRUE;
2209: break;
2210: } else if (c == '\n') {
2211: lineno++;
2212: } else if (c == '#') {
2213: ParseUnreadc(c);
2214: break;
2215: } else {
2216: /*
2217: * Anything else breaks out without doing anything
2218: */
2219: break;
2220: }
2221: }
2222:
2223: if (c != EOF) {
2224: lastc = c;
2225: buf = Buf_Init(MAKE_BSIZE);
2226:
2227: while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) &&
2228: (c != EOF))
2229: {
2230: test_char:
2231: switch(c) {
2232: case '\n':
2233: /*
2234: * Escaped newline: read characters until a non-space or an
2235: * unescaped newline and replace them all by a single space.
2236: * This is done by storing the space over the backslash and
2237: * dropping through with the next nonspace. If it is a
2238: * semi-colon and semiNL is TRUE, it will be recognized as a
2239: * newline in the code below this...
2240: */
2241: lineno++;
2242: lastc = ' ';
2243: while ((c = ParseReadc ()) == ' ' || c == '\t') {
2244: continue;
2245: }
2246: if (c == EOF || c == '\n') {
2247: goto line_read;
2248: } else {
2249: /*
2250: * Check for comments, semiNL's, etc. -- easier than
2251: * ParseUnreadc(c); continue;
2252: */
2253: goto test_char;
2254: }
2255: /*NOTREACHED*/
2256: break;
2257:
2258: case ';':
2259: /*
2260: * Semi-colon: Need to see if it should be interpreted as a
2261: * newline
2262: */
2263: if (semiNL) {
2264: /*
2265: * To make sure the command that may be following this
2266: * semi-colon begins with a tab, we push one back into the
2267: * input stream. This will overwrite the semi-colon in the
2268: * buffer. If there is no command following, this does no
2269: * harm, since the newline remains in the buffer and the
2270: * whole line is ignored.
2271: */
2272: ParseUnreadc('\t');
2273: goto line_read;
2274: }
2275: break;
2276: case '=':
2277: if (!semiNL) {
2278: /*
2279: * Haven't seen a dependency operator before this, so this
2280: * must be a variable assignment -- don't pay attention to
2281: * dependency operators after this.
2282: */
2283: ignDepOp = TRUE;
2284: } else if (lastc == ':' || lastc == '!') {
2285: /*
2286: * Well, we've seen a dependency operator already, but it
2287: * was the previous character, so this is really just an
2288: * expanded variable assignment. Revert semi-colons to
2289: * being just semi-colons again and ignore any more
2290: * dependency operators.
2291: *
2292: * XXX: Note that a line like "foo : a:=b" will blow up,
2293: * but who'd write a line like that anyway?
2294: */
2295: ignDepOp = TRUE; semiNL = FALSE;
2296: }
2297: break;
2298: case '#':
2299: if (!ignComment) {
1.2 deraadt 2300: if (
2301: #if 0
2302: compatMake &&
2303: #endif
2304: (lastc != '\\')) {
1.1 deraadt 2305: /*
2306: * If the character is a hash mark and it isn't escaped
2307: * (or we're being compatible), the thing is a comment.
2308: * Skip to the end of the line.
2309: */
2310: do {
2311: c = ParseReadc();
2312: } while ((c != '\n') && (c != EOF));
2313: goto line_read;
2314: } else {
2315: /*
2316: * Don't add the backslash. Just let the # get copied
2317: * over.
2318: */
2319: lastc = c;
2320: continue;
2321: }
2322: }
2323: break;
2324: case ':':
2325: case '!':
2326: if (!ignDepOp && (c == ':' || c == '!')) {
2327: /*
2328: * A semi-colon is recognized as a newline only on
2329: * dependency lines. Dependency lines are lines with a
2330: * colon or an exclamation point. Ergo...
2331: */
2332: semiNL = TRUE;
2333: }
2334: break;
2335: }
2336: /*
2337: * Copy in the previous character and save this one in lastc.
2338: */
2339: Buf_AddByte (buf, (Byte)lastc);
2340: lastc = c;
2341:
2342: }
2343: line_read:
2344: lineno++;
2345:
2346: if (lastc != '\0') {
2347: Buf_AddByte (buf, (Byte)lastc);
2348: }
2349: Buf_AddByte (buf, (Byte)'\0');
2350: line = (char *)Buf_GetAll (buf, &lineLength);
2351: Buf_Destroy (buf, FALSE);
2352:
2353: /*
2354: * Strip trailing blanks and tabs from the line.
2355: * Do not strip a blank or tab that is preceeded by
2356: * a '\'
2357: */
2358: ep = line;
2359: while (*ep)
2360: ++ep;
2361: while (ep > line && (ep[-1] == ' ' || ep[-1] == '\t')) {
2362: if (ep > line + 1 && ep[-2] == '\\')
2363: break;
2364: --ep;
2365: }
2366: *ep = 0;
2367:
2368: if (line[0] == '.') {
2369: /*
2370: * The line might be a conditional. Ask the conditional module
2371: * about it and act accordingly
2372: */
2373: switch (Cond_Eval (line)) {
2374: case COND_SKIP:
2375: /*
2376: * Skip to next conditional that evaluates to COND_PARSE.
2377: */
2378: do {
2379: free (line);
2380: line = ParseSkipLine(1);
2381: } while (line && Cond_Eval(line) != COND_PARSE);
2382: if (line == NULL)
2383: break;
2384: /*FALLTHRU*/
2385: case COND_PARSE:
2386: free ((Address) line);
2387: line = ParseReadLine();
2388: break;
2389: case COND_INVALID:
2390: if (For_Eval(line)) {
2391: int ok;
2392: free(line);
2393: do {
2394: /*
2395: * Skip after the matching end
2396: */
2397: line = ParseSkipLine(0);
2398: if (line == NULL) {
2399: Parse_Error (PARSE_FATAL,
2400: "Unexpected end of file in for loop.\n");
2401: break;
2402: }
2403: ok = For_Eval(line);
2404: free(line);
2405: }
2406: while (ok);
2407: if (line != NULL)
2408: For_Run();
2409: line = ParseReadLine();
2410: }
2411: break;
2412: }
2413: }
2414: return (line);
2415:
2416: } else {
2417: /*
2418: * Hit end-of-file, so return a NULL line to indicate this.
2419: */
2420: return((char *)NULL);
2421: }
2422: }
2423:
2424: /*-
2425: *-----------------------------------------------------------------------
2426: * ParseFinishLine --
2427: * Handle the end of a dependency group.
2428: *
2429: * Results:
2430: * Nothing.
2431: *
2432: * Side Effects:
2433: * inLine set FALSE. 'targets' list destroyed.
2434: *
2435: *-----------------------------------------------------------------------
2436: */
2437: static void
2438: ParseFinishLine()
2439: {
2440: if (inLine) {
2441: Lst_ForEach(targets, Suff_EndTransform, (ClientData)NULL);
2442: Lst_Destroy (targets, ParseHasCommands);
2443: targets = NULL;
2444: inLine = FALSE;
2445: }
2446: }
2447:
2448:
2449: /*-
2450: *---------------------------------------------------------------------
2451: * Parse_File --
2452: * Parse a file into its component parts, incorporating it into the
2453: * current dependency graph. This is the main function and controls
2454: * almost every other function in this module
2455: *
2456: * Results:
2457: * None
2458: *
2459: * Side Effects:
2460: * Loads. Nodes are added to the list of all targets, nodes and links
2461: * are added to the dependency graph. etc. etc. etc.
2462: *---------------------------------------------------------------------
2463: */
2464: void
2465: Parse_File(name, stream)
2466: char *name; /* the name of the file being read */
2467: FILE * stream; /* Stream open to makefile to parse */
2468: {
2469: register char *cp, /* pointer into the line */
2470: *line; /* the line we're working on */
2471:
2472: inLine = FALSE;
2473: fname = name;
2474: curFILE = stream;
2475: lineno = 0;
2476: fatals = 0;
2477:
2478: do {
2479: while ((line = ParseReadLine ()) != NULL) {
2480: if (*line == '.') {
2481: /*
2482: * Lines that begin with the special character are either
2483: * include or undef directives.
2484: */
2485: for (cp = line + 1; isspace (*cp); cp++) {
2486: continue;
2487: }
2488: if (strncmp (cp, "include", 7) == 0) {
2489: ParseDoInclude (cp + 7);
2490: goto nextLine;
2491: } else if (strncmp(cp, "undef", 5) == 0) {
2492: char *cp2;
2493: for (cp += 5; isspace((unsigned char) *cp); cp++) {
2494: continue;
2495: }
2496:
2497: for (cp2 = cp; !isspace((unsigned char) *cp2) &&
2498: (*cp2 != '\0'); cp2++) {
2499: continue;
2500: }
2501:
2502: *cp2 = '\0';
2503:
2504: Var_Delete(cp, VAR_GLOBAL);
2505: goto nextLine;
2506: }
2507: }
2508: if (*line == '#' || *line == '\0') {
2509: /* If we're this far, the line must be a comment.
2510: (Empty lines are ignored as well) */
2511: goto nextLine;
2512: }
2513:
2514: if (*line == '\t') {
2515: /*
2516: * If a line starts with a tab, it can only hope to be
2517: * a creation command.
2518: */
2519: #ifndef POSIX
2520: shellCommand:
2521: #endif
2522: for (cp = line + 1; isspace (*cp); cp++) {
2523: continue;
2524: }
2525: if (*cp) {
2526: if (inLine) {
2527: /*
2528: * So long as it's not a blank line and we're actually
2529: * in a dependency spec, add the command to the list of
2530: * commands of all targets in the dependency spec
2531: */
2532: Lst_ForEach (targets, ParseAddCmd, cp);
2533: Lst_AtEnd(targCmds, (ClientData) line);
2534: continue;
2535: } else {
2536: Parse_Error (PARSE_FATAL,
2537: "Unassociated shell command \"%.20s\"",
2538: cp);
2539: }
2540: }
2541: #ifdef SYSVINCLUDE
2542: } else if (strncmp (line, "include", 7) == 0 &&
1.6 tholo 2543: isspace((unsigned char) line[7]) &&
1.1 deraadt 2544: strchr(line, ':') == NULL) {
2545: /*
2546: * It's an S3/S5-style "include".
2547: */
2548: ParseTraditionalInclude (line + 7);
2549: goto nextLine;
2550: #endif
2551: } else if (Parse_IsVar (line)) {
2552: ParseFinishLine();
2553: Parse_DoVar (line, VAR_GLOBAL);
2554: } else {
2555: /*
2556: * We now know it's a dependency line so it needs to have all
2557: * variables expanded before being parsed. Tell the variable
2558: * module to complain if some variable is undefined...
2559: * To make life easier on novices, if the line is indented we
2560: * first make sure the line has a dependency operator in it.
2561: * If it doesn't have an operator and we're in a dependency
2562: * line's script, we assume it's actually a shell command
2563: * and add it to the current list of targets.
2564: */
2565: #ifndef POSIX
2566: Boolean nonSpace = FALSE;
2567: #endif
2568:
2569: cp = line;
2570: if (isspace((unsigned char) line[0])) {
2571: while ((*cp != '\0') && isspace((unsigned char) *cp)) {
2572: cp++;
2573: }
2574: if (*cp == '\0') {
2575: goto nextLine;
2576: }
2577: #ifndef POSIX
2578: while ((*cp != ':') && (*cp != '!') && (*cp != '\0')) {
2579: nonSpace = TRUE;
2580: cp++;
2581: }
2582: #endif
2583: }
2584:
2585: #ifndef POSIX
2586: if (*cp == '\0') {
2587: if (inLine) {
2588: Parse_Error (PARSE_WARNING,
2589: "Shell command needs a leading tab");
2590: goto shellCommand;
2591: } else if (nonSpace) {
2592: Parse_Error (PARSE_FATAL, "Missing operator");
2593: }
2594: } else {
2595: #endif
2596: ParseFinishLine();
2597:
2598: cp = Var_Subst (NULL, line, VAR_CMD, TRUE);
2599: free (line);
2600: line = cp;
2601:
2602: /*
2603: * Need a non-circular list for the target nodes
2604: */
2605: if (targets)
2606: Lst_Destroy(targets, NOFREE);
2607:
2608: targets = Lst_Init (FALSE);
2609: inLine = TRUE;
2610:
2611: ParseDoDependency (line);
2612: #ifndef POSIX
2613: }
2614: #endif
2615: }
2616:
2617: nextLine:
2618:
2619: free (line);
2620: }
2621: /*
2622: * Reached EOF, but it may be just EOF of an include file...
2623: */
2624: } while (ParseEOF(1) == CONTINUE);
2625:
2626: /*
2627: * Make sure conditionals are clean
2628: */
2629: Cond_End();
2630:
2631: if (fatals) {
2632: fprintf (stderr, "Fatal errors encountered -- cannot continue\n");
2633: exit (1);
2634: }
2635: }
2636:
2637: /*-
2638: *---------------------------------------------------------------------
2639: * Parse_Init --
2640: * initialize the parsing module
2641: *
2642: * Results:
2643: * none
2644: *
2645: * Side Effects:
2646: * the parseIncPath list is initialized...
2647: *---------------------------------------------------------------------
2648: */
2649: void
2650: Parse_Init ()
2651: {
2652: mainNode = NILGNODE;
2653: parseIncPath = Lst_Init (FALSE);
2654: sysIncPath = Lst_Init (FALSE);
2655: includes = Lst_Init (FALSE);
2656: targCmds = Lst_Init (FALSE);
2657: }
2658:
2659: void
2660: Parse_End()
2661: {
2662: Lst_Destroy(targCmds, (void (*) __P((ClientData))) free);
2663: if (targets)
2664: Lst_Destroy(targets, NOFREE);
2665: Lst_Destroy(sysIncPath, Dir_Destroy);
2666: Lst_Destroy(parseIncPath, Dir_Destroy);
2667: Lst_Destroy(includes, NOFREE); /* Should be empty now */
2668: }
2669:
2670:
2671: /*-
2672: *-----------------------------------------------------------------------
2673: * Parse_MainName --
2674: * Return a Lst of the main target to create for main()'s sake. If
2675: * no such target exists, we Punt with an obnoxious error message.
2676: *
2677: * Results:
2678: * A Lst of the single node to create.
2679: *
2680: * Side Effects:
2681: * None.
2682: *
2683: *-----------------------------------------------------------------------
2684: */
2685: Lst
2686: Parse_MainName()
2687: {
2688: Lst main; /* result list */
2689:
2690: main = Lst_Init (FALSE);
2691:
2692: if (mainNode == NILGNODE) {
2693: Punt ("make: no target to make.\n");
2694: /*NOTREACHED*/
2695: } else if (mainNode->type & OP_DOUBLEDEP) {
2696: (void) Lst_AtEnd (main, (ClientData)mainNode);
2697: Lst_Concat(main, mainNode->cohorts, LST_CONCNEW);
2698: }
2699: else
2700: (void) Lst_AtEnd (main, (ClientData)mainNode);
2701: return (main);
2702: }