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