* Fix backslash-escape in targets.
[make/kirr.git] / read.c
blobc5434f7eb3ba96c1012cbb596e247183049c3b18
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;
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 record_files (filenames, pattern, pattern_percent, deps, \
317 cmds_started, commands, commands_idx, two_colon, \
318 &fileinfo, !(flags & RM_NO_DEFAULT_GOAL)); \
319 using_filename |= commands_idx > 0; \
321 filenames = 0; \
322 commands_idx = 0; \
323 if (pattern) { free(pattern); pattern = 0; } \
324 } while (0)
326 fileinfo.filenm = filename;
327 fileinfo.lineno = 1;
329 pattern_percent = 0;
330 cmds_started = fileinfo.lineno;
332 if (ISDB (DB_VERBOSE))
334 printf (_("Reading makefile `%s'"), fileinfo.filenm);
335 if (flags & RM_NO_DEFAULT_GOAL)
336 printf (_(" (no default goal)"));
337 if (flags & RM_INCLUDED)
338 printf (_(" (search path)"));
339 if (flags & RM_DONTCARE)
340 printf (_(" (don't care)"));
341 if (flags & RM_NO_TILDE)
342 printf (_(" (no ~ expansion)"));
343 puts ("...");
346 /* First, get a stream to read. */
348 /* Expand ~ in FILENAME unless it came from `include',
349 in which case it was already done. */
350 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
352 char *expanded = tilde_expand (filename);
353 if (expanded != 0)
354 filename = expanded;
357 infile = fopen (filename, "r");
358 /* Save the error code so we print the right message later. */
359 makefile_errno = errno;
361 /* If the makefile wasn't found and it's either a makefile from
362 the `MAKEFILES' variable or an included makefile,
363 search the included makefile search path for this makefile. */
364 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
366 register unsigned int i;
367 for (i = 0; include_directories[i] != 0; ++i)
369 char *name = concat (include_directories[i], "/", filename);
370 infile = fopen (name, "r");
371 if (infile == 0)
372 free (name);
373 else
375 filename = name;
376 break;
381 /* Add FILENAME to the chain of read makefiles. */
382 deps = (struct dep *) xmalloc (sizeof (struct dep));
383 deps->next = read_makefiles;
384 read_makefiles = deps;
385 deps->name = 0;
386 deps->file = lookup_file (filename);
387 if (deps->file == 0)
389 deps->file = enter_file (xstrdup (filename));
390 if (flags & RM_DONTCARE)
391 deps->file->dontcare = 1;
393 if (filename != passed_filename)
394 free (filename);
395 filename = deps->file->name;
396 deps->changed = flags;
397 deps = 0;
399 /* If the makefile can't be found at all, give up entirely. */
401 if (infile == 0)
403 /* If we did some searching, errno has the error from the last
404 attempt, rather from FILENAME itself. Restore it in case the
405 caller wants to use it in a message. */
406 errno = makefile_errno;
407 return 0;
410 reading_file = &fileinfo;
412 /* Loop over lines in the file.
413 The strategy is to accumulate target names in FILENAMES, dependencies
414 in DEPS and commands in COMMANDS. These are used to define a rule
415 when the start of the next rule (or eof) is encountered. */
417 initbuffer (&lb);
418 commands = xmalloc (200);
420 while (!feof (infile))
422 fileinfo.lineno += nlines;
423 nlines = readline (&lb, infile, &fileinfo);
425 /* Check for a shell command line first.
426 If it is not one, we can stop treating tab specially. */
427 if (lb.buffer[0] == '\t')
429 /* This line is a probably shell command. */
430 unsigned int len;
432 if (no_targets)
433 /* Ignore the commands in a rule with no targets. */
434 continue;
436 /* If there is no preceding rule line, don't treat this line
437 as a command, even though it begins with a tab character.
438 SunOS 4 make appears to behave this way. */
440 if (filenames != 0)
442 if (ignoring)
443 /* Yep, this is a shell command, and we don't care. */
444 continue;
446 /* Append this command line to the line being accumulated. */
447 p = lb.buffer;
448 if (commands_idx == 0)
449 cmds_started = fileinfo.lineno;
450 len = strlen (p);
451 if (len + 1 + commands_idx > commands_len)
453 commands_len = (len + 1 + commands_idx) * 2;
454 commands = (char *) xrealloc (commands, commands_len);
456 bcopy (p, &commands[commands_idx], len);
457 commands_idx += len;
458 commands[commands_idx++] = '\n';
460 continue;
464 /* This line is not a shell command line. Don't worry about tabs. */
466 if (collapsed_length < lb.size)
468 collapsed_length = lb.size;
469 if (collapsed != 0)
470 free (collapsed);
471 collapsed = (char *) xmalloc (collapsed_length);
473 strcpy (collapsed, lb.buffer);
474 /* Collapse continuation lines. */
475 collapse_continuations (collapsed);
476 remove_comments (collapsed);
478 /* Compare a word, both length and contents. */
479 #define word1eq(s, l) (len == l && strneq (s, p, l))
480 p = collapsed;
481 while (isspace ((unsigned char)*p))
482 ++p;
483 if (*p == '\0')
484 /* This line is completely empty. */
485 continue;
487 /* Find the end of the first token. Note we don't need to worry about
488 * ":" here since we compare tokens by length (so "export" will never
489 * be equal to "export:").
491 for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
493 len = p2 - p;
495 /* Find the start of the second token. If it's a `:' remember it,
496 since it can't be a preprocessor token--this allows targets named
497 `ifdef', `export', etc. */
498 reading_target = 0;
499 while (isspace ((unsigned char)*p2))
500 ++p2;
501 if (*p2 == '\0')
502 p2 = NULL;
503 else if (p2[0] == ':' && p2[1] == '\0')
505 reading_target = 1;
506 goto skip_conditionals;
509 /* We must first check for conditional and `define' directives before
510 ignoring anything, since they control what we will do with
511 following lines. */
513 if (!in_ignored_define
514 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
515 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
516 || word1eq ("else", 4) || word1eq ("endif", 5)))
518 int i = conditional_line (p, &fileinfo);
519 if (i >= 0)
520 ignoring = i;
521 else
522 fatal (&fileinfo, _("invalid syntax in conditional"));
523 continue;
526 if (word1eq ("endef", 5))
528 if (in_ignored_define)
529 in_ignored_define = 0;
530 else
531 fatal (&fileinfo, _("extraneous `endef'"));
532 continue;
535 if (word1eq ("define", 6))
537 if (ignoring)
538 in_ignored_define = 1;
539 else
541 p2 = next_token (p + 6);
542 if (*p2 == '\0')
543 fatal (&fileinfo, _("empty variable name"));
545 /* Let the variable name be the whole rest of the line,
546 with trailing blanks stripped (comments have already been
547 removed), so it could be a complex variable/function
548 reference that might contain blanks. */
549 p = strchr (p2, '\0');
550 while (isblank (p[-1]))
551 --p;
552 do_define (p2, p - p2, o_file, infile, &fileinfo);
554 continue;
557 if (word1eq ("override", 8))
559 p2 = next_token (p + 8);
560 if (*p2 == '\0')
561 error (&fileinfo, _("empty `override' directive"));
562 if (strneq (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
564 if (ignoring)
565 in_ignored_define = 1;
566 else
568 p2 = next_token (p2 + 6);
569 if (*p2 == '\0')
570 fatal (&fileinfo, _("empty variable name"));
572 /* Let the variable name be the whole rest of the line,
573 with trailing blanks stripped (comments have already been
574 removed), so it could be a complex variable/function
575 reference that might contain blanks. */
576 p = strchr (p2, '\0');
577 while (isblank (p[-1]))
578 --p;
579 do_define (p2, p - p2, o_override, infile, &fileinfo);
582 else if (!ignoring
583 && !try_variable_definition (&fileinfo, p2, o_override, 0))
584 error (&fileinfo, _("invalid `override' directive"));
586 continue;
588 skip_conditionals:
590 if (ignoring)
591 /* Ignore the line. We continue here so conditionals
592 can appear in the middle of a rule. */
593 continue;
595 if (!reading_target && word1eq ("export", 6))
597 struct variable *v;
598 p2 = next_token (p + 6);
599 if (*p2 == '\0')
600 export_all_variables = 1;
601 v = try_variable_definition (&fileinfo, p2, o_file, 0);
602 if (v != 0)
603 v->export = v_export;
604 else
606 unsigned int len;
607 for (p = find_next_token (&p2, &len); p != 0;
608 p = find_next_token (&p2, &len))
610 v = lookup_variable (p, len);
611 if (v == 0)
612 v = define_variable_loc (p, len, "", o_file, 0, &fileinfo);
613 v->export = v_export;
617 else if (!reading_target && word1eq ("unexport", 8))
619 unsigned int len;
620 struct variable *v;
621 p2 = next_token (p + 8);
622 if (*p2 == '\0')
623 export_all_variables = 0;
624 for (p = find_next_token (&p2, &len); p != 0;
625 p = find_next_token (&p2, &len))
627 v = lookup_variable (p, len);
628 if (v == 0)
629 v = define_variable_loc (p, len, "", o_file, 0, &fileinfo);
630 v->export = v_noexport;
633 else if (word1eq ("vpath", 5))
635 char *pattern;
636 unsigned int len;
637 p2 = variable_expand (p + 5);
638 p = find_next_token (&p2, &len);
639 if (p != 0)
641 pattern = savestring (p, len);
642 p = find_next_token (&p2, &len);
643 /* No searchpath means remove all previous
644 selective VPATH's with the same pattern. */
646 else
647 /* No pattern means remove all previous selective VPATH's. */
648 pattern = 0;
649 construct_vpath_list (pattern, p);
650 if (pattern != 0)
651 free (pattern);
653 else if (word1eq ("include", 7) || word1eq ("-include", 8)
654 || word1eq ("sinclude", 8))
656 /* We have found an `include' line specifying a nested
657 makefile to be read at this point. */
658 struct conditionals *save, new_conditionals;
659 struct nameseq *files;
660 /* "-include" (vs "include") says no error if the file does not
661 exist. "sinclude" is an alias for this from SGI. */
662 int noerror = p[0] != 'i';
664 p = allocated_variable_expand (next_token (p + (noerror ? 8 : 7)));
665 if (*p == '\0')
667 error (&fileinfo,
668 _("no file name for `%sinclude'"), noerror ? "-" : "");
669 continue;
672 /* Parse the list of file names. */
673 p2 = p;
674 files = multi_glob (parse_file_seq (&p2, '\0',
675 sizeof (struct nameseq),
677 sizeof (struct nameseq));
678 free (p);
680 /* Save the state of conditionals and start
681 the included makefile with a clean slate. */
682 save = conditionals;
683 bzero ((char *) &new_conditionals, sizeof new_conditionals);
684 conditionals = &new_conditionals;
686 /* Record the rules that are waiting so they will determine
687 the default goal before those in the included makefile. */
688 record_waiting_files ();
690 /* Read each included makefile. */
691 while (files != 0)
693 struct nameseq *next = files->next;
694 char *name = files->name;
695 int r;
697 free ((char *)files);
698 files = next;
700 r = read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
701 | (noerror ? RM_DONTCARE : 0)));
702 if (!r && !noerror)
703 error (&fileinfo, "%s: %s", name, strerror (errno));
705 if (r < 2)
706 free (name);
709 /* Free any space allocated by conditional_line. */
710 if (conditionals->ignoring)
711 free (conditionals->ignoring);
712 if (conditionals->seen_else)
713 free (conditionals->seen_else);
715 /* Restore state. */
716 conditionals = save;
717 reading_file = &fileinfo;
719 #undef word1eq
720 else if (try_variable_definition (&fileinfo, p, o_file, 0))
721 /* This line has been dealt with. */
723 else if (lb.buffer[0] == '\t')
725 p = collapsed; /* Ignore comments. */
726 while (isblank (*p))
727 ++p;
728 if (*p == '\0')
729 /* The line is completely blank; that is harmless. */
730 continue;
731 /* This line starts with a tab but was not caught above
732 because there was no preceding target, and the line
733 might have been usable as a variable definition.
734 But now it is definitely lossage. */
735 fatal(&fileinfo, _("commands commence before first target"));
737 else
739 /* This line describes some target files. This is complicated by
740 the existence of target-specific variables, because we can't
741 expand the entire line until we know if we have one or not. So
742 we expand the line word by word until we find the first `:',
743 then check to see if it's a target-specific variable.
745 In this algorithm, `lb_next' will point to the beginning of the
746 unexpanded parts of the input buffer, while `p2' points to the
747 parts of the expanded buffer we haven't searched yet. */
749 enum make_word_type wtype;
750 enum variable_origin v_origin;
751 char *cmdleft, *lb_next;
752 unsigned int len, plen = 0;
753 char *colonp;
755 /* Record the previous rule. */
757 record_waiting_files ();
759 /* Search the line for an unquoted ; that is not after an
760 unquoted #. */
761 cmdleft = find_char_unquote (lb.buffer, ";#", 0);
762 if (cmdleft != 0 && *cmdleft == '#')
764 /* We found a comment before a semicolon. */
765 *cmdleft = '\0';
766 cmdleft = 0;
768 else if (cmdleft != 0)
769 /* Found one. Cut the line short there before expanding it. */
770 *(cmdleft++) = '\0';
772 collapse_continuations (lb.buffer);
774 /* We can't expand the entire line, since if it's a per-target
775 variable we don't want to expand it. So, walk from the
776 beginning, expanding as we go, and looking for "interesting"
777 chars. The first word is always expandable. */
778 wtype = get_next_mword(lb.buffer, NULL, &lb_next, &len);
779 switch (wtype)
781 case w_eol:
782 if (cmdleft != 0)
783 fatal(&fileinfo, _("missing rule before commands"));
784 /* This line contained something but turned out to be nothing
785 but whitespace (a comment?). */
786 continue;
788 case w_colon:
789 case w_dcolon:
790 /* We accept and ignore rules without targets for
791 compatibility with SunOS 4 make. */
792 no_targets = 1;
793 continue;
795 default:
796 break;
799 p2 = variable_expand_string(NULL, lb_next, len);
800 while (1)
802 lb_next += len;
803 if (cmdleft == 0)
805 /* Look for a semicolon in the expanded line. */
806 cmdleft = find_char_unquote (p2, ";", 0);
808 if (cmdleft != 0)
810 unsigned long p2_off = p2 - variable_buffer;
811 unsigned long cmd_off = cmdleft - variable_buffer;
812 char *pend = p2 + strlen(p2);
814 /* Append any remnants of lb, then cut the line short
815 at the semicolon. */
816 *cmdleft = '\0';
818 /* One school of thought says that you shouldn't expand
819 here, but merely copy, since now you're beyond a ";"
820 and into a command script. However, the old parser
821 expanded the whole line, so we continue that for
822 backwards-compatiblity. Also, it wouldn't be
823 entirely consistent, since we do an unconditional
824 expand below once we know we don't have a
825 target-specific variable. */
826 (void)variable_expand_string(pend, lb_next, (long)-1);
827 lb_next += strlen(lb_next);
828 p2 = variable_buffer + p2_off;
829 cmdleft = variable_buffer + cmd_off + 1;
833 colonp = find_char_unquote(p2, ":", 0);
834 #if defined(__MSDOS__) || defined(WINDOWS32)
835 /* The drive spec brain-damage strikes again... */
836 /* Note that the only separators of targets in this context
837 are whitespace and a left paren. If others are possible,
838 they should be added to the string in the call to index. */
839 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
840 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
841 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
842 colonp = find_char_unquote(colonp + 1, ":", 0);
843 #endif
844 if (colonp != 0)
845 break;
847 wtype = get_next_mword(lb_next, NULL, &lb_next, &len);
848 if (wtype == w_eol)
849 break;
851 p2 += strlen(p2);
852 *(p2++) = ' ';
853 p2 = variable_expand_string(p2, lb_next, len);
854 /* We don't need to worry about cmdleft here, because if it was
855 found in the variable_buffer the entire buffer has already
856 been expanded... we'll never get here. */
859 p2 = next_token (variable_buffer);
861 /* If the word we're looking at is EOL, see if there's _anything_
862 on the line. If not, a variable expanded to nothing, so ignore
863 it. If so, we can't parse this line so punt. */
864 if (wtype == w_eol)
866 if (*p2 != '\0')
867 /* There's no need to be ivory-tower about this: check for
868 one of the most common bugs found in makefiles... */
869 fatal (&fileinfo, _("missing separator%s"),
870 !strneq(lb.buffer, " ", 8) ? ""
871 : _(" (did you mean TAB instead of 8 spaces?)"));
872 continue;
875 /* Make the colon the end-of-string so we know where to stop
876 looking for targets. */
877 *colonp = '\0';
878 filenames = multi_glob (parse_file_seq (&p2, '\0',
879 sizeof (struct nameseq),
881 sizeof (struct nameseq));
882 *p2 = ':';
884 if (!filenames)
886 /* We accept and ignore rules without targets for
887 compatibility with SunOS 4 make. */
888 no_targets = 1;
889 continue;
891 /* This should never be possible; we handled it above. */
892 assert (*p2 != '\0');
893 ++p2;
895 /* Is this a one-colon or two-colon entry? */
896 two_colon = *p2 == ':';
897 if (two_colon)
898 p2++;
900 /* Test to see if it's a target-specific variable. Copy the rest
901 of the buffer over, possibly temporarily (we'll expand it later
902 if it's not a target-specific variable). PLEN saves the length
903 of the unparsed section of p2, for later. */
904 if (*lb_next != '\0')
906 unsigned int l = p2 - variable_buffer;
907 plen = strlen (p2);
908 (void) variable_buffer_output (p2+plen,
909 lb_next, strlen (lb_next)+1);
910 p2 = variable_buffer + l;
913 /* See if it's an "override" keyword; if so see if what comes after
914 it looks like a variable definition. */
916 wtype = get_next_mword (p2, NULL, &p, &len);
918 v_origin = o_file;
919 if (wtype == w_static && (len == (sizeof ("override")-1)
920 && strneq (p, "override", len)))
922 v_origin = o_override;
923 wtype = get_next_mword (p+len, NULL, &p, &len);
926 if (wtype != w_eol)
927 wtype = get_next_mword (p+len, NULL, NULL, NULL);
929 if (wtype == w_varassign)
931 record_target_var (filenames, p, two_colon, v_origin, &fileinfo);
932 filenames = 0;
933 continue;
936 /* This is a normal target, _not_ a target-specific variable.
937 Unquote any = in the dependency list. */
938 find_char_unquote (lb_next, "=", 0);
940 /* We have some targets, so don't ignore the following commands. */
941 no_targets = 0;
943 /* Expand the dependencies, etc. */
944 if (*lb_next != '\0')
946 unsigned int l = p2 - variable_buffer;
947 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
948 p2 = variable_buffer + l;
950 /* Look for a semicolon in the expanded line. */
951 if (cmdleft == 0)
953 cmdleft = find_char_unquote (p2, ";", 0);
954 if (cmdleft != 0)
955 *(cmdleft++) = '\0';
959 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
960 p = strchr (p2, ':');
961 while (p != 0 && p[-1] == '\\')
963 register char *q = &p[-1];
964 register int backslash = 0;
965 while (*q-- == '\\')
966 backslash = !backslash;
967 if (backslash)
968 p = strchr (p + 1, ':');
969 else
970 break;
972 #ifdef _AMIGA
973 /* Here, the situation is quite complicated. Let's have a look
974 at a couple of targets:
976 install: dev:make
978 dev:make: make
980 dev:make:: xyz
982 The rule is that it's only a target, if there are TWO :'s
983 OR a space around the :.
985 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
986 || isspace ((unsigned char)p[-1])))
987 p = 0;
988 #endif
989 #if defined (WINDOWS32) || defined (__MSDOS__)
990 do {
991 check_again = 0;
992 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
993 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
994 isalpha ((unsigned char)p[-1]) &&
995 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
996 p = strchr (p + 1, ':');
997 check_again = 1;
999 } while (check_again);
1000 #endif
1001 if (p != 0)
1003 struct nameseq *target;
1004 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
1005 ++p2;
1006 if (target == 0)
1007 fatal (&fileinfo, _("missing target pattern"));
1008 else if (target->next != 0)
1009 fatal (&fileinfo, _("multiple target patterns"));
1010 pattern = target->name;
1011 pattern_percent = find_percent (pattern);
1012 if (pattern_percent == 0)
1013 fatal (&fileinfo, _("target pattern contains no `%%'"));
1014 free((char *)target);
1016 else
1017 pattern = 0;
1019 /* Parse the dependencies. */
1020 deps = (struct dep *)
1021 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
1022 sizeof (struct dep));
1024 commands_idx = 0;
1025 if (cmdleft != 0)
1027 /* Semicolon means rest of line is a command. */
1028 unsigned int len = strlen (cmdleft);
1030 cmds_started = fileinfo.lineno;
1032 /* Add this command line to the buffer. */
1033 if (len + 2 > commands_len)
1035 commands_len = (len + 2) * 2;
1036 commands = (char *) xrealloc (commands, commands_len);
1038 bcopy (cmdleft, commands, len);
1039 commands_idx += len;
1040 commands[commands_idx++] = '\n';
1043 continue;
1046 /* We get here except in the case that we just read a rule line.
1047 Record now the last rule we read, so following spurious
1048 commands are properly diagnosed. */
1049 record_waiting_files ();
1050 no_targets = 0;
1053 if (conditionals->if_cmds)
1054 fatal (&fileinfo, _("missing `endif'"));
1056 /* At eof, record the last rule. */
1057 record_waiting_files ();
1059 freebuffer (&lb);
1060 free ((char *) commands);
1061 fclose (infile);
1063 reading_file = 0;
1065 return 1+using_filename;
1068 /* Execute a `define' directive.
1069 The first line has already been read, and NAME is the name of
1070 the variable to be defined. The following lines remain to be read.
1071 LINENO, INFILE and FILENAME refer to the makefile being read.
1072 The value returned is LINENO, updated for lines read here. */
1074 static void
1075 do_define (name, namelen, origin, infile, flocp)
1076 char *name;
1077 unsigned int namelen;
1078 enum variable_origin origin;
1079 FILE *infile;
1080 struct floc *flocp;
1082 struct linebuffer lb;
1083 unsigned int nlines = 0;
1084 unsigned int length = 100;
1085 char *definition = (char *) xmalloc (100);
1086 register unsigned int idx = 0;
1087 register char *p;
1089 /* Expand the variable name. */
1090 char *var = (char *) alloca (namelen + 1);
1091 bcopy (name, var, namelen);
1092 var[namelen] = '\0';
1093 var = variable_expand (var);
1095 initbuffer (&lb);
1096 while (!feof (infile))
1098 unsigned int len;
1100 flocp->lineno += nlines;
1101 nlines = readline (&lb, infile, flocp);
1103 collapse_continuations (lb.buffer);
1105 p = next_token (lb.buffer);
1106 len = strlen (p);
1107 if ((len == 5 || (len > 5 && isblank (p[5])))
1108 && strneq (p, "endef", 5))
1110 p += 5;
1111 remove_comments (p);
1112 if (*next_token (p) != '\0')
1113 error (flocp, _("Extraneous text after `endef' directive"));
1114 /* Define the variable. */
1115 if (idx == 0)
1116 definition[0] = '\0';
1117 else
1118 definition[idx - 1] = '\0';
1119 (void) define_variable_loc (var, strlen (var), definition, origin,
1120 1, flocp);
1121 free (definition);
1122 freebuffer (&lb);
1123 return;
1125 else
1127 len = strlen (lb.buffer);
1128 /* Increase the buffer size if necessary. */
1129 if (idx + len + 1 > length)
1131 length = (idx + len) * 2;
1132 definition = (char *) xrealloc (definition, length + 1);
1135 bcopy (lb.buffer, &definition[idx], len);
1136 idx += len;
1137 /* Separate lines with a newline. */
1138 definition[idx++] = '\n';
1142 /* No `endef'!! */
1143 fatal (flocp, _("missing `endef', unterminated `define'"));
1145 /* NOTREACHED */
1146 return;
1149 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1150 "ifneq", "else" and "endif".
1151 LINE is the input line, with the command as its first word.
1153 FILENAME and LINENO are the filename and line number in the
1154 current makefile. They are used for error messages.
1156 Value is -1 if the line is invalid,
1157 0 if following text should be interpreted,
1158 1 if following text should be ignored. */
1160 static int
1161 conditional_line (line, flocp)
1162 char *line;
1163 const struct floc *flocp;
1165 int notdef;
1166 char *cmdname;
1167 register unsigned int i;
1169 if (*line == 'i')
1171 /* It's an "if..." command. */
1172 notdef = line[2] == 'n';
1173 if (notdef)
1175 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
1176 line += cmdname[3] == 'd' ? 7 : 6;
1178 else
1180 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
1181 line += cmdname[2] == 'd' ? 6 : 5;
1184 else
1186 /* It's an "else" or "endif" command. */
1187 notdef = line[1] == 'n';
1188 cmdname = notdef ? "endif" : "else";
1189 line += notdef ? 5 : 4;
1192 line = next_token (line);
1194 if (*cmdname == 'e')
1196 if (*line != '\0')
1197 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1198 /* "Else" or "endif". */
1199 if (conditionals->if_cmds == 0)
1200 fatal (flocp, _("extraneous `%s'"), cmdname);
1201 /* NOTDEF indicates an `endif' command. */
1202 if (notdef)
1203 --conditionals->if_cmds;
1204 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1205 fatal (flocp, _("only one `else' per conditional"));
1206 else
1208 /* Toggle the state of ignorance. */
1209 conditionals->ignoring[conditionals->if_cmds - 1]
1210 = !conditionals->ignoring[conditionals->if_cmds - 1];
1211 /* Record that we have seen an `else' in this conditional.
1212 A second `else' will be erroneous. */
1213 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1215 for (i = 0; i < conditionals->if_cmds; ++i)
1216 if (conditionals->ignoring[i])
1217 return 1;
1218 return 0;
1221 if (conditionals->allocated == 0)
1223 conditionals->allocated = 5;
1224 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1225 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1228 ++conditionals->if_cmds;
1229 if (conditionals->if_cmds > conditionals->allocated)
1231 conditionals->allocated += 5;
1232 conditionals->ignoring = (char *)
1233 xrealloc (conditionals->ignoring, conditionals->allocated);
1234 conditionals->seen_else = (char *)
1235 xrealloc (conditionals->seen_else, conditionals->allocated);
1238 /* Record that we have seen an `if...' but no `else' so far. */
1239 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1241 /* Search through the stack to see if we're already ignoring. */
1242 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1243 if (conditionals->ignoring[i])
1245 /* We are already ignoring, so just push a level
1246 to match the next "else" or "endif", and keep ignoring.
1247 We don't want to expand variables in the condition. */
1248 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1249 return 1;
1252 if (cmdname[notdef ? 3 : 2] == 'd')
1254 /* "Ifdef" or "ifndef". */
1255 struct variable *v;
1256 register char *p = end_of_token (line);
1257 i = p - line;
1258 p = next_token (p);
1259 if (*p != '\0')
1260 return -1;
1261 v = lookup_variable (line, i);
1262 conditionals->ignoring[conditionals->if_cmds - 1]
1263 = (v != 0 && *v->value != '\0') == notdef;
1265 else
1267 /* "Ifeq" or "ifneq". */
1268 char *s1, *s2;
1269 unsigned int len;
1270 char termin = *line == '(' ? ',' : *line;
1272 if (termin != ',' && termin != '"' && termin != '\'')
1273 return -1;
1275 s1 = ++line;
1276 /* Find the end of the first string. */
1277 if (termin == ',')
1279 register int count = 0;
1280 for (; *line != '\0'; ++line)
1281 if (*line == '(')
1282 ++count;
1283 else if (*line == ')')
1284 --count;
1285 else if (*line == ',' && count <= 0)
1286 break;
1288 else
1289 while (*line != '\0' && *line != termin)
1290 ++line;
1292 if (*line == '\0')
1293 return -1;
1295 if (termin == ',')
1297 /* Strip blanks after the first string. */
1298 char *p = line++;
1299 while (isblank (p[-1]))
1300 --p;
1301 *p = '\0';
1303 else
1304 *line++ = '\0';
1306 s2 = variable_expand (s1);
1307 /* We must allocate a new copy of the expanded string because
1308 variable_expand re-uses the same buffer. */
1309 len = strlen (s2);
1310 s1 = (char *) alloca (len + 1);
1311 bcopy (s2, s1, len + 1);
1313 if (termin != ',')
1314 /* Find the start of the second string. */
1315 line = next_token (line);
1317 termin = termin == ',' ? ')' : *line;
1318 if (termin != ')' && termin != '"' && termin != '\'')
1319 return -1;
1321 /* Find the end of the second string. */
1322 if (termin == ')')
1324 register int count = 0;
1325 s2 = next_token (line);
1326 for (line = s2; *line != '\0'; ++line)
1328 if (*line == '(')
1329 ++count;
1330 else if (*line == ')')
1332 if (count <= 0)
1333 break;
1334 else
1335 --count;
1339 else
1341 ++line;
1342 s2 = line;
1343 while (*line != '\0' && *line != termin)
1344 ++line;
1347 if (*line == '\0')
1348 return -1;
1350 *line = '\0';
1351 line = next_token (++line);
1352 if (*line != '\0')
1353 error (flocp, _("Extraneous text after `%s' directive"), cmdname);
1355 s2 = variable_expand (s2);
1356 conditionals->ignoring[conditionals->if_cmds - 1]
1357 = streq (s1, s2) == notdef;
1360 /* Search through the stack to see if we're ignoring. */
1361 for (i = 0; i < conditionals->if_cmds; ++i)
1362 if (conditionals->ignoring[i])
1363 return 1;
1364 return 0;
1367 /* Remove duplicate dependencies in CHAIN. */
1369 void
1370 uniquize_deps (chain)
1371 struct dep *chain;
1373 register struct dep *d;
1375 /* Make sure that no dependencies are repeated. This does not
1376 really matter for the purpose of updating targets, but it
1377 might make some names be listed twice for $^ and $?. */
1379 for (d = chain; d != 0; d = d->next)
1381 struct dep *last, *next;
1383 last = d;
1384 next = d->next;
1385 while (next != 0)
1386 if (streq (dep_name (d), dep_name (next)))
1388 struct dep *n = next->next;
1389 last->next = n;
1390 if (next->name != 0 && next->name != d->name)
1391 free (next->name);
1392 if (next != d)
1393 free ((char *) next);
1394 next = n;
1396 else
1398 last = next;
1399 next = next->next;
1404 /* Record target-specific variable values for files FILENAMES.
1405 TWO_COLON is nonzero if a double colon was used.
1407 The links of FILENAMES are freed, and so are any names in it
1408 that are not incorporated into other data structures.
1410 If the target is a pattern, add the variable to the pattern-specific
1411 variable value list. */
1413 static void
1414 record_target_var (filenames, defn, two_colon, origin, flocp)
1415 struct nameseq *filenames;
1416 char *defn;
1417 int two_colon;
1418 enum variable_origin origin;
1419 const struct floc *flocp;
1421 struct nameseq *nextf;
1422 struct variable_set_list *global;
1424 global = current_variable_set_list;
1426 /* If the variable is an append version, store that but treat it as a
1427 normal recursive variable. */
1429 for (; filenames != 0; filenames = nextf)
1431 struct variable *v;
1432 register char *name = filenames->name;
1433 struct variable_set_list *vlist;
1434 char *fname;
1435 char *percent;
1437 nextf = filenames->next;
1438 free ((char *) filenames);
1440 /* If it's a pattern target, then add it to the pattern-specific
1441 variable list. */
1442 percent = find_percent (name);
1443 if (percent)
1445 struct pattern_var *p;
1447 /* Get a reference for this pattern-specific variable struct. */
1448 p = create_pattern_var(name, percent);
1449 vlist = p->vars;
1450 fname = p->target;
1452 else
1454 struct file *f;
1456 /* Get a file reference for this file, and initialize it. */
1457 f = enter_file (name);
1458 initialize_file_variables (f, 1);
1459 vlist = f->variables;
1460 fname = f->name;
1463 /* Make the new variable context current and define the variable. */
1464 current_variable_set_list = vlist;
1465 v = try_variable_definition (flocp, defn, origin, 1);
1466 if (!v)
1467 error (flocp, _("Malformed per-target variable definition"));
1468 v->per_target = 1;
1470 /* If it's not an override, check to see if there was a command-line
1471 setting. If so, reset the value. */
1472 if (origin != o_override)
1474 struct variable *gv;
1475 int len = strlen(v->name);
1477 current_variable_set_list = global;
1478 gv = lookup_variable (v->name, len);
1479 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1480 define_variable_in_set (v->name, len, gv->value, gv->origin,
1481 gv->recursive, vlist->set, flocp);
1484 /* Free name if not needed further. */
1485 if (name != fname && (name < fname || name > fname + strlen (fname)))
1486 free (name);
1489 current_variable_set_list = global;
1492 /* Record a description line for files FILENAMES,
1493 with dependencies DEPS, commands to execute described
1494 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1495 TWO_COLON is nonzero if a double colon was used.
1496 If not nil, PATTERN is the `%' pattern to make this
1497 a static pattern rule, and PATTERN_PERCENT is a pointer
1498 to the `%' within it.
1500 The links of FILENAMES are freed, and so are any names in it
1501 that are not incorporated into other data structures. */
1503 static void
1504 record_files (filenames, pattern, pattern_percent, deps, cmds_started,
1505 commands, commands_idx, two_colon, flocp, set_default)
1506 struct nameseq *filenames;
1507 char *pattern, *pattern_percent;
1508 struct dep *deps;
1509 unsigned int cmds_started;
1510 char *commands;
1511 unsigned int commands_idx;
1512 int two_colon;
1513 const struct floc *flocp;
1514 int set_default;
1516 struct nameseq *nextf;
1517 int implicit = 0;
1518 unsigned int max_targets = 0, target_idx = 0;
1519 char **targets = 0, **target_percents = 0;
1520 struct commands *cmds;
1522 if (commands_idx > 0)
1524 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1525 cmds->fileinfo.filenm = flocp->filenm;
1526 cmds->fileinfo.lineno = cmds_started;
1527 cmds->commands = savestring (commands, commands_idx);
1528 cmds->command_lines = 0;
1530 else
1531 cmds = 0;
1533 for (; filenames != 0; filenames = nextf)
1536 register char *name = filenames->name;
1537 register struct file *f;
1538 register struct dep *d;
1539 struct dep *this;
1540 char *implicit_percent;
1542 nextf = filenames->next;
1543 free ((char *) filenames);
1545 implicit_percent = find_percent (name);
1546 implicit |= implicit_percent != 0;
1548 if (implicit && pattern != 0)
1549 fatal (flocp, _("mixed implicit and static pattern rules"));
1551 if (implicit && implicit_percent == 0)
1552 fatal (flocp, _("mixed implicit and normal rules"));
1554 if (implicit)
1556 if (targets == 0)
1558 max_targets = 5;
1559 targets = (char **) xmalloc (5 * sizeof (char *));
1560 target_percents = (char **) xmalloc (5 * sizeof (char *));
1561 target_idx = 0;
1563 else if (target_idx == max_targets - 1)
1565 max_targets += 5;
1566 targets = (char **) xrealloc ((char *) targets,
1567 max_targets * sizeof (char *));
1568 target_percents
1569 = (char **) xrealloc ((char *) target_percents,
1570 max_targets * sizeof (char *));
1572 targets[target_idx] = name;
1573 target_percents[target_idx] = implicit_percent;
1574 ++target_idx;
1575 continue;
1578 /* If there are multiple filenames, copy the chain DEPS
1579 for all but the last one. It is not safe for the same deps
1580 to go in more than one place in the data base. */
1581 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1583 if (pattern != 0)
1585 /* If this is an extended static rule:
1586 `targets: target%pattern: dep%pattern; cmds',
1587 translate each dependency pattern into a plain filename
1588 using the target pattern and this target's name. */
1589 if (!pattern_matches (pattern, pattern_percent, name))
1591 /* Give a warning if the rule is meaningless. */
1592 error (flocp,
1593 _("target `%s' doesn't match the target pattern"), name);
1594 this = 0;
1596 else
1598 /* We use patsubst_expand to do the work of translating
1599 the target pattern, the target's name and the dependencies'
1600 patterns into plain dependency names. */
1601 char *buffer = variable_expand ("");
1603 for (d = this; d != 0; d = d->next)
1605 char *o;
1606 char *percent = find_percent (d->name);
1607 if (percent == 0)
1608 continue;
1609 o = patsubst_expand (buffer, name, pattern, d->name,
1610 pattern_percent, percent);
1611 free (d->name);
1612 d->name = savestring (buffer, o - buffer);
1617 if (!two_colon)
1619 /* Single-colon. Combine these dependencies
1620 with others in file's existing record, if any. */
1621 f = enter_file (name);
1623 if (f->double_colon)
1624 fatal (flocp,
1625 _("target file `%s' has both : and :: entries"), f->name);
1627 /* If CMDS == F->CMDS, this target was listed in this rule
1628 more than once. Just give a warning since this is harmless. */
1629 if (cmds != 0 && cmds == f->cmds)
1630 error (flocp, _("target `%s' given more than once in the same rule."),
1631 f->name);
1633 /* Check for two single-colon entries both with commands.
1634 Check is_target so that we don't lose on files such as .c.o
1635 whose commands were preinitialized. */
1636 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1638 error (&cmds->fileinfo,
1639 _("warning: overriding commands for target `%s'"), f->name);
1640 error (&f->cmds->fileinfo,
1641 _("warning: ignoring old commands for target `%s'"),
1642 f->name);
1645 f->is_target = 1;
1647 /* Defining .DEFAULT with no deps or cmds clears it. */
1648 if (f == default_file && this == 0 && cmds == 0)
1649 f->cmds = 0;
1650 if (cmds != 0)
1651 f->cmds = cmds;
1652 /* Defining .SUFFIXES with no dependencies
1653 clears out the list of suffixes. */
1654 if (f == suffix_file && this == 0)
1656 d = f->deps;
1657 while (d != 0)
1659 struct dep *nextd = d->next;
1660 free (d->name);
1661 free ((char *)d);
1662 d = nextd;
1664 f->deps = 0;
1666 else if (f->deps != 0)
1668 /* Add the file's old deps and the new ones in THIS together. */
1670 struct dep *firstdeps, *moredeps;
1671 if (cmds != 0)
1673 /* This is the rule with commands, so put its deps first.
1674 The rationale behind this is that $< expands to the
1675 first dep in the chain, and commands use $< expecting
1676 to get the dep that rule specifies. */
1677 firstdeps = this;
1678 moredeps = f->deps;
1680 else
1682 /* Append the new deps to the old ones. */
1683 firstdeps = f->deps;
1684 moredeps = this;
1687 if (firstdeps == 0)
1688 firstdeps = moredeps;
1689 else
1691 d = firstdeps;
1692 while (d->next != 0)
1693 d = d->next;
1694 d->next = moredeps;
1697 f->deps = firstdeps;
1699 else
1700 f->deps = this;
1702 /* If this is a static pattern rule, set the file's stem to
1703 the part of its name that matched the `%' in the pattern,
1704 so you can use $* in the commands. */
1705 if (pattern != 0)
1707 static char *percent = "%";
1708 char *buffer = variable_expand ("");
1709 char *o = patsubst_expand (buffer, name, pattern, percent,
1710 pattern_percent, percent);
1711 f->stem = savestring (buffer, o - buffer);
1714 else
1716 /* Double-colon. Make a new record
1717 even if the file already has one. */
1718 f = lookup_file (name);
1719 /* Check for both : and :: rules. Check is_target so
1720 we don't lose on default suffix rules or makefiles. */
1721 if (f != 0 && f->is_target && !f->double_colon)
1722 fatal (flocp,
1723 _("target file `%s' has both : and :: entries"), f->name);
1724 f = enter_file (name);
1725 /* If there was an existing entry and it was a double-colon
1726 entry, enter_file will have returned a new one, making it the
1727 prev pointer of the old one, and setting its double_colon
1728 pointer to the first one. */
1729 if (f->double_colon == 0)
1730 /* This is the first entry for this name, so we must
1731 set its double_colon pointer to itself. */
1732 f->double_colon = f;
1733 f->is_target = 1;
1734 f->deps = this;
1735 f->cmds = cmds;
1738 /* Free name if not needed further. */
1739 if (f != 0 && name != f->name
1740 && (name < f->name || name > f->name + strlen (f->name)))
1742 free (name);
1743 name = f->name;
1746 /* See if this is first target seen whose name does
1747 not start with a `.', unless it contains a slash. */
1748 if (default_goal_file == 0 && set_default
1749 && (*name != '.' || strchr (name, '/') != 0
1750 #if defined(__MSDOS__) || defined(WINDOWS32)
1751 || strchr (name, '\\') != 0
1752 #endif
1755 int reject = 0;
1757 /* If this file is a suffix, don't
1758 let it be the default goal file. */
1760 for (d = suffix_file->deps; d != 0; d = d->next)
1762 register struct dep *d2;
1763 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1765 reject = 1;
1766 break;
1768 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1770 register unsigned int len = strlen (dep_name (d2));
1771 if (!strneq (name, dep_name (d2), len))
1772 continue;
1773 if (streq (name + len, dep_name (d)))
1775 reject = 1;
1776 break;
1779 if (reject)
1780 break;
1783 if (!reject)
1784 default_goal_file = f;
1788 if (implicit)
1790 targets[target_idx] = 0;
1791 target_percents[target_idx] = 0;
1792 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1793 free ((char *) target_percents);
1797 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1798 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1799 Quoting backslashes are removed from STRING by compacting it into
1800 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1801 one, or nil if there are none. */
1803 char *
1804 find_char_unquote (string, stopchars, blank)
1805 char *string;
1806 char *stopchars;
1807 int blank;
1809 unsigned int string_len = 0;
1810 register char *p = string;
1812 while (1)
1814 while (*p != '\0' && strchr (stopchars, *p) == 0
1815 && (!blank || !isblank (*p)))
1816 ++p;
1817 if (*p == '\0')
1818 break;
1820 if (p > string && p[-1] == '\\')
1822 /* Search for more backslashes. */
1823 register int i = -2;
1824 while (&p[i] >= string && p[i] == '\\')
1825 --i;
1826 ++i;
1827 /* Only compute the length if really needed. */
1828 if (string_len == 0)
1829 string_len = strlen (string);
1830 /* The number of backslashes is now -I.
1831 Copy P over itself to swallow half of them. */
1832 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1833 p += i / 2;
1834 if (i % 2 == 0)
1835 /* All the backslashes quoted each other; the STOPCHAR was
1836 unquoted. */
1837 return p;
1839 /* The STOPCHAR was quoted by a backslash. Look for another. */
1841 else
1842 /* No backslash in sight. */
1843 return p;
1846 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1847 return 0;
1850 /* Search PATTERN for an unquoted %. */
1852 char *
1853 find_percent (pattern)
1854 char *pattern;
1856 return find_char_unquote (pattern, "%", 0);
1859 /* Parse a string into a sequence of filenames represented as a
1860 chain of struct nameseq's in reverse order and return that chain.
1862 The string is passed as STRINGP, the address of a string pointer.
1863 The string pointer is updated to point at the first character
1864 not parsed, which either is a null char or equals STOPCHAR.
1866 SIZE is how big to construct chain elements.
1867 This is useful if we want them actually to be other structures
1868 that have room for additional info.
1870 If STRIP is nonzero, strip `./'s off the beginning. */
1872 struct nameseq *
1873 parse_file_seq (stringp, stopchar, size, strip)
1874 char **stringp;
1875 int stopchar;
1876 unsigned int size;
1877 int strip;
1879 register struct nameseq *new = 0;
1880 register struct nameseq *new1, *lastnew1;
1881 register char *p = *stringp;
1882 char *q;
1883 char *name;
1884 char stopchars[3];
1886 #ifdef VMS
1887 stopchars[0] = ',';
1888 stopchars[1] = stopchar;
1889 stopchars[2] = '\0';
1890 #else
1891 stopchars[0] = stopchar;
1892 stopchars[1] = '\0';
1893 #endif
1895 while (1)
1897 /* Skip whitespace; see if any more names are left. */
1898 p = next_token (p);
1899 if (*p == '\0')
1900 break;
1901 if (*p == stopchar)
1902 break;
1904 /* Yes, find end of next name. */
1905 q = p;
1906 p = find_char_unquote (q, stopchars, 1);
1907 #ifdef VMS
1908 /* convert comma separated list to space separated */
1909 if (p && *p == ',')
1910 *p =' ';
1911 #endif
1912 #ifdef _AMIGA
1913 if (stopchar == ':' && p && *p == ':'
1914 && !(isspace ((unsigned char)p[1]) || !p[1]
1915 || isspace ((unsigned char)p[-1])))
1917 p = find_char_unquote (p+1, stopchars, 1);
1919 #endif
1920 #if defined(WINDOWS32) || defined(__MSDOS__)
1921 /* For WINDOWS32, skip a "C:\..." or a "C:/..." until we find the
1922 first colon which isn't followed by a slash or a backslash.
1923 Note that tokens separated by spaces should be treated as separate
1924 tokens since make doesn't allow path names with spaces */
1925 if (stopchar == ':')
1926 while (p != 0 && !isspace ((unsigned char)*p) &&
1927 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
1928 p = find_char_unquote (p + 1, stopchars, 1);
1929 #endif
1930 if (p == 0)
1931 p = q + strlen (q);
1933 if (strip)
1934 #ifdef VMS
1935 /* Skip leading `[]'s. */
1936 while (p - q > 2 && q[0] == '[' && q[1] == ']')
1937 #else
1938 /* Skip leading `./'s. */
1939 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1940 #endif
1942 q += 2; /* Skip "./". */
1943 while (q < p && *q == '/')
1944 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1945 ++q;
1948 /* Extract the filename just found, and skip it. */
1950 if (q == p)
1951 /* ".///" was stripped to "". */
1952 #ifdef VMS
1953 continue;
1954 #else
1955 #ifdef _AMIGA
1956 name = savestring ("", 0);
1957 #else
1958 name = savestring ("./", 2);
1959 #endif
1960 #endif
1961 else
1962 #ifdef VMS
1963 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
1964 * to remove this '\' before we can use the filename.
1965 * Savestring called because q may be read-only string constant.
1968 char *qbase = xstrdup (q);
1969 char *pbase = qbase + (p-q);
1970 char *q1 = qbase;
1971 char *q2 = q1;
1972 char *p1 = pbase;
1974 while (q1 != pbase)
1976 if (*q1 == '\\' && *(q1+1) == ':')
1978 q1++;
1979 p1--;
1981 *q2++ = *q1++;
1983 name = savestring (qbase, p1 - qbase);
1984 free (qbase);
1986 #else
1987 name = savestring (q, p - q);
1988 #endif
1990 /* Add it to the front of the chain. */
1991 new1 = (struct nameseq *) xmalloc (size);
1992 new1->name = name;
1993 new1->next = new;
1994 new = new1;
1997 #ifndef NO_ARCHIVES
1999 /* Look for multi-word archive references.
2000 They are indicated by a elt ending with an unmatched `)' and
2001 an elt further down the chain (i.e., previous in the file list)
2002 with an unmatched `(' (e.g., "lib(mem"). */
2004 new1 = new;
2005 lastnew1 = 0;
2006 while (new1 != 0)
2007 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2008 && new1->name[strlen (new1->name) - 1] == ')'
2009 && strchr (new1->name, '(') == 0)
2011 /* NEW1 ends with a `)' but does not contain a `('.
2012 Look back for an elt with an opening `(' but no closing `)'. */
2014 struct nameseq *n = new1->next, *lastn = new1;
2015 char *paren = 0;
2016 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2018 lastn = n;
2019 n = n->next;
2021 if (n != 0
2022 /* Ignore something starting with `(', as that cannot actually
2023 be an archive-member reference (and treating it as such
2024 results in an empty file name, which causes much lossage). */
2025 && n->name[0] != '(')
2027 /* N is the first element in the archive group.
2028 Its name looks like "lib(mem" (with no closing `)'). */
2030 char *libname;
2032 /* Copy "lib(" into LIBNAME. */
2033 ++paren;
2034 libname = (char *) alloca (paren - n->name + 1);
2035 bcopy (n->name, libname, paren - n->name);
2036 libname[paren - n->name] = '\0';
2038 if (*paren == '\0')
2040 /* N was just "lib(", part of something like "lib( a b)".
2041 Edit it out of the chain and free its storage. */
2042 lastn->next = n->next;
2043 free (n->name);
2044 free ((char *) n);
2045 /* LASTN->next is the new stopping elt for the loop below. */
2046 n = lastn->next;
2048 else
2050 /* Replace N's name with the full archive reference. */
2051 name = concat (libname, paren, ")");
2052 free (n->name);
2053 n->name = name;
2056 if (new1->name[1] == '\0')
2058 /* NEW1 is just ")", part of something like "lib(a b )".
2059 Omit it from the chain and free its storage. */
2060 if (lastnew1 == 0)
2061 new = new1->next;
2062 else
2063 lastnew1->next = new1->next;
2064 lastn = new1;
2065 new1 = new1->next;
2066 free (lastn->name);
2067 free ((char *) lastn);
2069 else
2071 /* Replace also NEW1->name, which already has closing `)'. */
2072 name = concat (libname, new1->name, "");
2073 free (new1->name);
2074 new1->name = name;
2075 new1 = new1->next;
2078 /* Trace back from NEW1 (the end of the list) until N
2079 (the beginning of the list), rewriting each name
2080 with the full archive reference. */
2082 while (new1 != n)
2084 name = concat (libname, new1->name, ")");
2085 free (new1->name);
2086 new1->name = name;
2087 lastnew1 = new1;
2088 new1 = new1->next;
2091 else
2093 /* No frobnication happening. Just step down the list. */
2094 lastnew1 = new1;
2095 new1 = new1->next;
2098 else
2100 lastnew1 = new1;
2101 new1 = new1->next;
2104 #endif
2106 *stringp = p;
2107 return new;
2110 /* Read a line of text from STREAM into LINEBUFFER.
2111 Combine continuation lines into one line.
2112 Return the number of actual lines read (> 1 if hacked continuation lines).
2115 static unsigned long
2116 readline (linebuffer, stream, flocp)
2117 struct linebuffer *linebuffer;
2118 FILE *stream;
2119 const struct floc *flocp;
2121 char *buffer = linebuffer->buffer;
2122 register char *p = linebuffer->buffer;
2123 register char *end = p + linebuffer->size;
2124 register int len, lastlen = 0;
2125 register char *p2;
2126 register unsigned int nlines = 0;
2127 register int backslash;
2129 *p = '\0';
2131 while (fgets (p, end - p, stream) != 0)
2133 len = strlen (p);
2134 if (len == 0)
2136 /* This only happens when the first thing on the line is a '\0'.
2137 It is a pretty hopeless case, but (wonder of wonders) Athena
2138 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2139 There is nothing really to be done; we synthesize a newline so
2140 the following line doesn't appear to be part of this line. */
2141 error (flocp, _("warning: NUL character seen; rest of line ignored"));
2142 p[0] = '\n';
2143 len = 1;
2146 p += len;
2147 if (p[-1] != '\n')
2149 /* Probably ran out of buffer space. */
2150 register unsigned int p_off = p - buffer;
2151 linebuffer->size *= 2;
2152 buffer = (char *) xrealloc (buffer, linebuffer->size);
2153 p = buffer + p_off;
2154 end = buffer + linebuffer->size;
2155 linebuffer->buffer = buffer;
2156 *p = '\0';
2157 lastlen = len;
2158 continue;
2161 ++nlines;
2163 #if !defined(WINDOWS32) && !defined(__MSDOS__)
2164 /* Check to see if the line was really ended with CRLF; if so ignore
2165 the CR. */
2166 if (len > 1 && p[-2] == '\r')
2168 --len;
2169 --p;
2170 p[-1] = '\n';
2172 #endif
2174 if (len == 1 && p > buffer)
2175 /* P is pointing at a newline and it's the beginning of
2176 the buffer returned by the last fgets call. However,
2177 it is not necessarily the beginning of a line if P is
2178 pointing past the beginning of the holding buffer.
2179 If the buffer was just enlarged (right before the newline),
2180 we must account for that, so we pretend that the two lines
2181 were one line. */
2182 len += lastlen;
2183 lastlen = len;
2184 backslash = 0;
2185 for (p2 = p - 2; --len > 0; --p2)
2187 if (*p2 == '\\')
2188 backslash = !backslash;
2189 else
2190 break;
2193 if (!backslash)
2195 p[-1] = '\0';
2196 break;
2199 if (end - p <= 1)
2201 /* Enlarge the buffer. */
2202 register unsigned int p_off = p - buffer;
2203 linebuffer->size *= 2;
2204 buffer = (char *) xrealloc (buffer, linebuffer->size);
2205 p = buffer + p_off;
2206 end = buffer + linebuffer->size;
2207 linebuffer->buffer = buffer;
2211 if (ferror (stream))
2212 pfatal_with_name (flocp->filenm);
2214 return nlines;
2217 /* Parse the next "makefile word" from the input buffer, and return info
2218 about it.
2220 A "makefile word" is one of:
2222 w_bogus Should never happen
2223 w_eol End of input
2224 w_static A static word; cannot be expanded
2225 w_variable A word containing one or more variables/functions
2226 w_colon A colon
2227 w_dcolon A double-colon
2228 w_semicolon A semicolon
2229 w_comment A comment character
2230 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2232 Note that this function is only used when reading certain parts of the
2233 makefile. Don't use it where special rules hold sway (RHS of a variable,
2234 in a command list, etc.) */
2236 static enum make_word_type
2237 get_next_mword (buffer, delim, startp, length)
2238 char *buffer;
2239 char *delim;
2240 char **startp;
2241 unsigned int *length;
2243 enum make_word_type wtype = w_bogus;
2244 char *p = buffer, *beg;
2245 char c;
2247 /* Skip any leading whitespace. */
2248 while (isblank(*p))
2249 ++p;
2251 beg = p;
2252 c = *(p++);
2253 switch (c)
2255 case '\0':
2256 wtype = w_eol;
2257 break;
2259 case '#':
2260 wtype = w_comment;
2261 break;
2263 case ';':
2264 wtype = w_semicolon;
2265 break;
2267 case '=':
2268 wtype = w_varassign;
2269 break;
2271 case ':':
2272 wtype = w_colon;
2273 switch (*p)
2275 case ':':
2276 ++p;
2277 wtype = w_dcolon;
2278 break;
2280 case '=':
2281 ++p;
2282 wtype = w_varassign;
2283 break;
2285 break;
2287 case '+':
2288 case '?':
2289 if (*p == '=')
2291 ++p;
2292 wtype = w_varassign;
2293 break;
2296 default:
2297 if (delim && strchr (delim, c))
2298 wtype = w_static;
2299 break;
2302 /* Did we find something? If so, return now. */
2303 if (wtype != w_bogus)
2304 goto done;
2306 /* This is some non-operator word. A word consists of the longest
2307 string of characters that doesn't contain whitespace, one of [:=#],
2308 or [?+]=, or one of the chars in the DELIM string. */
2310 /* We start out assuming a static word; if we see a variable we'll
2311 adjust our assumptions then. */
2312 wtype = w_static;
2314 /* We already found the first value of "c", above. */
2315 while (1)
2317 char closeparen;
2318 int count;
2320 switch (c)
2322 case '\0':
2323 case ' ':
2324 case '\t':
2325 case '=':
2326 case '#':
2327 goto done_word;
2329 case ':':
2330 #if defined(__MSDOS__) || defined(WINDOWS32)
2331 /* A word CAN include a colon in its drive spec. The drive
2332 spec is allowed either at the beginning of a word, or as part
2333 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2334 if (!(p - beg >= 2
2335 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
2336 && (p - beg == 2 || p[-3] == '(')))
2337 #endif
2338 goto done_word;
2340 case '$':
2341 c = *(p++);
2342 if (c == '$')
2343 break;
2345 /* This is a variable reference, so note that it's expandable.
2346 Then read it to the matching close paren. */
2347 wtype = w_variable;
2349 if (c == '(')
2350 closeparen = ')';
2351 else if (c == '{')
2352 closeparen = '}';
2353 else
2354 /* This is a single-letter variable reference. */
2355 break;
2357 for (count=0; *p != '\0'; ++p)
2359 if (*p == c)
2360 ++count;
2361 else if (*p == closeparen && --count < 0)
2363 ++p;
2364 break;
2367 break;
2369 case '?':
2370 case '+':
2371 if (*p == '=')
2372 goto done_word;
2373 break;
2375 case '\\':
2376 switch (*p)
2378 case ':':
2379 case ';':
2380 case '=':
2381 case '\\':
2382 ++p;
2383 break;
2385 break;
2387 default:
2388 if (delim && strchr (delim, c))
2389 goto done_word;
2390 break;
2393 c = *(p++);
2395 done_word:
2396 --p;
2398 done:
2399 if (startp)
2400 *startp = beg;
2401 if (length)
2402 *length = p - beg;
2403 return wtype;
2406 /* Construct the list of include directories
2407 from the arguments and the default list. */
2409 void
2410 construct_include_path (arg_dirs)
2411 char **arg_dirs;
2413 register unsigned int i;
2414 #ifdef VAXC /* just don't ask ... */
2415 stat_t stbuf;
2416 #else
2417 struct stat stbuf;
2418 #endif
2419 /* Table to hold the dirs. */
2421 register unsigned int defsize = (sizeof (default_include_directories)
2422 / sizeof (default_include_directories[0]));
2423 register unsigned int max = 5;
2424 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
2425 register unsigned int idx = 0;
2427 #ifdef __MSDOS__
2428 defsize++;
2429 #endif
2431 /* First consider any dirs specified with -I switches.
2432 Ignore dirs that don't exist. */
2434 if (arg_dirs != 0)
2435 while (*arg_dirs != 0)
2437 char *dir = *arg_dirs++;
2439 if (dir[0] == '~')
2441 char *expanded = tilde_expand (dir);
2442 if (expanded != 0)
2443 dir = expanded;
2446 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
2448 if (idx == max - 1)
2450 max += 5;
2451 dirs = (char **)
2452 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
2454 dirs[idx++] = dir;
2456 else if (dir != arg_dirs[-1])
2457 free (dir);
2460 /* Now add at the end the standard default dirs. */
2462 #ifdef __MSDOS__
2464 /* The environment variable $DJDIR holds the root of the
2465 DJGPP directory tree; add ${DJDIR}/include. */
2466 struct variable *djdir = lookup_variable ("DJDIR", 5);
2468 if (djdir)
2470 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2472 strcat (strcpy (defdir, djdir->value), "/include");
2473 dirs[idx++] = defdir;
2476 #endif
2478 for (i = 0; default_include_directories[i] != 0; ++i)
2479 if (stat (default_include_directories[i], &stbuf) == 0
2480 && S_ISDIR (stbuf.st_mode))
2481 dirs[idx++] = default_include_directories[i];
2483 dirs[idx] = 0;
2485 /* Now compute the maximum length of any name in it. */
2487 max_incl_len = 0;
2488 for (i = 0; i < idx; ++i)
2490 unsigned int len = strlen (dirs[i]);
2491 /* If dir name is written with a trailing slash, discard it. */
2492 if (dirs[i][len - 1] == '/')
2493 /* We can't just clobber a null in because it may have come from
2494 a literal string and literal strings may not be writable. */
2495 dirs[i] = savestring (dirs[i], len - 1);
2496 if (len > max_incl_len)
2497 max_incl_len = len;
2500 include_directories = dirs;
2503 /* Expand ~ or ~USER at the beginning of NAME.
2504 Return a newly malloc'd string or 0. */
2506 char *
2507 tilde_expand (name)
2508 char *name;
2510 #ifndef VMS
2511 if (name[1] == '/' || name[1] == '\0')
2513 extern char *getenv ();
2514 char *home_dir;
2515 int is_variable;
2518 /* Turn off --warn-undefined-variables while we expand HOME. */
2519 int save = warn_undefined_variables_flag;
2520 warn_undefined_variables_flag = 0;
2522 home_dir = allocated_variable_expand ("$(HOME)");
2524 warn_undefined_variables_flag = save;
2527 is_variable = home_dir[0] != '\0';
2528 if (!is_variable)
2530 free (home_dir);
2531 home_dir = getenv ("HOME");
2533 #if !defined(_AMIGA) && !defined(WINDOWS32)
2534 if (home_dir == 0 || home_dir[0] == '\0')
2536 extern char *getlogin ();
2537 char *logname = getlogin ();
2538 home_dir = 0;
2539 if (logname != 0)
2541 struct passwd *p = getpwnam (logname);
2542 if (p != 0)
2543 home_dir = p->pw_dir;
2546 #endif /* !AMIGA && !WINDOWS32 */
2547 if (home_dir != 0)
2549 char *new = concat (home_dir, "", name + 1);
2550 if (is_variable)
2551 free (home_dir);
2552 return new;
2555 #if !defined(_AMIGA) && !defined(WINDOWS32)
2556 else
2558 struct passwd *pwent;
2559 char *userend = strchr (name + 1, '/');
2560 if (userend != 0)
2561 *userend = '\0';
2562 pwent = getpwnam (name + 1);
2563 if (pwent != 0)
2565 if (userend == 0)
2566 return xstrdup (pwent->pw_dir);
2567 else
2568 return concat (pwent->pw_dir, "/", userend + 1);
2570 else if (userend != 0)
2571 *userend = '/';
2573 #endif /* !AMIGA && !WINDOWS32 */
2574 #endif /* !VMS */
2575 return 0;
2578 /* Given a chain of struct nameseq's describing a sequence of filenames,
2579 in reverse of the intended order, return a new chain describing the
2580 result of globbing the filenames. The new chain is in forward order.
2581 The links of the old chain are freed or used in the new chain.
2582 Likewise for the names in the old chain.
2584 SIZE is how big to construct chain elements.
2585 This is useful if we want them actually to be other structures
2586 that have room for additional info. */
2588 struct nameseq *
2589 multi_glob (chain, size)
2590 struct nameseq *chain;
2591 unsigned int size;
2593 extern void dir_setup_glob ();
2594 register struct nameseq *new = 0;
2595 register struct nameseq *old;
2596 struct nameseq *nexto;
2597 glob_t gl;
2599 dir_setup_glob (&gl);
2601 for (old = chain; old != 0; old = nexto)
2603 #ifndef NO_ARCHIVES
2604 char *memname;
2605 #endif
2607 nexto = old->next;
2609 if (old->name[0] == '~')
2611 char *newname = tilde_expand (old->name);
2612 if (newname != 0)
2614 free (old->name);
2615 old->name = newname;
2619 #ifndef NO_ARCHIVES
2620 if (ar_name (old->name))
2622 /* OLD->name is an archive member reference.
2623 Replace it with the archive file name,
2624 and save the member name in MEMNAME.
2625 We will glob on the archive name and then
2626 reattach MEMNAME later. */
2627 char *arname;
2628 ar_parse_name (old->name, &arname, &memname);
2629 free (old->name);
2630 old->name = arname;
2632 else
2633 memname = 0;
2634 #endif /* !NO_ARCHIVES */
2636 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
2638 case 0: /* Success. */
2640 register int i = gl.gl_pathc;
2641 while (i-- > 0)
2643 #ifndef NO_ARCHIVES
2644 if (memname != 0)
2646 /* Try to glob on MEMNAME within the archive. */
2647 struct nameseq *found
2648 = ar_glob (gl.gl_pathv[i], memname, size);
2649 if (found == 0)
2651 /* No matches. Use MEMNAME as-is. */
2652 struct nameseq *elt
2653 = (struct nameseq *) xmalloc (size);
2654 unsigned int alen = strlen (gl.gl_pathv[i]);
2655 unsigned int mlen = strlen (memname);
2656 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2657 bcopy (gl.gl_pathv[i], elt->name, alen);
2658 elt->name[alen] = '(';
2659 bcopy (memname, &elt->name[alen + 1], mlen);
2660 elt->name[alen + 1 + mlen] = ')';
2661 elt->name[alen + 1 + mlen + 1] = '\0';
2662 elt->next = new;
2663 new = elt;
2665 else
2667 /* Find the end of the FOUND chain. */
2668 struct nameseq *f = found;
2669 while (f->next != 0)
2670 f = f->next;
2672 /* Attach the chain being built to the end of the FOUND
2673 chain, and make FOUND the new NEW chain. */
2674 f->next = new;
2675 new = found;
2678 free (memname);
2680 else
2681 #endif /* !NO_ARCHIVES */
2683 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2684 elt->name = xstrdup (gl.gl_pathv[i]);
2685 elt->next = new;
2686 new = elt;
2689 globfree (&gl);
2690 free (old->name);
2691 free ((char *)old);
2692 break;
2695 case GLOB_NOSPACE:
2696 fatal (NILF, _("virtual memory exhausted"));
2697 break;
2699 default:
2700 old->next = new;
2701 new = old;
2702 break;
2706 return new;