* A few cleanups, and 3.77.94 release.
[make.git] / read.c
blob400a62c26a9e510001f13e1c9c8613f27ee7498e
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"
34 #ifndef WINDOWS32
35 #ifndef _AMIGA
36 #ifndef VMS
37 #include <pwd.h>
38 #else
39 struct passwd *getpwnam PARAMS ((char *name));
40 #endif
41 #endif
42 #endif /* !WINDOWS32 */
44 /* A `struct linebuffer' is a structure which holds a line of text.
45 `readline' reads a line from a stream into a linebuffer
46 and works regardless of the length of the line. */
48 struct linebuffer
50 /* Note: This is the number of bytes malloc'ed for `buffer'
51 It does not indicate `buffer's real length.
52 Instead, a null char indicates end-of-string. */
53 unsigned int size;
54 char *buffer;
57 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
58 #define freebuffer(lb) free ((lb)->buffer)
61 /* Types of "words" that can be read in a makefile. */
62 enum make_word_type
64 w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
65 w_comment, w_varassign
69 /* A `struct conditionals' contains the information describing
70 all the active conditionals in a makefile.
72 The global variable `conditionals' contains the conditionals
73 information for the current makefile. It is initialized from
74 the static structure `toplevel_conditionals' and is later changed
75 to new structures for included makefiles. */
77 struct conditionals
79 unsigned int if_cmds; /* Depth of conditional nesting. */
80 unsigned int allocated; /* Elts allocated in following arrays. */
81 char *ignoring; /* Are we ignoring or interepreting? */
82 char *seen_else; /* Have we already seen an `else'? */
85 static struct conditionals toplevel_conditionals;
86 static struct conditionals *conditionals = &toplevel_conditionals;
89 /* Default directories to search for include files in */
91 static char *default_include_directories[] =
93 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
95 * This completly 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;
122 /* The chain of makefiles read by read_makefile. */
124 static struct dep *read_makefiles = 0;
126 static int read_makefile PARAMS ((char *filename, int flags));
127 static unsigned long readline PARAMS ((struct linebuffer *linebuffer,
128 FILE *stream, const struct floc *flocp));
129 static void do_define PARAMS ((char *name, unsigned int namelen,
130 enum variable_origin origin, FILE *infile,
131 struct floc *flocp));
132 static int conditional_line PARAMS ((char *line, const struct floc *flocp));
133 static void record_files PARAMS ((struct nameseq *filenames, char *pattern, char *pattern_percent,
134 struct dep *deps, unsigned int cmds_started, char *commands,
135 unsigned int commands_idx, int two_colon,
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 if (debug_flag)
153 puts (_("Reading makefiles..."));
155 /* If there's a non-null variable MAKEFILES, its value is a list of
156 files to read first thing. But don't let it prevent reading the
157 default makefiles and don't let the default goal come from there. */
160 char *value;
161 char *name, *p;
162 unsigned int length;
165 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
166 int save = warn_undefined_variables_flag;
167 warn_undefined_variables_flag = 0;
169 value = allocated_variable_expand ("$(MAKEFILES)");
171 warn_undefined_variables_flag = save;
174 /* Set NAME to the start of next token and LENGTH to its length.
175 MAKEFILES is updated for finding remaining tokens. */
176 p = value;
178 while ((name = find_next_token (&p, &length)) != 0)
180 if (*p != '\0')
181 *p++ = '\0';
182 (void) read_makefile (name,
183 RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
186 free (value);
189 /* Read makefiles specified with -f switches. */
191 if (makefiles != 0)
192 while (*makefiles != 0)
194 struct dep *tail = read_makefiles;
195 register struct dep *d;
197 if (! read_makefile (*makefiles, 0))
198 perror_with_name ("", *makefiles);
200 /* Find the right element of read_makefiles. */
201 d = read_makefiles;
202 while (d->next != tail)
203 d = d->next;
205 /* Use the storage read_makefile allocates. */
206 *makefiles = dep_name (d);
207 ++num_makefiles;
208 ++makefiles;
211 /* If there were no -f switches, try the default names. */
213 if (num_makefiles == 0)
215 static char *default_makefiles[] =
216 #ifdef VMS
217 /* all lower case since readdir() (the vms version) 'lowercasifies' */
218 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
219 #else
220 #ifdef _AMIGA
221 { "GNUmakefile", "Makefile", "SMakefile", 0 };
222 #else /* !Amiga && !VMS */
223 { "GNUmakefile", "makefile", "Makefile", 0 };
224 #endif /* AMIGA */
225 #endif /* VMS */
226 register char **p = default_makefiles;
227 while (*p != 0 && !file_exists_p (*p))
228 ++p;
230 if (*p != 0)
232 if (! read_makefile (*p, 0))
233 perror_with_name ("", *p);
235 else
237 /* No default makefile was found. Add the default makefiles to the
238 `read_makefiles' chain so they will be updated if possible. */
239 struct dep *tail = read_makefiles;
240 /* Add them to the tail, after any MAKEFILES variable makefiles. */
241 while (tail != 0 && tail->next != 0)
242 tail = tail->next;
243 for (p = default_makefiles; *p != 0; ++p)
245 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
246 d->name = 0;
247 d->file = enter_file (*p);
248 d->file->dontcare = 1;
249 /* Tell update_goal_chain to bail out as soon as this file is
250 made, and main not to die if we can't make this file. */
251 d->changed = RM_DONTCARE;
252 if (tail == 0)
253 read_makefiles = d;
254 else
255 tail->next = d;
256 tail = d;
258 if (tail != 0)
259 tail->next = 0;
263 return read_makefiles;
266 /* Read file FILENAME as a makefile and add its contents to the data base.
268 FLAGS contains bits as above.
270 FILENAME is added to the `read_makefiles' chain.
272 Returns 1 if a file was found and read, 0 if not. */
274 static int
275 read_makefile (filename, flags)
276 char *filename;
277 int flags;
279 static char *collapsed = 0;
280 static unsigned int collapsed_length = 0;
281 register FILE *infile;
282 struct linebuffer lb;
283 unsigned int commands_len = 200;
284 char *commands;
285 unsigned int commands_idx = 0;
286 unsigned int cmds_started;
287 char *p;
288 char *p2;
289 int len, reading_target;
290 int ignoring = 0, in_ignored_define = 0;
291 int no_targets = 0; /* Set when reading a rule without targets. */
292 struct floc fileinfo;
293 char *passed_filename = filename;
295 struct nameseq *filenames = 0;
296 struct dep *deps;
297 unsigned int nlines = 0;
298 int two_colon = 0;
299 char *pattern = 0, *pattern_percent;
301 int makefile_errno;
302 #if defined (WINDOWS32) || defined (__MSDOS__)
303 int check_again;
304 #endif
306 #define record_waiting_files() \
307 do \
309 if (filenames != 0) \
310 record_files (filenames, pattern, pattern_percent, deps, \
311 cmds_started, commands, commands_idx, \
312 two_colon, &fileinfo, \
313 !(flags & RM_NO_DEFAULT_GOAL)); \
314 filenames = 0; \
315 commands_idx = 0; \
316 if (pattern) { free(pattern); pattern = 0; } \
317 } while (0)
319 fileinfo.filenm = filename;
320 fileinfo.lineno = 1;
322 pattern_percent = 0;
323 cmds_started = fileinfo.lineno;
325 if (debug_flag)
327 printf (_("Reading makefile `%s'"), fileinfo.filenm);
328 if (flags & RM_NO_DEFAULT_GOAL)
329 printf (_(" (no default goal)"));
330 if (flags & RM_INCLUDED)
331 printf (_(" (search path)"));
332 if (flags & RM_DONTCARE)
333 printf (_(" (don't care)"));
334 if (flags & RM_NO_TILDE)
335 printf (_(" (no ~ expansion)"));
336 puts ("...");
339 /* First, get a stream to read. */
341 /* Expand ~ in FILENAME unless it came from `include',
342 in which case it was already done. */
343 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
345 char *expanded = tilde_expand (filename);
346 if (expanded != 0)
347 filename = expanded;
350 infile = fopen (filename, "r");
351 /* Save the error code so we print the right message later. */
352 makefile_errno = errno;
354 /* If the makefile wasn't found and it's either a makefile from
355 the `MAKEFILES' variable or an included makefile,
356 search the included makefile search path for this makefile. */
357 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
359 register unsigned int i;
360 for (i = 0; include_directories[i] != 0; ++i)
362 char *name = concat (include_directories[i], "/", filename);
363 infile = fopen (name, "r");
364 if (infile == 0)
365 free (name);
366 else
368 filename = name;
369 break;
374 /* Add FILENAME to the chain of read makefiles. */
375 deps = (struct dep *) xmalloc (sizeof (struct dep));
376 deps->next = read_makefiles;
377 read_makefiles = deps;
378 deps->name = 0;
379 deps->file = lookup_file (filename);
380 if (deps->file == 0)
382 deps->file = enter_file (xstrdup (filename));
383 if (flags & RM_DONTCARE)
384 deps->file->dontcare = 1;
386 if (filename != passed_filename)
387 free (filename);
388 filename = deps->file->name;
389 deps->changed = flags;
390 deps = 0;
392 /* If the makefile can't be found at all, give up entirely. */
394 if (infile == 0)
396 /* If we did some searching, errno has the error from the last
397 attempt, rather from FILENAME itself. Restore it in case the
398 caller wants to use it in a message. */
399 errno = makefile_errno;
400 return 0;
403 reading_file = &fileinfo;
405 /* Loop over lines in the file.
406 The strategy is to accumulate target names in FILENAMES, dependencies
407 in DEPS and commands in COMMANDS. These are used to define a rule
408 when the start of the next rule (or eof) is encountered. */
410 initbuffer (&lb);
411 commands = xmalloc (200);
413 while (!feof (infile))
415 fileinfo.lineno += nlines;
416 nlines = readline (&lb, infile, &fileinfo);
418 /* Check for a shell command line first.
419 If it is not one, we can stop treating tab specially. */
420 if (lb.buffer[0] == '\t')
422 /* This line is a probably shell command. */
423 unsigned int len;
425 if (no_targets)
426 /* Ignore the commands in a rule with no targets. */
427 continue;
429 /* If there is no preceding rule line, don't treat this line
430 as a command, even though it begins with a tab character.
431 SunOS 4 make appears to behave this way. */
433 if (filenames != 0)
435 if (ignoring)
436 /* Yep, this is a shell command, and we don't care. */
437 continue;
439 /* Append this command line to the line being accumulated. */
440 p = lb.buffer;
441 if (commands_idx == 0)
442 cmds_started = fileinfo.lineno;
443 len = strlen (p);
444 if (len + 1 + commands_idx > commands_len)
446 commands_len = (len + 1 + commands_idx) * 2;
447 commands = (char *) xrealloc (commands, commands_len);
449 bcopy (p, &commands[commands_idx], len);
450 commands_idx += len;
451 commands[commands_idx++] = '\n';
453 continue;
457 /* This line is not a shell command line. Don't worry about tabs. */
459 if (collapsed_length < lb.size)
461 collapsed_length = lb.size;
462 if (collapsed != 0)
463 free (collapsed);
464 collapsed = (char *) xmalloc (collapsed_length);
466 strcpy (collapsed, lb.buffer);
467 /* Collapse continuation lines. */
468 collapse_continuations (collapsed);
469 remove_comments (collapsed);
471 /* Compare a word, both length and contents. */
472 #define word1eq(s, l) (len == l && strneq (s, p, l))
473 p = collapsed;
474 while (isspace (*p))
475 ++p;
476 if (*p == '\0')
477 /* This line is completely empty. */
478 continue;
480 /* Find the end of the first token. Note we don't need to worry about
481 * ":" here since we compare tokens by length (so "export" will never
482 * be equal to "export:").
484 for (p2 = p+1; *p2 != '\0' && !isspace(*p2); ++p2)
486 len = p2 - p;
488 /* Find the start of the second token. If it's a `:' remember it,
489 since it can't be a preprocessor token--this allows targets named
490 `ifdef', `export', etc. */
491 reading_target = 0;
492 while (isspace (*p2))
493 ++p2;
494 if (*p2 == '\0')
495 p2 = NULL;
496 else if (p2[0] == ':' && p2[1] == '\0')
498 reading_target = 1;
499 goto skip_conditionals;
502 /* We must first check for conditional and `define' directives before
503 ignoring anything, since they control what we will do with
504 following lines. */
506 if (!in_ignored_define
507 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
508 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
509 || word1eq ("else", 4) || word1eq ("endif", 5)))
511 int i = conditional_line (p, &fileinfo);
512 if (i >= 0)
513 ignoring = i;
514 else
515 fatal (&fileinfo, _("invalid syntax in conditional"));
516 continue;
519 if (word1eq ("endef", 5))
521 if (in_ignored_define)
522 in_ignored_define = 0;
523 else
524 fatal (&fileinfo, _("extraneous `endef'"));
525 continue;
528 if (word1eq ("define", 6))
530 if (ignoring)
531 in_ignored_define = 1;
532 else
534 p2 = next_token (p + 6);
535 if (*p2 == '\0')
536 fatal (&fileinfo, _("empty variable name"));
538 /* Let the variable name be the whole rest of the line,
539 with trailing blanks stripped (comments have already been
540 removed), so it could be a complex variable/function
541 reference that might contain blanks. */
542 p = index (p2, '\0');
543 while (isblank (p[-1]))
544 --p;
545 do_define (p2, p - p2, o_file, infile, &fileinfo);
547 continue;
550 if (word1eq ("override", 8))
552 p2 = next_token (p + 8);
553 if (*p2 == '\0')
554 error (&fileinfo, _("empty `override' directive"));
555 if (strneq (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
557 if (ignoring)
558 in_ignored_define = 1;
559 else
561 p2 = next_token (p2 + 6);
562 if (*p2 == '\0')
563 fatal (&fileinfo, _("empty variable name"));
565 /* Let the variable name be the whole rest of the line,
566 with trailing blanks stripped (comments have already been
567 removed), so it could be a complex variable/function
568 reference that might contain blanks. */
569 p = index (p2, '\0');
570 while (isblank (p[-1]))
571 --p;
572 do_define (p2, p - p2, o_override, infile, &fileinfo);
575 else if (!ignoring
576 && !try_variable_definition (&fileinfo, p2, o_override))
577 error (&fileinfo, _("invalid `override' directive"));
579 continue;
581 skip_conditionals:
583 if (ignoring)
584 /* Ignore the line. We continue here so conditionals
585 can appear in the middle of a rule. */
586 continue;
588 if (!reading_target && word1eq ("export", 6))
590 struct variable *v;
591 p2 = next_token (p + 6);
592 if (*p2 == '\0')
593 export_all_variables = 1;
594 v = try_variable_definition (&fileinfo, p2, o_file);
595 if (v != 0)
596 v->export = v_export;
597 else
599 unsigned int len;
600 for (p = find_next_token (&p2, &len); p != 0;
601 p = find_next_token (&p2, &len))
603 v = lookup_variable (p, len);
604 if (v == 0)
605 v = define_variable (p, len, "", o_file, 0);
606 v->export = v_export;
610 else if (!reading_target && word1eq ("unexport", 8))
612 unsigned int len;
613 struct variable *v;
614 p2 = next_token (p + 8);
615 if (*p2 == '\0')
616 export_all_variables = 0;
617 for (p = find_next_token (&p2, &len); p != 0;
618 p = find_next_token (&p2, &len))
620 v = lookup_variable (p, len);
621 if (v == 0)
622 v = define_variable (p, len, "", o_file, 0);
623 v->export = v_noexport;
626 else if (word1eq ("vpath", 5))
628 char *pattern;
629 unsigned int len;
630 p2 = variable_expand (p + 5);
631 p = find_next_token (&p2, &len);
632 if (p != 0)
634 pattern = savestring (p, len);
635 p = find_next_token (&p2, &len);
636 /* No searchpath means remove all previous
637 selective VPATH's with the same pattern. */
639 else
640 /* No pattern means remove all previous selective VPATH's. */
641 pattern = 0;
642 construct_vpath_list (pattern, p);
643 if (pattern != 0)
644 free (pattern);
646 else if (word1eq ("include", 7) || word1eq ("-include", 8)
647 || word1eq ("sinclude", 8))
649 /* We have found an `include' line specifying a nested
650 makefile to be read at this point. */
651 struct conditionals *save, new_conditionals;
652 struct nameseq *files;
653 /* "-include" (vs "include") says no error if the file does not
654 exist. "sinclude" is an alias for this from SGI. */
655 int noerror = p[0] != 'i';
657 p = allocated_variable_expand (next_token (p + (noerror ? 8 : 7)));
658 if (*p == '\0')
660 error (&fileinfo,
661 _("no file name for `%sinclude'"), noerror ? "-" : "");
662 continue;
665 /* Parse the list of file names. */
666 p2 = p;
667 files = multi_glob (parse_file_seq (&p2, '\0',
668 sizeof (struct nameseq),
670 sizeof (struct nameseq));
671 free (p);
673 /* Save the state of conditionals and start
674 the included makefile with a clean slate. */
675 save = conditionals;
676 bzero ((char *) &new_conditionals, sizeof new_conditionals);
677 conditionals = &new_conditionals;
679 /* Record the rules that are waiting so they will determine
680 the default goal before those in the included makefile. */
681 record_waiting_files ();
683 /* Read each included makefile. */
684 while (files != 0)
686 struct nameseq *next = files->next;
687 char *name = files->name;
688 free ((char *)files);
689 files = next;
691 if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
692 | (noerror ? RM_DONTCARE : 0)))
693 && ! noerror)
694 error (&fileinfo, "%s: %s", name, strerror (errno));
695 free(name);
698 /* Free any space allocated by conditional_line. */
699 if (conditionals->ignoring)
700 free (conditionals->ignoring);
701 if (conditionals->seen_else)
702 free (conditionals->seen_else);
704 /* Restore state. */
705 conditionals = save;
706 reading_file = &fileinfo;
708 #undef word1eq
709 else if (try_variable_definition (&fileinfo, p, o_file))
710 /* This line has been dealt with. */
712 else if (lb.buffer[0] == '\t')
714 p = collapsed; /* Ignore comments. */
715 while (isblank (*p))
716 ++p;
717 if (*p == '\0')
718 /* The line is completely blank; that is harmless. */
719 continue;
720 /* This line starts with a tab but was not caught above
721 because there was no preceding target, and the line
722 might have been usable as a variable definition.
723 But now it is definitely lossage. */
724 fatal(&fileinfo, _("commands commence before first target"));
726 else
728 /* This line describes some target files. This is complicated by
729 the existence of target-specific variables, because we can't
730 expand the entire line until we know if we have one or not. So
731 we expand the line word by word until we find the first `:',
732 then check to see if it's a target-specific variable.
734 In this algorithm, `lb_next' will point to the beginning of the
735 unexpanded parts of the input buffer, while `p2' points to the
736 parts of the expanded buffer we haven't searched yet. */
738 enum make_word_type wtype;
739 enum variable_origin v_origin;
740 char *cmdleft, *lb_next;
741 unsigned int len, plen = 0;
742 char *colonp;
744 /* Record the previous rule. */
746 record_waiting_files ();
748 /* Search the line for an unquoted ; that is not after an
749 unquoted #. */
750 cmdleft = find_char_unquote (lb.buffer, ";#", 0);
751 if (cmdleft != 0 && *cmdleft == '#')
753 /* We found a comment before a semicolon. */
754 *cmdleft = '\0';
755 cmdleft = 0;
757 else if (cmdleft != 0)
758 /* Found one. Cut the line short there before expanding it. */
759 *(cmdleft++) = '\0';
761 collapse_continuations (lb.buffer);
763 /* We can't expand the entire line, since if it's a per-target
764 variable we don't want to expand it. So, walk from the
765 beginning, expanding as we go, and looking for "interesting"
766 chars. The first word is always expandable. */
767 wtype = get_next_mword(lb.buffer, NULL, &lb_next, &len);
768 switch (wtype)
770 case w_eol:
771 if (cmdleft != 0)
772 fatal(&fileinfo, _("missing rule before commands"));
773 /* This line contained something but turned out to be nothing
774 but whitespace (a comment?). */
775 continue;
777 case w_colon:
778 case w_dcolon:
779 /* We accept and ignore rules without targets for
780 compatibility with SunOS 4 make. */
781 no_targets = 1;
782 continue;
784 default:
785 break;
788 p2 = variable_expand_string(NULL, lb_next, len);
789 while (1)
791 lb_next += len;
792 if (cmdleft == 0)
794 /* Look for a semicolon in the expanded line. */
795 cmdleft = find_char_unquote (p2, ";", 0);
797 if (cmdleft != 0)
799 unsigned long p2_off = p2 - variable_buffer;
800 unsigned long cmd_off = cmdleft - variable_buffer;
801 char *pend = p2 + strlen(p2);
803 /* Append any remnants of lb, then cut the line short
804 at the semicolon. */
805 *cmdleft = '\0';
807 /* One school of thought says that you shouldn't expand
808 here, but merely copy, since now you're beyond a ";"
809 and into a command script. However, the old parser
810 expanded the whole line, so we continue that for
811 backwards-compatiblity. Also, it wouldn't be
812 entirely consistent, since we do an unconditional
813 expand below once we know we don't have a
814 target-specific variable. */
815 (void)variable_expand_string(pend, lb_next, (long)-1);
816 lb_next += strlen(lb_next);
817 p2 = variable_buffer + p2_off;
818 cmdleft = variable_buffer + cmd_off + 1;
822 colonp = find_char_unquote(p2, ":", 0);
823 #if defined(__MSDOS__) || defined(WINDOWS32)
824 /* The drive spec brain-damage strikes again... */
825 /* Note that the only separators of targets in this context
826 are whitespace and a left paren. If others are possible,
827 they should be added to the string in the call to index. */
828 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
829 colonp > p2 && isalpha(colonp[-1]) &&
830 (colonp == p2 + 1 || index(" \t(", colonp[-2]) != 0))
831 colonp = find_char_unquote(colonp + 1, ":", 0);
832 #endif
833 if (colonp != 0)
834 break;
836 wtype = get_next_mword(lb_next, NULL, &lb_next, &len);
837 if (wtype == w_eol)
838 break;
840 p2 += strlen(p2);
841 *(p2++) = ' ';
842 p2 = variable_expand_string(p2, lb_next, len);
843 /* We don't need to worry about cmdleft here, because if it was
844 found in the variable_buffer the entire buffer has already
845 been expanded... we'll never get here. */
848 p2 = next_token (variable_buffer);
850 /* If the word we're looking at is EOL, see if there's _anything_
851 on the line. If not, a variable expanded to nothing, so ignore
852 it. If so, we can't parse this line so punt. */
853 if (wtype == w_eol)
855 if (*p2 != '\0')
856 /* There's no need to be ivory-tower about this: check for
857 one of the most common bugs found in makefiles... */
858 fatal (&fileinfo, _("missing separator%s"),
859 !strneq(lb.buffer, " ", 8) ? ""
860 : _(" (did you mean TAB instead of 8 spaces?)"));
861 continue;
864 /* Make the colon the end-of-string so we know where to stop
865 looking for targets. */
866 *colonp = '\0';
867 filenames = multi_glob (parse_file_seq (&p2, '\0',
868 sizeof (struct nameseq),
870 sizeof (struct nameseq));
871 *colonp = ':';
873 if (!filenames)
875 /* We accept and ignore rules without targets for
876 compatibility with SunOS 4 make. */
877 no_targets = 1;
878 continue;
880 /* This should never be possible; we handled it above. */
881 assert(*p2 != '\0');
882 ++p2;
884 /* Is this a one-colon or two-colon entry? */
885 two_colon = *p2 == ':';
886 if (two_colon)
887 p2++;
889 /* Test to see if it's a target-specific variable. Copy the rest
890 of the buffer over, possibly temporarily (we'll expand it later
891 if it's not a target-specific variable). PLEN saves the length
892 of the unparsed section of p2, for later. */
893 if (*lb_next != '\0')
895 unsigned int l = p2 - variable_buffer;
896 plen = strlen(p2);
897 (void)variable_buffer_output(p2+plen,
898 lb_next, strlen(lb_next)+1);
899 p2 = variable_buffer + l;
901 wtype = get_next_mword(p2, NULL, &p, &len);
902 v_origin = o_file;
903 if (wtype == w_static && (len == (sizeof("override")-1)
904 && strneq(p, "override", len)))
906 v_origin = o_override;
907 (void)get_next_mword(p+len, NULL, &p, &len);
909 else if (wtype != w_eol)
910 wtype = get_next_mword(p+len, NULL, NULL, NULL);
912 if (wtype == w_varassign || v_origin == o_override)
914 record_target_var(filenames, p, two_colon, v_origin, &fileinfo);
915 filenames = 0;
916 continue;
919 /* This is a normal target, _not_ a target-specific variable.
920 Unquote any = in the dependency list. */
921 find_char_unquote (lb_next, "=", 0);
923 /* We have some targets, so don't ignore the following commands. */
924 no_targets = 0;
926 /* Expand the dependencies, etc. */
927 if (*lb_next != '\0')
929 unsigned int l = p2 - variable_buffer;
930 (void)variable_expand_string(p2 + plen, lb_next, (long)-1);
931 p2 = variable_buffer + l;
933 /* Look for a semicolon in the expanded line. */
934 if (cmdleft == 0)
936 cmdleft = find_char_unquote (p2, ";", 0);
937 if (cmdleft != 0)
938 *(cmdleft++) = '\0';
942 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
943 p = index (p2, ':');
944 while (p != 0 && p[-1] == '\\')
946 register char *q = &p[-1];
947 register int backslash = 0;
948 while (*q-- == '\\')
949 backslash = !backslash;
950 if (backslash)
951 p = index (p + 1, ':');
952 else
953 break;
955 #ifdef _AMIGA
956 /* Here, the situation is quite complicated. Let's have a look
957 at a couple of targets:
959 install: dev:make
961 dev:make: make
963 dev:make:: xyz
965 The rule is that it's only a target, if there are TWO :'s
966 OR a space around the :.
968 if (p && !(isspace(p[1]) || !p[1] || isspace(p[-1])))
969 p = 0;
970 #endif
971 #if defined (WINDOWS32) || defined (__MSDOS__)
972 do {
973 check_again = 0;
974 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
975 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
976 isalpha(p[-1]) &&
977 (p == p2 + 1 || index(" \t:(", p[-2]) != 0)) {
978 p = index(p + 1, ':');
979 check_again = 1;
981 } while (check_again);
982 #endif
983 if (p != 0)
985 struct nameseq *target;
986 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
987 ++p2;
988 if (target == 0)
989 fatal (&fileinfo, _("missing target pattern"));
990 else if (target->next != 0)
991 fatal (&fileinfo, _("multiple target patterns"));
992 pattern = target->name;
993 pattern_percent = find_percent (pattern);
994 if (pattern_percent == 0)
995 fatal (&fileinfo, _("target pattern contains no `%%'"));
996 free((char *)target);
998 else
999 pattern = 0;
1001 /* Parse the dependencies. */
1002 deps = (struct dep *)
1003 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
1004 sizeof (struct dep));
1006 commands_idx = 0;
1007 if (cmdleft != 0)
1009 /* Semicolon means rest of line is a command. */
1010 unsigned int len = strlen (cmdleft);
1012 cmds_started = fileinfo.lineno;
1014 /* Add this command line to the buffer. */
1015 if (len + 2 > commands_len)
1017 commands_len = (len + 2) * 2;
1018 commands = (char *) xrealloc (commands, commands_len);
1020 bcopy (cmdleft, commands, len);
1021 commands_idx += len;
1022 commands[commands_idx++] = '\n';
1025 continue;
1028 /* We get here except in the case that we just read a rule line.
1029 Record now the last rule we read, so following spurious
1030 commands are properly diagnosed. */
1031 record_waiting_files ();
1032 no_targets = 0;
1035 if (conditionals->if_cmds)
1036 fatal (&fileinfo, _("missing `endif'"));
1038 /* At eof, record the last rule. */
1039 record_waiting_files ();
1041 freebuffer (&lb);
1042 free ((char *) commands);
1043 fclose (infile);
1045 reading_file = 0;
1047 return 1;
1050 /* Execute a `define' directive.
1051 The first line has already been read, and NAME is the name of
1052 the variable to be defined. The following lines remain to be read.
1053 LINENO, INFILE and FILENAME refer to the makefile being read.
1054 The value returned is LINENO, updated for lines read here. */
1056 static void
1057 do_define (name, namelen, origin, infile, flocp)
1058 char *name;
1059 unsigned int namelen;
1060 enum variable_origin origin;
1061 FILE *infile;
1062 struct floc *flocp;
1064 struct linebuffer lb;
1065 unsigned int nlines = 0;
1066 unsigned int length = 100;
1067 char *definition = (char *) xmalloc (100);
1068 register unsigned int idx = 0;
1069 register char *p;
1071 /* Expand the variable name. */
1072 char *var = (char *) alloca (namelen + 1);
1073 bcopy (name, var, namelen);
1074 var[namelen] = '\0';
1075 var = variable_expand (var);
1077 initbuffer (&lb);
1078 while (!feof (infile))
1080 unsigned int len;
1082 flocp->lineno += nlines;
1083 nlines = readline (&lb, infile, flocp);
1085 collapse_continuations (lb.buffer);
1087 p = next_token (lb.buffer);
1088 len = strlen (p);
1089 if ((len == 5 || (len > 5 && isblank (p[5])))
1090 && strneq (p, "endef", 5))
1092 p += 5;
1093 remove_comments (p);
1094 if (*next_token (p) != '\0')
1095 error (flocp, _("Extraneous text after `endef' directive"));
1096 /* Define the variable. */
1097 if (idx == 0)
1098 definition[0] = '\0';
1099 else
1100 definition[idx - 1] = '\0';
1101 (void) define_variable (var, strlen (var), definition, origin, 1);
1102 free (definition);
1103 freebuffer (&lb);
1104 return;
1106 else
1108 len = strlen (lb.buffer);
1109 /* Increase the buffer size if necessary. */
1110 if (idx + len + 1 > length)
1112 length = (idx + len) * 2;
1113 definition = (char *) xrealloc (definition, length + 1);
1116 bcopy (lb.buffer, &definition[idx], len);
1117 idx += len;
1118 /* Separate lines with a newline. */
1119 definition[idx++] = '\n';
1123 /* No `endef'!! */
1124 fatal (flocp, _("missing `endef', unterminated `define'"));
1126 /* NOTREACHED */
1127 return;
1130 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1131 "ifneq", "else" and "endif".
1132 LINE is the input line, with the command as its first word.
1134 FILENAME and LINENO are the filename and line number in the
1135 current makefile. They are used for error messages.
1137 Value is -1 if the line is invalid,
1138 0 if following text should be interpreted,
1139 1 if following text should be ignored. */
1141 static int
1142 conditional_line (line, flocp)
1143 char *line;
1144 const struct floc *flocp;
1146 int notdef;
1147 char *cmdname;
1148 register unsigned int i;
1150 if (*line == 'i')
1152 /* It's an "if..." command. */
1153 notdef = line[2] == 'n';
1154 if (notdef)
1156 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
1157 line += cmdname[3] == 'd' ? 7 : 6;
1159 else
1161 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
1162 line += cmdname[2] == 'd' ? 6 : 5;
1165 else
1167 /* It's an "else" or "endif" command. */
1168 notdef = line[1] == 'n';
1169 cmdname = notdef ? "endif" : "else";
1170 line += notdef ? 5 : 4;
1173 line = next_token (line);
1175 if (*cmdname == 'e')
1177 if (*line != '\0')
1178 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1179 /* "Else" or "endif". */
1180 if (conditionals->if_cmds == 0)
1181 fatal (flocp, _("extraneous `%s'"), cmdname);
1182 /* NOTDEF indicates an `endif' command. */
1183 if (notdef)
1184 --conditionals->if_cmds;
1185 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1186 fatal (flocp, _("only one `else' per conditional"));
1187 else
1189 /* Toggle the state of ignorance. */
1190 conditionals->ignoring[conditionals->if_cmds - 1]
1191 = !conditionals->ignoring[conditionals->if_cmds - 1];
1192 /* Record that we have seen an `else' in this conditional.
1193 A second `else' will be erroneous. */
1194 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1196 for (i = 0; i < conditionals->if_cmds; ++i)
1197 if (conditionals->ignoring[i])
1198 return 1;
1199 return 0;
1202 if (conditionals->allocated == 0)
1204 conditionals->allocated = 5;
1205 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1206 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1209 ++conditionals->if_cmds;
1210 if (conditionals->if_cmds > conditionals->allocated)
1212 conditionals->allocated += 5;
1213 conditionals->ignoring = (char *)
1214 xrealloc (conditionals->ignoring, conditionals->allocated);
1215 conditionals->seen_else = (char *)
1216 xrealloc (conditionals->seen_else, conditionals->allocated);
1219 /* Record that we have seen an `if...' but no `else' so far. */
1220 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1222 /* Search through the stack to see if we're already ignoring. */
1223 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1224 if (conditionals->ignoring[i])
1226 /* We are already ignoring, so just push a level
1227 to match the next "else" or "endif", and keep ignoring.
1228 We don't want to expand variables in the condition. */
1229 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1230 return 1;
1233 if (cmdname[notdef ? 3 : 2] == 'd')
1235 /* "Ifdef" or "ifndef". */
1236 struct variable *v;
1237 register char *p = end_of_token (line);
1238 i = p - line;
1239 p = next_token (p);
1240 if (*p != '\0')
1241 return -1;
1242 v = lookup_variable (line, i);
1243 conditionals->ignoring[conditionals->if_cmds - 1]
1244 = (v != 0 && *v->value != '\0') == notdef;
1246 else
1248 /* "Ifeq" or "ifneq". */
1249 char *s1, *s2;
1250 unsigned int len;
1251 char termin = *line == '(' ? ',' : *line;
1253 if (termin != ',' && termin != '"' && termin != '\'')
1254 return -1;
1256 s1 = ++line;
1257 /* Find the end of the first string. */
1258 if (termin == ',')
1260 register int count = 0;
1261 for (; *line != '\0'; ++line)
1262 if (*line == '(')
1263 ++count;
1264 else if (*line == ')')
1265 --count;
1266 else if (*line == ',' && count <= 0)
1267 break;
1269 else
1270 while (*line != '\0' && *line != termin)
1271 ++line;
1273 if (*line == '\0')
1274 return -1;
1276 if (termin == ',')
1278 /* Strip blanks after the first string. */
1279 char *p = line++;
1280 while (isblank (p[-1]))
1281 --p;
1282 *p = '\0';
1284 else
1285 *line++ = '\0';
1287 s2 = variable_expand (s1);
1288 /* We must allocate a new copy of the expanded string because
1289 variable_expand re-uses the same buffer. */
1290 len = strlen (s2);
1291 s1 = (char *) alloca (len + 1);
1292 bcopy (s2, s1, len + 1);
1294 if (termin != ',')
1295 /* Find the start of the second string. */
1296 line = next_token (line);
1298 termin = termin == ',' ? ')' : *line;
1299 if (termin != ')' && termin != '"' && termin != '\'')
1300 return -1;
1302 /* Find the end of the second string. */
1303 if (termin == ')')
1305 register int count = 0;
1306 s2 = next_token (line);
1307 for (line = s2; *line != '\0'; ++line)
1309 if (*line == '(')
1310 ++count;
1311 else if (*line == ')')
1313 if (count <= 0)
1314 break;
1315 else
1316 --count;
1320 else
1322 ++line;
1323 s2 = line;
1324 while (*line != '\0' && *line != termin)
1325 ++line;
1328 if (*line == '\0')
1329 return -1;
1331 *line = '\0';
1332 line = next_token (++line);
1333 if (*line != '\0')
1334 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1336 s2 = variable_expand (s2);
1337 conditionals->ignoring[conditionals->if_cmds - 1]
1338 = streq (s1, s2) == notdef;
1341 /* Search through the stack to see if we're ignoring. */
1342 for (i = 0; i < conditionals->if_cmds; ++i)
1343 if (conditionals->ignoring[i])
1344 return 1;
1345 return 0;
1348 /* Remove duplicate dependencies in CHAIN. */
1350 void
1351 uniquize_deps (chain)
1352 struct dep *chain;
1354 register struct dep *d;
1356 /* Make sure that no dependencies are repeated. This does not
1357 really matter for the purpose of updating targets, but it
1358 might make some names be listed twice for $^ and $?. */
1360 for (d = chain; d != 0; d = d->next)
1362 struct dep *last, *next;
1364 last = d;
1365 next = d->next;
1366 while (next != 0)
1367 if (streq (dep_name (d), dep_name (next)))
1369 struct dep *n = next->next;
1370 last->next = n;
1371 if (next->name != 0 && next->name != d->name)
1372 free (next->name);
1373 if (next != d)
1374 free ((char *) next);
1375 next = n;
1377 else
1379 last = next;
1380 next = next->next;
1385 /* Record target-specific variable values for files FILENAMES.
1386 TWO_COLON is nonzero if a double colon was used.
1388 The links of FILENAMES are freed, and so are any names in it
1389 that are not incorporated into other data structures.
1391 If the target is a pattern, add the variable to the pattern-specific
1392 variable value list. */
1394 static void
1395 record_target_var (filenames, defn, two_colon, origin, flocp)
1396 struct nameseq *filenames;
1397 char *defn;
1398 int two_colon;
1399 enum variable_origin origin;
1400 const struct floc *flocp;
1402 struct nameseq *nextf;
1403 struct variable_set_list *global;
1405 global = current_variable_set_list;
1407 for (; filenames != 0; filenames = nextf)
1409 struct variable *v;
1410 register char *name = filenames->name;
1411 struct variable_set_list *vlist;
1412 char *fname;
1413 char *percent;
1415 nextf = filenames->next;
1416 free ((char *) filenames);
1418 /* If it's a pattern target, then add it to the pattern-specific
1419 variable list. */
1420 percent = find_percent (name);
1421 if (percent)
1423 struct pattern_var *p;
1425 /* Get a reference for this pattern-specific variable struct. */
1426 p = create_pattern_var(name, percent);
1427 vlist = p->vars;
1428 fname = p->target;
1430 else
1432 struct file *f;
1434 /* Get a file reference for this file, and initialize it. */
1435 f = enter_file (name);
1436 initialize_file_variables (f);
1437 vlist = f->variables;
1438 fname = f->name;
1441 /* Make the new variable context current and define the variable. */
1442 current_variable_set_list = vlist;
1443 v = try_variable_definition (flocp, defn, origin);
1444 if (!v)
1445 error (flocp, _("Malformed per-target variable definition"));
1446 v->per_target = 1;
1448 /* If it's not an override, check to see if there was a command-line
1449 setting. If so, reset the value. */
1450 if (origin != o_override)
1452 struct variable *gv;
1453 int len = strlen(v->name);
1455 current_variable_set_list = global;
1456 gv = lookup_variable (v->name, len);
1457 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1458 define_variable_in_set (v->name, len, gv->value, gv->origin,
1459 gv->recursive, vlist->set);
1462 /* Free name if not needed further. */
1463 if (name != fname && (name < fname || name > fname + strlen (fname)))
1464 free (name);
1467 current_variable_set_list = global;
1470 /* Record a description line for files FILENAMES,
1471 with dependencies DEPS, commands to execute described
1472 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1473 TWO_COLON is nonzero if a double colon was used.
1474 If not nil, PATTERN is the `%' pattern to make this
1475 a static pattern rule, and PATTERN_PERCENT is a pointer
1476 to the `%' within it.
1478 The links of FILENAMES are freed, and so are any names in it
1479 that are not incorporated into other data structures. */
1481 static void
1482 record_files (filenames, pattern, pattern_percent, deps, cmds_started,
1483 commands, commands_idx, two_colon, flocp, set_default)
1484 struct nameseq *filenames;
1485 char *pattern, *pattern_percent;
1486 struct dep *deps;
1487 unsigned int cmds_started;
1488 char *commands;
1489 unsigned int commands_idx;
1490 int two_colon;
1491 const struct floc *flocp;
1492 int set_default;
1494 struct nameseq *nextf;
1495 int implicit = 0;
1496 unsigned int max_targets = 0, target_idx = 0;
1497 char **targets = 0, **target_percents = 0;
1498 struct commands *cmds;
1500 if (commands_idx > 0)
1502 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1503 cmds->fileinfo.filenm = flocp->filenm;
1504 cmds->fileinfo.lineno = cmds_started;
1505 cmds->commands = savestring (commands, commands_idx);
1506 cmds->command_lines = 0;
1508 else
1509 cmds = 0;
1511 for (; filenames != 0; filenames = nextf)
1514 register char *name = filenames->name;
1515 register struct file *f;
1516 register struct dep *d;
1517 struct dep *this;
1518 char *implicit_percent;
1520 nextf = filenames->next;
1521 free ((char *) filenames);
1523 implicit_percent = find_percent (name);
1524 implicit |= implicit_percent != 0;
1526 if (implicit && pattern != 0)
1527 fatal (flocp, _("mixed implicit and static pattern rules"));
1529 if (implicit && implicit_percent == 0)
1530 fatal (flocp, _("mixed implicit and normal rules"));
1532 if (implicit)
1534 if (targets == 0)
1536 max_targets = 5;
1537 targets = (char **) xmalloc (5 * sizeof (char *));
1538 target_percents = (char **) xmalloc (5 * sizeof (char *));
1539 target_idx = 0;
1541 else if (target_idx == max_targets - 1)
1543 max_targets += 5;
1544 targets = (char **) xrealloc ((char *) targets,
1545 max_targets * sizeof (char *));
1546 target_percents
1547 = (char **) xrealloc ((char *) target_percents,
1548 max_targets * sizeof (char *));
1550 targets[target_idx] = name;
1551 target_percents[target_idx] = implicit_percent;
1552 ++target_idx;
1553 continue;
1556 /* If there are multiple filenames, copy the chain DEPS
1557 for all but the last one. It is not safe for the same deps
1558 to go in more than one place in the data base. */
1559 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1561 if (pattern != 0)
1563 /* If this is an extended static rule:
1564 `targets: target%pattern: dep%pattern; cmds',
1565 translate each dependency pattern into a plain filename
1566 using the target pattern and this target's name. */
1567 if (!pattern_matches (pattern, pattern_percent, name))
1569 /* Give a warning if the rule is meaningless. */
1570 error (flocp,
1571 _("target `%s' doesn't match the target pattern"), name);
1572 this = 0;
1574 else
1576 /* We use patsubst_expand to do the work of translating
1577 the target pattern, the target's name and the dependencies'
1578 patterns into plain dependency names. */
1579 char *buffer = variable_expand ("");
1581 for (d = this; d != 0; d = d->next)
1583 char *o;
1584 char *percent = find_percent (d->name);
1585 if (percent == 0)
1586 continue;
1587 o = patsubst_expand (buffer, name, pattern, d->name,
1588 pattern_percent, percent);
1589 free (d->name);
1590 d->name = savestring (buffer, o - buffer);
1595 if (!two_colon)
1597 /* Single-colon. Combine these dependencies
1598 with others in file's existing record, if any. */
1599 f = enter_file (name);
1601 if (f->double_colon)
1602 fatal (flocp,
1603 _("target file `%s' has both : and :: entries"), f->name);
1605 /* If CMDS == F->CMDS, this target was listed in this rule
1606 more than once. Just give a warning since this is harmless. */
1607 if (cmds != 0 && cmds == f->cmds)
1608 error (flocp, _("target `%s' given more than once in the same rule."),
1609 f->name);
1611 /* Check for two single-colon entries both with commands.
1612 Check is_target so that we don't lose on files such as .c.o
1613 whose commands were preinitialized. */
1614 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1616 error (&cmds->fileinfo,
1617 _("warning: overriding commands for target `%s'"), f->name);
1618 error (&f->cmds->fileinfo,
1619 _("warning: ignoring old commands for target `%s'"),
1620 f->name);
1623 f->is_target = 1;
1625 /* Defining .DEFAULT with no deps or cmds clears it. */
1626 if (f == default_file && this == 0 && cmds == 0)
1627 f->cmds = 0;
1628 if (cmds != 0)
1629 f->cmds = cmds;
1630 /* Defining .SUFFIXES with no dependencies
1631 clears out the list of suffixes. */
1632 if (f == suffix_file && this == 0)
1634 d = f->deps;
1635 while (d != 0)
1637 struct dep *nextd = d->next;
1638 free (d->name);
1639 free ((char *)d);
1640 d = nextd;
1642 f->deps = 0;
1644 else if (f->deps != 0)
1646 /* Add the file's old deps and the new ones in THIS together. */
1648 struct dep *firstdeps, *moredeps;
1649 if (cmds != 0)
1651 /* This is the rule with commands, so put its deps first.
1652 The rationale behind this is that $< expands to the
1653 first dep in the chain, and commands use $< expecting
1654 to get the dep that rule specifies. */
1655 firstdeps = this;
1656 moredeps = f->deps;
1658 else
1660 /* Append the new deps to the old ones. */
1661 firstdeps = f->deps;
1662 moredeps = this;
1665 if (firstdeps == 0)
1666 firstdeps = moredeps;
1667 else
1669 d = firstdeps;
1670 while (d->next != 0)
1671 d = d->next;
1672 d->next = moredeps;
1675 f->deps = firstdeps;
1677 else
1678 f->deps = this;
1680 /* If this is a static pattern rule, set the file's stem to
1681 the part of its name that matched the `%' in the pattern,
1682 so you can use $* in the commands. */
1683 if (pattern != 0)
1685 static char *percent = "%";
1686 char *buffer = variable_expand ("");
1687 char *o = patsubst_expand (buffer, name, pattern, percent,
1688 pattern_percent, percent);
1689 f->stem = savestring (buffer, o - buffer);
1692 else
1694 /* Double-colon. Make a new record
1695 even if the file already has one. */
1696 f = lookup_file (name);
1697 /* Check for both : and :: rules. Check is_target so
1698 we don't lose on default suffix rules or makefiles. */
1699 if (f != 0 && f->is_target && !f->double_colon)
1700 fatal (flocp,
1701 _("target file `%s' has both : and :: entries"), f->name);
1702 f = enter_file (name);
1703 /* If there was an existing entry and it was a double-colon
1704 entry, enter_file will have returned a new one, making it the
1705 prev pointer of the old one, and setting its double_colon
1706 pointer to the first one. */
1707 if (f->double_colon == 0)
1708 /* This is the first entry for this name, so we must
1709 set its double_colon pointer to itself. */
1710 f->double_colon = f;
1711 f->is_target = 1;
1712 f->deps = this;
1713 f->cmds = cmds;
1716 /* Free name if not needed further. */
1717 if (f != 0 && name != f->name
1718 && (name < f->name || name > f->name + strlen (f->name)))
1720 free (name);
1721 name = f->name;
1724 /* See if this is first target seen whose name does
1725 not start with a `.', unless it contains a slash. */
1726 if (default_goal_file == 0 && set_default
1727 && (*name != '.' || index (name, '/') != 0
1728 #ifdef __MSDOS__
1729 || index (name, '\\') != 0
1730 #endif
1733 int reject = 0;
1735 /* If this file is a suffix, don't
1736 let it be the default goal file. */
1738 for (d = suffix_file->deps; d != 0; d = d->next)
1740 register struct dep *d2;
1741 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1743 reject = 1;
1744 break;
1746 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1748 register unsigned int len = strlen (dep_name (d2));
1749 if (!strneq (name, dep_name (d2), len))
1750 continue;
1751 if (streq (name + len, dep_name (d)))
1753 reject = 1;
1754 break;
1757 if (reject)
1758 break;
1761 if (!reject)
1762 default_goal_file = f;
1766 if (implicit)
1768 targets[target_idx] = 0;
1769 target_percents[target_idx] = 0;
1770 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1771 free ((char *) target_percents);
1775 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1776 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1777 Quoting backslashes are removed from STRING by compacting it into
1778 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1779 one, or nil if there are none. */
1781 char *
1782 find_char_unquote (string, stopchars, blank)
1783 char *string;
1784 char *stopchars;
1785 int blank;
1787 unsigned int string_len = 0;
1788 register char *p = string;
1790 while (1)
1792 while (*p != '\0' && index (stopchars, *p) == 0
1793 && (!blank || !isblank (*p)))
1794 ++p;
1795 if (*p == '\0')
1796 break;
1798 if (p > string && p[-1] == '\\')
1800 /* Search for more backslashes. */
1801 register int i = -2;
1802 while (&p[i] >= string && p[i] == '\\')
1803 --i;
1804 ++i;
1805 /* Only compute the length if really needed. */
1806 if (string_len == 0)
1807 string_len = strlen (string);
1808 /* The number of backslashes is now -I.
1809 Copy P over itself to swallow half of them. */
1810 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1811 p += i / 2;
1812 if (i % 2 == 0)
1813 /* All the backslashes quoted each other; the STOPCHAR was
1814 unquoted. */
1815 return p;
1817 /* The STOPCHAR was quoted by a backslash. Look for another. */
1819 else
1820 /* No backslash in sight. */
1821 return p;
1824 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1825 return 0;
1828 /* Search PATTERN for an unquoted %. */
1830 char *
1831 find_percent (pattern)
1832 char *pattern;
1834 return find_char_unquote (pattern, "%", 0);
1837 /* Parse a string into a sequence of filenames represented as a
1838 chain of struct nameseq's in reverse order and return that chain.
1840 The string is passed as STRINGP, the address of a string pointer.
1841 The string pointer is updated to point at the first character
1842 not parsed, which either is a null char or equals STOPCHAR.
1844 SIZE is how big to construct chain elements.
1845 This is useful if we want them actually to be other structures
1846 that have room for additional info.
1848 If STRIP is nonzero, strip `./'s off the beginning. */
1850 struct nameseq *
1851 parse_file_seq (stringp, stopchar, size, strip)
1852 char **stringp;
1853 int stopchar;
1854 unsigned int size;
1855 int strip;
1857 register struct nameseq *new = 0;
1858 register struct nameseq *new1, *lastnew1;
1859 register char *p = *stringp;
1860 char *q;
1861 char *name;
1862 char stopchars[3];
1864 #ifdef VMS
1865 stopchars[0] = ',';
1866 stopchars[1] = stopchar;
1867 stopchars[2] = '\0';
1868 #else
1869 stopchars[0] = stopchar;
1870 stopchars[1] = '\0';
1871 #endif
1873 while (1)
1875 /* Skip whitespace; see if any more names are left. */
1876 p = next_token (p);
1877 if (*p == '\0')
1878 break;
1879 if (*p == stopchar)
1880 break;
1882 /* Yes, find end of next name. */
1883 q = p;
1884 p = find_char_unquote (q, stopchars, 1);
1885 #ifdef VMS
1886 /* convert comma separated list to space separated */
1887 if (p && *p == ',')
1888 *p =' ';
1889 #endif
1890 #ifdef _AMIGA
1891 if (stopchar == ':' && p && *p == ':' &&
1892 !(isspace(p[1]) || !p[1] || isspace(p[-1])))
1894 p = find_char_unquote (p+1, stopchars, 1);
1896 #endif
1897 #if defined(WINDOWS32) || defined(__MSDOS__)
1898 /* For WINDOWS32, skip a "C:\..." or a "C:/..." until we find the
1899 first colon which isn't followed by a slash or a backslash.
1900 Note that tokens separated by spaces should be treated as separate
1901 tokens since make doesn't allow path names with spaces */
1902 if (stopchar == ':')
1903 while (p != 0 && !isspace(*p) &&
1904 (p[1] == '\\' || p[1] == '/') && isalpha (p[-1]))
1905 p = find_char_unquote (p + 1, stopchars, 1);
1906 #endif
1907 if (p == 0)
1908 p = q + strlen (q);
1910 if (strip)
1911 #ifdef VMS
1912 /* Skip leading `[]'s. */
1913 while (p - q > 2 && q[0] == '[' && q[1] == ']')
1914 #else
1915 /* Skip leading `./'s. */
1916 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1917 #endif
1919 q += 2; /* Skip "./". */
1920 while (q < p && *q == '/')
1921 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1922 ++q;
1925 /* Extract the filename just found, and skip it. */
1927 if (q == p)
1928 /* ".///" was stripped to "". */
1929 #ifdef VMS
1930 continue;
1931 #else
1932 #ifdef _AMIGA
1933 name = savestring ("", 0);
1934 #else
1935 name = savestring ("./", 2);
1936 #endif
1937 #endif
1938 else
1939 #ifdef VMS
1940 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
1941 * to remove this '\' before we can use the filename.
1942 * Savestring called because q may be read-only string constant.
1945 char *qbase = xstrdup (q);
1946 char *pbase = qbase + (p-q);
1947 char *q1 = qbase;
1948 char *q2 = q1;
1949 char *p1 = pbase;
1951 while (q1 != pbase)
1953 if (*q1 == '\\' && *(q1+1) == ':')
1955 q1++;
1956 p1--;
1958 *q2++ = *q1++;
1960 name = savestring (qbase, p1 - qbase);
1961 free (qbase);
1963 #else
1964 name = savestring (q, p - q);
1965 #endif
1967 /* Add it to the front of the chain. */
1968 new1 = (struct nameseq *) xmalloc (size);
1969 new1->name = name;
1970 new1->next = new;
1971 new = new1;
1974 #ifndef NO_ARCHIVES
1976 /* Look for multi-word archive references.
1977 They are indicated by a elt ending with an unmatched `)' and
1978 an elt further down the chain (i.e., previous in the file list)
1979 with an unmatched `(' (e.g., "lib(mem"). */
1981 new1 = new;
1982 lastnew1 = 0;
1983 while (new1 != 0)
1984 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1985 && new1->name[strlen (new1->name) - 1] == ')'
1986 && index (new1->name, '(') == 0)
1988 /* NEW1 ends with a `)' but does not contain a `('.
1989 Look back for an elt with an opening `(' but no closing `)'. */
1991 struct nameseq *n = new1->next, *lastn = new1;
1992 char *paren = 0;
1993 while (n != 0 && (paren = index (n->name, '(')) == 0)
1995 lastn = n;
1996 n = n->next;
1998 if (n != 0
1999 /* Ignore something starting with `(', as that cannot actually
2000 be an archive-member reference (and treating it as such
2001 results in an empty file name, which causes much lossage). */
2002 && n->name[0] != '(')
2004 /* N is the first element in the archive group.
2005 Its name looks like "lib(mem" (with no closing `)'). */
2007 char *libname;
2009 /* Copy "lib(" into LIBNAME. */
2010 ++paren;
2011 libname = (char *) alloca (paren - n->name + 1);
2012 bcopy (n->name, libname, paren - n->name);
2013 libname[paren - n->name] = '\0';
2015 if (*paren == '\0')
2017 /* N was just "lib(", part of something like "lib( a b)".
2018 Edit it out of the chain and free its storage. */
2019 lastn->next = n->next;
2020 free (n->name);
2021 free ((char *) n);
2022 /* LASTN->next is the new stopping elt for the loop below. */
2023 n = lastn->next;
2025 else
2027 /* Replace N's name with the full archive reference. */
2028 name = concat (libname, paren, ")");
2029 free (n->name);
2030 n->name = name;
2033 if (new1->name[1] == '\0')
2035 /* NEW1 is just ")", part of something like "lib(a b )".
2036 Omit it from the chain and free its storage. */
2037 if (lastnew1 == 0)
2038 new = new1->next;
2039 else
2040 lastnew1->next = new1->next;
2041 lastn = new1;
2042 new1 = new1->next;
2043 free (lastn->name);
2044 free ((char *) lastn);
2046 else
2048 /* Replace also NEW1->name, which already has closing `)'. */
2049 name = concat (libname, new1->name, "");
2050 free (new1->name);
2051 new1->name = name;
2052 new1 = new1->next;
2055 /* Trace back from NEW1 (the end of the list) until N
2056 (the beginning of the list), rewriting each name
2057 with the full archive reference. */
2059 while (new1 != n)
2061 name = concat (libname, new1->name, ")");
2062 free (new1->name);
2063 new1->name = name;
2064 lastnew1 = new1;
2065 new1 = new1->next;
2068 else
2070 /* No frobnication happening. Just step down the list. */
2071 lastnew1 = new1;
2072 new1 = new1->next;
2075 else
2077 lastnew1 = new1;
2078 new1 = new1->next;
2081 #endif
2083 *stringp = p;
2084 return new;
2087 /* Read a line of text from STREAM into LINEBUFFER.
2088 Combine continuation lines into one line.
2089 Return the number of actual lines read (> 1 if hacked continuation lines).
2092 static unsigned long
2093 readline (linebuffer, stream, flocp)
2094 struct linebuffer *linebuffer;
2095 FILE *stream;
2096 const struct floc *flocp;
2098 char *buffer = linebuffer->buffer;
2099 register char *p = linebuffer->buffer;
2100 register char *end = p + linebuffer->size;
2101 register int len, lastlen = 0;
2102 register char *p2;
2103 register unsigned int nlines = 0;
2104 register int backslash;
2106 *p = '\0';
2108 while (fgets (p, end - p, stream) != 0)
2110 len = strlen (p);
2111 if (len == 0)
2113 /* This only happens when the first thing on the line is a '\0'.
2114 It is a pretty hopeless case, but (wonder of wonders) Athena
2115 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2116 There is nothing really to be done; we synthesize a newline so
2117 the following line doesn't appear to be part of this line. */
2118 error (flocp, _("warning: NUL character seen; rest of line ignored"));
2119 p[0] = '\n';
2120 len = 1;
2123 p += len;
2124 if (p[-1] != '\n')
2126 /* Probably ran out of buffer space. */
2127 register unsigned int p_off = p - buffer;
2128 linebuffer->size *= 2;
2129 buffer = (char *) xrealloc (buffer, linebuffer->size);
2130 p = buffer + p_off;
2131 end = buffer + linebuffer->size;
2132 linebuffer->buffer = buffer;
2133 *p = '\0';
2134 lastlen = len;
2135 continue;
2138 ++nlines;
2140 #if !defined(WINDOWS32) && !defined(__MSDOS__)
2141 /* Check to see if the line was really ended with CRLF; if so ignore
2142 the CR. */
2143 if (len > 1 && p[-2] == '\r')
2145 --len;
2146 --p;
2147 p[-1] = '\n';
2149 #endif
2151 if (len == 1 && p > buffer)
2152 /* P is pointing at a newline and it's the beginning of
2153 the buffer returned by the last fgets call. However,
2154 it is not necessarily the beginning of a line if P is
2155 pointing past the beginning of the holding buffer.
2156 If the buffer was just enlarged (right before the newline),
2157 we must account for that, so we pretend that the two lines
2158 were one line. */
2159 len += lastlen;
2160 lastlen = len;
2161 backslash = 0;
2162 for (p2 = p - 2; --len > 0; --p2)
2164 if (*p2 == '\\')
2165 backslash = !backslash;
2166 else
2167 break;
2170 if (!backslash)
2172 p[-1] = '\0';
2173 break;
2176 if (end - p <= 1)
2178 /* Enlarge the buffer. */
2179 register unsigned int p_off = p - buffer;
2180 linebuffer->size *= 2;
2181 buffer = (char *) xrealloc (buffer, linebuffer->size);
2182 p = buffer + p_off;
2183 end = buffer + linebuffer->size;
2184 linebuffer->buffer = buffer;
2188 if (ferror (stream))
2189 pfatal_with_name (flocp->filenm);
2191 return nlines;
2194 /* Parse the next "makefile word" from the input buffer, and return info
2195 about it.
2197 A "makefile word" is one of:
2199 w_bogus Should never happen
2200 w_eol End of input
2201 w_static A static word; cannot be expanded
2202 w_variable A word containing one or more variables/functions
2203 w_colon A colon
2204 w_dcolon A double-colon
2205 w_semicolon A semicolon
2206 w_comment A comment character
2207 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2209 Note that this function is only used when reading certain parts of the
2210 makefile. Don't use it where special rules hold sway (RHS of a variable,
2211 in a command list, etc.) */
2213 static enum make_word_type
2214 get_next_mword (buffer, delim, startp, length)
2215 char *buffer;
2216 char *delim;
2217 char **startp;
2218 unsigned int *length;
2220 enum make_word_type wtype = w_bogus;
2221 char *p = buffer, *beg;
2222 char c;
2224 /* Skip any leading whitespace. */
2225 while (isblank(*p))
2226 ++p;
2228 beg = p;
2229 c = *(p++);
2230 switch (c)
2232 case '\0':
2233 wtype = w_eol;
2234 break;
2236 case '#':
2237 wtype = w_comment;
2238 break;
2240 case ';':
2241 wtype = w_semicolon;
2242 break;
2244 case '=':
2245 wtype = w_varassign;
2246 break;
2248 case ':':
2249 wtype = w_colon;
2250 switch (*p)
2252 case ':':
2253 ++p;
2254 wtype = w_dcolon;
2255 break;
2257 case '=':
2258 ++p;
2259 wtype = w_varassign;
2260 break;
2262 break;
2264 case '+':
2265 case '?':
2266 if (*p == '=')
2268 ++p;
2269 wtype = w_varassign;
2270 break;
2273 default:
2274 if (delim && index(delim, c))
2275 wtype = w_static;
2276 break;
2279 /* Did we find something? If so, return now. */
2280 if (wtype != w_bogus)
2281 goto done;
2283 /* This is some non-operator word. A word consists of the longest
2284 string of characters that doesn't contain whitespace, one of [:=#],
2285 or [?+]=, or one of the chars in the DELIM string. */
2287 /* We start out assuming a static word; if we see a variable we'll
2288 adjust our assumptions then. */
2289 wtype = w_static;
2291 /* We already found the first value of "c", above. */
2292 while (1)
2294 char closeparen;
2295 int count;
2297 switch (c)
2299 case '\0':
2300 case ' ':
2301 case '\t':
2302 case '=':
2303 case '#':
2304 goto done_word;
2306 case ':':
2307 #if defined(__MSDOS__) || defined(WINDOWS32)
2308 /* A word CAN include a colon in its drive spec. The drive
2309 spec is allowed either at the beginning of a word, or as part
2310 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2311 if (!(p - beg >= 2 &&
2312 (*p == '/' || *p == '\\') && isalpha (p[-2]) &&
2313 (p - beg == 2 || p[-3] == '(')))
2314 #endif
2315 goto done_word;
2317 case '$':
2318 c = *(p++);
2319 if (c == '$')
2320 break;
2322 /* This is a variable reference, so note that it's expandable.
2323 Then read it to the matching close paren. */
2324 wtype = w_variable;
2326 if (c == '(')
2327 closeparen = ')';
2328 else if (c == '{')
2329 closeparen = '}';
2330 else
2331 /* This is a single-letter variable reference. */
2332 break;
2334 for (count=0; *p != '\0'; ++p)
2336 if (*p == c)
2337 ++count;
2338 else if (*p == closeparen && --count < 0)
2340 ++p;
2341 break;
2344 break;
2346 case '?':
2347 case '+':
2348 if (*p == '=')
2349 goto done_word;
2350 break;
2352 case '\\':
2353 switch (*p)
2355 case ':':
2356 case ';':
2357 case '=':
2358 case '\\':
2359 ++p;
2360 break;
2362 break;
2364 default:
2365 if (delim && index(delim, c))
2366 goto done_word;
2367 break;
2370 c = *(p++);
2372 done_word:
2373 --p;
2375 done:
2376 if (startp)
2377 *startp = beg;
2378 if (length)
2379 *length = p - beg;
2380 return wtype;
2383 /* Construct the list of include directories
2384 from the arguments and the default list. */
2386 void
2387 construct_include_path (arg_dirs)
2388 char **arg_dirs;
2390 register unsigned int i;
2391 #ifdef VAXC /* just don't ask ... */
2392 stat_t stbuf;
2393 #else
2394 struct stat stbuf;
2395 #endif
2396 /* Table to hold the dirs. */
2398 register unsigned int defsize = (sizeof (default_include_directories)
2399 / sizeof (default_include_directories[0]));
2400 register unsigned int max = 5;
2401 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
2402 register unsigned int idx = 0;
2404 #ifdef __MSDOS__
2405 defsize++;
2406 #endif
2408 /* First consider any dirs specified with -I switches.
2409 Ignore dirs that don't exist. */
2411 if (arg_dirs != 0)
2412 while (*arg_dirs != 0)
2414 char *dir = *arg_dirs++;
2416 if (dir[0] == '~')
2418 char *expanded = tilde_expand (dir);
2419 if (expanded != 0)
2420 dir = expanded;
2423 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
2425 if (idx == max - 1)
2427 max += 5;
2428 dirs = (char **)
2429 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
2431 dirs[idx++] = dir;
2433 else if (dir != arg_dirs[-1])
2434 free (dir);
2437 /* Now add at the end the standard default dirs. */
2439 #ifdef __MSDOS__
2441 /* The environment variable $DJDIR holds the root of the
2442 DJGPP directory tree; add ${DJDIR}/include. */
2443 struct variable *djdir = lookup_variable ("DJDIR", 5);
2445 if (djdir)
2447 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2449 strcat (strcpy (defdir, djdir->value), "/include");
2450 dirs[idx++] = defdir;
2453 #endif
2455 for (i = 0; default_include_directories[i] != 0; ++i)
2456 if (stat (default_include_directories[i], &stbuf) == 0
2457 && S_ISDIR (stbuf.st_mode))
2458 dirs[idx++] = default_include_directories[i];
2460 dirs[idx] = 0;
2462 /* Now compute the maximum length of any name in it. */
2464 max_incl_len = 0;
2465 for (i = 0; i < idx; ++i)
2467 unsigned int len = strlen (dirs[i]);
2468 /* If dir name is written with a trailing slash, discard it. */
2469 if (dirs[i][len - 1] == '/')
2470 /* We can't just clobber a null in because it may have come from
2471 a literal string and literal strings may not be writable. */
2472 dirs[i] = savestring (dirs[i], len - 1);
2473 if (len > max_incl_len)
2474 max_incl_len = len;
2477 include_directories = dirs;
2480 /* Expand ~ or ~USER at the beginning of NAME.
2481 Return a newly malloc'd string or 0. */
2483 char *
2484 tilde_expand (name)
2485 char *name;
2487 #ifndef VMS
2488 if (name[1] == '/' || name[1] == '\0')
2490 extern char *getenv ();
2491 char *home_dir;
2492 int is_variable;
2495 /* Turn off --warn-undefined-variables while we expand HOME. */
2496 int save = warn_undefined_variables_flag;
2497 warn_undefined_variables_flag = 0;
2499 home_dir = allocated_variable_expand ("$(HOME)");
2501 warn_undefined_variables_flag = save;
2504 is_variable = home_dir[0] != '\0';
2505 if (!is_variable)
2507 free (home_dir);
2508 home_dir = getenv ("HOME");
2510 #if !defined(_AMIGA) && !defined(WINDOWS32)
2511 if (home_dir == 0 || home_dir[0] == '\0')
2513 extern char *getlogin ();
2514 char *logname = getlogin ();
2515 home_dir = 0;
2516 if (logname != 0)
2518 struct passwd *p = getpwnam (logname);
2519 if (p != 0)
2520 home_dir = p->pw_dir;
2523 #endif /* !AMIGA && !WINDOWS32 */
2524 if (home_dir != 0)
2526 char *new = concat (home_dir, "", name + 1);
2527 if (is_variable)
2528 free (home_dir);
2529 return new;
2532 #if !defined(_AMIGA) && !defined(WINDOWS32)
2533 else
2535 struct passwd *pwent;
2536 char *userend = index (name + 1, '/');
2537 if (userend != 0)
2538 *userend = '\0';
2539 pwent = getpwnam (name + 1);
2540 if (pwent != 0)
2542 if (userend == 0)
2543 return xstrdup (pwent->pw_dir);
2544 else
2545 return concat (pwent->pw_dir, "/", userend + 1);
2547 else if (userend != 0)
2548 *userend = '/';
2550 #endif /* !AMIGA && !WINDOWS32 */
2551 #endif /* !VMS */
2552 return 0;
2555 /* Given a chain of struct nameseq's describing a sequence of filenames,
2556 in reverse of the intended order, return a new chain describing the
2557 result of globbing the filenames. The new chain is in forward order.
2558 The links of the old chain are freed or used in the new chain.
2559 Likewise for the names in the old chain.
2561 SIZE is how big to construct chain elements.
2562 This is useful if we want them actually to be other structures
2563 that have room for additional info. */
2565 struct nameseq *
2566 multi_glob (chain, size)
2567 struct nameseq *chain;
2568 unsigned int size;
2570 extern void dir_setup_glob ();
2571 register struct nameseq *new = 0;
2572 register struct nameseq *old;
2573 struct nameseq *nexto;
2574 glob_t gl;
2576 dir_setup_glob (&gl);
2578 for (old = chain; old != 0; old = nexto)
2580 #ifndef NO_ARCHIVES
2581 char *memname;
2582 #endif
2584 nexto = old->next;
2586 if (old->name[0] == '~')
2588 char *newname = tilde_expand (old->name);
2589 if (newname != 0)
2591 free (old->name);
2592 old->name = newname;
2596 #ifndef NO_ARCHIVES
2597 if (ar_name (old->name))
2599 /* OLD->name is an archive member reference.
2600 Replace it with the archive file name,
2601 and save the member name in MEMNAME.
2602 We will glob on the archive name and then
2603 reattach MEMNAME later. */
2604 char *arname;
2605 ar_parse_name (old->name, &arname, &memname);
2606 free (old->name);
2607 old->name = arname;
2609 else
2610 memname = 0;
2611 #endif /* !NO_ARCHIVES */
2613 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
2615 case 0: /* Success. */
2617 register int i = gl.gl_pathc;
2618 while (i-- > 0)
2620 #ifndef NO_ARCHIVES
2621 if (memname != 0)
2623 /* Try to glob on MEMNAME within the archive. */
2624 struct nameseq *found
2625 = ar_glob (gl.gl_pathv[i], memname, size);
2626 if (found == 0)
2628 /* No matches. Use MEMNAME as-is. */
2629 struct nameseq *elt
2630 = (struct nameseq *) xmalloc (size);
2631 unsigned int alen = strlen (gl.gl_pathv[i]);
2632 unsigned int mlen = strlen (memname);
2633 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2634 bcopy (gl.gl_pathv[i], elt->name, alen);
2635 elt->name[alen] = '(';
2636 bcopy (memname, &elt->name[alen + 1], mlen);
2637 elt->name[alen + 1 + mlen] = ')';
2638 elt->name[alen + 1 + mlen + 1] = '\0';
2639 elt->next = new;
2640 new = elt;
2642 else
2644 /* Find the end of the FOUND chain. */
2645 struct nameseq *f = found;
2646 while (f->next != 0)
2647 f = f->next;
2649 /* Attach the chain being built to the end of the FOUND
2650 chain, and make FOUND the new NEW chain. */
2651 f->next = new;
2652 new = found;
2655 free (memname);
2657 else
2658 #endif /* !NO_ARCHIVES */
2660 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2661 elt->name = xstrdup (gl.gl_pathv[i]);
2662 elt->next = new;
2663 new = elt;
2666 globfree (&gl);
2667 free (old->name);
2668 free ((char *)old);
2669 break;
2672 case GLOB_NOSPACE:
2673 fatal (NILF, _("virtual memory exhausted"));
2674 break;
2676 default:
2677 old->next = new;
2678 new = old;
2679 break;
2683 return new;