VirtualBox

source: kBuild/vendor/gnumake/2003-11-23/read.c@ 1158

Last change on this file since 1158 was 54, checked in by (none), 21 years ago

This commit was manufactured by cvs2svn to create branch 'GNU'.

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