source: trunk/src/kmk/parse.c@ 39

Last change on this file since 39 was 36, checked in by bird, 22 years ago

emx is kind of working again...

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