VirtualBox

source: kBuild/vendor/gnumake/2005-05-16/read.c

Last change on this file was 280, checked in by bird, 19 years ago

Current make snaphot, 2005-05-16.

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