Updated for autoconf 2.57, automake 1.7.6, and gettext 0.12.1.
[make/kirr.git] / read.c
blobd79895e1687538695246b2455881b1f2ed907e16
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 int enabled,
143 const struct floc *flocp));
144 static enum make_word_type get_next_mword PARAMS ((char *buffer, char *delim,
145 char **startp, unsigned int *length));
147 /* Read in all the makefiles and return the chain of their names. */
149 struct dep *
150 read_all_makefiles (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;
275 /* Install a new conditional and return the previous one. */
277 static struct conditionals *
278 install_conditionals (struct conditionals *new)
280 struct conditionals *save = conditionals;
282 bzero ((char *) new, sizeof (*new));
283 conditionals = new;
285 return save;
288 /* Free the current conditionals and reinstate a saved one. */
290 static void
291 restore_conditionals (struct conditionals *saved)
293 /* Free any space allocated by conditional_line. */
294 if (conditionals->ignoring)
295 free (conditionals->ignoring);
296 if (conditionals->seen_else)
297 free (conditionals->seen_else);
299 /* Restore state. */
300 conditionals = saved;
303 static int
304 eval_makefile (char *filename, int flags)
306 struct dep *deps;
307 struct ebuffer ebuf;
308 const struct floc *curfile;
309 int makefile_errno;
310 int r;
312 ebuf.floc.filenm = filename;
313 ebuf.floc.lineno = 1;
315 if (ISDB (DB_VERBOSE))
317 printf (_("Reading makefile `%s'"), filename);
318 if (flags & RM_NO_DEFAULT_GOAL)
319 printf (_(" (no default goal)"));
320 if (flags & RM_INCLUDED)
321 printf (_(" (search path)"));
322 if (flags & RM_DONTCARE)
323 printf (_(" (don't care)"));
324 if (flags & RM_NO_TILDE)
325 printf (_(" (no ~ expansion)"));
326 puts ("...");
329 /* First, get a stream to read. */
331 /* Expand ~ in FILENAME unless it came from `include',
332 in which case it was already done. */
333 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
335 char *expanded = tilde_expand (filename);
336 if (expanded != 0)
337 filename = expanded;
340 ebuf.fp = fopen (filename, "r");
341 /* Save the error code so we print the right message later. */
342 makefile_errno = errno;
344 /* If the makefile wasn't found and it's either a makefile from
345 the `MAKEFILES' variable or an included makefile,
346 search the included makefile search path for this makefile. */
347 if (ebuf.fp == 0 && (flags & RM_INCLUDED) && *filename != '/')
349 register unsigned int i;
350 for (i = 0; include_directories[i] != 0; ++i)
352 char *name = concat (include_directories[i], "/", filename);
353 ebuf.fp = fopen (name, "r");
354 if (ebuf.fp == 0)
355 free (name);
356 else
358 filename = name;
359 break;
364 /* Add FILENAME to the chain of read makefiles. */
365 deps = (struct dep *) xmalloc (sizeof (struct dep));
366 deps->next = read_makefiles;
367 read_makefiles = deps;
368 deps->name = 0;
369 deps->file = lookup_file (filename);
370 if (deps->file == 0)
372 deps->file = enter_file (xstrdup (filename));
373 if (flags & RM_DONTCARE)
374 deps->file->dontcare = 1;
376 if (filename != ebuf.floc.filenm)
377 free (filename);
378 filename = deps->file->name;
379 deps->changed = flags;
380 deps->ignore_mtime = 0;
382 /* If the makefile can't be found at all, give up entirely. */
384 if (ebuf.fp == 0)
386 /* If we did some searching, errno has the error from the last
387 attempt, rather from FILENAME itself. Restore it in case the
388 caller wants to use it in a message. */
389 errno = makefile_errno;
390 return 0;
393 /* Add this makefile to the list. */
394 do_variable_definition (&ebuf.floc, "MAKEFILE_LIST", filename, o_file,
395 f_append, 0);
397 /* Evaluate the makefile */
399 ebuf.size = 200;
400 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = xmalloc (ebuf.size);
402 curfile = reading_file;
403 reading_file = &ebuf.floc;
405 r = eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
407 reading_file = curfile;
409 fclose (ebuf.fp);
411 free (ebuf.bufstart);
412 return r;
416 eval_buffer (char *buffer)
418 struct ebuffer ebuf;
419 struct conditionals *saved;
420 struct conditionals new;
421 const struct floc *curfile;
422 int r;
424 /* Evaluate the buffer */
426 ebuf.size = strlen (buffer);
427 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = buffer;
428 ebuf.fp = NULL;
430 ebuf.floc = *reading_file;
432 curfile = reading_file;
433 reading_file = &ebuf.floc;
435 saved = install_conditionals (&new);
437 r = eval (&ebuf, 1);
439 restore_conditionals (saved);
441 reading_file = curfile;
443 return r;
447 /* Read file FILENAME as a makefile and add its contents to the data base.
449 SET_DEFAULT is true if we are allowed to set the default goal. */
452 static int
453 eval (struct ebuffer *ebuf, int set_default)
455 char *collapsed = 0;
456 unsigned int collapsed_length = 0;
457 unsigned int commands_len = 200;
458 char *commands;
459 unsigned int commands_idx = 0;
460 unsigned int cmds_started, tgts_started;
461 int ignoring = 0, in_ignored_define = 0;
462 int no_targets = 0; /* Set when reading a rule without targets. */
463 int have_sysv_atvar = 0;
464 struct nameseq *filenames = 0;
465 struct dep *deps = 0;
466 long nlines = 0;
467 int two_colon = 0;
468 char *pattern = 0, *pattern_percent;
469 struct floc *fstart;
470 struct floc fi;
472 #define record_waiting_files() \
473 do \
475 if (filenames != 0) \
477 fi.lineno = tgts_started; \
478 record_files (filenames, pattern, pattern_percent, deps, \
479 cmds_started, commands, commands_idx, two_colon, \
480 have_sysv_atvar, &fi, set_default); \
482 filenames = 0; \
483 commands_idx = 0; \
484 no_targets = 0; \
485 if (pattern) { free(pattern); pattern = 0; } \
486 } while (0)
488 pattern_percent = 0;
489 cmds_started = tgts_started = 1;
491 fstart = &ebuf->floc;
492 fi.filenm = ebuf->floc.filenm;
494 /* Loop over lines in the file.
495 The strategy is to accumulate target names in FILENAMES, dependencies
496 in DEPS and commands in COMMANDS. These are used to define a rule
497 when the start of the next rule (or eof) is encountered.
499 When you see a "continue" in the loop below, that means we are moving on
500 to the next line _without_ ending any rule that we happen to be working
501 with at the moment. If you see a "goto rule_complete", then the
502 statement we just parsed also finishes the previous rule. */
504 commands = xmalloc (200);
506 while (1)
508 int linelen;
509 char *line;
510 int len;
511 char *p;
512 char *p2;
514 /* Grab the next line to be evaluated */
515 ebuf->floc.lineno += nlines;
516 nlines = readline (ebuf);
518 /* If there is nothing left to eval, we're done. */
519 if (nlines < 0)
520 break;
522 /* If this line is empty, skip it. */
523 line = ebuf->buffer;
524 if (line[0] == '\0')
525 continue;
527 linelen = strlen (line);
529 /* Check for a shell command line first.
530 If it is not one, we can stop treating tab specially. */
531 if (line[0] == '\t')
533 if (no_targets)
534 /* Ignore the commands in a rule with no targets. */
535 continue;
537 /* If there is no preceding rule line, don't treat this line
538 as a command, even though it begins with a tab character.
539 SunOS 4 make appears to behave this way. */
541 if (filenames != 0)
543 if (ignoring)
544 /* Yep, this is a shell command, and we don't care. */
545 continue;
547 /* Append this command line to the line being accumulated. */
548 if (commands_idx == 0)
549 cmds_started = ebuf->floc.lineno;
551 if (linelen + 1 + commands_idx > commands_len)
553 commands_len = (linelen + 1 + commands_idx) * 2;
554 commands = xrealloc (commands, commands_len);
556 bcopy (line, &commands[commands_idx], linelen);
557 commands_idx += linelen;
558 commands[commands_idx++] = '\n';
560 continue;
564 /* This line is not a shell command line. Don't worry about tabs.
565 Get more space if we need it; we don't need to preserve the current
566 contents of the buffer. */
568 if (collapsed_length < linelen+1)
570 collapsed_length = linelen+1;
571 if (collapsed)
572 free ((char *)collapsed);
573 collapsed = (char *) xmalloc (collapsed_length);
575 strcpy (collapsed, line);
576 /* Collapse continuation lines. */
577 collapse_continuations (collapsed);
578 remove_comments (collapsed);
580 /* Compare a word, both length and contents. */
581 #define word1eq(s) (len == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
582 p = collapsed;
583 while (isspace ((unsigned char)*p))
584 ++p;
586 if (*p == '\0')
587 /* This line is completely empty--ignore it. */
588 continue;
590 /* Find the end of the first token. Note we don't need to worry about
591 * ":" here since we compare tokens by length (so "export" will never
592 * be equal to "export:").
594 for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
596 len = p2 - p;
598 /* Find the start of the second token. If it looks like a target or
599 variable definition it can't be a preprocessor token so skip
600 them--this allows variables/targets named `ifdef', `export', etc. */
601 while (isspace ((unsigned char)*p2))
602 ++p2;
604 if ((p2[0] == ':' || p2[0] == '+' || p2[0] == '=') && p2[1] == '\0')
606 /* It can't be a preprocessor token so skip it if we're ignoring */
607 if (ignoring)
608 continue;
610 goto skip_conditionals;
613 /* We must first check for conditional and `define' directives before
614 ignoring anything, since they control what we will do with
615 following lines. */
617 if (!in_ignored_define
618 && (word1eq ("ifdef") || word1eq ("ifndef")
619 || word1eq ("ifeq") || word1eq ("ifneq")
620 || word1eq ("else") || word1eq ("endif")))
622 int i = conditional_line (p, fstart);
623 if (i < 0)
624 fatal (fstart, _("invalid syntax in conditional"));
626 ignoring = i;
627 continue;
630 if (word1eq ("endef"))
632 if (!in_ignored_define)
633 fatal (fstart, _("extraneous `endef'"));
634 in_ignored_define = 0;
635 continue;
638 if (word1eq ("define"))
640 if (ignoring)
641 in_ignored_define = 1;
642 else
644 if (*p2 == '\0')
645 fatal (fstart, _("empty variable name"));
647 /* Let the variable name be the whole rest of the line,
648 with trailing blanks stripped (comments have already been
649 removed), so it could be a complex variable/function
650 reference that might contain blanks. */
651 p = strchr (p2, '\0');
652 while (isblank ((unsigned char)p[-1]))
653 --p;
654 do_define (p2, p - p2, o_file, ebuf);
656 continue;
659 if (word1eq ("override"))
661 if (*p2 == '\0')
662 error (fstart, _("empty `override' directive"));
664 if (strneq (p2, "define", 6)
665 && (isblank ((unsigned char)p2[6]) || p2[6] == '\0'))
667 if (ignoring)
668 in_ignored_define = 1;
669 else
671 p2 = next_token (p2 + 6);
672 if (*p2 == '\0')
673 fatal (fstart, _("empty variable name"));
675 /* Let the variable name be the whole rest of the line,
676 with trailing blanks stripped (comments have already been
677 removed), so it could be a complex variable/function
678 reference that might contain blanks. */
679 p = strchr (p2, '\0');
680 while (isblank ((unsigned char)p[-1]))
681 --p;
682 do_define (p2, p - p2, o_override, ebuf);
685 else if (!ignoring
686 && !try_variable_definition (fstart, p2, o_override, 0))
687 error (fstart, _("invalid `override' directive"));
689 continue;
692 if (ignoring)
693 /* Ignore the line. We continue here so conditionals
694 can appear in the middle of a rule. */
695 continue;
697 if (word1eq ("export"))
699 /* 'export' by itself causes everything to be exported. */
700 if (*p2 == '\0')
701 export_all_variables = 1;
702 else
704 struct variable *v;
706 v = try_variable_definition (fstart, p2, o_file, 0);
707 if (v != 0)
708 v->export = v_export;
709 else
711 unsigned int len;
712 char *ap;
714 /* Expand the line so we can use indirect and constructed
715 variable names in an export command. */
716 p2 = ap = allocated_variable_expand (p2);
718 for (p = find_next_token (&p2, &len); p != 0;
719 p = find_next_token (&p2, &len))
721 v = lookup_variable (p, len);
722 if (v == 0)
723 v = define_variable_loc (p, len, "", o_file, 0,
724 fstart);
725 v->export = v_export;
728 free (ap);
731 goto rule_complete;
734 if (word1eq ("unexport"))
736 if (*p2 == '\0')
737 export_all_variables = 0;
738 else
740 unsigned int len;
741 struct variable *v;
742 char *ap;
744 /* Expand the line so we can use indirect and constructed
745 variable names in an unexport command. */
746 p2 = ap = allocated_variable_expand (p2);
748 for (p = find_next_token (&p2, &len); p != 0;
749 p = find_next_token (&p2, &len))
751 v = lookup_variable (p, len);
752 if (v == 0)
753 v = define_variable_loc (p, len, "", o_file, 0, fstart);
755 v->export = v_noexport;
758 free (ap);
760 goto rule_complete;
763 skip_conditionals:
764 if (word1eq ("vpath"))
766 char *pattern;
767 unsigned int len;
768 p2 = variable_expand (p2);
769 p = find_next_token (&p2, &len);
770 if (p != 0)
772 pattern = savestring (p, len);
773 p = find_next_token (&p2, &len);
774 /* No searchpath means remove all previous
775 selective VPATH's with the same pattern. */
777 else
778 /* No pattern means remove all previous selective VPATH's. */
779 pattern = 0;
780 construct_vpath_list (pattern, p);
781 if (pattern != 0)
782 free (pattern);
784 goto rule_complete;
787 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
789 /* We have found an `include' line specifying a nested
790 makefile to be read at this point. */
791 struct conditionals *save;
792 struct conditionals new_conditionals;
793 struct nameseq *files;
794 /* "-include" (vs "include") says no error if the file does not
795 exist. "sinclude" is an alias for this from SGI. */
796 int noerror = (p[0] != 'i');
798 p = allocated_variable_expand (p2);
799 if (*p == '\0')
801 error (fstart,
802 _("no file name for `%sinclude'"), noerror ? "-" : "");
803 continue;
806 /* Parse the list of file names. */
807 p2 = p;
808 files = multi_glob (parse_file_seq (&p2, '\0',
809 sizeof (struct nameseq),
811 sizeof (struct nameseq));
812 free (p);
814 /* Save the state of conditionals and start
815 the included makefile with a clean slate. */
816 save = install_conditionals (&new_conditionals);
818 /* Record the rules that are waiting so they will determine
819 the default goal before those in the included makefile. */
820 record_waiting_files ();
822 /* Read each included makefile. */
823 while (files != 0)
825 struct nameseq *next = files->next;
826 char *name = files->name;
827 int r;
829 free ((char *)files);
830 files = next;
832 r = eval_makefile (name, (RM_INCLUDED | RM_NO_TILDE
833 | (noerror ? RM_DONTCARE : 0)));
834 if (!r)
836 if (!noerror)
837 error (fstart, "%s: %s", name, strerror (errno));
838 free (name);
842 /* Restore conditional state. */
843 restore_conditionals (save);
845 goto rule_complete;
848 if (try_variable_definition (fstart, p, o_file, 0))
849 /* This line has been dealt with. */
850 goto rule_complete;
852 if (line[0] == '\t')
854 p = collapsed; /* Ignore comments, etc. */
855 while (isblank ((unsigned char)*p))
856 ++p;
857 if (*p == '\0')
858 /* The line is completely blank; that is harmless. */
859 continue;
861 /* This line starts with a tab but was not caught above
862 because there was no preceding target, and the line
863 might have been usable as a variable definition.
864 But now we know it is definitely lossage. */
865 fatal(fstart, _("commands commence before first target"));
868 /* This line describes some target files. This is complicated by
869 the existence of target-specific variables, because we can't
870 expand the entire line until we know if we have one or not. So
871 we expand the line word by word until we find the first `:',
872 then check to see if it's a target-specific variable.
874 In this algorithm, `lb_next' will point to the beginning of the
875 unexpanded parts of the input buffer, while `p2' points to the
876 parts of the expanded buffer we haven't searched yet. */
879 enum make_word_type wtype;
880 enum variable_origin v_origin;
881 int exported;
882 char *cmdleft, *semip, *lb_next;
883 unsigned int len, plen = 0;
884 char *colonp;
886 /* Record the previous rule. */
888 record_waiting_files ();
889 tgts_started = fstart->lineno;
891 /* Search the line for an unquoted ; that is not after an
892 unquoted #. */
893 cmdleft = find_char_unquote (line, ';', '#', 0);
894 if (cmdleft != 0 && *cmdleft == '#')
896 /* We found a comment before a semicolon. */
897 *cmdleft = '\0';
898 cmdleft = 0;
900 else if (cmdleft != 0)
901 /* Found one. Cut the line short there before expanding it. */
902 *(cmdleft++) = '\0';
903 semip = cmdleft;
905 collapse_continuations (line);
907 /* We can't expand the entire line, since if it's a per-target
908 variable we don't want to expand it. So, walk from the
909 beginning, expanding as we go, and looking for "interesting"
910 chars. The first word is always expandable. */
911 wtype = get_next_mword(line, NULL, &lb_next, &len);
912 switch (wtype)
914 case w_eol:
915 if (cmdleft != 0)
916 fatal(fstart, _("missing rule before commands"));
917 /* This line contained something but turned out to be nothing
918 but whitespace (a comment?). */
919 continue;
921 case w_colon:
922 case w_dcolon:
923 /* We accept and ignore rules without targets for
924 compatibility with SunOS 4 make. */
925 no_targets = 1;
926 continue;
928 default:
929 break;
932 p2 = variable_expand_string(NULL, lb_next, len);
933 while (1)
935 lb_next += len;
936 if (cmdleft == 0)
938 /* Look for a semicolon in the expanded line. */
939 cmdleft = find_char_unquote (p2, ';', 0, 0);
941 if (cmdleft != 0)
943 unsigned long p2_off = p2 - variable_buffer;
944 unsigned long cmd_off = cmdleft - variable_buffer;
945 char *pend = p2 + strlen(p2);
947 /* Append any remnants of lb, then cut the line short
948 at the semicolon. */
949 *cmdleft = '\0';
951 /* One school of thought says that you shouldn't expand
952 here, but merely copy, since now you're beyond a ";"
953 and into a command script. However, the old parser
954 expanded the whole line, so we continue that for
955 backwards-compatiblity. Also, it wouldn't be
956 entirely consistent, since we do an unconditional
957 expand below once we know we don't have a
958 target-specific variable. */
959 (void)variable_expand_string(pend, lb_next, (long)-1);
960 lb_next += strlen(lb_next);
961 p2 = variable_buffer + p2_off;
962 cmdleft = variable_buffer + cmd_off + 1;
966 colonp = find_char_unquote(p2, ':', 0, 0);
967 #ifdef HAVE_DOS_PATHS
968 /* The drive spec brain-damage strikes again... */
969 /* Note that the only separators of targets in this context
970 are whitespace and a left paren. If others are possible,
971 they should be added to the string in the call to index. */
972 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
973 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
974 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
975 colonp = find_char_unquote(colonp + 1, ':', 0, 0);
976 #endif
977 if (colonp != 0)
978 break;
980 wtype = get_next_mword(lb_next, NULL, &lb_next, &len);
981 if (wtype == w_eol)
982 break;
984 p2 += strlen(p2);
985 *(p2++) = ' ';
986 p2 = variable_expand_string(p2, lb_next, len);
987 /* We don't need to worry about cmdleft here, because if it was
988 found in the variable_buffer the entire buffer has already
989 been expanded... we'll never get here. */
992 p2 = next_token (variable_buffer);
994 /* If the word we're looking at is EOL, see if there's _anything_
995 on the line. If not, a variable expanded to nothing, so ignore
996 it. If so, we can't parse this line so punt. */
997 if (wtype == w_eol)
999 if (*p2 != '\0')
1000 /* There's no need to be ivory-tower about this: check for
1001 one of the most common bugs found in makefiles... */
1002 fatal (fstart, _("missing separator%s"),
1003 !strneq(line, " ", 8) ? ""
1004 : _(" (did you mean TAB instead of 8 spaces?)"));
1005 continue;
1008 /* Make the colon the end-of-string so we know where to stop
1009 looking for targets. */
1010 *colonp = '\0';
1011 filenames = multi_glob (parse_file_seq (&p2, '\0',
1012 sizeof (struct nameseq),
1014 sizeof (struct nameseq));
1015 *p2 = ':';
1017 if (!filenames)
1019 /* We accept and ignore rules without targets for
1020 compatibility with SunOS 4 make. */
1021 no_targets = 1;
1022 continue;
1024 /* This should never be possible; we handled it above. */
1025 assert (*p2 != '\0');
1026 ++p2;
1028 /* Is this a one-colon or two-colon entry? */
1029 two_colon = *p2 == ':';
1030 if (two_colon)
1031 p2++;
1033 /* Test to see if it's a target-specific variable. Copy the rest
1034 of the buffer over, possibly temporarily (we'll expand it later
1035 if it's not a target-specific variable). PLEN saves the length
1036 of the unparsed section of p2, for later. */
1037 if (*lb_next != '\0')
1039 unsigned int l = p - variable_buffer;
1040 unsigned int l2 = p2 - variable_buffer;
1041 plen = strlen (p2);
1042 (void) variable_buffer_output (p2+plen,
1043 lb_next, strlen (lb_next)+1);
1044 p = variable_buffer + l;
1045 p2 = variable_buffer + l2;
1048 /* See if it's an "override" or "export" keyword; if so see if what
1049 comes after it looks like a variable definition. */
1051 wtype = get_next_mword (p2, NULL, &p, &len);
1053 v_origin = o_file;
1054 exported = 0;
1055 if (wtype == w_static)
1056 if (word1eq ("override"))
1058 v_origin = o_override;
1059 wtype = get_next_mword (p+len, NULL, &p, &len);
1061 else if (word1eq ("export"))
1063 exported = 1;
1064 wtype = get_next_mword (p+len, NULL, &p, &len);
1067 if (wtype != w_eol)
1068 wtype = get_next_mword (p+len, NULL, NULL, NULL);
1070 if (wtype == w_varassign)
1072 /* If there was a semicolon found, add it back, plus anything
1073 after it. */
1074 if (semip)
1076 unsigned int l = p - variable_buffer;
1077 *(--semip) = ';';
1078 variable_buffer_output (p2 + strlen (p2),
1079 semip, strlen (semip)+1);
1080 p = variable_buffer + l;
1082 record_target_var (filenames, p, two_colon, v_origin, exported,
1083 fstart);
1084 filenames = 0;
1085 continue;
1088 /* This is a normal target, _not_ a target-specific variable.
1089 Unquote any = in the dependency list. */
1090 find_char_unquote (lb_next, '=', 0, 0);
1092 /* We have some targets, so don't ignore the following commands. */
1093 no_targets = 0;
1095 /* Expand the dependencies, etc. */
1096 if (*lb_next != '\0')
1098 unsigned int l = p2 - variable_buffer;
1099 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
1100 p2 = variable_buffer + l;
1102 /* Look for a semicolon in the expanded line. */
1103 if (cmdleft == 0)
1105 cmdleft = find_char_unquote (p2, ';', 0, 0);
1106 if (cmdleft != 0)
1107 *(cmdleft++) = '\0';
1111 /* Do any of the prerequisites appear to have $@ etc.? */
1112 have_sysv_atvar = 0;
1113 if (!posix_pedantic)
1114 for (p = strchr (p2, '$'); p != 0; p = strchr (p+1, '$'))
1115 if (p[1] == '@' || ((p[1] == '(' || p[1] == '{') && p[2] == '@'))
1117 have_sysv_atvar = 1;
1118 break;
1121 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
1122 p = strchr (p2, ':');
1123 while (p != 0 && p[-1] == '\\')
1125 register char *q = &p[-1];
1126 register int backslash = 0;
1127 while (*q-- == '\\')
1128 backslash = !backslash;
1129 if (backslash)
1130 p = strchr (p + 1, ':');
1131 else
1132 break;
1134 #ifdef _AMIGA
1135 /* Here, the situation is quite complicated. Let's have a look
1136 at a couple of targets:
1138 install: dev:make
1140 dev:make: make
1142 dev:make:: xyz
1144 The rule is that it's only a target, if there are TWO :'s
1145 OR a space around the :.
1147 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
1148 || isspace ((unsigned char)p[-1])))
1149 p = 0;
1150 #endif
1151 #ifdef HAVE_DOS_PATHS
1153 int check_again;
1155 do {
1156 check_again = 0;
1157 /* For DOS-style paths, skip a "C:\..." or a "C:/..." */
1158 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
1159 isalpha ((unsigned char)p[-1]) &&
1160 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1161 p = strchr (p + 1, ':');
1162 check_again = 1;
1164 } while (check_again);
1166 #endif
1167 if (p != 0)
1169 struct nameseq *target;
1170 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
1171 ++p2;
1172 if (target == 0)
1173 fatal (fstart, _("missing target pattern"));
1174 else if (target->next != 0)
1175 fatal (fstart, _("multiple target patterns"));
1176 pattern = target->name;
1177 pattern_percent = find_percent (pattern);
1178 if (pattern_percent == 0)
1179 fatal (fstart, _("target pattern contains no `%%'"));
1180 free((char *)target);
1182 else
1183 pattern = 0;
1185 /* Parse the dependencies. */
1186 deps = (struct dep *)
1187 multi_glob (parse_file_seq (&p2, '|', sizeof (struct dep), 1),
1188 sizeof (struct dep));
1189 if (*p2)
1191 /* Files that follow '|' are special prerequisites that
1192 need only exist in order to satisfy the dependency.
1193 Their modification times are irrelevant. */
1194 struct dep **deps_ptr = &deps;
1195 struct dep *d;
1196 for (deps_ptr = &deps; *deps_ptr; deps_ptr = &(*deps_ptr)->next)
1198 ++p2;
1199 *deps_ptr = (struct dep *)
1200 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
1201 sizeof (struct dep));
1202 for (d = *deps_ptr; d != 0; d = d->next)
1203 d->ignore_mtime = 1;
1206 commands_idx = 0;
1207 if (cmdleft != 0)
1209 /* Semicolon means rest of line is a command. */
1210 unsigned int len = strlen (cmdleft);
1212 cmds_started = fstart->lineno;
1214 /* Add this command line to the buffer. */
1215 if (len + 2 > commands_len)
1217 commands_len = (len + 2) * 2;
1218 commands = (char *) xrealloc (commands, commands_len);
1220 bcopy (cmdleft, commands, len);
1221 commands_idx += len;
1222 commands[commands_idx++] = '\n';
1225 continue;
1228 /* We get here except in the case that we just read a rule line.
1229 Record now the last rule we read, so following spurious
1230 commands are properly diagnosed. */
1231 rule_complete:
1232 record_waiting_files ();
1235 #undef word1eq
1237 if (conditionals->if_cmds)
1238 fatal (fstart, _("missing `endif'"));
1240 /* At eof, record the last rule. */
1241 record_waiting_files ();
1243 if (collapsed)
1244 free ((char *) collapsed);
1245 free ((char *) commands);
1247 return 1;
1251 /* Execute a `define' directive.
1252 The first line has already been read, and NAME is the name of
1253 the variable to be defined. The following lines remain to be read. */
1255 static void
1256 do_define (char *name, unsigned int namelen,
1257 enum variable_origin origin, struct ebuffer *ebuf)
1259 struct floc defstart;
1260 long nlines = 0;
1261 int nlevels = 1;
1262 unsigned int length = 100;
1263 char *definition = (char *) xmalloc (length);
1264 unsigned int idx = 0;
1265 char *p;
1267 /* Expand the variable name. */
1268 char *var = (char *) alloca (namelen + 1);
1269 bcopy (name, var, namelen);
1270 var[namelen] = '\0';
1271 var = variable_expand (var);
1273 defstart = ebuf->floc;
1275 while (1)
1277 unsigned int len;
1278 char *line;
1280 ebuf->floc.lineno += nlines;
1281 nlines = readline (ebuf);
1283 /* If there is nothing left to eval, we're done. */
1284 if (nlines < 0)
1285 break;
1287 line = ebuf->buffer;
1289 collapse_continuations (line);
1291 /* If the line doesn't begin with a tab, test to see if it introduces
1292 another define, or ends one. */
1294 /* Stop if we find an 'endef' */
1295 if (line[0] != '\t')
1297 p = next_token (line);
1298 len = strlen (p);
1300 /* If this is another 'define', increment the level count. */
1301 if ((len == 6 || (len > 6 && isblank ((unsigned char)p[6])))
1302 && strneq (p, "define", 6))
1303 ++nlevels;
1305 /* If this is an 'endef', decrement the count. If it's now 0,
1306 we've found the last one. */
1307 else if ((len == 5 || (len > 5 && isblank ((unsigned char)p[5])))
1308 && strneq (p, "endef", 5))
1310 p += 5;
1311 remove_comments (p);
1312 if (*next_token (p) != '\0')
1313 error (&ebuf->floc,
1314 _("Extraneous text after `endef' directive"));
1316 if (--nlevels == 0)
1318 /* Define the variable. */
1319 if (idx == 0)
1320 definition[0] = '\0';
1321 else
1322 definition[idx - 1] = '\0';
1324 /* Always define these variables in the global set. */
1325 define_variable_global (var, strlen (var), definition,
1326 origin, 1, &defstart);
1327 free (definition);
1328 return;
1333 /* Otherwise add this line to the variable definition. */
1334 len = strlen (line);
1335 if (idx + len + 1 > length)
1337 length = (idx + len) * 2;
1338 definition = (char *) xrealloc (definition, length + 1);
1341 bcopy (line, &definition[idx], len);
1342 idx += len;
1343 /* Separate lines with a newline. */
1344 definition[idx++] = '\n';
1347 /* No `endef'!! */
1348 fatal (&defstart, _("missing `endef', unterminated `define'"));
1350 /* NOTREACHED */
1351 return;
1354 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1355 "ifneq", "else" and "endif".
1356 LINE is the input line, with the command as its first word.
1358 FILENAME and LINENO are the filename and line number in the
1359 current makefile. They are used for error messages.
1361 Value is -1 if the line is invalid,
1362 0 if following text should be interpreted,
1363 1 if following text should be ignored. */
1365 static int
1366 conditional_line (char *line, const struct floc *flocp)
1368 int notdef;
1369 char *cmdname;
1370 register unsigned int i;
1372 if (*line == 'i')
1374 /* It's an "if..." command. */
1375 notdef = line[2] == 'n';
1376 if (notdef)
1378 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
1379 line += cmdname[3] == 'd' ? 7 : 6;
1381 else
1383 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
1384 line += cmdname[2] == 'd' ? 6 : 5;
1387 else
1389 /* It's an "else" or "endif" command. */
1390 notdef = line[1] == 'n';
1391 cmdname = notdef ? "endif" : "else";
1392 line += notdef ? 5 : 4;
1395 line = next_token (line);
1397 if (*cmdname == 'e')
1399 if (*line != '\0')
1400 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1401 /* "Else" or "endif". */
1402 if (conditionals->if_cmds == 0)
1403 fatal (flocp, _("extraneous `%s'"), cmdname);
1404 /* NOTDEF indicates an `endif' command. */
1405 if (notdef)
1406 --conditionals->if_cmds;
1407 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1408 fatal (flocp, _("only one `else' per conditional"));
1409 else
1411 /* Toggle the state of ignorance. */
1412 conditionals->ignoring[conditionals->if_cmds - 1]
1413 = !conditionals->ignoring[conditionals->if_cmds - 1];
1414 /* Record that we have seen an `else' in this conditional.
1415 A second `else' will be erroneous. */
1416 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1418 for (i = 0; i < conditionals->if_cmds; ++i)
1419 if (conditionals->ignoring[i])
1420 return 1;
1421 return 0;
1424 if (conditionals->allocated == 0)
1426 conditionals->allocated = 5;
1427 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1428 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1431 ++conditionals->if_cmds;
1432 if (conditionals->if_cmds > conditionals->allocated)
1434 conditionals->allocated += 5;
1435 conditionals->ignoring = (char *)
1436 xrealloc (conditionals->ignoring, conditionals->allocated);
1437 conditionals->seen_else = (char *)
1438 xrealloc (conditionals->seen_else, conditionals->allocated);
1441 /* Record that we have seen an `if...' but no `else' so far. */
1442 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1444 /* Search through the stack to see if we're already ignoring. */
1445 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1446 if (conditionals->ignoring[i])
1448 /* We are already ignoring, so just push a level
1449 to match the next "else" or "endif", and keep ignoring.
1450 We don't want to expand variables in the condition. */
1451 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1452 return 1;
1455 if (cmdname[notdef ? 3 : 2] == 'd')
1457 /* "Ifdef" or "ifndef". */
1458 char *var;
1459 struct variable *v;
1460 register char *p = end_of_token (line);
1461 i = p - line;
1462 p = next_token (p);
1463 if (*p != '\0')
1464 return -1;
1466 /* Expand the thing we're looking up, so we can use indirect and
1467 constructed variable names. */
1468 line[i] = '\0';
1469 var = allocated_variable_expand (line);
1471 v = lookup_variable (var, strlen (var));
1472 conditionals->ignoring[conditionals->if_cmds - 1]
1473 = (v != 0 && *v->value != '\0') == notdef;
1475 free (var);
1477 else
1479 /* "Ifeq" or "ifneq". */
1480 char *s1, *s2;
1481 unsigned int len;
1482 char termin = *line == '(' ? ',' : *line;
1484 if (termin != ',' && termin != '"' && termin != '\'')
1485 return -1;
1487 s1 = ++line;
1488 /* Find the end of the first string. */
1489 if (termin == ',')
1491 register int count = 0;
1492 for (; *line != '\0'; ++line)
1493 if (*line == '(')
1494 ++count;
1495 else if (*line == ')')
1496 --count;
1497 else if (*line == ',' && count <= 0)
1498 break;
1500 else
1501 while (*line != '\0' && *line != termin)
1502 ++line;
1504 if (*line == '\0')
1505 return -1;
1507 if (termin == ',')
1509 /* Strip blanks after the first string. */
1510 char *p = line++;
1511 while (isblank ((unsigned char)p[-1]))
1512 --p;
1513 *p = '\0';
1515 else
1516 *line++ = '\0';
1518 s2 = variable_expand (s1);
1519 /* We must allocate a new copy of the expanded string because
1520 variable_expand re-uses the same buffer. */
1521 len = strlen (s2);
1522 s1 = (char *) alloca (len + 1);
1523 bcopy (s2, s1, len + 1);
1525 if (termin != ',')
1526 /* Find the start of the second string. */
1527 line = next_token (line);
1529 termin = termin == ',' ? ')' : *line;
1530 if (termin != ')' && termin != '"' && termin != '\'')
1531 return -1;
1533 /* Find the end of the second string. */
1534 if (termin == ')')
1536 register int count = 0;
1537 s2 = next_token (line);
1538 for (line = s2; *line != '\0'; ++line)
1540 if (*line == '(')
1541 ++count;
1542 else if (*line == ')')
1544 if (count <= 0)
1545 break;
1546 else
1547 --count;
1551 else
1553 ++line;
1554 s2 = line;
1555 while (*line != '\0' && *line != termin)
1556 ++line;
1559 if (*line == '\0')
1560 return -1;
1562 *line = '\0';
1563 line = next_token (++line);
1564 if (*line != '\0')
1565 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1567 s2 = variable_expand (s2);
1568 conditionals->ignoring[conditionals->if_cmds - 1]
1569 = streq (s1, s2) == notdef;
1572 /* Search through the stack to see if we're ignoring. */
1573 for (i = 0; i < conditionals->if_cmds; ++i)
1574 if (conditionals->ignoring[i])
1575 return 1;
1576 return 0;
1579 /* Remove duplicate dependencies in CHAIN. */
1581 static unsigned long
1582 dep_hash_1 (const void *key)
1584 return_STRING_HASH_1 (dep_name ((struct dep const *) key));
1587 static unsigned long
1588 dep_hash_2 (const void *key)
1590 return_STRING_HASH_2 (dep_name ((struct dep const *) key));
1593 static int
1594 dep_hash_cmp (const void *x, const void *y)
1596 struct dep *dx = (struct dep *) x;
1597 struct dep *dy = (struct dep *) y;
1598 int cmp = strcmp (dep_name (dx), dep_name (dy));
1600 /* If the names are the same but ignore_mtimes are not equal, one of these
1601 is an order-only prerequisite and one isn't. That means that we should
1602 remove the one that isn't and keep the one that is. */
1604 if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
1605 dx->ignore_mtime = dy->ignore_mtime = 0;
1607 return cmp;
1611 void
1612 uniquize_deps (struct dep *chain)
1614 struct hash_table deps;
1615 register struct dep **depp;
1617 hash_init (&deps, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
1619 /* Make sure that no dependencies are repeated. This does not
1620 really matter for the purpose of updating targets, but it
1621 might make some names be listed twice for $^ and $?. */
1623 depp = &chain;
1624 while (*depp)
1626 struct dep *dep = *depp;
1627 struct dep **dep_slot = (struct dep **) hash_find_slot (&deps, dep);
1628 if (HASH_VACANT (*dep_slot))
1630 hash_insert_at (&deps, dep, dep_slot);
1631 depp = &dep->next;
1633 else
1635 /* Don't bother freeing duplicates.
1636 It's dangerous and little benefit accrues. */
1637 *depp = dep->next;
1641 hash_free (&deps, 0);
1644 /* Record target-specific variable values for files FILENAMES.
1645 TWO_COLON is nonzero if a double colon was used.
1647 The links of FILENAMES are freed, and so are any names in it
1648 that are not incorporated into other data structures.
1650 If the target is a pattern, add the variable to the pattern-specific
1651 variable value list. */
1653 static void
1654 record_target_var (struct nameseq *filenames, char *defn, int two_colon,
1655 enum variable_origin origin, int exported,
1656 const struct floc *flocp)
1658 struct nameseq *nextf;
1659 struct variable_set_list *global;
1661 global = current_variable_set_list;
1663 /* If the variable is an append version, store that but treat it as a
1664 normal recursive variable. */
1666 for (; filenames != 0; filenames = nextf)
1668 struct variable *v;
1669 register char *name = filenames->name;
1670 char *fname;
1671 char *percent;
1672 struct pattern_var *p;
1674 nextf = filenames->next;
1675 free ((char *) filenames);
1677 /* If it's a pattern target, then add it to the pattern-specific
1678 variable list. */
1679 percent = find_percent (name);
1680 if (percent)
1682 /* Get a reference for this pattern-specific variable struct. */
1683 p = create_pattern_var (name, percent);
1684 p->variable.fileinfo = *flocp;
1685 v = parse_variable_definition (&p->variable, defn);
1686 v->value = xstrdup (v->value);
1687 if (!v)
1688 error (flocp, _("Malformed pattern-specific variable definition"));
1689 fname = p->target;
1691 else
1693 struct file *f;
1695 /* Get a file reference for this file, and initialize it.
1696 We don't want to just call enter_file() because that allocates a
1697 new entry if the file is a double-colon, which we don't want in
1698 this situation. */
1699 f = lookup_file (name);
1700 if (!f)
1701 f = enter_file (name);
1702 else if (f->double_colon)
1703 f = f->double_colon;
1705 initialize_file_variables (f, 1);
1706 fname = f->name;
1708 current_variable_set_list = f->variables;
1709 v = try_variable_definition (flocp, defn, origin, 1);
1710 if (!v)
1711 error (flocp, _("Malformed target-specific variable definition"));
1712 current_variable_set_list = global;
1715 /* Set up the variable to be *-specific. */
1716 v->origin = origin;
1717 v->per_target = 1;
1718 if (exported)
1719 v->export = v_export;
1721 /* If it's not an override, check to see if there was a command-line
1722 setting. If so, reset the value. */
1723 if (origin != o_override)
1725 struct variable *gv;
1726 int len = strlen(v->name);
1728 gv = lookup_variable (v->name, len);
1729 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1731 if (v->value != 0)
1732 free (v->value);
1733 v->value = xstrdup (gv->value);
1734 v->origin = gv->origin;
1735 v->recursive = gv->recursive;
1736 v->append = 0;
1740 /* Free name if not needed further. */
1741 if (name != fname && (name < fname || name > fname + strlen (fname)))
1742 free (name);
1746 /* Record a description line for files FILENAMES,
1747 with dependencies DEPS, commands to execute described
1748 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1749 TWO_COLON is nonzero if a double colon was used.
1750 If not nil, PATTERN is the `%' pattern to make this
1751 a static pattern rule, and PATTERN_PERCENT is a pointer
1752 to the `%' within it.
1754 The links of FILENAMES are freed, and so are any names in it
1755 that are not incorporated into other data structures. */
1757 static void
1758 record_files (struct nameseq *filenames, char *pattern, char *pattern_percent,
1759 struct dep *deps, unsigned int cmds_started, char *commands,
1760 unsigned int commands_idx, int two_colon,
1761 int have_sysv_atvar, const struct floc *flocp, int set_default)
1763 struct nameseq *nextf;
1764 int implicit = 0;
1765 unsigned int max_targets = 0, target_idx = 0;
1766 char **targets = 0, **target_percents = 0;
1767 struct commands *cmds;
1769 if (commands_idx > 0)
1771 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1772 cmds->fileinfo.filenm = flocp->filenm;
1773 cmds->fileinfo.lineno = cmds_started;
1774 cmds->commands = savestring (commands, commands_idx);
1775 cmds->command_lines = 0;
1777 else
1778 cmds = 0;
1780 for (; filenames != 0; filenames = nextf)
1782 char *name = filenames->name;
1783 struct file *f;
1784 struct dep *d;
1785 struct dep *this;
1786 char *implicit_percent;
1788 nextf = filenames->next;
1789 free (filenames);
1791 /* Check for .POSIX. We used to do this in snap_deps() but that's not
1792 good enough: it doesn't happen until after the makefile is read,
1793 which means we cannot use its value during parsing. */
1795 if (streq (name, ".POSIX"))
1796 posix_pedantic = 1;
1798 implicit_percent = find_percent (name);
1799 implicit |= implicit_percent != 0;
1801 if (implicit && pattern != 0)
1802 fatal (flocp, _("mixed implicit and static pattern rules"));
1804 if (implicit && implicit_percent == 0)
1805 fatal (flocp, _("mixed implicit and normal rules"));
1807 if (implicit)
1809 if (targets == 0)
1811 max_targets = 5;
1812 targets = (char **) xmalloc (5 * sizeof (char *));
1813 target_percents = (char **) xmalloc (5 * sizeof (char *));
1814 target_idx = 0;
1816 else if (target_idx == max_targets - 1)
1818 max_targets += 5;
1819 targets = (char **) xrealloc ((char *) targets,
1820 max_targets * sizeof (char *));
1821 target_percents
1822 = (char **) xrealloc ((char *) target_percents,
1823 max_targets * sizeof (char *));
1825 targets[target_idx] = name;
1826 target_percents[target_idx] = implicit_percent;
1827 ++target_idx;
1828 continue;
1831 /* If there are multiple filenames, copy the chain DEPS
1832 for all but the last one. It is not safe for the same deps
1833 to go in more than one place in the data base. */
1834 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1836 if (pattern != 0)
1838 /* If this is an extended static rule:
1839 `targets: target%pattern: dep%pattern; cmds',
1840 translate each dependency pattern into a plain filename
1841 using the target pattern and this target's name. */
1842 if (!pattern_matches (pattern, pattern_percent, name))
1844 /* Give a warning if the rule is meaningless. */
1845 error (flocp,
1846 _("target `%s' doesn't match the target pattern"), name);
1847 this = 0;
1849 else
1851 /* We use patsubst_expand to do the work of translating
1852 the target pattern, the target's name and the dependencies'
1853 patterns into plain dependency names. */
1854 char *buffer = variable_expand ("");
1856 for (d = this; d != 0; d = d->next)
1858 char *o;
1859 char *percent = find_percent (d->name);
1860 if (percent == 0)
1861 continue;
1862 o = patsubst_expand (buffer, name, pattern, d->name,
1863 pattern_percent, percent);
1864 /* If the name expanded to the empty string, that's
1865 illegal. */
1866 if (o == buffer)
1867 fatal (flocp,
1868 _("target `%s' leaves prerequisite pattern empty"),
1869 name);
1870 free (d->name);
1871 d->name = savestring (buffer, o - buffer);
1876 /* If at least one of the dependencies uses $$@ etc. deal with that.
1877 It would be very nice and very simple to just expand everything, but
1878 it would break a lot of backward compatibility. Maybe that's OK
1879 since we're just emulating a SysV function, and if we do that then
1880 why not emulate it completely (that's what SysV make does: it
1881 re-expands the entire prerequisite list, all the time, with $@
1882 etc. in scope). But, it would be a pain indeed to document this
1883 ("iff you use $$@, your prerequisite lists is expanded twice...")
1884 Ouch. Maybe better to make the code more complex. */
1886 if (have_sysv_atvar)
1888 char *p;
1889 int tlen = strlen (name);
1890 char *fnp = strrchr (name, '/');
1891 int dlen;
1892 int flen;
1894 if (fnp)
1896 dlen = fnp - name;
1897 ++fnp;
1898 flen = strlen (fnp);
1900 else
1902 dlen = 0;
1903 fnp = name;
1904 flen = tlen;
1908 for (d = this; d != 0; d = d->next)
1909 for (p = strchr (d->name, '$'); p != 0; p = strchr (p+1, '$'))
1911 char *s = p;
1912 char *at;
1913 int atlen;
1915 /* If it's '$@', '$(@', or '${@', it's escaped */
1916 if ((++p)[0] == '$'
1917 && (p[1] == '@'
1918 || ((p[1] == '(' || p[1] == '{') && p[2] == '@')))
1920 bcopy (p, s, strlen (p)+1);
1921 continue;
1924 /* Maybe found one. We like anything of any form matching @,
1925 [({]@[}):], or [({]@[DF][}):]. */
1927 if (! (p[0] == '@'
1928 || ((p[0] == '(' || p[0] == '{') && (++p)[0] == '@'
1929 && (((++p)[0] == ')' || p[0] == '}' || p[0] == ':')
1930 || ((p[1] == ')' || p[1] == '}' || p[1] == ':')
1931 && (p[0] == 'D' || p[0] == 'F'))))))
1932 continue;
1934 /* Found one. Compute the length and string ptr. Move p
1935 past the variable reference. */
1936 switch (p[0])
1938 case 'D':
1939 atlen = dlen;
1940 at = name;
1941 p += 2;
1942 break;
1944 case 'F':
1945 atlen = flen;
1946 at = fnp;
1947 p += 2;
1948 break;
1950 default:
1951 atlen = tlen;
1952 at = name;
1953 ++p;
1954 break;
1957 /* Get more space. */
1959 int soff = s - d->name;
1960 int poff = p - d->name;
1961 d->name = (char *) xrealloc (d->name,
1962 strlen (d->name) + atlen + 1);
1963 s = d->name + soff;
1964 p = d->name + poff;
1967 /* Copy the string over. */
1968 bcopy(p, s+atlen, strlen (p)+1);
1969 bcopy(at, s, atlen);
1970 p = s + atlen - 1;
1974 if (!two_colon)
1976 /* Single-colon. Combine these dependencies
1977 with others in file's existing record, if any. */
1978 f = enter_file (name);
1980 if (f->double_colon)
1981 fatal (flocp,
1982 _("target file `%s' has both : and :: entries"), f->name);
1984 /* If CMDS == F->CMDS, this target was listed in this rule
1985 more than once. Just give a warning since this is harmless. */
1986 if (cmds != 0 && cmds == f->cmds)
1987 error (flocp,
1988 _("target `%s' given more than once in the same rule."),
1989 f->name);
1991 /* Check for two single-colon entries both with commands.
1992 Check is_target so that we don't lose on files such as .c.o
1993 whose commands were preinitialized. */
1994 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1996 error (&cmds->fileinfo,
1997 _("warning: overriding commands for target `%s'"),
1998 f->name);
1999 error (&f->cmds->fileinfo,
2000 _("warning: ignoring old commands for target `%s'"),
2001 f->name);
2004 f->is_target = 1;
2006 /* Defining .DEFAULT with no deps or cmds clears it. */
2007 if (f == default_file && this == 0 && cmds == 0)
2008 f->cmds = 0;
2009 if (cmds != 0)
2010 f->cmds = cmds;
2011 /* Defining .SUFFIXES with no dependencies
2012 clears out the list of suffixes. */
2013 if (f == suffix_file && this == 0)
2015 d = f->deps;
2016 while (d != 0)
2018 struct dep *nextd = d->next;
2019 free (d->name);
2020 free ((char *)d);
2021 d = nextd;
2023 f->deps = 0;
2025 else if (f->deps != 0)
2027 /* Add the file's old deps and the new ones in THIS together. */
2029 struct dep *firstdeps, *moredeps;
2030 if (cmds != 0)
2032 /* This is the rule with commands, so put its deps first.
2033 The rationale behind this is that $< expands to the
2034 first dep in the chain, and commands use $< expecting
2035 to get the dep that rule specifies. */
2036 firstdeps = this;
2037 moredeps = f->deps;
2039 else
2041 /* Append the new deps to the old ones. */
2042 firstdeps = f->deps;
2043 moredeps = this;
2046 if (firstdeps == 0)
2047 firstdeps = moredeps;
2048 else
2050 d = firstdeps;
2051 while (d->next != 0)
2052 d = d->next;
2053 d->next = moredeps;
2056 f->deps = firstdeps;
2058 else
2059 f->deps = this;
2061 /* If this is a static pattern rule, set the file's stem to
2062 the part of its name that matched the `%' in the pattern,
2063 so you can use $* in the commands. */
2064 if (pattern != 0)
2066 static char *percent = "%";
2067 char *buffer = variable_expand ("");
2068 char *o = patsubst_expand (buffer, name, pattern, percent,
2069 pattern_percent, percent);
2070 f->stem = savestring (buffer, o - buffer);
2073 else
2075 /* Double-colon. Make a new record
2076 even if the file already has one. */
2077 f = lookup_file (name);
2078 /* Check for both : and :: rules. Check is_target so
2079 we don't lose on default suffix rules or makefiles. */
2080 if (f != 0 && f->is_target && !f->double_colon)
2081 fatal (flocp,
2082 _("target file `%s' has both : and :: entries"), f->name);
2083 f = enter_file (name);
2084 /* If there was an existing entry and it was a double-colon
2085 entry, enter_file will have returned a new one, making it the
2086 prev pointer of the old one, and setting its double_colon
2087 pointer to the first one. */
2088 if (f->double_colon == 0)
2089 /* This is the first entry for this name, so we must
2090 set its double_colon pointer to itself. */
2091 f->double_colon = f;
2092 f->is_target = 1;
2093 f->deps = this;
2094 f->cmds = cmds;
2097 /* Free name if not needed further. */
2098 if (f != 0 && name != f->name
2099 && (name < f->name || name > f->name + strlen (f->name)))
2101 free (name);
2102 name = f->name;
2105 /* See if this is first target seen whose name does
2106 not start with a `.', unless it contains a slash. */
2107 if (default_goal_file == 0 && set_default
2108 && (*name != '.' || strchr (name, '/') != 0
2109 #ifdef HAVE_DOS_PATHS
2110 || strchr (name, '\\') != 0
2111 #endif
2114 int reject = 0;
2116 /* If this file is a suffix, don't
2117 let it be the default goal file. */
2119 for (d = suffix_file->deps; d != 0; d = d->next)
2121 register struct dep *d2;
2122 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
2124 reject = 1;
2125 break;
2127 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
2129 register unsigned int len = strlen (dep_name (d2));
2130 if (!strneq (name, dep_name (d2), len))
2131 continue;
2132 if (streq (name + len, dep_name (d)))
2134 reject = 1;
2135 break;
2138 if (reject)
2139 break;
2142 if (!reject)
2143 default_goal_file = f;
2147 if (implicit)
2149 targets[target_idx] = 0;
2150 target_percents[target_idx] = 0;
2151 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
2152 free ((char *) target_percents);
2156 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2157 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2158 Quoting backslashes are removed from STRING by compacting it into
2159 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2160 one, or nil if there are none. */
2162 char *
2163 find_char_unquote (char *string, int stop1, int stop2, int blank)
2165 unsigned int string_len = 0;
2166 register char *p = string;
2168 while (1)
2170 if (stop2 && blank)
2171 while (*p != '\0' && *p != stop1 && *p != stop2
2172 && ! isblank ((unsigned char) *p))
2173 ++p;
2174 else if (stop2)
2175 while (*p != '\0' && *p != stop1 && *p != stop2)
2176 ++p;
2177 else if (blank)
2178 while (*p != '\0' && *p != stop1
2179 && ! isblank ((unsigned char) *p))
2180 ++p;
2181 else
2182 while (*p != '\0' && *p != stop1)
2183 ++p;
2185 if (*p == '\0')
2186 break;
2188 if (p > string && p[-1] == '\\')
2190 /* Search for more backslashes. */
2191 register int i = -2;
2192 while (&p[i] >= string && p[i] == '\\')
2193 --i;
2194 ++i;
2195 /* Only compute the length if really needed. */
2196 if (string_len == 0)
2197 string_len = strlen (string);
2198 /* The number of backslashes is now -I.
2199 Copy P over itself to swallow half of them. */
2200 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
2201 p += i / 2;
2202 if (i % 2 == 0)
2203 /* All the backslashes quoted each other; the STOPCHAR was
2204 unquoted. */
2205 return p;
2207 /* The STOPCHAR was quoted by a backslash. Look for another. */
2209 else
2210 /* No backslash in sight. */
2211 return p;
2214 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2215 return 0;
2218 /* Search PATTERN for an unquoted %. */
2220 char *
2221 find_percent (char *pattern)
2223 return find_char_unquote (pattern, '%', 0, 0);
2226 /* Parse a string into a sequence of filenames represented as a
2227 chain of struct nameseq's in reverse order and return that chain.
2229 The string is passed as STRINGP, the address of a string pointer.
2230 The string pointer is updated to point at the first character
2231 not parsed, which either is a null char or equals STOPCHAR.
2233 SIZE is how big to construct chain elements.
2234 This is useful if we want them actually to be other structures
2235 that have room for additional info.
2237 If STRIP is nonzero, strip `./'s off the beginning. */
2239 struct nameseq *
2240 parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
2242 register struct nameseq *new = 0;
2243 register struct nameseq *new1, *lastnew1;
2244 register char *p = *stringp;
2245 char *q;
2246 char *name;
2248 #ifdef VMS
2249 # define VMS_COMMA ','
2250 #else
2251 # define VMS_COMMA 0
2252 #endif
2254 while (1)
2256 /* Skip whitespace; see if any more names are left. */
2257 p = next_token (p);
2258 if (*p == '\0')
2259 break;
2260 if (*p == stopchar)
2261 break;
2263 /* Yes, find end of next name. */
2264 q = p;
2265 p = find_char_unquote (q, stopchar, VMS_COMMA, 1);
2266 #ifdef VMS
2267 /* convert comma separated list to space separated */
2268 if (p && *p == ',')
2269 *p =' ';
2270 #endif
2271 #ifdef _AMIGA
2272 if (stopchar == ':' && p && *p == ':'
2273 && !(isspace ((unsigned char)p[1]) || !p[1]
2274 || isspace ((unsigned char)p[-1])))
2276 p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1);
2278 #endif
2279 #ifdef HAVE_DOS_PATHS
2280 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
2281 first colon which isn't followed by a slash or a backslash.
2282 Note that tokens separated by spaces should be treated as separate
2283 tokens since make doesn't allow path names with spaces */
2284 if (stopchar == ':')
2285 while (p != 0 && !isspace ((unsigned char)*p) &&
2286 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
2287 p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1);
2288 #endif
2289 if (p == 0)
2290 p = q + strlen (q);
2292 if (strip)
2293 #ifdef VMS
2294 /* Skip leading `[]'s. */
2295 while (p - q > 2 && q[0] == '[' && q[1] == ']')
2296 #else
2297 /* Skip leading `./'s. */
2298 while (p - q > 2 && q[0] == '.' && q[1] == '/')
2299 #endif
2301 q += 2; /* Skip "./". */
2302 while (q < p && *q == '/')
2303 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
2304 ++q;
2307 /* Extract the filename just found, and skip it. */
2309 if (q == p)
2310 /* ".///" was stripped to "". */
2311 #ifdef VMS
2312 continue;
2313 #else
2314 #ifdef _AMIGA
2315 name = savestring ("", 0);
2316 #else
2317 name = savestring ("./", 2);
2318 #endif
2319 #endif
2320 else
2321 #ifdef VMS
2322 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
2323 * to remove this '\' before we can use the filename.
2324 * Savestring called because q may be read-only string constant.
2327 char *qbase = xstrdup (q);
2328 char *pbase = qbase + (p-q);
2329 char *q1 = qbase;
2330 char *q2 = q1;
2331 char *p1 = pbase;
2333 while (q1 != pbase)
2335 if (*q1 == '\\' && *(q1+1) == ':')
2337 q1++;
2338 p1--;
2340 *q2++ = *q1++;
2342 name = savestring (qbase, p1 - qbase);
2343 free (qbase);
2345 #else
2346 name = savestring (q, p - q);
2347 #endif
2349 /* Add it to the front of the chain. */
2350 new1 = (struct nameseq *) xmalloc (size);
2351 new1->name = name;
2352 new1->next = new;
2353 new = new1;
2356 #ifndef NO_ARCHIVES
2358 /* Look for multi-word archive references.
2359 They are indicated by a elt ending with an unmatched `)' and
2360 an elt further down the chain (i.e., previous in the file list)
2361 with an unmatched `(' (e.g., "lib(mem"). */
2363 new1 = new;
2364 lastnew1 = 0;
2365 while (new1 != 0)
2366 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2367 && new1->name[strlen (new1->name) - 1] == ')'
2368 && strchr (new1->name, '(') == 0)
2370 /* NEW1 ends with a `)' but does not contain a `('.
2371 Look back for an elt with an opening `(' but no closing `)'. */
2373 struct nameseq *n = new1->next, *lastn = new1;
2374 char *paren = 0;
2375 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2377 lastn = n;
2378 n = n->next;
2380 if (n != 0
2381 /* Ignore something starting with `(', as that cannot actually
2382 be an archive-member reference (and treating it as such
2383 results in an empty file name, which causes much lossage). */
2384 && n->name[0] != '(')
2386 /* N is the first element in the archive group.
2387 Its name looks like "lib(mem" (with no closing `)'). */
2389 char *libname;
2391 /* Copy "lib(" into LIBNAME. */
2392 ++paren;
2393 libname = (char *) alloca (paren - n->name + 1);
2394 bcopy (n->name, libname, paren - n->name);
2395 libname[paren - n->name] = '\0';
2397 if (*paren == '\0')
2399 /* N was just "lib(", part of something like "lib( a b)".
2400 Edit it out of the chain and free its storage. */
2401 lastn->next = n->next;
2402 free (n->name);
2403 free ((char *) n);
2404 /* LASTN->next is the new stopping elt for the loop below. */
2405 n = lastn->next;
2407 else
2409 /* Replace N's name with the full archive reference. */
2410 name = concat (libname, paren, ")");
2411 free (n->name);
2412 n->name = name;
2415 if (new1->name[1] == '\0')
2417 /* NEW1 is just ")", part of something like "lib(a b )".
2418 Omit it from the chain and free its storage. */
2419 if (lastnew1 == 0)
2420 new = new1->next;
2421 else
2422 lastnew1->next = new1->next;
2423 lastn = new1;
2424 new1 = new1->next;
2425 free (lastn->name);
2426 free ((char *) lastn);
2428 else
2430 /* Replace also NEW1->name, which already has closing `)'. */
2431 name = concat (libname, new1->name, "");
2432 free (new1->name);
2433 new1->name = name;
2434 new1 = new1->next;
2437 /* Trace back from NEW1 (the end of the list) until N
2438 (the beginning of the list), rewriting each name
2439 with the full archive reference. */
2441 while (new1 != n)
2443 name = concat (libname, new1->name, ")");
2444 free (new1->name);
2445 new1->name = name;
2446 lastnew1 = new1;
2447 new1 = new1->next;
2450 else
2452 /* No frobnication happening. Just step down the list. */
2453 lastnew1 = new1;
2454 new1 = new1->next;
2457 else
2459 lastnew1 = new1;
2460 new1 = new1->next;
2463 #endif
2465 *stringp = p;
2466 return new;
2469 /* Find the next line of text in an eval buffer, combining continuation lines
2470 into one line.
2471 Return the number of actual lines read (> 1 if continuation lines).
2472 Returns -1 if there's nothing left in the buffer.
2474 After this function, ebuf->buffer points to the first character of the
2475 line we just found.
2478 /* Read a line of text from a STRING.
2479 Since we aren't really reading from a file, don't bother with linenumbers.
2482 static unsigned long
2483 readstring (struct ebuffer *ebuf)
2485 char *p;
2487 /* If there is nothing left in this buffer, return 0. */
2488 if (ebuf->bufnext > ebuf->bufstart + ebuf->size)
2489 return -1;
2491 /* Set up a new starting point for the buffer, and find the end of the
2492 next logical line (taking into account backslash/newline pairs). */
2494 p = ebuf->buffer = ebuf->bufnext;
2496 while (1)
2498 int backslash = 0;
2500 /* Find the next newline. Keep track of backslashes as we look. */
2501 for (; *p != '\n' && *p != '\0'; ++p)
2502 if (*p == '\\')
2503 backslash = !backslash;
2505 /* If we got to the end of the string or a newline with no backslash,
2506 we're done. */
2507 if (*p == '\0' || !backslash)
2508 break;
2511 /* Overwrite the newline char. */
2512 *p = '\0';
2513 ebuf->bufnext = p+1;
2515 return 0;
2518 static long
2519 readline (struct ebuffer *ebuf)
2521 char *p;
2522 char *end;
2523 char *start;
2524 long nlines = 0;
2526 /* The behaviors between string and stream buffers are different enough to
2527 warrant different functions. Do the Right Thing. */
2529 if (!ebuf->fp)
2530 return readstring (ebuf);
2532 /* When reading from a file, we always start over at the beginning of the
2533 buffer for each new line. */
2535 p = start = ebuf->bufstart;
2536 end = p + ebuf->size;
2537 *p = '\0';
2539 while (fgets (p, end - p, ebuf->fp) != 0)
2541 char *p2;
2542 unsigned long len;
2543 int backslash;
2545 len = strlen (p);
2546 if (len == 0)
2548 /* This only happens when the first thing on the line is a '\0'.
2549 It is a pretty hopeless case, but (wonder of wonders) Athena
2550 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2551 There is nothing really to be done; we synthesize a newline so
2552 the following line doesn't appear to be part of this line. */
2553 error (&ebuf->floc,
2554 _("warning: NUL character seen; rest of line ignored"));
2555 p[0] = '\n';
2556 len = 1;
2559 /* Jump past the text we just read. */
2560 p += len;
2562 /* If the last char isn't a newline, the whole line didn't fit into the
2563 buffer. Get some more buffer and try again. */
2564 if (p[-1] != '\n')
2565 goto more_buffer;
2567 /* We got a newline, so add one to the count of lines. */
2568 ++nlines;
2570 #if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
2571 /* Check to see if the line was really ended with CRLF; if so ignore
2572 the CR. */
2573 if ((p - start) > 1 && p[-2] == '\r')
2575 --p;
2576 p[-1] = '\n';
2578 #endif
2580 backslash = 0;
2581 for (p2 = p - 2; p2 >= start; --p2)
2583 if (*p2 != '\\')
2584 break;
2585 backslash = !backslash;
2588 if (!backslash)
2590 p[-1] = '\0';
2591 break;
2594 /* It was a backslash/newline combo. If we have more space, read
2595 another line. */
2596 if (end - p >= 80)
2597 continue;
2599 /* We need more space at the end of our buffer, so realloc it.
2600 Make sure to preserve the current offset of p. */
2601 more_buffer:
2603 unsigned long off = p - start;
2604 ebuf->size *= 2;
2605 start = ebuf->buffer = ebuf->bufstart = (char *) xrealloc (start,
2606 ebuf->size);
2607 p = start + off;
2608 end = start + ebuf->size;
2609 *p = '\0';
2613 if (ferror (ebuf->fp))
2614 pfatal_with_name (ebuf->floc.filenm);
2616 /* If we found some lines, return how many.
2617 If we didn't, but we did find _something_, that indicates we read the last
2618 line of a file with no final newline; return 1.
2619 If we read nothing, we're at EOF; return -1. */
2621 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
2624 /* Parse the next "makefile word" from the input buffer, and return info
2625 about it.
2627 A "makefile word" is one of:
2629 w_bogus Should never happen
2630 w_eol End of input
2631 w_static A static word; cannot be expanded
2632 w_variable A word containing one or more variables/functions
2633 w_colon A colon
2634 w_dcolon A double-colon
2635 w_semicolon A semicolon
2636 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2638 Note that this function is only used when reading certain parts of the
2639 makefile. Don't use it where special rules hold sway (RHS of a variable,
2640 in a command list, etc.) */
2642 static enum make_word_type
2643 get_next_mword (char *buffer, char *delim, char **startp, unsigned int *length)
2645 enum make_word_type wtype = w_bogus;
2646 char *p = buffer, *beg;
2647 char c;
2649 /* Skip any leading whitespace. */
2650 while (isblank ((unsigned char)*p))
2651 ++p;
2653 beg = p;
2654 c = *(p++);
2655 switch (c)
2657 case '\0':
2658 wtype = w_eol;
2659 break;
2661 case ';':
2662 wtype = w_semicolon;
2663 break;
2665 case '=':
2666 wtype = w_varassign;
2667 break;
2669 case ':':
2670 wtype = w_colon;
2671 switch (*p)
2673 case ':':
2674 ++p;
2675 wtype = w_dcolon;
2676 break;
2678 case '=':
2679 ++p;
2680 wtype = w_varassign;
2681 break;
2683 break;
2685 case '+':
2686 case '?':
2687 if (*p == '=')
2689 ++p;
2690 wtype = w_varassign;
2691 break;
2694 default:
2695 if (delim && strchr (delim, c))
2696 wtype = w_static;
2697 break;
2700 /* Did we find something? If so, return now. */
2701 if (wtype != w_bogus)
2702 goto done;
2704 /* This is some non-operator word. A word consists of the longest
2705 string of characters that doesn't contain whitespace, one of [:=#],
2706 or [?+]=, or one of the chars in the DELIM string. */
2708 /* We start out assuming a static word; if we see a variable we'll
2709 adjust our assumptions then. */
2710 wtype = w_static;
2712 /* We already found the first value of "c", above. */
2713 while (1)
2715 char closeparen;
2716 int count;
2718 switch (c)
2720 case '\0':
2721 case ' ':
2722 case '\t':
2723 case '=':
2724 goto done_word;
2726 case ':':
2727 #ifdef HAVE_DOS_PATHS
2728 /* A word CAN include a colon in its drive spec. The drive
2729 spec is allowed either at the beginning of a word, or as part
2730 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2731 if (!(p - beg >= 2
2732 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
2733 && (p - beg == 2 || p[-3] == '(')))
2734 #endif
2735 goto done_word;
2737 case '$':
2738 c = *(p++);
2739 if (c == '$')
2740 break;
2742 /* This is a variable reference, so note that it's expandable.
2743 Then read it to the matching close paren. */
2744 wtype = w_variable;
2746 if (c == '(')
2747 closeparen = ')';
2748 else if (c == '{')
2749 closeparen = '}';
2750 else
2751 /* This is a single-letter variable reference. */
2752 break;
2754 for (count=0; *p != '\0'; ++p)
2756 if (*p == c)
2757 ++count;
2758 else if (*p == closeparen && --count < 0)
2760 ++p;
2761 break;
2764 break;
2766 case '?':
2767 case '+':
2768 if (*p == '=')
2769 goto done_word;
2770 break;
2772 case '\\':
2773 switch (*p)
2775 case ':':
2776 case ';':
2777 case '=':
2778 case '\\':
2779 ++p;
2780 break;
2782 break;
2784 default:
2785 if (delim && strchr (delim, c))
2786 goto done_word;
2787 break;
2790 c = *(p++);
2792 done_word:
2793 --p;
2795 done:
2796 if (startp)
2797 *startp = beg;
2798 if (length)
2799 *length = p - beg;
2800 return wtype;
2803 /* Construct the list of include directories
2804 from the arguments and the default list. */
2806 void
2807 construct_include_path (char **arg_dirs)
2809 register unsigned int i;
2810 #ifdef VAXC /* just don't ask ... */
2811 stat_t stbuf;
2812 #else
2813 struct stat stbuf;
2814 #endif
2815 /* Table to hold the dirs. */
2817 register unsigned int defsize = (sizeof (default_include_directories)
2818 / sizeof (default_include_directories[0]));
2819 register unsigned int max = 5;
2820 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
2821 register unsigned int idx = 0;
2823 #ifdef __MSDOS__
2824 defsize++;
2825 #endif
2827 /* First consider any dirs specified with -I switches.
2828 Ignore dirs that don't exist. */
2830 if (arg_dirs != 0)
2831 while (*arg_dirs != 0)
2833 char *dir = *arg_dirs++;
2834 int e;
2836 if (dir[0] == '~')
2838 char *expanded = tilde_expand (dir);
2839 if (expanded != 0)
2840 dir = expanded;
2843 EINTRLOOP (e, stat (dir, &stbuf));
2844 if (e == 0 && S_ISDIR (stbuf.st_mode))
2846 if (idx == max - 1)
2848 max += 5;
2849 dirs = (char **)
2850 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
2852 dirs[idx++] = dir;
2854 else if (dir != arg_dirs[-1])
2855 free (dir);
2858 /* Now add at the end the standard default dirs. */
2860 #ifdef __MSDOS__
2862 /* The environment variable $DJDIR holds the root of the
2863 DJGPP directory tree; add ${DJDIR}/include. */
2864 struct variable *djdir = lookup_variable ("DJDIR", 5);
2866 if (djdir)
2868 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2870 strcat (strcpy (defdir, djdir->value), "/include");
2871 dirs[idx++] = defdir;
2874 #endif
2876 for (i = 0; default_include_directories[i] != 0; ++i)
2878 int e;
2880 EINTRLOOP (e, stat (default_include_directories[i], &stbuf));
2881 if (e == 0 && S_ISDIR (stbuf.st_mode))
2882 dirs[idx++] = default_include_directories[i];
2885 dirs[idx] = 0;
2887 /* Now compute the maximum length of any name in it. */
2889 max_incl_len = 0;
2890 for (i = 0; i < idx; ++i)
2892 unsigned int len = strlen (dirs[i]);
2893 /* If dir name is written with a trailing slash, discard it. */
2894 if (dirs[i][len - 1] == '/')
2895 /* We can't just clobber a null in because it may have come from
2896 a literal string and literal strings may not be writable. */
2897 dirs[i] = savestring (dirs[i], len - 1);
2898 if (len > max_incl_len)
2899 max_incl_len = len;
2902 include_directories = dirs;
2905 /* Expand ~ or ~USER at the beginning of NAME.
2906 Return a newly malloc'd string or 0. */
2908 char *
2909 tilde_expand (char *name)
2911 #ifndef VMS
2912 if (name[1] == '/' || name[1] == '\0')
2914 extern char *getenv ();
2915 char *home_dir;
2916 int is_variable;
2919 /* Turn off --warn-undefined-variables while we expand HOME. */
2920 int save = warn_undefined_variables_flag;
2921 warn_undefined_variables_flag = 0;
2923 home_dir = allocated_variable_expand ("$(HOME)");
2925 warn_undefined_variables_flag = save;
2928 is_variable = home_dir[0] != '\0';
2929 if (!is_variable)
2931 free (home_dir);
2932 home_dir = getenv ("HOME");
2934 #if !defined(_AMIGA) && !defined(WINDOWS32)
2935 if (home_dir == 0 || home_dir[0] == '\0')
2937 extern char *getlogin ();
2938 char *logname = getlogin ();
2939 home_dir = 0;
2940 if (logname != 0)
2942 struct passwd *p = getpwnam (logname);
2943 if (p != 0)
2944 home_dir = p->pw_dir;
2947 #endif /* !AMIGA && !WINDOWS32 */
2948 if (home_dir != 0)
2950 char *new = concat (home_dir, "", name + 1);
2951 if (is_variable)
2952 free (home_dir);
2953 return new;
2956 #if !defined(_AMIGA) && !defined(WINDOWS32)
2957 else
2959 struct passwd *pwent;
2960 char *userend = strchr (name + 1, '/');
2961 if (userend != 0)
2962 *userend = '\0';
2963 pwent = getpwnam (name + 1);
2964 if (pwent != 0)
2966 if (userend == 0)
2967 return xstrdup (pwent->pw_dir);
2968 else
2969 return concat (pwent->pw_dir, "/", userend + 1);
2971 else if (userend != 0)
2972 *userend = '/';
2974 #endif /* !AMIGA && !WINDOWS32 */
2975 #endif /* !VMS */
2976 return 0;
2979 /* Given a chain of struct nameseq's describing a sequence of filenames,
2980 in reverse of the intended order, return a new chain describing the
2981 result of globbing the filenames. The new chain is in forward order.
2982 The links of the old chain are freed or used in the new chain.
2983 Likewise for the names in the old chain.
2985 SIZE is how big to construct chain elements.
2986 This is useful if we want them actually to be other structures
2987 that have room for additional info. */
2989 struct nameseq *
2990 multi_glob (struct nameseq *chain, unsigned int size)
2992 extern void dir_setup_glob ();
2993 register struct nameseq *new = 0;
2994 register struct nameseq *old;
2995 struct nameseq *nexto;
2996 glob_t gl;
2998 dir_setup_glob (&gl);
3000 for (old = chain; old != 0; old = nexto)
3002 #ifndef NO_ARCHIVES
3003 char *memname;
3004 #endif
3006 nexto = old->next;
3008 if (old->name[0] == '~')
3010 char *newname = tilde_expand (old->name);
3011 if (newname != 0)
3013 free (old->name);
3014 old->name = newname;
3018 #ifndef NO_ARCHIVES
3019 if (ar_name (old->name))
3021 /* OLD->name is an archive member reference.
3022 Replace it with the archive file name,
3023 and save the member name in MEMNAME.
3024 We will glob on the archive name and then
3025 reattach MEMNAME later. */
3026 char *arname;
3027 ar_parse_name (old->name, &arname, &memname);
3028 free (old->name);
3029 old->name = arname;
3031 else
3032 memname = 0;
3033 #endif /* !NO_ARCHIVES */
3035 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
3037 case 0: /* Success. */
3039 register int i = gl.gl_pathc;
3040 while (i-- > 0)
3042 #ifndef NO_ARCHIVES
3043 if (memname != 0)
3045 /* Try to glob on MEMNAME within the archive. */
3046 struct nameseq *found
3047 = ar_glob (gl.gl_pathv[i], memname, size);
3048 if (found == 0)
3050 /* No matches. Use MEMNAME as-is. */
3051 unsigned int alen = strlen (gl.gl_pathv[i]);
3052 unsigned int mlen = strlen (memname);
3053 struct nameseq *elt
3054 = (struct nameseq *) xmalloc (size);
3055 if (size > sizeof (struct nameseq))
3056 bzero (((char *) elt) + sizeof (struct nameseq),
3057 size - sizeof (struct nameseq));
3058 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
3059 bcopy (gl.gl_pathv[i], elt->name, alen);
3060 elt->name[alen] = '(';
3061 bcopy (memname, &elt->name[alen + 1], mlen);
3062 elt->name[alen + 1 + mlen] = ')';
3063 elt->name[alen + 1 + mlen + 1] = '\0';
3064 elt->next = new;
3065 new = elt;
3067 else
3069 /* Find the end of the FOUND chain. */
3070 struct nameseq *f = found;
3071 while (f->next != 0)
3072 f = f->next;
3074 /* Attach the chain being built to the end of the FOUND
3075 chain, and make FOUND the new NEW chain. */
3076 f->next = new;
3077 new = found;
3080 free (memname);
3082 else
3083 #endif /* !NO_ARCHIVES */
3085 struct nameseq *elt = (struct nameseq *) xmalloc (size);
3086 if (size > sizeof (struct nameseq))
3087 bzero (((char *) elt) + sizeof (struct nameseq),
3088 size - sizeof (struct nameseq));
3089 elt->name = xstrdup (gl.gl_pathv[i]);
3090 elt->next = new;
3091 new = elt;
3094 globfree (&gl);
3095 free (old->name);
3096 free ((char *)old);
3097 break;
3100 case GLOB_NOSPACE:
3101 fatal (NILF, _("virtual memory exhausted"));
3102 break;
3104 default:
3105 old->next = new;
3106 new = old;
3107 break;
3111 return new;