VirtualBox

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

Last change on this file since 2767 was 2717, checked in by bird, 11 years ago

kmk: Hacking kBuild-define-*.

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

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