Implement SysV-style $$@ support. I looked at E.Parmelan's patch but
[make.git] / read.c
bloba6d85b257010a5219c2f293272f4c30aeaa631f4
1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include "make.h"
22 #include <assert.h>
24 #include <glob.h>
26 #include "dep.h"
27 #include "filedef.h"
28 #include "job.h"
29 #include "commands.h"
30 #include "variable.h"
31 #include "rule.h"
32 #include "debug.h"
35 #ifndef WINDOWS32
36 #ifndef _AMIGA
37 #ifndef VMS
38 #include <pwd.h>
39 #else
40 struct passwd *getpwnam PARAMS ((char *name));
41 #endif
42 #endif
43 #endif /* !WINDOWS32 */
45 /* A 'struct ebuffer' controls the origin of the makefile we are currently
46 eval'ing.
49 struct ebuffer
51 char *buffer; /* Start of the current line in the buffer. */
52 char *bufnext; /* Start of the next line in the buffer. */
53 char *bufstart; /* Start of the entire buffer. */
54 unsigned int size; /* Malloc'd size of buffer. */
55 FILE *fp; /* File, or NULL if this is an internal buffer. */
56 struct floc floc; /* Info on the file in fp (if any). */
59 /* Types of "words" that can be read in a makefile. */
60 enum make_word_type
62 w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
63 w_comment, w_varassign
67 /* A `struct conditionals' contains the information describing
68 all the active conditionals in a makefile.
70 The global variable `conditionals' contains the conditionals
71 information for the current makefile. It is initialized from
72 the static structure `toplevel_conditionals' and is later changed
73 to new structures for included makefiles. */
75 struct conditionals
77 unsigned int if_cmds; /* Depth of conditional nesting. */
78 unsigned int allocated; /* Elts allocated in following arrays. */
79 char *ignoring; /* Are we ignoring or interepreting? */
80 char *seen_else; /* Have we already seen an `else'? */
83 static struct conditionals toplevel_conditionals;
84 static struct conditionals *conditionals = &toplevel_conditionals;
87 /* Default directories to search for include files in */
89 static char *default_include_directories[] =
91 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
93 * This completely up to the user when they install MSVC or other packages.
94 * This is defined as a placeholder.
96 #define INCLUDEDIR "."
97 #endif
98 INCLUDEDIR,
99 #ifndef _AMIGA
100 "/usr/gnu/include",
101 "/usr/local/include",
102 "/usr/include",
103 #endif
107 /* List of directories to search for include files in */
109 static char **include_directories;
111 /* Maximum length of an element of the above. */
113 static unsigned int max_incl_len;
115 /* The filename and pointer to line number of the
116 makefile currently being read in. */
118 const struct floc *reading_file = 0;
120 /* The chain of makefiles read by read_makefile. */
122 static struct dep *read_makefiles = 0;
124 static int eval_makefile PARAMS ((char *filename, int flags));
125 static int eval PARAMS ((struct ebuffer *buffer, int flags));
127 static long readline PARAMS ((struct ebuffer *ebuf));
128 static void do_define PARAMS ((char *name, unsigned int namelen,
129 enum variable_origin origin,
130 struct ebuffer *ebuf));
131 static int conditional_line PARAMS ((char *line, const struct floc *flocp));
132 static void record_files PARAMS ((struct nameseq *filenames, char *pattern, char *pattern_percent,
133 struct dep *deps, unsigned int cmds_started, char *commands,
134 unsigned int commands_idx, int two_colon,
135 int have_sysv_atvar,
136 const struct floc *flocp, int set_default));
137 static void record_target_var PARAMS ((struct nameseq *filenames, char *defn,
138 int two_colon,
139 enum variable_origin origin,
140 const struct floc *flocp));
141 static enum make_word_type get_next_mword PARAMS ((char *buffer, char *delim,
142 char **startp, unsigned int *length));
144 /* Read in all the makefiles and return the chain of their names. */
146 struct dep *
147 read_all_makefiles (makefiles)
148 char **makefiles;
150 unsigned int num_makefiles = 0;
152 /* Create *_LIST variables, to hold the makefiles, targets, and variables
153 we will be reading. */
155 define_variable ("MAKEFILE_LIST", sizeof ("MAKEFILE_LIST")-1, "", o_file, 0);
157 DB (DB_BASIC, (_("Reading makefiles...\n")));
159 /* If there's a non-null variable MAKEFILES, its value is a list of
160 files to read first thing. But don't let it prevent reading the
161 default makefiles and don't let the default goal come from there. */
164 char *value;
165 char *name, *p;
166 unsigned int length;
169 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
170 int save = warn_undefined_variables_flag;
171 warn_undefined_variables_flag = 0;
173 value = allocated_variable_expand ("$(MAKEFILES)");
175 warn_undefined_variables_flag = save;
178 /* Set NAME to the start of next token and LENGTH to its length.
179 MAKEFILES is updated for finding remaining tokens. */
180 p = value;
182 while ((name = find_next_token (&p, &length)) != 0)
184 if (*p != '\0')
185 *p++ = '\0';
186 name = xstrdup (name);
187 if (eval_makefile (name,
188 RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE) < 2)
189 free (name);
192 free (value);
195 /* Read makefiles specified with -f switches. */
197 if (makefiles != 0)
198 while (*makefiles != 0)
200 struct dep *tail = read_makefiles;
201 register struct dep *d;
203 if (! eval_makefile (*makefiles, 0))
204 perror_with_name ("", *makefiles);
206 /* Find the right element of read_makefiles. */
207 d = read_makefiles;
208 while (d->next != tail)
209 d = d->next;
211 /* Use the storage read_makefile allocates. */
212 *makefiles = dep_name (d);
213 ++num_makefiles;
214 ++makefiles;
217 /* If there were no -f switches, try the default names. */
219 if (num_makefiles == 0)
221 static char *default_makefiles[] =
222 #ifdef VMS
223 /* all lower case since readdir() (the vms version) 'lowercasifies' */
224 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
225 #else
226 #ifdef _AMIGA
227 { "GNUmakefile", "Makefile", "SMakefile", 0 };
228 #else /* !Amiga && !VMS */
229 { "GNUmakefile", "makefile", "Makefile", 0 };
230 #endif /* AMIGA */
231 #endif /* VMS */
232 register char **p = default_makefiles;
233 while (*p != 0 && !file_exists_p (*p))
234 ++p;
236 if (*p != 0)
238 if (! eval_makefile (*p, 0))
239 perror_with_name ("", *p);
241 else
243 /* No default makefile was found. Add the default makefiles to the
244 `read_makefiles' chain so they will be updated if possible. */
245 struct dep *tail = read_makefiles;
246 /* Add them to the tail, after any MAKEFILES variable makefiles. */
247 while (tail != 0 && tail->next != 0)
248 tail = tail->next;
249 for (p = default_makefiles; *p != 0; ++p)
251 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
252 d->name = 0;
253 d->file = enter_file (*p);
254 d->file->dontcare = 1;
255 d->ignore_mtime = 0;
256 /* Tell update_goal_chain to bail out as soon as this file is
257 made, and main not to die if we can't make this file. */
258 d->changed = RM_DONTCARE;
259 if (tail == 0)
260 read_makefiles = d;
261 else
262 tail->next = d;
263 tail = d;
265 if (tail != 0)
266 tail->next = 0;
270 return read_makefiles;
274 eval_makefile (filename, flags)
275 char *filename;
276 int flags;
278 struct dep *deps;
279 struct ebuffer ebuf;
280 const struct floc *curfile;
281 int makefile_errno;
282 int r;
284 ebuf.floc.filenm = filename;
285 ebuf.floc.lineno = 1;
287 if (ISDB (DB_VERBOSE))
289 printf (_("Reading makefile `%s'"), filename);
290 if (flags & RM_NO_DEFAULT_GOAL)
291 printf (_(" (no default goal)"));
292 if (flags & RM_INCLUDED)
293 printf (_(" (search path)"));
294 if (flags & RM_DONTCARE)
295 printf (_(" (don't care)"));
296 if (flags & RM_NO_TILDE)
297 printf (_(" (no ~ expansion)"));
298 puts ("...");
301 /* First, get a stream to read. */
303 /* Expand ~ in FILENAME unless it came from `include',
304 in which case it was already done. */
305 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
307 char *expanded = tilde_expand (filename);
308 if (expanded != 0)
309 filename = expanded;
312 ebuf.fp = fopen (filename, "r");
313 /* Save the error code so we print the right message later. */
314 makefile_errno = errno;
316 /* If the makefile wasn't found and it's either a makefile from
317 the `MAKEFILES' variable or an included makefile,
318 search the included makefile search path for this makefile. */
319 if (ebuf.fp == 0 && (flags & RM_INCLUDED) && *filename != '/')
321 register unsigned int i;
322 for (i = 0; include_directories[i] != 0; ++i)
324 char *name = concat (include_directories[i], "/", filename);
325 ebuf.fp = fopen (name, "r");
326 if (ebuf.fp == 0)
327 free (name);
328 else
330 filename = name;
331 break;
336 /* Add FILENAME to the chain of read makefiles. */
337 deps = (struct dep *) xmalloc (sizeof (struct dep));
338 deps->next = read_makefiles;
339 read_makefiles = deps;
340 deps->name = 0;
341 deps->file = lookup_file (filename);
342 if (deps->file == 0)
344 deps->file = enter_file (xstrdup (filename));
345 if (flags & RM_DONTCARE)
346 deps->file->dontcare = 1;
348 if (filename != ebuf.floc.filenm)
349 free (filename);
350 filename = deps->file->name;
351 deps->changed = flags;
352 deps->ignore_mtime = 0;
354 /* If the makefile can't be found at all, give up entirely. */
356 if (ebuf.fp == 0)
358 /* If we did some searching, errno has the error from the last
359 attempt, rather from FILENAME itself. Restore it in case the
360 caller wants to use it in a message. */
361 errno = makefile_errno;
362 return 0;
365 /* Add this makefile to the list. */
366 do_variable_definition (&ebuf.floc, "MAKEFILE_LIST", filename, o_file,
367 f_append, 0);
369 /* Evaluate the makefile */
371 ebuf.size = 200;
372 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = xmalloc (ebuf.size);
374 curfile = reading_file;
375 reading_file = &ebuf.floc;
377 r = eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
379 reading_file = curfile;
381 return r;
385 eval_buffer (buffer)
386 char *buffer;
388 struct ebuffer ebuf;
389 const struct floc *curfile;
390 int r;
392 /* Evaluate the buffer */
394 ebuf.size = strlen (buffer);
395 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = buffer;
396 ebuf.fp = NULL;
398 ebuf.floc = *reading_file;
400 curfile = reading_file;
401 reading_file = &ebuf.floc;
403 r = eval (&ebuf, 1);
405 reading_file = curfile;
407 return r;
411 /* Read file FILENAME as a makefile and add its contents to the data base.
413 SET_DEFAULT is true if we are allowed to set the default goal.
415 FILENAME is added to the `read_makefiles' chain.
417 Returns 0 if a file was not found or not read.
418 Returns 1 if FILENAME was found and read.
419 Returns 2 if FILENAME was read, and we kept a reference (don't free it). */
421 static int
422 eval (ebuf, set_default)
423 struct ebuffer *ebuf;
424 int set_default;
426 static char *collapsed = 0;
427 static unsigned int collapsed_length = 0;
428 unsigned int commands_len = 200;
429 char *commands;
430 unsigned int commands_idx = 0;
431 unsigned int cmds_started, tgts_started;
432 int ignoring = 0, in_ignored_define = 0;
433 int no_targets = 0; /* Set when reading a rule without targets. */
434 int have_sysv_atvar = 0;
435 struct nameseq *filenames = 0;
436 struct dep *deps = 0;
437 long nlines = 0;
438 int two_colon = 0;
439 char *pattern = 0, *pattern_percent;
440 struct floc *fstart;
441 struct floc fi;
443 #if defined (WINDOWS32) || defined (__MSDOS__)
444 int check_again;
445 #endif
447 #define record_waiting_files() \
448 do \
450 if (filenames != 0) \
452 fi.lineno = tgts_started; \
453 record_files (filenames, pattern, pattern_percent, deps, \
454 cmds_started, commands, commands_idx, two_colon, \
455 have_sysv_atvar, &fi, set_default); \
457 filenames = 0; \
458 commands_idx = 0; \
459 no_targets = 0; \
460 if (pattern) { free(pattern); pattern = 0; } \
461 } while (0)
463 pattern_percent = 0;
464 cmds_started = tgts_started = 1;
466 fstart = &ebuf->floc;
467 fi.filenm = ebuf->floc.filenm;
469 /* Loop over lines in the file.
470 The strategy is to accumulate target names in FILENAMES, dependencies
471 in DEPS and commands in COMMANDS. These are used to define a rule
472 when the start of the next rule (or eof) is encountered.
474 When you see a "continue" in the loop below, that means we are moving on
475 to the next line _without_ ending any rule that we happen to be working
476 with at the moment. If you see a "goto rule_complete", then the
477 statement we just parsed also finishes the previous rule. */
479 commands = xmalloc (200);
481 while (1)
483 int linelen;
484 char *line;
485 int len;
486 char *p;
487 char *p2;
489 /* Grab the next line to be evaluated */
490 ebuf->floc.lineno += nlines;
491 nlines = readline (ebuf);
493 /* If there is nothing left to eval, we're done. */
494 if (nlines < 0)
495 break;
497 /* If this line is empty, skip it. */
498 line = ebuf->buffer;
499 if (line[0] == '\0')
500 continue;
502 linelen = strlen (line);
504 /* Check for a shell command line first.
505 If it is not one, we can stop treating tab specially. */
506 if (line[0] == '\t')
508 if (no_targets)
509 /* Ignore the commands in a rule with no targets. */
510 continue;
512 /* If there is no preceding rule line, don't treat this line
513 as a command, even though it begins with a tab character.
514 SunOS 4 make appears to behave this way. */
516 if (filenames != 0)
518 if (ignoring)
519 /* Yep, this is a shell command, and we don't care. */
520 continue;
522 /* Append this command line to the line being accumulated. */
523 if (commands_idx == 0)
524 cmds_started = ebuf->floc.lineno;
526 if (linelen + 1 + commands_idx > commands_len)
528 commands_len = (linelen + 1 + commands_idx) * 2;
529 commands = xrealloc (commands, commands_len);
531 bcopy (line, &commands[commands_idx], linelen);
532 commands_idx += linelen;
533 commands[commands_idx++] = '\n';
535 continue;
539 /* This line is not a shell command line. Don't worry about tabs. */
541 if (collapsed_length < linelen+1)
543 collapsed_length = linelen+1;
544 if (collapsed != 0)
545 free (collapsed);
546 collapsed = (char *) xmalloc (collapsed_length);
548 strcpy (collapsed, line);
549 /* Collapse continuation lines. */
550 collapse_continuations (collapsed);
551 remove_comments (collapsed);
553 /* Compare a word, both length and contents. */
554 #define word1eq(s) (len == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
555 p = collapsed;
556 while (isspace ((unsigned char)*p))
557 ++p;
559 if (*p == '\0')
560 /* This line is completely empty--ignore it. */
561 continue;
563 /* Find the end of the first token. Note we don't need to worry about
564 * ":" here since we compare tokens by length (so "export" will never
565 * be equal to "export:").
567 for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
569 len = p2 - p;
571 /* Find the start of the second token. If it looks like a target or
572 variable definition it can't be a preprocessor token so skip
573 them--this allows variables/targets named `ifdef', `export', etc. */
574 while (isspace ((unsigned char)*p2))
575 ++p2;
577 if ((p2[0] == ':' || p2[0] == '+' || p2[0] == '=') && p2[1] == '\0')
579 /* It can't be a preprocessor token so skip it if we're ignoring */
580 if (ignoring)
581 continue;
583 goto skip_conditionals;
586 /* We must first check for conditional and `define' directives before
587 ignoring anything, since they control what we will do with
588 following lines. */
590 if (!in_ignored_define
591 && (word1eq ("ifdef") || word1eq ("ifndef")
592 || word1eq ("ifeq") || word1eq ("ifneq")
593 || word1eq ("else") || word1eq ("endif")))
595 int i = conditional_line (p, fstart);
596 if (i < 0)
597 fatal (fstart, _("invalid syntax in conditional"));
599 ignoring = i;
600 continue;
603 if (word1eq ("endef"))
605 if (!in_ignored_define)
606 fatal (fstart, _("extraneous `endef'"));
607 in_ignored_define = 0;
608 continue;
611 if (word1eq ("define"))
613 if (ignoring)
614 in_ignored_define = 1;
615 else
617 if (*p2 == '\0')
618 fatal (fstart, _("empty variable name"));
620 /* Let the variable name be the whole rest of the line,
621 with trailing blanks stripped (comments have already been
622 removed), so it could be a complex variable/function
623 reference that might contain blanks. */
624 p = strchr (p2, '\0');
625 while (isblank ((unsigned char)p[-1]))
626 --p;
627 do_define (p2, p - p2, o_file, ebuf);
629 continue;
632 if (word1eq ("override"))
634 if (*p2 == '\0')
635 error (fstart, _("empty `override' directive"));
637 if (strneq (p2, "define", 6)
638 && (isblank ((unsigned char)p2[6]) || p2[6] == '\0'))
640 if (ignoring)
641 in_ignored_define = 1;
642 else
644 p2 = next_token (p2 + 6);
645 if (*p2 == '\0')
646 fatal (fstart, _("empty variable name"));
648 /* Let the variable name be the whole rest of the line,
649 with trailing blanks stripped (comments have already been
650 removed), so it could be a complex variable/function
651 reference that might contain blanks. */
652 p = strchr (p2, '\0');
653 while (isblank ((unsigned char)p[-1]))
654 --p;
655 do_define (p2, p - p2, o_override, ebuf);
658 else if (!ignoring
659 && !try_variable_definition (fstart, p2, o_override, 0))
660 error (fstart, _("invalid `override' directive"));
662 continue;
665 if (ignoring)
666 /* Ignore the line. We continue here so conditionals
667 can appear in the middle of a rule. */
668 continue;
670 if (word1eq ("export"))
672 /* 'export' by itself causes everything to be exported. */
673 if (*p2 == '\0')
674 export_all_variables = 1;
675 else
677 struct variable *v;
679 v = try_variable_definition (fstart, p2, o_file, 0);
680 if (v != 0)
681 v->export = v_export;
682 else
684 unsigned int len;
685 for (p = find_next_token (&p2, &len); p != 0;
686 p = find_next_token (&p2, &len))
688 v = lookup_variable (p, len);
689 if (v == 0)
690 v = define_variable_loc (p, len, "", o_file, 0,
691 fstart);
692 v->export = v_export;
696 goto rule_complete;
699 if (word1eq ("unexport"))
701 if (*p2 == '\0')
702 export_all_variables = 0;
703 else
705 unsigned int len;
706 struct variable *v;
707 for (p = find_next_token (&p2, &len); p != 0;
708 p = find_next_token (&p2, &len))
710 v = lookup_variable (p, len);
711 if (v == 0)
712 v = define_variable_loc (p, len, "", o_file, 0, fstart);
713 v->export = v_noexport;
716 goto rule_complete;
719 skip_conditionals:
720 if (word1eq ("vpath"))
722 char *pattern;
723 unsigned int len;
724 p2 = variable_expand (p2);
725 p = find_next_token (&p2, &len);
726 if (p != 0)
728 pattern = savestring (p, len);
729 p = find_next_token (&p2, &len);
730 /* No searchpath means remove all previous
731 selective VPATH's with the same pattern. */
733 else
734 /* No pattern means remove all previous selective VPATH's. */
735 pattern = 0;
736 construct_vpath_list (pattern, p);
737 if (pattern != 0)
738 free (pattern);
740 goto rule_complete;
743 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
745 /* We have found an `include' line specifying a nested
746 makefile to be read at this point. */
747 struct conditionals *save;
748 struct conditionals new_conditionals;
749 struct nameseq *files;
750 /* "-include" (vs "include") says no error if the file does not
751 exist. "sinclude" is an alias for this from SGI. */
752 int noerror = (p[0] != 'i');
754 p = allocated_variable_expand (p2);
755 if (*p == '\0')
757 error (fstart,
758 _("no file name for `%sinclude'"), noerror ? "-" : "");
759 continue;
762 /* Parse the list of file names. */
763 p2 = p;
764 files = multi_glob (parse_file_seq (&p2, '\0',
765 sizeof (struct nameseq),
767 sizeof (struct nameseq));
768 free (p);
770 /* Save the state of conditionals and start
771 the included makefile with a clean slate. */
772 save = conditionals;
773 bzero ((char *) &new_conditionals, sizeof new_conditionals);
774 conditionals = &new_conditionals;
776 /* Record the rules that are waiting so they will determine
777 the default goal before those in the included makefile. */
778 record_waiting_files ();
780 /* Read each included makefile. */
781 while (files != 0)
783 struct nameseq *next = files->next;
784 char *name = files->name;
785 int r;
787 free ((char *)files);
788 files = next;
790 r = eval_makefile (name, (RM_INCLUDED | RM_NO_TILDE
791 | (noerror ? RM_DONTCARE : 0)));
792 if (!r)
794 if (!noerror)
795 error (fstart, "%s: %s", name, strerror (errno));
796 free (name);
800 /* Free any space allocated by conditional_line. */
801 if (conditionals->ignoring)
802 free (conditionals->ignoring);
803 if (conditionals->seen_else)
804 free (conditionals->seen_else);
806 /* Restore state. */
807 conditionals = save;
809 goto rule_complete;
812 if (try_variable_definition (fstart, p, o_file, 0))
813 /* This line has been dealt with. */
814 goto rule_complete;
816 if (line[0] == '\t')
818 p = collapsed; /* Ignore comments, etc. */
819 while (isblank ((unsigned char)*p))
820 ++p;
821 if (*p == '\0')
822 /* The line is completely blank; that is harmless. */
823 continue;
825 /* This line starts with a tab but was not caught above
826 because there was no preceding target, and the line
827 might have been usable as a variable definition.
828 But now we know it is definitely lossage. */
829 fatal(fstart, _("commands commence before first target"));
832 /* This line describes some target files. This is complicated by
833 the existence of target-specific variables, because we can't
834 expand the entire line until we know if we have one or not. So
835 we expand the line word by word until we find the first `:',
836 then check to see if it's a target-specific variable.
838 In this algorithm, `lb_next' will point to the beginning of the
839 unexpanded parts of the input buffer, while `p2' points to the
840 parts of the expanded buffer we haven't searched yet. */
843 enum make_word_type wtype;
844 enum variable_origin v_origin;
845 char *cmdleft, *semip, *lb_next;
846 unsigned int len, plen = 0;
847 char *colonp;
849 /* Record the previous rule. */
851 record_waiting_files ();
852 tgts_started = fstart->lineno;
854 /* Search the line for an unquoted ; that is not after an
855 unquoted #. */
856 cmdleft = find_char_unquote (line, ";#", 0);
857 if (cmdleft != 0 && *cmdleft == '#')
859 /* We found a comment before a semicolon. */
860 *cmdleft = '\0';
861 cmdleft = 0;
863 else if (cmdleft != 0)
864 /* Found one. Cut the line short there before expanding it. */
865 *(cmdleft++) = '\0';
866 semip = cmdleft;
868 collapse_continuations (line);
870 /* We can't expand the entire line, since if it's a per-target
871 variable we don't want to expand it. So, walk from the
872 beginning, expanding as we go, and looking for "interesting"
873 chars. The first word is always expandable. */
874 wtype = get_next_mword(line, NULL, &lb_next, &len);
875 switch (wtype)
877 case w_eol:
878 if (cmdleft != 0)
879 fatal(fstart, _("missing rule before commands"));
880 /* This line contained something but turned out to be nothing
881 but whitespace (a comment?). */
882 continue;
884 case w_colon:
885 case w_dcolon:
886 /* We accept and ignore rules without targets for
887 compatibility with SunOS 4 make. */
888 no_targets = 1;
889 continue;
891 default:
892 break;
895 p2 = variable_expand_string(NULL, lb_next, len);
896 while (1)
898 lb_next += len;
899 if (cmdleft == 0)
901 /* Look for a semicolon in the expanded line. */
902 cmdleft = find_char_unquote (p2, ";", 0);
904 if (cmdleft != 0)
906 unsigned long p2_off = p2 - variable_buffer;
907 unsigned long cmd_off = cmdleft - variable_buffer;
908 char *pend = p2 + strlen(p2);
910 /* Append any remnants of lb, then cut the line short
911 at the semicolon. */
912 *cmdleft = '\0';
914 /* One school of thought says that you shouldn't expand
915 here, but merely copy, since now you're beyond a ";"
916 and into a command script. However, the old parser
917 expanded the whole line, so we continue that for
918 backwards-compatiblity. Also, it wouldn't be
919 entirely consistent, since we do an unconditional
920 expand below once we know we don't have a
921 target-specific variable. */
922 (void)variable_expand_string(pend, lb_next, (long)-1);
923 lb_next += strlen(lb_next);
924 p2 = variable_buffer + p2_off;
925 cmdleft = variable_buffer + cmd_off + 1;
929 colonp = find_char_unquote(p2, ":", 0);
930 #if defined(__MSDOS__) || defined(WINDOWS32)
931 /* The drive spec brain-damage strikes again... */
932 /* Note that the only separators of targets in this context
933 are whitespace and a left paren. If others are possible,
934 they should be added to the string in the call to index. */
935 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
936 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
937 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
938 colonp = find_char_unquote(colonp + 1, ":", 0);
939 #endif
940 if (colonp != 0)
941 break;
943 wtype = get_next_mword(lb_next, NULL, &lb_next, &len);
944 if (wtype == w_eol)
945 break;
947 p2 += strlen(p2);
948 *(p2++) = ' ';
949 p2 = variable_expand_string(p2, lb_next, len);
950 /* We don't need to worry about cmdleft here, because if it was
951 found in the variable_buffer the entire buffer has already
952 been expanded... we'll never get here. */
955 p2 = next_token (variable_buffer);
957 /* If the word we're looking at is EOL, see if there's _anything_
958 on the line. If not, a variable expanded to nothing, so ignore
959 it. If so, we can't parse this line so punt. */
960 if (wtype == w_eol)
962 if (*p2 != '\0')
963 /* There's no need to be ivory-tower about this: check for
964 one of the most common bugs found in makefiles... */
965 fatal (fstart, _("missing separator%s"),
966 !strneq(line, " ", 8) ? ""
967 : _(" (did you mean TAB instead of 8 spaces?)"));
968 continue;
971 /* Make the colon the end-of-string so we know where to stop
972 looking for targets. */
973 *colonp = '\0';
974 filenames = multi_glob (parse_file_seq (&p2, '\0',
975 sizeof (struct nameseq),
977 sizeof (struct nameseq));
978 *p2 = ':';
980 if (!filenames)
982 /* We accept and ignore rules without targets for
983 compatibility with SunOS 4 make. */
984 no_targets = 1;
985 continue;
987 /* This should never be possible; we handled it above. */
988 assert (*p2 != '\0');
989 ++p2;
991 /* Is this a one-colon or two-colon entry? */
992 two_colon = *p2 == ':';
993 if (two_colon)
994 p2++;
996 /* Test to see if it's a target-specific variable. Copy the rest
997 of the buffer over, possibly temporarily (we'll expand it later
998 if it's not a target-specific variable). PLEN saves the length
999 of the unparsed section of p2, for later. */
1000 if (*lb_next != '\0')
1002 unsigned int l = p2 - variable_buffer;
1003 plen = strlen (p2);
1004 (void) variable_buffer_output (p2+plen,
1005 lb_next, strlen (lb_next)+1);
1006 p2 = variable_buffer + l;
1009 /* See if it's an "override" keyword; if so see if what comes after
1010 it looks like a variable definition. */
1012 wtype = get_next_mword (p2, NULL, &p, &len);
1014 v_origin = o_file;
1015 if (wtype == w_static && word1eq ("override"))
1017 v_origin = o_override;
1018 wtype = get_next_mword (p+len, NULL, &p, &len);
1021 if (wtype != w_eol)
1022 wtype = get_next_mword (p+len, NULL, NULL, NULL);
1024 if (wtype == w_varassign)
1026 /* If there was a semicolon found, add it back, plus anything
1027 after it. */
1028 if (semip)
1030 *(--semip) = ';';
1031 variable_buffer_output (p2 + strlen (p2),
1032 semip, strlen (semip)+1);
1034 record_target_var (filenames, p, two_colon, v_origin, fstart);
1035 filenames = 0;
1036 continue;
1039 /* This is a normal target, _not_ a target-specific variable.
1040 Unquote any = in the dependency list. */
1041 find_char_unquote (lb_next, "=", 0);
1043 /* We have some targets, so don't ignore the following commands. */
1044 no_targets = 0;
1046 /* Expand the dependencies, etc. */
1047 if (*lb_next != '\0')
1049 unsigned int l = p2 - variable_buffer;
1050 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
1051 p2 = variable_buffer + l;
1053 /* Look for a semicolon in the expanded line. */
1054 if (cmdleft == 0)
1056 cmdleft = find_char_unquote (p2, ";", 0);
1057 if (cmdleft != 0)
1058 *(cmdleft++) = '\0';
1062 /* Do any of the prerequisites appear to have $@ etc.? */
1063 have_sysv_atvar = 0;
1064 if (!posix_pedantic)
1065 for (p = strchr (p2, '$'); p != 0; p = strchr (p+1, '$'))
1066 if (p[1] == '@' || (p[1] == '(' && p[2] == '@'))
1068 have_sysv_atvar = 1;
1069 break;
1072 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
1073 p = strchr (p2, ':');
1074 while (p != 0 && p[-1] == '\\')
1076 register char *q = &p[-1];
1077 register int backslash = 0;
1078 while (*q-- == '\\')
1079 backslash = !backslash;
1080 if (backslash)
1081 p = strchr (p + 1, ':');
1082 else
1083 break;
1085 #ifdef _AMIGA
1086 /* Here, the situation is quite complicated. Let's have a look
1087 at a couple of targets:
1089 install: dev:make
1091 dev:make: make
1093 dev:make:: xyz
1095 The rule is that it's only a target, if there are TWO :'s
1096 OR a space around the :.
1098 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
1099 || isspace ((unsigned char)p[-1])))
1100 p = 0;
1101 #endif
1102 #if defined (WINDOWS32) || defined (__MSDOS__)
1103 do {
1104 check_again = 0;
1105 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
1106 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
1107 isalpha ((unsigned char)p[-1]) &&
1108 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1109 p = strchr (p + 1, ':');
1110 check_again = 1;
1112 } while (check_again);
1113 #endif
1114 if (p != 0)
1116 struct nameseq *target;
1117 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
1118 ++p2;
1119 if (target == 0)
1120 fatal (fstart, _("missing target pattern"));
1121 else if (target->next != 0)
1122 fatal (fstart, _("multiple target patterns"));
1123 pattern = target->name;
1124 pattern_percent = find_percent (pattern);
1125 if (pattern_percent == 0)
1126 fatal (fstart, _("target pattern contains no `%%'"));
1127 free((char *)target);
1129 else
1130 pattern = 0;
1132 /* Parse the dependencies. */
1133 deps = (struct dep *)
1134 multi_glob (parse_file_seq (&p2, '|', sizeof (struct dep), 1),
1135 sizeof (struct dep));
1136 if (*p2)
1138 /* Files that follow '|' are special prerequisites that
1139 need only exist in order to satisfy the dependency.
1140 Their modification times are irrelevant. */
1141 struct dep **deps_ptr = &deps;
1142 struct dep *d;
1143 for (deps_ptr = &deps; *deps_ptr; deps_ptr = &(*deps_ptr)->next)
1145 ++p2;
1146 *deps_ptr = (struct dep *)
1147 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
1148 sizeof (struct dep));
1149 for (d = *deps_ptr; d != 0; d = d->next)
1150 d->ignore_mtime = 1;
1153 commands_idx = 0;
1154 if (cmdleft != 0)
1156 /* Semicolon means rest of line is a command. */
1157 unsigned int len = strlen (cmdleft);
1159 cmds_started = fstart->lineno;
1161 /* Add this command line to the buffer. */
1162 if (len + 2 > commands_len)
1164 commands_len = (len + 2) * 2;
1165 commands = (char *) xrealloc (commands, commands_len);
1167 bcopy (cmdleft, commands, len);
1168 commands_idx += len;
1169 commands[commands_idx++] = '\n';
1172 continue;
1175 /* We get here except in the case that we just read a rule line.
1176 Record now the last rule we read, so following spurious
1177 commands are properly diagnosed. */
1178 rule_complete:
1179 record_waiting_files ();
1182 #undef word1eq
1184 if (conditionals->if_cmds)
1185 fatal (fstart, _("missing `endif'"));
1187 /* At eof, record the last rule. */
1188 record_waiting_files ();
1190 free ((char *) commands);
1192 return 1;
1196 /* Execute a `define' directive.
1197 The first line has already been read, and NAME is the name of
1198 the variable to be defined. The following lines remain to be read. */
1200 static void
1201 do_define (name, namelen, origin, ebuf)
1202 char *name;
1203 unsigned int namelen;
1204 enum variable_origin origin;
1205 struct ebuffer *ebuf;
1207 struct floc defstart;
1208 long nlines = 0;
1209 int nlevels = 1;
1210 unsigned int length = 100;
1211 char *definition = (char *) xmalloc (length);
1212 unsigned int idx = 0;
1213 char *p;
1215 /* Expand the variable name. */
1216 char *var = (char *) alloca (namelen + 1);
1217 bcopy (name, var, namelen);
1218 var[namelen] = '\0';
1219 var = variable_expand (var);
1221 defstart = ebuf->floc;
1223 while (1)
1225 unsigned int len;
1226 char *line;
1228 ebuf->floc.lineno += nlines;
1229 nlines = readline (ebuf);
1231 /* If there is nothing left to eval, we're done. */
1232 if (nlines < 0)
1233 break;
1235 line = ebuf->buffer;
1237 collapse_continuations (line);
1239 /* If the line doesn't begin with a tab, test to see if it introduces
1240 another define, or ends one. */
1242 /* Stop if we find an 'endef' */
1243 if (line[0] != '\t')
1245 p = next_token (line);
1246 len = strlen (p);
1248 /* If this is another 'define', increment the level count. */
1249 if ((len == 6 || (len > 6 && isblank ((unsigned char)p[6])))
1250 && strneq (p, "define", 6))
1251 ++nlevels;
1253 /* If this is an 'endef', decrement the count. If it's now 0,
1254 we've found the last one. */
1255 else if ((len == 5 || (len > 5 && isblank ((unsigned char)p[5])))
1256 && strneq (p, "endef", 5))
1258 p += 5;
1259 remove_comments (p);
1260 if (*next_token (p) != '\0')
1261 error (&ebuf->floc,
1262 _("Extraneous text after `endef' directive"));
1264 if (--nlevels == 0)
1266 /* Define the variable. */
1267 if (idx == 0)
1268 definition[0] = '\0';
1269 else
1270 definition[idx - 1] = '\0';
1272 /* Always define these variables in the global set. */
1273 define_variable_global (var, strlen (var), definition,
1274 origin, 1, &defstart);
1275 free (definition);
1276 return;
1281 /* Otherwise add this line to the variable definition. */
1282 len = strlen (line);
1283 if (idx + len + 1 > length)
1285 length = (idx + len) * 2;
1286 definition = (char *) xrealloc (definition, length + 1);
1289 bcopy (line, &definition[idx], len);
1290 idx += len;
1291 /* Separate lines with a newline. */
1292 definition[idx++] = '\n';
1295 /* No `endef'!! */
1296 fatal (&defstart, _("missing `endef', unterminated `define'"));
1298 /* NOTREACHED */
1299 return;
1302 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1303 "ifneq", "else" and "endif".
1304 LINE is the input line, with the command as its first word.
1306 FILENAME and LINENO are the filename and line number in the
1307 current makefile. They are used for error messages.
1309 Value is -1 if the line is invalid,
1310 0 if following text should be interpreted,
1311 1 if following text should be ignored. */
1313 static int
1314 conditional_line (line, flocp)
1315 char *line;
1316 const struct floc *flocp;
1318 int notdef;
1319 char *cmdname;
1320 register unsigned int i;
1322 if (*line == 'i')
1324 /* It's an "if..." command. */
1325 notdef = line[2] == 'n';
1326 if (notdef)
1328 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
1329 line += cmdname[3] == 'd' ? 7 : 6;
1331 else
1333 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
1334 line += cmdname[2] == 'd' ? 6 : 5;
1337 else
1339 /* It's an "else" or "endif" command. */
1340 notdef = line[1] == 'n';
1341 cmdname = notdef ? "endif" : "else";
1342 line += notdef ? 5 : 4;
1345 line = next_token (line);
1347 if (*cmdname == 'e')
1349 if (*line != '\0')
1350 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1351 /* "Else" or "endif". */
1352 if (conditionals->if_cmds == 0)
1353 fatal (flocp, _("extraneous `%s'"), cmdname);
1354 /* NOTDEF indicates an `endif' command. */
1355 if (notdef)
1356 --conditionals->if_cmds;
1357 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1358 fatal (flocp, _("only one `else' per conditional"));
1359 else
1361 /* Toggle the state of ignorance. */
1362 conditionals->ignoring[conditionals->if_cmds - 1]
1363 = !conditionals->ignoring[conditionals->if_cmds - 1];
1364 /* Record that we have seen an `else' in this conditional.
1365 A second `else' will be erroneous. */
1366 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1368 for (i = 0; i < conditionals->if_cmds; ++i)
1369 if (conditionals->ignoring[i])
1370 return 1;
1371 return 0;
1374 if (conditionals->allocated == 0)
1376 conditionals->allocated = 5;
1377 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1378 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1381 ++conditionals->if_cmds;
1382 if (conditionals->if_cmds > conditionals->allocated)
1384 conditionals->allocated += 5;
1385 conditionals->ignoring = (char *)
1386 xrealloc (conditionals->ignoring, conditionals->allocated);
1387 conditionals->seen_else = (char *)
1388 xrealloc (conditionals->seen_else, conditionals->allocated);
1391 /* Record that we have seen an `if...' but no `else' so far. */
1392 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1394 /* Search through the stack to see if we're already ignoring. */
1395 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1396 if (conditionals->ignoring[i])
1398 /* We are already ignoring, so just push a level
1399 to match the next "else" or "endif", and keep ignoring.
1400 We don't want to expand variables in the condition. */
1401 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1402 return 1;
1405 if (cmdname[notdef ? 3 : 2] == 'd')
1407 /* "Ifdef" or "ifndef". */
1408 struct variable *v;
1409 register char *p = end_of_token (line);
1410 i = p - line;
1411 p = next_token (p);
1412 if (*p != '\0')
1413 return -1;
1414 v = lookup_variable (line, i);
1415 conditionals->ignoring[conditionals->if_cmds - 1]
1416 = (v != 0 && *v->value != '\0') == notdef;
1418 else
1420 /* "Ifeq" or "ifneq". */
1421 char *s1, *s2;
1422 unsigned int len;
1423 char termin = *line == '(' ? ',' : *line;
1425 if (termin != ',' && termin != '"' && termin != '\'')
1426 return -1;
1428 s1 = ++line;
1429 /* Find the end of the first string. */
1430 if (termin == ',')
1432 register int count = 0;
1433 for (; *line != '\0'; ++line)
1434 if (*line == '(')
1435 ++count;
1436 else if (*line == ')')
1437 --count;
1438 else if (*line == ',' && count <= 0)
1439 break;
1441 else
1442 while (*line != '\0' && *line != termin)
1443 ++line;
1445 if (*line == '\0')
1446 return -1;
1448 if (termin == ',')
1450 /* Strip blanks after the first string. */
1451 char *p = line++;
1452 while (isblank ((unsigned char)p[-1]))
1453 --p;
1454 *p = '\0';
1456 else
1457 *line++ = '\0';
1459 s2 = variable_expand (s1);
1460 /* We must allocate a new copy of the expanded string because
1461 variable_expand re-uses the same buffer. */
1462 len = strlen (s2);
1463 s1 = (char *) alloca (len + 1);
1464 bcopy (s2, s1, len + 1);
1466 if (termin != ',')
1467 /* Find the start of the second string. */
1468 line = next_token (line);
1470 termin = termin == ',' ? ')' : *line;
1471 if (termin != ')' && termin != '"' && termin != '\'')
1472 return -1;
1474 /* Find the end of the second string. */
1475 if (termin == ')')
1477 register int count = 0;
1478 s2 = next_token (line);
1479 for (line = s2; *line != '\0'; ++line)
1481 if (*line == '(')
1482 ++count;
1483 else if (*line == ')')
1485 if (count <= 0)
1486 break;
1487 else
1488 --count;
1492 else
1494 ++line;
1495 s2 = line;
1496 while (*line != '\0' && *line != termin)
1497 ++line;
1500 if (*line == '\0')
1501 return -1;
1503 *line = '\0';
1504 line = next_token (++line);
1505 if (*line != '\0')
1506 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1508 s2 = variable_expand (s2);
1509 conditionals->ignoring[conditionals->if_cmds - 1]
1510 = streq (s1, s2) == notdef;
1513 /* Search through the stack to see if we're ignoring. */
1514 for (i = 0; i < conditionals->if_cmds; ++i)
1515 if (conditionals->ignoring[i])
1516 return 1;
1517 return 0;
1520 /* Remove duplicate dependencies in CHAIN. */
1522 void
1523 uniquize_deps (chain)
1524 struct dep *chain;
1526 register struct dep *d;
1528 /* Make sure that no dependencies are repeated. This does not
1529 really matter for the purpose of updating targets, but it
1530 might make some names be listed twice for $^ and $?. */
1532 for (d = chain; d != 0; d = d->next)
1534 struct dep *last, *next;
1536 last = d;
1537 next = d->next;
1538 while (next != 0)
1539 if (streq (dep_name (d), dep_name (next)))
1541 struct dep *n = next->next;
1542 /* If ignore_mtimes are not equal, one of these is an order-only
1543 prerequisite and one isn't. That means that we should remove
1544 the one that isn't and keep the one that is. Ideally we'd
1545 like to keep the normal one always but that's hard, and
1546 probably not very important, so just remove the second one and
1547 force the first one to be normal. */
1548 if (d->ignore_mtime != next->ignore_mtime)
1549 d->ignore_mtime = 0;
1550 last->next = n;
1551 if (next->name != 0 && next->name != d->name)
1552 free (next->name);
1553 if (next != d)
1554 free ((char *) next);
1555 next = n;
1557 else
1559 last = next;
1560 next = next->next;
1565 /* Record target-specific variable values for files FILENAMES.
1566 TWO_COLON is nonzero if a double colon was used.
1568 The links of FILENAMES are freed, and so are any names in it
1569 that are not incorporated into other data structures.
1571 If the target is a pattern, add the variable to the pattern-specific
1572 variable value list. */
1574 static void
1575 record_target_var (filenames, defn, two_colon, origin, flocp)
1576 struct nameseq *filenames;
1577 char *defn;
1578 int two_colon;
1579 enum variable_origin origin;
1580 const struct floc *flocp;
1582 struct nameseq *nextf;
1583 struct variable_set_list *global;
1585 global = current_variable_set_list;
1587 /* If the variable is an append version, store that but treat it as a
1588 normal recursive variable. */
1590 for (; filenames != 0; filenames = nextf)
1592 struct variable *v;
1593 register char *name = filenames->name;
1594 struct variable_set_list *vlist;
1595 char *fname;
1596 char *percent;
1598 nextf = filenames->next;
1599 free ((char *) filenames);
1601 /* If it's a pattern target, then add it to the pattern-specific
1602 variable list. */
1603 percent = find_percent (name);
1604 if (percent)
1606 struct pattern_var *p;
1608 /* Get a reference for this pattern-specific variable struct. */
1609 p = create_pattern_var(name, percent);
1610 vlist = p->vars;
1611 fname = p->target;
1613 else
1615 struct file *f;
1617 /* Get a file reference for this file, and initialize it. */
1618 f = enter_file (name);
1619 initialize_file_variables (f, 1);
1620 vlist = f->variables;
1621 fname = f->name;
1624 /* Make the new variable context current and define the variable. */
1625 current_variable_set_list = vlist;
1626 v = try_variable_definition (flocp, defn, origin, 1);
1627 if (!v)
1628 error (flocp, _("Malformed per-target variable definition"));
1629 v->per_target = 1;
1631 /* If it's not an override, check to see if there was a command-line
1632 setting. If so, reset the value. */
1633 if (origin != o_override)
1635 struct variable *gv;
1636 int len = strlen(v->name);
1638 current_variable_set_list = global;
1639 gv = lookup_variable (v->name, len);
1640 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1642 v = define_variable_in_set (v->name, len, gv->value, gv->origin,
1643 gv->recursive, vlist->set, flocp);
1644 v->append = 0;
1648 /* Free name if not needed further. */
1649 if (name != fname && (name < fname || name > fname + strlen (fname)))
1650 free (name);
1653 current_variable_set_list = global;
1656 /* Record a description line for files FILENAMES,
1657 with dependencies DEPS, commands to execute described
1658 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1659 TWO_COLON is nonzero if a double colon was used.
1660 If not nil, PATTERN is the `%' pattern to make this
1661 a static pattern rule, and PATTERN_PERCENT is a pointer
1662 to the `%' within it.
1664 The links of FILENAMES are freed, and so are any names in it
1665 that are not incorporated into other data structures. */
1667 static void
1668 record_files (filenames, pattern, pattern_percent, deps, cmds_started,
1669 commands, commands_idx, two_colon, have_sysv_atvar,
1670 flocp, set_default)
1671 struct nameseq *filenames;
1672 char *pattern, *pattern_percent;
1673 struct dep *deps;
1674 unsigned int cmds_started;
1675 char *commands;
1676 unsigned int commands_idx;
1677 int two_colon;
1678 int have_sysv_atvar;
1679 const struct floc *flocp;
1680 int set_default;
1682 struct nameseq *nextf;
1683 int implicit = 0;
1684 unsigned int max_targets = 0, target_idx = 0;
1685 char **targets = 0, **target_percents = 0;
1686 struct commands *cmds;
1688 if (commands_idx > 0)
1690 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1691 cmds->fileinfo.filenm = flocp->filenm;
1692 cmds->fileinfo.lineno = cmds_started;
1693 cmds->commands = savestring (commands, commands_idx);
1694 cmds->command_lines = 0;
1696 else
1697 cmds = 0;
1699 for (; filenames != 0; filenames = nextf)
1701 char *name = filenames->name;
1702 struct file *f;
1703 struct dep *d;
1704 struct dep *this;
1705 char *implicit_percent;
1707 nextf = filenames->next;
1708 free (filenames);
1710 /* Check for .POSIX. We used to do this in snap_deps() but that's not
1711 good enough: it doesn't happen until after the makefile is read,
1712 which means we cannot use its value during parsing. */
1714 if (streq (name, ".POSIX"))
1715 posix_pedantic = 1;
1717 implicit_percent = find_percent (name);
1718 implicit |= implicit_percent != 0;
1720 if (implicit && pattern != 0)
1721 fatal (flocp, _("mixed implicit and static pattern rules"));
1723 if (implicit && implicit_percent == 0)
1724 fatal (flocp, _("mixed implicit and normal rules"));
1726 if (implicit)
1728 if (targets == 0)
1730 max_targets = 5;
1731 targets = (char **) xmalloc (5 * sizeof (char *));
1732 target_percents = (char **) xmalloc (5 * sizeof (char *));
1733 target_idx = 0;
1735 else if (target_idx == max_targets - 1)
1737 max_targets += 5;
1738 targets = (char **) xrealloc ((char *) targets,
1739 max_targets * sizeof (char *));
1740 target_percents
1741 = (char **) xrealloc ((char *) target_percents,
1742 max_targets * sizeof (char *));
1744 targets[target_idx] = name;
1745 target_percents[target_idx] = implicit_percent;
1746 ++target_idx;
1747 continue;
1750 /* If there are multiple filenames, copy the chain DEPS
1751 for all but the last one. It is not safe for the same deps
1752 to go in more than one place in the data base. */
1753 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1755 if (pattern != 0)
1757 /* If this is an extended static rule:
1758 `targets: target%pattern: dep%pattern; cmds',
1759 translate each dependency pattern into a plain filename
1760 using the target pattern and this target's name. */
1761 if (!pattern_matches (pattern, pattern_percent, name))
1763 /* Give a warning if the rule is meaningless. */
1764 error (flocp,
1765 _("target `%s' doesn't match the target pattern"), name);
1766 this = 0;
1768 else
1770 /* We use patsubst_expand to do the work of translating
1771 the target pattern, the target's name and the dependencies'
1772 patterns into plain dependency names. */
1773 char *buffer = variable_expand ("");
1775 for (d = this; d != 0; d = d->next)
1777 char *o;
1778 char *percent = find_percent (d->name);
1779 if (percent == 0)
1780 continue;
1781 o = patsubst_expand (buffer, name, pattern, d->name,
1782 pattern_percent, percent);
1783 /* If the name expanded to the empty string, that's
1784 illegal. */
1785 if (o == buffer)
1786 fatal (flocp,
1787 _("target `%s' leaves prerequisite pattern empty"),
1788 name);
1789 free (d->name);
1790 d->name = savestring (buffer, o - buffer);
1795 /* If at least one of the dependencies uses $$@ etc. deal with that.
1796 It would be very nice and very simple to just expand everything, but
1797 it would break a lot of backward compatibility. Maybe that's OK
1798 since we're just emulating a SysV function, and if we do that then
1799 why not emulate it completely (that's what SysV make does: it
1800 re-expands the entire prerequisite list, all the time, with $@
1801 etc. in scope. But, it would be a pain indeed to document this
1802 ("iff you use $$@, your prerequisite lists is expanded twice...")
1803 Ouch. Maybe better to make the code more complex. */
1805 if (have_sysv_atvar)
1807 char *p;
1808 int tlen = strlen (name);
1809 char *fnp = strrchr (name, '/');
1810 int dlen;
1811 int flen;
1813 if (fnp)
1815 dlen = fnp - name;
1816 ++fnp;
1817 flen = strlen (fnp);
1819 else
1821 dlen = 0;
1822 fnp = name;
1823 flen = tlen;
1827 for (d = this; d != 0; d = d->next)
1828 for (p = strchr (d->name, '$'); p != 0; p = strchr (p+1, '$'))
1830 char *s = p;
1831 char *at;
1832 int atlen;
1834 /* If it's a '$@' or '$(@', it's escaped */
1835 if ((++p)[0] == '$'
1836 && (p[1] == '@' || (p[1] == '(' && p[2] == '@')))
1838 bcopy (p, s, strlen (p)+1);
1839 continue;
1842 /* Maybe found one. Check. p will point to '@' [for $@] or
1843 ')' [for $(@)] or 'D' [for $(@D)] or 'F' [for $(@F)]. */
1844 if (p[0] != '@'
1845 && (p[0] != '(' || (++p)[0] != '@'
1846 || ((++p)[0] != ')'
1847 && (p[1] != ')' || (p[0] != 'D' && p[0] != 'F')))))
1848 continue;
1850 /* Found one. Compute the length and string ptr. Move p
1851 past the variable reference. */
1852 switch (p[0])
1854 case 'D':
1855 atlen = dlen;
1856 at = name;
1857 p += 2;
1858 break;
1860 case 'F':
1861 atlen = flen;
1862 at = fnp;
1863 p += 2;
1864 break;
1866 default:
1867 atlen = tlen;
1868 at = name;
1869 ++p;
1870 break;
1873 /* Get more space. */
1875 int soff = s - d->name;
1876 int poff = p - d->name;
1877 d->name = (char *) xrealloc (d->name,
1878 strlen (d->name) + atlen + 1);
1879 s = d->name + soff;
1880 p = d->name + poff;
1883 /* Copy the string over. */
1884 bcopy(p, s+atlen, strlen (p)+1);
1885 bcopy(at, s, atlen);
1886 p = s + atlen - 1;
1890 if (!two_colon)
1892 /* Single-colon. Combine these dependencies
1893 with others in file's existing record, if any. */
1894 f = enter_file (name);
1896 if (f->double_colon)
1897 fatal (flocp,
1898 _("target file `%s' has both : and :: entries"), f->name);
1900 /* If CMDS == F->CMDS, this target was listed in this rule
1901 more than once. Just give a warning since this is harmless. */
1902 if (cmds != 0 && cmds == f->cmds)
1903 error (flocp,
1904 _("target `%s' given more than once in the same rule."),
1905 f->name);
1907 /* Check for two single-colon entries both with commands.
1908 Check is_target so that we don't lose on files such as .c.o
1909 whose commands were preinitialized. */
1910 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1912 error (&cmds->fileinfo,
1913 _("warning: overriding commands for target `%s'"),
1914 f->name);
1915 error (&f->cmds->fileinfo,
1916 _("warning: ignoring old commands for target `%s'"),
1917 f->name);
1920 f->is_target = 1;
1922 /* Defining .DEFAULT with no deps or cmds clears it. */
1923 if (f == default_file && this == 0 && cmds == 0)
1924 f->cmds = 0;
1925 if (cmds != 0)
1926 f->cmds = cmds;
1927 /* Defining .SUFFIXES with no dependencies
1928 clears out the list of suffixes. */
1929 if (f == suffix_file && this == 0)
1931 d = f->deps;
1932 while (d != 0)
1934 struct dep *nextd = d->next;
1935 free (d->name);
1936 free ((char *)d);
1937 d = nextd;
1939 f->deps = 0;
1941 else if (f->deps != 0)
1943 /* Add the file's old deps and the new ones in THIS together. */
1945 struct dep *firstdeps, *moredeps;
1946 if (cmds != 0)
1948 /* This is the rule with commands, so put its deps first.
1949 The rationale behind this is that $< expands to the
1950 first dep in the chain, and commands use $< expecting
1951 to get the dep that rule specifies. */
1952 firstdeps = this;
1953 moredeps = f->deps;
1955 else
1957 /* Append the new deps to the old ones. */
1958 firstdeps = f->deps;
1959 moredeps = this;
1962 if (firstdeps == 0)
1963 firstdeps = moredeps;
1964 else
1966 d = firstdeps;
1967 while (d->next != 0)
1968 d = d->next;
1969 d->next = moredeps;
1972 f->deps = firstdeps;
1974 else
1975 f->deps = this;
1977 /* If this is a static pattern rule, set the file's stem to
1978 the part of its name that matched the `%' in the pattern,
1979 so you can use $* in the commands. */
1980 if (pattern != 0)
1982 static char *percent = "%";
1983 char *buffer = variable_expand ("");
1984 char *o = patsubst_expand (buffer, name, pattern, percent,
1985 pattern_percent, percent);
1986 f->stem = savestring (buffer, o - buffer);
1989 else
1991 /* Double-colon. Make a new record
1992 even if the file already has one. */
1993 f = lookup_file (name);
1994 /* Check for both : and :: rules. Check is_target so
1995 we don't lose on default suffix rules or makefiles. */
1996 if (f != 0 && f->is_target && !f->double_colon)
1997 fatal (flocp,
1998 _("target file `%s' has both : and :: entries"), f->name);
1999 f = enter_file (name);
2000 /* If there was an existing entry and it was a double-colon
2001 entry, enter_file will have returned a new one, making it the
2002 prev pointer of the old one, and setting its double_colon
2003 pointer to the first one. */
2004 if (f->double_colon == 0)
2005 /* This is the first entry for this name, so we must
2006 set its double_colon pointer to itself. */
2007 f->double_colon = f;
2008 f->is_target = 1;
2009 f->deps = this;
2010 f->cmds = cmds;
2013 /* Free name if not needed further. */
2014 if (f != 0 && name != f->name
2015 && (name < f->name || name > f->name + strlen (f->name)))
2017 free (name);
2018 name = f->name;
2021 /* See if this is first target seen whose name does
2022 not start with a `.', unless it contains a slash. */
2023 if (default_goal_file == 0 && set_default
2024 && (*name != '.' || strchr (name, '/') != 0
2025 #if defined(__MSDOS__) || defined(WINDOWS32)
2026 || strchr (name, '\\') != 0
2027 #endif
2030 int reject = 0;
2032 /* If this file is a suffix, don't
2033 let it be the default goal file. */
2035 for (d = suffix_file->deps; d != 0; d = d->next)
2037 register struct dep *d2;
2038 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
2040 reject = 1;
2041 break;
2043 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
2045 register unsigned int len = strlen (dep_name (d2));
2046 if (!strneq (name, dep_name (d2), len))
2047 continue;
2048 if (streq (name + len, dep_name (d)))
2050 reject = 1;
2051 break;
2054 if (reject)
2055 break;
2058 if (!reject)
2059 default_goal_file = f;
2063 if (implicit)
2065 targets[target_idx] = 0;
2066 target_percents[target_idx] = 0;
2067 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
2068 free ((char *) target_percents);
2072 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2073 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2074 Quoting backslashes are removed from STRING by compacting it into
2075 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2076 one, or nil if there are none. */
2078 char *
2079 find_char_unquote (string, stopchars, blank)
2080 char *string;
2081 char *stopchars;
2082 int blank;
2084 unsigned int string_len = 0;
2085 register char *p = string;
2087 while (1)
2089 while (*p != '\0' && strchr (stopchars, *p) == 0
2090 && (!blank || !isblank ((unsigned char)*p)))
2091 ++p;
2092 if (*p == '\0')
2093 break;
2095 if (p > string && p[-1] == '\\')
2097 /* Search for more backslashes. */
2098 register int i = -2;
2099 while (&p[i] >= string && p[i] == '\\')
2100 --i;
2101 ++i;
2102 /* Only compute the length if really needed. */
2103 if (string_len == 0)
2104 string_len = strlen (string);
2105 /* The number of backslashes is now -I.
2106 Copy P over itself to swallow half of them. */
2107 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
2108 p += i / 2;
2109 if (i % 2 == 0)
2110 /* All the backslashes quoted each other; the STOPCHAR was
2111 unquoted. */
2112 return p;
2114 /* The STOPCHAR was quoted by a backslash. Look for another. */
2116 else
2117 /* No backslash in sight. */
2118 return p;
2121 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2122 return 0;
2125 /* Search PATTERN for an unquoted %. */
2127 char *
2128 find_percent (pattern)
2129 char *pattern;
2131 return find_char_unquote (pattern, "%", 0);
2134 /* Parse a string into a sequence of filenames represented as a
2135 chain of struct nameseq's in reverse order and return that chain.
2137 The string is passed as STRINGP, the address of a string pointer.
2138 The string pointer is updated to point at the first character
2139 not parsed, which either is a null char or equals STOPCHAR.
2141 SIZE is how big to construct chain elements.
2142 This is useful if we want them actually to be other structures
2143 that have room for additional info.
2145 If STRIP is nonzero, strip `./'s off the beginning. */
2147 struct nameseq *
2148 parse_file_seq (stringp, stopchar, size, strip)
2149 char **stringp;
2150 int stopchar;
2151 unsigned int size;
2152 int strip;
2154 register struct nameseq *new = 0;
2155 register struct nameseq *new1, *lastnew1;
2156 register char *p = *stringp;
2157 char *q;
2158 char *name;
2159 char stopchars[3];
2161 #ifdef VMS
2162 stopchars[0] = ',';
2163 stopchars[1] = stopchar;
2164 stopchars[2] = '\0';
2165 #else
2166 stopchars[0] = stopchar;
2167 stopchars[1] = '\0';
2168 #endif
2170 while (1)
2172 /* Skip whitespace; see if any more names are left. */
2173 p = next_token (p);
2174 if (*p == '\0')
2175 break;
2176 if (*p == stopchar)
2177 break;
2179 /* Yes, find end of next name. */
2180 q = p;
2181 p = find_char_unquote (q, stopchars, 1);
2182 #ifdef VMS
2183 /* convert comma separated list to space separated */
2184 if (p && *p == ',')
2185 *p =' ';
2186 #endif
2187 #ifdef _AMIGA
2188 if (stopchar == ':' && p && *p == ':'
2189 && !(isspace ((unsigned char)p[1]) || !p[1]
2190 || isspace ((unsigned char)p[-1])))
2192 p = find_char_unquote (p+1, stopchars, 1);
2194 #endif
2195 #if defined(WINDOWS32) || defined(__MSDOS__)
2196 /* For WINDOWS32, skip a "C:\..." or a "C:/..." until we find the
2197 first colon which isn't followed by a slash or a backslash.
2198 Note that tokens separated by spaces should be treated as separate
2199 tokens since make doesn't allow path names with spaces */
2200 if (stopchar == ':')
2201 while (p != 0 && !isspace ((unsigned char)*p) &&
2202 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
2203 p = find_char_unquote (p + 1, stopchars, 1);
2204 #endif
2205 if (p == 0)
2206 p = q + strlen (q);
2208 if (strip)
2209 #ifdef VMS
2210 /* Skip leading `[]'s. */
2211 while (p - q > 2 && q[0] == '[' && q[1] == ']')
2212 #else
2213 /* Skip leading `./'s. */
2214 while (p - q > 2 && q[0] == '.' && q[1] == '/')
2215 #endif
2217 q += 2; /* Skip "./". */
2218 while (q < p && *q == '/')
2219 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
2220 ++q;
2223 /* Extract the filename just found, and skip it. */
2225 if (q == p)
2226 /* ".///" was stripped to "". */
2227 #ifdef VMS
2228 continue;
2229 #else
2230 #ifdef _AMIGA
2231 name = savestring ("", 0);
2232 #else
2233 name = savestring ("./", 2);
2234 #endif
2235 #endif
2236 else
2237 #ifdef VMS
2238 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
2239 * to remove this '\' before we can use the filename.
2240 * Savestring called because q may be read-only string constant.
2243 char *qbase = xstrdup (q);
2244 char *pbase = qbase + (p-q);
2245 char *q1 = qbase;
2246 char *q2 = q1;
2247 char *p1 = pbase;
2249 while (q1 != pbase)
2251 if (*q1 == '\\' && *(q1+1) == ':')
2253 q1++;
2254 p1--;
2256 *q2++ = *q1++;
2258 name = savestring (qbase, p1 - qbase);
2259 free (qbase);
2261 #else
2262 name = savestring (q, p - q);
2263 #endif
2265 /* Add it to the front of the chain. */
2266 new1 = (struct nameseq *) xmalloc (size);
2267 new1->name = name;
2268 new1->next = new;
2269 new = new1;
2272 #ifndef NO_ARCHIVES
2274 /* Look for multi-word archive references.
2275 They are indicated by a elt ending with an unmatched `)' and
2276 an elt further down the chain (i.e., previous in the file list)
2277 with an unmatched `(' (e.g., "lib(mem"). */
2279 new1 = new;
2280 lastnew1 = 0;
2281 while (new1 != 0)
2282 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2283 && new1->name[strlen (new1->name) - 1] == ')'
2284 && strchr (new1->name, '(') == 0)
2286 /* NEW1 ends with a `)' but does not contain a `('.
2287 Look back for an elt with an opening `(' but no closing `)'. */
2289 struct nameseq *n = new1->next, *lastn = new1;
2290 char *paren = 0;
2291 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2293 lastn = n;
2294 n = n->next;
2296 if (n != 0
2297 /* Ignore something starting with `(', as that cannot actually
2298 be an archive-member reference (and treating it as such
2299 results in an empty file name, which causes much lossage). */
2300 && n->name[0] != '(')
2302 /* N is the first element in the archive group.
2303 Its name looks like "lib(mem" (with no closing `)'). */
2305 char *libname;
2307 /* Copy "lib(" into LIBNAME. */
2308 ++paren;
2309 libname = (char *) alloca (paren - n->name + 1);
2310 bcopy (n->name, libname, paren - n->name);
2311 libname[paren - n->name] = '\0';
2313 if (*paren == '\0')
2315 /* N was just "lib(", part of something like "lib( a b)".
2316 Edit it out of the chain and free its storage. */
2317 lastn->next = n->next;
2318 free (n->name);
2319 free ((char *) n);
2320 /* LASTN->next is the new stopping elt for the loop below. */
2321 n = lastn->next;
2323 else
2325 /* Replace N's name with the full archive reference. */
2326 name = concat (libname, paren, ")");
2327 free (n->name);
2328 n->name = name;
2331 if (new1->name[1] == '\0')
2333 /* NEW1 is just ")", part of something like "lib(a b )".
2334 Omit it from the chain and free its storage. */
2335 if (lastnew1 == 0)
2336 new = new1->next;
2337 else
2338 lastnew1->next = new1->next;
2339 lastn = new1;
2340 new1 = new1->next;
2341 free (lastn->name);
2342 free ((char *) lastn);
2344 else
2346 /* Replace also NEW1->name, which already has closing `)'. */
2347 name = concat (libname, new1->name, "");
2348 free (new1->name);
2349 new1->name = name;
2350 new1 = new1->next;
2353 /* Trace back from NEW1 (the end of the list) until N
2354 (the beginning of the list), rewriting each name
2355 with the full archive reference. */
2357 while (new1 != n)
2359 name = concat (libname, new1->name, ")");
2360 free (new1->name);
2361 new1->name = name;
2362 lastnew1 = new1;
2363 new1 = new1->next;
2366 else
2368 /* No frobnication happening. Just step down the list. */
2369 lastnew1 = new1;
2370 new1 = new1->next;
2373 else
2375 lastnew1 = new1;
2376 new1 = new1->next;
2379 #endif
2381 *stringp = p;
2382 return new;
2385 /* Find the next line of text in an eval buffer, combining continuation lines
2386 into one line.
2387 Return the number of actual lines read (> 1 if continuation lines).
2388 Returns -1 if there's nothing left in the buffer.
2390 After this function, ebuf->buffer points to the first character of the
2391 line we just found.
2394 /* Read a line of text from a STRING.
2395 Since we aren't really reading from a file, don't bother with linenumbers.
2398 static unsigned long
2399 readstring (ebuf)
2400 struct ebuffer *ebuf;
2402 char *p;
2404 /* If there is nothing left in this buffer, return 0. */
2405 if (ebuf->bufnext > ebuf->bufstart + ebuf->size)
2406 return -1;
2408 /* Set up a new starting point for the buffer, and find the end of the
2409 next logical line (taking into account backslash/newline pairs). */
2411 p = ebuf->buffer = ebuf->bufnext;
2413 while (1)
2415 int backslash = 0;
2417 /* Find the next newline. Keep track of backslashes as we look. */
2418 for (; *p != '\n' && *p != '\0'; ++p)
2419 if (*p == '\\')
2420 backslash = !backslash;
2422 /* If we got to the end of the string or a newline with no backslash,
2423 we're done. */
2424 if (*p == '\0' || !backslash)
2425 break;
2428 /* Overwrite the newline char. */
2429 *p = '\0';
2430 ebuf->bufnext = p+1;
2432 return 0;
2435 static long
2436 readline (ebuf)
2437 struct ebuffer *ebuf;
2439 char *buffer = ebuf->buffer;
2440 char *p = ebuf->buffer;
2441 char *end = p + ebuf->size;
2442 long nlines = 0;
2444 /* The behaviors between string and stream buffers are different enough to
2445 warrant different functions. Do the Right Thing. */
2447 if (!ebuf->fp)
2448 return readstring (ebuf);
2450 *p = '\0';
2452 while (fgets (p, end - p, ebuf->fp) != 0)
2454 char *p2;
2455 unsigned long len;
2456 int backslash;
2458 len = strlen (p);
2459 if (len == 0)
2461 /* This only happens when the first thing on the line is a '\0'.
2462 It is a pretty hopeless case, but (wonder of wonders) Athena
2463 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2464 There is nothing really to be done; we synthesize a newline so
2465 the following line doesn't appear to be part of this line. */
2466 error (&ebuf->floc,
2467 _("warning: NUL character seen; rest of line ignored"));
2468 p[0] = '\n';
2469 len = 1;
2472 /* Jump past the text we just read. */
2473 p += len;
2475 /* If the last char isn't a newline, the whole line didn't fit into the
2476 buffer. Get some more buffer and try again. */
2477 if (p[-1] != '\n')
2479 unsigned long p_off = p - buffer;
2480 ebuf->size *= 2;
2481 buffer = (char *) xrealloc (buffer, ebuf->size);
2482 p = buffer + p_off;
2483 end = buffer + ebuf->size;
2484 ebuf->buffer = buffer;
2485 *p = '\0';
2486 continue;
2489 /* We got a newline, so add one to the count of lines. */
2490 ++nlines;
2492 #if !defined(WINDOWS32) && !defined(__MSDOS__)
2493 /* Check to see if the line was really ended with CRLF; if so ignore
2494 the CR. */
2495 if ((p - buffer) > 1 && p[-2] == '\r')
2497 --p;
2498 p[-1] = '\n';
2500 #endif
2502 backslash = 0;
2503 for (p2 = p - 2; p2 >= buffer; --p2)
2505 if (*p2 != '\\')
2506 break;
2507 backslash = !backslash;
2510 if (!backslash)
2512 p[-1] = '\0';
2513 break;
2516 if (end - p <= 1)
2518 /* Enlarge the buffer. */
2519 unsigned int p_off = p - buffer;
2520 ebuf->size *= 2;
2521 buffer = (char *) xrealloc (buffer, ebuf->size);
2522 p = buffer + p_off;
2523 end = buffer + ebuf->size;
2524 ebuf->buffer = buffer;
2528 if (ferror (ebuf->fp))
2529 pfatal_with_name (ebuf->floc.filenm);
2531 return nlines ? nlines : -1;
2534 /* Parse the next "makefile word" from the input buffer, and return info
2535 about it.
2537 A "makefile word" is one of:
2539 w_bogus Should never happen
2540 w_eol End of input
2541 w_static A static word; cannot be expanded
2542 w_variable A word containing one or more variables/functions
2543 w_colon A colon
2544 w_dcolon A double-colon
2545 w_semicolon A semicolon
2546 w_comment A comment character
2547 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2549 Note that this function is only used when reading certain parts of the
2550 makefile. Don't use it where special rules hold sway (RHS of a variable,
2551 in a command list, etc.) */
2553 static enum make_word_type
2554 get_next_mword (buffer, delim, startp, length)
2555 char *buffer;
2556 char *delim;
2557 char **startp;
2558 unsigned int *length;
2560 enum make_word_type wtype = w_bogus;
2561 char *p = buffer, *beg;
2562 char c;
2564 /* Skip any leading whitespace. */
2565 while (isblank ((unsigned char)*p))
2566 ++p;
2568 beg = p;
2569 c = *(p++);
2570 switch (c)
2572 case '\0':
2573 wtype = w_eol;
2574 break;
2576 case '#':
2577 wtype = w_comment;
2578 break;
2580 case ';':
2581 wtype = w_semicolon;
2582 break;
2584 case '=':
2585 wtype = w_varassign;
2586 break;
2588 case ':':
2589 wtype = w_colon;
2590 switch (*p)
2592 case ':':
2593 ++p;
2594 wtype = w_dcolon;
2595 break;
2597 case '=':
2598 ++p;
2599 wtype = w_varassign;
2600 break;
2602 break;
2604 case '+':
2605 case '?':
2606 if (*p == '=')
2608 ++p;
2609 wtype = w_varassign;
2610 break;
2613 default:
2614 if (delim && strchr (delim, c))
2615 wtype = w_static;
2616 break;
2619 /* Did we find something? If so, return now. */
2620 if (wtype != w_bogus)
2621 goto done;
2623 /* This is some non-operator word. A word consists of the longest
2624 string of characters that doesn't contain whitespace, one of [:=#],
2625 or [?+]=, or one of the chars in the DELIM string. */
2627 /* We start out assuming a static word; if we see a variable we'll
2628 adjust our assumptions then. */
2629 wtype = w_static;
2631 /* We already found the first value of "c", above. */
2632 while (1)
2634 char closeparen;
2635 int count;
2637 switch (c)
2639 case '\0':
2640 case ' ':
2641 case '\t':
2642 case '=':
2643 case '#':
2644 goto done_word;
2646 case ':':
2647 #if defined(__MSDOS__) || defined(WINDOWS32)
2648 /* A word CAN include a colon in its drive spec. The drive
2649 spec is allowed either at the beginning of a word, or as part
2650 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2651 if (!(p - beg >= 2
2652 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
2653 && (p - beg == 2 || p[-3] == '(')))
2654 #endif
2655 goto done_word;
2657 case '$':
2658 c = *(p++);
2659 if (c == '$')
2660 break;
2662 /* This is a variable reference, so note that it's expandable.
2663 Then read it to the matching close paren. */
2664 wtype = w_variable;
2666 if (c == '(')
2667 closeparen = ')';
2668 else if (c == '{')
2669 closeparen = '}';
2670 else
2671 /* This is a single-letter variable reference. */
2672 break;
2674 for (count=0; *p != '\0'; ++p)
2676 if (*p == c)
2677 ++count;
2678 else if (*p == closeparen && --count < 0)
2680 ++p;
2681 break;
2684 break;
2686 case '?':
2687 case '+':
2688 if (*p == '=')
2689 goto done_word;
2690 break;
2692 case '\\':
2693 switch (*p)
2695 case ':':
2696 case ';':
2697 case '=':
2698 case '\\':
2699 ++p;
2700 break;
2702 break;
2704 default:
2705 if (delim && strchr (delim, c))
2706 goto done_word;
2707 break;
2710 c = *(p++);
2712 done_word:
2713 --p;
2715 done:
2716 if (startp)
2717 *startp = beg;
2718 if (length)
2719 *length = p - beg;
2720 return wtype;
2723 /* Construct the list of include directories
2724 from the arguments and the default list. */
2726 void
2727 construct_include_path (arg_dirs)
2728 char **arg_dirs;
2730 register unsigned int i;
2731 #ifdef VAXC /* just don't ask ... */
2732 stat_t stbuf;
2733 #else
2734 struct stat stbuf;
2735 #endif
2736 /* Table to hold the dirs. */
2738 register unsigned int defsize = (sizeof (default_include_directories)
2739 / sizeof (default_include_directories[0]));
2740 register unsigned int max = 5;
2741 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
2742 register unsigned int idx = 0;
2744 #ifdef __MSDOS__
2745 defsize++;
2746 #endif
2748 /* First consider any dirs specified with -I switches.
2749 Ignore dirs that don't exist. */
2751 if (arg_dirs != 0)
2752 while (*arg_dirs != 0)
2754 char *dir = *arg_dirs++;
2756 if (dir[0] == '~')
2758 char *expanded = tilde_expand (dir);
2759 if (expanded != 0)
2760 dir = expanded;
2763 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
2765 if (idx == max - 1)
2767 max += 5;
2768 dirs = (char **)
2769 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
2771 dirs[idx++] = dir;
2773 else if (dir != arg_dirs[-1])
2774 free (dir);
2777 /* Now add at the end the standard default dirs. */
2779 #ifdef __MSDOS__
2781 /* The environment variable $DJDIR holds the root of the
2782 DJGPP directory tree; add ${DJDIR}/include. */
2783 struct variable *djdir = lookup_variable ("DJDIR", 5);
2785 if (djdir)
2787 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2789 strcat (strcpy (defdir, djdir->value), "/include");
2790 dirs[idx++] = defdir;
2793 #endif
2795 for (i = 0; default_include_directories[i] != 0; ++i)
2796 if (stat (default_include_directories[i], &stbuf) == 0
2797 && S_ISDIR (stbuf.st_mode))
2798 dirs[idx++] = default_include_directories[i];
2800 dirs[idx] = 0;
2802 /* Now compute the maximum length of any name in it. */
2804 max_incl_len = 0;
2805 for (i = 0; i < idx; ++i)
2807 unsigned int len = strlen (dirs[i]);
2808 /* If dir name is written with a trailing slash, discard it. */
2809 if (dirs[i][len - 1] == '/')
2810 /* We can't just clobber a null in because it may have come from
2811 a literal string and literal strings may not be writable. */
2812 dirs[i] = savestring (dirs[i], len - 1);
2813 if (len > max_incl_len)
2814 max_incl_len = len;
2817 include_directories = dirs;
2820 /* Expand ~ or ~USER at the beginning of NAME.
2821 Return a newly malloc'd string or 0. */
2823 char *
2824 tilde_expand (name)
2825 char *name;
2827 #ifndef VMS
2828 if (name[1] == '/' || name[1] == '\0')
2830 extern char *getenv ();
2831 char *home_dir;
2832 int is_variable;
2835 /* Turn off --warn-undefined-variables while we expand HOME. */
2836 int save = warn_undefined_variables_flag;
2837 warn_undefined_variables_flag = 0;
2839 home_dir = allocated_variable_expand ("$(HOME)");
2841 warn_undefined_variables_flag = save;
2844 is_variable = home_dir[0] != '\0';
2845 if (!is_variable)
2847 free (home_dir);
2848 home_dir = getenv ("HOME");
2850 #if !defined(_AMIGA) && !defined(WINDOWS32)
2851 if (home_dir == 0 || home_dir[0] == '\0')
2853 extern char *getlogin ();
2854 char *logname = getlogin ();
2855 home_dir = 0;
2856 if (logname != 0)
2858 struct passwd *p = getpwnam (logname);
2859 if (p != 0)
2860 home_dir = p->pw_dir;
2863 #endif /* !AMIGA && !WINDOWS32 */
2864 if (home_dir != 0)
2866 char *new = concat (home_dir, "", name + 1);
2867 if (is_variable)
2868 free (home_dir);
2869 return new;
2872 #if !defined(_AMIGA) && !defined(WINDOWS32)
2873 else
2875 struct passwd *pwent;
2876 char *userend = strchr (name + 1, '/');
2877 if (userend != 0)
2878 *userend = '\0';
2879 pwent = getpwnam (name + 1);
2880 if (pwent != 0)
2882 if (userend == 0)
2883 return xstrdup (pwent->pw_dir);
2884 else
2885 return concat (pwent->pw_dir, "/", userend + 1);
2887 else if (userend != 0)
2888 *userend = '/';
2890 #endif /* !AMIGA && !WINDOWS32 */
2891 #endif /* !VMS */
2892 return 0;
2895 /* Given a chain of struct nameseq's describing a sequence of filenames,
2896 in reverse of the intended order, return a new chain describing the
2897 result of globbing the filenames. The new chain is in forward order.
2898 The links of the old chain are freed or used in the new chain.
2899 Likewise for the names in the old chain.
2901 SIZE is how big to construct chain elements.
2902 This is useful if we want them actually to be other structures
2903 that have room for additional info. */
2905 struct nameseq *
2906 multi_glob (chain, size)
2907 struct nameseq *chain;
2908 unsigned int size;
2910 extern void dir_setup_glob ();
2911 register struct nameseq *new = 0;
2912 register struct nameseq *old;
2913 struct nameseq *nexto;
2914 glob_t gl;
2916 dir_setup_glob (&gl);
2918 for (old = chain; old != 0; old = nexto)
2920 #ifndef NO_ARCHIVES
2921 char *memname;
2922 #endif
2924 nexto = old->next;
2926 if (old->name[0] == '~')
2928 char *newname = tilde_expand (old->name);
2929 if (newname != 0)
2931 free (old->name);
2932 old->name = newname;
2936 #ifndef NO_ARCHIVES
2937 if (ar_name (old->name))
2939 /* OLD->name is an archive member reference.
2940 Replace it with the archive file name,
2941 and save the member name in MEMNAME.
2942 We will glob on the archive name and then
2943 reattach MEMNAME later. */
2944 char *arname;
2945 ar_parse_name (old->name, &arname, &memname);
2946 free (old->name);
2947 old->name = arname;
2949 else
2950 memname = 0;
2951 #endif /* !NO_ARCHIVES */
2953 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
2955 case 0: /* Success. */
2957 register int i = gl.gl_pathc;
2958 while (i-- > 0)
2960 #ifndef NO_ARCHIVES
2961 if (memname != 0)
2963 /* Try to glob on MEMNAME within the archive. */
2964 struct nameseq *found
2965 = ar_glob (gl.gl_pathv[i], memname, size);
2966 if (found == 0)
2968 /* No matches. Use MEMNAME as-is. */
2969 unsigned int alen = strlen (gl.gl_pathv[i]);
2970 unsigned int mlen = strlen (memname);
2971 struct nameseq *elt
2972 = (struct nameseq *) xmalloc (size);
2973 if (size > sizeof (struct nameseq))
2974 bzero (((char *) elt) + sizeof (struct nameseq),
2975 size - sizeof (struct nameseq));
2976 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2977 bcopy (gl.gl_pathv[i], elt->name, alen);
2978 elt->name[alen] = '(';
2979 bcopy (memname, &elt->name[alen + 1], mlen);
2980 elt->name[alen + 1 + mlen] = ')';
2981 elt->name[alen + 1 + mlen + 1] = '\0';
2982 elt->next = new;
2983 new = elt;
2985 else
2987 /* Find the end of the FOUND chain. */
2988 struct nameseq *f = found;
2989 while (f->next != 0)
2990 f = f->next;
2992 /* Attach the chain being built to the end of the FOUND
2993 chain, and make FOUND the new NEW chain. */
2994 f->next = new;
2995 new = found;
2998 free (memname);
3000 else
3001 #endif /* !NO_ARCHIVES */
3003 struct nameseq *elt = (struct nameseq *) xmalloc (size);
3004 if (size > sizeof (struct nameseq))
3005 bzero (((char *) elt) + sizeof (struct nameseq),
3006 size - sizeof (struct nameseq));
3007 elt->name = xstrdup (gl.gl_pathv[i]);
3008 elt->next = new;
3009 new = elt;
3012 globfree (&gl);
3013 free (old->name);
3014 free ((char *)old);
3015 break;
3018 case GLOB_NOSPACE:
3019 fatal (NILF, _("virtual memory exhausted"));
3020 break;
3022 default:
3023 old->next = new;
3024 new = old;
3025 break;
3029 return new;