VirtualBox

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

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

kmk_cc_exec.c: Sketched the basic makefile evaluation 'instructions'.

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