1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
39 struct passwd
*getpwnam
PARAMS ((char *name
));
42 #endif /* !WINDOWS32 */
44 /* A `struct linebuffer' is a structure which holds a line of text.
45 `readline' reads a line from a stream into a linebuffer
46 and works regardless of the length of the line. */
50 /* Note: This is the number of bytes malloc'ed for `buffer'
51 It does not indicate `buffer's real length.
52 Instead, a null char indicates end-of-string. */
57 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
58 #define freebuffer(lb) free ((lb)->buffer)
61 /* Types of "words" that can be read in a makefile. */
64 w_bogus
, w_eol
, w_static
, w_variable
, w_colon
, w_dcolon
, w_semicolon
,
65 w_comment
, w_varassign
69 /* A `struct conditionals' contains the information describing
70 all the active conditionals in a makefile.
72 The global variable `conditionals' contains the conditionals
73 information for the current makefile. It is initialized from
74 the static structure `toplevel_conditionals' and is later changed
75 to new structures for included makefiles. */
79 unsigned int if_cmds
; /* Depth of conditional nesting. */
80 unsigned int allocated
; /* Elts allocated in following arrays. */
81 char *ignoring
; /* Are we ignoring or interepreting? */
82 char *seen_else
; /* Have we already seen an `else'? */
85 static struct conditionals toplevel_conditionals
;
86 static struct conditionals
*conditionals
= &toplevel_conditionals
;
89 /* Default directories to search for include files in */
91 static char *default_include_directories
[] =
93 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
95 * This completly up to the user when they install MSVC or other packages.
96 * This is defined as a placeholder.
98 #define INCLUDEDIR "."
103 "/usr/local/include",
109 /* List of directories to search for include files in */
111 static char **include_directories
;
113 /* Maximum length of an element of the above. */
115 static unsigned int max_incl_len
;
117 /* The filename and pointer to line number of the
118 makefile currently being read in. */
120 const struct floc
*reading_file
;
122 /* The chain of makefiles read by read_makefile. */
124 static struct dep
*read_makefiles
= 0;
126 static int read_makefile
PARAMS ((char *filename
, int flags
));
127 static unsigned long readline
PARAMS ((struct linebuffer
*linebuffer
,
128 FILE *stream
, const struct floc
*flocp
));
129 static void do_define
PARAMS ((char *name
, unsigned int namelen
,
130 enum variable_origin origin
, FILE *infile
,
131 struct floc
*flocp
));
132 static int conditional_line
PARAMS ((char *line
, const struct floc
*flocp
));
133 static void record_files
PARAMS ((struct nameseq
*filenames
, char *pattern
, char *pattern_percent
,
134 struct dep
*deps
, unsigned int cmds_started
, char *commands
,
135 unsigned int commands_idx
, int two_colon
,
136 const struct floc
*flocp
, int set_default
));
137 static void record_target_var
PARAMS ((struct nameseq
*filenames
, char *defn
,
139 enum variable_origin origin
,
140 const struct floc
*flocp
));
141 static enum make_word_type get_next_mword
PARAMS ((char *buffer
, char *delim
,
142 char **startp
, unsigned int *length
));
144 /* Read in all the makefiles and return the chain of their names. */
147 read_all_makefiles (makefiles
)
150 unsigned int num_makefiles
= 0;
153 puts (_("Reading makefiles..."));
155 /* If there's a non-null variable MAKEFILES, its value is a list of
156 files to read first thing. But don't let it prevent reading the
157 default makefiles and don't let the default goal come from there. */
165 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
166 int save
= warn_undefined_variables_flag
;
167 warn_undefined_variables_flag
= 0;
169 value
= allocated_variable_expand ("$(MAKEFILES)");
171 warn_undefined_variables_flag
= save
;
174 /* Set NAME to the start of next token and LENGTH to its length.
175 MAKEFILES is updated for finding remaining tokens. */
178 while ((name
= find_next_token (&p
, &length
)) != 0)
180 name
= xstrdup (name
);
183 if (read_makefile (name
,
184 RM_NO_DEFAULT_GOAL
|RM_INCLUDED
|RM_DONTCARE
) < 2)
191 /* Read makefiles specified with -f switches. */
194 while (*makefiles
!= 0)
196 struct dep
*tail
= read_makefiles
;
197 register struct dep
*d
;
199 if (! read_makefile (*makefiles
, 0))
200 perror_with_name ("", *makefiles
);
202 /* Find the right element of read_makefiles. */
204 while (d
->next
!= tail
)
207 /* Use the storage read_makefile allocates. */
208 *makefiles
= dep_name (d
);
213 /* If there were no -f switches, try the default names. */
215 if (num_makefiles
== 0)
217 static char *default_makefiles
[] =
219 /* all lower case since readdir() (the vms version) 'lowercasifies' */
220 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
223 { "GNUmakefile", "Makefile", "SMakefile", 0 };
224 #else /* !Amiga && !VMS */
225 { "GNUmakefile", "makefile", "Makefile", 0 };
228 register char **p
= default_makefiles
;
229 while (*p
!= 0 && !file_exists_p (*p
))
234 if (! read_makefile (*p
, 0))
235 perror_with_name ("", *p
);
239 /* No default makefile was found. Add the default makefiles to the
240 `read_makefiles' chain so they will be updated if possible. */
241 struct dep
*tail
= read_makefiles
;
242 /* Add them to the tail, after any MAKEFILES variable makefiles. */
243 while (tail
!= 0 && tail
->next
!= 0)
245 for (p
= default_makefiles
; *p
!= 0; ++p
)
247 struct dep
*d
= (struct dep
*) xmalloc (sizeof (struct dep
));
249 d
->file
= enter_file (*p
);
250 d
->file
->dontcare
= 1;
251 /* Tell update_goal_chain to bail out as soon as this file is
252 made, and main not to die if we can't make this file. */
253 d
->changed
= RM_DONTCARE
;
265 return read_makefiles
;
268 /* Read file FILENAME as a makefile and add its contents to the data base.
270 FLAGS contains bits as above.
272 FILENAME is added to the `read_makefiles' chain.
274 Returns 0 if a file was not found or not read.
275 Returns 1 if FILENAME was found and read.
276 Returns 2 if FILENAME was read, and we kept a reference (don't free it). */
279 read_makefile (filename
, flags
)
283 static char *collapsed
= 0;
284 static unsigned int collapsed_length
= 0;
285 register FILE *infile
;
286 struct linebuffer lb
;
287 unsigned int commands_len
= 200;
289 unsigned int commands_idx
= 0;
290 unsigned int cmds_started
;
293 int len
, reading_target
;
294 int ignoring
= 0, in_ignored_define
= 0;
295 int no_targets
= 0; /* Set when reading a rule without targets. */
296 int using_filename
= 0;
297 struct floc fileinfo
;
298 char *passed_filename
= filename
;
300 struct nameseq
*filenames
= 0;
302 unsigned int nlines
= 0;
304 char *pattern
= 0, *pattern_percent
;
307 #if defined (WINDOWS32) || defined (__MSDOS__)
311 #define record_waiting_files() \
314 if (filenames != 0) \
316 record_files (filenames, pattern, pattern_percent, deps, \
317 cmds_started, commands, commands_idx, two_colon, \
318 &fileinfo, !(flags & RM_NO_DEFAULT_GOAL)); \
319 using_filename |= commands_idx > 0; \
323 if (pattern) { free(pattern); pattern = 0; } \
326 fileinfo
.filenm
= filename
;
330 cmds_started
= fileinfo
.lineno
;
334 printf (_("Reading makefile `%s'"), fileinfo
.filenm
);
335 if (flags
& RM_NO_DEFAULT_GOAL
)
336 printf (_(" (no default goal)"));
337 if (flags
& RM_INCLUDED
)
338 printf (_(" (search path)"));
339 if (flags
& RM_DONTCARE
)
340 printf (_(" (don't care)"));
341 if (flags
& RM_NO_TILDE
)
342 printf (_(" (no ~ expansion)"));
346 /* First, get a stream to read. */
348 /* Expand ~ in FILENAME unless it came from `include',
349 in which case it was already done. */
350 if (!(flags
& RM_NO_TILDE
) && filename
[0] == '~')
352 char *expanded
= tilde_expand (filename
);
357 infile
= fopen (filename
, "r");
358 /* Save the error code so we print the right message later. */
359 makefile_errno
= errno
;
361 /* If the makefile wasn't found and it's either a makefile from
362 the `MAKEFILES' variable or an included makefile,
363 search the included makefile search path for this makefile. */
364 if (infile
== 0 && (flags
& RM_INCLUDED
) && *filename
!= '/')
366 register unsigned int i
;
367 for (i
= 0; include_directories
[i
] != 0; ++i
)
369 char *name
= concat (include_directories
[i
], "/", filename
);
370 infile
= fopen (name
, "r");
381 /* Add FILENAME to the chain of read makefiles. */
382 deps
= (struct dep
*) xmalloc (sizeof (struct dep
));
383 deps
->next
= read_makefiles
;
384 read_makefiles
= deps
;
386 deps
->file
= lookup_file (filename
);
389 deps
->file
= enter_file (xstrdup (filename
));
390 if (flags
& RM_DONTCARE
)
391 deps
->file
->dontcare
= 1;
393 if (filename
!= passed_filename
)
395 filename
= deps
->file
->name
;
396 deps
->changed
= flags
;
399 /* If the makefile can't be found at all, give up entirely. */
403 /* If we did some searching, errno has the error from the last
404 attempt, rather from FILENAME itself. Restore it in case the
405 caller wants to use it in a message. */
406 errno
= makefile_errno
;
410 reading_file
= &fileinfo
;
412 /* Loop over lines in the file.
413 The strategy is to accumulate target names in FILENAMES, dependencies
414 in DEPS and commands in COMMANDS. These are used to define a rule
415 when the start of the next rule (or eof) is encountered. */
418 commands
= xmalloc (200);
420 while (!feof (infile
))
422 fileinfo
.lineno
+= nlines
;
423 nlines
= readline (&lb
, infile
, &fileinfo
);
425 /* Check for a shell command line first.
426 If it is not one, we can stop treating tab specially. */
427 if (lb
.buffer
[0] == '\t')
429 /* This line is a probably shell command. */
433 /* Ignore the commands in a rule with no targets. */
436 /* If there is no preceding rule line, don't treat this line
437 as a command, even though it begins with a tab character.
438 SunOS 4 make appears to behave this way. */
443 /* Yep, this is a shell command, and we don't care. */
446 /* Append this command line to the line being accumulated. */
448 if (commands_idx
== 0)
449 cmds_started
= fileinfo
.lineno
;
451 if (len
+ 1 + commands_idx
> commands_len
)
453 commands_len
= (len
+ 1 + commands_idx
) * 2;
454 commands
= (char *) xrealloc (commands
, commands_len
);
456 bcopy (p
, &commands
[commands_idx
], len
);
458 commands
[commands_idx
++] = '\n';
464 /* This line is not a shell command line. Don't worry about tabs. */
466 if (collapsed_length
< lb
.size
)
468 collapsed_length
= lb
.size
;
471 collapsed
= (char *) xmalloc (collapsed_length
);
473 strcpy (collapsed
, lb
.buffer
);
474 /* Collapse continuation lines. */
475 collapse_continuations (collapsed
);
476 remove_comments (collapsed
);
478 /* Compare a word, both length and contents. */
479 #define word1eq(s, l) (len == l && strneq (s, p, l))
484 /* This line is completely empty. */
487 /* Find the end of the first token. Note we don't need to worry about
488 * ":" here since we compare tokens by length (so "export" will never
489 * be equal to "export:").
491 for (p2
= p
+1; *p2
!= '\0' && !isspace(*p2
); ++p2
)
495 /* Find the start of the second token. If it's a `:' remember it,
496 since it can't be a preprocessor token--this allows targets named
497 `ifdef', `export', etc. */
499 while (isspace (*p2
))
503 else if (p2
[0] == ':' && p2
[1] == '\0')
506 goto skip_conditionals
;
509 /* We must first check for conditional and `define' directives before
510 ignoring anything, since they control what we will do with
513 if (!in_ignored_define
514 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
515 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
516 || word1eq ("else", 4) || word1eq ("endif", 5)))
518 int i
= conditional_line (p
, &fileinfo
);
522 fatal (&fileinfo
, _("invalid syntax in conditional"));
526 if (word1eq ("endef", 5))
528 if (in_ignored_define
)
529 in_ignored_define
= 0;
531 fatal (&fileinfo
, _("extraneous `endef'"));
535 if (word1eq ("define", 6))
538 in_ignored_define
= 1;
541 p2
= next_token (p
+ 6);
543 fatal (&fileinfo
, _("empty variable name"));
545 /* Let the variable name be the whole rest of the line,
546 with trailing blanks stripped (comments have already been
547 removed), so it could be a complex variable/function
548 reference that might contain blanks. */
549 p
= index (p2
, '\0');
550 while (isblank (p
[-1]))
552 do_define (p2
, p
- p2
, o_file
, infile
, &fileinfo
);
557 if (word1eq ("override", 8))
559 p2
= next_token (p
+ 8);
561 error (&fileinfo
, _("empty `override' directive"));
562 if (strneq (p2
, "define", 6) && (isblank (p2
[6]) || p2
[6] == '\0'))
565 in_ignored_define
= 1;
568 p2
= next_token (p2
+ 6);
570 fatal (&fileinfo
, _("empty variable name"));
572 /* Let the variable name be the whole rest of the line,
573 with trailing blanks stripped (comments have already been
574 removed), so it could be a complex variable/function
575 reference that might contain blanks. */
576 p
= index (p2
, '\0');
577 while (isblank (p
[-1]))
579 do_define (p2
, p
- p2
, o_override
, infile
, &fileinfo
);
583 && !try_variable_definition (&fileinfo
, p2
, o_override
))
584 error (&fileinfo
, _("invalid `override' directive"));
591 /* Ignore the line. We continue here so conditionals
592 can appear in the middle of a rule. */
595 if (!reading_target
&& word1eq ("export", 6))
598 p2
= next_token (p
+ 6);
600 export_all_variables
= 1;
601 v
= try_variable_definition (&fileinfo
, p2
, o_file
);
603 v
->export
= v_export
;
607 for (p
= find_next_token (&p2
, &len
); p
!= 0;
608 p
= find_next_token (&p2
, &len
))
610 v
= lookup_variable (p
, len
);
612 v
= define_variable (p
, len
, "", o_file
, 0);
613 v
->export
= v_export
;
617 else if (!reading_target
&& word1eq ("unexport", 8))
621 p2
= next_token (p
+ 8);
623 export_all_variables
= 0;
624 for (p
= find_next_token (&p2
, &len
); p
!= 0;
625 p
= find_next_token (&p2
, &len
))
627 v
= lookup_variable (p
, len
);
629 v
= define_variable (p
, len
, "", o_file
, 0);
630 v
->export
= v_noexport
;
633 else if (word1eq ("vpath", 5))
637 p2
= variable_expand (p
+ 5);
638 p
= find_next_token (&p2
, &len
);
641 pattern
= savestring (p
, len
);
642 p
= find_next_token (&p2
, &len
);
643 /* No searchpath means remove all previous
644 selective VPATH's with the same pattern. */
647 /* No pattern means remove all previous selective VPATH's. */
649 construct_vpath_list (pattern
, p
);
653 else if (word1eq ("include", 7) || word1eq ("-include", 8)
654 || word1eq ("sinclude", 8))
656 /* We have found an `include' line specifying a nested
657 makefile to be read at this point. */
658 struct conditionals
*save
, new_conditionals
;
659 struct nameseq
*files
;
660 /* "-include" (vs "include") says no error if the file does not
661 exist. "sinclude" is an alias for this from SGI. */
662 int noerror
= p
[0] != 'i';
664 p
= allocated_variable_expand (next_token (p
+ (noerror
? 8 : 7)));
668 _("no file name for `%sinclude'"), noerror
? "-" : "");
672 /* Parse the list of file names. */
674 files
= multi_glob (parse_file_seq (&p2
, '\0',
675 sizeof (struct nameseq
),
677 sizeof (struct nameseq
));
680 /* Save the state of conditionals and start
681 the included makefile with a clean slate. */
683 bzero ((char *) &new_conditionals
, sizeof new_conditionals
);
684 conditionals
= &new_conditionals
;
686 /* Record the rules that are waiting so they will determine
687 the default goal before those in the included makefile. */
688 record_waiting_files ();
690 /* Read each included makefile. */
693 struct nameseq
*next
= files
->next
;
694 char *name
= files
->name
;
697 free ((char *)files
);
700 r
= read_makefile (name
, (RM_INCLUDED
| RM_NO_TILDE
701 | (noerror
? RM_DONTCARE
: 0)));
703 error (&fileinfo
, "%s: %s", name
, strerror (errno
));
709 /* Free any space allocated by conditional_line. */
710 if (conditionals
->ignoring
)
711 free (conditionals
->ignoring
);
712 if (conditionals
->seen_else
)
713 free (conditionals
->seen_else
);
717 reading_file
= &fileinfo
;
720 else if (try_variable_definition (&fileinfo
, p
, o_file
))
721 /* This line has been dealt with. */
723 else if (lb
.buffer
[0] == '\t')
725 p
= collapsed
; /* Ignore comments. */
729 /* The line is completely blank; that is harmless. */
731 /* This line starts with a tab but was not caught above
732 because there was no preceding target, and the line
733 might have been usable as a variable definition.
734 But now it is definitely lossage. */
735 fatal(&fileinfo
, _("commands commence before first target"));
739 /* This line describes some target files. This is complicated by
740 the existence of target-specific variables, because we can't
741 expand the entire line until we know if we have one or not. So
742 we expand the line word by word until we find the first `:',
743 then check to see if it's a target-specific variable.
745 In this algorithm, `lb_next' will point to the beginning of the
746 unexpanded parts of the input buffer, while `p2' points to the
747 parts of the expanded buffer we haven't searched yet. */
749 enum make_word_type wtype
;
750 enum variable_origin v_origin
;
751 char *cmdleft
, *lb_next
;
752 unsigned int len
, plen
= 0;
755 /* Record the previous rule. */
757 record_waiting_files ();
759 /* Search the line for an unquoted ; that is not after an
761 cmdleft
= find_char_unquote (lb
.buffer
, ";#", 0);
762 if (cmdleft
!= 0 && *cmdleft
== '#')
764 /* We found a comment before a semicolon. */
768 else if (cmdleft
!= 0)
769 /* Found one. Cut the line short there before expanding it. */
772 collapse_continuations (lb
.buffer
);
774 /* We can't expand the entire line, since if it's a per-target
775 variable we don't want to expand it. So, walk from the
776 beginning, expanding as we go, and looking for "interesting"
777 chars. The first word is always expandable. */
778 wtype
= get_next_mword(lb
.buffer
, NULL
, &lb_next
, &len
);
783 fatal(&fileinfo
, _("missing rule before commands"));
784 /* This line contained something but turned out to be nothing
785 but whitespace (a comment?). */
790 /* We accept and ignore rules without targets for
791 compatibility with SunOS 4 make. */
799 p2
= variable_expand_string(NULL
, lb_next
, len
);
805 /* Look for a semicolon in the expanded line. */
806 cmdleft
= find_char_unquote (p2
, ";", 0);
810 unsigned long p2_off
= p2
- variable_buffer
;
811 unsigned long cmd_off
= cmdleft
- variable_buffer
;
812 char *pend
= p2
+ strlen(p2
);
814 /* Append any remnants of lb, then cut the line short
818 /* One school of thought says that you shouldn't expand
819 here, but merely copy, since now you're beyond a ";"
820 and into a command script. However, the old parser
821 expanded the whole line, so we continue that for
822 backwards-compatiblity. Also, it wouldn't be
823 entirely consistent, since we do an unconditional
824 expand below once we know we don't have a
825 target-specific variable. */
826 (void)variable_expand_string(pend
, lb_next
, (long)-1);
827 lb_next
+= strlen(lb_next
);
828 p2
= variable_buffer
+ p2_off
;
829 cmdleft
= variable_buffer
+ cmd_off
+ 1;
833 colonp
= find_char_unquote(p2
, ":", 0);
834 #if defined(__MSDOS__) || defined(WINDOWS32)
835 /* The drive spec brain-damage strikes again... */
836 /* Note that the only separators of targets in this context
837 are whitespace and a left paren. If others are possible,
838 they should be added to the string in the call to index. */
839 while (colonp
&& (colonp
[1] == '/' || colonp
[1] == '\\') &&
840 colonp
> p2
&& isalpha(colonp
[-1]) &&
841 (colonp
== p2
+ 1 || index(" \t(", colonp
[-2]) != 0))
842 colonp
= find_char_unquote(colonp
+ 1, ":", 0);
847 wtype
= get_next_mword(lb_next
, NULL
, &lb_next
, &len
);
853 p2
= variable_expand_string(p2
, lb_next
, len
);
854 /* We don't need to worry about cmdleft here, because if it was
855 found in the variable_buffer the entire buffer has already
856 been expanded... we'll never get here. */
859 p2
= next_token (variable_buffer
);
861 /* If the word we're looking at is EOL, see if there's _anything_
862 on the line. If not, a variable expanded to nothing, so ignore
863 it. If so, we can't parse this line so punt. */
867 /* There's no need to be ivory-tower about this: check for
868 one of the most common bugs found in makefiles... */
869 fatal (&fileinfo
, _("missing separator%s"),
870 !strneq(lb
.buffer
, " ", 8) ? ""
871 : _(" (did you mean TAB instead of 8 spaces?)"));
875 /* Make the colon the end-of-string so we know where to stop
876 looking for targets. */
878 filenames
= multi_glob (parse_file_seq (&p2
, '\0',
879 sizeof (struct nameseq
),
881 sizeof (struct nameseq
));
886 /* We accept and ignore rules without targets for
887 compatibility with SunOS 4 make. */
891 /* This should never be possible; we handled it above. */
892 assert (*p2
!= '\0');
895 /* Is this a one-colon or two-colon entry? */
896 two_colon
= *p2
== ':';
900 /* Test to see if it's a target-specific variable. Copy the rest
901 of the buffer over, possibly temporarily (we'll expand it later
902 if it's not a target-specific variable). PLEN saves the length
903 of the unparsed section of p2, for later. */
904 if (*lb_next
!= '\0')
906 unsigned int l
= p2
- variable_buffer
;
908 (void) variable_buffer_output (p2
+plen
,
909 lb_next
, strlen (lb_next
)+1);
910 p2
= variable_buffer
+ l
;
913 /* See if it's an "override" keyword; if so see if what comes after
914 it looks like a variable definition. */
916 wtype
= get_next_mword (p2
, NULL
, &p
, &len
);
919 if (wtype
== w_static
&& (len
== (sizeof ("override")-1)
920 && strneq (p
, "override", len
)))
922 v_origin
= o_override
;
923 wtype
= get_next_mword (p
+len
, NULL
, &p
, &len
);
927 wtype
= get_next_mword (p
+len
, NULL
, NULL
, NULL
);
929 if (wtype
== w_varassign
)
931 record_target_var (filenames
, p
, two_colon
, v_origin
, &fileinfo
);
936 /* This is a normal target, _not_ a target-specific variable.
937 Unquote any = in the dependency list. */
938 find_char_unquote (lb_next
, "=", 0);
940 /* We have some targets, so don't ignore the following commands. */
943 /* Expand the dependencies, etc. */
944 if (*lb_next
!= '\0')
946 unsigned int l
= p2
- variable_buffer
;
947 (void) variable_expand_string (p2
+ plen
, lb_next
, (long)-1);
948 p2
= variable_buffer
+ l
;
950 /* Look for a semicolon in the expanded line. */
953 cmdleft
= find_char_unquote (p2
, ";", 0);
959 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
961 while (p
!= 0 && p
[-1] == '\\')
963 register char *q
= &p
[-1];
964 register int backslash
= 0;
966 backslash
= !backslash
;
968 p
= index (p
+ 1, ':');
973 /* Here, the situation is quite complicated. Let's have a look
974 at a couple of targets:
982 The rule is that it's only a target, if there are TWO :'s
983 OR a space around the :.
985 if (p
&& !(isspace(p
[1]) || !p
[1] || isspace(p
[-1])))
988 #if defined (WINDOWS32) || defined (__MSDOS__)
991 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
992 if (p
!= 0 && (p
[1] == '\\' || p
[1] == '/') &&
994 (p
== p2
+ 1 || index(" \t:(", p
[-2]) != 0)) {
995 p
= index(p
+ 1, ':');
998 } while (check_again
);
1002 struct nameseq
*target
;
1003 target
= parse_file_seq (&p2
, ':', sizeof (struct nameseq
), 1);
1006 fatal (&fileinfo
, _("missing target pattern"));
1007 else if (target
->next
!= 0)
1008 fatal (&fileinfo
, _("multiple target patterns"));
1009 pattern
= target
->name
;
1010 pattern_percent
= find_percent (pattern
);
1011 if (pattern_percent
== 0)
1012 fatal (&fileinfo
, _("target pattern contains no `%%'"));
1013 free((char *)target
);
1018 /* Parse the dependencies. */
1019 deps
= (struct dep
*)
1020 multi_glob (parse_file_seq (&p2
, '\0', sizeof (struct dep
), 1),
1021 sizeof (struct dep
));
1026 /* Semicolon means rest of line is a command. */
1027 unsigned int len
= strlen (cmdleft
);
1029 cmds_started
= fileinfo
.lineno
;
1031 /* Add this command line to the buffer. */
1032 if (len
+ 2 > commands_len
)
1034 commands_len
= (len
+ 2) * 2;
1035 commands
= (char *) xrealloc (commands
, commands_len
);
1037 bcopy (cmdleft
, commands
, len
);
1038 commands_idx
+= len
;
1039 commands
[commands_idx
++] = '\n';
1045 /* We get here except in the case that we just read a rule line.
1046 Record now the last rule we read, so following spurious
1047 commands are properly diagnosed. */
1048 record_waiting_files ();
1052 if (conditionals
->if_cmds
)
1053 fatal (&fileinfo
, _("missing `endif'"));
1055 /* At eof, record the last rule. */
1056 record_waiting_files ();
1059 free ((char *) commands
);
1064 return 1+using_filename
;
1067 /* Execute a `define' directive.
1068 The first line has already been read, and NAME is the name of
1069 the variable to be defined. The following lines remain to be read.
1070 LINENO, INFILE and FILENAME refer to the makefile being read.
1071 The value returned is LINENO, updated for lines read here. */
1074 do_define (name
, namelen
, origin
, infile
, flocp
)
1076 unsigned int namelen
;
1077 enum variable_origin origin
;
1081 struct linebuffer lb
;
1082 unsigned int nlines
= 0;
1083 unsigned int length
= 100;
1084 char *definition
= (char *) xmalloc (100);
1085 register unsigned int idx
= 0;
1088 /* Expand the variable name. */
1089 char *var
= (char *) alloca (namelen
+ 1);
1090 bcopy (name
, var
, namelen
);
1091 var
[namelen
] = '\0';
1092 var
= variable_expand (var
);
1095 while (!feof (infile
))
1099 flocp
->lineno
+= nlines
;
1100 nlines
= readline (&lb
, infile
, flocp
);
1102 collapse_continuations (lb
.buffer
);
1104 p
= next_token (lb
.buffer
);
1106 if ((len
== 5 || (len
> 5 && isblank (p
[5])))
1107 && strneq (p
, "endef", 5))
1110 remove_comments (p
);
1111 if (*next_token (p
) != '\0')
1112 error (flocp
, _("Extraneous text after `endef' directive"));
1113 /* Define the variable. */
1115 definition
[0] = '\0';
1117 definition
[idx
- 1] = '\0';
1118 (void) define_variable (var
, strlen (var
), definition
, origin
, 1);
1125 len
= strlen (lb
.buffer
);
1126 /* Increase the buffer size if necessary. */
1127 if (idx
+ len
+ 1 > length
)
1129 length
= (idx
+ len
) * 2;
1130 definition
= (char *) xrealloc (definition
, length
+ 1);
1133 bcopy (lb
.buffer
, &definition
[idx
], len
);
1135 /* Separate lines with a newline. */
1136 definition
[idx
++] = '\n';
1141 fatal (flocp
, _("missing `endef', unterminated `define'"));
1147 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1148 "ifneq", "else" and "endif".
1149 LINE is the input line, with the command as its first word.
1151 FILENAME and LINENO are the filename and line number in the
1152 current makefile. They are used for error messages.
1154 Value is -1 if the line is invalid,
1155 0 if following text should be interpreted,
1156 1 if following text should be ignored. */
1159 conditional_line (line
, flocp
)
1161 const struct floc
*flocp
;
1165 register unsigned int i
;
1169 /* It's an "if..." command. */
1170 notdef
= line
[2] == 'n';
1173 cmdname
= line
[3] == 'd' ? "ifndef" : "ifneq";
1174 line
+= cmdname
[3] == 'd' ? 7 : 6;
1178 cmdname
= line
[2] == 'd' ? "ifdef" : "ifeq";
1179 line
+= cmdname
[2] == 'd' ? 6 : 5;
1184 /* It's an "else" or "endif" command. */
1185 notdef
= line
[1] == 'n';
1186 cmdname
= notdef
? "endif" : "else";
1187 line
+= notdef
? 5 : 4;
1190 line
= next_token (line
);
1192 if (*cmdname
== 'e')
1195 error (flocp
, _("Extraneous text after `%s' directive"), cmdname
);
1196 /* "Else" or "endif". */
1197 if (conditionals
->if_cmds
== 0)
1198 fatal (flocp
, _("extraneous `%s'"), cmdname
);
1199 /* NOTDEF indicates an `endif' command. */
1201 --conditionals
->if_cmds
;
1202 else if (conditionals
->seen_else
[conditionals
->if_cmds
- 1])
1203 fatal (flocp
, _("only one `else' per conditional"));
1206 /* Toggle the state of ignorance. */
1207 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1208 = !conditionals
->ignoring
[conditionals
->if_cmds
- 1];
1209 /* Record that we have seen an `else' in this conditional.
1210 A second `else' will be erroneous. */
1211 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 1;
1213 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1214 if (conditionals
->ignoring
[i
])
1219 if (conditionals
->allocated
== 0)
1221 conditionals
->allocated
= 5;
1222 conditionals
->ignoring
= (char *) xmalloc (conditionals
->allocated
);
1223 conditionals
->seen_else
= (char *) xmalloc (conditionals
->allocated
);
1226 ++conditionals
->if_cmds
;
1227 if (conditionals
->if_cmds
> conditionals
->allocated
)
1229 conditionals
->allocated
+= 5;
1230 conditionals
->ignoring
= (char *)
1231 xrealloc (conditionals
->ignoring
, conditionals
->allocated
);
1232 conditionals
->seen_else
= (char *)
1233 xrealloc (conditionals
->seen_else
, conditionals
->allocated
);
1236 /* Record that we have seen an `if...' but no `else' so far. */
1237 conditionals
->seen_else
[conditionals
->if_cmds
- 1] = 0;
1239 /* Search through the stack to see if we're already ignoring. */
1240 for (i
= 0; i
< conditionals
->if_cmds
- 1; ++i
)
1241 if (conditionals
->ignoring
[i
])
1243 /* We are already ignoring, so just push a level
1244 to match the next "else" or "endif", and keep ignoring.
1245 We don't want to expand variables in the condition. */
1246 conditionals
->ignoring
[conditionals
->if_cmds
- 1] = 1;
1250 if (cmdname
[notdef
? 3 : 2] == 'd')
1252 /* "Ifdef" or "ifndef". */
1254 register char *p
= end_of_token (line
);
1259 v
= lookup_variable (line
, i
);
1260 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1261 = (v
!= 0 && *v
->value
!= '\0') == notdef
;
1265 /* "Ifeq" or "ifneq". */
1268 char termin
= *line
== '(' ? ',' : *line
;
1270 if (termin
!= ',' && termin
!= '"' && termin
!= '\'')
1274 /* Find the end of the first string. */
1277 register int count
= 0;
1278 for (; *line
!= '\0'; ++line
)
1281 else if (*line
== ')')
1283 else if (*line
== ',' && count
<= 0)
1287 while (*line
!= '\0' && *line
!= termin
)
1295 /* Strip blanks after the first string. */
1297 while (isblank (p
[-1]))
1304 s2
= variable_expand (s1
);
1305 /* We must allocate a new copy of the expanded string because
1306 variable_expand re-uses the same buffer. */
1308 s1
= (char *) alloca (len
+ 1);
1309 bcopy (s2
, s1
, len
+ 1);
1312 /* Find the start of the second string. */
1313 line
= next_token (line
);
1315 termin
= termin
== ',' ? ')' : *line
;
1316 if (termin
!= ')' && termin
!= '"' && termin
!= '\'')
1319 /* Find the end of the second string. */
1322 register int count
= 0;
1323 s2
= next_token (line
);
1324 for (line
= s2
; *line
!= '\0'; ++line
)
1328 else if (*line
== ')')
1341 while (*line
!= '\0' && *line
!= termin
)
1349 line
= next_token (++line
);
1351 error (flocp
, _("Extraneous text after `%s' directive"), cmdname
);
1353 s2
= variable_expand (s2
);
1354 conditionals
->ignoring
[conditionals
->if_cmds
- 1]
1355 = streq (s1
, s2
) == notdef
;
1358 /* Search through the stack to see if we're ignoring. */
1359 for (i
= 0; i
< conditionals
->if_cmds
; ++i
)
1360 if (conditionals
->ignoring
[i
])
1365 /* Remove duplicate dependencies in CHAIN. */
1368 uniquize_deps (chain
)
1371 register struct dep
*d
;
1373 /* Make sure that no dependencies are repeated. This does not
1374 really matter for the purpose of updating targets, but it
1375 might make some names be listed twice for $^ and $?. */
1377 for (d
= chain
; d
!= 0; d
= d
->next
)
1379 struct dep
*last
, *next
;
1384 if (streq (dep_name (d
), dep_name (next
)))
1386 struct dep
*n
= next
->next
;
1388 if (next
->name
!= 0 && next
->name
!= d
->name
)
1391 free ((char *) next
);
1402 /* Record target-specific variable values for files FILENAMES.
1403 TWO_COLON is nonzero if a double colon was used.
1405 The links of FILENAMES are freed, and so are any names in it
1406 that are not incorporated into other data structures.
1408 If the target is a pattern, add the variable to the pattern-specific
1409 variable value list. */
1412 record_target_var (filenames
, defn
, two_colon
, origin
, flocp
)
1413 struct nameseq
*filenames
;
1416 enum variable_origin origin
;
1417 const struct floc
*flocp
;
1419 struct nameseq
*nextf
;
1420 struct variable_set_list
*global
;
1422 global
= current_variable_set_list
;
1424 for (; filenames
!= 0; filenames
= nextf
)
1427 register char *name
= filenames
->name
;
1428 struct variable_set_list
*vlist
;
1432 nextf
= filenames
->next
;
1433 free ((char *) filenames
);
1435 /* If it's a pattern target, then add it to the pattern-specific
1437 percent
= find_percent (name
);
1440 struct pattern_var
*p
;
1442 /* Get a reference for this pattern-specific variable struct. */
1443 p
= create_pattern_var(name
, percent
);
1451 /* Get a file reference for this file, and initialize it. */
1452 f
= enter_file (name
);
1453 initialize_file_variables (f
);
1454 vlist
= f
->variables
;
1458 /* Make the new variable context current and define the variable. */
1459 current_variable_set_list
= vlist
;
1460 v
= try_variable_definition (flocp
, defn
, origin
);
1462 error (flocp
, _("Malformed per-target variable definition"));
1465 /* If it's not an override, check to see if there was a command-line
1466 setting. If so, reset the value. */
1467 if (origin
!= o_override
)
1469 struct variable
*gv
;
1470 int len
= strlen(v
->name
);
1472 current_variable_set_list
= global
;
1473 gv
= lookup_variable (v
->name
, len
);
1474 if (gv
&& (gv
->origin
== o_env_override
|| gv
->origin
== o_command
))
1475 define_variable_in_set (v
->name
, len
, gv
->value
, gv
->origin
,
1476 gv
->recursive
, vlist
->set
);
1479 /* Free name if not needed further. */
1480 if (name
!= fname
&& (name
< fname
|| name
> fname
+ strlen (fname
)))
1484 current_variable_set_list
= global
;
1487 /* Record a description line for files FILENAMES,
1488 with dependencies DEPS, commands to execute described
1489 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1490 TWO_COLON is nonzero if a double colon was used.
1491 If not nil, PATTERN is the `%' pattern to make this
1492 a static pattern rule, and PATTERN_PERCENT is a pointer
1493 to the `%' within it.
1495 The links of FILENAMES are freed, and so are any names in it
1496 that are not incorporated into other data structures. */
1499 record_files (filenames
, pattern
, pattern_percent
, deps
, cmds_started
,
1500 commands
, commands_idx
, two_colon
, flocp
, set_default
)
1501 struct nameseq
*filenames
;
1502 char *pattern
, *pattern_percent
;
1504 unsigned int cmds_started
;
1506 unsigned int commands_idx
;
1508 const struct floc
*flocp
;
1511 struct nameseq
*nextf
;
1513 unsigned int max_targets
= 0, target_idx
= 0;
1514 char **targets
= 0, **target_percents
= 0;
1515 struct commands
*cmds
;
1517 if (commands_idx
> 0)
1519 cmds
= (struct commands
*) xmalloc (sizeof (struct commands
));
1520 cmds
->fileinfo
.filenm
= flocp
->filenm
;
1521 cmds
->fileinfo
.lineno
= cmds_started
;
1522 cmds
->commands
= savestring (commands
, commands_idx
);
1523 cmds
->command_lines
= 0;
1528 for (; filenames
!= 0; filenames
= nextf
)
1531 register char *name
= filenames
->name
;
1532 register struct file
*f
;
1533 register struct dep
*d
;
1535 char *implicit_percent
;
1537 nextf
= filenames
->next
;
1538 free ((char *) filenames
);
1540 implicit_percent
= find_percent (name
);
1541 implicit
|= implicit_percent
!= 0;
1543 if (implicit
&& pattern
!= 0)
1544 fatal (flocp
, _("mixed implicit and static pattern rules"));
1546 if (implicit
&& implicit_percent
== 0)
1547 fatal (flocp
, _("mixed implicit and normal rules"));
1554 targets
= (char **) xmalloc (5 * sizeof (char *));
1555 target_percents
= (char **) xmalloc (5 * sizeof (char *));
1558 else if (target_idx
== max_targets
- 1)
1561 targets
= (char **) xrealloc ((char *) targets
,
1562 max_targets
* sizeof (char *));
1564 = (char **) xrealloc ((char *) target_percents
,
1565 max_targets
* sizeof (char *));
1567 targets
[target_idx
] = name
;
1568 target_percents
[target_idx
] = implicit_percent
;
1573 /* If there are multiple filenames, copy the chain DEPS
1574 for all but the last one. It is not safe for the same deps
1575 to go in more than one place in the data base. */
1576 this = nextf
!= 0 ? copy_dep_chain (deps
) : deps
;
1580 /* If this is an extended static rule:
1581 `targets: target%pattern: dep%pattern; cmds',
1582 translate each dependency pattern into a plain filename
1583 using the target pattern and this target's name. */
1584 if (!pattern_matches (pattern
, pattern_percent
, name
))
1586 /* Give a warning if the rule is meaningless. */
1588 _("target `%s' doesn't match the target pattern"), name
);
1593 /* We use patsubst_expand to do the work of translating
1594 the target pattern, the target's name and the dependencies'
1595 patterns into plain dependency names. */
1596 char *buffer
= variable_expand ("");
1598 for (d
= this; d
!= 0; d
= d
->next
)
1601 char *percent
= find_percent (d
->name
);
1604 o
= patsubst_expand (buffer
, name
, pattern
, d
->name
,
1605 pattern_percent
, percent
);
1607 d
->name
= savestring (buffer
, o
- buffer
);
1614 /* Single-colon. Combine these dependencies
1615 with others in file's existing record, if any. */
1616 f
= enter_file (name
);
1618 if (f
->double_colon
)
1620 _("target file `%s' has both : and :: entries"), f
->name
);
1622 /* If CMDS == F->CMDS, this target was listed in this rule
1623 more than once. Just give a warning since this is harmless. */
1624 if (cmds
!= 0 && cmds
== f
->cmds
)
1625 error (flocp
, _("target `%s' given more than once in the same rule."),
1628 /* Check for two single-colon entries both with commands.
1629 Check is_target so that we don't lose on files such as .c.o
1630 whose commands were preinitialized. */
1631 else if (cmds
!= 0 && f
->cmds
!= 0 && f
->is_target
)
1633 error (&cmds
->fileinfo
,
1634 _("warning: overriding commands for target `%s'"), f
->name
);
1635 error (&f
->cmds
->fileinfo
,
1636 _("warning: ignoring old commands for target `%s'"),
1642 /* Defining .DEFAULT with no deps or cmds clears it. */
1643 if (f
== default_file
&& this == 0 && cmds
== 0)
1647 /* Defining .SUFFIXES with no dependencies
1648 clears out the list of suffixes. */
1649 if (f
== suffix_file
&& this == 0)
1654 struct dep
*nextd
= d
->next
;
1661 else if (f
->deps
!= 0)
1663 /* Add the file's old deps and the new ones in THIS together. */
1665 struct dep
*firstdeps
, *moredeps
;
1668 /* This is the rule with commands, so put its deps first.
1669 The rationale behind this is that $< expands to the
1670 first dep in the chain, and commands use $< expecting
1671 to get the dep that rule specifies. */
1677 /* Append the new deps to the old ones. */
1678 firstdeps
= f
->deps
;
1683 firstdeps
= moredeps
;
1687 while (d
->next
!= 0)
1692 f
->deps
= firstdeps
;
1697 /* If this is a static pattern rule, set the file's stem to
1698 the part of its name that matched the `%' in the pattern,
1699 so you can use $* in the commands. */
1702 static char *percent
= "%";
1703 char *buffer
= variable_expand ("");
1704 char *o
= patsubst_expand (buffer
, name
, pattern
, percent
,
1705 pattern_percent
, percent
);
1706 f
->stem
= savestring (buffer
, o
- buffer
);
1711 /* Double-colon. Make a new record
1712 even if the file already has one. */
1713 f
= lookup_file (name
);
1714 /* Check for both : and :: rules. Check is_target so
1715 we don't lose on default suffix rules or makefiles. */
1716 if (f
!= 0 && f
->is_target
&& !f
->double_colon
)
1718 _("target file `%s' has both : and :: entries"), f
->name
);
1719 f
= enter_file (name
);
1720 /* If there was an existing entry and it was a double-colon
1721 entry, enter_file will have returned a new one, making it the
1722 prev pointer of the old one, and setting its double_colon
1723 pointer to the first one. */
1724 if (f
->double_colon
== 0)
1725 /* This is the first entry for this name, so we must
1726 set its double_colon pointer to itself. */
1727 f
->double_colon
= f
;
1733 /* Free name if not needed further. */
1734 if (f
!= 0 && name
!= f
->name
1735 && (name
< f
->name
|| name
> f
->name
+ strlen (f
->name
)))
1741 /* See if this is first target seen whose name does
1742 not start with a `.', unless it contains a slash. */
1743 if (default_goal_file
== 0 && set_default
1744 && (*name
!= '.' || index (name
, '/') != 0
1745 #if defined(__MSDOS__) || defined(WINDOWS32)
1746 || index (name
, '\\') != 0
1752 /* If this file is a suffix, don't
1753 let it be the default goal file. */
1755 for (d
= suffix_file
->deps
; d
!= 0; d
= d
->next
)
1757 register struct dep
*d2
;
1758 if (*dep_name (d
) != '.' && streq (name
, dep_name (d
)))
1763 for (d2
= suffix_file
->deps
; d2
!= 0; d2
= d2
->next
)
1765 register unsigned int len
= strlen (dep_name (d2
));
1766 if (!strneq (name
, dep_name (d2
), len
))
1768 if (streq (name
+ len
, dep_name (d
)))
1779 default_goal_file
= f
;
1785 targets
[target_idx
] = 0;
1786 target_percents
[target_idx
] = 0;
1787 create_pattern_rule (targets
, target_percents
, two_colon
, deps
, cmds
, 1);
1788 free ((char *) target_percents
);
1792 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1793 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1794 Quoting backslashes are removed from STRING by compacting it into
1795 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1796 one, or nil if there are none. */
1799 find_char_unquote (string
, stopchars
, blank
)
1804 unsigned int string_len
= 0;
1805 register char *p
= string
;
1809 while (*p
!= '\0' && index (stopchars
, *p
) == 0
1810 && (!blank
|| !isblank (*p
)))
1815 if (p
> string
&& p
[-1] == '\\')
1817 /* Search for more backslashes. */
1818 register int i
= -2;
1819 while (&p
[i
] >= string
&& p
[i
] == '\\')
1822 /* Only compute the length if really needed. */
1823 if (string_len
== 0)
1824 string_len
= strlen (string
);
1825 /* The number of backslashes is now -I.
1826 Copy P over itself to swallow half of them. */
1827 bcopy (&p
[i
/ 2], &p
[i
], (string_len
- (p
- string
)) - (i
/ 2) + 1);
1830 /* All the backslashes quoted each other; the STOPCHAR was
1834 /* The STOPCHAR was quoted by a backslash. Look for another. */
1837 /* No backslash in sight. */
1841 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1845 /* Search PATTERN for an unquoted %. */
1848 find_percent (pattern
)
1851 return find_char_unquote (pattern
, "%", 0);
1854 /* Parse a string into a sequence of filenames represented as a
1855 chain of struct nameseq's in reverse order and return that chain.
1857 The string is passed as STRINGP, the address of a string pointer.
1858 The string pointer is updated to point at the first character
1859 not parsed, which either is a null char or equals STOPCHAR.
1861 SIZE is how big to construct chain elements.
1862 This is useful if we want them actually to be other structures
1863 that have room for additional info.
1865 If STRIP is nonzero, strip `./'s off the beginning. */
1868 parse_file_seq (stringp
, stopchar
, size
, strip
)
1874 register struct nameseq
*new = 0;
1875 register struct nameseq
*new1
, *lastnew1
;
1876 register char *p
= *stringp
;
1883 stopchars
[1] = stopchar
;
1884 stopchars
[2] = '\0';
1886 stopchars
[0] = stopchar
;
1887 stopchars
[1] = '\0';
1892 /* Skip whitespace; see if any more names are left. */
1899 /* Yes, find end of next name. */
1901 p
= find_char_unquote (q
, stopchars
, 1);
1903 /* convert comma separated list to space separated */
1908 if (stopchar
== ':' && p
&& *p
== ':' &&
1909 !(isspace(p
[1]) || !p
[1] || isspace(p
[-1])))
1911 p
= find_char_unquote (p
+1, stopchars
, 1);
1914 #if defined(WINDOWS32) || defined(__MSDOS__)
1915 /* For WINDOWS32, skip a "C:\..." or a "C:/..." until we find the
1916 first colon which isn't followed by a slash or a backslash.
1917 Note that tokens separated by spaces should be treated as separate
1918 tokens since make doesn't allow path names with spaces */
1919 if (stopchar
== ':')
1920 while (p
!= 0 && !isspace(*p
) &&
1921 (p
[1] == '\\' || p
[1] == '/') && isalpha (p
[-1]))
1922 p
= find_char_unquote (p
+ 1, stopchars
, 1);
1929 /* Skip leading `[]'s. */
1930 while (p
- q
> 2 && q
[0] == '[' && q
[1] == ']')
1932 /* Skip leading `./'s. */
1933 while (p
- q
> 2 && q
[0] == '.' && q
[1] == '/')
1936 q
+= 2; /* Skip "./". */
1937 while (q
< p
&& *q
== '/')
1938 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1942 /* Extract the filename just found, and skip it. */
1945 /* ".///" was stripped to "". */
1950 name
= savestring ("", 0);
1952 name
= savestring ("./", 2);
1957 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
1958 * to remove this '\' before we can use the filename.
1959 * Savestring called because q may be read-only string constant.
1962 char *qbase
= xstrdup (q
);
1963 char *pbase
= qbase
+ (p
-q
);
1970 if (*q1
== '\\' && *(q1
+1) == ':')
1977 name
= savestring (qbase
, p1
- qbase
);
1981 name
= savestring (q
, p
- q
);
1984 /* Add it to the front of the chain. */
1985 new1
= (struct nameseq
*) xmalloc (size
);
1993 /* Look for multi-word archive references.
1994 They are indicated by a elt ending with an unmatched `)' and
1995 an elt further down the chain (i.e., previous in the file list)
1996 with an unmatched `(' (e.g., "lib(mem"). */
2001 if (new1
->name
[0] != '(' /* Don't catch "(%)" and suchlike. */
2002 && new1
->name
[strlen (new1
->name
) - 1] == ')'
2003 && index (new1
->name
, '(') == 0)
2005 /* NEW1 ends with a `)' but does not contain a `('.
2006 Look back for an elt with an opening `(' but no closing `)'. */
2008 struct nameseq
*n
= new1
->next
, *lastn
= new1
;
2010 while (n
!= 0 && (paren
= index (n
->name
, '(')) == 0)
2016 /* Ignore something starting with `(', as that cannot actually
2017 be an archive-member reference (and treating it as such
2018 results in an empty file name, which causes much lossage). */
2019 && n
->name
[0] != '(')
2021 /* N is the first element in the archive group.
2022 Its name looks like "lib(mem" (with no closing `)'). */
2026 /* Copy "lib(" into LIBNAME. */
2028 libname
= (char *) alloca (paren
- n
->name
+ 1);
2029 bcopy (n
->name
, libname
, paren
- n
->name
);
2030 libname
[paren
- n
->name
] = '\0';
2034 /* N was just "lib(", part of something like "lib( a b)".
2035 Edit it out of the chain and free its storage. */
2036 lastn
->next
= n
->next
;
2039 /* LASTN->next is the new stopping elt for the loop below. */
2044 /* Replace N's name with the full archive reference. */
2045 name
= concat (libname
, paren
, ")");
2050 if (new1
->name
[1] == '\0')
2052 /* NEW1 is just ")", part of something like "lib(a b )".
2053 Omit it from the chain and free its storage. */
2057 lastnew1
->next
= new1
->next
;
2061 free ((char *) lastn
);
2065 /* Replace also NEW1->name, which already has closing `)'. */
2066 name
= concat (libname
, new1
->name
, "");
2072 /* Trace back from NEW1 (the end of the list) until N
2073 (the beginning of the list), rewriting each name
2074 with the full archive reference. */
2078 name
= concat (libname
, new1
->name
, ")");
2087 /* No frobnication happening. Just step down the list. */
2104 /* Read a line of text from STREAM into LINEBUFFER.
2105 Combine continuation lines into one line.
2106 Return the number of actual lines read (> 1 if hacked continuation lines).
2109 static unsigned long
2110 readline (linebuffer
, stream
, flocp
)
2111 struct linebuffer
*linebuffer
;
2113 const struct floc
*flocp
;
2115 char *buffer
= linebuffer
->buffer
;
2116 register char *p
= linebuffer
->buffer
;
2117 register char *end
= p
+ linebuffer
->size
;
2118 register int len
, lastlen
= 0;
2120 register unsigned int nlines
= 0;
2121 register int backslash
;
2125 while (fgets (p
, end
- p
, stream
) != 0)
2130 /* This only happens when the first thing on the line is a '\0'.
2131 It is a pretty hopeless case, but (wonder of wonders) Athena
2132 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2133 There is nothing really to be done; we synthesize a newline so
2134 the following line doesn't appear to be part of this line. */
2135 error (flocp
, _("warning: NUL character seen; rest of line ignored"));
2143 /* Probably ran out of buffer space. */
2144 register unsigned int p_off
= p
- buffer
;
2145 linebuffer
->size
*= 2;
2146 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
2148 end
= buffer
+ linebuffer
->size
;
2149 linebuffer
->buffer
= buffer
;
2157 #if !defined(WINDOWS32) && !defined(__MSDOS__)
2158 /* Check to see if the line was really ended with CRLF; if so ignore
2160 if (len
> 1 && p
[-2] == '\r')
2168 if (len
== 1 && p
> buffer
)
2169 /* P is pointing at a newline and it's the beginning of
2170 the buffer returned by the last fgets call. However,
2171 it is not necessarily the beginning of a line if P is
2172 pointing past the beginning of the holding buffer.
2173 If the buffer was just enlarged (right before the newline),
2174 we must account for that, so we pretend that the two lines
2179 for (p2
= p
- 2; --len
> 0; --p2
)
2182 backslash
= !backslash
;
2195 /* Enlarge the buffer. */
2196 register unsigned int p_off
= p
- buffer
;
2197 linebuffer
->size
*= 2;
2198 buffer
= (char *) xrealloc (buffer
, linebuffer
->size
);
2200 end
= buffer
+ linebuffer
->size
;
2201 linebuffer
->buffer
= buffer
;
2205 if (ferror (stream
))
2206 pfatal_with_name (flocp
->filenm
);
2211 /* Parse the next "makefile word" from the input buffer, and return info
2214 A "makefile word" is one of:
2216 w_bogus Should never happen
2218 w_static A static word; cannot be expanded
2219 w_variable A word containing one or more variables/functions
2221 w_dcolon A double-colon
2222 w_semicolon A semicolon
2223 w_comment A comment character
2224 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2226 Note that this function is only used when reading certain parts of the
2227 makefile. Don't use it where special rules hold sway (RHS of a variable,
2228 in a command list, etc.) */
2230 static enum make_word_type
2231 get_next_mword (buffer
, delim
, startp
, length
)
2235 unsigned int *length
;
2237 enum make_word_type wtype
= w_bogus
;
2238 char *p
= buffer
, *beg
;
2241 /* Skip any leading whitespace. */
2258 wtype
= w_semicolon
;
2262 wtype
= w_varassign
;
2276 wtype
= w_varassign
;
2286 wtype
= w_varassign
;
2291 if (delim
&& index(delim
, c
))
2296 /* Did we find something? If so, return now. */
2297 if (wtype
!= w_bogus
)
2300 /* This is some non-operator word. A word consists of the longest
2301 string of characters that doesn't contain whitespace, one of [:=#],
2302 or [?+]=, or one of the chars in the DELIM string. */
2304 /* We start out assuming a static word; if we see a variable we'll
2305 adjust our assumptions then. */
2308 /* We already found the first value of "c", above. */
2324 #if defined(__MSDOS__) || defined(WINDOWS32)
2325 /* A word CAN include a colon in its drive spec. The drive
2326 spec is allowed either at the beginning of a word, or as part
2327 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2328 if (!(p
- beg
>= 2 &&
2329 (*p
== '/' || *p
== '\\') && isalpha (p
[-2]) &&
2330 (p
- beg
== 2 || p
[-3] == '(')))
2339 /* This is a variable reference, so note that it's expandable.
2340 Then read it to the matching close paren. */
2348 /* This is a single-letter variable reference. */
2351 for (count
=0; *p
!= '\0'; ++p
)
2355 else if (*p
== closeparen
&& --count
< 0)
2382 if (delim
&& index(delim
, c
))
2400 /* Construct the list of include directories
2401 from the arguments and the default list. */
2404 construct_include_path (arg_dirs
)
2407 register unsigned int i
;
2408 #ifdef VAXC /* just don't ask ... */
2413 /* Table to hold the dirs. */
2415 register unsigned int defsize
= (sizeof (default_include_directories
)
2416 / sizeof (default_include_directories
[0]));
2417 register unsigned int max
= 5;
2418 register char **dirs
= (char **) xmalloc ((5 + defsize
) * sizeof (char *));
2419 register unsigned int idx
= 0;
2425 /* First consider any dirs specified with -I switches.
2426 Ignore dirs that don't exist. */
2429 while (*arg_dirs
!= 0)
2431 char *dir
= *arg_dirs
++;
2435 char *expanded
= tilde_expand (dir
);
2440 if (stat (dir
, &stbuf
) == 0 && S_ISDIR (stbuf
.st_mode
))
2446 xrealloc ((char *) dirs
, (max
+ defsize
) * sizeof (char *));
2450 else if (dir
!= arg_dirs
[-1])
2454 /* Now add at the end the standard default dirs. */
2458 /* The environment variable $DJDIR holds the root of the
2459 DJGPP directory tree; add ${DJDIR}/include. */
2460 struct variable
*djdir
= lookup_variable ("DJDIR", 5);
2464 char *defdir
= (char *) xmalloc (strlen (djdir
->value
) + 8 + 1);
2466 strcat (strcpy (defdir
, djdir
->value
), "/include");
2467 dirs
[idx
++] = defdir
;
2472 for (i
= 0; default_include_directories
[i
] != 0; ++i
)
2473 if (stat (default_include_directories
[i
], &stbuf
) == 0
2474 && S_ISDIR (stbuf
.st_mode
))
2475 dirs
[idx
++] = default_include_directories
[i
];
2479 /* Now compute the maximum length of any name in it. */
2482 for (i
= 0; i
< idx
; ++i
)
2484 unsigned int len
= strlen (dirs
[i
]);
2485 /* If dir name is written with a trailing slash, discard it. */
2486 if (dirs
[i
][len
- 1] == '/')
2487 /* We can't just clobber a null in because it may have come from
2488 a literal string and literal strings may not be writable. */
2489 dirs
[i
] = savestring (dirs
[i
], len
- 1);
2490 if (len
> max_incl_len
)
2494 include_directories
= dirs
;
2497 /* Expand ~ or ~USER at the beginning of NAME.
2498 Return a newly malloc'd string or 0. */
2505 if (name
[1] == '/' || name
[1] == '\0')
2507 extern char *getenv ();
2512 /* Turn off --warn-undefined-variables while we expand HOME. */
2513 int save
= warn_undefined_variables_flag
;
2514 warn_undefined_variables_flag
= 0;
2516 home_dir
= allocated_variable_expand ("$(HOME)");
2518 warn_undefined_variables_flag
= save
;
2521 is_variable
= home_dir
[0] != '\0';
2525 home_dir
= getenv ("HOME");
2527 #if !defined(_AMIGA) && !defined(WINDOWS32)
2528 if (home_dir
== 0 || home_dir
[0] == '\0')
2530 extern char *getlogin ();
2531 char *logname
= getlogin ();
2535 struct passwd
*p
= getpwnam (logname
);
2537 home_dir
= p
->pw_dir
;
2540 #endif /* !AMIGA && !WINDOWS32 */
2543 char *new = concat (home_dir
, "", name
+ 1);
2549 #if !defined(_AMIGA) && !defined(WINDOWS32)
2552 struct passwd
*pwent
;
2553 char *userend
= index (name
+ 1, '/');
2556 pwent
= getpwnam (name
+ 1);
2560 return xstrdup (pwent
->pw_dir
);
2562 return concat (pwent
->pw_dir
, "/", userend
+ 1);
2564 else if (userend
!= 0)
2567 #endif /* !AMIGA && !WINDOWS32 */
2572 /* Given a chain of struct nameseq's describing a sequence of filenames,
2573 in reverse of the intended order, return a new chain describing the
2574 result of globbing the filenames. The new chain is in forward order.
2575 The links of the old chain are freed or used in the new chain.
2576 Likewise for the names in the old chain.
2578 SIZE is how big to construct chain elements.
2579 This is useful if we want them actually to be other structures
2580 that have room for additional info. */
2583 multi_glob (chain
, size
)
2584 struct nameseq
*chain
;
2587 extern void dir_setup_glob ();
2588 register struct nameseq
*new = 0;
2589 register struct nameseq
*old
;
2590 struct nameseq
*nexto
;
2593 dir_setup_glob (&gl
);
2595 for (old
= chain
; old
!= 0; old
= nexto
)
2603 if (old
->name
[0] == '~')
2605 char *newname
= tilde_expand (old
->name
);
2609 old
->name
= newname
;
2614 if (ar_name (old
->name
))
2616 /* OLD->name is an archive member reference.
2617 Replace it with the archive file name,
2618 and save the member name in MEMNAME.
2619 We will glob on the archive name and then
2620 reattach MEMNAME later. */
2622 ar_parse_name (old
->name
, &arname
, &memname
);
2628 #endif /* !NO_ARCHIVES */
2630 switch (glob (old
->name
, GLOB_NOCHECK
|GLOB_ALTDIRFUNC
, NULL
, &gl
))
2632 case 0: /* Success. */
2634 register int i
= gl
.gl_pathc
;
2640 /* Try to glob on MEMNAME within the archive. */
2641 struct nameseq
*found
2642 = ar_glob (gl
.gl_pathv
[i
], memname
, size
);
2645 /* No matches. Use MEMNAME as-is. */
2647 = (struct nameseq
*) xmalloc (size
);
2648 unsigned int alen
= strlen (gl
.gl_pathv
[i
]);
2649 unsigned int mlen
= strlen (memname
);
2650 elt
->name
= (char *) xmalloc (alen
+ 1 + mlen
+ 2);
2651 bcopy (gl
.gl_pathv
[i
], elt
->name
, alen
);
2652 elt
->name
[alen
] = '(';
2653 bcopy (memname
, &elt
->name
[alen
+ 1], mlen
);
2654 elt
->name
[alen
+ 1 + mlen
] = ')';
2655 elt
->name
[alen
+ 1 + mlen
+ 1] = '\0';
2661 /* Find the end of the FOUND chain. */
2662 struct nameseq
*f
= found
;
2663 while (f
->next
!= 0)
2666 /* Attach the chain being built to the end of the FOUND
2667 chain, and make FOUND the new NEW chain. */
2675 #endif /* !NO_ARCHIVES */
2677 struct nameseq
*elt
= (struct nameseq
*) xmalloc (size
);
2678 elt
->name
= xstrdup (gl
.gl_pathv
[i
]);
2690 fatal (NILF
, _("virtual memory exhausted"));