VirtualBox

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

Last change on this file since 2091 was 2056, checked in by bird, 16 years ago

kmk: some MBs of memory during building by freeing up the chopped up command lines after we're done with them. (Code not perfect, but wtf., it saves me 7 MBs (out of 45), a bunch of faults and turns out to using less cpu time...)

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

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