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

Last change on this file since 49 was 46, checked in by bird, 22 years ago

kMk changes. Made extensions configurable from config.h. fixed parents.

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