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