VirtualBox

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

Last change on this file since 2426 was 2426, checked in by bird, 14 years ago

kmk/read.c: Must adjust s1 after calling variable_expand_string_2 to produce s2 in conditonal_line. Fixes #98.

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

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