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

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

kmk: Caught a variable::value update that I'd missed when implementing CONFIG_WITH_VALUE_LENGTH.

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