Update the test template. A few fixes in run_make_test().
[make/kirr.git] / read.c
blob3704836091d58c14f0abaad78c2cbcdfc0f403d6
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 enum variable_origin origin,
141 int enabled,
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 (char **makefiles)
151 unsigned int num_makefiles = 0;
153 /* Create *_LIST variables, to hold the makefiles, targets, and variables
154 we will be reading. */
156 define_variable ("MAKEFILE_LIST", sizeof ("MAKEFILE_LIST")-1, "", o_file, 0);
158 DB (DB_BASIC, (_("Reading makefiles...\n")));
160 /* If there's a non-null variable MAKEFILES, its value is a list of
161 files to read first thing. But don't let it prevent reading the
162 default makefiles and don't let the default goal come from there. */
165 char *value;
166 char *name, *p;
167 unsigned int length;
170 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
171 int save = warn_undefined_variables_flag;
172 warn_undefined_variables_flag = 0;
174 value = allocated_variable_expand ("$(MAKEFILES)");
176 warn_undefined_variables_flag = save;
179 /* Set NAME to the start of next token and LENGTH to its length.
180 MAKEFILES is updated for finding remaining tokens. */
181 p = value;
183 while ((name = find_next_token (&p, &length)) != 0)
185 if (*p != '\0')
186 *p++ = '\0';
187 name = xstrdup (name);
188 if (eval_makefile (name,
189 RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE) < 2)
190 free (name);
193 free (value);
196 /* Read makefiles specified with -f switches. */
198 if (makefiles != 0)
199 while (*makefiles != 0)
201 struct dep *tail = read_makefiles;
202 register struct dep *d;
204 if (! eval_makefile (*makefiles, 0))
205 perror_with_name ("", *makefiles);
207 /* Find the right element of read_makefiles. */
208 d = read_makefiles;
209 while (d->next != tail)
210 d = d->next;
212 /* Use the storage read_makefile allocates. */
213 *makefiles = dep_name (d);
214 ++num_makefiles;
215 ++makefiles;
218 /* If there were no -f switches, try the default names. */
220 if (num_makefiles == 0)
222 static char *default_makefiles[] =
223 #ifdef VMS
224 /* all lower case since readdir() (the vms version) 'lowercasifies' */
225 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
226 #else
227 #ifdef _AMIGA
228 { "GNUmakefile", "Makefile", "SMakefile", 0 };
229 #else /* !Amiga && !VMS */
230 { "GNUmakefile", "makefile", "Makefile", 0 };
231 #endif /* AMIGA */
232 #endif /* VMS */
233 register char **p = default_makefiles;
234 while (*p != 0 && !file_exists_p (*p))
235 ++p;
237 if (*p != 0)
239 if (! eval_makefile (*p, 0))
240 perror_with_name ("", *p);
242 else
244 /* No default makefile was found. Add the default makefiles to the
245 `read_makefiles' chain so they will be updated if possible. */
246 struct dep *tail = read_makefiles;
247 /* Add them to the tail, after any MAKEFILES variable makefiles. */
248 while (tail != 0 && tail->next != 0)
249 tail = tail->next;
250 for (p = default_makefiles; *p != 0; ++p)
252 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
253 d->name = 0;
254 d->file = enter_file (*p);
255 d->file->dontcare = 1;
256 d->ignore_mtime = 0;
257 /* Tell update_goal_chain to bail out as soon as this file is
258 made, and main not to die if we can't make this file. */
259 d->changed = RM_DONTCARE;
260 if (tail == 0)
261 read_makefiles = d;
262 else
263 tail->next = d;
264 tail = d;
266 if (tail != 0)
267 tail->next = 0;
271 return read_makefiles;
274 /* Install a new conditional and return the previous one. */
276 static struct conditionals *
277 install_conditionals (struct conditionals *new)
279 struct conditionals *save = conditionals;
281 bzero ((char *) new, sizeof (*new));
282 conditionals = new;
284 return save;
287 /* Free the current conditionals and reinstate a saved one. */
289 static void
290 restore_conditionals (struct conditionals *saved)
292 /* Free any space allocated by conditional_line. */
293 if (conditionals->ignoring)
294 free (conditionals->ignoring);
295 if (conditionals->seen_else)
296 free (conditionals->seen_else);
298 /* Restore state. */
299 conditionals = saved;
302 static int
303 eval_makefile (char *filename, int flags)
305 struct dep *deps;
306 struct ebuffer ebuf;
307 const struct floc *curfile;
308 int makefile_errno;
309 int r;
311 ebuf.floc.filenm = filename;
312 ebuf.floc.lineno = 1;
314 if (ISDB (DB_VERBOSE))
316 printf (_("Reading makefile `%s'"), filename);
317 if (flags & RM_NO_DEFAULT_GOAL)
318 printf (_(" (no default goal)"));
319 if (flags & RM_INCLUDED)
320 printf (_(" (search path)"));
321 if (flags & RM_DONTCARE)
322 printf (_(" (don't care)"));
323 if (flags & RM_NO_TILDE)
324 printf (_(" (no ~ expansion)"));
325 puts ("...");
328 /* First, get a stream to read. */
330 /* Expand ~ in FILENAME unless it came from `include',
331 in which case it was already done. */
332 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
334 char *expanded = tilde_expand (filename);
335 if (expanded != 0)
336 filename = expanded;
339 ebuf.fp = fopen (filename, "r");
340 /* Save the error code so we print the right message later. */
341 makefile_errno = errno;
343 /* If the makefile wasn't found and it's either a makefile from
344 the `MAKEFILES' variable or an included makefile,
345 search the included makefile search path for this makefile. */
346 if (ebuf.fp == 0 && (flags & RM_INCLUDED) && *filename != '/')
348 register unsigned int i;
349 for (i = 0; include_directories[i] != 0; ++i)
351 char *name = concat (include_directories[i], "/", filename);
352 ebuf.fp = fopen (name, "r");
353 if (ebuf.fp == 0)
354 free (name);
355 else
357 filename = name;
358 break;
363 /* Add FILENAME to the chain of read makefiles. */
364 deps = (struct dep *) xmalloc (sizeof (struct dep));
365 deps->next = read_makefiles;
366 read_makefiles = deps;
367 deps->name = 0;
368 deps->file = lookup_file (filename);
369 if (deps->file == 0)
370 deps->file = enter_file (xstrdup (filename));
371 if (filename != ebuf.floc.filenm)
372 free (filename);
373 filename = deps->file->name;
374 deps->changed = flags;
375 deps->ignore_mtime = 0;
376 if (flags & RM_DONTCARE)
377 deps->file->dontcare = 1;
379 /* If the makefile can't be found at all, give up entirely. */
381 if (ebuf.fp == 0)
383 /* If we did some searching, errno has the error from the last
384 attempt, rather from FILENAME itself. Restore it in case the
385 caller wants to use it in a message. */
386 errno = makefile_errno;
387 return 0;
390 /* Add this makefile to the list. */
391 do_variable_definition (&ebuf.floc, "MAKEFILE_LIST", filename, o_file,
392 f_append, 0);
394 /* Evaluate the makefile */
396 ebuf.size = 200;
397 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = xmalloc (ebuf.size);
399 curfile = reading_file;
400 reading_file = &ebuf.floc;
402 r = eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
404 reading_file = curfile;
406 fclose (ebuf.fp);
408 free (ebuf.bufstart);
409 return r;
413 eval_buffer (char *buffer)
415 struct ebuffer ebuf;
416 struct conditionals *saved;
417 struct conditionals new;
418 const struct floc *curfile;
419 int r;
421 /* Evaluate the buffer */
423 ebuf.size = strlen (buffer);
424 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = buffer;
425 ebuf.fp = NULL;
427 ebuf.floc = *reading_file;
429 curfile = reading_file;
430 reading_file = &ebuf.floc;
432 saved = install_conditionals (&new);
434 r = eval (&ebuf, 1);
436 restore_conditionals (saved);
438 reading_file = curfile;
440 return r;
444 /* Read file FILENAME as a makefile and add its contents to the data base.
446 SET_DEFAULT is true if we are allowed to set the default goal. */
449 static int
450 eval (struct ebuffer *ebuf, int set_default)
452 char *collapsed = 0;
453 unsigned int collapsed_length = 0;
454 unsigned int commands_len = 200;
455 char *commands;
456 unsigned int commands_idx = 0;
457 unsigned int cmds_started, tgts_started;
458 int ignoring = 0, in_ignored_define = 0;
459 int no_targets = 0; /* Set when reading a rule without targets. */
460 int have_sysv_atvar = 0;
461 struct nameseq *filenames = 0;
462 struct dep *deps = 0;
463 long nlines = 0;
464 int two_colon = 0;
465 char *pattern = 0, *pattern_percent;
466 struct floc *fstart;
467 struct floc fi;
469 #define record_waiting_files() \
470 do \
472 if (filenames != 0) \
474 fi.lineno = tgts_started; \
475 record_files (filenames, pattern, pattern_percent, deps, \
476 cmds_started, commands, commands_idx, two_colon, \
477 have_sysv_atvar, &fi, set_default); \
479 filenames = 0; \
480 commands_idx = 0; \
481 no_targets = 0; \
482 if (pattern) { free(pattern); pattern = 0; } \
483 } while (0)
485 pattern_percent = 0;
486 cmds_started = tgts_started = 1;
488 fstart = &ebuf->floc;
489 fi.filenm = ebuf->floc.filenm;
491 /* Loop over lines in the file.
492 The strategy is to accumulate target names in FILENAMES, dependencies
493 in DEPS and commands in COMMANDS. These are used to define a rule
494 when the start of the next rule (or eof) is encountered.
496 When you see a "continue" in the loop below, that means we are moving on
497 to the next line _without_ ending any rule that we happen to be working
498 with at the moment. If you see a "goto rule_complete", then the
499 statement we just parsed also finishes the previous rule. */
501 commands = xmalloc (200);
503 while (1)
505 unsigned int linelen;
506 char *line;
507 int len;
508 char *p;
509 char *p2;
511 /* Grab the next line to be evaluated */
512 ebuf->floc.lineno += nlines;
513 nlines = readline (ebuf);
515 /* If there is nothing left to eval, we're done. */
516 if (nlines < 0)
517 break;
519 /* If this line is empty, skip it. */
520 line = ebuf->buffer;
521 if (line[0] == '\0')
522 continue;
524 linelen = strlen (line);
526 /* Check for a shell command line first.
527 If it is not one, we can stop treating tab specially. */
528 if (line[0] == '\t')
530 if (no_targets)
531 /* Ignore the commands in a rule with no targets. */
532 continue;
534 /* If there is no preceding rule line, don't treat this line
535 as a command, even though it begins with a tab character.
536 SunOS 4 make appears to behave this way. */
538 if (filenames != 0)
540 if (ignoring)
541 /* Yep, this is a shell command, and we don't care. */
542 continue;
544 /* Append this command line to the line being accumulated. */
545 if (commands_idx == 0)
546 cmds_started = ebuf->floc.lineno;
548 if (linelen + 1 + commands_idx > commands_len)
550 commands_len = (linelen + 1 + commands_idx) * 2;
551 commands = xrealloc (commands, commands_len);
553 bcopy (line, &commands[commands_idx], linelen);
554 commands_idx += linelen;
555 commands[commands_idx++] = '\n';
557 continue;
561 /* This line is not a shell command line. Don't worry about tabs.
562 Get more space if we need it; we don't need to preserve the current
563 contents of the buffer. */
565 if (collapsed_length < linelen+1)
567 collapsed_length = linelen+1;
568 if (collapsed)
569 free ((char *)collapsed);
570 collapsed = (char *) xmalloc (collapsed_length);
572 strcpy (collapsed, line);
573 /* Collapse continuation lines. */
574 collapse_continuations (collapsed);
575 remove_comments (collapsed);
577 /* Compare a word, both length and contents. */
578 #define word1eq(s) (len == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
579 p = collapsed;
580 while (isspace ((unsigned char)*p))
581 ++p;
583 if (*p == '\0')
584 /* This line is completely empty--ignore it. */
585 continue;
587 /* Find the end of the first token. Note we don't need to worry about
588 * ":" here since we compare tokens by length (so "export" will never
589 * be equal to "export:").
591 for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
593 len = p2 - p;
595 /* Find the start of the second token. If it looks like a target or
596 variable definition it can't be a preprocessor token so skip
597 them--this allows variables/targets named `ifdef', `export', etc. */
598 while (isspace ((unsigned char)*p2))
599 ++p2;
601 if ((p2[0] == ':' || p2[0] == '+' || p2[0] == '=') && p2[1] == '\0')
603 /* It can't be a preprocessor token so skip it if we're ignoring */
604 if (ignoring)
605 continue;
607 goto skip_conditionals;
610 /* We must first check for conditional and `define' directives before
611 ignoring anything, since they control what we will do with
612 following lines. */
614 if (!in_ignored_define
615 && (word1eq ("ifdef") || word1eq ("ifndef")
616 || word1eq ("ifeq") || word1eq ("ifneq")
617 || word1eq ("else") || word1eq ("endif")))
619 int i = conditional_line (p, fstart);
620 if (i < 0)
621 fatal (fstart, _("invalid syntax in conditional"));
623 ignoring = i;
624 continue;
627 if (word1eq ("endef"))
629 if (!in_ignored_define)
630 fatal (fstart, _("extraneous `endef'"));
631 in_ignored_define = 0;
632 continue;
635 if (word1eq ("define"))
637 if (ignoring)
638 in_ignored_define = 1;
639 else
641 if (*p2 == '\0')
642 fatal (fstart, _("empty variable name"));
644 /* Let the variable name be the whole rest of the line,
645 with trailing blanks stripped (comments have already been
646 removed), so it could be a complex variable/function
647 reference that might contain blanks. */
648 p = strchr (p2, '\0');
649 while (isblank ((unsigned char)p[-1]))
650 --p;
651 do_define (p2, p - p2, o_file, ebuf);
653 continue;
656 if (word1eq ("override"))
658 if (*p2 == '\0')
659 error (fstart, _("empty `override' directive"));
661 if (strneq (p2, "define", 6)
662 && (isblank ((unsigned char)p2[6]) || p2[6] == '\0'))
664 if (ignoring)
665 in_ignored_define = 1;
666 else
668 p2 = next_token (p2 + 6);
669 if (*p2 == '\0')
670 fatal (fstart, _("empty variable name"));
672 /* Let the variable name be the whole rest of the line,
673 with trailing blanks stripped (comments have already been
674 removed), so it could be a complex variable/function
675 reference that might contain blanks. */
676 p = strchr (p2, '\0');
677 while (isblank ((unsigned char)p[-1]))
678 --p;
679 do_define (p2, p - p2, o_override, ebuf);
682 else if (!ignoring
683 && !try_variable_definition (fstart, p2, o_override, 0))
684 error (fstart, _("invalid `override' directive"));
686 continue;
689 if (ignoring)
690 /* Ignore the line. We continue here so conditionals
691 can appear in the middle of a rule. */
692 continue;
694 if (word1eq ("export"))
696 /* 'export' by itself causes everything to be exported. */
697 if (*p2 == '\0')
698 export_all_variables = 1;
699 else
701 struct variable *v;
703 v = try_variable_definition (fstart, p2, o_file, 0);
704 if (v != 0)
705 v->export = v_export;
706 else
708 unsigned int len;
709 char *ap;
711 /* Expand the line so we can use indirect and constructed
712 variable names in an export command. */
713 p2 = ap = allocated_variable_expand (p2);
715 for (p = find_next_token (&p2, &len); p != 0;
716 p = find_next_token (&p2, &len))
718 v = lookup_variable (p, len);
719 if (v == 0)
720 v = define_variable_loc (p, len, "", o_file, 0,
721 fstart);
722 v->export = v_export;
725 free (ap);
728 goto rule_complete;
731 if (word1eq ("unexport"))
733 if (*p2 == '\0')
734 export_all_variables = 0;
735 else
737 unsigned int len;
738 struct variable *v;
739 char *ap;
741 /* Expand the line so we can use indirect and constructed
742 variable names in an unexport command. */
743 p2 = ap = allocated_variable_expand (p2);
745 for (p = find_next_token (&p2, &len); p != 0;
746 p = find_next_token (&p2, &len))
748 v = lookup_variable (p, len);
749 if (v == 0)
750 v = define_variable_loc (p, len, "", o_file, 0, fstart);
752 v->export = v_noexport;
755 free (ap);
757 goto rule_complete;
760 skip_conditionals:
761 if (word1eq ("vpath"))
763 char *pattern;
764 unsigned int len;
765 p2 = variable_expand (p2);
766 p = find_next_token (&p2, &len);
767 if (p != 0)
769 pattern = savestring (p, len);
770 p = find_next_token (&p2, &len);
771 /* No searchpath means remove all previous
772 selective VPATH's with the same pattern. */
774 else
775 /* No pattern means remove all previous selective VPATH's. */
776 pattern = 0;
777 construct_vpath_list (pattern, p);
778 if (pattern != 0)
779 free (pattern);
781 goto rule_complete;
784 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
786 /* We have found an `include' line specifying a nested
787 makefile to be read at this point. */
788 struct conditionals *save;
789 struct conditionals new_conditionals;
790 struct nameseq *files;
791 /* "-include" (vs "include") says no error if the file does not
792 exist. "sinclude" is an alias for this from SGI. */
793 int noerror = (p[0] != 'i');
795 p = allocated_variable_expand (p2);
796 if (*p == '\0')
798 error (fstart,
799 _("no file name for `%sinclude'"), noerror ? "-" : "");
800 continue;
803 /* Parse the list of file names. */
804 p2 = p;
805 files = multi_glob (parse_file_seq (&p2, '\0',
806 sizeof (struct nameseq),
808 sizeof (struct nameseq));
809 free (p);
811 /* Save the state of conditionals and start
812 the included makefile with a clean slate. */
813 save = install_conditionals (&new_conditionals);
815 /* Record the rules that are waiting so they will determine
816 the default goal before those in the included makefile. */
817 record_waiting_files ();
819 /* Read each included makefile. */
820 while (files != 0)
822 struct nameseq *next = files->next;
823 char *name = files->name;
824 int r;
826 free ((char *)files);
827 files = next;
829 r = eval_makefile (name, (RM_INCLUDED | RM_NO_TILDE
830 | (noerror ? RM_DONTCARE : 0)));
831 if (!r)
833 if (!noerror)
834 error (fstart, "%s: %s", name, strerror (errno));
835 free (name);
839 /* Restore conditional state. */
840 restore_conditionals (save);
842 goto rule_complete;
845 if (try_variable_definition (fstart, p, o_file, 0))
846 /* This line has been dealt with. */
847 goto rule_complete;
849 /* This line starts with a tab but was not caught above because there
850 was no preceding target, and the line might have been usable as a
851 variable definition. But now we know it is definitely lossage. */
852 if (line[0] == '\t')
853 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 int exported;
869 char *cmdleft, *semip, *lb_next;
870 unsigned int len, plen = 0;
871 char *colonp;
873 /* Record the previous rule. */
875 record_waiting_files ();
876 tgts_started = fstart->lineno;
878 /* Search the line for an unquoted ; that is not after an
879 unquoted #. */
880 cmdleft = find_char_unquote (line, ';', '#', 0);
881 if (cmdleft != 0 && *cmdleft == '#')
883 /* We found a comment before a semicolon. */
884 *cmdleft = '\0';
885 cmdleft = 0;
887 else if (cmdleft != 0)
888 /* Found one. Cut the line short there before expanding it. */
889 *(cmdleft++) = '\0';
890 semip = cmdleft;
892 collapse_continuations (line);
894 /* We can't expand the entire line, since if it's a per-target
895 variable we don't want to expand it. So, walk from the
896 beginning, expanding as we go, and looking for "interesting"
897 chars. The first word is always expandable. */
898 wtype = get_next_mword(line, NULL, &lb_next, &len);
899 switch (wtype)
901 case w_eol:
902 if (cmdleft != 0)
903 fatal(fstart, _("missing rule before commands"));
904 /* This line contained something but turned out to be nothing
905 but whitespace (a comment?). */
906 continue;
908 case w_colon:
909 case w_dcolon:
910 /* We accept and ignore rules without targets for
911 compatibility with SunOS 4 make. */
912 no_targets = 1;
913 continue;
915 default:
916 break;
919 p2 = variable_expand_string(NULL, lb_next, len);
920 while (1)
922 lb_next += len;
923 if (cmdleft == 0)
925 /* Look for a semicolon in the expanded line. */
926 cmdleft = find_char_unquote (p2, ';', 0, 0);
928 if (cmdleft != 0)
930 unsigned long p2_off = p2 - variable_buffer;
931 unsigned long cmd_off = cmdleft - variable_buffer;
932 char *pend = p2 + strlen(p2);
934 /* Append any remnants of lb, then cut the line short
935 at the semicolon. */
936 *cmdleft = '\0';
938 /* One school of thought says that you shouldn't expand
939 here, but merely copy, since now you're beyond a ";"
940 and into a command script. However, the old parser
941 expanded the whole line, so we continue that for
942 backwards-compatiblity. Also, it wouldn't be
943 entirely consistent, since we do an unconditional
944 expand below once we know we don't have a
945 target-specific variable. */
946 (void)variable_expand_string(pend, lb_next, (long)-1);
947 lb_next += strlen(lb_next);
948 p2 = variable_buffer + p2_off;
949 cmdleft = variable_buffer + cmd_off + 1;
953 colonp = find_char_unquote(p2, ':', 0, 0);
954 #ifdef HAVE_DOS_PATHS
955 /* The drive spec brain-damage strikes again... */
956 /* Note that the only separators of targets in this context
957 are whitespace and a left paren. If others are possible,
958 they should be added to the string in the call to index. */
959 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
960 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
961 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
962 colonp = find_char_unquote(colonp + 1, ':', 0, 0);
963 #endif
964 if (colonp != 0)
965 break;
967 wtype = get_next_mword(lb_next, NULL, &lb_next, &len);
968 if (wtype == w_eol)
969 break;
971 p2 += strlen(p2);
972 *(p2++) = ' ';
973 p2 = variable_expand_string(p2, lb_next, len);
974 /* We don't need to worry about cmdleft here, because if it was
975 found in the variable_buffer the entire buffer has already
976 been expanded... we'll never get here. */
979 p2 = next_token (variable_buffer);
981 /* If the word we're looking at is EOL, see if there's _anything_
982 on the line. If not, a variable expanded to nothing, so ignore
983 it. If so, we can't parse this line so punt. */
984 if (wtype == w_eol)
986 if (*p2 != '\0')
987 /* There's no need to be ivory-tower about this: check for
988 one of the most common bugs found in makefiles... */
989 fatal (fstart, _("missing separator%s"),
990 !strneq(line, " ", 8) ? ""
991 : _(" (did you mean TAB instead of 8 spaces?)"));
992 continue;
995 /* Make the colon the end-of-string so we know where to stop
996 looking for targets. */
997 *colonp = '\0';
998 filenames = multi_glob (parse_file_seq (&p2, '\0',
999 sizeof (struct nameseq),
1001 sizeof (struct nameseq));
1002 *p2 = ':';
1004 if (!filenames)
1006 /* We accept and ignore rules without targets for
1007 compatibility with SunOS 4 make. */
1008 no_targets = 1;
1009 continue;
1011 /* This should never be possible; we handled it above. */
1012 assert (*p2 != '\0');
1013 ++p2;
1015 /* Is this a one-colon or two-colon entry? */
1016 two_colon = *p2 == ':';
1017 if (two_colon)
1018 p2++;
1020 /* Test to see if it's a target-specific variable. Copy the rest
1021 of the buffer over, possibly temporarily (we'll expand it later
1022 if it's not a target-specific variable). PLEN saves the length
1023 of the unparsed section of p2, for later. */
1024 if (*lb_next != '\0')
1026 unsigned int l = p2 - variable_buffer;
1027 plen = strlen (p2);
1028 (void) variable_buffer_output (p2+plen,
1029 lb_next, strlen (lb_next)+1);
1030 p2 = variable_buffer + l;
1033 /* See if it's an "override" or "export" keyword; if so see if what
1034 comes after it looks like a variable definition. */
1036 wtype = get_next_mword (p2, NULL, &p, &len);
1038 v_origin = o_file;
1039 exported = 0;
1040 if (wtype == w_static)
1042 if (word1eq ("override"))
1044 v_origin = o_override;
1045 wtype = get_next_mword (p+len, NULL, &p, &len);
1047 else if (word1eq ("export"))
1049 exported = 1;
1050 wtype = get_next_mword (p+len, NULL, &p, &len);
1054 if (wtype != w_eol)
1055 wtype = get_next_mword (p+len, NULL, NULL, NULL);
1057 if (wtype == w_varassign)
1059 /* If there was a semicolon found, add it back, plus anything
1060 after it. */
1061 if (semip)
1063 unsigned int l = p - variable_buffer;
1064 *(--semip) = ';';
1065 variable_buffer_output (p2 + strlen (p2),
1066 semip, strlen (semip)+1);
1067 p = variable_buffer + l;
1069 record_target_var (filenames, p, v_origin, exported, fstart);
1070 filenames = 0;
1071 continue;
1074 /* This is a normal target, _not_ a target-specific variable.
1075 Unquote any = in the dependency list. */
1076 find_char_unquote (lb_next, '=', 0, 0);
1078 /* We have some targets, so don't ignore the following commands. */
1079 no_targets = 0;
1081 /* Expand the dependencies, etc. */
1082 if (*lb_next != '\0')
1084 unsigned int l = p2 - variable_buffer;
1085 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
1086 p2 = variable_buffer + l;
1088 /* Look for a semicolon in the expanded line. */
1089 if (cmdleft == 0)
1091 cmdleft = find_char_unquote (p2, ';', 0, 0);
1092 if (cmdleft != 0)
1093 *(cmdleft++) = '\0';
1097 /* Do any of the prerequisites appear to have $@ etc.? */
1098 have_sysv_atvar = 0;
1099 if (!posix_pedantic)
1100 for (p = strchr (p2, '$'); p != 0; p = strchr (p+1, '$'))
1101 if (p[1] == '@' || ((p[1] == '(' || p[1] == '{') && p[2] == '@'))
1103 have_sysv_atvar = 1;
1104 break;
1107 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
1108 p = strchr (p2, ':');
1109 while (p != 0 && p[-1] == '\\')
1111 register char *q = &p[-1];
1112 register int backslash = 0;
1113 while (*q-- == '\\')
1114 backslash = !backslash;
1115 if (backslash)
1116 p = strchr (p + 1, ':');
1117 else
1118 break;
1120 #ifdef _AMIGA
1121 /* Here, the situation is quite complicated. Let's have a look
1122 at a couple of targets:
1124 install: dev:make
1126 dev:make: make
1128 dev:make:: xyz
1130 The rule is that it's only a target, if there are TWO :'s
1131 OR a space around the :.
1133 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
1134 || isspace ((unsigned char)p[-1])))
1135 p = 0;
1136 #endif
1137 #ifdef HAVE_DOS_PATHS
1139 int check_again;
1141 do {
1142 check_again = 0;
1143 /* For DOS-style paths, skip a "C:\..." or a "C:/..." */
1144 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
1145 isalpha ((unsigned char)p[-1]) &&
1146 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1147 p = strchr (p + 1, ':');
1148 check_again = 1;
1150 } while (check_again);
1152 #endif
1153 if (p != 0)
1155 struct nameseq *target;
1156 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
1157 ++p2;
1158 if (target == 0)
1159 fatal (fstart, _("missing target pattern"));
1160 else if (target->next != 0)
1161 fatal (fstart, _("multiple target patterns"));
1162 pattern = target->name;
1163 pattern_percent = find_percent (pattern);
1164 if (pattern_percent == 0)
1165 fatal (fstart, _("target pattern contains no `%%'"));
1166 free((char *)target);
1168 else
1169 pattern = 0;
1171 /* Parse the dependencies. */
1172 deps = (struct dep *)
1173 multi_glob (parse_file_seq (&p2, '|', sizeof (struct dep), 1),
1174 sizeof (struct dep));
1175 if (*p2)
1177 /* Files that follow '|' are special prerequisites that
1178 need only exist in order to satisfy the dependency.
1179 Their modification times are irrelevant. */
1180 struct dep **deps_ptr = &deps;
1181 struct dep *d;
1182 for (deps_ptr = &deps; *deps_ptr; deps_ptr = &(*deps_ptr)->next)
1184 ++p2;
1185 *deps_ptr = (struct dep *)
1186 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
1187 sizeof (struct dep));
1188 for (d = *deps_ptr; d != 0; d = d->next)
1189 d->ignore_mtime = 1;
1192 commands_idx = 0;
1193 if (cmdleft != 0)
1195 /* Semicolon means rest of line is a command. */
1196 unsigned int len = strlen (cmdleft);
1198 cmds_started = fstart->lineno;
1200 /* Add this command line to the buffer. */
1201 if (len + 2 > commands_len)
1203 commands_len = (len + 2) * 2;
1204 commands = (char *) xrealloc (commands, commands_len);
1206 bcopy (cmdleft, commands, len);
1207 commands_idx += len;
1208 commands[commands_idx++] = '\n';
1211 continue;
1214 /* We get here except in the case that we just read a rule line.
1215 Record now the last rule we read, so following spurious
1216 commands are properly diagnosed. */
1217 rule_complete:
1218 record_waiting_files ();
1221 #undef word1eq
1223 if (conditionals->if_cmds)
1224 fatal (fstart, _("missing `endif'"));
1226 /* At eof, record the last rule. */
1227 record_waiting_files ();
1229 if (collapsed)
1230 free ((char *) collapsed);
1231 free ((char *) commands);
1233 return 1;
1237 /* Execute a `define' directive.
1238 The first line has already been read, and NAME is the name of
1239 the variable to be defined. The following lines remain to be read. */
1241 static void
1242 do_define (char *name, unsigned int namelen,
1243 enum variable_origin origin, struct ebuffer *ebuf)
1245 struct floc defstart;
1246 long nlines = 0;
1247 int nlevels = 1;
1248 unsigned int length = 100;
1249 char *definition = (char *) xmalloc (length);
1250 unsigned int idx = 0;
1251 char *p;
1253 /* Expand the variable name. */
1254 char *var = (char *) alloca (namelen + 1);
1255 bcopy (name, var, namelen);
1256 var[namelen] = '\0';
1257 var = variable_expand (var);
1259 defstart = ebuf->floc;
1261 while (1)
1263 unsigned int len;
1264 char *line;
1266 nlines = readline (ebuf);
1267 ebuf->floc.lineno += nlines;
1269 /* If there is nothing left to eval, we're done. */
1270 if (nlines < 0)
1271 break;
1273 line = ebuf->buffer;
1275 collapse_continuations (line);
1277 /* If the line doesn't begin with a tab, test to see if it introduces
1278 another define, or ends one. */
1280 /* Stop if we find an 'endef' */
1281 if (line[0] != '\t')
1283 p = next_token (line);
1284 len = strlen (p);
1286 /* If this is another 'define', increment the level count. */
1287 if ((len == 6 || (len > 6 && isblank ((unsigned char)p[6])))
1288 && strneq (p, "define", 6))
1289 ++nlevels;
1291 /* If this is an 'endef', decrement the count. If it's now 0,
1292 we've found the last one. */
1293 else if ((len == 5 || (len > 5 && isblank ((unsigned char)p[5])))
1294 && strneq (p, "endef", 5))
1296 p += 5;
1297 remove_comments (p);
1298 if (*next_token (p) != '\0')
1299 error (&ebuf->floc,
1300 _("Extraneous text after `endef' directive"));
1302 if (--nlevels == 0)
1304 /* Define the variable. */
1305 if (idx == 0)
1306 definition[0] = '\0';
1307 else
1308 definition[idx - 1] = '\0';
1310 /* Always define these variables in the global set. */
1311 define_variable_global (var, strlen (var), definition,
1312 origin, 1, &defstart);
1313 free (definition);
1314 return;
1319 /* Otherwise add this line to the variable definition. */
1320 len = strlen (line);
1321 if (idx + len + 1 > length)
1323 length = (idx + len) * 2;
1324 definition = (char *) xrealloc (definition, length + 1);
1327 bcopy (line, &definition[idx], len);
1328 idx += len;
1329 /* Separate lines with a newline. */
1330 definition[idx++] = '\n';
1333 /* No `endef'!! */
1334 fatal (&defstart, _("missing `endef', unterminated `define'"));
1336 /* NOTREACHED */
1337 return;
1340 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1341 "ifneq", "else" and "endif".
1342 LINE is the input line, with the command as its first word.
1344 FILENAME and LINENO are the filename and line number in the
1345 current makefile. They are used for error messages.
1347 Value is -1 if the line is invalid,
1348 0 if following text should be interpreted,
1349 1 if following text should be ignored. */
1351 static int
1352 conditional_line (char *line, const struct floc *flocp)
1354 int notdef;
1355 char *cmdname;
1356 register unsigned int i;
1358 if (*line == 'i')
1360 /* It's an "if..." command. */
1361 notdef = line[2] == 'n';
1362 if (notdef)
1364 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
1365 line += cmdname[3] == 'd' ? 7 : 6;
1367 else
1369 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
1370 line += cmdname[2] == 'd' ? 6 : 5;
1373 else
1375 /* It's an "else" or "endif" command. */
1376 notdef = line[1] == 'n';
1377 cmdname = notdef ? "endif" : "else";
1378 line += notdef ? 5 : 4;
1381 line = next_token (line);
1383 if (*cmdname == 'e')
1385 if (*line != '\0')
1386 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1387 /* "Else" or "endif". */
1388 if (conditionals->if_cmds == 0)
1389 fatal (flocp, _("extraneous `%s'"), cmdname);
1390 /* NOTDEF indicates an `endif' command. */
1391 if (notdef)
1392 --conditionals->if_cmds;
1393 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1394 fatal (flocp, _("only one `else' per conditional"));
1395 else
1397 /* Toggle the state of ignorance. */
1398 conditionals->ignoring[conditionals->if_cmds - 1]
1399 = !conditionals->ignoring[conditionals->if_cmds - 1];
1400 /* Record that we have seen an `else' in this conditional.
1401 A second `else' will be erroneous. */
1402 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1404 for (i = 0; i < conditionals->if_cmds; ++i)
1405 if (conditionals->ignoring[i])
1406 return 1;
1407 return 0;
1410 if (conditionals->allocated == 0)
1412 conditionals->allocated = 5;
1413 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1414 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1417 ++conditionals->if_cmds;
1418 if (conditionals->if_cmds > conditionals->allocated)
1420 conditionals->allocated += 5;
1421 conditionals->ignoring = (char *)
1422 xrealloc (conditionals->ignoring, conditionals->allocated);
1423 conditionals->seen_else = (char *)
1424 xrealloc (conditionals->seen_else, conditionals->allocated);
1427 /* Record that we have seen an `if...' but no `else' so far. */
1428 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1430 /* Search through the stack to see if we're already ignoring. */
1431 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1432 if (conditionals->ignoring[i])
1434 /* We are already ignoring, so just push a level
1435 to match the next "else" or "endif", and keep ignoring.
1436 We don't want to expand variables in the condition. */
1437 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1438 return 1;
1441 if (cmdname[notdef ? 3 : 2] == 'd')
1443 /* "Ifdef" or "ifndef". */
1444 char *var;
1445 struct variable *v;
1446 register char *p;
1448 /* Expand the thing we're looking up, so we can use indirect and
1449 constructed variable names. */
1450 var = allocated_variable_expand (line);
1452 /* Make sure there's only one variable name to test. */
1453 p = end_of_token (var);
1454 i = p - var;
1455 p = next_token (p);
1456 if (*p != '\0')
1457 return -1;
1459 var[i] = '\0';
1460 v = lookup_variable (var, strlen (var));
1461 conditionals->ignoring[conditionals->if_cmds - 1]
1462 = (v != 0 && *v->value != '\0') == notdef;
1464 free (var);
1466 else
1468 /* "Ifeq" or "ifneq". */
1469 char *s1, *s2;
1470 unsigned int len;
1471 char termin = *line == '(' ? ',' : *line;
1473 if (termin != ',' && termin != '"' && termin != '\'')
1474 return -1;
1476 s1 = ++line;
1477 /* Find the end of the first string. */
1478 if (termin == ',')
1480 register int count = 0;
1481 for (; *line != '\0'; ++line)
1482 if (*line == '(')
1483 ++count;
1484 else if (*line == ')')
1485 --count;
1486 else if (*line == ',' && count <= 0)
1487 break;
1489 else
1490 while (*line != '\0' && *line != termin)
1491 ++line;
1493 if (*line == '\0')
1494 return -1;
1496 if (termin == ',')
1498 /* Strip blanks after the first string. */
1499 char *p = line++;
1500 while (isblank ((unsigned char)p[-1]))
1501 --p;
1502 *p = '\0';
1504 else
1505 *line++ = '\0';
1507 s2 = variable_expand (s1);
1508 /* We must allocate a new copy of the expanded string because
1509 variable_expand re-uses the same buffer. */
1510 len = strlen (s2);
1511 s1 = (char *) alloca (len + 1);
1512 bcopy (s2, s1, len + 1);
1514 if (termin != ',')
1515 /* Find the start of the second string. */
1516 line = next_token (line);
1518 termin = termin == ',' ? ')' : *line;
1519 if (termin != ')' && termin != '"' && termin != '\'')
1520 return -1;
1522 /* Find the end of the second string. */
1523 if (termin == ')')
1525 register int count = 0;
1526 s2 = next_token (line);
1527 for (line = s2; *line != '\0'; ++line)
1529 if (*line == '(')
1530 ++count;
1531 else if (*line == ')')
1533 if (count <= 0)
1534 break;
1535 else
1536 --count;
1540 else
1542 ++line;
1543 s2 = line;
1544 while (*line != '\0' && *line != termin)
1545 ++line;
1548 if (*line == '\0')
1549 return -1;
1551 *line = '\0';
1552 line = next_token (++line);
1553 if (*line != '\0')
1554 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1556 s2 = variable_expand (s2);
1557 conditionals->ignoring[conditionals->if_cmds - 1]
1558 = streq (s1, s2) == notdef;
1561 /* Search through the stack to see if we're ignoring. */
1562 for (i = 0; i < conditionals->if_cmds; ++i)
1563 if (conditionals->ignoring[i])
1564 return 1;
1565 return 0;
1568 /* Remove duplicate dependencies in CHAIN. */
1570 static unsigned long
1571 dep_hash_1 (const void *key)
1573 return_STRING_HASH_1 (dep_name ((struct dep const *) key));
1576 static unsigned long
1577 dep_hash_2 (const void *key)
1579 return_STRING_HASH_2 (dep_name ((struct dep const *) key));
1582 static int
1583 dep_hash_cmp (const void *x, const void *y)
1585 struct dep *dx = (struct dep *) x;
1586 struct dep *dy = (struct dep *) y;
1587 int cmp = strcmp (dep_name (dx), dep_name (dy));
1589 /* If the names are the same but ignore_mtimes are not equal, one of these
1590 is an order-only prerequisite and one isn't. That means that we should
1591 remove the one that isn't and keep the one that is. */
1593 if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
1594 dx->ignore_mtime = dy->ignore_mtime = 0;
1596 return cmp;
1600 void
1601 uniquize_deps (struct dep *chain)
1603 struct hash_table deps;
1604 register struct dep **depp;
1606 hash_init (&deps, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
1608 /* Make sure that no dependencies are repeated. This does not
1609 really matter for the purpose of updating targets, but it
1610 might make some names be listed twice for $^ and $?. */
1612 depp = &chain;
1613 while (*depp)
1615 struct dep *dep = *depp;
1616 struct dep **dep_slot = (struct dep **) hash_find_slot (&deps, dep);
1617 if (HASH_VACANT (*dep_slot))
1619 hash_insert_at (&deps, dep, dep_slot);
1620 depp = &dep->next;
1622 else
1624 /* Don't bother freeing duplicates.
1625 It's dangerous and little benefit accrues. */
1626 *depp = dep->next;
1630 hash_free (&deps, 0);
1633 /* Record target-specific variable values for files FILENAMES.
1634 TWO_COLON is nonzero if a double colon was used.
1636 The links of FILENAMES are freed, and so are any names in it
1637 that are not incorporated into other data structures.
1639 If the target is a pattern, add the variable to the pattern-specific
1640 variable value list. */
1642 static void
1643 record_target_var (struct nameseq *filenames, char *defn,
1644 enum variable_origin origin, int exported,
1645 const struct floc *flocp)
1647 struct nameseq *nextf;
1648 struct variable_set_list *global;
1650 global = current_variable_set_list;
1652 /* If the variable is an append version, store that but treat it as a
1653 normal recursive variable. */
1655 for (; filenames != 0; filenames = nextf)
1657 struct variable *v;
1658 register char *name = filenames->name;
1659 char *fname;
1660 char *percent;
1661 struct pattern_var *p;
1663 nextf = filenames->next;
1664 free ((char *) filenames);
1666 /* If it's a pattern target, then add it to the pattern-specific
1667 variable list. */
1668 percent = find_percent (name);
1669 if (percent)
1671 /* Get a reference for this pattern-specific variable struct. */
1672 p = create_pattern_var (name, percent);
1673 p->variable.fileinfo = *flocp;
1674 /* I don't think this can fail since we already determined it was a
1675 variable definition. */
1676 v = parse_variable_definition (&p->variable, defn);
1677 assert (v != 0);
1678 v->value = xstrdup (v->value);
1679 fname = p->target;
1681 else
1683 struct file *f;
1685 /* Get a file reference for this file, and initialize it.
1686 We don't want to just call enter_file() because that allocates a
1687 new entry if the file is a double-colon, which we don't want in
1688 this situation. */
1689 f = lookup_file (name);
1690 if (!f)
1691 f = enter_file (name);
1692 else if (f->double_colon)
1693 f = f->double_colon;
1695 initialize_file_variables (f, 1);
1696 fname = f->name;
1698 current_variable_set_list = f->variables;
1699 v = try_variable_definition (flocp, defn, origin, 1);
1700 if (!v)
1701 error (flocp, _("Malformed target-specific variable definition"));
1702 current_variable_set_list = global;
1705 /* Set up the variable to be *-specific. */
1706 v->origin = origin;
1707 v->per_target = 1;
1708 if (exported)
1709 v->export = v_export;
1711 /* If it's not an override, check to see if there was a command-line
1712 setting. If so, reset the value. */
1713 if (origin != o_override)
1715 struct variable *gv;
1716 int len = strlen(v->name);
1718 gv = lookup_variable (v->name, len);
1719 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1721 if (v->value != 0)
1722 free (v->value);
1723 v->value = xstrdup (gv->value);
1724 v->origin = gv->origin;
1725 v->recursive = gv->recursive;
1726 v->append = 0;
1730 /* Free name if not needed further. */
1731 if (name != fname && (name < fname || name > fname + strlen (fname)))
1732 free (name);
1736 /* Record a description line for files FILENAMES,
1737 with dependencies DEPS, commands to execute described
1738 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1739 TWO_COLON is nonzero if a double colon was used.
1740 If not nil, PATTERN is the `%' pattern to make this
1741 a static pattern rule, and PATTERN_PERCENT is a pointer
1742 to the `%' within it.
1744 The links of FILENAMES are freed, and so are any names in it
1745 that are not incorporated into other data structures. */
1747 static void
1748 record_files (struct nameseq *filenames, char *pattern, char *pattern_percent,
1749 struct dep *deps, unsigned int cmds_started, char *commands,
1750 unsigned int commands_idx, int two_colon,
1751 int have_sysv_atvar, const struct floc *flocp, int set_default)
1753 struct nameseq *nextf;
1754 int implicit = 0;
1755 unsigned int max_targets = 0, target_idx = 0;
1756 char **targets = 0, **target_percents = 0;
1757 struct commands *cmds;
1759 if (commands_idx > 0)
1761 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1762 cmds->fileinfo.filenm = flocp->filenm;
1763 cmds->fileinfo.lineno = cmds_started;
1764 cmds->commands = savestring (commands, commands_idx);
1765 cmds->command_lines = 0;
1767 else
1768 cmds = 0;
1770 for (; filenames != 0; filenames = nextf)
1772 char *name = filenames->name;
1773 struct file *f;
1774 struct dep *d;
1775 struct dep *this;
1776 char *implicit_percent;
1778 nextf = filenames->next;
1779 free (filenames);
1781 /* Check for .POSIX. We used to do this in snap_deps() but that's not
1782 good enough: it doesn't happen until after the makefile is read,
1783 which means we cannot use its value during parsing. */
1785 if (streq (name, ".POSIX"))
1786 posix_pedantic = 1;
1788 implicit_percent = find_percent (name);
1789 implicit |= implicit_percent != 0;
1791 if (implicit && pattern != 0)
1792 fatal (flocp, _("mixed implicit and static pattern rules"));
1794 if (implicit && implicit_percent == 0)
1795 fatal (flocp, _("mixed implicit and normal rules"));
1797 if (implicit)
1799 if (targets == 0)
1801 max_targets = 5;
1802 targets = (char **) xmalloc (5 * sizeof (char *));
1803 target_percents = (char **) xmalloc (5 * sizeof (char *));
1804 target_idx = 0;
1806 else if (target_idx == max_targets - 1)
1808 max_targets += 5;
1809 targets = (char **) xrealloc ((char *) targets,
1810 max_targets * sizeof (char *));
1811 target_percents
1812 = (char **) xrealloc ((char *) target_percents,
1813 max_targets * sizeof (char *));
1815 targets[target_idx] = name;
1816 target_percents[target_idx] = implicit_percent;
1817 ++target_idx;
1818 continue;
1821 /* If there are multiple filenames, copy the chain DEPS
1822 for all but the last one. It is not safe for the same deps
1823 to go in more than one place in the data base. */
1824 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1826 if (pattern != 0)
1828 /* If this is an extended static rule:
1829 `targets: target%pattern: dep%pattern; cmds',
1830 translate each dependency pattern into a plain filename
1831 using the target pattern and this target's name. */
1832 if (!pattern_matches (pattern, pattern_percent, name))
1834 /* Give a warning if the rule is meaningless. */
1835 error (flocp,
1836 _("target `%s' doesn't match the target pattern"), name);
1837 this = 0;
1839 else
1841 /* We use patsubst_expand to do the work of translating
1842 the target pattern, the target's name and the dependencies'
1843 patterns into plain dependency names. */
1844 char *buffer = variable_expand ("");
1846 for (d = this; d != 0; d = d->next)
1848 char *o;
1849 char *percent = find_percent (d->name);
1850 if (percent == 0)
1851 continue;
1852 o = patsubst_expand (buffer, name, pattern, d->name,
1853 pattern_percent+1, percent+1);
1854 /* If the name expanded to the empty string, that's
1855 illegal. */
1856 if (o == buffer)
1857 fatal (flocp,
1858 _("target `%s' leaves prerequisite pattern empty"),
1859 name);
1860 free (d->name);
1861 d->name = savestring (buffer, o - buffer);
1866 /* If at least one of the dependencies uses $$@ etc. deal with that.
1867 It would be very nice and very simple to just expand everything, but
1868 it would break a lot of backward compatibility. Maybe that's OK
1869 since we're just emulating a SysV function, and if we do that then
1870 why not emulate it completely (that's what SysV make does: it
1871 re-expands the entire prerequisite list, all the time, with $@
1872 etc. in scope). But, it would be a pain indeed to document this
1873 ("iff you use $$@, your prerequisite lists is expanded twice...")
1874 Ouch. Maybe better to make the code more complex. */
1876 if (have_sysv_atvar)
1878 char *p;
1879 int tlen = strlen (name);
1880 char *fnp = strrchr (name, '/');
1881 int dlen;
1882 int flen;
1884 if (fnp)
1886 dlen = fnp - name;
1887 ++fnp;
1888 flen = strlen (fnp);
1890 else
1892 dlen = 0;
1893 fnp = name;
1894 flen = tlen;
1898 for (d = this; d != 0; d = d->next)
1899 for (p = strchr (d->name, '$'); p != 0; p = strchr (p+1, '$'))
1901 char *s = p;
1902 char *at;
1903 int atlen;
1905 /* If it's '$@', '$(@', or '${@', it's escaped */
1906 if ((++p)[0] == '$'
1907 && (p[1] == '@'
1908 || ((p[1] == '(' || p[1] == '{') && p[2] == '@')))
1910 bcopy (p, s, strlen (p)+1);
1911 continue;
1914 /* Maybe found one. We like anything of any form matching @,
1915 [({]@[}):], or [({]@[DF][}):]. */
1917 if (! (p[0] == '@'
1918 || ((p[0] == '(' || p[0] == '{') && (++p)[0] == '@'
1919 && (((++p)[0] == ')' || p[0] == '}' || p[0] == ':')
1920 || ((p[1] == ')' || p[1] == '}' || p[1] == ':')
1921 && (p[0] == 'D' || p[0] == 'F'))))))
1922 continue;
1924 /* Found one. Compute the length and string ptr. Move p
1925 past the variable reference. */
1926 switch (p[0])
1928 case 'D':
1929 atlen = dlen;
1930 at = name;
1931 p += 2;
1932 break;
1934 case 'F':
1935 atlen = flen;
1936 at = fnp;
1937 p += 2;
1938 break;
1940 default:
1941 atlen = tlen;
1942 at = name;
1943 ++p;
1944 break;
1947 /* Get more space. */
1949 int soff = s - d->name;
1950 int poff = p - d->name;
1951 d->name = (char *) xrealloc (d->name,
1952 strlen (d->name) + atlen + 1);
1953 s = d->name + soff;
1954 p = d->name + poff;
1957 /* Copy the string over. */
1958 bcopy(p, s+atlen, strlen (p)+1);
1959 bcopy(at, s, atlen);
1960 p = s + atlen - 1;
1964 if (!two_colon)
1966 /* Single-colon. Combine these dependencies
1967 with others in file's existing record, if any. */
1968 f = enter_file (name);
1970 if (f->double_colon)
1971 fatal (flocp,
1972 _("target file `%s' has both : and :: entries"), f->name);
1974 /* If CMDS == F->CMDS, this target was listed in this rule
1975 more than once. Just give a warning since this is harmless. */
1976 if (cmds != 0 && cmds == f->cmds)
1977 error (flocp,
1978 _("target `%s' given more than once in the same rule."),
1979 f->name);
1981 /* Check for two single-colon entries both with commands.
1982 Check is_target so that we don't lose on files such as .c.o
1983 whose commands were preinitialized. */
1984 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1986 error (&cmds->fileinfo,
1987 _("warning: overriding commands for target `%s'"),
1988 f->name);
1989 error (&f->cmds->fileinfo,
1990 _("warning: ignoring old commands for target `%s'"),
1991 f->name);
1994 f->is_target = 1;
1996 /* Defining .DEFAULT with no deps or cmds clears it. */
1997 if (f == default_file && this == 0 && cmds == 0)
1998 f->cmds = 0;
1999 if (cmds != 0)
2000 f->cmds = cmds;
2001 /* Defining .SUFFIXES with no dependencies
2002 clears out the list of suffixes. */
2003 if (f == suffix_file && this == 0)
2005 d = f->deps;
2006 while (d != 0)
2008 struct dep *nextd = d->next;
2009 free (d->name);
2010 free ((char *)d);
2011 d = nextd;
2013 f->deps = 0;
2015 else if (f->deps != 0)
2017 /* Add the file's old deps and the new ones in THIS together. */
2019 struct dep *firstdeps, *moredeps;
2020 if (cmds != 0)
2022 /* This is the rule with commands, so put its deps first.
2023 The rationale behind this is that $< expands to the
2024 first dep in the chain, and commands use $< expecting
2025 to get the dep that rule specifies. */
2026 firstdeps = this;
2027 moredeps = f->deps;
2029 else
2031 /* Append the new deps to the old ones. */
2032 firstdeps = f->deps;
2033 moredeps = this;
2036 if (firstdeps == 0)
2037 firstdeps = moredeps;
2038 else
2040 d = firstdeps;
2041 while (d->next != 0)
2042 d = d->next;
2043 d->next = moredeps;
2046 f->deps = firstdeps;
2048 else
2049 f->deps = this;
2051 /* If this is a static pattern rule, set the file's stem to
2052 the part of its name that matched the `%' in the pattern,
2053 so you can use $* in the commands. */
2054 if (pattern != 0)
2056 static char *percent = "%";
2057 char *buffer = variable_expand ("");
2058 char *o = patsubst_expand (buffer, name, pattern, percent,
2059 pattern_percent+1, percent+1);
2060 f->stem = savestring (buffer, o - buffer);
2063 else
2065 /* Double-colon. Make a new record
2066 even if the file already has one. */
2067 f = lookup_file (name);
2068 /* Check for both : and :: rules. Check is_target so
2069 we don't lose on default suffix rules or makefiles. */
2070 if (f != 0 && f->is_target && !f->double_colon)
2071 fatal (flocp,
2072 _("target file `%s' has both : and :: entries"), f->name);
2073 f = enter_file (name);
2074 /* If there was an existing entry and it was a double-colon
2075 entry, enter_file will have returned a new one, making it the
2076 prev pointer of the old one, and setting its double_colon
2077 pointer to the first one. */
2078 if (f->double_colon == 0)
2079 /* This is the first entry for this name, so we must
2080 set its double_colon pointer to itself. */
2081 f->double_colon = f;
2082 f->is_target = 1;
2083 f->deps = this;
2084 f->cmds = cmds;
2087 /* Free name if not needed further. */
2088 if (f != 0 && name != f->name
2089 && (name < f->name || name > f->name + strlen (f->name)))
2091 free (name);
2092 name = f->name;
2095 /* See if this is first target seen whose name does
2096 not start with a `.', unless it contains a slash. */
2097 if (default_goal_file == 0 && set_default
2098 && (*name != '.' || strchr (name, '/') != 0
2099 #ifdef HAVE_DOS_PATHS
2100 || strchr (name, '\\') != 0
2101 #endif
2104 int reject = 0;
2106 /* If this file is a suffix, don't
2107 let it be the default goal file. */
2109 for (d = suffix_file->deps; d != 0; d = d->next)
2111 register struct dep *d2;
2112 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
2114 reject = 1;
2115 break;
2117 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
2119 register unsigned int len = strlen (dep_name (d2));
2120 if (!strneq (name, dep_name (d2), len))
2121 continue;
2122 if (streq (name + len, dep_name (d)))
2124 reject = 1;
2125 break;
2128 if (reject)
2129 break;
2132 if (!reject)
2133 default_goal_file = f;
2137 if (implicit)
2139 targets[target_idx] = 0;
2140 target_percents[target_idx] = 0;
2141 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
2142 free ((char *) target_percents);
2146 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2147 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2148 Quoting backslashes are removed from STRING by compacting it into
2149 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2150 one, or nil if there are none. */
2152 char *
2153 find_char_unquote (char *string, int stop1, int stop2, int blank)
2155 unsigned int string_len = 0;
2156 register char *p = string;
2158 while (1)
2160 if (stop2 && blank)
2161 while (*p != '\0' && *p != stop1 && *p != stop2
2162 && ! isblank ((unsigned char) *p))
2163 ++p;
2164 else if (stop2)
2165 while (*p != '\0' && *p != stop1 && *p != stop2)
2166 ++p;
2167 else if (blank)
2168 while (*p != '\0' && *p != stop1
2169 && ! isblank ((unsigned char) *p))
2170 ++p;
2171 else
2172 while (*p != '\0' && *p != stop1)
2173 ++p;
2175 if (*p == '\0')
2176 break;
2178 if (p > string && p[-1] == '\\')
2180 /* Search for more backslashes. */
2181 register int i = -2;
2182 while (&p[i] >= string && p[i] == '\\')
2183 --i;
2184 ++i;
2185 /* Only compute the length if really needed. */
2186 if (string_len == 0)
2187 string_len = strlen (string);
2188 /* The number of backslashes is now -I.
2189 Copy P over itself to swallow half of them. */
2190 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
2191 p += i / 2;
2192 if (i % 2 == 0)
2193 /* All the backslashes quoted each other; the STOPCHAR was
2194 unquoted. */
2195 return p;
2197 /* The STOPCHAR was quoted by a backslash. Look for another. */
2199 else
2200 /* No backslash in sight. */
2201 return p;
2204 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2205 return 0;
2208 /* Search PATTERN for an unquoted %. */
2210 char *
2211 find_percent (char *pattern)
2213 return find_char_unquote (pattern, '%', 0, 0);
2216 /* Parse a string into a sequence of filenames represented as a
2217 chain of struct nameseq's in reverse order and return that chain.
2219 The string is passed as STRINGP, the address of a string pointer.
2220 The string pointer is updated to point at the first character
2221 not parsed, which either is a null char or equals STOPCHAR.
2223 SIZE is how big to construct chain elements.
2224 This is useful if we want them actually to be other structures
2225 that have room for additional info.
2227 If STRIP is nonzero, strip `./'s off the beginning. */
2229 struct nameseq *
2230 parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
2232 register struct nameseq *new = 0;
2233 register struct nameseq *new1, *lastnew1;
2234 register char *p = *stringp;
2235 char *q;
2236 char *name;
2238 #ifdef VMS
2239 # define VMS_COMMA ','
2240 #else
2241 # define VMS_COMMA 0
2242 #endif
2244 while (1)
2246 /* Skip whitespace; see if any more names are left. */
2247 p = next_token (p);
2248 if (*p == '\0')
2249 break;
2250 if (*p == stopchar)
2251 break;
2253 /* Yes, find end of next name. */
2254 q = p;
2255 p = find_char_unquote (q, stopchar, VMS_COMMA, 1);
2256 #ifdef VMS
2257 /* convert comma separated list to space separated */
2258 if (p && *p == ',')
2259 *p =' ';
2260 #endif
2261 #ifdef _AMIGA
2262 if (stopchar == ':' && p && *p == ':'
2263 && !(isspace ((unsigned char)p[1]) || !p[1]
2264 || isspace ((unsigned char)p[-1])))
2266 p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1);
2268 #endif
2269 #ifdef HAVE_DOS_PATHS
2270 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
2271 first colon which isn't followed by a slash or a backslash.
2272 Note that tokens separated by spaces should be treated as separate
2273 tokens since make doesn't allow path names with spaces */
2274 if (stopchar == ':')
2275 while (p != 0 && !isspace ((unsigned char)*p) &&
2276 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
2277 p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1);
2278 #endif
2279 if (p == 0)
2280 p = q + strlen (q);
2282 if (strip)
2283 #ifdef VMS
2284 /* Skip leading `[]'s. */
2285 while (p - q > 2 && q[0] == '[' && q[1] == ']')
2286 #else
2287 /* Skip leading `./'s. */
2288 while (p - q > 2 && q[0] == '.' && q[1] == '/')
2289 #endif
2291 q += 2; /* Skip "./". */
2292 while (q < p && *q == '/')
2293 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
2294 ++q;
2297 /* Extract the filename just found, and skip it. */
2299 if (q == p)
2300 /* ".///" was stripped to "". */
2301 #ifdef VMS
2302 continue;
2303 #else
2304 #ifdef _AMIGA
2305 name = savestring ("", 0);
2306 #else
2307 name = savestring ("./", 2);
2308 #endif
2309 #endif
2310 else
2311 #ifdef VMS
2312 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
2313 * to remove this '\' before we can use the filename.
2314 * Savestring called because q may be read-only string constant.
2317 char *qbase = xstrdup (q);
2318 char *pbase = qbase + (p-q);
2319 char *q1 = qbase;
2320 char *q2 = q1;
2321 char *p1 = pbase;
2323 while (q1 != pbase)
2325 if (*q1 == '\\' && *(q1+1) == ':')
2327 q1++;
2328 p1--;
2330 *q2++ = *q1++;
2332 name = savestring (qbase, p1 - qbase);
2333 free (qbase);
2335 #else
2336 name = savestring (q, p - q);
2337 #endif
2339 /* Add it to the front of the chain. */
2340 new1 = (struct nameseq *) xmalloc (size);
2341 new1->name = name;
2342 new1->next = new;
2343 new = new1;
2346 #ifndef NO_ARCHIVES
2348 /* Look for multi-word archive references.
2349 They are indicated by a elt ending with an unmatched `)' and
2350 an elt further down the chain (i.e., previous in the file list)
2351 with an unmatched `(' (e.g., "lib(mem"). */
2353 new1 = new;
2354 lastnew1 = 0;
2355 while (new1 != 0)
2356 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2357 && new1->name[strlen (new1->name) - 1] == ')'
2358 && strchr (new1->name, '(') == 0)
2360 /* NEW1 ends with a `)' but does not contain a `('.
2361 Look back for an elt with an opening `(' but no closing `)'. */
2363 struct nameseq *n = new1->next, *lastn = new1;
2364 char *paren = 0;
2365 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2367 lastn = n;
2368 n = n->next;
2370 if (n != 0
2371 /* Ignore something starting with `(', as that cannot actually
2372 be an archive-member reference (and treating it as such
2373 results in an empty file name, which causes much lossage). */
2374 && n->name[0] != '(')
2376 /* N is the first element in the archive group.
2377 Its name looks like "lib(mem" (with no closing `)'). */
2379 char *libname;
2381 /* Copy "lib(" into LIBNAME. */
2382 ++paren;
2383 libname = (char *) alloca (paren - n->name + 1);
2384 bcopy (n->name, libname, paren - n->name);
2385 libname[paren - n->name] = '\0';
2387 if (*paren == '\0')
2389 /* N was just "lib(", part of something like "lib( a b)".
2390 Edit it out of the chain and free its storage. */
2391 lastn->next = n->next;
2392 free (n->name);
2393 free ((char *) n);
2394 /* LASTN->next is the new stopping elt for the loop below. */
2395 n = lastn->next;
2397 else
2399 /* Replace N's name with the full archive reference. */
2400 name = concat (libname, paren, ")");
2401 free (n->name);
2402 n->name = name;
2405 if (new1->name[1] == '\0')
2407 /* NEW1 is just ")", part of something like "lib(a b )".
2408 Omit it from the chain and free its storage. */
2409 if (lastnew1 == 0)
2410 new = new1->next;
2411 else
2412 lastnew1->next = new1->next;
2413 lastn = new1;
2414 new1 = new1->next;
2415 free (lastn->name);
2416 free ((char *) lastn);
2418 else
2420 /* Replace also NEW1->name, which already has closing `)'. */
2421 name = concat (libname, new1->name, "");
2422 free (new1->name);
2423 new1->name = name;
2424 new1 = new1->next;
2427 /* Trace back from NEW1 (the end of the list) until N
2428 (the beginning of the list), rewriting each name
2429 with the full archive reference. */
2431 while (new1 != n)
2433 name = concat (libname, new1->name, ")");
2434 free (new1->name);
2435 new1->name = name;
2436 lastnew1 = new1;
2437 new1 = new1->next;
2440 else
2442 /* No frobnication happening. Just step down the list. */
2443 lastnew1 = new1;
2444 new1 = new1->next;
2447 else
2449 lastnew1 = new1;
2450 new1 = new1->next;
2453 #endif
2455 *stringp = p;
2456 return new;
2459 /* Find the next line of text in an eval buffer, combining continuation lines
2460 into one line.
2461 Return the number of actual lines read (> 1 if continuation lines).
2462 Returns -1 if there's nothing left in the buffer.
2464 After this function, ebuf->buffer points to the first character of the
2465 line we just found.
2468 /* Read a line of text from a STRING.
2469 Since we aren't really reading from a file, don't bother with linenumbers.
2472 static unsigned long
2473 readstring (struct ebuffer *ebuf)
2475 char *eol;
2477 /* If there is nothing left in this buffer, return 0. */
2478 if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
2479 return -1;
2481 /* Set up a new starting point for the buffer, and find the end of the
2482 next logical line (taking into account backslash/newline pairs). */
2484 eol = ebuf->buffer = ebuf->bufnext;
2486 while (1)
2488 int backslash = 0;
2489 char *bol = eol;
2490 char *p;
2492 /* Find the next newline. At EOS, stop. */
2493 eol = p = strchr (eol , '\n');
2494 if (!eol)
2496 ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
2497 return 0;
2500 /* Found a newline; if it's escaped continue; else we're done. */
2501 while (p > bol && *(--p) == '\\')
2502 backslash = !backslash;
2503 if (!backslash)
2504 break;
2505 ++eol;
2508 /* Overwrite the newline char. */
2509 *eol = '\0';
2510 ebuf->bufnext = eol+1;
2512 return 0;
2515 static long
2516 readline (struct ebuffer *ebuf)
2518 char *p;
2519 char *end;
2520 char *start;
2521 long nlines = 0;
2523 /* The behaviors between string and stream buffers are different enough to
2524 warrant different functions. Do the Right Thing. */
2526 if (!ebuf->fp)
2527 return readstring (ebuf);
2529 /* When reading from a file, we always start over at the beginning of the
2530 buffer for each new line. */
2532 p = start = ebuf->bufstart;
2533 end = p + ebuf->size;
2534 *p = '\0';
2536 while (fgets (p, end - p, ebuf->fp) != 0)
2538 char *p2;
2539 unsigned long len;
2540 int backslash;
2542 len = strlen (p);
2543 if (len == 0)
2545 /* This only happens when the first thing on the line is a '\0'.
2546 It is a pretty hopeless case, but (wonder of wonders) Athena
2547 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2548 There is nothing really to be done; we synthesize a newline so
2549 the following line doesn't appear to be part of this line. */
2550 error (&ebuf->floc,
2551 _("warning: NUL character seen; rest of line ignored"));
2552 p[0] = '\n';
2553 len = 1;
2556 /* Jump past the text we just read. */
2557 p += len;
2559 /* If the last char isn't a newline, the whole line didn't fit into the
2560 buffer. Get some more buffer and try again. */
2561 if (p[-1] != '\n')
2562 goto more_buffer;
2564 /* We got a newline, so add one to the count of lines. */
2565 ++nlines;
2567 #if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
2568 /* Check to see if the line was really ended with CRLF; if so ignore
2569 the CR. */
2570 if ((p - start) > 1 && p[-2] == '\r')
2572 --p;
2573 p[-1] = '\n';
2575 #endif
2577 backslash = 0;
2578 for (p2 = p - 2; p2 >= start; --p2)
2580 if (*p2 != '\\')
2581 break;
2582 backslash = !backslash;
2585 if (!backslash)
2587 p[-1] = '\0';
2588 break;
2591 /* It was a backslash/newline combo. If we have more space, read
2592 another line. */
2593 if (end - p >= 80)
2594 continue;
2596 /* We need more space at the end of our buffer, so realloc it.
2597 Make sure to preserve the current offset of p. */
2598 more_buffer:
2600 unsigned long off = p - start;
2601 ebuf->size *= 2;
2602 start = ebuf->buffer = ebuf->bufstart = (char *) xrealloc (start,
2603 ebuf->size);
2604 p = start + off;
2605 end = start + ebuf->size;
2606 *p = '\0';
2610 if (ferror (ebuf->fp))
2611 pfatal_with_name (ebuf->floc.filenm);
2613 /* If we found some lines, return how many.
2614 If we didn't, but we did find _something_, that indicates we read the last
2615 line of a file with no final newline; return 1.
2616 If we read nothing, we're at EOF; return -1. */
2618 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
2621 /* Parse the next "makefile word" from the input buffer, and return info
2622 about it.
2624 A "makefile word" is one of:
2626 w_bogus Should never happen
2627 w_eol End of input
2628 w_static A static word; cannot be expanded
2629 w_variable A word containing one or more variables/functions
2630 w_colon A colon
2631 w_dcolon A double-colon
2632 w_semicolon A semicolon
2633 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2635 Note that this function is only used when reading certain parts of the
2636 makefile. Don't use it where special rules hold sway (RHS of a variable,
2637 in a command list, etc.) */
2639 static enum make_word_type
2640 get_next_mword (char *buffer, char *delim, char **startp, unsigned int *length)
2642 enum make_word_type wtype = w_bogus;
2643 char *p = buffer, *beg;
2644 char c;
2646 /* Skip any leading whitespace. */
2647 while (isblank ((unsigned char)*p))
2648 ++p;
2650 beg = p;
2651 c = *(p++);
2652 switch (c)
2654 case '\0':
2655 wtype = w_eol;
2656 break;
2658 case ';':
2659 wtype = w_semicolon;
2660 break;
2662 case '=':
2663 wtype = w_varassign;
2664 break;
2666 case ':':
2667 wtype = w_colon;
2668 switch (*p)
2670 case ':':
2671 ++p;
2672 wtype = w_dcolon;
2673 break;
2675 case '=':
2676 ++p;
2677 wtype = w_varassign;
2678 break;
2680 break;
2682 case '+':
2683 case '?':
2684 if (*p == '=')
2686 ++p;
2687 wtype = w_varassign;
2688 break;
2691 default:
2692 if (delim && strchr (delim, c))
2693 wtype = w_static;
2694 break;
2697 /* Did we find something? If so, return now. */
2698 if (wtype != w_bogus)
2699 goto done;
2701 /* This is some non-operator word. A word consists of the longest
2702 string of characters that doesn't contain whitespace, one of [:=#],
2703 or [?+]=, or one of the chars in the DELIM string. */
2705 /* We start out assuming a static word; if we see a variable we'll
2706 adjust our assumptions then. */
2707 wtype = w_static;
2709 /* We already found the first value of "c", above. */
2710 while (1)
2712 char closeparen;
2713 int count;
2715 switch (c)
2717 case '\0':
2718 case ' ':
2719 case '\t':
2720 case '=':
2721 goto done_word;
2723 case ':':
2724 #ifdef HAVE_DOS_PATHS
2725 /* A word CAN include a colon in its drive spec. The drive
2726 spec is allowed either at the beginning of a word, or as part
2727 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2728 if (!(p - beg >= 2
2729 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
2730 && (p - beg == 2 || p[-3] == '(')))
2731 #endif
2732 goto done_word;
2734 case '$':
2735 c = *(p++);
2736 if (c == '$')
2737 break;
2739 /* This is a variable reference, so note that it's expandable.
2740 Then read it to the matching close paren. */
2741 wtype = w_variable;
2743 if (c == '(')
2744 closeparen = ')';
2745 else if (c == '{')
2746 closeparen = '}';
2747 else
2748 /* This is a single-letter variable reference. */
2749 break;
2751 for (count=0; *p != '\0'; ++p)
2753 if (*p == c)
2754 ++count;
2755 else if (*p == closeparen && --count < 0)
2757 ++p;
2758 break;
2761 break;
2763 case '?':
2764 case '+':
2765 if (*p == '=')
2766 goto done_word;
2767 break;
2769 case '\\':
2770 switch (*p)
2772 case ':':
2773 case ';':
2774 case '=':
2775 case '\\':
2776 ++p;
2777 break;
2779 break;
2781 default:
2782 if (delim && strchr (delim, c))
2783 goto done_word;
2784 break;
2787 c = *(p++);
2789 done_word:
2790 --p;
2792 done:
2793 if (startp)
2794 *startp = beg;
2795 if (length)
2796 *length = p - beg;
2797 return wtype;
2800 /* Construct the list of include directories
2801 from the arguments and the default list. */
2803 void
2804 construct_include_path (char **arg_dirs)
2806 register unsigned int i;
2807 #ifdef VAXC /* just don't ask ... */
2808 stat_t stbuf;
2809 #else
2810 struct stat stbuf;
2811 #endif
2812 /* Table to hold the dirs. */
2814 register unsigned int defsize = (sizeof (default_include_directories)
2815 / sizeof (default_include_directories[0]));
2816 register unsigned int max = 5;
2817 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
2818 register unsigned int idx = 0;
2820 #ifdef __MSDOS__
2821 defsize++;
2822 #endif
2824 /* First consider any dirs specified with -I switches.
2825 Ignore dirs that don't exist. */
2827 if (arg_dirs != 0)
2828 while (*arg_dirs != 0)
2830 char *dir = *arg_dirs++;
2831 int e;
2833 if (dir[0] == '~')
2835 char *expanded = tilde_expand (dir);
2836 if (expanded != 0)
2837 dir = expanded;
2840 EINTRLOOP (e, stat (dir, &stbuf));
2841 if (e == 0 && S_ISDIR (stbuf.st_mode))
2843 if (idx == max - 1)
2845 max += 5;
2846 dirs = (char **)
2847 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
2849 dirs[idx++] = dir;
2851 else if (dir != arg_dirs[-1])
2852 free (dir);
2855 /* Now add at the end the standard default dirs. */
2857 #ifdef __MSDOS__
2859 /* The environment variable $DJDIR holds the root of the
2860 DJGPP directory tree; add ${DJDIR}/include. */
2861 struct variable *djdir = lookup_variable ("DJDIR", 5);
2863 if (djdir)
2865 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2867 strcat (strcpy (defdir, djdir->value), "/include");
2868 dirs[idx++] = defdir;
2871 #endif
2873 for (i = 0; default_include_directories[i] != 0; ++i)
2875 int e;
2877 EINTRLOOP (e, stat (default_include_directories[i], &stbuf));
2878 if (e == 0 && S_ISDIR (stbuf.st_mode))
2879 dirs[idx++] = default_include_directories[i];
2882 dirs[idx] = 0;
2884 /* Now compute the maximum length of any name in it. */
2886 max_incl_len = 0;
2887 for (i = 0; i < idx; ++i)
2889 unsigned int len = strlen (dirs[i]);
2890 /* If dir name is written with a trailing slash, discard it. */
2891 if (dirs[i][len - 1] == '/')
2892 /* We can't just clobber a null in because it may have come from
2893 a literal string and literal strings may not be writable. */
2894 dirs[i] = savestring (dirs[i], len - 1);
2895 if (len > max_incl_len)
2896 max_incl_len = len;
2899 include_directories = dirs;
2902 /* Expand ~ or ~USER at the beginning of NAME.
2903 Return a newly malloc'd string or 0. */
2905 char *
2906 tilde_expand (char *name)
2908 #ifndef VMS
2909 if (name[1] == '/' || name[1] == '\0')
2911 extern char *getenv ();
2912 char *home_dir;
2913 int is_variable;
2916 /* Turn off --warn-undefined-variables while we expand HOME. */
2917 int save = warn_undefined_variables_flag;
2918 warn_undefined_variables_flag = 0;
2920 home_dir = allocated_variable_expand ("$(HOME)");
2922 warn_undefined_variables_flag = save;
2925 is_variable = home_dir[0] != '\0';
2926 if (!is_variable)
2928 free (home_dir);
2929 home_dir = getenv ("HOME");
2931 #if !defined(_AMIGA) && !defined(WINDOWS32)
2932 if (home_dir == 0 || home_dir[0] == '\0')
2934 extern char *getlogin ();
2935 char *logname = getlogin ();
2936 home_dir = 0;
2937 if (logname != 0)
2939 struct passwd *p = getpwnam (logname);
2940 if (p != 0)
2941 home_dir = p->pw_dir;
2944 #endif /* !AMIGA && !WINDOWS32 */
2945 if (home_dir != 0)
2947 char *new = concat (home_dir, "", name + 1);
2948 if (is_variable)
2949 free (home_dir);
2950 return new;
2953 #if !defined(_AMIGA) && !defined(WINDOWS32)
2954 else
2956 struct passwd *pwent;
2957 char *userend = strchr (name + 1, '/');
2958 if (userend != 0)
2959 *userend = '\0';
2960 pwent = getpwnam (name + 1);
2961 if (pwent != 0)
2963 if (userend == 0)
2964 return xstrdup (pwent->pw_dir);
2965 else
2966 return concat (pwent->pw_dir, "/", userend + 1);
2968 else if (userend != 0)
2969 *userend = '/';
2971 #endif /* !AMIGA && !WINDOWS32 */
2972 #endif /* !VMS */
2973 return 0;
2976 /* Given a chain of struct nameseq's describing a sequence of filenames,
2977 in reverse of the intended order, return a new chain describing the
2978 result of globbing the filenames. The new chain is in forward order.
2979 The links of the old chain are freed or used in the new chain.
2980 Likewise for the names in the old chain.
2982 SIZE is how big to construct chain elements.
2983 This is useful if we want them actually to be other structures
2984 that have room for additional info. */
2986 struct nameseq *
2987 multi_glob (struct nameseq *chain, unsigned int size)
2989 extern void dir_setup_glob ();
2990 register struct nameseq *new = 0;
2991 register struct nameseq *old;
2992 struct nameseq *nexto;
2993 glob_t gl;
2995 dir_setup_glob (&gl);
2997 for (old = chain; old != 0; old = nexto)
2999 #ifndef NO_ARCHIVES
3000 char *memname;
3001 #endif
3003 nexto = old->next;
3005 if (old->name[0] == '~')
3007 char *newname = tilde_expand (old->name);
3008 if (newname != 0)
3010 free (old->name);
3011 old->name = newname;
3015 #ifndef NO_ARCHIVES
3016 if (ar_name (old->name))
3018 /* OLD->name is an archive member reference.
3019 Replace it with the archive file name,
3020 and save the member name in MEMNAME.
3021 We will glob on the archive name and then
3022 reattach MEMNAME later. */
3023 char *arname;
3024 ar_parse_name (old->name, &arname, &memname);
3025 free (old->name);
3026 old->name = arname;
3028 else
3029 memname = 0;
3030 #endif /* !NO_ARCHIVES */
3032 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
3034 case 0: /* Success. */
3036 register int i = gl.gl_pathc;
3037 while (i-- > 0)
3039 #ifndef NO_ARCHIVES
3040 if (memname != 0)
3042 /* Try to glob on MEMNAME within the archive. */
3043 struct nameseq *found
3044 = ar_glob (gl.gl_pathv[i], memname, size);
3045 if (found == 0)
3047 /* No matches. Use MEMNAME as-is. */
3048 unsigned int alen = strlen (gl.gl_pathv[i]);
3049 unsigned int mlen = strlen (memname);
3050 struct nameseq *elt
3051 = (struct nameseq *) xmalloc (size);
3052 if (size > sizeof (struct nameseq))
3053 bzero (((char *) elt) + sizeof (struct nameseq),
3054 size - sizeof (struct nameseq));
3055 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
3056 bcopy (gl.gl_pathv[i], elt->name, alen);
3057 elt->name[alen] = '(';
3058 bcopy (memname, &elt->name[alen + 1], mlen);
3059 elt->name[alen + 1 + mlen] = ')';
3060 elt->name[alen + 1 + mlen + 1] = '\0';
3061 elt->next = new;
3062 new = elt;
3064 else
3066 /* Find the end of the FOUND chain. */
3067 struct nameseq *f = found;
3068 while (f->next != 0)
3069 f = f->next;
3071 /* Attach the chain being built to the end of the FOUND
3072 chain, and make FOUND the new NEW chain. */
3073 f->next = new;
3074 new = found;
3077 free (memname);
3079 else
3080 #endif /* !NO_ARCHIVES */
3082 struct nameseq *elt = (struct nameseq *) xmalloc (size);
3083 if (size > sizeof (struct nameseq))
3084 bzero (((char *) elt) + sizeof (struct nameseq),
3085 size - sizeof (struct nameseq));
3086 elt->name = xstrdup (gl.gl_pathv[i]);
3087 elt->next = new;
3088 new = elt;
3091 globfree (&gl);
3092 free (old->name);
3093 free ((char *)old);
3094 break;
3097 case GLOB_NOSPACE:
3098 fatal (NILF, _("virtual memory exhausted"));
3099 break;
3101 default:
3102 old->next = new;
3103 new = old;
3104 break;
3108 return new;