VirtualBox

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

Last change on this file since 2770 was 2770, checked in by bird, 10 years ago

string expansion debugged and enabled. fixed access-after-alloc bug in func_sort and could lead to heap corruption.

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

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