1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988-2020 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 it under the
6 terms of the GNU General Public License as published by the Free Software
7 Foundation; either version 3 of the License, or (at your option) any later
10 GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
11 WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
12 A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License along with
15 this program. If not, see <http://www.gnu.org/licenses/>. */
35 #else /* !WINDOWS32 */
40 struct passwd *getpwnam (char *name);
43 #endif /* !WINDOWS32 */
45 /* A 'struct ebuffer' controls the origin of the makefile we are currently
51 char *buffer; /* Start of the current line in the buffer. */
52 char *bufnext; /* Start of the next line in the buffer. */
53 char *bufstart; /* Start of the entire buffer. */
54 size_t size; /* Malloc'd size of buffer. */
55 FILE *fp; /* File, or NULL if this is an internal buffer. */
56 floc floc; /* Info on the file in fp (if any). */
59 /* Track the modifiers we can have on variable assignments */
63 unsigned int assign_v:1;
64 unsigned int define_v:1;
65 unsigned int undefine_v:1;
66 unsigned int export_v:1;
67 unsigned int override_v:1;
68 unsigned int private_v:1;
71 /* Types of "words" that can be read in a makefile. */
74 w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
75 w_varassign, w_ampcolon, w_ampdcolon
79 /* A 'struct conditionals' contains the information describing
80 all the active conditionals in a makefile.
82 The global variable 'conditionals' contains the conditionals
83 information for the current makefile. It is initialized from
84 the static structure 'toplevel_conditionals' and is later changed
85 to new structures for included makefiles. */
89 unsigned int if_cmds; /* Depth of conditional nesting. */
90 unsigned int allocated; /* Elts allocated in following arrays. */
91 char *ignoring; /* Are we ignoring or interpreting?
92 0=interpreting, 1=not yet interpreted,
93 2=already interpreted */
94 char *seen_else; /* Have we already seen an 'else'? */
97 static struct conditionals toplevel_conditionals;
98 static struct conditionals *conditionals = &toplevel_conditionals;
101 /* Default directories to search for include files in */
103 static const char *default_include_directories[] =
105 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
106 /* This completely up to the user when they install MSVC or other packages.
107 This is defined as a placeholder. */
108 # define INCLUDEDIR "."
113 "/usr/local/include",
119 /* List of directories to search for include files in */
121 static const char **include_directories;
123 /* Maximum length of an element of the above. */
125 static size_t max_incl_len;
127 /* The filename and pointer to line number of the
128 makefile currently being read in. */
130 const floc *reading_file = 0;
132 /* The chain of files read by read_all_makefiles. */
134 static struct goaldep *read_files = 0;
136 static struct goaldep *eval_makefile (const char *filename, unsigned short flags);
137 static void eval (struct ebuffer *buffer, int flags);
139 static long readline (struct ebuffer *ebuf);
140 static void do_undefine (char *name, enum variable_origin origin,
141 struct ebuffer *ebuf);
142 static struct variable *do_define (char *name, enum variable_origin origin,
143 struct ebuffer *ebuf);
144 static int conditional_line (char *line, size_t len, const floc *flocp);
145 static void record_files (struct nameseq *filenames, int are_also_makes,
147 const char *pattern_percent, char *depstr,
148 unsigned int cmds_started, char *commands,
149 size_t commands_idx, int two_colon,
150 char prefix, const floc *flocp);
151 static void record_target_var (struct nameseq *filenames, char *defn,
152 enum variable_origin origin,
153 struct vmodifiers *vmod,
155 static enum make_word_type get_next_mword (char *buffer,
156 char **startp, size_t *length);
157 static void remove_comments (char *line);
158 static char *find_map_unquote (char *string, int map);
159 static char *find_char_unquote (char *string, int stop);
160 static char *unescape_char (char *string, int c);
163 /* Compare a word, both length and contents.
164 P must point to the word to be tested, and WLEN must be the length.
166 #define word1eq(s) (wlen == CSTRLEN (s) && strneq (s, p, CSTRLEN (s)))
169 /* Read in all the makefiles and return a chain of targets to rebuild. */
172 read_all_makefiles (const char **makefiles)
174 unsigned int num_makefiles = 0;
176 /* Create *_LIST variables, to hold the makefiles, targets, and variables
177 we will be reading. */
179 define_variable_cname ("MAKEFILE_LIST", "", o_file, 0);
181 DB (DB_BASIC, (_("Reading makefiles...\n")));
183 /* If there's a non-null variable MAKEFILES, its value is a list of
184 files to read first thing. But don't let it prevent reading the
185 default makefiles and don't let the default goal come from there. */
193 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
194 int save = warn_undefined_variables_flag;
195 warn_undefined_variables_flag = 0;
197 value = allocated_variable_expand ("$(MAKEFILES)");
199 warn_undefined_variables_flag = save;
202 /* Set NAME to the start of next token and LENGTH to its length.
203 MAKEFILES is updated for finding remaining tokens. */
206 while ((name = find_next_token ((const char **)&p, &length)) != 0)
210 eval_makefile (strcache_add (name), RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE);
216 /* Read makefiles specified with -f switches. */
219 while (*makefiles != 0)
221 struct goaldep *d = eval_makefile (*makefiles, 0);
224 perror_with_name ("", *makefiles);
226 /* Reuse the storage allocated for the read_file. */
227 *makefiles = dep_name (d);
232 /* If there were no -f switches, try the default names. */
234 if (num_makefiles == 0)
236 static const char *default_makefiles[] =
238 /* all lower case since readdir() (the vms version) 'lowercasifies' */
239 /* TODO: Above is not always true, this needs more work */
240 { "makefile.vms", "gnumakefile", "makefile", 0 };
243 { "GNUmakefile", "Makefile", "SMakefile", 0 };
244 #else /* !Amiga && !VMS */
246 { "GNUmakefile", "makefile", "Makefile", "makefile.mak", 0 };
247 #else /* !Amiga && !VMS && !WINDOWS32 */
248 { "GNUmakefile", "makefile", "Makefile", 0 };
249 #endif /* !Amiga && !VMS && !WINDOWS32 */
252 const char **p = default_makefiles;
253 while (*p != 0 && !file_exists_p (*p))
258 eval_makefile (*p, 0);
260 perror_with_name ("", *p);
264 /* No default makefile was found. Add the default makefiles to the
265 'read_files' chain so they will be updated if possible. */
266 struct goaldep *tail = read_files;
267 /* Add them to the tail, after any MAKEFILES variable makefiles. */
268 while (tail != 0 && tail->next != 0)
270 for (p = default_makefiles; *p != 0; ++p)
272 struct goaldep *d = alloc_goaldep ();
273 d->file = enter_file (strcache_add (*p));
274 /* Tell update_goal_chain to bail out as soon as this file is
275 made, and main not to die if we can't make this file. */
276 d->flags = RM_DONTCARE;
291 /* Install a new conditional and return the previous one. */
293 static struct conditionals *
294 install_conditionals (struct conditionals *new)
296 struct conditionals *save = conditionals;
298 memset (new, '\0', sizeof (*new));
304 /* Free the current conditionals and reinstate a saved one. */
307 restore_conditionals (struct conditionals *saved)
309 /* Free any space allocated by conditional_line. */
310 free (conditionals->ignoring);
311 free (conditionals->seen_else);
314 conditionals = saved;
317 static struct goaldep *
318 eval_makefile (const char *filename, unsigned short flags)
320 struct goaldep *deps;
325 /* Create a new goaldep entry. */
326 deps = alloc_goaldep ();
327 deps->next = read_files;
330 ebuf.floc.filenm = filename; /* Use the original file name. */
331 ebuf.floc.lineno = 1;
332 ebuf.floc.offset = 0;
334 if (ISDB (DB_VERBOSE))
336 printf (_("Reading makefile '%s'"), filename);
337 if (flags & RM_NO_DEFAULT_GOAL)
338 printf (_(" (no default goal)"));
339 if (flags & RM_INCLUDED)
340 printf (_(" (search path)"));
341 if (flags & RM_DONTCARE)
342 printf (_(" (don't care)"));
343 if (flags & RM_NO_TILDE)
344 printf (_(" (no ~ expansion)"));
348 /* First, get a stream to read. */
350 /* Expand ~ in FILENAME unless it came from 'include',
351 in which case it was already done. */
352 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
354 expanded = tilde_expand (filename);
360 ENULLLOOP (ebuf.fp, fopen (filename, "r"));
363 /* Check for unrecoverable errors: out of mem or FILE slots. */
374 const char *err = strerror (deps->error);
375 OS (fatal, reading_file, "%s", err);
379 /* If the makefile wasn't found and it's either a makefile from
380 the 'MAKEFILES' variable or an included makefile,
381 search the included makefile search path for this makefile. */
382 if (ebuf.fp == 0 && (flags & RM_INCLUDED) && *filename != '/')
385 for (i = 0; include_directories[i] != 0; ++i)
387 const char *included = concat (3, include_directories[i],
389 ebuf.fp = fopen (included, "r");
398 /* Enter the final name for this makefile as a goaldep. */
399 filename = strcache_add (filename);
400 deps->file = lookup_file (filename);
402 deps->file = enter_file (filename);
403 filename = deps->file->name;
410 /* The makefile can't be read at all, give up entirely.
411 If we did some searching errno has the error from the last attempt,
412 rather from FILENAME itself: recover the more accurate one. */
414 deps->file->last_mtime = NONEXISTENT_MTIME;
418 /* Success; clear errno. */
421 /* Avoid leaking the makefile to children. */
422 fd_noinherit (fileno (ebuf.fp));
424 /* Add this makefile to the list. */
425 do_variable_definition (&ebuf.floc, "MAKEFILE_LIST", filename, o_file,
428 /* Evaluate the makefile */
431 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = xmalloc (ebuf.size);
433 curfile = reading_file;
434 reading_file = &ebuf.floc;
436 eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
438 reading_file = curfile;
442 free (ebuf.bufstart);
450 eval_buffer (char *buffer, const floc *flocp)
453 struct conditionals *saved;
454 struct conditionals new;
457 /* Evaluate the buffer */
459 ebuf.size = strlen (buffer);
460 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = buffer;
465 else if (reading_file)
466 ebuf.floc = *reading_file;
469 ebuf.floc.filenm = NULL;
470 ebuf.floc.lineno = 1;
471 ebuf.floc.offset = 0;
474 curfile = reading_file;
475 reading_file = &ebuf.floc;
477 saved = install_conditionals (&new);
481 restore_conditionals (saved);
483 reading_file = curfile;
488 /* Check LINE to see if it's a variable assignment or undefine.
490 It might use one of the modifiers "export", "override", "private", or it
491 might be one of the conditional tokens like "ifdef", "include", etc.
493 If it's not a variable assignment or undefine, VMOD.V_ASSIGN is 0.
496 Returns a pointer to the first non-modifier character, and sets VMOD
497 based on the modifiers found if any, plus V_ASSIGN is 1.
500 parse_var_assignment (const char *line, struct vmodifiers *vmod)
503 memset (vmod, '\0', sizeof (*vmod));
505 /* Find the start of the next token. If there isn't one we're done. */
508 return (char *) line;
517 p2 = parse_variable_definition (p, &v);
519 /* If this is a variable assignment, we're done. */
523 /* It's not a variable; see if it's a modifier. */
524 p2 = end_of_token (p);
527 if (word1eq ("export"))
529 else if (word1eq ("override"))
530 vmod->override_v = 1;
531 else if (word1eq ("private"))
533 else if (word1eq ("define"))
535 /* We can't have modifiers after 'define' */
540 else if (word1eq ("undefine"))
542 /* We can't have modifiers after 'undefine' */
543 vmod->undefine_v = 1;
548 /* Not a variable or modifier: this is not a variable assignment. */
549 return (char *) line;
551 /* It was a modifier. Try the next word. */
554 return (char *) line;
557 /* Found a variable assignment or undefine. */
563 /* Read file FILENAME as a makefile and add its contents to the data base.
565 SET_DEFAULT is true if we are allowed to set the default goal. */
568 eval (struct ebuffer *ebuf, int set_default)
571 size_t collapsed_length = 0;
572 size_t commands_len = 200;
574 size_t commands_idx = 0;
575 unsigned int cmds_started, tgts_started;
576 int ignoring = 0, in_ignored_define = 0;
577 int no_targets = 0; /* Set when reading a rule without targets. */
578 int also_make_targets = 0; /* Set when reading grouped targets. */
579 struct nameseq *filenames = 0;
583 char prefix = cmd_prefix;
584 const char *pattern = 0;
585 const char *pattern_percent;
589 #define record_waiting_files() \
592 if (filenames != 0) \
594 fi.lineno = tgts_started; \
596 record_files (filenames, also_make_targets, pattern, \
597 pattern_percent, depstr, \
598 cmds_started, commands, commands_idx, two_colon, \
605 also_make_targets = 0; \
609 cmds_started = tgts_started = 1;
611 fstart = &ebuf->floc;
612 fi.filenm = ebuf->floc.filenm;
614 /* Loop over lines in the file.
615 The strategy is to accumulate target names in FILENAMES, dependencies
616 in DEPS and commands in COMMANDS. These are used to define a rule
617 when the start of the next rule (or eof) is encountered.
619 When you see a "continue" in the loop below, that means we are moving on
620 to the next line. If you see record_waiting_files(), then the statement
621 we are parsing also finishes the previous rule. */
623 commands = xmalloc (200);
632 struct vmodifiers vmod;
634 /* At the top of this loop, we are starting a brand new line. */
635 /* Grab the next line to be evaluated */
636 ebuf->floc.lineno += nlines;
637 nlines = readline (ebuf);
639 /* If there is nothing left to eval, we're done. */
645 /* If this is the first line, check for a UTF-8 BOM and skip it. */
646 if (ebuf->floc.lineno == 1)
648 unsigned char *ul = (unsigned char *) line;
649 if (ul[0] == 0xEF && ul[1] == 0xBB && ul[2] == 0xBF)
654 if (ebuf->floc.filenm)
655 printf (_("Skipping UTF-8 BOM in makefile '%s'\n"),
658 printf (_("Skipping UTF-8 BOM in makefile buffer\n"));
662 /* If this line is empty, skip it. */
666 linelen = strlen (line);
668 /* Check for a shell command line first.
669 If it is not one, we can stop treating cmd_prefix specially. */
670 if (line[0] == cmd_prefix)
673 /* Ignore the commands in a rule with no targets. */
676 /* If there is no preceding rule line, don't treat this line
677 as a command, even though it begins with a recipe prefix.
678 SunOS 4 make appears to behave this way. */
683 /* Yep, this is a shell command, and we don't care. */
686 if (commands_idx == 0)
687 cmds_started = ebuf->floc.lineno;
689 /* Append this command line to the line being accumulated.
690 Skip the initial command prefix character. */
691 if (linelen + commands_idx > commands_len)
693 commands_len = (linelen + commands_idx) * 2;
694 commands = xrealloc (commands, commands_len);
696 memcpy (&commands[commands_idx], line + 1, linelen - 1);
697 commands_idx += linelen - 1;
698 commands[commands_idx++] = '\n';
703 /* This line is not a shell command line. Don't worry about whitespace.
704 Get more space if we need it; we don't need to preserve the current
705 contents of the buffer. */
707 if (collapsed_length < linelen+1)
709 collapsed_length = linelen+1;
711 /* Don't need xrealloc: we don't need to preserve the content. */
712 collapsed = xmalloc (collapsed_length);
714 strcpy (collapsed, line);
715 /* Collapse continuation lines. */
716 collapse_continuations (collapsed);
717 remove_comments (collapsed);
719 /* Get rid if starting space (including formfeed, vtab, etc.) */
723 /* See if this is a variable assignment. We need to do this early, to
724 allow variables with names like 'ifdef', 'export', 'private', etc. */
725 p = parse_var_assignment (p, &vmod);
729 enum variable_origin origin = vmod.override_v ? o_override : o_file;
731 /* If we're ignoring then we're done now. */
735 in_ignored_define = 1;
739 /* Variable assignment ends the previous rule. */
740 record_waiting_files ();
744 do_undefine (p, origin, ebuf);
747 else if (vmod.define_v)
748 v = do_define (p, origin, ebuf);
750 v = try_variable_definition (fstart, p, origin, 0);
755 v->export = v_export;
759 /* This line has been dealt with. */
763 /* If this line is completely empty, ignore it. */
767 p2 = end_of_token (p);
771 /* If we're in an ignored define, skip this line (but maybe get out). */
772 if (in_ignored_define)
774 /* See if this is an endef line (plus optional comment). */
775 if (word1eq ("endef") && STOP_SET (*p2, MAP_COMMENT|MAP_NUL))
776 in_ignored_define = 0;
781 /* Check for conditional state changes. */
783 int i = conditional_line (p, wlen, fstart);
787 O (fatal, fstart, _("invalid syntax in conditional"));
794 /* Nothing to see here... move along. */
798 /* Manage the "export" keyword used outside of variable assignment
799 as well as "unexport". */
800 if (word1eq ("export") || word1eq ("unexport"))
802 int exporting = *p == 'u' ? 0 : 1;
804 /* Export/unexport ends the previous rule. */
805 record_waiting_files ();
807 /* (un)export by itself causes everything to be (un)exported. */
809 export_all_variables = exporting;
816 /* Expand the line so we can use indirect and constructed
817 variable names in an (un)export command. */
818 cp = ap = allocated_variable_expand (p2);
820 for (p = find_next_token (&cp, &l); p != 0;
821 p = find_next_token (&cp, &l))
823 struct variable *v = lookup_variable (p, l);
825 v = define_variable_global (p, l, "", o_file, 0, fstart);
826 v->export = exporting ? v_export : v_noexport;
834 /* Handle the special syntax for vpath. */
835 if (word1eq ("vpath"))
841 /* vpath ends the previous rule. */
842 record_waiting_files ();
844 cp = variable_expand (p2);
845 p = find_next_token (&cp, &l);
848 vpat = xstrndup (p, l);
849 p = find_next_token (&cp, &l);
850 /* No searchpath means remove all previous
851 selective VPATH's with the same pattern. */
854 /* No pattern means remove all previous selective VPATH's. */
856 construct_vpath_list (vpat, p);
862 /* Handle include and variants. */
863 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
865 /* We have found an 'include' line specifying a nested
866 makefile to be read at this point. */
867 struct conditionals *save;
868 struct conditionals new_conditionals;
869 struct nameseq *files;
870 /* "-include" (vs "include") says no error if the file does not
871 exist. "sinclude" is an alias for this from SGI. */
872 int noerror = (p[0] != 'i');
874 /* Include ends the previous rule. */
875 record_waiting_files ();
877 p = allocated_variable_expand (p2);
879 /* If no filenames, it's a no-op. */
886 /* Parse the list of file names. Don't expand archive references! */
888 files = PARSE_FILE_SEQ (&p2, struct nameseq, MAP_NUL, NULL,
892 /* Save the state of conditionals and start
893 the included makefile with a clean slate. */
894 save = install_conditionals (&new_conditionals);
896 /* Record the rules that are waiting so they will determine
897 the default goal before those in the included makefile. */
898 record_waiting_files ();
900 /* Read each included makefile. */
903 struct nameseq *next = files->next;
904 unsigned short flags = (RM_INCLUDED | RM_NO_TILDE
905 | (noerror ? RM_DONTCARE : 0)
906 | (set_default ? 0 : RM_NO_DEFAULT_GOAL));
908 struct goaldep *d = eval_makefile (files->name, flags);
917 /* Restore conditional state. */
918 restore_conditionals (save);
923 /* Handle the load operations. */
924 if (word1eq ("load") || word1eq ("-load"))
926 /* A 'load' line specifies a dynamic object to load. */
927 struct nameseq *files;
928 int noerror = (p[0] == '-');
930 /* Load ends the previous rule. */
931 record_waiting_files ();
933 p = allocated_variable_expand (p2);
935 /* If no filenames, it's a no-op. */
942 /* Parse the list of file names.
943 Don't expand archive references or strip "./" */
945 files = PARSE_FILE_SEQ (&p2, struct nameseq, MAP_NUL, NULL,
949 /* Load each file. */
952 struct nameseq *next = files->next;
953 const char *name = files->name;
954 struct goaldep *deps;
957 /* Load the file. 0 means failure. */
958 r = load_file (&ebuf->floc, &name, noerror);
959 if (! r && ! noerror)
960 OS (fatal, &ebuf->floc, _("%s: failed to load"), name);
965 /* Return of -1 means a special load: don't rebuild it. */
969 /* It succeeded, so add it to the list "to be rebuilt". */
970 deps = alloc_goaldep ();
971 deps->next = read_files;
973 deps->file = lookup_file (name);
975 deps->file = enter_file (name);
976 deps->file->loaded = 1;
982 /* This line starts with a tab but was not caught above because there
983 was no preceding target, and the line might have been usable as a
984 variable definition. But now we know it is definitely lossage. */
985 if (line[0] == cmd_prefix)
986 O (fatal, fstart, _("recipe commences before first target"));
988 /* This line describes some target files. This is complicated by
989 the existence of target-specific variables, because we can't
990 expand the entire line until we know if we have one or not. So
991 we expand the line word by word until we find the first ':',
992 then check to see if it's a target-specific variable.
994 In this algorithm, 'lb_next' will point to the beginning of the
995 unexpanded parts of the input buffer, while 'p2' points to the
996 parts of the expanded buffer we haven't searched yet. */
999 enum make_word_type wtype;
1000 char *cmdleft, *semip, *lb_next;
1003 const char *end, *beg; /* Helpers for whitespace stripping. */
1005 /* Record the previous rule. */
1007 record_waiting_files ();
1008 tgts_started = fstart->lineno;
1010 /* Search the line for an unquoted ; that is not after an
1012 cmdleft = find_map_unquote (line, MAP_SEMI|MAP_COMMENT|MAP_VARIABLE);
1013 if (cmdleft != 0 && *cmdleft == '#')
1015 /* We found a comment before a semicolon. */
1019 else if (cmdleft != 0)
1020 /* Found one. Cut the line short there before expanding it. */
1021 *(cmdleft++) = '\0';
1024 collapse_continuations (line);
1026 /* We can't expand the entire line, since if it's a per-target
1027 variable we don't want to expand it. So, walk from the
1028 beginning, expanding as we go, and looking for "interesting"
1029 chars. The first word is always expandable. */
1030 wtype = get_next_mword (line, &lb_next, &wlen);
1035 O (fatal, fstart, _("missing rule before recipe"));
1036 /* This line contained something but turned out to be nothing
1037 but whitespace (a comment?). */
1044 /* We accept and ignore rules without targets for
1045 compatibility with SunOS 4 make. */
1053 p2 = variable_expand_string (NULL, lb_next, wlen);
1060 /* Look for a semicolon in the expanded line. */
1061 cmdleft = find_char_unquote (p2, ';');
1065 size_t p2_off = p2 - variable_buffer;
1066 size_t cmd_off = cmdleft - variable_buffer;
1067 char *pend = p2 + strlen (p2);
1069 /* Append any remnants of lb, then cut the line short
1070 at the semicolon. */
1073 /* One school of thought says that you shouldn't expand
1074 here, but merely copy, since now you're beyond a ";"
1075 and into a command script. However, the old parser
1076 expanded the whole line, so we continue that for
1077 backwards-compatibility. Also, it wouldn't be
1078 entirely consistent, since we do an unconditional
1079 expand below once we know we don't have a
1080 target-specific variable. */
1081 variable_expand_string (pend, lb_next, SIZE_MAX);
1082 lb_next += strlen (lb_next);
1083 p2 = variable_buffer + p2_off;
1084 cmdleft = variable_buffer + cmd_off + 1;
1088 colonp = find_char_unquote (p2, ':');
1090 #ifdef HAVE_DOS_PATHS
1092 /* The drive spec brain-damage strikes again...
1093 Note that the only separators of targets in this context are
1094 whitespace and a left paren. If others are possible, add them
1095 to the string in the call to strchr. */
1096 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
1097 isalpha ((unsigned char) colonp[-1]) &&
1098 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
1099 colonp = find_char_unquote (colonp + 1, ':');
1104 /* If the previous character is '&', back up before '&:' */
1105 if (colonp > p2 && colonp[-1] == '&')
1111 wtype = get_next_mword (lb_next, &lb_next, &wlen);
1117 p2 = variable_expand_string (p2, lb_next, wlen);
1118 /* We don't need to worry about cmdleft here, because if it was
1119 found in the variable_buffer the entire buffer has already
1120 been expanded... we'll never get here. */
1123 p2 = next_token (variable_buffer);
1125 /* If the word we're looking at is EOL, see if there's _anything_
1126 on the line. If not, a variable expanded to nothing, so ignore
1127 it. If so, we can't parse this line so punt. */
1133 /* There's no need to be ivory-tower about this: check for
1134 one of the most common bugs found in makefiles... */
1135 if (cmd_prefix == '\t' && strneq (line, " ", 8))
1136 O (fatal, fstart, _("missing separator (did you mean TAB instead of 8 spaces?)"));
1138 O (fatal, fstart, _("missing separator"));
1142 char save = *colonp;
1144 /* If we have &:, it specifies that the targets are understood to be
1145 updated/created together by a single invocation of the recipe. */
1147 also_make_targets = 1;
1149 /* Make the colon the end-of-string so we know where to stop
1150 looking for targets. Start there again once we're done. */
1152 filenames = PARSE_SIMPLE_SEQ (&p2, struct nameseq);
1154 p2 = colonp + (save == '&');
1159 /* We accept and ignore rules without targets for
1160 compatibility with SunOS 4 make. */
1164 /* This should never be possible; we handled it above. */
1165 assert (*p2 != '\0');
1168 /* Is this a one-colon or two-colon entry? */
1169 two_colon = *p2 == ':';
1173 /* Test to see if it's a target-specific variable. Copy the rest
1174 of the buffer over, possibly temporarily (we'll expand it later
1175 if it's not a target-specific variable). PLEN saves the length
1176 of the unparsed section of p2, for later. */
1177 if (*lb_next != '\0')
1179 size_t l = p2 - variable_buffer;
1181 variable_buffer_output (p2+plen, lb_next, strlen (lb_next)+1);
1182 p2 = variable_buffer + l;
1185 p2 = parse_var_assignment (p2, &vmod);
1188 /* If there was a semicolon found, add it back, plus anything
1192 size_t l = p2 - variable_buffer;
1194 collapse_continuations (semip);
1195 variable_buffer_output (p2 + strlen (p2),
1196 semip, strlen (semip)+1);
1197 p2 = variable_buffer + l;
1199 record_target_var (filenames, p2,
1200 vmod.override_v ? o_override : o_file,
1206 /* This is a normal target, _not_ a target-specific variable.
1207 Unquote any = in the dependency list. */
1208 find_char_unquote (lb_next, '=');
1210 /* Remember the command prefix for this target. */
1211 prefix = cmd_prefix;
1213 /* We have some targets, so don't ignore the following commands. */
1216 /* Expand the dependencies, etc. */
1217 if (*lb_next != '\0')
1219 size_t l = p2 - variable_buffer;
1220 variable_expand_string (p2 + plen, lb_next, SIZE_MAX);
1221 p2 = variable_buffer + l;
1223 /* Look for a semicolon in the expanded line. */
1226 cmdleft = find_char_unquote (p2, ';');
1228 *(cmdleft++) = '\0';
1232 /* Is this a static pattern rule: 'target: %targ: %dep; ...'? */
1233 p = strchr (p2, ':');
1234 while (p != 0 && p[-1] == '\\')
1238 while (*q-- == '\\')
1239 backslash = !backslash;
1241 p = strchr (p + 1, ':');
1246 /* Here, the situation is quite complicated. Let's have a look
1247 at a couple of targets:
1255 The rule is that it's only a target, if there are TWO :'s
1256 OR a space around the :.
1258 if (p && !(ISSPACE (p[1]) || !p[1] || ISSPACE (p[-1])))
1261 #ifdef HAVE_DOS_PATHS
1266 /* For DOS-style paths, skip a "C:\..." or a "C:/..." */
1267 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
1268 isalpha ((unsigned char)p[-1]) &&
1269 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1270 p = strchr (p + 1, ':');
1273 } while (check_again);
1278 struct nameseq *target;
1279 target = PARSE_FILE_SEQ (&p2, struct nameseq, MAP_COLON, NULL,
1283 O (fatal, fstart, _("missing target pattern"));
1284 else if (target->next != 0)
1285 O (fatal, fstart, _("multiple target patterns"));
1286 pattern_percent = find_percent_cached (&target->name);
1287 pattern = target->name;
1288 if (pattern_percent == 0)
1289 O (fatal, fstart, _("target pattern contains no '%%'"));
1295 /* Strip leading and trailing whitespaces. */
1297 end = beg + strlen (beg) - 1;
1298 strip_whitespace (&beg, &end);
1300 /* Put all the prerequisites here; they'll be parsed later. */
1301 if (beg <= end && *beg != '\0')
1302 depstr = xstrndup (beg, end - beg + 1);
1309 /* Semicolon means rest of line is a command. */
1310 size_t l = strlen (cmdleft);
1312 cmds_started = fstart->lineno;
1314 /* Add this command line to the buffer. */
1315 if (l + 2 > commands_len)
1317 commands_len = (l + 2) * 2;
1318 commands = xrealloc (commands, commands_len);
1320 memcpy (commands, cmdleft, l);
1322 commands[commands_idx++] = '\n';
1325 /* Determine if this target should be made default. We used to do
1326 this in record_files() but because of the delayed target recording
1327 and because preprocessor directives are legal in target's commands
1328 it is too late. Consider this fragment for example:
1332 ifeq ($(.DEFAULT_GOAL),foo)
1336 Because the target is not recorded until after ifeq directive is
1337 evaluated the .DEFAULT_GOAL does not contain foo yet as one
1338 would expect. Because of this we have to move the logic here. */
1340 if (set_default && default_goal_var->value[0] == '\0')
1343 struct nameseq *t = filenames;
1345 for (; t != 0; t = t->next)
1348 const char *name = t->name;
1350 /* We have nothing to do if this is an implicit rule. */
1351 if (strchr (name, '%') != 0)
1354 /* See if this target's name does not start with a '.',
1355 unless it contains a slash. */
1356 if (*name == '.' && strchr (name, '/') == 0
1357 #ifdef HAVE_DOS_PATHS
1358 && strchr (name, '\\') == 0
1364 /* If this file is a suffix, don't let it be
1365 the default goal file. */
1366 for (d = suffix_file->deps; d != 0; d = d->next)
1369 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1374 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1376 size_t l = strlen (dep_name (d2));
1377 if (!strneq (name, dep_name (d2), l))
1379 if (streq (name + l, dep_name (d)))
1392 define_variable_global (".DEFAULT_GOAL", 13, t->name,
1403 if (conditionals->if_cmds)
1404 O (fatal, fstart, _("missing 'endif'"));
1406 /* At eof, record the last rule. */
1407 record_waiting_files ();
1414 /* Remove comments from LINE.
1415 This will also remove backslashes that escape things.
1416 It ignores comment characters that appear inside variable references. */
1419 remove_comments (char *line)
1423 comment = find_map_unquote (line, MAP_COMMENT|MAP_VARIABLE);
1426 /* Cut off the line at the #. */
1430 /* Execute a 'undefine' directive.
1431 The undefine line has already been read, and NAME is the name of
1432 the variable to be undefined. */
1435 do_undefine (char *name, enum variable_origin origin, struct ebuffer *ebuf)
1439 /* Expand the variable name and find the beginning (NAME) and end. */
1440 var = allocated_variable_expand (name);
1441 name = next_token (var);
1443 O (fatal, &ebuf->floc, _("empty variable name"));
1444 p = name + strlen (name) - 1;
1445 while (p > name && ISBLANK (*p))
1449 undefine_variable_global (name, p - name + 1, origin);
1453 /* Execute a 'define' directive.
1454 The first line has already been read, and NAME is the name of
1455 the variable to be defined. The following lines remain to be read. */
1457 static struct variable *
1458 do_define (char *name, enum variable_origin origin, struct ebuffer *ebuf)
1461 struct variable var;
1464 size_t length = 100;
1465 char *definition = xmalloc (length);
1469 defstart = ebuf->floc;
1471 p = parse_variable_definition (name, &var);
1473 /* No assignment token, so assume recursive. */
1474 var.flavor = f_recursive;
1477 if (var.value[0] != '\0')
1478 O (error, &defstart, _("extraneous text after 'define' directive"));
1480 /* Chop the string before the assignment token to get the name. */
1481 var.name[var.length] = '\0';
1484 /* Expand the variable name and find the beginning (NAME) and end. */
1485 n = allocated_variable_expand (name);
1486 name = next_token (n);
1487 if (name[0] == '\0')
1488 O (fatal, &defstart, _("empty variable name"));
1489 p = name + strlen (name) - 1;
1490 while (p > name && ISBLANK (*p))
1494 /* Now read the value of the variable. */
1499 long nlines = readline (ebuf);
1501 /* If there is nothing left to be eval'd, there's no 'endef'!! */
1503 O (fatal, &defstart, _("missing 'endef', unterminated 'define'"));
1505 ebuf->floc.lineno += nlines;
1506 line = ebuf->buffer;
1508 collapse_continuations (line);
1510 /* If the line doesn't begin with a tab, test to see if it introduces
1511 another define, or ends one. Stop if we find an 'endef' */
1512 if (line[0] != cmd_prefix)
1514 p = next_token (line);
1517 /* If this is another 'define', increment the level count. */
1518 if ((len == 6 || (len > 6 && ISBLANK (p[6])))
1519 && strneq (p, "define", 6))
1522 /* If this is an 'endef', decrement the count. If it's now 0,
1523 we've found the last one. */
1524 else if ((len == 5 || (len > 5 && ISBLANK (p[5])))
1525 && strneq (p, "endef", 5))
1528 remove_comments (p);
1529 if (*(next_token (p)) != '\0')
1530 O (error, &ebuf->floc,
1531 _("extraneous text after 'endef' directive"));
1538 /* Add this line to the variable definition. */
1539 len = strlen (line);
1540 if (idx + len + 1 > length)
1542 length = (idx + len) * 2;
1543 definition = xrealloc (definition, length + 1);
1546 memcpy (&definition[idx], line, len);
1548 /* Separate lines with a newline. */
1549 definition[idx++] = '\n';
1552 /* We've got what we need; define the variable. */
1554 definition[0] = '\0';
1556 definition[idx - 1] = '\0';
1558 v = do_variable_definition (&defstart, name,
1559 definition, origin, var.flavor, 0);
1565 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1566 "ifneq", "else" and "endif".
1567 LINE is the input line, with the command as its first word.
1569 FILENAME and LINENO are the filename and line number in the
1570 current makefile. They are used for error messages.
1572 Value is -2 if the line is not a conditional at all,
1573 -1 if the line is an invalid conditional,
1574 0 if following text should be interpreted,
1575 1 if following text should be ignored. */
1578 conditional_line (char *line, size_t len, const floc *flocp)
1580 const char *cmdname;
1581 enum { c_ifdef, c_ifndef, c_ifeq, c_ifneq, c_else, c_endif } cmdtype;
1585 /* Compare a word, both length and contents. */
1586 #define word1eq(s) (len == CSTRLEN (s) && strneq (s, line, CSTRLEN (s)))
1587 #define chkword(s, t) if (word1eq (s)) { cmdtype = (t); cmdname = (s); }
1589 /* Make sure this line is a conditional. */
1590 chkword ("ifdef", c_ifdef)
1591 else chkword ("ifndef", c_ifndef)
1592 else chkword ("ifeq", c_ifeq)
1593 else chkword ("ifneq", c_ifneq)
1594 else chkword ("else", c_else)
1595 else chkword ("endif", c_endif)
1599 /* Found one: skip past it and any whitespace after it. */
1603 #define EXTRATEXT() OS (error, flocp, _("extraneous text after '%s' directive"), cmdname)
1604 #define EXTRACMD() OS (fatal, flocp, _("extraneous '%s'"), cmdname)
1606 /* An 'endif' cannot contain extra text, and reduces the if-depth by 1 */
1607 if (cmdtype == c_endif)
1612 if (!conditionals->if_cmds)
1615 --conditionals->if_cmds;
1620 /* An 'else' statement can either be simple, or it can have another
1621 conditional after it. */
1622 if (cmdtype == c_else)
1626 if (!conditionals->if_cmds)
1629 o = conditionals->if_cmds - 1;
1631 if (conditionals->seen_else[o])
1632 O (fatal, flocp, _("only one 'else' per conditional"));
1634 /* Change the state of ignorance. */
1635 switch (conditionals->ignoring[o])
1638 /* We've just been interpreting. Never do it again. */
1639 conditionals->ignoring[o] = 2;
1642 /* We've never interpreted yet. Maybe this time! */
1643 conditionals->ignoring[o] = 0;
1647 /* It's a simple 'else'. */
1650 conditionals->seen_else[o] = 1;
1654 /* The 'else' has extra text. That text must be another conditional
1655 and cannot be an 'else' or 'endif'. */
1657 /* Find the length of the next word. */
1658 for (p = line+1; ! STOP_SET (*p, MAP_SPACE|MAP_NUL); ++p)
1662 /* If it's 'else' or 'endif' or an illegal conditional, fail. */
1663 if (word1eq ("else") || word1eq ("endif")
1664 || conditional_line (line, len, flocp) < 0)
1668 /* conditional_line() created a new level of conditional.
1669 Raise it back to this level. */
1670 if (conditionals->ignoring[o] < 2)
1671 conditionals->ignoring[o] = conditionals->ignoring[o+1];
1672 --conditionals->if_cmds;
1678 if (conditionals->allocated == 0)
1680 conditionals->allocated = 5;
1681 conditionals->ignoring = xmalloc (conditionals->allocated);
1682 conditionals->seen_else = xmalloc (conditionals->allocated);
1685 o = conditionals->if_cmds++;
1686 if (conditionals->if_cmds > conditionals->allocated)
1688 conditionals->allocated += 5;
1689 conditionals->ignoring = xrealloc (conditionals->ignoring,
1690 conditionals->allocated);
1691 conditionals->seen_else = xrealloc (conditionals->seen_else,
1692 conditionals->allocated);
1695 /* Record that we have seen an 'if...' but no 'else' so far. */
1696 conditionals->seen_else[o] = 0;
1698 /* Search through the stack to see if we're already ignoring. */
1699 for (i = 0; i < o; ++i)
1700 if (conditionals->ignoring[i])
1702 /* We are already ignoring, so just push a level to match the next
1703 "else" or "endif", and keep ignoring. We don't want to expand
1704 variables in the condition. */
1705 conditionals->ignoring[o] = 1;
1709 if (cmdtype == c_ifdef || cmdtype == c_ifndef)
1716 /* Expand the thing we're looking up, so we can use indirect and
1717 constructed variable names. */
1718 var = allocated_variable_expand (line);
1720 /* Make sure there's only one variable name to test. */
1721 p = end_of_token (var);
1728 v = lookup_variable (var, l);
1730 conditionals->ignoring[o] =
1731 ((v != 0 && *v->value != '\0') == (cmdtype == c_ifndef));
1737 /* "ifeq" or "ifneq". */
1740 char termin = *line == '(' ? ',' : *line;
1742 if (termin != ',' && termin != '"' && termin != '\'')
1746 /* Find the end of the first string. */
1750 for (; *line != '\0'; ++line)
1753 else if (*line == ')')
1755 else if (*line == ',' && count <= 0)
1759 while (*line != '\0' && *line != termin)
1767 /* Strip blanks after the first string. */
1769 while (ISBLANK (p[-1]))
1776 s2 = variable_expand (s1);
1777 /* We must allocate a new copy of the expanded string because
1778 variable_expand re-uses the same buffer. */
1780 s1 = alloca (l + 1);
1781 memcpy (s1, s2, l + 1);
1784 /* Find the start of the second string. */
1787 termin = termin == ',' ? ')' : *line;
1788 if (termin != ')' && termin != '"' && termin != '\'')
1791 /* Find the end of the second string. */
1795 s2 = next_token (line);
1796 for (line = s2; *line != '\0'; ++line)
1800 else if (*line == ')')
1813 while (*line != '\0' && *line != termin)
1825 s2 = variable_expand (s2);
1826 conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
1830 /* Search through the stack to see if we're ignoring. */
1831 for (i = 0; i < conditionals->if_cmds; ++i)
1832 if (conditionals->ignoring[i])
1838 /* Record target-specific variable values for files FILENAMES.
1839 TWO_COLON is nonzero if a double colon was used.
1841 The links of FILENAMES are freed, and so are any names in it
1842 that are not incorporated into other data structures.
1844 If the target is a pattern, add the variable to the pattern-specific
1845 variable value list. */
1848 record_target_var (struct nameseq *filenames, char *defn,
1849 enum variable_origin origin, struct vmodifiers *vmod,
1852 struct nameseq *nextf;
1853 struct variable_set_list *global;
1855 global = current_variable_set_list;
1857 /* If the variable is an append version, store that but treat it as a
1858 normal recursive variable. */
1860 for (; filenames != 0; filenames = nextf)
1863 const char *name = filenames->name;
1864 const char *percent;
1865 struct pattern_var *p;
1867 nextf = filenames->next;
1868 free_ns (filenames);
1870 /* If it's a pattern target, then add it to the pattern-specific
1872 percent = find_percent_cached (&name);
1875 /* Get a reference for this pattern-specific variable struct. */
1876 p = create_pattern_var (name, percent);
1877 p->variable.fileinfo = *flocp;
1878 /* I don't think this can fail since we already determined it was a
1879 variable definition. */
1880 v = assign_variable_definition (&p->variable, defn);
1884 if (v->flavor == f_simple)
1885 v->value = allocated_variable_expand (v->value);
1887 v->value = xstrdup (v->value);
1893 /* Get a file reference for this file, and initialize it.
1894 We don't want to just call enter_file() because that allocates a
1895 new entry if the file is a double-colon, which we don't want in
1897 f = lookup_file (name);
1899 f = enter_file (strcache_add (name));
1900 else if (f->double_colon)
1901 f = f->double_colon;
1903 initialize_file_variables (f, 1);
1905 current_variable_set_list = f->variables;
1906 v = try_variable_definition (flocp, defn, origin, 1);
1908 O (fatal, flocp, _("Malformed target-specific variable definition"));
1909 current_variable_set_list = global;
1912 /* Set up the variable to be *-specific. */
1914 v->private_var = vmod->private_v;
1915 v->export = vmod->export_v ? v_export : v_default;
1917 /* If it's not an override, check to see if there was a command-line
1918 setting. If so, reset the value. */
1919 if (v->origin != o_override)
1921 struct variable *gv;
1922 size_t len = strlen (v->name);
1924 gv = lookup_variable (v->name, len);
1926 && (gv->origin == o_env_override || gv->origin == o_command))
1929 v->value = xstrdup (gv->value);
1930 v->origin = gv->origin;
1931 v->recursive = gv->recursive;
1938 /* Record a description line for files FILENAMES,
1939 with dependencies DEPS, commands to execute described
1940 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1941 TWO_COLON is nonzero if a double colon was used.
1942 If not nil, PATTERN is the '%' pattern to make this
1943 a static pattern rule, and PATTERN_PERCENT is a pointer
1944 to the '%' within it.
1946 The links of FILENAMES are freed, and so are any names in it
1947 that are not incorporated into other data structures. */
1950 record_files (struct nameseq *filenames, int are_also_makes,
1951 const char *pattern,
1952 const char *pattern_percent, char *depstr,
1953 unsigned int cmds_started, char *commands,
1954 size_t commands_idx, int two_colon,
1955 char prefix, const floc *flocp)
1957 struct commands *cmds;
1959 struct dep *also_make = NULL;
1960 const char *implicit_percent;
1963 /* If we've already snapped deps, that means we're in an eval being
1964 resolved after the makefiles have been read in. We can't add more rules
1965 at this time, since they won't get snapped and we'll get core dumps.
1966 See Savannah bug # 12124. */
1968 O (fatal, flocp, _("prerequisites cannot be defined in recipes"));
1970 /* Determine if this is a pattern rule or not. */
1971 name = filenames->name;
1972 implicit_percent = find_percent_cached (&name);
1974 /* If there's a recipe, set up a struct for it. */
1975 if (commands_idx > 0)
1977 cmds = xmalloc (sizeof (struct commands));
1978 cmds->fileinfo.filenm = flocp->filenm;
1979 cmds->fileinfo.lineno = cmds_started;
1980 cmds->fileinfo.offset = 0;
1981 cmds->commands = xstrndup (commands, commands_idx);
1982 cmds->command_lines = 0;
1983 cmds->recipe_prefix = prefix;
1985 else if (are_also_makes)
1986 O (fatal, flocp, _("grouped targets must provide a recipe"));
1990 /* If there's a prereq string then parse it--unless it's eligible for 2nd
1991 expansion: if so, snap_deps() will do it. */
1996 depstr = unescape_char (depstr, ':');
1997 if (second_expansion && strchr (depstr, '$'))
1999 deps = alloc_dep ();
2000 deps->name = depstr;
2001 deps->need_2nd_expansion = 1;
2002 deps->staticpattern = pattern != 0;
2006 deps = split_prereqs (depstr);
2009 /* We'll enter static pattern prereqs later when we have the stem.
2010 We don't want to enter pattern rules at all so that we don't
2011 think that they ought to exist (make manual "Implicit Rule Search
2012 Algorithm", item 5c). */
2013 if (! pattern && ! implicit_percent)
2014 deps = enter_prereqs (deps, NULL);
2018 /* For implicit rules, _all_ the targets must have a pattern. That means we
2019 can test the first one to see if we're working with an implicit rule; if
2020 so we handle it specially. */
2022 if (implicit_percent)
2024 struct nameseq *nextf;
2025 const char **targets, **target_pats;
2029 O (fatal, flocp, _("mixed implicit and static pattern rules"));
2031 /* Count the targets to create an array of target names.
2032 We already have the first one. */
2033 nextf = filenames->next;
2034 free_ns (filenames);
2037 for (c = 1; nextf; ++c, nextf = nextf->next)
2039 targets = xmalloc (c * sizeof (const char *));
2040 target_pats = xmalloc (c * sizeof (const char *));
2043 target_pats[0] = implicit_percent;
2048 name = filenames->name;
2049 implicit_percent = find_percent_cached (&name);
2051 if (implicit_percent == 0)
2052 O (fatal, flocp, _("mixed implicit and normal rules"));
2055 target_pats[c] = implicit_percent;
2058 nextf = filenames->next;
2059 free_ns (filenames);
2063 create_pattern_rule (targets, target_pats, c, two_colon, deps, cmds, 1);
2069 /* Walk through each target and create it in the database.
2070 We already set up the first target, above. */
2073 struct nameseq *nextf = filenames->next;
2075 struct dep *this = 0;
2077 free_ns (filenames);
2079 /* Check for special targets. Do it here instead of, say, snap_deps()
2080 so that we can immediately use the value. */
2081 if (!posix_pedantic && streq (name, ".POSIX"))
2084 define_variable_cname (".SHELLFLAGS", "-ec", o_default, 0);
2085 /* These default values are based on IEEE Std 1003.1-2008.
2086 It requires '-O 1' for [CF]FLAGS, but GCC doesn't allow space
2087 between -O and the number so omit it here. */
2088 define_variable_cname ("ARFLAGS", "-rv", o_default, 0);
2089 define_variable_cname ("CC", "c99", o_default, 0);
2090 define_variable_cname ("CFLAGS", "-O1", o_default, 0);
2091 define_variable_cname ("FC", "fort77", o_default, 0);
2092 define_variable_cname ("FFLAGS", "-O1", o_default, 0);
2093 define_variable_cname ("SCCSGETFLAGS", "-s", o_default, 0);
2095 else if (!second_expansion && streq (name, ".SECONDEXPANSION"))
2096 second_expansion = 1;
2097 #if !defined (__MSDOS__) && !defined (__EMX__)
2098 else if (!one_shell && streq (name, ".ONESHELL"))
2102 /* If this is a static pattern rule:
2103 'targets: target%pattern: prereq%pattern; recipe',
2104 make sure the pattern matches this target name. */
2105 if (pattern && !pattern_matches (pattern, pattern_percent, name))
2107 _("target '%s' doesn't match the target pattern"), name);
2109 /* If there are multiple targets, copy the chain DEPS for all but the
2110 last one. It is not safe for the same deps to go in more than one
2111 place in the database. */
2112 this = nextf != 0 ? copy_dep_chain (deps) : deps;
2114 /* Find or create an entry in the file database for this target. */
2117 /* Single-colon. Combine this rule with the file's existing record,
2119 f = enter_file (strcache_add (name));
2120 if (f->double_colon)
2122 _("target file '%s' has both : and :: entries"), f->name);
2124 /* If CMDS == F->CMDS, this target was listed in this rule
2125 more than once. Just give a warning since this is harmless. */
2126 if (cmds != 0 && cmds == f->cmds)
2128 _("target '%s' given more than once in the same rule"),
2131 /* Check for two single-colon entries both with commands.
2132 Check is_target so that we don't lose on files such as .c.o
2133 whose commands were preinitialized. */
2134 else if (cmds != 0 && f->cmds != 0 && f->is_target)
2136 size_t l = strlen (f->name);
2137 error (&cmds->fileinfo, l,
2138 _("warning: overriding recipe for target '%s'"),
2140 error (&f->cmds->fileinfo, l,
2141 _("warning: ignoring old recipe for target '%s'"),
2145 /* Defining .DEFAULT with no deps or cmds clears it. */
2146 if (f == default_file && this == 0 && cmds == 0)
2151 /* Defining .SUFFIXES with no dependencies clears out the list of
2153 if (f == suffix_file && this == 0)
2155 free_dep_chain (f->deps);
2161 /* Double-colon. Make a new record even if there already is one. */
2162 f = lookup_file (name);
2164 /* Check for both : and :: rules. Check is_target so we don't lose
2165 on default suffix rules or makefiles. */
2166 if (f != 0 && f->is_target && !f->double_colon)
2168 _("target file '%s' has both : and :: entries"), f->name);
2170 f = enter_file (strcache_add (name));
2171 /* If there was an existing entry and it was a double-colon entry,
2172 enter_file will have returned a new one, making it the prev
2173 pointer of the old one, and setting its double_colon pointer to
2175 if (f->double_colon == 0)
2176 /* This is the first entry for this name, so we must set its
2177 double_colon pointer to itself. */
2178 f->double_colon = f;
2185 struct dep *also = alloc_dep();
2186 also->name = f->name;
2188 also->next = also_make;
2194 /* If this is a static pattern rule, set the stem to the part of its
2195 name that matched the '%' in the pattern, so you can use $* in the
2196 commands. If we didn't do it before, enter the prereqs now. */
2199 static const char *percent = "%";
2200 char *buffer = variable_expand ("");
2201 char *o = patsubst_expand_pat (buffer, name, pattern, percent,
2202 pattern_percent+1, percent+1);
2203 f->stem = strcache_add_len (buffer, o - buffer);
2206 if (! this->need_2nd_expansion)
2207 this = enter_prereqs (this, f->stem);
2209 this->stem = f->stem;
2213 /* Add the dependencies to this file entry. */
2216 /* Add the file's old deps and the new ones in THIS together. */
2221 struct dep *d = this;
2223 /* If this rule has commands, put these deps first. */
2224 while (d->next != 0)
2232 struct dep *d = f->deps;
2234 /* A rule without commands: put its prereqs at the end. */
2235 while (d->next != 0)
2244 /* All done! Set up for the next one. */
2250 /* Reduce escaped percents. If there are any unescaped it's an error */
2251 name = filenames->name;
2252 if (find_percent_cached (&name))
2254 _("*** mixed implicit and normal rules: deprecated syntax"));
2257 /* If there are also-makes, then populate a copy of the also-make list into
2258 each one. For the last file, we take our original also_make list instead
2259 wastefully copying it one more time and freeing it. */
2263 for (i = also_make; i != NULL; i = i->next)
2265 struct file *f = i->file;
2266 struct dep *cpy = i->next ? copy_dep_chain (also_make) : also_make;
2270 OS (error, &cmds->fileinfo,
2271 _("warning: overriding group membership for target '%s'"),
2273 free_dep_chain (f->also_make);
2281 /* Search STRING for an unquoted STOPMAP.
2282 Backslashes quote elements from STOPMAP and backslash.
2283 Quoting backslashes are removed from STRING by compacting it into itself.
2284 Returns a pointer to the first unquoted STOPCHAR if there is one, or nil if
2287 If MAP_VARIABLE is set, then the complete contents of variable references
2288 are skipped, even if the contain STOPMAP characters. */
2291 find_map_unquote (char *string, int stopmap)
2293 size_t string_len = 0;
2296 /* Always stop on NUL. */
2301 while (! STOP_SET (*p, stopmap))
2307 /* If we stopped due to a variable reference, skip over its contents. */
2310 char openparen = p[1];
2312 /* Check if '$' is the last character in the string. */
2313 if (openparen == '\0')
2318 /* Skip the contents of a non-quoted, multi-char variable ref. */
2319 if (openparen == '(' || openparen == '{')
2321 unsigned int pcount = 1;
2322 char closeparen = (openparen == '(' ? ')' : '}');
2326 if (*p == openparen)
2328 else if (*p == closeparen)
2338 /* Skipped the variable reference: look for STOPCHARS again. */
2342 if (p > string && p[-1] == '\\')
2344 /* Search for more backslashes. */
2346 while (&p[i] >= string && p[i] == '\\')
2349 /* Only compute the length if really needed. */
2350 if (string_len == 0)
2351 string_len = strlen (string);
2352 /* The number of backslashes is now -I.
2353 Copy P over itself to swallow half of them. */
2354 memmove (&p[i], &p[i/2], (string_len - (p - string)) - (i/2) + 1);
2357 /* All the backslashes quoted each other; the STOPCHAR was
2361 /* The STOPCHAR was quoted by a backslash. Look for another. */
2364 /* No backslash in sight. */
2368 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2373 find_char_unquote (char *string, int stop)
2375 size_t string_len = 0;
2380 p = strchr(p, stop);
2385 if (p > string && p[-1] == '\\')
2387 /* Search for more backslashes. */
2389 while (&p[i] >= string && p[i] == '\\')
2392 /* Only compute the length if really needed. */
2393 if (string_len == 0)
2394 string_len = strlen (string);
2395 /* The number of backslashes is now -I.
2396 Copy P over itself to swallow half of them. */
2397 memmove (&p[i], &p[i/2], (string_len - (p - string)) - (i/2) + 1);
2400 /* All the backslashes quoted each other; the STOPCHAR was
2404 /* The STOPCHAR was quoted by a backslash. Look for another. */
2407 /* No backslash in sight. */
2412 /* Unescape a character in a string. The string is compressed onto itself. */
2415 unescape_char (char *string, int c)
2427 /* We found a backslash. See if it's escaping our character. */
2432 if (*e != c || l%2 == 0)
2434 /* It's not; just take it all without unescaping. */
2438 /* If we hit the end of the string, we're done. */
2444 /* It is, and there's >1 backslash. Take half of them. */
2460 /* Search PATTERN for an unquoted % and handle quoting. */
2463 find_percent (char *pattern)
2465 return find_char_unquote (pattern, '%');
2468 /* Search STRING for an unquoted % and handle quoting. Returns a pointer to
2469 the % or NULL if no % was found.
2470 This version is used with strings in the string cache: if there's a need to
2471 modify the string a new version will be added to the string cache and
2472 *STRING will be set to that. */
2475 find_percent_cached (const char **string)
2477 const char *p = *string;
2481 /* If the first char is a % return now. This lets us avoid extra tests
2493 /* See if this % is escaped with a backslash; if not we're done. */
2498 /* Search for more backslashes. */
2502 while (&p[i] >= *string && p[i] == '\\')
2506 /* At this point we know we'll need to allocate a new string.
2507 Make a copy if we haven't yet done so. */
2510 slen = strlen (*string);
2511 new = alloca (slen + 1);
2512 memcpy (new, *string, slen + 1);
2513 p = new + (p - *string);
2517 /* At this point *string, p, and new all point into the same string.
2518 Get a non-const version of p so we can modify new. */
2519 pv = new + (p - *string);
2521 /* The number of backslashes is now -I.
2522 Copy P over itself to swallow half of them. */
2523 memmove (&pv[i], &pv[i/2], (slen - (pv - new)) - (i/2) + 1);
2526 /* If the backslashes quoted each other; the % was unquoted. */
2532 /* If we had to change STRING, add it to the strcache. */
2535 *string = strcache_add (*string);
2537 p = *string + (p - new);
2540 /* If we didn't find a %, return NULL. Otherwise return a ptr to it. */
2544 /* Find the next line of text in an eval buffer, combining continuation lines
2546 Return the number of actual lines read (> 1 if continuation lines).
2547 Returns -1 if there's nothing left in the buffer.
2549 After this function, ebuf->buffer points to the first character of the
2553 /* Read a line of text from a STRING.
2554 Since we aren't really reading from a file, don't bother with linenumbers.
2558 readstring (struct ebuffer *ebuf)
2562 /* If there is nothing left in this buffer, return 0. */
2563 if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
2566 /* Set up a new starting point for the buffer, and find the end of the
2567 next logical line (taking into account backslash/newline pairs). */
2569 eol = ebuf->buffer = ebuf->bufnext;
2574 const char *bol = eol;
2577 /* Find the next newline. At EOS, stop. */
2578 p = eol = strchr (eol , '\n');
2581 ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
2585 /* Found a newline; if it's escaped continue; else we're done. */
2586 while (p > bol && *(--p) == '\\')
2587 backslash = !backslash;
2593 /* Overwrite the newline char. */
2595 ebuf->bufnext = eol+1;
2601 readline (struct ebuffer *ebuf)
2608 /* The behaviors between string and stream buffers are different enough to
2609 warrant different functions. Do the Right Thing. */
2612 return readstring (ebuf);
2614 /* When reading from a file, we always start over at the beginning of the
2615 buffer for each new line. */
2617 p = start = ebuf->bufstart;
2618 end = p + ebuf->size;
2621 while (fgets (p, (int) (end - p), ebuf->fp) != 0)
2630 /* This only happens when the first thing on the line is a '\0'.
2631 It is a pretty hopeless case, but (wonder of wonders) Athena
2632 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2633 There is nothing really to be done; we synthesize a newline so
2634 the following line doesn't appear to be part of this line. */
2635 O (error, &ebuf->floc,
2636 _("warning: NUL character seen; rest of line ignored"));
2641 /* Jump past the text we just read. */
2644 /* If the last char isn't a newline, the whole line didn't fit into the
2645 buffer. Get some more buffer and try again. */
2649 /* We got a newline, so add one to the count of lines. */
2652 #if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
2653 /* Check to see if the line was really ended with CRLF; if so ignore
2655 if ((p - start) > 1 && p[-2] == '\r')
2658 memmove (p-1, p, strlen (p) + 1);
2663 for (p2 = p - 2; p2 >= start; --p2)
2667 backslash = !backslash;
2676 /* It was a backslash/newline combo. If we have more space, read
2681 /* We need more space at the end of our buffer, so realloc it.
2682 Make sure to preserve the current offset of p. */
2685 size_t off = p - start;
2687 start = ebuf->buffer = ebuf->bufstart = xrealloc (start, ebuf->size);
2689 end = start + ebuf->size;
2694 if (ferror (ebuf->fp))
2695 pfatal_with_name (ebuf->floc.filenm);
2697 /* If we found some lines, return how many.
2698 If we didn't, but we did find _something_, that indicates we read the last
2699 line of a file with no final newline; return 1.
2700 If we read nothing, we're at EOF; return -1. */
2702 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
2705 /* Parse the next "makefile word" from the input buffer, and return info
2708 A "makefile word" is one of:
2710 w_bogus Should never happen
2712 w_static A static word; cannot be expanded
2713 w_variable A word containing one or more variables/functions
2715 w_dcolon A double-colon
2716 w_ampcolon An ampersand-colon (&:) token
2717 w_ampdcolon An ampersand-double-colon (&::) token
2718 w_semicolon A semicolon
2719 w_varassign A variable assignment operator (=, :=, ::=, +=, ?=, or !=)
2721 Note that this function is only used when reading certain parts of the
2722 makefile. Don't use it where special rules hold sway (RHS of a variable,
2723 in a command list, etc.) */
2725 static enum make_word_type
2726 get_next_mword (char *buffer, char **startp, size_t *length)
2728 enum make_word_type wtype;
2729 char *p = buffer, *beg;
2732 /* Skip any leading whitespace. */
2733 while (ISBLANK (*p))
2739 /* Look at the start of the word to see if it's simple. */
2747 wtype = w_semicolon;
2751 wtype = w_varassign;
2758 wtype = w_varassign; /* := */
2766 wtype = w_varassign; /* ::= */
2780 wtype = w_ampcolon; /* &: */
2784 wtype = w_ampdcolon; /* &:: */
2796 wtype = w_varassign; /* += or ?= or != */
2805 /* This is some non-operator word. A word consists of the longest
2806 string of characters that doesn't contain whitespace, one of [:=#],
2807 or [?+!]=, or &:. */
2809 /* We start out assuming a static word; if we see a variable we'll
2810 adjust our assumptions then. */
2813 /* We already found the first value of "c", above. */
2828 #ifdef HAVE_DOS_PATHS
2829 /* A word CAN include a colon in its drive spec. The drive
2830 spec is allowed either at the beginning of a word, or as part
2831 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2832 if ((p - beg == 2 || (p - beg > 2 && p[-3] == '('))
2833 && isalpha ((unsigned char)p[-2]))
2845 /* This is a variable reference, so note that it's expandable.
2846 Then read it to the matching close paren. */
2854 /* This is a single-letter variable reference. */
2857 for (count=0; *p != '\0'; ++p)
2861 else if (*p == closeparen && --count < 0)
2909 /* Construct the list of include directories
2910 from the arguments and the default list. */
2913 construct_include_path (const char **arg_dirs)
2915 #ifdef VAXC /* just don't ask ... */
2924 /* Compute the number of pointers we need in the table. */
2925 idx = sizeof (default_include_directories) / sizeof (const char *);
2927 for (cpp = arg_dirs; *cpp != 0; ++cpp)
2931 /* Add one for $DJDIR. */
2935 dirs = xmalloc (idx * sizeof (const char *));
2940 /* First consider any dirs specified with -I switches.
2941 Ignore any that don't exist. Remember the maximum string length. */
2944 while (*arg_dirs != 0)
2946 const char *dir = *(arg_dirs++);
2952 expanded = tilde_expand (dir);
2957 EINTRLOOP (e, stat (dir, &stbuf));
2958 if (e == 0 && S_ISDIR (stbuf.st_mode))
2960 size_t len = strlen (dir);
2961 /* If dir name is written with trailing slashes, discard them. */
2962 while (len > 1 && dir[len - 1] == '/')
2964 if (len > max_incl_len)
2966 dirs[idx++] = strcache_add_len (dir, len);
2972 /* Now add the standard default dirs at the end. */
2976 /* The environment variable $DJDIR holds the root of the DJGPP directory
2977 tree; add ${DJDIR}/include. */
2978 struct variable *djdir = lookup_variable ("DJDIR", 5);
2982 size_t len = strlen (djdir->value) + 8;
2983 char *defdir = alloca (len + 1);
2985 strcat (strcpy (defdir, djdir->value), "/include");
2986 dirs[idx++] = strcache_add (defdir);
2988 if (len > max_incl_len)
2994 for (cpp = default_include_directories; *cpp != 0; ++cpp)
2998 EINTRLOOP (e, stat (*cpp, &stbuf));
2999 if (e == 0 && S_ISDIR (stbuf.st_mode))
3001 size_t len = strlen (*cpp);
3002 /* If dir name is written with trailing slashes, discard them. */
3003 while (len > 1 && (*cpp)[len - 1] == '/')
3005 if (len > max_incl_len)
3007 dirs[idx++] = strcache_add_len (*cpp, len);
3013 /* Now add each dir to the .INCLUDE_DIRS variable. */
3015 for (cpp = dirs; *cpp != 0; ++cpp)
3016 do_variable_definition (NILF, ".INCLUDE_DIRS", *cpp,
3017 o_default, f_append, 0);
3019 include_directories = dirs;
3022 /* Expand ~ or ~USER at the beginning of NAME.
3023 Return a newly malloc'd string or 0. */
3026 tilde_expand (const char *name)
3029 if (name[1] == '/' || name[1] == '\0')
3035 /* Turn off --warn-undefined-variables while we expand HOME. */
3036 int save = warn_undefined_variables_flag;
3037 warn_undefined_variables_flag = 0;
3039 home_dir = allocated_variable_expand ("$(HOME)");
3041 warn_undefined_variables_flag = save;
3044 is_variable = home_dir[0] != '\0';
3048 home_dir = getenv ("HOME");
3050 # if !defined(_AMIGA) && !defined(WINDOWS32)
3051 if (home_dir == 0 || home_dir[0] == '\0')
3053 char *logname = getlogin ();
3057 struct passwd *p = getpwnam (logname);
3059 home_dir = p->pw_dir;
3062 # endif /* !AMIGA && !WINDOWS32 */
3065 char *new = xstrdup (concat (2, home_dir, name + 1));
3071 # if !defined(_AMIGA) && !defined(WINDOWS32)
3074 struct passwd *pwent;
3075 char *userend = strchr (name + 1, '/');
3078 pwent = getpwnam (name + 1);
3082 return xstrdup (pwent->pw_dir);
3084 return xstrdup (concat (3, pwent->pw_dir, "/", userend + 1));
3086 else if (userend != 0)
3089 # endif /* !AMIGA && !WINDOWS32 */
3094 /* Parse a string into a sequence of filenames represented as a chain of
3095 struct nameseq's and return that chain. Optionally expand the strings via
3098 The string is passed as STRINGP, the address of a string pointer.
3099 The string pointer is updated to point at the first character
3100 not parsed, which either is a null char or equals STOPMAP.
3102 SIZE is how large (in bytes) each element in the new chain should be.
3103 This is useful if we want them actually to be other structures
3104 that have room for additional info.
3106 STOPMAP is a map of characters that tell us to stop parsing.
3108 PREFIX, if non-null, is added to the beginning of each filename.
3110 FLAGS allows one or more of the following bitflags to be set:
3111 PARSEFS_NOSTRIP - Do no strip './'s off the beginning
3112 PARSEFS_NOAR - Do not check filenames for archive references
3113 PARSEFS_NOGLOB - Do not expand globbing characters
3114 PARSEFS_EXISTS - Only return globbed files that actually exist
3115 (cannot also set NOGLOB)
3116 PARSEFS_NOCACHE - Do not add filenames to the strcache (caller frees)
3120 parse_file_seq (char **stringp, size_t size, int stopmap,
3121 const char *prefix, int flags)
3123 /* tmp points to tmpbuf after the prefix, if any.
3124 tp is the end of the buffer. */
3125 static char *tmpbuf = NULL;
3127 int cachep = NONE_SET (flags, PARSEFS_NOCACHE);
3129 struct nameseq *new = 0;
3130 struct nameseq **newp = &new;
3131 #define NEWELT(_n) do { \
3132 const char *__n = (_n); \
3133 *newp = xcalloc (size); \
3134 (*newp)->name = (cachep ? strcache_add (__n) : xstrdup (__n)); \
3135 newp = &(*newp)->next; \
3141 int findmap = stopmap|MAP_VMSCOMMA|MAP_NUL;
3143 if (NONE_SET (flags, PARSEFS_ONEWORD))
3144 findmap |= MAP_BLANK;
3146 /* Always stop on NUL. */
3149 if (size < sizeof (struct nameseq))
3150 size = sizeof (struct nameseq);
3152 if (NONE_SET (flags, PARSEFS_NOGLOB))
3153 dir_setup_glob (&gl);
3155 /* Get enough temporary space to construct the largest possible target. */
3157 static size_t tmpbuf_len = 0;
3158 size_t l = strlen (*stringp) + 1;
3161 tmpbuf = xrealloc (tmpbuf, l);
3167 /* Parse STRING. P will always point to the end of the parsed content. */
3172 const char **nlist = 0;
3183 /* Skip whitespace; at the end of the string or STOPCHAR we're done. */
3185 if (STOP_SET (*p, stopmap))
3188 /* There are names left, so find the end of the next name.
3189 Throughout this iteration S points to the start. */
3191 p = find_map_unquote (p, findmap);
3194 /* convert comma separated list to space separated */
3199 /* If we stopped due to a device name, skip it. */
3200 if (p && p != s+1 && p[0] == ':')
3201 p = find_map_unquote (p+1, findmap);
3203 #ifdef HAVE_DOS_PATHS
3204 /* If we stopped due to a drive specifier, skip it.
3205 Tokens separated by spaces are treated as separate paths since make
3206 doesn't allow path names with spaces. */
3207 if (p && p == s+1 && p[0] == ':'
3208 && isalpha ((unsigned char)s[0]) && STOP_SET (p[1], MAP_DIRSEP))
3209 p = find_map_unquote (p+1, findmap);
3215 /* Strip leading "this directory" references. */
3216 if (NONE_SET (flags, PARSEFS_NOSTRIP))
3218 /* Skip leading '[]'s. should only be one set or bug somwhere else */
3219 if (p - s > 2 && s[0] == '[' && s[1] == ']')
3221 /* Skip leading '<>'s. should only be one set or bug somwhere else */
3222 if (p - s > 2 && s[0] == '<' && s[1] == '>')
3225 /* Skip leading './'s. */
3226 while (p - s > 2 && s[0] == '.' && s[1] == '/')
3228 /* Skip "./" and all following slashes. */
3234 /* Extract the filename just found, and skip it.
3235 Set NAME to the string, and NLEN to its length. */
3239 /* The name was stripped to empty ("./"). */
3241 /* PDS-- This cannot be right!! */
3254 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
3255 * to remove this '\' before we can use the filename.
3256 * xstrdup called because S may be read-only string constant.
3261 if (s[0] == '\\' && s[1] == ':')
3269 memcpy (tp, s, nlen);
3274 /* At this point, TP points to the element and NLEN is its length. */
3277 /* If this is the start of an archive group that isn't complete, set up
3278 to add the archive prefix for future files. A file list like:
3279 "libf.a(x.o y.o z.o)" needs to be expanded as:
3280 "libf.a(x.o) libf.a(y.o) libf.a(z.o)"
3282 TP == TMP means we're not already in an archive group. Ignore
3283 something starting with '(', as that cannot actually be an
3284 archive-member reference (and treating it as such results in an empty
3285 file name, which causes much lossage). Also if it ends in ")" then
3286 it's a complete reference so we don't need to treat it specially.
3288 Finally, note that archive groups must end with ')' as the last
3289 character, so ensure there's some word ending like that before
3290 considering this an archive group. */
3291 if (NONE_SET (flags, PARSEFS_NOAR)
3292 && tp == tmpbuf && tp[0] != '(' && tp[nlen-1] != ')')
3294 char *n = strchr (tp, '(');
3297 /* This looks like the first element in an open archive group.
3298 A valid group MUST have ')' as the last character. */
3304 /* Find the end of this word. We don't want to unquote and
3305 we don't care about quoting since we're looking for the
3306 last char in the word. */
3307 while (! STOP_SET (*e, findmap))
3309 /* If we didn't move, we're done now. */
3314 /* Found the end, so this is the first element in an
3315 open archive group. It looks like "lib(mem".
3316 Reset TP past the open paren. */
3317 nlen -= (n + 1) - tp;
3320 /* We can stop looking now. */
3326 /* If we have just "lib(", part of something like "lib( a b)",
3327 go to the next item. */
3333 /* If we are inside an archive group, make sure it has an end. */
3336 if (tp[nlen-1] == ')')
3338 /* This is the natural end; reset TP. */
3341 /* This is just ")", something like "lib(a b )": skip it. */
3347 /* Not the end, so add a "fake" end. */
3354 /* If we're not globbing we're done: add it to the end of the chain.
3355 Go to the next item in the string. */
3356 if (ANY_SET (flags, PARSEFS_NOGLOB))
3358 NEWELT (concat (2, prefix, tmpbuf));
3362 /* If we get here we know we're doing glob expansion.
3363 TP is a string in tmpbuf. NLEN is no longer used.
3364 We may need to do more work: after this NAME will be set. */
3367 /* Expand tilde if applicable. */
3368 if (tmpbuf[0] == '~')
3370 tildep = tilde_expand (tmpbuf);
3376 /* If NAME is an archive member reference replace it with the archive
3377 file name, and save the member name in MEMNAME. We will glob on the
3378 archive name and then reattach MEMNAME later. */
3379 if (NONE_SET (flags, PARSEFS_NOAR) && ar_name (name))
3381 ar_parse_name (name, &arname, &memname);
3384 #endif /* !NO_ARCHIVES */
3386 /* glob() is expensive: don't call it unless we need to. */
3387 if (NONE_SET (flags, PARSEFS_EXISTS) && strpbrk (name, "?*[") == NULL)
3394 switch (glob (name, GLOB_ALTDIRFUNC, NULL, &gl))
3402 nlist = (const char **)gl.gl_pathv;
3406 /* If we want only existing items, skip this one. */
3407 if (ANY_SET (flags, PARSEFS_EXISTS))
3415 /* By default keep this name. */
3421 /* For each matched element, add it to the list. */
3422 for (i = 0; i < tot; ++i)
3426 /* Try to glob on MEMNAME within the archive. */
3427 struct nameseq *found = ar_glob (nlist[i], memname, size);
3429 /* No matches. Use MEMNAME as-is. */
3430 NEWELT (concat (5, prefix, nlist[i], "(", memname, ")"));
3433 /* We got a chain of items. Attach them. */
3435 (*newp)->next = found;
3439 /* Find and set the new end. Massage names if necessary. */
3443 found->name = xstrdup (concat (2, prefix, name));
3445 found->name = strcache_add (concat (2, prefix, name));
3447 if (found->next == 0)
3450 found = found->next;
3452 newp = &found->next;
3456 #endif /* !NO_ARCHIVES */
3457 NEWELT (concat (2, prefix, nlist[i]));