VirtualBox

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

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

kmk: Allocation caches for nameseq, dep and idep. next: variable.

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