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