VirtualBox

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

Last change on this file since 1811 was 1811, checked in by bird, 16 years ago

kmk: More string length optimizations.

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

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