VirtualBox

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

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

kmk: more length optimizations.

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