VirtualBox

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

Last change on this file since 2554 was 2548, checked in by bird, 13 years ago

kmk: hacking on a new kmk/kBuild language extension.

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

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