VirtualBox

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

Last change on this file since 2040 was 2040, checked in by bird, 17 years ago

Another try.

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

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