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