VirtualBox

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

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

read.c: don't allocate the ifdef/ifndef variable, just use the current variable buffer like below.

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