Add VMS updates from Martin Zinser.
[make.git] / read.c
blob0ac59e124e17046c3efb0884b370643a2ade260b
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_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 char *ap;
688 /* Expand the line so we can use indirect and constructed
689 variable names in an export command. */
690 p2 = ap = allocated_variable_expand (p2);
692 for (p = find_next_token (&p2, &len); p != 0;
693 p = find_next_token (&p2, &len))
695 v = lookup_variable (p, len);
696 if (v == 0)
697 v = define_variable_loc (p, len, "", o_file, 0,
698 fstart);
699 v->export = v_export;
702 free (ap);
705 goto rule_complete;
708 if (word1eq ("unexport"))
710 if (*p2 == '\0')
711 export_all_variables = 0;
712 else
714 unsigned int len;
715 struct variable *v;
716 char *ap;
718 /* Expand the line so we can use indirect and constructed
719 variable names in an unexport command. */
720 p2 = ap = allocated_variable_expand (p2);
722 for (p = find_next_token (&p2, &len); p != 0;
723 p = find_next_token (&p2, &len))
725 v = lookup_variable (p, len);
726 if (v == 0)
727 v = define_variable_loc (p, len, "", o_file, 0, fstart);
729 v->export = v_noexport;
732 free (ap);
734 goto rule_complete;
737 skip_conditionals:
738 if (word1eq ("vpath"))
740 char *pattern;
741 unsigned int len;
742 p2 = variable_expand (p2);
743 p = find_next_token (&p2, &len);
744 if (p != 0)
746 pattern = savestring (p, len);
747 p = find_next_token (&p2, &len);
748 /* No searchpath means remove all previous
749 selective VPATH's with the same pattern. */
751 else
752 /* No pattern means remove all previous selective VPATH's. */
753 pattern = 0;
754 construct_vpath_list (pattern, p);
755 if (pattern != 0)
756 free (pattern);
758 goto rule_complete;
761 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
763 /* We have found an `include' line specifying a nested
764 makefile to be read at this point. */
765 struct conditionals *save;
766 struct conditionals new_conditionals;
767 struct nameseq *files;
768 /* "-include" (vs "include") says no error if the file does not
769 exist. "sinclude" is an alias for this from SGI. */
770 int noerror = (p[0] != 'i');
772 p = allocated_variable_expand (p2);
773 if (*p == '\0')
775 error (fstart,
776 _("no file name for `%sinclude'"), noerror ? "-" : "");
777 continue;
780 /* Parse the list of file names. */
781 p2 = p;
782 files = multi_glob (parse_file_seq (&p2, '\0',
783 sizeof (struct nameseq),
785 sizeof (struct nameseq));
786 free (p);
788 /* Save the state of conditionals and start
789 the included makefile with a clean slate. */
790 save = conditionals;
791 bzero ((char *) &new_conditionals, sizeof new_conditionals);
792 conditionals = &new_conditionals;
794 /* Record the rules that are waiting so they will determine
795 the default goal before those in the included makefile. */
796 record_waiting_files ();
798 /* Read each included makefile. */
799 while (files != 0)
801 struct nameseq *next = files->next;
802 char *name = files->name;
803 int r;
805 free ((char *)files);
806 files = next;
808 r = eval_makefile (name, (RM_INCLUDED | RM_NO_TILDE
809 | (noerror ? RM_DONTCARE : 0)));
810 if (!r)
812 if (!noerror)
813 error (fstart, "%s: %s", name, strerror (errno));
814 free (name);
818 /* Free any space allocated by conditional_line. */
819 if (conditionals->ignoring)
820 free (conditionals->ignoring);
821 if (conditionals->seen_else)
822 free (conditionals->seen_else);
824 /* Restore state. */
825 conditionals = save;
827 goto rule_complete;
830 if (try_variable_definition (fstart, p, o_file, 0))
831 /* This line has been dealt with. */
832 goto rule_complete;
834 if (line[0] == '\t')
836 p = collapsed; /* Ignore comments, etc. */
837 while (isblank ((unsigned char)*p))
838 ++p;
839 if (*p == '\0')
840 /* The line is completely blank; that is harmless. */
841 continue;
843 /* This line starts with a tab but was not caught above
844 because there was no preceding target, and the line
845 might have been usable as a variable definition.
846 But now we know it is definitely lossage. */
847 fatal(fstart, _("commands commence before first target"));
850 /* This line describes some target files. This is complicated by
851 the existence of target-specific variables, because we can't
852 expand the entire line until we know if we have one or not. So
853 we expand the line word by word until we find the first `:',
854 then check to see if it's a target-specific variable.
856 In this algorithm, `lb_next' will point to the beginning of the
857 unexpanded parts of the input buffer, while `p2' points to the
858 parts of the expanded buffer we haven't searched yet. */
861 enum make_word_type wtype;
862 enum variable_origin v_origin;
863 char *cmdleft, *semip, *lb_next;
864 unsigned int len, plen = 0;
865 char *colonp;
867 /* Record the previous rule. */
869 record_waiting_files ();
870 tgts_started = fstart->lineno;
872 /* Search the line for an unquoted ; that is not after an
873 unquoted #. */
874 cmdleft = find_char_unquote (line, ';', '#', 0);
875 if (cmdleft != 0 && *cmdleft == '#')
877 /* We found a comment before a semicolon. */
878 *cmdleft = '\0';
879 cmdleft = 0;
881 else if (cmdleft != 0)
882 /* Found one. Cut the line short there before expanding it. */
883 *(cmdleft++) = '\0';
884 semip = cmdleft;
886 collapse_continuations (line);
888 /* We can't expand the entire line, since if it's a per-target
889 variable we don't want to expand it. So, walk from the
890 beginning, expanding as we go, and looking for "interesting"
891 chars. The first word is always expandable. */
892 wtype = get_next_mword(line, NULL, &lb_next, &len);
893 switch (wtype)
895 case w_eol:
896 if (cmdleft != 0)
897 fatal(fstart, _("missing rule before commands"));
898 /* This line contained something but turned out to be nothing
899 but whitespace (a comment?). */
900 continue;
902 case w_colon:
903 case w_dcolon:
904 /* We accept and ignore rules without targets for
905 compatibility with SunOS 4 make. */
906 no_targets = 1;
907 continue;
909 default:
910 break;
913 p2 = variable_expand_string(NULL, lb_next, len);
914 while (1)
916 lb_next += len;
917 if (cmdleft == 0)
919 /* Look for a semicolon in the expanded line. */
920 cmdleft = find_char_unquote (p2, ';', 0, 0);
922 if (cmdleft != 0)
924 unsigned long p2_off = p2 - variable_buffer;
925 unsigned long cmd_off = cmdleft - variable_buffer;
926 char *pend = p2 + strlen(p2);
928 /* Append any remnants of lb, then cut the line short
929 at the semicolon. */
930 *cmdleft = '\0';
932 /* One school of thought says that you shouldn't expand
933 here, but merely copy, since now you're beyond a ";"
934 and into a command script. However, the old parser
935 expanded the whole line, so we continue that for
936 backwards-compatiblity. Also, it wouldn't be
937 entirely consistent, since we do an unconditional
938 expand below once we know we don't have a
939 target-specific variable. */
940 (void)variable_expand_string(pend, lb_next, (long)-1);
941 lb_next += strlen(lb_next);
942 p2 = variable_buffer + p2_off;
943 cmdleft = variable_buffer + cmd_off + 1;
947 colonp = find_char_unquote(p2, ':', 0, 0);
948 #ifdef HAVE_DOS_PATHS
949 /* The drive spec brain-damage strikes again... */
950 /* Note that the only separators of targets in this context
951 are whitespace and a left paren. If others are possible,
952 they should be added to the string in the call to index. */
953 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
954 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
955 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
956 colonp = find_char_unquote(colonp + 1, ':', 0, 0);
957 #endif
958 if (colonp != 0)
959 break;
961 wtype = get_next_mword(lb_next, NULL, &lb_next, &len);
962 if (wtype == w_eol)
963 break;
965 p2 += strlen(p2);
966 *(p2++) = ' ';
967 p2 = variable_expand_string(p2, lb_next, len);
968 /* We don't need to worry about cmdleft here, because if it was
969 found in the variable_buffer the entire buffer has already
970 been expanded... we'll never get here. */
973 p2 = next_token (variable_buffer);
975 /* If the word we're looking at is EOL, see if there's _anything_
976 on the line. If not, a variable expanded to nothing, so ignore
977 it. If so, we can't parse this line so punt. */
978 if (wtype == w_eol)
980 if (*p2 != '\0')
981 /* There's no need to be ivory-tower about this: check for
982 one of the most common bugs found in makefiles... */
983 fatal (fstart, _("missing separator%s"),
984 !strneq(line, " ", 8) ? ""
985 : _(" (did you mean TAB instead of 8 spaces?)"));
986 continue;
989 /* Make the colon the end-of-string so we know where to stop
990 looking for targets. */
991 *colonp = '\0';
992 filenames = multi_glob (parse_file_seq (&p2, '\0',
993 sizeof (struct nameseq),
995 sizeof (struct nameseq));
996 *p2 = ':';
998 if (!filenames)
1000 /* We accept and ignore rules without targets for
1001 compatibility with SunOS 4 make. */
1002 no_targets = 1;
1003 continue;
1005 /* This should never be possible; we handled it above. */
1006 assert (*p2 != '\0');
1007 ++p2;
1009 /* Is this a one-colon or two-colon entry? */
1010 two_colon = *p2 == ':';
1011 if (two_colon)
1012 p2++;
1014 /* Test to see if it's a target-specific variable. Copy the rest
1015 of the buffer over, possibly temporarily (we'll expand it later
1016 if it's not a target-specific variable). PLEN saves the length
1017 of the unparsed section of p2, for later. */
1018 if (*lb_next != '\0')
1020 unsigned int l = p2 - variable_buffer;
1021 plen = strlen (p2);
1022 (void) variable_buffer_output (p2+plen,
1023 lb_next, strlen (lb_next)+1);
1024 p2 = variable_buffer + l;
1027 /* See if it's an "override" keyword; if so see if what comes after
1028 it looks like a variable definition. */
1030 wtype = get_next_mword (p2, NULL, &p, &len);
1032 v_origin = o_file;
1033 if (wtype == w_static && word1eq ("override"))
1035 v_origin = o_override;
1036 wtype = get_next_mword (p+len, NULL, &p, &len);
1039 if (wtype != w_eol)
1040 wtype = get_next_mword (p+len, NULL, NULL, NULL);
1042 if (wtype == w_varassign)
1044 /* If there was a semicolon found, add it back, plus anything
1045 after it. */
1046 if (semip)
1048 *(--semip) = ';';
1049 variable_buffer_output (p2 + strlen (p2),
1050 semip, strlen (semip)+1);
1052 record_target_var (filenames, p, two_colon, v_origin, fstart);
1053 filenames = 0;
1054 continue;
1057 /* This is a normal target, _not_ a target-specific variable.
1058 Unquote any = in the dependency list. */
1059 find_char_unquote (lb_next, '=', 0, 0);
1061 /* We have some targets, so don't ignore the following commands. */
1062 no_targets = 0;
1064 /* Expand the dependencies, etc. */
1065 if (*lb_next != '\0')
1067 unsigned int l = p2 - variable_buffer;
1068 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
1069 p2 = variable_buffer + l;
1071 /* Look for a semicolon in the expanded line. */
1072 if (cmdleft == 0)
1074 cmdleft = find_char_unquote (p2, ';', 0, 0);
1075 if (cmdleft != 0)
1076 *(cmdleft++) = '\0';
1080 /* Do any of the prerequisites appear to have $@ etc.? */
1081 have_sysv_atvar = 0;
1082 if (!posix_pedantic)
1083 for (p = strchr (p2, '$'); p != 0; p = strchr (p+1, '$'))
1084 if (p[1] == '@' || (p[1] == '(' && p[2] == '@'))
1086 have_sysv_atvar = 1;
1087 break;
1090 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
1091 p = strchr (p2, ':');
1092 while (p != 0 && p[-1] == '\\')
1094 register char *q = &p[-1];
1095 register int backslash = 0;
1096 while (*q-- == '\\')
1097 backslash = !backslash;
1098 if (backslash)
1099 p = strchr (p + 1, ':');
1100 else
1101 break;
1103 #ifdef _AMIGA
1104 /* Here, the situation is quite complicated. Let's have a look
1105 at a couple of targets:
1107 install: dev:make
1109 dev:make: make
1111 dev:make:: xyz
1113 The rule is that it's only a target, if there are TWO :'s
1114 OR a space around the :.
1116 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
1117 || isspace ((unsigned char)p[-1])))
1118 p = 0;
1119 #endif
1120 #ifdef HAVE_DOS_PATHS
1122 int check_again;
1124 do {
1125 check_again = 0;
1126 /* For DOS paths, skip a "C:\..." or a "C:/..." */
1127 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
1128 isalpha ((unsigned char)p[-1]) &&
1129 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1130 p = strchr (p + 1, ':');
1131 check_again = 1;
1133 } while (check_again);
1135 #endif
1136 if (p != 0)
1138 struct nameseq *target;
1139 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
1140 ++p2;
1141 if (target == 0)
1142 fatal (fstart, _("missing target pattern"));
1143 else if (target->next != 0)
1144 fatal (fstart, _("multiple target patterns"));
1145 pattern = target->name;
1146 pattern_percent = find_percent (pattern);
1147 if (pattern_percent == 0)
1148 fatal (fstart, _("target pattern contains no `%%'"));
1149 free((char *)target);
1151 else
1152 pattern = 0;
1154 /* Parse the dependencies. */
1155 deps = (struct dep *)
1156 multi_glob (parse_file_seq (&p2, '|', sizeof (struct dep), 1),
1157 sizeof (struct dep));
1158 if (*p2)
1160 /* Files that follow '|' are special prerequisites that
1161 need only exist in order to satisfy the dependency.
1162 Their modification times are irrelevant. */
1163 struct dep **deps_ptr = &deps;
1164 struct dep *d;
1165 for (deps_ptr = &deps; *deps_ptr; deps_ptr = &(*deps_ptr)->next)
1167 ++p2;
1168 *deps_ptr = (struct dep *)
1169 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
1170 sizeof (struct dep));
1171 for (d = *deps_ptr; d != 0; d = d->next)
1172 d->ignore_mtime = 1;
1175 commands_idx = 0;
1176 if (cmdleft != 0)
1178 /* Semicolon means rest of line is a command. */
1179 unsigned int len = strlen (cmdleft);
1181 cmds_started = fstart->lineno;
1183 /* Add this command line to the buffer. */
1184 if (len + 2 > commands_len)
1186 commands_len = (len + 2) * 2;
1187 commands = (char *) xrealloc (commands, commands_len);
1189 bcopy (cmdleft, commands, len);
1190 commands_idx += len;
1191 commands[commands_idx++] = '\n';
1194 continue;
1197 /* We get here except in the case that we just read a rule line.
1198 Record now the last rule we read, so following spurious
1199 commands are properly diagnosed. */
1200 rule_complete:
1201 record_waiting_files ();
1204 #undef word1eq
1206 if (conditionals->if_cmds)
1207 fatal (fstart, _("missing `endif'"));
1209 /* At eof, record the last rule. */
1210 record_waiting_files ();
1212 free ((char *) commands);
1214 return 1;
1218 /* Execute a `define' directive.
1219 The first line has already been read, and NAME is the name of
1220 the variable to be defined. The following lines remain to be read. */
1222 static void
1223 do_define (name, namelen, origin, ebuf)
1224 char *name;
1225 unsigned int namelen;
1226 enum variable_origin origin;
1227 struct ebuffer *ebuf;
1229 struct floc defstart;
1230 long nlines = 0;
1231 int nlevels = 1;
1232 unsigned int length = 100;
1233 char *definition = (char *) xmalloc (length);
1234 unsigned int idx = 0;
1235 char *p;
1237 /* Expand the variable name. */
1238 char *var = (char *) alloca (namelen + 1);
1239 bcopy (name, var, namelen);
1240 var[namelen] = '\0';
1241 var = variable_expand (var);
1243 defstart = ebuf->floc;
1245 while (1)
1247 unsigned int len;
1248 char *line;
1250 ebuf->floc.lineno += nlines;
1251 nlines = readline (ebuf);
1253 /* If there is nothing left to eval, we're done. */
1254 if (nlines < 0)
1255 break;
1257 line = ebuf->buffer;
1259 collapse_continuations (line);
1261 /* If the line doesn't begin with a tab, test to see if it introduces
1262 another define, or ends one. */
1264 /* Stop if we find an 'endef' */
1265 if (line[0] != '\t')
1267 p = next_token (line);
1268 len = strlen (p);
1270 /* If this is another 'define', increment the level count. */
1271 if ((len == 6 || (len > 6 && isblank ((unsigned char)p[6])))
1272 && strneq (p, "define", 6))
1273 ++nlevels;
1275 /* If this is an 'endef', decrement the count. If it's now 0,
1276 we've found the last one. */
1277 else if ((len == 5 || (len > 5 && isblank ((unsigned char)p[5])))
1278 && strneq (p, "endef", 5))
1280 p += 5;
1281 remove_comments (p);
1282 if (*next_token (p) != '\0')
1283 error (&ebuf->floc,
1284 _("Extraneous text after `endef' directive"));
1286 if (--nlevels == 0)
1288 /* Define the variable. */
1289 if (idx == 0)
1290 definition[0] = '\0';
1291 else
1292 definition[idx - 1] = '\0';
1294 /* Always define these variables in the global set. */
1295 define_variable_global (var, strlen (var), definition,
1296 origin, 1, &defstart);
1297 free (definition);
1298 return;
1303 /* Otherwise add this line to the variable definition. */
1304 len = strlen (line);
1305 if (idx + len + 1 > length)
1307 length = (idx + len) * 2;
1308 definition = (char *) xrealloc (definition, length + 1);
1311 bcopy (line, &definition[idx], len);
1312 idx += len;
1313 /* Separate lines with a newline. */
1314 definition[idx++] = '\n';
1317 /* No `endef'!! */
1318 fatal (&defstart, _("missing `endef', unterminated `define'"));
1320 /* NOTREACHED */
1321 return;
1324 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1325 "ifneq", "else" and "endif".
1326 LINE is the input line, with the command as its first word.
1328 FILENAME and LINENO are the filename and line number in the
1329 current makefile. They are used for error messages.
1331 Value is -1 if the line is invalid,
1332 0 if following text should be interpreted,
1333 1 if following text should be ignored. */
1335 static int
1336 conditional_line (line, flocp)
1337 char *line;
1338 const struct floc *flocp;
1340 int notdef;
1341 char *cmdname;
1342 register unsigned int i;
1344 if (*line == 'i')
1346 /* It's an "if..." command. */
1347 notdef = line[2] == 'n';
1348 if (notdef)
1350 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
1351 line += cmdname[3] == 'd' ? 7 : 6;
1353 else
1355 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
1356 line += cmdname[2] == 'd' ? 6 : 5;
1359 else
1361 /* It's an "else" or "endif" command. */
1362 notdef = line[1] == 'n';
1363 cmdname = notdef ? "endif" : "else";
1364 line += notdef ? 5 : 4;
1367 line = next_token (line);
1369 if (*cmdname == 'e')
1371 if (*line != '\0')
1372 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1373 /* "Else" or "endif". */
1374 if (conditionals->if_cmds == 0)
1375 fatal (flocp, _("extraneous `%s'"), cmdname);
1376 /* NOTDEF indicates an `endif' command. */
1377 if (notdef)
1378 --conditionals->if_cmds;
1379 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1380 fatal (flocp, _("only one `else' per conditional"));
1381 else
1383 /* Toggle the state of ignorance. */
1384 conditionals->ignoring[conditionals->if_cmds - 1]
1385 = !conditionals->ignoring[conditionals->if_cmds - 1];
1386 /* Record that we have seen an `else' in this conditional.
1387 A second `else' will be erroneous. */
1388 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1390 for (i = 0; i < conditionals->if_cmds; ++i)
1391 if (conditionals->ignoring[i])
1392 return 1;
1393 return 0;
1396 if (conditionals->allocated == 0)
1398 conditionals->allocated = 5;
1399 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1400 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1403 ++conditionals->if_cmds;
1404 if (conditionals->if_cmds > conditionals->allocated)
1406 conditionals->allocated += 5;
1407 conditionals->ignoring = (char *)
1408 xrealloc (conditionals->ignoring, conditionals->allocated);
1409 conditionals->seen_else = (char *)
1410 xrealloc (conditionals->seen_else, conditionals->allocated);
1413 /* Record that we have seen an `if...' but no `else' so far. */
1414 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1416 /* Search through the stack to see if we're already ignoring. */
1417 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1418 if (conditionals->ignoring[i])
1420 /* We are already ignoring, so just push a level
1421 to match the next "else" or "endif", and keep ignoring.
1422 We don't want to expand variables in the condition. */
1423 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1424 return 1;
1427 if (cmdname[notdef ? 3 : 2] == 'd')
1429 /* "Ifdef" or "ifndef". */
1430 char *var;
1431 struct variable *v;
1432 register char *p = end_of_token (line);
1433 i = p - line;
1434 p = next_token (p);
1435 if (*p != '\0')
1436 return -1;
1438 /* Expand the thing we're looking up, so we can use indirect and
1439 constructed variable names. */
1440 line[i] = '\0';
1441 var = allocated_variable_expand (line);
1443 v = lookup_variable (var, strlen (var));
1444 conditionals->ignoring[conditionals->if_cmds - 1]
1445 = (v != 0 && *v->value != '\0') == notdef;
1447 free (var);
1449 else
1451 /* "Ifeq" or "ifneq". */
1452 char *s1, *s2;
1453 unsigned int len;
1454 char termin = *line == '(' ? ',' : *line;
1456 if (termin != ',' && termin != '"' && termin != '\'')
1457 return -1;
1459 s1 = ++line;
1460 /* Find the end of the first string. */
1461 if (termin == ',')
1463 register int count = 0;
1464 for (; *line != '\0'; ++line)
1465 if (*line == '(')
1466 ++count;
1467 else if (*line == ')')
1468 --count;
1469 else if (*line == ',' && count <= 0)
1470 break;
1472 else
1473 while (*line != '\0' && *line != termin)
1474 ++line;
1476 if (*line == '\0')
1477 return -1;
1479 if (termin == ',')
1481 /* Strip blanks after the first string. */
1482 char *p = line++;
1483 while (isblank ((unsigned char)p[-1]))
1484 --p;
1485 *p = '\0';
1487 else
1488 *line++ = '\0';
1490 s2 = variable_expand (s1);
1491 /* We must allocate a new copy of the expanded string because
1492 variable_expand re-uses the same buffer. */
1493 len = strlen (s2);
1494 s1 = (char *) alloca (len + 1);
1495 bcopy (s2, s1, len + 1);
1497 if (termin != ',')
1498 /* Find the start of the second string. */
1499 line = next_token (line);
1501 termin = termin == ',' ? ')' : *line;
1502 if (termin != ')' && termin != '"' && termin != '\'')
1503 return -1;
1505 /* Find the end of the second string. */
1506 if (termin == ')')
1508 register int count = 0;
1509 s2 = next_token (line);
1510 for (line = s2; *line != '\0'; ++line)
1512 if (*line == '(')
1513 ++count;
1514 else if (*line == ')')
1516 if (count <= 0)
1517 break;
1518 else
1519 --count;
1523 else
1525 ++line;
1526 s2 = line;
1527 while (*line != '\0' && *line != termin)
1528 ++line;
1531 if (*line == '\0')
1532 return -1;
1534 *line = '\0';
1535 line = next_token (++line);
1536 if (*line != '\0')
1537 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1539 s2 = variable_expand (s2);
1540 conditionals->ignoring[conditionals->if_cmds - 1]
1541 = streq (s1, s2) == notdef;
1544 /* Search through the stack to see if we're ignoring. */
1545 for (i = 0; i < conditionals->if_cmds; ++i)
1546 if (conditionals->ignoring[i])
1547 return 1;
1548 return 0;
1551 /* Remove duplicate dependencies in CHAIN. */
1553 static unsigned long
1554 dep_hash_1 (void const *key)
1556 return_STRING_HASH_1 (dep_name ((struct dep const *) key));
1559 static unsigned long
1560 dep_hash_2 (void const *key)
1562 return_STRING_HASH_2 (dep_name ((struct dep const *) key));
1565 static int
1566 dep_hash_cmp (void const *x, void const *y)
1568 struct dep *dx = (struct dep *) x;
1569 struct dep *dy = (struct dep *) y;
1570 int cmp = strcmp (dep_name (dx), dep_name (dy));
1572 /* If the names are the same but ignore_mtimes are not equal, one of these
1573 is an order-only prerequisite and one isn't. That means that we should
1574 remove the one that isn't and keep the one that is. */
1576 if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
1577 dx->ignore_mtime = dy->ignore_mtime = 0;
1579 return cmp;
1583 void
1584 uniquize_deps (chain)
1585 struct dep *chain;
1587 struct hash_table deps;
1588 register struct dep **depp;
1590 hash_init (&deps, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
1592 /* Make sure that no dependencies are repeated. This does not
1593 really matter for the purpose of updating targets, but it
1594 might make some names be listed twice for $^ and $?. */
1596 depp = &chain;
1597 while (*depp)
1599 struct dep *dep = *depp;
1600 struct dep **dep_slot = (struct dep **) hash_find_slot (&deps, dep);
1601 if (HASH_VACANT (*dep_slot))
1603 hash_insert_at (&deps, dep, dep_slot);
1604 depp = &dep->next;
1606 else
1608 /* Don't bother freeing duplicates.
1609 It's dangerous and little benefit accrues. */
1610 *depp = dep->next;
1614 hash_free (&deps, 0);
1617 /* Record target-specific variable values for files FILENAMES.
1618 TWO_COLON is nonzero if a double colon was used.
1620 The links of FILENAMES are freed, and so are any names in it
1621 that are not incorporated into other data structures.
1623 If the target is a pattern, add the variable to the pattern-specific
1624 variable value list. */
1626 static void
1627 record_target_var (filenames, defn, two_colon, origin, flocp)
1628 struct nameseq *filenames;
1629 char *defn;
1630 int two_colon;
1631 enum variable_origin origin;
1632 const struct floc *flocp;
1634 struct nameseq *nextf;
1635 struct variable_set_list *global;
1637 global = current_variable_set_list;
1639 /* If the variable is an append version, store that but treat it as a
1640 normal recursive variable. */
1642 for (; filenames != 0; filenames = nextf)
1644 struct variable *v;
1645 register char *name = filenames->name;
1646 struct variable_set_list *vlist;
1647 char *fname;
1648 char *percent;
1650 nextf = filenames->next;
1651 free ((char *) filenames);
1653 /* If it's a pattern target, then add it to the pattern-specific
1654 variable list. */
1655 percent = find_percent (name);
1656 if (percent)
1658 struct pattern_var *p;
1660 /* Get a reference for this pattern-specific variable struct. */
1661 p = create_pattern_var(name, percent);
1662 vlist = p->vars;
1663 fname = p->target;
1665 else
1667 struct file *f;
1669 /* Get a file reference for this file, and initialize it.
1670 We don't want to just call enter_file() because that allocates a
1671 new entry if the file is a double-colon, which we don't want in
1672 this situation. */
1673 f = lookup_file (name);
1674 if (!f)
1675 f = enter_file (name);
1676 else if (f->double_colon)
1677 f = f->double_colon;
1679 initialize_file_variables (f, 1);
1680 vlist = f->variables;
1681 fname = f->name;
1684 /* Make the new variable context current and define the variable. */
1685 current_variable_set_list = vlist;
1686 v = try_variable_definition (flocp, defn, origin, 1);
1687 if (!v)
1688 error (flocp, _("Malformed per-target variable definition"));
1689 v->per_target = 1;
1691 /* If it's not an override, check to see if there was a command-line
1692 setting. If so, reset the value. */
1693 if (origin != o_override)
1695 struct variable *gv;
1696 int len = strlen(v->name);
1698 current_variable_set_list = global;
1699 gv = lookup_variable (v->name, len);
1700 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1702 v = define_variable_in_set (v->name, len, gv->value, gv->origin,
1703 gv->recursive, vlist->set, flocp);
1704 v->append = 0;
1708 /* Free name if not needed further. */
1709 if (name != fname && (name < fname || name > fname + strlen (fname)))
1710 free (name);
1713 current_variable_set_list = global;
1716 /* Record a description line for files FILENAMES,
1717 with dependencies DEPS, commands to execute described
1718 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1719 TWO_COLON is nonzero if a double colon was used.
1720 If not nil, PATTERN is the `%' pattern to make this
1721 a static pattern rule, and PATTERN_PERCENT is a pointer
1722 to the `%' within it.
1724 The links of FILENAMES are freed, and so are any names in it
1725 that are not incorporated into other data structures. */
1727 static void
1728 record_files (filenames, pattern, pattern_percent, deps, cmds_started,
1729 commands, commands_idx, two_colon, have_sysv_atvar,
1730 flocp, set_default)
1731 struct nameseq *filenames;
1732 char *pattern, *pattern_percent;
1733 struct dep *deps;
1734 unsigned int cmds_started;
1735 char *commands;
1736 unsigned int commands_idx;
1737 int two_colon;
1738 int have_sysv_atvar;
1739 const struct floc *flocp;
1740 int set_default;
1742 struct nameseq *nextf;
1743 int implicit = 0;
1744 unsigned int max_targets = 0, target_idx = 0;
1745 char **targets = 0, **target_percents = 0;
1746 struct commands *cmds;
1748 if (commands_idx > 0)
1750 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1751 cmds->fileinfo.filenm = flocp->filenm;
1752 cmds->fileinfo.lineno = cmds_started;
1753 cmds->commands = savestring (commands, commands_idx);
1754 cmds->command_lines = 0;
1756 else
1757 cmds = 0;
1759 for (; filenames != 0; filenames = nextf)
1761 char *name = filenames->name;
1762 struct file *f;
1763 struct dep *d;
1764 struct dep *this;
1765 char *implicit_percent;
1767 nextf = filenames->next;
1768 free (filenames);
1770 /* Check for .POSIX. We used to do this in snap_deps() but that's not
1771 good enough: it doesn't happen until after the makefile is read,
1772 which means we cannot use its value during parsing. */
1774 if (streq (name, ".POSIX"))
1775 posix_pedantic = 1;
1777 implicit_percent = find_percent (name);
1778 implicit |= implicit_percent != 0;
1780 if (implicit && pattern != 0)
1781 fatal (flocp, _("mixed implicit and static pattern rules"));
1783 if (implicit && implicit_percent == 0)
1784 fatal (flocp, _("mixed implicit and normal rules"));
1786 if (implicit)
1788 if (targets == 0)
1790 max_targets = 5;
1791 targets = (char **) xmalloc (5 * sizeof (char *));
1792 target_percents = (char **) xmalloc (5 * sizeof (char *));
1793 target_idx = 0;
1795 else if (target_idx == max_targets - 1)
1797 max_targets += 5;
1798 targets = (char **) xrealloc ((char *) targets,
1799 max_targets * sizeof (char *));
1800 target_percents
1801 = (char **) xrealloc ((char *) target_percents,
1802 max_targets * sizeof (char *));
1804 targets[target_idx] = name;
1805 target_percents[target_idx] = implicit_percent;
1806 ++target_idx;
1807 continue;
1810 /* If there are multiple filenames, copy the chain DEPS
1811 for all but the last one. It is not safe for the same deps
1812 to go in more than one place in the data base. */
1813 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1815 if (pattern != 0)
1817 /* If this is an extended static rule:
1818 `targets: target%pattern: dep%pattern; cmds',
1819 translate each dependency pattern into a plain filename
1820 using the target pattern and this target's name. */
1821 if (!pattern_matches (pattern, pattern_percent, name))
1823 /* Give a warning if the rule is meaningless. */
1824 error (flocp,
1825 _("target `%s' doesn't match the target pattern"), name);
1826 this = 0;
1828 else
1830 /* We use patsubst_expand to do the work of translating
1831 the target pattern, the target's name and the dependencies'
1832 patterns into plain dependency names. */
1833 char *buffer = variable_expand ("");
1835 for (d = this; d != 0; d = d->next)
1837 char *o;
1838 char *percent = find_percent (d->name);
1839 if (percent == 0)
1840 continue;
1841 o = patsubst_expand (buffer, name, pattern, d->name,
1842 pattern_percent, percent);
1843 /* If the name expanded to the empty string, that's
1844 illegal. */
1845 if (o == buffer)
1846 fatal (flocp,
1847 _("target `%s' leaves prerequisite pattern empty"),
1848 name);
1849 free (d->name);
1850 d->name = savestring (buffer, o - buffer);
1855 /* If at least one of the dependencies uses $$@ etc. deal with that.
1856 It would be very nice and very simple to just expand everything, but
1857 it would break a lot of backward compatibility. Maybe that's OK
1858 since we're just emulating a SysV function, and if we do that then
1859 why not emulate it completely (that's what SysV make does: it
1860 re-expands the entire prerequisite list, all the time, with $@
1861 etc. in scope. But, it would be a pain indeed to document this
1862 ("iff you use $$@, your prerequisite lists is expanded twice...")
1863 Ouch. Maybe better to make the code more complex. */
1865 if (have_sysv_atvar)
1867 char *p;
1868 int tlen = strlen (name);
1869 char *fnp = strrchr (name, '/');
1870 int dlen;
1871 int flen;
1873 if (fnp)
1875 dlen = fnp - name;
1876 ++fnp;
1877 flen = strlen (fnp);
1879 else
1881 dlen = 0;
1882 fnp = name;
1883 flen = tlen;
1887 for (d = this; d != 0; d = d->next)
1888 for (p = strchr (d->name, '$'); p != 0; p = strchr (p+1, '$'))
1890 char *s = p;
1891 char *at;
1892 int atlen;
1894 /* If it's a '$@' or '$(@', it's escaped */
1895 if ((++p)[0] == '$'
1896 && (p[1] == '@' || (p[1] == '(' && p[2] == '@')))
1898 bcopy (p, s, strlen (p)+1);
1899 continue;
1902 /* Maybe found one. Check. p will point to '@' [for $@] or
1903 ')' [for $(@)] or 'D' [for $(@D)] or 'F' [for $(@F)]. */
1904 if (p[0] != '@'
1905 && (p[0] != '(' || (++p)[0] != '@'
1906 || ((++p)[0] != ')'
1907 && (p[1] != ')' || (p[0] != 'D' && p[0] != 'F')))))
1908 continue;
1910 /* Found one. Compute the length and string ptr. Move p
1911 past the variable reference. */
1912 switch (p[0])
1914 case 'D':
1915 atlen = dlen;
1916 at = name;
1917 p += 2;
1918 break;
1920 case 'F':
1921 atlen = flen;
1922 at = fnp;
1923 p += 2;
1924 break;
1926 default:
1927 atlen = tlen;
1928 at = name;
1929 ++p;
1930 break;
1933 /* Get more space. */
1935 int soff = s - d->name;
1936 int poff = p - d->name;
1937 d->name = (char *) xrealloc (d->name,
1938 strlen (d->name) + atlen + 1);
1939 s = d->name + soff;
1940 p = d->name + poff;
1943 /* Copy the string over. */
1944 bcopy(p, s+atlen, strlen (p)+1);
1945 bcopy(at, s, atlen);
1946 p = s + atlen - 1;
1950 if (!two_colon)
1952 /* Single-colon. Combine these dependencies
1953 with others in file's existing record, if any. */
1954 f = enter_file (name);
1956 if (f->double_colon)
1957 fatal (flocp,
1958 _("target file `%s' has both : and :: entries"), f->name);
1960 /* If CMDS == F->CMDS, this target was listed in this rule
1961 more than once. Just give a warning since this is harmless. */
1962 if (cmds != 0 && cmds == f->cmds)
1963 error (flocp,
1964 _("target `%s' given more than once in the same rule."),
1965 f->name);
1967 /* Check for two single-colon entries both with commands.
1968 Check is_target so that we don't lose on files such as .c.o
1969 whose commands were preinitialized. */
1970 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1972 error (&cmds->fileinfo,
1973 _("warning: overriding commands for target `%s'"),
1974 f->name);
1975 error (&f->cmds->fileinfo,
1976 _("warning: ignoring old commands for target `%s'"),
1977 f->name);
1980 f->is_target = 1;
1982 /* Defining .DEFAULT with no deps or cmds clears it. */
1983 if (f == default_file && this == 0 && cmds == 0)
1984 f->cmds = 0;
1985 if (cmds != 0)
1986 f->cmds = cmds;
1987 /* Defining .SUFFIXES with no dependencies
1988 clears out the list of suffixes. */
1989 if (f == suffix_file && this == 0)
1991 d = f->deps;
1992 while (d != 0)
1994 struct dep *nextd = d->next;
1995 free (d->name);
1996 free ((char *)d);
1997 d = nextd;
1999 f->deps = 0;
2001 else if (f->deps != 0)
2003 /* Add the file's old deps and the new ones in THIS together. */
2005 struct dep *firstdeps, *moredeps;
2006 if (cmds != 0)
2008 /* This is the rule with commands, so put its deps first.
2009 The rationale behind this is that $< expands to the
2010 first dep in the chain, and commands use $< expecting
2011 to get the dep that rule specifies. */
2012 firstdeps = this;
2013 moredeps = f->deps;
2015 else
2017 /* Append the new deps to the old ones. */
2018 firstdeps = f->deps;
2019 moredeps = this;
2022 if (firstdeps == 0)
2023 firstdeps = moredeps;
2024 else
2026 d = firstdeps;
2027 while (d->next != 0)
2028 d = d->next;
2029 d->next = moredeps;
2032 f->deps = firstdeps;
2034 else
2035 f->deps = this;
2037 /* If this is a static pattern rule, set the file's stem to
2038 the part of its name that matched the `%' in the pattern,
2039 so you can use $* in the commands. */
2040 if (pattern != 0)
2042 static char *percent = "%";
2043 char *buffer = variable_expand ("");
2044 char *o = patsubst_expand (buffer, name, pattern, percent,
2045 pattern_percent, percent);
2046 f->stem = savestring (buffer, o - buffer);
2049 else
2051 /* Double-colon. Make a new record
2052 even if the file already has one. */
2053 f = lookup_file (name);
2054 /* Check for both : and :: rules. Check is_target so
2055 we don't lose on default suffix rules or makefiles. */
2056 if (f != 0 && f->is_target && !f->double_colon)
2057 fatal (flocp,
2058 _("target file `%s' has both : and :: entries"), f->name);
2059 f = enter_file (name);
2060 /* If there was an existing entry and it was a double-colon
2061 entry, enter_file will have returned a new one, making it the
2062 prev pointer of the old one, and setting its double_colon
2063 pointer to the first one. */
2064 if (f->double_colon == 0)
2065 /* This is the first entry for this name, so we must
2066 set its double_colon pointer to itself. */
2067 f->double_colon = f;
2068 f->is_target = 1;
2069 f->deps = this;
2070 f->cmds = cmds;
2073 /* Free name if not needed further. */
2074 if (f != 0 && name != f->name
2075 && (name < f->name || name > f->name + strlen (f->name)))
2077 free (name);
2078 name = f->name;
2081 /* See if this is first target seen whose name does
2082 not start with a `.', unless it contains a slash. */
2083 if (default_goal_file == 0 && set_default
2084 && (*name != '.' || strchr (name, '/') != 0
2085 #ifdef HAVE_DOS_PATHS
2086 || strchr (name, '\\') != 0
2087 #endif
2090 int reject = 0;
2092 /* If this file is a suffix, don't
2093 let it be the default goal file. */
2095 for (d = suffix_file->deps; d != 0; d = d->next)
2097 register struct dep *d2;
2098 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
2100 reject = 1;
2101 break;
2103 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
2105 register unsigned int len = strlen (dep_name (d2));
2106 if (!strneq (name, dep_name (d2), len))
2107 continue;
2108 if (streq (name + len, dep_name (d)))
2110 reject = 1;
2111 break;
2114 if (reject)
2115 break;
2118 if (!reject)
2119 default_goal_file = f;
2123 if (implicit)
2125 targets[target_idx] = 0;
2126 target_percents[target_idx] = 0;
2127 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
2128 free ((char *) target_percents);
2132 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2133 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2134 Quoting backslashes are removed from STRING by compacting it into
2135 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2136 one, or nil if there are none. */
2138 char *
2139 find_char_unquote (string, stop1, stop2, blank)
2140 char *string;
2141 int stop1;
2142 int stop2;
2143 int blank;
2145 unsigned int string_len = 0;
2146 register char *p = string;
2148 while (1)
2150 if (stop2 && blank)
2151 while (*p != '\0' && *p != stop1 && *p != stop2
2152 && ! isblank ((unsigned char) *p))
2153 ++p;
2154 else if (stop2)
2155 while (*p != '\0' && *p != stop1 && *p != stop2)
2156 ++p;
2157 else if (blank)
2158 while (*p != '\0' && *p != stop1
2159 && ! isblank ((unsigned char) *p))
2160 ++p;
2161 else
2162 while (*p != '\0' && *p != stop1)
2163 ++p;
2165 if (*p == '\0')
2166 break;
2168 if (p > string && p[-1] == '\\')
2170 /* Search for more backslashes. */
2171 register int i = -2;
2172 while (&p[i] >= string && p[i] == '\\')
2173 --i;
2174 ++i;
2175 /* Only compute the length if really needed. */
2176 if (string_len == 0)
2177 string_len = strlen (string);
2178 /* The number of backslashes is now -I.
2179 Copy P over itself to swallow half of them. */
2180 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
2181 p += i / 2;
2182 if (i % 2 == 0)
2183 /* All the backslashes quoted each other; the STOPCHAR was
2184 unquoted. */
2185 return p;
2187 /* The STOPCHAR was quoted by a backslash. Look for another. */
2189 else
2190 /* No backslash in sight. */
2191 return p;
2194 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2195 return 0;
2198 /* Search PATTERN for an unquoted %. */
2200 char *
2201 find_percent (pattern)
2202 char *pattern;
2204 return find_char_unquote (pattern, '%', 0, 0);
2207 /* Parse a string into a sequence of filenames represented as a
2208 chain of struct nameseq's in reverse order and return that chain.
2210 The string is passed as STRINGP, the address of a string pointer.
2211 The string pointer is updated to point at the first character
2212 not parsed, which either is a null char or equals STOPCHAR.
2214 SIZE is how big to construct chain elements.
2215 This is useful if we want them actually to be other structures
2216 that have room for additional info.
2218 If STRIP is nonzero, strip `./'s off the beginning. */
2220 struct nameseq *
2221 parse_file_seq (stringp, stopchar, size, strip)
2222 char **stringp;
2223 int stopchar;
2224 unsigned int size;
2225 int strip;
2227 register struct nameseq *new = 0;
2228 register struct nameseq *new1, *lastnew1;
2229 register char *p = *stringp;
2230 char *q;
2231 char *name;
2233 #ifdef VMS
2234 # define VMS_COMMA ','
2235 #else
2236 # define VMS_COMMA 0
2237 #endif
2239 while (1)
2241 /* Skip whitespace; see if any more names are left. */
2242 p = next_token (p);
2243 if (*p == '\0')
2244 break;
2245 if (*p == stopchar)
2246 break;
2248 /* Yes, find end of next name. */
2249 q = p;
2250 p = find_char_unquote (q, stopchar, VMS_COMMA, 1);
2251 #ifdef VMS
2252 /* convert comma separated list to space separated */
2253 if (p && *p == ',')
2254 *p =' ';
2255 #endif
2256 #ifdef _AMIGA
2257 if (stopchar == ':' && p && *p == ':'
2258 && !(isspace ((unsigned char)p[1]) || !p[1]
2259 || isspace ((unsigned char)p[-1])))
2261 p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1);
2263 #endif
2264 #ifdef HAVE_DOS_PATHS
2265 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
2266 first colon which isn't followed by a slash or a backslash.
2267 Note that tokens separated by spaces should be treated as separate
2268 tokens since make doesn't allow path names with spaces */
2269 if (stopchar == ':')
2270 while (p != 0 && !isspace ((unsigned char)*p) &&
2271 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
2272 p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1);
2273 #endif
2274 if (p == 0)
2275 p = q + strlen (q);
2277 if (strip)
2278 #ifdef VMS
2279 /* Skip leading `[]'s. */
2280 while (p - q > 2 && q[0] == '[' && q[1] == ']')
2281 #else
2282 /* Skip leading `./'s. */
2283 while (p - q > 2 && q[0] == '.' && q[1] == '/')
2284 #endif
2286 q += 2; /* Skip "./". */
2287 while (q < p && *q == '/')
2288 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
2289 ++q;
2292 /* Extract the filename just found, and skip it. */
2294 if (q == p)
2295 /* ".///" was stripped to "". */
2296 #ifdef VMS
2297 continue;
2298 #else
2299 #ifdef _AMIGA
2300 name = savestring ("", 0);
2301 #else
2302 name = savestring ("./", 2);
2303 #endif
2304 #endif
2305 else
2306 #ifdef VMS
2307 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
2308 * to remove this '\' before we can use the filename.
2309 * Savestring called because q may be read-only string constant.
2312 char *qbase = xstrdup (q);
2313 char *pbase = qbase + (p-q);
2314 char *q1 = qbase;
2315 char *q2 = q1;
2316 char *p1 = pbase;
2318 while (q1 != pbase)
2320 if (*q1 == '\\' && *(q1+1) == ':')
2322 q1++;
2323 p1--;
2325 *q2++ = *q1++;
2327 name = savestring (qbase, p1 - qbase);
2328 free (qbase);
2330 #else
2331 name = savestring (q, p - q);
2332 #endif
2334 /* Add it to the front of the chain. */
2335 new1 = (struct nameseq *) xmalloc (size);
2336 new1->name = name;
2337 new1->next = new;
2338 new = new1;
2341 #ifndef NO_ARCHIVES
2343 /* Look for multi-word archive references.
2344 They are indicated by a elt ending with an unmatched `)' and
2345 an elt further down the chain (i.e., previous in the file list)
2346 with an unmatched `(' (e.g., "lib(mem"). */
2348 new1 = new;
2349 lastnew1 = 0;
2350 while (new1 != 0)
2351 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2352 && new1->name[strlen (new1->name) - 1] == ')'
2353 && strchr (new1->name, '(') == 0)
2355 /* NEW1 ends with a `)' but does not contain a `('.
2356 Look back for an elt with an opening `(' but no closing `)'. */
2358 struct nameseq *n = new1->next, *lastn = new1;
2359 char *paren = 0;
2360 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2362 lastn = n;
2363 n = n->next;
2365 if (n != 0
2366 /* Ignore something starting with `(', as that cannot actually
2367 be an archive-member reference (and treating it as such
2368 results in an empty file name, which causes much lossage). */
2369 && n->name[0] != '(')
2371 /* N is the first element in the archive group.
2372 Its name looks like "lib(mem" (with no closing `)'). */
2374 char *libname;
2376 /* Copy "lib(" into LIBNAME. */
2377 ++paren;
2378 libname = (char *) alloca (paren - n->name + 1);
2379 bcopy (n->name, libname, paren - n->name);
2380 libname[paren - n->name] = '\0';
2382 if (*paren == '\0')
2384 /* N was just "lib(", part of something like "lib( a b)".
2385 Edit it out of the chain and free its storage. */
2386 lastn->next = n->next;
2387 free (n->name);
2388 free ((char *) n);
2389 /* LASTN->next is the new stopping elt for the loop below. */
2390 n = lastn->next;
2392 else
2394 /* Replace N's name with the full archive reference. */
2395 name = concat (libname, paren, ")");
2396 free (n->name);
2397 n->name = name;
2400 if (new1->name[1] == '\0')
2402 /* NEW1 is just ")", part of something like "lib(a b )".
2403 Omit it from the chain and free its storage. */
2404 if (lastnew1 == 0)
2405 new = new1->next;
2406 else
2407 lastnew1->next = new1->next;
2408 lastn = new1;
2409 new1 = new1->next;
2410 free (lastn->name);
2411 free ((char *) lastn);
2413 else
2415 /* Replace also NEW1->name, which already has closing `)'. */
2416 name = concat (libname, new1->name, "");
2417 free (new1->name);
2418 new1->name = name;
2419 new1 = new1->next;
2422 /* Trace back from NEW1 (the end of the list) until N
2423 (the beginning of the list), rewriting each name
2424 with the full archive reference. */
2426 while (new1 != n)
2428 name = concat (libname, new1->name, ")");
2429 free (new1->name);
2430 new1->name = name;
2431 lastnew1 = new1;
2432 new1 = new1->next;
2435 else
2437 /* No frobnication happening. Just step down the list. */
2438 lastnew1 = new1;
2439 new1 = new1->next;
2442 else
2444 lastnew1 = new1;
2445 new1 = new1->next;
2448 #endif
2450 *stringp = p;
2451 return new;
2454 /* Find the next line of text in an eval buffer, combining continuation lines
2455 into one line.
2456 Return the number of actual lines read (> 1 if continuation lines).
2457 Returns -1 if there's nothing left in the buffer.
2459 After this function, ebuf->buffer points to the first character of the
2460 line we just found.
2463 /* Read a line of text from a STRING.
2464 Since we aren't really reading from a file, don't bother with linenumbers.
2467 static unsigned long
2468 readstring (ebuf)
2469 struct ebuffer *ebuf;
2471 char *p;
2473 /* If there is nothing left in this buffer, return 0. */
2474 if (ebuf->bufnext > ebuf->bufstart + ebuf->size)
2475 return -1;
2477 /* Set up a new starting point for the buffer, and find the end of the
2478 next logical line (taking into account backslash/newline pairs). */
2480 p = ebuf->buffer = ebuf->bufnext;
2482 while (1)
2484 int backslash = 0;
2486 /* Find the next newline. Keep track of backslashes as we look. */
2487 for (; *p != '\n' && *p != '\0'; ++p)
2488 if (*p == '\\')
2489 backslash = !backslash;
2491 /* If we got to the end of the string or a newline with no backslash,
2492 we're done. */
2493 if (*p == '\0' || !backslash)
2494 break;
2497 /* Overwrite the newline char. */
2498 *p = '\0';
2499 ebuf->bufnext = p+1;
2501 return 0;
2504 static long
2505 readline (ebuf)
2506 struct ebuffer *ebuf;
2508 char *p;
2509 char *end;
2510 char *start;
2511 long nlines = 0;
2513 /* The behaviors between string and stream buffers are different enough to
2514 warrant different functions. Do the Right Thing. */
2516 if (!ebuf->fp)
2517 return readstring (ebuf);
2519 /* When reading from a file, we always start over at the beginning of the
2520 buffer for each new line. */
2522 p = start = ebuf->bufstart;
2523 end = p + ebuf->size;
2524 *p = '\0';
2526 while (fgets (p, end - p, ebuf->fp) != 0)
2528 char *p2;
2529 unsigned long len;
2530 int backslash;
2532 len = strlen (p);
2533 if (len == 0)
2535 /* This only happens when the first thing on the line is a '\0'.
2536 It is a pretty hopeless case, but (wonder of wonders) Athena
2537 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2538 There is nothing really to be done; we synthesize a newline so
2539 the following line doesn't appear to be part of this line. */
2540 error (&ebuf->floc,
2541 _("warning: NUL character seen; rest of line ignored"));
2542 p[0] = '\n';
2543 len = 1;
2546 /* Jump past the text we just read. */
2547 p += len;
2549 /* If the last char isn't a newline, the whole line didn't fit into the
2550 buffer. Get some more buffer and try again. */
2551 if (p[-1] != '\n')
2552 goto more_buffer;
2554 /* We got a newline, so add one to the count of lines. */
2555 ++nlines;
2557 #if !defined(WINDOWS32) && !defined(__MSDOS__)
2558 /* Check to see if the line was really ended with CRLF; if so ignore
2559 the CR. */
2560 if ((p - start) > 1 && p[-2] == '\r')
2562 --p;
2563 p[-1] = '\n';
2565 #endif
2567 backslash = 0;
2568 for (p2 = p - 2; p2 >= start; --p2)
2570 if (*p2 != '\\')
2571 break;
2572 backslash = !backslash;
2575 if (!backslash)
2577 p[-1] = '\0';
2578 break;
2581 /* It was a backslash/newline combo. If we have more space, read
2582 another line. */
2583 if (end - p >= 80)
2584 continue;
2586 /* We need more space at the end of our buffer, so realloc it.
2587 Make sure to preserve the current offset of p. */
2588 more_buffer:
2590 unsigned long off = p - start;
2591 ebuf->size *= 2;
2592 start = ebuf->buffer = ebuf->bufstart = (char *) xrealloc (start,
2593 ebuf->size);
2594 p = start + off;
2595 end = start + ebuf->size;
2596 *p = '\0';
2600 if (ferror (ebuf->fp))
2601 pfatal_with_name (ebuf->floc.filenm);
2603 /* If we found some lines, return how many.
2604 If we didn't, but we did find _something_, that indicates we read the last
2605 line of a file with no final newline; return 1.
2606 If we read nothing, we're at EOF; return -1. */
2608 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
2611 /* Parse the next "makefile word" from the input buffer, and return info
2612 about it.
2614 A "makefile word" is one of:
2616 w_bogus Should never happen
2617 w_eol End of input
2618 w_static A static word; cannot be expanded
2619 w_variable A word containing one or more variables/functions
2620 w_colon A colon
2621 w_dcolon A double-colon
2622 w_semicolon A semicolon
2623 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2625 Note that this function is only used when reading certain parts of the
2626 makefile. Don't use it where special rules hold sway (RHS of a variable,
2627 in a command list, etc.) */
2629 static enum make_word_type
2630 get_next_mword (buffer, delim, startp, length)
2631 char *buffer;
2632 char *delim;
2633 char **startp;
2634 unsigned int *length;
2636 enum make_word_type wtype = w_bogus;
2637 char *p = buffer, *beg;
2638 char c;
2640 /* Skip any leading whitespace. */
2641 while (isblank ((unsigned char)*p))
2642 ++p;
2644 beg = p;
2645 c = *(p++);
2646 switch (c)
2648 case '\0':
2649 wtype = w_eol;
2650 break;
2652 case ';':
2653 wtype = w_semicolon;
2654 break;
2656 case '=':
2657 wtype = w_varassign;
2658 break;
2660 case ':':
2661 wtype = w_colon;
2662 switch (*p)
2664 case ':':
2665 ++p;
2666 wtype = w_dcolon;
2667 break;
2669 case '=':
2670 ++p;
2671 wtype = w_varassign;
2672 break;
2674 break;
2676 case '+':
2677 case '?':
2678 if (*p == '=')
2680 ++p;
2681 wtype = w_varassign;
2682 break;
2685 default:
2686 if (delim && strchr (delim, c))
2687 wtype = w_static;
2688 break;
2691 /* Did we find something? If so, return now. */
2692 if (wtype != w_bogus)
2693 goto done;
2695 /* This is some non-operator word. A word consists of the longest
2696 string of characters that doesn't contain whitespace, one of [:=#],
2697 or [?+]=, or one of the chars in the DELIM string. */
2699 /* We start out assuming a static word; if we see a variable we'll
2700 adjust our assumptions then. */
2701 wtype = w_static;
2703 /* We already found the first value of "c", above. */
2704 while (1)
2706 char closeparen;
2707 int count;
2709 switch (c)
2711 case '\0':
2712 case ' ':
2713 case '\t':
2714 case '=':
2715 goto done_word;
2717 case ':':
2718 #ifdef HAVE_DOS_PATHS
2719 /* A word CAN include a colon in its drive spec. The drive
2720 spec is allowed either at the beginning of a word, or as part
2721 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2722 if (!(p - beg >= 2
2723 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
2724 && (p - beg == 2 || p[-3] == '(')))
2725 #endif
2726 goto done_word;
2728 case '$':
2729 c = *(p++);
2730 if (c == '$')
2731 break;
2733 /* This is a variable reference, so note that it's expandable.
2734 Then read it to the matching close paren. */
2735 wtype = w_variable;
2737 if (c == '(')
2738 closeparen = ')';
2739 else if (c == '{')
2740 closeparen = '}';
2741 else
2742 /* This is a single-letter variable reference. */
2743 break;
2745 for (count=0; *p != '\0'; ++p)
2747 if (*p == c)
2748 ++count;
2749 else if (*p == closeparen && --count < 0)
2751 ++p;
2752 break;
2755 break;
2757 case '?':
2758 case '+':
2759 if (*p == '=')
2760 goto done_word;
2761 break;
2763 case '\\':
2764 switch (*p)
2766 case ':':
2767 case ';':
2768 case '=':
2769 case '\\':
2770 ++p;
2771 break;
2773 break;
2775 default:
2776 if (delim && strchr (delim, c))
2777 goto done_word;
2778 break;
2781 c = *(p++);
2783 done_word:
2784 --p;
2786 done:
2787 if (startp)
2788 *startp = beg;
2789 if (length)
2790 *length = p - beg;
2791 return wtype;
2794 /* Construct the list of include directories
2795 from the arguments and the default list. */
2797 void
2798 construct_include_path (arg_dirs)
2799 char **arg_dirs;
2801 register unsigned int i;
2802 #ifdef VAXC /* just don't ask ... */
2803 stat_t stbuf;
2804 #else
2805 struct stat stbuf;
2806 #endif
2807 /* Table to hold the dirs. */
2809 register unsigned int defsize = (sizeof (default_include_directories)
2810 / sizeof (default_include_directories[0]));
2811 register unsigned int max = 5;
2812 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
2813 register unsigned int idx = 0;
2815 #ifdef __MSDOS__
2816 defsize++;
2817 #endif
2819 /* First consider any dirs specified with -I switches.
2820 Ignore dirs that don't exist. */
2822 if (arg_dirs != 0)
2823 while (*arg_dirs != 0)
2825 char *dir = *arg_dirs++;
2827 if (dir[0] == '~')
2829 char *expanded = tilde_expand (dir);
2830 if (expanded != 0)
2831 dir = expanded;
2834 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
2836 if (idx == max - 1)
2838 max += 5;
2839 dirs = (char **)
2840 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
2842 dirs[idx++] = dir;
2844 else if (dir != arg_dirs[-1])
2845 free (dir);
2848 /* Now add at the end the standard default dirs. */
2850 #ifdef __MSDOS__
2852 /* The environment variable $DJDIR holds the root of the
2853 DJGPP directory tree; add ${DJDIR}/include. */
2854 struct variable *djdir = lookup_variable ("DJDIR", 5);
2856 if (djdir)
2858 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2860 strcat (strcpy (defdir, djdir->value), "/include");
2861 dirs[idx++] = defdir;
2864 #endif
2866 for (i = 0; default_include_directories[i] != 0; ++i)
2867 if (stat (default_include_directories[i], &stbuf) == 0
2868 && S_ISDIR (stbuf.st_mode))
2869 dirs[idx++] = default_include_directories[i];
2871 dirs[idx] = 0;
2873 /* Now compute the maximum length of any name in it. */
2875 max_incl_len = 0;
2876 for (i = 0; i < idx; ++i)
2878 unsigned int len = strlen (dirs[i]);
2879 /* If dir name is written with a trailing slash, discard it. */
2880 if (dirs[i][len - 1] == '/')
2881 /* We can't just clobber a null in because it may have come from
2882 a literal string and literal strings may not be writable. */
2883 dirs[i] = savestring (dirs[i], len - 1);
2884 if (len > max_incl_len)
2885 max_incl_len = len;
2888 include_directories = dirs;
2891 /* Expand ~ or ~USER at the beginning of NAME.
2892 Return a newly malloc'd string or 0. */
2894 char *
2895 tilde_expand (name)
2896 char *name;
2898 #ifndef VMS
2899 if (name[1] == '/' || name[1] == '\0')
2901 extern char *getenv ();
2902 char *home_dir;
2903 int is_variable;
2906 /* Turn off --warn-undefined-variables while we expand HOME. */
2907 int save = warn_undefined_variables_flag;
2908 warn_undefined_variables_flag = 0;
2910 home_dir = allocated_variable_expand ("$(HOME)");
2912 warn_undefined_variables_flag = save;
2915 is_variable = home_dir[0] != '\0';
2916 if (!is_variable)
2918 free (home_dir);
2919 home_dir = getenv ("HOME");
2921 #if !defined(_AMIGA) && !defined(WINDOWS32)
2922 if (home_dir == 0 || home_dir[0] == '\0')
2924 extern char *getlogin ();
2925 char *logname = getlogin ();
2926 home_dir = 0;
2927 if (logname != 0)
2929 struct passwd *p = getpwnam (logname);
2930 if (p != 0)
2931 home_dir = p->pw_dir;
2934 #endif /* !AMIGA && !WINDOWS32 */
2935 if (home_dir != 0)
2937 char *new = concat (home_dir, "", name + 1);
2938 if (is_variable)
2939 free (home_dir);
2940 return new;
2943 #if !defined(_AMIGA) && !defined(WINDOWS32)
2944 else
2946 struct passwd *pwent;
2947 char *userend = strchr (name + 1, '/');
2948 if (userend != 0)
2949 *userend = '\0';
2950 pwent = getpwnam (name + 1);
2951 if (pwent != 0)
2953 if (userend == 0)
2954 return xstrdup (pwent->pw_dir);
2955 else
2956 return concat (pwent->pw_dir, "/", userend + 1);
2958 else if (userend != 0)
2959 *userend = '/';
2961 #endif /* !AMIGA && !WINDOWS32 */
2962 #endif /* !VMS */
2963 return 0;
2966 /* Given a chain of struct nameseq's describing a sequence of filenames,
2967 in reverse of the intended order, return a new chain describing the
2968 result of globbing the filenames. The new chain is in forward order.
2969 The links of the old chain are freed or used in the new chain.
2970 Likewise for the names in the old chain.
2972 SIZE is how big to construct chain elements.
2973 This is useful if we want them actually to be other structures
2974 that have room for additional info. */
2976 struct nameseq *
2977 multi_glob (chain, size)
2978 struct nameseq *chain;
2979 unsigned int size;
2981 extern void dir_setup_glob ();
2982 register struct nameseq *new = 0;
2983 register struct nameseq *old;
2984 struct nameseq *nexto;
2985 glob_t gl;
2987 dir_setup_glob (&gl);
2989 for (old = chain; old != 0; old = nexto)
2991 #ifndef NO_ARCHIVES
2992 char *memname;
2993 #endif
2995 nexto = old->next;
2997 if (old->name[0] == '~')
2999 char *newname = tilde_expand (old->name);
3000 if (newname != 0)
3002 free (old->name);
3003 old->name = newname;
3007 #ifndef NO_ARCHIVES
3008 if (ar_name (old->name))
3010 /* OLD->name is an archive member reference.
3011 Replace it with the archive file name,
3012 and save the member name in MEMNAME.
3013 We will glob on the archive name and then
3014 reattach MEMNAME later. */
3015 char *arname;
3016 ar_parse_name (old->name, &arname, &memname);
3017 free (old->name);
3018 old->name = arname;
3020 else
3021 memname = 0;
3022 #endif /* !NO_ARCHIVES */
3024 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
3026 case 0: /* Success. */
3028 register int i = gl.gl_pathc;
3029 while (i-- > 0)
3031 #ifndef NO_ARCHIVES
3032 if (memname != 0)
3034 /* Try to glob on MEMNAME within the archive. */
3035 struct nameseq *found
3036 = ar_glob (gl.gl_pathv[i], memname, size);
3037 if (found == 0)
3039 /* No matches. Use MEMNAME as-is. */
3040 unsigned int alen = strlen (gl.gl_pathv[i]);
3041 unsigned int mlen = strlen (memname);
3042 struct nameseq *elt
3043 = (struct nameseq *) xmalloc (size);
3044 if (size > sizeof (struct nameseq))
3045 bzero (((char *) elt) + sizeof (struct nameseq),
3046 size - sizeof (struct nameseq));
3047 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
3048 bcopy (gl.gl_pathv[i], elt->name, alen);
3049 elt->name[alen] = '(';
3050 bcopy (memname, &elt->name[alen + 1], mlen);
3051 elt->name[alen + 1 + mlen] = ')';
3052 elt->name[alen + 1 + mlen + 1] = '\0';
3053 elt->next = new;
3054 new = elt;
3056 else
3058 /* Find the end of the FOUND chain. */
3059 struct nameseq *f = found;
3060 while (f->next != 0)
3061 f = f->next;
3063 /* Attach the chain being built to the end of the FOUND
3064 chain, and make FOUND the new NEW chain. */
3065 f->next = new;
3066 new = found;
3069 free (memname);
3071 else
3072 #endif /* !NO_ARCHIVES */
3074 struct nameseq *elt = (struct nameseq *) xmalloc (size);
3075 if (size > sizeof (struct nameseq))
3076 bzero (((char *) elt) + sizeof (struct nameseq),
3077 size - sizeof (struct nameseq));
3078 elt->name = xstrdup (gl.gl_pathv[i]);
3079 elt->next = new;
3080 new = elt;
3083 globfree (&gl);
3084 free (old->name);
3085 free ((char *)old);
3086 break;
3089 case GLOB_NOSPACE:
3090 fatal (NILF, _("virtual memory exhausted"));
3091 break;
3093 default:
3094 old->next = new;
3095 new = old;
3096 break;
3100 return new;