VirtualBox

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

Last change on this file since 1111 was 1109, checked in by bird, 17 years ago

New feature: if1of and ifn1of (CONFIG_WITH_SET_CONDITIONALS).

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