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