VirtualBox

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

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

kmk: readstring - use memchr.

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