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