VirtualBox

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

Last change on this file since 2989 was 2857, checked in by bird, 9 years ago

updates

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