VirtualBox

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

最後變更 在這個檔案從2421是 2401,由 bird 提交於 15 年 前

kmk: corrected bad setvbuf call for zero-sized files.

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

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