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