source: trunk/src/kmk/read.c@ 1930

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

kmk/read.c: more free() avoidance, this time some very small buffers for conditionals. Use static buffers for the first chunk, this avoids all alloc/free for a typical kBuild run. the cost is 16 bytes of stack per recursion.

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