VirtualBox

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

Last change on this file since 2665 was 2591, checked in by bird, 13 years ago

kmk: Merged in changes from GNU make 3.82. Previous GNU make base version was gnumake-2008-10-28-CVS.

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

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