parse.c revision 1.42 1 /* $NetBSD: parse.c,v 1.42 1999/06/02 18:47:11 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.42 1999/06/02 18:47:11 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.42 1999/06/02 18:47:11 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 ((unsigned char)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 ((unsigned char)*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 ((unsigned char)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 ((unsigned char)*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 ((unsigned char)*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 ((unsigned char)*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 ((unsigned char)*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 ((unsigned char)*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 ((unsigned char)*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 ((unsigned char)*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 > line && *opc != ':')
1490 opc--;
1491
1492 if (strncmp(opc, ":sh", 3) == 0) {
1493 type = VAR_SHELL;
1494 *opc = '\0';
1495 break;
1496 }
1497 #endif
1498 type = VAR_NORMAL;
1499 break;
1500 }
1501
1502 while (isspace ((unsigned char)*cp)) {
1503 cp++;
1504 }
1505
1506 if (type == VAR_APPEND) {
1507 Var_Append (line, cp, ctxt);
1508 } else if (type == VAR_SUBST) {
1509 /*
1510 * Allow variables in the old value to be undefined, but leave their
1511 * invocation alone -- this is done by forcing oldVars to be false.
1512 * XXX: This can cause recursive variables, but that's not hard to do,
1513 * and this allows someone to do something like
1514 *
1515 * CFLAGS = $(.INCLUDES)
1516 * CFLAGS := -I.. $(CFLAGS)
1517 *
1518 * And not get an error.
1519 */
1520 Boolean oldOldVars = oldVars;
1521
1522 oldVars = FALSE;
1523
1524 /*
1525 * make sure that we set the variable the first time to nothing
1526 * so that it gets substituted!
1527 */
1528 if (!Var_Exists(line, ctxt))
1529 Var_Set(line, "", ctxt);
1530
1531 cp = Var_Subst(NULL, cp, ctxt, FALSE);
1532 oldVars = oldOldVars;
1533
1534 Var_Set(line, cp, ctxt);
1535 free(cp);
1536 } else if (type == VAR_SHELL) {
1537 Boolean freeCmd = FALSE; /* TRUE if the command needs to be freed, i.e.
1538 * if any variable expansion was performed */
1539 char *res, *err;
1540
1541 if (strchr(cp, '$') != NULL) {
1542 /*
1543 * There's a dollar sign in the command, so perform variable
1544 * expansion on the whole thing. The resulting string will need
1545 * freeing when we're done, so set freeCmd to TRUE.
1546 */
1547 cp = Var_Subst(NULL, cp, VAR_CMD, TRUE);
1548 freeCmd = TRUE;
1549 }
1550
1551 res = Cmd_Exec(cp, &err);
1552 Var_Set(line, res, ctxt);
1553 free(res);
1554
1555 if (err)
1556 Parse_Error(PARSE_WARNING, err, cp);
1557
1558 if (freeCmd)
1559 free(cp);
1560 } else {
1561 /*
1562 * Normal assignment -- just do it.
1563 */
1564 Var_Set(line, cp, ctxt);
1565 }
1566 }
1567
1568
1569 /*-
1570 * ParseAddCmd --
1571 * Lst_ForEach function to add a command line to all targets
1572 *
1573 * Results:
1574 * Always 0
1575 *
1576 * Side Effects:
1577 * A new element is added to the commands list of the node.
1578 */
1579 static int
1580 ParseAddCmd(gnp, cmd)
1581 ClientData gnp; /* the node to which the command is to be added */
1582 ClientData cmd; /* the command to add */
1583 {
1584 GNode *gn = (GNode *) gnp;
1585 /* if target already supplied, ignore commands */
1586 if (!(gn->type & OP_HAS_COMMANDS))
1587 (void)Lst_AtEnd(gn->commands, cmd);
1588 return(0);
1589 }
1590
1591 /*-
1592 *-----------------------------------------------------------------------
1593 * ParseHasCommands --
1594 * Callback procedure for Parse_File when destroying the list of
1595 * targets on the last dependency line. Marks a target as already
1596 * having commands if it does, to keep from having shell commands
1597 * on multiple dependency lines.
1598 *
1599 * Results:
1600 * None
1601 *
1602 * Side Effects:
1603 * OP_HAS_COMMANDS may be set for the target.
1604 *
1605 *-----------------------------------------------------------------------
1606 */
1607 static void
1608 ParseHasCommands(gnp)
1609 ClientData gnp; /* Node to examine */
1610 {
1611 GNode *gn = (GNode *) gnp;
1612 if (!Lst_IsEmpty(gn->commands)) {
1613 gn->type |= OP_HAS_COMMANDS;
1614 }
1615 }
1616
1617 /*-
1618 *-----------------------------------------------------------------------
1619 * Parse_AddIncludeDir --
1620 * Add a directory to the path searched for included makefiles
1621 * bracketed by double-quotes. Used by functions in main.c
1622 *
1623 * Results:
1624 * None.
1625 *
1626 * Side Effects:
1627 * The directory is appended to the list.
1628 *
1629 *-----------------------------------------------------------------------
1630 */
1631 void
1632 Parse_AddIncludeDir (dir)
1633 char *dir; /* The name of the directory to add */
1634 {
1635 (void) Dir_AddDir (parseIncPath, dir);
1636 }
1637
1638 /*-
1639 *---------------------------------------------------------------------
1640 * ParseDoInclude --
1641 * Push to another file.
1642 *
1643 * The input is the line minus the `.'. A file spec is a string
1644 * enclosed in <> or "". The former is looked for only in sysIncPath.
1645 * The latter in . and the directories specified by -I command line
1646 * options
1647 *
1648 * Results:
1649 * None
1650 *
1651 * Side Effects:
1652 * A structure is added to the includes Lst and readProc, lineno,
1653 * fname and curFILE are altered for the new file
1654 *---------------------------------------------------------------------
1655 */
1656 static void
1657 ParseDoInclude (line)
1658 char *line;
1659 {
1660 char *fullname; /* full pathname of file */
1661 IFile *oldFile; /* state associated with current file */
1662 char endc; /* the character which ends the file spec */
1663 char *cp; /* current position in file spec */
1664 Boolean isSystem; /* TRUE if makefile is a system makefile */
1665 int silent = (*line != 'i') ? 1 : 0;
1666 char *file = &line[7 + silent];
1667
1668 /*
1669 * Skip to delimiter character so we know where to look
1670 */
1671 while ((*file == ' ') || (*file == '\t')) {
1672 file++;
1673 }
1674
1675 if ((*file != '"') && (*file != '<')) {
1676 Parse_Error (PARSE_FATAL,
1677 ".include filename must be delimited by '\"' or '<'");
1678 return;
1679 }
1680
1681 /*
1682 * Set the search path on which to find the include file based on the
1683 * characters which bracket its name. Angle-brackets imply it's
1684 * a system Makefile while double-quotes imply it's a user makefile
1685 */
1686 if (*file == '<') {
1687 isSystem = TRUE;
1688 endc = '>';
1689 } else {
1690 isSystem = FALSE;
1691 endc = '"';
1692 }
1693
1694 /*
1695 * Skip to matching delimiter
1696 */
1697 for (cp = ++file; *cp && *cp != endc; cp++) {
1698 continue;
1699 }
1700
1701 if (*cp != endc) {
1702 Parse_Error (PARSE_FATAL,
1703 "Unclosed %cinclude filename. '%c' expected",
1704 '.', endc);
1705 return;
1706 }
1707 *cp = '\0';
1708
1709 /*
1710 * Substitute for any variables in the file name before trying to
1711 * find the thing.
1712 */
1713 file = Var_Subst (NULL, file, VAR_CMD, FALSE);
1714
1715 /*
1716 * Now we know the file's name and its search path, we attempt to
1717 * find the durn thing. A return of NULL indicates the file don't
1718 * exist.
1719 */
1720 if (!isSystem) {
1721 /*
1722 * Include files contained in double-quotes are first searched for
1723 * relative to the including file's location. We don't want to
1724 * cd there, of course, so we just tack on the old file's
1725 * leading path components and call Dir_FindFile to see if
1726 * we can locate the beast.
1727 */
1728 char *prefEnd, *Fname;
1729
1730 /* Make a temporary copy of this, to be safe. */
1731 Fname = estrdup(fname);
1732
1733 prefEnd = strrchr (Fname, '/');
1734 if (prefEnd != (char *)NULL) {
1735 char *newName;
1736
1737 *prefEnd = '\0';
1738 if (file[0] == '/')
1739 newName = estrdup(file);
1740 else
1741 newName = str_concat (Fname, file, STR_ADDSLASH);
1742 fullname = Dir_FindFile (newName, parseIncPath);
1743 if (fullname == (char *)NULL) {
1744 fullname = Dir_FindFile(newName, dirSearchPath);
1745 }
1746 free (newName);
1747 *prefEnd = '/';
1748 } else {
1749 fullname = (char *)NULL;
1750 }
1751 free (Fname);
1752 } else {
1753 fullname = (char *)NULL;
1754 }
1755
1756 if (fullname == (char *)NULL) {
1757 /*
1758 * System makefile or makefile wasn't found in same directory as
1759 * included makefile. Search for it first on the -I search path,
1760 * then on the .PATH search path, if not found in a -I directory.
1761 * XXX: Suffix specific?
1762 */
1763 fullname = Dir_FindFile (file, parseIncPath);
1764 if (fullname == (char *)NULL) {
1765 fullname = Dir_FindFile(file, dirSearchPath);
1766 }
1767 }
1768
1769 if (fullname == (char *)NULL) {
1770 /*
1771 * Still haven't found the makefile. Look for it on the system
1772 * path as a last resort.
1773 */
1774 fullname = Dir_FindFile(file, sysIncPath);
1775 }
1776
1777 if (fullname == (char *) NULL) {
1778 *cp = endc;
1779 if (!silent)
1780 Parse_Error (PARSE_FATAL, "Could not find %s", file);
1781 return;
1782 }
1783
1784 free(file);
1785
1786 /*
1787 * Once we find the absolute path to the file, we get to save all the
1788 * state from the current file before we can start reading this
1789 * include file. The state is stored in an IFile structure which
1790 * is placed on a list with other IFile structures. The list makes
1791 * a very nice stack to track how we got here...
1792 */
1793 oldFile = (IFile *) emalloc (sizeof (IFile));
1794 oldFile->fname = fname;
1795
1796 oldFile->F = curFILE;
1797 oldFile->p = curPTR;
1798 oldFile->lineno = lineno;
1799
1800 (void) Lst_AtFront (includes, (ClientData)oldFile);
1801
1802 /*
1803 * Once the previous state has been saved, we can get down to reading
1804 * the new file. We set up the name of the file to be the absolute
1805 * name of the include file so error messages refer to the right
1806 * place. Naturally enough, we start reading at line number 0.
1807 */
1808 fname = fullname;
1809 lineno = 0;
1810
1811 curFILE = fopen (fullname, "r");
1812 curPTR = NULL;
1813 if (curFILE == (FILE * ) NULL) {
1814 if (!silent)
1815 Parse_Error (PARSE_FATAL, "Cannot open %s", fullname);
1816 /*
1817 * Pop to previous file
1818 */
1819 (void) ParseEOF(0);
1820 }
1821 }
1822
1823
1824 /*-
1825 *---------------------------------------------------------------------
1826 * Parse_FromString --
1827 * Start Parsing from the given string
1828 *
1829 * Results:
1830 * None
1831 *
1832 * Side Effects:
1833 * A structure is added to the includes Lst and readProc, lineno,
1834 * fname and curFILE are altered for the new file
1835 *---------------------------------------------------------------------
1836 */
1837 void
1838 Parse_FromString(str)
1839 char *str;
1840 {
1841 IFile *oldFile; /* state associated with this file */
1842
1843 if (DEBUG(FOR))
1844 (void) fprintf(stderr, "%s\n----\n", str);
1845
1846 oldFile = (IFile *) emalloc (sizeof (IFile));
1847 oldFile->lineno = lineno;
1848 oldFile->fname = fname;
1849 oldFile->F = curFILE;
1850 oldFile->p = curPTR;
1851
1852 (void) Lst_AtFront (includes, (ClientData)oldFile);
1853
1854 curFILE = NULL;
1855 curPTR = (PTR *) emalloc (sizeof (PTR));
1856 curPTR->str = curPTR->ptr = str;
1857 lineno = 0;
1858 fname = estrdup(fname);
1859 }
1860
1861
1862 #ifdef SYSVINCLUDE
1863 /*-
1864 *---------------------------------------------------------------------
1865 * ParseTraditionalInclude --
1866 * Push to another file.
1867 *
1868 * The input is the current line. The file name(s) are
1869 * following the "include".
1870 *
1871 * Results:
1872 * None
1873 *
1874 * Side Effects:
1875 * A structure is added to the includes Lst and readProc, lineno,
1876 * fname and curFILE are altered for the new file
1877 *---------------------------------------------------------------------
1878 */
1879 static void
1880 ParseTraditionalInclude (line)
1881 char *line;
1882 {
1883 char *fullname; /* full pathname of file */
1884 IFile *oldFile; /* state associated with current file */
1885 char *cp; /* current position in file spec */
1886 char *prefEnd;
1887 int done = 0;
1888 int silent = (line[0] != 'i') ? 1 : 0;
1889 char *file = &line[silent + 7];
1890 char *cfname = fname;
1891 size_t clineno = lineno;
1892
1893
1894 /*
1895 * Skip over whitespace
1896 */
1897 while (isspace((unsigned char)*file))
1898 file++;
1899
1900 if (*file == '\0') {
1901 Parse_Error (PARSE_FATAL,
1902 "Filename missing from \"include\"");
1903 return;
1904 }
1905
1906 for (; !done; file = cp + 1) {
1907 /*
1908 * Skip to end of line or next whitespace
1909 */
1910 for (cp = file; *cp && !isspace((unsigned char) *cp); cp++)
1911 continue;
1912
1913 if (*cp)
1914 *cp = '\0';
1915 else
1916 done = 1;
1917
1918 /*
1919 * Substitute for any variables in the file name before trying to
1920 * find the thing.
1921 */
1922 file = Var_Subst(NULL, file, VAR_CMD, FALSE);
1923
1924 /*
1925 * Now we know the file's name, we attempt to find the durn thing.
1926 * A return of NULL indicates the file don't exist.
1927 *
1928 * Include files are first searched for relative to the including
1929 * file's location. We don't want to cd there, of course, so we
1930 * just tack on the old file's leading path components and call
1931 * Dir_FindFile to see if we can locate the beast.
1932 * XXX - this *does* search in the current directory, right?
1933 */
1934
1935 prefEnd = strrchr(cfname, '/');
1936 if (prefEnd != NULL) {
1937 char *newName;
1938
1939 *prefEnd = '\0';
1940 newName = str_concat(cfname, file, STR_ADDSLASH);
1941 fullname = Dir_FindFile(newName, parseIncPath);
1942 if (fullname == NULL) {
1943 fullname = Dir_FindFile(newName, dirSearchPath);
1944 }
1945 free (newName);
1946 *prefEnd = '/';
1947 } else {
1948 fullname = NULL;
1949 }
1950
1951 if (fullname == NULL) {
1952 /*
1953 * System makefile or makefile wasn't found in same directory as
1954 * included makefile. Search for it first on the -I search path,
1955 * then on the .PATH search path, if not found in a
1956 * -I directory. XXX: Suffix specific?
1957 */
1958 fullname = Dir_FindFile(file, parseIncPath);
1959 if (fullname == NULL) {
1960 fullname = Dir_FindFile(file, dirSearchPath);
1961 }
1962 }
1963
1964 if (fullname == NULL) {
1965 /*
1966 * Still haven't found the makefile. Look for it on the system
1967 * path as a last resort.
1968 */
1969 fullname = Dir_FindFile(file, sysIncPath);
1970 }
1971
1972 if (fullname == NULL) {
1973 if (!silent)
1974 ParseErrorInternal(cfname, clineno, PARSE_FATAL,
1975 "Could not find %s", file);
1976 free(file);
1977 continue;
1978 }
1979
1980 free(file);
1981
1982 /*
1983 * Once we find the absolute path to the file, we get to save all
1984 * the state from the current file before we can start reading this
1985 * include file. The state is stored in an IFile structure which
1986 * is placed on a list with other IFile structures. The list makes
1987 * a very nice stack to track how we got here...
1988 */
1989 oldFile = (IFile *) emalloc(sizeof(IFile));
1990 oldFile->fname = fname;
1991
1992 oldFile->F = curFILE;
1993 oldFile->p = curPTR;
1994 oldFile->lineno = lineno;
1995
1996 (void) Lst_AtFront(includes, (ClientData)oldFile);
1997
1998 /*
1999 * Once the previous state has been saved, we can get down to
2000 * reading the new file. We set up the name of the file to be the
2001 * absolute name of the include file so error messages refer to the
2002 * right place. Naturally enough, we start reading at line number 0.
2003 */
2004 fname = fullname;
2005 lineno = 0;
2006
2007 curFILE = fopen(fullname, "r");
2008 curPTR = NULL;
2009 if (curFILE == NULL) {
2010 if (!silent)
2011 ParseErrorInternal(cfname, clineno, PARSE_FATAL,
2012 "Cannot open %s", fullname);
2013 /*
2014 * Pop to previous file
2015 */
2016 (void) ParseEOF(1);
2017 }
2018 }
2019 }
2020 #endif
2021
2022 /*-
2023 *---------------------------------------------------------------------
2024 * ParseEOF --
2025 * Called when EOF is reached in the current file. If we were reading
2026 * an include file, the includes stack is popped and things set up
2027 * to go back to reading the previous file at the previous location.
2028 *
2029 * Results:
2030 * CONTINUE if there's more to do. DONE if not.
2031 *
2032 * Side Effects:
2033 * The old curFILE, is closed. The includes list is shortened.
2034 * lineno, curFILE, and fname are changed if CONTINUE is returned.
2035 *---------------------------------------------------------------------
2036 */
2037 static int
2038 ParseEOF (opened)
2039 int opened;
2040 {
2041 IFile *ifile; /* the state on the top of the includes stack */
2042
2043 if (Lst_IsEmpty (includes)) {
2044 return (DONE);
2045 }
2046
2047 ifile = (IFile *) Lst_DeQueue (includes);
2048 free ((Address) fname);
2049 fname = ifile->fname;
2050 lineno = ifile->lineno;
2051 if (opened && curFILE)
2052 (void) fclose (curFILE);
2053 if (curPTR) {
2054 free((Address) curPTR->str);
2055 free((Address) curPTR);
2056 }
2057 curFILE = ifile->F;
2058 curPTR = ifile->p;
2059 free ((Address)ifile);
2060 return (CONTINUE);
2061 }
2062
2063 /*-
2064 *---------------------------------------------------------------------
2065 * ParseReadc --
2066 * Read a character from the current file
2067 *
2068 * Results:
2069 * The character that was read
2070 *
2071 * Side Effects:
2072 *---------------------------------------------------------------------
2073 */
2074 static int
2075 ParseReadc()
2076 {
2077 if (curFILE)
2078 return fgetc(curFILE);
2079
2080 if (curPTR && *curPTR->ptr)
2081 return *curPTR->ptr++;
2082 return EOF;
2083 }
2084
2085
2086 /*-
2087 *---------------------------------------------------------------------
2088 * ParseUnreadc --
2089 * Put back a character to the current file
2090 *
2091 * Results:
2092 * None.
2093 *
2094 * Side Effects:
2095 *---------------------------------------------------------------------
2096 */
2097 static void
2098 ParseUnreadc(c)
2099 int c;
2100 {
2101 if (curFILE) {
2102 ungetc(c, curFILE);
2103 return;
2104 }
2105 if (curPTR) {
2106 *--(curPTR->ptr) = c;
2107 return;
2108 }
2109 }
2110
2111
2112 /* ParseSkipLine():
2113 * Grab the next line
2114 */
2115 static char *
2116 ParseSkipLine(skip)
2117 int skip; /* Skip lines that don't start with . */
2118 {
2119 char *line;
2120 int c, lastc, lineLength = 0;
2121 Buffer buf;
2122
2123 buf = Buf_Init(MAKE_BSIZE);
2124
2125 do {
2126 Buf_Discard(buf, lineLength);
2127 lastc = '\0';
2128
2129 while (((c = ParseReadc()) != '\n' || lastc == '\\')
2130 && c != EOF) {
2131 if (c == '\n') {
2132 Buf_ReplaceLastByte(buf, (Byte)' ');
2133 lineno++;
2134
2135 while ((c = ParseReadc()) == ' ' || c == '\t');
2136
2137 if (c == EOF)
2138 break;
2139 }
2140
2141 Buf_AddByte(buf, (Byte)c);
2142 lastc = c;
2143 }
2144
2145 if (c == EOF) {
2146 Parse_Error(PARSE_FATAL, "Unclosed conditional/for loop");
2147 Buf_Destroy(buf, TRUE);
2148 return((char *)NULL);
2149 }
2150
2151 lineno++;
2152 Buf_AddByte(buf, (Byte)'\0');
2153 line = (char *)Buf_GetAll(buf, &lineLength);
2154 } while (skip == 1 && line[0] != '.');
2155
2156 Buf_Destroy(buf, FALSE);
2157 return line;
2158 }
2159
2160
2161 /*-
2162 *---------------------------------------------------------------------
2163 * ParseReadLine --
2164 * Read an entire line from the input file. Called only by Parse_File.
2165 * To facilitate escaped newlines and what have you, a character is
2166 * buffered in 'lastc', which is '\0' when no characters have been
2167 * read. When we break out of the loop, c holds the terminating
2168 * character and lastc holds a character that should be added to
2169 * the line (unless we don't read anything but a terminator).
2170 *
2171 * Results:
2172 * A line w/o its newline
2173 *
2174 * Side Effects:
2175 * Only those associated with reading a character
2176 *---------------------------------------------------------------------
2177 */
2178 static char *
2179 ParseReadLine ()
2180 {
2181 Buffer buf; /* Buffer for current line */
2182 register int c; /* the current character */
2183 register int lastc; /* The most-recent character */
2184 Boolean semiNL; /* treat semi-colons as newlines */
2185 Boolean ignDepOp; /* TRUE if should ignore dependency operators
2186 * for the purposes of setting semiNL */
2187 Boolean ignComment; /* TRUE if should ignore comments (in a
2188 * shell command */
2189 char *line; /* Result */
2190 char *ep; /* to strip trailing blanks */
2191 int lineLength; /* Length of result */
2192
2193 semiNL = FALSE;
2194 ignDepOp = FALSE;
2195 ignComment = FALSE;
2196
2197 /*
2198 * Handle special-characters at the beginning of the line. Either a
2199 * leading tab (shell command) or pound-sign (possible conditional)
2200 * forces us to ignore comments and dependency operators and treat
2201 * semi-colons as semi-colons (by leaving semiNL FALSE). This also
2202 * discards completely blank lines.
2203 */
2204 for (;;) {
2205 c = ParseReadc();
2206
2207 if (c == '\t') {
2208 ignComment = ignDepOp = TRUE;
2209 break;
2210 } else if (c == '\n') {
2211 lineno++;
2212 } else if (c == '#') {
2213 ParseUnreadc(c);
2214 break;
2215 } else {
2216 /*
2217 * Anything else breaks out without doing anything
2218 */
2219 break;
2220 }
2221 }
2222
2223 if (c != EOF) {
2224 lastc = c;
2225 buf = Buf_Init(MAKE_BSIZE);
2226
2227 while (((c = ParseReadc ()) != '\n' || (lastc == '\\')) &&
2228 (c != EOF))
2229 {
2230 test_char:
2231 switch(c) {
2232 case '\n':
2233 /*
2234 * Escaped newline: read characters until a non-space or an
2235 * unescaped newline and replace them all by a single space.
2236 * This is done by storing the space over the backslash and
2237 * dropping through with the next nonspace. If it is a
2238 * semi-colon and semiNL is TRUE, it will be recognized as a
2239 * newline in the code below this...
2240 */
2241 lineno++;
2242 lastc = ' ';
2243 while ((c = ParseReadc ()) == ' ' || c == '\t') {
2244 continue;
2245 }
2246 if (c == EOF || c == '\n') {
2247 goto line_read;
2248 } else {
2249 /*
2250 * Check for comments, semiNL's, etc. -- easier than
2251 * ParseUnreadc(c); continue;
2252 */
2253 goto test_char;
2254 }
2255 /*NOTREACHED*/
2256 break;
2257
2258 case ';':
2259 /*
2260 * Semi-colon: Need to see if it should be interpreted as a
2261 * newline
2262 */
2263 if (semiNL) {
2264 /*
2265 * To make sure the command that may be following this
2266 * semi-colon begins with a tab, we push one back into the
2267 * input stream. This will overwrite the semi-colon in the
2268 * buffer. If there is no command following, this does no
2269 * harm, since the newline remains in the buffer and the
2270 * whole line is ignored.
2271 */
2272 ParseUnreadc('\t');
2273 goto line_read;
2274 }
2275 break;
2276 case '=':
2277 if (!semiNL) {
2278 /*
2279 * Haven't seen a dependency operator before this, so this
2280 * must be a variable assignment -- don't pay attention to
2281 * dependency operators after this.
2282 */
2283 ignDepOp = TRUE;
2284 } else if (lastc == ':' || lastc == '!') {
2285 /*
2286 * Well, we've seen a dependency operator already, but it
2287 * was the previous character, so this is really just an
2288 * expanded variable assignment. Revert semi-colons to
2289 * being just semi-colons again and ignore any more
2290 * dependency operators.
2291 *
2292 * XXX: Note that a line like "foo : a:=b" will blow up,
2293 * but who'd write a line like that anyway?
2294 */
2295 ignDepOp = TRUE; semiNL = FALSE;
2296 }
2297 break;
2298 case '#':
2299 if (!ignComment) {
2300 if (
2301 #if 0
2302 compatMake &&
2303 #endif
2304 (lastc != '\\')) {
2305 /*
2306 * If the character is a hash mark and it isn't escaped
2307 * (or we're being compatible), the thing is a comment.
2308 * Skip to the end of the line.
2309 */
2310 do {
2311 c = ParseReadc();
2312 } while ((c != '\n') && (c != EOF));
2313 goto line_read;
2314 } else {
2315 /*
2316 * Don't add the backslash. Just let the # get copied
2317 * over.
2318 */
2319 lastc = c;
2320 continue;
2321 }
2322 }
2323 break;
2324 case ':':
2325 case '!':
2326 if (!ignDepOp && (c == ':' || c == '!')) {
2327 /*
2328 * A semi-colon is recognized as a newline only on
2329 * dependency lines. Dependency lines are lines with a
2330 * colon or an exclamation point. Ergo...
2331 */
2332 semiNL = TRUE;
2333 }
2334 break;
2335 }
2336 /*
2337 * Copy in the previous character and save this one in lastc.
2338 */
2339 Buf_AddByte (buf, (Byte)lastc);
2340 lastc = c;
2341
2342 }
2343 line_read:
2344 lineno++;
2345
2346 if (lastc != '\0') {
2347 Buf_AddByte (buf, (Byte)lastc);
2348 }
2349 Buf_AddByte (buf, (Byte)'\0');
2350 line = (char *)Buf_GetAll (buf, &lineLength);
2351 Buf_Destroy (buf, FALSE);
2352
2353 /*
2354 * Strip trailing blanks and tabs from the line.
2355 * Do not strip a blank or tab that is preceeded by
2356 * a '\'
2357 */
2358 ep = line;
2359 while (*ep)
2360 ++ep;
2361 while (ep > line + 1 && (ep[-1] == ' ' || ep[-1] == '\t')) {
2362 if (ep > line + 1 && ep[-2] == '\\')
2363 break;
2364 --ep;
2365 }
2366 *ep = 0;
2367
2368 if (line[0] == '.') {
2369 /*
2370 * The line might be a conditional. Ask the conditional module
2371 * about it and act accordingly
2372 */
2373 switch (Cond_Eval (line)) {
2374 case COND_SKIP:
2375 /*
2376 * Skip to next conditional that evaluates to COND_PARSE.
2377 */
2378 do {
2379 free (line);
2380 line = ParseSkipLine(1);
2381 } while (line && Cond_Eval(line) != COND_PARSE);
2382 if (line == NULL)
2383 break;
2384 /*FALLTHRU*/
2385 case COND_PARSE:
2386 free ((Address) line);
2387 line = ParseReadLine();
2388 break;
2389 case COND_INVALID:
2390 if (For_Eval(line)) {
2391 int ok;
2392 free(line);
2393 do {
2394 /*
2395 * Skip after the matching end
2396 */
2397 line = ParseSkipLine(0);
2398 if (line == NULL) {
2399 Parse_Error (PARSE_FATAL,
2400 "Unexpected end of file in for loop.\n");
2401 break;
2402 }
2403 ok = For_Eval(line);
2404 free(line);
2405 }
2406 while (ok);
2407 if (line != NULL)
2408 For_Run();
2409 line = ParseReadLine();
2410 }
2411 break;
2412 }
2413 }
2414 return (line);
2415
2416 } else {
2417 /*
2418 * Hit end-of-file, so return a NULL line to indicate this.
2419 */
2420 return((char *)NULL);
2421 }
2422 }
2423
2424 /*-
2425 *-----------------------------------------------------------------------
2426 * ParseFinishLine --
2427 * Handle the end of a dependency group.
2428 *
2429 * Results:
2430 * Nothing.
2431 *
2432 * Side Effects:
2433 * inLine set FALSE. 'targets' list destroyed.
2434 *
2435 *-----------------------------------------------------------------------
2436 */
2437 static void
2438 ParseFinishLine()
2439 {
2440 if (inLine) {
2441 Lst_ForEach(targets, Suff_EndTransform, (ClientData)NULL);
2442 Lst_Destroy (targets, ParseHasCommands);
2443 targets = NULL;
2444 inLine = FALSE;
2445 }
2446 }
2447
2448
2449 /*-
2450 *---------------------------------------------------------------------
2451 * Parse_File --
2452 * Parse a file into its component parts, incorporating it into the
2453 * current dependency graph. This is the main function and controls
2454 * almost every other function in this module
2455 *
2456 * Results:
2457 * None
2458 *
2459 * Side Effects:
2460 * Loads. Nodes are added to the list of all targets, nodes and links
2461 * are added to the dependency graph. etc. etc. etc.
2462 *---------------------------------------------------------------------
2463 */
2464 void
2465 Parse_File(name, stream)
2466 char *name; /* the name of the file being read */
2467 FILE * stream; /* Stream open to makefile to parse */
2468 {
2469 register char *cp, /* pointer into the line */
2470 *line; /* the line we're working on */
2471
2472 inLine = FALSE;
2473 fname = name;
2474 curFILE = stream;
2475 lineno = 0;
2476 fatals = 0;
2477
2478 do {
2479 while ((line = ParseReadLine ()) != NULL) {
2480 if (*line == '.') {
2481 /*
2482 * Lines that begin with the special character are either
2483 * include or undef directives.
2484 */
2485 for (cp = line + 1; isspace ((unsigned char)*cp); cp++) {
2486 continue;
2487 }
2488 if (strncmp(cp, "include", 7) == 0 ||
2489 ((cp[0] == 's' || cp[0] == '-') &&
2490 strncmp(&cp[1], "include", 7) == 0)) {
2491 ParseDoInclude (cp);
2492 goto nextLine;
2493 } else if (strncmp(cp, "undef", 5) == 0) {
2494 char *cp2;
2495 for (cp += 5; isspace((unsigned char) *cp); cp++) {
2496 continue;
2497 }
2498
2499 for (cp2 = cp; !isspace((unsigned char) *cp2) &&
2500 (*cp2 != '\0'); cp2++) {
2501 continue;
2502 }
2503
2504 *cp2 = '\0';
2505
2506 Var_Delete(cp, VAR_GLOBAL);
2507 goto nextLine;
2508 }
2509 }
2510 if (*line == '#') {
2511 /* If we're this far, the line must be a comment. */
2512 goto nextLine;
2513 }
2514
2515 if (*line == '\t') {
2516 /*
2517 * If a line starts with a tab, it can only hope to be
2518 * a creation command.
2519 */
2520 #ifndef POSIX
2521 shellCommand:
2522 #endif
2523 for (cp = line + 1; isspace ((unsigned char)*cp); cp++) {
2524 continue;
2525 }
2526 if (*cp) {
2527 if (inLine) {
2528 /*
2529 * So long as it's not a blank line and we're actually
2530 * in a dependency spec, add the command to the list of
2531 * commands of all targets in the dependency spec
2532 */
2533 Lst_ForEach (targets, ParseAddCmd, cp);
2534 Lst_AtEnd(targCmds, (ClientData) line);
2535 continue;
2536 } else {
2537 Parse_Error (PARSE_FATAL,
2538 "Unassociated shell command \"%s\"",
2539 cp);
2540 }
2541 }
2542 #ifdef SYSVINCLUDE
2543 } else if (((strncmp(line, "include", 7) == 0 &&
2544 isspace((unsigned char) line[7])) ||
2545 ((line[0] == 's' || line[0] == '-') &&
2546 strncmp(&line[1], "include", 7) == 0 &&
2547 isspace((unsigned char) line[8]))) &&
2548 strchr(line, ':') == NULL) {
2549 /*
2550 * It's an S3/S5-style "include".
2551 */
2552 ParseTraditionalInclude (line);
2553 goto nextLine;
2554 #endif
2555 } else if (Parse_IsVar (line)) {
2556 ParseFinishLine();
2557 Parse_DoVar (line, VAR_GLOBAL);
2558 } else {
2559 /*
2560 * We now know it's a dependency line so it needs to have all
2561 * variables expanded before being parsed. Tell the variable
2562 * module to complain if some variable is undefined...
2563 * To make life easier on novices, if the line is indented we
2564 * first make sure the line has a dependency operator in it.
2565 * If it doesn't have an operator and we're in a dependency
2566 * line's script, we assume it's actually a shell command
2567 * and add it to the current list of targets.
2568 */
2569 #ifndef POSIX
2570 Boolean nonSpace = FALSE;
2571 #endif
2572
2573 cp = line;
2574 if (isspace((unsigned char) line[0])) {
2575 while ((*cp != '\0') && isspace((unsigned char) *cp)) {
2576 cp++;
2577 }
2578 if (*cp == '\0') {
2579 goto nextLine;
2580 }
2581 #ifndef POSIX
2582 while ((*cp != ':') && (*cp != '!') && (*cp != '\0')) {
2583 nonSpace = TRUE;
2584 cp++;
2585 }
2586 #endif
2587 }
2588
2589 #ifndef POSIX
2590 if (*cp == '\0') {
2591 if (inLine) {
2592 Parse_Error (PARSE_WARNING,
2593 "Shell command needs a leading tab");
2594 goto shellCommand;
2595 } else if (nonSpace) {
2596 Parse_Error (PARSE_FATAL, "Missing operator");
2597 }
2598 } else {
2599 #endif
2600 ParseFinishLine();
2601
2602 cp = Var_Subst (NULL, line, VAR_CMD, TRUE);
2603 free (line);
2604 line = cp;
2605
2606 /*
2607 * Need a non-circular list for the target nodes
2608 */
2609 if (targets)
2610 Lst_Destroy(targets, NOFREE);
2611
2612 targets = Lst_Init (FALSE);
2613 inLine = TRUE;
2614
2615 ParseDoDependency (line);
2616 #ifndef POSIX
2617 }
2618 #endif
2619 }
2620
2621 nextLine:
2622
2623 free (line);
2624 }
2625 /*
2626 * Reached EOF, but it may be just EOF of an include file...
2627 */
2628 } while (ParseEOF(1) == CONTINUE);
2629
2630 /*
2631 * Make sure conditionals are clean
2632 */
2633 Cond_End();
2634
2635 if (fatals) {
2636 fprintf (stderr, "Fatal errors encountered -- cannot continue\n");
2637 exit (1);
2638 }
2639 }
2640
2641 /*-
2642 *---------------------------------------------------------------------
2643 * Parse_Init --
2644 * initialize the parsing module
2645 *
2646 * Results:
2647 * none
2648 *
2649 * Side Effects:
2650 * the parseIncPath list is initialized...
2651 *---------------------------------------------------------------------
2652 */
2653 void
2654 Parse_Init ()
2655 {
2656 mainNode = NILGNODE;
2657 parseIncPath = Lst_Init (FALSE);
2658 sysIncPath = Lst_Init (FALSE);
2659 includes = Lst_Init (FALSE);
2660 targCmds = Lst_Init (FALSE);
2661 }
2662
2663 void
2664 Parse_End()
2665 {
2666 Lst_Destroy(targCmds, (void (*) __P((ClientData))) free);
2667 if (targets)
2668 Lst_Destroy(targets, NOFREE);
2669 Lst_Destroy(sysIncPath, Dir_Destroy);
2670 Lst_Destroy(parseIncPath, Dir_Destroy);
2671 Lst_Destroy(includes, NOFREE); /* Should be empty now */
2672 }
2673
2674
2675 /*-
2676 *-----------------------------------------------------------------------
2677 * Parse_MainName --
2678 * Return a Lst of the main target to create for main()'s sake. If
2679 * no such target exists, we Punt with an obnoxious error message.
2680 *
2681 * Results:
2682 * A Lst of the single node to create.
2683 *
2684 * Side Effects:
2685 * None.
2686 *
2687 *-----------------------------------------------------------------------
2688 */
2689 Lst
2690 Parse_MainName()
2691 {
2692 Lst mainList; /* result list */
2693
2694 mainList = Lst_Init (FALSE);
2695
2696 if (mainNode == NILGNODE) {
2697 Punt ("no target to make.");
2698 /*NOTREACHED*/
2699 } else if (mainNode->type & OP_DOUBLEDEP) {
2700 (void) Lst_AtEnd (mainList, (ClientData)mainNode);
2701 Lst_Concat(mainList, mainNode->cohorts, LST_CONCNEW);
2702 }
2703 else
2704 (void) Lst_AtEnd (mainList, (ClientData)mainNode);
2705 return (mainList);
2706 }
2707