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