VirtualBox

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

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

read.c: fixed regression from previous commit.

  • Property svn:eol-style set to native
File size: 115.0 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#ifndef CONFIG_WITH_STRCACHE2
2448 int len = strlen(v->name);
2449#else
2450 int len = !percent
2451 ? strcache2_get_len (&variable_strcache, v->name)
2452 : strlen(v->name);
2453#endif
2454
2455 gv = lookup_variable (v->name, len);
2456 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
2457 {
2458#ifdef CONFIG_WITH_RDONLY_VARIABLE_VALUE
2459 assert (!v->rdonly_val); /* paranoia */
2460#endif
2461 if (v->value != 0)
2462 free (v->value);
2463#ifndef CONFIG_WITH_VALUE_LENGTH
2464 v->value = xstrdup (gv->value);
2465#else
2466 v->value = savestring (gv->value, gv->value_length);
2467 v->value_length = gv->value_length;
2468#endif
2469 v->origin = gv->origin;
2470 v->recursive = gv->recursive;
2471 v->append = 0;
2472 }
2473 }
2474 }
2475}
2476
2477
2478/* Record a description line for files FILENAMES,
2479 with dependencies DEPS, commands to execute described
2480 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
2481 TWO_COLON is nonzero if a double colon was used.
2482 If not nil, PATTERN is the `%' pattern to make this
2483 a static pattern rule, and PATTERN_PERCENT is a pointer
2484 to the `%' within it.
2485
2486 The links of FILENAMES are freed, and so are any names in it
2487 that are not incorporated into other data structures. */
2488
2489#ifndef CONFIG_WITH_INCLUDEDEP
2490static void
2491#else
2492void
2493#endif
2494record_files (struct nameseq *filenames, const char *pattern,
2495 const char *pattern_percent, struct dep *deps,
2496 unsigned int cmds_started, char *commands,
2497 unsigned int commands_idx, int two_colon,
2498 const struct floc *flocp)
2499{
2500 struct nameseq *nextf;
2501 int implicit = 0;
2502 unsigned int max_targets = 0, target_idx = 0;
2503 const char **targets = 0, **target_percents = 0;
2504 struct commands *cmds;
2505#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2506 struct file *prev_file = 0;
2507 enum multitarget_mode { m_unsettled, m_no, m_yes, m_yes_maybe }
2508 multi_mode = !two_colon && !pattern ? m_unsettled : m_no;
2509#endif
2510
2511 /* If we've already snapped deps, that means we're in an eval being
2512 resolved after the makefiles have been read in. We can't add more rules
2513 at this time, since they won't get snapped and we'll get core dumps.
2514 See Savannah bug # 12124. */
2515 if (snapped_deps)
2516 fatal (flocp, _("prerequisites cannot be defined in command scripts"));
2517
2518 if (commands_idx > 0)
2519 {
2520#ifndef CONFIG_WITH_ALLOC_CACHES
2521 cmds = xmalloc (sizeof (struct commands));
2522#else
2523 cmds = alloccache_alloc (&commands_cache);
2524#endif
2525 cmds->fileinfo.filenm = flocp->filenm;
2526 cmds->fileinfo.lineno = cmds_started;
2527 cmds->commands = savestring (commands, commands_idx);
2528 cmds->command_lines = 0;
2529 }
2530 else
2531 cmds = 0;
2532
2533 for (; filenames != 0; filenames = nextf)
2534 {
2535 const char *name = filenames->name;
2536 struct file *f;
2537 struct dep *this = 0;
2538 const char *implicit_percent;
2539
2540 nextf = filenames->next;
2541#ifndef CONFIG_WITH_ALLOC_CACHES
2542 free (filenames);
2543#else
2544 alloccache_free (&nameseq_cache, filenames);
2545#endif
2546
2547 /* Check for special targets. Do it here instead of, say, snap_deps()
2548 so that we can immediately use the value. */
2549
2550 if (streq (name, ".POSIX"))
2551 posix_pedantic = 1;
2552 else if (streq (name, ".SECONDEXPANSION"))
2553 second_expansion = 1;
2554#ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
2555 else if (streq (name, ".SECONDTARGETEXPANSION"))
2556 second_target_expansion = 1;
2557#endif
2558
2559 implicit_percent = find_percent_cached (&name);
2560 implicit |= implicit_percent != 0;
2561
2562 if (implicit)
2563 {
2564 if (pattern != 0)
2565 fatal (flocp, _("mixed implicit and static pattern rules"));
2566
2567 if (implicit_percent == 0)
2568 fatal (flocp, _("mixed implicit and normal rules"));
2569
2570 if (targets == 0)
2571 {
2572 max_targets = 5;
2573 targets = xmalloc (5 * sizeof (char *));
2574 target_percents = xmalloc (5 * sizeof (char *));
2575 target_idx = 0;
2576 }
2577 else if (target_idx == max_targets - 1)
2578 {
2579 max_targets += 5;
2580 targets = xrealloc ((void *)targets, max_targets * sizeof (char *));
2581 target_percents = xrealloc ((void *)target_percents,
2582 max_targets * sizeof (char *));
2583 }
2584 targets[target_idx] = name;
2585 target_percents[target_idx] = implicit_percent;
2586 ++target_idx;
2587 continue;
2588 }
2589
2590#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2591 /* Check for the explicit multitarget mode operators. For this to be
2592 identified as an explicit multiple target rule, the first + or +|
2593 operator *must* appear between the first two files. If not found as
2594 the 2nd file or if found as the 1st file, the rule will be rejected
2595 as a potential multiple first target rule. For the subsequent files
2596 the operator is only required to switch between maybe and non-maybe
2597 mode:
2598 `primary + 2nd 3rd +| 4th-maybe + 5th-for-sure: deps; cmds'
2599
2600 The whole idea of the maybe-updated files is this:
2601 timestamp +| maybe.h: src1.c src2.c
2602 grep goes-into-maybe.h $* > timestamp
2603 cmp timestamp maybe.h || cp -f timestamp maybe.h
2604
2605 This is implemented in remake.c where we don't consider the mtime of
2606 the maybe-updated targets. */
2607 if (multi_mode != m_no && name[0] == '+'
2608 && (name[1] == '\0' || (name[1] == '|' && name[2] == '\0')))
2609 {
2610 if (!prev_file)
2611 multi_mode = m_no; /* first */
2612 else
2613 {
2614 if (multi_mode == m_unsettled)
2615 {
2616 prev_file->multi_head = prev_file;
2617
2618 /* Only the primary file needs the dependencies. */
2619 if (deps)
2620 {
2621 free_dep_chain (deps);
2622 deps = NULL;
2623 }
2624 }
2625 multi_mode = name[1] == '\0' ? m_yes : m_yes_maybe;
2626 continue;
2627 }
2628 }
2629 else if (multi_mode == m_unsettled && prev_file)
2630 multi_mode = m_no;
2631#endif
2632
2633 /* If this is a static pattern rule:
2634 `targets: target%pattern: dep%pattern; cmds',
2635 make sure the pattern matches this target name. */
2636 if (pattern && !pattern_matches (pattern, pattern_percent, name))
2637 error (flocp, _("target `%s' doesn't match the target pattern"), name);
2638 else if (deps)
2639 {
2640 /* If there are multiple filenames, copy the chain DEPS for all but
2641 the last one. It is not safe for the same deps to go in more
2642 than one place in the database. */
2643 this = nextf != 0 ? copy_dep_chain (deps) : deps;
2644 this->need_2nd_expansion = (second_expansion
2645 && strchr (this->name, '$'));
2646 }
2647
2648 if (!two_colon)
2649 {
2650 /* Single-colon. Combine these dependencies
2651 with others in file's existing record, if any. */
2652#ifndef KMK
2653 f = enter_file (strcache_add (name));
2654#else /* KMK - the name is already in the cache, don't waste time. */
2655 f = enter_file (name);
2656#endif
2657
2658 if (f->double_colon)
2659 fatal (flocp,
2660 _("target file `%s' has both : and :: entries"), f->name);
2661
2662 /* If CMDS == F->CMDS, this target was listed in this rule
2663 more than once. Just give a warning since this is harmless. */
2664 if (cmds != 0 && cmds == f->cmds)
2665 error (flocp,
2666 _("target `%s' given more than once in the same rule."),
2667 f->name);
2668
2669 /* Check for two single-colon entries both with commands.
2670 Check is_target so that we don't lose on files such as .c.o
2671 whose commands were preinitialized. */
2672 else if (cmds != 0 && f->cmds != 0 && f->is_target)
2673 {
2674 error (&cmds->fileinfo,
2675 _("warning: overriding commands for target `%s'"),
2676 f->name);
2677 error (&f->cmds->fileinfo,
2678 _("warning: ignoring old commands for target `%s'"),
2679 f->name);
2680 }
2681
2682 f->is_target = 1;
2683
2684 /* Defining .DEFAULT with no deps or cmds clears it. */
2685 if (f == default_file && this == 0 && cmds == 0)
2686 f->cmds = 0;
2687 if (cmds != 0)
2688 f->cmds = cmds;
2689
2690#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2691 /* If this is an explicit multi target rule, add it to the
2692 target chain and set the multi_maybe flag according to
2693 the current mode. */
2694
2695 if (multi_mode >= m_yes)
2696 {
2697 f->multi_maybe = multi_mode == m_yes_maybe;
2698 prev_file->multi_next = f;
2699 assert (prev_file->multi_head != 0);
2700 f->multi_head = prev_file->multi_head;
2701
2702 if (f == suffix_file)
2703 error (flocp,
2704 _(".SUFFIXES encountered in an explicit multi target rule"));
2705 }
2706 prev_file = f;
2707#endif
2708
2709 /* Defining .SUFFIXES with no dependencies clears out the list of
2710 suffixes. */
2711 if (f == suffix_file && this == 0)
2712 {
2713 free_dep_chain (f->deps);
2714 f->deps = 0;
2715 }
2716 else if (this != 0)
2717 {
2718 /* Add the file's old deps and the new ones in THIS together. */
2719
2720 if (f->deps != 0)
2721 {
2722 struct dep **d_ptr = &f->deps;
2723
2724 while ((*d_ptr)->next != 0)
2725 d_ptr = &(*d_ptr)->next;
2726
2727 if (cmds != 0)
2728 /* This is the rule with commands, so put its deps
2729 last. The rationale behind this is that $< expands to
2730 the first dep in the chain, and commands use $<
2731 expecting to get the dep that rule specifies. However
2732 the second expansion algorithm reverses the order thus
2733 we need to make it last here. */
2734 (*d_ptr)->next = this;
2735 else
2736 {
2737 /* This is the rule without commands. Put its
2738 dependencies at the end but before dependencies from
2739 the rule with commands (if any). This way everything
2740 appears in makefile order. */
2741
2742 if (f->cmds != 0)
2743 {
2744#ifndef KMK /* bugfix: Don't chop the chain! */
2745 this->next = *d_ptr;
2746 *d_ptr = this;
2747#else /* KMK */
2748 struct dep *this_last = this;
2749 while (this_last->next)
2750 this_last = this_last->next;
2751 this_last->next = *d_ptr;
2752 *d_ptr = this;
2753#endif /* KMK */
2754 }
2755 else
2756 (*d_ptr)->next = this;
2757 }
2758 }
2759 else
2760 f->deps = this;
2761
2762 /* This is a hack. I need a way to communicate to snap_deps()
2763 that the last dependency line in this file came with commands
2764 (so that logic in snap_deps() can put it in front and all
2765 this $< -logic works). I cannot simply rely on file->cmds
2766 being not 0 because of the cases like the following:
2767
2768 foo: bar
2769 foo:
2770 ...
2771
2772 I am going to temporarily "borrow" UPDATING member in
2773 `struct file' for this. */
2774
2775 if (cmds != 0)
2776 f->updating = 1;
2777 }
2778 }
2779 else
2780 {
2781 /* Double-colon. Make a new record even if there already is one. */
2782#ifndef KMK
2783 f = lookup_file (name);
2784#else /* KMK - the name is already in the cache, don't waste time. */
2785 f = lookup_file_cached (name);
2786#endif
2787
2788 /* Check for both : and :: rules. Check is_target so
2789 we don't lose on default suffix rules or makefiles. */
2790 if (f != 0 && f->is_target && !f->double_colon)
2791 fatal (flocp,
2792 _("target file `%s' has both : and :: entries"), f->name);
2793#ifndef KMK
2794 f = enter_file (strcache_add (name));
2795#else /* KMK - the name is already in the cache, don't waste time. */
2796 f = enter_file (name);
2797#endif
2798 /* If there was an existing entry and it was a double-colon entry,
2799 enter_file will have returned a new one, making it the prev
2800 pointer of the old one, and setting its double_colon pointer to
2801 the first one. */
2802 if (f->double_colon == 0)
2803 /* This is the first entry for this name, so we must set its
2804 double_colon pointer to itself. */
2805 f->double_colon = f;
2806 f->is_target = 1;
2807 f->deps = this;
2808 f->cmds = cmds;
2809 }
2810
2811 /* If this is a static pattern rule, set the stem to the part of its
2812 name that matched the `%' in the pattern, so you can use $* in the
2813 commands. */
2814 if (pattern)
2815 {
2816 static const char *percent = "%";
2817 char *buffer = variable_expand ("");
2818 const size_t buffer_offset = buffer - variable_buffer; /* bird */
2819 char *o = patsubst_expand_pat (buffer, name, pattern, percent,
2820 pattern_percent+1, percent+1);
2821 buffer = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. */
2822 f->stem = strcache_add_len (buffer, o - buffer);
2823 if (this)
2824 {
2825 this->staticpattern = 1;
2826 this->stem = f->stem;
2827 }
2828 }
2829
2830 name = f->name;
2831
2832 /* If this target is a default target, update DEFAULT_GOAL_FILE. */
2833 if (streq (*default_goal_name, name)
2834 && (default_goal_file == 0
2835 || ! streq (default_goal_file->name, name)))
2836 default_goal_file = f;
2837 }
2838
2839 if (implicit)
2840 {
2841 if (deps)
2842 deps->need_2nd_expansion = second_expansion;
2843 create_pattern_rule (targets, target_percents, target_idx,
2844 two_colon, deps, cmds, 1);
2845 }
2846}
2847
2848
2849/* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2850 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2851 Quoting backslashes are removed from STRING by compacting it into
2852 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2853 one, or nil if there are none. STOPCHARs inside variable references are
2854 ignored if IGNOREVARS is true.
2855
2856 STOPCHAR _cannot_ be '$' if IGNOREVARS is true. */
2857
2858#ifndef CONFIG_WITH_VALUE_LENGTH
2859static char *
2860find_char_unquote (char *string, int stop1, int stop2, int blank,
2861 int ignorevars)
2862#else
2863static char *
2864find_char_unquote_2 (char *string, int stop1, int stop2, int blank,
2865 int ignorevars, unsigned int string_len)
2866#endif
2867{
2868#ifndef CONFIG_WITH_VALUE_LENGTH
2869 unsigned int string_len = 0;
2870#endif
2871 char *p = string;
2872 register int ch; /* bird: 'optimiziations' */
2873#ifdef CONFIG_WITH_VALUE_LENGTH
2874 assert (string_len == 0 || string_len == strlen (string));
2875#endif
2876
2877 if (ignorevars)
2878 ignorevars = '$';
2879
2880 while (1)
2881 {
2882 if (stop2 && blank)
2883 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2
2884 && ! isblank ((unsigned char) ch))
2885 ++p;
2886 else if (stop2)
2887 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2)
2888 ++p;
2889 else if (blank)
2890 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1
2891 && ! isblank ((unsigned char) ch))
2892 ++p;
2893 else
2894 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1)
2895 ++p;
2896
2897 if (ch == '\0')
2898 break;
2899
2900 /* If we stopped due to a variable reference, skip over its contents. */
2901 if (ch == ignorevars)
2902 {
2903 char openparen = p[1];
2904
2905 p += 2;
2906
2907 /* Skip the contents of a non-quoted, multi-char variable ref. */
2908 if (openparen == '(' || openparen == '{')
2909 {
2910 unsigned int pcount = 1;
2911 char closeparen = (openparen == '(' ? ')' : '}');
2912
2913 while ((ch = *p))
2914 {
2915 if (ch == openparen)
2916 ++pcount;
2917 else if (ch == closeparen)
2918 if (--pcount == 0)
2919 {
2920 ++p;
2921 break;
2922 }
2923 ++p;
2924 }
2925 }
2926
2927 /* Skipped the variable reference: look for STOPCHARS again. */
2928 continue;
2929 }
2930
2931 if (p > string && p[-1] == '\\')
2932 {
2933 /* Search for more backslashes. */
2934 int i = -2;
2935 while (&p[i] >= string && p[i] == '\\')
2936 --i;
2937 ++i;
2938 /* Only compute the length if really needed. */
2939 if (string_len == 0)
2940 string_len = strlen (string);
2941 /* The number of backslashes is now -I.
2942 Copy P over itself to swallow half of them. */
2943 memmove (&p[i], &p[i/2], (string_len - (p - string)) - (i/2) + 1);
2944 p += i/2;
2945 if (i % 2 == 0)
2946 /* All the backslashes quoted each other; the STOPCHAR was
2947 unquoted. */
2948 return p;
2949
2950 /* The STOPCHAR was quoted by a backslash. Look for another. */
2951 }
2952 else
2953 /* No backslash in sight. */
2954 return p;
2955 }
2956
2957 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2958 return 0;
2959}
2960
2961#ifdef CONFIG_WITH_VALUE_LENGTH
2962/* Special case version of find_char_unquote that only takes stop1.
2963 This is so common that it makes a lot of sense to specialize this.
2964 */
2965__inline static char *
2966find_char_unquote_0 (char *string, int stop1, char **eosp)
2967{
2968 unsigned int string_len = *eosp - string;
2969 char *p = (char *)memchr (string, stop1, string_len);
2970 assert (strlen (string) == string_len);
2971 if (!p)
2972 return NULL;
2973 if (p <= string || p[-1] != '\\')
2974 return p;
2975
2976 p = find_char_unquote_2 (string, stop1, 0, 0, 0, string_len);
2977 *eosp = memchr (string, '\0', string_len);
2978 return p;
2979}
2980#endif
2981
2982/* Search PATTERN for an unquoted % and handle quoting. */
2983
2984char *
2985find_percent (char *pattern)
2986{
2987 return find_char_unquote (pattern, '%', 0, 0, 0);
2988}
2989
2990/* Search STRING for an unquoted % and handle quoting. Returns a pointer to
2991 the % or NULL if no % was found.
2992 This version is used with strings in the string cache: if there's a need to
2993 modify the string a new version will be added to the string cache and
2994 *STRING will be set to that. */
2995
2996const char *
2997find_percent_cached (const char **string)
2998{
2999 const char *p = *string;
3000 char *new = 0;
3001 int slen = 0;
3002
3003 /* If the first char is a % return now. This lets us avoid extra tests
3004 inside the loop. */
3005 if (*p == '%')
3006 return p;
3007
3008 while (1)
3009 {
3010 while (*p != '\0' && *p != '%')
3011 ++p;
3012
3013 if (*p == '\0')
3014 break;
3015
3016 /* See if this % is escaped with a backslash; if not we're done. */
3017 if (p[-1] != '\\')
3018 break;
3019
3020 {
3021 /* Search for more backslashes. */
3022 char *pv;
3023 int i = -2;
3024
3025 while (&p[i] >= *string && p[i] == '\\')
3026 --i;
3027 ++i;
3028
3029 /* At this point we know we'll need to allocate a new string.
3030 Make a copy if we haven't yet done so. */
3031 if (! new)
3032 {
3033 slen = strlen (*string);
3034 new = alloca (slen + 1);
3035 memcpy (new, *string, slen + 1);
3036 p = new + (p - *string);
3037 *string = new;
3038 }
3039
3040 /* At this point *string, p, and new all point into the same string.
3041 Get a non-const version of p so we can modify new. */
3042 pv = new + (p - *string);
3043
3044 /* The number of backslashes is now -I.
3045 Copy P over itself to swallow half of them. */
3046 memmove (&pv[i], &pv[i/2], (slen - (pv - new)) - (i/2) + 1);
3047 p += i/2;
3048
3049 /* If the backslashes quoted each other; the % was unquoted. */
3050 if (i % 2 == 0)
3051 break;
3052 }
3053 }
3054
3055 /* If we had to change STRING, add it to the strcache. */
3056 if (new)
3057 {
3058 *string = strcache_add (*string);
3059 p = *string + (p - new);
3060 }
3061
3062 /* If we didn't find a %, return NULL. Otherwise return a ptr to it. */
3063 return (*p == '\0') ? NULL : p;
3064}
3065
3066
3067/* Parse a string into a sequence of filenames represented as a
3068 chain of struct nameseq's in reverse order and return that chain.
3069
3070 The string is passed as STRINGP, the address of a string pointer.
3071 The string pointer is updated to point at the first character
3072 not parsed, which either is a null char or equals STOPCHAR.
3073
3074 SIZE is how big to construct chain elements.
3075 This is useful if we want them actually to be other structures
3076 that have room for additional info.
3077
3078 If STRIP is nonzero, strip `./'s off the beginning. */
3079
3080#ifndef CONFIG_WITH_ALLOC_CACHES
3081struct nameseq *
3082parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
3083#else
3084struct nameseq *
3085parse_file_seq (char **stringp, int stopchar, struct alloccache *cache, int strip)
3086#endif
3087{
3088 struct nameseq *new = 0;
3089 struct nameseq *new1;
3090#ifndef NO_ARCHIVES /* bird: MSC warning */
3091 struct nameseq *lastnew1;
3092#endif
3093 char *p = *stringp;
3094
3095#ifdef VMS
3096# define VMS_COMMA ','
3097#else
3098# define VMS_COMMA 0
3099#endif
3100
3101 while (1)
3102 {
3103 const char *name;
3104 char *q;
3105
3106 /* Skip whitespace; see if any more names are left. */
3107 p = next_token (p);
3108 if (*p == '\0')
3109 break;
3110 if (*p == stopchar)
3111 break;
3112
3113 /* There are, so find the end of the next name. */
3114 q = p;
3115 p = find_char_unquote (q, stopchar, VMS_COMMA, 1, 0);
3116#ifdef VMS
3117 /* convert comma separated list to space separated */
3118 if (p && *p == ',')
3119 *p =' ';
3120#endif
3121#ifdef _AMIGA
3122 if (stopchar == ':' && p && *p == ':'
3123 && !(isspace ((unsigned char)p[1]) || !p[1]
3124 || isspace ((unsigned char)p[-1])))
3125 p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1, 0);
3126#endif
3127#ifdef HAVE_DOS_PATHS
3128 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
3129 first colon which isn't followed by a slash or a backslash.
3130 Note that tokens separated by spaces should be treated as separate
3131 tokens since make doesn't allow path names with spaces */
3132 if (stopchar == ':')
3133 while (p != 0 && !isspace ((unsigned char)*p) &&
3134 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
3135 p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1, 0);
3136#endif
3137 if (p == 0)
3138 p = q + strlen (q);
3139
3140 if (strip)
3141#ifdef VMS
3142 /* Skip leading `[]'s. */
3143 while (p - q > 2 && q[0] == '[' && q[1] == ']')
3144#else
3145 /* Skip leading `./'s. */
3146 while (p - q > 2 && q[0] == '.' && q[1] == '/')
3147#endif
3148 {
3149 q += 2; /* Skip "./". */
3150 while (q < p && *q == '/')
3151 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
3152 ++q;
3153 }
3154
3155 /* Extract the filename just found, and skip it. */
3156
3157 if (q == p)
3158 /* ".///" was stripped to "". */
3159#if defined(VMS)
3160 continue;
3161#elif defined(_AMIGA)
3162 name = "";
3163#else
3164 name = "./";
3165#endif
3166 else
3167#ifdef VMS
3168/* VMS filenames can have a ':' in them but they have to be '\'ed but we need
3169 * to remove this '\' before we can use the filename.
3170 * Savestring called because q may be read-only string constant.
3171 */
3172 {
3173 char *qbase = xstrdup (q);
3174 char *pbase = qbase + (p-q);
3175 char *q1 = qbase;
3176 char *q2 = q1;
3177 char *p1 = pbase;
3178
3179 while (q1 != pbase)
3180 {
3181 if (*q1 == '\\' && *(q1+1) == ':')
3182 {
3183 q1++;
3184 p1--;
3185 }
3186 *q2++ = *q1++;
3187 }
3188 name = strcache_add_len (qbase, p1 - qbase);
3189 free (qbase);
3190 }
3191#elif !defined(CONFIG_WITH_VALUE_LENGTH)
3192 name = strcache_add_len (q, p - q);
3193#else /* CONFIG_WITH_VALUE_LENGTH */
3194 {
3195 /* Make sure it's terminated, strcache_add_len has to make a
3196 temp copy on the stack otherwise. */
3197 char saved = *p;
3198 *p = '\0';
3199 name = strcache_add_len (q, p - q);
3200 *p = saved;
3201 }
3202#endif /* CONFIG_WITH_VALUE_LENGTH */
3203
3204 /* Add it to the front of the chain. */
3205#ifndef CONFIG_WITH_ALLOC_CACHES
3206 new1 = xmalloc (size);
3207#else
3208 new1 = (struct nameseq *)alloccache_alloc (cache);
3209#endif
3210 new1->name = name;
3211 new1->next = new;
3212 new = new1;
3213 }
3214
3215#ifndef NO_ARCHIVES
3216
3217 /* Look for multi-word archive references.
3218 They are indicated by a elt ending with an unmatched `)' and
3219 an elt further down the chain (i.e., previous in the file list)
3220 with an unmatched `(' (e.g., "lib(mem"). */
3221
3222 new1 = new;
3223 lastnew1 = 0;
3224 while (new1 != 0)
3225 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
3226 && new1->name[strlen (new1->name) - 1] == ')'
3227 && strchr (new1->name, '(') == 0)
3228 {
3229 /* NEW1 ends with a `)' but does not contain a `('.
3230 Look back for an elt with an opening `(' but no closing `)'. */
3231
3232 struct nameseq *n = new1->next, *lastn = new1;
3233 char *paren = 0;
3234 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
3235 {
3236 lastn = n;
3237 n = n->next;
3238 }
3239 if (n != 0
3240 /* Ignore something starting with `(', as that cannot actually
3241 be an archive-member reference (and treating it as such
3242 results in an empty file name, which causes much lossage). */
3243 && n->name[0] != '(')
3244 {
3245 /* N is the first element in the archive group.
3246 Its name looks like "lib(mem" (with no closing `)'). */
3247
3248 char *libname;
3249
3250 /* Copy "lib(" into LIBNAME. */
3251 ++paren;
3252 libname = alloca (paren - n->name + 1);
3253 memcpy (libname, n->name, paren - n->name);
3254 libname[paren - n->name] = '\0';
3255
3256 if (*paren == '\0')
3257 {
3258 /* N was just "lib(", part of something like "lib( a b)".
3259 Edit it out of the chain and free its storage. */
3260 lastn->next = n->next;
3261#ifndef CONFIG_WITH_ALLOC_CACHES
3262 free (n);
3263#else
3264 alloccache_free (cache, n);
3265#endif
3266 /* LASTN->next is the new stopping elt for the loop below. */
3267 n = lastn->next;
3268 }
3269 else
3270 {
3271 /* Replace N's name with the full archive reference. */
3272 n->name = strcache_add (concat (libname, paren, ")"));
3273 }
3274
3275 if (new1->name[1] == '\0')
3276 {
3277 /* NEW1 is just ")", part of something like "lib(a b )".
3278 Omit it from the chain and free its storage. */
3279 if (lastnew1 == 0)
3280 new = new1->next;
3281 else
3282 lastnew1->next = new1->next;
3283 lastn = new1;
3284 new1 = new1->next;
3285#ifndef CONFIG_WITH_ALLOC_CACHES
3286 free (lastn);
3287#else
3288 alloccache_free (cache, lastn);
3289#endif
3290 }
3291 else
3292 {
3293 /* Replace also NEW1->name, which already has closing `)'. */
3294 new1->name = strcache_add (concat (libname, new1->name, ""));
3295 new1 = new1->next;
3296 }
3297
3298 /* Trace back from NEW1 (the end of the list) until N
3299 (the beginning of the list), rewriting each name
3300 with the full archive reference. */
3301
3302 while (new1 != n)
3303 {
3304 new1->name = strcache_add (concat (libname, new1->name, ")"));
3305 lastnew1 = new1;
3306 new1 = new1->next;
3307 }
3308 }
3309 else
3310 {
3311 /* No frobnication happening. Just step down the list. */
3312 lastnew1 = new1;
3313 new1 = new1->next;
3314 }
3315 }
3316 else
3317 {
3318 lastnew1 = new1;
3319 new1 = new1->next;
3320 }
3321
3322#endif
3323
3324 *stringp = p;
3325 return new;
3326}
3327
3328
3329/* Find the next line of text in an eval buffer, combining continuation lines
3330 into one line.
3331 Return the number of actual lines read (> 1 if continuation lines).
3332 Returns -1 if there's nothing left in the buffer.
3333
3334 After this function, ebuf->buffer points to the first character of the
3335 line we just found.
3336 */
3337
3338/* Read a line of text from a STRING.
3339 Since we aren't really reading from a file, don't bother with linenumbers.
3340 */
3341
3342static unsigned long
3343readstring (struct ebuffer *ebuf)
3344{
3345 char *eol;
3346#ifdef CONFIG_WITH_VALUE_LENGTH
3347 char *end;
3348#endif
3349
3350 /* If there is nothing left in this buffer, return 0. */
3351 if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
3352 return -1;
3353
3354 /* Set up a new starting point for the buffer, and find the end of the
3355 next logical line (taking into account backslash/newline pairs). */
3356
3357 eol = ebuf->buffer = ebuf->bufnext;
3358#ifdef CONFIG_WITH_VALUE_LENGTH
3359 end = ebuf->bufstart + ebuf->size;
3360#endif
3361
3362 while (1)
3363 {
3364 int backslash = 0;
3365 char *bol = eol;
3366 char *p;
3367
3368 /* Find the next newline. At EOS, stop. */
3369#ifndef CONFIG_WITH_VALUE_LENGTH
3370 eol = p = strchr (eol , '\n');
3371#else
3372 p = (char *)memchr (eol, '\n', end - eol);
3373 assert (!memchr (eol, '\0', p != 0 ? p - eol : end - eol));
3374 eol = p;
3375#endif
3376 if (!eol)
3377 {
3378 ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
3379#ifdef CONFIG_WITH_VALUE_LENGTH
3380 ebuf->eol = end;
3381#endif
3382 return 0;
3383 }
3384
3385 /* Found a newline; if it's escaped continue; else we're done. */
3386 while (p > bol && *(--p) == '\\')
3387 backslash = !backslash;
3388 if (!backslash)
3389 break;
3390 ++eol;
3391 }
3392
3393 /* Overwrite the newline char. */
3394 *eol = '\0';
3395 ebuf->bufnext = eol+1;
3396#ifdef CONFIG_WITH_VALUE_LENGTH
3397 ebuf->eol = eol;
3398#endif
3399
3400 return 0;
3401}
3402
3403static long
3404readline (struct ebuffer *ebuf)
3405{
3406 char *p;
3407 char *end;
3408 char *start;
3409 long nlines = 0;
3410
3411 /* The behaviors between string and stream buffers are different enough to
3412 warrant different functions. Do the Right Thing. */
3413
3414 if (!ebuf->fp)
3415 return readstring (ebuf);
3416
3417 /* When reading from a file, we always start over at the beginning of the
3418 buffer for each new line. */
3419
3420 p = start = ebuf->bufstart;
3421 end = p + ebuf->size;
3422 *p = '\0';
3423#ifdef CONFIG_WITH_VALUE_LENGTH
3424 ebuf->eol = p;
3425#endif
3426
3427 while (fgets (p, end - p, ebuf->fp) != 0)
3428 {
3429 char *p2;
3430 unsigned long len;
3431 int backslash;
3432
3433 len = strlen (p);
3434 if (len == 0)
3435 {
3436 /* This only happens when the first thing on the line is a '\0'.
3437 It is a pretty hopeless case, but (wonder of wonders) Athena
3438 lossage strikes again! (xmkmf puts NULs in its makefiles.)
3439 There is nothing really to be done; we synthesize a newline so
3440 the following line doesn't appear to be part of this line. */
3441 error (&ebuf->floc,
3442 _("warning: NUL character seen; rest of line ignored"));
3443 p[0] = '\n';
3444 len = 1;
3445 }
3446
3447 /* Jump past the text we just read. */
3448 p += len;
3449
3450 /* If the last char isn't a newline, the whole line didn't fit into the
3451 buffer. Get some more buffer and try again. */
3452 if (p[-1] != '\n')
3453 goto more_buffer;
3454
3455 /* We got a newline, so add one to the count of lines. */
3456 ++nlines;
3457
3458#if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
3459 /* Check to see if the line was really ended with CRLF; if so ignore
3460 the CR. */
3461 if ((p - start) > 1 && p[-2] == '\r')
3462 {
3463 --p;
3464 p[-1] = '\n';
3465 }
3466#endif
3467
3468 backslash = 0;
3469 for (p2 = p - 2; p2 >= start; --p2)
3470 {
3471 if (*p2 != '\\')
3472 break;
3473 backslash = !backslash;
3474 }
3475
3476 if (!backslash)
3477 {
3478 p[-1] = '\0';
3479#ifdef CONFIG_WITH_VALUE_LENGTH
3480 ebuf->eol = p - 1;
3481#endif
3482 break;
3483 }
3484
3485 /* It was a backslash/newline combo. If we have more space, read
3486 another line. */
3487 if (end - p >= 80)
3488 {
3489#ifdef CONFIG_WITH_VALUE_LENGTH
3490 ebuf->eol = p;
3491#endif
3492 continue;
3493 }
3494
3495 /* We need more space at the end of our buffer, so realloc it.
3496 Make sure to preserve the current offset of p. */
3497 more_buffer:
3498 {
3499 unsigned long off = p - start;
3500 ebuf->size *= 2;
3501 start = ebuf->buffer = ebuf->bufstart = xrealloc (start, ebuf->size);
3502 p = start + off;
3503 end = start + ebuf->size;
3504 *p = '\0';
3505#ifdef CONFIG_WITH_VALUE_LENGTH
3506 ebuf->eol = p;
3507#endif
3508 }
3509 }
3510
3511 if (ferror (ebuf->fp))
3512 pfatal_with_name (ebuf->floc.filenm);
3513
3514 /* If we found some lines, return how many.
3515 If we didn't, but we did find _something_, that indicates we read the last
3516 line of a file with no final newline; return 1.
3517 If we read nothing, we're at EOF; return -1. */
3518
3519 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
3520}
3521
3522
3523/* Parse the next "makefile word" from the input buffer, and return info
3524 about it.
3525
3526 A "makefile word" is one of:
3527
3528 w_bogus Should never happen
3529 w_eol End of input
3530 w_static A static word; cannot be expanded
3531 w_variable A word containing one or more variables/functions
3532 w_colon A colon
3533 w_dcolon A double-colon
3534 w_semicolon A semicolon
3535 w_varassign A variable assignment operator (=, :=, +=, >=, or ?=)
3536
3537 Note that this function is only used when reading certain parts of the
3538 makefile. Don't use it where special rules hold sway (RHS of a variable,
3539 in a command list, etc.) */
3540
3541static enum make_word_type
3542get_next_mword (char *buffer, char *delim, char **startp, unsigned int *length)
3543{
3544 enum make_word_type wtype = w_bogus;
3545 char *p = buffer, *beg;
3546 char c;
3547
3548 /* Skip any leading whitespace. */
3549 while (isblank ((unsigned char)*p))
3550 ++p;
3551
3552 beg = p;
3553 c = *(p++);
3554 switch (c)
3555 {
3556 case '\0':
3557 wtype = w_eol;
3558 break;
3559
3560 case ';':
3561 wtype = w_semicolon;
3562 break;
3563
3564 case '=':
3565 wtype = w_varassign;
3566 break;
3567
3568 case ':':
3569 wtype = w_colon;
3570 switch (*p)
3571 {
3572 case ':':
3573 ++p;
3574 wtype = w_dcolon;
3575 break;
3576
3577 case '=':
3578 ++p;
3579 wtype = w_varassign;
3580 break;
3581 }
3582 break;
3583
3584 case '+':
3585 case '?':
3586#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3587 case '>':
3588#endif
3589 if (*p == '=')
3590 {
3591 ++p;
3592 wtype = w_varassign;
3593 break;
3594 }
3595
3596 default:
3597 if (delim && strchr (delim, c))
3598 wtype = w_static;
3599 break;
3600 }
3601
3602 /* Did we find something? If so, return now. */
3603 if (wtype != w_bogus)
3604 goto done;
3605
3606 /* This is some non-operator word. A word consists of the longest
3607 string of characters that doesn't contain whitespace, one of [:=#],
3608 or [?+]=, or one of the chars in the DELIM string. */
3609
3610 /* We start out assuming a static word; if we see a variable we'll
3611 adjust our assumptions then. */
3612 wtype = w_static;
3613
3614 /* We already found the first value of "c", above. */
3615 while (1)
3616 {
3617 char closeparen;
3618 int count;
3619
3620 switch (c)
3621 {
3622 case '\0':
3623 case ' ':
3624 case '\t':
3625 case '=':
3626 goto done_word;
3627
3628 case ':':
3629#ifdef HAVE_DOS_PATHS
3630 /* A word CAN include a colon in its drive spec. The drive
3631 spec is allowed either at the beginning of a word, or as part
3632 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
3633 if (!(p - beg >= 2
3634 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
3635 && (p - beg == 2 || p[-3] == '(')))
3636#endif
3637 goto done_word;
3638
3639 case '$':
3640 c = *(p++);
3641 if (c == '$')
3642 break;
3643
3644 /* This is a variable reference, so note that it's expandable.
3645 Then read it to the matching close paren. */
3646 wtype = w_variable;
3647
3648 if (c == '(')
3649 closeparen = ')';
3650 else if (c == '{')
3651 closeparen = '}';
3652 else
3653 /* This is a single-letter variable reference. */
3654 break;
3655
3656 for (count=0; *p != '\0'; ++p)
3657 {
3658 if (*p == c)
3659 ++count;
3660 else if (*p == closeparen && --count < 0)
3661 {
3662 ++p;
3663 break;
3664 }
3665 }
3666 break;
3667
3668 case '?':
3669 case '+':
3670#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3671 case '>':
3672#endif
3673 if (*p == '=')
3674 goto done_word;
3675 break;
3676
3677 case '\\':
3678 switch (*p)
3679 {
3680 case ':':
3681 case ';':
3682 case '=':
3683 case '\\':
3684 ++p;
3685 break;
3686 }
3687 break;
3688
3689 default:
3690 if (delim && strchr (delim, c))
3691 goto done_word;
3692 break;
3693 }
3694
3695 c = *(p++);
3696 }
3697 done_word:
3698 --p;
3699
3700 done:
3701 if (startp)
3702 *startp = beg;
3703 if (length)
3704 *length = p - beg;
3705 return wtype;
3706}
3707
3708
3709/* Construct the list of include directories
3710 from the arguments and the default list. */
3711
3712void
3713construct_include_path (const char **arg_dirs)
3714{
3715#ifdef VAXC /* just don't ask ... */
3716 stat_t stbuf;
3717#else
3718 struct stat stbuf;
3719#endif
3720 const char **dirs;
3721 const char **cpp;
3722 unsigned int idx;
3723
3724 /* Compute the number of pointers we need in the table. */
3725 idx = sizeof (default_include_directories) / sizeof (const char *);
3726 if (arg_dirs)
3727 for (cpp = arg_dirs; *cpp != 0; ++cpp)
3728 ++idx;
3729
3730#ifdef __MSDOS__
3731 /* Add one for $DJDIR. */
3732 ++idx;
3733#endif
3734#ifdef KMK
3735 /* Add one for the kBuild directory. */
3736 ++idx;
3737#endif
3738
3739 dirs = xmalloc (idx * sizeof (const char *));
3740
3741 idx = 0;
3742 max_incl_len = 0;
3743
3744 /* First consider any dirs specified with -I switches.
3745 Ignore any that don't exist. Remember the maximum string length. */
3746
3747 if (arg_dirs)
3748 while (*arg_dirs != 0)
3749 {
3750 const char *dir = *(arg_dirs++);
3751 char *expanded = 0;
3752 int e;
3753
3754 if (dir[0] == '~')
3755 {
3756 expanded = tilde_expand (dir);
3757 if (expanded != 0)
3758 dir = expanded;
3759 }
3760
3761 EINTRLOOP (e, stat (dir, &stbuf));
3762 if (e == 0 && S_ISDIR (stbuf.st_mode))
3763 {
3764 unsigned int len = strlen (dir);
3765 /* If dir name is written with trailing slashes, discard them. */
3766 while (len > 1 && dir[len - 1] == '/')
3767 --len;
3768 if (len > max_incl_len)
3769 max_incl_len = len;
3770 dirs[idx++] = strcache_add_len (dir, len);
3771 }
3772
3773 if (expanded)
3774 free (expanded);
3775 }
3776
3777 /* Now add the standard default dirs at the end. */
3778
3779#ifdef __MSDOS__
3780 {
3781 /* The environment variable $DJDIR holds the root of the DJGPP directory
3782 tree; add ${DJDIR}/include. */
3783 struct variable *djdir = lookup_variable ("DJDIR", 5);
3784
3785 if (djdir)
3786 {
3787 unsigned int len = strlen (djdir->value) + 8;
3788 char *defdir = alloca (len + 1);
3789
3790 strcat (strcpy (defdir, djdir->value), "/include");
3791 dirs[idx++] = strcache_add (defdir);
3792
3793 if (len > max_incl_len)
3794 max_incl_len = len;
3795 }
3796 }
3797#endif
3798#ifdef KMK
3799 /* Add $(KBUILD_PATH). */
3800 {
3801 size_t len = strlen (get_kbuild_path ());
3802 dirs[idx++] = strcache_add_len (get_kbuild_path (), len);
3803 if (len > max_incl_len)
3804 max_incl_len = len;
3805 }
3806#endif
3807
3808 for (cpp = default_include_directories; *cpp != 0; ++cpp)
3809 {
3810 int e;
3811
3812 EINTRLOOP (e, stat (*cpp, &stbuf));
3813 if (e == 0 && S_ISDIR (stbuf.st_mode))
3814 {
3815 unsigned int len = strlen (*cpp);
3816 /* If dir name is written with trailing slashes, discard them. */
3817 while (len > 1 && (*cpp)[len - 1] == '/')
3818 --len;
3819 if (len > max_incl_len)
3820 max_incl_len = len;
3821 dirs[idx++] = strcache_add_len (*cpp, len - 1);
3822 }
3823 }
3824
3825 dirs[idx] = 0;
3826
3827 /* Now add each dir to the .INCLUDE_DIRS variable. */
3828
3829 for (cpp = dirs; *cpp != 0; ++cpp)
3830 do_variable_definition (NILF, ".INCLUDE_DIRS", *cpp,
3831 o_default, f_append, 0);
3832
3833 include_directories = dirs;
3834}
3835
3836
3837/* Expand ~ or ~USER at the beginning of NAME.
3838 Return a newly malloc'd string or 0. */
3839
3840char *
3841tilde_expand (const char *name)
3842{
3843#ifndef VMS
3844 if (name[1] == '/' || name[1] == '\0')
3845 {
3846 extern char *getenv ();
3847 char *home_dir;
3848 int is_variable;
3849
3850 {
3851 /* Turn off --warn-undefined-variables while we expand HOME. */
3852 int save = warn_undefined_variables_flag;
3853 warn_undefined_variables_flag = 0;
3854
3855#ifndef CONFIG_WITH_VALUE_LENGTH
3856 home_dir = allocated_variable_expand ("$(HOME)");
3857#else
3858 home_dir = allocated_variable_expand_2 (STRING_SIZE_TUPLE("$(HOME)"), NULL);
3859#endif
3860
3861 warn_undefined_variables_flag = save;
3862 }
3863
3864 is_variable = home_dir[0] != '\0';
3865 if (!is_variable)
3866 {
3867 free (home_dir);
3868 home_dir = getenv ("HOME");
3869 }
3870# if !defined(_AMIGA) && !defined(WINDOWS32)
3871 if (home_dir == 0 || home_dir[0] == '\0')
3872 {
3873 extern char *getlogin ();
3874 char *logname = getlogin ();
3875 home_dir = 0;
3876 if (logname != 0)
3877 {
3878 struct passwd *p = getpwnam (logname);
3879 if (p != 0)
3880 home_dir = p->pw_dir;
3881 }
3882 }
3883# endif /* !AMIGA && !WINDOWS32 */
3884 if (home_dir != 0)
3885 {
3886 char *new = xstrdup (concat (home_dir, "", name + 1));
3887 if (is_variable)
3888 free (home_dir);
3889 return new;
3890 }
3891 }
3892# if !defined(_AMIGA) && !defined(WINDOWS32)
3893 else
3894 {
3895 struct passwd *pwent;
3896 char *userend = strchr (name + 1, '/');
3897 if (userend != 0)
3898 *userend = '\0';
3899 pwent = getpwnam (name + 1);
3900 if (pwent != 0)
3901 {
3902 if (userend == 0)
3903 return xstrdup (pwent->pw_dir);
3904 else
3905 return xstrdup (concat (pwent->pw_dir, "/", userend + 1));
3906 }
3907 else if (userend != 0)
3908 *userend = '/';
3909 }
3910# endif /* !AMIGA && !WINDOWS32 */
3911#endif /* !VMS */
3912 return 0;
3913}
3914
3915/* Given a chain of struct nameseq's describing a sequence of filenames,
3916 in reverse of the intended order, return a new chain describing the
3917 result of globbing the filenames. The new chain is in forward order.
3918 The links of the old chain are freed or used in the new chain.
3919 Likewise for the names in the old chain.
3920
3921 SIZE is how big to construct chain elements.
3922 This is useful if we want them actually to be other structures
3923 that have room for additional info. */
3924
3925#ifndef CONFIG_WITH_ALLOC_CACHES
3926struct nameseq *
3927multi_glob (struct nameseq *chain, unsigned int size)
3928#else
3929struct nameseq *
3930multi_glob (struct nameseq *chain, struct alloccache *cache)
3931#endif
3932{
3933 void dir_setup_glob (glob_t *);
3934 struct nameseq *new = 0;
3935 struct nameseq *old;
3936 struct nameseq *nexto;
3937 glob_t gl;
3938#if defined(KMK) || defined(__EMX__) /* speed optimization */
3939 int rc;
3940#endif
3941
3942 dir_setup_glob (&gl);
3943
3944 for (old = chain; old != 0; old = nexto)
3945 {
3946 const char *gname;
3947#ifndef NO_ARCHIVES
3948 char *arname = 0;
3949 char *memname = 0;
3950#endif
3951 nexto = old->next;
3952 gname = old->name;
3953
3954 if (gname[0] == '~')
3955 {
3956 char *newname = tilde_expand (old->name);
3957 if (newname != 0)
3958 gname = newname;
3959 }
3960
3961#ifndef NO_ARCHIVES
3962 if (ar_name (gname))
3963 {
3964 /* OLD->name is an archive member reference. Replace it with the
3965 archive file name, and save the member name in MEMNAME. We will
3966 glob on the archive name and then reattach MEMNAME later. */
3967 ar_parse_name (gname, &arname, &memname);
3968 gname = arname;
3969 }
3970#endif /* !NO_ARCHIVES */
3971
3972#if defined(KMK) || defined(__EMX__) /* speed optimization */
3973 if (!strpbrk(gname, "*?["))
3974 {
3975 gl.gl_pathc = 1;
3976 gl.gl_pathv = (char **)&gname;
3977 rc = 0;
3978 }
3979 else
3980 rc = glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl);
3981 switch (rc)
3982#else
3983 switch (glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
3984#endif
3985 {
3986 case 0: /* Success. */
3987 {
3988 int i = gl.gl_pathc;
3989 while (i-- > 0)
3990 {
3991#ifndef NO_ARCHIVES
3992 if (memname != 0)
3993 {
3994 /* Try to glob on MEMNAME within the archive. */
3995 struct nameseq *found
3996 = ar_glob (gl.gl_pathv[i], memname, size);
3997 if (! found)
3998 {
3999 /* No matches. Use MEMNAME as-is. */
4000 unsigned int alen = strlen (gl.gl_pathv[i]);
4001 unsigned int mlen = strlen (memname);
4002 char *name;
4003 struct nameseq *elt = xmalloc (size);
4004 memset (elt, '\0', size);
4005
4006 name = alloca (alen + 1 + mlen + 2);
4007 memcpy (name, gl.gl_pathv[i], alen);
4008 name[alen] = '(';
4009 memcpy (name+alen+1, memname, mlen);
4010 name[alen + 1 + mlen] = ')';
4011 name[alen + 1 + mlen + 1] = '\0';
4012 elt->name = strcache_add (name);
4013 elt->next = new;
4014 new = elt;
4015 }
4016 else
4017 {
4018 /* Find the end of the FOUND chain. */
4019 struct nameseq *f = found;
4020 while (f->next != 0)
4021 f = f->next;
4022
4023 /* Attach the chain being built to the end of the FOUND
4024 chain, and make FOUND the new NEW chain. */
4025 f->next = new;
4026 new = found;
4027 }
4028 }
4029 else
4030#endif /* !NO_ARCHIVES */
4031 {
4032#ifndef CONFIG_WITH_ALLOC_CACHES
4033 struct nameseq *elt = xmalloc (size);
4034 memset (elt, '\0', size);
4035#else
4036 struct nameseq *elt = alloccache_calloc (cache);
4037#endif
4038 elt->name = strcache_add (gl.gl_pathv[i]);
4039 elt->next = new;
4040 new = elt;
4041 }
4042 }
4043#if defined(KMK) || defined(__EMX__) /* speed optimization */
4044 if (gl.gl_pathv != (char **)&gname)
4045#endif
4046 globfree (&gl);
4047#ifndef CONFIG_WITH_ALLOC_CACHES
4048 free (old);
4049#else
4050 alloccache_free (cache, old);
4051#endif
4052 break;
4053 }
4054
4055 case GLOB_NOSPACE:
4056 fatal (NILF, _("virtual memory exhausted"));
4057 break;
4058
4059 default:
4060 old->next = new;
4061 new = old;
4062 break;
4063 }
4064
4065#ifndef NO_ARCHIVES
4066 if (arname)
4067 free (arname);
4068#endif
4069 }
4070
4071 return new;
4072}
4073
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