* New config.sub and config.guess
[make.git] / read.c
blob9f35753a17c520b117a070bc9dba81317162925b
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 int using_filename = 0;
297 struct floc fileinfo;
298 char *passed_filename = filename;
300 struct nameseq *filenames = 0;
301 struct dep *deps;
302 unsigned int nlines = 0;
303 int two_colon = 0;
304 char *pattern = 0, *pattern_percent;
306 int makefile_errno;
307 #if defined (WINDOWS32) || defined (__MSDOS__)
308 int check_again;
309 #endif
311 #define record_waiting_files() \
312 do \
314 if (filenames != 0) \
316 int lineno = fileinfo.lineno; \
317 struct floc fi; \
318 fi.filenm = fileinfo.filenm; \
319 fi.lineno = tgts_started; \
320 record_files (filenames, pattern, pattern_percent, deps, \
321 cmds_started, commands, commands_idx, two_colon, \
322 &fi, !(flags & RM_NO_DEFAULT_GOAL)); \
323 using_filename |= commands_idx > 0; \
325 filenames = 0; \
326 commands_idx = 0; \
327 if (pattern) { free(pattern); pattern = 0; } \
328 } while (0)
330 fileinfo.filenm = filename;
331 fileinfo.lineno = 1;
333 pattern_percent = 0;
334 /* cmds_started = fileinfo.lineno; */
336 if (ISDB (DB_VERBOSE))
338 printf (_("Reading makefile `%s'"), fileinfo.filenm);
339 if (flags & RM_NO_DEFAULT_GOAL)
340 printf (_(" (no default goal)"));
341 if (flags & RM_INCLUDED)
342 printf (_(" (search path)"));
343 if (flags & RM_DONTCARE)
344 printf (_(" (don't care)"));
345 if (flags & RM_NO_TILDE)
346 printf (_(" (no ~ expansion)"));
347 puts ("...");
350 /* First, get a stream to read. */
352 /* Expand ~ in FILENAME unless it came from `include',
353 in which case it was already done. */
354 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
356 char *expanded = tilde_expand (filename);
357 if (expanded != 0)
358 filename = expanded;
361 infile = fopen (filename, "r");
362 /* Save the error code so we print the right message later. */
363 makefile_errno = errno;
365 /* If the makefile wasn't found and it's either a makefile from
366 the `MAKEFILES' variable or an included makefile,
367 search the included makefile search path for this makefile. */
368 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
370 register unsigned int i;
371 for (i = 0; include_directories[i] != 0; ++i)
373 char *name = concat (include_directories[i], "/", filename);
374 infile = fopen (name, "r");
375 if (infile == 0)
376 free (name);
377 else
379 filename = name;
380 break;
385 /* Add FILENAME to the chain of read makefiles. */
386 deps = (struct dep *) xmalloc (sizeof (struct dep));
387 deps->next = read_makefiles;
388 read_makefiles = deps;
389 deps->name = 0;
390 deps->file = lookup_file (filename);
391 if (deps->file == 0)
393 deps->file = enter_file (xstrdup (filename));
394 if (flags & RM_DONTCARE)
395 deps->file->dontcare = 1;
397 if (filename != passed_filename)
398 free (filename);
399 filename = deps->file->name;
400 deps->changed = flags;
401 deps = 0;
403 /* If the makefile can't be found at all, give up entirely. */
405 if (infile == 0)
407 /* If we did some searching, errno has the error from the last
408 attempt, rather from FILENAME itself. Restore it in case the
409 caller wants to use it in a message. */
410 errno = makefile_errno;
411 return 0;
414 reading_file = &fileinfo;
416 /* Loop over lines in the file.
417 The strategy is to accumulate target names in FILENAMES, dependencies
418 in DEPS and commands in COMMANDS. These are used to define a rule
419 when the start of the next rule (or eof) is encountered. */
421 initbuffer (&lb);
422 commands = xmalloc (200);
424 while (!feof (infile))
426 fileinfo.lineno += nlines;
427 nlines = readline (&lb, infile, &fileinfo);
429 /* Check for a shell command line first.
430 If it is not one, we can stop treating tab specially. */
431 if (lb.buffer[0] == '\t')
433 /* This line is a probably shell command. */
434 unsigned int len;
436 if (no_targets)
437 /* Ignore the commands in a rule with no targets. */
438 continue;
440 /* If there is no preceding rule line, don't treat this line
441 as a command, even though it begins with a tab character.
442 SunOS 4 make appears to behave this way. */
444 if (filenames != 0)
446 if (ignoring)
447 /* Yep, this is a shell command, and we don't care. */
448 continue;
450 /* Append this command line to the line being accumulated. */
451 p = lb.buffer;
452 if (commands_idx == 0)
453 cmds_started = fileinfo.lineno;
454 len = strlen (p);
455 if (len + 1 + commands_idx > commands_len)
457 commands_len = (len + 1 + commands_idx) * 2;
458 commands = (char *) xrealloc (commands, commands_len);
460 bcopy (p, &commands[commands_idx], len);
461 commands_idx += len;
462 commands[commands_idx++] = '\n';
464 continue;
468 /* This line is not a shell command line. Don't worry about tabs. */
470 if (collapsed_length < lb.size)
472 collapsed_length = lb.size;
473 if (collapsed != 0)
474 free (collapsed);
475 collapsed = (char *) xmalloc (collapsed_length);
477 strcpy (collapsed, lb.buffer);
478 /* Collapse continuation lines. */
479 collapse_continuations (collapsed);
480 remove_comments (collapsed);
482 /* Compare a word, both length and contents. */
483 #define word1eq(s, l) (len == l && strneq (s, p, l))
484 p = collapsed;
485 while (isspace ((unsigned char)*p))
486 ++p;
487 if (*p == '\0')
488 /* This line is completely empty. */
489 continue;
491 /* Find the end of the first token. Note we don't need to worry about
492 * ":" here since we compare tokens by length (so "export" will never
493 * be equal to "export:").
495 for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
497 len = p2 - p;
499 /* Find the start of the second token. If it's a `:' remember it,
500 since it can't be a preprocessor token--this allows targets named
501 `ifdef', `export', etc. */
502 reading_target = 0;
503 while (isspace ((unsigned char)*p2))
504 ++p2;
505 if (*p2 == '\0')
506 p2 = NULL;
507 else if (p2[0] == ':' && p2[1] == '\0')
509 reading_target = 1;
510 goto skip_conditionals;
513 /* We must first check for conditional and `define' directives before
514 ignoring anything, since they control what we will do with
515 following lines. */
517 if (!in_ignored_define
518 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
519 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
520 || word1eq ("else", 4) || word1eq ("endif", 5)))
522 int i = conditional_line (p, &fileinfo);
523 if (i >= 0)
524 ignoring = i;
525 else
526 fatal (&fileinfo, _("invalid syntax in conditional"));
527 continue;
530 if (word1eq ("endef", 5))
532 if (in_ignored_define)
533 in_ignored_define = 0;
534 else
535 fatal (&fileinfo, _("extraneous `endef'"));
536 continue;
539 if (word1eq ("define", 6))
541 if (ignoring)
542 in_ignored_define = 1;
543 else
545 p2 = next_token (p + 6);
546 if (*p2 == '\0')
547 fatal (&fileinfo, _("empty variable name"));
549 /* Let the variable name be the whole rest of the line,
550 with trailing blanks stripped (comments have already been
551 removed), so it could be a complex variable/function
552 reference that might contain blanks. */
553 p = strchr (p2, '\0');
554 while (isblank (p[-1]))
555 --p;
556 do_define (p2, p - p2, o_file, infile, &fileinfo);
558 continue;
561 if (word1eq ("override", 8))
563 p2 = next_token (p + 8);
564 if (*p2 == '\0')
565 error (&fileinfo, _("empty `override' directive"));
566 if (strneq (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
568 if (ignoring)
569 in_ignored_define = 1;
570 else
572 p2 = next_token (p2 + 6);
573 if (*p2 == '\0')
574 fatal (&fileinfo, _("empty variable name"));
576 /* Let the variable name be the whole rest of the line,
577 with trailing blanks stripped (comments have already been
578 removed), so it could be a complex variable/function
579 reference that might contain blanks. */
580 p = strchr (p2, '\0');
581 while (isblank (p[-1]))
582 --p;
583 do_define (p2, p - p2, o_override, infile, &fileinfo);
586 else if (!ignoring
587 && !try_variable_definition (&fileinfo, p2, o_override, 0))
588 error (&fileinfo, _("invalid `override' directive"));
590 continue;
592 skip_conditionals:
594 if (ignoring)
595 /* Ignore the line. We continue here so conditionals
596 can appear in the middle of a rule. */
597 continue;
599 if (!reading_target && word1eq ("export", 6))
601 struct variable *v;
602 p2 = next_token (p + 6);
603 if (*p2 == '\0')
604 export_all_variables = 1;
605 v = try_variable_definition (&fileinfo, p2, o_file, 0);
606 if (v != 0)
607 v->export = v_export;
608 else
610 unsigned int len;
611 for (p = find_next_token (&p2, &len); p != 0;
612 p = find_next_token (&p2, &len))
614 v = lookup_variable (p, len);
615 if (v == 0)
616 v = define_variable_loc (p, len, "", o_file, 0, &fileinfo);
617 v->export = v_export;
621 else if (!reading_target && word1eq ("unexport", 8))
623 unsigned int len;
624 struct variable *v;
625 p2 = next_token (p + 8);
626 if (*p2 == '\0')
627 export_all_variables = 0;
628 for (p = find_next_token (&p2, &len); p != 0;
629 p = find_next_token (&p2, &len))
631 v = lookup_variable (p, len);
632 if (v == 0)
633 v = define_variable_loc (p, len, "", o_file, 0, &fileinfo);
634 v->export = v_noexport;
637 else if (word1eq ("vpath", 5))
639 char *pattern;
640 unsigned int len;
641 p2 = variable_expand (p + 5);
642 p = find_next_token (&p2, &len);
643 if (p != 0)
645 pattern = savestring (p, len);
646 p = find_next_token (&p2, &len);
647 /* No searchpath means remove all previous
648 selective VPATH's with the same pattern. */
650 else
651 /* No pattern means remove all previous selective VPATH's. */
652 pattern = 0;
653 construct_vpath_list (pattern, p);
654 if (pattern != 0)
655 free (pattern);
657 else if (word1eq ("include", 7) || word1eq ("-include", 8)
658 || word1eq ("sinclude", 8))
660 /* We have found an `include' line specifying a nested
661 makefile to be read at this point. */
662 struct conditionals *save, new_conditionals;
663 struct nameseq *files;
664 /* "-include" (vs "include") says no error if the file does not
665 exist. "sinclude" is an alias for this from SGI. */
666 int noerror = p[0] != 'i';
668 p = allocated_variable_expand (next_token (p + (noerror ? 8 : 7)));
669 if (*p == '\0')
671 error (&fileinfo,
672 _("no file name for `%sinclude'"), noerror ? "-" : "");
673 continue;
676 /* Parse the list of file names. */
677 p2 = p;
678 files = multi_glob (parse_file_seq (&p2, '\0',
679 sizeof (struct nameseq),
681 sizeof (struct nameseq));
682 free (p);
684 /* Save the state of conditionals and start
685 the included makefile with a clean slate. */
686 save = conditionals;
687 bzero ((char *) &new_conditionals, sizeof new_conditionals);
688 conditionals = &new_conditionals;
690 /* Record the rules that are waiting so they will determine
691 the default goal before those in the included makefile. */
692 record_waiting_files ();
694 /* Read each included makefile. */
695 while (files != 0)
697 struct nameseq *next = files->next;
698 char *name = files->name;
699 int r;
701 free ((char *)files);
702 files = next;
704 r = read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
705 | (noerror ? RM_DONTCARE : 0)));
706 if (!r && !noerror)
707 error (&fileinfo, "%s: %s", name, strerror (errno));
709 if (r < 2)
710 free (name);
713 /* Free any space allocated by conditional_line. */
714 if (conditionals->ignoring)
715 free (conditionals->ignoring);
716 if (conditionals->seen_else)
717 free (conditionals->seen_else);
719 /* Restore state. */
720 conditionals = save;
721 reading_file = &fileinfo;
723 #undef word1eq
724 else if (try_variable_definition (&fileinfo, p, o_file, 0))
725 /* This line has been dealt with. */
727 else if (lb.buffer[0] == '\t')
729 p = collapsed; /* Ignore comments. */
730 while (isblank (*p))
731 ++p;
732 if (*p == '\0')
733 /* The line is completely blank; that is harmless. */
734 continue;
735 /* This line starts with a tab but was not caught above
736 because there was no preceding target, and the line
737 might have been usable as a variable definition.
738 But now it is definitely lossage. */
739 fatal(&fileinfo, _("commands commence before first target"));
741 else
743 /* This line describes some target files. This is complicated by
744 the existence of target-specific variables, because we can't
745 expand the entire line until we know if we have one or not. So
746 we expand the line word by word until we find the first `:',
747 then check to see if it's a target-specific variable.
749 In this algorithm, `lb_next' will point to the beginning of the
750 unexpanded parts of the input buffer, while `p2' points to the
751 parts of the expanded buffer we haven't searched yet. */
753 enum make_word_type wtype;
754 enum variable_origin v_origin;
755 char *cmdleft, *lb_next;
756 unsigned int len, plen = 0;
757 char *colonp;
759 /* Record the previous rule. */
761 record_waiting_files ();
762 tgts_started = fileinfo.lineno;
764 /* Search the line for an unquoted ; that is not after an
765 unquoted #. */
766 cmdleft = find_char_unquote (lb.buffer, ";#", 0);
767 if (cmdleft != 0 && *cmdleft == '#')
769 /* We found a comment before a semicolon. */
770 *cmdleft = '\0';
771 cmdleft = 0;
773 else if (cmdleft != 0)
774 /* Found one. Cut the line short there before expanding it. */
775 *(cmdleft++) = '\0';
777 collapse_continuations (lb.buffer);
779 /* We can't expand the entire line, since if it's a per-target
780 variable we don't want to expand it. So, walk from the
781 beginning, expanding as we go, and looking for "interesting"
782 chars. The first word is always expandable. */
783 wtype = get_next_mword(lb.buffer, NULL, &lb_next, &len);
784 switch (wtype)
786 case w_eol:
787 if (cmdleft != 0)
788 fatal(&fileinfo, _("missing rule before commands"));
789 /* This line contained something but turned out to be nothing
790 but whitespace (a comment?). */
791 continue;
793 case w_colon:
794 case w_dcolon:
795 /* We accept and ignore rules without targets for
796 compatibility with SunOS 4 make. */
797 no_targets = 1;
798 continue;
800 default:
801 break;
804 p2 = variable_expand_string(NULL, lb_next, len);
805 while (1)
807 lb_next += len;
808 if (cmdleft == 0)
810 /* Look for a semicolon in the expanded line. */
811 cmdleft = find_char_unquote (p2, ";", 0);
813 if (cmdleft != 0)
815 unsigned long p2_off = p2 - variable_buffer;
816 unsigned long cmd_off = cmdleft - variable_buffer;
817 char *pend = p2 + strlen(p2);
819 /* Append any remnants of lb, then cut the line short
820 at the semicolon. */
821 *cmdleft = '\0';
823 /* One school of thought says that you shouldn't expand
824 here, but merely copy, since now you're beyond a ";"
825 and into a command script. However, the old parser
826 expanded the whole line, so we continue that for
827 backwards-compatiblity. Also, it wouldn't be
828 entirely consistent, since we do an unconditional
829 expand below once we know we don't have a
830 target-specific variable. */
831 (void)variable_expand_string(pend, lb_next, (long)-1);
832 lb_next += strlen(lb_next);
833 p2 = variable_buffer + p2_off;
834 cmdleft = variable_buffer + cmd_off + 1;
838 colonp = find_char_unquote(p2, ":", 0);
839 #if defined(__MSDOS__) || defined(WINDOWS32)
840 /* The drive spec brain-damage strikes again... */
841 /* Note that the only separators of targets in this context
842 are whitespace and a left paren. If others are possible,
843 they should be added to the string in the call to index. */
844 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
845 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
846 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
847 colonp = find_char_unquote(colonp + 1, ":", 0);
848 #endif
849 if (colonp != 0)
850 break;
852 wtype = get_next_mword(lb_next, NULL, &lb_next, &len);
853 if (wtype == w_eol)
854 break;
856 p2 += strlen(p2);
857 *(p2++) = ' ';
858 p2 = variable_expand_string(p2, lb_next, len);
859 /* We don't need to worry about cmdleft here, because if it was
860 found in the variable_buffer the entire buffer has already
861 been expanded... we'll never get here. */
864 p2 = next_token (variable_buffer);
866 /* If the word we're looking at is EOL, see if there's _anything_
867 on the line. If not, a variable expanded to nothing, so ignore
868 it. If so, we can't parse this line so punt. */
869 if (wtype == w_eol)
871 if (*p2 != '\0')
872 /* There's no need to be ivory-tower about this: check for
873 one of the most common bugs found in makefiles... */
874 fatal (&fileinfo, _("missing separator%s"),
875 !strneq(lb.buffer, " ", 8) ? ""
876 : _(" (did you mean TAB instead of 8 spaces?)"));
877 continue;
880 /* Make the colon the end-of-string so we know where to stop
881 looking for targets. */
882 *colonp = '\0';
883 filenames = multi_glob (parse_file_seq (&p2, '\0',
884 sizeof (struct nameseq),
886 sizeof (struct nameseq));
887 *p2 = ':';
889 if (!filenames)
891 /* We accept and ignore rules without targets for
892 compatibility with SunOS 4 make. */
893 no_targets = 1;
894 continue;
896 /* This should never be possible; we handled it above. */
897 assert (*p2 != '\0');
898 ++p2;
900 /* Is this a one-colon or two-colon entry? */
901 two_colon = *p2 == ':';
902 if (two_colon)
903 p2++;
905 /* Test to see if it's a target-specific variable. Copy the rest
906 of the buffer over, possibly temporarily (we'll expand it later
907 if it's not a target-specific variable). PLEN saves the length
908 of the unparsed section of p2, for later. */
909 if (*lb_next != '\0')
911 unsigned int l = p2 - variable_buffer;
912 plen = strlen (p2);
913 (void) variable_buffer_output (p2+plen,
914 lb_next, strlen (lb_next)+1);
915 p2 = variable_buffer + l;
918 /* See if it's an "override" keyword; if so see if what comes after
919 it looks like a variable definition. */
921 wtype = get_next_mword (p2, NULL, &p, &len);
923 v_origin = o_file;
924 if (wtype == w_static && (len == (sizeof ("override")-1)
925 && strneq (p, "override", len)))
927 v_origin = o_override;
928 wtype = get_next_mword (p+len, NULL, &p, &len);
931 if (wtype != w_eol)
932 wtype = get_next_mword (p+len, NULL, NULL, NULL);
934 if (wtype == w_varassign)
936 record_target_var (filenames, p, two_colon, v_origin, &fileinfo);
937 filenames = 0;
938 continue;
941 /* This is a normal target, _not_ a target-specific variable.
942 Unquote any = in the dependency list. */
943 find_char_unquote (lb_next, "=", 0);
945 /* We have some targets, so don't ignore the following commands. */
946 no_targets = 0;
948 /* Expand the dependencies, etc. */
949 if (*lb_next != '\0')
951 unsigned int l = p2 - variable_buffer;
952 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
953 p2 = variable_buffer + l;
955 /* Look for a semicolon in the expanded line. */
956 if (cmdleft == 0)
958 cmdleft = find_char_unquote (p2, ";", 0);
959 if (cmdleft != 0)
960 *(cmdleft++) = '\0';
964 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
965 p = strchr (p2, ':');
966 while (p != 0 && p[-1] == '\\')
968 register char *q = &p[-1];
969 register int backslash = 0;
970 while (*q-- == '\\')
971 backslash = !backslash;
972 if (backslash)
973 p = strchr (p + 1, ':');
974 else
975 break;
977 #ifdef _AMIGA
978 /* Here, the situation is quite complicated. Let's have a look
979 at a couple of targets:
981 install: dev:make
983 dev:make: make
985 dev:make:: xyz
987 The rule is that it's only a target, if there are TWO :'s
988 OR a space around the :.
990 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
991 || isspace ((unsigned char)p[-1])))
992 p = 0;
993 #endif
994 #if defined (WINDOWS32) || defined (__MSDOS__)
995 do {
996 check_again = 0;
997 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
998 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
999 isalpha ((unsigned char)p[-1]) &&
1000 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1001 p = strchr (p + 1, ':');
1002 check_again = 1;
1004 } while (check_again);
1005 #endif
1006 if (p != 0)
1008 struct nameseq *target;
1009 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
1010 ++p2;
1011 if (target == 0)
1012 fatal (&fileinfo, _("missing target pattern"));
1013 else if (target->next != 0)
1014 fatal (&fileinfo, _("multiple target patterns"));
1015 pattern = target->name;
1016 pattern_percent = find_percent (pattern);
1017 if (pattern_percent == 0)
1018 fatal (&fileinfo, _("target pattern contains no `%%'"));
1019 free((char *)target);
1021 else
1022 pattern = 0;
1024 /* Parse the dependencies. */
1025 deps = (struct dep *)
1026 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
1027 sizeof (struct dep));
1029 commands_idx = 0;
1030 if (cmdleft != 0)
1032 /* Semicolon means rest of line is a command. */
1033 unsigned int len = strlen (cmdleft);
1035 cmds_started = fileinfo.lineno;
1037 /* Add this command line to the buffer. */
1038 if (len + 2 > commands_len)
1040 commands_len = (len + 2) * 2;
1041 commands = (char *) xrealloc (commands, commands_len);
1043 bcopy (cmdleft, commands, len);
1044 commands_idx += len;
1045 commands[commands_idx++] = '\n';
1048 continue;
1051 /* We get here except in the case that we just read a rule line.
1052 Record now the last rule we read, so following spurious
1053 commands are properly diagnosed. */
1054 record_waiting_files ();
1055 no_targets = 0;
1058 if (conditionals->if_cmds)
1059 fatal (&fileinfo, _("missing `endif'"));
1061 /* At eof, record the last rule. */
1062 record_waiting_files ();
1064 freebuffer (&lb);
1065 free ((char *) commands);
1066 fclose (infile);
1068 reading_file = 0;
1070 return 1+using_filename;
1073 /* Execute a `define' directive.
1074 The first line has already been read, and NAME is the name of
1075 the variable to be defined. The following lines remain to be read.
1076 LINENO, INFILE and FILENAME refer to the makefile being read.
1077 The value returned is LINENO, updated for lines read here. */
1079 static void
1080 do_define (name, namelen, origin, infile, flocp)
1081 char *name;
1082 unsigned int namelen;
1083 enum variable_origin origin;
1084 FILE *infile;
1085 struct floc *flocp;
1087 struct linebuffer lb;
1088 unsigned int nlines = 0;
1089 unsigned int length = 100;
1090 char *definition = (char *) xmalloc (100);
1091 register unsigned int idx = 0;
1092 register char *p;
1094 /* Expand the variable name. */
1095 char *var = (char *) alloca (namelen + 1);
1096 bcopy (name, var, namelen);
1097 var[namelen] = '\0';
1098 var = variable_expand (var);
1100 initbuffer (&lb);
1101 while (!feof (infile))
1103 unsigned int len;
1105 flocp->lineno += nlines;
1106 nlines = readline (&lb, infile, flocp);
1108 collapse_continuations (lb.buffer);
1110 p = next_token (lb.buffer);
1111 len = strlen (p);
1112 if ((len == 5 || (len > 5 && isblank (p[5])))
1113 && strneq (p, "endef", 5))
1115 p += 5;
1116 remove_comments (p);
1117 if (*next_token (p) != '\0')
1118 error (flocp, _("Extraneous text after `endef' directive"));
1119 /* Define the variable. */
1120 if (idx == 0)
1121 definition[0] = '\0';
1122 else
1123 definition[idx - 1] = '\0';
1124 (void) define_variable_loc (var, strlen (var), definition, origin,
1125 1, flocp);
1126 free (definition);
1127 freebuffer (&lb);
1128 return;
1130 else
1132 len = strlen (lb.buffer);
1133 /* Increase the buffer size if necessary. */
1134 if (idx + len + 1 > length)
1136 length = (idx + len) * 2;
1137 definition = (char *) xrealloc (definition, length + 1);
1140 bcopy (lb.buffer, &definition[idx], len);
1141 idx += len;
1142 /* Separate lines with a newline. */
1143 definition[idx++] = '\n';
1147 /* No `endef'!! */
1148 fatal (flocp, _("missing `endef', unterminated `define'"));
1150 /* NOTREACHED */
1151 return;
1154 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1155 "ifneq", "else" and "endif".
1156 LINE is the input line, with the command as its first word.
1158 FILENAME and LINENO are the filename and line number in the
1159 current makefile. They are used for error messages.
1161 Value is -1 if the line is invalid,
1162 0 if following text should be interpreted,
1163 1 if following text should be ignored. */
1165 static int
1166 conditional_line (line, flocp)
1167 char *line;
1168 const struct floc *flocp;
1170 int notdef;
1171 char *cmdname;
1172 register unsigned int i;
1174 if (*line == 'i')
1176 /* It's an "if..." command. */
1177 notdef = line[2] == 'n';
1178 if (notdef)
1180 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
1181 line += cmdname[3] == 'd' ? 7 : 6;
1183 else
1185 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
1186 line += cmdname[2] == 'd' ? 6 : 5;
1189 else
1191 /* It's an "else" or "endif" command. */
1192 notdef = line[1] == 'n';
1193 cmdname = notdef ? "endif" : "else";
1194 line += notdef ? 5 : 4;
1197 line = next_token (line);
1199 if (*cmdname == 'e')
1201 if (*line != '\0')
1202 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1203 /* "Else" or "endif". */
1204 if (conditionals->if_cmds == 0)
1205 fatal (flocp, _("extraneous `%s'"), cmdname);
1206 /* NOTDEF indicates an `endif' command. */
1207 if (notdef)
1208 --conditionals->if_cmds;
1209 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1210 fatal (flocp, _("only one `else' per conditional"));
1211 else
1213 /* Toggle the state of ignorance. */
1214 conditionals->ignoring[conditionals->if_cmds - 1]
1215 = !conditionals->ignoring[conditionals->if_cmds - 1];
1216 /* Record that we have seen an `else' in this conditional.
1217 A second `else' will be erroneous. */
1218 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1220 for (i = 0; i < conditionals->if_cmds; ++i)
1221 if (conditionals->ignoring[i])
1222 return 1;
1223 return 0;
1226 if (conditionals->allocated == 0)
1228 conditionals->allocated = 5;
1229 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1230 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1233 ++conditionals->if_cmds;
1234 if (conditionals->if_cmds > conditionals->allocated)
1236 conditionals->allocated += 5;
1237 conditionals->ignoring = (char *)
1238 xrealloc (conditionals->ignoring, conditionals->allocated);
1239 conditionals->seen_else = (char *)
1240 xrealloc (conditionals->seen_else, conditionals->allocated);
1243 /* Record that we have seen an `if...' but no `else' so far. */
1244 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1246 /* Search through the stack to see if we're already ignoring. */
1247 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1248 if (conditionals->ignoring[i])
1250 /* We are already ignoring, so just push a level
1251 to match the next "else" or "endif", and keep ignoring.
1252 We don't want to expand variables in the condition. */
1253 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1254 return 1;
1257 if (cmdname[notdef ? 3 : 2] == 'd')
1259 /* "Ifdef" or "ifndef". */
1260 struct variable *v;
1261 register char *p = end_of_token (line);
1262 i = p - line;
1263 p = next_token (p);
1264 if (*p != '\0')
1265 return -1;
1266 v = lookup_variable (line, i);
1267 conditionals->ignoring[conditionals->if_cmds - 1]
1268 = (v != 0 && *v->value != '\0') == notdef;
1270 else
1272 /* "Ifeq" or "ifneq". */
1273 char *s1, *s2;
1274 unsigned int len;
1275 char termin = *line == '(' ? ',' : *line;
1277 if (termin != ',' && termin != '"' && termin != '\'')
1278 return -1;
1280 s1 = ++line;
1281 /* Find the end of the first string. */
1282 if (termin == ',')
1284 register int count = 0;
1285 for (; *line != '\0'; ++line)
1286 if (*line == '(')
1287 ++count;
1288 else if (*line == ')')
1289 --count;
1290 else if (*line == ',' && count <= 0)
1291 break;
1293 else
1294 while (*line != '\0' && *line != termin)
1295 ++line;
1297 if (*line == '\0')
1298 return -1;
1300 if (termin == ',')
1302 /* Strip blanks after the first string. */
1303 char *p = line++;
1304 while (isblank (p[-1]))
1305 --p;
1306 *p = '\0';
1308 else
1309 *line++ = '\0';
1311 s2 = variable_expand (s1);
1312 /* We must allocate a new copy of the expanded string because
1313 variable_expand re-uses the same buffer. */
1314 len = strlen (s2);
1315 s1 = (char *) alloca (len + 1);
1316 bcopy (s2, s1, len + 1);
1318 if (termin != ',')
1319 /* Find the start of the second string. */
1320 line = next_token (line);
1322 termin = termin == ',' ? ')' : *line;
1323 if (termin != ')' && termin != '"' && termin != '\'')
1324 return -1;
1326 /* Find the end of the second string. */
1327 if (termin == ')')
1329 register int count = 0;
1330 s2 = next_token (line);
1331 for (line = s2; *line != '\0'; ++line)
1333 if (*line == '(')
1334 ++count;
1335 else if (*line == ')')
1337 if (count <= 0)
1338 break;
1339 else
1340 --count;
1344 else
1346 ++line;
1347 s2 = line;
1348 while (*line != '\0' && *line != termin)
1349 ++line;
1352 if (*line == '\0')
1353 return -1;
1355 *line = '\0';
1356 line = next_token (++line);
1357 if (*line != '\0')
1358 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1360 s2 = variable_expand (s2);
1361 conditionals->ignoring[conditionals->if_cmds - 1]
1362 = streq (s1, s2) == notdef;
1365 /* Search through the stack to see if we're ignoring. */
1366 for (i = 0; i < conditionals->if_cmds; ++i)
1367 if (conditionals->ignoring[i])
1368 return 1;
1369 return 0;
1372 /* Remove duplicate dependencies in CHAIN. */
1374 void
1375 uniquize_deps (chain)
1376 struct dep *chain;
1378 register struct dep *d;
1380 /* Make sure that no dependencies are repeated. This does not
1381 really matter for the purpose of updating targets, but it
1382 might make some names be listed twice for $^ and $?. */
1384 for (d = chain; d != 0; d = d->next)
1386 struct dep *last, *next;
1388 last = d;
1389 next = d->next;
1390 while (next != 0)
1391 if (streq (dep_name (d), dep_name (next)))
1393 struct dep *n = next->next;
1394 last->next = n;
1395 if (next->name != 0 && next->name != d->name)
1396 free (next->name);
1397 if (next != d)
1398 free ((char *) next);
1399 next = n;
1401 else
1403 last = next;
1404 next = next->next;
1409 /* Record target-specific variable values for files FILENAMES.
1410 TWO_COLON is nonzero if a double colon was used.
1412 The links of FILENAMES are freed, and so are any names in it
1413 that are not incorporated into other data structures.
1415 If the target is a pattern, add the variable to the pattern-specific
1416 variable value list. */
1418 static void
1419 record_target_var (filenames, defn, two_colon, origin, flocp)
1420 struct nameseq *filenames;
1421 char *defn;
1422 int two_colon;
1423 enum variable_origin origin;
1424 const struct floc *flocp;
1426 struct nameseq *nextf;
1427 struct variable_set_list *global;
1429 global = current_variable_set_list;
1431 /* If the variable is an append version, store that but treat it as a
1432 normal recursive variable. */
1434 for (; filenames != 0; filenames = nextf)
1436 struct variable *v;
1437 register char *name = filenames->name;
1438 struct variable_set_list *vlist;
1439 char *fname;
1440 char *percent;
1442 nextf = filenames->next;
1443 free ((char *) filenames);
1445 /* If it's a pattern target, then add it to the pattern-specific
1446 variable list. */
1447 percent = find_percent (name);
1448 if (percent)
1450 struct pattern_var *p;
1452 /* Get a reference for this pattern-specific variable struct. */
1453 p = create_pattern_var(name, percent);
1454 vlist = p->vars;
1455 fname = p->target;
1457 else
1459 struct file *f;
1461 /* Get a file reference for this file, and initialize it. */
1462 f = enter_file (name);
1463 initialize_file_variables (f, 1);
1464 vlist = f->variables;
1465 fname = f->name;
1468 /* Make the new variable context current and define the variable. */
1469 current_variable_set_list = vlist;
1470 v = try_variable_definition (flocp, defn, origin, 1);
1471 if (!v)
1472 error (flocp, _("Malformed per-target variable definition"));
1473 v->per_target = 1;
1475 /* If it's not an override, check to see if there was a command-line
1476 setting. If so, reset the value. */
1477 if (origin != o_override)
1479 struct variable *gv;
1480 int len = strlen(v->name);
1482 current_variable_set_list = global;
1483 gv = lookup_variable (v->name, len);
1484 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1485 define_variable_in_set (v->name, len, gv->value, gv->origin,
1486 gv->recursive, vlist->set, flocp);
1489 /* Free name if not needed further. */
1490 if (name != fname && (name < fname || name > fname + strlen (fname)))
1491 free (name);
1494 current_variable_set_list = global;
1497 /* Record a description line for files FILENAMES,
1498 with dependencies DEPS, commands to execute described
1499 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1500 TWO_COLON is nonzero if a double colon was used.
1501 If not nil, PATTERN is the `%' pattern to make this
1502 a static pattern rule, and PATTERN_PERCENT is a pointer
1503 to the `%' within it.
1505 The links of FILENAMES are freed, and so are any names in it
1506 that are not incorporated into other data structures. */
1508 static void
1509 record_files (filenames, pattern, pattern_percent, deps, cmds_started,
1510 commands, commands_idx, two_colon, flocp, set_default)
1511 struct nameseq *filenames;
1512 char *pattern, *pattern_percent;
1513 struct dep *deps;
1514 unsigned int cmds_started;
1515 char *commands;
1516 unsigned int commands_idx;
1517 int two_colon;
1518 const struct floc *flocp;
1519 int set_default;
1521 struct nameseq *nextf;
1522 int implicit = 0;
1523 unsigned int max_targets = 0, target_idx = 0;
1524 char **targets = 0, **target_percents = 0;
1525 struct commands *cmds;
1527 if (commands_idx > 0)
1529 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1530 cmds->fileinfo.filenm = flocp->filenm;
1531 cmds->fileinfo.lineno = cmds_started;
1532 cmds->commands = savestring (commands, commands_idx);
1533 cmds->command_lines = 0;
1535 else
1536 cmds = 0;
1538 for (; filenames != 0; filenames = nextf)
1541 register char *name = filenames->name;
1542 register struct file *f;
1543 register struct dep *d;
1544 struct dep *this;
1545 char *implicit_percent;
1547 nextf = filenames->next;
1548 free (filenames);
1550 implicit_percent = find_percent (name);
1551 implicit |= implicit_percent != 0;
1553 if (implicit && pattern != 0)
1554 fatal (flocp, _("mixed implicit and static pattern rules"));
1556 if (implicit && implicit_percent == 0)
1557 fatal (flocp, _("mixed implicit and normal rules"));
1559 if (implicit)
1561 if (targets == 0)
1563 max_targets = 5;
1564 targets = (char **) xmalloc (5 * sizeof (char *));
1565 target_percents = (char **) xmalloc (5 * sizeof (char *));
1566 target_idx = 0;
1568 else if (target_idx == max_targets - 1)
1570 max_targets += 5;
1571 targets = (char **) xrealloc ((char *) targets,
1572 max_targets * sizeof (char *));
1573 target_percents
1574 = (char **) xrealloc ((char *) target_percents,
1575 max_targets * sizeof (char *));
1577 targets[target_idx] = name;
1578 target_percents[target_idx] = implicit_percent;
1579 ++target_idx;
1580 continue;
1583 /* If there are multiple filenames, copy the chain DEPS
1584 for all but the last one. It is not safe for the same deps
1585 to go in more than one place in the data base. */
1586 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1588 if (pattern != 0)
1590 /* If this is an extended static rule:
1591 `targets: target%pattern: dep%pattern; cmds',
1592 translate each dependency pattern into a plain filename
1593 using the target pattern and this target's name. */
1594 if (!pattern_matches (pattern, pattern_percent, name))
1596 /* Give a warning if the rule is meaningless. */
1597 error (flocp,
1598 _("target `%s' doesn't match the target pattern"), name);
1599 this = 0;
1601 else
1603 /* We use patsubst_expand to do the work of translating
1604 the target pattern, the target's name and the dependencies'
1605 patterns into plain dependency names. */
1606 char *buffer = variable_expand ("");
1608 for (d = this; d != 0; d = d->next)
1610 char *o;
1611 char *percent = find_percent (d->name);
1612 if (percent == 0)
1613 continue;
1614 o = patsubst_expand (buffer, name, pattern, d->name,
1615 pattern_percent, percent);
1616 /* If the name expanded to the empty string, that's
1617 illegal. */
1618 if (o == buffer)
1619 fatal (flocp,
1620 _("target `%s' leaves prerequisite pattern empty"),
1621 name);
1622 free (d->name);
1623 d->name = savestring (buffer, o - buffer);
1628 if (!two_colon)
1630 /* Single-colon. Combine these dependencies
1631 with others in file's existing record, if any. */
1632 f = enter_file (name);
1634 if (f->double_colon)
1635 fatal (flocp,
1636 _("target file `%s' has both : and :: entries"), f->name);
1638 /* If CMDS == F->CMDS, this target was listed in this rule
1639 more than once. Just give a warning since this is harmless. */
1640 if (cmds != 0 && cmds == f->cmds)
1641 error (flocp,
1642 _("target `%s' given more than once in the same rule."),
1643 f->name);
1645 /* Check for two single-colon entries both with commands.
1646 Check is_target so that we don't lose on files such as .c.o
1647 whose commands were preinitialized. */
1648 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1650 error (&cmds->fileinfo,
1651 _("warning: overriding commands for target `%s'"),
1652 f->name);
1653 error (&f->cmds->fileinfo,
1654 _("warning: ignoring old commands for target `%s'"),
1655 f->name);
1658 f->is_target = 1;
1660 /* Defining .DEFAULT with no deps or cmds clears it. */
1661 if (f == default_file && this == 0 && cmds == 0)
1662 f->cmds = 0;
1663 if (cmds != 0)
1664 f->cmds = cmds;
1665 /* Defining .SUFFIXES with no dependencies
1666 clears out the list of suffixes. */
1667 if (f == suffix_file && this == 0)
1669 d = f->deps;
1670 while (d != 0)
1672 struct dep *nextd = d->next;
1673 free (d->name);
1674 free ((char *)d);
1675 d = nextd;
1677 f->deps = 0;
1679 else if (f->deps != 0)
1681 /* Add the file's old deps and the new ones in THIS together. */
1683 struct dep *firstdeps, *moredeps;
1684 if (cmds != 0)
1686 /* This is the rule with commands, so put its deps first.
1687 The rationale behind this is that $< expands to the
1688 first dep in the chain, and commands use $< expecting
1689 to get the dep that rule specifies. */
1690 firstdeps = this;
1691 moredeps = f->deps;
1693 else
1695 /* Append the new deps to the old ones. */
1696 firstdeps = f->deps;
1697 moredeps = this;
1700 if (firstdeps == 0)
1701 firstdeps = moredeps;
1702 else
1704 d = firstdeps;
1705 while (d->next != 0)
1706 d = d->next;
1707 d->next = moredeps;
1710 f->deps = firstdeps;
1712 else
1713 f->deps = this;
1715 /* If this is a static pattern rule, set the file's stem to
1716 the part of its name that matched the `%' in the pattern,
1717 so you can use $* in the commands. */
1718 if (pattern != 0)
1720 static char *percent = "%";
1721 char *buffer = variable_expand ("");
1722 char *o = patsubst_expand (buffer, name, pattern, percent,
1723 pattern_percent, percent);
1724 f->stem = savestring (buffer, o - buffer);
1727 else
1729 /* Double-colon. Make a new record
1730 even if the file already has one. */
1731 f = lookup_file (name);
1732 /* Check for both : and :: rules. Check is_target so
1733 we don't lose on default suffix rules or makefiles. */
1734 if (f != 0 && f->is_target && !f->double_colon)
1735 fatal (flocp,
1736 _("target file `%s' has both : and :: entries"), f->name);
1737 f = enter_file (name);
1738 /* If there was an existing entry and it was a double-colon
1739 entry, enter_file will have returned a new one, making it the
1740 prev pointer of the old one, and setting its double_colon
1741 pointer to the first one. */
1742 if (f->double_colon == 0)
1743 /* This is the first entry for this name, so we must
1744 set its double_colon pointer to itself. */
1745 f->double_colon = f;
1746 f->is_target = 1;
1747 f->deps = this;
1748 f->cmds = cmds;
1751 /* Free name if not needed further. */
1752 if (f != 0 && name != f->name
1753 && (name < f->name || name > f->name + strlen (f->name)))
1755 free (name);
1756 name = f->name;
1759 /* See if this is first target seen whose name does
1760 not start with a `.', unless it contains a slash. */
1761 if (default_goal_file == 0 && set_default
1762 && (*name != '.' || strchr (name, '/') != 0
1763 #if defined(__MSDOS__) || defined(WINDOWS32)
1764 || strchr (name, '\\') != 0
1765 #endif
1768 int reject = 0;
1770 /* If this file is a suffix, don't
1771 let it be the default goal file. */
1773 for (d = suffix_file->deps; d != 0; d = d->next)
1775 register struct dep *d2;
1776 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1778 reject = 1;
1779 break;
1781 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1783 register unsigned int len = strlen (dep_name (d2));
1784 if (!strneq (name, dep_name (d2), len))
1785 continue;
1786 if (streq (name + len, dep_name (d)))
1788 reject = 1;
1789 break;
1792 if (reject)
1793 break;
1796 if (!reject)
1797 default_goal_file = f;
1801 if (implicit)
1803 targets[target_idx] = 0;
1804 target_percents[target_idx] = 0;
1805 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1806 free ((char *) target_percents);
1810 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1811 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1812 Quoting backslashes are removed from STRING by compacting it into
1813 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1814 one, or nil if there are none. */
1816 char *
1817 find_char_unquote (string, stopchars, blank)
1818 char *string;
1819 char *stopchars;
1820 int blank;
1822 unsigned int string_len = 0;
1823 register char *p = string;
1825 while (1)
1827 while (*p != '\0' && strchr (stopchars, *p) == 0
1828 && (!blank || !isblank (*p)))
1829 ++p;
1830 if (*p == '\0')
1831 break;
1833 if (p > string && p[-1] == '\\')
1835 /* Search for more backslashes. */
1836 register int i = -2;
1837 while (&p[i] >= string && p[i] == '\\')
1838 --i;
1839 ++i;
1840 /* Only compute the length if really needed. */
1841 if (string_len == 0)
1842 string_len = strlen (string);
1843 /* The number of backslashes is now -I.
1844 Copy P over itself to swallow half of them. */
1845 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1846 p += i / 2;
1847 if (i % 2 == 0)
1848 /* All the backslashes quoted each other; the STOPCHAR was
1849 unquoted. */
1850 return p;
1852 /* The STOPCHAR was quoted by a backslash. Look for another. */
1854 else
1855 /* No backslash in sight. */
1856 return p;
1859 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1860 return 0;
1863 /* Search PATTERN for an unquoted %. */
1865 char *
1866 find_percent (pattern)
1867 char *pattern;
1869 return find_char_unquote (pattern, "%", 0);
1872 /* Parse a string into a sequence of filenames represented as a
1873 chain of struct nameseq's in reverse order and return that chain.
1875 The string is passed as STRINGP, the address of a string pointer.
1876 The string pointer is updated to point at the first character
1877 not parsed, which either is a null char or equals STOPCHAR.
1879 SIZE is how big to construct chain elements.
1880 This is useful if we want them actually to be other structures
1881 that have room for additional info.
1883 If STRIP is nonzero, strip `./'s off the beginning. */
1885 struct nameseq *
1886 parse_file_seq (stringp, stopchar, size, strip)
1887 char **stringp;
1888 int stopchar;
1889 unsigned int size;
1890 int strip;
1892 register struct nameseq *new = 0;
1893 register struct nameseq *new1, *lastnew1;
1894 register char *p = *stringp;
1895 char *q;
1896 char *name;
1897 char stopchars[3];
1899 #ifdef VMS
1900 stopchars[0] = ',';
1901 stopchars[1] = stopchar;
1902 stopchars[2] = '\0';
1903 #else
1904 stopchars[0] = stopchar;
1905 stopchars[1] = '\0';
1906 #endif
1908 while (1)
1910 /* Skip whitespace; see if any more names are left. */
1911 p = next_token (p);
1912 if (*p == '\0')
1913 break;
1914 if (*p == stopchar)
1915 break;
1917 /* Yes, find end of next name. */
1918 q = p;
1919 p = find_char_unquote (q, stopchars, 1);
1920 #ifdef VMS
1921 /* convert comma separated list to space separated */
1922 if (p && *p == ',')
1923 *p =' ';
1924 #endif
1925 #ifdef _AMIGA
1926 if (stopchar == ':' && p && *p == ':'
1927 && !(isspace ((unsigned char)p[1]) || !p[1]
1928 || isspace ((unsigned char)p[-1])))
1930 p = find_char_unquote (p+1, stopchars, 1);
1932 #endif
1933 #if defined(WINDOWS32) || defined(__MSDOS__)
1934 /* For WINDOWS32, skip a "C:\..." or a "C:/..." until we find the
1935 first colon which isn't followed by a slash or a backslash.
1936 Note that tokens separated by spaces should be treated as separate
1937 tokens since make doesn't allow path names with spaces */
1938 if (stopchar == ':')
1939 while (p != 0 && !isspace ((unsigned char)*p) &&
1940 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
1941 p = find_char_unquote (p + 1, stopchars, 1);
1942 #endif
1943 if (p == 0)
1944 p = q + strlen (q);
1946 if (strip)
1947 #ifdef VMS
1948 /* Skip leading `[]'s. */
1949 while (p - q > 2 && q[0] == '[' && q[1] == ']')
1950 #else
1951 /* Skip leading `./'s. */
1952 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1953 #endif
1955 q += 2; /* Skip "./". */
1956 while (q < p && *q == '/')
1957 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1958 ++q;
1961 /* Extract the filename just found, and skip it. */
1963 if (q == p)
1964 /* ".///" was stripped to "". */
1965 #ifdef VMS
1966 continue;
1967 #else
1968 #ifdef _AMIGA
1969 name = savestring ("", 0);
1970 #else
1971 name = savestring ("./", 2);
1972 #endif
1973 #endif
1974 else
1975 #ifdef VMS
1976 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
1977 * to remove this '\' before we can use the filename.
1978 * Savestring called because q may be read-only string constant.
1981 char *qbase = xstrdup (q);
1982 char *pbase = qbase + (p-q);
1983 char *q1 = qbase;
1984 char *q2 = q1;
1985 char *p1 = pbase;
1987 while (q1 != pbase)
1989 if (*q1 == '\\' && *(q1+1) == ':')
1991 q1++;
1992 p1--;
1994 *q2++ = *q1++;
1996 name = savestring (qbase, p1 - qbase);
1997 free (qbase);
1999 #else
2000 name = savestring (q, p - q);
2001 #endif
2003 /* Add it to the front of the chain. */
2004 new1 = (struct nameseq *) xmalloc (size);
2005 new1->name = name;
2006 new1->next = new;
2007 new = new1;
2010 #ifndef NO_ARCHIVES
2012 /* Look for multi-word archive references.
2013 They are indicated by a elt ending with an unmatched `)' and
2014 an elt further down the chain (i.e., previous in the file list)
2015 with an unmatched `(' (e.g., "lib(mem"). */
2017 new1 = new;
2018 lastnew1 = 0;
2019 while (new1 != 0)
2020 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2021 && new1->name[strlen (new1->name) - 1] == ')'
2022 && strchr (new1->name, '(') == 0)
2024 /* NEW1 ends with a `)' but does not contain a `('.
2025 Look back for an elt with an opening `(' but no closing `)'. */
2027 struct nameseq *n = new1->next, *lastn = new1;
2028 char *paren = 0;
2029 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2031 lastn = n;
2032 n = n->next;
2034 if (n != 0
2035 /* Ignore something starting with `(', as that cannot actually
2036 be an archive-member reference (and treating it as such
2037 results in an empty file name, which causes much lossage). */
2038 && n->name[0] != '(')
2040 /* N is the first element in the archive group.
2041 Its name looks like "lib(mem" (with no closing `)'). */
2043 char *libname;
2045 /* Copy "lib(" into LIBNAME. */
2046 ++paren;
2047 libname = (char *) alloca (paren - n->name + 1);
2048 bcopy (n->name, libname, paren - n->name);
2049 libname[paren - n->name] = '\0';
2051 if (*paren == '\0')
2053 /* N was just "lib(", part of something like "lib( a b)".
2054 Edit it out of the chain and free its storage. */
2055 lastn->next = n->next;
2056 free (n->name);
2057 free ((char *) n);
2058 /* LASTN->next is the new stopping elt for the loop below. */
2059 n = lastn->next;
2061 else
2063 /* Replace N's name with the full archive reference. */
2064 name = concat (libname, paren, ")");
2065 free (n->name);
2066 n->name = name;
2069 if (new1->name[1] == '\0')
2071 /* NEW1 is just ")", part of something like "lib(a b )".
2072 Omit it from the chain and free its storage. */
2073 if (lastnew1 == 0)
2074 new = new1->next;
2075 else
2076 lastnew1->next = new1->next;
2077 lastn = new1;
2078 new1 = new1->next;
2079 free (lastn->name);
2080 free ((char *) lastn);
2082 else
2084 /* Replace also NEW1->name, which already has closing `)'. */
2085 name = concat (libname, new1->name, "");
2086 free (new1->name);
2087 new1->name = name;
2088 new1 = new1->next;
2091 /* Trace back from NEW1 (the end of the list) until N
2092 (the beginning of the list), rewriting each name
2093 with the full archive reference. */
2095 while (new1 != n)
2097 name = concat (libname, new1->name, ")");
2098 free (new1->name);
2099 new1->name = name;
2100 lastnew1 = new1;
2101 new1 = new1->next;
2104 else
2106 /* No frobnication happening. Just step down the list. */
2107 lastnew1 = new1;
2108 new1 = new1->next;
2111 else
2113 lastnew1 = new1;
2114 new1 = new1->next;
2117 #endif
2119 *stringp = p;
2120 return new;
2123 /* Read a line of text from STREAM into LINEBUFFER.
2124 Combine continuation lines into one line.
2125 Return the number of actual lines read (> 1 if hacked continuation lines).
2128 static unsigned long
2129 readline (linebuffer, stream, flocp)
2130 struct linebuffer *linebuffer;
2131 FILE *stream;
2132 const struct floc *flocp;
2134 char *buffer = linebuffer->buffer;
2135 register char *p = linebuffer->buffer;
2136 register char *end = p + linebuffer->size;
2137 register int len, lastlen = 0;
2138 register char *p2;
2139 register unsigned int nlines = 0;
2140 register int backslash;
2142 *p = '\0';
2144 while (fgets (p, end - p, stream) != 0)
2146 len = strlen (p);
2147 if (len == 0)
2149 /* This only happens when the first thing on the line is a '\0'.
2150 It is a pretty hopeless case, but (wonder of wonders) Athena
2151 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2152 There is nothing really to be done; we synthesize a newline so
2153 the following line doesn't appear to be part of this line. */
2154 error (flocp, _("warning: NUL character seen; rest of line ignored"));
2155 p[0] = '\n';
2156 len = 1;
2159 p += len;
2160 if (p[-1] != '\n')
2162 /* Probably ran out of buffer space. */
2163 register unsigned int p_off = p - buffer;
2164 linebuffer->size *= 2;
2165 buffer = (char *) xrealloc (buffer, linebuffer->size);
2166 p = buffer + p_off;
2167 end = buffer + linebuffer->size;
2168 linebuffer->buffer = buffer;
2169 *p = '\0';
2170 lastlen = len;
2171 continue;
2174 ++nlines;
2176 #if !defined(WINDOWS32) && !defined(__MSDOS__)
2177 /* Check to see if the line was really ended with CRLF; if so ignore
2178 the CR. */
2179 if (len > 1 && p[-2] == '\r')
2181 --len;
2182 --p;
2183 p[-1] = '\n';
2185 #endif
2187 if (len == 1 && p > buffer)
2188 /* P is pointing at a newline and it's the beginning of
2189 the buffer returned by the last fgets call. However,
2190 it is not necessarily the beginning of a line if P is
2191 pointing past the beginning of the holding buffer.
2192 If the buffer was just enlarged (right before the newline),
2193 we must account for that, so we pretend that the two lines
2194 were one line. */
2195 len += lastlen;
2196 lastlen = len;
2197 backslash = 0;
2198 for (p2 = p - 2; --len > 0; --p2)
2200 if (*p2 == '\\')
2201 backslash = !backslash;
2202 else
2203 break;
2206 if (!backslash)
2208 p[-1] = '\0';
2209 break;
2212 if (end - p <= 1)
2214 /* Enlarge the buffer. */
2215 register unsigned int p_off = p - buffer;
2216 linebuffer->size *= 2;
2217 buffer = (char *) xrealloc (buffer, linebuffer->size);
2218 p = buffer + p_off;
2219 end = buffer + linebuffer->size;
2220 linebuffer->buffer = buffer;
2224 if (ferror (stream))
2225 pfatal_with_name (flocp->filenm);
2227 return nlines;
2230 /* Parse the next "makefile word" from the input buffer, and return info
2231 about it.
2233 A "makefile word" is one of:
2235 w_bogus Should never happen
2236 w_eol End of input
2237 w_static A static word; cannot be expanded
2238 w_variable A word containing one or more variables/functions
2239 w_colon A colon
2240 w_dcolon A double-colon
2241 w_semicolon A semicolon
2242 w_comment A comment character
2243 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2245 Note that this function is only used when reading certain parts of the
2246 makefile. Don't use it where special rules hold sway (RHS of a variable,
2247 in a command list, etc.) */
2249 static enum make_word_type
2250 get_next_mword (buffer, delim, startp, length)
2251 char *buffer;
2252 char *delim;
2253 char **startp;
2254 unsigned int *length;
2256 enum make_word_type wtype = w_bogus;
2257 char *p = buffer, *beg;
2258 char c;
2260 /* Skip any leading whitespace. */
2261 while (isblank(*p))
2262 ++p;
2264 beg = p;
2265 c = *(p++);
2266 switch (c)
2268 case '\0':
2269 wtype = w_eol;
2270 break;
2272 case '#':
2273 wtype = w_comment;
2274 break;
2276 case ';':
2277 wtype = w_semicolon;
2278 break;
2280 case '=':
2281 wtype = w_varassign;
2282 break;
2284 case ':':
2285 wtype = w_colon;
2286 switch (*p)
2288 case ':':
2289 ++p;
2290 wtype = w_dcolon;
2291 break;
2293 case '=':
2294 ++p;
2295 wtype = w_varassign;
2296 break;
2298 break;
2300 case '+':
2301 case '?':
2302 if (*p == '=')
2304 ++p;
2305 wtype = w_varassign;
2306 break;
2309 default:
2310 if (delim && strchr (delim, c))
2311 wtype = w_static;
2312 break;
2315 /* Did we find something? If so, return now. */
2316 if (wtype != w_bogus)
2317 goto done;
2319 /* This is some non-operator word. A word consists of the longest
2320 string of characters that doesn't contain whitespace, one of [:=#],
2321 or [?+]=, or one of the chars in the DELIM string. */
2323 /* We start out assuming a static word; if we see a variable we'll
2324 adjust our assumptions then. */
2325 wtype = w_static;
2327 /* We already found the first value of "c", above. */
2328 while (1)
2330 char closeparen;
2331 int count;
2333 switch (c)
2335 case '\0':
2336 case ' ':
2337 case '\t':
2338 case '=':
2339 case '#':
2340 goto done_word;
2342 case ':':
2343 #if defined(__MSDOS__) || defined(WINDOWS32)
2344 /* A word CAN include a colon in its drive spec. The drive
2345 spec is allowed either at the beginning of a word, or as part
2346 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2347 if (!(p - beg >= 2
2348 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
2349 && (p - beg == 2 || p[-3] == '(')))
2350 #endif
2351 goto done_word;
2353 case '$':
2354 c = *(p++);
2355 if (c == '$')
2356 break;
2358 /* This is a variable reference, so note that it's expandable.
2359 Then read it to the matching close paren. */
2360 wtype = w_variable;
2362 if (c == '(')
2363 closeparen = ')';
2364 else if (c == '{')
2365 closeparen = '}';
2366 else
2367 /* This is a single-letter variable reference. */
2368 break;
2370 for (count=0; *p != '\0'; ++p)
2372 if (*p == c)
2373 ++count;
2374 else if (*p == closeparen && --count < 0)
2376 ++p;
2377 break;
2380 break;
2382 case '?':
2383 case '+':
2384 if (*p == '=')
2385 goto done_word;
2386 break;
2388 case '\\':
2389 switch (*p)
2391 case ':':
2392 case ';':
2393 case '=':
2394 case '\\':
2395 ++p;
2396 break;
2398 break;
2400 default:
2401 if (delim && strchr (delim, c))
2402 goto done_word;
2403 break;
2406 c = *(p++);
2408 done_word:
2409 --p;
2411 done:
2412 if (startp)
2413 *startp = beg;
2414 if (length)
2415 *length = p - beg;
2416 return wtype;
2419 /* Construct the list of include directories
2420 from the arguments and the default list. */
2422 void
2423 construct_include_path (arg_dirs)
2424 char **arg_dirs;
2426 register unsigned int i;
2427 #ifdef VAXC /* just don't ask ... */
2428 stat_t stbuf;
2429 #else
2430 struct stat stbuf;
2431 #endif
2432 /* Table to hold the dirs. */
2434 register unsigned int defsize = (sizeof (default_include_directories)
2435 / sizeof (default_include_directories[0]));
2436 register unsigned int max = 5;
2437 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
2438 register unsigned int idx = 0;
2440 #ifdef __MSDOS__
2441 defsize++;
2442 #endif
2444 /* First consider any dirs specified with -I switches.
2445 Ignore dirs that don't exist. */
2447 if (arg_dirs != 0)
2448 while (*arg_dirs != 0)
2450 char *dir = *arg_dirs++;
2452 if (dir[0] == '~')
2454 char *expanded = tilde_expand (dir);
2455 if (expanded != 0)
2456 dir = expanded;
2459 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
2461 if (idx == max - 1)
2463 max += 5;
2464 dirs = (char **)
2465 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
2467 dirs[idx++] = dir;
2469 else if (dir != arg_dirs[-1])
2470 free (dir);
2473 /* Now add at the end the standard default dirs. */
2475 #ifdef __MSDOS__
2477 /* The environment variable $DJDIR holds the root of the
2478 DJGPP directory tree; add ${DJDIR}/include. */
2479 struct variable *djdir = lookup_variable ("DJDIR", 5);
2481 if (djdir)
2483 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2485 strcat (strcpy (defdir, djdir->value), "/include");
2486 dirs[idx++] = defdir;
2489 #endif
2491 for (i = 0; default_include_directories[i] != 0; ++i)
2492 if (stat (default_include_directories[i], &stbuf) == 0
2493 && S_ISDIR (stbuf.st_mode))
2494 dirs[idx++] = default_include_directories[i];
2496 dirs[idx] = 0;
2498 /* Now compute the maximum length of any name in it. */
2500 max_incl_len = 0;
2501 for (i = 0; i < idx; ++i)
2503 unsigned int len = strlen (dirs[i]);
2504 /* If dir name is written with a trailing slash, discard it. */
2505 if (dirs[i][len - 1] == '/')
2506 /* We can't just clobber a null in because it may have come from
2507 a literal string and literal strings may not be writable. */
2508 dirs[i] = savestring (dirs[i], len - 1);
2509 if (len > max_incl_len)
2510 max_incl_len = len;
2513 include_directories = dirs;
2516 /* Expand ~ or ~USER at the beginning of NAME.
2517 Return a newly malloc'd string or 0. */
2519 char *
2520 tilde_expand (name)
2521 char *name;
2523 #ifndef VMS
2524 if (name[1] == '/' || name[1] == '\0')
2526 extern char *getenv ();
2527 char *home_dir;
2528 int is_variable;
2531 /* Turn off --warn-undefined-variables while we expand HOME. */
2532 int save = warn_undefined_variables_flag;
2533 warn_undefined_variables_flag = 0;
2535 home_dir = allocated_variable_expand ("$(HOME)");
2537 warn_undefined_variables_flag = save;
2540 is_variable = home_dir[0] != '\0';
2541 if (!is_variable)
2543 free (home_dir);
2544 home_dir = getenv ("HOME");
2546 #if !defined(_AMIGA) && !defined(WINDOWS32)
2547 if (home_dir == 0 || home_dir[0] == '\0')
2549 extern char *getlogin ();
2550 char *logname = getlogin ();
2551 home_dir = 0;
2552 if (logname != 0)
2554 struct passwd *p = getpwnam (logname);
2555 if (p != 0)
2556 home_dir = p->pw_dir;
2559 #endif /* !AMIGA && !WINDOWS32 */
2560 if (home_dir != 0)
2562 char *new = concat (home_dir, "", name + 1);
2563 if (is_variable)
2564 free (home_dir);
2565 return new;
2568 #if !defined(_AMIGA) && !defined(WINDOWS32)
2569 else
2571 struct passwd *pwent;
2572 char *userend = strchr (name + 1, '/');
2573 if (userend != 0)
2574 *userend = '\0';
2575 pwent = getpwnam (name + 1);
2576 if (pwent != 0)
2578 if (userend == 0)
2579 return xstrdup (pwent->pw_dir);
2580 else
2581 return concat (pwent->pw_dir, "/", userend + 1);
2583 else if (userend != 0)
2584 *userend = '/';
2586 #endif /* !AMIGA && !WINDOWS32 */
2587 #endif /* !VMS */
2588 return 0;
2591 /* Given a chain of struct nameseq's describing a sequence of filenames,
2592 in reverse of the intended order, return a new chain describing the
2593 result of globbing the filenames. The new chain is in forward order.
2594 The links of the old chain are freed or used in the new chain.
2595 Likewise for the names in the old chain.
2597 SIZE is how big to construct chain elements.
2598 This is useful if we want them actually to be other structures
2599 that have room for additional info. */
2601 struct nameseq *
2602 multi_glob (chain, size)
2603 struct nameseq *chain;
2604 unsigned int size;
2606 extern void dir_setup_glob ();
2607 register struct nameseq *new = 0;
2608 register struct nameseq *old;
2609 struct nameseq *nexto;
2610 glob_t gl;
2612 dir_setup_glob (&gl);
2614 for (old = chain; old != 0; old = nexto)
2616 #ifndef NO_ARCHIVES
2617 char *memname;
2618 #endif
2620 nexto = old->next;
2622 if (old->name[0] == '~')
2624 char *newname = tilde_expand (old->name);
2625 if (newname != 0)
2627 free (old->name);
2628 old->name = newname;
2632 #ifndef NO_ARCHIVES
2633 if (ar_name (old->name))
2635 /* OLD->name is an archive member reference.
2636 Replace it with the archive file name,
2637 and save the member name in MEMNAME.
2638 We will glob on the archive name and then
2639 reattach MEMNAME later. */
2640 char *arname;
2641 ar_parse_name (old->name, &arname, &memname);
2642 free (old->name);
2643 old->name = arname;
2645 else
2646 memname = 0;
2647 #endif /* !NO_ARCHIVES */
2649 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
2651 case 0: /* Success. */
2653 register int i = gl.gl_pathc;
2654 while (i-- > 0)
2656 #ifndef NO_ARCHIVES
2657 if (memname != 0)
2659 /* Try to glob on MEMNAME within the archive. */
2660 struct nameseq *found
2661 = ar_glob (gl.gl_pathv[i], memname, size);
2662 if (found == 0)
2664 /* No matches. Use MEMNAME as-is. */
2665 struct nameseq *elt
2666 = (struct nameseq *) xmalloc (size);
2667 unsigned int alen = strlen (gl.gl_pathv[i]);
2668 unsigned int mlen = strlen (memname);
2669 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2670 bcopy (gl.gl_pathv[i], elt->name, alen);
2671 elt->name[alen] = '(';
2672 bcopy (memname, &elt->name[alen + 1], mlen);
2673 elt->name[alen + 1 + mlen] = ')';
2674 elt->name[alen + 1 + mlen + 1] = '\0';
2675 elt->next = new;
2676 new = elt;
2678 else
2680 /* Find the end of the FOUND chain. */
2681 struct nameseq *f = found;
2682 while (f->next != 0)
2683 f = f->next;
2685 /* Attach the chain being built to the end of the FOUND
2686 chain, and make FOUND the new NEW chain. */
2687 f->next = new;
2688 new = found;
2691 free (memname);
2693 else
2694 #endif /* !NO_ARCHIVES */
2696 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2697 elt->name = xstrdup (gl.gl_pathv[i]);
2698 elt->next = new;
2699 new = elt;
2702 globfree (&gl);
2703 free (old->name);
2704 free ((char *)old);
2705 break;
2708 case GLOB_NOSPACE:
2709 fatal (NILF, _("virtual memory exhausted"));
2710 break;
2712 default:
2713 old->next = new;
2714 new = old;
2715 break;
2719 return new;