VirtualBox

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

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

spaces (read.c)

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

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