VirtualBox

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

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

kmk: strcache_get_len -> strcache2_get_len

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

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