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