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