Annotation of src/usr.bin/make/parse.c, Revision 1.38
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 */
1595: if (!(gn->type & OP_HAS_COMMANDS))
1.35 espie 1596: Lst_AtEnd(gn->commands, cmd);
1.1 deraadt 1597: return(0);
1598: }
1599:
1600: /*-
1601: *-----------------------------------------------------------------------
1602: * ParseHasCommands --
1603: * Callback procedure for Parse_File when destroying the list of
1604: * targets on the last dependency line. Marks a target as already
1605: * having commands if it does, to keep from having shell commands
1606: * on multiple dependency lines.
1607: *
1608: * Results:
1609: * None
1610: *
1611: * Side Effects:
1612: * OP_HAS_COMMANDS may be set for the target.
1613: *
1614: *-----------------------------------------------------------------------
1615: */
1616: static void
1617: ParseHasCommands(gnp)
1618: ClientData gnp; /* Node to examine */
1619: {
1620: GNode *gn = (GNode *) gnp;
1621: if (!Lst_IsEmpty(gn->commands)) {
1622: gn->type |= OP_HAS_COMMANDS;
1623: }
1624: }
1625:
1626: /*-
1627: *-----------------------------------------------------------------------
1628: * Parse_AddIncludeDir --
1629: * Add a directory to the path searched for included makefiles
1630: * bracketed by double-quotes. Used by functions in main.c
1631: *
1632: * Results:
1633: * None.
1634: *
1635: * Side Effects:
1636: * The directory is appended to the list.
1637: *
1638: *-----------------------------------------------------------------------
1639: */
1640: void
1641: Parse_AddIncludeDir (dir)
1642: char *dir; /* The name of the directory to add */
1643: {
1644: Dir_AddDir (parseIncPath, dir);
1645: }
1646:
1647: /*-
1648: *---------------------------------------------------------------------
1649: * ParseDoInclude --
1650: * Push to another file.
1.11 millert 1651: *
1.1 deraadt 1652: * The input is the line minus the #include. A file spec is a string
1653: * enclosed in <> or "". The former is looked for only in sysIncPath.
1654: * The latter in . and the directories specified by -I command line
1655: * options
1656: *
1657: * Results:
1658: * None
1659: *
1660: * Side Effects:
1661: * A structure is added to the includes Lst and readProc, lineno,
1662: * fname and curFILE are altered for the new file
1663: *---------------------------------------------------------------------
1664: */
1665: static void
1666: ParseDoInclude (file)
1667: char *file; /* file specification */
1668: {
1669: char *fullname; /* full pathname of file */
1670: IFile *oldFile; /* state associated with current file */
1671: char endc; /* the character which ends the file spec */
1672: char *cp; /* current position in file spec */
1673: Boolean isSystem; /* TRUE if makefile is a system makefile */
1674:
1675: /*
1676: * Skip to delimiter character so we know where to look
1677: */
1678: while ((*file == ' ') || (*file == '\t')) {
1679: file++;
1680: }
1681:
1682: if ((*file != '"') && (*file != '<')) {
1683: Parse_Error (PARSE_FATAL,
1684: ".include filename must be delimited by '\"' or '<'");
1685: return;
1686: }
1687:
1688: /*
1689: * Set the search path on which to find the include file based on the
1690: * characters which bracket its name. Angle-brackets imply it's
1691: * a system Makefile while double-quotes imply it's a user makefile
1692: */
1693: if (*file == '<') {
1694: isSystem = TRUE;
1695: endc = '>';
1696: } else {
1697: isSystem = FALSE;
1698: endc = '"';
1699: }
1700:
1701: /*
1702: * Skip to matching delimiter
1703: */
1704: for (cp = ++file; *cp && *cp != endc; cp++) {
1705: continue;
1706: }
1707:
1708: if (*cp != endc) {
1709: Parse_Error (PARSE_FATAL,
1710: "Unclosed %cinclude filename. '%c' expected",
1711: '.', endc);
1712: return;
1713: }
1714: *cp = '\0';
1715:
1716: /*
1717: * Substitute for any variables in the file name before trying to
1718: * find the thing.
1719: */
1.31 espie 1720: file = Var_Subst(file, VAR_CMD, FALSE);
1.1 deraadt 1721:
1722: /*
1723: * Now we know the file's name and its search path, we attempt to
1724: * find the durn thing. A return of NULL indicates the file don't
1725: * exist.
1726: */
1727: if (!isSystem) {
1728: /*
1729: * Include files contained in double-quotes are first searched for
1730: * relative to the including file's location. We don't want to
1731: * cd there, of course, so we just tack on the old file's
1732: * leading path components and call Dir_FindFile to see if
1733: * we can locate the beast.
1734: */
1.4 niklas 1735: char *prefEnd, *Fname;
1.1 deraadt 1736:
1.4 niklas 1737: /* Make a temporary copy of this, to be safe. */
1.9 briggs 1738: Fname = estrdup(fname);
1.4 niklas 1739:
1740: prefEnd = strrchr (Fname, '/');
1.1 deraadt 1741: if (prefEnd != (char *)NULL) {
1742: char *newName;
1.11 millert 1743:
1.1 deraadt 1744: *prefEnd = '\0';
1745: if (file[0] == '/')
1.9 briggs 1746: newName = estrdup(file);
1.1 deraadt 1747: else
1.4 niklas 1748: newName = str_concat (Fname, file, STR_ADDSLASH);
1.1 deraadt 1749: fullname = Dir_FindFile (newName, parseIncPath);
1750: if (fullname == (char *)NULL) {
1751: fullname = Dir_FindFile(newName, dirSearchPath);
1752: }
1753: free (newName);
1754: *prefEnd = '/';
1755: } else {
1756: fullname = (char *)NULL;
1757: }
1.4 niklas 1758: free (Fname);
1.1 deraadt 1759: } else {
1760: fullname = (char *)NULL;
1761: }
1762:
1763: if (fullname == (char *)NULL) {
1764: /*
1765: * System makefile or makefile wasn't found in same directory as
1766: * included makefile. Search for it first on the -I search path,
1767: * then on the .PATH search path, if not found in a -I directory.
1768: * XXX: Suffix specific?
1769: */
1770: fullname = Dir_FindFile (file, parseIncPath);
1771: if (fullname == (char *)NULL) {
1772: fullname = Dir_FindFile(file, dirSearchPath);
1773: }
1774: }
1775:
1776: if (fullname == (char *)NULL) {
1777: /*
1778: * Still haven't found the makefile. Look for it on the system
1779: * path as a last resort.
1780: */
1781: fullname = Dir_FindFile(file, sysIncPath);
1782: }
1783:
1784: if (fullname == (char *) NULL) {
1785: *cp = endc;
1786: Parse_Error (PARSE_FATAL, "Could not find %s", file);
1787: return;
1788: }
1789:
1790: free(file);
1791:
1792: /*
1793: * Once we find the absolute path to the file, we get to save all the
1794: * state from the current file before we can start reading this
1795: * include file. The state is stored in an IFile structure which
1796: * is placed on a list with other IFile structures. The list makes
1797: * a very nice stack to track how we got here...
1798: */
1799: oldFile = (IFile *) emalloc (sizeof (IFile));
1800: oldFile->fname = fname;
1801:
1802: oldFile->F = curFILE;
1803: oldFile->p = curPTR;
1804: oldFile->lineno = lineno;
1805:
1.37 espie 1806: Lst_AtFront(includes, oldFile);
1.1 deraadt 1807:
1808: /*
1809: * Once the previous state has been saved, we can get down to reading
1810: * the new file. We set up the name of the file to be the absolute
1811: * name of the include file so error messages refer to the right
1812: * place. Naturally enough, we start reading at line number 0.
1813: */
1814: fname = fullname;
1.38 ! espie 1815: #ifdef CLEANUP
! 1816: Lst_AtEnd(fileNames, fname);
! 1817: #endif
1.1 deraadt 1818: lineno = 0;
1819:
1820: curFILE = fopen (fullname, "r");
1821: curPTR = NULL;
1822: if (curFILE == (FILE * ) NULL) {
1823: Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1824: /*
1825: * Pop to previous file
1826: */
1827: (void) ParseEOF(0);
1828: }
1829: }
1830:
1831:
1832: /*-
1833: *---------------------------------------------------------------------
1834: * Parse_FromString --
1835: * Start Parsing from the given string
1.11 millert 1836: *
1.1 deraadt 1837: * Results:
1838: * None
1839: *
1840: * Side Effects:
1841: * A structure is added to the includes Lst and readProc, lineno,
1842: * fname and curFILE are altered for the new file
1843: *---------------------------------------------------------------------
1844: */
1845: void
1.24 espie 1846: Parse_FromString(str, newlineno)
1847: char *str;
1848: unsigned long newlineno;
1.1 deraadt 1849: {
1850: IFile *oldFile; /* state associated with this file */
1851:
1852: if (DEBUG(FOR))
1853: (void) fprintf(stderr, "%s\n----\n", str);
1854:
1855: oldFile = (IFile *) emalloc (sizeof (IFile));
1856: oldFile->lineno = lineno;
1857: oldFile->fname = fname;
1858: oldFile->F = curFILE;
1859: oldFile->p = curPTR;
1.11 millert 1860:
1.37 espie 1861: Lst_AtFront(includes, oldFile);
1.1 deraadt 1862:
1863: curFILE = NULL;
1864: curPTR = (PTR *) emalloc (sizeof (PTR));
1865: curPTR->str = curPTR->ptr = str;
1.24 espie 1866: lineno = newlineno;
1.1 deraadt 1867: }
1868:
1869:
1870: #ifdef SYSVINCLUDE
1871: /*-
1872: *---------------------------------------------------------------------
1873: * ParseTraditionalInclude --
1874: * Push to another file.
1.11 millert 1875: *
1.1 deraadt 1876: * The input is the line minus the "include". The file name is
1877: * the string following the "include".
1878: *
1879: * Results:
1880: * None
1881: *
1882: * Side Effects:
1883: * A structure is added to the includes Lst and readProc, lineno,
1884: * fname and curFILE are altered for the new file
1885: *---------------------------------------------------------------------
1886: */
1887: static void
1888: ParseTraditionalInclude (file)
1889: char *file; /* file specification */
1890: {
1891: char *fullname; /* full pathname of file */
1892: IFile *oldFile; /* state associated with current file */
1893: char *cp; /* current position in file spec */
1894: char *prefEnd;
1895:
1896: /*
1897: * Skip over whitespace
1898: */
1899: while ((*file == ' ') || (*file == '\t')) {
1900: file++;
1901: }
1902:
1903: if (*file == '\0') {
1904: Parse_Error (PARSE_FATAL,
1905: "Filename missing from \"include\"");
1906: return;
1907: }
1908:
1909: /*
1910: * Skip to end of line or next whitespace
1911: */
1912: for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1913: continue;
1914: }
1915:
1916: *cp = '\0';
1917:
1918: /*
1919: * Substitute for any variables in the file name before trying to
1920: * find the thing.
1921: */
1.31 espie 1922: file = Var_Subst(file, VAR_CMD, FALSE);
1.1 deraadt 1923:
1924: /*
1925: * Now we know the file's name, we attempt to find the durn thing.
1926: * A return of NULL indicates the file don't exist.
1927: *
1928: * Include files are first searched for relative to the including
1929: * file's location. We don't want to cd there, of course, so we
1930: * just tack on the old file's leading path components and call
1931: * Dir_FindFile to see if we can locate the beast.
1932: * XXX - this *does* search in the current directory, right?
1933: */
1934:
1935: prefEnd = strrchr (fname, '/');
1936: if (prefEnd != (char *)NULL) {
1937: char *newName;
1.11 millert 1938:
1.1 deraadt 1939: *prefEnd = '\0';
1940: newName = str_concat (fname, file, STR_ADDSLASH);
1941: fullname = Dir_FindFile (newName, parseIncPath);
1942: if (fullname == (char *)NULL) {
1943: fullname = Dir_FindFile(newName, dirSearchPath);
1944: }
1945: free (newName);
1946: *prefEnd = '/';
1947: } else {
1948: fullname = (char *)NULL;
1949: }
1950:
1951: if (fullname == (char *)NULL) {
1952: /*
1953: * System makefile or makefile wasn't found in same directory as
1954: * included makefile. Search for it first on the -I search path,
1955: * then on the .PATH search path, if not found in a -I directory.
1956: * XXX: Suffix specific?
1957: */
1958: fullname = Dir_FindFile (file, parseIncPath);
1959: if (fullname == (char *)NULL) {
1960: fullname = Dir_FindFile(file, dirSearchPath);
1961: }
1962: }
1963:
1964: if (fullname == (char *)NULL) {
1965: /*
1966: * Still haven't found the makefile. Look for it on the system
1967: * path as a last resort.
1968: */
1969: fullname = Dir_FindFile(file, sysIncPath);
1970: }
1971:
1972: if (fullname == (char *) NULL) {
1973: Parse_Error (PARSE_FATAL, "Could not find %s", file);
1974: return;
1975: }
1976:
1977: /*
1978: * Once we find the absolute path to the file, we get to save all the
1979: * state from the current file before we can start reading this
1980: * include file. The state is stored in an IFile structure which
1981: * is placed on a list with other IFile structures. The list makes
1982: * a very nice stack to track how we got here...
1983: */
1984: oldFile = (IFile *) emalloc (sizeof (IFile));
1985: oldFile->fname = fname;
1986:
1987: oldFile->F = curFILE;
1988: oldFile->p = curPTR;
1989: oldFile->lineno = lineno;
1990:
1.37 espie 1991: Lst_AtFront(includes, oldFile);
1.1 deraadt 1992:
1993: /*
1994: * Once the previous state has been saved, we can get down to reading
1995: * the new file. We set up the name of the file to be the absolute
1996: * name of the include file so error messages refer to the right
1997: * place. Naturally enough, we start reading at line number 0.
1998: */
1999: fname = fullname;
1.38 ! espie 2000: #ifdef CLEANUP
! 2001: lst_AtEnd(fileNames, fname);
! 2002: #endif
1.1 deraadt 2003: lineno = 0;
2004:
2005: curFILE = fopen (fullname, "r");
2006: curPTR = NULL;
2007: if (curFILE == (FILE * ) NULL) {
2008: Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
2009: /*
2010: * Pop to previous file
2011: */
2012: (void) ParseEOF(1);
2013: }
2014: }
2015: #endif
2016:
2017: /*-
2018: *---------------------------------------------------------------------
2019: * ParseEOF --
2020: * Called when EOF is reached in the current file. If we were reading
2021: * an include file, the includes stack is popped and things set up
2022: * to go back to reading the previous file at the previous location.
2023: *
2024: * Results:
2025: * CONTINUE if there's more to do. DONE if not.
2026: *
2027: * Side Effects:
2028: * The old curFILE, is closed. The includes list is shortened.
2029: * lineno, curFILE, and fname are changed if CONTINUE is returned.
2030: *---------------------------------------------------------------------
2031: */
2032: static int
2033: ParseEOF (opened)
2034: int opened;
2035: {
2036: IFile *ifile; /* the state on the top of the includes stack */
2037:
1.34 espie 2038: if ((ifile = (IFile *)Lst_DeQueue(includes)) == NULL)
2039: return DONE;
1.1 deraadt 2040: fname = ifile->fname;
2041: lineno = ifile->lineno;
2042: if (opened && curFILE)
2043: (void) fclose (curFILE);
2044: if (curPTR) {
2045: free((Address) curPTR->str);
2046: free((Address) curPTR);
2047: }
2048: curFILE = ifile->F;
2049: curPTR = ifile->p;
2050: free ((Address)ifile);
2051: return (CONTINUE);
2052: }
2053:
2054: /*-
2055: *---------------------------------------------------------------------
2056: * ParseReadc --
1.11 millert 2057: * Read a character from the current file
1.1 deraadt 2058: *
2059: * Results:
2060: * The character that was read
2061: *
2062: * Side Effects:
2063: *---------------------------------------------------------------------
2064: */
1.21 espie 2065: static int __inline
1.1 deraadt 2066: ParseReadc()
2067: {
2068: if (curFILE)
2069: return fgetc(curFILE);
1.11 millert 2070:
1.1 deraadt 2071: if (curPTR && *curPTR->ptr)
2072: return *curPTR->ptr++;
2073: return EOF;
2074: }
2075:
2076:
2077: /*-
2078: *---------------------------------------------------------------------
2079: * ParseUnreadc --
1.11 millert 2080: * Put back a character to the current file
1.1 deraadt 2081: *
2082: * Results:
2083: * None.
2084: *
2085: * Side Effects:
2086: *---------------------------------------------------------------------
2087: */
2088: static void
2089: ParseUnreadc(c)
2090: int c;
2091: {
2092: if (curFILE) {
2093: ungetc(c, curFILE);
2094: return;
2095: }
2096: if (curPTR) {
2097: *--(curPTR->ptr) = c;
2098: return;
2099: }
2100: }
2101:
2102:
2103: /* ParseSkipLine():
2104: * Grab the next line
2105: */
2106: static char *
2107: ParseSkipLine(skip)
2108: int skip; /* Skip lines that don't start with . */
2109: {
2110: char *line;
1.25 espie 2111: int c, lastc;
1.29 espie 2112: BUFFER buf;
1.1 deraadt 2113:
1.29 espie 2114: Buf_Init(&buf, MAKE_BSIZE);
1.11 millert 2115:
1.22 espie 2116: for (;;) {
1.29 espie 2117: Buf_Reset(&buf);
1.11 millert 2118: lastc = '\0';
2119:
2120: while (((c = ParseReadc()) != '\n' || lastc == '\\')
2121: && c != EOF) {
2122: if (c == '\n') {
1.29 espie 2123: Buf_ReplaceLastChar(&buf, ' ');
1.11 millert 2124: lineno++;
2125:
2126: while ((c = ParseReadc()) == ' ' || c == '\t');
2127:
2128: if (c == EOF)
2129: break;
2130: }
2131:
1.29 espie 2132: Buf_AddChar(&buf, c);
1.11 millert 2133: lastc = c;
2134: }
2135:
1.29 espie 2136: line = Buf_Retrieve(&buf);
1.22 espie 2137: lineno++;
2138: /* allow for non-newline terminated lines while skipping */
2139: if (line[0] == '.')
2140: break;
2141:
1.11 millert 2142: if (c == EOF) {
2143: Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
1.29 espie 2144: Buf_Destroy(&buf);
2145: return NULL;
1.11 millert 2146: }
1.22 espie 2147: if (skip == 0)
2148: break;
1.11 millert 2149:
1.22 espie 2150: }
1.1 deraadt 2151:
2152: return line;
2153: }
2154:
2155:
2156: /*-
2157: *---------------------------------------------------------------------
2158: * ParseReadLine --
2159: * Read an entire line from the input file. Called only by Parse_File.
2160: * To facilitate escaped newlines and what have you, a character is
2161: * buffered in 'lastc', which is '\0' when no characters have been
2162: * read. When we break out of the loop, c holds the terminating
2163: * character and lastc holds a character that should be added to
2164: * the line (unless we don't read anything but a terminator).
2165: *
2166: * Results:
2167: * A line w/o its newline
2168: *
2169: * Side Effects:
2170: * Only those associated with reading a character
2171: *---------------------------------------------------------------------
2172: */
2173: static char *
2174: ParseReadLine ()
2175: {
1.29 espie 2176: BUFFER buf; /* Buffer for current line */
1.1 deraadt 2177: register int c; /* the current character */
2178: register int lastc; /* The most-recent character */
2179: Boolean semiNL; /* treat semi-colons as newlines */
2180: Boolean ignDepOp; /* TRUE if should ignore dependency operators
2181: * for the purposes of setting semiNL */
2182: Boolean ignComment; /* TRUE if should ignore comments (in a
2183: * shell command */
2184: char *line; /* Result */
2185: char *ep; /* to strip trailing blanks */
2186:
2187: semiNL = FALSE;
2188: ignDepOp = FALSE;
2189: ignComment = FALSE;
2190:
2191: /*
2192: * Handle special-characters at the beginning of the line. Either a
2193: * leading tab (shell command) or pound-sign (possible conditional)
2194: * forces us to ignore comments and dependency operators and treat
2195: * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2196: * discards completely blank lines.
2197: */
2198: for (;;) {
2199: c = ParseReadc();
2200:
2201: if (c == '\t') {
2202: ignComment = ignDepOp = TRUE;
2203: break;
2204: } else if (c == '\n') {
2205: lineno++;
2206: } else if (c == '#') {
2207: ParseUnreadc(c);
2208: break;
2209: } else {
2210: /*
2211: * Anything else breaks out without doing anything
2212: */
2213: break;
2214: }
2215: }
1.11 millert 2216:
1.1 deraadt 2217: if (c != EOF) {
2218: lastc = c;
1.29 espie 2219: Buf_Init(&buf, MAKE_BSIZE);
1.11 millert 2220:
1.1 deraadt 2221: while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) &&
2222: (c != EOF))
2223: {
2224: test_char:
2225: switch(c) {
2226: case '\n':
2227: /*
2228: * Escaped newline: read characters until a non-space or an
2229: * unescaped newline and replace them all by a single space.
2230: * This is done by storing the space over the backslash and
2231: * dropping through with the next nonspace. If it is a
2232: * semi-colon and semiNL is TRUE, it will be recognized as a
2233: * newline in the code below this...
2234: */
2235: lineno++;
2236: lastc = ' ';
2237: while ((c = ParseReadc ()) == ' ' || c == '\t') {
2238: continue;
2239: }
2240: if (c == EOF || c == '\n') {
2241: goto line_read;
2242: } else {
2243: /*
2244: * Check for comments, semiNL's, etc. -- easier than
2245: * ParseUnreadc(c); continue;
2246: */
2247: goto test_char;
2248: }
2249: /*NOTREACHED*/
2250: break;
2251:
2252: case ';':
2253: /*
2254: * Semi-colon: Need to see if it should be interpreted as a
2255: * newline
2256: */
2257: if (semiNL) {
2258: /*
2259: * To make sure the command that may be following this
2260: * semi-colon begins with a tab, we push one back into the
2261: * input stream. This will overwrite the semi-colon in the
2262: * buffer. If there is no command following, this does no
2263: * harm, since the newline remains in the buffer and the
2264: * whole line is ignored.
2265: */
2266: ParseUnreadc('\t');
2267: goto line_read;
1.11 millert 2268: }
1.1 deraadt 2269: break;
2270: case '=':
2271: if (!semiNL) {
2272: /*
2273: * Haven't seen a dependency operator before this, so this
2274: * must be a variable assignment -- don't pay attention to
2275: * dependency operators after this.
2276: */
2277: ignDepOp = TRUE;
2278: } else if (lastc == ':' || lastc == '!') {
2279: /*
2280: * Well, we've seen a dependency operator already, but it
2281: * was the previous character, so this is really just an
2282: * expanded variable assignment. Revert semi-colons to
2283: * being just semi-colons again and ignore any more
2284: * dependency operators.
2285: *
2286: * XXX: Note that a line like "foo : a:=b" will blow up,
2287: * but who'd write a line like that anyway?
2288: */
2289: ignDepOp = TRUE; semiNL = FALSE;
2290: }
2291: break;
2292: case '#':
2293: if (!ignComment) {
1.2 deraadt 2294: if (
2295: #if 0
2296: compatMake &&
2297: #endif
2298: (lastc != '\\')) {
1.1 deraadt 2299: /*
2300: * If the character is a hash mark and it isn't escaped
2301: * (or we're being compatible), the thing is a comment.
2302: * Skip to the end of the line.
2303: */
2304: do {
2305: c = ParseReadc();
2306: } while ((c != '\n') && (c != EOF));
2307: goto line_read;
2308: } else {
2309: /*
2310: * Don't add the backslash. Just let the # get copied
2311: * over.
2312: */
2313: lastc = c;
2314: continue;
2315: }
2316: }
2317: break;
2318: case ':':
2319: case '!':
2320: if (!ignDepOp && (c == ':' || c == '!')) {
2321: /*
2322: * A semi-colon is recognized as a newline only on
2323: * dependency lines. Dependency lines are lines with a
2324: * colon or an exclamation point. Ergo...
2325: */
2326: semiNL = TRUE;
2327: }
2328: break;
2329: }
2330: /*
2331: * Copy in the previous character and save this one in lastc.
2332: */
1.29 espie 2333: Buf_AddChar(&buf, lastc);
1.1 deraadt 2334: lastc = c;
1.11 millert 2335:
1.1 deraadt 2336: }
2337: line_read:
2338: lineno++;
1.11 millert 2339:
1.28 espie 2340: if (lastc != '\0')
1.29 espie 2341: Buf_AddChar(&buf, lastc);
2342: line = Buf_Retrieve(&buf);
1.1 deraadt 2343:
2344: /*
2345: * Strip trailing blanks and tabs from the line.
2346: * Do not strip a blank or tab that is preceeded by
2347: * a '\'
2348: */
2349: ep = line;
2350: while (*ep)
2351: ++ep;
1.11 millert 2352: while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
1.1 deraadt 2353: if (ep > line + 1 && ep[-2] == '\\')
2354: break;
2355: --ep;
2356: }
2357: *ep = 0;
1.11 millert 2358:
1.1 deraadt 2359: if (line[0] == '.') {
2360: /*
2361: * The line might be a conditional. Ask the conditional module
2362: * about it and act accordingly
2363: */
2364: switch (Cond_Eval (line)) {
2365: case COND_SKIP:
2366: /*
2367: * Skip to next conditional that evaluates to COND_PARSE.
2368: */
2369: do {
2370: free (line);
2371: line = ParseSkipLine(1);
2372: } while (line && Cond_Eval(line) != COND_PARSE);
2373: if (line == NULL)
2374: break;
2375: /*FALLTHRU*/
2376: case COND_PARSE:
2377: free ((Address) line);
2378: line = ParseReadLine();
2379: break;
2380: case COND_INVALID:
1.30 espie 2381: {
2382: For *loop;
2383:
2384: loop = For_Eval(line);
2385: if (loop != NULL) {
2386: Boolean ok;
2387:
1.1 deraadt 2388: free(line);
2389: do {
1.30 espie 2390: /* Find the matching endfor. */
1.1 deraadt 2391: line = ParseSkipLine(0);
2392: if (line == NULL) {
1.30 espie 2393: Parse_Error(PARSE_FATAL,
1.1 deraadt 2394: "Unexpected end of file in for loop.\n");
1.30 espie 2395: return line;
1.1 deraadt 2396: }
1.30 espie 2397: ok = For_Accumulate(loop, line);
1.1 deraadt 2398: free(line);
1.30 espie 2399: } while (ok);
2400: For_Run(loop);
1.1 deraadt 2401: line = ParseReadLine();
2402: }
2403: break;
1.30 espie 2404: }
1.1 deraadt 2405: }
2406: }
2407: return (line);
2408:
2409: } else {
2410: /*
2411: * Hit end-of-file, so return a NULL line to indicate this.
2412: */
2413: return((char *)NULL);
2414: }
2415: }
2416:
2417: /*-
2418: *-----------------------------------------------------------------------
2419: * ParseFinishLine --
2420: * Handle the end of a dependency group.
2421: *
2422: * Results:
2423: * Nothing.
2424: *
2425: * Side Effects:
2426: * inLine set FALSE. 'targets' list destroyed.
2427: *
2428: *-----------------------------------------------------------------------
2429: */
2430: static void
2431: ParseFinishLine()
2432: {
2433: if (inLine) {
1.37 espie 2434: Lst_ForEach(targets, Suff_EndTransform, NULL);
1.1 deraadt 2435: Lst_Destroy (targets, ParseHasCommands);
2436: targets = NULL;
2437: inLine = FALSE;
2438: }
2439: }
1.11 millert 2440:
1.1 deraadt 2441:
2442: /*-
2443: *---------------------------------------------------------------------
2444: * Parse_File --
2445: * Parse a file into its component parts, incorporating it into the
2446: * current dependency graph. This is the main function and controls
2447: * almost every other function in this module
2448: *
2449: * Results:
2450: * None
2451: *
2452: * Side Effects:
2453: * Loads. Nodes are added to the list of all targets, nodes and links
2454: * are added to the dependency graph. etc. etc. etc.
2455: *---------------------------------------------------------------------
2456: */
2457: void
2458: Parse_File(name, stream)
2459: char *name; /* the name of the file being read */
2460: FILE * stream; /* Stream open to makefile to parse */
2461: {
2462: register char *cp, /* pointer into the line */
2463: *line; /* the line we're working on */
2464:
2465: inLine = FALSE;
1.38 ! espie 2466: fname = estrdup(name);
! 2467: #ifdef CLEANUP
! 2468: Lst_AtEnd(fileNames, fname);
! 2469: #endif
1.1 deraadt 2470: curFILE = stream;
2471: lineno = 0;
2472: fatals = 0;
2473:
2474: do {
2475: while ((line = ParseReadLine ()) != NULL) {
2476: if (*line == '.') {
2477: /*
2478: * Lines that begin with the special character are either
2479: * include or undef directives.
2480: */
2481: for (cp = line + 1; isspace (*cp); cp++) {
2482: continue;
2483: }
2484: if (strncmp (cp, "include", 7) == 0) {
2485: ParseDoInclude (cp + 7);
2486: goto nextLine;
2487: } else if (strncmp(cp, "undef", 5) == 0) {
2488: char *cp2;
2489: for (cp += 5; isspace((unsigned char) *cp); cp++) {
2490: continue;
2491: }
2492:
2493: for (cp2 = cp; !isspace((unsigned char) *cp2) &&
2494: (*cp2 != '\0'); cp2++) {
2495: continue;
2496: }
2497:
2498: *cp2 = '\0';
2499:
2500: Var_Delete(cp, VAR_GLOBAL);
2501: goto nextLine;
2502: }
2503: }
1.11 millert 2504: if (*line == '#') {
2505: /* If we're this far, the line must be a comment. */
1.1 deraadt 2506: goto nextLine;
2507: }
1.11 millert 2508:
1.1 deraadt 2509: if (*line == '\t') {
2510: /*
2511: * If a line starts with a tab, it can only hope to be
2512: * a creation command.
2513: */
2514: #ifndef POSIX
2515: shellCommand:
2516: #endif
2517: for (cp = line + 1; isspace (*cp); cp++) {
2518: continue;
2519: }
2520: if (*cp) {
2521: if (inLine) {
2522: /*
2523: * So long as it's not a blank line and we're actually
2524: * in a dependency spec, add the command to the list of
1.11 millert 2525: * commands of all targets in the dependency spec
1.1 deraadt 2526: */
2527: Lst_ForEach (targets, ParseAddCmd, cp);
1.20 espie 2528: #ifdef CLEANUP
1.37 espie 2529: Lst_AtEnd(targCmds, line);
1.20 espie 2530: #endif
1.1 deraadt 2531: continue;
2532: } else {
2533: Parse_Error (PARSE_FATAL,
1.10 briggs 2534: "Unassociated shell command \"%s\"",
1.1 deraadt 2535: cp);
2536: }
2537: }
2538: #ifdef SYSVINCLUDE
1.11 millert 2539: } else if (strncmp (line, "include", 7) == 0 &&
1.6 tholo 2540: isspace((unsigned char) line[7]) &&
1.1 deraadt 2541: strchr(line, ':') == NULL) {
2542: /*
2543: * It's an S3/S5-style "include".
2544: */
2545: ParseTraditionalInclude (line + 7);
2546: goto nextLine;
2547: #endif
2548: } else if (Parse_IsVar (line)) {
2549: ParseFinishLine();
2550: Parse_DoVar (line, VAR_GLOBAL);
2551: } else {
2552: /*
2553: * We now know it's a dependency line so it needs to have all
2554: * variables expanded before being parsed. Tell the variable
2555: * module to complain if some variable is undefined...
2556: * To make life easier on novices, if the line is indented we
2557: * first make sure the line has a dependency operator in it.
2558: * If it doesn't have an operator and we're in a dependency
2559: * line's script, we assume it's actually a shell command
2560: * and add it to the current list of targets.
2561: */
2562: #ifndef POSIX
2563: Boolean nonSpace = FALSE;
2564: #endif
1.11 millert 2565:
1.1 deraadt 2566: cp = line;
2567: if (isspace((unsigned char) line[0])) {
2568: while ((*cp != '\0') && isspace((unsigned char) *cp)) {
2569: cp++;
2570: }
2571: if (*cp == '\0') {
2572: goto nextLine;
2573: }
2574: #ifndef POSIX
2575: while ((*cp != ':') && (*cp != '!') && (*cp != '\0')) {
2576: nonSpace = TRUE;
2577: cp++;
2578: }
2579: #endif
2580: }
1.11 millert 2581:
1.1 deraadt 2582: #ifndef POSIX
2583: if (*cp == '\0') {
2584: if (inLine) {
2585: Parse_Error (PARSE_WARNING,
2586: "Shell command needs a leading tab");
2587: goto shellCommand;
2588: } else if (nonSpace) {
2589: Parse_Error (PARSE_FATAL, "Missing operator");
2590: }
2591: } else {
2592: #endif
2593: ParseFinishLine();
2594:
1.31 espie 2595: cp = Var_Subst(line, VAR_CMD, TRUE);
1.1 deraadt 2596: free (line);
2597: line = cp;
1.11 millert 2598:
1.1 deraadt 2599: /*
1.11 millert 2600: * Need a non-circular list for the target nodes
1.1 deraadt 2601: */
2602: if (targets)
2603: Lst_Destroy(targets, NOFREE);
2604:
1.32 espie 2605: targets = Lst_Init();
1.1 deraadt 2606: inLine = TRUE;
1.11 millert 2607:
1.1 deraadt 2608: ParseDoDependency (line);
2609: #ifndef POSIX
2610: }
2611: #endif
2612: }
2613:
2614: nextLine:
2615:
2616: free (line);
2617: }
2618: /*
1.11 millert 2619: * Reached EOF, but it may be just EOF of an include file...
1.1 deraadt 2620: */
2621: } while (ParseEOF(1) == CONTINUE);
2622:
2623: /*
2624: * Make sure conditionals are clean
2625: */
2626: Cond_End();
2627:
2628: if (fatals) {
2629: fprintf (stderr, "Fatal errors encountered -- cannot continue\n");
2630: exit (1);
2631: }
2632: }
2633:
2634: /*-
2635: *---------------------------------------------------------------------
2636: * Parse_Init --
2637: * initialize the parsing module
2638: *
2639: * Results:
2640: * none
2641: *
2642: * Side Effects:
2643: * the parseIncPath list is initialized...
2644: *---------------------------------------------------------------------
2645: */
2646: void
2647: Parse_Init ()
2648: {
1.33 espie 2649: mainNode = NULL;
1.32 espie 2650: parseIncPath = Lst_Init();
2651: sysIncPath = Lst_Init();
2652: includes = Lst_Init();
1.20 espie 2653: #ifdef CLEANUP
1.32 espie 2654: targCmds = Lst_Init();
1.38 ! espie 2655: fileNames = Lst_Init();
1.20 espie 2656: #endif
1.1 deraadt 2657: }
2658:
2659: void
2660: Parse_End()
2661: {
1.20 espie 2662: #ifdef CLEANUP
1.1 deraadt 2663: Lst_Destroy(targCmds, (void (*) __P((ClientData))) free);
1.38 ! espie 2664: Lst_Destroy(fileNames, (void (*) __P((ClientData))) free);
1.1 deraadt 2665: if (targets)
2666: Lst_Destroy(targets, NOFREE);
2667: Lst_Destroy(sysIncPath, Dir_Destroy);
2668: Lst_Destroy(parseIncPath, Dir_Destroy);
2669: Lst_Destroy(includes, NOFREE); /* Should be empty now */
1.20 espie 2670: #endif
1.1 deraadt 2671: }
1.11 millert 2672:
1.1 deraadt 2673:
2674: /*-
2675: *-----------------------------------------------------------------------
2676: * Parse_MainName --
2677: * Return a Lst of the main target to create for main()'s sake. If
2678: * no such target exists, we Punt with an obnoxious error message.
2679: *
2680: * Results:
2681: * A Lst of the single node to create.
2682: *
2683: * Side Effects:
2684: * None.
2685: *
2686: *-----------------------------------------------------------------------
2687: */
2688: Lst
2689: Parse_MainName()
2690: {
1.14 millert 2691: Lst listmain; /* result list */
1.1 deraadt 2692:
1.32 espie 2693: listmain = Lst_Init();
1.1 deraadt 2694:
1.33 espie 2695: if (mainNode == NULL) {
1.8 deraadt 2696: Punt ("no target to make.");
1.1 deraadt 2697: /*NOTREACHED*/
2698: } else if (mainNode->type & OP_DOUBLEDEP) {
1.37 espie 2699: Lst_AtEnd(listmain, mainNode);
1.14 millert 2700: Lst_Concat(listmain, mainNode->cohorts, LST_CONCNEW);
1.1 deraadt 2701: }
2702: else
1.37 espie 2703: Lst_AtEnd(listmain, mainNode);
1.14 millert 2704: return (listmain);
1.1 deraadt 2705: }
1.24 espie 2706:
2707: unsigned long
2708: Parse_Getlineno()
2709: {
2710: return lineno;
1.38 ! espie 2711: }
! 2712:
! 2713: const char *
! 2714: Parse_Getfilename()
! 2715: {
! 2716: return fname;
1.24 espie 2717: }
2718: