VirtualBox

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

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

kmk: and another strcache_get_len.

  • Property svn:eol-style set to native
File size: 110.8 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#ifndef CONFIG_WITH_VALUE_LENGTH
1521 unsigned int l = strlen (dep_name (d2));
1522#else
1523 unsigned int l = strcache_get_len (dep_name (d2));
1524#endif
1525 if (!strneq (name, dep_name (d2), l))
1526 continue;
1527 if (streq (name + l, dep_name (d)))
1528 {
1529 reject = 1;
1530 break;
1531 }
1532 }
1533
1534 if (reject)
1535 break;
1536 }
1537
1538 if (!reject)
1539 {
1540 define_variable_global (".DEFAULT_GOAL", 13, t->name,
1541 o_file, 0, NILF);
1542 break;
1543 }
1544 }
1545 }
1546
1547 continue;
1548 }
1549
1550 /* We get here except in the case that we just read a rule line.
1551 Record now the last rule we read, so following spurious
1552 commands are properly diagnosed. */
1553 rule_complete:
1554 record_waiting_files ();
1555 }
1556
1557#undef word1eq
1558
1559 if (conditionals->if_cmds)
1560 fatal (fstart, _("missing `endif'"));
1561
1562 /* At eof, record the last rule. */
1563 record_waiting_files ();
1564
1565 if (collapsed)
1566 free (collapsed);
1567 free (commands);
1568
1569 return 1;
1570}
1571
1572
1573
1574/* Remove comments from LINE.
1575 This is done by copying the text at LINE onto itself. */
1576
1577#ifndef CONFIG_WITH_VALUE_LENGTH
1578static void
1579remove_comments (char *line)
1580{
1581 char *comment;
1582
1583 comment = find_char_unquote (line, '#', 0, 0, 0);
1584
1585 if (comment != 0)
1586 /* Cut off the line at the #. */
1587 *comment = '\0';
1588}
1589#else /* CONFIG_WITH_VALUE_LENGTH */
1590__inline static char *
1591remove_comments (char *line, char *eol)
1592{
1593 unsigned int string_len = eol - line;
1594 register int ch;
1595 char *p;
1596
1597 /* Hope for simple (no comments). */
1598 p = memchr (line, '#', string_len);
1599 if (!p)
1600 return eol;
1601
1602 /* Found potential comment, enter the slow route. */
1603 for (;;)
1604 {
1605 if (p > line && p[-1] == '\\')
1606 {
1607 /* Search for more backslashes. */
1608 int i = -2;
1609 while (&p[i] >= line && p[i] == '\\')
1610 --i;
1611 ++i;
1612
1613 /* The number of backslashes is now -I.
1614 Copy P over itself to swallow half of them. */
1615 memmove (&p[i], &p[i/2], (string_len - (p - line)) - (i/2) + 1);
1616 p += i/2;
1617 if (i % 2 == 0)
1618 {
1619 /* All the backslashes quoted each other; the STOPCHAR was
1620 unquoted. */
1621 *p = '\0';
1622 return p;
1623 }
1624
1625 /* The '#' was quoted by a backslash. Look for another. */
1626 }
1627 else
1628 {
1629 /* No backslash in sight. */
1630 *p = '\0';
1631 return p;
1632 }
1633
1634 /* lazy, string_len isn't correct so do it the slow way. */
1635 while ((ch = *p) != '#')
1636 {
1637 if (ch == '\0')
1638 return p;
1639 ++p;
1640 }
1641 }
1642 /* won't ever get here. */
1643}
1644#endif /* CONFIG_WITH_VALUE_LENGTH */
1645
1646/* Execute a `define' directive.
1647 The first line has already been read, and NAME is the name of
1648 the variable to be defined. The following lines remain to be read. */
1649
1650static void
1651do_define (char *name, unsigned int namelen,
1652 enum variable_origin origin, struct ebuffer *ebuf)
1653{
1654 struct floc defstart;
1655 long nlines = 0;
1656 int nlevels = 1;
1657 unsigned int length = 100;
1658 char *definition = xmalloc (length);
1659 unsigned int idx = 0;
1660 char *p;
1661
1662 /* Expand the variable name. */
1663 char *var = alloca (namelen + 1);
1664 memcpy (var, name, namelen);
1665 var[namelen] = '\0';
1666 var = variable_expand (var);
1667
1668 defstart = ebuf->floc;
1669
1670 while (1)
1671 {
1672 unsigned int len;
1673 char *line;
1674
1675 nlines = readline (ebuf);
1676 ebuf->floc.lineno += nlines;
1677
1678 /* If there is nothing left to eval, we're done. */
1679 if (nlines < 0)
1680 break;
1681
1682 line = ebuf->buffer;
1683
1684#ifndef CONFIG_WITH_VALUE_LENGTH
1685 collapse_continuations (line);
1686#else
1687 ebuf->eol = collapse_continuations (line, ebuf->eol - line);
1688#endif
1689
1690 /* If the line doesn't begin with a tab, test to see if it introduces
1691 another define, or ends one. */
1692
1693 /* Stop if we find an 'endef' */
1694 if (line[0] != cmd_prefix)
1695 {
1696 p = next_token (line);
1697#ifndef CONFIG_WITH_VALUE_LENGTH
1698 len = strlen (p);
1699#else
1700 len = ebuf->eol - p;
1701 assert (len == strlen (p));
1702#endif
1703
1704 /* If this is another 'define', increment the level count. */
1705 if ((len == 6 || (len > 6 && isblank ((unsigned char)p[6])))
1706 && strneq (p, "define", 6))
1707 ++nlevels;
1708
1709 /* If this is an 'endef', decrement the count. If it's now 0,
1710 we've found the last one. */
1711 else if ((len == 5 || (len > 5 && isblank ((unsigned char)p[5])))
1712 && strneq (p, "endef", 5))
1713 {
1714 p += 5;
1715#ifndef CONFIG_WITH_VALUE_LENGTH
1716 remove_comments (p);
1717#else
1718 ebuf->eol = remove_comments (p, ebuf->eol);
1719#endif
1720 if (*next_token (p) != '\0')
1721 error (&ebuf->floc,
1722 _("Extraneous text after `endef' directive"));
1723
1724 if (--nlevels == 0)
1725 {
1726 /* Define the variable. */
1727 if (idx == 0)
1728 definition[0] = '\0';
1729 else
1730 definition[idx - 1] = '\0';
1731
1732 /* Always define these variables in the global set. */
1733 define_variable_global (var, strlen (var), definition,
1734 origin, 1, &defstart);
1735 free (definition);
1736 return;
1737 }
1738 }
1739 }
1740
1741 /* Otherwise add this line to the variable definition. */
1742#ifndef CONFIG_WITH_VALUE_LENGTH
1743 len = strlen (line);
1744#else
1745 len = ebuf->eol - line;
1746 assert (len == strlen (line));
1747#endif
1748 if (idx + len + 1 > length)
1749 {
1750 length = (idx + len) * 2;
1751 definition = xrealloc (definition, length + 1);
1752 }
1753
1754 memcpy (&definition[idx], line, len);
1755 idx += len;
1756 /* Separate lines with a newline. */
1757 definition[idx++] = '\n';
1758 }
1759
1760 /* No `endef'!! */
1761 fatal (&defstart, _("missing `endef', unterminated `define'"));
1762
1763 /* NOTREACHED */
1764 return;
1765}
1766
1767
1768/* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1769 "ifneq", "if1of", "ifn1of", "else" and "endif".
1770 LINE is the input line, with the command as its first word.
1771
1772 FILENAME and LINENO are the filename and line number in the
1773 current makefile. They are used for error messages.
1774
1775 Value is -2 if the line is not a conditional at all,
1776 -1 if the line is an invalid conditional,
1777 0 if following text should be interpreted,
1778 1 if following text should be ignored. */
1779
1780static int
1781#ifndef CONFIG_WITH_VALUE_LENGTH
1782conditional_line (char *line, int len, const struct floc *flocp)
1783#else
1784conditional_line (char *line, char *eol, int len, const struct floc *flocp)
1785#endif
1786{
1787 char *cmdname;
1788 enum { c_ifdef, c_ifndef, c_ifeq, c_ifneq,
1789#ifdef CONFIG_WITH_SET_CONDITIONALS
1790 c_if1of, c_ifn1of,
1791#endif
1792#ifdef CONFIG_WITH_IF_CONDITIONALS
1793 c_ifcond,
1794#endif
1795 c_else, c_endif
1796 } cmdtype;
1797 unsigned int i;
1798 unsigned int o;
1799#ifdef CONFIG_WITH_VALUE_LENGTH
1800 assert (strchr (line, '\0') == eol);
1801#endif
1802
1803 /* Compare a word, both length and contents. */
1804#define word1eq(s) (len == sizeof(s)-1 && strneq (s, line, sizeof(s)-1))
1805#define chkword(s, t) if (word1eq (s)) { cmdtype = (t); cmdname = (s); }
1806
1807 /* Make sure this line is a conditional. */
1808 chkword ("ifdef", c_ifdef)
1809 else chkword ("ifndef", c_ifndef)
1810 else chkword ("ifeq", c_ifeq)
1811 else chkword ("ifneq", c_ifneq)
1812#ifdef CONFIG_WITH_SET_CONDITIONALS
1813 else chkword ("if1of", c_if1of)
1814 else chkword ("ifn1of", c_ifn1of)
1815#endif
1816#ifdef CONFIG_WITH_IF_CONDITIONALS
1817 else chkword ("if", c_ifcond)
1818#endif
1819 else chkword ("else", c_else)
1820 else chkword ("endif", c_endif)
1821 else
1822 return -2;
1823
1824 /* Found one: skip past it and any whitespace after it. */
1825 line = next_token (line + len);
1826
1827#define EXTRANEOUS() error (flocp, _("Extraneous text after `%s' directive"), cmdname)
1828
1829 /* An 'endif' cannot contain extra text, and reduces the if-depth by 1 */
1830 if (cmdtype == c_endif)
1831 {
1832 if (*line != '\0')
1833 EXTRANEOUS ();
1834
1835 if (!conditionals->if_cmds)
1836 fatal (flocp, _("extraneous `%s'"), cmdname);
1837
1838 --conditionals->if_cmds;
1839
1840 goto DONE;
1841 }
1842
1843 /* An 'else' statement can either be simple, or it can have another
1844 conditional after it. */
1845 if (cmdtype == c_else)
1846 {
1847 const char *p;
1848
1849 if (!conditionals->if_cmds)
1850 fatal (flocp, _("extraneous `%s'"), cmdname);
1851
1852 o = conditionals->if_cmds - 1;
1853
1854 if (conditionals->seen_else[o])
1855 fatal (flocp, _("only one `else' per conditional"));
1856
1857 /* Change the state of ignorance. */
1858 switch (conditionals->ignoring[o])
1859 {
1860 case 0:
1861 /* We've just been interpreting. Never do it again. */
1862 conditionals->ignoring[o] = 2;
1863 break;
1864 case 1:
1865 /* We've never interpreted yet. Maybe this time! */
1866 conditionals->ignoring[o] = 0;
1867 break;
1868 }
1869
1870 /* It's a simple 'else'. */
1871 if (*line == '\0')
1872 {
1873 conditionals->seen_else[o] = 1;
1874 goto DONE;
1875 }
1876
1877 /* The 'else' has extra text. That text must be another conditional
1878 and cannot be an 'else' or 'endif'. */
1879
1880 /* Find the length of the next word. */
1881 for (p = line+1; *p != '\0' && !isspace ((unsigned char)*p); ++p)
1882 ;
1883 len = p - line;
1884
1885 /* If it's 'else' or 'endif' or an illegal conditional, fail. */
1886 if (word1eq("else") || word1eq("endif")
1887#ifndef CONFIG_WITH_VALUE_LENGTH
1888 || conditional_line (line, len, flocp) < 0)
1889#else
1890 || conditional_line (line, eol, len, flocp) < 0)
1891#endif
1892 EXTRANEOUS ();
1893 else
1894 {
1895 /* conditional_line() created a new level of conditional.
1896 Raise it back to this level. */
1897 if (conditionals->ignoring[o] < 2)
1898 conditionals->ignoring[o] = conditionals->ignoring[o+1];
1899 --conditionals->if_cmds;
1900 }
1901
1902 goto DONE;
1903 }
1904
1905 if (conditionals->allocated == 0)
1906 {
1907 conditionals->allocated = 5;
1908 conditionals->ignoring = xmalloc (conditionals->allocated);
1909 conditionals->seen_else = xmalloc (conditionals->allocated);
1910 }
1911
1912 o = conditionals->if_cmds++;
1913 if (conditionals->if_cmds > conditionals->allocated)
1914 {
1915 conditionals->allocated += 5;
1916 conditionals->ignoring = xrealloc (conditionals->ignoring,
1917 conditionals->allocated);
1918 conditionals->seen_else = xrealloc (conditionals->seen_else,
1919 conditionals->allocated);
1920 }
1921
1922 /* Record that we have seen an `if...' but no `else' so far. */
1923 conditionals->seen_else[o] = 0;
1924
1925 /* Search through the stack to see if we're already ignoring. */
1926 for (i = 0; i < o; ++i)
1927 if (conditionals->ignoring[i])
1928 {
1929 /* We are already ignoring, so just push a level to match the next
1930 "else" or "endif", and keep ignoring. We don't want to expand
1931 variables in the condition. */
1932 conditionals->ignoring[o] = 1;
1933 return 1;
1934 }
1935
1936 if (cmdtype == c_ifdef || cmdtype == c_ifndef)
1937 {
1938 char *var;
1939 struct variable *v;
1940 char *p;
1941
1942 /* Expand the thing we're looking up, so we can use indirect and
1943 constructed variable names. */
1944#ifndef CONFIG_WITH_VALUE_LENGTH
1945 var = allocated_variable_expand (line);
1946#else
1947 var = allocated_variable_expand_2 (line, eol - line, NULL);
1948#endif
1949
1950 /* Make sure there's only one variable name to test. */
1951 p = end_of_token (var);
1952 i = p - var;
1953 p = next_token (p);
1954 if (*p != '\0')
1955 return -1;
1956
1957 var[i] = '\0';
1958 v = lookup_variable (var, i);
1959
1960 conditionals->ignoring[o] =
1961 ((v != 0 && *v->value != '\0') == (cmdtype == c_ifndef));
1962
1963 free (var);
1964 }
1965#ifdef CONFIG_WITH_IF_CONDITIONALS
1966 else if (cmdtype == c_ifcond)
1967 {
1968 int rval = expr_eval_if_conditionals (line, flocp);
1969 if (rval == -1)
1970 return rval;
1971 conditionals->ignoring[o] = rval;
1972 }
1973#endif
1974 else
1975 {
1976#ifdef CONFIG_WITH_SET_CONDITIONALS
1977 /* "ifeq", "ifneq", "if1of" or "ifn1of". */
1978#else
1979 /* "ifeq" or "ifneq". */
1980#endif
1981 char *s1, *s2;
1982 unsigned int l;
1983 char termin = *line == '(' ? ',' : *line;
1984#ifdef CONFIG_WITH_VALUE_LENGTH
1985 char *buf_pos;
1986#endif
1987
1988 if (termin != ',' && termin != '"' && termin != '\'')
1989 return -1;
1990
1991 s1 = ++line;
1992 /* Find the end of the first string. */
1993 if (termin == ',')
1994 {
1995 int count = 0;
1996 for (; *line != '\0'; ++line)
1997 if (*line == '(')
1998 ++count;
1999 else if (*line == ')')
2000 --count;
2001 else if (*line == ',' && count <= 0)
2002 break;
2003 }
2004 else
2005 while (*line != '\0' && *line != termin)
2006 ++line;
2007
2008 if (*line == '\0')
2009 return -1;
2010
2011 if (termin == ',')
2012 {
2013 /* Strip blanks after the first string. */
2014 char *p = line++;
2015 while (isblank ((unsigned char)p[-1]))
2016 --p;
2017 *p = '\0';
2018#ifdef CONFIG_WITH_VALUE_LENGTH
2019 l = p - s1;
2020#endif
2021 }
2022 else
2023 {
2024#ifdef CONFIG_WITH_VALUE_LENGTH
2025 l = line - s1;
2026#endif
2027 *line++ = '\0';
2028 }
2029
2030#ifndef CONFIG_WITH_VALUE_LENGTH
2031 s2 = variable_expand (s1);
2032 /* We must allocate a new copy of the expanded string because
2033 variable_expand re-uses the same buffer. */
2034 l = strlen (s2);
2035 s1 = alloca (l + 1);
2036 memcpy (s1, s2, l + 1);
2037#else
2038 s1 = variable_expand_string_2 (NULL, s1, l, &buf_pos);
2039 ++buf_pos;
2040#endif
2041
2042 if (termin != ',')
2043 /* Find the start of the second string. */
2044 line = next_token (line);
2045
2046 termin = termin == ',' ? ')' : *line;
2047 if (termin != ')' && termin != '"' && termin != '\'')
2048 return -1;
2049
2050 /* Find the end of the second string. */
2051 if (termin == ')')
2052 {
2053 int count = 0;
2054 s2 = next_token (line);
2055 for (line = s2; *line != '\0'; ++line)
2056 {
2057 if (*line == '(')
2058 ++count;
2059 else if (*line == ')')
2060 {
2061 if (count <= 0)
2062 break;
2063 else
2064 --count;
2065 }
2066 }
2067 }
2068 else
2069 {
2070 ++line;
2071 s2 = line;
2072 while (*line != '\0' && *line != termin)
2073 ++line;
2074 }
2075
2076 if (*line == '\0')
2077 return -1;
2078
2079 *line = '\0';
2080#ifdef CONFIG_WITH_VALUE_LENGTH
2081 l = line - s2;
2082#endif
2083 line = next_token (++line);
2084 if (*line != '\0')
2085 EXTRANEOUS ();
2086
2087#ifndef CONFIG_WITH_VALUE_LENGTH
2088 s2 = variable_expand (s2);
2089#else
2090 if ((size_t)buf_pos & 7)
2091 buf_pos = variable_buffer_output (buf_pos, "\0\0\0\0\0\0\0\0",
2092 8 - ((size_t)buf_pos & 7));
2093 s2 = variable_expand_string_2 (buf_pos, s2, l, &buf_pos);
2094#endif
2095#ifdef CONFIG_WITH_SET_CONDITIONALS
2096 if (cmdtype == c_if1of || cmdtype == c_ifn1of)
2097 {
2098 const char *s1_cur;
2099 unsigned int s1_len;
2100 const char *s1_iterator = s1;
2101
2102 conditionals->ignoring[o] = (cmdtype == c_if1of); /* if not found */
2103 while ((s1_cur = find_next_token (&s1_iterator, &s1_len)) != 0)
2104 {
2105 const char *s2_cur;
2106 unsigned int s2_len;
2107 const char *s2_iterator = s2;
2108 while ((s2_cur = find_next_token (&s2_iterator, &s2_len)) != 0)
2109 if (s2_len == s1_len
2110 && strneq (s2_cur, s1_cur, s1_len) )
2111 {
2112 conditionals->ignoring[o] = (cmdtype != c_if1of); /* found */
2113 break;
2114 }
2115 }
2116 }
2117 else
2118 conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
2119#else
2120 conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
2121#endif
2122 }
2123
2124 DONE:
2125 /* Search through the stack to see if we're ignoring. */
2126 for (i = 0; i < conditionals->if_cmds; ++i)
2127 if (conditionals->ignoring[i])
2128 return 1;
2129 return 0;
2130}
2131
2132
2133/* Remove duplicate dependencies in CHAIN. */
2134#ifndef CONFIG_WITH_VALUE_LENGTH
2135
2136static unsigned long
2137dep_hash_1 (const void *key)
2138{
2139 return_STRING_HASH_1 (dep_name ((struct dep const *) key));
2140}
2141
2142static unsigned long
2143dep_hash_2 (const void *key)
2144{
2145 return_STRING_HASH_2 (dep_name ((struct dep const *) key));
2146}
2147
2148static int
2149dep_hash_cmp (const void *x, const void *y)
2150{
2151 struct dep *dx = (struct dep *) x;
2152 struct dep *dy = (struct dep *) y;
2153 int cmp = strcmp (dep_name (dx), dep_name (dy));
2154
2155 /* If the names are the same but ignore_mtimes are not equal, one of these
2156 is an order-only prerequisite and one isn't. That means that we should
2157 remove the one that isn't and keep the one that is. */
2158
2159 if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
2160 dx->ignore_mtime = dy->ignore_mtime = 0;
2161
2162 return cmp;
2163}
2164
2165#else /* CONFIG_WITH_VALUE_LENGTH */
2166
2167/* Exploit the fact that all names are in the string cache. This means equal
2168 names shall have the same storage and there is no need for hashing or
2169 comparing. Use the address as the first hash, avoiding any touching of
2170 the name, and the length as the second. */
2171
2172static unsigned long
2173dep_hash_1 (const void *key)
2174{
2175 const char *name = dep_name ((struct dep const *) key);
2176 assert (strcache_iscached (name));
2177 return (size_t) name / sizeof(void *);
2178}
2179
2180static unsigned long
2181dep_hash_2 (const void *key)
2182{
2183 const char *name = dep_name ((struct dep const *) key);
2184 return strcache_get_len (name);
2185}
2186
2187static int
2188dep_hash_cmp (const void *x, const void *y)
2189{
2190 struct dep *dx = (struct dep *) x;
2191 struct dep *dy = (struct dep *) y;
2192 const char *dxname = dep_name (dx);
2193 const char *dyname = dep_name (dy);
2194 int cmp = dxname == dyname ? 0 : 1;
2195
2196 /* check preconds: both cached and the cache contains no duplicates. */
2197 assert (strcache_iscached (dxname));
2198 assert (strcache_iscached (dyname));
2199 assert (cmp == 0 || strcmp (dxname, dyname) != 0);
2200
2201 /* If the names are the same but ignore_mtimes are not equal, one of these
2202 is an order-only prerequisite and one isn't. That means that we should
2203 remove the one that isn't and keep the one that is. */
2204
2205 if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
2206 dx->ignore_mtime = dy->ignore_mtime = 0;
2207
2208 return cmp;
2209}
2210
2211#endif /* CONFIG_WITH_VALUE_LENGTH */
2212
2213void
2214uniquize_deps (struct dep *chain)
2215{
2216 struct hash_table deps;
2217 register struct dep **depp;
2218
2219 hash_init (&deps, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
2220
2221 /* Make sure that no dependencies are repeated. This does not
2222 really matter for the purpose of updating targets, but it
2223 might make some names be listed twice for $^ and $?. */
2224
2225 depp = &chain;
2226 while (*depp)
2227 {
2228 struct dep *dep = *depp;
2229 struct dep **dep_slot = (struct dep **) hash_find_slot (&deps, dep);
2230 if (HASH_VACANT (*dep_slot))
2231 {
2232 hash_insert_at (&deps, dep, dep_slot);
2233 depp = &dep->next;
2234 }
2235 else
2236 {
2237 /* Don't bother freeing duplicates.
2238 It's dangerous and little benefit accrues. */
2239 *depp = dep->next;
2240 }
2241 }
2242
2243 hash_free (&deps, 0);
2244}
2245
2246
2247/* Record target-specific variable values for files FILENAMES.
2248 TWO_COLON is nonzero if a double colon was used.
2249
2250 The links of FILENAMES are freed, and so are any names in it
2251 that are not incorporated into other data structures.
2252
2253 If the target is a pattern, add the variable to the pattern-specific
2254 variable value list. */
2255
2256static void
2257record_target_var (struct nameseq *filenames, char *defn,
2258 enum variable_origin origin, int exported,
2259 const struct floc *flocp)
2260{
2261 struct nameseq *nextf;
2262 struct variable_set_list *global;
2263
2264 global = current_variable_set_list;
2265
2266 /* If the variable is an append version, store that but treat it as a
2267 normal recursive variable. */
2268
2269 for (; filenames != 0; filenames = nextf)
2270 {
2271 struct variable *v;
2272 const char *name = filenames->name;
2273 const char *fname;
2274 const char *percent;
2275 struct pattern_var *p;
2276
2277 nextf = filenames->next;
2278 free (filenames);
2279
2280 /* If it's a pattern target, then add it to the pattern-specific
2281 variable list. */
2282 percent = find_percent_cached (&name);
2283 if (percent)
2284 {
2285 /* Get a reference for this pattern-specific variable struct. */
2286 p = create_pattern_var (name, percent);
2287 p->variable.fileinfo = *flocp;
2288 /* I don't think this can fail since we already determined it was a
2289 variable definition. */
2290#ifndef CONFIG_WITH_VALUE_LENGTH
2291 v = parse_variable_definition (&p->variable, defn);
2292#else
2293 v = parse_variable_definition (&p->variable, defn, NULL);
2294#endif
2295 assert (v != 0);
2296
2297 if (v->flavor == f_simple)
2298 v->value = allocated_variable_expand (v->value);
2299 else
2300 v->value = xstrdup (v->value);
2301
2302 fname = p->target;
2303 }
2304 else
2305 {
2306 struct file *f;
2307
2308 /* Get a file reference for this file, and initialize it.
2309 We don't want to just call enter_file() because that allocates a
2310 new entry if the file is a double-colon, which we don't want in
2311 this situation. */
2312#ifndef KMK
2313 f = lookup_file (name);
2314 if (!f)
2315 f = enter_file (strcache_add (name));
2316#else /* KMK */
2317 /* XXX: this is probably already a cached string. */
2318 fname = strcache_add (name);
2319 f = lookup_file_cached (fname);
2320 if (!f)
2321 f = enter_file (fname);
2322#endif
2323 else if (f->double_colon)
2324 f = f->double_colon;
2325
2326 initialize_file_variables (f, 1);
2327 fname = f->name;
2328
2329 current_variable_set_list = f->variables;
2330#ifndef CONFIG_WITH_VALUE_LENGTH
2331 v = try_variable_definition (flocp, defn, origin, 1);
2332#else
2333 v = try_variable_definition (flocp, defn, NULL, origin, 1);
2334#endif
2335 if (!v)
2336 error (flocp, _("Malformed target-specific variable definition"));
2337 current_variable_set_list = global;
2338 }
2339
2340 /* Set up the variable to be *-specific. */
2341 v->origin = origin;
2342 v->per_target = 1;
2343 v->export = exported ? v_export : v_default;
2344
2345 /* If it's not an override, check to see if there was a command-line
2346 setting. If so, reset the value. */
2347 if (origin != o_override)
2348 {
2349 struct variable *gv;
2350 int len = strlen(v->name);
2351
2352 gv = lookup_variable (v->name, len);
2353 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
2354 {
2355 if (v->value != 0)
2356 free (v->value);
2357 v->value = xstrdup (gv->value);
2358 v->origin = gv->origin;
2359 v->recursive = gv->recursive;
2360 v->append = 0;
2361 }
2362 }
2363 }
2364}
2365
2366
2367/* Record a description line for files FILENAMES,
2368 with dependencies DEPS, commands to execute described
2369 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
2370 TWO_COLON is nonzero if a double colon was used.
2371 If not nil, PATTERN is the `%' pattern to make this
2372 a static pattern rule, and PATTERN_PERCENT is a pointer
2373 to the `%' within it.
2374
2375 The links of FILENAMES are freed, and so are any names in it
2376 that are not incorporated into other data structures. */
2377
2378#ifndef CONFIG_WITH_INCLUDEDEP
2379static void
2380#else
2381void
2382#endif
2383record_files (struct nameseq *filenames, const char *pattern,
2384 const char *pattern_percent, struct dep *deps,
2385 unsigned int cmds_started, char *commands,
2386 unsigned int commands_idx, int two_colon,
2387 const struct floc *flocp)
2388{
2389 struct nameseq *nextf;
2390 int implicit = 0;
2391 unsigned int max_targets = 0, target_idx = 0;
2392 const char **targets = 0, **target_percents = 0;
2393 struct commands *cmds;
2394#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2395 struct file *prev_file = 0;
2396 enum multitarget_mode { m_unsettled, m_no, m_yes, m_yes_maybe }
2397 multi_mode = !two_colon && !pattern ? m_unsettled : m_no;
2398#endif
2399
2400 /* If we've already snapped deps, that means we're in an eval being
2401 resolved after the makefiles have been read in. We can't add more rules
2402 at this time, since they won't get snapped and we'll get core dumps.
2403 See Savannah bug # 12124. */
2404 if (snapped_deps)
2405 fatal (flocp, _("prerequisites cannot be defined in command scripts"));
2406
2407 if (commands_idx > 0)
2408 {
2409 cmds = xmalloc (sizeof (struct commands));
2410 cmds->fileinfo.filenm = flocp->filenm;
2411 cmds->fileinfo.lineno = cmds_started;
2412 cmds->commands = savestring (commands, commands_idx);
2413 cmds->command_lines = 0;
2414 }
2415 else
2416 cmds = 0;
2417
2418 for (; filenames != 0; filenames = nextf)
2419 {
2420 const char *name = filenames->name;
2421 struct file *f;
2422 struct dep *this = 0;
2423 const char *implicit_percent;
2424
2425 nextf = filenames->next;
2426 free (filenames);
2427
2428 /* Check for special targets. Do it here instead of, say, snap_deps()
2429 so that we can immediately use the value. */
2430
2431 if (streq (name, ".POSIX"))
2432 posix_pedantic = 1;
2433 else if (streq (name, ".SECONDEXPANSION"))
2434 second_expansion = 1;
2435#ifdef CONFIG_WITH_2ND_TARGET_EXPANSION
2436 else if (streq (name, ".SECONDTARGETEXPANSION"))
2437 second_target_expansion = 1;
2438#endif
2439
2440 implicit_percent = find_percent_cached (&name);
2441 implicit |= implicit_percent != 0;
2442
2443 if (implicit)
2444 {
2445 if (pattern != 0)
2446 fatal (flocp, _("mixed implicit and static pattern rules"));
2447
2448 if (implicit_percent == 0)
2449 fatal (flocp, _("mixed implicit and normal rules"));
2450
2451 if (targets == 0)
2452 {
2453 max_targets = 5;
2454 targets = xmalloc (5 * sizeof (char *));
2455 target_percents = xmalloc (5 * sizeof (char *));
2456 target_idx = 0;
2457 }
2458 else if (target_idx == max_targets - 1)
2459 {
2460 max_targets += 5;
2461 targets = xrealloc ((void *)targets, max_targets * sizeof (char *));
2462 target_percents = xrealloc ((void *)target_percents,
2463 max_targets * sizeof (char *));
2464 }
2465 targets[target_idx] = name;
2466 target_percents[target_idx] = implicit_percent;
2467 ++target_idx;
2468 continue;
2469 }
2470
2471#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2472 /* Check for the explicit multitarget mode operators. For this to be
2473 identified as an explicit multiple target rule, the first + or +|
2474 operator *must* appear between the first two files. If not found as
2475 the 2nd file or if found as the 1st file, the rule will be rejected
2476 as a potential multiple first target rule. For the subsequent files
2477 the operator is only required to switch between maybe and non-maybe
2478 mode:
2479 `primary + 2nd 3rd +| 4th-maybe + 5th-for-sure: deps; cmds'
2480
2481 The whole idea of the maybe-updated files is this:
2482 timestamp +| maybe.h: src1.c src2.c
2483 grep goes-into-maybe.h $* > timestamp
2484 cmp timestamp maybe.h || cp -f timestamp maybe.h
2485
2486 This is implemented in remake.c where we don't consider the mtime of
2487 the maybe-updated targets. */
2488 if (multi_mode != m_no && name[0] == '+'
2489 && (name[1] == '\0' || (name[1] == '|' && name[2] == '\0')))
2490 {
2491 if (!prev_file)
2492 multi_mode = m_no; /* first */
2493 else
2494 {
2495 if (multi_mode == m_unsettled)
2496 {
2497 prev_file->multi_head = prev_file;
2498
2499 /* Only the primary file needs the dependencies. */
2500 if (deps)
2501 {
2502 free_dep_chain (deps);
2503 deps = NULL;
2504 }
2505 }
2506 multi_mode = name[1] == '\0' ? m_yes : m_yes_maybe;
2507 continue;
2508 }
2509 }
2510 else if (multi_mode == m_unsettled && prev_file)
2511 multi_mode = m_no;
2512#endif
2513
2514 /* If this is a static pattern rule:
2515 `targets: target%pattern: dep%pattern; cmds',
2516 make sure the pattern matches this target name. */
2517 if (pattern && !pattern_matches (pattern, pattern_percent, name))
2518 error (flocp, _("target `%s' doesn't match the target pattern"), name);
2519 else if (deps)
2520 {
2521 /* If there are multiple filenames, copy the chain DEPS for all but
2522 the last one. It is not safe for the same deps to go in more
2523 than one place in the database. */
2524 this = nextf != 0 ? copy_dep_chain (deps) : deps;
2525 this->need_2nd_expansion = (second_expansion
2526 && strchr (this->name, '$'));
2527 }
2528
2529 if (!two_colon)
2530 {
2531 /* Single-colon. Combine these dependencies
2532 with others in file's existing record, if any. */
2533#ifndef KMK
2534 f = enter_file (strcache_add (name));
2535#else /* KMK - the name is already in the cache, don't waste time. */
2536 f = enter_file (name);
2537#endif
2538
2539 if (f->double_colon)
2540 fatal (flocp,
2541 _("target file `%s' has both : and :: entries"), f->name);
2542
2543 /* If CMDS == F->CMDS, this target was listed in this rule
2544 more than once. Just give a warning since this is harmless. */
2545 if (cmds != 0 && cmds == f->cmds)
2546 error (flocp,
2547 _("target `%s' given more than once in the same rule."),
2548 f->name);
2549
2550 /* Check for two single-colon entries both with commands.
2551 Check is_target so that we don't lose on files such as .c.o
2552 whose commands were preinitialized. */
2553 else if (cmds != 0 && f->cmds != 0 && f->is_target)
2554 {
2555 error (&cmds->fileinfo,
2556 _("warning: overriding commands for target `%s'"),
2557 f->name);
2558 error (&f->cmds->fileinfo,
2559 _("warning: ignoring old commands for target `%s'"),
2560 f->name);
2561 }
2562
2563 f->is_target = 1;
2564
2565 /* Defining .DEFAULT with no deps or cmds clears it. */
2566 if (f == default_file && this == 0 && cmds == 0)
2567 f->cmds = 0;
2568 if (cmds != 0)
2569 f->cmds = cmds;
2570
2571#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2572 /* If this is an explicit multi target rule, add it to the
2573 target chain and set the multi_maybe flag according to
2574 the current mode. */
2575
2576 if (multi_mode >= m_yes)
2577 {
2578 f->multi_maybe = multi_mode == m_yes_maybe;
2579 prev_file->multi_next = f;
2580 assert (prev_file->multi_head != 0);
2581 f->multi_head = prev_file->multi_head;
2582
2583 if (f == suffix_file)
2584 error (flocp,
2585 _(".SUFFIXES encountered in an explicit multi target rule"));
2586 }
2587 prev_file = f;
2588#endif
2589
2590 /* Defining .SUFFIXES with no dependencies clears out the list of
2591 suffixes. */
2592 if (f == suffix_file && this == 0)
2593 {
2594 free_dep_chain (f->deps);
2595 f->deps = 0;
2596 }
2597 else if (this != 0)
2598 {
2599 /* Add the file's old deps and the new ones in THIS together. */
2600
2601 if (f->deps != 0)
2602 {
2603 struct dep **d_ptr = &f->deps;
2604
2605 while ((*d_ptr)->next != 0)
2606 d_ptr = &(*d_ptr)->next;
2607
2608 if (cmds != 0)
2609 /* This is the rule with commands, so put its deps
2610 last. The rationale behind this is that $< expands to
2611 the first dep in the chain, and commands use $<
2612 expecting to get the dep that rule specifies. However
2613 the second expansion algorithm reverses the order thus
2614 we need to make it last here. */
2615 (*d_ptr)->next = this;
2616 else
2617 {
2618 /* This is the rule without commands. Put its
2619 dependencies at the end but before dependencies from
2620 the rule with commands (if any). This way everything
2621 appears in makefile order. */
2622
2623 if (f->cmds != 0)
2624 {
2625#ifndef KMK /* bugfix: Don't chop the chain! */
2626 this->next = *d_ptr;
2627 *d_ptr = this;
2628#else /* KMK */
2629 struct dep *this_last = this;
2630 while (this_last->next)
2631 this_last = this_last->next;
2632 this_last->next = *d_ptr;
2633 *d_ptr = this;
2634#endif /* KMK */
2635 }
2636 else
2637 (*d_ptr)->next = this;
2638 }
2639 }
2640 else
2641 f->deps = this;
2642
2643 /* This is a hack. I need a way to communicate to snap_deps()
2644 that the last dependency line in this file came with commands
2645 (so that logic in snap_deps() can put it in front and all
2646 this $< -logic works). I cannot simply rely on file->cmds
2647 being not 0 because of the cases like the following:
2648
2649 foo: bar
2650 foo:
2651 ...
2652
2653 I am going to temporarily "borrow" UPDATING member in
2654 `struct file' for this. */
2655
2656 if (cmds != 0)
2657 f->updating = 1;
2658 }
2659 }
2660 else
2661 {
2662 /* Double-colon. Make a new record even if there already is one. */
2663#ifndef KMK
2664 f = lookup_file (name);
2665#else /* KMK - the name is already in the cache, don't waste time. */
2666 f = lookup_file_cached (name);
2667#endif
2668
2669 /* Check for both : and :: rules. Check is_target so
2670 we don't lose on default suffix rules or makefiles. */
2671 if (f != 0 && f->is_target && !f->double_colon)
2672 fatal (flocp,
2673 _("target file `%s' has both : and :: entries"), f->name);
2674#ifndef KMK
2675 f = enter_file (strcache_add (name));
2676#else /* KMK - the name is already in the cache, don't waste time. */
2677 f = enter_file (name);
2678#endif
2679 /* If there was an existing entry and it was a double-colon entry,
2680 enter_file will have returned a new one, making it the prev
2681 pointer of the old one, and setting its double_colon pointer to
2682 the first one. */
2683 if (f->double_colon == 0)
2684 /* This is the first entry for this name, so we must set its
2685 double_colon pointer to itself. */
2686 f->double_colon = f;
2687 f->is_target = 1;
2688 f->deps = this;
2689 f->cmds = cmds;
2690 }
2691
2692 /* If this is a static pattern rule, set the stem to the part of its
2693 name that matched the `%' in the pattern, so you can use $* in the
2694 commands. */
2695 if (pattern)
2696 {
2697 static const char *percent = "%";
2698 char *buffer = variable_expand ("");
2699 const size_t buffer_offset = buffer - variable_buffer; /* bird */
2700 char *o = patsubst_expand_pat (buffer, name, pattern, percent,
2701 pattern_percent+1, percent+1);
2702 buffer = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. */
2703 f->stem = strcache_add_len (buffer, o - buffer);
2704 if (this)
2705 {
2706 this->staticpattern = 1;
2707 this->stem = f->stem;
2708 }
2709 }
2710
2711 name = f->name;
2712
2713 /* If this target is a default target, update DEFAULT_GOAL_FILE. */
2714 if (streq (*default_goal_name, name)
2715 && (default_goal_file == 0
2716 || ! streq (default_goal_file->name, name)))
2717 default_goal_file = f;
2718 }
2719
2720 if (implicit)
2721 {
2722 if (deps)
2723 deps->need_2nd_expansion = second_expansion;
2724 create_pattern_rule (targets, target_percents, target_idx,
2725 two_colon, deps, cmds, 1);
2726 }
2727}
2728
2729
2730/* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2731 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2732 Quoting backslashes are removed from STRING by compacting it into
2733 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2734 one, or nil if there are none. STOPCHARs inside variable references are
2735 ignored if IGNOREVARS is true.
2736
2737 STOPCHAR _cannot_ be '$' if IGNOREVARS is true. */
2738
2739#ifndef CONFIG_WITH_VALUE_LENGTH
2740static char *
2741find_char_unquote (char *string, int stop1, int stop2, int blank,
2742 int ignorevars)
2743#else
2744static char *
2745find_char_unquote_2 (char *string, int stop1, int stop2, int blank,
2746 int ignorevars, unsigned int string_len)
2747#endif
2748{
2749#ifndef CONFIG_WITH_VALUE_LENGTH
2750 unsigned int string_len = 0;
2751#endif
2752 char *p = string;
2753 register int ch; /* bird: 'optimiziations' */
2754#ifdef CONFIG_WITH_VALUE_LENGTH
2755 assert (string_len == 0 || string_len == strlen (string));
2756#endif
2757
2758 if (ignorevars)
2759 ignorevars = '$';
2760
2761 while (1)
2762 {
2763 if (stop2 && blank)
2764 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2
2765 && ! isblank ((unsigned char) ch))
2766 ++p;
2767 else if (stop2)
2768 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2)
2769 ++p;
2770 else if (blank)
2771 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1
2772 && ! isblank ((unsigned char) ch))
2773 ++p;
2774 else
2775 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1)
2776 ++p;
2777
2778 if (ch == '\0')
2779 break;
2780
2781 /* If we stopped due to a variable reference, skip over its contents. */
2782 if (ch == ignorevars)
2783 {
2784 char openparen = p[1];
2785
2786 p += 2;
2787
2788 /* Skip the contents of a non-quoted, multi-char variable ref. */
2789 if (openparen == '(' || openparen == '{')
2790 {
2791 unsigned int pcount = 1;
2792 char closeparen = (openparen == '(' ? ')' : '}');
2793
2794 while ((ch = *p))
2795 {
2796 if (ch == openparen)
2797 ++pcount;
2798 else if (ch == closeparen)
2799 if (--pcount == 0)
2800 {
2801 ++p;
2802 break;
2803 }
2804 ++p;
2805 }
2806 }
2807
2808 /* Skipped the variable reference: look for STOPCHARS again. */
2809 continue;
2810 }
2811
2812 if (p > string && p[-1] == '\\')
2813 {
2814 /* Search for more backslashes. */
2815 int i = -2;
2816 while (&p[i] >= string && p[i] == '\\')
2817 --i;
2818 ++i;
2819 /* Only compute the length if really needed. */
2820 if (string_len == 0)
2821 string_len = strlen (string);
2822 /* The number of backslashes is now -I.
2823 Copy P over itself to swallow half of them. */
2824 memmove (&p[i], &p[i/2], (string_len - (p - string)) - (i/2) + 1);
2825 p += i/2;
2826 if (i % 2 == 0)
2827 /* All the backslashes quoted each other; the STOPCHAR was
2828 unquoted. */
2829 return p;
2830
2831 /* The STOPCHAR was quoted by a backslash. Look for another. */
2832 }
2833 else
2834 /* No backslash in sight. */
2835 return p;
2836 }
2837
2838 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2839 return 0;
2840}
2841
2842#ifdef CONFIG_WITH_VALUE_LENGTH
2843/* Special case version of find_char_unquote that only takes stop1.
2844 This is so common that it makes a lot of sense to specialize this.
2845 */
2846__inline static char *
2847find_char_unquote_0 (char *string, int stop1, char **eosp)
2848{
2849 unsigned int string_len = *eosp - string;
2850 char *p = (char *)memchr (string, stop1, string_len);
2851 assert (strlen (string) == string_len);
2852 if (!p)
2853 return NULL;
2854 if (p <= string || p[-1] != '\\')
2855 return p;
2856
2857 p = find_char_unquote_2 (string, stop1, 0, 0, 0, string_len);
2858 *eosp = memchr (string, '\0', string_len);
2859 return p;
2860}
2861#endif
2862
2863/* Search PATTERN for an unquoted % and handle quoting. */
2864
2865char *
2866find_percent (char *pattern)
2867{
2868 return find_char_unquote (pattern, '%', 0, 0, 0);
2869}
2870
2871/* Search STRING for an unquoted % and handle quoting. Returns a pointer to
2872 the % or NULL if no % was found.
2873 This version is used with strings in the string cache: if there's a need to
2874 modify the string a new version will be added to the string cache and
2875 *STRING will be set to that. */
2876
2877const char *
2878find_percent_cached (const char **string)
2879{
2880 const char *p = *string;
2881 char *new = 0;
2882 int slen;
2883
2884 /* If the first char is a % return now. This lets us avoid extra tests
2885 inside the loop. */
2886 if (*p == '%')
2887 return p;
2888
2889 while (1)
2890 {
2891 while (*p != '\0' && *p != '%')
2892 ++p;
2893
2894 if (*p == '\0')
2895 break;
2896
2897 /* See if this % is escaped with a backslash; if not we're done. */
2898 if (p[-1] != '\\')
2899 break;
2900
2901 {
2902 /* Search for more backslashes. */
2903 char *pv;
2904 int i = -2;
2905
2906 while (&p[i] >= *string && p[i] == '\\')
2907 --i;
2908 ++i;
2909
2910 /* At this point we know we'll need to allocate a new string.
2911 Make a copy if we haven't yet done so. */
2912 if (! new)
2913 {
2914 slen = strlen (*string);
2915 new = alloca (slen + 1);
2916 memcpy (new, *string, slen + 1);
2917 p = new + (p - *string);
2918 *string = new;
2919 }
2920
2921 /* At this point *string, p, and new all point into the same string.
2922 Get a non-const version of p so we can modify new. */
2923 pv = new + (p - *string);
2924
2925 /* The number of backslashes is now -I.
2926 Copy P over itself to swallow half of them. */
2927 memmove (&pv[i], &pv[i/2], (slen - (pv - new)) - (i/2) + 1);
2928 p += i/2;
2929
2930 /* If the backslashes quoted each other; the % was unquoted. */
2931 if (i % 2 == 0)
2932 break;
2933 }
2934 }
2935
2936 /* If we had to change STRING, add it to the strcache. */
2937 if (new)
2938 {
2939 *string = strcache_add (*string);
2940 p = *string + (p - new);
2941 }
2942
2943 /* If we didn't find a %, return NULL. Otherwise return a ptr to it. */
2944 return (*p == '\0') ? NULL : p;
2945}
2946
2947
2948/* Parse a string into a sequence of filenames represented as a
2949 chain of struct nameseq's in reverse order and return that chain.
2950
2951 The string is passed as STRINGP, the address of a string pointer.
2952 The string pointer is updated to point at the first character
2953 not parsed, which either is a null char or equals STOPCHAR.
2954
2955 SIZE is how big to construct chain elements.
2956 This is useful if we want them actually to be other structures
2957 that have room for additional info.
2958
2959 If STRIP is nonzero, strip `./'s off the beginning. */
2960
2961struct nameseq *
2962parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
2963{
2964 struct nameseq *new = 0;
2965 struct nameseq *new1;
2966#ifndef NO_ARCHIVES /* bird: MSC warning */
2967 struct nameseq *lastnew1;
2968#endif
2969 char *p = *stringp;
2970
2971#ifdef VMS
2972# define VMS_COMMA ','
2973#else
2974# define VMS_COMMA 0
2975#endif
2976
2977 while (1)
2978 {
2979 const char *name;
2980 char *q;
2981
2982 /* Skip whitespace; see if any more names are left. */
2983 p = next_token (p);
2984 if (*p == '\0')
2985 break;
2986 if (*p == stopchar)
2987 break;
2988
2989 /* There are, so find the end of the next name. */
2990 q = p;
2991 p = find_char_unquote (q, stopchar, VMS_COMMA, 1, 0);
2992#ifdef VMS
2993 /* convert comma separated list to space separated */
2994 if (p && *p == ',')
2995 *p =' ';
2996#endif
2997#ifdef _AMIGA
2998 if (stopchar == ':' && p && *p == ':'
2999 && !(isspace ((unsigned char)p[1]) || !p[1]
3000 || isspace ((unsigned char)p[-1])))
3001 p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1, 0);
3002#endif
3003#ifdef HAVE_DOS_PATHS
3004 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
3005 first colon which isn't followed by a slash or a backslash.
3006 Note that tokens separated by spaces should be treated as separate
3007 tokens since make doesn't allow path names with spaces */
3008 if (stopchar == ':')
3009 while (p != 0 && !isspace ((unsigned char)*p) &&
3010 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
3011 p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1, 0);
3012#endif
3013 if (p == 0)
3014 p = q + strlen (q);
3015
3016 if (strip)
3017#ifdef VMS
3018 /* Skip leading `[]'s. */
3019 while (p - q > 2 && q[0] == '[' && q[1] == ']')
3020#else
3021 /* Skip leading `./'s. */
3022 while (p - q > 2 && q[0] == '.' && q[1] == '/')
3023#endif
3024 {
3025 q += 2; /* Skip "./". */
3026 while (q < p && *q == '/')
3027 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
3028 ++q;
3029 }
3030
3031 /* Extract the filename just found, and skip it. */
3032
3033 if (q == p)
3034 /* ".///" was stripped to "". */
3035#if defined(VMS)
3036 continue;
3037#elif defined(_AMIGA)
3038 name = "";
3039#else
3040 name = "./";
3041#endif
3042 else
3043#ifdef VMS
3044/* VMS filenames can have a ':' in them but they have to be '\'ed but we need
3045 * to remove this '\' before we can use the filename.
3046 * Savestring called because q may be read-only string constant.
3047 */
3048 {
3049 char *qbase = xstrdup (q);
3050 char *pbase = qbase + (p-q);
3051 char *q1 = qbase;
3052 char *q2 = q1;
3053 char *p1 = pbase;
3054
3055 while (q1 != pbase)
3056 {
3057 if (*q1 == '\\' && *(q1+1) == ':')
3058 {
3059 q1++;
3060 p1--;
3061 }
3062 *q2++ = *q1++;
3063 }
3064 name = strcache_add_len (qbase, p1 - qbase);
3065 free (qbase);
3066 }
3067#elif !defined(CONFIG_WITH_VALUE_LENGTH)
3068 name = strcache_add_len (q, p - q);
3069#else /* CONFIG_WITH_VALUE_LENGTH */
3070 {
3071 /* Make sure it's terminated, strcache_add_len has to make a
3072 temp copy on the stack otherwise. */
3073 char saved = *p;
3074 *p = '\0';
3075 name = strcache_add_len (q, p - q);
3076 *p = saved;
3077 }
3078#endif /* CONFIG_WITH_VALUE_LENGTH */
3079
3080 /* Add it to the front of the chain. */
3081 new1 = xmalloc (size);
3082 new1->name = name;
3083 new1->next = new;
3084 new = new1;
3085 }
3086
3087#ifndef NO_ARCHIVES
3088
3089 /* Look for multi-word archive references.
3090 They are indicated by a elt ending with an unmatched `)' and
3091 an elt further down the chain (i.e., previous in the file list)
3092 with an unmatched `(' (e.g., "lib(mem"). */
3093
3094 new1 = new;
3095 lastnew1 = 0;
3096 while (new1 != 0)
3097 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
3098 && new1->name[strlen (new1->name) - 1] == ')'
3099 && strchr (new1->name, '(') == 0)
3100 {
3101 /* NEW1 ends with a `)' but does not contain a `('.
3102 Look back for an elt with an opening `(' but no closing `)'. */
3103
3104 struct nameseq *n = new1->next, *lastn = new1;
3105 char *paren = 0;
3106 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
3107 {
3108 lastn = n;
3109 n = n->next;
3110 }
3111 if (n != 0
3112 /* Ignore something starting with `(', as that cannot actually
3113 be an archive-member reference (and treating it as such
3114 results in an empty file name, which causes much lossage). */
3115 && n->name[0] != '(')
3116 {
3117 /* N is the first element in the archive group.
3118 Its name looks like "lib(mem" (with no closing `)'). */
3119
3120 char *libname;
3121
3122 /* Copy "lib(" into LIBNAME. */
3123 ++paren;
3124 libname = alloca (paren - n->name + 1);
3125 memcpy (libname, n->name, paren - n->name);
3126 libname[paren - n->name] = '\0';
3127
3128 if (*paren == '\0')
3129 {
3130 /* N was just "lib(", part of something like "lib( a b)".
3131 Edit it out of the chain and free its storage. */
3132 lastn->next = n->next;
3133 free (n);
3134 /* LASTN->next is the new stopping elt for the loop below. */
3135 n = lastn->next;
3136 }
3137 else
3138 {
3139 /* Replace N's name with the full archive reference. */
3140 n->name = strcache_add (concat (libname, paren, ")"));
3141 }
3142
3143 if (new1->name[1] == '\0')
3144 {
3145 /* NEW1 is just ")", part of something like "lib(a b )".
3146 Omit it from the chain and free its storage. */
3147 if (lastnew1 == 0)
3148 new = new1->next;
3149 else
3150 lastnew1->next = new1->next;
3151 lastn = new1;
3152 new1 = new1->next;
3153 free (lastn);
3154 }
3155 else
3156 {
3157 /* Replace also NEW1->name, which already has closing `)'. */
3158 new1->name = strcache_add (concat (libname, new1->name, ""));
3159 new1 = new1->next;
3160 }
3161
3162 /* Trace back from NEW1 (the end of the list) until N
3163 (the beginning of the list), rewriting each name
3164 with the full archive reference. */
3165
3166 while (new1 != n)
3167 {
3168 new1->name = strcache_add (concat (libname, new1->name, ")"));
3169 lastnew1 = new1;
3170 new1 = new1->next;
3171 }
3172 }
3173 else
3174 {
3175 /* No frobnication happening. Just step down the list. */
3176 lastnew1 = new1;
3177 new1 = new1->next;
3178 }
3179 }
3180 else
3181 {
3182 lastnew1 = new1;
3183 new1 = new1->next;
3184 }
3185
3186#endif
3187
3188 *stringp = p;
3189 return new;
3190}
3191
3192
3193/* Find the next line of text in an eval buffer, combining continuation lines
3194 into one line.
3195 Return the number of actual lines read (> 1 if continuation lines).
3196 Returns -1 if there's nothing left in the buffer.
3197
3198 After this function, ebuf->buffer points to the first character of the
3199 line we just found.
3200 */
3201
3202/* Read a line of text from a STRING.
3203 Since we aren't really reading from a file, don't bother with linenumbers.
3204 */
3205
3206static unsigned long
3207readstring (struct ebuffer *ebuf)
3208{
3209 char *eol;
3210#ifdef CONFIG_WITH_VALUE_LENGTH
3211 char *end;
3212#endif
3213
3214 /* If there is nothing left in this buffer, return 0. */
3215 if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
3216 return -1;
3217
3218 /* Set up a new starting point for the buffer, and find the end of the
3219 next logical line (taking into account backslash/newline pairs). */
3220
3221 eol = ebuf->buffer = ebuf->bufnext;
3222#ifdef CONFIG_WITH_VALUE_LENGTH
3223 end = ebuf->bufstart + ebuf->size;
3224#endif
3225
3226 while (1)
3227 {
3228 int backslash = 0;
3229 char *bol = eol;
3230 char *p;
3231
3232 /* Find the next newline. At EOS, stop. */
3233#ifndef CONFIG_WITH_VALUE_LENGTH
3234 eol = p = strchr (eol , '\n');
3235#else
3236 p = (char *)memchr (eol, '\n', end - eol);
3237 assert (!memchr (eol, '\0', p != 0 ? p - eol : end - eol));
3238 eol = p;
3239#endif
3240 if (!eol)
3241 {
3242 ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
3243#ifdef CONFIG_WITH_VALUE_LENGTH
3244 ebuf->eol = end;
3245#endif
3246 return 0;
3247 }
3248
3249 /* Found a newline; if it's escaped continue; else we're done. */
3250 while (p > bol && *(--p) == '\\')
3251 backslash = !backslash;
3252 if (!backslash)
3253 break;
3254 ++eol;
3255 }
3256
3257 /* Overwrite the newline char. */
3258 *eol = '\0';
3259 ebuf->bufnext = eol+1;
3260#ifdef CONFIG_WITH_VALUE_LENGTH
3261 ebuf->eol = eol;
3262#endif
3263
3264 return 0;
3265}
3266
3267static long
3268readline (struct ebuffer *ebuf)
3269{
3270 char *p;
3271 char *end;
3272 char *start;
3273 long nlines = 0;
3274
3275 /* The behaviors between string and stream buffers are different enough to
3276 warrant different functions. Do the Right Thing. */
3277
3278 if (!ebuf->fp)
3279 return readstring (ebuf);
3280
3281 /* When reading from a file, we always start over at the beginning of the
3282 buffer for each new line. */
3283
3284 p = start = ebuf->bufstart;
3285 end = p + ebuf->size;
3286 *p = '\0';
3287#ifdef CONFIG_WITH_VALUE_LENGTH
3288 ebuf->eol = p;
3289#endif
3290
3291 while (fgets (p, end - p, ebuf->fp) != 0)
3292 {
3293 char *p2;
3294 unsigned long len;
3295 int backslash;
3296
3297 len = strlen (p);
3298 if (len == 0)
3299 {
3300 /* This only happens when the first thing on the line is a '\0'.
3301 It is a pretty hopeless case, but (wonder of wonders) Athena
3302 lossage strikes again! (xmkmf puts NULs in its makefiles.)
3303 There is nothing really to be done; we synthesize a newline so
3304 the following line doesn't appear to be part of this line. */
3305 error (&ebuf->floc,
3306 _("warning: NUL character seen; rest of line ignored"));
3307 p[0] = '\n';
3308 len = 1;
3309 }
3310
3311 /* Jump past the text we just read. */
3312 p += len;
3313
3314 /* If the last char isn't a newline, the whole line didn't fit into the
3315 buffer. Get some more buffer and try again. */
3316 if (p[-1] != '\n')
3317 goto more_buffer;
3318
3319 /* We got a newline, so add one to the count of lines. */
3320 ++nlines;
3321
3322#if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
3323 /* Check to see if the line was really ended with CRLF; if so ignore
3324 the CR. */
3325 if ((p - start) > 1 && p[-2] == '\r')
3326 {
3327 --p;
3328 p[-1] = '\n';
3329 }
3330#endif
3331
3332 backslash = 0;
3333 for (p2 = p - 2; p2 >= start; --p2)
3334 {
3335 if (*p2 != '\\')
3336 break;
3337 backslash = !backslash;
3338 }
3339
3340 if (!backslash)
3341 {
3342 p[-1] = '\0';
3343#ifdef CONFIG_WITH_VALUE_LENGTH
3344 ebuf->eol = p - 1;
3345#endif
3346 break;
3347 }
3348
3349 /* It was a backslash/newline combo. If we have more space, read
3350 another line. */
3351 if (end - p >= 80)
3352 {
3353#ifdef CONFIG_WITH_VALUE_LENGTH
3354 ebuf->eol = p;
3355#endif
3356 continue;
3357 }
3358
3359 /* We need more space at the end of our buffer, so realloc it.
3360 Make sure to preserve the current offset of p. */
3361 more_buffer:
3362 {
3363 unsigned long off = p - start;
3364 ebuf->size *= 2;
3365 start = ebuf->buffer = ebuf->bufstart = xrealloc (start, ebuf->size);
3366 p = start + off;
3367 end = start + ebuf->size;
3368 *p = '\0';
3369#ifdef CONFIG_WITH_VALUE_LENGTH
3370 ebuf->eol = p;
3371#endif
3372 }
3373 }
3374
3375 if (ferror (ebuf->fp))
3376 pfatal_with_name (ebuf->floc.filenm);
3377
3378 /* If we found some lines, return how many.
3379 If we didn't, but we did find _something_, that indicates we read the last
3380 line of a file with no final newline; return 1.
3381 If we read nothing, we're at EOF; return -1. */
3382
3383 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
3384}
3385
3386
3387/* Parse the next "makefile word" from the input buffer, and return info
3388 about it.
3389
3390 A "makefile word" is one of:
3391
3392 w_bogus Should never happen
3393 w_eol End of input
3394 w_static A static word; cannot be expanded
3395 w_variable A word containing one or more variables/functions
3396 w_colon A colon
3397 w_dcolon A double-colon
3398 w_semicolon A semicolon
3399 w_varassign A variable assignment operator (=, :=, +=, >=, or ?=)
3400
3401 Note that this function is only used when reading certain parts of the
3402 makefile. Don't use it where special rules hold sway (RHS of a variable,
3403 in a command list, etc.) */
3404
3405static enum make_word_type
3406get_next_mword (char *buffer, char *delim, char **startp, unsigned int *length)
3407{
3408 enum make_word_type wtype = w_bogus;
3409 char *p = buffer, *beg;
3410 char c;
3411
3412 /* Skip any leading whitespace. */
3413 while (isblank ((unsigned char)*p))
3414 ++p;
3415
3416 beg = p;
3417 c = *(p++);
3418 switch (c)
3419 {
3420 case '\0':
3421 wtype = w_eol;
3422 break;
3423
3424 case ';':
3425 wtype = w_semicolon;
3426 break;
3427
3428 case '=':
3429 wtype = w_varassign;
3430 break;
3431
3432 case ':':
3433 wtype = w_colon;
3434 switch (*p)
3435 {
3436 case ':':
3437 ++p;
3438 wtype = w_dcolon;
3439 break;
3440
3441 case '=':
3442 ++p;
3443 wtype = w_varassign;
3444 break;
3445 }
3446 break;
3447
3448 case '+':
3449 case '?':
3450#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3451 case '>':
3452#endif
3453 if (*p == '=')
3454 {
3455 ++p;
3456 wtype = w_varassign;
3457 break;
3458 }
3459
3460 default:
3461 if (delim && strchr (delim, c))
3462 wtype = w_static;
3463 break;
3464 }
3465
3466 /* Did we find something? If so, return now. */
3467 if (wtype != w_bogus)
3468 goto done;
3469
3470 /* This is some non-operator word. A word consists of the longest
3471 string of characters that doesn't contain whitespace, one of [:=#],
3472 or [?+]=, or one of the chars in the DELIM string. */
3473
3474 /* We start out assuming a static word; if we see a variable we'll
3475 adjust our assumptions then. */
3476 wtype = w_static;
3477
3478 /* We already found the first value of "c", above. */
3479 while (1)
3480 {
3481 char closeparen;
3482 int count;
3483
3484 switch (c)
3485 {
3486 case '\0':
3487 case ' ':
3488 case '\t':
3489 case '=':
3490 goto done_word;
3491
3492 case ':':
3493#ifdef HAVE_DOS_PATHS
3494 /* A word CAN include a colon in its drive spec. The drive
3495 spec is allowed either at the beginning of a word, or as part
3496 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
3497 if (!(p - beg >= 2
3498 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
3499 && (p - beg == 2 || p[-3] == '(')))
3500#endif
3501 goto done_word;
3502
3503 case '$':
3504 c = *(p++);
3505 if (c == '$')
3506 break;
3507
3508 /* This is a variable reference, so note that it's expandable.
3509 Then read it to the matching close paren. */
3510 wtype = w_variable;
3511
3512 if (c == '(')
3513 closeparen = ')';
3514 else if (c == '{')
3515 closeparen = '}';
3516 else
3517 /* This is a single-letter variable reference. */
3518 break;
3519
3520 for (count=0; *p != '\0'; ++p)
3521 {
3522 if (*p == c)
3523 ++count;
3524 else if (*p == closeparen && --count < 0)
3525 {
3526 ++p;
3527 break;
3528 }
3529 }
3530 break;
3531
3532 case '?':
3533 case '+':
3534#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3535 case '>':
3536#endif
3537 if (*p == '=')
3538 goto done_word;
3539 break;
3540
3541 case '\\':
3542 switch (*p)
3543 {
3544 case ':':
3545 case ';':
3546 case '=':
3547 case '\\':
3548 ++p;
3549 break;
3550 }
3551 break;
3552
3553 default:
3554 if (delim && strchr (delim, c))
3555 goto done_word;
3556 break;
3557 }
3558
3559 c = *(p++);
3560 }
3561 done_word:
3562 --p;
3563
3564 done:
3565 if (startp)
3566 *startp = beg;
3567 if (length)
3568 *length = p - beg;
3569 return wtype;
3570}
3571
3572
3573/* Construct the list of include directories
3574 from the arguments and the default list. */
3575
3576void
3577construct_include_path (const char **arg_dirs)
3578{
3579#ifdef VAXC /* just don't ask ... */
3580 stat_t stbuf;
3581#else
3582 struct stat stbuf;
3583#endif
3584 const char **dirs;
3585 const char **cpp;
3586 unsigned int idx;
3587
3588 /* Compute the number of pointers we need in the table. */
3589 idx = sizeof (default_include_directories) / sizeof (const char *);
3590 if (arg_dirs)
3591 for (cpp = arg_dirs; *cpp != 0; ++cpp)
3592 ++idx;
3593
3594#ifdef __MSDOS__
3595 /* Add one for $DJDIR. */
3596 ++idx;
3597#endif
3598#ifdef KMK
3599 /* Add one for the kBuild directory. */
3600 ++idx;
3601#endif
3602
3603 dirs = xmalloc (idx * sizeof (const char *));
3604
3605 idx = 0;
3606 max_incl_len = 0;
3607
3608 /* First consider any dirs specified with -I switches.
3609 Ignore any that don't exist. Remember the maximum string length. */
3610
3611 if (arg_dirs)
3612 while (*arg_dirs != 0)
3613 {
3614 const char *dir = *(arg_dirs++);
3615 char *expanded = 0;
3616 int e;
3617
3618 if (dir[0] == '~')
3619 {
3620 expanded = tilde_expand (dir);
3621 if (expanded != 0)
3622 dir = expanded;
3623 }
3624
3625 EINTRLOOP (e, stat (dir, &stbuf));
3626 if (e == 0 && S_ISDIR (stbuf.st_mode))
3627 {
3628 unsigned int len = strlen (dir);
3629 /* If dir name is written with trailing slashes, discard them. */
3630 while (len > 1 && dir[len - 1] == '/')
3631 --len;
3632 if (len > max_incl_len)
3633 max_incl_len = len;
3634 dirs[idx++] = strcache_add_len (dir, len);
3635 }
3636
3637 if (expanded)
3638 free (expanded);
3639 }
3640
3641 /* Now add the standard default dirs at the end. */
3642
3643#ifdef __MSDOS__
3644 {
3645 /* The environment variable $DJDIR holds the root of the DJGPP directory
3646 tree; add ${DJDIR}/include. */
3647 struct variable *djdir = lookup_variable ("DJDIR", 5);
3648
3649 if (djdir)
3650 {
3651 unsigned int len = strlen (djdir->value) + 8;
3652 char *defdir = alloca (len + 1);
3653
3654 strcat (strcpy (defdir, djdir->value), "/include");
3655 dirs[idx++] = strcache_add (defdir);
3656
3657 if (len > max_incl_len)
3658 max_incl_len = len;
3659 }
3660 }
3661#endif
3662#ifdef KMK
3663 /* Add $(KBUILD_PATH). */
3664 {
3665 size_t len = strlen (get_kbuild_path ());
3666 dirs[idx++] = strcache_add_len (get_kbuild_path (), len);
3667 if (len > max_incl_len)
3668 max_incl_len = len;
3669 }
3670#endif
3671
3672 for (cpp = default_include_directories; *cpp != 0; ++cpp)
3673 {
3674 int e;
3675
3676 EINTRLOOP (e, stat (*cpp, &stbuf));
3677 if (e == 0 && S_ISDIR (stbuf.st_mode))
3678 {
3679 unsigned int len = strlen (*cpp);
3680 /* If dir name is written with trailing slashes, discard them. */
3681 while (len > 1 && (*cpp)[len - 1] == '/')
3682 --len;
3683 if (len > max_incl_len)
3684 max_incl_len = len;
3685 dirs[idx++] = strcache_add_len (*cpp, len - 1);
3686 }
3687 }
3688
3689 dirs[idx] = 0;
3690
3691 /* Now add each dir to the .INCLUDE_DIRS variable. */
3692
3693 for (cpp = dirs; *cpp != 0; ++cpp)
3694 do_variable_definition (NILF, ".INCLUDE_DIRS", *cpp,
3695 o_default, f_append, 0);
3696
3697 include_directories = dirs;
3698}
3699
3700
3701/* Expand ~ or ~USER at the beginning of NAME.
3702 Return a newly malloc'd string or 0. */
3703
3704char *
3705tilde_expand (const char *name)
3706{
3707#ifndef VMS
3708 if (name[1] == '/' || name[1] == '\0')
3709 {
3710 extern char *getenv ();
3711 char *home_dir;
3712 int is_variable;
3713
3714 {
3715 /* Turn off --warn-undefined-variables while we expand HOME. */
3716 int save = warn_undefined_variables_flag;
3717 warn_undefined_variables_flag = 0;
3718
3719#ifndef CONFIG_WITH_VALUE_LENGTH
3720 home_dir = allocated_variable_expand ("$(HOME)");
3721#else
3722 home_dir = allocated_variable_expand_2 (STRING_SIZE_TUPLE("$(HOME)"), NULL);
3723#endif
3724
3725 warn_undefined_variables_flag = save;
3726 }
3727
3728 is_variable = home_dir[0] != '\0';
3729 if (!is_variable)
3730 {
3731 free (home_dir);
3732 home_dir = getenv ("HOME");
3733 }
3734# if !defined(_AMIGA) && !defined(WINDOWS32)
3735 if (home_dir == 0 || home_dir[0] == '\0')
3736 {
3737 extern char *getlogin ();
3738 char *logname = getlogin ();
3739 home_dir = 0;
3740 if (logname != 0)
3741 {
3742 struct passwd *p = getpwnam (logname);
3743 if (p != 0)
3744 home_dir = p->pw_dir;
3745 }
3746 }
3747# endif /* !AMIGA && !WINDOWS32 */
3748 if (home_dir != 0)
3749 {
3750 char *new = xstrdup (concat (home_dir, "", name + 1));
3751 if (is_variable)
3752 free (home_dir);
3753 return new;
3754 }
3755 }
3756# if !defined(_AMIGA) && !defined(WINDOWS32)
3757 else
3758 {
3759 struct passwd *pwent;
3760 char *userend = strchr (name + 1, '/');
3761 if (userend != 0)
3762 *userend = '\0';
3763 pwent = getpwnam (name + 1);
3764 if (pwent != 0)
3765 {
3766 if (userend == 0)
3767 return xstrdup (pwent->pw_dir);
3768 else
3769 return xstrdup (concat (pwent->pw_dir, "/", userend + 1));
3770 }
3771 else if (userend != 0)
3772 *userend = '/';
3773 }
3774# endif /* !AMIGA && !WINDOWS32 */
3775#endif /* !VMS */
3776 return 0;
3777}
3778
3779/* Given a chain of struct nameseq's describing a sequence of filenames,
3780 in reverse of the intended order, return a new chain describing the
3781 result of globbing the filenames. The new chain is in forward order.
3782 The links of the old chain are freed or used in the new chain.
3783 Likewise for the names in the old chain.
3784
3785 SIZE is how big to construct chain elements.
3786 This is useful if we want them actually to be other structures
3787 that have room for additional info. */
3788
3789struct nameseq *
3790multi_glob (struct nameseq *chain, unsigned int size)
3791{
3792 void dir_setup_glob (glob_t *);
3793 struct nameseq *new = 0;
3794 struct nameseq *old;
3795 struct nameseq *nexto;
3796 glob_t gl;
3797#if defined(KMK) || defined(__EMX__) /* speed optimization */
3798 int rc;
3799#endif
3800
3801 dir_setup_glob (&gl);
3802
3803 for (old = chain; old != 0; old = nexto)
3804 {
3805 const char *gname;
3806#ifndef NO_ARCHIVES
3807 char *arname = 0;
3808 char *memname = 0;
3809#endif
3810 nexto = old->next;
3811 gname = old->name;
3812
3813 if (gname[0] == '~')
3814 {
3815 char *newname = tilde_expand (old->name);
3816 if (newname != 0)
3817 gname = newname;
3818 }
3819
3820#ifndef NO_ARCHIVES
3821 if (ar_name (gname))
3822 {
3823 /* OLD->name is an archive member reference. Replace it with the
3824 archive file name, and save the member name in MEMNAME. We will
3825 glob on the archive name and then reattach MEMNAME later. */
3826 ar_parse_name (gname, &arname, &memname);
3827 gname = arname;
3828 }
3829#endif /* !NO_ARCHIVES */
3830
3831#if defined(KMK) || defined(__EMX__) /* speed optimization */
3832 if (!strpbrk(gname, "*?["))
3833 {
3834 gl.gl_pathc = 1;
3835 gl.gl_pathv = (char **)&gname;
3836 rc = 0;
3837 }
3838 else
3839 rc = glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl);
3840 switch (rc)
3841#else
3842 switch (glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
3843#endif
3844 {
3845 case 0: /* Success. */
3846 {
3847 int i = gl.gl_pathc;
3848 while (i-- > 0)
3849 {
3850#ifndef NO_ARCHIVES
3851 if (memname != 0)
3852 {
3853 /* Try to glob on MEMNAME within the archive. */
3854 struct nameseq *found
3855 = ar_glob (gl.gl_pathv[i], memname, size);
3856 if (! found)
3857 {
3858 /* No matches. Use MEMNAME as-is. */
3859 unsigned int alen = strlen (gl.gl_pathv[i]);
3860 unsigned int mlen = strlen (memname);
3861 char *name;
3862 struct nameseq *elt = xmalloc (size);
3863 memset (elt, '\0', size);
3864
3865 name = alloca (alen + 1 + mlen + 2);
3866 memcpy (name, gl.gl_pathv[i], alen);
3867 name[alen] = '(';
3868 memcpy (name+alen+1, memname, mlen);
3869 name[alen + 1 + mlen] = ')';
3870 name[alen + 1 + mlen + 1] = '\0';
3871 elt->name = strcache_add (name);
3872 elt->next = new;
3873 new = elt;
3874 }
3875 else
3876 {
3877 /* Find the end of the FOUND chain. */
3878 struct nameseq *f = found;
3879 while (f->next != 0)
3880 f = f->next;
3881
3882 /* Attach the chain being built to the end of the FOUND
3883 chain, and make FOUND the new NEW chain. */
3884 f->next = new;
3885 new = found;
3886 }
3887 }
3888 else
3889#endif /* !NO_ARCHIVES */
3890 {
3891 struct nameseq *elt = xmalloc (size);
3892 memset (elt, '\0', size);
3893 elt->name = strcache_add (gl.gl_pathv[i]);
3894 elt->next = new;
3895 new = elt;
3896 }
3897 }
3898#if defined(KMK) || defined(__EMX__) /* speed optimization */
3899 if (gl.gl_pathv != (char **)&gname)
3900#endif
3901 globfree (&gl);
3902 free (old);
3903 break;
3904 }
3905
3906 case GLOB_NOSPACE:
3907 fatal (NILF, _("virtual memory exhausted"));
3908 break;
3909
3910 default:
3911 old->next = new;
3912 new = old;
3913 break;
3914 }
3915
3916#ifndef NO_ARCHIVES
3917 if (arname)
3918 free (arname);
3919#endif
3920 }
3921
3922 return new;
3923}
3924
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