VirtualBox

source: kBuild/trunk/src/kmk/read.c@ 2016

Last change on this file since 2016 was 1993, checked in by bird, 16 years ago

Merged in current GNU Make code (CVS from 2008-10-28). Ref #55.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette