Fix HAVE_BROKEN_RESTART logic.
[make.git] / read.c
blobdd930f751b5354efdf7c98ae835d563a301c5357
1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 2002 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
9 any later version.
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
21 #include "make.h"
23 #include <assert.h>
25 #include <glob.h>
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"
37 #ifndef WINDOWS32
38 #ifndef _AMIGA
39 #ifndef VMS
40 #include <pwd.h>
41 #else
42 struct passwd *getpwnam PARAMS ((char *name));
43 #endif
44 #endif
45 #endif /* !WINDOWS32 */
47 /* A 'struct ebuffer' controls the origin of the makefile we are currently
48 eval'ing.
51 struct ebuffer
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). */
61 /* Types of "words" that can be read in a makefile. */
62 enum make_word_type
64 w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
65 w_comment, w_varassign
69 /* A `struct conditionals' contains the information describing
70 all the active conditionals in a makefile.
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. */
77 struct conditionals
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'? */
85 static struct conditionals toplevel_conditionals;
86 static struct conditionals *conditionals = &toplevel_conditionals;
89 /* Default directories to search for include files in */
91 static char *default_include_directories[] =
93 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
95 * This completely up to the user when they install MSVC or other packages.
96 * This is defined as a placeholder.
98 #define INCLUDEDIR "."
99 #endif
100 INCLUDEDIR,
101 #ifndef _AMIGA
102 "/usr/gnu/include",
103 "/usr/local/include",
104 "/usr/include",
105 #endif
109 /* List of directories to search for include files in */
111 static char **include_directories;
113 /* Maximum length of an element of the above. */
115 static unsigned int max_incl_len;
117 /* The filename and pointer to line number of the
118 makefile currently being read in. */
120 const struct floc *reading_file = 0;
122 /* The chain of makefiles read by read_makefile. */
124 static struct dep *read_makefiles = 0;
126 static int eval_makefile PARAMS ((char *filename, int flags));
127 static int eval PARAMS ((struct ebuffer *buffer, int flags));
129 static long readline PARAMS ((struct ebuffer *ebuf));
130 static void do_define PARAMS ((char *name, unsigned int namelen,
131 enum variable_origin origin,
132 struct ebuffer *ebuf));
133 static int conditional_line PARAMS ((char *line, const struct floc *flocp));
134 static 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));
139 static void record_target_var PARAMS ((struct nameseq *filenames, char *defn,
140 int two_colon,
141 enum variable_origin origin,
142 const struct floc *flocp));
143 static enum make_word_type get_next_mword PARAMS ((char *buffer, char *delim,
144 char **startp, unsigned int *length));
146 /* Read in all the makefiles and return the chain of their names. */
148 struct dep *
149 read_all_makefiles (makefiles)
150 char **makefiles;
152 unsigned int num_makefiles = 0;
154 /* Create *_LIST variables, to hold the makefiles, targets, and variables
155 we will be reading. */
157 define_variable ("MAKEFILE_LIST", sizeof ("MAKEFILE_LIST")-1, "", o_file, 0);
159 DB (DB_BASIC, (_("Reading makefiles...\n")));
161 /* If there's a non-null variable MAKEFILES, its value is a list of
162 files to read first thing. But don't let it prevent reading the
163 default makefiles and don't let the default goal come from there. */
166 char *value;
167 char *name, *p;
168 unsigned int length;
171 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
172 int save = warn_undefined_variables_flag;
173 warn_undefined_variables_flag = 0;
175 value = allocated_variable_expand ("$(MAKEFILES)");
177 warn_undefined_variables_flag = save;
180 /* Set NAME to the start of next token and LENGTH to its length.
181 MAKEFILES is updated for finding remaining tokens. */
182 p = value;
184 while ((name = find_next_token (&p, &length)) != 0)
186 if (*p != '\0')
187 *p++ = '\0';
188 name = xstrdup (name);
189 if (eval_makefile (name,
190 RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE) < 2)
191 free (name);
194 free (value);
197 /* Read makefiles specified with -f switches. */
199 if (makefiles != 0)
200 while (*makefiles != 0)
202 struct dep *tail = read_makefiles;
203 register struct dep *d;
205 if (! eval_makefile (*makefiles, 0))
206 perror_with_name ("", *makefiles);
208 /* Find the right element of read_makefiles. */
209 d = read_makefiles;
210 while (d->next != tail)
211 d = d->next;
213 /* Use the storage read_makefile allocates. */
214 *makefiles = dep_name (d);
215 ++num_makefiles;
216 ++makefiles;
219 /* If there were no -f switches, try the default names. */
221 if (num_makefiles == 0)
223 static char *default_makefiles[] =
224 #ifdef VMS
225 /* all lower case since readdir() (the vms version) 'lowercasifies' */
226 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
227 #else
228 #ifdef _AMIGA
229 { "GNUmakefile", "Makefile", "SMakefile", 0 };
230 #else /* !Amiga && !VMS */
231 { "GNUmakefile", "makefile", "Makefile", 0 };
232 #endif /* AMIGA */
233 #endif /* VMS */
234 register char **p = default_makefiles;
235 while (*p != 0 && !file_exists_p (*p))
236 ++p;
238 if (*p != 0)
240 if (! eval_makefile (*p, 0))
241 perror_with_name ("", *p);
243 else
245 /* No default makefile was found. Add the default makefiles to the
246 `read_makefiles' chain so they will be updated if possible. */
247 struct dep *tail = read_makefiles;
248 /* Add them to the tail, after any MAKEFILES variable makefiles. */
249 while (tail != 0 && tail->next != 0)
250 tail = tail->next;
251 for (p = default_makefiles; *p != 0; ++p)
253 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
254 d->name = 0;
255 d->file = enter_file (*p);
256 d->file->dontcare = 1;
257 d->ignore_mtime = 0;
258 /* Tell update_goal_chain to bail out as soon as this file is
259 made, and main not to die if we can't make this file. */
260 d->changed = RM_DONTCARE;
261 if (tail == 0)
262 read_makefiles = d;
263 else
264 tail->next = d;
265 tail = d;
267 if (tail != 0)
268 tail->next = 0;
272 return read_makefiles;
276 eval_makefile (filename, flags)
277 char *filename;
278 int flags;
280 struct dep *deps;
281 struct ebuffer ebuf;
282 const struct floc *curfile;
283 int makefile_errno;
284 int r;
286 ebuf.floc.filenm = filename;
287 ebuf.floc.lineno = 1;
289 if (ISDB (DB_VERBOSE))
291 printf (_("Reading makefile `%s'"), filename);
292 if (flags & RM_NO_DEFAULT_GOAL)
293 printf (_(" (no default goal)"));
294 if (flags & RM_INCLUDED)
295 printf (_(" (search path)"));
296 if (flags & RM_DONTCARE)
297 printf (_(" (don't care)"));
298 if (flags & RM_NO_TILDE)
299 printf (_(" (no ~ expansion)"));
300 puts ("...");
303 /* First, get a stream to read. */
305 /* Expand ~ in FILENAME unless it came from `include',
306 in which case it was already done. */
307 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
309 char *expanded = tilde_expand (filename);
310 if (expanded != 0)
311 filename = expanded;
314 ebuf.fp = fopen (filename, "r");
315 /* Save the error code so we print the right message later. */
316 makefile_errno = errno;
318 /* If the makefile wasn't found and it's either a makefile from
319 the `MAKEFILES' variable or an included makefile,
320 search the included makefile search path for this makefile. */
321 if (ebuf.fp == 0 && (flags & RM_INCLUDED) && *filename != '/')
323 register unsigned int i;
324 for (i = 0; include_directories[i] != 0; ++i)
326 char *name = concat (include_directories[i], "/", filename);
327 ebuf.fp = fopen (name, "r");
328 if (ebuf.fp == 0)
329 free (name);
330 else
332 filename = name;
333 break;
338 /* Add FILENAME to the chain of read makefiles. */
339 deps = (struct dep *) xmalloc (sizeof (struct dep));
340 deps->next = read_makefiles;
341 read_makefiles = deps;
342 deps->name = 0;
343 deps->file = lookup_file (filename);
344 if (deps->file == 0)
346 deps->file = enter_file (xstrdup (filename));
347 if (flags & RM_DONTCARE)
348 deps->file->dontcare = 1;
350 if (filename != ebuf.floc.filenm)
351 free (filename);
352 filename = deps->file->name;
353 deps->changed = flags;
354 deps->ignore_mtime = 0;
356 /* If the makefile can't be found at all, give up entirely. */
358 if (ebuf.fp == 0)
360 /* If we did some searching, errno has the error from the last
361 attempt, rather from FILENAME itself. Restore it in case the
362 caller wants to use it in a message. */
363 errno = makefile_errno;
364 return 0;
367 /* Add this makefile to the list. */
368 do_variable_definition (&ebuf.floc, "MAKEFILE_LIST", filename, o_file,
369 f_append, 0);
371 /* Evaluate the makefile */
373 ebuf.size = 200;
374 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = xmalloc (ebuf.size);
376 curfile = reading_file;
377 reading_file = &ebuf.floc;
379 r = eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
381 reading_file = curfile;
383 fclose (ebuf.fp);
385 free (ebuf.bufstart);
386 return r;
390 eval_buffer (buffer)
391 char *buffer;
393 struct ebuffer ebuf;
394 const struct floc *curfile;
395 int r;
397 /* Evaluate the buffer */
399 ebuf.size = strlen (buffer);
400 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = buffer;
401 ebuf.fp = NULL;
403 ebuf.floc = *reading_file;
405 curfile = reading_file;
406 reading_file = &ebuf.floc;
408 r = eval (&ebuf, 1);
410 reading_file = curfile;
412 return r;
416 /* Read file FILENAME as a makefile and add its contents to the data base.
418 SET_DEFAULT is true if we are allowed to set the default goal.
420 FILENAME is added to the `read_makefiles' chain.
422 Returns 0 if a file was not found or not read.
423 Returns 1 if FILENAME was found and read.
424 Returns 2 if FILENAME was read, and we kept a reference (don't free it). */
426 static int
427 eval (ebuf, set_default)
428 struct ebuffer *ebuf;
429 int set_default;
431 static char *collapsed = 0;
432 static unsigned int collapsed_length = 0;
433 unsigned int commands_len = 200;
434 char *commands;
435 unsigned int commands_idx = 0;
436 unsigned int cmds_started, tgts_started;
437 int ignoring = 0, in_ignored_define = 0;
438 int no_targets = 0; /* Set when reading a rule without targets. */
439 int have_sysv_atvar = 0;
440 struct nameseq *filenames = 0;
441 struct dep *deps = 0;
442 long nlines = 0;
443 int two_colon = 0;
444 char *pattern = 0, *pattern_percent;
445 struct floc *fstart;
446 struct floc fi;
448 #define record_waiting_files() \
449 do \
451 if (filenames != 0) \
453 fi.lineno = tgts_started; \
454 record_files (filenames, pattern, pattern_percent, deps, \
455 cmds_started, commands, commands_idx, two_colon, \
456 have_sysv_atvar, &fi, set_default); \
458 filenames = 0; \
459 commands_idx = 0; \
460 no_targets = 0; \
461 if (pattern) { free(pattern); pattern = 0; } \
462 } while (0)
464 pattern_percent = 0;
465 cmds_started = tgts_started = 1;
467 fstart = &ebuf->floc;
468 fi.filenm = ebuf->floc.filenm;
470 /* Loop over lines in the file.
471 The strategy is to accumulate target names in FILENAMES, dependencies
472 in DEPS and commands in COMMANDS. These are used to define a rule
473 when the start of the next rule (or eof) is encountered.
475 When you see a "continue" in the loop below, that means we are moving on
476 to the next line _without_ ending any rule that we happen to be working
477 with at the moment. If you see a "goto rule_complete", then the
478 statement we just parsed also finishes the previous rule. */
480 commands = xmalloc (200);
482 while (1)
484 int linelen;
485 char *line;
486 int len;
487 char *p;
488 char *p2;
490 /* Grab the next line to be evaluated */
491 ebuf->floc.lineno += nlines;
492 nlines = readline (ebuf);
494 /* If there is nothing left to eval, we're done. */
495 if (nlines < 0)
496 break;
498 /* If this line is empty, skip it. */
499 line = ebuf->buffer;
500 if (line[0] == '\0')
501 continue;
503 linelen = strlen (line);
505 /* Check for a shell command line first.
506 If it is not one, we can stop treating tab specially. */
507 if (line[0] == '\t')
509 if (no_targets)
510 /* Ignore the commands in a rule with no targets. */
511 continue;
513 /* If there is no preceding rule line, don't treat this line
514 as a command, even though it begins with a tab character.
515 SunOS 4 make appears to behave this way. */
517 if (filenames != 0)
519 if (ignoring)
520 /* Yep, this is a shell command, and we don't care. */
521 continue;
523 /* Append this command line to the line being accumulated. */
524 if (commands_idx == 0)
525 cmds_started = ebuf->floc.lineno;
527 if (linelen + 1 + commands_idx > commands_len)
529 commands_len = (linelen + 1 + commands_idx) * 2;
530 commands = xrealloc (commands, commands_len);
532 bcopy (line, &commands[commands_idx], linelen);
533 commands_idx += linelen;
534 commands[commands_idx++] = '\n';
536 continue;
540 /* This line is not a shell command line. Don't worry about tabs. */
542 if (collapsed_length < linelen+1)
544 collapsed_length = linelen+1;
545 if (collapsed != 0)
546 free (collapsed);
547 collapsed = (char *) xmalloc (collapsed_length);
549 strcpy (collapsed, line);
550 /* Collapse continuation lines. */
551 collapse_continuations (collapsed);
552 remove_comments (collapsed);
554 /* Compare a word, both length and contents. */
555 #define word1eq(s) (len == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
556 p = collapsed;
557 while (isspace ((unsigned char)*p))
558 ++p;
560 if (*p == '\0')
561 /* This line is completely empty--ignore it. */
562 continue;
564 /* Find the end of the first token. Note we don't need to worry about
565 * ":" here since we compare tokens by length (so "export" will never
566 * be equal to "export:").
568 for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
570 len = p2 - p;
572 /* Find the start of the second token. If it looks like a target or
573 variable definition it can't be a preprocessor token so skip
574 them--this allows variables/targets named `ifdef', `export', etc. */
575 while (isspace ((unsigned char)*p2))
576 ++p2;
578 if ((p2[0] == ':' || p2[0] == '+' || p2[0] == '=') && p2[1] == '\0')
580 /* It can't be a preprocessor token so skip it if we're ignoring */
581 if (ignoring)
582 continue;
584 goto skip_conditionals;
587 /* We must first check for conditional and `define' directives before
588 ignoring anything, since they control what we will do with
589 following lines. */
591 if (!in_ignored_define
592 && (word1eq ("ifdef") || word1eq ("ifndef")
593 || word1eq ("ifeq") || word1eq ("ifneq")
594 || word1eq ("else") || word1eq ("endif")))
596 int i = conditional_line (p, fstart);
597 if (i < 0)
598 fatal (fstart, _("invalid syntax in conditional"));
600 ignoring = i;
601 continue;
604 if (word1eq ("endef"))
606 if (!in_ignored_define)
607 fatal (fstart, _("extraneous `endef'"));
608 in_ignored_define = 0;
609 continue;
612 if (word1eq ("define"))
614 if (ignoring)
615 in_ignored_define = 1;
616 else
618 if (*p2 == '\0')
619 fatal (fstart, _("empty variable name"));
621 /* Let the variable name be the whole rest of the line,
622 with trailing blanks stripped (comments have already been
623 removed), so it could be a complex variable/function
624 reference that might contain blanks. */
625 p = strchr (p2, '\0');
626 while (isblank ((unsigned char)p[-1]))
627 --p;
628 do_define (p2, p - p2, o_file, ebuf);
630 continue;
633 if (word1eq ("override"))
635 if (*p2 == '\0')
636 error (fstart, _("empty `override' directive"));
638 if (strneq (p2, "define", 6)
639 && (isblank ((unsigned char)p2[6]) || p2[6] == '\0'))
641 if (ignoring)
642 in_ignored_define = 1;
643 else
645 p2 = next_token (p2 + 6);
646 if (*p2 == '\0')
647 fatal (fstart, _("empty variable name"));
649 /* Let the variable name be the whole rest of the line,
650 with trailing blanks stripped (comments have already been
651 removed), so it could be a complex variable/function
652 reference that might contain blanks. */
653 p = strchr (p2, '\0');
654 while (isblank ((unsigned char)p[-1]))
655 --p;
656 do_define (p2, p - p2, o_override, ebuf);
659 else if (!ignoring
660 && !try_variable_definition (fstart, p2, o_override, 0))
661 error (fstart, _("invalid `override' directive"));
663 continue;
666 if (ignoring)
667 /* Ignore the line. We continue here so conditionals
668 can appear in the middle of a rule. */
669 continue;
671 if (word1eq ("export"))
673 /* 'export' by itself causes everything to be exported. */
674 if (*p2 == '\0')
675 export_all_variables = 1;
676 else
678 struct variable *v;
680 v = try_variable_definition (fstart, p2, o_file, 0);
681 if (v != 0)
682 v->export = v_export;
683 else
685 unsigned int len;
686 for (p = find_next_token (&p2, &len); p != 0;
687 p = find_next_token (&p2, &len))
689 char *var;
690 int l;
692 /* Expand the thing we're looking up, so we can use
693 indirect and constructed variable names. */
694 p[len] = '\0';
695 var = allocated_variable_expand (p);
696 l = strlen (var);
698 v = lookup_variable (var, l);
699 if (v == 0)
700 v = define_variable_loc (var, l, "", o_file, 0,
701 fstart);
702 v->export = v_export;
703 free (var);
707 goto rule_complete;
710 if (word1eq ("unexport"))
712 if (*p2 == '\0')
713 export_all_variables = 0;
714 else
716 unsigned int len;
717 struct variable *v;
718 for (p = find_next_token (&p2, &len); p != 0;
719 p = find_next_token (&p2, &len))
721 char *var;
722 int l;
724 /* Expand the thing we're looking up, so we can use
725 indirect and constructed variable names. */
726 p[len] = '\0';
727 var = allocated_variable_expand (p);
728 l = strlen (var);
730 v = lookup_variable (var, l);
731 if (v == 0)
732 v = define_variable_loc (var, l, "", o_file, 0, fstart);
734 v->export = v_noexport;
736 free (var);
739 goto rule_complete;
742 skip_conditionals:
743 if (word1eq ("vpath"))
745 char *pattern;
746 unsigned int len;
747 p2 = variable_expand (p2);
748 p = find_next_token (&p2, &len);
749 if (p != 0)
751 pattern = savestring (p, len);
752 p = find_next_token (&p2, &len);
753 /* No searchpath means remove all previous
754 selective VPATH's with the same pattern. */
756 else
757 /* No pattern means remove all previous selective VPATH's. */
758 pattern = 0;
759 construct_vpath_list (pattern, p);
760 if (pattern != 0)
761 free (pattern);
763 goto rule_complete;
766 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
768 /* We have found an `include' line specifying a nested
769 makefile to be read at this point. */
770 struct conditionals *save;
771 struct conditionals new_conditionals;
772 struct nameseq *files;
773 /* "-include" (vs "include") says no error if the file does not
774 exist. "sinclude" is an alias for this from SGI. */
775 int noerror = (p[0] != 'i');
777 p = allocated_variable_expand (p2);
778 if (*p == '\0')
780 error (fstart,
781 _("no file name for `%sinclude'"), noerror ? "-" : "");
782 continue;
785 /* Parse the list of file names. */
786 p2 = p;
787 files = multi_glob (parse_file_seq (&p2, '\0',
788 sizeof (struct nameseq),
790 sizeof (struct nameseq));
791 free (p);
793 /* Save the state of conditionals and start
794 the included makefile with a clean slate. */
795 save = conditionals;
796 bzero ((char *) &new_conditionals, sizeof new_conditionals);
797 conditionals = &new_conditionals;
799 /* Record the rules that are waiting so they will determine
800 the default goal before those in the included makefile. */
801 record_waiting_files ();
803 /* Read each included makefile. */
804 while (files != 0)
806 struct nameseq *next = files->next;
807 char *name = files->name;
808 int r;
810 free ((char *)files);
811 files = next;
813 r = eval_makefile (name, (RM_INCLUDED | RM_NO_TILDE
814 | (noerror ? RM_DONTCARE : 0)));
815 if (!r)
817 if (!noerror)
818 error (fstart, "%s: %s", name, strerror (errno));
819 free (name);
823 /* Free any space allocated by conditional_line. */
824 if (conditionals->ignoring)
825 free (conditionals->ignoring);
826 if (conditionals->seen_else)
827 free (conditionals->seen_else);
829 /* Restore state. */
830 conditionals = save;
832 goto rule_complete;
835 if (try_variable_definition (fstart, p, o_file, 0))
836 /* This line has been dealt with. */
837 goto rule_complete;
839 if (line[0] == '\t')
841 p = collapsed; /* Ignore comments, etc. */
842 while (isblank ((unsigned char)*p))
843 ++p;
844 if (*p == '\0')
845 /* The line is completely blank; that is harmless. */
846 continue;
848 /* This line starts with a tab but was not caught above
849 because there was no preceding target, and the line
850 might have been usable as a variable definition.
851 But now we know it is definitely lossage. */
852 fatal(fstart, _("commands commence before first target"));
855 /* This line describes some target files. This is complicated by
856 the existence of target-specific variables, because we can't
857 expand the entire line until we know if we have one or not. So
858 we expand the line word by word until we find the first `:',
859 then check to see if it's a target-specific variable.
861 In this algorithm, `lb_next' will point to the beginning of the
862 unexpanded parts of the input buffer, while `p2' points to the
863 parts of the expanded buffer we haven't searched yet. */
866 enum make_word_type wtype;
867 enum variable_origin v_origin;
868 char *cmdleft, *semip, *lb_next;
869 unsigned int len, plen = 0;
870 char *colonp;
872 /* Record the previous rule. */
874 record_waiting_files ();
875 tgts_started = fstart->lineno;
877 /* Search the line for an unquoted ; that is not after an
878 unquoted #. */
879 cmdleft = find_char_unquote (line, ';', '#', 0);
880 if (cmdleft != 0 && *cmdleft == '#')
882 /* We found a comment before a semicolon. */
883 *cmdleft = '\0';
884 cmdleft = 0;
886 else if (cmdleft != 0)
887 /* Found one. Cut the line short there before expanding it. */
888 *(cmdleft++) = '\0';
889 semip = cmdleft;
891 collapse_continuations (line);
893 /* We can't expand the entire line, since if it's a per-target
894 variable we don't want to expand it. So, walk from the
895 beginning, expanding as we go, and looking for "interesting"
896 chars. The first word is always expandable. */
897 wtype = get_next_mword(line, NULL, &lb_next, &len);
898 switch (wtype)
900 case w_eol:
901 if (cmdleft != 0)
902 fatal(fstart, _("missing rule before commands"));
903 /* This line contained something but turned out to be nothing
904 but whitespace (a comment?). */
905 continue;
907 case w_colon:
908 case w_dcolon:
909 /* We accept and ignore rules without targets for
910 compatibility with SunOS 4 make. */
911 no_targets = 1;
912 continue;
914 default:
915 break;
918 p2 = variable_expand_string(NULL, lb_next, len);
919 while (1)
921 lb_next += len;
922 if (cmdleft == 0)
924 /* Look for a semicolon in the expanded line. */
925 cmdleft = find_char_unquote (p2, ';', 0, 0);
927 if (cmdleft != 0)
929 unsigned long p2_off = p2 - variable_buffer;
930 unsigned long cmd_off = cmdleft - variable_buffer;
931 char *pend = p2 + strlen(p2);
933 /* Append any remnants of lb, then cut the line short
934 at the semicolon. */
935 *cmdleft = '\0';
937 /* One school of thought says that you shouldn't expand
938 here, but merely copy, since now you're beyond a ";"
939 and into a command script. However, the old parser
940 expanded the whole line, so we continue that for
941 backwards-compatiblity. Also, it wouldn't be
942 entirely consistent, since we do an unconditional
943 expand below once we know we don't have a
944 target-specific variable. */
945 (void)variable_expand_string(pend, lb_next, (long)-1);
946 lb_next += strlen(lb_next);
947 p2 = variable_buffer + p2_off;
948 cmdleft = variable_buffer + cmd_off + 1;
952 colonp = find_char_unquote(p2, ':', 0, 0);
953 #ifdef HAVE_DOS_PATHS
954 /* The drive spec brain-damage strikes again... */
955 /* Note that the only separators of targets in this context
956 are whitespace and a left paren. If others are possible,
957 they should be added to the string in the call to index. */
958 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
959 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
960 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
961 colonp = find_char_unquote(colonp + 1, ':', 0, 0);
962 #endif
963 if (colonp != 0)
964 break;
966 wtype = get_next_mword(lb_next, NULL, &lb_next, &len);
967 if (wtype == w_eol)
968 break;
970 p2 += strlen(p2);
971 *(p2++) = ' ';
972 p2 = variable_expand_string(p2, lb_next, len);
973 /* We don't need to worry about cmdleft here, because if it was
974 found in the variable_buffer the entire buffer has already
975 been expanded... we'll never get here. */
978 p2 = next_token (variable_buffer);
980 /* If the word we're looking at is EOL, see if there's _anything_
981 on the line. If not, a variable expanded to nothing, so ignore
982 it. If so, we can't parse this line so punt. */
983 if (wtype == w_eol)
985 if (*p2 != '\0')
986 /* There's no need to be ivory-tower about this: check for
987 one of the most common bugs found in makefiles... */
988 fatal (fstart, _("missing separator%s"),
989 !strneq(line, " ", 8) ? ""
990 : _(" (did you mean TAB instead of 8 spaces?)"));
991 continue;
994 /* Make the colon the end-of-string so we know where to stop
995 looking for targets. */
996 *colonp = '\0';
997 filenames = multi_glob (parse_file_seq (&p2, '\0',
998 sizeof (struct nameseq),
1000 sizeof (struct nameseq));
1001 *p2 = ':';
1003 if (!filenames)
1005 /* We accept and ignore rules without targets for
1006 compatibility with SunOS 4 make. */
1007 no_targets = 1;
1008 continue;
1010 /* This should never be possible; we handled it above. */
1011 assert (*p2 != '\0');
1012 ++p2;
1014 /* Is this a one-colon or two-colon entry? */
1015 two_colon = *p2 == ':';
1016 if (two_colon)
1017 p2++;
1019 /* Test to see if it's a target-specific variable. Copy the rest
1020 of the buffer over, possibly temporarily (we'll expand it later
1021 if it's not a target-specific variable). PLEN saves the length
1022 of the unparsed section of p2, for later. */
1023 if (*lb_next != '\0')
1025 unsigned int l = p2 - variable_buffer;
1026 plen = strlen (p2);
1027 (void) variable_buffer_output (p2+plen,
1028 lb_next, strlen (lb_next)+1);
1029 p2 = variable_buffer + l;
1032 /* See if it's an "override" keyword; if so see if what comes after
1033 it looks like a variable definition. */
1035 wtype = get_next_mword (p2, NULL, &p, &len);
1037 v_origin = o_file;
1038 if (wtype == w_static && word1eq ("override"))
1040 v_origin = o_override;
1041 wtype = get_next_mword (p+len, NULL, &p, &len);
1044 if (wtype != w_eol)
1045 wtype = get_next_mword (p+len, NULL, NULL, NULL);
1047 if (wtype == w_varassign)
1049 /* If there was a semicolon found, add it back, plus anything
1050 after it. */
1051 if (semip)
1053 *(--semip) = ';';
1054 variable_buffer_output (p2 + strlen (p2),
1055 semip, strlen (semip)+1);
1057 record_target_var (filenames, p, two_colon, v_origin, fstart);
1058 filenames = 0;
1059 continue;
1062 /* This is a normal target, _not_ a target-specific variable.
1063 Unquote any = in the dependency list. */
1064 find_char_unquote (lb_next, '=', 0, 0);
1066 /* We have some targets, so don't ignore the following commands. */
1067 no_targets = 0;
1069 /* Expand the dependencies, etc. */
1070 if (*lb_next != '\0')
1072 unsigned int l = p2 - variable_buffer;
1073 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
1074 p2 = variable_buffer + l;
1076 /* Look for a semicolon in the expanded line. */
1077 if (cmdleft == 0)
1079 cmdleft = find_char_unquote (p2, ';', 0, 0);
1080 if (cmdleft != 0)
1081 *(cmdleft++) = '\0';
1085 /* Do any of the prerequisites appear to have $@ etc.? */
1086 have_sysv_atvar = 0;
1087 if (!posix_pedantic)
1088 for (p = strchr (p2, '$'); p != 0; p = strchr (p+1, '$'))
1089 if (p[1] == '@' || (p[1] == '(' && p[2] == '@'))
1091 have_sysv_atvar = 1;
1092 break;
1095 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
1096 p = strchr (p2, ':');
1097 while (p != 0 && p[-1] == '\\')
1099 register char *q = &p[-1];
1100 register int backslash = 0;
1101 while (*q-- == '\\')
1102 backslash = !backslash;
1103 if (backslash)
1104 p = strchr (p + 1, ':');
1105 else
1106 break;
1108 #ifdef _AMIGA
1109 /* Here, the situation is quite complicated. Let's have a look
1110 at a couple of targets:
1112 install: dev:make
1114 dev:make: make
1116 dev:make:: xyz
1118 The rule is that it's only a target, if there are TWO :'s
1119 OR a space around the :.
1121 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
1122 || isspace ((unsigned char)p[-1])))
1123 p = 0;
1124 #endif
1125 #ifdef HAVE_DOS_PATHS
1127 int check_again;
1129 do {
1130 check_again = 0;
1131 /* For DOS paths, skip a "C:\..." or a "C:/..." */
1132 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
1133 isalpha ((unsigned char)p[-1]) &&
1134 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1135 p = strchr (p + 1, ':');
1136 check_again = 1;
1138 } while (check_again);
1140 #endif
1141 if (p != 0)
1143 struct nameseq *target;
1144 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
1145 ++p2;
1146 if (target == 0)
1147 fatal (fstart, _("missing target pattern"));
1148 else if (target->next != 0)
1149 fatal (fstart, _("multiple target patterns"));
1150 pattern = target->name;
1151 pattern_percent = find_percent (pattern);
1152 if (pattern_percent == 0)
1153 fatal (fstart, _("target pattern contains no `%%'"));
1154 free((char *)target);
1156 else
1157 pattern = 0;
1159 /* Parse the dependencies. */
1160 deps = (struct dep *)
1161 multi_glob (parse_file_seq (&p2, '|', sizeof (struct dep), 1),
1162 sizeof (struct dep));
1163 if (*p2)
1165 /* Files that follow '|' are special prerequisites that
1166 need only exist in order to satisfy the dependency.
1167 Their modification times are irrelevant. */
1168 struct dep **deps_ptr = &deps;
1169 struct dep *d;
1170 for (deps_ptr = &deps; *deps_ptr; deps_ptr = &(*deps_ptr)->next)
1172 ++p2;
1173 *deps_ptr = (struct dep *)
1174 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
1175 sizeof (struct dep));
1176 for (d = *deps_ptr; d != 0; d = d->next)
1177 d->ignore_mtime = 1;
1180 commands_idx = 0;
1181 if (cmdleft != 0)
1183 /* Semicolon means rest of line is a command. */
1184 unsigned int len = strlen (cmdleft);
1186 cmds_started = fstart->lineno;
1188 /* Add this command line to the buffer. */
1189 if (len + 2 > commands_len)
1191 commands_len = (len + 2) * 2;
1192 commands = (char *) xrealloc (commands, commands_len);
1194 bcopy (cmdleft, commands, len);
1195 commands_idx += len;
1196 commands[commands_idx++] = '\n';
1199 continue;
1202 /* We get here except in the case that we just read a rule line.
1203 Record now the last rule we read, so following spurious
1204 commands are properly diagnosed. */
1205 rule_complete:
1206 record_waiting_files ();
1209 #undef word1eq
1211 if (conditionals->if_cmds)
1212 fatal (fstart, _("missing `endif'"));
1214 /* At eof, record the last rule. */
1215 record_waiting_files ();
1217 free ((char *) commands);
1219 return 1;
1223 /* Execute a `define' directive.
1224 The first line has already been read, and NAME is the name of
1225 the variable to be defined. The following lines remain to be read. */
1227 static void
1228 do_define (name, namelen, origin, ebuf)
1229 char *name;
1230 unsigned int namelen;
1231 enum variable_origin origin;
1232 struct ebuffer *ebuf;
1234 struct floc defstart;
1235 long nlines = 0;
1236 int nlevels = 1;
1237 unsigned int length = 100;
1238 char *definition = (char *) xmalloc (length);
1239 unsigned int idx = 0;
1240 char *p;
1242 /* Expand the variable name. */
1243 char *var = (char *) alloca (namelen + 1);
1244 bcopy (name, var, namelen);
1245 var[namelen] = '\0';
1246 var = variable_expand (var);
1248 defstart = ebuf->floc;
1250 while (1)
1252 unsigned int len;
1253 char *line;
1255 ebuf->floc.lineno += nlines;
1256 nlines = readline (ebuf);
1258 /* If there is nothing left to eval, we're done. */
1259 if (nlines < 0)
1260 break;
1262 line = ebuf->buffer;
1264 collapse_continuations (line);
1266 /* If the line doesn't begin with a tab, test to see if it introduces
1267 another define, or ends one. */
1269 /* Stop if we find an 'endef' */
1270 if (line[0] != '\t')
1272 p = next_token (line);
1273 len = strlen (p);
1275 /* If this is another 'define', increment the level count. */
1276 if ((len == 6 || (len > 6 && isblank ((unsigned char)p[6])))
1277 && strneq (p, "define", 6))
1278 ++nlevels;
1280 /* If this is an 'endef', decrement the count. If it's now 0,
1281 we've found the last one. */
1282 else if ((len == 5 || (len > 5 && isblank ((unsigned char)p[5])))
1283 && strneq (p, "endef", 5))
1285 p += 5;
1286 remove_comments (p);
1287 if (*next_token (p) != '\0')
1288 error (&ebuf->floc,
1289 _("Extraneous text after `endef' directive"));
1291 if (--nlevels == 0)
1293 /* Define the variable. */
1294 if (idx == 0)
1295 definition[0] = '\0';
1296 else
1297 definition[idx - 1] = '\0';
1299 /* Always define these variables in the global set. */
1300 define_variable_global (var, strlen (var), definition,
1301 origin, 1, &defstart);
1302 free (definition);
1303 return;
1308 /* Otherwise add this line to the variable definition. */
1309 len = strlen (line);
1310 if (idx + len + 1 > length)
1312 length = (idx + len) * 2;
1313 definition = (char *) xrealloc (definition, length + 1);
1316 bcopy (line, &definition[idx], len);
1317 idx += len;
1318 /* Separate lines with a newline. */
1319 definition[idx++] = '\n';
1322 /* No `endef'!! */
1323 fatal (&defstart, _("missing `endef', unterminated `define'"));
1325 /* NOTREACHED */
1326 return;
1329 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1330 "ifneq", "else" and "endif".
1331 LINE is the input line, with the command as its first word.
1333 FILENAME and LINENO are the filename and line number in the
1334 current makefile. They are used for error messages.
1336 Value is -1 if the line is invalid,
1337 0 if following text should be interpreted,
1338 1 if following text should be ignored. */
1340 static int
1341 conditional_line (line, flocp)
1342 char *line;
1343 const struct floc *flocp;
1345 int notdef;
1346 char *cmdname;
1347 register unsigned int i;
1349 if (*line == 'i')
1351 /* It's an "if..." command. */
1352 notdef = line[2] == 'n';
1353 if (notdef)
1355 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
1356 line += cmdname[3] == 'd' ? 7 : 6;
1358 else
1360 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
1361 line += cmdname[2] == 'd' ? 6 : 5;
1364 else
1366 /* It's an "else" or "endif" command. */
1367 notdef = line[1] == 'n';
1368 cmdname = notdef ? "endif" : "else";
1369 line += notdef ? 5 : 4;
1372 line = next_token (line);
1374 if (*cmdname == 'e')
1376 if (*line != '\0')
1377 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1378 /* "Else" or "endif". */
1379 if (conditionals->if_cmds == 0)
1380 fatal (flocp, _("extraneous `%s'"), cmdname);
1381 /* NOTDEF indicates an `endif' command. */
1382 if (notdef)
1383 --conditionals->if_cmds;
1384 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1385 fatal (flocp, _("only one `else' per conditional"));
1386 else
1388 /* Toggle the state of ignorance. */
1389 conditionals->ignoring[conditionals->if_cmds - 1]
1390 = !conditionals->ignoring[conditionals->if_cmds - 1];
1391 /* Record that we have seen an `else' in this conditional.
1392 A second `else' will be erroneous. */
1393 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1395 for (i = 0; i < conditionals->if_cmds; ++i)
1396 if (conditionals->ignoring[i])
1397 return 1;
1398 return 0;
1401 if (conditionals->allocated == 0)
1403 conditionals->allocated = 5;
1404 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1405 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1408 ++conditionals->if_cmds;
1409 if (conditionals->if_cmds > conditionals->allocated)
1411 conditionals->allocated += 5;
1412 conditionals->ignoring = (char *)
1413 xrealloc (conditionals->ignoring, conditionals->allocated);
1414 conditionals->seen_else = (char *)
1415 xrealloc (conditionals->seen_else, conditionals->allocated);
1418 /* Record that we have seen an `if...' but no `else' so far. */
1419 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1421 /* Search through the stack to see if we're already ignoring. */
1422 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1423 if (conditionals->ignoring[i])
1425 /* We are already ignoring, so just push a level
1426 to match the next "else" or "endif", and keep ignoring.
1427 We don't want to expand variables in the condition. */
1428 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1429 return 1;
1432 if (cmdname[notdef ? 3 : 2] == 'd')
1434 /* "Ifdef" or "ifndef". */
1435 char *var;
1436 struct variable *v;
1437 register char *p = end_of_token (line);
1438 i = p - line;
1439 p = next_token (p);
1440 if (*p != '\0')
1441 return -1;
1443 /* Expand the thing we're looking up, so we can use indirect and
1444 constructed variable names. */
1445 line[i] = '\0';
1446 var = allocated_variable_expand (line);
1448 v = lookup_variable (var, strlen (var));
1449 conditionals->ignoring[conditionals->if_cmds - 1]
1450 = (v != 0 && *v->value != '\0') == notdef;
1452 free (var);
1454 else
1456 /* "Ifeq" or "ifneq". */
1457 char *s1, *s2;
1458 unsigned int len;
1459 char termin = *line == '(' ? ',' : *line;
1461 if (termin != ',' && termin != '"' && termin != '\'')
1462 return -1;
1464 s1 = ++line;
1465 /* Find the end of the first string. */
1466 if (termin == ',')
1468 register int count = 0;
1469 for (; *line != '\0'; ++line)
1470 if (*line == '(')
1471 ++count;
1472 else if (*line == ')')
1473 --count;
1474 else if (*line == ',' && count <= 0)
1475 break;
1477 else
1478 while (*line != '\0' && *line != termin)
1479 ++line;
1481 if (*line == '\0')
1482 return -1;
1484 if (termin == ',')
1486 /* Strip blanks after the first string. */
1487 char *p = line++;
1488 while (isblank ((unsigned char)p[-1]))
1489 --p;
1490 *p = '\0';
1492 else
1493 *line++ = '\0';
1495 s2 = variable_expand (s1);
1496 /* We must allocate a new copy of the expanded string because
1497 variable_expand re-uses the same buffer. */
1498 len = strlen (s2);
1499 s1 = (char *) alloca (len + 1);
1500 bcopy (s2, s1, len + 1);
1502 if (termin != ',')
1503 /* Find the start of the second string. */
1504 line = next_token (line);
1506 termin = termin == ',' ? ')' : *line;
1507 if (termin != ')' && termin != '"' && termin != '\'')
1508 return -1;
1510 /* Find the end of the second string. */
1511 if (termin == ')')
1513 register int count = 0;
1514 s2 = next_token (line);
1515 for (line = s2; *line != '\0'; ++line)
1517 if (*line == '(')
1518 ++count;
1519 else if (*line == ')')
1521 if (count <= 0)
1522 break;
1523 else
1524 --count;
1528 else
1530 ++line;
1531 s2 = line;
1532 while (*line != '\0' && *line != termin)
1533 ++line;
1536 if (*line == '\0')
1537 return -1;
1539 *line = '\0';
1540 line = next_token (++line);
1541 if (*line != '\0')
1542 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1544 s2 = variable_expand (s2);
1545 conditionals->ignoring[conditionals->if_cmds - 1]
1546 = streq (s1, s2) == notdef;
1549 /* Search through the stack to see if we're ignoring. */
1550 for (i = 0; i < conditionals->if_cmds; ++i)
1551 if (conditionals->ignoring[i])
1552 return 1;
1553 return 0;
1556 /* Remove duplicate dependencies in CHAIN. */
1558 static unsigned long
1559 dep_hash_1 (void const *key)
1561 return_STRING_HASH_1 (dep_name ((struct dep const *) key));
1564 static unsigned long
1565 dep_hash_2 (void const *key)
1567 return_STRING_HASH_2 (dep_name ((struct dep const *) key));
1570 static int
1571 dep_hash_cmp (void const *x, void const *y)
1573 struct dep *dx = (struct dep *) x;
1574 struct dep *dy = (struct dep *) y;
1575 int cmp = strcmp (dep_name (dx), dep_name (dy));
1577 /* If the names are the same but ignore_mtimes are not equal, one of these
1578 is an order-only prerequisite and one isn't. That means that we should
1579 remove the one that isn't and keep the one that is. */
1581 if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
1582 dx->ignore_mtime = dy->ignore_mtime = 0;
1584 return cmp;
1588 void
1589 uniquize_deps (chain)
1590 struct dep *chain;
1592 struct hash_table deps;
1593 register struct dep **depp;
1595 hash_init (&deps, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
1597 /* Make sure that no dependencies are repeated. This does not
1598 really matter for the purpose of updating targets, but it
1599 might make some names be listed twice for $^ and $?. */
1601 depp = &chain;
1602 while (*depp)
1604 struct dep *dep = *depp;
1605 struct dep **dep_slot = (struct dep **) hash_find_slot (&deps, dep);
1606 if (HASH_VACANT (*dep_slot))
1608 hash_insert_at (&deps, dep, dep_slot);
1609 depp = &dep->next;
1611 else
1613 /* Don't bother freeing duplicates.
1614 It's dangerous and little benefit accrues. */
1615 *depp = dep->next;
1619 hash_free (&deps, 0);
1622 /* Record target-specific variable values for files FILENAMES.
1623 TWO_COLON is nonzero if a double colon was used.
1625 The links of FILENAMES are freed, and so are any names in it
1626 that are not incorporated into other data structures.
1628 If the target is a pattern, add the variable to the pattern-specific
1629 variable value list. */
1631 static void
1632 record_target_var (filenames, defn, two_colon, origin, flocp)
1633 struct nameseq *filenames;
1634 char *defn;
1635 int two_colon;
1636 enum variable_origin origin;
1637 const struct floc *flocp;
1639 struct nameseq *nextf;
1640 struct variable_set_list *global;
1642 global = current_variable_set_list;
1644 /* If the variable is an append version, store that but treat it as a
1645 normal recursive variable. */
1647 for (; filenames != 0; filenames = nextf)
1649 struct variable *v;
1650 register char *name = filenames->name;
1651 struct variable_set_list *vlist;
1652 char *fname;
1653 char *percent;
1655 nextf = filenames->next;
1656 free ((char *) filenames);
1658 /* If it's a pattern target, then add it to the pattern-specific
1659 variable list. */
1660 percent = find_percent (name);
1661 if (percent)
1663 struct pattern_var *p;
1665 /* Get a reference for this pattern-specific variable struct. */
1666 p = create_pattern_var(name, percent);
1667 vlist = p->vars;
1668 fname = p->target;
1670 else
1672 struct file *f;
1674 /* Get a file reference for this file, and initialize it. */
1675 f = enter_file (name);
1676 initialize_file_variables (f, 1);
1677 vlist = f->variables;
1678 fname = f->name;
1681 /* Make the new variable context current and define the variable. */
1682 current_variable_set_list = vlist;
1683 v = try_variable_definition (flocp, defn, origin, 1);
1684 if (!v)
1685 error (flocp, _("Malformed per-target variable definition"));
1686 v->per_target = 1;
1688 /* If it's not an override, check to see if there was a command-line
1689 setting. If so, reset the value. */
1690 if (origin != o_override)
1692 struct variable *gv;
1693 int len = strlen(v->name);
1695 current_variable_set_list = global;
1696 gv = lookup_variable (v->name, len);
1697 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1699 v = define_variable_in_set (v->name, len, gv->value, gv->origin,
1700 gv->recursive, vlist->set, flocp);
1701 v->append = 0;
1705 /* Free name if not needed further. */
1706 if (name != fname && (name < fname || name > fname + strlen (fname)))
1707 free (name);
1710 current_variable_set_list = global;
1713 /* Record a description line for files FILENAMES,
1714 with dependencies DEPS, commands to execute described
1715 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1716 TWO_COLON is nonzero if a double colon was used.
1717 If not nil, PATTERN is the `%' pattern to make this
1718 a static pattern rule, and PATTERN_PERCENT is a pointer
1719 to the `%' within it.
1721 The links of FILENAMES are freed, and so are any names in it
1722 that are not incorporated into other data structures. */
1724 static void
1725 record_files (filenames, pattern, pattern_percent, deps, cmds_started,
1726 commands, commands_idx, two_colon, have_sysv_atvar,
1727 flocp, set_default)
1728 struct nameseq *filenames;
1729 char *pattern, *pattern_percent;
1730 struct dep *deps;
1731 unsigned int cmds_started;
1732 char *commands;
1733 unsigned int commands_idx;
1734 int two_colon;
1735 int have_sysv_atvar;
1736 const struct floc *flocp;
1737 int set_default;
1739 struct nameseq *nextf;
1740 int implicit = 0;
1741 unsigned int max_targets = 0, target_idx = 0;
1742 char **targets = 0, **target_percents = 0;
1743 struct commands *cmds;
1745 if (commands_idx > 0)
1747 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1748 cmds->fileinfo.filenm = flocp->filenm;
1749 cmds->fileinfo.lineno = cmds_started;
1750 cmds->commands = savestring (commands, commands_idx);
1751 cmds->command_lines = 0;
1753 else
1754 cmds = 0;
1756 for (; filenames != 0; filenames = nextf)
1758 char *name = filenames->name;
1759 struct file *f;
1760 struct dep *d;
1761 struct dep *this;
1762 char *implicit_percent;
1764 nextf = filenames->next;
1765 free (filenames);
1767 /* Check for .POSIX. We used to do this in snap_deps() but that's not
1768 good enough: it doesn't happen until after the makefile is read,
1769 which means we cannot use its value during parsing. */
1771 if (streq (name, ".POSIX"))
1772 posix_pedantic = 1;
1774 implicit_percent = find_percent (name);
1775 implicit |= implicit_percent != 0;
1777 if (implicit && pattern != 0)
1778 fatal (flocp, _("mixed implicit and static pattern rules"));
1780 if (implicit && implicit_percent == 0)
1781 fatal (flocp, _("mixed implicit and normal rules"));
1783 if (implicit)
1785 if (targets == 0)
1787 max_targets = 5;
1788 targets = (char **) xmalloc (5 * sizeof (char *));
1789 target_percents = (char **) xmalloc (5 * sizeof (char *));
1790 target_idx = 0;
1792 else if (target_idx == max_targets - 1)
1794 max_targets += 5;
1795 targets = (char **) xrealloc ((char *) targets,
1796 max_targets * sizeof (char *));
1797 target_percents
1798 = (char **) xrealloc ((char *) target_percents,
1799 max_targets * sizeof (char *));
1801 targets[target_idx] = name;
1802 target_percents[target_idx] = implicit_percent;
1803 ++target_idx;
1804 continue;
1807 /* If there are multiple filenames, copy the chain DEPS
1808 for all but the last one. It is not safe for the same deps
1809 to go in more than one place in the data base. */
1810 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1812 if (pattern != 0)
1814 /* If this is an extended static rule:
1815 `targets: target%pattern: dep%pattern; cmds',
1816 translate each dependency pattern into a plain filename
1817 using the target pattern and this target's name. */
1818 if (!pattern_matches (pattern, pattern_percent, name))
1820 /* Give a warning if the rule is meaningless. */
1821 error (flocp,
1822 _("target `%s' doesn't match the target pattern"), name);
1823 this = 0;
1825 else
1827 /* We use patsubst_expand to do the work of translating
1828 the target pattern, the target's name and the dependencies'
1829 patterns into plain dependency names. */
1830 char *buffer = variable_expand ("");
1832 for (d = this; d != 0; d = d->next)
1834 char *o;
1835 char *percent = find_percent (d->name);
1836 if (percent == 0)
1837 continue;
1838 o = patsubst_expand (buffer, name, pattern, d->name,
1839 pattern_percent, percent);
1840 /* If the name expanded to the empty string, that's
1841 illegal. */
1842 if (o == buffer)
1843 fatal (flocp,
1844 _("target `%s' leaves prerequisite pattern empty"),
1845 name);
1846 free (d->name);
1847 d->name = savestring (buffer, o - buffer);
1852 /* If at least one of the dependencies uses $$@ etc. deal with that.
1853 It would be very nice and very simple to just expand everything, but
1854 it would break a lot of backward compatibility. Maybe that's OK
1855 since we're just emulating a SysV function, and if we do that then
1856 why not emulate it completely (that's what SysV make does: it
1857 re-expands the entire prerequisite list, all the time, with $@
1858 etc. in scope. But, it would be a pain indeed to document this
1859 ("iff you use $$@, your prerequisite lists is expanded twice...")
1860 Ouch. Maybe better to make the code more complex. */
1862 if (have_sysv_atvar)
1864 char *p;
1865 int tlen = strlen (name);
1866 char *fnp = strrchr (name, '/');
1867 int dlen;
1868 int flen;
1870 if (fnp)
1872 dlen = fnp - name;
1873 ++fnp;
1874 flen = strlen (fnp);
1876 else
1878 dlen = 0;
1879 fnp = name;
1880 flen = tlen;
1884 for (d = this; d != 0; d = d->next)
1885 for (p = strchr (d->name, '$'); p != 0; p = strchr (p+1, '$'))
1887 char *s = p;
1888 char *at;
1889 int atlen;
1891 /* If it's a '$@' or '$(@', it's escaped */
1892 if ((++p)[0] == '$'
1893 && (p[1] == '@' || (p[1] == '(' && p[2] == '@')))
1895 bcopy (p, s, strlen (p)+1);
1896 continue;
1899 /* Maybe found one. Check. p will point to '@' [for $@] or
1900 ')' [for $(@)] or 'D' [for $(@D)] or 'F' [for $(@F)]. */
1901 if (p[0] != '@'
1902 && (p[0] != '(' || (++p)[0] != '@'
1903 || ((++p)[0] != ')'
1904 && (p[1] != ')' || (p[0] != 'D' && p[0] != 'F')))))
1905 continue;
1907 /* Found one. Compute the length and string ptr. Move p
1908 past the variable reference. */
1909 switch (p[0])
1911 case 'D':
1912 atlen = dlen;
1913 at = name;
1914 p += 2;
1915 break;
1917 case 'F':
1918 atlen = flen;
1919 at = fnp;
1920 p += 2;
1921 break;
1923 default:
1924 atlen = tlen;
1925 at = name;
1926 ++p;
1927 break;
1930 /* Get more space. */
1932 int soff = s - d->name;
1933 int poff = p - d->name;
1934 d->name = (char *) xrealloc (d->name,
1935 strlen (d->name) + atlen + 1);
1936 s = d->name + soff;
1937 p = d->name + poff;
1940 /* Copy the string over. */
1941 bcopy(p, s+atlen, strlen (p)+1);
1942 bcopy(at, s, atlen);
1943 p = s + atlen - 1;
1947 if (!two_colon)
1949 /* Single-colon. Combine these dependencies
1950 with others in file's existing record, if any. */
1951 f = enter_file (name);
1953 if (f->double_colon)
1954 fatal (flocp,
1955 _("target file `%s' has both : and :: entries"), f->name);
1957 /* If CMDS == F->CMDS, this target was listed in this rule
1958 more than once. Just give a warning since this is harmless. */
1959 if (cmds != 0 && cmds == f->cmds)
1960 error (flocp,
1961 _("target `%s' given more than once in the same rule."),
1962 f->name);
1964 /* Check for two single-colon entries both with commands.
1965 Check is_target so that we don't lose on files such as .c.o
1966 whose commands were preinitialized. */
1967 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1969 error (&cmds->fileinfo,
1970 _("warning: overriding commands for target `%s'"),
1971 f->name);
1972 error (&f->cmds->fileinfo,
1973 _("warning: ignoring old commands for target `%s'"),
1974 f->name);
1977 f->is_target = 1;
1979 /* Defining .DEFAULT with no deps or cmds clears it. */
1980 if (f == default_file && this == 0 && cmds == 0)
1981 f->cmds = 0;
1982 if (cmds != 0)
1983 f->cmds = cmds;
1984 /* Defining .SUFFIXES with no dependencies
1985 clears out the list of suffixes. */
1986 if (f == suffix_file && this == 0)
1988 d = f->deps;
1989 while (d != 0)
1991 struct dep *nextd = d->next;
1992 free (d->name);
1993 free ((char *)d);
1994 d = nextd;
1996 f->deps = 0;
1998 else if (f->deps != 0)
2000 /* Add the file's old deps and the new ones in THIS together. */
2002 struct dep *firstdeps, *moredeps;
2003 if (cmds != 0)
2005 /* This is the rule with commands, so put its deps first.
2006 The rationale behind this is that $< expands to the
2007 first dep in the chain, and commands use $< expecting
2008 to get the dep that rule specifies. */
2009 firstdeps = this;
2010 moredeps = f->deps;
2012 else
2014 /* Append the new deps to the old ones. */
2015 firstdeps = f->deps;
2016 moredeps = this;
2019 if (firstdeps == 0)
2020 firstdeps = moredeps;
2021 else
2023 d = firstdeps;
2024 while (d->next != 0)
2025 d = d->next;
2026 d->next = moredeps;
2029 f->deps = firstdeps;
2031 else
2032 f->deps = this;
2034 /* If this is a static pattern rule, set the file's stem to
2035 the part of its name that matched the `%' in the pattern,
2036 so you can use $* in the commands. */
2037 if (pattern != 0)
2039 static char *percent = "%";
2040 char *buffer = variable_expand ("");
2041 char *o = patsubst_expand (buffer, name, pattern, percent,
2042 pattern_percent, percent);
2043 f->stem = savestring (buffer, o - buffer);
2046 else
2048 /* Double-colon. Make a new record
2049 even if the file already has one. */
2050 f = lookup_file (name);
2051 /* Check for both : and :: rules. Check is_target so
2052 we don't lose on default suffix rules or makefiles. */
2053 if (f != 0 && f->is_target && !f->double_colon)
2054 fatal (flocp,
2055 _("target file `%s' has both : and :: entries"), f->name);
2056 f = enter_file (name);
2057 /* If there was an existing entry and it was a double-colon
2058 entry, enter_file will have returned a new one, making it the
2059 prev pointer of the old one, and setting its double_colon
2060 pointer to the first one. */
2061 if (f->double_colon == 0)
2062 /* This is the first entry for this name, so we must
2063 set its double_colon pointer to itself. */
2064 f->double_colon = f;
2065 f->is_target = 1;
2066 f->deps = this;
2067 f->cmds = cmds;
2070 /* Free name if not needed further. */
2071 if (f != 0 && name != f->name
2072 && (name < f->name || name > f->name + strlen (f->name)))
2074 free (name);
2075 name = f->name;
2078 /* See if this is first target seen whose name does
2079 not start with a `.', unless it contains a slash. */
2080 if (default_goal_file == 0 && set_default
2081 && (*name != '.' || strchr (name, '/') != 0
2082 #ifdef HAVE_DOS_PATHS
2083 || strchr (name, '\\') != 0
2084 #endif
2087 int reject = 0;
2089 /* If this file is a suffix, don't
2090 let it be the default goal file. */
2092 for (d = suffix_file->deps; d != 0; d = d->next)
2094 register struct dep *d2;
2095 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
2097 reject = 1;
2098 break;
2100 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
2102 register unsigned int len = strlen (dep_name (d2));
2103 if (!strneq (name, dep_name (d2), len))
2104 continue;
2105 if (streq (name + len, dep_name (d)))
2107 reject = 1;
2108 break;
2111 if (reject)
2112 break;
2115 if (!reject)
2116 default_goal_file = f;
2120 if (implicit)
2122 targets[target_idx] = 0;
2123 target_percents[target_idx] = 0;
2124 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
2125 free ((char *) target_percents);
2129 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2130 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2131 Quoting backslashes are removed from STRING by compacting it into
2132 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2133 one, or nil if there are none. */
2135 char *
2136 find_char_unquote (string, stop1, stop2, blank)
2137 char *string;
2138 int stop1;
2139 int stop2;
2140 int blank;
2142 unsigned int string_len = 0;
2143 register char *p = string;
2145 while (1)
2147 if (stop2 && blank)
2148 while (*p != '\0' && *p != stop1 && *p != stop2
2149 && ! isblank ((unsigned char) *p))
2150 ++p;
2151 else if (stop2)
2152 while (*p != '\0' && *p != stop1 && *p != stop2)
2153 ++p;
2154 else if (blank)
2155 while (*p != '\0' && *p != stop1
2156 && ! isblank ((unsigned char) *p))
2157 ++p;
2158 else
2159 while (*p != '\0' && *p != stop1)
2160 ++p;
2162 if (*p == '\0')
2163 break;
2165 if (p > string && p[-1] == '\\')
2167 /* Search for more backslashes. */
2168 register int i = -2;
2169 while (&p[i] >= string && p[i] == '\\')
2170 --i;
2171 ++i;
2172 /* Only compute the length if really needed. */
2173 if (string_len == 0)
2174 string_len = strlen (string);
2175 /* The number of backslashes is now -I.
2176 Copy P over itself to swallow half of them. */
2177 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
2178 p += i / 2;
2179 if (i % 2 == 0)
2180 /* All the backslashes quoted each other; the STOPCHAR was
2181 unquoted. */
2182 return p;
2184 /* The STOPCHAR was quoted by a backslash. Look for another. */
2186 else
2187 /* No backslash in sight. */
2188 return p;
2191 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2192 return 0;
2195 /* Search PATTERN for an unquoted %. */
2197 char *
2198 find_percent (pattern)
2199 char *pattern;
2201 return find_char_unquote (pattern, '%', 0, 0);
2204 /* Parse a string into a sequence of filenames represented as a
2205 chain of struct nameseq's in reverse order and return that chain.
2207 The string is passed as STRINGP, the address of a string pointer.
2208 The string pointer is updated to point at the first character
2209 not parsed, which either is a null char or equals STOPCHAR.
2211 SIZE is how big to construct chain elements.
2212 This is useful if we want them actually to be other structures
2213 that have room for additional info.
2215 If STRIP is nonzero, strip `./'s off the beginning. */
2217 struct nameseq *
2218 parse_file_seq (stringp, stopchar, size, strip)
2219 char **stringp;
2220 int stopchar;
2221 unsigned int size;
2222 int strip;
2224 register struct nameseq *new = 0;
2225 register struct nameseq *new1, *lastnew1;
2226 register char *p = *stringp;
2227 char *q;
2228 char *name;
2230 #ifdef VMS
2231 # define VMS_COMMA ','
2232 #else
2233 # define VMS_COMMA 0
2234 #endif
2236 while (1)
2238 /* Skip whitespace; see if any more names are left. */
2239 p = next_token (p);
2240 if (*p == '\0')
2241 break;
2242 if (*p == stopchar)
2243 break;
2245 /* Yes, find end of next name. */
2246 q = p;
2247 p = find_char_unquote (q, stopchar, VMS_COMMA, 1);
2248 #ifdef VMS
2249 /* convert comma separated list to space separated */
2250 if (p && *p == ',')
2251 *p =' ';
2252 #endif
2253 #ifdef _AMIGA
2254 if (stopchar == ':' && p && *p == ':'
2255 && !(isspace ((unsigned char)p[1]) || !p[1]
2256 || isspace ((unsigned char)p[-1])))
2258 p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1);
2260 #endif
2261 #ifdef HAVE_DOS_PATHS
2262 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
2263 first colon which isn't followed by a slash or a backslash.
2264 Note that tokens separated by spaces should be treated as separate
2265 tokens since make doesn't allow path names with spaces */
2266 if (stopchar == ':')
2267 while (p != 0 && !isspace ((unsigned char)*p) &&
2268 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
2269 p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1);
2270 #endif
2271 if (p == 0)
2272 p = q + strlen (q);
2274 if (strip)
2275 #ifdef VMS
2276 /* Skip leading `[]'s. */
2277 while (p - q > 2 && q[0] == '[' && q[1] == ']')
2278 #else
2279 /* Skip leading `./'s. */
2280 while (p - q > 2 && q[0] == '.' && q[1] == '/')
2281 #endif
2283 q += 2; /* Skip "./". */
2284 while (q < p && *q == '/')
2285 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
2286 ++q;
2289 /* Extract the filename just found, and skip it. */
2291 if (q == p)
2292 /* ".///" was stripped to "". */
2293 #ifdef VMS
2294 continue;
2295 #else
2296 #ifdef _AMIGA
2297 name = savestring ("", 0);
2298 #else
2299 name = savestring ("./", 2);
2300 #endif
2301 #endif
2302 else
2303 #ifdef VMS
2304 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
2305 * to remove this '\' before we can use the filename.
2306 * Savestring called because q may be read-only string constant.
2309 char *qbase = xstrdup (q);
2310 char *pbase = qbase + (p-q);
2311 char *q1 = qbase;
2312 char *q2 = q1;
2313 char *p1 = pbase;
2315 while (q1 != pbase)
2317 if (*q1 == '\\' && *(q1+1) == ':')
2319 q1++;
2320 p1--;
2322 *q2++ = *q1++;
2324 name = savestring (qbase, p1 - qbase);
2325 free (qbase);
2327 #else
2328 name = savestring (q, p - q);
2329 #endif
2331 /* Add it to the front of the chain. */
2332 new1 = (struct nameseq *) xmalloc (size);
2333 new1->name = name;
2334 new1->next = new;
2335 new = new1;
2338 #ifndef NO_ARCHIVES
2340 /* Look for multi-word archive references.
2341 They are indicated by a elt ending with an unmatched `)' and
2342 an elt further down the chain (i.e., previous in the file list)
2343 with an unmatched `(' (e.g., "lib(mem"). */
2345 new1 = new;
2346 lastnew1 = 0;
2347 while (new1 != 0)
2348 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2349 && new1->name[strlen (new1->name) - 1] == ')'
2350 && strchr (new1->name, '(') == 0)
2352 /* NEW1 ends with a `)' but does not contain a `('.
2353 Look back for an elt with an opening `(' but no closing `)'. */
2355 struct nameseq *n = new1->next, *lastn = new1;
2356 char *paren = 0;
2357 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2359 lastn = n;
2360 n = n->next;
2362 if (n != 0
2363 /* Ignore something starting with `(', as that cannot actually
2364 be an archive-member reference (and treating it as such
2365 results in an empty file name, which causes much lossage). */
2366 && n->name[0] != '(')
2368 /* N is the first element in the archive group.
2369 Its name looks like "lib(mem" (with no closing `)'). */
2371 char *libname;
2373 /* Copy "lib(" into LIBNAME. */
2374 ++paren;
2375 libname = (char *) alloca (paren - n->name + 1);
2376 bcopy (n->name, libname, paren - n->name);
2377 libname[paren - n->name] = '\0';
2379 if (*paren == '\0')
2381 /* N was just "lib(", part of something like "lib( a b)".
2382 Edit it out of the chain and free its storage. */
2383 lastn->next = n->next;
2384 free (n->name);
2385 free ((char *) n);
2386 /* LASTN->next is the new stopping elt for the loop below. */
2387 n = lastn->next;
2389 else
2391 /* Replace N's name with the full archive reference. */
2392 name = concat (libname, paren, ")");
2393 free (n->name);
2394 n->name = name;
2397 if (new1->name[1] == '\0')
2399 /* NEW1 is just ")", part of something like "lib(a b )".
2400 Omit it from the chain and free its storage. */
2401 if (lastnew1 == 0)
2402 new = new1->next;
2403 else
2404 lastnew1->next = new1->next;
2405 lastn = new1;
2406 new1 = new1->next;
2407 free (lastn->name);
2408 free ((char *) lastn);
2410 else
2412 /* Replace also NEW1->name, which already has closing `)'. */
2413 name = concat (libname, new1->name, "");
2414 free (new1->name);
2415 new1->name = name;
2416 new1 = new1->next;
2419 /* Trace back from NEW1 (the end of the list) until N
2420 (the beginning of the list), rewriting each name
2421 with the full archive reference. */
2423 while (new1 != n)
2425 name = concat (libname, new1->name, ")");
2426 free (new1->name);
2427 new1->name = name;
2428 lastnew1 = new1;
2429 new1 = new1->next;
2432 else
2434 /* No frobnication happening. Just step down the list. */
2435 lastnew1 = new1;
2436 new1 = new1->next;
2439 else
2441 lastnew1 = new1;
2442 new1 = new1->next;
2445 #endif
2447 *stringp = p;
2448 return new;
2451 /* Find the next line of text in an eval buffer, combining continuation lines
2452 into one line.
2453 Return the number of actual lines read (> 1 if continuation lines).
2454 Returns -1 if there's nothing left in the buffer.
2456 After this function, ebuf->buffer points to the first character of the
2457 line we just found.
2460 /* Read a line of text from a STRING.
2461 Since we aren't really reading from a file, don't bother with linenumbers.
2464 static unsigned long
2465 readstring (ebuf)
2466 struct ebuffer *ebuf;
2468 char *p;
2470 /* If there is nothing left in this buffer, return 0. */
2471 if (ebuf->bufnext > ebuf->bufstart + ebuf->size)
2472 return -1;
2474 /* Set up a new starting point for the buffer, and find the end of the
2475 next logical line (taking into account backslash/newline pairs). */
2477 p = ebuf->buffer = ebuf->bufnext;
2479 while (1)
2481 int backslash = 0;
2483 /* Find the next newline. Keep track of backslashes as we look. */
2484 for (; *p != '\n' && *p != '\0'; ++p)
2485 if (*p == '\\')
2486 backslash = !backslash;
2488 /* If we got to the end of the string or a newline with no backslash,
2489 we're done. */
2490 if (*p == '\0' || !backslash)
2491 break;
2494 /* Overwrite the newline char. */
2495 *p = '\0';
2496 ebuf->bufnext = p+1;
2498 return 0;
2501 static long
2502 readline (ebuf)
2503 struct ebuffer *ebuf;
2505 char *p;
2506 char *end;
2507 char *start;
2508 long nlines = 0;
2510 /* The behaviors between string and stream buffers are different enough to
2511 warrant different functions. Do the Right Thing. */
2513 if (!ebuf->fp)
2514 return readstring (ebuf);
2516 /* When reading from a file, we always start over at the beginning of the
2517 buffer for each new line. */
2519 p = start = ebuf->bufstart;
2520 end = p + ebuf->size;
2521 *p = '\0';
2523 while (fgets (p, end - p, ebuf->fp) != 0)
2525 char *p2;
2526 unsigned long len;
2527 int backslash;
2529 len = strlen (p);
2530 if (len == 0)
2532 /* This only happens when the first thing on the line is a '\0'.
2533 It is a pretty hopeless case, but (wonder of wonders) Athena
2534 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2535 There is nothing really to be done; we synthesize a newline so
2536 the following line doesn't appear to be part of this line. */
2537 error (&ebuf->floc,
2538 _("warning: NUL character seen; rest of line ignored"));
2539 p[0] = '\n';
2540 len = 1;
2543 /* Jump past the text we just read. */
2544 p += len;
2546 /* If the last char isn't a newline, the whole line didn't fit into the
2547 buffer. Get some more buffer and try again. */
2548 if (p[-1] != '\n')
2549 goto more_buffer;
2551 /* We got a newline, so add one to the count of lines. */
2552 ++nlines;
2554 #if !defined(WINDOWS32) && !defined(__MSDOS__)
2555 /* Check to see if the line was really ended with CRLF; if so ignore
2556 the CR. */
2557 if ((p - start) > 1 && p[-2] == '\r')
2559 --p;
2560 p[-1] = '\n';
2562 #endif
2564 backslash = 0;
2565 for (p2 = p - 2; p2 >= start; --p2)
2567 if (*p2 != '\\')
2568 break;
2569 backslash = !backslash;
2572 if (!backslash)
2574 p[-1] = '\0';
2575 break;
2578 /* It was a backslash/newline combo. If we have more space, read
2579 another line. */
2580 if (end - p >= 80)
2581 continue;
2583 /* We need more space at the end of our buffer, so realloc it.
2584 Make sure to preserve the current offset of p. */
2585 more_buffer:
2587 unsigned long off = p - start;
2588 ebuf->size *= 2;
2589 start = ebuf->buffer = ebuf->bufstart = (char *) xrealloc (start,
2590 ebuf->size);
2591 p = start + off;
2592 end = start + ebuf->size;
2593 *p = '\0';
2597 if (ferror (ebuf->fp))
2598 pfatal_with_name (ebuf->floc.filenm);
2600 /* If we found some lines, return how many.
2601 If we didn't, but we did find _something_, that indicates we read the last
2602 line of a file with no final newline; return 1.
2603 If we read nothing, we're at EOF; return -1. */
2605 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
2608 /* Parse the next "makefile word" from the input buffer, and return info
2609 about it.
2611 A "makefile word" is one of:
2613 w_bogus Should never happen
2614 w_eol End of input
2615 w_static A static word; cannot be expanded
2616 w_variable A word containing one or more variables/functions
2617 w_colon A colon
2618 w_dcolon A double-colon
2619 w_semicolon A semicolon
2620 w_comment A comment character
2621 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2623 Note that this function is only used when reading certain parts of the
2624 makefile. Don't use it where special rules hold sway (RHS of a variable,
2625 in a command list, etc.) */
2627 static enum make_word_type
2628 get_next_mword (buffer, delim, startp, length)
2629 char *buffer;
2630 char *delim;
2631 char **startp;
2632 unsigned int *length;
2634 enum make_word_type wtype = w_bogus;
2635 char *p = buffer, *beg;
2636 char c;
2638 /* Skip any leading whitespace. */
2639 while (isblank ((unsigned char)*p))
2640 ++p;
2642 beg = p;
2643 c = *(p++);
2644 switch (c)
2646 case '\0':
2647 wtype = w_eol;
2648 break;
2650 case '#':
2651 wtype = w_comment;
2652 break;
2654 case ';':
2655 wtype = w_semicolon;
2656 break;
2658 case '=':
2659 wtype = w_varassign;
2660 break;
2662 case ':':
2663 wtype = w_colon;
2664 switch (*p)
2666 case ':':
2667 ++p;
2668 wtype = w_dcolon;
2669 break;
2671 case '=':
2672 ++p;
2673 wtype = w_varassign;
2674 break;
2676 break;
2678 case '+':
2679 case '?':
2680 if (*p == '=')
2682 ++p;
2683 wtype = w_varassign;
2684 break;
2687 default:
2688 if (delim && strchr (delim, c))
2689 wtype = w_static;
2690 break;
2693 /* Did we find something? If so, return now. */
2694 if (wtype != w_bogus)
2695 goto done;
2697 /* This is some non-operator word. A word consists of the longest
2698 string of characters that doesn't contain whitespace, one of [:=#],
2699 or [?+]=, or one of the chars in the DELIM string. */
2701 /* We start out assuming a static word; if we see a variable we'll
2702 adjust our assumptions then. */
2703 wtype = w_static;
2705 /* We already found the first value of "c", above. */
2706 while (1)
2708 char closeparen;
2709 int count;
2711 switch (c)
2713 case '\0':
2714 case ' ':
2715 case '\t':
2716 case '=':
2717 case '#':
2718 goto done_word;
2720 case ':':
2721 #ifdef HAVE_DOS_PATHS
2722 /* A word CAN include a colon in its drive spec. The drive
2723 spec is allowed either at the beginning of a word, or as part
2724 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2725 if (!(p - beg >= 2
2726 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
2727 && (p - beg == 2 || p[-3] == '(')))
2728 #endif
2729 goto done_word;
2731 case '$':
2732 c = *(p++);
2733 if (c == '$')
2734 break;
2736 /* This is a variable reference, so note that it's expandable.
2737 Then read it to the matching close paren. */
2738 wtype = w_variable;
2740 if (c == '(')
2741 closeparen = ')';
2742 else if (c == '{')
2743 closeparen = '}';
2744 else
2745 /* This is a single-letter variable reference. */
2746 break;
2748 for (count=0; *p != '\0'; ++p)
2750 if (*p == c)
2751 ++count;
2752 else if (*p == closeparen && --count < 0)
2754 ++p;
2755 break;
2758 break;
2760 case '?':
2761 case '+':
2762 if (*p == '=')
2763 goto done_word;
2764 break;
2766 case '\\':
2767 switch (*p)
2769 case ':':
2770 case ';':
2771 case '=':
2772 case '\\':
2773 ++p;
2774 break;
2776 break;
2778 default:
2779 if (delim && strchr (delim, c))
2780 goto done_word;
2781 break;
2784 c = *(p++);
2786 done_word:
2787 --p;
2789 done:
2790 if (startp)
2791 *startp = beg;
2792 if (length)
2793 *length = p - beg;
2794 return wtype;
2797 /* Construct the list of include directories
2798 from the arguments and the default list. */
2800 void
2801 construct_include_path (arg_dirs)
2802 char **arg_dirs;
2804 register unsigned int i;
2805 #ifdef VAXC /* just don't ask ... */
2806 stat_t stbuf;
2807 #else
2808 struct stat stbuf;
2809 #endif
2810 /* Table to hold the dirs. */
2812 register unsigned int defsize = (sizeof (default_include_directories)
2813 / sizeof (default_include_directories[0]));
2814 register unsigned int max = 5;
2815 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
2816 register unsigned int idx = 0;
2818 #ifdef __MSDOS__
2819 defsize++;
2820 #endif
2822 /* First consider any dirs specified with -I switches.
2823 Ignore dirs that don't exist. */
2825 if (arg_dirs != 0)
2826 while (*arg_dirs != 0)
2828 char *dir = *arg_dirs++;
2830 if (dir[0] == '~')
2832 char *expanded = tilde_expand (dir);
2833 if (expanded != 0)
2834 dir = expanded;
2837 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
2839 if (idx == max - 1)
2841 max += 5;
2842 dirs = (char **)
2843 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
2845 dirs[idx++] = dir;
2847 else if (dir != arg_dirs[-1])
2848 free (dir);
2851 /* Now add at the end the standard default dirs. */
2853 #ifdef __MSDOS__
2855 /* The environment variable $DJDIR holds the root of the
2856 DJGPP directory tree; add ${DJDIR}/include. */
2857 struct variable *djdir = lookup_variable ("DJDIR", 5);
2859 if (djdir)
2861 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2863 strcat (strcpy (defdir, djdir->value), "/include");
2864 dirs[idx++] = defdir;
2867 #endif
2869 for (i = 0; default_include_directories[i] != 0; ++i)
2870 if (stat (default_include_directories[i], &stbuf) == 0
2871 && S_ISDIR (stbuf.st_mode))
2872 dirs[idx++] = default_include_directories[i];
2874 dirs[idx] = 0;
2876 /* Now compute the maximum length of any name in it. */
2878 max_incl_len = 0;
2879 for (i = 0; i < idx; ++i)
2881 unsigned int len = strlen (dirs[i]);
2882 /* If dir name is written with a trailing slash, discard it. */
2883 if (dirs[i][len - 1] == '/')
2884 /* We can't just clobber a null in because it may have come from
2885 a literal string and literal strings may not be writable. */
2886 dirs[i] = savestring (dirs[i], len - 1);
2887 if (len > max_incl_len)
2888 max_incl_len = len;
2891 include_directories = dirs;
2894 /* Expand ~ or ~USER at the beginning of NAME.
2895 Return a newly malloc'd string or 0. */
2897 char *
2898 tilde_expand (name)
2899 char *name;
2901 #ifndef VMS
2902 if (name[1] == '/' || name[1] == '\0')
2904 extern char *getenv ();
2905 char *home_dir;
2906 int is_variable;
2909 /* Turn off --warn-undefined-variables while we expand HOME. */
2910 int save = warn_undefined_variables_flag;
2911 warn_undefined_variables_flag = 0;
2913 home_dir = allocated_variable_expand ("$(HOME)");
2915 warn_undefined_variables_flag = save;
2918 is_variable = home_dir[0] != '\0';
2919 if (!is_variable)
2921 free (home_dir);
2922 home_dir = getenv ("HOME");
2924 #if !defined(_AMIGA) && !defined(WINDOWS32)
2925 if (home_dir == 0 || home_dir[0] == '\0')
2927 extern char *getlogin ();
2928 char *logname = getlogin ();
2929 home_dir = 0;
2930 if (logname != 0)
2932 struct passwd *p = getpwnam (logname);
2933 if (p != 0)
2934 home_dir = p->pw_dir;
2937 #endif /* !AMIGA && !WINDOWS32 */
2938 if (home_dir != 0)
2940 char *new = concat (home_dir, "", name + 1);
2941 if (is_variable)
2942 free (home_dir);
2943 return new;
2946 #if !defined(_AMIGA) && !defined(WINDOWS32)
2947 else
2949 struct passwd *pwent;
2950 char *userend = strchr (name + 1, '/');
2951 if (userend != 0)
2952 *userend = '\0';
2953 pwent = getpwnam (name + 1);
2954 if (pwent != 0)
2956 if (userend == 0)
2957 return xstrdup (pwent->pw_dir);
2958 else
2959 return concat (pwent->pw_dir, "/", userend + 1);
2961 else if (userend != 0)
2962 *userend = '/';
2964 #endif /* !AMIGA && !WINDOWS32 */
2965 #endif /* !VMS */
2966 return 0;
2969 /* Given a chain of struct nameseq's describing a sequence of filenames,
2970 in reverse of the intended order, return a new chain describing the
2971 result of globbing the filenames. The new chain is in forward order.
2972 The links of the old chain are freed or used in the new chain.
2973 Likewise for the names in the old chain.
2975 SIZE is how big to construct chain elements.
2976 This is useful if we want them actually to be other structures
2977 that have room for additional info. */
2979 struct nameseq *
2980 multi_glob (chain, size)
2981 struct nameseq *chain;
2982 unsigned int size;
2984 extern void dir_setup_glob ();
2985 register struct nameseq *new = 0;
2986 register struct nameseq *old;
2987 struct nameseq *nexto;
2988 glob_t gl;
2990 dir_setup_glob (&gl);
2992 for (old = chain; old != 0; old = nexto)
2994 #ifndef NO_ARCHIVES
2995 char *memname;
2996 #endif
2998 nexto = old->next;
3000 if (old->name[0] == '~')
3002 char *newname = tilde_expand (old->name);
3003 if (newname != 0)
3005 free (old->name);
3006 old->name = newname;
3010 #ifndef NO_ARCHIVES
3011 if (ar_name (old->name))
3013 /* OLD->name is an archive member reference.
3014 Replace it with the archive file name,
3015 and save the member name in MEMNAME.
3016 We will glob on the archive name and then
3017 reattach MEMNAME later. */
3018 char *arname;
3019 ar_parse_name (old->name, &arname, &memname);
3020 free (old->name);
3021 old->name = arname;
3023 else
3024 memname = 0;
3025 #endif /* !NO_ARCHIVES */
3027 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
3029 case 0: /* Success. */
3031 register int i = gl.gl_pathc;
3032 while (i-- > 0)
3034 #ifndef NO_ARCHIVES
3035 if (memname != 0)
3037 /* Try to glob on MEMNAME within the archive. */
3038 struct nameseq *found
3039 = ar_glob (gl.gl_pathv[i], memname, size);
3040 if (found == 0)
3042 /* No matches. Use MEMNAME as-is. */
3043 unsigned int alen = strlen (gl.gl_pathv[i]);
3044 unsigned int mlen = strlen (memname);
3045 struct nameseq *elt
3046 = (struct nameseq *) xmalloc (size);
3047 if (size > sizeof (struct nameseq))
3048 bzero (((char *) elt) + sizeof (struct nameseq),
3049 size - sizeof (struct nameseq));
3050 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
3051 bcopy (gl.gl_pathv[i], elt->name, alen);
3052 elt->name[alen] = '(';
3053 bcopy (memname, &elt->name[alen + 1], mlen);
3054 elt->name[alen + 1 + mlen] = ')';
3055 elt->name[alen + 1 + mlen + 1] = '\0';
3056 elt->next = new;
3057 new = elt;
3059 else
3061 /* Find the end of the FOUND chain. */
3062 struct nameseq *f = found;
3063 while (f->next != 0)
3064 f = f->next;
3066 /* Attach the chain being built to the end of the FOUND
3067 chain, and make FOUND the new NEW chain. */
3068 f->next = new;
3069 new = found;
3072 free (memname);
3074 else
3075 #endif /* !NO_ARCHIVES */
3077 struct nameseq *elt = (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 = xstrdup (gl.gl_pathv[i]);
3082 elt->next = new;
3083 new = elt;
3086 globfree (&gl);
3087 free (old->name);
3088 free ((char *)old);
3089 break;
3092 case GLOB_NOSPACE:
3093 fatal (NILF, _("virtual memory exhausted"));
3094 break;
3096 default:
3097 old->next = new;
3098 new = old;
3099 break;
3103 return new;