* texinfo.tex (\value): handle active _ or - in argument (happens
[make.git] / read.c
blob02075162e54dfcaae4a6e5dcff0cab478d418133
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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include <assert.h>
21 #include "make.h"
22 #include "dep.h"
23 #include "filedef.h"
24 #include "job.h"
25 #include "commands.h"
26 #include "variable.h"
27 #include "rule.h"
29 /* This is POSIX.2, but most systems using -DPOSIX probably don't have it. */
30 #ifdef HAVE_GLOB_H
31 #include <glob.h>
32 #else
33 #include "glob/glob.h"
34 #endif
36 #ifndef WINDOWS32
37 #ifndef _AMIGA
38 #ifndef VMS
39 #include <pwd.h>
40 #else
41 struct passwd *getpwnam PARAMS ((char *name));
42 #endif
43 #endif
44 #endif /* !WINDOWS32 */
46 /* A `struct linebuffer' is a structure which holds a line of text.
47 `readline' reads a line from a stream into a linebuffer
48 and works regardless of the length of the line. */
50 struct linebuffer
52 /* Note: This is the number of bytes malloc'ed for `buffer'
53 It does not indicate `buffer's real length.
54 Instead, a null char indicates end-of-string. */
55 unsigned int size;
56 char *buffer;
59 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
60 #define freebuffer(lb) free ((lb)->buffer)
63 /* Types of "words" that can be read in a makefile. */
64 enum make_word_type
66 w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
67 w_comment, w_varassign
71 /* A `struct conditionals' contains the information describing
72 all the active conditionals in a makefile.
74 The global variable `conditionals' contains the conditionals
75 information for the current makefile. It is initialized from
76 the static structure `toplevel_conditionals' and is later changed
77 to new structures for included makefiles. */
79 struct conditionals
81 unsigned int if_cmds; /* Depth of conditional nesting. */
82 unsigned int allocated; /* Elts allocated in following arrays. */
83 char *ignoring; /* Are we ignoring or interepreting? */
84 char *seen_else; /* Have we already seen an `else'? */
87 static struct conditionals toplevel_conditionals;
88 static struct conditionals *conditionals = &toplevel_conditionals;
91 /* Default directories to search for include files in */
93 static char *default_include_directories[] =
95 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
97 * This completly up to the user when they install MSVC or other packages.
98 * This is defined as a placeholder.
100 #define INCLUDEDIR "."
101 #endif
102 INCLUDEDIR,
103 #ifndef _AMIGA
104 "/usr/gnu/include",
105 "/usr/local/include",
106 "/usr/include",
107 #endif
111 /* List of directories to search for include files in */
113 static char **include_directories;
115 /* Maximum length of an element of the above. */
117 static unsigned int max_incl_len;
119 /* The filename and pointer to line number of the
120 makefile currently being read in. */
122 char *reading_filename;
123 unsigned int *reading_lineno_ptr;
125 /* The chain of makefiles read by read_makefile. */
127 static struct dep *read_makefiles = 0;
129 static int read_makefile PARAMS ((char *filename, int flags));
130 static unsigned int readline PARAMS ((struct linebuffer *linebuffer, FILE *stream,
131 char *filename, unsigned int lineno));
132 static unsigned int do_define PARAMS ((char *name, unsigned int namelen, enum variable_origin origin,
133 unsigned int lineno, FILE *infile, char *filename));
134 static int conditional_line PARAMS ((char *line, char *filename, unsigned int lineno));
135 static void record_files PARAMS ((struct nameseq *filenames, char *pattern, char *pattern_percent,
136 struct dep *deps, unsigned int cmds_started, char *commands,
137 unsigned int commands_idx, int two_colon, char *filename,
138 unsigned int lineno, int set_default));
139 static void record_target_var PARAMS ((struct nameseq *filenames, char *defn,
140 int two_colon, enum variable_origin origin,
141 char *filename, unsigned int lineno));
142 static enum make_word_type get_next_mword PARAMS ((char *buffer, char *delim,
143 char **startp, unsigned int *length));
145 /* Read in all the makefiles and return the chain of their names. */
147 struct dep *
148 read_all_makefiles (makefiles)
149 char **makefiles;
151 unsigned int num_makefiles = 0;
153 if (debug_flag)
154 puts ("Reading makefiles...");
156 /* If there's a non-null variable MAKEFILES, its value is a list of
157 files to read first thing. But don't let it prevent reading the
158 default makefiles and don't let the default goal come from there. */
161 char *value;
162 char *name, *p;
163 unsigned int length;
166 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
167 int save = warn_undefined_variables_flag;
168 warn_undefined_variables_flag = 0;
170 value = allocated_variable_expand ("$(MAKEFILES)");
172 warn_undefined_variables_flag = save;
175 /* Set NAME to the start of next token and LENGTH to its length.
176 MAKEFILES is updated for finding remaining tokens. */
177 p = value;
179 while ((name = find_next_token (&p, &length)) != 0)
181 if (*p != '\0')
182 *p++ = '\0';
183 (void) read_makefile (name,
184 RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
187 free (value);
190 /* Read makefiles specified with -f switches. */
192 if (makefiles != 0)
193 while (*makefiles != 0)
195 struct dep *tail = read_makefiles;
196 register struct dep *d;
198 if (! read_makefile (*makefiles, 0))
199 perror_with_name ("", *makefiles);
201 /* Find the right element of read_makefiles. */
202 d = read_makefiles;
203 while (d->next != tail)
204 d = d->next;
206 /* Use the storage read_makefile allocates. */
207 *makefiles = dep_name (d);
208 ++num_makefiles;
209 ++makefiles;
212 /* If there were no -f switches, try the default names. */
214 if (num_makefiles == 0)
216 static char *default_makefiles[] =
217 #ifdef VMS
218 /* all lower case since readdir() (the vms version) 'lowercasifies' */
219 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
220 #else
221 #ifdef _AMIGA
222 { "GNUmakefile", "Makefile", "SMakefile", 0 };
223 #else /* !Amiga && !VMS */
224 { "GNUmakefile", "makefile", "Makefile", 0 };
225 #endif /* AMIGA */
226 #endif /* VMS */
227 register char **p = default_makefiles;
228 while (*p != 0 && !file_exists_p (*p))
229 ++p;
231 if (*p != 0)
233 if (! read_makefile (*p, 0))
234 perror_with_name ("", *p);
236 else
238 /* No default makefile was found. Add the default makefiles to the
239 `read_makefiles' chain so they will be updated if possible. */
240 struct dep *tail = read_makefiles;
241 /* Add them to the tail, after any MAKEFILES variable makefiles. */
242 while (tail != 0 && tail->next != 0)
243 tail = tail->next;
244 for (p = default_makefiles; *p != 0; ++p)
246 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
247 d->name = 0;
248 d->file = enter_file (*p);
249 d->file->dontcare = 1;
250 /* Tell update_goal_chain to bail out as soon as this file is
251 made, and main not to die if we can't make this file. */
252 d->changed = RM_DONTCARE;
253 if (tail == 0)
254 read_makefiles = d;
255 else
256 tail->next = d;
257 tail = d;
259 if (tail != 0)
260 tail->next = 0;
264 return read_makefiles;
267 /* Read file FILENAME as a makefile and add its contents to the data base.
269 FLAGS contains bits as above.
271 FILENAME is added to the `read_makefiles' chain.
273 Returns 1 if a file was found and read, 0 if not. */
275 static int
276 read_makefile (filename, flags)
277 char *filename;
278 int flags;
280 static char *collapsed = 0;
281 static unsigned int collapsed_length = 0;
282 register FILE *infile;
283 struct linebuffer lb;
284 unsigned int commands_len = 200;
285 char *commands = (char *) xmalloc (200);
286 unsigned int commands_idx = 0;
287 unsigned int cmds_started;
288 char *p;
289 char *p2;
290 int len, reading_target;
291 int ignoring = 0, in_ignored_define = 0;
292 int no_targets = 0; /* Set when reading a rule without targets. */
293 char *passed_filename = filename;
295 struct nameseq *filenames = 0;
296 struct dep *deps;
297 unsigned int lineno = 1;
298 unsigned int nlines = 0;
299 int two_colon = 0;
300 char *pattern = 0, *pattern_percent;
302 int makefile_errno;
303 #if defined (WINDOWS32) || defined (__MSDOS__)
304 int check_again;
305 #endif
307 #define record_waiting_files() \
308 do \
310 if (filenames != 0) \
311 record_files (filenames, pattern, pattern_percent, deps, \
312 cmds_started, commands, commands_idx, \
313 two_colon, filename, lineno, \
314 !(flags & RM_NO_DEFAULT_GOAL)); \
315 filenames = 0; \
316 commands_idx = 0; \
317 if (pattern) { free(pattern); pattern = 0; } \
318 } while (0)
320 pattern_percent = 0;
321 cmds_started = lineno;
323 if (debug_flag)
325 printf ("Reading makefile `%s'", filename);
326 if (flags & RM_NO_DEFAULT_GOAL)
327 printf (" (no default goal)");
328 if (flags & RM_INCLUDED)
329 printf (" (search path)");
330 if (flags & RM_DONTCARE)
331 printf (" (don't care)");
332 if (flags & RM_NO_TILDE)
333 printf (" (no ~ expansion)");
334 puts ("...");
337 /* First, get a stream to read. */
339 /* Expand ~ in FILENAME unless it came from `include',
340 in which case it was already done. */
341 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
343 char *expanded = tilde_expand (filename);
344 if (expanded != 0)
345 filename = expanded;
348 infile = fopen (filename, "r");
349 /* Save the error code so we print the right message later. */
350 makefile_errno = errno;
352 /* If the makefile wasn't found and it's either a makefile from
353 the `MAKEFILES' variable or an included makefile,
354 search the included makefile search path for this makefile. */
355 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
357 register unsigned int i;
358 for (i = 0; include_directories[i] != 0; ++i)
360 char *name = concat (include_directories[i], "/", filename);
361 infile = fopen (name, "r");
362 if (infile == 0)
363 free (name);
364 else
366 filename = name;
367 break;
372 /* Add FILENAME to the chain of read makefiles. */
373 deps = (struct dep *) xmalloc (sizeof (struct dep));
374 deps->next = read_makefiles;
375 read_makefiles = deps;
376 deps->name = 0;
377 deps->file = lookup_file (filename);
378 if (deps->file == 0)
380 deps->file = enter_file (savestring (filename, strlen (filename)));
381 if (flags & RM_DONTCARE)
382 deps->file->dontcare = 1;
384 if (filename != passed_filename)
385 free (filename);
386 filename = deps->file->name;
387 deps->changed = flags;
388 deps = 0;
390 /* If the makefile can't be found at all, give up entirely. */
392 if (infile == 0)
394 /* If we did some searching, errno has the error from the last
395 attempt, rather from FILENAME itself. Restore it in case the
396 caller wants to use it in a message. */
397 errno = makefile_errno;
398 return 0;
401 reading_filename = filename;
402 reading_lineno_ptr = &lineno;
404 /* Loop over lines in the file.
405 The strategy is to accumulate target names in FILENAMES, dependencies
406 in DEPS and commands in COMMANDS. These are used to define a rule
407 when the start of the next rule (or eof) is encountered. */
409 initbuffer (&lb);
411 while (!feof (infile))
413 lineno += nlines;
414 nlines = readline (&lb, infile, filename, lineno);
416 /* Check for a shell command line first.
417 If it is not one, we can stop treating tab specially. */
418 if (lb.buffer[0] == '\t')
420 /* This line is a probably shell command. */
421 unsigned int len;
423 if (no_targets)
424 /* Ignore the commands in a rule with no targets. */
425 continue;
427 /* If there is no preceding rule line, don't treat this line
428 as a command, even though it begins with a tab character.
429 SunOS 4 make appears to behave this way. */
431 if (filenames != 0)
433 if (ignoring)
434 /* Yep, this is a shell command, and we don't care. */
435 continue;
437 /* Append this command line to the line being accumulated. */
438 p = lb.buffer;
439 if (commands_idx == 0)
440 cmds_started = lineno;
441 len = strlen (p);
442 if (len + 1 + commands_idx > commands_len)
444 commands_len = (len + 1 + commands_idx) * 2;
445 commands = (char *) xrealloc (commands, commands_len);
447 bcopy (p, &commands[commands_idx], len);
448 commands_idx += len;
449 commands[commands_idx++] = '\n';
451 continue;
455 /* This line is not a shell command line. Don't worry about tabs. */
457 if (collapsed_length < lb.size)
459 collapsed_length = lb.size;
460 if (collapsed != 0)
461 free (collapsed);
462 collapsed = (char *) xmalloc (collapsed_length);
464 strcpy (collapsed, lb.buffer);
465 /* Collapse continuation lines. */
466 collapse_continuations (collapsed);
467 remove_comments (collapsed);
469 /* Compare a word, both length and contents. */
470 #define word1eq(s, l) (len == l && !strncmp (s, p, l))
471 p = collapsed;
472 while (isspace (*p))
473 ++p;
474 if (*p == '\0')
475 /* This line is completely empty. */
476 continue;
478 /* Find the end of the first token. Note we don't need to worry about
479 * ":" here since we compare tokens by length (so "export" will never
480 * be equal to "export:").
482 for (p2 = p+1; *p2 != '\0' && !isspace(*p2); ++p2)
484 len = p2 - p;
486 /* Find the start of the second token. If it's a `:' remember it,
487 since it can't be a preprocessor token--this allows targets named
488 `ifdef', `export', etc. */
489 reading_target = 0;
490 while (isspace (*p2))
491 ++p2;
492 if (*p2 == '\0')
493 p2 = NULL;
494 else if (p2[0] == ':' && p2[1] == '\0')
496 reading_target = 1;
497 goto skip_conditionals;
500 /* We must first check for conditional and `define' directives before
501 ignoring anything, since they control what we will do with
502 following lines. */
504 if (!in_ignored_define
505 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
506 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
507 || word1eq ("else", 4) || word1eq ("endif", 5)))
509 int i = conditional_line (p, filename, lineno);
510 if (i >= 0)
511 ignoring = i;
512 else
513 makefile_fatal (filename, lineno,
514 "invalid syntax in conditional");
515 continue;
518 if (word1eq ("endef", 5))
520 if (in_ignored_define)
521 in_ignored_define = 0;
522 else
523 makefile_fatal (filename, lineno, "extraneous `endef'");
524 continue;
527 if (word1eq ("define", 6))
529 if (ignoring)
530 in_ignored_define = 1;
531 else
533 p2 = next_token (p + 6);
534 /* Let the variable name be the whole rest of the line,
535 with trailing blanks stripped (comments have already been
536 removed), so it could be a complex variable/function
537 reference that might contain blanks. */
538 p = index (p2, '\0');
539 while (isblank (p[-1]))
540 --p;
541 lineno = do_define (p2, p - p2, o_file,
542 lineno, infile, filename);
544 continue;
547 if (word1eq ("override", 8))
549 p2 = next_token (p + 8);
550 if (p2 == 0)
551 makefile_error (filename, lineno, "empty `override' directive");
552 if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
554 if (ignoring)
555 in_ignored_define = 1;
556 else
558 p2 = next_token (p2 + 6);
559 /* Let the variable name be the whole rest of the line,
560 with trailing blanks stripped (comments have already been
561 removed), so it could be a complex variable/function
562 reference that might contain blanks. */
563 p = index (p2, '\0');
564 while (isblank (p[-1]))
565 --p;
566 lineno = do_define (p2, p - p2, o_override,
567 lineno, infile, filename);
570 else if (!ignoring
571 && !try_variable_definition (filename, lineno,
572 p2, o_override))
573 makefile_error (filename, lineno, "empty `override' directive");
575 continue;
577 skip_conditionals:
579 if (ignoring)
580 /* Ignore the line. We continue here so conditionals
581 can appear in the middle of a rule. */
582 continue;
584 if (!reading_target && word1eq ("export", 6))
586 struct variable *v;
587 p2 = next_token (p + 6);
588 if (*p2 == '\0')
589 export_all_variables = 1;
590 v = try_variable_definition (filename, lineno, p2, o_file);
591 if (v != 0)
592 v->export = v_export;
593 else
595 unsigned int len;
596 for (p = find_next_token (&p2, &len); p != 0;
597 p = find_next_token (&p2, &len))
599 v = lookup_variable (p, len);
600 if (v == 0)
601 v = define_variable (p, len, "", o_file, 0);
602 v->export = v_export;
606 else if (!reading_target && word1eq ("unexport", 8))
608 unsigned int len;
609 struct variable *v;
610 p2 = next_token (p + 8);
611 if (*p2 == '\0')
612 export_all_variables = 0;
613 for (p = find_next_token (&p2, &len); p != 0;
614 p = find_next_token (&p2, &len))
616 v = lookup_variable (p, len);
617 if (v == 0)
618 v = define_variable (p, len, "", o_file, 0);
619 v->export = v_noexport;
622 else if (word1eq ("vpath", 5))
624 char *pattern;
625 unsigned int len;
626 p2 = variable_expand (p + 5);
627 p = find_next_token (&p2, &len);
628 if (p != 0)
630 pattern = savestring (p, len);
631 p = find_next_token (&p2, &len);
632 /* No searchpath means remove all previous
633 selective VPATH's with the same pattern. */
635 else
636 /* No pattern means remove all previous selective VPATH's. */
637 pattern = 0;
638 construct_vpath_list (pattern, p);
639 if (pattern != 0)
640 free (pattern);
642 else if (word1eq ("include", 7) || word1eq ("-include", 8)
643 || word1eq ("sinclude", 8))
645 /* We have found an `include' line specifying a nested
646 makefile to be read at this point. */
647 struct conditionals *save, new_conditionals;
648 struct nameseq *files;
649 /* "-include" (vs "include") says no error if the file does not
650 exist. "sinclude" is an alias for this from SGI. */
651 int noerror = p[0] != 'i';
653 p = allocated_variable_expand (next_token (p + (noerror ? 8 : 7)));
654 if (*p == '\0')
656 makefile_error (filename, lineno,
657 "no file name for `%sinclude'",
658 noerror ? "-" : "");
659 continue;
662 /* Parse the list of file names. */
663 p2 = p;
664 files = multi_glob (parse_file_seq (&p2, '\0',
665 sizeof (struct nameseq),
667 sizeof (struct nameseq));
668 free (p);
670 /* Save the state of conditionals and start
671 the included makefile with a clean slate. */
672 save = conditionals;
673 bzero ((char *) &new_conditionals, sizeof new_conditionals);
674 conditionals = &new_conditionals;
676 /* Record the rules that are waiting so they will determine
677 the default goal before those in the included makefile. */
678 record_waiting_files ();
680 /* Read each included makefile. */
681 while (files != 0)
683 struct nameseq *next = files->next;
684 char *name = files->name;
685 free ((char *)files);
686 files = next;
688 if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
689 | (noerror ? RM_DONTCARE : 0)))
690 && ! noerror)
691 makefile_error (filename, lineno,
692 "%s: %s", name, strerror (errno));
693 free(name);
696 /* Free any space allocated by conditional_line. */
697 if (conditionals->ignoring)
698 free (conditionals->ignoring);
699 if (conditionals->seen_else)
700 free (conditionals->seen_else);
702 /* Restore state. */
703 conditionals = save;
704 reading_filename = filename;
705 reading_lineno_ptr = &lineno;
707 #undef word1eq
708 else if (try_variable_definition (filename, lineno, p, o_file))
709 /* This line has been dealt with. */
711 else if (lb.buffer[0] == '\t')
713 p = collapsed; /* Ignore comments. */
714 while (isblank (*p))
715 ++p;
716 if (*p == '\0')
717 /* The line is completely blank; that is harmless. */
718 continue;
719 /* This line starts with a tab but was not caught above
720 because there was no preceding target, and the line
721 might have been usable as a variable definition.
722 But now it is definitely lossage. */
723 makefile_fatal (filename, lineno,
724 "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;
743 /* Record the previous rule. */
745 record_waiting_files ();
747 /* Search the line for an unquoted ; that is not after an
748 unquoted #. */
749 cmdleft = find_char_unquote (lb.buffer, ";#", 0);
750 if (cmdleft != 0 && *cmdleft == '#')
752 /* We found a comment before a semicolon. */
753 *cmdleft = '\0';
754 cmdleft = 0;
756 else if (cmdleft != 0)
757 /* Found one. Cut the line short there before expanding it. */
758 *(cmdleft++) = '\0';
760 collapse_continuations (lb.buffer);
762 /* We can't expand the entire line, since if it's a per-target
763 variable we don't want to expand it. So, walk from the
764 beginning, expanding as we go, and looking for "interesting"
765 chars. The first word is always expandable. */
766 wtype = get_next_mword(lb.buffer, NULL, &lb_next, &len);
767 switch (wtype)
769 case w_eol:
770 if (cmdleft != 0)
771 makefile_fatal (filename, lineno,
772 "missing rule before commands");
773 else
774 /* This line contained a variable reference that
775 expanded to nothing but whitespace. */
776 continue;
778 case w_colon:
779 case w_dcolon:
780 /* We accept and ignore rules without targets for
781 compatibility with SunOS 4 make. */
782 no_targets = 1;
783 continue;
785 default:
786 break;
789 p2 = variable_expand_string(NULL, lb_next, len);
790 while (1)
792 char *colonp;
794 lb_next += len;
795 if (cmdleft == 0)
797 /* Look for a semicolon in the expanded line. */
798 cmdleft = find_char_unquote (p2, ";", 0);
800 if (cmdleft != 0)
802 unsigned long p2_off = p2 - variable_buffer;
803 unsigned long cmd_off = cmdleft - variable_buffer;
804 char *pend = p2 + strlen(p2);
806 /* Append any remnants of lb, then cut the line short
807 at the semicolon. */
808 *cmdleft = '\0';
810 /* One school of thought says that you shouldn't expand
811 here, but merely copy, since now you're beyond a ";"
812 and into a command script. However, the old parser
813 expanded the whole line, so we continue that for
814 backwards-compatiblity. Also, it wouldn't be
815 entirely consistent, since we do an unconditional
816 expand below once we know we don't have a
817 target-specific variable. */
818 (void)variable_expand_string(pend, lb_next, -1);
819 lb_next += strlen(lb_next);
820 p2 = variable_buffer + p2_off;
821 cmdleft = variable_buffer + cmd_off + 1;
825 colonp = find_char_unquote(p2, ":", 0);
826 #if defined(__MSDOS__) || defined(WINDOWS32)
827 /* The drive spec brain-damage strikes again... */
828 /* FIXME: is whitespace the only possible separator of words
829 in this context? If not, the `isspace' test below will
830 need to be changed into a call to `index'. */
831 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
832 colonp > p2 && isalpha(colonp[-1]) &&
833 (colonp == p2 + 1 || isspace(colonp[-2])))
834 colonp = find_char_unquote(colonp + 1, ":", 0);
835 #endif
836 if (colonp != 0)
837 break;
839 wtype = get_next_mword(lb_next, NULL, &lb_next, &len);
840 if (wtype == w_eol)
841 makefile_fatal (filename, lineno, "missing separator");
843 p2 += strlen(p2);
844 *(p2++) = ' ';
845 p2 = variable_expand_string(p2, lb_next, len);
846 /* We don't need to worry about cmdleft here, because if it was
847 found in the variable_buffer the entire buffer has already
848 been expanded... we'll never get here. */
851 p2 = next_token (variable_buffer);
853 filenames = multi_glob (parse_file_seq (&p2, ':',
854 sizeof (struct nameseq),
856 sizeof (struct nameseq));
858 if (!filenames)
860 /* We accept and ignore rules without targets for
861 compatibility with SunOS 4 make. */
862 no_targets = 1;
863 continue;
865 /* This should never be possible; we handled it above. */
866 assert(*p2 != '\0');
867 ++p2;
869 /* Is this a one-colon or two-colon entry? */
870 two_colon = *p2 == ':';
871 if (two_colon)
872 p2++;
874 /* Test to see if it's a target-specific variable. Copy the rest
875 of the buffer over, possibly temporarily (we'll expand it later
876 if it's not a target-specific variable). PLEN saves the length
877 of the unparsed section of p2, for later. */
878 if (*lb_next != '\0')
880 unsigned int l = p2 - variable_buffer;
881 plen = strlen(p2);
882 (void)variable_buffer_output(p2+plen,
883 lb_next, strlen(lb_next)+1);
884 p2 = variable_buffer + l;
886 wtype = get_next_mword(p2, NULL, &p, &len);
887 v_origin = o_file;
888 if (wtype == w_static && (len == (sizeof("override")-1)
889 && !strncmp(p, "override", len)))
891 v_origin = o_override;
892 (void)get_next_mword(p+len, NULL, &p, &len);
894 else if (wtype != w_eol)
895 wtype = get_next_mword(p+len, NULL, NULL, NULL);
897 if (wtype == w_varassign || v_origin == o_override)
899 record_target_var(filenames, p, two_colon, v_origin,
900 filename, lineno);
901 filenames = 0;
902 continue;
905 /* This is a normal target, _not_ a target-specific variable.
906 Unquote any = in the dependency list. */
907 find_char_unquote (lb_next, "=", 0);
909 /* We have some targets, so don't ignore the following commands. */
910 no_targets = 0;
912 /* Expand the dependencies, etc. */
913 if (*lb_next != '\0')
915 unsigned int l = p2 - variable_buffer;
916 (void)variable_expand_string(p2 + plen, lb_next, -1);
917 p2 = variable_buffer + l;
919 /* Look for a semicolon in the expanded line. */
920 if (cmdleft == 0)
922 cmdleft = find_char_unquote (p2, ";", 0);
923 if (cmdleft != 0)
924 *(cmdleft++) = '\0';
928 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
929 p = index (p2, ':');
930 while (p != 0 && p[-1] == '\\')
932 register char *q = &p[-1];
933 register int backslash = 0;
934 while (*q-- == '\\')
935 backslash = !backslash;
936 if (backslash)
937 p = index (p + 1, ':');
938 else
939 break;
941 #ifdef _AMIGA
942 /* Here, the situation is quite complicated. Let's have a look
943 at a couple of targets:
945 install: dev:make
947 dev:make: make
949 dev:make:: xyz
951 The rule is that it's only a target, if there are TWO :'s
952 OR a space around the :.
954 if (p && !(isspace(p[1]) || !p[1] || isspace(p[-1])))
955 p = 0;
956 #endif
957 #if defined (WINDOWS32) || defined (__MSDOS__)
958 do {
959 check_again = 0;
960 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
961 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
962 isalpha(p[-1]) && (p == p2 + 1 || index(" \t:", p[-2]) != 0)) {
963 p = index(p + 1, ':');
964 check_again = 1;
966 } while (check_again);
967 #endif
968 if (p != 0)
970 struct nameseq *target;
971 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
972 ++p2;
973 if (target == 0)
974 makefile_fatal (filename, lineno, "missing target pattern");
975 else if (target->next != 0)
976 makefile_fatal (filename, lineno, "multiple target patterns");
977 pattern = target->name;
978 pattern_percent = find_percent (pattern);
979 if (pattern_percent == 0)
980 makefile_fatal (filename, lineno,
981 "target pattern contains no `%%'");
982 free((char *)target);
984 else
985 pattern = 0;
987 /* Parse the dependencies. */
988 deps = (struct dep *)
989 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
990 sizeof (struct dep));
992 commands_idx = 0;
993 if (cmdleft != 0)
995 /* Semicolon means rest of line is a command. */
996 unsigned int len = strlen (cmdleft);
998 cmds_started = lineno;
1000 /* Add this command line to the buffer. */
1001 if (len + 2 > commands_len)
1003 commands_len = (len + 2) * 2;
1004 commands = (char *) xrealloc (commands, commands_len);
1006 bcopy (cmdleft, commands, len);
1007 commands_idx += len;
1008 commands[commands_idx++] = '\n';
1011 continue;
1014 /* We get here except in the case that we just read a rule line.
1015 Record now the last rule we read, so following spurious
1016 commands are properly diagnosed. */
1017 record_waiting_files ();
1018 no_targets = 0;
1021 if (conditionals->if_cmds)
1022 makefile_fatal (filename, lineno, "missing `endif'");
1024 /* At eof, record the last rule. */
1025 record_waiting_files ();
1027 freebuffer (&lb);
1028 free ((char *) commands);
1029 fclose (infile);
1031 reading_filename = 0;
1032 reading_lineno_ptr = 0;
1034 return 1;
1037 /* Execute a `define' directive.
1038 The first line has already been read, and NAME is the name of
1039 the variable to be defined. The following lines remain to be read.
1040 LINENO, INFILE and FILENAME refer to the makefile being read.
1041 The value returned is LINENO, updated for lines read here. */
1043 static unsigned int
1044 do_define (name, namelen, origin, lineno, infile, filename)
1045 char *name;
1046 unsigned int namelen;
1047 enum variable_origin origin;
1048 unsigned int lineno;
1049 FILE *infile;
1050 char *filename;
1052 struct linebuffer lb;
1053 unsigned int nlines = 0;
1054 unsigned int length = 100;
1055 char *definition = (char *) xmalloc (100);
1056 register unsigned int idx = 0;
1057 register char *p;
1059 /* Expand the variable name. */
1060 char *var = (char *) alloca (namelen + 1);
1061 bcopy (name, var, namelen);
1062 var[namelen] = '\0';
1063 var = variable_expand (var);
1065 initbuffer (&lb);
1066 while (!feof (infile))
1068 unsigned int len;
1070 lineno += nlines;
1071 nlines = readline (&lb, infile, filename, lineno);
1073 collapse_continuations (lb.buffer);
1075 p = next_token (lb.buffer);
1076 len = strlen (p);
1077 if ((len == 5 || (len > 5 && isblank (p[5])))
1078 && !strncmp (p, "endef", 5))
1080 p += 5;
1081 remove_comments (p);
1082 if (*next_token (p) != '\0')
1083 makefile_error (filename, lineno,
1084 "Extraneous text after `endef' directive");
1085 /* Define the variable. */
1086 if (idx == 0)
1087 definition[0] = '\0';
1088 else
1089 definition[idx - 1] = '\0';
1090 (void) define_variable (var, strlen (var), definition, origin, 1);
1091 free (definition);
1092 freebuffer (&lb);
1093 return (lineno + nlines);
1095 else
1097 len = strlen (lb.buffer);
1098 /* Increase the buffer size if necessary. */
1099 if (idx + len + 1 > length)
1101 length = (idx + len) * 2;
1102 definition = (char *) xrealloc (definition, length + 1);
1105 bcopy (lb.buffer, &definition[idx], len);
1106 idx += len;
1107 /* Separate lines with a newline. */
1108 definition[idx++] = '\n';
1112 /* No `endef'!! */
1113 makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
1115 /* NOTREACHED */
1116 return 0;
1119 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1120 "ifneq", "else" and "endif".
1121 LINE is the input line, with the command as its first word.
1123 FILENAME and LINENO are the filename and line number in the
1124 current makefile. They are used for error messages.
1126 Value is -1 if the line is invalid,
1127 0 if following text should be interpreted,
1128 1 if following text should be ignored. */
1130 static int
1131 conditional_line (line, filename, lineno)
1132 char *line;
1133 char *filename;
1134 unsigned int lineno;
1136 int notdef;
1137 char *cmdname;
1138 register unsigned int i;
1140 if (*line == 'i')
1142 /* It's an "if..." command. */
1143 notdef = line[2] == 'n';
1144 if (notdef)
1146 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
1147 line += cmdname[3] == 'd' ? 7 : 6;
1149 else
1151 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
1152 line += cmdname[2] == 'd' ? 6 : 5;
1155 else
1157 /* It's an "else" or "endif" command. */
1158 notdef = line[1] == 'n';
1159 cmdname = notdef ? "endif" : "else";
1160 line += notdef ? 5 : 4;
1163 line = next_token (line);
1165 if (*cmdname == 'e')
1167 if (*line != '\0')
1168 makefile_error (filename, lineno,
1169 "Extraneous text after `%s' directive",
1170 cmdname);
1171 /* "Else" or "endif". */
1172 if (conditionals->if_cmds == 0)
1173 makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
1174 /* NOTDEF indicates an `endif' command. */
1175 if (notdef)
1176 --conditionals->if_cmds;
1177 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1178 makefile_fatal (filename, lineno, "only one `else' per conditional");
1179 else
1181 /* Toggle the state of ignorance. */
1182 conditionals->ignoring[conditionals->if_cmds - 1]
1183 = !conditionals->ignoring[conditionals->if_cmds - 1];
1184 /* Record that we have seen an `else' in this conditional.
1185 A second `else' will be erroneous. */
1186 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1188 for (i = 0; i < conditionals->if_cmds; ++i)
1189 if (conditionals->ignoring[i])
1190 return 1;
1191 return 0;
1194 if (conditionals->allocated == 0)
1196 conditionals->allocated = 5;
1197 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1198 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1201 ++conditionals->if_cmds;
1202 if (conditionals->if_cmds > conditionals->allocated)
1204 conditionals->allocated += 5;
1205 conditionals->ignoring = (char *)
1206 xrealloc (conditionals->ignoring, conditionals->allocated);
1207 conditionals->seen_else = (char *)
1208 xrealloc (conditionals->seen_else, conditionals->allocated);
1211 /* Record that we have seen an `if...' but no `else' so far. */
1212 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1214 /* Search through the stack to see if we're already ignoring. */
1215 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1216 if (conditionals->ignoring[i])
1218 /* We are already ignoring, so just push a level
1219 to match the next "else" or "endif", and keep ignoring.
1220 We don't want to expand variables in the condition. */
1221 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1222 return 1;
1225 if (cmdname[notdef ? 3 : 2] == 'd')
1227 /* "Ifdef" or "ifndef". */
1228 struct variable *v;
1229 register char *p = end_of_token (line);
1230 i = p - line;
1231 p = next_token (p);
1232 if (*p != '\0')
1233 return -1;
1234 v = lookup_variable (line, i);
1235 conditionals->ignoring[conditionals->if_cmds - 1]
1236 = (v != 0 && *v->value != '\0') == notdef;
1238 else
1240 /* "Ifeq" or "ifneq". */
1241 char *s1, *s2;
1242 unsigned int len;
1243 char termin = *line == '(' ? ',' : *line;
1245 if (termin != ',' && termin != '"' && termin != '\'')
1246 return -1;
1248 s1 = ++line;
1249 /* Find the end of the first string. */
1250 if (termin == ',')
1252 register int count = 0;
1253 for (; *line != '\0'; ++line)
1254 if (*line == '(')
1255 ++count;
1256 else if (*line == ')')
1257 --count;
1258 else if (*line == ',' && count <= 0)
1259 break;
1261 else
1262 while (*line != '\0' && *line != termin)
1263 ++line;
1265 if (*line == '\0')
1266 return -1;
1268 if (termin == ',')
1270 /* Strip blanks after the first string. */
1271 char *p = line++;
1272 while (isblank (p[-1]))
1273 --p;
1274 *p = '\0';
1276 else
1277 *line++ = '\0';
1279 s2 = variable_expand (s1);
1280 /* We must allocate a new copy of the expanded string because
1281 variable_expand re-uses the same buffer. */
1282 len = strlen (s2);
1283 s1 = (char *) alloca (len + 1);
1284 bcopy (s2, s1, len + 1);
1286 if (termin != ',')
1287 /* Find the start of the second string. */
1288 line = next_token (line);
1290 termin = termin == ',' ? ')' : *line;
1291 if (termin != ')' && termin != '"' && termin != '\'')
1292 return -1;
1294 /* Find the end of the second string. */
1295 if (termin == ')')
1297 register int count = 0;
1298 s2 = next_token (line);
1299 for (line = s2; *line != '\0'; ++line)
1301 if (*line == '(')
1302 ++count;
1303 else if (*line == ')')
1304 if (count <= 0)
1305 break;
1306 else
1307 --count;
1310 else
1312 ++line;
1313 s2 = line;
1314 while (*line != '\0' && *line != termin)
1315 ++line;
1318 if (*line == '\0')
1319 return -1;
1321 *line = '\0';
1322 line = next_token (++line);
1323 if (*line != '\0')
1324 makefile_error (filename, lineno,
1325 "Extraneous text after `%s' directive",
1326 cmdname);
1328 s2 = variable_expand (s2);
1329 conditionals->ignoring[conditionals->if_cmds - 1]
1330 = streq (s1, s2) == notdef;
1333 /* Search through the stack to see if we're ignoring. */
1334 for (i = 0; i < conditionals->if_cmds; ++i)
1335 if (conditionals->ignoring[i])
1336 return 1;
1337 return 0;
1340 /* Remove duplicate dependencies in CHAIN. */
1342 void
1343 uniquize_deps (chain)
1344 struct dep *chain;
1346 register struct dep *d;
1348 /* Make sure that no dependencies are repeated. This does not
1349 really matter for the purpose of updating targets, but it
1350 might make some names be listed twice for $^ and $?. */
1352 for (d = chain; d != 0; d = d->next)
1354 struct dep *last, *next;
1356 last = d;
1357 next = d->next;
1358 while (next != 0)
1359 if (streq (dep_name (d), dep_name (next)))
1361 struct dep *n = next->next;
1362 last->next = n;
1363 if (next->name != 0 && next->name != d->name)
1364 free (next->name);
1365 if (next != d)
1366 free ((char *) next);
1367 next = n;
1369 else
1371 last = next;
1372 next = next->next;
1377 /* Record target-specific variable values for files FILENAMES.
1378 TWO_COLON is nonzero if a double colon was used.
1380 The links of FILENAMES are freed, and so are any names in it
1381 that are not incorporated into other data structures.
1383 If the target is a pattern, add the variable to the pattern-specific
1384 variable value list. */
1386 static void
1387 record_target_var (filenames, defn, two_colon, origin, filename, lineno)
1388 struct nameseq *filenames;
1389 char *defn;
1390 int two_colon;
1391 enum variable_origin origin;
1392 char *filename;
1393 unsigned int lineno;
1395 struct nameseq *nextf;
1396 struct variable_set_list *global;
1398 global = current_variable_set_list;
1400 for (; filenames != 0; filenames = nextf)
1402 struct variable *v;
1403 register char *name = filenames->name;
1404 struct variable_set_list *vlist;
1405 char *fname;
1406 char *percent;
1408 nextf = filenames->next;
1409 free ((char *) filenames);
1411 /* If it's a pattern target, then add it to the pattern-specific
1412 variable list. */
1413 percent = find_percent (name);
1414 if (percent)
1416 struct pattern_var *p;
1418 /* Get a reference for this pattern-specific variable struct. */
1419 p = create_pattern_var(name, percent);
1420 vlist = p->vars;
1421 fname = p->target;
1423 else
1425 struct file *f;
1427 /* Get a file reference for this file, and initialize it. */
1428 f = enter_file (name);
1429 initialize_file_variables (f);
1430 vlist = f->variables;
1431 fname = f->name;
1434 /* Make the new variable context current and define the variable. */
1435 current_variable_set_list = vlist;
1436 v = try_variable_definition(filename, lineno, defn, origin);
1437 if (!v)
1438 makefile_error(filename, lineno,
1439 "Malformed per-target variable definition");
1440 v->per_target = 1;
1442 /* If it's not an override, check to see if there was a command-line
1443 setting. If so, reset the value. */
1444 if (origin != o_override)
1446 struct variable *gv;
1447 int len = strlen(v->name);
1449 current_variable_set_list = global;
1450 gv = lookup_variable(v->name, len);
1451 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1452 define_variable_in_set(v->name, len, gv->value, gv->origin,
1453 gv->recursive, vlist->set);
1456 /* Free name if not needed further. */
1457 if (name != fname && (name < fname || name > fname + strlen (fname)))
1458 free (name);
1461 current_variable_set_list = global;
1464 /* Record a description line for files FILENAMES,
1465 with dependencies DEPS, commands to execute described
1466 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1467 TWO_COLON is nonzero if a double colon was used.
1468 If not nil, PATTERN is the `%' pattern to make this
1469 a static pattern rule, and PATTERN_PERCENT is a pointer
1470 to the `%' within it.
1472 The links of FILENAMES are freed, and so are any names in it
1473 that are not incorporated into other data structures. */
1475 static void
1476 record_files (filenames, pattern, pattern_percent, deps, cmds_started,
1477 commands, commands_idx, two_colon, filename, lineno, set_default)
1478 struct nameseq *filenames;
1479 char *pattern, *pattern_percent;
1480 struct dep *deps;
1481 unsigned int cmds_started;
1482 char *commands;
1483 unsigned int commands_idx;
1484 int two_colon;
1485 char *filename;
1486 unsigned int lineno;
1487 int set_default;
1489 struct nameseq *nextf;
1490 int implicit = 0;
1491 unsigned int max_targets = 0, target_idx = 0;
1492 char **targets = 0, **target_percents = 0;
1493 struct commands *cmds;
1495 if (commands_idx > 0)
1497 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1498 cmds->filename = filename;
1499 cmds->lineno = cmds_started;
1500 cmds->commands = savestring (commands, commands_idx);
1501 cmds->command_lines = 0;
1503 else
1504 cmds = 0;
1506 for (; filenames != 0; filenames = nextf)
1509 register char *name = filenames->name;
1510 register struct file *f;
1511 register struct dep *d;
1512 struct dep *this;
1513 char *implicit_percent;
1515 nextf = filenames->next;
1516 free ((char *) filenames);
1518 implicit_percent = find_percent (name);
1519 implicit |= implicit_percent != 0;
1521 if (implicit && pattern != 0)
1522 makefile_fatal (filename, lineno,
1523 "mixed implicit and static pattern rules");
1525 if (implicit && implicit_percent == 0)
1526 makefile_fatal (filename, lineno, "mixed implicit and normal rules");
1528 if (implicit)
1530 if (targets == 0)
1532 max_targets = 5;
1533 targets = (char **) xmalloc (5 * sizeof (char *));
1534 target_percents = (char **) xmalloc (5 * sizeof (char *));
1535 target_idx = 0;
1537 else if (target_idx == max_targets - 1)
1539 max_targets += 5;
1540 targets = (char **) xrealloc ((char *) targets,
1541 max_targets * sizeof (char *));
1542 target_percents
1543 = (char **) xrealloc ((char *) target_percents,
1544 max_targets * sizeof (char *));
1546 targets[target_idx] = name;
1547 target_percents[target_idx] = implicit_percent;
1548 ++target_idx;
1549 continue;
1552 /* If there are multiple filenames, copy the chain DEPS
1553 for all but the last one. It is not safe for the same deps
1554 to go in more than one place in the data base. */
1555 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1557 if (pattern != 0)
1558 /* If this is an extended static rule:
1559 `targets: target%pattern: dep%pattern; cmds',
1560 translate each dependency pattern into a plain filename
1561 using the target pattern and this target's name. */
1562 if (!pattern_matches (pattern, pattern_percent, name))
1564 /* Give a warning if the rule is meaningless. */
1565 makefile_error (filename, lineno,
1566 "target `%s' doesn't match the target pattern",
1567 name);
1568 this = 0;
1570 else
1572 /* We use patsubst_expand to do the work of translating
1573 the target pattern, the target's name and the dependencies'
1574 patterns into plain dependency names. */
1575 char *buffer = variable_expand ("");
1577 for (d = this; d != 0; d = d->next)
1579 char *o;
1580 char *percent = find_percent (d->name);
1581 if (percent == 0)
1582 continue;
1583 o = patsubst_expand (buffer, name, pattern, d->name,
1584 pattern_percent, percent);
1585 free (d->name);
1586 d->name = savestring (buffer, o - buffer);
1590 if (!two_colon)
1592 /* Single-colon. Combine these dependencies
1593 with others in file's existing record, if any. */
1594 f = enter_file (name);
1596 if (f->double_colon)
1597 makefile_fatal (filename, lineno,
1598 "target file `%s' has both : and :: entries",
1599 f->name);
1601 /* If CMDS == F->CMDS, this target was listed in this rule
1602 more than once. Just give a warning since this is harmless. */
1603 if (cmds != 0 && cmds == f->cmds)
1604 makefile_error
1605 (filename, lineno,
1606 "target `%s' given more than once in the same rule.",
1607 f->name);
1609 /* Check for two single-colon entries both with commands.
1610 Check is_target so that we don't lose on files such as .c.o
1611 whose commands were preinitialized. */
1612 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1614 makefile_error (cmds->filename, cmds->lineno,
1615 "warning: overriding commands for target `%s'",
1616 f->name);
1617 makefile_error (f->cmds->filename, f->cmds->lineno,
1618 "warning: ignoring old commands for target `%s'",
1619 f->name);
1622 f->is_target = 1;
1624 /* Defining .DEFAULT with no deps or cmds clears it. */
1625 if (f == default_file && this == 0 && cmds == 0)
1626 f->cmds = 0;
1627 if (cmds != 0)
1628 f->cmds = cmds;
1629 /* Defining .SUFFIXES with no dependencies
1630 clears out the list of suffixes. */
1631 if (f == suffix_file && this == 0)
1633 d = f->deps;
1634 while (d != 0)
1636 struct dep *nextd = d->next;
1637 free (d->name);
1638 free ((char *)d);
1639 d = nextd;
1641 f->deps = 0;
1643 else if (f->deps != 0)
1645 /* Add the file's old deps and the new ones in THIS together. */
1647 struct dep *firstdeps, *moredeps;
1648 if (cmds != 0)
1650 /* This is the rule with commands, so put its deps first.
1651 The rationale behind this is that $< expands to the
1652 first dep in the chain, and commands use $< expecting
1653 to get the dep that rule specifies. */
1654 firstdeps = this;
1655 moredeps = f->deps;
1657 else
1659 /* Append the new deps to the old ones. */
1660 firstdeps = f->deps;
1661 moredeps = this;
1664 if (firstdeps == 0)
1665 firstdeps = moredeps;
1666 else
1668 d = firstdeps;
1669 while (d->next != 0)
1670 d = d->next;
1671 d->next = moredeps;
1674 f->deps = firstdeps;
1676 else
1677 f->deps = this;
1679 /* If this is a static pattern rule, set the file's stem to
1680 the part of its name that matched the `%' in the pattern,
1681 so you can use $* in the commands. */
1682 if (pattern != 0)
1684 static char *percent = "%";
1685 char *buffer = variable_expand ("");
1686 char *o = patsubst_expand (buffer, name, pattern, percent,
1687 pattern_percent, percent);
1688 f->stem = savestring (buffer, o - buffer);
1691 else
1693 /* Double-colon. Make a new record
1694 even if the file already has one. */
1695 f = lookup_file (name);
1696 /* Check for both : and :: rules. Check is_target so
1697 we don't lose on default suffix rules or makefiles. */
1698 if (f != 0 && f->is_target && !f->double_colon)
1699 makefile_fatal (filename, lineno,
1700 "target file `%s' has both : and :: entries",
1701 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 (strncmp (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 = savestring(q, strlen(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 int
2093 readline (linebuffer, stream, filename, lineno)
2094 struct linebuffer *linebuffer;
2095 FILE *stream;
2096 char *filename;
2097 unsigned int lineno;
2099 char *buffer = linebuffer->buffer;
2100 register char *p = linebuffer->buffer;
2101 register char *end = p + linebuffer->size;
2102 register int len, lastlen = 0;
2103 register char *p2;
2104 register unsigned int nlines = 0;
2105 register int backslash;
2107 *p = '\0';
2109 while (fgets (p, end - p, stream) != 0)
2111 len = strlen (p);
2112 if (len == 0)
2114 /* This only happens when the first thing on the line is a '\0'.
2115 It is a pretty hopeless case, but (wonder of wonders) Athena
2116 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2117 There is nothing really to be done; we synthesize a newline so
2118 the following line doesn't appear to be part of this line. */
2119 makefile_error (filename, lineno,
2120 "warning: NUL character seen; rest of line ignored");
2121 p[0] = '\n';
2122 len = 1;
2125 p += len;
2126 if (p[-1] != '\n')
2128 /* Probably ran out of buffer space. */
2129 register unsigned int p_off = p - buffer;
2130 linebuffer->size *= 2;
2131 buffer = (char *) xrealloc (buffer, linebuffer->size);
2132 p = buffer + p_off;
2133 end = buffer + linebuffer->size;
2134 linebuffer->buffer = buffer;
2135 *p = '\0';
2136 lastlen = len;
2137 continue;
2140 ++nlines;
2142 if (len == 1 && p > buffer)
2143 /* P is pointing at a newline and it's the beginning of
2144 the buffer returned by the last fgets call. However,
2145 it is not necessarily the beginning of a line if P is
2146 pointing past the beginning of the holding buffer.
2147 If the buffer was just enlarged (right before the newline),
2148 we must account for that, so we pretend that the two lines
2149 were one line. */
2150 len += lastlen;
2151 lastlen = len;
2152 backslash = 0;
2153 for (p2 = p - 2; --len > 0; --p2)
2155 if (*p2 == '\\')
2156 backslash = !backslash;
2157 else
2158 break;
2161 if (!backslash)
2163 p[-1] = '\0';
2164 break;
2167 if (end - p <= 1)
2169 /* Enlarge the buffer. */
2170 register unsigned int p_off = p - buffer;
2171 linebuffer->size *= 2;
2172 buffer = (char *) xrealloc (buffer, linebuffer->size);
2173 p = buffer + p_off;
2174 end = buffer + linebuffer->size;
2175 linebuffer->buffer = buffer;
2179 if (ferror (stream))
2180 pfatal_with_name (filename);
2182 return nlines;
2185 /* Parse the next "makefile word" from the input buffer, and return info
2186 about it.
2188 A "makefile word" is one of:
2190 w_bogus Should never happen
2191 w_eol End of input
2192 w_static A static word; cannot be expanded
2193 w_variable A word containing one or more variables/functions
2194 w_colon A colon
2195 w_dcolon A double-colon
2196 w_semicolon A semicolon
2197 w_comment A comment character
2198 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2200 Note that this function is only used when reading certain parts of the
2201 makefile. Don't use it where special rules hold sway (RHS of a variable,
2202 in a command list, etc.) */
2204 static enum make_word_type
2205 get_next_mword (buffer, delim, startp, length)
2206 char *buffer;
2207 char *delim;
2208 char **startp;
2209 unsigned int *length;
2211 enum make_word_type wtype = w_bogus;
2212 char *p = buffer, *beg;
2213 char c;
2215 /* Skip any leading whitespace. */
2216 while (isblank(*p))
2217 ++p;
2219 beg = p;
2220 c = *(p++);
2221 switch (c)
2223 case '\0':
2224 wtype = w_eol;
2225 break;
2227 case '#':
2228 wtype = w_comment;
2229 break;
2231 case ';':
2232 wtype = w_semicolon;
2233 break;
2235 case '=':
2236 wtype = w_varassign;
2237 break;
2239 case ':':
2240 wtype = w_colon;
2241 switch (*p)
2243 case ':':
2244 ++p;
2245 wtype = w_dcolon;
2246 break;
2248 case '=':
2249 ++p;
2250 wtype = w_varassign;
2251 break;
2253 break;
2255 case '+':
2256 case '?':
2257 if (*p == '=')
2259 ++p;
2260 wtype = w_varassign;
2261 break;
2264 default:
2265 if (delim && index(delim, c))
2266 wtype = w_static;
2267 break;
2270 /* Did we find something? If so, return now. */
2271 if (wtype != w_bogus)
2272 goto done;
2274 /* This is some non-operator word. A word consists of the longest
2275 string of characters that doesn't contain whitespace, one of [:=#],
2276 or [?+]=, or one of the chars in the DELIM string. */
2278 /* We start out assuming a static word; if we see a variable we'll
2279 adjust our assumptions then. */
2280 wtype = w_static;
2282 /* We already found the first value of "c", above. */
2283 while (1)
2285 char closeparen;
2286 int count;
2288 switch (c)
2290 case '\0':
2291 case ' ':
2292 case '\t':
2293 case '=':
2294 case '#':
2295 goto done_word;
2297 case ':':
2298 #if defined(__MSDOS__) || defined(WINDOWS32)
2299 /* A word CAN include a colon in its drive spec. */
2300 if (!(p - beg == 2 && (*p == '/' || *p == '\\') && isalpha (*beg)))
2301 #endif
2302 goto done_word;
2304 case '$':
2305 c = *(p++);
2306 if (c == '$')
2307 break;
2309 /* This is a variable reference, so note that it's expandable.
2310 Then read it to the matching close paren. */
2311 wtype = w_variable;
2313 if (c == '(')
2314 closeparen = ')';
2315 else if (c == '{')
2316 closeparen = '}';
2317 else
2318 /* This is a single-letter variable reference. */
2319 break;
2321 for (count=0; *p != '\0'; ++p)
2323 if (*p == c)
2324 ++count;
2325 else if (*p == closeparen && --count < 0)
2327 ++p;
2328 break;
2331 break;
2333 case '?':
2334 case '+':
2335 if (*p == '=')
2336 goto done_word;
2337 break;
2339 case '\\':
2340 switch (*p)
2342 case ';':
2343 case '=':
2344 case '\\':
2345 ++p;
2346 break;
2348 break;
2350 default:
2351 if (delim && index(delim, c))
2352 goto done_word;
2353 break;
2356 c = *(p++);
2358 done_word:
2359 --p;
2361 done:
2362 if (startp)
2363 *startp = beg;
2364 if (length)
2365 *length = p - beg;
2366 return wtype;
2369 /* Construct the list of include directories
2370 from the arguments and the default list. */
2372 void
2373 construct_include_path (arg_dirs)
2374 char **arg_dirs;
2376 register unsigned int i;
2377 #ifdef VAXC /* just don't ask ... */
2378 stat_t stbuf;
2379 #else
2380 struct stat stbuf;
2381 #endif
2382 /* Table to hold the dirs. */
2384 register unsigned int defsize = (sizeof (default_include_directories)
2385 / sizeof (default_include_directories[0]));
2386 register unsigned int max = 5;
2387 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
2388 register unsigned int idx = 0;
2390 #ifdef __MSDOS__
2391 defsize++;
2392 #endif
2394 /* First consider any dirs specified with -I switches.
2395 Ignore dirs that don't exist. */
2397 if (arg_dirs != 0)
2398 while (*arg_dirs != 0)
2400 char *dir = *arg_dirs++;
2402 if (dir[0] == '~')
2404 char *expanded = tilde_expand (dir);
2405 if (expanded != 0)
2406 dir = expanded;
2409 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
2411 if (idx == max - 1)
2413 max += 5;
2414 dirs = (char **)
2415 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
2417 dirs[idx++] = dir;
2419 else if (dir != arg_dirs[-1])
2420 free (dir);
2423 /* Now add at the end the standard default dirs. */
2425 #ifdef __MSDOS__
2427 /* The environment variable $DJDIR holds the root of the
2428 DJGPP directory tree; add ${DJDIR}/include. */
2429 struct variable *djdir = lookup_variable ("DJDIR", 5);
2431 if (djdir)
2433 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2435 strcat (strcpy (defdir, djdir->value), "/include");
2436 dirs[idx++] = defdir;
2439 #endif
2441 for (i = 0; default_include_directories[i] != 0; ++i)
2442 if (stat (default_include_directories[i], &stbuf) == 0
2443 && S_ISDIR (stbuf.st_mode))
2444 dirs[idx++] = default_include_directories[i];
2446 dirs[idx] = 0;
2448 /* Now compute the maximum length of any name in it. */
2450 max_incl_len = 0;
2451 for (i = 0; i < idx; ++i)
2453 unsigned int len = strlen (dirs[i]);
2454 /* If dir name is written with a trailing slash, discard it. */
2455 if (dirs[i][len - 1] == '/')
2456 /* We can't just clobber a null in because it may have come from
2457 a literal string and literal strings may not be writable. */
2458 dirs[i] = savestring (dirs[i], len - 1);
2459 if (len > max_incl_len)
2460 max_incl_len = len;
2463 include_directories = dirs;
2466 /* Expand ~ or ~USER at the beginning of NAME.
2467 Return a newly malloc'd string or 0. */
2469 char *
2470 tilde_expand (name)
2471 char *name;
2473 #ifndef VMS
2474 if (name[1] == '/' || name[1] == '\0')
2476 extern char *getenv ();
2477 char *home_dir;
2478 int is_variable;
2481 /* Turn off --warn-undefined-variables while we expand HOME. */
2482 int save = warn_undefined_variables_flag;
2483 warn_undefined_variables_flag = 0;
2485 home_dir = allocated_variable_expand ("$(HOME)");
2487 warn_undefined_variables_flag = save;
2490 is_variable = home_dir[0] != '\0';
2491 if (!is_variable)
2493 free (home_dir);
2494 home_dir = getenv ("HOME");
2496 #if !defined(_AMIGA) && !defined(WINDOWS32)
2497 if (home_dir == 0 || home_dir[0] == '\0')
2499 extern char *getlogin ();
2500 char *logname = getlogin ();
2501 home_dir = 0;
2502 if (logname != 0)
2504 struct passwd *p = getpwnam (logname);
2505 if (p != 0)
2506 home_dir = p->pw_dir;
2509 #endif /* !AMIGA && !WINDOWS32 */
2510 if (home_dir != 0)
2512 char *new = concat (home_dir, "", name + 1);
2513 if (is_variable)
2514 free (home_dir);
2515 return new;
2518 #if !defined(_AMIGA) && !defined(WINDOWS32)
2519 else
2521 struct passwd *pwent;
2522 char *userend = index (name + 1, '/');
2523 if (userend != 0)
2524 *userend = '\0';
2525 pwent = getpwnam (name + 1);
2526 if (pwent != 0)
2528 if (userend == 0)
2529 return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
2530 else
2531 return concat (pwent->pw_dir, "/", userend + 1);
2533 else if (userend != 0)
2534 *userend = '/';
2536 #endif /* !AMIGA && !WINDOWS32 */
2537 #endif /* !VMS */
2538 return 0;
2541 /* Given a chain of struct nameseq's describing a sequence of filenames,
2542 in reverse of the intended order, return a new chain describing the
2543 result of globbing the filenames. The new chain is in forward order.
2544 The links of the old chain are freed or used in the new chain.
2545 Likewise for the names in the old chain.
2547 SIZE is how big to construct chain elements.
2548 This is useful if we want them actually to be other structures
2549 that have room for additional info. */
2551 struct nameseq *
2552 multi_glob (chain, size)
2553 struct nameseq *chain;
2554 unsigned int size;
2556 extern void dir_setup_glob ();
2557 register struct nameseq *new = 0;
2558 register struct nameseq *old;
2559 struct nameseq *nexto;
2560 glob_t gl;
2562 dir_setup_glob (&gl);
2564 for (old = chain; old != 0; old = nexto)
2566 #ifndef NO_ARCHIVES
2567 char *memname;
2568 #endif
2570 nexto = old->next;
2572 if (old->name[0] == '~')
2574 char *newname = tilde_expand (old->name);
2575 if (newname != 0)
2577 free (old->name);
2578 old->name = newname;
2582 #ifndef NO_ARCHIVES
2583 if (ar_name (old->name))
2585 /* OLD->name is an archive member reference.
2586 Replace it with the archive file name,
2587 and save the member name in MEMNAME.
2588 We will glob on the archive name and then
2589 reattach MEMNAME later. */
2590 char *arname;
2591 ar_parse_name (old->name, &arname, &memname);
2592 free (old->name);
2593 old->name = arname;
2595 else
2596 memname = 0;
2597 #endif /* !NO_ARCHIVES */
2599 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
2601 case 0: /* Success. */
2603 register int i = gl.gl_pathc;
2604 while (i-- > 0)
2606 #ifndef NO_ARCHIVES
2607 if (memname != 0)
2609 /* Try to glob on MEMNAME within the archive. */
2610 struct nameseq *found
2611 = ar_glob (gl.gl_pathv[i], memname, size);
2612 if (found == 0)
2614 /* No matches. Use MEMNAME as-is. */
2615 struct nameseq *elt
2616 = (struct nameseq *) xmalloc (size);
2617 unsigned int alen = strlen (gl.gl_pathv[i]);
2618 unsigned int mlen = strlen (memname);
2619 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2620 bcopy (gl.gl_pathv[i], elt->name, alen);
2621 elt->name[alen] = '(';
2622 bcopy (memname, &elt->name[alen + 1], mlen);
2623 elt->name[alen + 1 + mlen] = ')';
2624 elt->name[alen + 1 + mlen + 1] = '\0';
2625 elt->next = new;
2626 new = elt;
2628 else
2630 /* Find the end of the FOUND chain. */
2631 struct nameseq *f = found;
2632 while (f->next != 0)
2633 f = f->next;
2635 /* Attach the chain being built to the end of the FOUND
2636 chain, and make FOUND the new NEW chain. */
2637 f->next = new;
2638 new = found;
2641 free (memname);
2643 else
2644 #endif /* !NO_ARCHIVES */
2646 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2647 elt->name = savestring (gl.gl_pathv[i],
2648 strlen (gl.gl_pathv[i]));
2649 elt->next = new;
2650 new = elt;
2653 globfree (&gl);
2654 free (old->name);
2655 free ((char *)old);
2656 break;
2659 case GLOB_NOSPACE:
2660 fatal ("virtual memory exhausted");
2661 break;
2663 default:
2664 old->next = new;
2665 new = old;
2666 break;
2670 return new;