parse.c revision 1.144 1 /* $NetBSD: parse.c,v 1.144 2008/02/14 22:11:20 christos 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.144 2008/02/14 22:11:20 christos 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.144 2008/02/14 22:11:20 christos 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 size_t lineno; /* line number in file */
160 int fd; /* the open file */
161 size_t 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 size_t 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 size_t length;
871 void *freeIt;
872
873 Var_Parse(cp, VAR_CMD, TRUE, &length, &freeIt);
874 if (freeIt)
875 free(freeIt);
876 cp += length-1;
877 }
878 }
879
880 if (!ParseIsEscaped(lstart, cp) && *cp == LPAREN) {
881 /*
882 * Archives must be handled specially to make sure the OP_ARCHV
883 * flag is set in their 'type' field, for one thing, and because
884 * things like "archive(file1.o file2.o file3.o)" are permissible.
885 * Arch_ParseArchive will set 'line' to be the first non-blank
886 * after the archive-spec. It creates/finds nodes for the members
887 * and places them on the given list, returning SUCCESS if all
888 * went well and FAILURE if there was an error in the
889 * specification. On error, line should remain untouched.
890 */
891 if (Arch_ParseArchive(&line, targets, VAR_CMD) != SUCCESS) {
892 Parse_Error(PARSE_FATAL,
893 "Error in archive specification: \"%s\"", line);
894 goto out;
895 } else {
896 continue;
897 }
898 }
899 savec = *cp;
900
901 if (!*cp) {
902 /*
903 * Ending a dependency line without an operator is a Bozo
904 * no-no. As a heuristic, this is also often triggered by
905 * undetected conflicts from cvs/rcs merges.
906 */
907 if ((strncmp(line, "<<<<<<", 6) == 0) ||
908 (strncmp(line, "======", 6) == 0) ||
909 (strncmp(line, ">>>>>>", 6) == 0))
910 Parse_Error(PARSE_FATAL,
911 "Makefile appears to contain unresolved cvs/rcs/??? merge conflicts");
912 else
913 Parse_Error(PARSE_FATAL, "Need an operator");
914 goto out;
915 }
916 *cp = '\0';
917
918 /*
919 * Have a word in line. See if it's a special target and set
920 * specType to match it.
921 */
922 if (*line == '.' && isupper ((unsigned char)line[1])) {
923 /*
924 * See if the target is a special target that must have it
925 * or its sources handled specially.
926 */
927 int keywd = ParseFindKeyword(line);
928 if (keywd != -1) {
929 if (specType == ExPath && parseKeywords[keywd].spec != ExPath) {
930 Parse_Error(PARSE_FATAL, "Mismatched special targets");
931 goto out;
932 }
933
934 specType = parseKeywords[keywd].spec;
935 tOp = parseKeywords[keywd].op;
936
937 /*
938 * Certain special targets have special semantics:
939 * .PATH Have to set the dirSearchPath
940 * variable too
941 * .MAIN Its sources are only used if
942 * nothing has been specified to
943 * create.
944 * .DEFAULT Need to create a node to hang
945 * commands on, but we don't want
946 * it in the graph, nor do we want
947 * it to be the Main Target, so we
948 * create it, set OP_NOTMAIN and
949 * add it to the list, setting
950 * DEFAULT to the new node for
951 * later use. We claim the node is
952 * A transformation rule to make
953 * life easier later, when we'll
954 * use Make_HandleUse to actually
955 * apply the .DEFAULT commands.
956 * .PHONY The list of targets
957 * .NOPATH Don't search for file in the path
958 * .BEGIN
959 * .END
960 * .INTERRUPT Are not to be considered the
961 * main target.
962 * .NOTPARALLEL Make only one target at a time.
963 * .SINGLESHELL Create a shell for each command.
964 * .ORDER Must set initial predecessor to NIL
965 */
966 switch (specType) {
967 case ExPath:
968 if (paths == NULL) {
969 paths = Lst_Init(FALSE);
970 }
971 (void)Lst_AtEnd(paths, dirSearchPath);
972 break;
973 case Main:
974 if (!Lst_IsEmpty(create)) {
975 specType = Not;
976 }
977 break;
978 case Begin:
979 case End:
980 case Interrupt:
981 gn = Targ_FindNode(line, TARG_CREATE);
982 gn->type |= OP_NOTMAIN|OP_SPECIAL;
983 (void)Lst_AtEnd(targets, gn);
984 break;
985 case Default:
986 gn = Targ_NewGN(".DEFAULT");
987 gn->type |= (OP_NOTMAIN|OP_TRANSFORM);
988 (void)Lst_AtEnd(targets, gn);
989 DEFAULT = gn;
990 break;
991 case NotParallel:
992 maxJobs = 1;
993 break;
994 case SingleShell:
995 compatMake = TRUE;
996 break;
997 case Order:
998 predecessor = NILGNODE;
999 break;
1000 default:
1001 break;
1002 }
1003 } else if (strncmp(line, ".PATH", 5) == 0) {
1004 /*
1005 * .PATH<suffix> has to be handled specially.
1006 * Call on the suffix module to give us a path to
1007 * modify.
1008 */
1009 Lst path;
1010
1011 specType = ExPath;
1012 path = Suff_GetPath(&line[5]);
1013 if (path == NILLST) {
1014 Parse_Error(PARSE_FATAL,
1015 "Suffix '%s' not defined (yet)",
1016 &line[5]);
1017 goto out;
1018 } else {
1019 if (paths == (Lst)NULL) {
1020 paths = Lst_Init(FALSE);
1021 }
1022 (void)Lst_AtEnd(paths, path);
1023 }
1024 }
1025 }
1026
1027 /*
1028 * Have word in line. Get or create its node and stick it at
1029 * the end of the targets list
1030 */
1031 if ((specType == Not) && (*line != '\0')) {
1032 if (Dir_HasWildcards(line)) {
1033 /*
1034 * Targets are to be sought only in the current directory,
1035 * so create an empty path for the thing. Note we need to
1036 * use Dir_Destroy in the destruction of the path as the
1037 * Dir module could have added a directory to the path...
1038 */
1039 Lst emptyPath = Lst_Init(FALSE);
1040
1041 Dir_Expand(line, emptyPath, curTargs);
1042
1043 Lst_Destroy(emptyPath, Dir_Destroy);
1044 } else {
1045 /*
1046 * No wildcards, but we want to avoid code duplication,
1047 * so create a list with the word on it.
1048 */
1049 (void)Lst_AtEnd(curTargs, line);
1050 }
1051
1052 while(!Lst_IsEmpty(curTargs)) {
1053 char *targName = (char *)Lst_DeQueue(curTargs);
1054
1055 if (!Suff_IsTransform (targName)) {
1056 gn = Targ_FindNode(targName, TARG_CREATE);
1057 } else {
1058 gn = Suff_AddTransform(targName);
1059 }
1060
1061 (void)Lst_AtEnd(targets, gn);
1062 }
1063 } else if (specType == ExPath && *line != '.' && *line != '\0') {
1064 Parse_Error(PARSE_WARNING, "Extra target (%s) ignored", line);
1065 }
1066
1067 *cp = savec;
1068 /*
1069 * If it is a special type and not .PATH, it's the only target we
1070 * allow on this line...
1071 */
1072 if (specType != Not && specType != ExPath) {
1073 Boolean warning = FALSE;
1074
1075 while (*cp && (ParseIsEscaped(lstart, cp) ||
1076 ((*cp != '!') && (*cp != ':')))) {
1077 if (ParseIsEscaped(lstart, cp) ||
1078 (*cp != ' ' && *cp != '\t')) {
1079 warning = TRUE;
1080 }
1081 cp++;
1082 }
1083 if (warning) {
1084 Parse_Error(PARSE_WARNING, "Extra target ignored");
1085 }
1086 } else {
1087 while (*cp && isspace ((unsigned char)*cp)) {
1088 cp++;
1089 }
1090 }
1091 line = cp;
1092 } while (*line && (ParseIsEscaped(lstart, line) ||
1093 ((*line != '!') && (*line != ':'))));
1094
1095 /*
1096 * Don't need the list of target names anymore...
1097 */
1098 Lst_Destroy(curTargs, NOFREE);
1099 curTargs = NULL;
1100
1101 if (!Lst_IsEmpty(targets)) {
1102 switch(specType) {
1103 default:
1104 Parse_Error(PARSE_WARNING, "Special and mundane targets don't mix. Mundane ones ignored");
1105 break;
1106 case Default:
1107 case Begin:
1108 case End:
1109 case Interrupt:
1110 /*
1111 * These four create nodes on which to hang commands, so
1112 * targets shouldn't be empty...
1113 */
1114 case Not:
1115 /*
1116 * Nothing special here -- targets can be empty if it wants.
1117 */
1118 break;
1119 }
1120 }
1121
1122 /*
1123 * Have now parsed all the target names. Must parse the operator next. The
1124 * result is left in op .
1125 */
1126 if (*cp == '!') {
1127 op = OP_FORCE;
1128 } else if (*cp == ':') {
1129 if (cp[1] == ':') {
1130 op = OP_DOUBLEDEP;
1131 cp++;
1132 } else {
1133 op = OP_DEPENDS;
1134 }
1135 } else {
1136 Parse_Error(PARSE_FATAL, "Missing dependency operator");
1137 goto out;
1138 }
1139
1140 cp++; /* Advance beyond operator */
1141
1142 Lst_ForEach(targets, ParseDoOp, &op);
1143
1144 /*
1145 * Get to the first source
1146 */
1147 while (*cp && isspace ((unsigned char)*cp)) {
1148 cp++;
1149 }
1150 line = cp;
1151
1152 /*
1153 * Several special targets take different actions if present with no
1154 * sources:
1155 * a .SUFFIXES line with no sources clears out all old suffixes
1156 * a .PRECIOUS line makes all targets precious
1157 * a .IGNORE line ignores errors for all targets
1158 * a .SILENT line creates silence when making all targets
1159 * a .PATH removes all directories from the search path(s).
1160 */
1161 if (!*line) {
1162 switch (specType) {
1163 case Suffixes:
1164 Suff_ClearSuffixes();
1165 break;
1166 case Precious:
1167 allPrecious = TRUE;
1168 break;
1169 case Ignore:
1170 ignoreErrors = TRUE;
1171 break;
1172 case Silent:
1173 beSilent = TRUE;
1174 break;
1175 case ExPath:
1176 Lst_ForEach(paths, ParseClearPath, NULL);
1177 Dir_SetPATH();
1178 break;
1179 #ifdef POSIX
1180 case Posix:
1181 Var_Set("%POSIX", "1003.2", VAR_GLOBAL, 0);
1182 break;
1183 #endif
1184 default:
1185 break;
1186 }
1187 } else if (specType == MFlags) {
1188 /*
1189 * Call on functions in main.c to deal with these arguments and
1190 * set the initial character to a null-character so the loop to
1191 * get sources won't get anything
1192 */
1193 Main_ParseArgLine(line);
1194 *line = '\0';
1195 } else if (specType == ExShell) {
1196 if (Job_ParseShell(line) != SUCCESS) {
1197 Parse_Error(PARSE_FATAL, "improper shell specification");
1198 goto out;
1199 }
1200 *line = '\0';
1201 } else if ((specType == NotParallel) || (specType == SingleShell)) {
1202 *line = '\0';
1203 }
1204
1205 /*
1206 * NOW GO FOR THE SOURCES
1207 */
1208 if ((specType == Suffixes) || (specType == ExPath) ||
1209 (specType == Includes) || (specType == Libs) ||
1210 (specType == Null) || (specType == ExObjdir))
1211 {
1212 while (*line) {
1213 /*
1214 * If the target was one that doesn't take files as its sources
1215 * but takes something like suffixes, we take each
1216 * space-separated word on the line as a something and deal
1217 * with it accordingly.
1218 *
1219 * If the target was .SUFFIXES, we take each source as a
1220 * suffix and add it to the list of suffixes maintained by the
1221 * Suff module.
1222 *
1223 * If the target was a .PATH, we add the source as a directory
1224 * to search on the search path.
1225 *
1226 * If it was .INCLUDES, the source is taken to be the suffix of
1227 * files which will be #included and whose search path should
1228 * be present in the .INCLUDES variable.
1229 *
1230 * If it was .LIBS, the source is taken to be the suffix of
1231 * files which are considered libraries and whose search path
1232 * should be present in the .LIBS variable.
1233 *
1234 * If it was .NULL, the source is the suffix to use when a file
1235 * has no valid suffix.
1236 *
1237 * If it was .OBJDIR, the source is a new definition for .OBJDIR,
1238 * and will cause make to do a new chdir to that path.
1239 */
1240 while (*cp && !isspace ((unsigned char)*cp)) {
1241 cp++;
1242 }
1243 savec = *cp;
1244 *cp = '\0';
1245 switch (specType) {
1246 case Suffixes:
1247 Suff_AddSuffix(line, &mainNode);
1248 break;
1249 case ExPath:
1250 Lst_ForEach(paths, ParseAddDir, line);
1251 break;
1252 case Includes:
1253 Suff_AddInclude(line);
1254 break;
1255 case Libs:
1256 Suff_AddLib(line);
1257 break;
1258 case Null:
1259 Suff_SetNull(line);
1260 break;
1261 case ExObjdir:
1262 Main_SetObjdir(line);
1263 break;
1264 default:
1265 break;
1266 }
1267 *cp = savec;
1268 if (savec != '\0') {
1269 cp++;
1270 }
1271 while (*cp && isspace ((unsigned char)*cp)) {
1272 cp++;
1273 }
1274 line = cp;
1275 }
1276 if (paths) {
1277 Lst_Destroy(paths, NOFREE);
1278 }
1279 if (specType == ExPath)
1280 Dir_SetPATH();
1281 } else {
1282 while (*line) {
1283 /*
1284 * The targets take real sources, so we must beware of archive
1285 * specifications (i.e. things with left parentheses in them)
1286 * and handle them accordingly.
1287 */
1288 for (; *cp && !isspace ((unsigned char)*cp); cp++) {
1289 if ((*cp == LPAREN) && (cp > line) && (cp[-1] != '$')) {
1290 /*
1291 * Only stop for a left parenthesis if it isn't at the
1292 * start of a word (that'll be for variable changes
1293 * later) and isn't preceded by a dollar sign (a dynamic
1294 * source).
1295 */
1296 break;
1297 }
1298 }
1299
1300 if (*cp == LPAREN) {
1301 sources = Lst_Init(FALSE);
1302 if (Arch_ParseArchive(&line, sources, VAR_CMD) != SUCCESS) {
1303 Parse_Error(PARSE_FATAL,
1304 "Error in source archive spec \"%s\"", line);
1305 goto out;
1306 }
1307
1308 while (!Lst_IsEmpty (sources)) {
1309 gn = (GNode *)Lst_DeQueue(sources);
1310 ParseDoSrc(tOp, gn->name);
1311 }
1312 Lst_Destroy(sources, NOFREE);
1313 cp = line;
1314 } else {
1315 if (*cp) {
1316 *cp = '\0';
1317 cp += 1;
1318 }
1319
1320 ParseDoSrc(tOp, line);
1321 }
1322 while (*cp && isspace ((unsigned char)*cp)) {
1323 cp++;
1324 }
1325 line = cp;
1326 }
1327 }
1328
1329 if (mainNode == NILGNODE) {
1330 /*
1331 * If we have yet to decide on a main target to make, in the
1332 * absence of any user input, we want the first target on
1333 * the first dependency line that is actually a real target
1334 * (i.e. isn't a .USE or .EXEC rule) to be made.
1335 */
1336 Lst_ForEach(targets, ParseFindMain, NULL);
1337 }
1338
1339 out:
1340 if (curTargs)
1341 Lst_Destroy(curTargs, NOFREE);
1342 }
1343
1344 /*-
1345 *---------------------------------------------------------------------
1346 * Parse_IsVar --
1347 * Return TRUE if the passed line is a variable assignment. A variable
1348 * assignment consists of a single word followed by optional whitespace
1349 * followed by either a += or an = operator.
1350 * This function is used both by the Parse_File function and main when
1351 * parsing the command-line arguments.
1352 *
1353 * Input:
1354 * line the line to check
1355 *
1356 * Results:
1357 * TRUE if it is. FALSE if it ain't
1358 *
1359 * Side Effects:
1360 * none
1361 *---------------------------------------------------------------------
1362 */
1363 Boolean
1364 Parse_IsVar(char *line)
1365 {
1366 Boolean wasSpace = FALSE; /* set TRUE if found a space */
1367 Boolean haveName = FALSE; /* Set TRUE if have a variable name */
1368 int level = 0;
1369 #define ISEQOPERATOR(c) \
1370 (((c) == '+') || ((c) == ':') || ((c) == '?') || ((c) == '!'))
1371
1372 /*
1373 * Skip to variable name
1374 */
1375 for (;(*line == ' ') || (*line == '\t'); line++)
1376 continue;
1377
1378 for (; *line != '=' || level != 0; line++)
1379 switch (*line) {
1380 case '\0':
1381 /*
1382 * end-of-line -- can't be a variable assignment.
1383 */
1384 return FALSE;
1385
1386 case ' ':
1387 case '\t':
1388 /*
1389 * there can be as much white space as desired so long as there is
1390 * only one word before the operator
1391 */
1392 wasSpace = TRUE;
1393 break;
1394
1395 case LPAREN:
1396 case '{':
1397 level++;
1398 break;
1399
1400 case '}':
1401 case RPAREN:
1402 level--;
1403 break;
1404
1405 default:
1406 if (wasSpace && haveName) {
1407 if (ISEQOPERATOR(*line)) {
1408 /*
1409 * We must have a finished word
1410 */
1411 if (level != 0)
1412 return FALSE;
1413
1414 /*
1415 * When an = operator [+?!:] is found, the next
1416 * character must be an = or it ain't a valid
1417 * assignment.
1418 */
1419 if (line[1] == '=')
1420 return haveName;
1421 #ifdef SUNSHCMD
1422 /*
1423 * This is a shell command
1424 */
1425 if (strncmp(line, ":sh", 3) == 0)
1426 return haveName;
1427 #endif
1428 }
1429 /*
1430 * This is the start of another word, so not assignment.
1431 */
1432 return FALSE;
1433 }
1434 else {
1435 haveName = TRUE;
1436 wasSpace = FALSE;
1437 }
1438 break;
1439 }
1440
1441 return haveName;
1442 }
1443
1444 /*-
1445 *---------------------------------------------------------------------
1446 * Parse_DoVar --
1447 * Take the variable assignment in the passed line and do it in the
1448 * global context.
1449 *
1450 * Note: There is a lexical ambiguity with assignment modifier characters
1451 * in variable names. This routine interprets the character before the =
1452 * as a modifier. Therefore, an assignment like
1453 * C++=/usr/bin/CC
1454 * is interpreted as "C+ +=" instead of "C++ =".
1455 *
1456 * Input:
1457 * line a line guaranteed to be a variable assignment.
1458 * This reduces error checks
1459 * ctxt Context in which to do the assignment
1460 *
1461 * Results:
1462 * none
1463 *
1464 * Side Effects:
1465 * the variable structure of the given variable name is altered in the
1466 * global context.
1467 *---------------------------------------------------------------------
1468 */
1469 void
1470 Parse_DoVar(char *line, GNode *ctxt)
1471 {
1472 char *cp; /* pointer into line */
1473 enum {
1474 VAR_SUBST, VAR_APPEND, VAR_SHELL, VAR_NORMAL
1475 } type; /* Type of assignment */
1476 char *opc; /* ptr to operator character to
1477 * null-terminate the variable name */
1478 Boolean freeCp = FALSE; /* TRUE if cp needs to be freed,
1479 * i.e. if any variable expansion was
1480 * performed */
1481
1482 /*
1483 * Skip to variable name
1484 */
1485 while ((*line == ' ') || (*line == '\t')) {
1486 line++;
1487 }
1488
1489 /*
1490 * Skip to operator character, nulling out whitespace as we go
1491 */
1492 for (cp = line + 1; *cp != '='; cp++) {
1493 if (isspace ((unsigned char)*cp)) {
1494 *cp = '\0';
1495 }
1496 }
1497 opc = cp-1; /* operator is the previous character */
1498 *cp++ = '\0'; /* nuke the = */
1499
1500 /*
1501 * Check operator type
1502 */
1503 switch (*opc) {
1504 case '+':
1505 type = VAR_APPEND;
1506 *opc = '\0';
1507 break;
1508
1509 case '?':
1510 /*
1511 * If the variable already has a value, we don't do anything.
1512 */
1513 *opc = '\0';
1514 if (Var_Exists(line, ctxt)) {
1515 return;
1516 } else {
1517 type = VAR_NORMAL;
1518 }
1519 break;
1520
1521 case ':':
1522 type = VAR_SUBST;
1523 *opc = '\0';
1524 break;
1525
1526 case '!':
1527 type = VAR_SHELL;
1528 *opc = '\0';
1529 break;
1530
1531 default:
1532 #ifdef SUNSHCMD
1533 while (opc > line && *opc != ':')
1534 opc--;
1535
1536 if (strncmp(opc, ":sh", 3) == 0) {
1537 type = VAR_SHELL;
1538 *opc = '\0';
1539 break;
1540 }
1541 #endif
1542 type = VAR_NORMAL;
1543 break;
1544 }
1545
1546 while (isspace ((unsigned char)*cp)) {
1547 cp++;
1548 }
1549
1550 if (type == VAR_APPEND) {
1551 Var_Append(line, cp, ctxt);
1552 } else if (type == VAR_SUBST) {
1553 /*
1554 * Allow variables in the old value to be undefined, but leave their
1555 * invocation alone -- this is done by forcing oldVars to be false.
1556 * XXX: This can cause recursive variables, but that's not hard to do,
1557 * and this allows someone to do something like
1558 *
1559 * CFLAGS = $(.INCLUDES)
1560 * CFLAGS := -I.. $(CFLAGS)
1561 *
1562 * And not get an error.
1563 */
1564 Boolean oldOldVars = oldVars;
1565
1566 oldVars = FALSE;
1567
1568 /*
1569 * make sure that we set the variable the first time to nothing
1570 * so that it gets substituted!
1571 */
1572 if (!Var_Exists(line, ctxt))
1573 Var_Set(line, "", ctxt, 0);
1574
1575 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1576 oldVars = oldOldVars;
1577 freeCp = TRUE;
1578
1579 Var_Set(line, cp, ctxt, 0);
1580 } else if (type == VAR_SHELL) {
1581 char *res;
1582 const char *error;
1583
1584 if (strchr(cp, '$') != NULL) {
1585 /*
1586 * There's a dollar sign in the command, so perform variable
1587 * expansion on the whole thing. The resulting string will need
1588 * freeing when we're done, so set freeCmd to TRUE.
1589 */
1590 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1591 freeCp = TRUE;
1592 }
1593
1594 res = Cmd_Exec(cp, &error);
1595 Var_Set(line, res, ctxt, 0);
1596 free(res);
1597
1598 if (error)
1599 Parse_Error(PARSE_WARNING, error, cp);
1600 } else {
1601 /*
1602 * Normal assignment -- just do it.
1603 */
1604 Var_Set(line, cp, ctxt, 0);
1605 }
1606 if (strcmp(line, MAKEOVERRIDES) == 0)
1607 Main_ExportMAKEFLAGS(FALSE); /* re-export MAKEFLAGS */
1608 else if (strcmp(line, ".CURDIR") == 0) {
1609 /*
1610 * Somone is being (too?) clever...
1611 * Let's pretend they know what they are doing and
1612 * re-initialize the 'cur' Path.
1613 */
1614 Dir_InitCur(cp);
1615 Dir_SetPATH();
1616 } else if (strcmp(line, MAKE_JOB_PREFIX) == 0) {
1617 Job_SetPrefix();
1618 } else if (strcmp(line, MAKE_EXPORTED) == 0) {
1619 Var_Export(cp, 0);
1620 }
1621 if (freeCp)
1622 free(cp);
1623 }
1624
1625
1626 /*-
1627 * ParseAddCmd --
1628 * Lst_ForEach function to add a command line to all targets
1629 *
1630 * Input:
1631 * gnp the node to which the command is to be added
1632 * cmd the command to add
1633 *
1634 * Results:
1635 * Always 0
1636 *
1637 * Side Effects:
1638 * A new element is added to the commands list of the node.
1639 */
1640 static int
1641 ParseAddCmd(ClientData gnp, ClientData cmd)
1642 {
1643 GNode *gn = (GNode *)gnp;
1644
1645 /* Add to last (ie current) cohort for :: targets */
1646 if ((gn->type & OP_DOUBLEDEP) && !Lst_IsEmpty (gn->cohorts))
1647 gn = (GNode *)Lst_Datum(Lst_Last(gn->cohorts));
1648
1649 /* if target already supplied, ignore commands */
1650 if (!(gn->type & OP_HAS_COMMANDS)) {
1651 (void)Lst_AtEnd(gn->commands, cmd);
1652 ParseMark(gn);
1653 } else {
1654 #ifdef notyet
1655 /* XXX: We cannot do this until we fix the tree */
1656 (void)Lst_AtEnd(gn->commands, cmd);
1657 Parse_Error(PARSE_WARNING,
1658 "overriding commands for target \"%s\"; "
1659 "previous commands defined at %s: %d ignored",
1660 gn->name, gn->fname, gn->lineno);
1661 #else
1662 Parse_Error(PARSE_WARNING,
1663 "duplicate script for target \"%s\" ignored",
1664 gn->name);
1665 ParseErrorInternal(gn->fname, gn->lineno, PARSE_WARNING,
1666 "using previous script for \"%s\" defined here",
1667 gn->name);
1668 #endif
1669 }
1670 return(0);
1671 }
1672
1673 /*-
1674 *-----------------------------------------------------------------------
1675 * ParseHasCommands --
1676 * Callback procedure for Parse_File when destroying the list of
1677 * targets on the last dependency line. Marks a target as already
1678 * having commands if it does, to keep from having shell commands
1679 * on multiple dependency lines.
1680 *
1681 * Input:
1682 * gnp Node to examine
1683 *
1684 * Results:
1685 * None
1686 *
1687 * Side Effects:
1688 * OP_HAS_COMMANDS may be set for the target.
1689 *
1690 *-----------------------------------------------------------------------
1691 */
1692 static void
1693 ParseHasCommands(ClientData gnp)
1694 {
1695 GNode *gn = (GNode *)gnp;
1696 if (!Lst_IsEmpty(gn->commands)) {
1697 gn->type |= OP_HAS_COMMANDS;
1698 }
1699 }
1700
1701 /*-
1702 *-----------------------------------------------------------------------
1703 * Parse_AddIncludeDir --
1704 * Add a directory to the path searched for included makefiles
1705 * bracketed by double-quotes. Used by functions in main.c
1706 *
1707 * Input:
1708 * dir The name of the directory to add
1709 *
1710 * Results:
1711 * None.
1712 *
1713 * Side Effects:
1714 * The directory is appended to the list.
1715 *
1716 *-----------------------------------------------------------------------
1717 */
1718 void
1719 Parse_AddIncludeDir(char *dir)
1720 {
1721 (void)Dir_AddDir(parseIncPath, dir);
1722 }
1723
1724 /*-
1725 *---------------------------------------------------------------------
1726 * ParseDoInclude --
1727 * Push to another file.
1728 *
1729 * The input is the line minus the `.'. A file spec is a string
1730 * enclosed in <> or "". The former is looked for only in sysIncPath.
1731 * The latter in . and the directories specified by -I command line
1732 * options
1733 *
1734 * Results:
1735 * None
1736 *
1737 * Side Effects:
1738 * A structure is added to the includes Lst and readProc, lineno,
1739 * fname and curFILE are altered for the new file
1740 *---------------------------------------------------------------------
1741 */
1742
1743 static void
1744 Parse_include_file(char *file, Boolean isSystem, int silent)
1745 {
1746 char *fullname; /* full pathname of file */
1747 char *newName;
1748 char *prefEnd, *incdir;
1749 int fd;
1750 int i;
1751
1752 /*
1753 * Now we know the file's name and its search path, we attempt to
1754 * find the durn thing. A return of NULL indicates the file don't
1755 * exist.
1756 */
1757 fullname = file[0] == '/' ? estrdup(file) : NULL;
1758
1759 if (fullname == NULL && !isSystem) {
1760 /*
1761 * Include files contained in double-quotes are first searched for
1762 * relative to the including file's location. We don't want to
1763 * cd there, of course, so we just tack on the old file's
1764 * leading path components and call Dir_FindFile to see if
1765 * we can locate the beast.
1766 */
1767
1768 incdir = estrdup(curFile->fname);
1769 prefEnd = strrchr(incdir, '/');
1770 if (prefEnd != NULL) {
1771 *prefEnd = '\0';
1772 /* Now do lexical processing of leading "../" on the filename */
1773 for (i = 0; strncmp(file + i, "../", 3) == 0; i += 3) {
1774 prefEnd = strrchr(incdir + 1, '/');
1775 if (prefEnd == NULL || strcmp(prefEnd, "/..") == 0)
1776 break;
1777 *prefEnd = '\0';
1778 }
1779 newName = str_concat(incdir, file + i, STR_ADDSLASH);
1780 fullname = Dir_FindFile(newName, parseIncPath);
1781 if (fullname == NULL)
1782 fullname = Dir_FindFile(newName, dirSearchPath);
1783 free(newName);
1784 }
1785 free(incdir);
1786
1787 if (fullname == NULL) {
1788 /*
1789 * Makefile wasn't found in same directory as included makefile.
1790 * Search for it first on the -I search path,
1791 * then on the .PATH search path, if not found in a -I directory.
1792 * If we have a suffix specific path we should use that.
1793 */
1794 char *suff;
1795 Lst suffPath = NILLST;
1796
1797 if ((suff = strrchr(file, '.')) != NULL) {
1798 suffPath = Suff_GetPath(suff);
1799 if (suffPath != NILLST) {
1800 fullname = Dir_FindFile(file, suffPath);
1801 }
1802 }
1803 if (fullname == NULL) {
1804 fullname = Dir_FindFile(file, parseIncPath);
1805 if (fullname == NULL) {
1806 fullname = Dir_FindFile(file, dirSearchPath);
1807 }
1808 }
1809 }
1810 }
1811
1812 /* Looking for a system file or file still not found */
1813 if (fullname == NULL) {
1814 /*
1815 * Look for it on the system path
1816 */
1817 fullname = Dir_FindFile(file,
1818 Lst_IsEmpty(sysIncPath) ? defIncPath : sysIncPath);
1819 }
1820
1821 if (fullname == NULL) {
1822 if (!silent)
1823 Parse_Error(PARSE_FATAL, "Could not find %s", file);
1824 return;
1825 }
1826
1827 /* Actually open the file... */
1828 fd = open(fullname, O_RDONLY);
1829 if (fd == -1) {
1830 if (!silent)
1831 Parse_Error(PARSE_FATAL, "Cannot open %s", fullname);
1832 free(fullname);
1833 return;
1834 }
1835
1836 /* Start reading from this file next */
1837 Parse_SetInput(fullname, 0, fd, NULL);
1838 }
1839
1840 static void
1841 ParseDoInclude(char *line)
1842 {
1843 char endc; /* the character which ends the file spec */
1844 char *cp; /* current position in file spec */
1845 int silent = (*line != 'i') ? 1 : 0;
1846 char *file = &line[7 + silent];
1847
1848 /* Skip to delimiter character so we know where to look */
1849 while (*file == ' ' || *file == '\t')
1850 file++;
1851
1852 if (*file != '"' && *file != '<') {
1853 Parse_Error(PARSE_FATAL,
1854 ".include filename must be delimited by '\"' or '<'");
1855 return;
1856 }
1857
1858 /*
1859 * Set the search path on which to find the include file based on the
1860 * characters which bracket its name. Angle-brackets imply it's
1861 * a system Makefile while double-quotes imply it's a user makefile
1862 */
1863 if (*file == '<') {
1864 endc = '>';
1865 } else {
1866 endc = '"';
1867 }
1868
1869 /* Skip to matching delimiter */
1870 for (cp = ++file; *cp && *cp != endc; cp++)
1871 continue;
1872
1873 if (*cp != endc) {
1874 Parse_Error(PARSE_FATAL,
1875 "Unclosed %cinclude filename. '%c' expected",
1876 '.', endc);
1877 return;
1878 }
1879 *cp = '\0';
1880
1881 /*
1882 * Substitute for any variables in the file name before trying to
1883 * find the thing.
1884 */
1885 file = Var_Subst(NULL, file, VAR_CMD, FALSE);
1886
1887 Parse_include_file(file, endc == '>', silent);
1888 free(file);
1889 }
1890
1891
1892 /*-
1893 *---------------------------------------------------------------------
1894 * ParseSetParseFile --
1895 * Set the .PARSEDIR and .PARSEFILE variables to the dirname and
1896 * basename of the given filename
1897 *
1898 * Results:
1899 * None
1900 *
1901 * Side Effects:
1902 * The .PARSEDIR and .PARSEFILE variables are overwritten by the
1903 * dirname and basename of the given filename.
1904 *---------------------------------------------------------------------
1905 */
1906 static void
1907 ParseSetParseFile(const char *filename)
1908 {
1909 char *slash;
1910 char *dirname;
1911 size_t len;
1912
1913 slash = strrchr(filename, '/');
1914 if (slash == NULL) {
1915 Var_Set(".PARSEDIR", ".", VAR_GLOBAL, 0);
1916 Var_Set(".PARSEFILE", filename, VAR_GLOBAL, 0);
1917 } else {
1918 len = slash - filename;
1919 dirname = emalloc(len + 1);
1920 (void)memcpy(dirname, filename, len);
1921 dirname[len] = 0;
1922 Var_Set(".PARSEDIR", dirname, VAR_GLOBAL, 0);
1923 Var_Set(".PARSEFILE", slash+1, VAR_GLOBAL, 0);
1924 free(dirname);
1925 }
1926 }
1927
1928 /*
1929 * Track the makefiles we read - so makefiles can
1930 * set dependencies on them.
1931 * Avoid adding anything more than once.
1932 */
1933
1934 static void
1935 ParseTrackInput(const char *name)
1936 {
1937 char *old;
1938 char *fp = NULL;
1939 size_t name_len = strlen(name);
1940
1941 old = Var_Value(MAKE_MAKEFILES, VAR_GLOBAL, &fp);
1942 if (old) {
1943 /* does it contain name? */
1944 for (; old != NULL; old = strchr(old, ' ')) {
1945 if (*old == ' ')
1946 old++;
1947 if (memcmp(old, name, name_len) == 0
1948 && (old[name_len] == 0 || old[name_len] == ' '))
1949 goto cleanup;
1950 }
1951 }
1952 Var_Append (MAKE_MAKEFILES, name, VAR_GLOBAL);
1953 cleanup:
1954 if (fp) {
1955 free(fp);
1956 }
1957 }
1958
1959
1960 /*-
1961 *---------------------------------------------------------------------
1962 * Parse_setInput --
1963 * Start Parsing from the given source
1964 *
1965 * Results:
1966 * None
1967 *
1968 * Side Effects:
1969 * A structure is added to the includes Lst and readProc, lineno,
1970 * fname and curFile are altered for the new file
1971 *---------------------------------------------------------------------
1972 */
1973 void
1974 Parse_SetInput(const char *name, int line, int fd, char *buf)
1975 {
1976 if (name == NULL)
1977 name = curFile->fname;
1978 else
1979 ParseTrackInput(name);
1980
1981 if (DEBUG(PARSE))
1982 fprintf(debug_file, "Parse_SetInput: file %s, line %d, fd %d, buf %p\n",
1983 name, line, fd, buf);
1984
1985 if (fd == -1 && buf == NULL)
1986 /* sanity */
1987 return;
1988
1989 if (curFile != NULL)
1990 /* Save exiting file info */
1991 Lst_AtFront(includes, curFile);
1992
1993 /* Allocate and fill in new structure */
1994 curFile = emalloc(sizeof *curFile);
1995
1996 /*
1997 * Once the previous state has been saved, we can get down to reading
1998 * the new file. We set up the name of the file to be the absolute
1999 * name of the include file so error messages refer to the right
2000 * place.
2001 */
2002 curFile->fname = name;
2003 curFile->lineno = line;
2004 curFile->fd = fd;
2005 curFile->cond_depth = Cond_save_depth();
2006
2007 ParseSetParseFile(name);
2008
2009 if (buf == NULL) {
2010 /*
2011 * Allocate a 32k data buffer (as stdio seems to).
2012 * Set pointers so that first ParseReadc has to do a file read.
2013 */
2014 buf = emalloc(IFILE_BUFLEN);
2015 buf[0] = 0;
2016 curFile->P_str = buf;
2017 curFile->P_ptr = buf;
2018 curFile->P_end = buf;
2019 curFile->P_buflen = IFILE_BUFLEN;
2020 } else {
2021 /* Start reading from the start of the buffer */
2022 curFile->P_str = buf;
2023 curFile->P_ptr = buf;
2024 curFile->P_end = NULL;
2025 }
2026
2027 }
2028
2029 #ifdef SYSVINCLUDE
2030 /*-
2031 *---------------------------------------------------------------------
2032 * ParseTraditionalInclude --
2033 * Push to another file.
2034 *
2035 * The input is the current line. The file name(s) are
2036 * following the "include".
2037 *
2038 * Results:
2039 * None
2040 *
2041 * Side Effects:
2042 * A structure is added to the includes Lst and readProc, lineno,
2043 * fname and curFILE are altered for the new file
2044 *---------------------------------------------------------------------
2045 */
2046 static void
2047 ParseTraditionalInclude(char *line)
2048 {
2049 char *cp; /* current position in file spec */
2050 int done = 0;
2051 int silent = (line[0] != 'i') ? 1 : 0;
2052 char *file = &line[silent + 7];
2053 char *all_files;
2054
2055 if (DEBUG(PARSE)) {
2056 fprintf(debug_file, "ParseTraditionalInclude: %s\n", file);
2057 }
2058
2059 /*
2060 * Skip over whitespace
2061 */
2062 while (isspace((unsigned char)*file))
2063 file++;
2064
2065 /*
2066 * Substitute for any variables in the file name before trying to
2067 * find the thing.
2068 */
2069 all_files = Var_Subst(NULL, file, VAR_CMD, FALSE);
2070
2071 if (*file == '\0') {
2072 Parse_Error(PARSE_FATAL,
2073 "Filename missing from \"include\"");
2074 return;
2075 }
2076
2077 for (file = all_files; !done; file = cp + 1) {
2078 /* Skip to end of line or next whitespace */
2079 for (cp = file; *cp && !isspace((unsigned char) *cp); cp++)
2080 continue;
2081
2082 if (*cp)
2083 *cp = '\0';
2084 else
2085 done = 1;
2086
2087 Parse_include_file(file, FALSE, silent);
2088 }
2089 free(all_files);
2090 }
2091 #endif
2092
2093 /*-
2094 *---------------------------------------------------------------------
2095 * ParseEOF --
2096 * Called when EOF is reached in the current file. If we were reading
2097 * an include file, the includes stack is popped and things set up
2098 * to go back to reading the previous file at the previous location.
2099 *
2100 * Results:
2101 * CONTINUE if there's more to do. DONE if not.
2102 *
2103 * Side Effects:
2104 * The old curFILE, is closed. The includes list is shortened.
2105 * lineno, curFILE, and fname are changed if CONTINUE is returned.
2106 *---------------------------------------------------------------------
2107 */
2108 static int
2109 ParseEOF(void)
2110 {
2111 /* Ensure the makefile (or loop) didn't have mismatched conditionals */
2112 Cond_restore_depth(curFile->cond_depth);
2113
2114 /* Dispose of curFile info */
2115 /* Leak curFile->fname because all the gnodes have pointers to it */
2116 if (curFile->fd != -1)
2117 close(curFile->fd);
2118 free(curFile->P_str);
2119 free(curFile);
2120
2121 curFile = Lst_DeQueue(includes);
2122
2123 if (curFile == (IFile *)NIL) {
2124 /* We've run out of input */
2125 Var_Delete(".PARSEDIR", VAR_GLOBAL);
2126 Var_Delete(".PARSEFILE", VAR_GLOBAL);
2127 return DONE;
2128 }
2129
2130 if (DEBUG(PARSE))
2131 fprintf(debug_file, "ParseEOF: returning to file %s, line %zu, fd %d\n",
2132 curFile->fname, curFile->lineno, curFile->fd);
2133
2134 /* Restore the PARSEDIR/PARSEFILE variables */
2135 ParseSetParseFile(curFile->fname);
2136 return (CONTINUE);
2137 }
2138
2139 #define PARSE_RAW 1
2140 #define PARSE_SKIP 2
2141
2142 static char *
2143 ParseGetLine(int flags, int *length)
2144 {
2145 IFile *cf = curFile;
2146 char *ptr;
2147 char ch;
2148 char *line;
2149 char *line_end;
2150 char *escaped;
2151 char *comment;
2152 char *tp;
2153 ssize_t len;
2154 int dist;
2155
2156 /* Loop through blank lines and comment lines */
2157 for (;;) {
2158 cf->lineno++;
2159 line = cf->P_ptr;
2160 ptr = line;
2161 line_end = line;
2162 escaped = NULL;
2163 comment = NULL;
2164 for (;;) {
2165 ch = *ptr;
2166 if (ch == 0 || (ch == '\\' && ptr[1] == 0)) {
2167 if (cf->P_end == NULL)
2168 /* End of string (aka for loop) data */
2169 break;
2170 /* End of data read from file, read more data */
2171 if (ptr != cf->P_end && (ch != '\\' || ptr + 1 != cf->P_end)) {
2172 Parse_Error(PARSE_FATAL, "Zero byte read from file");
2173 return NULL;
2174 }
2175 /* Move existing data to (near) start of file buffer */
2176 len = cf->P_end - cf->P_ptr;
2177 tp = cf->P_str + 32;
2178 (void)memmove(tp, cf->P_ptr, (size_t)len);
2179 dist = cf->P_ptr - tp;
2180 /* Update all pointers to reflect moved data */
2181 ptr -= dist;
2182 line -= dist;
2183 line_end -= dist;
2184 if (escaped)
2185 escaped -= dist;
2186 if (comment)
2187 comment -= dist;
2188 cf->P_ptr = tp;
2189 tp += len;
2190 cf->P_end = tp;
2191 /* Try to read more data from file into buffer space */
2192 len = cf->P_str + cf->P_buflen - tp - 32;
2193 if (len <= 0) {
2194 /* We need a bigger buffer to hold this line */
2195 tp = erealloc(cf->P_str, cf->P_buflen + IFILE_BUFLEN);
2196 cf->P_end = cf->P_end - cf->P_str + tp;
2197 ptr = ptr - cf->P_str + tp;
2198 line = line - cf->P_str + tp;
2199 line_end = line_end - cf->P_str + tp;
2200 if (escaped)
2201 escaped = escaped - cf->P_str + tp;
2202 if (comment)
2203 comment = comment - cf->P_str + tp;
2204 cf->P_str = tp;
2205 tp = cf->P_end;
2206 len += IFILE_BUFLEN;
2207 cf->P_buflen += IFILE_BUFLEN;
2208 }
2209 len = read(cf->fd, tp, (size_t)len);
2210 if (len <= 0) {
2211 if (len < 0) {
2212 Parse_Error(PARSE_FATAL, "Makefile read error: %s",
2213 strerror(errno));
2214 return NULL;
2215 }
2216 /* End of file */
2217 break;
2218 }
2219 /* 0 terminate the data, and update end pointer */
2220 tp += len;
2221 cf->P_end = tp;
2222 *tp = 0;
2223 /* Process newly read characters */
2224 continue;
2225 }
2226
2227 if (ch == '\\') {
2228 /* Don't treat next character as special, remember first one */
2229 if (escaped == NULL)
2230 escaped = ptr;
2231 if (ptr[1] == '\n')
2232 cf->lineno++;
2233 ptr += 2;
2234 line_end = ptr;
2235 continue;
2236 }
2237 if (ch == '#' && comment == NULL) {
2238 /* Remember first '#' for comment stripping */
2239 comment = line_end;
2240 }
2241 ptr++;
2242 if (ch == '\n')
2243 break;
2244 if (!isspace((unsigned char)ch))
2245 /* We are not interested in trailing whitespace */
2246 line_end = ptr;
2247 }
2248
2249 /* Save next 'to be processed' location */
2250 cf->P_ptr = ptr;
2251
2252 /* Check we have a non-comment, non-blank line */
2253 if (line_end == line || comment == line) {
2254 if (ch == 0)
2255 /* At end of file */
2256 return NULL;
2257 /* Parse another line */
2258 continue;
2259 }
2260
2261 /* We now have a line of data */
2262 *line_end = 0;
2263
2264 if (flags & PARSE_RAW) {
2265 /* Leave '\' (etc) in line buffer (eg 'for' lines) */
2266 *length = line_end - line;
2267 return line;
2268 }
2269
2270 if (flags & PARSE_SKIP) {
2271 /* Completely ignore non-directives */
2272 if (line[0] != '.')
2273 continue;
2274 /* We could do more of the .else/.elif/.endif checks here */
2275 }
2276 break;
2277 }
2278
2279 /* Brutally ignore anything after a non-escaped '#' in non-commands */
2280 if (comment != NULL && line[0] != '\t') {
2281 line_end = comment;
2282 *line_end = 0;
2283 }
2284
2285 /* If we didn't see a '\\' then the in-situ data is fine */
2286 if (escaped == NULL) {
2287 *length = line_end - line;
2288 return line;
2289 }
2290
2291 /* Remove escapes from '\n' and '#' */
2292 tp = ptr = escaped;
2293 escaped = line;
2294 for (; ; *tp++ = ch) {
2295 ch = *ptr++;
2296 if (ch != '\\') {
2297 if (ch == 0)
2298 break;
2299 continue;
2300 }
2301
2302 ch = *ptr++;
2303 if (ch == 0) {
2304 /* Delete '\\' at end of buffer */
2305 tp--;
2306 break;
2307 }
2308
2309 if (ch == '#' && line[0] != '\t')
2310 /* Delete '\\' from before '#' on non-command lines */
2311 continue;
2312
2313 if (ch != '\n') {
2314 /* Leave '\\' in buffer for later */
2315 *tp++ = '\\';
2316 /* Make sure we don't delete an escaped ' ' from the line end */
2317 escaped = tp + 1;
2318 continue;
2319 }
2320
2321 /* Escaped '\n' replace following whitespace with a single ' ' */
2322 while (ptr[0] == ' ' || ptr[0] == '\t')
2323 ptr++;
2324 ch = ' ';
2325 }
2326
2327 /* Delete any trailing spaces - eg from empty continuations */
2328 while (tp > escaped && isspace((unsigned char)tp[-1]))
2329 tp--;
2330
2331 *tp = 0;
2332 *length = tp - line;
2333 return line;
2334 }
2335
2336 /*-
2337 *---------------------------------------------------------------------
2338 * ParseReadLine --
2339 * Read an entire line from the input file. Called only by Parse_File.
2340 *
2341 * Results:
2342 * A line w/o its newline
2343 *
2344 * Side Effects:
2345 * Only those associated with reading a character
2346 *---------------------------------------------------------------------
2347 */
2348 static char *
2349 ParseReadLine(void)
2350 {
2351 char *line; /* Result */
2352 int lineLength; /* Length of result */
2353 int lineno; /* Saved line # */
2354
2355 for (;;) {
2356 line = ParseGetLine(0, &lineLength);
2357 if (line == NULL)
2358 return NULL;
2359
2360 if (line[0] != '.')
2361 return line;
2362
2363 /*
2364 * The line might be a conditional. Ask the conditional module
2365 * about it and act accordingly
2366 */
2367 switch (Cond_Eval(line)) {
2368 case COND_SKIP:
2369 /* Skip to next conditional that evaluates to COND_PARSE. */
2370 do {
2371 line = ParseGetLine(PARSE_SKIP, &lineLength);
2372 } while (line && Cond_Eval(line) != COND_PARSE);
2373 if (line == NULL)
2374 break;
2375 continue;
2376 case COND_PARSE:
2377 continue;
2378 case COND_INVALID: /* Not a conditional line */
2379 if (!For_Eval(line))
2380 break;
2381 lineno = curFile->lineno;
2382 /* Skip after the matching end */
2383 do {
2384 line = ParseGetLine(PARSE_RAW, &lineLength);
2385 if (line == NULL) {
2386 Parse_Error(PARSE_FATAL,
2387 "Unexpected end of file in for loop.\n");
2388 break;
2389 }
2390 } while (For_Eval(line));
2391 /* Stash each iteration as a new 'input file' */
2392 For_Run(lineno);
2393 /* Read next line from for-loop buffer */
2394 continue;
2395 }
2396 return (line);
2397 }
2398 }
2399
2400 /*-
2401 *-----------------------------------------------------------------------
2402 * ParseFinishLine --
2403 * Handle the end of a dependency group.
2404 *
2405 * Results:
2406 * Nothing.
2407 *
2408 * Side Effects:
2409 * inLine set FALSE. 'targets' list destroyed.
2410 *
2411 *-----------------------------------------------------------------------
2412 */
2413 static void
2414 ParseFinishLine(void)
2415 {
2416 if (inLine) {
2417 Lst_ForEach(targets, Suff_EndTransform, NULL);
2418 Lst_Destroy(targets, ParseHasCommands);
2419 targets = NULL;
2420 inLine = FALSE;
2421 }
2422 }
2423
2424
2425 /*-
2426 *---------------------------------------------------------------------
2427 * Parse_File --
2428 * Parse a file into its component parts, incorporating it into the
2429 * current dependency graph. This is the main function and controls
2430 * almost every other function in this module
2431 *
2432 * Input:
2433 * name the name of the file being read
2434 * fd Open file to makefile to parse
2435 *
2436 * Results:
2437 * None
2438 *
2439 * Side Effects:
2440 * closes fd.
2441 * Loads. Nodes are added to the list of all targets, nodes and links
2442 * are added to the dependency graph. etc. etc. etc.
2443 *---------------------------------------------------------------------
2444 */
2445 void
2446 Parse_File(const char *name, int fd)
2447 {
2448 char *cp; /* pointer into the line */
2449 char *line; /* the line we're working on */
2450
2451 inLine = FALSE;
2452 fatals = 0;
2453
2454 Parse_SetInput(name, 0, fd, NULL);
2455
2456 do {
2457 for (; (line = ParseReadLine()) != NULL; ) {
2458 if (DEBUG(PARSE))
2459 (void)fprintf(debug_file, "ParseReadLine (%zu): '%s'\n",
2460 curFile->lineno, line);
2461 if (*line == '.') {
2462 /*
2463 * Lines that begin with the special character are either
2464 * include or undef directives.
2465 */
2466 for (cp = line + 1; isspace((unsigned char)*cp); cp++) {
2467 continue;
2468 }
2469 if (strncmp(cp, "include", 7) == 0 ||
2470 ((cp[0] == 's' || cp[0] == '-') &&
2471 strncmp(&cp[1], "include", 7) == 0)) {
2472 ParseDoInclude(cp);
2473 continue;
2474 }
2475 if (strncmp(cp, "undef", 5) == 0) {
2476 char *cp2;
2477 for (cp += 5; isspace((unsigned char) *cp); cp++)
2478 continue;
2479 for (cp2 = cp; !isspace((unsigned char) *cp2) &&
2480 (*cp2 != '\0'); cp2++)
2481 continue;
2482 *cp2 = '\0';
2483 Var_Delete(cp, VAR_GLOBAL);
2484 continue;
2485 } else if (strncmp(cp, "export", 6) == 0) {
2486 for (cp += 6; isspace((unsigned char) *cp); cp++)
2487 continue;
2488 Var_Export(cp, 1);
2489 continue;
2490 }
2491 }
2492
2493 if (*line == '\t') {
2494 /*
2495 * If a line starts with a tab, it can only hope to be
2496 * a creation command.
2497 */
2498 cp = line + 1;
2499 shellCommand:
2500 for (; isspace ((unsigned char)*cp); cp++) {
2501 continue;
2502 }
2503 if (*cp) {
2504 if (!inLine)
2505 Parse_Error(PARSE_FATAL,
2506 "Unassociated shell command \"%s\"",
2507 cp);
2508 /*
2509 * So long as it's not a blank line and we're actually
2510 * in a dependency spec, add the command to the list of
2511 * commands of all targets in the dependency spec
2512 */
2513 if (targets) {
2514 cp = estrdup(cp);
2515 Lst_ForEach(targets, ParseAddCmd, cp);
2516 #ifdef CLEANUP
2517 Lst_AtEnd(targCmds, cp);
2518 #endif
2519 }
2520 }
2521 continue;
2522 }
2523
2524 #ifdef SYSVINCLUDE
2525 if (((strncmp(line, "include", 7) == 0 &&
2526 isspace((unsigned char) line[7])) ||
2527 ((line[0] == 's' || line[0] == '-') &&
2528 strncmp(&line[1], "include", 7) == 0 &&
2529 isspace((unsigned char) line[8]))) &&
2530 strchr(line, ':') == NULL) {
2531 /*
2532 * It's an S3/S5-style "include".
2533 */
2534 ParseTraditionalInclude(line);
2535 continue;
2536 }
2537 #endif
2538 if (Parse_IsVar(line)) {
2539 ParseFinishLine();
2540 Parse_DoVar(line, VAR_GLOBAL);
2541 continue;
2542 }
2543
2544 #ifndef POSIX
2545 /*
2546 * To make life easier on novices, if the line is indented we
2547 * first make sure the line has a dependency operator in it.
2548 * If it doesn't have an operator and we're in a dependency
2549 * line's script, we assume it's actually a shell command
2550 * and add it to the current list of targets.
2551 */
2552 cp = line;
2553 if (isspace((unsigned char) line[0])) {
2554 while ((*cp != '\0') && isspace((unsigned char) *cp))
2555 cp++;
2556 while (*cp && (ParseIsEscaped(line, cp) ||
2557 (*cp != ':') && (*cp != '!'))) {
2558 cp++;
2559 }
2560 if (*cp == '\0') {
2561 if (inLine) {
2562 Parse_Error(PARSE_WARNING,
2563 "Shell command needs a leading tab");
2564 goto shellCommand;
2565 }
2566 }
2567 }
2568 #endif
2569 ParseFinishLine();
2570
2571 /*
2572 * For some reason - probably to make the parser impossible -
2573 * a ';' can be used to separate commands from dependencies.
2574 * No attempt is made to avoid ';' inside substitution patterns.
2575 */
2576 for (cp = line; *cp != 0; cp++) {
2577 if (*cp == '\\' && cp[1] != 0) {
2578 cp++;
2579 continue;
2580 }
2581 if (*cp == ';')
2582 break;
2583 }
2584 if (*cp != 0)
2585 /* Terminate the dependency list at the ';' */
2586 *cp++ = 0;
2587 else
2588 cp = NULL;
2589
2590 /*
2591 * We now know it's a dependency line so it needs to have all
2592 * variables expanded before being parsed. Tell the variable
2593 * module to complain if some variable is undefined...
2594 */
2595 line = Var_Subst(NULL, line, VAR_CMD, TRUE);
2596
2597 /*
2598 * Need a non-circular list for the target nodes
2599 */
2600 if (targets)
2601 Lst_Destroy(targets, NOFREE);
2602
2603 targets = Lst_Init(FALSE);
2604 inLine = TRUE;
2605
2606 ParseDoDependency(line);
2607 free(line);
2608
2609 /* If there were commands after a ';', add them now */
2610 if (cp != NULL) {
2611 goto shellCommand;
2612 }
2613 }
2614 /*
2615 * Reached EOF, but it may be just EOF of an include file...
2616 */
2617 } while (ParseEOF() == CONTINUE);
2618
2619 if (fatals) {
2620 (void)fprintf(stderr,
2621 "%s: Fatal errors encountered -- cannot continue\n",
2622 progname);
2623 PrintOnError(NULL);
2624 exit(1);
2625 }
2626 }
2627
2628 /*-
2629 *---------------------------------------------------------------------
2630 * Parse_Init --
2631 * initialize the parsing module
2632 *
2633 * Results:
2634 * none
2635 *
2636 * Side Effects:
2637 * the parseIncPath list is initialized...
2638 *---------------------------------------------------------------------
2639 */
2640 void
2641 Parse_Init(void)
2642 {
2643 mainNode = NILGNODE;
2644 parseIncPath = Lst_Init(FALSE);
2645 sysIncPath = Lst_Init(FALSE);
2646 defIncPath = Lst_Init(FALSE);
2647 includes = Lst_Init(FALSE);
2648 #ifdef CLEANUP
2649 targCmds = Lst_Init(FALSE);
2650 #endif
2651 }
2652
2653 void
2654 Parse_End(void)
2655 {
2656 #ifdef CLEANUP
2657 Lst_Destroy(targCmds, (FreeProc *)free);
2658 if (targets)
2659 Lst_Destroy(targets, NOFREE);
2660 Lst_Destroy(defIncPath, Dir_Destroy);
2661 Lst_Destroy(sysIncPath, Dir_Destroy);
2662 Lst_Destroy(parseIncPath, Dir_Destroy);
2663 Lst_Destroy(includes, NOFREE); /* Should be empty now */
2664 #endif
2665 }
2666
2667
2668 /*-
2669 *-----------------------------------------------------------------------
2670 * Parse_MainName --
2671 * Return a Lst of the main target to create for main()'s sake. If
2672 * no such target exists, we Punt with an obnoxious error message.
2673 *
2674 * Results:
2675 * A Lst of the single node to create.
2676 *
2677 * Side Effects:
2678 * None.
2679 *
2680 *-----------------------------------------------------------------------
2681 */
2682 Lst
2683 Parse_MainName(void)
2684 {
2685 Lst mainList; /* result list */
2686
2687 mainList = Lst_Init(FALSE);
2688
2689 if (mainNode == NILGNODE) {
2690 Punt("no target to make.");
2691 /*NOTREACHED*/
2692 } else if (mainNode->type & OP_DOUBLEDEP) {
2693 (void)Lst_AtEnd(mainList, mainNode);
2694 Lst_Concat(mainList, mainNode->cohorts, LST_CONCNEW);
2695 }
2696 else
2697 (void)Lst_AtEnd(mainList, mainNode);
2698 Var_Append(".TARGETS", mainNode->name, VAR_GLOBAL);
2699 return (mainList);
2700 }
2701
2702 /*-
2703 *-----------------------------------------------------------------------
2704 * ParseMark --
2705 * Add the filename and lineno to the GNode so that we remember
2706 * where it was first defined.
2707 *
2708 * Side Effects:
2709 * None.
2710 *
2711 *-----------------------------------------------------------------------
2712 */
2713 static void
2714 ParseMark(GNode *gn)
2715 {
2716 gn->fname = curFile->fname;
2717 gn->lineno = curFile->lineno;
2718 }
2719