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