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