VirtualBox

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

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

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

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