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