1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
3 2002 Free Software Foundation, Inc.
4 This file is part of GNU Make.
6 GNU Make is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2, or (at your option)
11 GNU Make is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Make; see the file COPYING. If not, write to
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 Boston, MA 02111-1307, USA. */
42 struct passwd
*getpwnam
PARAMS ((char *name
));
45 #endif /* !WINDOWS32 */
47 /* A 'struct ebuffer' controls the origin of the makefile we are currently
53 char *buffer
; /* Start of the current line in the buffer. */
54 char *bufnext
; /* Start of the next line in the buffer. */
55 char *bufstart
; /* Start of the entire buffer. */
56 unsigned int size
; /* Malloc'd size of buffer. */
57 FILE *fp
; /* File, or NULL if this is an internal buffer. */
58 struct floc floc
; /* Info on the file in fp (if any). */
61 /* Types of "words" that can be read in a makefile. */
64 w_bogus
, w_eol
, w_static
, w_variable
, w_colon
, w_dcolon
, w_semicolon
,
69 /* A `struct conditionals' contains the information describing
70 all the active conditionals in a makefile.
72 The global variable `conditionals' contains the conditionals
73 information for the current makefile. It is initialized from
74 the static structure `toplevel_conditionals' and is later changed
75 to new structures for included makefiles. */
79 unsigned int if_cmds
; /* Depth of conditional nesting. */
80 unsigned int allocated
; /* Elts allocated in following arrays. */
81 char *ignoring
; /* Are we ignoring or interpreting?
82 0=interpreting, 1=not yet interpreted,
83 2=already interpreted */
84 char *seen_else
; /* Have we already seen an `else'? */
87 static struct conditionals toplevel_conditionals
;
88 static struct conditionals
*conditionals
= &toplevel_conditionals
;
91 /* Default directories to search for include files in */
93 static char *default_include_directories
[] =
95 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
97 * This completely up to the user when they install MSVC or other packages.
98 * This is defined as a placeholder.
100 #define INCLUDEDIR "."
105 "/usr/local/include",
111 /* List of directories to search for include files in */
113 static char **include_directories
;
115 /* Maximum length of an element of the above. */
117 static unsigned int max_incl_len
;
119 /* The filename and pointer to line number of the
120 makefile currently being read in. */
122 const struct floc
*reading_file
= 0;
124 /* The chain of makefiles read by read_makefile. */
126 static struct dep
*read_makefiles
= 0;
128 static int eval_makefile
PARAMS ((char *filename
, int flags
));
129 static int eval
PARAMS ((struct ebuffer
*buffer
, int flags
));
131 static long readline
PARAMS ((struct ebuffer
*ebuf
));
132 static void do_define
PARAMS ((char *name
, unsigned int namelen
,
133 enum variable_origin origin
,
134 struct ebuffer
*ebuf
));
135 static int conditional_line
PARAMS ((char *line
, int len
, const struct floc
*flocp
));
136 static void record_files
PARAMS ((struct nameseq
*filenames
, char *pattern
, char *pattern_percent
,
137 struct dep
*deps
, unsigned int cmds_started
, char *commands
,
138 unsigned int commands_idx
, int two_colon
,
139 const struct floc
*flocp
));
140 static void record_target_var
PARAMS ((struct nameseq
*filenames
, char *defn
,
141 enum variable_origin origin
,
143 const struct floc
*flocp
));
144 static enum make_word_type get_next_mword
PARAMS ((char *buffer
, char *delim
,
145 char **startp
, unsigned int *length
));
146 static void remove_comments
PARAMS ((char *line
));
147 static char *find_char_unquote
PARAMS ((char *string
, int stop1
,
148 int stop2
, int blank
, int ignorevars
));
150 /* Read in all the makefiles and return the chain of their names. */
153 read_all_makefiles (char **makefiles
)
155 unsigned int num_makefiles
= 0;
157 /* Create *_LIST variables, to hold the makefiles, targets, and variables
158 we will be reading. */
160 define_variable ("MAKEFILE_LIST", sizeof ("MAKEFILE_LIST")-1, "", o_file
, 0);
162 DB (DB_BASIC
, (_("Reading makefiles...\n")));
164 /* If there's a non-null variable MAKEFILES, its value is a list of
165 files to read first thing. But don't let it prevent reading the
166 default makefiles and don't let the default goal come from there. */
174 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
175 int save
= warn_undefined_variables_flag
;
176 warn_undefined_variables_flag
= 0;
178 value
= allocated_variable_expand ("$(MAKEFILES)");
180 warn_undefined_variables_flag
= save
;
183 /* Set NAME to the start of next token and LENGTH to its length.
184 MAKEFILES is updated for finding remaining tokens. */
187 while ((name
= find_next_token (&p
, &length
)) != 0)
191 name
= xstrdup (name
);
192 if (eval_makefile (name
,
193 RM_NO_DEFAULT_GOAL
|RM_INCLUDED
|RM_DONTCARE
) < 2)
200 /* Read makefiles specified with -f switches. */
203 while (*makefiles
!= 0)
205 struct dep
*tail
= read_makefiles
;
206 register struct dep
*d
;
208 if (! eval_makefile (*makefiles
, 0))
209 perror_with_name ("", *makefiles
);
211 /* Find the right element of read_makefiles. */
213 while (d
->next
!= tail
)
216 /* Use the storage read_makefile allocates. */
217 *makefiles
= dep_name (d
);
222 /* If there were no -f switches, try the default names. */
224 if (num_makefiles
== 0)
226 static char *default_makefiles
[] =
228 /* all lower case since readdir() (the vms version) 'lowercasifies' */
229 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
232 { "GNUmakefile", "Makefile", "SMakefile", 0 };
233 #else /* !Amiga && !VMS */
234 { "GNUmakefile", "makefile", "Makefile", 0 };
237 register char **p
= default_makefiles
;
238 while (*p
!= 0 && !file_exists_p (*p
))
243 if (! eval_makefile (*p
, 0))
244 perror_with_name ("", *p
);
248 /* No default makefile was found. Add the default makefiles to the
249 `read_makefiles' chain so they will be updated if possible. */
250 struct dep
*tail
= read_makefiles
;
251 /* Add them to the tail, after any MAKEFILES variable makefiles. */
252 while (tail
!= 0 && tail
->next
!= 0)
254 for (p
= default_makefiles
; *p
!= 0; ++p
)
256 struct dep
*d
= (struct dep
*) xmalloc (sizeof (struct dep
));
258 d
->file
= enter_file (*p
);
259 d
->file
->dontcare
= 1;
261 d
->need_2nd_expansion
= 0;
262 /* Tell update_goal_chain to bail out as soon as this file is
263 made, and main not to die if we can't make this file. */
264 d
->changed
= RM_DONTCARE
;
276 return read_makefiles
;
279 /* Install a new conditional and return the previous one. */
281 static struct conditionals
*
282 install_conditionals (struct conditionals
*new)
284 struct conditionals
*save
= conditionals
;
286 bzero ((char *) new, sizeof (*new));
292 /* Free the current conditionals and reinstate a saved one. */
295 restore_conditionals (struct conditionals
*saved
)
297 /* Free any space allocated by conditional_line. */
298 if (conditionals
->ignoring
)
299 free (conditionals
->ignoring
);
300 if (conditionals
->seen_else
)
301 free (conditionals
->seen_else
);
304 conditionals
= saved
;
308 eval_makefile (char *filename
, int flags
)
312 const struct floc
*curfile
;
316 ebuf
.floc
.filenm
= filename
;
317 ebuf
.floc
.lineno
= 1;
319 if (ISDB (DB_VERBOSE
))
321 printf (_("Reading makefile `%s'"), filename
);
322 if (flags
& RM_NO_DEFAULT_GOAL
)
323 printf (_(" (no default goal)"));
324 if (flags
& RM_INCLUDED
)
325 printf (_(" (search path)"));
326 if (flags
& RM_DONTCARE
)
327 printf (_(" (don't care)"));
328 if (flags
& RM_NO_TILDE
)
329 printf (_(" (no ~ expansion)"));
333 /* First, get a stream to read. */
335 /* Expand ~ in FILENAME unless it came from `include',
336 in which case it was already done. */
337 if (!(flags
& RM_NO_TILDE
) && filename
[0] == '~')
339 char *expanded
= tilde_expand (filename
);
344 ebuf
.fp
= fopen (filename
, "r");
345 /* Save the error code so we print the right message later. */
346 makefile_errno
= errno
;
348 /* If the makefile wasn't found and it's either a makefile from
349 the `MAKEFILES' variable or an included makefile,
350 search the included makefile search path for this makefile. */
351 if (ebuf
.fp
== 0 && (flags
& RM_INCLUDED
) && *filename
!= '/')
353 register unsigned int i
;
354 for (i
= 0; include_directories
[i
] != 0; ++i
)
356 char *name
= concat (include_directories
[i
], "/", filename
);
357 ebuf
.fp
= fopen (name
, "r");
368 /* Add FILENAME to the chain of read makefiles. */
369 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
370 deps
->next
= read_makefiles
;
371 read_makefiles
= deps
;
373 deps
->file
= lookup_file (filename
);
375 deps
->file
= enter_file (xstrdup (filename
));
376 if (filename
!= ebuf
.floc
.filenm
)
378 filename
= deps
->file
->name
;
379 deps
->changed
= flags
;
380 deps
->ignore_mtime
= 0;
381 deps
->need_2nd_expansion
= 0;
382 if (flags
& RM_DONTCARE
)
383 deps
->file
->dontcare
= 1;
385 /* If the makefile can't be found at all, give up entirely. */
389 /* If we did some searching, errno has the error from the last
390 attempt, rather from FILENAME itself. Restore it in case the
391 caller wants to use it in a message. */
392 errno
= makefile_errno
;
396 /* Add this makefile to the list. */
397 do_variable_definition (&ebuf
.floc
, "MAKEFILE_LIST", filename
, o_file
,
400 /* Evaluate the makefile */
403 ebuf
.buffer
= ebuf
.bufnext
= ebuf
.bufstart
= xmalloc (ebuf
.size
);
405 curfile
= reading_file
;
406 reading_file
= &ebuf
.floc
;
408 r
= eval (&ebuf
, !(flags
& RM_NO_DEFAULT_GOAL
));
410 reading_file
= curfile
;
414 free (ebuf
.bufstart
);
420 eval_buffer (char *buffer
)
423 struct conditionals
*saved
;
424 struct conditionals
new;
425 const struct floc
*curfile
;
428 /* Evaluate the buffer */
430 ebuf
.size
= strlen (buffer
);
431 ebuf
.buffer
= ebuf
.bufnext
= ebuf
.bufstart
= buffer
;
434 ebuf
.floc
= *reading_file
;
436 curfile
= reading_file
;
437 reading_file
= &ebuf
.floc
;
439 saved
= install_conditionals (&new);
443 restore_conditionals (saved
);
445 reading_file
= curfile
;
452 /* Read file FILENAME as a makefile and add its contents to the data base.
454 SET_DEFAULT is true if we are allowed to set the default goal. */
458 eval (struct ebuffer
*ebuf
, int set_default
)
461 unsigned int collapsed_length
= 0;
462 unsigned int commands_len
= 200;
464 unsigned int commands_idx
= 0;
465 unsigned int cmds_started
, tgts_started
;
466 int ignoring
= 0, in_ignored_define
= 0;
467 int no_targets
= 0; /* Set when reading a rule without targets. */
468 struct nameseq
*filenames
= 0;
469 struct dep
*deps
= 0;
472 char *pattern
= 0, *pattern_percent
;
476 #define record_waiting_files() \
479 if (filenames != 0) \
481 fi.lineno = tgts_started; \
482 record_files (filenames, pattern, pattern_percent, deps, \
483 cmds_started, commands, commands_idx, two_colon, \
489 if (pattern) { free(pattern); pattern = 0; } \
493 cmds_started
= tgts_started
= 1;
495 fstart
= &ebuf
->floc
;
496 fi
.filenm
= ebuf
->floc
.filenm
;
498 /* Loop over lines in the file.
499 The strategy is to accumulate target names in FILENAMES, dependencies
500 in DEPS and commands in COMMANDS. These are used to define a rule
501 when the start of the next rule (or eof) is encountered.
503 When you see a "continue" in the loop below, that means we are moving on
504 to the next line _without_ ending any rule that we happen to be working
505 with at the moment. If you see a "goto rule_complete", then the
506 statement we just parsed also finishes the previous rule. */
508 commands
= xmalloc (200);
512 unsigned int linelen
;
518 /* Grab the next line to be evaluated */
519 ebuf
->floc
.lineno
+= nlines
;
520 nlines
= readline (ebuf
);
522 /* If there is nothing left to eval, we're done. */
526 /* If this line is empty, skip it. */
531 linelen
= strlen (line
);
533 /* Check for a shell command line first.
534 If it is not one, we can stop treating tab specially. */
538 /* Ignore the commands in a rule with no targets. */
541 /* If there is no preceding rule line, don't treat this line
542 as a command, even though it begins with a tab character.
543 SunOS 4 make appears to behave this way. */
548 /* Yep, this is a shell command, and we don't care. */
551 /* Append this command line to the line being accumulated. */
552 if (commands_idx
== 0)
553 cmds_started
= ebuf
->floc
.lineno
;
555 if (linelen
+ 1 + commands_idx
> commands_len
)
557 commands_len
= (linelen
+ 1 + commands_idx
) * 2;
558 commands
= xrealloc (commands
, commands_len
);
560 bcopy (line
, &commands
[commands_idx
], linelen
);
561 commands_idx
+= linelen
;
562 commands
[commands_idx
++] = '\n';
568 /* This line is not a shell command line. Don't worry about tabs.
569 Get more space if we need it; we don't need to preserve the current
570 contents of the buffer. */
572 if (collapsed_length
< linelen
+1)
574 collapsed_length
= linelen
+1;
576 free ((char *)collapsed
);
577 collapsed
= (char *) xmalloc (collapsed_length
);
579 strcpy (collapsed
, line
);
580 /* Collapse continuation lines. */
581 collapse_continuations (collapsed
);
582 remove_comments (collapsed
);
584 /* Compare a word, both length and contents. */
585 #define word1eq(s) (len == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
587 while (isspace ((unsigned char)*p
))
591 /* This line is completely empty--ignore it. */
594 /* Find the end of the first token. Note we don't need to worry about
595 * ":" here since we compare tokens by length (so "export" will never
596 * be equal to "export:").
598 for (p2
= p
+1; *p2
!= '\0' && !isspace ((unsigned char)*p2
); ++p2
)
602 /* Find the start of the second token. If it looks like a target or
603 variable definition it can't be a preprocessor token so skip
604 them--this allows variables/targets named `ifdef', `export', etc. */
605 while (isspace ((unsigned char)*p2
))
608 if ((p2
[0] == ':' || p2
[0] == '+' || p2
[0] == '=') && p2
[1] == '\0')
610 /* It can't be a preprocessor token so skip it if we're ignoring */
614 goto skip_conditionals
;
617 /* We must first check for conditional and `define' directives before
618 ignoring anything, since they control what we will do with
621 if (!in_ignored_define
)
623 int i
= conditional_line (p
, len
, fstart
);
627 fatal (fstart
, _("invalid syntax in conditional"));
634 if (word1eq ("endef"))
636 if (!in_ignored_define
)
637 fatal (fstart
, _("extraneous `endef'"));
638 in_ignored_define
= 0;
642 if (word1eq ("define"))
645 in_ignored_define
= 1;
649 fatal (fstart
, _("empty variable name"));
651 /* Let the variable name be the whole rest of the line,
652 with trailing blanks stripped (comments have already been
653 removed), so it could be a complex variable/function
654 reference that might contain blanks. */
655 p
= strchr (p2
, '\0');
656 while (isblank ((unsigned char)p
[-1]))
658 do_define (p2
, p
- p2
, o_file
, ebuf
);
663 if (word1eq ("override"))
666 error (fstart
, _("empty `override' directive"));
668 if (strneq (p2
, "define", 6)
669 && (isblank ((unsigned char)p2
[6]) || p2
[6] == '\0'))
672 in_ignored_define
= 1;
675 p2
= next_token (p2
+ 6);
677 fatal (fstart
, _("empty variable name"));
679 /* Let the variable name be the whole rest of the line,
680 with trailing blanks stripped (comments have already been
681 removed), so it could be a complex variable/function
682 reference that might contain blanks. */
683 p
= strchr (p2
, '\0');
684 while (isblank ((unsigned char)p
[-1]))
686 do_define (p2
, p
- p2
, o_override
, ebuf
);
690 && !try_variable_definition (fstart
, p2
, o_override
, 0))
691 error (fstart
, _("invalid `override' directive"));
697 /* Ignore the line. We continue here so conditionals
698 can appear in the middle of a rule. */
701 if (word1eq ("export"))
703 /* 'export' by itself causes everything to be exported. */
705 export_all_variables
= 1;
710 v
= try_variable_definition (fstart
, p2
, o_file
, 0);
712 v
->export
= v_export
;
718 /* Expand the line so we can use indirect and constructed
719 variable names in an export command. */
720 p2
= ap
= allocated_variable_expand (p2
);
722 for (p
= find_next_token (&p2
, &len
); p
!= 0;
723 p
= find_next_token (&p2
, &len
))
725 v
= lookup_variable (p
, len
);
727 v
= define_variable_loc (p
, len
, "", o_file
, 0,
729 v
->export
= v_export
;
738 if (word1eq ("unexport"))
741 export_all_variables
= 0;
748 /* Expand the line so we can use indirect and constructed
749 variable names in an unexport command. */
750 p2
= ap
= allocated_variable_expand (p2
);
752 for (p
= find_next_token (&p2
, &len
); p
!= 0;
753 p
= find_next_token (&p2
, &len
))
755 v
= lookup_variable (p
, len
);
757 v
= define_variable_loc (p
, len
, "", o_file
, 0, fstart
);
759 v
->export
= v_noexport
;
768 if (word1eq ("vpath"))
772 p2
= variable_expand (p2
);
773 p
= find_next_token (&p2
, &len
);
776 pattern
= savestring (p
, len
);
777 p
= find_next_token (&p2
, &len
);
778 /* No searchpath means remove all previous
779 selective VPATH's with the same pattern. */
782 /* No pattern means remove all previous selective VPATH's. */
784 construct_vpath_list (pattern
, p
);
791 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
793 /* We have found an `include' line specifying a nested
794 makefile to be read at this point. */
795 struct conditionals
*save
;
796 struct conditionals new_conditionals
;
797 struct nameseq
*files
;
798 /* "-include" (vs "include") says no error if the file does not
799 exist. "sinclude" is an alias for this from SGI. */
800 int noerror
= (p
[0] != 'i');
802 p
= allocated_variable_expand (p2
);
806 _("no file name for `%sinclude'"), noerror
? "-" : "");
810 /* Parse the list of file names. */
812 files
= multi_glob (parse_file_seq (&p2
, '\0',
813 sizeof (struct nameseq
),
815 sizeof (struct nameseq
));
818 /* Save the state of conditionals and start
819 the included makefile with a clean slate. */
820 save
= install_conditionals (&new_conditionals
);
822 /* Record the rules that are waiting so they will determine
823 the default goal before those in the included makefile. */
824 record_waiting_files ();
826 /* Read each included makefile. */
829 struct nameseq
*next
= files
->next
;
830 char *name
= files
->name
;
833 free ((char *)files
);
836 r
= eval_makefile (name
, (RM_INCLUDED
| RM_NO_TILDE
837 | (noerror
? RM_DONTCARE
: 0)));
841 error (fstart
, "%s: %s", name
, strerror (errno
));
846 /* Restore conditional state. */
847 restore_conditionals (save
);
852 if (try_variable_definition (fstart
, p
, o_file
, 0))
853 /* This line has been dealt with. */
856 /* This line starts with a tab but was not caught above because there
857 was no preceding target, and the line might have been usable as a
858 variable definition. But now we know it is definitely lossage. */
860 fatal(fstart
, _("commands commence before first target"));
862 /* This line describes some target files. This is complicated by
863 the existence of target-specific variables, because we can't
864 expand the entire line until we know if we have one or not. So
865 we expand the line word by word until we find the first `:',
866 then check to see if it's a target-specific variable.
868 In this algorithm, `lb_next' will point to the beginning of the
869 unexpanded parts of the input buffer, while `p2' points to the
870 parts of the expanded buffer we haven't searched yet. */
873 enum make_word_type wtype
;
874 enum variable_origin v_origin
;
876 char *cmdleft
, *semip
, *lb_next
;
877 unsigned int len
, plen
= 0;
879 const char *end
, *beg
; /* Helpers for whitespace stripping. */
881 /* Record the previous rule. */
883 record_waiting_files ();
884 tgts_started
= fstart
->lineno
;
886 /* Search the line for an unquoted ; that is not after an
888 cmdleft
= find_char_unquote (line
, ';', '#', 0, 1);
889 if (cmdleft
!= 0 && *cmdleft
== '#')
891 /* We found a comment before a semicolon. */
895 else if (cmdleft
!= 0)
896 /* Found one. Cut the line short there before expanding it. */
900 collapse_continuations (line
);
902 /* We can't expand the entire line, since if it's a per-target
903 variable we don't want to expand it. So, walk from the
904 beginning, expanding as we go, and looking for "interesting"
905 chars. The first word is always expandable. */
906 wtype
= get_next_mword(line
, NULL
, &lb_next
, &len
);
911 fatal(fstart
, _("missing rule before commands"));
912 /* This line contained something but turned out to be nothing
913 but whitespace (a comment?). */
918 /* We accept and ignore rules without targets for
919 compatibility with SunOS 4 make. */
927 p2
= variable_expand_string(NULL
, lb_next
, len
);
934 /* Look for a semicolon in the expanded line. */
935 cmdleft
= find_char_unquote (p2
, ';', 0, 0, 0);
939 unsigned long p2_off
= p2
- variable_buffer
;
940 unsigned long cmd_off
= cmdleft
- variable_buffer
;
941 char *pend
= p2
+ strlen(p2
);
943 /* Append any remnants of lb, then cut the line short
947 /* One school of thought says that you shouldn't expand
948 here, but merely copy, since now you're beyond a ";"
949 and into a command script. However, the old parser
950 expanded the whole line, so we continue that for
951 backwards-compatiblity. Also, it wouldn't be
952 entirely consistent, since we do an unconditional
953 expand below once we know we don't have a
954 target-specific variable. */
955 (void)variable_expand_string(pend
, lb_next
, (long)-1);
956 lb_next
+= strlen(lb_next
);
957 p2
= variable_buffer
+ p2_off
;
958 cmdleft
= variable_buffer
+ cmd_off
+ 1;
962 colonp
= find_char_unquote(p2
, ':', 0, 0, 0);
963 #ifdef HAVE_DOS_PATHS
964 /* The drive spec brain-damage strikes again... */
965 /* Note that the only separators of targets in this context
966 are whitespace and a left paren. If others are possible,
967 they should be added to the string in the call to index. */
968 while (colonp
&& (colonp
[1] == '/' || colonp
[1] == '\\') &&
969 colonp
> p2
&& isalpha ((unsigned char)colonp
[-1]) &&
970 (colonp
== p2
+ 1 || strchr (" \t(", colonp
[-2]) != 0))
971 colonp
= find_char_unquote(colonp
+ 1, ':', 0, 0, 0);
976 wtype
= get_next_mword(lb_next
, NULL
, &lb_next
, &len
);
982 p2
= variable_expand_string(p2
, lb_next
, len
);
983 /* We don't need to worry about cmdleft here, because if it was
984 found in the variable_buffer the entire buffer has already
985 been expanded... we'll never get here. */
988 p2
= next_token (variable_buffer
);
990 /* If the word we're looking at is EOL, see if there's _anything_
991 on the line. If not, a variable expanded to nothing, so ignore
992 it. If so, we can't parse this line so punt. */
996 /* There's no need to be ivory-tower about this: check for
997 one of the most common bugs found in makefiles... */
998 fatal (fstart
, _("missing separator%s"),
999 !strneq(line
, " ", 8) ? ""
1000 : _(" (did you mean TAB instead of 8 spaces?)"));
1004 /* Make the colon the end-of-string so we know where to stop
1005 looking for targets. */
1007 filenames
= multi_glob (parse_file_seq (&p2
, '\0',
1008 sizeof (struct nameseq
),
1010 sizeof (struct nameseq
));
1015 /* We accept and ignore rules without targets for
1016 compatibility with SunOS 4 make. */
1020 /* This should never be possible; we handled it above. */
1021 assert (*p2
!= '\0');
1024 /* Is this a one-colon or two-colon entry? */
1025 two_colon
= *p2
== ':';
1029 /* Test to see if it's a target-specific variable. Copy the rest
1030 of the buffer over, possibly temporarily (we'll expand it later
1031 if it's not a target-specific variable). PLEN saves the length
1032 of the unparsed section of p2, for later. */
1033 if (*lb_next
!= '\0')
1035 unsigned int l
= p2
- variable_buffer
;
1037 (void) variable_buffer_output (p2
+plen
,
1038 lb_next
, strlen (lb_next
)+1);
1039 p2
= variable_buffer
+ l
;
1042 /* See if it's an "override" or "export" keyword; if so see if what
1043 comes after it looks like a variable definition. */
1045 wtype
= get_next_mword (p2
, NULL
, &p
, &len
);
1049 if (wtype
== w_static
)
1051 if (word1eq ("override"))
1053 v_origin
= o_override
;
1054 wtype
= get_next_mword (p
+len
, NULL
, &p
, &len
);
1056 else if (word1eq ("export"))
1059 wtype
= get_next_mword (p
+len
, NULL
, &p
, &len
);
1064 wtype
= get_next_mword (p
+len
, NULL
, NULL
, NULL
);
1066 if (wtype
== w_varassign
)
1068 /* If there was a semicolon found, add it back, plus anything
1072 unsigned int l
= p
- variable_buffer
;
1074 variable_buffer_output (p2
+ strlen (p2
),
1075 semip
, strlen (semip
)+1);
1076 p
= variable_buffer
+ l
;
1078 record_target_var (filenames
, p
, v_origin
, exported
, fstart
);
1083 /* This is a normal target, _not_ a target-specific variable.
1084 Unquote any = in the dependency list. */
1085 find_char_unquote (lb_next
, '=', 0, 0, 0);
1087 /* We have some targets, so don't ignore the following commands. */
1090 /* Expand the dependencies, etc. */
1091 if (*lb_next
!= '\0')
1093 unsigned int l
= p2
- variable_buffer
;
1094 (void) variable_expand_string (p2
+ plen
, lb_next
, (long)-1);
1095 p2
= variable_buffer
+ l
;
1097 /* Look for a semicolon in the expanded line. */
1100 cmdleft
= find_char_unquote (p2
, ';', 0, 0, 0);
1102 *(cmdleft
++) = '\0';
1106 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
1107 p
= strchr (p2
, ':');
1108 while (p
!= 0 && p
[-1] == '\\')
1110 register char *q
= &p
[-1];
1111 register int backslash
= 0;
1112 while (*q
-- == '\\')
1113 backslash
= !backslash
;
1115 p
= strchr (p
+ 1, ':');
1120 /* Here, the situation is quite complicated. Let's have a look
1121 at a couple of targets:
1129 The rule is that it's only a target, if there are TWO :'s
1130 OR a space around the :.
1132 if (p
&& !(isspace ((unsigned char)p
[1]) || !p
[1]
1133 || isspace ((unsigned char)p
[-1])))
1136 #ifdef HAVE_DOS_PATHS
1142 /* For DOS-style paths, skip a "C:\..." or a "C:/..." */
1143 if (p
!= 0 && (p
[1] == '\\' || p
[1] == '/') &&
1144 isalpha ((unsigned char)p
[-1]) &&
1145 (p
== p2
+ 1 || strchr (" \t:(", p
[-2]) != 0)) {
1146 p
= strchr (p
+ 1, ':');
1149 } while (check_again
);
1154 struct nameseq
*target
;
1155 target
= parse_file_seq (&p2
, ':', sizeof (struct nameseq
), 1);
1158 fatal (fstart
, _("missing target pattern"));
1159 else if (target
->next
!= 0)
1160 fatal (fstart
, _("multiple target patterns"));
1161 pattern
= target
->name
;
1162 pattern_percent
= find_percent (pattern
);
1163 if (pattern_percent
== 0)
1164 fatal (fstart
, _("target pattern contains no `%%'"));
1165 free((char *)target
);
1170 /* Strip leading and trailing whitespaces. */
1172 end
= beg
+ strlen (beg
) - 1;
1173 strip_whitespace (&beg
, &end
);
1175 if (beg
<= end
&& *beg
!= '\0')
1178 const char *fromp
= beg
;
1180 /* Make a copy of the dependency string. Note if we find '$'. */
1181 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
1183 deps
->name
= top
= (char *) xmalloc (end
- beg
+ 2);
1184 deps
->need_2nd_expansion
= 0;
1185 while (fromp
<= end
)
1188 deps
->need_2nd_expansion
= 1;
1189 *(top
++) = *(fromp
++);
1200 /* Semicolon means rest of line is a command. */
1201 unsigned int len
= strlen (cmdleft
);
1203 cmds_started
= fstart
->lineno
;
1205 /* Add this command line to the buffer. */
1206 if (len
+ 2 > commands_len
)
1208 commands_len
= (len
+ 2) * 2;
1209 commands
= (char *) xrealloc (commands
, commands_len
);
1211 bcopy (cmdleft
, commands
, len
);
1212 commands_idx
+= len
;
1213 commands
[commands_idx
++] = '\n';
1216 /* Determine if this target should be made default. We used to do
1217 this in record_files() but because of the delayed target recording
1218 and because preprocessor directives are legal in target's commands
1219 it is too late. Consider this fragment for example:
1223 ifeq ($(.DEFAULT_GOAL),foo)
1227 Because the target is not recorded until after ifeq directive is
1228 evaluated the .DEFAULT_GOAL does not contain foo yet as one
1229 would expect. Because of this we have to move some of the logic
1232 if (**default_goal_name
== '\0' && set_default
)
1236 struct nameseq
*t
= filenames
;
1238 for (; t
!= 0; t
= t
->next
)
1243 /* We have nothing to do if this is an implicit rule. */
1244 if (strchr (name
, '%') != 0)
1247 /* See if this target's name does not start with a `.',
1248 unless it contains a slash. */
1249 if (*name
== '.' && strchr (name
, '/') == 0
1250 #ifdef HAVE_DOS_PATHS
1251 && strchr (name
, '\\') == 0
1257 /* If this file is a suffix, don't let it be
1258 the default goal file. */
1259 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
1261 register struct dep
*d2
;
1262 if (*dep_name (d
) != '.' && streq (name
, dep_name (d
)))
1267 for (d2
= suffix_file
->deps
; d2
!= 0; d2
= d2
->next
)
1269 register unsigned int len
= strlen (dep_name (d2
));
1270 if (!strneq (name
, dep_name (d2
), len
))
1272 if (streq (name
+ len
, dep_name (d
)))
1285 define_variable_global (".DEFAULT_GOAL", 13, t
->name
,
1295 /* We get here except in the case that we just read a rule line.
1296 Record now the last rule we read, so following spurious
1297 commands are properly diagnosed. */
1299 record_waiting_files ();
1304 if (conditionals
->if_cmds
)
1305 fatal (fstart
, _("missing `endif'"));
1307 /* At eof, record the last rule. */
1308 record_waiting_files ();
1311 free ((char *) collapsed
);
1312 free ((char *) commands
);
1318 /* Remove comments from LINE.
1319 This is done by copying the text at LINE onto itself. */
1322 remove_comments (char *line
)
1326 comment
= find_char_unquote (line
, '#', 0, 0, 0);
1329 /* Cut off the line at the #. */
1333 /* Execute a `define' directive.
1334 The first line has already been read, and NAME is the name of
1335 the variable to be defined. The following lines remain to be read. */
1338 do_define (char *name
, unsigned int namelen
,
1339 enum variable_origin origin
, struct ebuffer
*ebuf
)
1341 struct floc defstart
;
1344 unsigned int length
= 100;
1345 char *definition
= (char *) xmalloc (length
);
1346 unsigned int idx
= 0;
1349 /* Expand the variable name. */
1350 char *var
= (char *) alloca (namelen
+ 1);
1351 bcopy (name
, var
, namelen
);
1352 var
[namelen
] = '\0';
1353 var
= variable_expand (var
);
1355 defstart
= ebuf
->floc
;
1362 nlines
= readline (ebuf
);
1363 ebuf
->floc
.lineno
+= nlines
;
1365 /* If there is nothing left to eval, we're done. */
1369 line
= ebuf
->buffer
;
1371 collapse_continuations (line
);
1373 /* If the line doesn't begin with a tab, test to see if it introduces
1374 another define, or ends one. */
1376 /* Stop if we find an 'endef' */
1377 if (line
[0] != '\t')
1379 p
= next_token (line
);
1382 /* If this is another 'define', increment the level count. */
1383 if ((len
== 6 || (len
> 6 && isblank ((unsigned char)p
[6])))
1384 && strneq (p
, "define", 6))
1387 /* If this is an 'endef', decrement the count. If it's now 0,
1388 we've found the last one. */
1389 else if ((len
== 5 || (len
> 5 && isblank ((unsigned char)p
[5])))
1390 && strneq (p
, "endef", 5))
1393 remove_comments (p
);
1394 if (*next_token (p
) != '\0')
1396 _("Extraneous text after `endef' directive"));
1400 /* Define the variable. */
1402 definition
[0] = '\0';
1404 definition
[idx
- 1] = '\0';
1406 /* Always define these variables in the global set. */
1407 define_variable_global (var
, strlen (var
), definition
,
1408 origin
, 1, &defstart
);
1415 /* Otherwise add this line to the variable definition. */
1416 len
= strlen (line
);
1417 if (idx
+ len
+ 1 > length
)
1419 length
= (idx
+ len
) * 2;
1420 definition
= (char *) xrealloc (definition
, length
+ 1);
1423 bcopy (line
, &definition
[idx
], len
);
1425 /* Separate lines with a newline. */
1426 definition
[idx
++] = '\n';
1430 fatal (&defstart
, _("missing `endef', unterminated `define'"));
1436 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1437 "ifneq", "else" and "endif".
1438 LINE is the input line, with the command as its first word.
1440 FILENAME and LINENO are the filename and line number in the
1441 current makefile. They are used for error messages.
1443 Value is -2 if the line is not a conditional at all,
1444 -1 if the line is an invalid conditional,
1445 0 if following text should be interpreted,
1446 1 if following text should be ignored. */
1449 conditional_line (char *line
, int len
, const struct floc
*flocp
)
1452 enum { c_ifdef
, c_ifndef
, c_ifeq
, c_ifneq
, c_else
, c_endif
} cmdtype
;
1456 /* Compare a word, both length and contents. */
1457 #define word1eq(s) (len == sizeof(s)-1 && strneq (s, line, sizeof(s)-1))
1458 #define chkword(s, t) if (word1eq (s)) { cmdtype = (t); cmdname = (s); }
1460 /* Make sure this line is a conditional. */
1461 chkword ("ifdef", c_ifdef
)
1462 else chkword ("ifndef", c_ifndef
)
1463 else chkword ("ifeq", c_ifeq
)
1464 else chkword ("ifneq", c_ifneq
)
1465 else chkword ("else", c_else
)
1466 else chkword ("endif", c_endif
)
1470 /* Found one: skip past it and any whitespace after it. */
1471 line
= next_token (line
+ len
);
1473 #define EXTRANEOUS() error (flocp, _("Extraneous text after `%s' directive"), cmdname)
1475 /* An 'endif' cannot contain extra text, and reduces the if-depth by 1 */
1476 if (cmdtype
== c_endif
)
1481 if (!conditionals
->if_cmds
)
1482 fatal (flocp
, _("extraneous `%s'"), cmdname
);
1484 --conditionals
->if_cmds
;
1489 /* An 'else' statement can either be simple, or it can have another
1490 conditional after it. */
1491 if (cmdtype
== c_else
)
1495 if (!conditionals
->if_cmds
)
1496 fatal (flocp
, _("extraneous `%s'"), cmdname
);
1498 o
= conditionals
->if_cmds
- 1;
1500 if (conditionals
->seen_else
[o
])
1501 fatal (flocp
, _("only one `else' per conditional"));
1503 /* Change the state of ignorance. */
1504 switch (conditionals
->ignoring
[o
])
1507 /* We've just been interpreting. Never do it again. */
1508 conditionals
->ignoring
[o
] = 2;
1511 /* We've never interpreted yet. Maybe this time! */
1512 conditionals
->ignoring
[o
] = 0;
1516 /* It's a simple 'else'. */
1519 conditionals
->seen_else
[o
] = 1;
1523 /* The 'else' has extra text. That text must be another conditional
1524 and cannot be an 'else' or 'endif'. */
1526 /* Find the length of the next word. */
1527 for (p
= line
+1; *p
!= '\0' && !isspace ((unsigned char)*p
); ++p
)
1531 /* If it's 'else' or 'endif' or an illegal conditional, fail. */
1532 if (word1eq("else") || word1eq("endif")
1533 || conditional_line (line
, len
, flocp
) < 0)
1537 /* conditional_line() created a new level of conditional.
1538 Raise it back to this level. */
1539 if (conditionals
->ignoring
[o
] < 2)
1540 conditionals
->ignoring
[o
] = conditionals
->ignoring
[o
+1];
1541 --conditionals
->if_cmds
;
1547 if (conditionals
->allocated
== 0)
1549 conditionals
->allocated
= 5;
1550 conditionals
->ignoring
= (char *) xmalloc (conditionals
->allocated
);
1551 conditionals
->seen_else
= (char *) xmalloc (conditionals
->allocated
);
1554 o
= conditionals
->if_cmds
++;
1555 if (conditionals
->if_cmds
> conditionals
->allocated
)
1557 conditionals
->allocated
+= 5;
1558 conditionals
->ignoring
= (char *)
1559 xrealloc (conditionals
->ignoring
, conditionals
->allocated
);
1560 conditionals
->seen_else
= (char *)
1561 xrealloc (conditionals
->seen_else
, conditionals
->allocated
);
1564 /* Record that we have seen an `if...' but no `else' so far. */
1565 conditionals
->seen_else
[o
] = 0;
1567 /* Search through the stack to see if we're already ignoring. */
1568 for (i
= 0; i
< o
; ++i
)
1569 if (conditionals
->ignoring
[i
])
1571 /* We are already ignoring, so just push a level to match the next
1572 "else" or "endif", and keep ignoring. We don't want to expand
1573 variables in the condition. */
1574 conditionals
->ignoring
[o
] = 1;
1578 if (cmdtype
== c_ifdef
|| cmdtype
== c_ifndef
)
1584 /* Expand the thing we're looking up, so we can use indirect and
1585 constructed variable names. */
1586 var
= allocated_variable_expand (line
);
1588 /* Make sure there's only one variable name to test. */
1589 p
= end_of_token (var
);
1596 v
= lookup_variable (var
, i
);
1598 conditionals
->ignoring
[o
] =
1599 ((v
!= 0 && *v
->value
!= '\0') == (cmdtype
== c_ifndef
));
1605 /* "Ifeq" or "ifneq". */
1608 char termin
= *line
== '(' ? ',' : *line
;
1610 if (termin
!= ',' && termin
!= '"' && termin
!= '\'')
1614 /* Find the end of the first string. */
1618 for (; *line
!= '\0'; ++line
)
1621 else if (*line
== ')')
1623 else if (*line
== ',' && count
<= 0)
1627 while (*line
!= '\0' && *line
!= termin
)
1635 /* Strip blanks after the first string. */
1637 while (isblank ((unsigned char)p
[-1]))
1644 s2
= variable_expand (s1
);
1645 /* We must allocate a new copy of the expanded string because
1646 variable_expand re-uses the same buffer. */
1648 s1
= (char *) alloca (len
+ 1);
1649 bcopy (s2
, s1
, len
+ 1);
1652 /* Find the start of the second string. */
1653 line
= next_token (line
);
1655 termin
= termin
== ',' ? ')' : *line
;
1656 if (termin
!= ')' && termin
!= '"' && termin
!= '\'')
1659 /* Find the end of the second string. */
1662 register int count
= 0;
1663 s2
= next_token (line
);
1664 for (line
= s2
; *line
!= '\0'; ++line
)
1668 else if (*line
== ')')
1681 while (*line
!= '\0' && *line
!= termin
)
1689 line
= next_token (++line
);
1693 s2
= variable_expand (s2
);
1694 conditionals
->ignoring
[o
] = (streq (s1
, s2
) == (cmdtype
== c_ifneq
));
1698 /* Search through the stack to see if we're ignoring. */
1699 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1700 if (conditionals
->ignoring
[i
])
1705 /* Remove duplicate dependencies in CHAIN. */
1707 static unsigned long
1708 dep_hash_1 (const void *key
)
1710 return_STRING_HASH_1 (dep_name ((struct dep
const *) key
));
1713 static unsigned long
1714 dep_hash_2 (const void *key
)
1716 return_STRING_HASH_2 (dep_name ((struct dep
const *) key
));
1720 dep_hash_cmp (const void *x
, const void *y
)
1722 struct dep
*dx
= (struct dep
*) x
;
1723 struct dep
*dy
= (struct dep
*) y
;
1724 int cmp
= strcmp (dep_name (dx
), dep_name (dy
));
1726 /* If the names are the same but ignore_mtimes are not equal, one of these
1727 is an order-only prerequisite and one isn't. That means that we should
1728 remove the one that isn't and keep the one that is. */
1730 if (!cmp
&& dx
->ignore_mtime
!= dy
->ignore_mtime
)
1731 dx
->ignore_mtime
= dy
->ignore_mtime
= 0;
1738 uniquize_deps (struct dep
*chain
)
1740 struct hash_table deps
;
1741 register struct dep
**depp
;
1743 hash_init (&deps
, 500, dep_hash_1
, dep_hash_2
, dep_hash_cmp
);
1745 /* Make sure that no dependencies are repeated. This does not
1746 really matter for the purpose of updating targets, but it
1747 might make some names be listed twice for $^ and $?. */
1752 struct dep
*dep
= *depp
;
1753 struct dep
**dep_slot
= (struct dep
**) hash_find_slot (&deps
, dep
);
1754 if (HASH_VACANT (*dep_slot
))
1756 hash_insert_at (&deps
, dep
, dep_slot
);
1761 /* Don't bother freeing duplicates.
1762 It's dangerous and little benefit accrues. */
1767 hash_free (&deps
, 0);
1770 /* Record target-specific variable values for files FILENAMES.
1771 TWO_COLON is nonzero if a double colon was used.
1773 The links of FILENAMES are freed, and so are any names in it
1774 that are not incorporated into other data structures.
1776 If the target is a pattern, add the variable to the pattern-specific
1777 variable value list. */
1780 record_target_var (struct nameseq
*filenames
, char *defn
,
1781 enum variable_origin origin
, int exported
,
1782 const struct floc
*flocp
)
1784 struct nameseq
*nextf
;
1785 struct variable_set_list
*global
;
1787 global
= current_variable_set_list
;
1789 /* If the variable is an append version, store that but treat it as a
1790 normal recursive variable. */
1792 for (; filenames
!= 0; filenames
= nextf
)
1795 register char *name
= filenames
->name
;
1798 struct pattern_var
*p
;
1800 nextf
= filenames
->next
;
1801 free ((char *) filenames
);
1803 /* If it's a pattern target, then add it to the pattern-specific
1805 percent
= find_percent (name
);
1808 /* Get a reference for this pattern-specific variable struct. */
1809 p
= create_pattern_var (name
, percent
);
1810 p
->variable
.fileinfo
= *flocp
;
1811 /* I don't think this can fail since we already determined it was a
1812 variable definition. */
1813 v
= parse_variable_definition (&p
->variable
, defn
);
1816 if (v
->flavor
== f_simple
)
1817 v
->value
= allocated_variable_expand (v
->value
);
1819 v
->value
= xstrdup (v
->value
);
1827 /* Get a file reference for this file, and initialize it.
1828 We don't want to just call enter_file() because that allocates a
1829 new entry if the file is a double-colon, which we don't want in
1831 f
= lookup_file (name
);
1833 f
= enter_file (name
);
1834 else if (f
->double_colon
)
1835 f
= f
->double_colon
;
1837 initialize_file_variables (f
, 1);
1840 current_variable_set_list
= f
->variables
;
1841 v
= try_variable_definition (flocp
, defn
, origin
, 1);
1843 error (flocp
, _("Malformed target-specific variable definition"));
1844 current_variable_set_list
= global
;
1847 /* Set up the variable to be *-specific. */
1851 v
->export
= v_export
;
1853 /* If it's not an override, check to see if there was a command-line
1854 setting. If so, reset the value. */
1855 if (origin
!= o_override
)
1857 struct variable
*gv
;
1858 int len
= strlen(v
->name
);
1860 gv
= lookup_variable (v
->name
, len
);
1861 if (gv
&& (gv
->origin
== o_env_override
|| gv
->origin
== o_command
))
1865 v
->value
= xstrdup (gv
->value
);
1866 v
->origin
= gv
->origin
;
1867 v
->recursive
= gv
->recursive
;
1872 /* Free name if not needed further. */
1873 if (name
!= fname
&& (name
< fname
|| name
> fname
+ strlen (fname
)))
1878 /* Record a description line for files FILENAMES,
1879 with dependencies DEPS, commands to execute described
1880 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1881 TWO_COLON is nonzero if a double colon was used.
1882 If not nil, PATTERN is the `%' pattern to make this
1883 a static pattern rule, and PATTERN_PERCENT is a pointer
1884 to the `%' within it.
1886 The links of FILENAMES are freed, and so are any names in it
1887 that are not incorporated into other data structures. */
1890 record_files (struct nameseq
*filenames
, char *pattern
, char *pattern_percent
,
1891 struct dep
*deps
, unsigned int cmds_started
, char *commands
,
1892 unsigned int commands_idx
, int two_colon
,
1893 const struct floc
*flocp
)
1895 struct nameseq
*nextf
;
1897 unsigned int max_targets
= 0, target_idx
= 0;
1898 char **targets
= 0, **target_percents
= 0;
1899 struct commands
*cmds
;
1901 /* If we've already snapped deps, that means we're in an eval being
1902 resolved after the makefiles have been read in. We can't add more rules
1903 at this time, since they won't get snapped and we'll get core dumps.
1904 See Savannah bug # 12124. */
1906 fatal (flocp
, _("prerequisites cannot be defined in command scripts"));
1908 if (commands_idx
> 0)
1910 cmds
= (struct commands
*) xmalloc (sizeof (struct commands
));
1911 cmds
->fileinfo
.filenm
= flocp
->filenm
;
1912 cmds
->fileinfo
.lineno
= cmds_started
;
1913 cmds
->commands
= savestring (commands
, commands_idx
);
1914 cmds
->command_lines
= 0;
1919 for (; filenames
!= 0; filenames
= nextf
)
1921 char *name
= filenames
->name
;
1925 char *implicit_percent
;
1927 nextf
= filenames
->next
;
1930 /* Check for .POSIX. We used to do this in snap_deps() but that's not
1931 good enough: it doesn't happen until after the makefile is read,
1932 which means we cannot use its value during parsing. */
1934 if (streq (name
, ".POSIX"))
1937 implicit_percent
= find_percent (name
);
1938 implicit
|= implicit_percent
!= 0;
1940 if (implicit
&& pattern
!= 0)
1941 fatal (flocp
, _("mixed implicit and static pattern rules"));
1943 if (implicit
&& implicit_percent
== 0)
1944 fatal (flocp
, _("mixed implicit and normal rules"));
1951 targets
= (char **) xmalloc (5 * sizeof (char *));
1952 target_percents
= (char **) xmalloc (5 * sizeof (char *));
1955 else if (target_idx
== max_targets
- 1)
1958 targets
= (char **) xrealloc ((char *) targets
,
1959 max_targets
* sizeof (char *));
1961 = (char **) xrealloc ((char *) target_percents
,
1962 max_targets
* sizeof (char *));
1964 targets
[target_idx
] = name
;
1965 target_percents
[target_idx
] = implicit_percent
;
1970 /* If there are multiple filenames, copy the chain DEPS
1971 for all but the last one. It is not safe for the same deps
1972 to go in more than one place in the database. */
1973 this = nextf
!= 0 ? copy_dep_chain (deps
) : deps
;
1977 /* If this is an extended static rule:
1978 `targets: target%pattern: dep%pattern; cmds',
1979 translate each dependency pattern into a plain filename
1980 using the target pattern and this target's name. */
1981 if (!pattern_matches (pattern
, pattern_percent
, name
))
1983 /* Give a warning if the rule is meaningless. */
1985 _("target `%s' doesn't match the target pattern"), name
);
1989 /* We use subst_expand to do the work of translating % to $* in
1990 the dependency line. */
1992 if (this != 0 && find_percent (this->name
) != 0)
1995 char *buffer
= variable_expand ("");
1997 o
= subst_expand (buffer
, this->name
, "%", "$*", 1, 2, 0);
2000 this->name
= savestring (buffer
, o
- buffer
);
2001 this->need_2nd_expansion
= 1;
2007 /* Single-colon. Combine these dependencies
2008 with others in file's existing record, if any. */
2009 f
= enter_file (name
);
2011 if (f
->double_colon
)
2013 _("target file `%s' has both : and :: entries"), f
->name
);
2015 /* If CMDS == F->CMDS, this target was listed in this rule
2016 more than once. Just give a warning since this is harmless. */
2017 if (cmds
!= 0 && cmds
== f
->cmds
)
2019 _("target `%s' given more than once in the same rule."),
2022 /* Check for two single-colon entries both with commands.
2023 Check is_target so that we don't lose on files such as .c.o
2024 whose commands were preinitialized. */
2025 else if (cmds
!= 0 && f
->cmds
!= 0 && f
->is_target
)
2027 error (&cmds
->fileinfo
,
2028 _("warning: overriding commands for target `%s'"),
2030 error (&f
->cmds
->fileinfo
,
2031 _("warning: ignoring old commands for target `%s'"),
2037 /* Defining .DEFAULT with no deps or cmds clears it. */
2038 if (f
== default_file
&& this == 0 && cmds
== 0)
2043 /* Defining .SUFFIXES with no dependencies
2044 clears out the list of suffixes. */
2045 if (f
== suffix_file
&& this == 0)
2050 struct dep
*nextd
= d
->next
;
2059 /* Add the file's old deps and the new ones in THIS together. */
2063 struct dep
**d_ptr
= &f
->deps
;
2065 while ((*d_ptr
)->next
!= 0)
2066 d_ptr
= &(*d_ptr
)->next
;
2070 /* This is the rule with commands, so put its deps
2071 last. The rationale behind this is that $< expands
2072 to the first dep in the chain, and commands use $<
2073 expecting to get the dep that rule specifies.
2074 However the second expansion algorithm reverses
2075 the order thus we need to make it last here. */
2077 (*d_ptr
)->next
= this;
2081 /* This is the rule without commands. Put its
2082 dependencies at the end but before dependencies
2083 from the rule with commands (if any). This way
2084 everything appears in makefile order. */
2088 this->next
= *d_ptr
;
2092 (*d_ptr
)->next
= this;
2098 /* This is a hack. I need a way to communicate to snap_deps()
2099 that the last dependency line in this file came with commands
2100 (so that logic in snap_deps() can put it in front and all
2101 this $< -logic works). I cannot simply rely on file->cmds
2102 being not 0 because of the cases like the following:
2108 I am going to temporarily "borrow" UPDATING member in
2109 `struct file' for this. */
2115 /* If this is a static pattern rule, set the file's stem to
2116 the part of its name that matched the `%' in the pattern,
2117 so you can use $* in the commands. */
2120 static char *percent
= "%";
2121 char *buffer
= variable_expand ("");
2122 char *o
= patsubst_expand (buffer
, name
, pattern
, percent
,
2123 pattern_percent
+1, percent
+1);
2124 f
->stem
= savestring (buffer
, o
- buffer
);
2129 /* Double-colon. Make a new record
2130 even if the file already has one. */
2131 f
= lookup_file (name
);
2132 /* Check for both : and :: rules. Check is_target so
2133 we don't lose on default suffix rules or makefiles. */
2134 if (f
!= 0 && f
->is_target
&& !f
->double_colon
)
2136 _("target file `%s' has both : and :: entries"), f
->name
);
2137 f
= enter_file (name
);
2138 /* If there was an existing entry and it was a double-colon
2139 entry, enter_file will have returned a new one, making it the
2140 prev pointer of the old one, and setting its double_colon
2141 pointer to the first one. */
2142 if (f
->double_colon
== 0)
2143 /* This is the first entry for this name, so we must
2144 set its double_colon pointer to itself. */
2145 f
->double_colon
= f
;
2151 /* Free name if not needed further. */
2152 if (f
!= 0 && name
!= f
->name
2153 && (name
< f
->name
|| name
> f
->name
+ strlen (f
->name
)))
2159 /* If this target is a default target, update DEFAULT_GOAL_FILE. */
2160 if (strcmp (*default_goal_name
, name
) == 0
2161 && (default_goal_file
== 0
2162 || strcmp (default_goal_file
->name
, name
) != 0))
2163 default_goal_file
= f
;
2168 targets
[target_idx
] = 0;
2169 target_percents
[target_idx
] = 0;
2170 create_pattern_rule (targets
, target_percents
, two_colon
, deps
, cmds
, 1);
2171 free ((char *) target_percents
);
2175 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2176 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2177 Quoting backslashes are removed from STRING by compacting it into
2178 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2179 one, or nil if there are none. STOPCHARs inside variable references are
2180 ignored if IGNOREVARS is true.
2182 STOPCHAR _cannot_ be '$' if IGNOREVARS is true. */
2185 find_char_unquote (char *string
, int stop1
, int stop2
, int blank
,
2188 unsigned int string_len
= 0;
2189 register char *p
= string
;
2197 while (*p
!= '\0' && *p
!= ignorevars
&& *p
!= stop1
&& *p
!= stop2
2198 && ! isblank ((unsigned char) *p
))
2201 while (*p
!= '\0' && *p
!= ignorevars
&& *p
!= stop1
&& *p
!= stop2
)
2204 while (*p
!= '\0' && *p
!= ignorevars
&& *p
!= stop1
2205 && ! isblank ((unsigned char) *p
))
2208 while (*p
!= '\0' && *p
!= ignorevars
&& *p
!= stop1
)
2214 /* If we stopped due to a variable reference, skip over its contents. */
2215 if (*p
== ignorevars
)
2217 char openparen
= p
[1];
2221 /* Skip the contents of a non-quoted, multi-char variable ref. */
2222 if (openparen
== '(' || openparen
== '{')
2224 unsigned int pcount
= 1;
2225 char closeparen
= (openparen
== '(' ? ')' : '}');
2229 if (*p
== openparen
)
2231 else if (*p
== closeparen
)
2241 /* Skipped the variable reference: look for STOPCHARS again. */
2245 if (p
> string
&& p
[-1] == '\\')
2247 /* Search for more backslashes. */
2248 register int i
= -2;
2249 while (&p
[i
] >= string
&& p
[i
] == '\\')
2252 /* Only compute the length if really needed. */
2253 if (string_len
== 0)
2254 string_len
= strlen (string
);
2255 /* The number of backslashes is now -I.
2256 Copy P over itself to swallow half of them. */
2257 bcopy (&p
[i
/ 2], &p
[i
], (string_len
- (p
- string
)) - (i
/ 2) + 1);
2260 /* All the backslashes quoted each other; the STOPCHAR was
2264 /* The STOPCHAR was quoted by a backslash. Look for another. */
2267 /* No backslash in sight. */
2271 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2275 /* Search PATTERN for an unquoted %. */
2278 find_percent (char *pattern
)
2280 return find_char_unquote (pattern
, '%', 0, 0, 0);
2283 /* Parse a string into a sequence of filenames represented as a
2284 chain of struct nameseq's in reverse order and return that chain.
2286 The string is passed as STRINGP, the address of a string pointer.
2287 The string pointer is updated to point at the first character
2288 not parsed, which either is a null char or equals STOPCHAR.
2290 SIZE is how big to construct chain elements.
2291 This is useful if we want them actually to be other structures
2292 that have room for additional info.
2294 If STRIP is nonzero, strip `./'s off the beginning. */
2297 parse_file_seq (char **stringp
, int stopchar
, unsigned int size
, int strip
)
2299 register struct nameseq
*new = 0;
2300 register struct nameseq
*new1
, *lastnew1
;
2301 register char *p
= *stringp
;
2306 # define VMS_COMMA ','
2308 # define VMS_COMMA 0
2313 /* Skip whitespace; see if any more names are left. */
2320 /* Yes, find end of next name. */
2322 p
= find_char_unquote (q
, stopchar
, VMS_COMMA
, 1, 0);
2324 /* convert comma separated list to space separated */
2329 if (stopchar
== ':' && p
&& *p
== ':'
2330 && !(isspace ((unsigned char)p
[1]) || !p
[1]
2331 || isspace ((unsigned char)p
[-1])))
2333 p
= find_char_unquote (p
+1, stopchar
, VMS_COMMA
, 1, 0);
2336 #ifdef HAVE_DOS_PATHS
2337 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
2338 first colon which isn't followed by a slash or a backslash.
2339 Note that tokens separated by spaces should be treated as separate
2340 tokens since make doesn't allow path names with spaces */
2341 if (stopchar
== ':')
2342 while (p
!= 0 && !isspace ((unsigned char)*p
) &&
2343 (p
[1] == '\\' || p
[1] == '/') && isalpha ((unsigned char)p
[-1]))
2344 p
= find_char_unquote (p
+ 1, stopchar
, VMS_COMMA
, 1, 0);
2351 /* Skip leading `[]'s. */
2352 while (p
- q
> 2 && q
[0] == '[' && q
[1] == ']')
2354 /* Skip leading `./'s. */
2355 while (p
- q
> 2 && q
[0] == '.' && q
[1] == '/')
2358 q
+= 2; /* Skip "./". */
2359 while (q
< p
&& *q
== '/')
2360 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
2364 /* Extract the filename just found, and skip it. */
2367 /* ".///" was stripped to "". */
2372 name
= savestring ("", 0);
2374 name
= savestring ("./", 2);
2379 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
2380 * to remove this '\' before we can use the filename.
2381 * Savestring called because q may be read-only string constant.
2384 char *qbase
= xstrdup (q
);
2385 char *pbase
= qbase
+ (p
-q
);
2392 if (*q1
== '\\' && *(q1
+1) == ':')
2399 name
= savestring (qbase
, p1
- qbase
);
2403 name
= savestring (q
, p
- q
);
2406 /* Add it to the front of the chain. */
2407 new1
= (struct nameseq
*) xmalloc (size
);
2415 /* Look for multi-word archive references.
2416 They are indicated by a elt ending with an unmatched `)' and
2417 an elt further down the chain (i.e., previous in the file list)
2418 with an unmatched `(' (e.g., "lib(mem"). */
2423 if (new1
->name
[0] != '(' /* Don't catch "(%)" and suchlike. */
2424 && new1
->name
[strlen (new1
->name
) - 1] == ')'
2425 && strchr (new1
->name
, '(') == 0)
2427 /* NEW1 ends with a `)' but does not contain a `('.
2428 Look back for an elt with an opening `(' but no closing `)'. */
2430 struct nameseq
*n
= new1
->next
, *lastn
= new1
;
2432 while (n
!= 0 && (paren
= strchr (n
->name
, '(')) == 0)
2438 /* Ignore something starting with `(', as that cannot actually
2439 be an archive-member reference (and treating it as such
2440 results in an empty file name, which causes much lossage). */
2441 && n
->name
[0] != '(')
2443 /* N is the first element in the archive group.
2444 Its name looks like "lib(mem" (with no closing `)'). */
2448 /* Copy "lib(" into LIBNAME. */
2450 libname
= (char *) alloca (paren
- n
->name
+ 1);
2451 bcopy (n
->name
, libname
, paren
- n
->name
);
2452 libname
[paren
- n
->name
] = '\0';
2456 /* N was just "lib(", part of something like "lib( a b)".
2457 Edit it out of the chain and free its storage. */
2458 lastn
->next
= n
->next
;
2461 /* LASTN->next is the new stopping elt for the loop below. */
2466 /* Replace N's name with the full archive reference. */
2467 name
= concat (libname
, paren
, ")");
2472 if (new1
->name
[1] == '\0')
2474 /* NEW1 is just ")", part of something like "lib(a b )".
2475 Omit it from the chain and free its storage. */
2479 lastnew1
->next
= new1
->next
;
2483 free ((char *) lastn
);
2487 /* Replace also NEW1->name, which already has closing `)'. */
2488 name
= concat (libname
, new1
->name
, "");
2494 /* Trace back from NEW1 (the end of the list) until N
2495 (the beginning of the list), rewriting each name
2496 with the full archive reference. */
2500 name
= concat (libname
, new1
->name
, ")");
2509 /* No frobnication happening. Just step down the list. */
2526 /* Find the next line of text in an eval buffer, combining continuation lines
2528 Return the number of actual lines read (> 1 if continuation lines).
2529 Returns -1 if there's nothing left in the buffer.
2531 After this function, ebuf->buffer points to the first character of the
2535 /* Read a line of text from a STRING.
2536 Since we aren't really reading from a file, don't bother with linenumbers.
2539 static unsigned long
2540 readstring (struct ebuffer
*ebuf
)
2544 /* If there is nothing left in this buffer, return 0. */
2545 if (ebuf
->bufnext
>= ebuf
->bufstart
+ ebuf
->size
)
2548 /* Set up a new starting point for the buffer, and find the end of the
2549 next logical line (taking into account backslash/newline pairs). */
2551 eol
= ebuf
->buffer
= ebuf
->bufnext
;
2559 /* Find the next newline. At EOS, stop. */
2560 eol
= p
= strchr (eol
, '\n');
2563 ebuf
->bufnext
= ebuf
->bufstart
+ ebuf
->size
+ 1;
2567 /* Found a newline; if it's escaped continue; else we're done. */
2568 while (p
> bol
&& *(--p
) == '\\')
2569 backslash
= !backslash
;
2575 /* Overwrite the newline char. */
2577 ebuf
->bufnext
= eol
+1;
2583 readline (struct ebuffer
*ebuf
)
2590 /* The behaviors between string and stream buffers are different enough to
2591 warrant different functions. Do the Right Thing. */
2594 return readstring (ebuf
);
2596 /* When reading from a file, we always start over at the beginning of the
2597 buffer for each new line. */
2599 p
= start
= ebuf
->bufstart
;
2600 end
= p
+ ebuf
->size
;
2603 while (fgets (p
, end
- p
, ebuf
->fp
) != 0)
2612 /* This only happens when the first thing on the line is a '\0'.
2613 It is a pretty hopeless case, but (wonder of wonders) Athena
2614 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2615 There is nothing really to be done; we synthesize a newline so
2616 the following line doesn't appear to be part of this line. */
2618 _("warning: NUL character seen; rest of line ignored"));
2623 /* Jump past the text we just read. */
2626 /* If the last char isn't a newline, the whole line didn't fit into the
2627 buffer. Get some more buffer and try again. */
2631 /* We got a newline, so add one to the count of lines. */
2634 #if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
2635 /* Check to see if the line was really ended with CRLF; if so ignore
2637 if ((p
- start
) > 1 && p
[-2] == '\r')
2645 for (p2
= p
- 2; p2
>= start
; --p2
)
2649 backslash
= !backslash
;
2658 /* It was a backslash/newline combo. If we have more space, read
2663 /* We need more space at the end of our buffer, so realloc it.
2664 Make sure to preserve the current offset of p. */
2667 unsigned long off
= p
- start
;
2669 start
= ebuf
->buffer
= ebuf
->bufstart
= (char *) xrealloc (start
,
2672 end
= start
+ ebuf
->size
;
2677 if (ferror (ebuf
->fp
))
2678 pfatal_with_name (ebuf
->floc
.filenm
);
2680 /* If we found some lines, return how many.
2681 If we didn't, but we did find _something_, that indicates we read the last
2682 line of a file with no final newline; return 1.
2683 If we read nothing, we're at EOF; return -1. */
2685 return nlines
? nlines
: p
== ebuf
->bufstart
? -1 : 1;
2688 /* Parse the next "makefile word" from the input buffer, and return info
2691 A "makefile word" is one of:
2693 w_bogus Should never happen
2695 w_static A static word; cannot be expanded
2696 w_variable A word containing one or more variables/functions
2698 w_dcolon A double-colon
2699 w_semicolon A semicolon
2700 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2702 Note that this function is only used when reading certain parts of the
2703 makefile. Don't use it where special rules hold sway (RHS of a variable,
2704 in a command list, etc.) */
2706 static enum make_word_type
2707 get_next_mword (char *buffer
, char *delim
, char **startp
, unsigned int *length
)
2709 enum make_word_type wtype
= w_bogus
;
2710 char *p
= buffer
, *beg
;
2713 /* Skip any leading whitespace. */
2714 while (isblank ((unsigned char)*p
))
2726 wtype
= w_semicolon
;
2730 wtype
= w_varassign
;
2744 wtype
= w_varassign
;
2754 wtype
= w_varassign
;
2759 if (delim
&& strchr (delim
, c
))
2764 /* Did we find something? If so, return now. */
2765 if (wtype
!= w_bogus
)
2768 /* This is some non-operator word. A word consists of the longest
2769 string of characters that doesn't contain whitespace, one of [:=#],
2770 or [?+]=, or one of the chars in the DELIM string. */
2772 /* We start out assuming a static word; if we see a variable we'll
2773 adjust our assumptions then. */
2776 /* We already found the first value of "c", above. */
2791 #ifdef HAVE_DOS_PATHS
2792 /* A word CAN include a colon in its drive spec. The drive
2793 spec is allowed either at the beginning of a word, or as part
2794 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2796 && (*p
== '/' || *p
== '\\') && isalpha ((unsigned char)p
[-2])
2797 && (p
- beg
== 2 || p
[-3] == '(')))
2806 /* This is a variable reference, so note that it's expandable.
2807 Then read it to the matching close paren. */
2815 /* This is a single-letter variable reference. */
2818 for (count
=0; *p
!= '\0'; ++p
)
2822 else if (*p
== closeparen
&& --count
< 0)
2849 if (delim
&& strchr (delim
, c
))
2867 /* Construct the list of include directories
2868 from the arguments and the default list. */
2871 construct_include_path (char **arg_dirs
)
2873 register unsigned int i
;
2874 #ifdef VAXC /* just don't ask ... */
2879 /* Table to hold the dirs. */
2881 register unsigned int defsize
= (sizeof (default_include_directories
)
2882 / sizeof (default_include_directories
[0]));
2883 register unsigned int max
= 5;
2884 register char **dirs
= (char **) xmalloc ((5 + defsize
) * sizeof (char *));
2885 register unsigned int idx
= 0;
2891 /* First consider any dirs specified with -I switches.
2892 Ignore dirs that don't exist. */
2895 while (*arg_dirs
!= 0)
2897 char *dir
= *arg_dirs
++;
2902 char *expanded
= tilde_expand (dir
);
2907 EINTRLOOP (e
, stat (dir
, &stbuf
));
2908 if (e
== 0 && S_ISDIR (stbuf
.st_mode
))
2914 xrealloc ((char *) dirs
, (max
+ defsize
) * sizeof (char *));
2918 else if (dir
!= arg_dirs
[-1])
2922 /* Now add at the end the standard default dirs. */
2926 /* The environment variable $DJDIR holds the root of the
2927 DJGPP directory tree; add ${DJDIR}/include. */
2928 struct variable
*djdir
= lookup_variable ("DJDIR", 5);
2932 char *defdir
= (char *) xmalloc (strlen (djdir
->value
) + 8 + 1);
2934 strcat (strcpy (defdir
, djdir
->value
), "/include");
2935 dirs
[idx
++] = defdir
;
2940 for (i
= 0; default_include_directories
[i
] != 0; ++i
)
2944 EINTRLOOP (e
, stat (default_include_directories
[i
], &stbuf
));
2945 if (e
== 0 && S_ISDIR (stbuf
.st_mode
))
2946 dirs
[idx
++] = default_include_directories
[i
];
2951 /* Now compute the maximum length of any name in it. */
2954 for (i
= 0; i
< idx
; ++i
)
2956 unsigned int len
= strlen (dirs
[i
]);
2957 /* If dir name is written with a trailing slash, discard it. */
2958 if (dirs
[i
][len
- 1] == '/')
2959 /* We can't just clobber a null in because it may have come from
2960 a literal string and literal strings may not be writable. */
2961 dirs
[i
] = savestring (dirs
[i
], len
- 1);
2962 if (len
> max_incl_len
)
2966 include_directories
= dirs
;
2969 /* Expand ~ or ~USER at the beginning of NAME.
2970 Return a newly malloc'd string or 0. */
2973 tilde_expand (char *name
)
2976 if (name
[1] == '/' || name
[1] == '\0')
2978 extern char *getenv ();
2983 /* Turn off --warn-undefined-variables while we expand HOME. */
2984 int save
= warn_undefined_variables_flag
;
2985 warn_undefined_variables_flag
= 0;
2987 home_dir
= allocated_variable_expand ("$(HOME)");
2989 warn_undefined_variables_flag
= save
;
2992 is_variable
= home_dir
[0] != '\0';
2996 home_dir
= getenv ("HOME");
2998 #if !defined(_AMIGA) && !defined(WINDOWS32)
2999 if (home_dir
== 0 || home_dir
[0] == '\0')
3001 extern char *getlogin ();
3002 char *logname
= getlogin ();
3006 struct passwd
*p
= getpwnam (logname
);
3008 home_dir
= p
->pw_dir
;
3011 #endif /* !AMIGA && !WINDOWS32 */
3014 char *new = concat (home_dir
, "", name
+ 1);
3020 #if !defined(_AMIGA) && !defined(WINDOWS32)
3023 struct passwd
*pwent
;
3024 char *userend
= strchr (name
+ 1, '/');
3027 pwent
= getpwnam (name
+ 1);
3031 return xstrdup (pwent
->pw_dir
);
3033 return concat (pwent
->pw_dir
, "/", userend
+ 1);
3035 else if (userend
!= 0)
3038 #endif /* !AMIGA && !WINDOWS32 */
3043 /* Given a chain of struct nameseq's describing a sequence of filenames,
3044 in reverse of the intended order, return a new chain describing the
3045 result of globbing the filenames. The new chain is in forward order.
3046 The links of the old chain are freed or used in the new chain.
3047 Likewise for the names in the old chain.
3049 SIZE is how big to construct chain elements.
3050 This is useful if we want them actually to be other structures
3051 that have room for additional info. */
3054 multi_glob (struct nameseq
*chain
, unsigned int size
)
3056 extern void dir_setup_glob ();
3057 register struct nameseq
*new = 0;
3058 register struct nameseq
*old
;
3059 struct nameseq
*nexto
;
3062 dir_setup_glob (&gl
);
3064 for (old
= chain
; old
!= 0; old
= nexto
)
3072 if (old
->name
[0] == '~')
3074 char *newname
= tilde_expand (old
->name
);
3078 old
->name
= newname
;
3083 if (ar_name (old
->name
))
3085 /* OLD->name is an archive member reference.
3086 Replace it with the archive file name,
3087 and save the member name in MEMNAME.
3088 We will glob on the archive name and then
3089 reattach MEMNAME later. */
3091 ar_parse_name (old
->name
, &arname
, &memname
);
3097 #endif /* !NO_ARCHIVES */
3099 switch (glob (old
->name
, GLOB_NOCHECK
|GLOB_ALTDIRFUNC
, NULL
, &gl
))
3101 case 0: /* Success. */
3103 register int i
= gl
.gl_pathc
;
3109 /* Try to glob on MEMNAME within the archive. */
3110 struct nameseq
*found
3111 = ar_glob (gl
.gl_pathv
[i
], memname
, size
);
3114 /* No matches. Use MEMNAME as-is. */
3115 unsigned int alen
= strlen (gl
.gl_pathv
[i
]);
3116 unsigned int mlen
= strlen (memname
);
3118 = (struct nameseq
*) xmalloc (size
);
3119 if (size
> sizeof (struct nameseq
))
3120 bzero (((char *) elt
) + sizeof (struct nameseq
),
3121 size
- sizeof (struct nameseq
));
3122 elt
->name
= (char *) xmalloc (alen
+ 1 + mlen
+ 2);
3123 bcopy (gl
.gl_pathv
[i
], elt
->name
, alen
);
3124 elt
->name
[alen
] = '(';
3125 bcopy (memname
, &elt
->name
[alen
+ 1], mlen
);
3126 elt
->name
[alen
+ 1 + mlen
] = ')';
3127 elt
->name
[alen
+ 1 + mlen
+ 1] = '\0';
3133 /* Find the end of the FOUND chain. */
3134 struct nameseq
*f
= found
;
3135 while (f
->next
!= 0)
3138 /* Attach the chain being built to the end of the FOUND
3139 chain, and make FOUND the new NEW chain. */
3147 #endif /* !NO_ARCHIVES */
3149 struct nameseq
*elt
= (struct nameseq
*) xmalloc (size
);
3150 if (size
> sizeof (struct nameseq
))
3151 bzero (((char *) elt
) + sizeof (struct nameseq
),
3152 size
- sizeof (struct nameseq
));
3153 elt
->name
= xstrdup (gl
.gl_pathv
[i
]);
3165 fatal (NILF
, _("virtual memory exhausted"));