VirtualBox

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

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

kmk: Optimized the dependency hashing done by uniquize_deps() by making it take advantage of the fact that all strings are in the strcache.

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

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