* Installed new versions of GLIBC glob library.
[make.git] / read.c
blob740d93dbe0ace2676c616d9d5d715aaa22cc9a83
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 <assert.h>
22 #include <glob.h>
24 #include "make.h"
25 #include "dep.h"
26 #include "filedef.h"
27 #include "job.h"
28 #include "commands.h"
29 #include "variable.h"
30 #include "rule.h"
33 #ifndef WINDOWS32
34 #ifndef _AMIGA
35 #ifndef VMS
36 #include <pwd.h>
37 #else
38 struct passwd *getpwnam PARAMS ((char *name));
39 #endif
40 #endif
41 #endif /* !WINDOWS32 */
43 /* A `struct linebuffer' is a structure which holds a line of text.
44 `readline' reads a line from a stream into a linebuffer
45 and works regardless of the length of the line. */
47 struct linebuffer
49 /* Note: This is the number of bytes malloc'ed for `buffer'
50 It does not indicate `buffer's real length.
51 Instead, a null char indicates end-of-string. */
52 unsigned int size;
53 char *buffer;
56 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
57 #define freebuffer(lb) free ((lb)->buffer)
60 /* Types of "words" that can be read in a makefile. */
61 enum make_word_type
63 w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
64 w_comment, w_varassign
68 /* A `struct conditionals' contains the information describing
69 all the active conditionals in a makefile.
71 The global variable `conditionals' contains the conditionals
72 information for the current makefile. It is initialized from
73 the static structure `toplevel_conditionals' and is later changed
74 to new structures for included makefiles. */
76 struct conditionals
78 unsigned int if_cmds; /* Depth of conditional nesting. */
79 unsigned int allocated; /* Elts allocated in following arrays. */
80 char *ignoring; /* Are we ignoring or interepreting? */
81 char *seen_else; /* Have we already seen an `else'? */
84 static struct conditionals toplevel_conditionals;
85 static struct conditionals *conditionals = &toplevel_conditionals;
88 /* Default directories to search for include files in */
90 static char *default_include_directories[] =
92 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
94 * This completly up to the user when they install MSVC or other packages.
95 * This is defined as a placeholder.
97 #define INCLUDEDIR "."
98 #endif
99 INCLUDEDIR,
100 #ifndef _AMIGA
101 "/usr/gnu/include",
102 "/usr/local/include",
103 "/usr/include",
104 #endif
108 /* List of directories to search for include files in */
110 static char **include_directories;
112 /* Maximum length of an element of the above. */
114 static unsigned int max_incl_len;
116 /* The filename and pointer to line number of the
117 makefile currently being read in. */
119 const struct floc *reading_file;
121 /* The chain of makefiles read by read_makefile. */
123 static struct dep *read_makefiles = 0;
125 static int read_makefile PARAMS ((char *filename, int flags));
126 static unsigned long readline PARAMS ((struct linebuffer *linebuffer,
127 FILE *stream, const struct floc *flocp));
128 static void do_define PARAMS ((char *name, unsigned int namelen,
129 enum variable_origin origin, FILE *infile,
130 struct floc *flocp));
131 static int conditional_line PARAMS ((char *line, const struct floc *flocp));
132 static void record_files PARAMS ((struct nameseq *filenames, char *pattern, char *pattern_percent,
133 struct dep *deps, unsigned int cmds_started, char *commands,
134 unsigned int commands_idx, int two_colon,
135 const struct floc *flocp, int set_default));
136 static void record_target_var PARAMS ((struct nameseq *filenames, char *defn,
137 int two_colon,
138 enum variable_origin origin,
139 const struct floc *flocp));
140 static enum make_word_type get_next_mword PARAMS ((char *buffer, char *delim,
141 char **startp, unsigned int *length));
143 /* Read in all the makefiles and return the chain of their names. */
145 struct dep *
146 read_all_makefiles (makefiles)
147 char **makefiles;
149 unsigned int num_makefiles = 0;
151 if (debug_flag)
152 puts ("Reading makefiles...");
154 /* If there's a non-null variable MAKEFILES, its value is a list of
155 files to read first thing. But don't let it prevent reading the
156 default makefiles and don't let the default goal come from there. */
159 char *value;
160 char *name, *p;
161 unsigned int length;
164 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
165 int save = warn_undefined_variables_flag;
166 warn_undefined_variables_flag = 0;
168 value = allocated_variable_expand ("$(MAKEFILES)");
170 warn_undefined_variables_flag = save;
173 /* Set NAME to the start of next token and LENGTH to its length.
174 MAKEFILES is updated for finding remaining tokens. */
175 p = value;
177 while ((name = find_next_token (&p, &length)) != 0)
179 if (*p != '\0')
180 *p++ = '\0';
181 (void) read_makefile (name,
182 RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
185 free (value);
188 /* Read makefiles specified with -f switches. */
190 if (makefiles != 0)
191 while (*makefiles != 0)
193 struct dep *tail = read_makefiles;
194 register struct dep *d;
196 if (! read_makefile (*makefiles, 0))
197 perror_with_name ("", *makefiles);
199 /* Find the right element of read_makefiles. */
200 d = read_makefiles;
201 while (d->next != tail)
202 d = d->next;
204 /* Use the storage read_makefile allocates. */
205 *makefiles = dep_name (d);
206 ++num_makefiles;
207 ++makefiles;
210 /* If there were no -f switches, try the default names. */
212 if (num_makefiles == 0)
214 static char *default_makefiles[] =
215 #ifdef VMS
216 /* all lower case since readdir() (the vms version) 'lowercasifies' */
217 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
218 #else
219 #ifdef _AMIGA
220 { "GNUmakefile", "Makefile", "SMakefile", 0 };
221 #else /* !Amiga && !VMS */
222 { "GNUmakefile", "makefile", "Makefile", 0 };
223 #endif /* AMIGA */
224 #endif /* VMS */
225 register char **p = default_makefiles;
226 while (*p != 0 && !file_exists_p (*p))
227 ++p;
229 if (*p != 0)
231 if (! read_makefile (*p, 0))
232 perror_with_name ("", *p);
234 else
236 /* No default makefile was found. Add the default makefiles to the
237 `read_makefiles' chain so they will be updated if possible. */
238 struct dep *tail = read_makefiles;
239 /* Add them to the tail, after any MAKEFILES variable makefiles. */
240 while (tail != 0 && tail->next != 0)
241 tail = tail->next;
242 for (p = default_makefiles; *p != 0; ++p)
244 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
245 d->name = 0;
246 d->file = enter_file (*p);
247 d->file->dontcare = 1;
248 /* Tell update_goal_chain to bail out as soon as this file is
249 made, and main not to die if we can't make this file. */
250 d->changed = RM_DONTCARE;
251 if (tail == 0)
252 read_makefiles = d;
253 else
254 tail->next = d;
255 tail = d;
257 if (tail != 0)
258 tail->next = 0;
262 return read_makefiles;
265 /* Read file FILENAME as a makefile and add its contents to the data base.
267 FLAGS contains bits as above.
269 FILENAME is added to the `read_makefiles' chain.
271 Returns 1 if a file was found and read, 0 if not. */
273 static int
274 read_makefile (filename, flags)
275 char *filename;
276 int flags;
278 static char *collapsed = 0;
279 static unsigned int collapsed_length = 0;
280 register FILE *infile;
281 struct linebuffer lb;
282 unsigned int commands_len = 200;
283 char *commands;
284 unsigned int commands_idx = 0;
285 unsigned int cmds_started;
286 char *p;
287 char *p2;
288 int len, reading_target;
289 int ignoring = 0, in_ignored_define = 0;
290 int no_targets = 0; /* Set when reading a rule without targets. */
291 struct floc fileinfo;
292 char *passed_filename = filename;
294 struct nameseq *filenames = 0;
295 struct dep *deps;
296 unsigned int nlines = 0;
297 int two_colon = 0;
298 char *pattern = 0, *pattern_percent;
300 int makefile_errno;
301 #if defined (WINDOWS32) || defined (__MSDOS__)
302 int check_again;
303 #endif
305 #define record_waiting_files() \
306 do \
308 if (filenames != 0) \
309 record_files (filenames, pattern, pattern_percent, deps, \
310 cmds_started, commands, commands_idx, \
311 two_colon, &fileinfo, \
312 !(flags & RM_NO_DEFAULT_GOAL)); \
313 filenames = 0; \
314 commands_idx = 0; \
315 if (pattern) { free(pattern); pattern = 0; } \
316 } while (0)
318 fileinfo.filenm = filename;
319 fileinfo.lineno = 1;
321 pattern_percent = 0;
322 cmds_started = fileinfo.lineno;
324 if (debug_flag)
326 printf ("Reading makefile `%s'", fileinfo.filenm);
327 if (flags & RM_NO_DEFAULT_GOAL)
328 printf (" (no default goal)");
329 if (flags & RM_INCLUDED)
330 printf (" (search path)");
331 if (flags & RM_DONTCARE)
332 printf (" (don't care)");
333 if (flags & RM_NO_TILDE)
334 printf (" (no ~ expansion)");
335 puts ("...");
338 /* First, get a stream to read. */
340 /* Expand ~ in FILENAME unless it came from `include',
341 in which case it was already done. */
342 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
344 char *expanded = tilde_expand (filename);
345 if (expanded != 0)
346 filename = expanded;
349 infile = fopen (filename, "r");
350 /* Save the error code so we print the right message later. */
351 makefile_errno = errno;
353 /* If the makefile wasn't found and it's either a makefile from
354 the `MAKEFILES' variable or an included makefile,
355 search the included makefile search path for this makefile. */
356 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
358 register unsigned int i;
359 for (i = 0; include_directories[i] != 0; ++i)
361 char *name = concat (include_directories[i], "/", filename);
362 infile = fopen (name, "r");
363 if (infile == 0)
364 free (name);
365 else
367 filename = name;
368 break;
373 /* Add FILENAME to the chain of read makefiles. */
374 deps = (struct dep *) xmalloc (sizeof (struct dep));
375 deps->next = read_makefiles;
376 read_makefiles = deps;
377 deps->name = 0;
378 deps->file = lookup_file (filename);
379 if (deps->file == 0)
381 deps->file = enter_file (xstrdup (filename));
382 if (flags & RM_DONTCARE)
383 deps->file->dontcare = 1;
385 if (filename != passed_filename)
386 free (filename);
387 filename = deps->file->name;
388 deps->changed = flags;
389 deps = 0;
391 /* If the makefile can't be found at all, give up entirely. */
393 if (infile == 0)
395 /* If we did some searching, errno has the error from the last
396 attempt, rather from FILENAME itself. Restore it in case the
397 caller wants to use it in a message. */
398 errno = makefile_errno;
399 return 0;
402 reading_file = &fileinfo;
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);
410 commands = xmalloc (200);
412 while (!feof (infile))
414 fileinfo.lineno += nlines;
415 nlines = readline (&lb, infile, &fileinfo);
417 /* Check for a shell command line first.
418 If it is not one, we can stop treating tab specially. */
419 if (lb.buffer[0] == '\t')
421 /* This line is a probably shell command. */
422 unsigned int len;
424 if (no_targets)
425 /* Ignore the commands in a rule with no targets. */
426 continue;
428 /* If there is no preceding rule line, don't treat this line
429 as a command, even though it begins with a tab character.
430 SunOS 4 make appears to behave this way. */
432 if (filenames != 0)
434 if (ignoring)
435 /* Yep, this is a shell command, and we don't care. */
436 continue;
438 /* Append this command line to the line being accumulated. */
439 p = lb.buffer;
440 if (commands_idx == 0)
441 cmds_started = fileinfo.lineno;
442 len = strlen (p);
443 if (len + 1 + commands_idx > commands_len)
445 commands_len = (len + 1 + commands_idx) * 2;
446 commands = (char *) xrealloc (commands, commands_len);
448 bcopy (p, &commands[commands_idx], len);
449 commands_idx += len;
450 commands[commands_idx++] = '\n';
452 continue;
456 /* This line is not a shell command line. Don't worry about tabs. */
458 if (collapsed_length < lb.size)
460 collapsed_length = lb.size;
461 if (collapsed != 0)
462 free (collapsed);
463 collapsed = (char *) xmalloc (collapsed_length);
465 strcpy (collapsed, lb.buffer);
466 /* Collapse continuation lines. */
467 collapse_continuations (collapsed);
468 remove_comments (collapsed);
470 /* Compare a word, both length and contents. */
471 #define word1eq(s, l) (len == l && strneq (s, p, l))
472 p = collapsed;
473 while (isspace (*p))
474 ++p;
475 if (*p == '\0')
476 /* This line is completely empty. */
477 continue;
479 /* Find the end of the first token. Note we don't need to worry about
480 * ":" here since we compare tokens by length (so "export" will never
481 * be equal to "export:").
483 for (p2 = p+1; *p2 != '\0' && !isspace(*p2); ++p2)
485 len = p2 - p;
487 /* Find the start of the second token. If it's a `:' remember it,
488 since it can't be a preprocessor token--this allows targets named
489 `ifdef', `export', etc. */
490 reading_target = 0;
491 while (isspace (*p2))
492 ++p2;
493 if (*p2 == '\0')
494 p2 = NULL;
495 else if (p2[0] == ':' && p2[1] == '\0')
497 reading_target = 1;
498 goto skip_conditionals;
501 /* We must first check for conditional and `define' directives before
502 ignoring anything, since they control what we will do with
503 following lines. */
505 if (!in_ignored_define
506 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
507 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
508 || word1eq ("else", 4) || word1eq ("endif", 5)))
510 int i = conditional_line (p, &fileinfo);
511 if (i >= 0)
512 ignoring = i;
513 else
514 fatal (&fileinfo, "invalid syntax in conditional");
515 continue;
518 if (word1eq ("endef", 5))
520 if (in_ignored_define)
521 in_ignored_define = 0;
522 else
523 fatal (&fileinfo, "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 if (*p2 == '\0')
535 fatal (&fileinfo, "empty variable name");
537 /* Let the variable name be the whole rest of the line,
538 with trailing blanks stripped (comments have already been
539 removed), so it could be a complex variable/function
540 reference that might contain blanks. */
541 p = index (p2, '\0');
542 while (isblank (p[-1]))
543 --p;
544 do_define (p2, p - p2, o_file, infile, &fileinfo);
546 continue;
549 if (word1eq ("override", 8))
551 p2 = next_token (p + 8);
552 if (*p2 == '\0')
553 error (&fileinfo, "empty `override' directive");
554 if (strneq (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
556 if (ignoring)
557 in_ignored_define = 1;
558 else
560 p2 = next_token (p2 + 6);
561 if (*p2 == '\0')
562 fatal (&fileinfo, "empty variable name");
564 /* Let the variable name be the whole rest of the line,
565 with trailing blanks stripped (comments have already been
566 removed), so it could be a complex variable/function
567 reference that might contain blanks. */
568 p = index (p2, '\0');
569 while (isblank (p[-1]))
570 --p;
571 do_define (p2, p - p2, o_override, infile, &fileinfo);
574 else if (!ignoring
575 && !try_variable_definition (&fileinfo, p2, o_override))
576 error (&fileinfo, "invalid `override' directive");
578 continue;
580 skip_conditionals:
582 if (ignoring)
583 /* Ignore the line. We continue here so conditionals
584 can appear in the middle of a rule. */
585 continue;
587 if (!reading_target && word1eq ("export", 6))
589 struct variable *v;
590 p2 = next_token (p + 6);
591 if (*p2 == '\0')
592 export_all_variables = 1;
593 v = try_variable_definition (&fileinfo, p2, o_file);
594 if (v != 0)
595 v->export = v_export;
596 else
598 unsigned int len;
599 for (p = find_next_token (&p2, &len); p != 0;
600 p = find_next_token (&p2, &len))
602 v = lookup_variable (p, len);
603 if (v == 0)
604 v = define_variable (p, len, "", o_file, 0);
605 v->export = v_export;
609 else if (!reading_target && word1eq ("unexport", 8))
611 unsigned int len;
612 struct variable *v;
613 p2 = next_token (p + 8);
614 if (*p2 == '\0')
615 export_all_variables = 0;
616 for (p = find_next_token (&p2, &len); p != 0;
617 p = find_next_token (&p2, &len))
619 v = lookup_variable (p, len);
620 if (v == 0)
621 v = define_variable (p, len, "", o_file, 0);
622 v->export = v_noexport;
625 else if (word1eq ("vpath", 5))
627 char *pattern;
628 unsigned int len;
629 p2 = variable_expand (p + 5);
630 p = find_next_token (&p2, &len);
631 if (p != 0)
633 pattern = savestring (p, len);
634 p = find_next_token (&p2, &len);
635 /* No searchpath means remove all previous
636 selective VPATH's with the same pattern. */
638 else
639 /* No pattern means remove all previous selective VPATH's. */
640 pattern = 0;
641 construct_vpath_list (pattern, p);
642 if (pattern != 0)
643 free (pattern);
645 else if (word1eq ("include", 7) || word1eq ("-include", 8)
646 || word1eq ("sinclude", 8))
648 /* We have found an `include' line specifying a nested
649 makefile to be read at this point. */
650 struct conditionals *save, new_conditionals;
651 struct nameseq *files;
652 /* "-include" (vs "include") says no error if the file does not
653 exist. "sinclude" is an alias for this from SGI. */
654 int noerror = p[0] != 'i';
656 p = allocated_variable_expand (next_token (p + (noerror ? 8 : 7)));
657 if (*p == '\0')
659 error (&fileinfo,
660 "no file name for `%sinclude'",
661 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,
695 "%s: %s", name, strerror (errno));
696 free(name);
699 /* Free any space allocated by conditional_line. */
700 if (conditionals->ignoring)
701 free (conditionals->ignoring);
702 if (conditionals->seen_else)
703 free (conditionals->seen_else);
705 /* Restore state. */
706 conditionals = save;
707 reading_file = &fileinfo;
709 #undef word1eq
710 else if (try_variable_definition (&fileinfo, p, o_file))
711 /* This line has been dealt with. */
713 else if (lb.buffer[0] == '\t')
715 p = collapsed; /* Ignore comments. */
716 while (isblank (*p))
717 ++p;
718 if (*p == '\0')
719 /* The line is completely blank; that is harmless. */
720 continue;
721 /* This line starts with a tab but was not caught above
722 because there was no preceding target, and the line
723 might have been usable as a variable definition.
724 But now it is definitely lossage. */
725 fatal(&fileinfo, "commands commence before first target");
727 else
729 /* This line describes some target files. This is complicated by
730 the existence of target-specific variables, because we can't
731 expand the entire line until we know if we have one or not. So
732 we expand the line word by word until we find the first `:',
733 then check to see if it's a target-specific variable.
735 In this algorithm, `lb_next' will point to the beginning of the
736 unexpanded parts of the input buffer, while `p2' points to the
737 parts of the expanded buffer we haven't searched yet. */
739 enum make_word_type wtype;
740 enum variable_origin v_origin;
741 char *cmdleft, *lb_next;
742 unsigned int len, plen = 0;
743 char *colonp;
745 /* Record the previous rule. */
747 record_waiting_files ();
749 /* Search the line for an unquoted ; that is not after an
750 unquoted #. */
751 cmdleft = find_char_unquote (lb.buffer, ";#", 0);
752 if (cmdleft != 0 && *cmdleft == '#')
754 /* We found a comment before a semicolon. */
755 *cmdleft = '\0';
756 cmdleft = 0;
758 else if (cmdleft != 0)
759 /* Found one. Cut the line short there before expanding it. */
760 *(cmdleft++) = '\0';
762 collapse_continuations (lb.buffer);
764 /* We can't expand the entire line, since if it's a per-target
765 variable we don't want to expand it. So, walk from the
766 beginning, expanding as we go, and looking for "interesting"
767 chars. The first word is always expandable. */
768 wtype = get_next_mword(lb.buffer, NULL, &lb_next, &len);
769 switch (wtype)
771 case w_eol:
772 if (cmdleft != 0)
773 fatal(&fileinfo, "missing rule before commands");
774 /* This line contained something but turned out to be nothing
775 but whitespace (a comment?). */
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 lb_next += len;
793 if (cmdleft == 0)
795 /* Look for a semicolon in the expanded line. */
796 cmdleft = find_char_unquote (p2, ";", 0);
798 if (cmdleft != 0)
800 unsigned long p2_off = p2 - variable_buffer;
801 unsigned long cmd_off = cmdleft - variable_buffer;
802 char *pend = p2 + strlen(p2);
804 /* Append any remnants of lb, then cut the line short
805 at the semicolon. */
806 *cmdleft = '\0';
808 /* One school of thought says that you shouldn't expand
809 here, but merely copy, since now you're beyond a ";"
810 and into a command script. However, the old parser
811 expanded the whole line, so we continue that for
812 backwards-compatiblity. Also, it wouldn't be
813 entirely consistent, since we do an unconditional
814 expand below once we know we don't have a
815 target-specific variable. */
816 (void)variable_expand_string(pend, lb_next, (long)-1);
817 lb_next += strlen(lb_next);
818 p2 = variable_buffer + p2_off;
819 cmdleft = variable_buffer + cmd_off + 1;
823 colonp = find_char_unquote(p2, ":", 0);
824 #if defined(__MSDOS__) || defined(WINDOWS32)
825 /* The drive spec brain-damage strikes again... */
826 /* Note that the only separators of targets in this context
827 are whitespace and a left paren. If others are possible,
828 they should be added to the string in the call to index. */
829 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
830 colonp > p2 && isalpha(colonp[-1]) &&
831 (colonp == p2 + 1 || index(" \t(", colonp[-2]) != 0))
832 colonp = find_char_unquote(colonp + 1, ":", 0);
833 #endif
834 if (colonp != 0)
835 break;
837 wtype = get_next_mword(lb_next, NULL, &lb_next, &len);
838 if (wtype == w_eol)
839 break;
841 p2 += strlen(p2);
842 *(p2++) = ' ';
843 p2 = variable_expand_string(p2, lb_next, len);
844 /* We don't need to worry about cmdleft here, because if it was
845 found in the variable_buffer the entire buffer has already
846 been expanded... we'll never get here. */
849 p2 = next_token (variable_buffer);
851 /* If the word we're looking at is EOL, see if there's _anything_
852 on the line. If not, a variable expanded to nothing, so ignore
853 it. If so, we can't parse this line so punt. */
854 if (wtype == w_eol)
856 if (*p2 != '\0')
857 /* There's no need to be ivory-tower about this: check for
858 one of the most common bugs found in makefiles... */
859 fatal (&fileinfo, "missing separator%s",
860 !strneq(lb.buffer, " ", 8) ? ""
861 : " (did you mean TAB instead of 8 spaces?)");
862 continue;
865 /* Make the colon the end-of-string so we know where to stop
866 looking for targets. */
867 *colonp = '\0';
868 filenames = multi_glob (parse_file_seq (&p2, '\0',
869 sizeof (struct nameseq),
871 sizeof (struct nameseq));
872 *colonp = ':';
874 if (!filenames)
876 /* We accept and ignore rules without targets for
877 compatibility with SunOS 4 make. */
878 no_targets = 1;
879 continue;
881 /* This should never be possible; we handled it above. */
882 assert(*p2 != '\0');
883 ++p2;
885 /* Is this a one-colon or two-colon entry? */
886 two_colon = *p2 == ':';
887 if (two_colon)
888 p2++;
890 /* Test to see if it's a target-specific variable. Copy the rest
891 of the buffer over, possibly temporarily (we'll expand it later
892 if it's not a target-specific variable). PLEN saves the length
893 of the unparsed section of p2, for later. */
894 if (*lb_next != '\0')
896 unsigned int l = p2 - variable_buffer;
897 plen = strlen(p2);
898 (void)variable_buffer_output(p2+plen,
899 lb_next, strlen(lb_next)+1);
900 p2 = variable_buffer + l;
902 wtype = get_next_mword(p2, NULL, &p, &len);
903 v_origin = o_file;
904 if (wtype == w_static && (len == (sizeof("override")-1)
905 && strneq(p, "override", len)))
907 v_origin = o_override;
908 (void)get_next_mword(p+len, NULL, &p, &len);
910 else if (wtype != w_eol)
911 wtype = get_next_mword(p+len, NULL, NULL, NULL);
913 if (wtype == w_varassign || v_origin == o_override)
915 record_target_var(filenames, p, two_colon, v_origin, &fileinfo);
916 filenames = 0;
917 continue;
920 /* This is a normal target, _not_ a target-specific variable.
921 Unquote any = in the dependency list. */
922 find_char_unquote (lb_next, "=", 0);
924 /* We have some targets, so don't ignore the following commands. */
925 no_targets = 0;
927 /* Expand the dependencies, etc. */
928 if (*lb_next != '\0')
930 unsigned int l = p2 - variable_buffer;
931 (void)variable_expand_string(p2 + plen, lb_next, (long)-1);
932 p2 = variable_buffer + l;
934 /* Look for a semicolon in the expanded line. */
935 if (cmdleft == 0)
937 cmdleft = find_char_unquote (p2, ";", 0);
938 if (cmdleft != 0)
939 *(cmdleft++) = '\0';
943 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
944 p = index (p2, ':');
945 while (p != 0 && p[-1] == '\\')
947 register char *q = &p[-1];
948 register int backslash = 0;
949 while (*q-- == '\\')
950 backslash = !backslash;
951 if (backslash)
952 p = index (p + 1, ':');
953 else
954 break;
956 #ifdef _AMIGA
957 /* Here, the situation is quite complicated. Let's have a look
958 at a couple of targets:
960 install: dev:make
962 dev:make: make
964 dev:make:: xyz
966 The rule is that it's only a target, if there are TWO :'s
967 OR a space around the :.
969 if (p && !(isspace(p[1]) || !p[1] || isspace(p[-1])))
970 p = 0;
971 #endif
972 #if defined (WINDOWS32) || defined (__MSDOS__)
973 do {
974 check_again = 0;
975 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
976 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
977 isalpha(p[-1]) &&
978 (p == p2 + 1 || index(" \t:(", p[-2]) != 0)) {
979 p = index(p + 1, ':');
980 check_again = 1;
982 } while (check_again);
983 #endif
984 if (p != 0)
986 struct nameseq *target;
987 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
988 ++p2;
989 if (target == 0)
990 fatal (&fileinfo, "missing target pattern");
991 else if (target->next != 0)
992 fatal (&fileinfo, "multiple target patterns");
993 pattern = target->name;
994 pattern_percent = find_percent (pattern);
995 if (pattern_percent == 0)
996 fatal (&fileinfo,
997 "target pattern contains no `%%'");
998 free((char *)target);
1000 else
1001 pattern = 0;
1003 /* Parse the dependencies. */
1004 deps = (struct dep *)
1005 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
1006 sizeof (struct dep));
1008 commands_idx = 0;
1009 if (cmdleft != 0)
1011 /* Semicolon means rest of line is a command. */
1012 unsigned int len = strlen (cmdleft);
1014 cmds_started = fileinfo.lineno;
1016 /* Add this command line to the buffer. */
1017 if (len + 2 > commands_len)
1019 commands_len = (len + 2) * 2;
1020 commands = (char *) xrealloc (commands, commands_len);
1022 bcopy (cmdleft, commands, len);
1023 commands_idx += len;
1024 commands[commands_idx++] = '\n';
1027 continue;
1030 /* We get here except in the case that we just read a rule line.
1031 Record now the last rule we read, so following spurious
1032 commands are properly diagnosed. */
1033 record_waiting_files ();
1034 no_targets = 0;
1037 if (conditionals->if_cmds)
1038 fatal (&fileinfo, "missing `endif'");
1040 /* At eof, record the last rule. */
1041 record_waiting_files ();
1043 freebuffer (&lb);
1044 free ((char *) commands);
1045 fclose (infile);
1047 reading_file = 0;
1049 return 1;
1052 /* Execute a `define' directive.
1053 The first line has already been read, and NAME is the name of
1054 the variable to be defined. The following lines remain to be read.
1055 LINENO, INFILE and FILENAME refer to the makefile being read.
1056 The value returned is LINENO, updated for lines read here. */
1058 static void
1059 do_define (name, namelen, origin, infile, flocp)
1060 char *name;
1061 unsigned int namelen;
1062 enum variable_origin origin;
1063 FILE *infile;
1064 struct floc *flocp;
1066 struct linebuffer lb;
1067 unsigned int nlines = 0;
1068 unsigned int length = 100;
1069 char *definition = (char *) xmalloc (100);
1070 register unsigned int idx = 0;
1071 register char *p;
1073 /* Expand the variable name. */
1074 char *var = (char *) alloca (namelen + 1);
1075 bcopy (name, var, namelen);
1076 var[namelen] = '\0';
1077 var = variable_expand (var);
1079 initbuffer (&lb);
1080 while (!feof (infile))
1082 unsigned int len;
1084 flocp->lineno += nlines;
1085 nlines = readline (&lb, infile, flocp);
1087 collapse_continuations (lb.buffer);
1089 p = next_token (lb.buffer);
1090 len = strlen (p);
1091 if ((len == 5 || (len > 5 && isblank (p[5])))
1092 && strneq (p, "endef", 5))
1094 p += 5;
1095 remove_comments (p);
1096 if (*next_token (p) != '\0')
1097 error (flocp, "Extraneous text after `endef' directive");
1098 /* Define the variable. */
1099 if (idx == 0)
1100 definition[0] = '\0';
1101 else
1102 definition[idx - 1] = '\0';
1103 (void) define_variable (var, strlen (var), definition, origin, 1);
1104 free (definition);
1105 freebuffer (&lb);
1106 return;
1108 else
1110 len = strlen (lb.buffer);
1111 /* Increase the buffer size if necessary. */
1112 if (idx + len + 1 > length)
1114 length = (idx + len) * 2;
1115 definition = (char *) xrealloc (definition, length + 1);
1118 bcopy (lb.buffer, &definition[idx], len);
1119 idx += len;
1120 /* Separate lines with a newline. */
1121 definition[idx++] = '\n';
1125 /* No `endef'!! */
1126 fatal (flocp, "missing `endef', unterminated `define'");
1128 /* NOTREACHED */
1129 return;
1132 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1133 "ifneq", "else" and "endif".
1134 LINE is the input line, with the command as its first word.
1136 FILENAME and LINENO are the filename and line number in the
1137 current makefile. They are used for error messages.
1139 Value is -1 if the line is invalid,
1140 0 if following text should be interpreted,
1141 1 if following text should be ignored. */
1143 static int
1144 conditional_line (line, flocp)
1145 char *line;
1146 const struct floc *flocp;
1148 int notdef;
1149 char *cmdname;
1150 register unsigned int i;
1152 if (*line == 'i')
1154 /* It's an "if..." command. */
1155 notdef = line[2] == 'n';
1156 if (notdef)
1158 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
1159 line += cmdname[3] == 'd' ? 7 : 6;
1161 else
1163 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
1164 line += cmdname[2] == 'd' ? 6 : 5;
1167 else
1169 /* It's an "else" or "endif" command. */
1170 notdef = line[1] == 'n';
1171 cmdname = notdef ? "endif" : "else";
1172 line += notdef ? 5 : 4;
1175 line = next_token (line);
1177 if (*cmdname == 'e')
1179 if (*line != '\0')
1180 error (flocp,
1181 "Extraneous text after `%s' directive", cmdname);
1182 /* "Else" or "endif". */
1183 if (conditionals->if_cmds == 0)
1184 fatal (flocp, "extraneous `%s'", cmdname);
1185 /* NOTDEF indicates an `endif' command. */
1186 if (notdef)
1187 --conditionals->if_cmds;
1188 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1189 fatal (flocp, "only one `else' per conditional");
1190 else
1192 /* Toggle the state of ignorance. */
1193 conditionals->ignoring[conditionals->if_cmds - 1]
1194 = !conditionals->ignoring[conditionals->if_cmds - 1];
1195 /* Record that we have seen an `else' in this conditional.
1196 A second `else' will be erroneous. */
1197 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1199 for (i = 0; i < conditionals->if_cmds; ++i)
1200 if (conditionals->ignoring[i])
1201 return 1;
1202 return 0;
1205 if (conditionals->allocated == 0)
1207 conditionals->allocated = 5;
1208 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1209 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1212 ++conditionals->if_cmds;
1213 if (conditionals->if_cmds > conditionals->allocated)
1215 conditionals->allocated += 5;
1216 conditionals->ignoring = (char *)
1217 xrealloc (conditionals->ignoring, conditionals->allocated);
1218 conditionals->seen_else = (char *)
1219 xrealloc (conditionals->seen_else, conditionals->allocated);
1222 /* Record that we have seen an `if...' but no `else' so far. */
1223 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1225 /* Search through the stack to see if we're already ignoring. */
1226 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1227 if (conditionals->ignoring[i])
1229 /* We are already ignoring, so just push a level
1230 to match the next "else" or "endif", and keep ignoring.
1231 We don't want to expand variables in the condition. */
1232 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1233 return 1;
1236 if (cmdname[notdef ? 3 : 2] == 'd')
1238 /* "Ifdef" or "ifndef". */
1239 struct variable *v;
1240 register char *p = end_of_token (line);
1241 i = p - line;
1242 p = next_token (p);
1243 if (*p != '\0')
1244 return -1;
1245 v = lookup_variable (line, i);
1246 conditionals->ignoring[conditionals->if_cmds - 1]
1247 = (v != 0 && *v->value != '\0') == notdef;
1249 else
1251 /* "Ifeq" or "ifneq". */
1252 char *s1, *s2;
1253 unsigned int len;
1254 char termin = *line == '(' ? ',' : *line;
1256 if (termin != ',' && termin != '"' && termin != '\'')
1257 return -1;
1259 s1 = ++line;
1260 /* Find the end of the first string. */
1261 if (termin == ',')
1263 register int count = 0;
1264 for (; *line != '\0'; ++line)
1265 if (*line == '(')
1266 ++count;
1267 else if (*line == ')')
1268 --count;
1269 else if (*line == ',' && count <= 0)
1270 break;
1272 else
1273 while (*line != '\0' && *line != termin)
1274 ++line;
1276 if (*line == '\0')
1277 return -1;
1279 if (termin == ',')
1281 /* Strip blanks after the first string. */
1282 char *p = line++;
1283 while (isblank (p[-1]))
1284 --p;
1285 *p = '\0';
1287 else
1288 *line++ = '\0';
1290 s2 = variable_expand (s1);
1291 /* We must allocate a new copy of the expanded string because
1292 variable_expand re-uses the same buffer. */
1293 len = strlen (s2);
1294 s1 = (char *) alloca (len + 1);
1295 bcopy (s2, s1, len + 1);
1297 if (termin != ',')
1298 /* Find the start of the second string. */
1299 line = next_token (line);
1301 termin = termin == ',' ? ')' : *line;
1302 if (termin != ')' && termin != '"' && termin != '\'')
1303 return -1;
1305 /* Find the end of the second string. */
1306 if (termin == ')')
1308 register int count = 0;
1309 s2 = next_token (line);
1310 for (line = s2; *line != '\0'; ++line)
1312 if (*line == '(')
1313 ++count;
1314 else if (*line == ')')
1316 if (count <= 0)
1317 break;
1318 else
1319 --count;
1323 else
1325 ++line;
1326 s2 = line;
1327 while (*line != '\0' && *line != termin)
1328 ++line;
1331 if (*line == '\0')
1332 return -1;
1334 *line = '\0';
1335 line = next_token (++line);
1336 if (*line != '\0')
1337 error (flocp,
1338 "Extraneous text after `%s' directive", cmdname);
1340 s2 = variable_expand (s2);
1341 conditionals->ignoring[conditionals->if_cmds - 1]
1342 = streq (s1, s2) == notdef;
1345 /* Search through the stack to see if we're ignoring. */
1346 for (i = 0; i < conditionals->if_cmds; ++i)
1347 if (conditionals->ignoring[i])
1348 return 1;
1349 return 0;
1352 /* Remove duplicate dependencies in CHAIN. */
1354 void
1355 uniquize_deps (chain)
1356 struct dep *chain;
1358 register struct dep *d;
1360 /* Make sure that no dependencies are repeated. This does not
1361 really matter for the purpose of updating targets, but it
1362 might make some names be listed twice for $^ and $?. */
1364 for (d = chain; d != 0; d = d->next)
1366 struct dep *last, *next;
1368 last = d;
1369 next = d->next;
1370 while (next != 0)
1371 if (streq (dep_name (d), dep_name (next)))
1373 struct dep *n = next->next;
1374 last->next = n;
1375 if (next->name != 0 && next->name != d->name)
1376 free (next->name);
1377 if (next != d)
1378 free ((char *) next);
1379 next = n;
1381 else
1383 last = next;
1384 next = next->next;
1389 /* Record target-specific variable values for files FILENAMES.
1390 TWO_COLON is nonzero if a double colon was used.
1392 The links of FILENAMES are freed, and so are any names in it
1393 that are not incorporated into other data structures.
1395 If the target is a pattern, add the variable to the pattern-specific
1396 variable value list. */
1398 static void
1399 record_target_var (filenames, defn, two_colon, origin, flocp)
1400 struct nameseq *filenames;
1401 char *defn;
1402 int two_colon;
1403 enum variable_origin origin;
1404 const struct floc *flocp;
1406 struct nameseq *nextf;
1407 struct variable_set_list *global;
1409 global = current_variable_set_list;
1411 for (; filenames != 0; filenames = nextf)
1413 struct variable *v;
1414 register char *name = filenames->name;
1415 struct variable_set_list *vlist;
1416 char *fname;
1417 char *percent;
1419 nextf = filenames->next;
1420 free ((char *) filenames);
1422 /* If it's a pattern target, then add it to the pattern-specific
1423 variable list. */
1424 percent = find_percent (name);
1425 if (percent)
1427 struct pattern_var *p;
1429 /* Get a reference for this pattern-specific variable struct. */
1430 p = create_pattern_var(name, percent);
1431 vlist = p->vars;
1432 fname = p->target;
1434 else
1436 struct file *f;
1438 /* Get a file reference for this file, and initialize it. */
1439 f = enter_file (name);
1440 initialize_file_variables (f);
1441 vlist = f->variables;
1442 fname = f->name;
1445 /* Make the new variable context current and define the variable. */
1446 current_variable_set_list = vlist;
1447 v = try_variable_definition(flocp, defn, origin);
1448 if (!v)
1449 error (flocp, "Malformed per-target variable definition");
1450 v->per_target = 1;
1452 /* If it's not an override, check to see if there was a command-line
1453 setting. If so, reset the value. */
1454 if (origin != o_override)
1456 struct variable *gv;
1457 int len = strlen(v->name);
1459 current_variable_set_list = global;
1460 gv = lookup_variable(v->name, len);
1461 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1462 define_variable_in_set(v->name, len, gv->value, gv->origin,
1463 gv->recursive, vlist->set);
1466 /* Free name if not needed further. */
1467 if (name != fname && (name < fname || name > fname + strlen (fname)))
1468 free (name);
1471 current_variable_set_list = global;
1474 /* Record a description line for files FILENAMES,
1475 with dependencies DEPS, commands to execute described
1476 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1477 TWO_COLON is nonzero if a double colon was used.
1478 If not nil, PATTERN is the `%' pattern to make this
1479 a static pattern rule, and PATTERN_PERCENT is a pointer
1480 to the `%' within it.
1482 The links of FILENAMES are freed, and so are any names in it
1483 that are not incorporated into other data structures. */
1485 static void
1486 record_files (filenames, pattern, pattern_percent, deps, cmds_started,
1487 commands, commands_idx, two_colon, flocp, set_default)
1488 struct nameseq *filenames;
1489 char *pattern, *pattern_percent;
1490 struct dep *deps;
1491 unsigned int cmds_started;
1492 char *commands;
1493 unsigned int commands_idx;
1494 int two_colon;
1495 const struct floc *flocp;
1496 int set_default;
1498 struct nameseq *nextf;
1499 int implicit = 0;
1500 unsigned int max_targets = 0, target_idx = 0;
1501 char **targets = 0, **target_percents = 0;
1502 struct commands *cmds;
1504 if (commands_idx > 0)
1506 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1507 cmds->fileinfo.filenm = flocp->filenm;
1508 cmds->fileinfo.lineno = cmds_started;
1509 cmds->commands = savestring (commands, commands_idx);
1510 cmds->command_lines = 0;
1512 else
1513 cmds = 0;
1515 for (; filenames != 0; filenames = nextf)
1518 register char *name = filenames->name;
1519 register struct file *f;
1520 register struct dep *d;
1521 struct dep *this;
1522 char *implicit_percent;
1524 nextf = filenames->next;
1525 free ((char *) filenames);
1527 implicit_percent = find_percent (name);
1528 implicit |= implicit_percent != 0;
1530 if (implicit && pattern != 0)
1531 fatal (flocp, "mixed implicit and static pattern rules");
1533 if (implicit && implicit_percent == 0)
1534 fatal (flocp, "mixed implicit and normal rules");
1536 if (implicit)
1538 if (targets == 0)
1540 max_targets = 5;
1541 targets = (char **) xmalloc (5 * sizeof (char *));
1542 target_percents = (char **) xmalloc (5 * sizeof (char *));
1543 target_idx = 0;
1545 else if (target_idx == max_targets - 1)
1547 max_targets += 5;
1548 targets = (char **) xrealloc ((char *) targets,
1549 max_targets * sizeof (char *));
1550 target_percents
1551 = (char **) xrealloc ((char *) target_percents,
1552 max_targets * sizeof (char *));
1554 targets[target_idx] = name;
1555 target_percents[target_idx] = implicit_percent;
1556 ++target_idx;
1557 continue;
1560 /* If there are multiple filenames, copy the chain DEPS
1561 for all but the last one. It is not safe for the same deps
1562 to go in more than one place in the data base. */
1563 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1565 if (pattern != 0)
1567 /* If this is an extended static rule:
1568 `targets: target%pattern: dep%pattern; cmds',
1569 translate each dependency pattern into a plain filename
1570 using the target pattern and this target's name. */
1571 if (!pattern_matches (pattern, pattern_percent, name))
1573 /* Give a warning if the rule is meaningless. */
1574 error (flocp,
1575 "target `%s' doesn't match the target pattern", name);
1576 this = 0;
1578 else
1580 /* We use patsubst_expand to do the work of translating
1581 the target pattern, the target's name and the dependencies'
1582 patterns into plain dependency names. */
1583 char *buffer = variable_expand ("");
1585 for (d = this; d != 0; d = d->next)
1587 char *o;
1588 char *percent = find_percent (d->name);
1589 if (percent == 0)
1590 continue;
1591 o = patsubst_expand (buffer, name, pattern, d->name,
1592 pattern_percent, percent);
1593 free (d->name);
1594 d->name = savestring (buffer, o - buffer);
1599 if (!two_colon)
1601 /* Single-colon. Combine these dependencies
1602 with others in file's existing record, if any. */
1603 f = enter_file (name);
1605 if (f->double_colon)
1606 fatal (flocp,
1607 "target file `%s' has both : and :: entries", f->name);
1609 /* If CMDS == F->CMDS, this target was listed in this rule
1610 more than once. Just give a warning since this is harmless. */
1611 if (cmds != 0 && cmds == f->cmds)
1612 error (flocp, "target `%s' given more than once in the same rule.",
1613 f->name);
1615 /* Check for two single-colon entries both with commands.
1616 Check is_target so that we don't lose on files such as .c.o
1617 whose commands were preinitialized. */
1618 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1620 error (&cmds->fileinfo,
1621 "warning: overriding commands for target `%s'", f->name);
1622 error (&f->cmds->fileinfo,
1623 "warning: ignoring old commands for target `%s'",
1624 f->name);
1627 f->is_target = 1;
1629 /* Defining .DEFAULT with no deps or cmds clears it. */
1630 if (f == default_file && this == 0 && cmds == 0)
1631 f->cmds = 0;
1632 if (cmds != 0)
1633 f->cmds = cmds;
1634 /* Defining .SUFFIXES with no dependencies
1635 clears out the list of suffixes. */
1636 if (f == suffix_file && this == 0)
1638 d = f->deps;
1639 while (d != 0)
1641 struct dep *nextd = d->next;
1642 free (d->name);
1643 free ((char *)d);
1644 d = nextd;
1646 f->deps = 0;
1648 else if (f->deps != 0)
1650 /* Add the file's old deps and the new ones in THIS together. */
1652 struct dep *firstdeps, *moredeps;
1653 if (cmds != 0)
1655 /* This is the rule with commands, so put its deps first.
1656 The rationale behind this is that $< expands to the
1657 first dep in the chain, and commands use $< expecting
1658 to get the dep that rule specifies. */
1659 firstdeps = this;
1660 moredeps = f->deps;
1662 else
1664 /* Append the new deps to the old ones. */
1665 firstdeps = f->deps;
1666 moredeps = this;
1669 if (firstdeps == 0)
1670 firstdeps = moredeps;
1671 else
1673 d = firstdeps;
1674 while (d->next != 0)
1675 d = d->next;
1676 d->next = moredeps;
1679 f->deps = firstdeps;
1681 else
1682 f->deps = this;
1684 /* If this is a static pattern rule, set the file's stem to
1685 the part of its name that matched the `%' in the pattern,
1686 so you can use $* in the commands. */
1687 if (pattern != 0)
1689 static char *percent = "%";
1690 char *buffer = variable_expand ("");
1691 char *o = patsubst_expand (buffer, name, pattern, percent,
1692 pattern_percent, percent);
1693 f->stem = savestring (buffer, o - buffer);
1696 else
1698 /* Double-colon. Make a new record
1699 even if the file already has one. */
1700 f = lookup_file (name);
1701 /* Check for both : and :: rules. Check is_target so
1702 we don't lose on default suffix rules or makefiles. */
1703 if (f != 0 && f->is_target && !f->double_colon)
1704 fatal (flocp,
1705 "target file `%s' has both : and :: entries", f->name);
1706 f = enter_file (name);
1707 /* If there was an existing entry and it was a double-colon
1708 entry, enter_file will have returned a new one, making it the
1709 prev pointer of the old one, and setting its double_colon
1710 pointer to the first one. */
1711 if (f->double_colon == 0)
1712 /* This is the first entry for this name, so we must
1713 set its double_colon pointer to itself. */
1714 f->double_colon = f;
1715 f->is_target = 1;
1716 f->deps = this;
1717 f->cmds = cmds;
1720 /* Free name if not needed further. */
1721 if (f != 0 && name != f->name
1722 && (name < f->name || name > f->name + strlen (f->name)))
1724 free (name);
1725 name = f->name;
1728 /* See if this is first target seen whose name does
1729 not start with a `.', unless it contains a slash. */
1730 if (default_goal_file == 0 && set_default
1731 && (*name != '.' || index (name, '/') != 0
1732 #ifdef __MSDOS__
1733 || index (name, '\\') != 0
1734 #endif
1737 int reject = 0;
1739 /* If this file is a suffix, don't
1740 let it be the default goal file. */
1742 for (d = suffix_file->deps; d != 0; d = d->next)
1744 register struct dep *d2;
1745 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1747 reject = 1;
1748 break;
1750 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1752 register unsigned int len = strlen (dep_name (d2));
1753 if (!strneq (name, dep_name (d2), len))
1754 continue;
1755 if (streq (name + len, dep_name (d)))
1757 reject = 1;
1758 break;
1761 if (reject)
1762 break;
1765 if (!reject)
1766 default_goal_file = f;
1770 if (implicit)
1772 targets[target_idx] = 0;
1773 target_percents[target_idx] = 0;
1774 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1775 free ((char *) target_percents);
1779 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1780 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1781 Quoting backslashes are removed from STRING by compacting it into
1782 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1783 one, or nil if there are none. */
1785 char *
1786 find_char_unquote (string, stopchars, blank)
1787 char *string;
1788 char *stopchars;
1789 int blank;
1791 unsigned int string_len = 0;
1792 register char *p = string;
1794 while (1)
1796 while (*p != '\0' && index (stopchars, *p) == 0
1797 && (!blank || !isblank (*p)))
1798 ++p;
1799 if (*p == '\0')
1800 break;
1802 if (p > string && p[-1] == '\\')
1804 /* Search for more backslashes. */
1805 register int i = -2;
1806 while (&p[i] >= string && p[i] == '\\')
1807 --i;
1808 ++i;
1809 /* Only compute the length if really needed. */
1810 if (string_len == 0)
1811 string_len = strlen (string);
1812 /* The number of backslashes is now -I.
1813 Copy P over itself to swallow half of them. */
1814 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1815 p += i / 2;
1816 if (i % 2 == 0)
1817 /* All the backslashes quoted each other; the STOPCHAR was
1818 unquoted. */
1819 return p;
1821 /* The STOPCHAR was quoted by a backslash. Look for another. */
1823 else
1824 /* No backslash in sight. */
1825 return p;
1828 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1829 return 0;
1832 /* Search PATTERN for an unquoted %. */
1834 char *
1835 find_percent (pattern)
1836 char *pattern;
1838 return find_char_unquote (pattern, "%", 0);
1841 /* Parse a string into a sequence of filenames represented as a
1842 chain of struct nameseq's in reverse order and return that chain.
1844 The string is passed as STRINGP, the address of a string pointer.
1845 The string pointer is updated to point at the first character
1846 not parsed, which either is a null char or equals STOPCHAR.
1848 SIZE is how big to construct chain elements.
1849 This is useful if we want them actually to be other structures
1850 that have room for additional info.
1852 If STRIP is nonzero, strip `./'s off the beginning. */
1854 struct nameseq *
1855 parse_file_seq (stringp, stopchar, size, strip)
1856 char **stringp;
1857 int stopchar;
1858 unsigned int size;
1859 int strip;
1861 register struct nameseq *new = 0;
1862 register struct nameseq *new1, *lastnew1;
1863 register char *p = *stringp;
1864 char *q;
1865 char *name;
1866 char stopchars[3];
1868 #ifdef VMS
1869 stopchars[0] = ',';
1870 stopchars[1] = stopchar;
1871 stopchars[2] = '\0';
1872 #else
1873 stopchars[0] = stopchar;
1874 stopchars[1] = '\0';
1875 #endif
1877 while (1)
1879 /* Skip whitespace; see if any more names are left. */
1880 p = next_token (p);
1881 if (*p == '\0')
1882 break;
1883 if (*p == stopchar)
1884 break;
1886 /* Yes, find end of next name. */
1887 q = p;
1888 p = find_char_unquote (q, stopchars, 1);
1889 #ifdef VMS
1890 /* convert comma separated list to space separated */
1891 if (p && *p == ',')
1892 *p =' ';
1893 #endif
1894 #ifdef _AMIGA
1895 if (stopchar == ':' && p && *p == ':' &&
1896 !(isspace(p[1]) || !p[1] || isspace(p[-1])))
1898 p = find_char_unquote (p+1, stopchars, 1);
1900 #endif
1901 #if defined(WINDOWS32) || defined(__MSDOS__)
1902 /* For WINDOWS32, skip a "C:\..." or a "C:/..." until we find the
1903 first colon which isn't followed by a slash or a backslash.
1904 Note that tokens separated by spaces should be treated as separate
1905 tokens since make doesn't allow path names with spaces */
1906 if (stopchar == ':')
1907 while (p != 0 && !isspace(*p) &&
1908 (p[1] == '\\' || p[1] == '/') && isalpha (p[-1]))
1909 p = find_char_unquote (p + 1, stopchars, 1);
1910 #endif
1911 if (p == 0)
1912 p = q + strlen (q);
1914 if (strip)
1915 #ifdef VMS
1916 /* Skip leading `[]'s. */
1917 while (p - q > 2 && q[0] == '[' && q[1] == ']')
1918 #else
1919 /* Skip leading `./'s. */
1920 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1921 #endif
1923 q += 2; /* Skip "./". */
1924 while (q < p && *q == '/')
1925 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1926 ++q;
1929 /* Extract the filename just found, and skip it. */
1931 if (q == p)
1932 /* ".///" was stripped to "". */
1933 #ifdef VMS
1934 continue;
1935 #else
1936 #ifdef _AMIGA
1937 name = savestring ("", 0);
1938 #else
1939 name = savestring ("./", 2);
1940 #endif
1941 #endif
1942 else
1943 #ifdef VMS
1944 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
1945 * to remove this '\' before we can use the filename.
1946 * Savestring called because q may be read-only string constant.
1949 char *qbase = xstrdup (q);
1950 char *pbase = qbase + (p-q);
1951 char *q1 = qbase;
1952 char *q2 = q1;
1953 char *p1 = pbase;
1955 while (q1 != pbase)
1957 if (*q1 == '\\' && *(q1+1) == ':')
1959 q1++;
1960 p1--;
1962 *q2++ = *q1++;
1964 name = savestring (qbase, p1 - qbase);
1965 free (qbase);
1967 #else
1968 name = savestring (q, p - q);
1969 #endif
1971 /* Add it to the front of the chain. */
1972 new1 = (struct nameseq *) xmalloc (size);
1973 new1->name = name;
1974 new1->next = new;
1975 new = new1;
1978 #ifndef NO_ARCHIVES
1980 /* Look for multi-word archive references.
1981 They are indicated by a elt ending with an unmatched `)' and
1982 an elt further down the chain (i.e., previous in the file list)
1983 with an unmatched `(' (e.g., "lib(mem"). */
1985 new1 = new;
1986 lastnew1 = 0;
1987 while (new1 != 0)
1988 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1989 && new1->name[strlen (new1->name) - 1] == ')'
1990 && index (new1->name, '(') == 0)
1992 /* NEW1 ends with a `)' but does not contain a `('.
1993 Look back for an elt with an opening `(' but no closing `)'. */
1995 struct nameseq *n = new1->next, *lastn = new1;
1996 char *paren = 0;
1997 while (n != 0 && (paren = index (n->name, '(')) == 0)
1999 lastn = n;
2000 n = n->next;
2002 if (n != 0
2003 /* Ignore something starting with `(', as that cannot actually
2004 be an archive-member reference (and treating it as such
2005 results in an empty file name, which causes much lossage). */
2006 && n->name[0] != '(')
2008 /* N is the first element in the archive group.
2009 Its name looks like "lib(mem" (with no closing `)'). */
2011 char *libname;
2013 /* Copy "lib(" into LIBNAME. */
2014 ++paren;
2015 libname = (char *) alloca (paren - n->name + 1);
2016 bcopy (n->name, libname, paren - n->name);
2017 libname[paren - n->name] = '\0';
2019 if (*paren == '\0')
2021 /* N was just "lib(", part of something like "lib( a b)".
2022 Edit it out of the chain and free its storage. */
2023 lastn->next = n->next;
2024 free (n->name);
2025 free ((char *) n);
2026 /* LASTN->next is the new stopping elt for the loop below. */
2027 n = lastn->next;
2029 else
2031 /* Replace N's name with the full archive reference. */
2032 name = concat (libname, paren, ")");
2033 free (n->name);
2034 n->name = name;
2037 if (new1->name[1] == '\0')
2039 /* NEW1 is just ")", part of something like "lib(a b )".
2040 Omit it from the chain and free its storage. */
2041 if (lastnew1 == 0)
2042 new = new1->next;
2043 else
2044 lastnew1->next = new1->next;
2045 lastn = new1;
2046 new1 = new1->next;
2047 free (lastn->name);
2048 free ((char *) lastn);
2050 else
2052 /* Replace also NEW1->name, which already has closing `)'. */
2053 name = concat (libname, new1->name, "");
2054 free (new1->name);
2055 new1->name = name;
2056 new1 = new1->next;
2059 /* Trace back from NEW1 (the end of the list) until N
2060 (the beginning of the list), rewriting each name
2061 with the full archive reference. */
2063 while (new1 != n)
2065 name = concat (libname, new1->name, ")");
2066 free (new1->name);
2067 new1->name = name;
2068 lastnew1 = new1;
2069 new1 = new1->next;
2072 else
2074 /* No frobnication happening. Just step down the list. */
2075 lastnew1 = new1;
2076 new1 = new1->next;
2079 else
2081 lastnew1 = new1;
2082 new1 = new1->next;
2085 #endif
2087 *stringp = p;
2088 return new;
2091 /* Read a line of text from STREAM into LINEBUFFER.
2092 Combine continuation lines into one line.
2093 Return the number of actual lines read (> 1 if hacked continuation lines).
2096 static unsigned long
2097 readline (linebuffer, stream, flocp)
2098 struct linebuffer *linebuffer;
2099 FILE *stream;
2100 const struct floc *flocp;
2102 char *buffer = linebuffer->buffer;
2103 register char *p = linebuffer->buffer;
2104 register char *end = p + linebuffer->size;
2105 register int len, lastlen = 0;
2106 register char *p2;
2107 register unsigned int nlines = 0;
2108 register int backslash;
2110 *p = '\0';
2112 while (fgets (p, end - p, stream) != 0)
2114 len = strlen (p);
2115 if (len == 0)
2117 /* This only happens when the first thing on the line is a '\0'.
2118 It is a pretty hopeless case, but (wonder of wonders) Athena
2119 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2120 There is nothing really to be done; we synthesize a newline so
2121 the following line doesn't appear to be part of this line. */
2122 error (flocp, "warning: NUL character seen; rest of line ignored");
2123 p[0] = '\n';
2124 len = 1;
2127 p += len;
2128 if (p[-1] != '\n')
2130 /* Probably ran out of buffer space. */
2131 register unsigned int p_off = p - buffer;
2132 linebuffer->size *= 2;
2133 buffer = (char *) xrealloc (buffer, linebuffer->size);
2134 p = buffer + p_off;
2135 end = buffer + linebuffer->size;
2136 linebuffer->buffer = buffer;
2137 *p = '\0';
2138 lastlen = len;
2139 continue;
2142 ++nlines;
2144 #if !defined(WINDOWS32) && !defined(__MSDOS__)
2145 /* Check to see if the line was really ended with CRLF; if so ignore
2146 the CR. */
2147 if (len > 1 && p[-2] == '\r')
2149 --len;
2150 --p;
2151 p[-1] = '\n';
2153 #endif
2155 if (len == 1 && p > buffer)
2156 /* P is pointing at a newline and it's the beginning of
2157 the buffer returned by the last fgets call. However,
2158 it is not necessarily the beginning of a line if P is
2159 pointing past the beginning of the holding buffer.
2160 If the buffer was just enlarged (right before the newline),
2161 we must account for that, so we pretend that the two lines
2162 were one line. */
2163 len += lastlen;
2164 lastlen = len;
2165 backslash = 0;
2166 for (p2 = p - 2; --len > 0; --p2)
2168 if (*p2 == '\\')
2169 backslash = !backslash;
2170 else
2171 break;
2174 if (!backslash)
2176 p[-1] = '\0';
2177 break;
2180 if (end - p <= 1)
2182 /* Enlarge the buffer. */
2183 register unsigned int p_off = p - buffer;
2184 linebuffer->size *= 2;
2185 buffer = (char *) xrealloc (buffer, linebuffer->size);
2186 p = buffer + p_off;
2187 end = buffer + linebuffer->size;
2188 linebuffer->buffer = buffer;
2192 if (ferror (stream))
2193 pfatal_with_name (flocp->filenm);
2195 return nlines;
2198 /* Parse the next "makefile word" from the input buffer, and return info
2199 about it.
2201 A "makefile word" is one of:
2203 w_bogus Should never happen
2204 w_eol End of input
2205 w_static A static word; cannot be expanded
2206 w_variable A word containing one or more variables/functions
2207 w_colon A colon
2208 w_dcolon A double-colon
2209 w_semicolon A semicolon
2210 w_comment A comment character
2211 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2213 Note that this function is only used when reading certain parts of the
2214 makefile. Don't use it where special rules hold sway (RHS of a variable,
2215 in a command list, etc.) */
2217 static enum make_word_type
2218 get_next_mword (buffer, delim, startp, length)
2219 char *buffer;
2220 char *delim;
2221 char **startp;
2222 unsigned int *length;
2224 enum make_word_type wtype = w_bogus;
2225 char *p = buffer, *beg;
2226 char c;
2228 /* Skip any leading whitespace. */
2229 while (isblank(*p))
2230 ++p;
2232 beg = p;
2233 c = *(p++);
2234 switch (c)
2236 case '\0':
2237 wtype = w_eol;
2238 break;
2240 case '#':
2241 wtype = w_comment;
2242 break;
2244 case ';':
2245 wtype = w_semicolon;
2246 break;
2248 case '=':
2249 wtype = w_varassign;
2250 break;
2252 case ':':
2253 wtype = w_colon;
2254 switch (*p)
2256 case ':':
2257 ++p;
2258 wtype = w_dcolon;
2259 break;
2261 case '=':
2262 ++p;
2263 wtype = w_varassign;
2264 break;
2266 break;
2268 case '+':
2269 case '?':
2270 if (*p == '=')
2272 ++p;
2273 wtype = w_varassign;
2274 break;
2277 default:
2278 if (delim && index(delim, c))
2279 wtype = w_static;
2280 break;
2283 /* Did we find something? If so, return now. */
2284 if (wtype != w_bogus)
2285 goto done;
2287 /* This is some non-operator word. A word consists of the longest
2288 string of characters that doesn't contain whitespace, one of [:=#],
2289 or [?+]=, or one of the chars in the DELIM string. */
2291 /* We start out assuming a static word; if we see a variable we'll
2292 adjust our assumptions then. */
2293 wtype = w_static;
2295 /* We already found the first value of "c", above. */
2296 while (1)
2298 char closeparen;
2299 int count;
2301 switch (c)
2303 case '\0':
2304 case ' ':
2305 case '\t':
2306 case '=':
2307 case '#':
2308 goto done_word;
2310 case ':':
2311 #if defined(__MSDOS__) || defined(WINDOWS32)
2312 /* A word CAN include a colon in its drive spec. The drive
2313 spec is allowed either at the beginning of a word, or as part
2314 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2315 if (!(p - beg >= 2 &&
2316 (*p == '/' || *p == '\\') && isalpha (p[-2]) &&
2317 (p - beg == 2 || p[-3] == '(')))
2318 #endif
2319 goto done_word;
2321 case '$':
2322 c = *(p++);
2323 if (c == '$')
2324 break;
2326 /* This is a variable reference, so note that it's expandable.
2327 Then read it to the matching close paren. */
2328 wtype = w_variable;
2330 if (c == '(')
2331 closeparen = ')';
2332 else if (c == '{')
2333 closeparen = '}';
2334 else
2335 /* This is a single-letter variable reference. */
2336 break;
2338 for (count=0; *p != '\0'; ++p)
2340 if (*p == c)
2341 ++count;
2342 else if (*p == closeparen && --count < 0)
2344 ++p;
2345 break;
2348 break;
2350 case '?':
2351 case '+':
2352 if (*p == '=')
2353 goto done_word;
2354 break;
2356 case '\\':
2357 switch (*p)
2359 case ':':
2360 case ';':
2361 case '=':
2362 case '\\':
2363 ++p;
2364 break;
2366 break;
2368 default:
2369 if (delim && index(delim, c))
2370 goto done_word;
2371 break;
2374 c = *(p++);
2376 done_word:
2377 --p;
2379 done:
2380 if (startp)
2381 *startp = beg;
2382 if (length)
2383 *length = p - beg;
2384 return wtype;
2387 /* Construct the list of include directories
2388 from the arguments and the default list. */
2390 void
2391 construct_include_path (arg_dirs)
2392 char **arg_dirs;
2394 register unsigned int i;
2395 #ifdef VAXC /* just don't ask ... */
2396 stat_t stbuf;
2397 #else
2398 struct stat stbuf;
2399 #endif
2400 /* Table to hold the dirs. */
2402 register unsigned int defsize = (sizeof (default_include_directories)
2403 / sizeof (default_include_directories[0]));
2404 register unsigned int max = 5;
2405 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
2406 register unsigned int idx = 0;
2408 #ifdef __MSDOS__
2409 defsize++;
2410 #endif
2412 /* First consider any dirs specified with -I switches.
2413 Ignore dirs that don't exist. */
2415 if (arg_dirs != 0)
2416 while (*arg_dirs != 0)
2418 char *dir = *arg_dirs++;
2420 if (dir[0] == '~')
2422 char *expanded = tilde_expand (dir);
2423 if (expanded != 0)
2424 dir = expanded;
2427 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
2429 if (idx == max - 1)
2431 max += 5;
2432 dirs = (char **)
2433 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
2435 dirs[idx++] = dir;
2437 else if (dir != arg_dirs[-1])
2438 free (dir);
2441 /* Now add at the end the standard default dirs. */
2443 #ifdef __MSDOS__
2445 /* The environment variable $DJDIR holds the root of the
2446 DJGPP directory tree; add ${DJDIR}/include. */
2447 struct variable *djdir = lookup_variable ("DJDIR", 5);
2449 if (djdir)
2451 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2453 strcat (strcpy (defdir, djdir->value), "/include");
2454 dirs[idx++] = defdir;
2457 #endif
2459 for (i = 0; default_include_directories[i] != 0; ++i)
2460 if (stat (default_include_directories[i], &stbuf) == 0
2461 && S_ISDIR (stbuf.st_mode))
2462 dirs[idx++] = default_include_directories[i];
2464 dirs[idx] = 0;
2466 /* Now compute the maximum length of any name in it. */
2468 max_incl_len = 0;
2469 for (i = 0; i < idx; ++i)
2471 unsigned int len = strlen (dirs[i]);
2472 /* If dir name is written with a trailing slash, discard it. */
2473 if (dirs[i][len - 1] == '/')
2474 /* We can't just clobber a null in because it may have come from
2475 a literal string and literal strings may not be writable. */
2476 dirs[i] = savestring (dirs[i], len - 1);
2477 if (len > max_incl_len)
2478 max_incl_len = len;
2481 include_directories = dirs;
2484 /* Expand ~ or ~USER at the beginning of NAME.
2485 Return a newly malloc'd string or 0. */
2487 char *
2488 tilde_expand (name)
2489 char *name;
2491 #ifndef VMS
2492 if (name[1] == '/' || name[1] == '\0')
2494 extern char *getenv ();
2495 char *home_dir;
2496 int is_variable;
2499 /* Turn off --warn-undefined-variables while we expand HOME. */
2500 int save = warn_undefined_variables_flag;
2501 warn_undefined_variables_flag = 0;
2503 home_dir = allocated_variable_expand ("$(HOME)");
2505 warn_undefined_variables_flag = save;
2508 is_variable = home_dir[0] != '\0';
2509 if (!is_variable)
2511 free (home_dir);
2512 home_dir = getenv ("HOME");
2514 #if !defined(_AMIGA) && !defined(WINDOWS32)
2515 if (home_dir == 0 || home_dir[0] == '\0')
2517 extern char *getlogin ();
2518 char *logname = getlogin ();
2519 home_dir = 0;
2520 if (logname != 0)
2522 struct passwd *p = getpwnam (logname);
2523 if (p != 0)
2524 home_dir = p->pw_dir;
2527 #endif /* !AMIGA && !WINDOWS32 */
2528 if (home_dir != 0)
2530 char *new = concat (home_dir, "", name + 1);
2531 if (is_variable)
2532 free (home_dir);
2533 return new;
2536 #if !defined(_AMIGA) && !defined(WINDOWS32)
2537 else
2539 struct passwd *pwent;
2540 char *userend = index (name + 1, '/');
2541 if (userend != 0)
2542 *userend = '\0';
2543 pwent = getpwnam (name + 1);
2544 if (pwent != 0)
2546 if (userend == 0)
2547 return xstrdup (pwent->pw_dir);
2548 else
2549 return concat (pwent->pw_dir, "/", userend + 1);
2551 else if (userend != 0)
2552 *userend = '/';
2554 #endif /* !AMIGA && !WINDOWS32 */
2555 #endif /* !VMS */
2556 return 0;
2559 /* Given a chain of struct nameseq's describing a sequence of filenames,
2560 in reverse of the intended order, return a new chain describing the
2561 result of globbing the filenames. The new chain is in forward order.
2562 The links of the old chain are freed or used in the new chain.
2563 Likewise for the names in the old chain.
2565 SIZE is how big to construct chain elements.
2566 This is useful if we want them actually to be other structures
2567 that have room for additional info. */
2569 struct nameseq *
2570 multi_glob (chain, size)
2571 struct nameseq *chain;
2572 unsigned int size;
2574 extern void dir_setup_glob ();
2575 register struct nameseq *new = 0;
2576 register struct nameseq *old;
2577 struct nameseq *nexto;
2578 glob_t gl;
2580 dir_setup_glob (&gl);
2582 for (old = chain; old != 0; old = nexto)
2584 #ifndef NO_ARCHIVES
2585 char *memname;
2586 #endif
2588 nexto = old->next;
2590 if (old->name[0] == '~')
2592 char *newname = tilde_expand (old->name);
2593 if (newname != 0)
2595 free (old->name);
2596 old->name = newname;
2600 #ifndef NO_ARCHIVES
2601 if (ar_name (old->name))
2603 /* OLD->name is an archive member reference.
2604 Replace it with the archive file name,
2605 and save the member name in MEMNAME.
2606 We will glob on the archive name and then
2607 reattach MEMNAME later. */
2608 char *arname;
2609 ar_parse_name (old->name, &arname, &memname);
2610 free (old->name);
2611 old->name = arname;
2613 else
2614 memname = 0;
2615 #endif /* !NO_ARCHIVES */
2617 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
2619 case 0: /* Success. */
2621 register int i = gl.gl_pathc;
2622 while (i-- > 0)
2624 #ifndef NO_ARCHIVES
2625 if (memname != 0)
2627 /* Try to glob on MEMNAME within the archive. */
2628 struct nameseq *found
2629 = ar_glob (gl.gl_pathv[i], memname, size);
2630 if (found == 0)
2632 /* No matches. Use MEMNAME as-is. */
2633 struct nameseq *elt
2634 = (struct nameseq *) xmalloc (size);
2635 unsigned int alen = strlen (gl.gl_pathv[i]);
2636 unsigned int mlen = strlen (memname);
2637 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2638 bcopy (gl.gl_pathv[i], elt->name, alen);
2639 elt->name[alen] = '(';
2640 bcopy (memname, &elt->name[alen + 1], mlen);
2641 elt->name[alen + 1 + mlen] = ')';
2642 elt->name[alen + 1 + mlen + 1] = '\0';
2643 elt->next = new;
2644 new = elt;
2646 else
2648 /* Find the end of the FOUND chain. */
2649 struct nameseq *f = found;
2650 while (f->next != 0)
2651 f = f->next;
2653 /* Attach the chain being built to the end of the FOUND
2654 chain, and make FOUND the new NEW chain. */
2655 f->next = new;
2656 new = found;
2659 free (memname);
2661 else
2662 #endif /* !NO_ARCHIVES */
2664 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2665 elt->name = xstrdup (gl.gl_pathv[i]);
2666 elt->next = new;
2667 new = elt;
2670 globfree (&gl);
2671 free (old->name);
2672 free ((char *)old);
2673 break;
2676 case GLOB_NOSPACE:
2677 fatal (NILF, "virtual memory exhausted");
2678 break;
2680 default:
2681 old->next = new;
2682 new = old;
2683 break;
2687 return new;