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