* Various fixes for problems in the 3.79.0.1 pretest.
[make.git] / read.c
blob7ef1081f6d52e495eab236175bf575fd6897caf8
1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97 Free Software Foundation, Inc.
3 This file is part of GNU Make.
5 GNU Make is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2, or (at your option)
8 any later version.
10 GNU Make is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with GNU Make; see the file COPYING. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA. */
20 #include "make.h"
22 #include <assert.h>
24 #include <glob.h>
26 #include "dep.h"
27 #include "filedef.h"
28 #include "job.h"
29 #include "commands.h"
30 #include "variable.h"
31 #include "rule.h"
32 #include "debug.h"
35 #ifndef WINDOWS32
36 #ifndef _AMIGA
37 #ifndef VMS
38 #include <pwd.h>
39 #else
40 struct passwd *getpwnam PARAMS ((char *name));
41 #endif
42 #endif
43 #endif /* !WINDOWS32 */
45 /* A `struct linebuffer' is a structure which holds a line of text.
46 `readline' reads a line from a stream into a linebuffer
47 and works regardless of the length of the line. */
49 struct linebuffer
51 /* Note: This is the number of bytes malloc'ed for `buffer'
52 It does not indicate `buffer's real length.
53 Instead, a null char indicates end-of-string. */
54 unsigned int size;
55 char *buffer;
58 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
59 #define freebuffer(lb) free ((lb)->buffer)
62 /* Types of "words" that can be read in a makefile. */
63 enum make_word_type
65 w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
66 w_comment, w_varassign
70 /* A `struct conditionals' contains the information describing
71 all the active conditionals in a makefile.
73 The global variable `conditionals' contains the conditionals
74 information for the current makefile. It is initialized from
75 the static structure `toplevel_conditionals' and is later changed
76 to new structures for included makefiles. */
78 struct conditionals
80 unsigned int if_cmds; /* Depth of conditional nesting. */
81 unsigned int allocated; /* Elts allocated in following arrays. */
82 char *ignoring; /* Are we ignoring or interepreting? */
83 char *seen_else; /* Have we already seen an `else'? */
86 static struct conditionals toplevel_conditionals;
87 static struct conditionals *conditionals = &toplevel_conditionals;
90 /* Default directories to search for include files in */
92 static char *default_include_directories[] =
94 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
96 * This completly up to the user when they install MSVC or other packages.
97 * This is defined as a placeholder.
99 #define INCLUDEDIR "."
100 #endif
101 INCLUDEDIR,
102 #ifndef _AMIGA
103 "/usr/gnu/include",
104 "/usr/local/include",
105 "/usr/include",
106 #endif
110 /* List of directories to search for include files in */
112 static char **include_directories;
114 /* Maximum length of an element of the above. */
116 static unsigned int max_incl_len;
118 /* The filename and pointer to line number of the
119 makefile currently being read in. */
121 const struct floc *reading_file;
123 /* The chain of makefiles read by read_makefile. */
125 static struct dep *read_makefiles = 0;
127 static int read_makefile PARAMS ((char *filename, int flags));
128 static unsigned long readline PARAMS ((struct linebuffer *linebuffer,
129 FILE *stream, const struct floc *flocp));
130 static void do_define PARAMS ((char *name, unsigned int namelen,
131 enum variable_origin origin, FILE *infile,
132 struct floc *flocp));
133 static int conditional_line PARAMS ((char *line, const struct floc *flocp));
134 static void record_files PARAMS ((struct nameseq *filenames, char *pattern, char *pattern_percent,
135 struct dep *deps, unsigned int cmds_started, char *commands,
136 unsigned int commands_idx, int two_colon,
137 const struct floc *flocp, int set_default));
138 static void record_target_var PARAMS ((struct nameseq *filenames, char *defn,
139 int two_colon,
140 enum variable_origin origin,
141 const struct floc *flocp));
142 static enum make_word_type get_next_mword PARAMS ((char *buffer, char *delim,
143 char **startp, unsigned int *length));
145 /* Read in all the makefiles and return the chain of their names. */
147 struct dep *
148 read_all_makefiles (makefiles)
149 char **makefiles;
151 unsigned int num_makefiles = 0;
153 DB (DB_BASIC, (_("Reading makefiles...\n")));
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. */
160 char *value;
161 char *name, *p;
162 unsigned int length;
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. */
176 p = value;
178 while ((name = find_next_token (&p, &length)) != 0)
180 if (*p != '\0')
181 *p++ = '\0';
182 name = xstrdup (name);
183 if (read_makefile (name,
184 RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE) < 2)
185 free (name);
188 free (value);
191 /* Read makefiles specified with -f switches. */
193 if (makefiles != 0)
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. */
203 d = read_makefiles;
204 while (d->next != tail)
205 d = d->next;
207 /* Use the storage read_makefile allocates. */
208 *makefiles = dep_name (d);
209 ++num_makefiles;
210 ++makefiles;
213 /* If there were no -f switches, try the default names. */
215 if (num_makefiles == 0)
217 static char *default_makefiles[] =
218 #ifdef VMS
219 /* all lower case since readdir() (the vms version) 'lowercasifies' */
220 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
221 #else
222 #ifdef _AMIGA
223 { "GNUmakefile", "Makefile", "SMakefile", 0 };
224 #else /* !Amiga && !VMS */
225 { "GNUmakefile", "makefile", "Makefile", 0 };
226 #endif /* AMIGA */
227 #endif /* VMS */
228 register char **p = default_makefiles;
229 while (*p != 0 && !file_exists_p (*p))
230 ++p;
232 if (*p != 0)
234 if (! read_makefile (*p, 0))
235 perror_with_name ("", *p);
237 else
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)
244 tail = tail->next;
245 for (p = default_makefiles; *p != 0; ++p)
247 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
248 d->name = 0;
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;
254 if (tail == 0)
255 read_makefiles = d;
256 else
257 tail->next = d;
258 tail = d;
260 if (tail != 0)
261 tail->next = 0;
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). */
278 static int
279 read_makefile (filename, flags)
280 char *filename;
281 int 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;
288 char *commands;
289 unsigned int commands_idx = 0;
290 unsigned int cmds_started, tgts_started;
291 char *p;
292 char *p2;
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 struct floc fileinfo;
297 char *passed_filename = filename;
299 struct nameseq *filenames = 0;
300 struct dep *deps;
301 unsigned int nlines = 0;
302 int two_colon = 0;
303 char *pattern = 0, *pattern_percent;
305 int makefile_errno;
306 #if defined (WINDOWS32) || defined (__MSDOS__)
307 int check_again;
308 #endif
310 #define record_waiting_files() \
311 do \
313 if (filenames != 0) \
315 struct floc fi; \
316 fi.filenm = fileinfo.filenm; \
317 fi.lineno = tgts_started; \
318 record_files (filenames, pattern, pattern_percent, deps, \
319 cmds_started, commands, commands_idx, two_colon, \
320 &fi, !(flags & RM_NO_DEFAULT_GOAL)); \
322 filenames = 0; \
323 commands_idx = 0; \
324 if (pattern) { free(pattern); pattern = 0; } \
325 } while (0)
327 fileinfo.filenm = filename;
328 fileinfo.lineno = 1;
330 pattern_percent = 0;
331 cmds_started = tgts_started = fileinfo.lineno;
333 if (ISDB (DB_VERBOSE))
335 printf (_("Reading makefile `%s'"), fileinfo.filenm);
336 if (flags & RM_NO_DEFAULT_GOAL)
337 printf (_(" (no default goal)"));
338 if (flags & RM_INCLUDED)
339 printf (_(" (search path)"));
340 if (flags & RM_DONTCARE)
341 printf (_(" (don't care)"));
342 if (flags & RM_NO_TILDE)
343 printf (_(" (no ~ expansion)"));
344 puts ("...");
347 /* First, get a stream to read. */
349 /* Expand ~ in FILENAME unless it came from `include',
350 in which case it was already done. */
351 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
353 char *expanded = tilde_expand (filename);
354 if (expanded != 0)
355 filename = expanded;
358 infile = fopen (filename, "r");
359 /* Save the error code so we print the right message later. */
360 makefile_errno = errno;
362 /* If the makefile wasn't found and it's either a makefile from
363 the `MAKEFILES' variable or an included makefile,
364 search the included makefile search path for this makefile. */
365 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
367 register unsigned int i;
368 for (i = 0; include_directories[i] != 0; ++i)
370 char *name = concat (include_directories[i], "/", filename);
371 infile = fopen (name, "r");
372 if (infile == 0)
373 free (name);
374 else
376 filename = name;
377 break;
382 /* Add FILENAME to the chain of read makefiles. */
383 deps = (struct dep *) xmalloc (sizeof (struct dep));
384 deps->next = read_makefiles;
385 read_makefiles = deps;
386 deps->name = 0;
387 deps->file = lookup_file (filename);
388 if (deps->file == 0)
390 deps->file = enter_file (xstrdup (filename));
391 if (flags & RM_DONTCARE)
392 deps->file->dontcare = 1;
394 if (filename != passed_filename)
395 free (filename);
396 filename = deps->file->name;
397 deps->changed = flags;
398 deps = 0;
400 /* If the makefile can't be found at all, give up entirely. */
402 if (infile == 0)
404 /* If we did some searching, errno has the error from the last
405 attempt, rather from FILENAME itself. Restore it in case the
406 caller wants to use it in a message. */
407 errno = makefile_errno;
408 return 0;
411 reading_file = &fileinfo;
413 /* Loop over lines in the file.
414 The strategy is to accumulate target names in FILENAMES, dependencies
415 in DEPS and commands in COMMANDS. These are used to define a rule
416 when the start of the next rule (or eof) is encountered. */
418 initbuffer (&lb);
419 commands = xmalloc (200);
421 while (!feof (infile))
423 fileinfo.lineno += nlines;
424 nlines = readline (&lb, infile, &fileinfo);
426 /* Check for a shell command line first.
427 If it is not one, we can stop treating tab specially. */
428 if (lb.buffer[0] == '\t')
430 /* This line is a probably shell command. */
431 unsigned int len;
433 if (no_targets)
434 /* Ignore the commands in a rule with no targets. */
435 continue;
437 /* If there is no preceding rule line, don't treat this line
438 as a command, even though it begins with a tab character.
439 SunOS 4 make appears to behave this way. */
441 if (filenames != 0)
443 if (ignoring)
444 /* Yep, this is a shell command, and we don't care. */
445 continue;
447 /* Append this command line to the line being accumulated. */
448 p = lb.buffer;
449 if (commands_idx == 0)
450 cmds_started = fileinfo.lineno;
451 len = strlen (p);
452 if (len + 1 + commands_idx > commands_len)
454 commands_len = (len + 1 + commands_idx) * 2;
455 commands = (char *) xrealloc (commands, commands_len);
457 bcopy (p, &commands[commands_idx], len);
458 commands_idx += len;
459 commands[commands_idx++] = '\n';
461 continue;
465 /* This line is not a shell command line. Don't worry about tabs. */
467 if (collapsed_length < lb.size)
469 collapsed_length = lb.size;
470 if (collapsed != 0)
471 free (collapsed);
472 collapsed = (char *) xmalloc (collapsed_length);
474 strcpy (collapsed, lb.buffer);
475 /* Collapse continuation lines. */
476 collapse_continuations (collapsed);
477 remove_comments (collapsed);
479 /* Compare a word, both length and contents. */
480 #define word1eq(s, l) (len == l && strneq (s, p, l))
481 p = collapsed;
482 while (isspace ((unsigned char)*p))
483 ++p;
484 if (*p == '\0')
485 /* This line is completely empty. */
486 continue;
488 /* Find the end of the first token. Note we don't need to worry about
489 * ":" here since we compare tokens by length (so "export" will never
490 * be equal to "export:").
492 for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
494 len = p2 - p;
496 /* Find the start of the second token. If it's a `:' remember it,
497 since it can't be a preprocessor token--this allows targets named
498 `ifdef', `export', etc. */
499 reading_target = 0;
500 while (isspace ((unsigned char)*p2))
501 ++p2;
502 if (*p2 == '\0')
503 p2 = NULL;
504 else if (p2[0] == ':' && p2[1] == '\0')
506 reading_target = 1;
507 goto skip_conditionals;
510 /* We must first check for conditional and `define' directives before
511 ignoring anything, since they control what we will do with
512 following lines. */
514 if (!in_ignored_define
515 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
516 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
517 || word1eq ("else", 4) || word1eq ("endif", 5)))
519 int i = conditional_line (p, &fileinfo);
520 if (i >= 0)
521 ignoring = i;
522 else
523 fatal (&fileinfo, _("invalid syntax in conditional"));
524 continue;
527 if (word1eq ("endef", 5))
529 if (in_ignored_define)
530 in_ignored_define = 0;
531 else
532 fatal (&fileinfo, _("extraneous `endef'"));
533 continue;
536 if (word1eq ("define", 6))
538 if (ignoring)
539 in_ignored_define = 1;
540 else
542 p2 = next_token (p + 6);
543 if (*p2 == '\0')
544 fatal (&fileinfo, _("empty variable name"));
546 /* Let the variable name be the whole rest of the line,
547 with trailing blanks stripped (comments have already been
548 removed), so it could be a complex variable/function
549 reference that might contain blanks. */
550 p = strchr (p2, '\0');
551 while (isblank ((unsigned char)p[-1]))
552 --p;
553 do_define (p2, p - p2, o_file, infile, &fileinfo);
555 continue;
558 if (word1eq ("override", 8))
560 p2 = next_token (p + 8);
561 if (*p2 == '\0')
562 error (&fileinfo, _("empty `override' directive"));
563 if (strneq (p2, "define", 6)
564 && (isblank ((unsigned char)p2[6]) || p2[6] == '\0'))
566 if (ignoring)
567 in_ignored_define = 1;
568 else
570 p2 = next_token (p2 + 6);
571 if (*p2 == '\0')
572 fatal (&fileinfo, _("empty variable name"));
574 /* Let the variable name be the whole rest of the line,
575 with trailing blanks stripped (comments have already been
576 removed), so it could be a complex variable/function
577 reference that might contain blanks. */
578 p = strchr (p2, '\0');
579 while (isblank ((unsigned char)p[-1]))
580 --p;
581 do_define (p2, p - p2, o_override, infile, &fileinfo);
584 else if (!ignoring
585 && !try_variable_definition (&fileinfo, p2, o_override, 0))
586 error (&fileinfo, _("invalid `override' directive"));
588 continue;
590 skip_conditionals:
592 if (ignoring)
593 /* Ignore the line. We continue here so conditionals
594 can appear in the middle of a rule. */
595 continue;
597 if (!reading_target && word1eq ("export", 6))
599 struct variable *v;
600 p2 = next_token (p + 6);
601 if (*p2 == '\0')
602 export_all_variables = 1;
603 v = try_variable_definition (&fileinfo, p2, o_file, 0);
604 if (v != 0)
605 v->export = v_export;
606 else
608 unsigned int len;
609 for (p = find_next_token (&p2, &len); p != 0;
610 p = find_next_token (&p2, &len))
612 v = lookup_variable (p, len);
613 if (v == 0)
614 v = define_variable_loc (p, len, "", o_file, 0, &fileinfo);
615 v->export = v_export;
619 else if (!reading_target && word1eq ("unexport", 8))
621 unsigned int len;
622 struct variable *v;
623 p2 = next_token (p + 8);
624 if (*p2 == '\0')
625 export_all_variables = 0;
626 for (p = find_next_token (&p2, &len); p != 0;
627 p = find_next_token (&p2, &len))
629 v = lookup_variable (p, len);
630 if (v == 0)
631 v = define_variable_loc (p, len, "", o_file, 0, &fileinfo);
632 v->export = v_noexport;
635 else if (word1eq ("vpath", 5))
637 char *pattern;
638 unsigned int len;
639 p2 = variable_expand (p + 5);
640 p = find_next_token (&p2, &len);
641 if (p != 0)
643 pattern = savestring (p, len);
644 p = find_next_token (&p2, &len);
645 /* No searchpath means remove all previous
646 selective VPATH's with the same pattern. */
648 else
649 /* No pattern means remove all previous selective VPATH's. */
650 pattern = 0;
651 construct_vpath_list (pattern, p);
652 if (pattern != 0)
653 free (pattern);
655 else if (word1eq ("include", 7) || word1eq ("-include", 8)
656 || word1eq ("sinclude", 8))
658 /* We have found an `include' line specifying a nested
659 makefile to be read at this point. */
660 struct conditionals *save, new_conditionals;
661 struct nameseq *files;
662 /* "-include" (vs "include") says no error if the file does not
663 exist. "sinclude" is an alias for this from SGI. */
664 int noerror = p[0] != 'i';
666 p = allocated_variable_expand (next_token (p + (noerror ? 8 : 7)));
667 if (*p == '\0')
669 error (&fileinfo,
670 _("no file name for `%sinclude'"), noerror ? "-" : "");
671 continue;
674 /* Parse the list of file names. */
675 p2 = p;
676 files = multi_glob (parse_file_seq (&p2, '\0',
677 sizeof (struct nameseq),
679 sizeof (struct nameseq));
680 free (p);
682 /* Save the state of conditionals and start
683 the included makefile with a clean slate. */
684 save = conditionals;
685 bzero ((char *) &new_conditionals, sizeof new_conditionals);
686 conditionals = &new_conditionals;
688 /* Record the rules that are waiting so they will determine
689 the default goal before those in the included makefile. */
690 record_waiting_files ();
692 /* Read each included makefile. */
693 while (files != 0)
695 struct nameseq *next = files->next;
696 char *name = files->name;
697 int r;
699 free ((char *)files);
700 files = next;
702 r = read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
703 | (noerror ? RM_DONTCARE : 0)));
704 if (!r)
706 if (!noerror)
707 error (&fileinfo, "%s: %s", name, strerror (errno));
708 free (name);
712 /* Free any space allocated by conditional_line. */
713 if (conditionals->ignoring)
714 free (conditionals->ignoring);
715 if (conditionals->seen_else)
716 free (conditionals->seen_else);
718 /* Restore state. */
719 conditionals = save;
720 reading_file = &fileinfo;
722 #undef word1eq
723 else if (try_variable_definition (&fileinfo, p, o_file, 0))
724 /* This line has been dealt with. */
726 else if (lb.buffer[0] == '\t')
728 p = collapsed; /* Ignore comments. */
729 while (isblank ((unsigned char)*p))
730 ++p;
731 if (*p == '\0')
732 /* The line is completely blank; that is harmless. */
733 continue;
734 /* This line starts with a tab but was not caught above
735 because there was no preceding target, and the line
736 might have been usable as a variable definition.
737 But now it is definitely lossage. */
738 fatal(&fileinfo, _("commands commence before first target"));
740 else
742 /* This line describes some target files. This is complicated by
743 the existence of target-specific variables, because we can't
744 expand the entire line until we know if we have one or not. So
745 we expand the line word by word until we find the first `:',
746 then check to see if it's a target-specific variable.
748 In this algorithm, `lb_next' will point to the beginning of the
749 unexpanded parts of the input buffer, while `p2' points to the
750 parts of the expanded buffer we haven't searched yet. */
752 enum make_word_type wtype;
753 enum variable_origin v_origin;
754 char *cmdleft, *lb_next;
755 unsigned int len, plen = 0;
756 char *colonp;
758 /* Record the previous rule. */
760 record_waiting_files ();
761 tgts_started = fileinfo.lineno;
763 /* Search the line for an unquoted ; that is not after an
764 unquoted #. */
765 cmdleft = find_char_unquote (lb.buffer, ";#", 0);
766 if (cmdleft != 0 && *cmdleft == '#')
768 /* We found a comment before a semicolon. */
769 *cmdleft = '\0';
770 cmdleft = 0;
772 else if (cmdleft != 0)
773 /* Found one. Cut the line short there before expanding it. */
774 *(cmdleft++) = '\0';
776 collapse_continuations (lb.buffer);
778 /* We can't expand the entire line, since if it's a per-target
779 variable we don't want to expand it. So, walk from the
780 beginning, expanding as we go, and looking for "interesting"
781 chars. The first word is always expandable. */
782 wtype = get_next_mword(lb.buffer, NULL, &lb_next, &len);
783 switch (wtype)
785 case w_eol:
786 if (cmdleft != 0)
787 fatal(&fileinfo, _("missing rule before commands"));
788 /* This line contained something but turned out to be nothing
789 but whitespace (a comment?). */
790 continue;
792 case w_colon:
793 case w_dcolon:
794 /* We accept and ignore rules without targets for
795 compatibility with SunOS 4 make. */
796 no_targets = 1;
797 continue;
799 default:
800 break;
803 p2 = variable_expand_string(NULL, lb_next, len);
804 while (1)
806 lb_next += len;
807 if (cmdleft == 0)
809 /* Look for a semicolon in the expanded line. */
810 cmdleft = find_char_unquote (p2, ";", 0);
812 if (cmdleft != 0)
814 unsigned long p2_off = p2 - variable_buffer;
815 unsigned long cmd_off = cmdleft - variable_buffer;
816 char *pend = p2 + strlen(p2);
818 /* Append any remnants of lb, then cut the line short
819 at the semicolon. */
820 *cmdleft = '\0';
822 /* One school of thought says that you shouldn't expand
823 here, but merely copy, since now you're beyond a ";"
824 and into a command script. However, the old parser
825 expanded the whole line, so we continue that for
826 backwards-compatiblity. Also, it wouldn't be
827 entirely consistent, since we do an unconditional
828 expand below once we know we don't have a
829 target-specific variable. */
830 (void)variable_expand_string(pend, lb_next, (long)-1);
831 lb_next += strlen(lb_next);
832 p2 = variable_buffer + p2_off;
833 cmdleft = variable_buffer + cmd_off + 1;
837 colonp = find_char_unquote(p2, ":", 0);
838 #if defined(__MSDOS__) || defined(WINDOWS32)
839 /* The drive spec brain-damage strikes again... */
840 /* Note that the only separators of targets in this context
841 are whitespace and a left paren. If others are possible,
842 they should be added to the string in the call to index. */
843 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
844 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
845 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
846 colonp = find_char_unquote(colonp + 1, ":", 0);
847 #endif
848 if (colonp != 0)
849 break;
851 wtype = get_next_mword(lb_next, NULL, &lb_next, &len);
852 if (wtype == w_eol)
853 break;
855 p2 += strlen(p2);
856 *(p2++) = ' ';
857 p2 = variable_expand_string(p2, lb_next, len);
858 /* We don't need to worry about cmdleft here, because if it was
859 found in the variable_buffer the entire buffer has already
860 been expanded... we'll never get here. */
863 p2 = next_token (variable_buffer);
865 /* If the word we're looking at is EOL, see if there's _anything_
866 on the line. If not, a variable expanded to nothing, so ignore
867 it. If so, we can't parse this line so punt. */
868 if (wtype == w_eol)
870 if (*p2 != '\0')
871 /* There's no need to be ivory-tower about this: check for
872 one of the most common bugs found in makefiles... */
873 fatal (&fileinfo, _("missing separator%s"),
874 !strneq(lb.buffer, " ", 8) ? ""
875 : _(" (did you mean TAB instead of 8 spaces?)"));
876 continue;
879 /* Make the colon the end-of-string so we know where to stop
880 looking for targets. */
881 *colonp = '\0';
882 filenames = multi_glob (parse_file_seq (&p2, '\0',
883 sizeof (struct nameseq),
885 sizeof (struct nameseq));
886 *p2 = ':';
888 if (!filenames)
890 /* We accept and ignore rules without targets for
891 compatibility with SunOS 4 make. */
892 no_targets = 1;
893 continue;
895 /* This should never be possible; we handled it above. */
896 assert (*p2 != '\0');
897 ++p2;
899 /* Is this a one-colon or two-colon entry? */
900 two_colon = *p2 == ':';
901 if (two_colon)
902 p2++;
904 /* Test to see if it's a target-specific variable. Copy the rest
905 of the buffer over, possibly temporarily (we'll expand it later
906 if it's not a target-specific variable). PLEN saves the length
907 of the unparsed section of p2, for later. */
908 if (*lb_next != '\0')
910 unsigned int l = p2 - variable_buffer;
911 plen = strlen (p2);
912 (void) variable_buffer_output (p2+plen,
913 lb_next, strlen (lb_next)+1);
914 p2 = variable_buffer + l;
917 /* See if it's an "override" keyword; if so see if what comes after
918 it looks like a variable definition. */
920 wtype = get_next_mword (p2, NULL, &p, &len);
922 v_origin = o_file;
923 if (wtype == w_static && (len == (sizeof ("override")-1)
924 && strneq (p, "override", len)))
926 v_origin = o_override;
927 wtype = get_next_mword (p+len, NULL, &p, &len);
930 if (wtype != w_eol)
931 wtype = get_next_mword (p+len, NULL, NULL, NULL);
933 if (wtype == w_varassign)
935 record_target_var (filenames, p, two_colon, v_origin, &fileinfo);
936 filenames = 0;
937 continue;
940 /* This is a normal target, _not_ a target-specific variable.
941 Unquote any = in the dependency list. */
942 find_char_unquote (lb_next, "=", 0);
944 /* We have some targets, so don't ignore the following commands. */
945 no_targets = 0;
947 /* Expand the dependencies, etc. */
948 if (*lb_next != '\0')
950 unsigned int l = p2 - variable_buffer;
951 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
952 p2 = variable_buffer + l;
954 /* Look for a semicolon in the expanded line. */
955 if (cmdleft == 0)
957 cmdleft = find_char_unquote (p2, ";", 0);
958 if (cmdleft != 0)
959 *(cmdleft++) = '\0';
963 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
964 p = strchr (p2, ':');
965 while (p != 0 && p[-1] == '\\')
967 register char *q = &p[-1];
968 register int backslash = 0;
969 while (*q-- == '\\')
970 backslash = !backslash;
971 if (backslash)
972 p = strchr (p + 1, ':');
973 else
974 break;
976 #ifdef _AMIGA
977 /* Here, the situation is quite complicated. Let's have a look
978 at a couple of targets:
980 install: dev:make
982 dev:make: make
984 dev:make:: xyz
986 The rule is that it's only a target, if there are TWO :'s
987 OR a space around the :.
989 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
990 || isspace ((unsigned char)p[-1])))
991 p = 0;
992 #endif
993 #if defined (WINDOWS32) || defined (__MSDOS__)
994 do {
995 check_again = 0;
996 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
997 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
998 isalpha ((unsigned char)p[-1]) &&
999 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1000 p = strchr (p + 1, ':');
1001 check_again = 1;
1003 } while (check_again);
1004 #endif
1005 if (p != 0)
1007 struct nameseq *target;
1008 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
1009 ++p2;
1010 if (target == 0)
1011 fatal (&fileinfo, _("missing target pattern"));
1012 else if (target->next != 0)
1013 fatal (&fileinfo, _("multiple target patterns"));
1014 pattern = target->name;
1015 pattern_percent = find_percent (pattern);
1016 if (pattern_percent == 0)
1017 fatal (&fileinfo, _("target pattern contains no `%%'"));
1018 free((char *)target);
1020 else
1021 pattern = 0;
1023 /* Parse the dependencies. */
1024 deps = (struct dep *)
1025 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
1026 sizeof (struct dep));
1028 commands_idx = 0;
1029 if (cmdleft != 0)
1031 /* Semicolon means rest of line is a command. */
1032 unsigned int len = strlen (cmdleft);
1034 cmds_started = fileinfo.lineno;
1036 /* Add this command line to the buffer. */
1037 if (len + 2 > commands_len)
1039 commands_len = (len + 2) * 2;
1040 commands = (char *) xrealloc (commands, commands_len);
1042 bcopy (cmdleft, commands, len);
1043 commands_idx += len;
1044 commands[commands_idx++] = '\n';
1047 continue;
1050 /* We get here except in the case that we just read a rule line.
1051 Record now the last rule we read, so following spurious
1052 commands are properly diagnosed. */
1053 record_waiting_files ();
1054 no_targets = 0;
1057 if (conditionals->if_cmds)
1058 fatal (&fileinfo, _("missing `endif'"));
1060 /* At eof, record the last rule. */
1061 record_waiting_files ();
1063 freebuffer (&lb);
1064 free ((char *) commands);
1065 fclose (infile);
1067 reading_file = 0;
1069 return 1;
1072 /* Execute a `define' directive.
1073 The first line has already been read, and NAME is the name of
1074 the variable to be defined. The following lines remain to be read.
1075 LINENO, INFILE and FILENAME refer to the makefile being read.
1076 The value returned is LINENO, updated for lines read here. */
1078 static void
1079 do_define (name, namelen, origin, infile, flocp)
1080 char *name;
1081 unsigned int namelen;
1082 enum variable_origin origin;
1083 FILE *infile;
1084 struct floc *flocp;
1086 struct linebuffer lb;
1087 unsigned int nlines = 0;
1088 unsigned int length = 100;
1089 char *definition = (char *) xmalloc (100);
1090 register unsigned int idx = 0;
1091 register char *p;
1093 /* Expand the variable name. */
1094 char *var = (char *) alloca (namelen + 1);
1095 bcopy (name, var, namelen);
1096 var[namelen] = '\0';
1097 var = variable_expand (var);
1099 initbuffer (&lb);
1100 while (!feof (infile))
1102 unsigned int len;
1104 flocp->lineno += nlines;
1105 nlines = readline (&lb, infile, flocp);
1107 collapse_continuations (lb.buffer);
1109 p = next_token (lb.buffer);
1110 len = strlen (p);
1111 if ((len == 5 || (len > 5 && isblank ((unsigned char)p[5])))
1112 && strneq (p, "endef", 5))
1114 p += 5;
1115 remove_comments (p);
1116 if (*next_token (p) != '\0')
1117 error (flocp, _("Extraneous text after `endef' directive"));
1118 /* Define the variable. */
1119 if (idx == 0)
1120 definition[0] = '\0';
1121 else
1122 definition[idx - 1] = '\0';
1123 (void) define_variable_loc (var, strlen (var), definition, origin,
1124 1, flocp);
1125 free (definition);
1126 freebuffer (&lb);
1127 return;
1129 else
1131 len = strlen (lb.buffer);
1132 /* Increase the buffer size if necessary. */
1133 if (idx + len + 1 > length)
1135 length = (idx + len) * 2;
1136 definition = (char *) xrealloc (definition, length + 1);
1139 bcopy (lb.buffer, &definition[idx], len);
1140 idx += len;
1141 /* Separate lines with a newline. */
1142 definition[idx++] = '\n';
1146 /* No `endef'!! */
1147 fatal (flocp, _("missing `endef', unterminated `define'"));
1149 /* NOTREACHED */
1150 return;
1153 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1154 "ifneq", "else" and "endif".
1155 LINE is the input line, with the command as its first word.
1157 FILENAME and LINENO are the filename and line number in the
1158 current makefile. They are used for error messages.
1160 Value is -1 if the line is invalid,
1161 0 if following text should be interpreted,
1162 1 if following text should be ignored. */
1164 static int
1165 conditional_line (line, flocp)
1166 char *line;
1167 const struct floc *flocp;
1169 int notdef;
1170 char *cmdname;
1171 register unsigned int i;
1173 if (*line == 'i')
1175 /* It's an "if..." command. */
1176 notdef = line[2] == 'n';
1177 if (notdef)
1179 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
1180 line += cmdname[3] == 'd' ? 7 : 6;
1182 else
1184 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
1185 line += cmdname[2] == 'd' ? 6 : 5;
1188 else
1190 /* It's an "else" or "endif" command. */
1191 notdef = line[1] == 'n';
1192 cmdname = notdef ? "endif" : "else";
1193 line += notdef ? 5 : 4;
1196 line = next_token (line);
1198 if (*cmdname == 'e')
1200 if (*line != '\0')
1201 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1202 /* "Else" or "endif". */
1203 if (conditionals->if_cmds == 0)
1204 fatal (flocp, _("extraneous `%s'"), cmdname);
1205 /* NOTDEF indicates an `endif' command. */
1206 if (notdef)
1207 --conditionals->if_cmds;
1208 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1209 fatal (flocp, _("only one `else' per conditional"));
1210 else
1212 /* Toggle the state of ignorance. */
1213 conditionals->ignoring[conditionals->if_cmds - 1]
1214 = !conditionals->ignoring[conditionals->if_cmds - 1];
1215 /* Record that we have seen an `else' in this conditional.
1216 A second `else' will be erroneous. */
1217 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1219 for (i = 0; i < conditionals->if_cmds; ++i)
1220 if (conditionals->ignoring[i])
1221 return 1;
1222 return 0;
1225 if (conditionals->allocated == 0)
1227 conditionals->allocated = 5;
1228 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1229 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1232 ++conditionals->if_cmds;
1233 if (conditionals->if_cmds > conditionals->allocated)
1235 conditionals->allocated += 5;
1236 conditionals->ignoring = (char *)
1237 xrealloc (conditionals->ignoring, conditionals->allocated);
1238 conditionals->seen_else = (char *)
1239 xrealloc (conditionals->seen_else, conditionals->allocated);
1242 /* Record that we have seen an `if...' but no `else' so far. */
1243 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1245 /* Search through the stack to see if we're already ignoring. */
1246 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1247 if (conditionals->ignoring[i])
1249 /* We are already ignoring, so just push a level
1250 to match the next "else" or "endif", and keep ignoring.
1251 We don't want to expand variables in the condition. */
1252 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1253 return 1;
1256 if (cmdname[notdef ? 3 : 2] == 'd')
1258 /* "Ifdef" or "ifndef". */
1259 struct variable *v;
1260 register char *p = end_of_token (line);
1261 i = p - line;
1262 p = next_token (p);
1263 if (*p != '\0')
1264 return -1;
1265 v = lookup_variable (line, i);
1266 conditionals->ignoring[conditionals->if_cmds - 1]
1267 = (v != 0 && *v->value != '\0') == notdef;
1269 else
1271 /* "Ifeq" or "ifneq". */
1272 char *s1, *s2;
1273 unsigned int len;
1274 char termin = *line == '(' ? ',' : *line;
1276 if (termin != ',' && termin != '"' && termin != '\'')
1277 return -1;
1279 s1 = ++line;
1280 /* Find the end of the first string. */
1281 if (termin == ',')
1283 register int count = 0;
1284 for (; *line != '\0'; ++line)
1285 if (*line == '(')
1286 ++count;
1287 else if (*line == ')')
1288 --count;
1289 else if (*line == ',' && count <= 0)
1290 break;
1292 else
1293 while (*line != '\0' && *line != termin)
1294 ++line;
1296 if (*line == '\0')
1297 return -1;
1299 if (termin == ',')
1301 /* Strip blanks after the first string. */
1302 char *p = line++;
1303 while (isblank ((unsigned char)p[-1]))
1304 --p;
1305 *p = '\0';
1307 else
1308 *line++ = '\0';
1310 s2 = variable_expand (s1);
1311 /* We must allocate a new copy of the expanded string because
1312 variable_expand re-uses the same buffer. */
1313 len = strlen (s2);
1314 s1 = (char *) alloca (len + 1);
1315 bcopy (s2, s1, len + 1);
1317 if (termin != ',')
1318 /* Find the start of the second string. */
1319 line = next_token (line);
1321 termin = termin == ',' ? ')' : *line;
1322 if (termin != ')' && termin != '"' && termin != '\'')
1323 return -1;
1325 /* Find the end of the second string. */
1326 if (termin == ')')
1328 register int count = 0;
1329 s2 = next_token (line);
1330 for (line = s2; *line != '\0'; ++line)
1332 if (*line == '(')
1333 ++count;
1334 else if (*line == ')')
1336 if (count <= 0)
1337 break;
1338 else
1339 --count;
1343 else
1345 ++line;
1346 s2 = line;
1347 while (*line != '\0' && *line != termin)
1348 ++line;
1351 if (*line == '\0')
1352 return -1;
1354 *line = '\0';
1355 line = next_token (++line);
1356 if (*line != '\0')
1357 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1359 s2 = variable_expand (s2);
1360 conditionals->ignoring[conditionals->if_cmds - 1]
1361 = streq (s1, s2) == notdef;
1364 /* Search through the stack to see if we're ignoring. */
1365 for (i = 0; i < conditionals->if_cmds; ++i)
1366 if (conditionals->ignoring[i])
1367 return 1;
1368 return 0;
1371 /* Remove duplicate dependencies in CHAIN. */
1373 void
1374 uniquize_deps (chain)
1375 struct dep *chain;
1377 register struct dep *d;
1379 /* Make sure that no dependencies are repeated. This does not
1380 really matter for the purpose of updating targets, but it
1381 might make some names be listed twice for $^ and $?. */
1383 for (d = chain; d != 0; d = d->next)
1385 struct dep *last, *next;
1387 last = d;
1388 next = d->next;
1389 while (next != 0)
1390 if (streq (dep_name (d), dep_name (next)))
1392 struct dep *n = next->next;
1393 last->next = n;
1394 if (next->name != 0 && next->name != d->name)
1395 free (next->name);
1396 if (next != d)
1397 free ((char *) next);
1398 next = n;
1400 else
1402 last = next;
1403 next = next->next;
1408 /* Record target-specific variable values for files FILENAMES.
1409 TWO_COLON is nonzero if a double colon was used.
1411 The links of FILENAMES are freed, and so are any names in it
1412 that are not incorporated into other data structures.
1414 If the target is a pattern, add the variable to the pattern-specific
1415 variable value list. */
1417 static void
1418 record_target_var (filenames, defn, two_colon, origin, flocp)
1419 struct nameseq *filenames;
1420 char *defn;
1421 int two_colon;
1422 enum variable_origin origin;
1423 const struct floc *flocp;
1425 struct nameseq *nextf;
1426 struct variable_set_list *global;
1428 global = current_variable_set_list;
1430 /* If the variable is an append version, store that but treat it as a
1431 normal recursive variable. */
1433 for (; filenames != 0; filenames = nextf)
1435 struct variable *v;
1436 register char *name = filenames->name;
1437 struct variable_set_list *vlist;
1438 char *fname;
1439 char *percent;
1441 nextf = filenames->next;
1442 free ((char *) filenames);
1444 /* If it's a pattern target, then add it to the pattern-specific
1445 variable list. */
1446 percent = find_percent (name);
1447 if (percent)
1449 struct pattern_var *p;
1451 /* Get a reference for this pattern-specific variable struct. */
1452 p = create_pattern_var(name, percent);
1453 vlist = p->vars;
1454 fname = p->target;
1456 else
1458 struct file *f;
1460 /* Get a file reference for this file, and initialize it. */
1461 f = enter_file (name);
1462 initialize_file_variables (f, 1);
1463 vlist = f->variables;
1464 fname = f->name;
1467 /* Make the new variable context current and define the variable. */
1468 current_variable_set_list = vlist;
1469 v = try_variable_definition (flocp, defn, origin, 1);
1470 if (!v)
1471 error (flocp, _("Malformed per-target variable definition"));
1472 v->per_target = 1;
1474 /* If it's not an override, check to see if there was a command-line
1475 setting. If so, reset the value. */
1476 if (origin != o_override)
1478 struct variable *gv;
1479 int len = strlen(v->name);
1481 current_variable_set_list = global;
1482 gv = lookup_variable (v->name, len);
1483 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1484 define_variable_in_set (v->name, len, gv->value, gv->origin,
1485 gv->recursive, vlist->set, flocp);
1488 /* Free name if not needed further. */
1489 if (name != fname && (name < fname || name > fname + strlen (fname)))
1490 free (name);
1493 current_variable_set_list = global;
1496 /* Record a description line for files FILENAMES,
1497 with dependencies DEPS, commands to execute described
1498 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1499 TWO_COLON is nonzero if a double colon was used.
1500 If not nil, PATTERN is the `%' pattern to make this
1501 a static pattern rule, and PATTERN_PERCENT is a pointer
1502 to the `%' within it.
1504 The links of FILENAMES are freed, and so are any names in it
1505 that are not incorporated into other data structures. */
1507 static void
1508 record_files (filenames, pattern, pattern_percent, deps, cmds_started,
1509 commands, commands_idx, two_colon, flocp, set_default)
1510 struct nameseq *filenames;
1511 char *pattern, *pattern_percent;
1512 struct dep *deps;
1513 unsigned int cmds_started;
1514 char *commands;
1515 unsigned int commands_idx;
1516 int two_colon;
1517 const struct floc *flocp;
1518 int set_default;
1520 struct nameseq *nextf;
1521 int implicit = 0;
1522 unsigned int max_targets = 0, target_idx = 0;
1523 char **targets = 0, **target_percents = 0;
1524 struct commands *cmds;
1526 if (commands_idx > 0)
1528 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1529 cmds->fileinfo.filenm = flocp->filenm;
1530 cmds->fileinfo.lineno = cmds_started;
1531 cmds->commands = savestring (commands, commands_idx);
1532 cmds->command_lines = 0;
1534 else
1535 cmds = 0;
1537 for (; filenames != 0; filenames = nextf)
1540 register char *name = filenames->name;
1541 register struct file *f;
1542 register struct dep *d;
1543 struct dep *this;
1544 char *implicit_percent;
1546 nextf = filenames->next;
1547 free (filenames);
1549 implicit_percent = find_percent (name);
1550 implicit |= implicit_percent != 0;
1552 if (implicit && pattern != 0)
1553 fatal (flocp, _("mixed implicit and static pattern rules"));
1555 if (implicit && implicit_percent == 0)
1556 fatal (flocp, _("mixed implicit and normal rules"));
1558 if (implicit)
1560 if (targets == 0)
1562 max_targets = 5;
1563 targets = (char **) xmalloc (5 * sizeof (char *));
1564 target_percents = (char **) xmalloc (5 * sizeof (char *));
1565 target_idx = 0;
1567 else if (target_idx == max_targets - 1)
1569 max_targets += 5;
1570 targets = (char **) xrealloc ((char *) targets,
1571 max_targets * sizeof (char *));
1572 target_percents
1573 = (char **) xrealloc ((char *) target_percents,
1574 max_targets * sizeof (char *));
1576 targets[target_idx] = name;
1577 target_percents[target_idx] = implicit_percent;
1578 ++target_idx;
1579 continue;
1582 /* If there are multiple filenames, copy the chain DEPS
1583 for all but the last one. It is not safe for the same deps
1584 to go in more than one place in the data base. */
1585 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1587 if (pattern != 0)
1589 /* If this is an extended static rule:
1590 `targets: target%pattern: dep%pattern; cmds',
1591 translate each dependency pattern into a plain filename
1592 using the target pattern and this target's name. */
1593 if (!pattern_matches (pattern, pattern_percent, name))
1595 /* Give a warning if the rule is meaningless. */
1596 error (flocp,
1597 _("target `%s' doesn't match the target pattern"), name);
1598 this = 0;
1600 else
1602 /* We use patsubst_expand to do the work of translating
1603 the target pattern, the target's name and the dependencies'
1604 patterns into plain dependency names. */
1605 char *buffer = variable_expand ("");
1607 for (d = this; d != 0; d = d->next)
1609 char *o;
1610 char *percent = find_percent (d->name);
1611 if (percent == 0)
1612 continue;
1613 o = patsubst_expand (buffer, name, pattern, d->name,
1614 pattern_percent, percent);
1615 /* If the name expanded to the empty string, that's
1616 illegal. */
1617 if (o == buffer)
1618 fatal (flocp,
1619 _("target `%s' leaves prerequisite pattern empty"),
1620 name);
1621 free (d->name);
1622 d->name = savestring (buffer, o - buffer);
1627 if (!two_colon)
1629 /* Single-colon. Combine these dependencies
1630 with others in file's existing record, if any. */
1631 f = enter_file (name);
1633 if (f->double_colon)
1634 fatal (flocp,
1635 _("target file `%s' has both : and :: entries"), f->name);
1637 /* If CMDS == F->CMDS, this target was listed in this rule
1638 more than once. Just give a warning since this is harmless. */
1639 if (cmds != 0 && cmds == f->cmds)
1640 error (flocp,
1641 _("target `%s' given more than once in the same rule."),
1642 f->name);
1644 /* Check for two single-colon entries both with commands.
1645 Check is_target so that we don't lose on files such as .c.o
1646 whose commands were preinitialized. */
1647 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1649 error (&cmds->fileinfo,
1650 _("warning: overriding commands for target `%s'"),
1651 f->name);
1652 error (&f->cmds->fileinfo,
1653 _("warning: ignoring old commands for target `%s'"),
1654 f->name);
1657 f->is_target = 1;
1659 /* Defining .DEFAULT with no deps or cmds clears it. */
1660 if (f == default_file && this == 0 && cmds == 0)
1661 f->cmds = 0;
1662 if (cmds != 0)
1663 f->cmds = cmds;
1664 /* Defining .SUFFIXES with no dependencies
1665 clears out the list of suffixes. */
1666 if (f == suffix_file && this == 0)
1668 d = f->deps;
1669 while (d != 0)
1671 struct dep *nextd = d->next;
1672 free (d->name);
1673 free ((char *)d);
1674 d = nextd;
1676 f->deps = 0;
1678 else if (f->deps != 0)
1680 /* Add the file's old deps and the new ones in THIS together. */
1682 struct dep *firstdeps, *moredeps;
1683 if (cmds != 0)
1685 /* This is the rule with commands, so put its deps first.
1686 The rationale behind this is that $< expands to the
1687 first dep in the chain, and commands use $< expecting
1688 to get the dep that rule specifies. */
1689 firstdeps = this;
1690 moredeps = f->deps;
1692 else
1694 /* Append the new deps to the old ones. */
1695 firstdeps = f->deps;
1696 moredeps = this;
1699 if (firstdeps == 0)
1700 firstdeps = moredeps;
1701 else
1703 d = firstdeps;
1704 while (d->next != 0)
1705 d = d->next;
1706 d->next = moredeps;
1709 f->deps = firstdeps;
1711 else
1712 f->deps = this;
1714 /* If this is a static pattern rule, set the file's stem to
1715 the part of its name that matched the `%' in the pattern,
1716 so you can use $* in the commands. */
1717 if (pattern != 0)
1719 static char *percent = "%";
1720 char *buffer = variable_expand ("");
1721 char *o = patsubst_expand (buffer, name, pattern, percent,
1722 pattern_percent, percent);
1723 f->stem = savestring (buffer, o - buffer);
1726 else
1728 /* Double-colon. Make a new record
1729 even if the file already has one. */
1730 f = lookup_file (name);
1731 /* Check for both : and :: rules. Check is_target so
1732 we don't lose on default suffix rules or makefiles. */
1733 if (f != 0 && f->is_target && !f->double_colon)
1734 fatal (flocp,
1735 _("target file `%s' has both : and :: entries"), f->name);
1736 f = enter_file (name);
1737 /* If there was an existing entry and it was a double-colon
1738 entry, enter_file will have returned a new one, making it the
1739 prev pointer of the old one, and setting its double_colon
1740 pointer to the first one. */
1741 if (f->double_colon == 0)
1742 /* This is the first entry for this name, so we must
1743 set its double_colon pointer to itself. */
1744 f->double_colon = f;
1745 f->is_target = 1;
1746 f->deps = this;
1747 f->cmds = cmds;
1750 /* Free name if not needed further. */
1751 if (f != 0 && name != f->name
1752 && (name < f->name || name > f->name + strlen (f->name)))
1754 free (name);
1755 name = f->name;
1758 /* See if this is first target seen whose name does
1759 not start with a `.', unless it contains a slash. */
1760 if (default_goal_file == 0 && set_default
1761 && (*name != '.' || strchr (name, '/') != 0
1762 #if defined(__MSDOS__) || defined(WINDOWS32)
1763 || strchr (name, '\\') != 0
1764 #endif
1767 int reject = 0;
1769 /* If this file is a suffix, don't
1770 let it be the default goal file. */
1772 for (d = suffix_file->deps; d != 0; d = d->next)
1774 register struct dep *d2;
1775 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1777 reject = 1;
1778 break;
1780 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1782 register unsigned int len = strlen (dep_name (d2));
1783 if (!strneq (name, dep_name (d2), len))
1784 continue;
1785 if (streq (name + len, dep_name (d)))
1787 reject = 1;
1788 break;
1791 if (reject)
1792 break;
1795 if (!reject)
1796 default_goal_file = f;
1800 if (implicit)
1802 targets[target_idx] = 0;
1803 target_percents[target_idx] = 0;
1804 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1805 free ((char *) target_percents);
1809 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1810 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1811 Quoting backslashes are removed from STRING by compacting it into
1812 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1813 one, or nil if there are none. */
1815 char *
1816 find_char_unquote (string, stopchars, blank)
1817 char *string;
1818 char *stopchars;
1819 int blank;
1821 unsigned int string_len = 0;
1822 register char *p = string;
1824 while (1)
1826 while (*p != '\0' && strchr (stopchars, *p) == 0
1827 && (!blank || !isblank ((unsigned char)*p)))
1828 ++p;
1829 if (*p == '\0')
1830 break;
1832 if (p > string && p[-1] == '\\')
1834 /* Search for more backslashes. */
1835 register int i = -2;
1836 while (&p[i] >= string && p[i] == '\\')
1837 --i;
1838 ++i;
1839 /* Only compute the length if really needed. */
1840 if (string_len == 0)
1841 string_len = strlen (string);
1842 /* The number of backslashes is now -I.
1843 Copy P over itself to swallow half of them. */
1844 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1845 p += i / 2;
1846 if (i % 2 == 0)
1847 /* All the backslashes quoted each other; the STOPCHAR was
1848 unquoted. */
1849 return p;
1851 /* The STOPCHAR was quoted by a backslash. Look for another. */
1853 else
1854 /* No backslash in sight. */
1855 return p;
1858 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1859 return 0;
1862 /* Search PATTERN for an unquoted %. */
1864 char *
1865 find_percent (pattern)
1866 char *pattern;
1868 return find_char_unquote (pattern, "%", 0);
1871 /* Parse a string into a sequence of filenames represented as a
1872 chain of struct nameseq's in reverse order and return that chain.
1874 The string is passed as STRINGP, the address of a string pointer.
1875 The string pointer is updated to point at the first character
1876 not parsed, which either is a null char or equals STOPCHAR.
1878 SIZE is how big to construct chain elements.
1879 This is useful if we want them actually to be other structures
1880 that have room for additional info.
1882 If STRIP is nonzero, strip `./'s off the beginning. */
1884 struct nameseq *
1885 parse_file_seq (stringp, stopchar, size, strip)
1886 char **stringp;
1887 int stopchar;
1888 unsigned int size;
1889 int strip;
1891 register struct nameseq *new = 0;
1892 register struct nameseq *new1, *lastnew1;
1893 register char *p = *stringp;
1894 char *q;
1895 char *name;
1896 char stopchars[3];
1898 #ifdef VMS
1899 stopchars[0] = ',';
1900 stopchars[1] = stopchar;
1901 stopchars[2] = '\0';
1902 #else
1903 stopchars[0] = stopchar;
1904 stopchars[1] = '\0';
1905 #endif
1907 while (1)
1909 /* Skip whitespace; see if any more names are left. */
1910 p = next_token (p);
1911 if (*p == '\0')
1912 break;
1913 if (*p == stopchar)
1914 break;
1916 /* Yes, find end of next name. */
1917 q = p;
1918 p = find_char_unquote (q, stopchars, 1);
1919 #ifdef VMS
1920 /* convert comma separated list to space separated */
1921 if (p && *p == ',')
1922 *p =' ';
1923 #endif
1924 #ifdef _AMIGA
1925 if (stopchar == ':' && p && *p == ':'
1926 && !(isspace ((unsigned char)p[1]) || !p[1]
1927 || isspace ((unsigned char)p[-1])))
1929 p = find_char_unquote (p+1, stopchars, 1);
1931 #endif
1932 #if defined(WINDOWS32) || defined(__MSDOS__)
1933 /* For WINDOWS32, skip a "C:\..." or a "C:/..." until we find the
1934 first colon which isn't followed by a slash or a backslash.
1935 Note that tokens separated by spaces should be treated as separate
1936 tokens since make doesn't allow path names with spaces */
1937 if (stopchar == ':')
1938 while (p != 0 && !isspace ((unsigned char)*p) &&
1939 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
1940 p = find_char_unquote (p + 1, stopchars, 1);
1941 #endif
1942 if (p == 0)
1943 p = q + strlen (q);
1945 if (strip)
1946 #ifdef VMS
1947 /* Skip leading `[]'s. */
1948 while (p - q > 2 && q[0] == '[' && q[1] == ']')
1949 #else
1950 /* Skip leading `./'s. */
1951 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1952 #endif
1954 q += 2; /* Skip "./". */
1955 while (q < p && *q == '/')
1956 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1957 ++q;
1960 /* Extract the filename just found, and skip it. */
1962 if (q == p)
1963 /* ".///" was stripped to "". */
1964 #ifdef VMS
1965 continue;
1966 #else
1967 #ifdef _AMIGA
1968 name = savestring ("", 0);
1969 #else
1970 name = savestring ("./", 2);
1971 #endif
1972 #endif
1973 else
1974 #ifdef VMS
1975 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
1976 * to remove this '\' before we can use the filename.
1977 * Savestring called because q may be read-only string constant.
1980 char *qbase = xstrdup (q);
1981 char *pbase = qbase + (p-q);
1982 char *q1 = qbase;
1983 char *q2 = q1;
1984 char *p1 = pbase;
1986 while (q1 != pbase)
1988 if (*q1 == '\\' && *(q1+1) == ':')
1990 q1++;
1991 p1--;
1993 *q2++ = *q1++;
1995 name = savestring (qbase, p1 - qbase);
1996 free (qbase);
1998 #else
1999 name = savestring (q, p - q);
2000 #endif
2002 /* Add it to the front of the chain. */
2003 new1 = (struct nameseq *) xmalloc (size);
2004 new1->name = name;
2005 new1->next = new;
2006 new = new1;
2009 #ifndef NO_ARCHIVES
2011 /* Look for multi-word archive references.
2012 They are indicated by a elt ending with an unmatched `)' and
2013 an elt further down the chain (i.e., previous in the file list)
2014 with an unmatched `(' (e.g., "lib(mem"). */
2016 new1 = new;
2017 lastnew1 = 0;
2018 while (new1 != 0)
2019 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2020 && new1->name[strlen (new1->name) - 1] == ')'
2021 && strchr (new1->name, '(') == 0)
2023 /* NEW1 ends with a `)' but does not contain a `('.
2024 Look back for an elt with an opening `(' but no closing `)'. */
2026 struct nameseq *n = new1->next, *lastn = new1;
2027 char *paren = 0;
2028 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2030 lastn = n;
2031 n = n->next;
2033 if (n != 0
2034 /* Ignore something starting with `(', as that cannot actually
2035 be an archive-member reference (and treating it as such
2036 results in an empty file name, which causes much lossage). */
2037 && n->name[0] != '(')
2039 /* N is the first element in the archive group.
2040 Its name looks like "lib(mem" (with no closing `)'). */
2042 char *libname;
2044 /* Copy "lib(" into LIBNAME. */
2045 ++paren;
2046 libname = (char *) alloca (paren - n->name + 1);
2047 bcopy (n->name, libname, paren - n->name);
2048 libname[paren - n->name] = '\0';
2050 if (*paren == '\0')
2052 /* N was just "lib(", part of something like "lib( a b)".
2053 Edit it out of the chain and free its storage. */
2054 lastn->next = n->next;
2055 free (n->name);
2056 free ((char *) n);
2057 /* LASTN->next is the new stopping elt for the loop below. */
2058 n = lastn->next;
2060 else
2062 /* Replace N's name with the full archive reference. */
2063 name = concat (libname, paren, ")");
2064 free (n->name);
2065 n->name = name;
2068 if (new1->name[1] == '\0')
2070 /* NEW1 is just ")", part of something like "lib(a b )".
2071 Omit it from the chain and free its storage. */
2072 if (lastnew1 == 0)
2073 new = new1->next;
2074 else
2075 lastnew1->next = new1->next;
2076 lastn = new1;
2077 new1 = new1->next;
2078 free (lastn->name);
2079 free ((char *) lastn);
2081 else
2083 /* Replace also NEW1->name, which already has closing `)'. */
2084 name = concat (libname, new1->name, "");
2085 free (new1->name);
2086 new1->name = name;
2087 new1 = new1->next;
2090 /* Trace back from NEW1 (the end of the list) until N
2091 (the beginning of the list), rewriting each name
2092 with the full archive reference. */
2094 while (new1 != n)
2096 name = concat (libname, new1->name, ")");
2097 free (new1->name);
2098 new1->name = name;
2099 lastnew1 = new1;
2100 new1 = new1->next;
2103 else
2105 /* No frobnication happening. Just step down the list. */
2106 lastnew1 = new1;
2107 new1 = new1->next;
2110 else
2112 lastnew1 = new1;
2113 new1 = new1->next;
2116 #endif
2118 *stringp = p;
2119 return new;
2122 /* Read a line of text from STREAM into LINEBUFFER.
2123 Combine continuation lines into one line.
2124 Return the number of actual lines read (> 1 if hacked continuation lines).
2127 static unsigned long
2128 readline (linebuffer, stream, flocp)
2129 struct linebuffer *linebuffer;
2130 FILE *stream;
2131 const struct floc *flocp;
2133 char *buffer = linebuffer->buffer;
2134 register char *p = linebuffer->buffer;
2135 register char *end = p + linebuffer->size;
2136 register int len, lastlen = 0;
2137 register char *p2;
2138 register unsigned int nlines = 0;
2139 register int backslash;
2141 *p = '\0';
2143 while (fgets (p, end - p, stream) != 0)
2145 len = strlen (p);
2146 if (len == 0)
2148 /* This only happens when the first thing on the line is a '\0'.
2149 It is a pretty hopeless case, but (wonder of wonders) Athena
2150 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2151 There is nothing really to be done; we synthesize a newline so
2152 the following line doesn't appear to be part of this line. */
2153 error (flocp, _("warning: NUL character seen; rest of line ignored"));
2154 p[0] = '\n';
2155 len = 1;
2158 p += len;
2159 if (p[-1] != '\n')
2161 /* Probably ran out of buffer space. */
2162 register unsigned int p_off = p - buffer;
2163 linebuffer->size *= 2;
2164 buffer = (char *) xrealloc (buffer, linebuffer->size);
2165 p = buffer + p_off;
2166 end = buffer + linebuffer->size;
2167 linebuffer->buffer = buffer;
2168 *p = '\0';
2169 lastlen = len;
2170 continue;
2173 ++nlines;
2175 #if !defined(WINDOWS32) && !defined(__MSDOS__)
2176 /* Check to see if the line was really ended with CRLF; if so ignore
2177 the CR. */
2178 if (len > 1 && p[-2] == '\r')
2180 --len;
2181 --p;
2182 p[-1] = '\n';
2184 #endif
2186 if (len == 1 && p > buffer)
2187 /* P is pointing at a newline and it's the beginning of
2188 the buffer returned by the last fgets call. However,
2189 it is not necessarily the beginning of a line if P is
2190 pointing past the beginning of the holding buffer.
2191 If the buffer was just enlarged (right before the newline),
2192 we must account for that, so we pretend that the two lines
2193 were one line. */
2194 len += lastlen;
2195 lastlen = len;
2196 backslash = 0;
2197 for (p2 = p - 2; --len > 0; --p2)
2199 if (*p2 == '\\')
2200 backslash = !backslash;
2201 else
2202 break;
2205 if (!backslash)
2207 p[-1] = '\0';
2208 break;
2211 if (end - p <= 1)
2213 /* Enlarge the buffer. */
2214 register unsigned int p_off = p - buffer;
2215 linebuffer->size *= 2;
2216 buffer = (char *) xrealloc (buffer, linebuffer->size);
2217 p = buffer + p_off;
2218 end = buffer + linebuffer->size;
2219 linebuffer->buffer = buffer;
2223 if (ferror (stream))
2224 pfatal_with_name (flocp->filenm);
2226 return nlines;
2229 /* Parse the next "makefile word" from the input buffer, and return info
2230 about it.
2232 A "makefile word" is one of:
2234 w_bogus Should never happen
2235 w_eol End of input
2236 w_static A static word; cannot be expanded
2237 w_variable A word containing one or more variables/functions
2238 w_colon A colon
2239 w_dcolon A double-colon
2240 w_semicolon A semicolon
2241 w_comment A comment character
2242 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2244 Note that this function is only used when reading certain parts of the
2245 makefile. Don't use it where special rules hold sway (RHS of a variable,
2246 in a command list, etc.) */
2248 static enum make_word_type
2249 get_next_mword (buffer, delim, startp, length)
2250 char *buffer;
2251 char *delim;
2252 char **startp;
2253 unsigned int *length;
2255 enum make_word_type wtype = w_bogus;
2256 char *p = buffer, *beg;
2257 char c;
2259 /* Skip any leading whitespace. */
2260 while (isblank ((unsigned char)*p))
2261 ++p;
2263 beg = p;
2264 c = *(p++);
2265 switch (c)
2267 case '\0':
2268 wtype = w_eol;
2269 break;
2271 case '#':
2272 wtype = w_comment;
2273 break;
2275 case ';':
2276 wtype = w_semicolon;
2277 break;
2279 case '=':
2280 wtype = w_varassign;
2281 break;
2283 case ':':
2284 wtype = w_colon;
2285 switch (*p)
2287 case ':':
2288 ++p;
2289 wtype = w_dcolon;
2290 break;
2292 case '=':
2293 ++p;
2294 wtype = w_varassign;
2295 break;
2297 break;
2299 case '+':
2300 case '?':
2301 if (*p == '=')
2303 ++p;
2304 wtype = w_varassign;
2305 break;
2308 default:
2309 if (delim && strchr (delim, c))
2310 wtype = w_static;
2311 break;
2314 /* Did we find something? If so, return now. */
2315 if (wtype != w_bogus)
2316 goto done;
2318 /* This is some non-operator word. A word consists of the longest
2319 string of characters that doesn't contain whitespace, one of [:=#],
2320 or [?+]=, or one of the chars in the DELIM string. */
2322 /* We start out assuming a static word; if we see a variable we'll
2323 adjust our assumptions then. */
2324 wtype = w_static;
2326 /* We already found the first value of "c", above. */
2327 while (1)
2329 char closeparen;
2330 int count;
2332 switch (c)
2334 case '\0':
2335 case ' ':
2336 case '\t':
2337 case '=':
2338 case '#':
2339 goto done_word;
2341 case ':':
2342 #if defined(__MSDOS__) || defined(WINDOWS32)
2343 /* A word CAN include a colon in its drive spec. The drive
2344 spec is allowed either at the beginning of a word, or as part
2345 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2346 if (!(p - beg >= 2
2347 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
2348 && (p - beg == 2 || p[-3] == '(')))
2349 #endif
2350 goto done_word;
2352 case '$':
2353 c = *(p++);
2354 if (c == '$')
2355 break;
2357 /* This is a variable reference, so note that it's expandable.
2358 Then read it to the matching close paren. */
2359 wtype = w_variable;
2361 if (c == '(')
2362 closeparen = ')';
2363 else if (c == '{')
2364 closeparen = '}';
2365 else
2366 /* This is a single-letter variable reference. */
2367 break;
2369 for (count=0; *p != '\0'; ++p)
2371 if (*p == c)
2372 ++count;
2373 else if (*p == closeparen && --count < 0)
2375 ++p;
2376 break;
2379 break;
2381 case '?':
2382 case '+':
2383 if (*p == '=')
2384 goto done_word;
2385 break;
2387 case '\\':
2388 switch (*p)
2390 case ':':
2391 case ';':
2392 case '=':
2393 case '\\':
2394 ++p;
2395 break;
2397 break;
2399 default:
2400 if (delim && strchr (delim, c))
2401 goto done_word;
2402 break;
2405 c = *(p++);
2407 done_word:
2408 --p;
2410 done:
2411 if (startp)
2412 *startp = beg;
2413 if (length)
2414 *length = p - beg;
2415 return wtype;
2418 /* Construct the list of include directories
2419 from the arguments and the default list. */
2421 void
2422 construct_include_path (arg_dirs)
2423 char **arg_dirs;
2425 register unsigned int i;
2426 #ifdef VAXC /* just don't ask ... */
2427 stat_t stbuf;
2428 #else
2429 struct stat stbuf;
2430 #endif
2431 /* Table to hold the dirs. */
2433 register unsigned int defsize = (sizeof (default_include_directories)
2434 / sizeof (default_include_directories[0]));
2435 register unsigned int max = 5;
2436 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
2437 register unsigned int idx = 0;
2439 #ifdef __MSDOS__
2440 defsize++;
2441 #endif
2443 /* First consider any dirs specified with -I switches.
2444 Ignore dirs that don't exist. */
2446 if (arg_dirs != 0)
2447 while (*arg_dirs != 0)
2449 char *dir = *arg_dirs++;
2451 if (dir[0] == '~')
2453 char *expanded = tilde_expand (dir);
2454 if (expanded != 0)
2455 dir = expanded;
2458 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
2460 if (idx == max - 1)
2462 max += 5;
2463 dirs = (char **)
2464 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
2466 dirs[idx++] = dir;
2468 else if (dir != arg_dirs[-1])
2469 free (dir);
2472 /* Now add at the end the standard default dirs. */
2474 #ifdef __MSDOS__
2476 /* The environment variable $DJDIR holds the root of the
2477 DJGPP directory tree; add ${DJDIR}/include. */
2478 struct variable *djdir = lookup_variable ("DJDIR", 5);
2480 if (djdir)
2482 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2484 strcat (strcpy (defdir, djdir->value), "/include");
2485 dirs[idx++] = defdir;
2488 #endif
2490 for (i = 0; default_include_directories[i] != 0; ++i)
2491 if (stat (default_include_directories[i], &stbuf) == 0
2492 && S_ISDIR (stbuf.st_mode))
2493 dirs[idx++] = default_include_directories[i];
2495 dirs[idx] = 0;
2497 /* Now compute the maximum length of any name in it. */
2499 max_incl_len = 0;
2500 for (i = 0; i < idx; ++i)
2502 unsigned int len = strlen (dirs[i]);
2503 /* If dir name is written with a trailing slash, discard it. */
2504 if (dirs[i][len - 1] == '/')
2505 /* We can't just clobber a null in because it may have come from
2506 a literal string and literal strings may not be writable. */
2507 dirs[i] = savestring (dirs[i], len - 1);
2508 if (len > max_incl_len)
2509 max_incl_len = len;
2512 include_directories = dirs;
2515 /* Expand ~ or ~USER at the beginning of NAME.
2516 Return a newly malloc'd string or 0. */
2518 char *
2519 tilde_expand (name)
2520 char *name;
2522 #ifndef VMS
2523 if (name[1] == '/' || name[1] == '\0')
2525 extern char *getenv ();
2526 char *home_dir;
2527 int is_variable;
2530 /* Turn off --warn-undefined-variables while we expand HOME. */
2531 int save = warn_undefined_variables_flag;
2532 warn_undefined_variables_flag = 0;
2534 home_dir = allocated_variable_expand ("$(HOME)");
2536 warn_undefined_variables_flag = save;
2539 is_variable = home_dir[0] != '\0';
2540 if (!is_variable)
2542 free (home_dir);
2543 home_dir = getenv ("HOME");
2545 #if !defined(_AMIGA) && !defined(WINDOWS32)
2546 if (home_dir == 0 || home_dir[0] == '\0')
2548 extern char *getlogin ();
2549 char *logname = getlogin ();
2550 home_dir = 0;
2551 if (logname != 0)
2553 struct passwd *p = getpwnam (logname);
2554 if (p != 0)
2555 home_dir = p->pw_dir;
2558 #endif /* !AMIGA && !WINDOWS32 */
2559 if (home_dir != 0)
2561 char *new = concat (home_dir, "", name + 1);
2562 if (is_variable)
2563 free (home_dir);
2564 return new;
2567 #if !defined(_AMIGA) && !defined(WINDOWS32)
2568 else
2570 struct passwd *pwent;
2571 char *userend = strchr (name + 1, '/');
2572 if (userend != 0)
2573 *userend = '\0';
2574 pwent = getpwnam (name + 1);
2575 if (pwent != 0)
2577 if (userend == 0)
2578 return xstrdup (pwent->pw_dir);
2579 else
2580 return concat (pwent->pw_dir, "/", userend + 1);
2582 else if (userend != 0)
2583 *userend = '/';
2585 #endif /* !AMIGA && !WINDOWS32 */
2586 #endif /* !VMS */
2587 return 0;
2590 /* Given a chain of struct nameseq's describing a sequence of filenames,
2591 in reverse of the intended order, return a new chain describing the
2592 result of globbing the filenames. The new chain is in forward order.
2593 The links of the old chain are freed or used in the new chain.
2594 Likewise for the names in the old chain.
2596 SIZE is how big to construct chain elements.
2597 This is useful if we want them actually to be other structures
2598 that have room for additional info. */
2600 struct nameseq *
2601 multi_glob (chain, size)
2602 struct nameseq *chain;
2603 unsigned int size;
2605 extern void dir_setup_glob ();
2606 register struct nameseq *new = 0;
2607 register struct nameseq *old;
2608 struct nameseq *nexto;
2609 glob_t gl;
2611 dir_setup_glob (&gl);
2613 for (old = chain; old != 0; old = nexto)
2615 #ifndef NO_ARCHIVES
2616 char *memname;
2617 #endif
2619 nexto = old->next;
2621 if (old->name[0] == '~')
2623 char *newname = tilde_expand (old->name);
2624 if (newname != 0)
2626 free (old->name);
2627 old->name = newname;
2631 #ifndef NO_ARCHIVES
2632 if (ar_name (old->name))
2634 /* OLD->name is an archive member reference.
2635 Replace it with the archive file name,
2636 and save the member name in MEMNAME.
2637 We will glob on the archive name and then
2638 reattach MEMNAME later. */
2639 char *arname;
2640 ar_parse_name (old->name, &arname, &memname);
2641 free (old->name);
2642 old->name = arname;
2644 else
2645 memname = 0;
2646 #endif /* !NO_ARCHIVES */
2648 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
2650 case 0: /* Success. */
2652 register int i = gl.gl_pathc;
2653 while (i-- > 0)
2655 #ifndef NO_ARCHIVES
2656 if (memname != 0)
2658 /* Try to glob on MEMNAME within the archive. */
2659 struct nameseq *found
2660 = ar_glob (gl.gl_pathv[i], memname, size);
2661 if (found == 0)
2663 /* No matches. Use MEMNAME as-is. */
2664 struct nameseq *elt
2665 = (struct nameseq *) xmalloc (size);
2666 unsigned int alen = strlen (gl.gl_pathv[i]);
2667 unsigned int mlen = strlen (memname);
2668 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2669 bcopy (gl.gl_pathv[i], elt->name, alen);
2670 elt->name[alen] = '(';
2671 bcopy (memname, &elt->name[alen + 1], mlen);
2672 elt->name[alen + 1 + mlen] = ')';
2673 elt->name[alen + 1 + mlen + 1] = '\0';
2674 elt->next = new;
2675 new = elt;
2677 else
2679 /* Find the end of the FOUND chain. */
2680 struct nameseq *f = found;
2681 while (f->next != 0)
2682 f = f->next;
2684 /* Attach the chain being built to the end of the FOUND
2685 chain, and make FOUND the new NEW chain. */
2686 f->next = new;
2687 new = found;
2690 free (memname);
2692 else
2693 #endif /* !NO_ARCHIVES */
2695 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2696 elt->name = xstrdup (gl.gl_pathv[i]);
2697 elt->next = new;
2698 new = elt;
2701 globfree (&gl);
2702 free (old->name);
2703 free ((char *)old);
2704 break;
2707 case GLOB_NOSPACE:
2708 fatal (NILF, _("virtual memory exhausted"));
2709 break;
2711 default:
2712 old->next = new;
2713 new = old;
2714 break;
2718 return new;