VirtualBox

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

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

kmk: recycle more allocate_variable_expand_* results.

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