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