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