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

Last change on this file since 41 was 40, checked in by bird, 22 years ago

Inline files and such.

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