source: trunk/src/kmk/read.c@ 1168

Last change on this file since 1168 was 1168, checked in by bird, 18 years ago

Wrote the eval_include_dep parser.

  • Property svn:eol-style set to native
File size: 103.1 KB
Line 
1/* Reading and parsing of makefiles for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18
19#include "make.h"
20
21#include <assert.h>
22
23#include <glob.h>
24
25#include "dep.h"
26#include "filedef.h"
27#include "job.h"
28#include "commands.h"
29#include "variable.h"
30#include "rule.h"
31#include "debug.h"
32#include "hash.h"
33#ifdef KMK
34# include "kbuild.h"
35#endif
36
37
38#ifndef WINDOWS32
39#ifndef _AMIGA
40#ifndef VMS
41#include <pwd.h>
42#else
43struct passwd *getpwnam (char *name);
44#endif
45#endif
46#endif /* !WINDOWS32 */
47
48/* A 'struct ebuffer' controls the origin of the makefile we are currently
49 eval'ing.
50*/
51
52struct ebuffer
53 {
54 char *buffer; /* Start of the current line in the buffer. */
55 char *bufnext; /* Start of the next line in the buffer. */
56 char *bufstart; /* Start of the entire buffer. */
57 unsigned int size; /* Malloc'd size of buffer. */
58 FILE *fp; /* File, or NULL if this is an internal buffer. */
59 struct floc floc; /* Info on the file in fp (if any). */
60 };
61
62/* Types of "words" that can be read in a makefile. */
63enum make_word_type
64 {
65 w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
66 w_varassign
67 };
68
69
70/* A `struct conditionals' contains the information describing
71 all the active conditionals in a makefile.
72
73 The global variable `conditionals' contains the conditionals
74 information for the current makefile. It is initialized from
75 the static structure `toplevel_conditionals' and is later changed
76 to new structures for included makefiles. */
77
78struct conditionals
79 {
80 unsigned int if_cmds; /* Depth of conditional nesting. */
81 unsigned int allocated; /* Elts allocated in following arrays. */
82 char *ignoring; /* Are we ignoring or interpreting?
83 0=interpreting, 1=not yet interpreted,
84 2=already interpreted */
85 char *seen_else; /* Have we already seen an `else'? */
86 };
87
88static struct conditionals toplevel_conditionals;
89static struct conditionals *conditionals = &toplevel_conditionals;
90
91
92/* Default directories to search for include files in */
93
94static const char *default_include_directories[] =
95 {
96#ifndef KMK
97#if defined(WINDOWS32) && !defined(INCLUDEDIR)
98/* This completely up to the user when they install MSVC or other packages.
99 This is defined as a placeholder. */
100# define INCLUDEDIR "."
101#endif
102# ifdef INCLUDEDIR /* bird */
103 INCLUDEDIR,
104# else /* bird */
105 ".", /* bird */
106# endif /* bird */
107#ifndef _AMIGA
108 "/usr/gnu/include",
109 "/usr/local/include",
110 "/usr/include",
111#endif
112#endif /* !KMK */
113 0
114 };
115
116/* List of directories to search for include files in */
117
118static const char **include_directories;
119
120/* Maximum length of an element of the above. */
121
122static unsigned int max_incl_len;
123
124/* The filename and pointer to line number of the
125 makefile currently being read in. */
126
127const struct floc *reading_file = 0;
128
129/* The chain of makefiles read by read_makefile. */
130
131static struct dep *read_makefiles = 0;
132
133static int eval_makefile (const char *filename, int flags);
134static int eval (struct ebuffer *buffer, int flags);
135
136static long readline (struct ebuffer *ebuf);
137static void do_define (char *name, unsigned int namelen,
138 enum variable_origin origin, struct ebuffer *ebuf);
139static int conditional_line (char *line, int len, const struct floc *flocp);
140static void record_files (struct nameseq *filenames, const char *pattern,
141 const char *pattern_percent, struct dep *deps,
142 unsigned int cmds_started, char *commands,
143 unsigned int commands_idx, int two_colon,
144 const struct floc *flocp);
145static void record_target_var (struct nameseq *filenames, char *defn,
146 enum variable_origin origin, int enabled,
147 const struct floc *flocp);
148static enum make_word_type get_next_mword (char *buffer, char *delim,
149 char **startp, unsigned int *length);
150static void remove_comments (char *line);
151static char *find_char_unquote (char *string, int stop1, int stop2,
152 int blank, int ignorevars);
153
154
155/* Read in all the makefiles and return the chain of their names. */
156
157struct dep *
158read_all_makefiles (const char **makefiles)
159{
160 unsigned int num_makefiles = 0;
161
162 /* Create *_LIST variables, to hold the makefiles, targets, and variables
163 we will be reading. */
164
165 define_variable ("MAKEFILE_LIST", sizeof ("MAKEFILE_LIST")-1, "", o_file, 0);
166
167 DB (DB_BASIC, (_("Reading makefiles...\n")));
168
169 /* If there's a non-null variable MAKEFILES, its value is a list of
170 files to read first thing. But don't let it prevent reading the
171 default makefiles and don't let the default goal come from there. */
172
173 {
174 char *value;
175 char *name, *p;
176 unsigned int length;
177
178 {
179 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
180 int save = warn_undefined_variables_flag;
181 warn_undefined_variables_flag = 0;
182
183 value = allocated_variable_expand ("$(MAKEFILES)");
184
185 warn_undefined_variables_flag = save;
186 }
187
188 /* Set NAME to the start of next token and LENGTH to its length.
189 MAKEFILES is updated for finding remaining tokens. */
190 p = value;
191
192 while ((name = find_next_token ((const char **)&p, &length)) != 0)
193 {
194 if (*p != '\0')
195 *p++ = '\0';
196 eval_makefile (name, RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE);
197 }
198
199 free (value);
200 }
201
202 /* Read makefiles specified with -f switches. */
203
204 if (makefiles != 0)
205 while (*makefiles != 0)
206 {
207 struct dep *tail = read_makefiles;
208 register struct dep *d;
209
210 if (! eval_makefile (*makefiles, 0))
211 perror_with_name ("", *makefiles);
212
213 /* Find the right element of read_makefiles. */
214 d = read_makefiles;
215 while (d->next != tail)
216 d = d->next;
217
218 /* Use the storage read_makefile allocates. */
219 *makefiles = dep_name (d);
220 ++num_makefiles;
221 ++makefiles;
222 }
223
224 /* If there were no -f switches, try the default names. */
225
226 if (num_makefiles == 0)
227 {
228 static char *default_makefiles[] =
229#ifdef VMS
230 /* all lower case since readdir() (the vms version) 'lowercasifies' */
231# ifdef KMK
232 { "makefile.kmk", "makefile.vms", "gnumakefile.", "makefile.", 0 };
233# else
234 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
235# endif
236#else
237#ifdef _AMIGA
238 /* what's the deal here? no dots? */
239# ifdef KMK
240 { "Makefile.kmk", "makefile.kmk", "GNUmakefile", "Makefile", "SMakefile", 0 };
241# else
242 { "GNUmakefile", "Makefile", "SMakefile", 0 };
243# endif
244#else /* !Amiga && !VMS */
245# ifdef KMK
246 { "Makefile.kmk", "makefile.kmk", "GNUmakefile", "makefile", "Makefile", 0 };
247# else
248 { "GNUmakefile", "makefile", "Makefile", 0 };
249# endif
250#endif /* AMIGA */
251#endif /* VMS */
252 register char **p = default_makefiles;
253 while (*p != 0 && !file_exists_p (*p))
254 ++p;
255
256 if (*p != 0)
257 {
258 if (! eval_makefile (*p, 0))
259 perror_with_name ("", *p);
260 }
261 else
262 {
263 /* No default makefile was found. Add the default makefiles to the
264 `read_makefiles' chain so they will be updated if possible. */
265 struct dep *tail = read_makefiles;
266 /* Add them to the tail, after any MAKEFILES variable makefiles. */
267 while (tail != 0 && tail->next != 0)
268 tail = tail->next;
269 for (p = default_makefiles; *p != 0; ++p)
270 {
271 struct dep *d = alloc_dep ();
272 d->file = enter_file (strcache_add (*p));
273 d->file->dontcare = 1;
274 /* Tell update_goal_chain to bail out as soon as this file is
275 made, and main not to die if we can't make this file. */
276 d->changed = RM_DONTCARE;
277 if (tail == 0)
278 read_makefiles = d;
279 else
280 tail->next = d;
281 tail = d;
282 }
283 if (tail != 0)
284 tail->next = 0;
285 }
286 }
287
288 return read_makefiles;
289}
290
291
292/* Install a new conditional and return the previous one. */
293
294static struct conditionals *
295install_conditionals (struct conditionals *new)
296{
297 struct conditionals *save = conditionals;
298
299 memset (new, '\0', sizeof (*new));
300 conditionals = new;
301
302 return save;
303}
304
305/* Free the current conditionals and reinstate a saved one. */
306
307static void
308restore_conditionals (struct conditionals *saved)
309{
310 /* Free any space allocated by conditional_line. */
311 if (conditionals->ignoring)
312 free (conditionals->ignoring);
313 if (conditionals->seen_else)
314 free (conditionals->seen_else);
315
316 /* Restore state. */
317 conditionals = saved;
318}
319
320
321static int
322eval_makefile (const char *filename, int flags)
323{
324 struct dep *deps;
325 struct ebuffer ebuf;
326 const struct floc *curfile;
327 char *expanded = 0;
328 int makefile_errno;
329 int r;
330
331 filename = strcache_add (filename);
332 ebuf.floc.filenm = filename;
333 ebuf.floc.lineno = 1;
334
335 if (ISDB (DB_VERBOSE))
336 {
337 printf (_("Reading makefile `%s'"), filename);
338 if (flags & RM_NO_DEFAULT_GOAL)
339 printf (_(" (no default goal)"));
340 if (flags & RM_INCLUDED)
341 printf (_(" (search path)"));
342 if (flags & RM_DONTCARE)
343 printf (_(" (don't care)"));
344 if (flags & RM_NO_TILDE)
345 printf (_(" (no ~ expansion)"));
346 puts ("...");
347 }
348
349 /* First, get a stream to read. */
350
351 /* Expand ~ in FILENAME unless it came from `include',
352 in which case it was already done. */
353 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
354 {
355 expanded = tilde_expand (filename);
356 if (expanded != 0)
357 filename = expanded;
358 }
359
360 ebuf.fp = fopen (filename, "r");
361 /* Save the error code so we print the right message later. */
362 makefile_errno = errno;
363
364 /* If the makefile wasn't found and it's either a makefile from
365 the `MAKEFILES' variable or an included makefile,
366 search the included makefile search path for this makefile. */
367 if (ebuf.fp == 0 && (flags & RM_INCLUDED) && *filename != '/')
368 {
369 unsigned int i;
370 for (i = 0; include_directories[i] != 0; ++i)
371 {
372 const char *included = concat (include_directories[i], "/", filename);
373 ebuf.fp = fopen (included, "r");
374 if (ebuf.fp)
375 {
376 filename = strcache_add (included);
377 break;
378 }
379 }
380 }
381
382 /* Add FILENAME to the chain of read makefiles. */
383 deps = alloc_dep ();
384 deps->next = read_makefiles;
385 read_makefiles = deps;
386 deps->file = lookup_file (filename);
387 if (deps->file == 0)
388 deps->file = enter_file (filename);
389 filename = deps->file->name;
390 deps->changed = flags;
391 if (flags & RM_DONTCARE)
392 deps->file->dontcare = 1;
393
394 if (expanded)
395 free (expanded);
396
397 /* If the makefile can't be found at all, give up entirely. */
398
399 if (ebuf.fp == 0)
400 {
401 /* If we did some searching, errno has the error from the last
402 attempt, rather from FILENAME itself. Restore it in case the
403 caller wants to use it in a message. */
404 errno = makefile_errno;
405 return 0;
406 }
407
408 /* Add this makefile to the list. */
409 do_variable_definition (&ebuf.floc, "MAKEFILE_LIST", filename, o_file,
410 f_append, 0);
411
412 /* Evaluate the makefile */
413
414 ebuf.size = 200;
415 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = xmalloc (ebuf.size);
416
417 curfile = reading_file;
418 reading_file = &ebuf.floc;
419
420 r = eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
421
422 reading_file = curfile;
423
424 fclose (ebuf.fp);
425
426 free (ebuf.bufstart);
427 alloca (0);
428 return r;
429}
430
431int
432eval_buffer (char *buffer)
433{
434 struct ebuffer ebuf;
435 struct conditionals *saved;
436 struct conditionals new;
437 const struct floc *curfile;
438 int r;
439
440 /* Evaluate the buffer */
441
442 ebuf.size = strlen (buffer);
443 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = buffer;
444 ebuf.fp = NULL;
445
446 ebuf.floc = *reading_file;
447
448 curfile = reading_file;
449 reading_file = &ebuf.floc;
450
451 saved = install_conditionals (&new);
452
453 r = eval (&ebuf, 1);
454
455 restore_conditionals (saved);
456
457 reading_file = curfile;
458
459 alloca (0);
460 return r;
461}
462
463#ifdef CONFIG_WITH_INCLUDEDEP
464/* no nonsense dependency file including.
465
466 Because nobody wants bogus dependency files to break their incremental
467 builds with hard to comprehend error messages, this function does not
468 use the normal eval routine but does all the parsing itself. This isn't,
469 as much work as it sounds, because the necessary feature set is very
470 limited. */
471void
472eval_include_dep (const char *name, struct floc *f)
473{
474 FILE *fp;
475 char line_buf[8192];
476 unsigned line_no = 0;
477
478 /* open it, ignore non-existing dependency files and don't
479 fail fataly if we cannot open it. */
480 if (!file_exists_p (name))
481 return;
482 errno = 0;
483 fp = fopen (name, "rb");
484 if (!fp)
485 {
486 error (f, "%s: %s", name, strerror (errno));
487 return;
488 }
489
490 /* parse the file line by line... */
491 while (fgets (line_buf, sizeof (line_buf), fp))
492 {
493 char *p;
494 char *endp;
495
496 line_no++;
497 p = line_buf;
498 while (isspace ((unsigned char)*p))
499 ++p;
500 if (*p == '\0' || *p == '#')
501 continue;
502
503 /* define var ... endef for command tracking. */
504 if (strneq (p, "define ", 7))
505 {
506 size_t value_len = 0;
507 size_t value_alloc_size = 0;
508 char *value = 0;
509 int newline = 1;
510 int found_endef = 0;
511 const char *var;
512 unsigned var_len;
513
514 /* extract the variable name. */
515 p += 7;
516 while (isblank ((unsigned char)*p))
517 ++p;
518 endp = strchr(p, '\0');
519 while (endp > p && isspace ((unsigned char)endp[-1]))
520 --endp;
521 var_len = endp - p;
522 if (!var_len)
523 {
524 error (f, "%s(%d): bogus define statement.", name, line_no);
525 break;
526 }
527 var = strcache_add_len (p, var_len);
528
529 /* read the define contents. */
530 while (fgets (line_buf, sizeof (line_buf), fp))
531 {
532 size_t len;
533
534 line_no++;
535
536 /* check for endef */
537 if (newline)
538 {
539 p = line_buf;
540 while (isblank ((unsigned char)*p))
541 ++p;
542 if (strneq (p, "endef", 5))
543 {
544 p += 5;
545 while (isspace ((unsigned char)*p))
546 p++;
547 if (*p == '\0')
548 {
549 found_endef = 1;
550 break;
551 }
552 }
553 }
554
555 /* append the line to the buffer. */
556 len = strlen (line_buf);
557 if (value_len + len + 1 > value_alloc_size)
558 {
559 if (value_alloc_size == 0)
560 value_alloc_size = (len + 1 + 0x100) & ~(size_t)0xff;
561 else
562 {
563 value_alloc_size *= 2;
564 if (value_len + len + 1 > value_alloc_size)
565 value_alloc_size = (value_len + len * 2 + 1) & ~(size_t)0xff;
566 }
567 value = xrealloc (value, value_alloc_size);
568 }
569 memcpy (value + value_len, line_buf, len + 1);
570 value_len += len;
571 newline = len ? line_buf[len - 1] == '\n' : 0;
572 }
573
574 if (!found_endef)
575 {
576 error (f, "%s(%d): bogus define statement.", name, line_no);
577 break;
578 }
579
580 /* all that's left is to define it. */
581#ifdef CONFIG_WITH_VALUE_LENGTH
582 define_variable_in_set (var, var_len, value, value_len, 0 /* don't duplicate */,
583 o_file, 0 /* not recursive */, NULL /* global set */,
584 NULL /*no floc*/);
585#else
586 define_variable_in_set (var, var_len, value, o_file, 0 /* not recursive */,
587 NULL /* global set */, NULL /*no floc*/);
588 free (value);
589#endif
590 }
591 /* file: deps */
592 else
593 {
594 struct nameseq *filenames = 0;
595 struct dep *deps = 0;
596 struct dep **nextdep = &deps;
597 struct dep *dep;
598 int next_line = 1;
599 char *colonp;
600
601 /* look for a colon. */
602 colonp = strchr (p, ':');
603#ifdef HAVE_DOS_PATHS
604 while ( colonp
605 && (colonp[1] == '/' || colonp[1] == '\\')
606 && colonp > p
607 && isalpha ((unsigned char)colonp[-1])
608 && ( colonp == p + 1
609 || strchr (" \t(", colonp[-2]) != 0))
610 colonp = strchr (colonp + 1, ':');
611#endif
612 if (!colonp)
613 {
614 error (f, "%s(%d): no colon.", name, line_no);
615 break;
616 }
617
618 /* extract the filename, ASSUME a single one. */
619 endp = colonp;
620 while (endp > p && isblank ((unsigned char)endp[-1]))
621 --endp;
622 if (p == endp)
623 {
624 error (f, "%s(%d): empty filename.", name, line_no);
625 break;
626 }
627 filenames = xmalloc (sizeof (struct nameseq));
628 memset (filenames, 0, sizeof (*filenames));
629 filenames->name = strcache_add_len (p, endp - p);
630
631 /* parse any dependencies, taking care with very long lines. */
632 p = colonp + 1;
633 for (;;)
634 {
635 while (isblank ((unsigned char)*p))
636 ++p;
637
638 /* eol + continuation mark? read the next line. */
639 if (*p == '\\'
640 && ( (p[1] == '\n' && p[2] == '\0')
641 || (p[1] == '\r' && p[2] == '\n' && p[3] == '\0')
642 || p[1] == '\0'))
643 {
644 if (!fgets (line_buf, sizeof (line_buf), fp))
645 break;
646 p = line_buf;
647 line_no++;
648 continue;
649 }
650
651 /* eol? */
652 if ((p[0] == '\n' && p[1] == '\0')
653 || p[0] == '\0'
654 || (p[0] == '\r' && p[1] == '\n' && p[2] == '\0'))
655 break;
656
657 /* find the end of the filename */
658 endp = p;
659 while (!isspace ((unsigned char) *endp) && *endp != '\0')
660 ++endp;
661 if (*endp == '\0' && !feof (fp))
662 {
663 size_t len = endp - p;
664 if (len > (sizeof (line_buf) / 4) * 3)
665 {
666 error (f, "%s(%d): dependency name too long.", name, line_no);
667 break;
668 }
669 memmove (line_buf, p, len);
670 p -= len;
671 endp -= len;
672 if (!fgets (endp, sizeof (line_buf) - len, fp))
673 break;
674 while (!isblank ((unsigned char) *endp) && *endp != '\0')
675 ++endp;
676 if (*endp == '\0' && !feof (fp))
677 {
678 error (f, "%s(%d): dependency name too long.", name, line_no);
679 break;
680 }
681 }
682
683 /* add it to the list. */
684 *nextdep = dep = alloc_dep ();
685 dep->name = strcache_add_len (p, endp - p);
686 nextdep = &dep->next;
687
688 p = endp;
689 }
690
691 /* enter the file with its dependencies. */
692 record_files (filenames, NULL, NULL, deps, 0, NULL, 0, 0, NULL);
693 }
694 } /* while (fgets) */
695
696 fclose (fp);
697}
698#endif /* CONFIG_WITH_INCLUDEDEP */
699
700
701
702/* Read file FILENAME as a makefile and add its contents to the data base.
703
704 SET_DEFAULT is true if we are allowed to set the default goal. */
705
706
707static int
708eval (struct ebuffer *ebuf, int set_default)
709{
710 char *collapsed = 0;
711 unsigned int collapsed_length = 0;
712 unsigned int commands_len = 200;
713 char *commands;
714 unsigned int commands_idx = 0;
715 unsigned int cmds_started, tgts_started;
716 int ignoring = 0, in_ignored_define = 0;
717 int no_targets = 0; /* Set when reading a rule without targets. */
718 struct nameseq *filenames = 0;
719 struct dep *deps = 0;
720 long nlines = 0;
721 int two_colon = 0;
722 const char *pattern = 0;
723 const char *pattern_percent;
724 struct floc *fstart;
725 struct floc fi;
726
727#define record_waiting_files() \
728 do \
729 { \
730 if (filenames != 0) \
731 { \
732 fi.lineno = tgts_started; \
733 record_files (filenames, pattern, pattern_percent, deps, \
734 cmds_started, commands, commands_idx, two_colon, \
735 &fi); \
736 } \
737 filenames = 0; \
738 commands_idx = 0; \
739 no_targets = 0; \
740 pattern = 0; \
741 } while (0)
742
743 pattern_percent = 0;
744 cmds_started = tgts_started = 1;
745
746 fstart = &ebuf->floc;
747 fi.filenm = ebuf->floc.filenm;
748
749 /* Loop over lines in the file.
750 The strategy is to accumulate target names in FILENAMES, dependencies
751 in DEPS and commands in COMMANDS. These are used to define a rule
752 when the start of the next rule (or eof) is encountered.
753
754 When you see a "continue" in the loop below, that means we are moving on
755 to the next line _without_ ending any rule that we happen to be working
756 with at the moment. If you see a "goto rule_complete", then the
757 statement we just parsed also finishes the previous rule. */
758
759 commands = xmalloc (200);
760
761 while (1)
762 {
763 unsigned int linelen;
764 char *line;
765 unsigned int wlen;
766 char *p;
767 char *p2;
768
769 /* Grab the next line to be evaluated */
770 ebuf->floc.lineno += nlines;
771 nlines = readline (ebuf);
772
773 /* If there is nothing left to eval, we're done. */
774 if (nlines < 0)
775 break;
776
777 /* If this line is empty, skip it. */
778 line = ebuf->buffer;
779 if (line[0] == '\0')
780 continue;
781
782 linelen = strlen (line);
783
784 /* Check for a shell command line first.
785 If it is not one, we can stop treating tab specially. */
786 if (line[0] == cmd_prefix)
787 {
788 if (no_targets)
789 /* Ignore the commands in a rule with no targets. */
790 continue;
791
792 /* If there is no preceding rule line, don't treat this line
793 as a command, even though it begins with a tab character.
794 SunOS 4 make appears to behave this way. */
795
796 if (filenames != 0)
797 {
798 if (ignoring)
799 /* Yep, this is a shell command, and we don't care. */
800 continue;
801
802 /* Append this command line to the line being accumulated. */
803 if (commands_idx == 0)
804 cmds_started = ebuf->floc.lineno;
805
806 if (linelen + 1 + commands_idx > commands_len)
807 {
808 commands_len = (linelen + 1 + commands_idx) * 2;
809 commands = xrealloc (commands, commands_len);
810 }
811 memcpy (&commands[commands_idx], line, linelen);
812 commands_idx += linelen;
813 commands[commands_idx++] = '\n';
814
815 continue;
816 }
817 }
818
819 /* This line is not a shell command line. Don't worry about tabs.
820 Get more space if we need it; we don't need to preserve the current
821 contents of the buffer. */
822
823 if (collapsed_length < linelen+1)
824 {
825 collapsed_length = linelen+1;
826 if (collapsed)
827 free (collapsed);
828 collapsed = xmalloc (collapsed_length);
829 }
830 strcpy (collapsed, line);
831 /* Collapse continuation lines. */
832 collapse_continuations (collapsed);
833 remove_comments (collapsed);
834
835 /* Compare a word, both length and contents. */
836#define word1eq(s) (wlen == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
837 p = collapsed;
838 while (isspace ((unsigned char)*p))
839 ++p;
840
841 if (*p == '\0')
842 /* This line is completely empty--ignore it. */
843 continue;
844
845 /* Find the end of the first token. Note we don't need to worry about
846 * ":" here since we compare tokens by length (so "export" will never
847 * be equal to "export:").
848 */
849 for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
850 ;
851 wlen = p2 - p;
852
853 /* Find the start of the second token. If it looks like a target or
854 variable definition it can't be a preprocessor token so skip
855 them--this allows variables/targets named `ifdef', `export', etc. */
856 while (isspace ((unsigned char)*p2))
857 ++p2;
858
859 if ((p2[0] == ':' || p2[0] == '+' || p2[0] == '=') && p2[1] == '\0')
860 {
861 /* It can't be a preprocessor token so skip it if we're ignoring */
862 if (ignoring)
863 continue;
864
865 goto skip_conditionals;
866 }
867
868 /* We must first check for conditional and `define' directives before
869 ignoring anything, since they control what we will do with
870 following lines. */
871
872 if (!in_ignored_define)
873 {
874 int i = conditional_line (p, wlen, fstart);
875 if (i != -2)
876 {
877 if (i == -1)
878 fatal (fstart, _("invalid syntax in conditional"));
879
880 ignoring = i;
881 continue;
882 }
883 }
884
885 if (word1eq ("endef"))
886 {
887 if (!in_ignored_define)
888 fatal (fstart, _("extraneous `endef'"));
889 in_ignored_define = 0;
890 continue;
891 }
892
893 if (word1eq ("define"))
894 {
895 if (ignoring)
896 in_ignored_define = 1;
897 else
898 {
899 if (*p2 == '\0')
900 fatal (fstart, _("empty variable name"));
901
902 /* Let the variable name be the whole rest of the line,
903 with trailing blanks stripped (comments have already been
904 removed), so it could be a complex variable/function
905 reference that might contain blanks. */
906 p = strchr (p2, '\0');
907 while (isblank ((unsigned char)p[-1]))
908 --p;
909 do_define (p2, p - p2, o_file, ebuf);
910 }
911 continue;
912 }
913
914 if (word1eq ("override"))
915 {
916 if (*p2 == '\0')
917 error (fstart, _("empty `override' directive"));
918
919 if (strneq (p2, "define", 6)
920 && (isblank ((unsigned char)p2[6]) || p2[6] == '\0'))
921 {
922 if (ignoring)
923 in_ignored_define = 1;
924 else
925 {
926 p2 = next_token (p2 + 6);
927 if (*p2 == '\0')
928 fatal (fstart, _("empty variable name"));
929
930 /* Let the variable name be the whole rest of the line,
931 with trailing blanks stripped (comments have already been
932 removed), so it could be a complex variable/function
933 reference that might contain blanks. */
934 p = strchr (p2, '\0');
935 while (isblank ((unsigned char)p[-1]))
936 --p;
937 do_define (p2, p - p2, o_override, ebuf);
938 }
939 }
940 else if (!ignoring
941 && !try_variable_definition (fstart, p2, o_override, 0))
942 error (fstart, _("invalid `override' directive"));
943
944 continue;
945 }
946
947 if (ignoring)
948 /* Ignore the line. We continue here so conditionals
949 can appear in the middle of a rule. */
950 continue;
951
952 if (word1eq ("export"))
953 {
954 /* 'export' by itself causes everything to be exported. */
955 if (*p2 == '\0')
956 export_all_variables = 1;
957 else
958 {
959 struct variable *v;
960
961 v = try_variable_definition (fstart, p2, o_file, 0);
962 if (v != 0)
963 v->export = v_export;
964 else
965 {
966 unsigned int l;
967 const char *cp;
968 char *ap;
969
970 /* Expand the line so we can use indirect and constructed
971 variable names in an export command. */
972 cp = ap = allocated_variable_expand (p2);
973
974 for (p = find_next_token (&cp, &l); p != 0;
975 p = find_next_token (&cp, &l))
976 {
977 v = lookup_variable (p, l);
978 if (v == 0)
979 v = define_variable_loc (p, l, "", o_file, 0, fstart);
980 v->export = v_export;
981 }
982
983 free (ap);
984 }
985 }
986 goto rule_complete;
987 }
988
989 if (word1eq ("unexport"))
990 {
991 if (*p2 == '\0')
992 export_all_variables = 0;
993 else
994 {
995 unsigned int l;
996 struct variable *v;
997 const char *cp;
998 char *ap;
999
1000 /* Expand the line so we can use indirect and constructed
1001 variable names in an unexport command. */
1002 cp = ap = allocated_variable_expand (p2);
1003
1004 for (p = find_next_token (&cp, &l); p != 0;
1005 p = find_next_token (&cp, &l))
1006 {
1007 v = lookup_variable (p, l);
1008 if (v == 0)
1009 v = define_variable_loc (p, l, "", o_file, 0, fstart);
1010
1011 v->export = v_noexport;
1012 }
1013
1014 free (ap);
1015 }
1016 goto rule_complete;
1017 }
1018
1019 skip_conditionals:
1020 if (word1eq ("vpath"))
1021 {
1022 const char *cp;
1023 char *vpat;
1024 unsigned int l;
1025 cp = variable_expand (p2);
1026 p = find_next_token (&cp, &l);
1027 if (p != 0)
1028 {
1029 vpat = savestring (p, l);
1030 p = find_next_token (&cp, &l);
1031 /* No searchpath means remove all previous
1032 selective VPATH's with the same pattern. */
1033 }
1034 else
1035 /* No pattern means remove all previous selective VPATH's. */
1036 vpat = 0;
1037 construct_vpath_list (vpat, p);
1038 if (vpat != 0)
1039 free (vpat);
1040
1041 goto rule_complete;
1042 }
1043
1044#ifdef CONFIG_WITH_INCLUDEDEP
1045 if (word1eq ("includedep"))
1046 {
1047 /* We have found an `includedep' line specifying a single nested
1048 makefile to be read at this point. This include variation
1049 does not globbing and doesn't support multiple names. It's
1050 trying to save time by being dead simple. */
1051 char *name = p2;
1052 char *end = strchr(name, '\0');
1053 char saved;
1054
1055 while (end > name && isspace ((unsigned char)end[-1]))
1056 --end;
1057
1058 saved = *end; /* not sure if this is required... */
1059 *end = '\0';
1060 eval_include_dep (name, fstart);
1061 *end = saved;
1062
1063 goto rule_complete;
1064 }
1065#endif /* CONFIG_WITH_INCLUDEDEP */
1066
1067 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
1068 {
1069 /* We have found an `include' line specifying a nested
1070 makefile to be read at this point. */
1071 struct conditionals *save;
1072 struct conditionals new_conditionals;
1073 struct nameseq *files;
1074 /* "-include" (vs "include") says no error if the file does not
1075 exist. "sinclude" is an alias for this from SGI. */
1076 int noerror = (p[0] != 'i');
1077
1078 p = allocated_variable_expand (p2);
1079
1080 /* If no filenames, it's a no-op. */
1081 if (*p == '\0')
1082 {
1083 free (p);
1084 continue;
1085 }
1086
1087 /* Parse the list of file names. */
1088 p2 = p;
1089 files = multi_glob (parse_file_seq (&p2, '\0',
1090 sizeof (struct nameseq),
1091 1),
1092 sizeof (struct nameseq));
1093 free (p);
1094
1095 /* Save the state of conditionals and start
1096 the included makefile with a clean slate. */
1097 save = install_conditionals (&new_conditionals);
1098
1099 /* Record the rules that are waiting so they will determine
1100 the default goal before those in the included makefile. */
1101 record_waiting_files ();
1102
1103 /* Read each included makefile. */
1104 while (files != 0)
1105 {
1106 struct nameseq *next = files->next;
1107 const char *name = files->name;
1108 int r;
1109
1110 free (files);
1111 files = next;
1112
1113 r = eval_makefile (name, (RM_INCLUDED | RM_NO_TILDE
1114 | (noerror ? RM_DONTCARE : 0)));
1115 if (!r && !noerror)
1116 error (fstart, "%s: %s", name, strerror (errno));
1117 }
1118
1119 /* Restore conditional state. */
1120 restore_conditionals (save);
1121
1122 goto rule_complete;
1123 }
1124
1125 if (try_variable_definition (fstart, p, o_file, 0))
1126 /* This line has been dealt with. */
1127 goto rule_complete;
1128
1129 /* This line starts with a tab but was not caught above because there
1130 was no preceding target, and the line might have been usable as a
1131 variable definition. But now we know it is definitely lossage. */
1132 if (line[0] == cmd_prefix)
1133 fatal(fstart, _("commands commence before first target"));
1134
1135 /* This line describes some target files. This is complicated by
1136 the existence of target-specific variables, because we can't
1137 expand the entire line until we know if we have one or not. So
1138 we expand the line word by word until we find the first `:',
1139 then check to see if it's a target-specific variable.
1140
1141 In this algorithm, `lb_next' will point to the beginning of the
1142 unexpanded parts of the input buffer, while `p2' points to the
1143 parts of the expanded buffer we haven't searched yet. */
1144
1145 {
1146 enum make_word_type wtype;
1147 enum variable_origin v_origin;
1148 int exported;
1149 char *cmdleft, *semip, *lb_next;
1150 unsigned int plen = 0;
1151 char *colonp;
1152 const char *end, *beg; /* Helpers for whitespace stripping. */
1153
1154 /* Record the previous rule. */
1155
1156 record_waiting_files ();
1157 tgts_started = fstart->lineno;
1158
1159 /* Search the line for an unquoted ; that is not after an
1160 unquoted #. */
1161 cmdleft = find_char_unquote (line, ';', '#', 0, 1);
1162 if (cmdleft != 0 && *cmdleft == '#')
1163 {
1164 /* We found a comment before a semicolon. */
1165 *cmdleft = '\0';
1166 cmdleft = 0;
1167 }
1168 else if (cmdleft != 0)
1169 /* Found one. Cut the line short there before expanding it. */
1170 *(cmdleft++) = '\0';
1171 semip = cmdleft;
1172
1173 collapse_continuations (line);
1174
1175 /* We can't expand the entire line, since if it's a per-target
1176 variable we don't want to expand it. So, walk from the
1177 beginning, expanding as we go, and looking for "interesting"
1178 chars. The first word is always expandable. */
1179 wtype = get_next_mword(line, NULL, &lb_next, &wlen);
1180 switch (wtype)
1181 {
1182 case w_eol:
1183 if (cmdleft != 0)
1184 fatal(fstart, _("missing rule before commands"));
1185 /* This line contained something but turned out to be nothing
1186 but whitespace (a comment?). */
1187 continue;
1188
1189 case w_colon:
1190 case w_dcolon:
1191 /* We accept and ignore rules without targets for
1192 compatibility with SunOS 4 make. */
1193 no_targets = 1;
1194 continue;
1195
1196 default:
1197 break;
1198 }
1199
1200 p2 = variable_expand_string(NULL, lb_next, wlen);
1201
1202 while (1)
1203 {
1204 lb_next += wlen;
1205 if (cmdleft == 0)
1206 {
1207 /* Look for a semicolon in the expanded line. */
1208 cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
1209
1210 if (cmdleft != 0)
1211 {
1212 unsigned long p2_off = p2 - variable_buffer;
1213 unsigned long cmd_off = cmdleft - variable_buffer;
1214 char *pend = p2 + strlen(p2);
1215
1216 /* Append any remnants of lb, then cut the line short
1217 at the semicolon. */
1218 *cmdleft = '\0';
1219
1220 /* One school of thought says that you shouldn't expand
1221 here, but merely copy, since now you're beyond a ";"
1222 and into a command script. However, the old parser
1223 expanded the whole line, so we continue that for
1224 backwards-compatiblity. Also, it wouldn't be
1225 entirely consistent, since we do an unconditional
1226 expand below once we know we don't have a
1227 target-specific variable. */
1228 (void)variable_expand_string(pend, lb_next, (long)-1);
1229 lb_next += strlen(lb_next);
1230 p2 = variable_buffer + p2_off;
1231 cmdleft = variable_buffer + cmd_off + 1;
1232 }
1233 }
1234
1235 colonp = find_char_unquote(p2, ':', 0, 0, 0);
1236#ifdef HAVE_DOS_PATHS
1237 /* The drive spec brain-damage strikes again... */
1238 /* Note that the only separators of targets in this context
1239 are whitespace and a left paren. If others are possible,
1240 they should be added to the string in the call to index. */
1241 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
1242 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
1243 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
1244 colonp = find_char_unquote(colonp + 1, ':', 0, 0, 0);
1245#endif
1246 if (colonp != 0)
1247 break;
1248
1249 wtype = get_next_mword(lb_next, NULL, &lb_next, &wlen);
1250 if (wtype == w_eol)
1251 break;
1252
1253 p2 += strlen(p2);
1254 *(p2++) = ' ';
1255 p2 = variable_expand_string(p2, lb_next, wlen);
1256 /* We don't need to worry about cmdleft here, because if it was
1257 found in the variable_buffer the entire buffer has already
1258 been expanded... we'll never get here. */
1259 }
1260
1261 p2 = next_token (variable_buffer);
1262
1263 /* If the word we're looking at is EOL, see if there's _anything_
1264 on the line. If not, a variable expanded to nothing, so ignore
1265 it. If so, we can't parse this line so punt. */
1266 if (wtype == w_eol)
1267 {
1268 if (*p2 != '\0')
1269 /* There's no need to be ivory-tower about this: check for
1270 one of the most common bugs found in makefiles... */
1271 fatal (fstart, _("missing separator%s"),
1272 !strneq(line, " ", 8) ? ""
1273 : _(" (did you mean TAB instead of 8 spaces?)"));
1274 continue;
1275 }
1276
1277 /* Make the colon the end-of-string so we know where to stop
1278 looking for targets. */
1279 *colonp = '\0';
1280 filenames = multi_glob (parse_file_seq (&p2, '\0',
1281 sizeof (struct nameseq),
1282 1),
1283 sizeof (struct nameseq));
1284 *p2 = ':';
1285
1286 if (!filenames)
1287 {
1288 /* We accept and ignore rules without targets for
1289 compatibility with SunOS 4 make. */
1290 no_targets = 1;
1291 continue;
1292 }
1293 /* This should never be possible; we handled it above. */
1294 assert (*p2 != '\0');
1295 ++p2;
1296
1297 /* Is this a one-colon or two-colon entry? */
1298 two_colon = *p2 == ':';
1299 if (two_colon)
1300 p2++;
1301
1302 /* Test to see if it's a target-specific variable. Copy the rest
1303 of the buffer over, possibly temporarily (we'll expand it later
1304 if it's not a target-specific variable). PLEN saves the length
1305 of the unparsed section of p2, for later. */
1306 if (*lb_next != '\0')
1307 {
1308 unsigned int l = p2 - variable_buffer;
1309 plen = strlen (p2);
1310 variable_buffer_output (p2+plen, lb_next, strlen (lb_next)+1);
1311 p2 = variable_buffer + l;
1312 }
1313
1314 /* See if it's an "override" or "export" keyword; if so see if what
1315 comes after it looks like a variable definition. */
1316
1317 wtype = get_next_mword (p2, NULL, &p, &wlen);
1318
1319 v_origin = o_file;
1320 exported = 0;
1321 if (wtype == w_static)
1322 {
1323 if (word1eq ("override"))
1324 {
1325 v_origin = o_override;
1326 wtype = get_next_mword (p+wlen, NULL, &p, &wlen);
1327 }
1328 else if (word1eq ("export"))
1329 {
1330 exported = 1;
1331 wtype = get_next_mword (p+wlen, NULL, &p, &wlen);
1332 }
1333 }
1334
1335 if (wtype != w_eol)
1336 wtype = get_next_mword (p+wlen, NULL, NULL, NULL);
1337
1338 if (wtype == w_varassign)
1339 {
1340 /* If there was a semicolon found, add it back, plus anything
1341 after it. */
1342 if (semip)
1343 {
1344 unsigned int l = p - variable_buffer;
1345 *(--semip) = ';';
1346 variable_buffer_output (p2 + strlen (p2),
1347 semip, strlen (semip)+1);
1348 p = variable_buffer + l;
1349 }
1350 record_target_var (filenames, p, v_origin, exported, fstart);
1351 filenames = 0;
1352 continue;
1353 }
1354
1355 /* This is a normal target, _not_ a target-specific variable.
1356 Unquote any = in the dependency list. */
1357 find_char_unquote (lb_next, '=', 0, 0, 0);
1358
1359 /* We have some targets, so don't ignore the following commands. */
1360 no_targets = 0;
1361
1362 /* Expand the dependencies, etc. */
1363 if (*lb_next != '\0')
1364 {
1365 unsigned int l = p2 - variable_buffer;
1366 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
1367 p2 = variable_buffer + l;
1368
1369 /* Look for a semicolon in the expanded line. */
1370 if (cmdleft == 0)
1371 {
1372 cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
1373 if (cmdleft != 0)
1374 *(cmdleft++) = '\0';
1375 }
1376 }
1377
1378 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
1379 p = strchr (p2, ':');
1380 while (p != 0 && p[-1] == '\\')
1381 {
1382 register char *q = &p[-1];
1383 register int backslash = 0;
1384 while (*q-- == '\\')
1385 backslash = !backslash;
1386 if (backslash)
1387 p = strchr (p + 1, ':');
1388 else
1389 break;
1390 }
1391#ifdef _AMIGA
1392 /* Here, the situation is quite complicated. Let's have a look
1393 at a couple of targets:
1394
1395 install: dev:make
1396
1397 dev:make: make
1398
1399 dev:make:: xyz
1400
1401 The rule is that it's only a target, if there are TWO :'s
1402 OR a space around the :.
1403 */
1404 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
1405 || isspace ((unsigned char)p[-1])))
1406 p = 0;
1407#endif
1408#ifdef HAVE_DOS_PATHS
1409 {
1410 int check_again;
1411 do {
1412 check_again = 0;
1413 /* For DOS-style paths, skip a "C:\..." or a "C:/..." */
1414 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
1415 isalpha ((unsigned char)p[-1]) &&
1416 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1417 p = strchr (p + 1, ':');
1418 check_again = 1;
1419 }
1420 } while (check_again);
1421 }
1422#endif
1423 if (p != 0)
1424 {
1425 struct nameseq *target;
1426 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
1427 ++p2;
1428 if (target == 0)
1429 fatal (fstart, _("missing target pattern"));
1430 else if (target->next != 0)
1431 fatal (fstart, _("multiple target patterns (target `%s')"), target->name); /* bird */
1432 pattern_percent = find_percent_cached (&target->name);
1433 pattern = target->name;
1434 if (pattern_percent == 0)
1435 fatal (fstart, _("target pattern contains no `%%' (target `%s')"), target->name); /* bird */
1436 free (target);
1437 }
1438 else
1439 pattern = 0;
1440
1441 /* Strip leading and trailing whitespaces. */
1442 beg = p2;
1443 end = beg + strlen (beg) - 1;
1444 strip_whitespace (&beg, &end);
1445
1446 if (beg <= end && *beg != '\0')
1447 {
1448 /* Put all the prerequisites here; they'll be parsed later. */
1449 deps = alloc_dep ();
1450 deps->name = strcache_add_len (beg, end - beg + 1);
1451 }
1452 else
1453 deps = 0;
1454
1455 commands_idx = 0;
1456 if (cmdleft != 0)
1457 {
1458 /* Semicolon means rest of line is a command. */
1459 unsigned int l = strlen (cmdleft);
1460
1461 cmds_started = fstart->lineno;
1462
1463 /* Add this command line to the buffer. */
1464 if (l + 2 > commands_len)
1465 {
1466 commands_len = (l + 2) * 2;
1467 commands = xrealloc (commands, commands_len);
1468 }
1469 memcpy (commands, cmdleft, l);
1470 commands_idx += l;
1471 commands[commands_idx++] = '\n';
1472 }
1473
1474 /* Determine if this target should be made default. We used to do
1475 this in record_files() but because of the delayed target recording
1476 and because preprocessor directives are legal in target's commands
1477 it is too late. Consider this fragment for example:
1478
1479 foo:
1480
1481 ifeq ($(.DEFAULT_GOAL),foo)
1482 ...
1483 endif
1484
1485 Because the target is not recorded until after ifeq directive is
1486 evaluated the .DEFAULT_GOAL does not contain foo yet as one
1487 would expect. Because of this we have to move some of the logic
1488 here. */
1489
1490 if (**default_goal_name == '\0' && set_default)
1491 {
1492 const char *name;
1493 struct dep *d;
1494 struct nameseq *t = filenames;
1495
1496 for (; t != 0; t = t->next)
1497 {
1498 int reject = 0;
1499 name = t->name;
1500
1501 /* We have nothing to do if this is an implicit rule. */
1502 if (strchr (name, '%') != 0)
1503 break;
1504
1505 /* See if this target's name does not start with a `.',
1506 unless it contains a slash. */
1507 if (*name == '.' && strchr (name, '/') == 0
1508#ifdef HAVE_DOS_PATHS
1509 && strchr (name, '\\') == 0
1510#endif
1511 )
1512 continue;
1513
1514
1515 /* If this file is a suffix, don't let it be
1516 the default goal file. */
1517 for (d = suffix_file->deps; d != 0; d = d->next)
1518 {
1519 register struct dep *d2;
1520 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1521 {
1522 reject = 1;
1523 break;
1524 }
1525 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1526 {
1527 unsigned int l = strlen (dep_name (d2));
1528 if (!strneq (name, dep_name (d2), l))
1529 continue;
1530 if (streq (name + l, dep_name (d)))
1531 {
1532 reject = 1;
1533 break;
1534 }
1535 }
1536
1537 if (reject)
1538 break;
1539 }
1540
1541 if (!reject)
1542 {
1543 define_variable_global (".DEFAULT_GOAL", 13, t->name,
1544 o_file, 0, NILF);
1545 break;
1546 }
1547 }
1548 }
1549
1550 continue;
1551 }
1552
1553 /* We get here except in the case that we just read a rule line.
1554 Record now the last rule we read, so following spurious
1555 commands are properly diagnosed. */
1556 rule_complete:
1557 record_waiting_files ();
1558 }
1559
1560#undef word1eq
1561
1562 if (conditionals->if_cmds)
1563 fatal (fstart, _("missing `endif'"));
1564
1565 /* At eof, record the last rule. */
1566 record_waiting_files ();
1567
1568 if (collapsed)
1569 free (collapsed);
1570 free (commands);
1571
1572 return 1;
1573}
1574
1575
1576
1577/* Remove comments from LINE.
1578 This is done by copying the text at LINE onto itself. */
1579
1580static void
1581remove_comments (char *line)
1582{
1583 char *comment;
1584
1585 comment = find_char_unquote (line, '#', 0, 0, 0);
1586
1587 if (comment != 0)
1588 /* Cut off the line at the #. */
1589 *comment = '\0';
1590}
1591
1592/* Execute a `define' directive.
1593 The first line has already been read, and NAME is the name of
1594 the variable to be defined. The following lines remain to be read. */
1595
1596static void
1597do_define (char *name, unsigned int namelen,
1598 enum variable_origin origin, struct ebuffer *ebuf)
1599{
1600 struct floc defstart;
1601 long nlines = 0;
1602 int nlevels = 1;
1603 unsigned int length = 100;
1604 char *definition = xmalloc (length);
1605 unsigned int idx = 0;
1606 char *p;
1607
1608 /* Expand the variable name. */
1609 char *var = alloca (namelen + 1);
1610 memcpy (var, name, namelen);
1611 var[namelen] = '\0';
1612 var = variable_expand (var);
1613
1614 defstart = ebuf->floc;
1615
1616 while (1)
1617 {
1618 unsigned int len;
1619 char *line;
1620
1621 nlines = readline (ebuf);
1622 ebuf->floc.lineno += nlines;
1623
1624 /* If there is nothing left to eval, we're done. */
1625 if (nlines < 0)
1626 break;
1627
1628 line = ebuf->buffer;
1629
1630 collapse_continuations (line);
1631
1632 /* If the line doesn't begin with a tab, test to see if it introduces
1633 another define, or ends one. */
1634
1635 /* Stop if we find an 'endef' */
1636 if (line[0] != cmd_prefix)
1637 {
1638 p = next_token (line);
1639 len = strlen (p);
1640
1641 /* If this is another 'define', increment the level count. */
1642 if ((len == 6 || (len > 6 && isblank ((unsigned char)p[6])))
1643 && strneq (p, "define", 6))
1644 ++nlevels;
1645
1646 /* If this is an 'endef', decrement the count. If it's now 0,
1647 we've found the last one. */
1648 else if ((len == 5 || (len > 5 && isblank ((unsigned char)p[5])))
1649 && strneq (p, "endef", 5))
1650 {
1651 p += 5;
1652 remove_comments (p);
1653 if (*next_token (p) != '\0')
1654 error (&ebuf->floc,
1655 _("Extraneous text after `endef' directive"));
1656
1657 if (--nlevels == 0)
1658 {
1659 /* Define the variable. */
1660 if (idx == 0)
1661 definition[0] = '\0';
1662 else
1663 definition[idx - 1] = '\0';
1664
1665 /* Always define these variables in the global set. */
1666 define_variable_global (var, strlen (var), definition,
1667 origin, 1, &defstart);
1668 free (definition);
1669 return;
1670 }
1671 }
1672 }
1673
1674 /* Otherwise add this line to the variable definition. */
1675 len = strlen (line);
1676 if (idx + len + 1 > length)
1677 {
1678 length = (idx + len) * 2;
1679 definition = xrealloc (definition, length + 1);
1680 }
1681
1682 memcpy (&definition[idx], line, len);
1683 idx += len;
1684 /* Separate lines with a newline. */
1685 definition[idx++] = '\n';
1686 }
1687
1688 /* No `endef'!! */
1689 fatal (&defstart, _("missing `endef', unterminated `define'"));
1690
1691 /* NOTREACHED */
1692 return;
1693}
1694
1695
1696/* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1697 "ifneq", "if1of", "ifn1of", "else" and "endif".
1698 LINE is the input line, with the command as its first word.
1699
1700 FILENAME and LINENO are the filename and line number in the
1701 current makefile. They are used for error messages.
1702
1703 Value is -2 if the line is not a conditional at all,
1704 -1 if the line is an invalid conditional,
1705 0 if following text should be interpreted,
1706 1 if following text should be ignored. */
1707
1708static int
1709conditional_line (char *line, int len, const struct floc *flocp)
1710{
1711 char *cmdname;
1712#ifdef CONFIG_WITH_SET_CONDITIONALS
1713 enum { c_ifdef, c_ifndef, c_ifeq, c_ifneq, c_if1of, c_ifn1of, c_else, c_endif } cmdtype;
1714#else
1715 enum { c_ifdef, c_ifndef, c_ifeq, c_ifneq, c_else, c_endif } cmdtype;
1716#endif
1717 unsigned int i;
1718 unsigned int o;
1719
1720 /* Compare a word, both length and contents. */
1721#define word1eq(s) (len == sizeof(s)-1 && strneq (s, line, sizeof(s)-1))
1722#define chkword(s, t) if (word1eq (s)) { cmdtype = (t); cmdname = (s); }
1723
1724 /* Make sure this line is a conditional. */
1725 chkword ("ifdef", c_ifdef)
1726 else chkword ("ifndef", c_ifndef)
1727 else chkword ("ifeq", c_ifeq)
1728 else chkword ("ifneq", c_ifneq)
1729#ifdef CONFIG_WITH_SET_CONDITIONALS
1730 else chkword ("if1of", c_if1of)
1731 else chkword ("ifn1of", c_ifn1of)
1732#endif
1733 else chkword ("else", c_else)
1734 else chkword ("endif", c_endif)
1735 else
1736 return -2;
1737
1738 /* Found one: skip past it and any whitespace after it. */
1739 line = next_token (line + len);
1740
1741#define EXTRANEOUS() error (flocp, _("Extraneous text after `%s' directive"), cmdname)
1742
1743 /* An 'endif' cannot contain extra text, and reduces the if-depth by 1 */
1744 if (cmdtype == c_endif)
1745 {
1746 if (*line != '\0')
1747 EXTRANEOUS ();
1748
1749 if (!conditionals->if_cmds)
1750 fatal (flocp, _("extraneous `%s'"), cmdname);
1751
1752 --conditionals->if_cmds;
1753
1754 goto DONE;
1755 }
1756
1757 /* An 'else' statement can either be simple, or it can have another
1758 conditional after it. */
1759 if (cmdtype == c_else)
1760 {
1761 const char *p;
1762
1763 if (!conditionals->if_cmds)
1764 fatal (flocp, _("extraneous `%s'"), cmdname);
1765
1766 o = conditionals->if_cmds - 1;
1767
1768 if (conditionals->seen_else[o])
1769 fatal (flocp, _("only one `else' per conditional"));
1770
1771 /* Change the state of ignorance. */
1772 switch (conditionals->ignoring[o])
1773 {
1774 case 0:
1775 /* We've just been interpreting. Never do it again. */
1776 conditionals->ignoring[o] = 2;
1777 break;
1778 case 1:
1779 /* We've never interpreted yet. Maybe this time! */
1780 conditionals->ignoring[o] = 0;
1781 break;
1782 }
1783
1784 /* It's a simple 'else'. */
1785 if (*line == '\0')
1786 {
1787 conditionals->seen_else[o] = 1;
1788 goto DONE;
1789 }
1790
1791 /* The 'else' has extra text. That text must be another conditional
1792 and cannot be an 'else' or 'endif'. */
1793
1794 /* Find the length of the next word. */
1795 for (p = line+1; *p != '\0' && !isspace ((unsigned char)*p); ++p)
1796 ;
1797 len = p - line;
1798
1799 /* If it's 'else' or 'endif' or an illegal conditional, fail. */
1800 if (word1eq("else") || word1eq("endif")
1801 || conditional_line (line, len, flocp) < 0)
1802 EXTRANEOUS ();
1803 else
1804 {
1805 /* conditional_line() created a new level of conditional.
1806 Raise it back to this level. */
1807 if (conditionals->ignoring[o] < 2)
1808 conditionals->ignoring[o] = conditionals->ignoring[o+1];
1809 --conditionals->if_cmds;
1810 }
1811
1812 goto DONE;
1813 }
1814
1815 if (conditionals->allocated == 0)
1816 {
1817 conditionals->allocated = 5;
1818 conditionals->ignoring = xmalloc (conditionals->allocated);
1819 conditionals->seen_else = xmalloc (conditionals->allocated);
1820 }
1821
1822 o = conditionals->if_cmds++;
1823 if (conditionals->if_cmds > conditionals->allocated)
1824 {
1825 conditionals->allocated += 5;
1826 conditionals->ignoring = xrealloc (conditionals->ignoring,
1827 conditionals->allocated);
1828 conditionals->seen_else = xrealloc (conditionals->seen_else,
1829 conditionals->allocated);
1830 }
1831
1832 /* Record that we have seen an `if...' but no `else' so far. */
1833 conditionals->seen_else[o] = 0;
1834
1835 /* Search through the stack to see if we're already ignoring. */
1836 for (i = 0; i < o; ++i)
1837 if (conditionals->ignoring[i])
1838 {
1839 /* We are already ignoring, so just push a level to match the next
1840 "else" or "endif", and keep ignoring. We don't want to expand
1841 variables in the condition. */
1842 conditionals->ignoring[o] = 1;
1843 return 1;
1844 }
1845
1846 if (cmdtype == c_ifdef || cmdtype == c_ifndef)
1847 {
1848 char *var;
1849 struct variable *v;
1850 char *p;
1851
1852 /* Expand the thing we're looking up, so we can use indirect and
1853 constructed variable names. */
1854 var = allocated_variable_expand (line);
1855
1856 /* Make sure there's only one variable name to test. */
1857 p = end_of_token (var);
1858 i = p - var;
1859 p = next_token (p);
1860 if (*p != '\0')
1861 return -1;
1862
1863 var[i] = '\0';
1864 v = lookup_variable (var, i);
1865
1866 conditionals->ignoring[o] =
1867 ((v != 0 && *v->value != '\0') == (cmdtype == c_ifndef));
1868
1869 free (var);
1870 }
1871 else
1872 {
1873#ifdef CONFIG_WITH_SET_CONDITIONALS
1874 /* "ifeq", "ifneq", "if1of" or "ifn1of". */
1875#else
1876 /* "ifeq" or "ifneq". */
1877#endif
1878 char *s1, *s2;
1879 unsigned int l;
1880 char termin = *line == '(' ? ',' : *line;
1881
1882 if (termin != ',' && termin != '"' && termin != '\'')
1883 return -1;
1884
1885 s1 = ++line;
1886 /* Find the end of the first string. */
1887 if (termin == ',')
1888 {
1889 int count = 0;
1890 for (; *line != '\0'; ++line)
1891 if (*line == '(')
1892 ++count;
1893 else if (*line == ')')
1894 --count;
1895 else if (*line == ',' && count <= 0)
1896 break;
1897 }
1898 else
1899 while (*line != '\0' && *line != termin)
1900 ++line;
1901
1902 if (*line == '\0')
1903 return -1;
1904
1905 if (termin == ',')
1906 {
1907 /* Strip blanks after the first string. */
1908 char *p = line++;
1909 while (isblank ((unsigned char)p[-1]))
1910 --p;
1911 *p = '\0';
1912 }
1913 else
1914 *line++ = '\0';
1915
1916 s2 = variable_expand (s1);
1917 /* We must allocate a new copy of the expanded string because
1918 variable_expand re-uses the same buffer. */
1919 l = strlen (s2);
1920 s1 = alloca (l + 1);
1921 memcpy (s1, s2, l + 1);
1922
1923 if (termin != ',')
1924 /* Find the start of the second string. */
1925 line = next_token (line);
1926
1927 termin = termin == ',' ? ')' : *line;
1928 if (termin != ')' && termin != '"' && termin != '\'')
1929 return -1;
1930
1931 /* Find the end of the second string. */
1932 if (termin == ')')
1933 {
1934 int count = 0;
1935 s2 = next_token (line);
1936 for (line = s2; *line != '\0'; ++line)
1937 {
1938 if (*line == '(')
1939 ++count;
1940 else if (*line == ')')
1941 {
1942 if (count <= 0)
1943 break;
1944 else
1945 --count;
1946 }
1947 }
1948 }
1949 else
1950 {
1951 ++line;
1952 s2 = line;
1953 while (*line != '\0' && *line != termin)
1954 ++line;
1955 }
1956
1957 if (*line == '\0')
1958 return -1;
1959
1960 *line = '\0';
1961 line = next_token (++line);
1962 if (*line != '\0')
1963 EXTRANEOUS ();
1964
1965 s2 = variable_expand (s2);
1966#ifdef CONFIG_WITH_SET_CONDITIONALS
1967 if (cmdtype == c_if1of || cmdtype == c_ifn1of)
1968 {
1969 const char *s1_cur;
1970 unsigned int s1_len;
1971 const char *s1_iterator = s1;
1972
1973 conditionals->ignoring[o] = (cmdtype == c_if1of); /* if not found */
1974 while ((s1_cur = find_next_token (&s1_iterator, &s1_len)) != 0)
1975 {
1976 const char *s2_cur;
1977 unsigned int s2_len;
1978 const char *s2_iterator = s2;
1979 while ((s2_cur = find_next_token (&s2_iterator, &s2_len)) != 0)
1980 if (s2_len == s1_len
1981 && strneq (s2_cur, s1_cur, s1_len) )
1982 {
1983 conditionals->ignoring[o] = (cmdtype != c_if1of); /* found */
1984 break;
1985 }
1986 }
1987 }
1988 else
1989 conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
1990#else
1991 conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
1992#endif
1993 }
1994
1995 DONE:
1996 /* Search through the stack to see if we're ignoring. */
1997 for (i = 0; i < conditionals->if_cmds; ++i)
1998 if (conditionals->ignoring[i])
1999 return 1;
2000 return 0;
2001}
2002
2003
2004/* Remove duplicate dependencies in CHAIN. */
2005
2006static unsigned long
2007dep_hash_1 (const void *key)
2008{
2009 return_STRING_HASH_1 (dep_name ((struct dep const *) key));
2010}
2011
2012static unsigned long
2013dep_hash_2 (const void *key)
2014{
2015 return_STRING_HASH_2 (dep_name ((struct dep const *) key));
2016}
2017
2018static int
2019dep_hash_cmp (const void *x, const void *y)
2020{
2021 struct dep *dx = (struct dep *) x;
2022 struct dep *dy = (struct dep *) y;
2023 int cmp = strcmp (dep_name (dx), dep_name (dy));
2024
2025 /* If the names are the same but ignore_mtimes are not equal, one of these
2026 is an order-only prerequisite and one isn't. That means that we should
2027 remove the one that isn't and keep the one that is. */
2028
2029 if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
2030 dx->ignore_mtime = dy->ignore_mtime = 0;
2031
2032 return cmp;
2033}
2034
2035
2036void
2037uniquize_deps (struct dep *chain)
2038{
2039 struct hash_table deps;
2040 register struct dep **depp;
2041
2042 hash_init (&deps, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
2043
2044 /* Make sure that no dependencies are repeated. This does not
2045 really matter for the purpose of updating targets, but it
2046 might make some names be listed twice for $^ and $?. */
2047
2048 depp = &chain;
2049 while (*depp)
2050 {
2051 struct dep *dep = *depp;
2052 struct dep **dep_slot = (struct dep **) hash_find_slot (&deps, dep);
2053 if (HASH_VACANT (*dep_slot))
2054 {
2055 hash_insert_at (&deps, dep, dep_slot);
2056 depp = &dep->next;
2057 }
2058 else
2059 {
2060 /* Don't bother freeing duplicates.
2061 It's dangerous and little benefit accrues. */
2062 *depp = dep->next;
2063 }
2064 }
2065
2066 hash_free (&deps, 0);
2067}
2068
2069
2070/* Record target-specific variable values for files FILENAMES.
2071 TWO_COLON is nonzero if a double colon was used.
2072
2073 The links of FILENAMES are freed, and so are any names in it
2074 that are not incorporated into other data structures.
2075
2076 If the target is a pattern, add the variable to the pattern-specific
2077 variable value list. */
2078
2079static void
2080record_target_var (struct nameseq *filenames, char *defn,
2081 enum variable_origin origin, int exported,
2082 const struct floc *flocp)
2083{
2084 struct nameseq *nextf;
2085 struct variable_set_list *global;
2086
2087 global = current_variable_set_list;
2088
2089 /* If the variable is an append version, store that but treat it as a
2090 normal recursive variable. */
2091
2092 for (; filenames != 0; filenames = nextf)
2093 {
2094 struct variable *v;
2095 const char *name = filenames->name;
2096 const char *fname;
2097 const char *percent;
2098 struct pattern_var *p;
2099
2100 nextf = filenames->next;
2101 free (filenames);
2102
2103 /* If it's a pattern target, then add it to the pattern-specific
2104 variable list. */
2105 percent = find_percent_cached (&name);
2106 if (percent)
2107 {
2108 /* Get a reference for this pattern-specific variable struct. */
2109 p = create_pattern_var (name, percent);
2110 p->variable.fileinfo = *flocp;
2111 /* I don't think this can fail since we already determined it was a
2112 variable definition. */
2113 v = parse_variable_definition (&p->variable, defn);
2114 assert (v != 0);
2115
2116 if (v->flavor == f_simple)
2117 v->value = allocated_variable_expand (v->value);
2118 else
2119 v->value = xstrdup (v->value);
2120
2121 fname = p->target;
2122 }
2123 else
2124 {
2125 struct file *f;
2126
2127 /* Get a file reference for this file, and initialize it.
2128 We don't want to just call enter_file() because that allocates a
2129 new entry if the file is a double-colon, which we don't want in
2130 this situation. */
2131 f = lookup_file (name);
2132 if (!f)
2133 f = enter_file (strcache_add (name));
2134 else if (f->double_colon)
2135 f = f->double_colon;
2136
2137 initialize_file_variables (f, 1);
2138 fname = f->name;
2139
2140 current_variable_set_list = f->variables;
2141 v = try_variable_definition (flocp, defn, origin, 1);
2142 if (!v)
2143 error (flocp, _("Malformed target-specific variable definition"));
2144 current_variable_set_list = global;
2145 }
2146
2147 /* Set up the variable to be *-specific. */
2148 v->origin = origin;
2149 v->per_target = 1;
2150 v->export = exported ? v_export : v_default;
2151
2152 /* If it's not an override, check to see if there was a command-line
2153 setting. If so, reset the value. */
2154 if (origin != o_override)
2155 {
2156 struct variable *gv;
2157 int len = strlen(v->name);
2158
2159 gv = lookup_variable (v->name, len);
2160 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
2161 {
2162 if (v->value != 0)
2163 free (v->value);
2164 v->value = xstrdup (gv->value);
2165 v->origin = gv->origin;
2166 v->recursive = gv->recursive;
2167 v->append = 0;
2168 }
2169 }
2170 }
2171}
2172
2173
2174/* Record a description line for files FILENAMES,
2175 with dependencies DEPS, commands to execute described
2176 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
2177 TWO_COLON is nonzero if a double colon was used.
2178 If not nil, PATTERN is the `%' pattern to make this
2179 a static pattern rule, and PATTERN_PERCENT is a pointer
2180 to the `%' within it.
2181
2182 The links of FILENAMES are freed, and so are any names in it
2183 that are not incorporated into other data structures. */
2184
2185static void
2186record_files (struct nameseq *filenames, const char *pattern,
2187 const char *pattern_percent, struct dep *deps,
2188 unsigned int cmds_started, char *commands,
2189 unsigned int commands_idx, int two_colon,
2190 const struct floc *flocp)
2191{
2192 struct nameseq *nextf;
2193 int implicit = 0;
2194 unsigned int max_targets = 0, target_idx = 0;
2195 const char **targets = 0, **target_percents = 0;
2196 struct commands *cmds;
2197#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2198 struct file *prev_file = 0;
2199 enum multitarget_mode { m_unsettled, m_no, m_yes, m_yes_maybe }
2200 multi_mode = !two_colon && !pattern ? m_unsettled : m_no;
2201#endif
2202
2203 /* If we've already snapped deps, that means we're in an eval being
2204 resolved after the makefiles have been read in. We can't add more rules
2205 at this time, since they won't get snapped and we'll get core dumps.
2206 See Savannah bug # 12124. */
2207 if (snapped_deps)
2208 fatal (flocp, _("prerequisites cannot be defined in command scripts"));
2209
2210 if (commands_idx > 0)
2211 {
2212 cmds = xmalloc (sizeof (struct commands));
2213 cmds->fileinfo.filenm = flocp->filenm;
2214 cmds->fileinfo.lineno = cmds_started;
2215 cmds->commands = savestring (commands, commands_idx);
2216 cmds->command_lines = 0;
2217 }
2218 else
2219 cmds = 0;
2220
2221 for (; filenames != 0; filenames = nextf)
2222 {
2223 const char *name = filenames->name;
2224 struct file *f;
2225 struct dep *this = 0;
2226 const char *implicit_percent;
2227
2228 nextf = filenames->next;
2229 free (filenames);
2230
2231 /* Check for special targets. Do it here instead of, say, snap_deps()
2232 so that we can immediately use the value. */
2233
2234 if (streq (name, ".POSIX"))
2235 posix_pedantic = 1;
2236 else if (streq (name, ".SECONDEXPANSION"))
2237 second_expansion = 1;
2238
2239 implicit_percent = find_percent_cached (&name);
2240 implicit |= implicit_percent != 0;
2241
2242 if (implicit)
2243 {
2244 if (pattern != 0)
2245 fatal (flocp, _("mixed implicit and static pattern rules"));
2246
2247 if (implicit_percent == 0)
2248 fatal (flocp, _("mixed implicit and normal rules"));
2249
2250 if (targets == 0)
2251 {
2252 max_targets = 5;
2253 targets = xmalloc (5 * sizeof (char *));
2254 target_percents = xmalloc (5 * sizeof (char *));
2255 target_idx = 0;
2256 }
2257 else if (target_idx == max_targets - 1)
2258 {
2259 max_targets += 5;
2260 targets = xrealloc ((void *)targets, max_targets * sizeof (char *));
2261 target_percents = xrealloc ((void *)target_percents,
2262 max_targets * sizeof (char *));
2263 }
2264 targets[target_idx] = name;
2265 target_percents[target_idx] = implicit_percent;
2266 ++target_idx;
2267 continue;
2268 }
2269
2270#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2271 /* Check for the explicit multitarget mode operators. For this to be
2272 identified as an explicit multiple target rule, the first + or +|
2273 operator *must* appear between the first two files. If not found as
2274 the 2nd file or if found as the 1st file, the rule will be rejected
2275 as a potential multiple first target rule. For the subsequent files
2276 the operator is only required to switch between maybe and non-maybe
2277 mode:
2278 `primary + 2nd 3rd +| 4th-maybe + 5th-for-sure: deps; cmds'
2279
2280 The whole idea of the maybe-updated files is this:
2281 timestamp +| maybe.h: src1.c src2.c
2282 grep goes-into-maybe.h $* > timestamp
2283 cmp timestamp maybe.h || cp -f timestamp maybe.h
2284
2285 This is implemented in remake.c where we don't consider the mtime of
2286 the maybe-updated targets. */
2287 if (multi_mode != m_no && name[0] == '+'
2288 && (name[1] == '\0' || (name[1] == '|' && name[2] == '\0')))
2289 {
2290 if (!prev_file)
2291 multi_mode = m_no; /* first */
2292 else
2293 {
2294 if (multi_mode == m_unsettled)
2295 {
2296 prev_file->multi_head = prev_file;
2297
2298 /* Only the primary file needs the dependencies. */
2299 if (deps)
2300 {
2301 free_dep_chain (deps);
2302 deps = NULL;
2303 }
2304 }
2305 multi_mode = name[1] == '\0' ? m_yes : m_yes_maybe;
2306 continue;
2307 }
2308 }
2309 else if (multi_mode == m_unsettled && prev_file)
2310 multi_mode = m_no;
2311#endif
2312
2313 /* If this is a static pattern rule:
2314 `targets: target%pattern: dep%pattern; cmds',
2315 make sure the pattern matches this target name. */
2316 if (pattern && !pattern_matches (pattern, pattern_percent, name))
2317 error (flocp, _("target `%s' doesn't match the target pattern"), name);
2318 else if (deps)
2319 {
2320 /* If there are multiple filenames, copy the chain DEPS for all but
2321 the last one. It is not safe for the same deps to go in more
2322 than one place in the database. */
2323 this = nextf != 0 ? copy_dep_chain (deps) : deps;
2324 this->need_2nd_expansion = (second_expansion
2325 && strchr (this->name, '$'));
2326 }
2327
2328 if (!two_colon)
2329 {
2330 /* Single-colon. Combine these dependencies
2331 with others in file's existing record, if any. */
2332 f = enter_file (strcache_add (name));
2333
2334 if (f->double_colon)
2335 fatal (flocp,
2336 _("target file `%s' has both : and :: entries"), f->name);
2337
2338 /* If CMDS == F->CMDS, this target was listed in this rule
2339 more than once. Just give a warning since this is harmless. */
2340 if (cmds != 0 && cmds == f->cmds)
2341 error (flocp,
2342 _("target `%s' given more than once in the same rule."),
2343 f->name);
2344
2345 /* Check for two single-colon entries both with commands.
2346 Check is_target so that we don't lose on files such as .c.o
2347 whose commands were preinitialized. */
2348 else if (cmds != 0 && f->cmds != 0 && f->is_target)
2349 {
2350 error (&cmds->fileinfo,
2351 _("warning: overriding commands for target `%s'"),
2352 f->name);
2353 error (&f->cmds->fileinfo,
2354 _("warning: ignoring old commands for target `%s'"),
2355 f->name);
2356 }
2357
2358 f->is_target = 1;
2359
2360 /* Defining .DEFAULT with no deps or cmds clears it. */
2361 if (f == default_file && this == 0 && cmds == 0)
2362 f->cmds = 0;
2363 if (cmds != 0)
2364 f->cmds = cmds;
2365
2366#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2367 /* If this is an explicit multi target rule, add it to the
2368 target chain and set the multi_maybe flag according to
2369 the current mode. */
2370
2371 if (multi_mode >= m_yes)
2372 {
2373 f->multi_maybe = multi_mode == m_yes_maybe;
2374 prev_file->multi_next = f;
2375 assert (prev_file->multi_head != 0);
2376 f->multi_head = prev_file->multi_head;
2377
2378 if (f == suffix_file)
2379 error (flocp,
2380 _(".SUFFIXES encountered in an explicit multi target rule"));
2381 }
2382 prev_file = f;
2383#endif
2384
2385 /* Defining .SUFFIXES with no dependencies clears out the list of
2386 suffixes. */
2387 if (f == suffix_file && this == 0)
2388 {
2389 free_dep_chain (f->deps);
2390 f->deps = 0;
2391 }
2392 else if (this != 0)
2393 {
2394 /* Add the file's old deps and the new ones in THIS together. */
2395
2396 if (f->deps != 0)
2397 {
2398 struct dep **d_ptr = &f->deps;
2399
2400 while ((*d_ptr)->next != 0)
2401 d_ptr = &(*d_ptr)->next;
2402
2403 if (cmds != 0)
2404 /* This is the rule with commands, so put its deps
2405 last. The rationale behind this is that $< expands to
2406 the first dep in the chain, and commands use $<
2407 expecting to get the dep that rule specifies. However
2408 the second expansion algorithm reverses the order thus
2409 we need to make it last here. */
2410 (*d_ptr)->next = this;
2411 else
2412 {
2413 /* This is the rule without commands. Put its
2414 dependencies at the end but before dependencies from
2415 the rule with commands (if any). This way everything
2416 appears in makefile order. */
2417
2418 if (f->cmds != 0)
2419 {
2420 this->next = *d_ptr;
2421 *d_ptr = this;
2422 }
2423 else
2424 (*d_ptr)->next = this;
2425 }
2426 }
2427 else
2428 f->deps = this;
2429
2430 /* This is a hack. I need a way to communicate to snap_deps()
2431 that the last dependency line in this file came with commands
2432 (so that logic in snap_deps() can put it in front and all
2433 this $< -logic works). I cannot simply rely on file->cmds
2434 being not 0 because of the cases like the following:
2435
2436 foo: bar
2437 foo:
2438 ...
2439
2440 I am going to temporarily "borrow" UPDATING member in
2441 `struct file' for this. */
2442
2443 if (cmds != 0)
2444 f->updating = 1;
2445 }
2446 }
2447 else
2448 {
2449 /* Double-colon. Make a new record even if there already is one. */
2450 f = lookup_file (name);
2451
2452 /* Check for both : and :: rules. Check is_target so
2453 we don't lose on default suffix rules or makefiles. */
2454 if (f != 0 && f->is_target && !f->double_colon)
2455 fatal (flocp,
2456 _("target file `%s' has both : and :: entries"), f->name);
2457 f = enter_file (strcache_add (name));
2458 /* If there was an existing entry and it was a double-colon entry,
2459 enter_file will have returned a new one, making it the prev
2460 pointer of the old one, and setting its double_colon pointer to
2461 the first one. */
2462 if (f->double_colon == 0)
2463 /* This is the first entry for this name, so we must set its
2464 double_colon pointer to itself. */
2465 f->double_colon = f;
2466 f->is_target = 1;
2467 f->deps = this;
2468 f->cmds = cmds;
2469 }
2470
2471 /* If this is a static pattern rule, set the stem to the part of its
2472 name that matched the `%' in the pattern, so you can use $* in the
2473 commands. */
2474 if (pattern)
2475 {
2476 static const char *percent = "%";
2477 char *buffer = variable_expand ("");
2478 const size_t buffer_offset = buffer - variable_buffer; /* bird */
2479 char *o = patsubst_expand_pat (buffer, name, pattern, percent,
2480 pattern_percent+1, percent+1);
2481 buffer = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. */
2482 f->stem = strcache_add_len (buffer, o - buffer);
2483 if (this)
2484 {
2485 this->staticpattern = 1;
2486 this->stem = f->stem;
2487 }
2488 }
2489
2490 name = f->name;
2491
2492 /* If this target is a default target, update DEFAULT_GOAL_FILE. */
2493 if (streq (*default_goal_name, name)
2494 && (default_goal_file == 0
2495 || ! streq (default_goal_file->name, name)))
2496 default_goal_file = f;
2497 }
2498
2499 if (implicit)
2500 {
2501 if (deps)
2502 deps->need_2nd_expansion = second_expansion;
2503 create_pattern_rule (targets, target_percents, target_idx,
2504 two_colon, deps, cmds, 1);
2505 }
2506}
2507
2508
2509/* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2510 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2511 Quoting backslashes are removed from STRING by compacting it into
2512 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2513 one, or nil if there are none. STOPCHARs inside variable references are
2514 ignored if IGNOREVARS is true.
2515
2516 STOPCHAR _cannot_ be '$' if IGNOREVARS is true. */
2517
2518static char *
2519find_char_unquote (char *string, int stop1, int stop2, int blank,
2520 int ignorevars)
2521{
2522 unsigned int string_len = 0;
2523 char *p = string;
2524 register int ch; /* bird: 'optimiziations' */
2525
2526 if (ignorevars)
2527 ignorevars = '$';
2528
2529 while (1)
2530 {
2531 if (stop2 && blank)
2532 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2
2533 && ! isblank ((unsigned char) ch))
2534 ++p;
2535 else if (stop2)
2536 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2)
2537 ++p;
2538 else if (blank)
2539 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1
2540 && ! isblank ((unsigned char) ch))
2541 ++p;
2542 else
2543 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1)
2544 ++p;
2545
2546 if (ch == '\0')
2547 break;
2548
2549 /* If we stopped due to a variable reference, skip over its contents. */
2550 if (ch == ignorevars)
2551 {
2552 char openparen = p[1];
2553
2554 p += 2;
2555
2556 /* Skip the contents of a non-quoted, multi-char variable ref. */
2557 if (openparen == '(' || openparen == '{')
2558 {
2559 unsigned int pcount = 1;
2560 char closeparen = (openparen == '(' ? ')' : '}');
2561
2562 while ((ch = *p))
2563 {
2564 if (ch == openparen)
2565 ++pcount;
2566 else if (ch == closeparen)
2567 if (--pcount == 0)
2568 {
2569 ++p;
2570 break;
2571 }
2572 ++p;
2573 }
2574 }
2575
2576 /* Skipped the variable reference: look for STOPCHARS again. */
2577 continue;
2578 }
2579
2580 if (p > string && p[-1] == '\\')
2581 {
2582 /* Search for more backslashes. */
2583 int i = -2;
2584 while (&p[i] >= string && p[i] == '\\')
2585 --i;
2586 ++i;
2587 /* Only compute the length if really needed. */
2588 if (string_len == 0)
2589 string_len = strlen (string);
2590 /* The number of backslashes is now -I.
2591 Copy P over itself to swallow half of them. */
2592 memmove (&p[i], &p[i/2], (string_len - (p - string)) - (i/2) + 1);
2593 p += i/2;
2594 if (i % 2 == 0)
2595 /* All the backslashes quoted each other; the STOPCHAR was
2596 unquoted. */
2597 return p;
2598
2599 /* The STOPCHAR was quoted by a backslash. Look for another. */
2600 }
2601 else
2602 /* No backslash in sight. */
2603 return p;
2604 }
2605
2606 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2607 return 0;
2608}
2609
2610/* Search PATTERN for an unquoted % and handle quoting. */
2611
2612char *
2613find_percent (char *pattern)
2614{
2615 return find_char_unquote (pattern, '%', 0, 0, 0);
2616}
2617
2618/* Search STRING for an unquoted % and handle quoting. Returns a pointer to
2619 the % or NULL if no % was found.
2620 This version is used with strings in the string cache: if there's a need to
2621 modify the string a new version will be added to the string cache and
2622 *STRING will be set to that. */
2623
2624const char *
2625find_percent_cached (const char **string)
2626{
2627 const char *p = *string;
2628 char *new = 0;
2629 int slen;
2630
2631 /* If the first char is a % return now. This lets us avoid extra tests
2632 inside the loop. */
2633 if (*p == '%')
2634 return p;
2635
2636 while (1)
2637 {
2638 while (*p != '\0' && *p != '%')
2639 ++p;
2640
2641 if (*p == '\0')
2642 break;
2643
2644 /* See if this % is escaped with a backslash; if not we're done. */
2645 if (p[-1] != '\\')
2646 break;
2647
2648 {
2649 /* Search for more backslashes. */
2650 char *pv;
2651 int i = -2;
2652
2653 while (&p[i] >= *string && p[i] == '\\')
2654 --i;
2655 ++i;
2656
2657 /* At this point we know we'll need to allocate a new string.
2658 Make a copy if we haven't yet done so. */
2659 if (! new)
2660 {
2661 slen = strlen (*string);
2662 new = alloca (slen + 1);
2663 memcpy (new, *string, slen + 1);
2664 p = new + (p - *string);
2665 *string = new;
2666 }
2667
2668 /* At this point *string, p, and new all point into the same string.
2669 Get a non-const version of p so we can modify new. */
2670 pv = new + (p - *string);
2671
2672 /* The number of backslashes is now -I.
2673 Copy P over itself to swallow half of them. */
2674 memmove (&pv[i], &pv[i/2], (slen - (pv - new)) - (i/2) + 1);
2675 p += i/2;
2676
2677 /* If the backslashes quoted each other; the % was unquoted. */
2678 if (i % 2 == 0)
2679 break;
2680 }
2681 }
2682
2683 /* If we had to change STRING, add it to the strcache. */
2684 if (new)
2685 {
2686 *string = strcache_add (*string);
2687 p = *string + (p - new);
2688 }
2689
2690 /* If we didn't find a %, return NULL. Otherwise return a ptr to it. */
2691 return (*p == '\0') ? NULL : p;
2692}
2693
2694
2695/* Parse a string into a sequence of filenames represented as a
2696 chain of struct nameseq's in reverse order and return that chain.
2697
2698 The string is passed as STRINGP, the address of a string pointer.
2699 The string pointer is updated to point at the first character
2700 not parsed, which either is a null char or equals STOPCHAR.
2701
2702 SIZE is how big to construct chain elements.
2703 This is useful if we want them actually to be other structures
2704 that have room for additional info.
2705
2706 If STRIP is nonzero, strip `./'s off the beginning. */
2707
2708struct nameseq *
2709parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
2710{
2711 struct nameseq *new = 0;
2712 struct nameseq *new1;
2713#ifndef NO_ARCHIVES /* bird: MSC warning */
2714 struct nameseq *lastnew1;
2715#endif
2716 char *p = *stringp;
2717
2718#ifdef VMS
2719# define VMS_COMMA ','
2720#else
2721# define VMS_COMMA 0
2722#endif
2723
2724 while (1)
2725 {
2726 const char *name;
2727 char *q;
2728
2729 /* Skip whitespace; see if any more names are left. */
2730 p = next_token (p);
2731 if (*p == '\0')
2732 break;
2733 if (*p == stopchar)
2734 break;
2735
2736 /* There are, so find the end of the next name. */
2737 q = p;
2738 p = find_char_unquote (q, stopchar, VMS_COMMA, 1, 0);
2739#ifdef VMS
2740 /* convert comma separated list to space separated */
2741 if (p && *p == ',')
2742 *p =' ';
2743#endif
2744#ifdef _AMIGA
2745 if (stopchar == ':' && p && *p == ':'
2746 && !(isspace ((unsigned char)p[1]) || !p[1]
2747 || isspace ((unsigned char)p[-1])))
2748 p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1, 0);
2749#endif
2750#ifdef HAVE_DOS_PATHS
2751 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
2752 first colon which isn't followed by a slash or a backslash.
2753 Note that tokens separated by spaces should be treated as separate
2754 tokens since make doesn't allow path names with spaces */
2755 if (stopchar == ':')
2756 while (p != 0 && !isspace ((unsigned char)*p) &&
2757 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
2758 p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1, 0);
2759#endif
2760 if (p == 0)
2761 p = q + strlen (q);
2762
2763 if (strip)
2764#ifdef VMS
2765 /* Skip leading `[]'s. */
2766 while (p - q > 2 && q[0] == '[' && q[1] == ']')
2767#else
2768 /* Skip leading `./'s. */
2769 while (p - q > 2 && q[0] == '.' && q[1] == '/')
2770#endif
2771 {
2772 q += 2; /* Skip "./". */
2773 while (q < p && *q == '/')
2774 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
2775 ++q;
2776 }
2777
2778 /* Extract the filename just found, and skip it. */
2779
2780 if (q == p)
2781 /* ".///" was stripped to "". */
2782#if defined(VMS)
2783 continue;
2784#elif defined(_AMIGA)
2785 name = "";
2786#else
2787 name = "./";
2788#endif
2789 else
2790#ifdef VMS
2791/* VMS filenames can have a ':' in them but they have to be '\'ed but we need
2792 * to remove this '\' before we can use the filename.
2793 * Savestring called because q may be read-only string constant.
2794 */
2795 {
2796 char *qbase = xstrdup (q);
2797 char *pbase = qbase + (p-q);
2798 char *q1 = qbase;
2799 char *q2 = q1;
2800 char *p1 = pbase;
2801
2802 while (q1 != pbase)
2803 {
2804 if (*q1 == '\\' && *(q1+1) == ':')
2805 {
2806 q1++;
2807 p1--;
2808 }
2809 *q2++ = *q1++;
2810 }
2811 name = strcache_add_len (qbase, p1 - qbase);
2812 free (qbase);
2813 }
2814#else
2815 name = strcache_add_len (q, p - q);
2816#endif
2817
2818 /* Add it to the front of the chain. */
2819 new1 = xmalloc (size);
2820 new1->name = name;
2821 new1->next = new;
2822 new = new1;
2823 }
2824
2825#ifndef NO_ARCHIVES
2826
2827 /* Look for multi-word archive references.
2828 They are indicated by a elt ending with an unmatched `)' and
2829 an elt further down the chain (i.e., previous in the file list)
2830 with an unmatched `(' (e.g., "lib(mem"). */
2831
2832 new1 = new;
2833 lastnew1 = 0;
2834 while (new1 != 0)
2835 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2836 && new1->name[strlen (new1->name) - 1] == ')'
2837 && strchr (new1->name, '(') == 0)
2838 {
2839 /* NEW1 ends with a `)' but does not contain a `('.
2840 Look back for an elt with an opening `(' but no closing `)'. */
2841
2842 struct nameseq *n = new1->next, *lastn = new1;
2843 char *paren = 0;
2844 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2845 {
2846 lastn = n;
2847 n = n->next;
2848 }
2849 if (n != 0
2850 /* Ignore something starting with `(', as that cannot actually
2851 be an archive-member reference (and treating it as such
2852 results in an empty file name, which causes much lossage). */
2853 && n->name[0] != '(')
2854 {
2855 /* N is the first element in the archive group.
2856 Its name looks like "lib(mem" (with no closing `)'). */
2857
2858 char *libname;
2859
2860 /* Copy "lib(" into LIBNAME. */
2861 ++paren;
2862 libname = alloca (paren - n->name + 1);
2863 memcpy (libname, n->name, paren - n->name);
2864 libname[paren - n->name] = '\0';
2865
2866 if (*paren == '\0')
2867 {
2868 /* N was just "lib(", part of something like "lib( a b)".
2869 Edit it out of the chain and free its storage. */
2870 lastn->next = n->next;
2871 free (n);
2872 /* LASTN->next is the new stopping elt for the loop below. */
2873 n = lastn->next;
2874 }
2875 else
2876 {
2877 /* Replace N's name with the full archive reference. */
2878 n->name = strcache_add (concat (libname, paren, ")"));
2879 }
2880
2881 if (new1->name[1] == '\0')
2882 {
2883 /* NEW1 is just ")", part of something like "lib(a b )".
2884 Omit it from the chain and free its storage. */
2885 if (lastnew1 == 0)
2886 new = new1->next;
2887 else
2888 lastnew1->next = new1->next;
2889 lastn = new1;
2890 new1 = new1->next;
2891 free (lastn);
2892 }
2893 else
2894 {
2895 /* Replace also NEW1->name, which already has closing `)'. */
2896 new1->name = strcache_add (concat (libname, new1->name, ""));
2897 new1 = new1->next;
2898 }
2899
2900 /* Trace back from NEW1 (the end of the list) until N
2901 (the beginning of the list), rewriting each name
2902 with the full archive reference. */
2903
2904 while (new1 != n)
2905 {
2906 new1->name = strcache_add (concat (libname, new1->name, ")"));
2907 lastnew1 = new1;
2908 new1 = new1->next;
2909 }
2910 }
2911 else
2912 {
2913 /* No frobnication happening. Just step down the list. */
2914 lastnew1 = new1;
2915 new1 = new1->next;
2916 }
2917 }
2918 else
2919 {
2920 lastnew1 = new1;
2921 new1 = new1->next;
2922 }
2923
2924#endif
2925
2926 *stringp = p;
2927 return new;
2928}
2929
2930
2931/* Find the next line of text in an eval buffer, combining continuation lines
2932 into one line.
2933 Return the number of actual lines read (> 1 if continuation lines).
2934 Returns -1 if there's nothing left in the buffer.
2935
2936 After this function, ebuf->buffer points to the first character of the
2937 line we just found.
2938 */
2939
2940/* Read a line of text from a STRING.
2941 Since we aren't really reading from a file, don't bother with linenumbers.
2942 */
2943
2944static unsigned long
2945readstring (struct ebuffer *ebuf)
2946{
2947 char *eol;
2948
2949 /* If there is nothing left in this buffer, return 0. */
2950 if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
2951 return -1;
2952
2953 /* Set up a new starting point for the buffer, and find the end of the
2954 next logical line (taking into account backslash/newline pairs). */
2955
2956 eol = ebuf->buffer = ebuf->bufnext;
2957
2958 while (1)
2959 {
2960 int backslash = 0;
2961 char *bol = eol;
2962 char *p;
2963
2964 /* Find the next newline. At EOS, stop. */
2965 eol = p = strchr (eol , '\n');
2966 if (!eol)
2967 {
2968 ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
2969 return 0;
2970 }
2971
2972 /* Found a newline; if it's escaped continue; else we're done. */
2973 while (p > bol && *(--p) == '\\')
2974 backslash = !backslash;
2975 if (!backslash)
2976 break;
2977 ++eol;
2978 }
2979
2980 /* Overwrite the newline char. */
2981 *eol = '\0';
2982 ebuf->bufnext = eol+1;
2983
2984 return 0;
2985}
2986
2987static long
2988readline (struct ebuffer *ebuf)
2989{
2990 char *p;
2991 char *end;
2992 char *start;
2993 long nlines = 0;
2994
2995 /* The behaviors between string and stream buffers are different enough to
2996 warrant different functions. Do the Right Thing. */
2997
2998 if (!ebuf->fp)
2999 return readstring (ebuf);
3000
3001 /* When reading from a file, we always start over at the beginning of the
3002 buffer for each new line. */
3003
3004 p = start = ebuf->bufstart;
3005 end = p + ebuf->size;
3006 *p = '\0';
3007
3008 while (fgets (p, end - p, ebuf->fp) != 0)
3009 {
3010 char *p2;
3011 unsigned long len;
3012 int backslash;
3013
3014 len = strlen (p);
3015 if (len == 0)
3016 {
3017 /* This only happens when the first thing on the line is a '\0'.
3018 It is a pretty hopeless case, but (wonder of wonders) Athena
3019 lossage strikes again! (xmkmf puts NULs in its makefiles.)
3020 There is nothing really to be done; we synthesize a newline so
3021 the following line doesn't appear to be part of this line. */
3022 error (&ebuf->floc,
3023 _("warning: NUL character seen; rest of line ignored"));
3024 p[0] = '\n';
3025 len = 1;
3026 }
3027
3028 /* Jump past the text we just read. */
3029 p += len;
3030
3031 /* If the last char isn't a newline, the whole line didn't fit into the
3032 buffer. Get some more buffer and try again. */
3033 if (p[-1] != '\n')
3034 goto more_buffer;
3035
3036 /* We got a newline, so add one to the count of lines. */
3037 ++nlines;
3038
3039#if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
3040 /* Check to see if the line was really ended with CRLF; if so ignore
3041 the CR. */
3042 if ((p - start) > 1 && p[-2] == '\r')
3043 {
3044 --p;
3045 p[-1] = '\n';
3046 }
3047#endif
3048
3049 backslash = 0;
3050 for (p2 = p - 2; p2 >= start; --p2)
3051 {
3052 if (*p2 != '\\')
3053 break;
3054 backslash = !backslash;
3055 }
3056
3057 if (!backslash)
3058 {
3059 p[-1] = '\0';
3060 break;
3061 }
3062
3063 /* It was a backslash/newline combo. If we have more space, read
3064 another line. */
3065 if (end - p >= 80)
3066 continue;
3067
3068 /* We need more space at the end of our buffer, so realloc it.
3069 Make sure to preserve the current offset of p. */
3070 more_buffer:
3071 {
3072 unsigned long off = p - start;
3073 ebuf->size *= 2;
3074 start = ebuf->buffer = ebuf->bufstart = xrealloc (start, ebuf->size);
3075 p = start + off;
3076 end = start + ebuf->size;
3077 *p = '\0';
3078 }
3079 }
3080
3081 if (ferror (ebuf->fp))
3082 pfatal_with_name (ebuf->floc.filenm);
3083
3084 /* If we found some lines, return how many.
3085 If we didn't, but we did find _something_, that indicates we read the last
3086 line of a file with no final newline; return 1.
3087 If we read nothing, we're at EOF; return -1. */
3088
3089 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
3090}
3091
3092
3093/* Parse the next "makefile word" from the input buffer, and return info
3094 about it.
3095
3096 A "makefile word" is one of:
3097
3098 w_bogus Should never happen
3099 w_eol End of input
3100 w_static A static word; cannot be expanded
3101 w_variable A word containing one or more variables/functions
3102 w_colon A colon
3103 w_dcolon A double-colon
3104 w_semicolon A semicolon
3105 w_varassign A variable assignment operator (=, :=, +=, >=, or ?=)
3106
3107 Note that this function is only used when reading certain parts of the
3108 makefile. Don't use it where special rules hold sway (RHS of a variable,
3109 in a command list, etc.) */
3110
3111static enum make_word_type
3112get_next_mword (char *buffer, char *delim, char **startp, unsigned int *length)
3113{
3114 enum make_word_type wtype = w_bogus;
3115 char *p = buffer, *beg;
3116 char c;
3117
3118 /* Skip any leading whitespace. */
3119 while (isblank ((unsigned char)*p))
3120 ++p;
3121
3122 beg = p;
3123 c = *(p++);
3124 switch (c)
3125 {
3126 case '\0':
3127 wtype = w_eol;
3128 break;
3129
3130 case ';':
3131 wtype = w_semicolon;
3132 break;
3133
3134 case '=':
3135 wtype = w_varassign;
3136 break;
3137
3138 case ':':
3139 wtype = w_colon;
3140 switch (*p)
3141 {
3142 case ':':
3143 ++p;
3144 wtype = w_dcolon;
3145 break;
3146
3147 case '=':
3148 ++p;
3149 wtype = w_varassign;
3150 break;
3151 }
3152 break;
3153
3154 case '+':
3155 case '?':
3156#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3157 case '>':
3158#endif
3159 if (*p == '=')
3160 {
3161 ++p;
3162 wtype = w_varassign;
3163 break;
3164 }
3165
3166 default:
3167 if (delim && strchr (delim, c))
3168 wtype = w_static;
3169 break;
3170 }
3171
3172 /* Did we find something? If so, return now. */
3173 if (wtype != w_bogus)
3174 goto done;
3175
3176 /* This is some non-operator word. A word consists of the longest
3177 string of characters that doesn't contain whitespace, one of [:=#],
3178 or [?+]=, or one of the chars in the DELIM string. */
3179
3180 /* We start out assuming a static word; if we see a variable we'll
3181 adjust our assumptions then. */
3182 wtype = w_static;
3183
3184 /* We already found the first value of "c", above. */
3185 while (1)
3186 {
3187 char closeparen;
3188 int count;
3189
3190 switch (c)
3191 {
3192 case '\0':
3193 case ' ':
3194 case '\t':
3195 case '=':
3196 goto done_word;
3197
3198 case ':':
3199#ifdef HAVE_DOS_PATHS
3200 /* A word CAN include a colon in its drive spec. The drive
3201 spec is allowed either at the beginning of a word, or as part
3202 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
3203 if (!(p - beg >= 2
3204 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
3205 && (p - beg == 2 || p[-3] == '(')))
3206#endif
3207 goto done_word;
3208
3209 case '$':
3210 c = *(p++);
3211 if (c == '$')
3212 break;
3213
3214 /* This is a variable reference, so note that it's expandable.
3215 Then read it to the matching close paren. */
3216 wtype = w_variable;
3217
3218 if (c == '(')
3219 closeparen = ')';
3220 else if (c == '{')
3221 closeparen = '}';
3222 else
3223 /* This is a single-letter variable reference. */
3224 break;
3225
3226 for (count=0; *p != '\0'; ++p)
3227 {
3228 if (*p == c)
3229 ++count;
3230 else if (*p == closeparen && --count < 0)
3231 {
3232 ++p;
3233 break;
3234 }
3235 }
3236 break;
3237
3238 case '?':
3239 case '+':
3240#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3241 case '>':
3242#endif
3243 if (*p == '=')
3244 goto done_word;
3245 break;
3246
3247 case '\\':
3248 switch (*p)
3249 {
3250 case ':':
3251 case ';':
3252 case '=':
3253 case '\\':
3254 ++p;
3255 break;
3256 }
3257 break;
3258
3259 default:
3260 if (delim && strchr (delim, c))
3261 goto done_word;
3262 break;
3263 }
3264
3265 c = *(p++);
3266 }
3267 done_word:
3268 --p;
3269
3270 done:
3271 if (startp)
3272 *startp = beg;
3273 if (length)
3274 *length = p - beg;
3275 return wtype;
3276}
3277
3278
3279/* Construct the list of include directories
3280 from the arguments and the default list. */
3281
3282void
3283construct_include_path (const char **arg_dirs)
3284{
3285#ifdef VAXC /* just don't ask ... */
3286 stat_t stbuf;
3287#else
3288 struct stat stbuf;
3289#endif
3290 const char **dirs;
3291 const char **cpp;
3292 unsigned int idx;
3293
3294 /* Compute the number of pointers we need in the table. */
3295 idx = sizeof (default_include_directories) / sizeof (const char *);
3296 if (arg_dirs)
3297 for (cpp = arg_dirs; *cpp != 0; ++cpp)
3298 ++idx;
3299
3300#ifdef __MSDOS__
3301 /* Add one for $DJDIR. */
3302 ++idx;
3303#endif
3304#ifdef KMK
3305 /* Add one for the kBuild directory. */
3306 ++idx;
3307#endif
3308
3309 dirs = xmalloc (idx * sizeof (const char *));
3310
3311 idx = 0;
3312 max_incl_len = 0;
3313
3314 /* First consider any dirs specified with -I switches.
3315 Ignore any that don't exist. Remember the maximum string length. */
3316
3317 if (arg_dirs)
3318 while (*arg_dirs != 0)
3319 {
3320 const char *dir = *(arg_dirs++);
3321 char *expanded = 0;
3322 int e;
3323
3324 if (dir[0] == '~')
3325 {
3326 expanded = tilde_expand (dir);
3327 if (expanded != 0)
3328 dir = expanded;
3329 }
3330
3331 EINTRLOOP (e, stat (dir, &stbuf));
3332 if (e == 0 && S_ISDIR (stbuf.st_mode))
3333 {
3334 unsigned int len = strlen (dir);
3335 /* If dir name is written with trailing slashes, discard them. */
3336 while (len > 1 && dir[len - 1] == '/')
3337 --len;
3338 if (len > max_incl_len)
3339 max_incl_len = len;
3340 dirs[idx++] = strcache_add_len (dir, len);
3341 }
3342
3343 if (expanded)
3344 free (expanded);
3345 }
3346
3347 /* Now add the standard default dirs at the end. */
3348
3349#ifdef __MSDOS__
3350 {
3351 /* The environment variable $DJDIR holds the root of the DJGPP directory
3352 tree; add ${DJDIR}/include. */
3353 struct variable *djdir = lookup_variable ("DJDIR", 5);
3354
3355 if (djdir)
3356 {
3357 unsigned int len = strlen (djdir->value) + 8;
3358 char *defdir = alloca (len + 1);
3359
3360 strcat (strcpy (defdir, djdir->value), "/include");
3361 dirs[idx++] = strcache_add (defdir);
3362
3363 if (len > max_incl_len)
3364 max_incl_len = len;
3365 }
3366 }
3367#endif
3368#ifdef KMK
3369 /* Add $(PATH_KBUILD). */
3370 {
3371 size_t len = strlen (get_path_kbuild ());
3372 dirs[idx++] = strcache_add_len (get_path_kbuild (), len);
3373 if (len > max_incl_len)
3374 max_incl_len = len;
3375 }
3376#endif
3377
3378 for (cpp = default_include_directories; *cpp != 0; ++cpp)
3379 {
3380 int e;
3381
3382 EINTRLOOP (e, stat (*cpp, &stbuf));
3383 if (e == 0 && S_ISDIR (stbuf.st_mode))
3384 {
3385 unsigned int len = strlen (*cpp);
3386 /* If dir name is written with trailing slashes, discard them. */
3387 while (len > 1 && (*cpp)[len - 1] == '/')
3388 --len;
3389 if (len > max_incl_len)
3390 max_incl_len = len;
3391 dirs[idx++] = strcache_add_len (*cpp, len - 1);
3392 }
3393 }
3394
3395 dirs[idx] = 0;
3396
3397 /* Now add each dir to the .INCLUDE_DIRS variable. */
3398
3399 for (cpp = dirs; *cpp != 0; ++cpp)
3400 do_variable_definition (NILF, ".INCLUDE_DIRS", *cpp,
3401 o_default, f_append, 0);
3402
3403 include_directories = dirs;
3404}
3405
3406
3407/* Expand ~ or ~USER at the beginning of NAME.
3408 Return a newly malloc'd string or 0. */
3409
3410char *
3411tilde_expand (const char *name)
3412{
3413#ifndef VMS
3414 if (name[1] == '/' || name[1] == '\0')
3415 {
3416 extern char *getenv ();
3417 char *home_dir;
3418 int is_variable;
3419
3420 {
3421 /* Turn off --warn-undefined-variables while we expand HOME. */
3422 int save = warn_undefined_variables_flag;
3423 warn_undefined_variables_flag = 0;
3424
3425 home_dir = allocated_variable_expand ("$(HOME)");
3426
3427 warn_undefined_variables_flag = save;
3428 }
3429
3430 is_variable = home_dir[0] != '\0';
3431 if (!is_variable)
3432 {
3433 free (home_dir);
3434 home_dir = getenv ("HOME");
3435 }
3436# if !defined(_AMIGA) && !defined(WINDOWS32)
3437 if (home_dir == 0 || home_dir[0] == '\0')
3438 {
3439 extern char *getlogin ();
3440 char *logname = getlogin ();
3441 home_dir = 0;
3442 if (logname != 0)
3443 {
3444 struct passwd *p = getpwnam (logname);
3445 if (p != 0)
3446 home_dir = p->pw_dir;
3447 }
3448 }
3449# endif /* !AMIGA && !WINDOWS32 */
3450 if (home_dir != 0)
3451 {
3452 char *new = xstrdup (concat (home_dir, "", name + 1));
3453 if (is_variable)
3454 free (home_dir);
3455 return new;
3456 }
3457 }
3458# if !defined(_AMIGA) && !defined(WINDOWS32)
3459 else
3460 {
3461 struct passwd *pwent;
3462 char *userend = strchr (name + 1, '/');
3463 if (userend != 0)
3464 *userend = '\0';
3465 pwent = getpwnam (name + 1);
3466 if (pwent != 0)
3467 {
3468 if (userend == 0)
3469 return xstrdup (pwent->pw_dir);
3470 else
3471 return xstrdup (concat (pwent->pw_dir, "/", userend + 1));
3472 }
3473 else if (userend != 0)
3474 *userend = '/';
3475 }
3476# endif /* !AMIGA && !WINDOWS32 */
3477#endif /* !VMS */
3478 return 0;
3479}
3480
3481/* Given a chain of struct nameseq's describing a sequence of filenames,
3482 in reverse of the intended order, return a new chain describing the
3483 result of globbing the filenames. The new chain is in forward order.
3484 The links of the old chain are freed or used in the new chain.
3485 Likewise for the names in the old chain.
3486
3487 SIZE is how big to construct chain elements.
3488 This is useful if we want them actually to be other structures
3489 that have room for additional info. */
3490
3491struct nameseq *
3492multi_glob (struct nameseq *chain, unsigned int size)
3493{
3494 void dir_setup_glob (glob_t *);
3495 struct nameseq *new = 0;
3496 struct nameseq *old;
3497 struct nameseq *nexto;
3498 glob_t gl;
3499#if defined(KMK) || defined(__EMX__) /* speed optimization */
3500 int rc;
3501#endif
3502
3503 dir_setup_glob (&gl);
3504
3505 for (old = chain; old != 0; old = nexto)
3506 {
3507 const char *gname;
3508#ifndef NO_ARCHIVES
3509 char *arname = 0;
3510 char *memname = 0;
3511#endif
3512 nexto = old->next;
3513 gname = old->name;
3514
3515 if (gname[0] == '~')
3516 {
3517 char *newname = tilde_expand (old->name);
3518 if (newname != 0)
3519 gname = newname;
3520 }
3521
3522#ifndef NO_ARCHIVES
3523 if (ar_name (gname))
3524 {
3525 /* OLD->name is an archive member reference. Replace it with the
3526 archive file name, and save the member name in MEMNAME. We will
3527 glob on the archive name and then reattach MEMNAME later. */
3528 ar_parse_name (gname, &arname, &memname);
3529 gname = arname;
3530 }
3531#endif /* !NO_ARCHIVES */
3532
3533#if defined(KMK) || defined(__EMX__) /* speed optimization */
3534 if (!strpbrk(gname, "*?["))
3535 {
3536 gl.gl_pathc = 1;
3537 gl.gl_pathv = (char **)&gname;
3538 rc = 0;
3539 }
3540 else
3541 rc = glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl);
3542 switch (rc)
3543#else
3544 switch (glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
3545#endif
3546 {
3547 case 0: /* Success. */
3548 {
3549 int i = gl.gl_pathc;
3550 while (i-- > 0)
3551 {
3552#ifndef NO_ARCHIVES
3553 if (memname != 0)
3554 {
3555 /* Try to glob on MEMNAME within the archive. */
3556 struct nameseq *found
3557 = ar_glob (gl.gl_pathv[i], memname, size);
3558 if (! found)
3559 {
3560 /* No matches. Use MEMNAME as-is. */
3561 unsigned int alen = strlen (gl.gl_pathv[i]);
3562 unsigned int mlen = strlen (memname);
3563 char *name;
3564 struct nameseq *elt = xmalloc (size);
3565 memset (elt, '\0', size);
3566
3567 name = alloca (alen + 1 + mlen + 2);
3568 memcpy (name, gl.gl_pathv[i], alen);
3569 name[alen] = '(';
3570 memcpy (name+alen+1, memname, mlen);
3571 name[alen + 1 + mlen] = ')';
3572 name[alen + 1 + mlen + 1] = '\0';
3573 elt->name = strcache_add (name);
3574 elt->next = new;
3575 new = elt;
3576 }
3577 else
3578 {
3579 /* Find the end of the FOUND chain. */
3580 struct nameseq *f = found;
3581 while (f->next != 0)
3582 f = f->next;
3583
3584 /* Attach the chain being built to the end of the FOUND
3585 chain, and make FOUND the new NEW chain. */
3586 f->next = new;
3587 new = found;
3588 }
3589 }
3590 else
3591#endif /* !NO_ARCHIVES */
3592 {
3593 struct nameseq *elt = xmalloc (size);
3594 memset (elt, '\0', size);
3595 elt->name = strcache_add (gl.gl_pathv[i]);
3596 elt->next = new;
3597 new = elt;
3598 }
3599 }
3600#if defined(KMK) || defined(__EMX__) /* speed optimization */
3601 if (gl.gl_pathv != (char **)&gname)
3602#endif
3603 globfree (&gl);
3604 free (old);
3605 break;
3606 }
3607
3608 case GLOB_NOSPACE:
3609 fatal (NILF, _("virtual memory exhausted"));
3610 break;
3611
3612 default:
3613 old->next = new;
3614 new = old;
3615 break;
3616 }
3617
3618#ifndef NO_ARCHIVES
3619 if (arname)
3620 free (arname);
3621#endif
3622 }
3623
3624 return new;
3625}
Note: See TracBrowser for help on using the repository browser.