Annotation of src/usr.bin/make/parse.c, Revision 1.19
1.19 ! millert 1: /* $OpenBSD: parse.c,v 1.18 1999/05/04 16:09:25 millert 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.19 ! millert 46: static char rcsid[] = "$OpenBSD: parse.c,v 1.18 1999/05/04 16:09:25 millert 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 */
115: static Lst targCmds; /* command lines for targets */
116: static Boolean inLine; /* true if currently in a dependency
117: * line or its commands */
118: typedef struct {
119: char *str;
120: char *ptr;
121: } PTR;
122:
123: static char *fname; /* name of current file (for errors) */
124: static int lineno; /* line number in current file */
125: static FILE *curFILE = NULL; /* current makefile */
126:
127: static PTR *curPTR = NULL; /* current makefile */
128:
129: static int fatals = 0;
130:
131: static GNode *mainNode; /* The main target to create. This is the
132: * first target on the first dependency
133: * line in the first makefile */
134: /*
135: * Definitions for handling #include specifications
136: */
137: typedef struct IFile {
138: char *fname; /* name of previous file */
139: int lineno; /* saved line number */
140: FILE * F; /* the open stream */
141: PTR * p; /* the char pointer */
142: } IFile;
143:
144: static Lst includes; /* stack of IFiles generated by
145: * #includes */
146: Lst parseIncPath; /* list of directories for "..." includes */
147: Lst sysIncPath; /* list of directories for <...> includes */
148:
149: /*-
150: * specType contains the SPECial TYPE of the current target. It is
151: * Not if the target is unspecial. If it *is* special, however, the children
152: * are linked as children of the parent but not vice versa. This variable is
153: * set in ParseDoDependency
154: */
155: typedef enum {
156: Begin, /* .BEGIN */
157: Default, /* .DEFAULT */
158: End, /* .END */
159: Ignore, /* .IGNORE */
160: Includes, /* .INCLUDES */
161: Interrupt, /* .INTERRUPT */
162: Libs, /* .LIBS */
163: MFlags, /* .MFLAGS or .MAKEFLAGS */
164: Main, /* .MAIN and we don't have anything user-specified to
165: * make */
166: NoExport, /* .NOEXPORT */
1.17 espie 167: NoPath, /* .NOPATH */
1.1 deraadt 168: Not, /* Not special */
169: NotParallel, /* .NOTPARALELL */
170: Null, /* .NULL */
171: Order, /* .ORDER */
1.3 deraadt 172: Parallel, /* .PARALLEL */
1.1 deraadt 173: ExPath, /* .PATH */
1.7 niklas 174: Phony, /* .PHONY */
1.1 deraadt 175: Precious, /* .PRECIOUS */
176: ExShell, /* .SHELL */
177: Silent, /* .SILENT */
178: SingleShell, /* .SINGLESHELL */
179: Suffixes, /* .SUFFIXES */
1.3 deraadt 180: Wait, /* .WAIT */
1.1 deraadt 181: Attribute /* Generic attribute */
182: } ParseSpecial;
183:
184: static ParseSpecial specType;
1.3 deraadt 185: static int waiting;
1.1 deraadt 186:
187: /*
188: * Predecessor node for handling .ORDER. Initialized to NILGNODE when .ORDER
189: * seen, then set to each successive source on the line.
190: */
191: static GNode *predecessor;
192:
193: /*
194: * The parseKeywords table is searched using binary search when deciding
195: * if a target or source is special. The 'spec' field is the ParseSpecial
196: * type of the keyword ("Not" if the keyword isn't special as a target) while
197: * the 'op' field is the operator to apply to the list of targets if the
198: * keyword is used as a source ("0" if the keyword isn't special as a source)
199: */
200: static struct {
201: char *name; /* Name of keyword */
202: ParseSpecial spec; /* Type when used as a target */
203: int op; /* Operator when used as a source */
204: } parseKeywords[] = {
205: { ".BEGIN", Begin, 0 },
206: { ".DEFAULT", Default, 0 },
207: { ".END", End, 0 },
208: { ".EXEC", Attribute, OP_EXEC },
209: { ".IGNORE", Ignore, OP_IGNORE },
210: { ".INCLUDES", Includes, 0 },
211: { ".INTERRUPT", Interrupt, 0 },
212: { ".INVISIBLE", Attribute, OP_INVISIBLE },
213: { ".JOIN", Attribute, OP_JOIN },
214: { ".LIBS", Libs, 0 },
1.13 millert 215: { ".MADE", Attribute, OP_MADE },
1.1 deraadt 216: { ".MAIN", Main, 0 },
217: { ".MAKE", Attribute, OP_MAKE },
218: { ".MAKEFLAGS", MFlags, 0 },
219: { ".MFLAGS", MFlags, 0 },
1.17 espie 220: #if 0 /* basic scaffolding for NOPATH, not working yet */
221: { ".NOPATH", NoPath, OP_NOPATH },
222: #endif
1.1 deraadt 223: { ".NOTMAIN", Attribute, OP_NOTMAIN },
224: { ".NOTPARALLEL", NotParallel, 0 },
1.3 deraadt 225: { ".NO_PARALLEL", NotParallel, 0 },
1.1 deraadt 226: { ".NULL", Null, 0 },
227: { ".OPTIONAL", Attribute, OP_OPTIONAL },
228: { ".ORDER", Order, 0 },
1.3 deraadt 229: { ".PARALLEL", Parallel, 0 },
1.1 deraadt 230: { ".PATH", ExPath, 0 },
1.7 niklas 231: { ".PHONY", Phony, OP_PHONY },
1.1 deraadt 232: { ".PRECIOUS", Precious, OP_PRECIOUS },
233: { ".RECURSIVE", Attribute, OP_MAKE },
234: { ".SHELL", ExShell, 0 },
235: { ".SILENT", Silent, OP_SILENT },
236: { ".SINGLESHELL", SingleShell, 0 },
237: { ".SUFFIXES", Suffixes, 0 },
238: { ".USE", Attribute, OP_USE },
1.3 deraadt 239: { ".WAIT", Wait, 0 },
1.1 deraadt 240: };
241:
1.17 espie 242: static void ParseErrorInternal __P((char *, size_t, int, char *, ...));
243: static void ParseVErrorInternal __P((char *, size_t, int, char *, va_list));
1.1 deraadt 244: static int ParseFindKeyword __P((char *));
245: static int ParseLinkSrc __P((ClientData, ClientData));
246: static int ParseDoOp __P((ClientData, ClientData));
1.3 deraadt 247: static int ParseAddDep __P((ClientData, ClientData));
248: static void ParseDoSrc __P((int, char *, Lst));
1.1 deraadt 249: static int ParseFindMain __P((ClientData, ClientData));
250: static int ParseAddDir __P((ClientData, ClientData));
251: static int ParseClearPath __P((ClientData, ClientData));
252: static void ParseDoDependency __P((char *));
253: static int ParseAddCmd __P((ClientData, ClientData));
254: static int ParseReadc __P((void));
255: static void ParseUnreadc __P((int));
256: static void ParseHasCommands __P((ClientData));
257: static void ParseDoInclude __P((char *));
258: #ifdef SYSVINCLUDE
259: static void ParseTraditionalInclude __P((char *));
260: #endif
261: static int ParseEOF __P((int));
262: static char *ParseReadLine __P((void));
263: static char *ParseSkipLine __P((int));
264: static void ParseFinishLine __P((void));
265:
266: /*-
267: *----------------------------------------------------------------------
268: * ParseFindKeyword --
269: * Look in the table of keywords for one matching the given string.
270: *
271: * Results:
272: * The index of the keyword, or -1 if it isn't there.
273: *
274: * Side Effects:
275: * None
276: *----------------------------------------------------------------------
277: */
278: static int
279: ParseFindKeyword (str)
280: char *str; /* String to find */
281: {
282: register int start,
283: end,
284: cur;
285: register int diff;
1.11 millert 286:
1.1 deraadt 287: start = 0;
288: end = (sizeof(parseKeywords)/sizeof(parseKeywords[0])) - 1;
289:
290: do {
291: cur = start + ((end - start) / 2);
292: diff = strcmp (str, parseKeywords[cur].name);
293:
294: if (diff == 0) {
295: return (cur);
296: } else if (diff < 0) {
297: end = cur - 1;
298: } else {
299: start = cur + 1;
300: }
301: } while (start <= end);
302: return (-1);
303: }
304:
305: /*-
1.17 espie 306: * ParseVErrorInternal --
1.1 deraadt 307: * Error message abort function for parsing. Prints out the context
308: * of the error (line number and file) as well as the message with
309: * two optional arguments.
310: *
311: * Results:
312: * None
313: *
314: * Side Effects:
315: * "fatals" is incremented if the level is PARSE_FATAL.
316: */
317: /* VARARGS */
1.17 espie 318: static void
319: #ifdef __STDC__
320: ParseVErrorInternal(char *cfname, size_t clineno, int type, char *fmt,
321: va_list ap)
322: #else
323: ParseVErrorInternal(va_alist)
324: va_dcl
325: #endif
326: {
327: (void)fprintf(stderr, "\"%s\", line %d: ", cfname, (int) clineno);
328: if (type == PARSE_WARNING)
329: (void)fprintf(stderr, "warning: ");
330: (void)vfprintf(stderr, fmt, ap);
331: va_end(ap);
332: (void)fprintf(stderr, "\n");
333: (void)fflush(stderr);
334: if (type == PARSE_FATAL)
335: fatals += 1;
336: }
337:
338: /*-
339: * ParseErrorInternal --
340: * Error function
341: *
342: * Results:
343: * None
344: *
345: * Side Effects:
346: * None
347: */
348: /* VARARGS */
349: static void
350: #ifdef __STDC__
351: ParseErrorInternal(char *cfname, size_t clineno, int type, char *fmt, ...)
352: #else
353: ParseErrorInternal(va_alist)
354: va_dcl
355: #endif
356: {
357: va_list ap;
358: #ifdef __STDC__
359: va_start(ap, fmt);
360: #else
361: int type; /* Error type (PARSE_WARNING, PARSE_FATAL) */
362: char *fmt;
363: char *cfname;
364: size_t clineno;
365:
366: va_start(ap);
367: cfname = va_arg(ap, char *);
368: clineno = va_arg(ap, size_t);
369: type = va_arg(ap, int);
370: fmt = va_arg(ap, char *);
371: #endif
372:
373: ParseVErrorInternal(cfname, clineno, type, fmt, ap);
374: va_end(ap);
375: }
376:
377: /*-
378: * Parse_Error --
379: * External interface to ParseErrorInternal; uses the default filename
380: * Line number.
381: *
382: * Results:
383: * None
384: *
385: * Side Effects:
386: * None
387: */
388: /* VARARGS */
1.1 deraadt 389: void
1.15 mickey 390: #ifdef __STDC__
1.1 deraadt 391: Parse_Error(int type, char *fmt, ...)
392: #else
393: Parse_Error(va_alist)
394: va_dcl
395: #endif
396: {
397: va_list ap;
1.15 mickey 398: #ifdef __STDC__
1.1 deraadt 399: va_start(ap, fmt);
400: #else
401: int type; /* Error type (PARSE_WARNING, PARSE_FATAL) */
402: char *fmt;
403:
404: va_start(ap);
405: type = va_arg(ap, int);
406: fmt = va_arg(ap, char *);
407: #endif
408:
1.17 espie 409: ParseVErrorInternal(fname, lineno, type, fmt, ap);
1.1 deraadt 410: }
411:
412: /*-
413: *---------------------------------------------------------------------
414: * ParseLinkSrc --
415: * Link the parent node to its new child. Used in a Lst_ForEach by
416: * ParseDoDependency. If the specType isn't 'Not', the parent
417: * isn't linked as a parent of the child.
418: *
419: * Results:
420: * Always = 0
421: *
422: * Side Effects:
423: * New elements are added to the parents list of cgn and the
424: * children list of cgn. the unmade field of pgn is updated
425: * to reflect the additional child.
426: *---------------------------------------------------------------------
427: */
428: static int
429: ParseLinkSrc (pgnp, cgnp)
430: ClientData pgnp; /* The parent node */
431: ClientData cgnp; /* The child node */
432: {
433: GNode *pgn = (GNode *) pgnp;
434: GNode *cgn = (GNode *) cgnp;
435: if (Lst_Member (pgn->children, (ClientData)cgn) == NILLNODE) {
436: (void)Lst_AtEnd (pgn->children, (ClientData)cgn);
437: if (specType == Not) {
438: (void)Lst_AtEnd (cgn->parents, (ClientData)pgn);
439: }
440: pgn->unmade += 1;
441: }
442: return (0);
443: }
444:
445: /*-
446: *---------------------------------------------------------------------
447: * ParseDoOp --
448: * Apply the parsed operator to the given target node. Used in a
449: * Lst_ForEach call by ParseDoDependency once all targets have
450: * been found and their operator parsed. If the previous and new
451: * operators are incompatible, a major error is taken.
452: *
453: * Results:
454: * Always 0
455: *
456: * Side Effects:
457: * The type field of the node is altered to reflect any new bits in
458: * the op.
459: *---------------------------------------------------------------------
460: */
461: static int
462: ParseDoOp (gnp, opp)
463: ClientData gnp; /* The node to which the operator is to be
464: * applied */
465: ClientData opp; /* The operator to apply */
466: {
467: GNode *gn = (GNode *) gnp;
468: int op = *(int *) opp;
469: /*
470: * If the dependency mask of the operator and the node don't match and
471: * the node has actually had an operator applied to it before, and
1.11 millert 472: * the operator actually has some dependency information in it, complain.
1.1 deraadt 473: */
474: if (((op & OP_OPMASK) != (gn->type & OP_OPMASK)) &&
475: !OP_NOP(gn->type) && !OP_NOP(op))
476: {
477: Parse_Error (PARSE_FATAL, "Inconsistent operator for %s", gn->name);
478: return (1);
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: */
503: Lst_ForEach(gn->parents, ParseLinkSrc, (ClientData)cohort);
504: cohort->type = OP_DOUBLEDEP|OP_INVISIBLE;
505: (void)Lst_AtEnd(gn->cohorts, (ClientData)cohort);
506:
507: /*
508: * Replace the node in the targets list with the new copy
509: */
510: ln = Lst_Member(targets, (ClientData)gn);
511: Lst_Replace(ln, (ClientData)cohort);
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:
520: return (0);
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:
531: * Returns 1 if the two targets need to be ordered, 0 otherwise.
532: * If it returns 1, the search can stop
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)
541: ClientData pp;
542: ClientData sp;
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: */
553: (void)Lst_AtEnd(p->successors, (ClientData)s);
554: (void)Lst_AtEnd(s->preds, (ClientData)p);
555: return 0;
556: }
557: else
558: return 1;
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) {
593: Lst_ForEach (targets, ParseDoOp, (ClientData)&op);
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.9 briggs 613: (void) Lst_AtEnd (create, (ClientData)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);
627: if (predecessor != NILGNODE) {
628: (void)Lst_AtEnd(predecessor->successors, (ClientData)gn);
629: (void)Lst_AtEnd(gn->preds, (ClientData)predecessor);
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 {
653: Lst_ForEach (targets, ParseLinkSrc, (ClientData)gn);
654: }
655: if ((gn->type & OP_OPMASK) == OP_DOUBLEDEP) {
656: register GNode *cohort;
657: register LstNode ln;
658:
659: for (ln=Lst_First(gn->cohorts); ln != NILLNODE; ln = Lst_Succ(ln)){
660: cohort = (GNode *)Lst_Datum(ln);
661: if (tOp) {
662: cohort->type |= tOp;
663: } else {
664: Lst_ForEach(targets, ParseLinkSrc, (ClientData)cohort);
665: }
666: }
667: }
1.3 deraadt 668: break;
669: }
670:
671: gn->order = waiting;
672: (void)Lst_AtEnd(allsrc, (ClientData)gn);
673: if (waiting) {
674: Lst_ForEach(allsrc, ParseAddDep, (ClientData)gn);
1.1 deraadt 675: }
676: }
677:
678: /*-
679: *-----------------------------------------------------------------------
680: * ParseFindMain --
681: * Find a real target in the list and set it to be the main one.
682: * Called by ParseDoDependency when a main target hasn't been found
683: * yet.
684: *
685: * Results:
686: * 0 if main not found yet, 1 if it is.
687: *
688: * Side Effects:
689: * mainNode is changed and Targ_SetMain is called.
690: *
691: *-----------------------------------------------------------------------
692: */
693: static int
694: ParseFindMain(gnp, dummy)
695: ClientData gnp; /* Node to examine */
696: ClientData dummy;
697: {
698: GNode *gn = (GNode *) gnp;
1.17 espie 699: if ((gn->type & OP_NOTARGET) == 0) {
1.1 deraadt 700: mainNode = gn;
701: Targ_SetMain(gn);
702: return (dummy ? 1 : 1);
703: } else {
704: return (dummy ? 0 : 0);
705: }
706: }
707:
708: /*-
709: *-----------------------------------------------------------------------
710: * ParseAddDir --
711: * Front-end for Dir_AddDir to make sure Lst_ForEach keeps going
712: *
713: * Results:
714: * === 0
715: *
716: * Side Effects:
717: * See Dir_AddDir.
718: *
719: *-----------------------------------------------------------------------
720: */
721: static int
722: ParseAddDir(path, name)
723: ClientData path;
724: ClientData name;
725: {
726: Dir_AddDir((Lst) path, (char *) name);
727: return(0);
728: }
729:
730: /*-
731: *-----------------------------------------------------------------------
732: * ParseClearPath --
733: * Front-end for Dir_ClearPath to make sure Lst_ForEach keeps going
734: *
735: * Results:
736: * === 0
737: *
738: * Side Effects:
739: * See Dir_ClearPath
740: *
741: *-----------------------------------------------------------------------
742: */
743: static int
744: ParseClearPath(path, dummy)
745: ClientData path;
746: ClientData dummy;
747: {
748: Dir_ClearPath((Lst) path);
749: return(dummy ? 0 : 0);
750: }
751:
752: /*-
753: *---------------------------------------------------------------------
754: * ParseDoDependency --
755: * Parse the dependency line in line.
756: *
757: * Results:
758: * None
759: *
760: * Side Effects:
761: * The nodes of the sources are linked as children to the nodes of the
762: * targets. Some nodes may be created.
763: *
764: * We parse a dependency line by first extracting words from the line and
765: * finding nodes in the list of all targets with that name. This is done
766: * until a character is encountered which is an operator character. Currently
767: * these are only ! and :. At this point the operator is parsed and the
768: * pointer into the line advanced until the first source is encountered.
769: * The parsed operator is applied to each node in the 'targets' list,
770: * which is where the nodes found for the targets are kept, by means of
771: * the ParseDoOp function.
772: * The sources are read in much the same way as the targets were except
773: * that now they are expanded using the wildcarding scheme of the C-Shell
774: * and all instances of the resulting words in the list of all targets
775: * are found. Each of the resulting nodes is then linked to each of the
776: * targets as one of its children.
777: * Certain targets are handled specially. These are the ones detailed
778: * by the specType variable.
779: * The storing of transformation rules is also taken care of here.
780: * A target is recognized as a transformation rule by calling
781: * Suff_IsTransform. If it is a transformation rule, its node is gotten
782: * from the suffix module via Suff_AddTransform rather than the standard
783: * Targ_FindNode in the target module.
784: *---------------------------------------------------------------------
785: */
786: static void
787: ParseDoDependency (line)
788: char *line; /* the line to parse */
789: {
790: char *cp; /* our current position */
791: GNode *gn; /* a general purpose temporary node */
792: int op; /* the operator on the line */
793: char savec; /* a place to save a character */
794: Lst paths; /* List of search paths to alter when parsing
795: * a list of .PATH targets */
796: int tOp; /* operator from special target */
1.3 deraadt 797: Lst sources; /* list of archive source names after
798: * expansion */
1.1 deraadt 799: Lst curTargs; /* list of target names to be found and added
800: * to the targets list */
1.3 deraadt 801: Lst curSrcs; /* list of sources in order */
1.1 deraadt 802:
803: tOp = 0;
804:
805: specType = Not;
1.3 deraadt 806: waiting = 0;
1.1 deraadt 807: paths = (Lst)NULL;
808:
809: curTargs = Lst_Init(FALSE);
1.3 deraadt 810: curSrcs = Lst_Init(FALSE);
1.11 millert 811:
1.1 deraadt 812: do {
813: for (cp = line;
1.18 millert 814: *cp && !isspace (*cp) && (*cp != '(');
1.1 deraadt 815: cp++)
816: {
1.19 ! millert 817: /*
! 818: * We don't want to end a word on ':' or '!' if there is a
! 819: * better match later on in the string. By "better" I mean
! 820: * one that is followed by whitespace. This allows the user
! 821: * to have targets like:
! 822: * fie::fi:fo: fum
! 823: * where "fie::fi:fo" is the target. In real life this is used
! 824: * for perl5 library man pages where "::" separates an object
! 825: * from its class. Ie: "File::Spec::Unix". This behaviour
! 826: * is also consistent with other versions of make.
! 827: */
1.18 millert 828: if (*cp == '!' || *cp == ':') {
829: char *p = cp + 1;
830:
831: if (*p == '\0')
832: break; /* no chance, not enough room */
833: /*
834: * Only end the word on ':' or '!' if there is not
1.19 ! millert 835: * a match later on followed by whitespace.
1.18 millert 836: */
837: while ((p = strchr(p + 1, *cp)) && !isspace(*(p + 1)))
838: ;
839: if (!p || !isspace(*(p + 1)))
840: break;
841: } else if (*cp == '$') {
1.1 deraadt 842: /*
843: * Must be a dynamic source (would have been expanded
844: * otherwise), so call the Var module to parse the puppy
845: * so we can safely advance beyond it...There should be
846: * no errors in this, as they would have been discovered
847: * in the initial Var_Subst and we wouldn't be here.
848: */
849: int length;
850: Boolean freeIt;
851: char *result;
852:
853: result=Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
854:
855: if (freeIt) {
856: free(result);
857: }
858: cp += length-1;
859: }
860: continue;
861: }
862: if (*cp == '(') {
863: /*
864: * Archives must be handled specially to make sure the OP_ARCHV
865: * flag is set in their 'type' field, for one thing, and because
866: * things like "archive(file1.o file2.o file3.o)" are permissible.
867: * Arch_ParseArchive will set 'line' to be the first non-blank
868: * after the archive-spec. It creates/finds nodes for the members
869: * and places them on the given list, returning SUCCESS if all
870: * went well and FAILURE if there was an error in the
871: * specification. On error, line should remain untouched.
872: */
873: if (Arch_ParseArchive (&line, targets, VAR_CMD) != SUCCESS) {
874: Parse_Error (PARSE_FATAL,
875: "Error in archive specification: \"%s\"", line);
876: return;
877: } else {
878: continue;
879: }
880: }
881: savec = *cp;
1.11 millert 882:
1.1 deraadt 883: if (!*cp) {
884: /*
885: * Ending a dependency line without an operator is a Bozo
1.11 millert 886: * no-no
1.1 deraadt 887: */
888: Parse_Error (PARSE_FATAL, "Need an operator");
889: return;
890: }
891: *cp = '\0';
892: /*
893: * Have a word in line. See if it's a special target and set
894: * specType to match it.
895: */
896: if (*line == '.' && isupper (line[1])) {
897: /*
898: * See if the target is a special target that must have it
1.11 millert 899: * or its sources handled specially.
1.1 deraadt 900: */
901: int keywd = ParseFindKeyword(line);
902: if (keywd != -1) {
903: if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
904: Parse_Error(PARSE_FATAL, "Mismatched special targets");
905: return;
906: }
1.11 millert 907:
1.1 deraadt 908: specType = parseKeywords[keywd].spec;
909: tOp = parseKeywords[keywd].op;
910:
911: /*
912: * Certain special targets have special semantics:
913: * .PATH Have to set the dirSearchPath
914: * variable too
915: * .MAIN Its sources are only used if
916: * nothing has been specified to
917: * create.
918: * .DEFAULT Need to create a node to hang
919: * commands on, but we don't want
920: * it in the graph, nor do we want
921: * it to be the Main Target, so we
922: * create it, set OP_NOTMAIN and
923: * add it to the list, setting
924: * DEFAULT to the new node for
925: * later use. We claim the node is
926: * A transformation rule to make
927: * life easier later, when we'll
928: * use Make_HandleUse to actually
929: * apply the .DEFAULT commands.
1.7 niklas 930: * .PHONY The list of targets
1.17 espie 931: * .NOPATH Don't search for file in the path
1.1 deraadt 932: * .BEGIN
933: * .END
934: * .INTERRUPT Are not to be considered the
935: * main target.
936: * .NOTPARALLEL Make only one target at a time.
937: * .SINGLESHELL Create a shell for each command.
938: * .ORDER Must set initial predecessor to NIL
939: */
940: switch (specType) {
941: case ExPath:
942: if (paths == NULL) {
943: paths = Lst_Init(FALSE);
944: }
945: (void)Lst_AtEnd(paths, (ClientData)dirSearchPath);
946: break;
947: case Main:
948: if (!Lst_IsEmpty(create)) {
949: specType = Not;
950: }
951: break;
952: case Begin:
953: case End:
954: case Interrupt:
955: gn = Targ_FindNode(line, TARG_CREATE);
956: gn->type |= OP_NOTMAIN;
957: (void)Lst_AtEnd(targets, (ClientData)gn);
958: break;
959: case Default:
960: gn = Targ_NewGN(".DEFAULT");
961: gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
962: (void)Lst_AtEnd(targets, (ClientData)gn);
963: DEFAULT = gn;
964: break;
965: case NotParallel:
966: {
967: extern int maxJobs;
1.11 millert 968:
1.1 deraadt 969: maxJobs = 1;
970: break;
971: }
972: case SingleShell:
973: compatMake = 1;
974: break;
975: case Order:
976: predecessor = NILGNODE;
977: break;
978: default:
979: break;
980: }
981: } else if (strncmp (line, ".PATH", 5) == 0) {
982: /*
983: * .PATH<suffix> has to be handled specially.
984: * Call on the suffix module to give us a path to
985: * modify.
986: */
987: Lst path;
1.11 millert 988:
1.1 deraadt 989: specType = ExPath;
990: path = Suff_GetPath (&line[5]);
991: if (path == NILLST) {
992: Parse_Error (PARSE_FATAL,
993: "Suffix '%s' not defined (yet)",
994: &line[5]);
995: return;
996: } else {
997: if (paths == (Lst)NULL) {
998: paths = Lst_Init(FALSE);
999: }
1000: (void)Lst_AtEnd(paths, (ClientData)path);
1001: }
1002: }
1003: }
1.11 millert 1004:
1.1 deraadt 1005: /*
1006: * Have word in line. Get or create its node and stick it at
1.11 millert 1007: * the end of the targets list
1.1 deraadt 1008: */
1009: if ((specType == Not) && (*line != '\0')) {
1010: if (Dir_HasWildcards(line)) {
1011: /*
1012: * Targets are to be sought only in the current directory,
1013: * so create an empty path for the thing. Note we need to
1014: * use Dir_Destroy in the destruction of the path as the
1015: * Dir module could have added a directory to the path...
1016: */
1017: Lst emptyPath = Lst_Init(FALSE);
1.11 millert 1018:
1.1 deraadt 1019: Dir_Expand(line, emptyPath, curTargs);
1.11 millert 1020:
1.1 deraadt 1021: Lst_Destroy(emptyPath, Dir_Destroy);
1022: } else {
1023: /*
1024: * No wildcards, but we want to avoid code duplication,
1025: * so create a list with the word on it.
1026: */
1027: (void)Lst_AtEnd(curTargs, (ClientData)line);
1028: }
1.11 millert 1029:
1.1 deraadt 1030: while(!Lst_IsEmpty(curTargs)) {
1031: char *targName = (char *)Lst_DeQueue(curTargs);
1.11 millert 1032:
1.1 deraadt 1033: if (!Suff_IsTransform (targName)) {
1034: gn = Targ_FindNode (targName, TARG_CREATE);
1035: } else {
1036: gn = Suff_AddTransform (targName);
1037: }
1.11 millert 1038:
1.16 millert 1039: if (gn != NULL)
1040: (void)Lst_AtEnd (targets, (ClientData)gn);
1.1 deraadt 1041: }
1042: } else if (specType == ExPath && *line != '.' && *line != '\0') {
1043: Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
1044: }
1.11 millert 1045:
1.1 deraadt 1046: *cp = savec;
1047: /*
1048: * If it is a special type and not .PATH, it's the only target we
1049: * allow on this line...
1050: */
1051: if (specType != Not && specType != ExPath) {
1052: Boolean warn = FALSE;
1.11 millert 1053:
1.1 deraadt 1054: while ((*cp != '!') && (*cp != ':') && *cp) {
1055: if (*cp != ' ' && *cp != '\t') {
1056: warn = TRUE;
1057: }
1058: cp++;
1059: }
1060: if (warn) {
1061: Parse_Error(PARSE_WARNING, "Extra target ignored");
1062: }
1063: } else {
1064: while (*cp && isspace (*cp)) {
1065: cp++;
1066: }
1067: }
1068: line = cp;
1069: } while ((*line != '!') && (*line != ':') && *line);
1070:
1071: /*
1072: * Don't need the list of target names anymore...
1073: */
1074: Lst_Destroy(curTargs, NOFREE);
1075:
1076: if (!Lst_IsEmpty(targets)) {
1077: switch(specType) {
1078: default:
1079: Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
1080: break;
1081: case Default:
1082: case Begin:
1083: case End:
1084: case Interrupt:
1085: /*
1086: * These four create nodes on which to hang commands, so
1087: * targets shouldn't be empty...
1088: */
1089: case Not:
1090: /*
1091: * Nothing special here -- targets can be empty if it wants.
1092: */
1093: break;
1094: }
1095: }
1096:
1097: /*
1098: * Have now parsed all the target names. Must parse the operator next. The
1099: * result is left in op .
1100: */
1101: if (*cp == '!') {
1102: op = OP_FORCE;
1103: } else if (*cp == ':') {
1104: if (cp[1] == ':') {
1105: op = OP_DOUBLEDEP;
1106: cp++;
1107: } else {
1108: op = OP_DEPENDS;
1109: }
1110: } else {
1111: Parse_Error (PARSE_FATAL, "Missing dependency operator");
1112: return;
1113: }
1114:
1115: cp++; /* Advance beyond operator */
1116:
1117: Lst_ForEach (targets, ParseDoOp, (ClientData)&op);
1118:
1119: /*
1.11 millert 1120: * Get to the first source
1.1 deraadt 1121: */
1122: while (*cp && isspace (*cp)) {
1123: cp++;
1124: }
1125: line = cp;
1126:
1127: /*
1128: * Several special targets take different actions if present with no
1129: * sources:
1130: * a .SUFFIXES line with no sources clears out all old suffixes
1131: * a .PRECIOUS line makes all targets precious
1132: * a .IGNORE line ignores errors for all targets
1133: * a .SILENT line creates silence when making all targets
1134: * a .PATH removes all directories from the search path(s).
1135: */
1136: if (!*line) {
1137: switch (specType) {
1138: case Suffixes:
1139: Suff_ClearSuffixes ();
1140: break;
1141: case Precious:
1142: allPrecious = TRUE;
1143: break;
1144: case Ignore:
1145: ignoreErrors = TRUE;
1146: break;
1147: case Silent:
1148: beSilent = TRUE;
1149: break;
1150: case ExPath:
1151: Lst_ForEach(paths, ParseClearPath, (ClientData)NULL);
1152: break;
1153: default:
1154: break;
1155: }
1156: } else if (specType == MFlags) {
1157: /*
1158: * Call on functions in main.c to deal with these arguments and
1159: * set the initial character to a null-character so the loop to
1160: * get sources won't get anything
1161: */
1162: Main_ParseArgLine (line);
1163: *line = '\0';
1164: } else if (specType == ExShell) {
1165: if (Job_ParseShell (line) != SUCCESS) {
1166: Parse_Error (PARSE_FATAL, "improper shell specification");
1167: return;
1168: }
1169: *line = '\0';
1170: } else if ((specType == NotParallel) || (specType == SingleShell)) {
1171: *line = '\0';
1172: }
1.11 millert 1173:
1.1 deraadt 1174: /*
1.11 millert 1175: * NOW GO FOR THE SOURCES
1.1 deraadt 1176: */
1177: if ((specType == Suffixes) || (specType == ExPath) ||
1178: (specType == Includes) || (specType == Libs) ||
1179: (specType == Null))
1180: {
1181: while (*line) {
1182: /*
1183: * If the target was one that doesn't take files as its sources
1184: * but takes something like suffixes, we take each
1185: * space-separated word on the line as a something and deal
1186: * with it accordingly.
1187: *
1188: * If the target was .SUFFIXES, we take each source as a
1189: * suffix and add it to the list of suffixes maintained by the
1190: * Suff module.
1191: *
1192: * If the target was a .PATH, we add the source as a directory
1193: * to search on the search path.
1194: *
1195: * If it was .INCLUDES, the source is taken to be the suffix of
1196: * files which will be #included and whose search path should
1197: * be present in the .INCLUDES variable.
1198: *
1199: * If it was .LIBS, the source is taken to be the suffix of
1200: * files which are considered libraries and whose search path
1201: * should be present in the .LIBS variable.
1202: *
1203: * If it was .NULL, the source is the suffix to use when a file
1204: * has no valid suffix.
1205: */
1206: char savec;
1207: while (*cp && !isspace (*cp)) {
1208: cp++;
1209: }
1210: savec = *cp;
1211: *cp = '\0';
1212: switch (specType) {
1213: case Suffixes:
1214: Suff_AddSuffix (line);
1215: break;
1216: case ExPath:
1217: Lst_ForEach(paths, ParseAddDir, (ClientData)line);
1218: break;
1219: case Includes:
1220: Suff_AddInclude (line);
1221: break;
1222: case Libs:
1223: Suff_AddLib (line);
1224: break;
1225: case Null:
1226: Suff_SetNull (line);
1227: break;
1228: default:
1229: break;
1230: }
1231: *cp = savec;
1232: if (savec != '\0') {
1233: cp++;
1234: }
1235: while (*cp && isspace (*cp)) {
1236: cp++;
1237: }
1238: line = cp;
1239: }
1240: if (paths) {
1241: Lst_Destroy(paths, NOFREE);
1242: }
1243: } else {
1244: while (*line) {
1245: /*
1246: * The targets take real sources, so we must beware of archive
1247: * specifications (i.e. things with left parentheses in them)
1248: * and handle them accordingly.
1249: */
1250: while (*cp && !isspace (*cp)) {
1251: if ((*cp == '(') && (cp > line) && (cp[-1] != '$')) {
1252: /*
1253: * Only stop for a left parenthesis if it isn't at the
1254: * start of a word (that'll be for variable changes
1255: * later) and isn't preceded by a dollar sign (a dynamic
1256: * source).
1257: */
1258: break;
1259: } else {
1260: cp++;
1261: }
1262: }
1263:
1264: if (*cp == '(') {
1265: GNode *gn;
1266:
1267: sources = Lst_Init (FALSE);
1268: if (Arch_ParseArchive (&line, sources, VAR_CMD) != SUCCESS) {
1269: Parse_Error (PARSE_FATAL,
1270: "Error in source archive spec \"%s\"", line);
1271: return;
1272: }
1273:
1274: while (!Lst_IsEmpty (sources)) {
1275: gn = (GNode *) Lst_DeQueue (sources);
1.3 deraadt 1276: ParseDoSrc (tOp, gn->name, curSrcs);
1.1 deraadt 1277: }
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.1 deraadt 1295: if (mainNode == NILGNODE) {
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;
1536: cp = Var_Subst(NULL, cp, ctxt, FALSE);
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.9 briggs 1552: cp = Var_Subst(NULL, 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))
1592: (void)Lst_AtEnd(gn->commands, cmd);
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: */
1716: file = Var_Subst (NULL, file, VAR_CMD, FALSE);
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:
1802: (void) Lst_AtFront (includes, (ClientData)oldFile);
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
1839: Parse_FromString(str)
1840: char *str;
1841: {
1842: IFile *oldFile; /* state associated with this file */
1843:
1844: if (DEBUG(FOR))
1845: (void) fprintf(stderr, "%s\n----\n", str);
1846:
1847: oldFile = (IFile *) emalloc (sizeof (IFile));
1848: oldFile->lineno = lineno;
1849: oldFile->fname = fname;
1850: oldFile->F = curFILE;
1851: oldFile->p = curPTR;
1.11 millert 1852:
1.1 deraadt 1853: (void) Lst_AtFront (includes, (ClientData)oldFile);
1854:
1855: curFILE = NULL;
1856: curPTR = (PTR *) emalloc (sizeof (PTR));
1857: curPTR->str = curPTR->ptr = str;
1858: lineno = 0;
1.9 briggs 1859: fname = estrdup(fname);
1.1 deraadt 1860: }
1861:
1862:
1863: #ifdef SYSVINCLUDE
1864: /*-
1865: *---------------------------------------------------------------------
1866: * ParseTraditionalInclude --
1867: * Push to another file.
1.11 millert 1868: *
1.1 deraadt 1869: * The input is the line minus the "include". The file name is
1870: * the string following the "include".
1871: *
1872: * Results:
1873: * None
1874: *
1875: * Side Effects:
1876: * A structure is added to the includes Lst and readProc, lineno,
1877: * fname and curFILE are altered for the new file
1878: *---------------------------------------------------------------------
1879: */
1880: static void
1881: ParseTraditionalInclude (file)
1882: char *file; /* file specification */
1883: {
1884: char *fullname; /* full pathname of file */
1885: IFile *oldFile; /* state associated with current file */
1886: char *cp; /* current position in file spec */
1887: char *prefEnd;
1888:
1889: /*
1890: * Skip over whitespace
1891: */
1892: while ((*file == ' ') || (*file == '\t')) {
1893: file++;
1894: }
1895:
1896: if (*file == '\0') {
1897: Parse_Error (PARSE_FATAL,
1898: "Filename missing from \"include\"");
1899: return;
1900: }
1901:
1902: /*
1903: * Skip to end of line or next whitespace
1904: */
1905: for (cp = file; *cp && *cp != '\n' && *cp != '\t' && *cp != ' '; cp++) {
1906: continue;
1907: }
1908:
1909: *cp = '\0';
1910:
1911: /*
1912: * Substitute for any variables in the file name before trying to
1913: * find the thing.
1914: */
1915: file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1916:
1917: /*
1918: * Now we know the file's name, we attempt to find the durn thing.
1919: * A return of NULL indicates the file don't exist.
1920: *
1921: * Include files are first searched for relative to the including
1922: * file's location. We don't want to cd there, of course, so we
1923: * just tack on the old file's leading path components and call
1924: * Dir_FindFile to see if we can locate the beast.
1925: * XXX - this *does* search in the current directory, right?
1926: */
1927:
1928: prefEnd = strrchr (fname, '/');
1929: if (prefEnd != (char *)NULL) {
1930: char *newName;
1.11 millert 1931:
1.1 deraadt 1932: *prefEnd = '\0';
1933: newName = str_concat (fname, file, STR_ADDSLASH);
1934: fullname = Dir_FindFile (newName, parseIncPath);
1935: if (fullname == (char *)NULL) {
1936: fullname = Dir_FindFile(newName, dirSearchPath);
1937: }
1938: free (newName);
1939: *prefEnd = '/';
1940: } else {
1941: fullname = (char *)NULL;
1942: }
1943:
1944: if (fullname == (char *)NULL) {
1945: /*
1946: * System makefile or makefile wasn't found in same directory as
1947: * included makefile. Search for it first on the -I search path,
1948: * then on the .PATH search path, if not found in a -I directory.
1949: * XXX: Suffix specific?
1950: */
1951: fullname = Dir_FindFile (file, parseIncPath);
1952: if (fullname == (char *)NULL) {
1953: fullname = Dir_FindFile(file, dirSearchPath);
1954: }
1955: }
1956:
1957: if (fullname == (char *)NULL) {
1958: /*
1959: * Still haven't found the makefile. Look for it on the system
1960: * path as a last resort.
1961: */
1962: fullname = Dir_FindFile(file, sysIncPath);
1963: }
1964:
1965: if (fullname == (char *) NULL) {
1966: Parse_Error (PARSE_FATAL, "Could not find %s", file);
1967: return;
1968: }
1969:
1970: /*
1971: * Once we find the absolute path to the file, we get to save all the
1972: * state from the current file before we can start reading this
1973: * include file. The state is stored in an IFile structure which
1974: * is placed on a list with other IFile structures. The list makes
1975: * a very nice stack to track how we got here...
1976: */
1977: oldFile = (IFile *) emalloc (sizeof (IFile));
1978: oldFile->fname = fname;
1979:
1980: oldFile->F = curFILE;
1981: oldFile->p = curPTR;
1982: oldFile->lineno = lineno;
1983:
1984: (void) Lst_AtFront (includes, (ClientData)oldFile);
1985:
1986: /*
1987: * Once the previous state has been saved, we can get down to reading
1988: * the new file. We set up the name of the file to be the absolute
1989: * name of the include file so error messages refer to the right
1990: * place. Naturally enough, we start reading at line number 0.
1991: */
1992: fname = fullname;
1993: lineno = 0;
1994:
1995: curFILE = fopen (fullname, "r");
1996: curPTR = NULL;
1997: if (curFILE == (FILE * ) NULL) {
1998: Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1999: /*
2000: * Pop to previous file
2001: */
2002: (void) ParseEOF(1);
2003: }
2004: }
2005: #endif
2006:
2007: /*-
2008: *---------------------------------------------------------------------
2009: * ParseEOF --
2010: * Called when EOF is reached in the current file. If we were reading
2011: * an include file, the includes stack is popped and things set up
2012: * to go back to reading the previous file at the previous location.
2013: *
2014: * Results:
2015: * CONTINUE if there's more to do. DONE if not.
2016: *
2017: * Side Effects:
2018: * The old curFILE, is closed. The includes list is shortened.
2019: * lineno, curFILE, and fname are changed if CONTINUE is returned.
2020: *---------------------------------------------------------------------
2021: */
2022: static int
2023: ParseEOF (opened)
2024: int opened;
2025: {
2026: IFile *ifile; /* the state on the top of the includes stack */
2027:
2028: if (Lst_IsEmpty (includes)) {
2029: return (DONE);
2030: }
2031:
2032: ifile = (IFile *) Lst_DeQueue (includes);
2033: free ((Address) fname);
2034: fname = ifile->fname;
2035: lineno = ifile->lineno;
2036: if (opened && curFILE)
2037: (void) fclose (curFILE);
2038: if (curPTR) {
2039: free((Address) curPTR->str);
2040: free((Address) curPTR);
2041: }
2042: curFILE = ifile->F;
2043: curPTR = ifile->p;
2044: free ((Address)ifile);
2045: return (CONTINUE);
2046: }
2047:
2048: /*-
2049: *---------------------------------------------------------------------
2050: * ParseReadc --
1.11 millert 2051: * Read a character from the current file
1.1 deraadt 2052: *
2053: * Results:
2054: * The character that was read
2055: *
2056: * Side Effects:
2057: *---------------------------------------------------------------------
2058: */
2059: static int
2060: ParseReadc()
2061: {
2062: if (curFILE)
2063: return fgetc(curFILE);
1.11 millert 2064:
1.1 deraadt 2065: if (curPTR && *curPTR->ptr)
2066: return *curPTR->ptr++;
2067: return EOF;
2068: }
2069:
2070:
2071: /*-
2072: *---------------------------------------------------------------------
2073: * ParseUnreadc --
1.11 millert 2074: * Put back a character to the current file
1.1 deraadt 2075: *
2076: * Results:
2077: * None.
2078: *
2079: * Side Effects:
2080: *---------------------------------------------------------------------
2081: */
2082: static void
2083: ParseUnreadc(c)
2084: int c;
2085: {
2086: if (curFILE) {
2087: ungetc(c, curFILE);
2088: return;
2089: }
2090: if (curPTR) {
2091: *--(curPTR->ptr) = c;
2092: return;
2093: }
2094: }
2095:
2096:
2097: /* ParseSkipLine():
2098: * Grab the next line
2099: */
2100: static char *
2101: ParseSkipLine(skip)
2102: int skip; /* Skip lines that don't start with . */
2103: {
2104: char *line;
1.11 millert 2105: int c, lastc, lineLength = 0;
1.1 deraadt 2106: Buffer buf;
2107:
1.11 millert 2108: buf = Buf_Init(MAKE_BSIZE);
2109:
2110: do {
2111: Buf_Discard(buf, lineLength);
2112: lastc = '\0';
2113:
2114: while (((c = ParseReadc()) != '\n' || lastc == '\\')
2115: && c != EOF) {
2116: if (c == '\n') {
2117: Buf_ReplaceLastByte(buf, (Byte)' ');
2118: lineno++;
2119:
2120: while ((c = ParseReadc()) == ' ' || c == '\t');
2121:
2122: if (c == EOF)
2123: break;
2124: }
2125:
2126: Buf_AddByte(buf, (Byte)c);
2127: lastc = c;
2128: }
2129:
2130: if (c == EOF) {
2131: Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
2132: Buf_Destroy(buf, TRUE);
2133: return((char *)NULL);
2134: }
2135:
2136: lineno++;
2137: Buf_AddByte(buf, (Byte)'\0');
2138: line = (char *)Buf_GetAll(buf, &lineLength);
2139: } while (skip == 1 && line[0] != '.');
1.1 deraadt 2140:
1.11 millert 2141: Buf_Destroy(buf, FALSE);
1.1 deraadt 2142: return line;
2143: }
2144:
2145:
2146: /*-
2147: *---------------------------------------------------------------------
2148: * ParseReadLine --
2149: * Read an entire line from the input file. Called only by Parse_File.
2150: * To facilitate escaped newlines and what have you, a character is
2151: * buffered in 'lastc', which is '\0' when no characters have been
2152: * read. When we break out of the loop, c holds the terminating
2153: * character and lastc holds a character that should be added to
2154: * the line (unless we don't read anything but a terminator).
2155: *
2156: * Results:
2157: * A line w/o its newline
2158: *
2159: * Side Effects:
2160: * Only those associated with reading a character
2161: *---------------------------------------------------------------------
2162: */
2163: static char *
2164: ParseReadLine ()
2165: {
2166: Buffer buf; /* Buffer for current line */
2167: register int c; /* the current character */
2168: register int lastc; /* The most-recent character */
2169: Boolean semiNL; /* treat semi-colons as newlines */
2170: Boolean ignDepOp; /* TRUE if should ignore dependency operators
2171: * for the purposes of setting semiNL */
2172: Boolean ignComment; /* TRUE if should ignore comments (in a
2173: * shell command */
2174: char *line; /* Result */
2175: char *ep; /* to strip trailing blanks */
2176: int lineLength; /* Length of result */
2177:
2178: semiNL = FALSE;
2179: ignDepOp = FALSE;
2180: ignComment = FALSE;
2181:
2182: /*
2183: * Handle special-characters at the beginning of the line. Either a
2184: * leading tab (shell command) or pound-sign (possible conditional)
2185: * forces us to ignore comments and dependency operators and treat
2186: * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2187: * discards completely blank lines.
2188: */
2189: for (;;) {
2190: c = ParseReadc();
2191:
2192: if (c == '\t') {
2193: ignComment = ignDepOp = TRUE;
2194: break;
2195: } else if (c == '\n') {
2196: lineno++;
2197: } else if (c == '#') {
2198: ParseUnreadc(c);
2199: break;
2200: } else {
2201: /*
2202: * Anything else breaks out without doing anything
2203: */
2204: break;
2205: }
2206: }
1.11 millert 2207:
1.1 deraadt 2208: if (c != EOF) {
2209: lastc = c;
2210: buf = Buf_Init(MAKE_BSIZE);
1.11 millert 2211:
1.1 deraadt 2212: while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) &&
2213: (c != EOF))
2214: {
2215: test_char:
2216: switch(c) {
2217: case '\n':
2218: /*
2219: * Escaped newline: read characters until a non-space or an
2220: * unescaped newline and replace them all by a single space.
2221: * This is done by storing the space over the backslash and
2222: * dropping through with the next nonspace. If it is a
2223: * semi-colon and semiNL is TRUE, it will be recognized as a
2224: * newline in the code below this...
2225: */
2226: lineno++;
2227: lastc = ' ';
2228: while ((c = ParseReadc ()) == ' ' || c == '\t') {
2229: continue;
2230: }
2231: if (c == EOF || c == '\n') {
2232: goto line_read;
2233: } else {
2234: /*
2235: * Check for comments, semiNL's, etc. -- easier than
2236: * ParseUnreadc(c); continue;
2237: */
2238: goto test_char;
2239: }
2240: /*NOTREACHED*/
2241: break;
2242:
2243: case ';':
2244: /*
2245: * Semi-colon: Need to see if it should be interpreted as a
2246: * newline
2247: */
2248: if (semiNL) {
2249: /*
2250: * To make sure the command that may be following this
2251: * semi-colon begins with a tab, we push one back into the
2252: * input stream. This will overwrite the semi-colon in the
2253: * buffer. If there is no command following, this does no
2254: * harm, since the newline remains in the buffer and the
2255: * whole line is ignored.
2256: */
2257: ParseUnreadc('\t');
2258: goto line_read;
1.11 millert 2259: }
1.1 deraadt 2260: break;
2261: case '=':
2262: if (!semiNL) {
2263: /*
2264: * Haven't seen a dependency operator before this, so this
2265: * must be a variable assignment -- don't pay attention to
2266: * dependency operators after this.
2267: */
2268: ignDepOp = TRUE;
2269: } else if (lastc == ':' || lastc == '!') {
2270: /*
2271: * Well, we've seen a dependency operator already, but it
2272: * was the previous character, so this is really just an
2273: * expanded variable assignment. Revert semi-colons to
2274: * being just semi-colons again and ignore any more
2275: * dependency operators.
2276: *
2277: * XXX: Note that a line like "foo : a:=b" will blow up,
2278: * but who'd write a line like that anyway?
2279: */
2280: ignDepOp = TRUE; semiNL = FALSE;
2281: }
2282: break;
2283: case '#':
2284: if (!ignComment) {
1.2 deraadt 2285: if (
2286: #if 0
2287: compatMake &&
2288: #endif
2289: (lastc != '\\')) {
1.1 deraadt 2290: /*
2291: * If the character is a hash mark and it isn't escaped
2292: * (or we're being compatible), the thing is a comment.
2293: * Skip to the end of the line.
2294: */
2295: do {
2296: c = ParseReadc();
2297: } while ((c != '\n') && (c != EOF));
2298: goto line_read;
2299: } else {
2300: /*
2301: * Don't add the backslash. Just let the # get copied
2302: * over.
2303: */
2304: lastc = c;
2305: continue;
2306: }
2307: }
2308: break;
2309: case ':':
2310: case '!':
2311: if (!ignDepOp && (c == ':' || c == '!')) {
2312: /*
2313: * A semi-colon is recognized as a newline only on
2314: * dependency lines. Dependency lines are lines with a
2315: * colon or an exclamation point. Ergo...
2316: */
2317: semiNL = TRUE;
2318: }
2319: break;
2320: }
2321: /*
2322: * Copy in the previous character and save this one in lastc.
2323: */
2324: Buf_AddByte (buf, (Byte)lastc);
2325: lastc = c;
1.11 millert 2326:
1.1 deraadt 2327: }
2328: line_read:
2329: lineno++;
1.11 millert 2330:
1.1 deraadt 2331: if (lastc != '\0') {
2332: Buf_AddByte (buf, (Byte)lastc);
2333: }
2334: Buf_AddByte (buf, (Byte)'\0');
2335: line = (char *)Buf_GetAll (buf, &lineLength);
2336: Buf_Destroy (buf, FALSE);
2337:
2338: /*
2339: * Strip trailing blanks and tabs from the line.
2340: * Do not strip a blank or tab that is preceeded by
2341: * a '\'
2342: */
2343: ep = line;
2344: while (*ep)
2345: ++ep;
1.11 millert 2346: while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
1.1 deraadt 2347: if (ep > line + 1 && ep[-2] == '\\')
2348: break;
2349: --ep;
2350: }
2351: *ep = 0;
1.11 millert 2352:
1.1 deraadt 2353: if (line[0] == '.') {
2354: /*
2355: * The line might be a conditional. Ask the conditional module
2356: * about it and act accordingly
2357: */
2358: switch (Cond_Eval (line)) {
2359: case COND_SKIP:
2360: /*
2361: * Skip to next conditional that evaluates to COND_PARSE.
2362: */
2363: do {
2364: free (line);
2365: line = ParseSkipLine(1);
2366: } while (line && Cond_Eval(line) != COND_PARSE);
2367: if (line == NULL)
2368: break;
2369: /*FALLTHRU*/
2370: case COND_PARSE:
2371: free ((Address) line);
2372: line = ParseReadLine();
2373: break;
2374: case COND_INVALID:
2375: if (For_Eval(line)) {
2376: int ok;
2377: free(line);
2378: do {
2379: /*
2380: * Skip after the matching end
2381: */
2382: line = ParseSkipLine(0);
2383: if (line == NULL) {
1.11 millert 2384: Parse_Error (PARSE_FATAL,
1.1 deraadt 2385: "Unexpected end of file in for loop.\n");
2386: break;
2387: }
2388: ok = For_Eval(line);
2389: free(line);
2390: }
2391: while (ok);
2392: if (line != NULL)
2393: For_Run();
2394: line = ParseReadLine();
2395: }
2396: break;
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);
2517: Lst_AtEnd(targCmds, (ClientData) line);
2518: continue;
2519: } else {
2520: Parse_Error (PARSE_FATAL,
1.10 briggs 2521: "Unassociated shell command \"%s\"",
1.1 deraadt 2522: cp);
2523: }
2524: }
2525: #ifdef SYSVINCLUDE
1.11 millert 2526: } else if (strncmp (line, "include", 7) == 0 &&
1.6 tholo 2527: isspace((unsigned char) line[7]) &&
1.1 deraadt 2528: strchr(line, ':') == NULL) {
2529: /*
2530: * It's an S3/S5-style "include".
2531: */
2532: ParseTraditionalInclude (line + 7);
2533: goto nextLine;
2534: #endif
2535: } else if (Parse_IsVar (line)) {
2536: ParseFinishLine();
2537: Parse_DoVar (line, VAR_GLOBAL);
2538: } else {
2539: /*
2540: * We now know it's a dependency line so it needs to have all
2541: * variables expanded before being parsed. Tell the variable
2542: * module to complain if some variable is undefined...
2543: * To make life easier on novices, if the line is indented we
2544: * first make sure the line has a dependency operator in it.
2545: * If it doesn't have an operator and we're in a dependency
2546: * line's script, we assume it's actually a shell command
2547: * and add it to the current list of targets.
2548: */
2549: #ifndef POSIX
2550: Boolean nonSpace = FALSE;
2551: #endif
1.11 millert 2552:
1.1 deraadt 2553: cp = line;
2554: if (isspace((unsigned char) line[0])) {
2555: while ((*cp != '\0') && isspace((unsigned char) *cp)) {
2556: cp++;
2557: }
2558: if (*cp == '\0') {
2559: goto nextLine;
2560: }
2561: #ifndef POSIX
2562: while ((*cp != ':') && (*cp != '!') && (*cp != '\0')) {
2563: nonSpace = TRUE;
2564: cp++;
2565: }
2566: #endif
2567: }
1.11 millert 2568:
1.1 deraadt 2569: #ifndef POSIX
2570: if (*cp == '\0') {
2571: if (inLine) {
2572: Parse_Error (PARSE_WARNING,
2573: "Shell command needs a leading tab");
2574: goto shellCommand;
2575: } else if (nonSpace) {
2576: Parse_Error (PARSE_FATAL, "Missing operator");
2577: }
2578: } else {
2579: #endif
2580: ParseFinishLine();
2581:
2582: cp = Var_Subst (NULL, line, VAR_CMD, TRUE);
2583: free (line);
2584: line = cp;
1.11 millert 2585:
1.1 deraadt 2586: /*
1.11 millert 2587: * Need a non-circular list for the target nodes
1.1 deraadt 2588: */
2589: if (targets)
2590: Lst_Destroy(targets, NOFREE);
2591:
2592: targets = Lst_Init (FALSE);
2593: inLine = TRUE;
1.11 millert 2594:
1.1 deraadt 2595: ParseDoDependency (line);
2596: #ifndef POSIX
2597: }
2598: #endif
2599: }
2600:
2601: nextLine:
2602:
2603: free (line);
2604: }
2605: /*
1.11 millert 2606: * Reached EOF, but it may be just EOF of an include file...
1.1 deraadt 2607: */
2608: } while (ParseEOF(1) == CONTINUE);
2609:
2610: /*
2611: * Make sure conditionals are clean
2612: */
2613: Cond_End();
2614:
2615: if (fatals) {
2616: fprintf (stderr, "Fatal errors encountered -- cannot continue\n");
2617: exit (1);
2618: }
2619: }
2620:
2621: /*-
2622: *---------------------------------------------------------------------
2623: * Parse_Init --
2624: * initialize the parsing module
2625: *
2626: * Results:
2627: * none
2628: *
2629: * Side Effects:
2630: * the parseIncPath list is initialized...
2631: *---------------------------------------------------------------------
2632: */
2633: void
2634: Parse_Init ()
2635: {
2636: mainNode = NILGNODE;
2637: parseIncPath = Lst_Init (FALSE);
2638: sysIncPath = Lst_Init (FALSE);
2639: includes = Lst_Init (FALSE);
2640: targCmds = Lst_Init (FALSE);
2641: }
2642:
2643: void
2644: Parse_End()
2645: {
2646: Lst_Destroy(targCmds, (void (*) __P((ClientData))) free);
2647: if (targets)
2648: Lst_Destroy(targets, NOFREE);
2649: Lst_Destroy(sysIncPath, Dir_Destroy);
2650: Lst_Destroy(parseIncPath, Dir_Destroy);
2651: Lst_Destroy(includes, NOFREE); /* Should be empty now */
2652: }
1.11 millert 2653:
1.1 deraadt 2654:
2655: /*-
2656: *-----------------------------------------------------------------------
2657: * Parse_MainName --
2658: * Return a Lst of the main target to create for main()'s sake. If
2659: * no such target exists, we Punt with an obnoxious error message.
2660: *
2661: * Results:
2662: * A Lst of the single node to create.
2663: *
2664: * Side Effects:
2665: * None.
2666: *
2667: *-----------------------------------------------------------------------
2668: */
2669: Lst
2670: Parse_MainName()
2671: {
1.14 millert 2672: Lst listmain; /* result list */
1.1 deraadt 2673:
1.14 millert 2674: listmain = Lst_Init (FALSE);
1.1 deraadt 2675:
2676: if (mainNode == NILGNODE) {
1.8 deraadt 2677: Punt ("no target to make.");
1.1 deraadt 2678: /*NOTREACHED*/
2679: } else if (mainNode->type & OP_DOUBLEDEP) {
1.14 millert 2680: (void) Lst_AtEnd (listmain, (ClientData)mainNode);
2681: Lst_Concat(listmain, mainNode->cohorts, LST_CONCNEW);
1.1 deraadt 2682: }
2683: else
1.14 millert 2684: (void) Lst_AtEnd (listmain, (ClientData)mainNode);
2685: return (listmain);
1.1 deraadt 2686: }