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