VirtualBox

source: kBuild/trunk/src/gmake/read.c@ 287

Last change on this file since 287 was 287, checked in by bird, 20 years ago

join + optimizations.

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