VirtualBox

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

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

kmk: improved the hashing of file table entries by making the strcache cache their hash values along with the string length.

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