entered into RCS
[make.git] / read.c
blob2d26f24d9a5cd0e5c71dff77d1793d3e944f0f05
1 /* Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993
2 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, 675 Mass Ave, Cambridge, MA 02139, USA. */
19 #include "make.h"
20 #include "commands.h"
21 #include "dep.h"
22 #include "file.h"
23 #include "variable.h"
25 /* This is POSIX.2, but most systems using -DPOSIX probably don't have it. */
26 #ifdef HAVE_GLOB_H
27 #include <glob.h>
28 #else
29 #include "glob/glob.h"
30 #endif
32 #include <pwd.h>
33 struct passwd *getpwnam ();
36 static void read_makefile ();
37 static unsigned int readline (), do_define ();
38 static int conditional_line ();
39 static void record_files ();
40 static char *find_semicolon ();
43 /* A `struct linebuffer' is a structure which holds a line of text.
44 `readline' reads a line from a stream into a linebuffer
45 and works regardless of the length of the line. */
47 struct linebuffer
49 /* Note: This is the number of bytes malloc'ed for `buffer'
50 It does not indicate `buffer's real length.
51 Instead, a null char indicates end-of-string. */
52 unsigned int size;
53 char *buffer;
56 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
57 #define freebuffer(lb) free ((lb)->buffer)
60 /* A `struct conditionals' contains the information describing
61 all the active conditionals in a makefile.
63 The global variable `conditionals' contains the conditionals
64 information for the current makefile. It is initialized from
65 the static structure `toplevel_conditionals' and is later changed
66 to new structures for included makefiles. */
68 struct conditionals
70 unsigned int if_cmds; /* Depth of conditional nesting. */
71 unsigned int allocated; /* Elts allocated in following arrays. */
72 char *ignoring; /* Are we ignoring or interepreting? */
73 char *seen_else; /* Have we already seen an `else'? */
76 static struct conditionals toplevel_conditionals;
77 static struct conditionals *conditionals = &toplevel_conditionals;
80 /* Default directories to search for include files in */
82 static char *default_include_directories[] =
84 INCLUDEDIR,
85 "/usr/gnu/include",
86 "/usr/local/include",
87 "/usr/include",
91 /* List of directories to search for include files in */
93 static char **include_directories;
95 /* Maximum length of an element of the above. */
97 static unsigned int max_incl_len;
99 /* The filename and pointer to line number of the
100 makefile currently being read in. */
102 char *reading_filename;
103 unsigned int *reading_lineno_ptr;
105 /* The chain of makefiles read by read_makefile. */
107 static struct dep *read_makefiles = 0;
109 /* Read in all the makefiles and return the chain of their names. */
111 struct dep *
112 read_all_makefiles (makefiles)
113 char **makefiles;
115 unsigned int num_makefiles = 0;
117 if (debug_flag)
118 puts ("Reading makefiles...");
120 /* If there's a non-null variable MAKEFILES, its value is a list of
121 files to read first thing. But don't let it prevent reading the
122 default makefiles and don't let the default goal come from there. */
125 char *value;
126 char *name, *p;
127 unsigned int length;
130 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
131 int save = warn_undefined_variables_flag;
132 warn_undefined_variables_flag = 0;
134 value = allocated_variable_expand ("$(MAKEFILES)");
136 warn_undefined_variables_flag = save;
139 /* Set NAME to the start of next token and LENGTH to its length.
140 MAKEFILES is updated for finding remaining tokens. */
141 p = value;
142 while ((name = find_next_token (&p, &length)) != 0)
144 if (*p != '\0')
145 *p++ = '\0';
146 read_makefile (name, RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
149 free (value);
152 /* Read makefiles specified with -f switches. */
154 if (makefiles != 0)
155 while (*makefiles != 0)
157 struct dep *tail = read_makefiles;
158 register struct dep *d;
160 read_makefile (*makefiles, 0);
162 /* Find the right element of read_makefiles. */
163 d = read_makefiles;
164 while (d->next != tail)
165 d = d->next;
167 /* Use the storage read_makefile allocates. */
168 *makefiles = dep_name (d);
169 ++num_makefiles;
170 ++makefiles;
173 /* If there were no -f switches, try the default names. */
175 if (num_makefiles == 0)
177 static char *default_makefiles[] =
178 { "GNUmakefile", "makefile", "Makefile", 0 };
179 register char **p = default_makefiles;
180 while (*p != 0 && !file_exists_p (*p))
181 ++p;
183 if (*p != 0)
184 read_makefile (*p, 0);
185 else
187 /* No default makefile was found. Add the default makefiles to the
188 `read_makefiles' chain so they will be updated if possible. */
189 struct dep *tail = read_makefiles;
190 for (p = default_makefiles; *p != 0; ++p)
192 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
193 d->name = 0;
194 d->file = enter_file (*p);
195 d->file->dontcare = 1;
196 /* Tell update_goal_chain to bail out as soon as this file is
197 made, and main not to die if we can't make this file. */
198 d->changed = RM_DONTCARE;
199 if (tail == 0)
200 read_makefiles = d;
201 else
202 tail->next = d;
203 tail = d;
205 if (tail != 0)
206 tail->next = 0;
210 return read_makefiles;
213 /* Read file FILENAME as a makefile and add its contents to the data base.
215 FLAGS contains bits as above.
217 FILENAME is added to the `read_makefiles' chain. */
219 static void
220 read_makefile (filename, flags)
221 char *filename;
222 int flags;
224 static char *collapsed = 0;
225 static unsigned int collapsed_length = 0;
226 register FILE *infile;
227 struct linebuffer lb;
228 unsigned int commands_len = 200;
229 char *commands = (char *) xmalloc (200);
230 unsigned int commands_idx = 0;
231 unsigned int commands_started;
232 register char *p;
233 char *p2;
234 int ignoring = 0, in_ignored_define = 0;
235 int no_targets = 0; /* Set when reading a rule without targets. */
237 struct nameseq *filenames = 0;
238 struct dep *deps;
239 unsigned int lineno = 1;
240 unsigned int nlines = 0;
241 int two_colon;
242 char *pattern = 0, *pattern_percent;
244 int makefile_errno;
246 #define record_waiting_files() \
247 do \
249 if (filenames != 0) \
250 record_files (filenames, pattern, pattern_percent, deps, \
251 commands_started, commands, commands_idx, \
252 two_colon, filename, lineno, \
253 !(flags & RM_NO_DEFAULT_GOAL)); \
254 filenames = 0; \
255 commands_idx = 0; \
256 pattern = 0; \
257 } while (0)
259 #ifdef lint /* Suppress `used before set' messages. */
260 two_colon = 0;
261 #endif
263 /* First, get a stream to read. */
265 /* Expand ~ in FILENAME unless it came from `include',
266 in which case it was already done. */
267 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
269 char *expanded = tilde_expand (filename);
270 /* This is a possible memory leak, but I don't care. */
271 if (expanded != 0)
272 filename = expanded;
275 infile = fopen (filename, "r");
276 /* Save the error code so we print the right message later. */
277 makefile_errno = errno;
279 /* If the makefile wasn't found and it's either a makefile from
280 the `MAKEFILES' variable or an included makefile,
281 search the included makefile search path for this makefile. */
283 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
285 register unsigned int i;
286 for (i = 0; include_directories[i] != 0; ++i)
288 char *name = concat (include_directories[i], "/", filename);
289 infile = fopen (name, "r");
290 if (infile == 0)
291 free (name);
292 else
294 filename = name;
295 break;
300 /* Add FILENAME to the chain of read makefiles. */
301 deps = (struct dep *) xmalloc (sizeof (struct dep));
302 deps->next = read_makefiles;
303 read_makefiles = deps;
304 deps->name = 0;
305 deps->file = lookup_file (filename);
306 if (deps->file == 0)
308 deps->file = enter_file (savestring (filename, strlen (filename)));
309 if (flags & RM_DONTCARE)
310 deps->file->dontcare = 1;
312 filename = deps->file->name;
313 deps->file->precious = 1;
314 deps->changed = flags;
315 deps = 0;
317 /* If the makefile can't be found at all,
318 either ignore it or give up entirely. */
320 if (infile == 0)
322 if (! (flags & RM_DONTCARE))
324 /* If we did some searching, errno has the error
325 from the last attempt, rather from FILENAME itself. */
326 errno = makefile_errno;
327 perror_with_name ("fopen: ", filename);
329 return;
332 reading_filename = filename;
333 reading_lineno_ptr = &lineno;
335 /* Loop over lines in the file.
336 The strategy is to accumulate target names in FILENAMES, dependencies
337 in DEPS and commands in COMMANDS. These are used to define a rule
338 when the start of the next rule (or eof) is encountered. */
340 initbuffer (&lb);
342 while (!feof (infile))
344 lineno += nlines;
345 nlines = readline (&lb, infile, filename, lineno);
347 if (collapsed_length < lb.size)
349 collapsed_length = lb.size;
350 if (collapsed != 0)
351 free (collapsed);
352 collapsed = (char *) xmalloc (collapsed_length);
354 strcpy (collapsed, lb.buffer);
355 /* Collapse continuation lines. */
356 collapse_continuations (collapsed);
357 remove_comments (collapsed);
359 p = collapsed;
360 while (isspace (*p))
361 ++p;
362 /* We cannot consider a line containing just a tab to be empty
363 because it might constitute an empty command for a target. */
364 if (*p == '\0' && lb.buffer[0] != '\t')
365 continue;
367 /* strncmp is first to avoid dereferencing out into space. */
368 #define word1eq(s, l) (!strncmp (s, p, l) \
369 && (p[l] == '\0' || isblank (p[l])))
370 if (!in_ignored_define
371 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
372 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
373 || word1eq ("else", 4) || word1eq ("endif", 5)))
375 int i = conditional_line (p, filename, lineno);
376 if (i >= 0)
377 ignoring = i;
378 else
379 makefile_fatal (filename, lineno,
380 "invalid syntax in conditional");
381 continue;
383 else if (word1eq ("endef", 5))
385 if (in_ignored_define)
386 in_ignored_define = 0;
387 else
388 makefile_fatal (filename, lineno, "extraneous `endef'");
389 continue;
391 else if (word1eq ("define", 6))
393 if (ignoring)
394 in_ignored_define = 1;
395 else
397 p2 = next_token (p + 6);
398 p = end_of_token (p2);
399 lineno = do_define (p2, p - p2, o_file,
400 lineno, infile, filename);
402 continue;
404 else if (word1eq ("override", 8))
406 p2 = next_token (p + 8);
407 if (p2 == 0)
408 makefile_error (filename, lineno, "empty `override' directive");
409 if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
411 if (ignoring)
412 in_ignored_define = 1;
413 else
415 unsigned int len;
416 p2 = end_of_token (p2);
417 p = find_next_token (&p2, &len);
418 lineno = do_define (p, len, o_override,
419 lineno, infile, filename);
422 else if (!ignoring
423 && !try_variable_definition (filename, lineno,
424 p2, o_override))
425 makefile_error (filename, lineno, "empty `override' directive");
427 continue;
431 if (ignoring)
432 /* Ignore the line. We continue here so conditionals
433 can appear in the middle of a rule. */
434 continue;
435 else if (lb.buffer[0] == '\t')
437 /* This line is a shell command. */
438 unsigned int len;
440 if (no_targets)
441 /* Ignore the commands in a rule with no targets. */
442 continue;
444 /* If there is no preceding rule line, don't treat this line
445 as a command, even though it begins with a tab character.
446 SunOS 4 make appears to behave this way. */
448 if (filenames != 0)
450 /* Append this command line to the line being accumulated. */
451 p = lb.buffer;
452 if (commands_idx == 0)
453 commands_started = 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 if (word1eq ("export", 6))
470 struct variable *v;
471 p2 = next_token (p + 6);
472 if (*p2 == '\0')
473 export_all_variables = 1;
474 v = try_variable_definition (filename, lineno, p2, o_file);
475 if (v != 0)
476 v->export = v_export;
477 else
479 unsigned int len;
480 for (p = find_next_token (&p2, &len); p != 0;
481 p = find_next_token (&p2, &len))
483 v = lookup_variable (p, len);
484 if (v == 0)
485 v = define_variable (p, len, "", o_file, 0);
486 v->export = v_export;
490 else if (word1eq ("unexport", 8))
492 unsigned int len;
493 struct variable *v;
494 p2 = next_token (p + 8);
495 if (*p2 == '\0')
496 export_all_variables = 0;
497 for (p = find_next_token (&p2, &len); p != 0;
498 p = find_next_token (&p2, &len))
500 v = lookup_variable (p, len);
501 if (v == 0)
502 v = define_variable (p, len, "", o_file, 0);
503 v->export = v_noexport;
506 else if (word1eq ("include", 7) || word1eq ("-include", 8))
508 /* We have found an `include' line specifying a nested
509 makefile to be read at this point. */
510 struct conditionals *save, new_conditionals;
511 struct nameseq *files;
512 /* "-include" (vs "include") says no
513 error if the file does not exist. */
514 int noerror = p[0] == '-';
516 p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
517 if (*p == '\0')
519 makefile_error (filename, lineno,
520 "no file name for `%sinclude'",
521 noerror ? "-" : "");
522 continue;
525 /* Parse the list of file names. */
526 p2 = p;
527 files = multi_glob (parse_file_seq (&p2, '\0',
528 sizeof (struct nameseq),
530 sizeof (struct nameseq));
531 free (p);
533 /* Save the state of conditionals and start
534 the included makefile with a clean slate. */
535 save = conditionals;
536 bzero ((char *) &new_conditionals, sizeof new_conditionals);
537 conditionals = &new_conditionals;
539 /* Record the rules that are waiting so they will determine
540 the default goal before those in the included makefile. */
541 record_waiting_files ();
543 /* Read each included makefile. */
544 while (files != 0)
546 struct nameseq *next = files->next;
547 char *name = files->name;
548 free (files);
549 files = next;
551 read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
552 | (noerror ? RM_DONTCARE : 0)));
555 /* Restore state. */
556 conditionals = save;
557 reading_filename = filename;
558 reading_lineno_ptr = &lineno;
560 else if (word1eq ("vpath", 5))
562 char *pattern;
563 unsigned int len;
564 p2 = variable_expand (p + 5);
565 p = find_next_token (&p2, &len);
566 if (p != 0)
568 pattern = savestring (p, len);
569 p = find_next_token (&p2, &len);
570 /* No searchpath means remove all previous
571 selective VPATH's with the same pattern. */
573 else
574 /* No pattern means remove all previous selective VPATH's. */
575 pattern = 0;
576 construct_vpath_list (pattern, p);
577 if (pattern != 0)
578 free (pattern);
580 #undef word1eq
581 else if (try_variable_definition (filename, lineno, p, o_file))
582 /* This line has been dealt with. */
584 else
586 /* This line describes some target files. */
588 char *cmdleft;
590 /* Record the previous rule. */
592 record_waiting_files ();
594 /* Look for a semicolon in the unexpanded line. */
595 cmdleft = find_semicolon (lb.buffer);
596 if (cmdleft != 0)
597 /* Found one. Cut the line short there before expanding it. */
598 *cmdleft = '\0';
600 collapse_continuations (lb.buffer);
602 /* Expand variable and function references before doing anything
603 else so that special characters can be inside variables. */
604 p = variable_expand (lb.buffer);
606 if (cmdleft == 0)
607 /* Look for a semicolon in the expanded line. */
608 cmdleft = find_semicolon (p);
610 if (cmdleft != 0)
611 /* Cut the line short at the semicolon. */
612 *cmdleft = '\0';
614 /* Remove comments from the line. */
615 remove_comments (p);
617 p2 = next_token (p);
618 if (*p2 == '\0')
620 if (cmdleft != 0)
621 makefile_fatal (filename, lineno,
622 "missing rule before commands");
623 else
624 /* This line contained a variable reference that
625 expanded to nothing but whitespace. */
626 continue;
628 else if (*p2 == ':')
630 /* We accept and ignore rules without targets for
631 compatibility with SunOS 4 make. */
632 no_targets = 1;
633 continue;
636 filenames = multi_glob (parse_file_seq (&p2, ':',
637 sizeof (struct nameseq),
639 sizeof (struct nameseq));
640 if (*p2++ == '\0')
641 makefile_fatal (filename, lineno, "missing separator");
642 /* Is this a one-colon or two-colon entry? */
643 two_colon = *p2 == ':';
644 if (two_colon)
645 p2++;
647 /* We have some targets, so don't ignore the following commands. */
648 no_targets = 0;
650 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
651 p = index (p2, ':');
652 while (p != 0 && p[-1] == '\\')
654 register char *q = &p[-1];
655 register int backslash = 0;
656 while (*q-- == '\\')
657 backslash = !backslash;
658 if (backslash)
659 p = index (p + 1, ':');
660 else
661 break;
663 if (p != 0)
665 struct nameseq *target;
666 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
667 ++p2;
668 if (target == 0)
669 makefile_fatal (filename, lineno, "missing target pattern");
670 else if (target->next != 0)
671 makefile_fatal (filename, lineno, "multiple target patterns");
672 pattern = target->name;
673 pattern_percent = find_percent (pattern);
674 if (pattern_percent == 0)
675 makefile_fatal (filename, lineno,
676 "target pattern contains no `%%'");
678 else
679 pattern = 0;
681 /* Parse the dependencies. */
682 deps = (struct dep *)
683 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
684 sizeof (struct dep));
686 commands_idx = 0;
687 if (cmdleft != 0)
689 /* Semicolon means rest of line is a command. */
690 unsigned int len = strlen (cmdleft + 1);
692 commands_started = lineno;
694 /* Add this command line to the buffer. */
695 if (len + 2 > commands_len)
697 commands_len = (len + 2) * 2;
698 commands = (char *) xrealloc (commands, commands_len);
700 bcopy (cmdleft + 1, commands, len);
701 commands_idx += len;
702 commands[commands_idx++] = '\n';
705 continue;
708 /* We get here except in the case that we just read a rule line.
709 Record now the last rule we read, so following spurious
710 commands are properly diagnosed. */
711 record_waiting_files ();
712 no_targets = 0;
715 if (conditionals->if_cmds)
716 makefile_fatal (filename, lineno, "missing `endif'");
718 /* At eof, record the last rule. */
719 record_waiting_files ();
721 freebuffer (&lb);
722 free ((char *) commands);
723 fclose (infile);
725 reading_filename = 0;
726 reading_lineno_ptr = 0;
729 /* Execute a `define' directive.
730 The first line has already been read, and NAME is the name of
731 the variable to be defined. The following lines remain to be read.
732 LINENO, INFILE and FILENAME refer to the makefile being read.
733 The value returned is LINENO, updated for lines read here. */
735 static unsigned int
736 do_define (name, namelen, origin, lineno, infile, filename)
737 char *name;
738 unsigned int namelen;
739 enum variable_origin origin;
740 unsigned int lineno;
741 FILE *infile;
742 char *filename;
744 struct linebuffer lb;
745 unsigned int nlines = 0;
746 unsigned int length = 100;
747 char *definition = (char *) xmalloc (100);
748 register unsigned int idx = 0;
749 register char *p;
751 /* Expand the variable name. */
752 char *var = (char *) alloca (namelen + 1);
753 bcopy (name, var, namelen);
754 var[namelen] = '\0';
755 var = variable_expand (var);
757 initbuffer (&lb);
758 while (!feof (infile))
760 lineno += nlines;
761 nlines = readline (&lb, infile, filename, lineno);
762 p = next_token (lb.buffer);
764 if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
766 p += 5;
767 collapse_continuations (p);
768 remove_comments (p);
769 if (*next_token (p) != '\0')
770 makefile_error (filename, lineno,
771 "Extraneous text after `endef' directive");
772 /* Define the variable. */
773 if (idx == 0)
774 definition[0] = '\0';
775 else
776 definition[idx - 1] = '\0';
777 (void) define_variable (var, strlen (var), definition, origin, 1);
778 free (definition);
779 freebuffer (&lb);
780 return lineno;
782 else
784 unsigned int len = strlen (p);
786 /* Increase the buffer size if necessary. */
787 if (idx + len + 1 > length)
789 length = (idx + len) * 2;
790 definition = (char *) xrealloc (definition, length + 1);
793 bcopy (p, &definition[idx], len);
794 idx += len;
795 /* Separate lines with a newline. */
796 definition[idx++] = '\n';
800 /* No `endef'!! */
801 makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
803 /* NOTREACHED */
804 return 0;
807 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
808 "ifneq", "else" and "endif".
809 LINE is the input line, with the command as its first word.
811 FILENAME and LINENO are the filename and line number in the
812 current makefile. They are used for error messages.
814 Value is -1 if the line is invalid,
815 0 if following text should be interpreted,
816 1 if following text should be ignored. */
818 static int
819 conditional_line (line, filename, lineno)
820 char *line;
821 char *filename;
822 unsigned int lineno;
824 int notdef;
825 char *cmdname;
826 register unsigned int i;
828 if (*line == 'i')
830 /* It's an "if..." command. */
831 notdef = line[2] == 'n';
832 if (notdef)
834 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
835 line += cmdname[3] == 'd' ? 7 : 6;
837 else
839 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
840 line += cmdname[2] == 'd' ? 6 : 5;
843 else
845 /* It's an "else" or "endif" command. */
846 notdef = line[1] == 'n';
847 cmdname = notdef ? "endif" : "else";
848 line += notdef ? 5 : 4;
851 line = next_token (line);
853 if (*cmdname == 'e')
855 if (*line != '\0')
856 makefile_error (filename, lineno,
857 "Extraneous text after `%s' directive",
858 cmdname);
859 /* "Else" or "endif". */
860 if (conditionals->if_cmds == 0)
861 makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
862 /* NOTDEF indicates an `endif' command. */
863 if (notdef)
864 --conditionals->if_cmds;
865 else if (conditionals->seen_else[conditionals->if_cmds - 1])
866 makefile_fatal (filename, lineno, "only one `else' per conditional");
867 else
869 /* Toggle the state of ignorance. */
870 conditionals->ignoring[conditionals->if_cmds - 1]
871 = !conditionals->ignoring[conditionals->if_cmds - 1];
872 /* Record that we have seen an `else' in this conditional.
873 A second `else' will be erroneous. */
874 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
876 for (i = 0; i < conditionals->if_cmds; ++i)
877 if (conditionals->ignoring[i])
878 return 1;
879 return 0;
882 if (conditionals->allocated == 0)
884 conditionals->allocated = 5;
885 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
886 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
889 ++conditionals->if_cmds;
890 if (conditionals->if_cmds > conditionals->allocated)
892 conditionals->allocated += 5;
893 conditionals->ignoring = (char *)
894 xrealloc (conditionals->ignoring, conditionals->allocated);
895 conditionals->seen_else = (char *)
896 xrealloc (conditionals->seen_else, conditionals->allocated);
899 /* Record that we have seen an `if...' but no `else' so far. */
900 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
902 /* Search through the stack to see if we're already ignoring. */
903 for (i = 0; i < conditionals->if_cmds - 1; ++i)
904 if (conditionals->ignoring[i])
906 /* We are already ignoring, so just push a level
907 to match the next "else" or "endif", and keep ignoring.
908 We don't want to expand variables in the condition. */
909 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
910 return 1;
913 if (cmdname[notdef ? 3 : 2] == 'd')
915 /* "Ifdef" or "ifndef". */
916 struct variable *v;
917 register char *p = end_of_token (line);
918 i = p - line;
919 p = next_token (p);
920 if (*p != '\0')
921 return -1;
922 v = lookup_variable (line, i);
923 conditionals->ignoring[conditionals->if_cmds - 1]
924 = (v != 0 && *v->value != '\0') == notdef;
926 else
928 /* "Ifeq" or "ifneq". */
929 char *s1, *s2;
930 unsigned int len;
931 char termin = *line == '(' ? ',' : *line;
933 if (termin != ',' && termin != '"' && termin != '\'')
934 return -1;
936 s1 = ++line;
937 /* Find the end of the first string. */
938 if (termin == ',')
940 register int count = 0;
941 for (; *line != '\0'; ++line)
942 if (*line == '(')
943 ++count;
944 else if (*line == ')')
945 --count;
946 else if (*line == ',' && count <= 0)
947 break;
949 else
950 while (*line != '\0' && *line != termin)
951 ++line;
953 if (*line == '\0')
954 return -1;
956 *line++ = '\0';
958 s2 = variable_expand (s1);
959 /* We must allocate a new copy of the expanded string because
960 variable_expand re-uses the same buffer. */
961 len = strlen (s2);
962 s1 = (char *) alloca (len + 1);
963 bcopy (s2, s1, len + 1);
965 if (termin != ',')
966 /* Find the start of the second string. */
967 line = next_token (line);
969 termin = termin == ',' ? ')' : *line;
970 if (termin != ')' && termin != '"' && termin != '\'')
971 return -1;
973 /* Find the end of the second string. */
974 if (termin == ')')
976 register int count = 0;
977 s2 = next_token (line);
978 for (line = s2; *line != '\0'; ++line)
980 if (*line == '(')
981 ++count;
982 else if (*line == ')')
983 if (count <= 0)
984 break;
985 else
986 --count;
989 else
991 ++line;
992 s2 = line;
993 while (*line != '\0' && *line != termin)
994 ++line;
997 if (*line == '\0')
998 return -1;
1000 *line = '\0';
1001 line = next_token (++line);
1002 if (*line != '\0')
1003 makefile_error (filename, lineno,
1004 "Extraneous text after `%s' directive",
1005 cmdname);
1007 s2 = variable_expand (s2);
1008 conditionals->ignoring[conditionals->if_cmds - 1]
1009 = streq (s1, s2) == notdef;
1012 /* Search through the stack to see if we're ignoring. */
1013 for (i = 0; i < conditionals->if_cmds; ++i)
1014 if (conditionals->ignoring[i])
1015 return 1;
1016 return 0;
1019 /* Remove duplicate dependencies in CHAIN. */
1021 void
1022 uniquize_deps (chain)
1023 struct dep *chain;
1025 register struct dep *d;
1027 /* Make sure that no dependencies are repeated. This does not
1028 really matter for the purpose of updating targets, but it
1029 might make some names be listed twice for $^ and $?. */
1031 for (d = chain; d != 0; d = d->next)
1033 struct dep *last, *next;
1035 last = d;
1036 next = d->next;
1037 while (next != 0)
1038 if (streq (dep_name (d), dep_name (next)))
1040 struct dep *n = next->next;
1041 last->next = n;
1042 if (next->name != 0 && next->name != d->name)
1043 free (next->name);
1044 if (next != d)
1045 free ((char *) next);
1046 next = n;
1048 else
1050 last = next;
1051 next = next->next;
1056 /* Record a description line for files FILENAMES,
1057 with dependencies DEPS, commands to execute described
1058 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1059 TWO_COLON is nonzero if a double colon was used.
1060 If not nil, PATTERN is the `%' pattern to make this
1061 a static pattern rule, and PATTERN_PERCENT is a pointer
1062 to the `%' within it.
1064 The links of FILENAMES are freed, and so are any names in it
1065 that are not incorporated into other data structures. */
1067 static void
1068 record_files (filenames, pattern, pattern_percent, deps, commands_started,
1069 commands, commands_idx, two_colon, filename, lineno, set_default)
1070 struct nameseq *filenames;
1071 char *pattern, *pattern_percent;
1072 struct dep *deps;
1073 unsigned int commands_started;
1074 char *commands;
1075 unsigned int commands_idx;
1076 int two_colon;
1077 char *filename;
1078 unsigned int lineno;
1079 int set_default;
1081 struct nameseq *nextf;
1082 int implicit = 0;
1083 unsigned int max_targets, target_idx;
1084 char **targets = 0, **target_percents = 0;
1085 struct commands *cmds;
1087 if (commands_idx > 0)
1089 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1090 cmds->filename = filename;
1091 cmds->lineno = commands_started;
1092 cmds->commands = savestring (commands, commands_idx);
1093 cmds->command_lines = 0;
1095 else
1096 cmds = 0;
1098 for (; filenames != 0; filenames = nextf)
1100 register char *name = filenames->name;
1101 register struct file *f;
1102 register struct dep *d;
1103 struct dep *this;
1104 char *implicit_percent;
1106 nextf = filenames->next;
1107 free ((char *) filenames);
1109 implicit_percent = find_percent (name);
1110 implicit |= implicit_percent != 0;
1112 if (implicit && pattern != 0)
1113 makefile_fatal (filename, lineno,
1114 "mixed implicit and static pattern rules");
1116 if (implicit && implicit_percent == 0)
1117 makefile_fatal (filename, lineno, "mixed implicit and normal rules");
1119 if (implicit)
1121 if (targets == 0)
1123 max_targets = 5;
1124 targets = (char **) xmalloc (5 * sizeof (char *));
1125 target_percents = (char **) xmalloc (5 * sizeof (char *));
1126 target_idx = 0;
1128 else if (target_idx == max_targets - 1)
1130 max_targets += 5;
1131 targets = (char **) xrealloc ((char *) targets,
1132 max_targets * sizeof (char *));
1133 target_percents
1134 = (char **) xrealloc ((char *) target_percents,
1135 max_targets * sizeof (char *));
1137 targets[target_idx] = name;
1138 target_percents[target_idx] = implicit_percent;
1139 ++target_idx;
1140 continue;
1143 /* If there are multiple filenames, copy the chain DEPS
1144 for all but the last one. It is not safe for the same deps
1145 to go in more than one place in the data base. */
1146 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1148 if (pattern != 0)
1149 /* If this is an extended static rule:
1150 `targets: target%pattern: dep%pattern; cmds',
1151 translate each dependency pattern into a plain filename
1152 using the target pattern and this target's name. */
1153 if (!pattern_matches (pattern, pattern_percent, name))
1155 /* Give a warning if the rule is meaningless. */
1156 makefile_error (filename, lineno,
1157 "target `%s' doesn't match the target pattern",
1158 name);
1159 this = 0;
1161 else
1163 /* We use patsubst_expand to do the work of translating
1164 the target pattern, the target's name and the dependencies'
1165 patterns into plain dependency names. */
1166 char *buffer = variable_expand ("");
1168 for (d = this; d != 0; d = d->next)
1170 char *o;
1171 char *percent = find_percent (d->name);
1172 if (percent == 0)
1173 continue;
1174 o = patsubst_expand (buffer, name, pattern, d->name,
1175 pattern_percent, percent);
1176 free (d->name);
1177 d->name = savestring (buffer, o - buffer);
1181 if (!two_colon)
1183 /* Single-colon. Combine these dependencies
1184 with others in file's existing record, if any. */
1185 f = enter_file (name);
1187 if (f->double_colon)
1188 makefile_fatal (filename, lineno,
1189 "target file `%s' has both : and :: entries",
1190 f->name);
1192 /* If CMDS == F->CMDS, this target was listed in this rule
1193 more than once. Just give a warning since this is harmless. */
1194 if (cmds != 0 && cmds == f->cmds)
1195 makefile_error
1196 (filename, lineno,
1197 "target `%s' given more than once in the same rule.",
1198 f->name);
1200 /* Check for two single-colon entries both with commands.
1201 Check is_target so that we don't lose on files such as .c.o
1202 whose commands were preinitialized. */
1203 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1205 makefile_error (cmds->filename, cmds->lineno,
1206 "warning: overriding commands for target `%s'",
1207 f->name);
1208 makefile_error (f->cmds->filename, f->cmds->lineno,
1209 "warning: ignoring old commands for target `%s'",
1210 f->name);
1213 f->is_target = 1;
1215 /* Defining .DEFAULT with no deps or cmds clears it. */
1216 if (f == default_file && this == 0 && cmds == 0)
1217 f->cmds = 0;
1218 if (cmds != 0)
1219 f->cmds = cmds;
1220 /* Defining .SUFFIXES with no dependencies
1221 clears out the list of suffixes. */
1222 if (f == suffix_file && this == 0)
1224 d = f->deps;
1225 while (d != 0)
1227 struct dep *nextd = d->next;
1228 free (d->name);
1229 free (d);
1230 d = nextd;
1232 f->deps = 0;
1234 else if (f->deps != 0)
1236 /* Add the file's old deps and the new ones in THIS together. */
1238 struct dep *firstdeps, *moredeps;
1239 if (cmds != 0)
1241 /* This is the rule with commands, so put its deps first.
1242 The rationale behind this is that $< expands to the
1243 first dep in the chain, and commands use $< expecting
1244 to get the dep that rule specifies. */
1245 firstdeps = this;
1246 moredeps = f->deps;
1248 else
1250 /* Append the new deps to the old ones. */
1251 firstdeps = f->deps;
1252 moredeps = this;
1255 if (firstdeps == 0)
1256 firstdeps = moredeps;
1257 else
1259 d = firstdeps;
1260 while (d->next != 0)
1261 d = d->next;
1262 d->next = moredeps;
1265 f->deps = firstdeps;
1267 else
1268 f->deps = this;
1270 /* If this is a static pattern rule, set the file's stem to
1271 the part of its name that matched the `%' in the pattern,
1272 so you can use $* in the commands. */
1273 if (pattern != 0)
1275 static char *percent = "%";
1276 char *buffer = variable_expand ("");
1277 char *o = patsubst_expand (buffer, name, pattern, percent,
1278 pattern_percent, percent);
1279 f->stem = savestring (buffer, o - buffer);
1282 else
1284 /* Double-colon. Make a new record
1285 even if the file already has one. */
1286 f = lookup_file (name);
1287 /* Check for both : and :: rules. Check is_target so
1288 we don't lose on default suffix rules or makefiles. */
1289 if (f != 0 && f->is_target && !f->double_colon)
1290 makefile_fatal (filename, lineno,
1291 "target file `%s' has both : and :: entries",
1292 f->name);
1293 f = enter_file (name);
1294 /* If there was an existing entry and it was a
1295 double-colon entry, enter_file will have returned a
1296 new one, making it the prev pointer of the old one. */
1297 f->is_target = 1;
1298 f->double_colon = 1;
1299 f->deps = this;
1300 f->cmds = cmds;
1303 /* Free name if not needed further. */
1304 if (f != 0 && name != f->name
1305 && (name < f->name || name > f->name + strlen (f->name)))
1307 free (name);
1308 name = f->name;
1311 /* See if this is first target seen whose name does
1312 not start with a `.', unless it contains a slash. */
1313 if (default_goal_file == 0 && set_default
1314 && (*name != '.' || index (name, '/') != 0))
1316 int reject = 0;
1318 /* If this file is a suffix, don't
1319 let it be the default goal file. */
1321 for (d = suffix_file->deps; d != 0; d = d->next)
1323 register struct dep *d2;
1324 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1326 reject = 1;
1327 break;
1329 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1331 register unsigned int len = strlen (dep_name (d2));
1332 if (strncmp (name, dep_name (d2), len))
1333 continue;
1334 if (streq (name + len, dep_name (d)))
1336 reject = 1;
1337 break;
1340 if (reject)
1341 break;
1344 if (!reject)
1345 default_goal_file = f;
1349 if (implicit)
1351 targets[target_idx] = 0;
1352 target_percents[target_idx] = 0;
1353 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1354 free ((char *) target_percents);
1358 /* Search STRING for an unquoted ; that is not after an unquoted #. */
1360 static char *
1361 find_semicolon (string)
1362 char *string;
1364 char *found, *p;
1366 found = index (string, ';');
1367 while (found != 0 && found[-1] == '\\')
1369 register char *q = &found[-1];
1370 register int backslash = 0;
1371 while (*q-- == '\\')
1372 backslash = !backslash;
1373 if (backslash)
1374 found = index (found + 1, ';');
1375 else
1376 break;
1378 if (found == 0)
1379 return 0;
1381 /* Look for a comment character (#) before the ; we found. */
1382 p = lindex (string, found, '#');
1383 while (p != 0 && p[-1] == '\\')
1385 register char *q = &p[-1];
1386 register int backslash = 0;
1387 while (*q-- == '\\')
1388 backslash = !backslash;
1389 if (backslash)
1390 p = lindex (p + 1, found, '#');
1391 else
1392 break;
1394 if (p == 0)
1395 return found;
1396 return 0;
1399 /* Search PATTERN for an unquoted %. Backslashes quote % and backslash.
1400 Quoting backslashes are removed from PATTERN by compacting it into
1401 itself. Returns a pointer to the first unquoted % if there is one,
1402 or nil if there are none. */
1404 char *
1405 find_percent (pattern)
1406 char *pattern;
1408 unsigned int pattern_len = strlen (pattern);
1409 register char *p = pattern;
1411 while ((p = index (p, '%')) != 0)
1412 if (p > pattern && p[-1] == '\\')
1414 /* Search for more backslashes. */
1415 register int i = -2;
1416 while (&p[i] >= pattern && p[i] == '\\')
1417 --i;
1418 ++i;
1419 /* The number of backslashes is now -I.
1420 Copy P over itself to swallow half of them. */
1421 bcopy (&p[i / 2], &p[i], (pattern_len - (p - pattern)) - (i / 2) + 1);
1422 p += i / 2;
1423 if (i % 2 == 0)
1424 /* All the backslashes quoted each other; the % was unquoted. */
1425 return p;
1427 /* The % was quoted by a backslash. Look for another. */
1429 else
1430 /* No backslash in sight. */
1431 return p;
1433 /* Never hit a %. */
1434 return 0;
1437 /* Parse a string into a sequence of filenames represented as a
1438 chain of struct nameseq's in reverse order and return that chain.
1440 The string is passed as STRINGP, the address of a string pointer.
1441 The string pointer is updated to point at the first character
1442 not parsed, which either is a null char or equals STOPCHAR.
1444 SIZE is how big to construct chain elements.
1445 This is useful if we want them actually to be other structures
1446 that have room for additional info.
1448 If STRIP is nonzero, strip `./'s off the beginning. */
1450 struct nameseq *
1451 parse_file_seq (stringp, stopchar, size, strip)
1452 char **stringp;
1453 char stopchar;
1454 unsigned int size;
1455 int strip;
1457 register struct nameseq *new = 0;
1458 register struct nameseq *new1;
1459 register char *p = *stringp;
1460 char *q;
1461 char *name;
1462 register int c;
1464 while (1)
1466 /* Skip whitespace; see if any more names are left. */
1467 p = next_token (p);
1468 if (*p == '\0')
1469 break;
1470 if (*p == stopchar)
1471 break;
1472 /* Yes, find end of next name. */
1473 q = p;
1474 while (1)
1476 c = *p++;
1477 if (c == '\0')
1478 break;
1479 else if (c == '\\' &&
1480 (*p == '\\' || isblank (*p) || *p == stopchar))
1481 ++p;
1482 else if (isblank (c) || c == stopchar)
1483 break;
1485 p--;
1487 if (strip)
1488 /* Skip leading `./'s. */
1489 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1491 q += 2; /* Skip "./". */
1492 while (q < p && *q == '/')
1493 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1494 ++q;
1497 /* Extract the filename just found, and skip it. */
1499 if (q == p)
1500 /* ".///" was stripped to "". */
1501 name = savestring ("./", 2);
1502 else
1503 name = savestring (q, p - q);
1505 /* Add it to the front of the chain. */
1506 new1 = (struct nameseq *) xmalloc (size);
1507 new1->name = name;
1508 new1->next = new;
1509 new = new1;
1512 #ifndef NO_ARCHIVES
1514 /* Look for multi-word archive references.
1515 They are indicated by a elt ending with an unmatched `)' and
1516 an elt further down the chain (i.e., previous in the file list)
1517 with an unmatched `(' (e.g., "lib(mem"). */
1519 for (new1 = new; new1 != 0; new1 = new1->next)
1520 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1521 && new1->name[strlen (new1->name) - 1] == ')'
1522 && index (new1->name, '(') == 0)
1524 /* NEW1 ends with a `)' but does not contain a `('.
1525 Look back for an elt with an opening `(' but no closing `)'. */
1527 struct nameseq *n = new1->next, *lastn = new1;
1528 char *paren;
1529 while (n != 0 && (paren = index (n->name, '(')) == 0)
1531 lastn = n;
1532 n = n->next;
1534 if (n != 0)
1536 /* N is the first element in the archive group.
1537 Its name looks like "lib(mem" (with no closing `)'). */
1539 char *libname;
1541 /* Copy "lib(" into LIBNAME. */
1542 ++paren;
1543 libname = (char *) alloca (paren - n->name + 1);
1544 bcopy (n->name, libname, paren - n->name);
1545 libname[paren - n->name] = '\0';
1547 if (*paren == '\0')
1549 /* N was just "lib(", part of something like "lib( a b)".
1550 Edit it out of the chain and free its storage. */
1551 lastn->next = n->next;
1552 free (n->name);
1553 free ((char *) n);
1554 /* LASTN->next is the new stopping elt for the loop below. */
1555 n = lastn->next;
1557 else
1559 /* Replace N's name with the full archive reference. */
1560 name = concat (libname, paren, ")");
1561 free (n->name);
1562 n->name = name;
1565 if (new1->name[1] == '\0')
1567 /* NEW1 is just ")", part of something like "lib(a b )".
1568 Omit it from the chain and free its storage. */
1569 lastn = new1;
1570 new1 = new1->next;
1571 if (new == lastn)
1572 new = new1;
1573 free (lastn->name);
1574 free ((char *) lastn);
1576 else
1578 /* Replace also NEW1->name, which already has closing `)'. */
1579 name = concat (libname, new1->name, "");
1580 free (new1->name);
1581 new1->name = name;
1582 new1 = new1->next;
1585 /* Trace back from NEW1 (the end of the list) until N
1586 (the beginning of the list), rewriting each name
1587 with the full archive reference. */
1589 while (new1 != n)
1591 name = concat (libname, new1->name, ")");
1592 free (new1->name);
1593 new1->name = name;
1594 new1 = new1->next;
1599 #endif
1601 *stringp = p;
1602 return new;
1605 /* Read a line of text from STREAM into LINEBUFFER.
1606 Combine continuation lines into one line.
1607 Return the number of actual lines read (> 1 if hacked continuation lines).
1610 static unsigned int
1611 readline (linebuffer, stream, filename, lineno)
1612 struct linebuffer *linebuffer;
1613 FILE *stream;
1614 char *filename;
1615 unsigned int lineno;
1617 char *buffer = linebuffer->buffer;
1618 register char *p = linebuffer->buffer;
1619 register char *end = p + linebuffer->size;
1620 register int len, lastlen = 0;
1621 register char *p2;
1622 register unsigned int nlines = 0;
1623 register int backslash;
1625 *p = '\0';
1627 while (1)
1629 if (fgets (p, end - p, stream) == 0)
1630 if (feof (stream))
1631 break;
1632 else
1633 pfatal_with_name (filename);
1635 len = strlen (p);
1636 if (len == 0)
1637 /* This only happens when the first thing on the line is a '\0'. */
1638 makefile_fatal (filename, lineno, "NUL not allowed in makefile");
1640 p += len;
1641 if (p[-1] != '\n')
1643 /* Probably ran out of buffer space. */
1644 register unsigned int p_off = p - buffer;
1645 linebuffer->size *= 2;
1646 buffer = (char *) xrealloc (buffer, linebuffer->size);
1647 p = buffer + p_off;
1648 end = buffer + linebuffer->size;
1649 linebuffer->buffer = buffer;
1650 *p = '\0';
1651 lastlen = len;
1652 continue;
1655 ++nlines;
1657 if (len == 1 && p > buffer)
1658 /* P is pointing at a newline and it's the beginning of
1659 the buffer returned by the last fgets call. However,
1660 it is not necessarily the beginning of a line if P is
1661 pointing past the beginning of the holding buffer.
1662 If the buffer was just enlarged (right before the newline),
1663 we must account for that, so we pretend that the two lines
1664 were one line. */
1665 len += lastlen;
1666 lastlen = len;
1667 backslash = 0;
1668 for (p2 = p - 2; --len > 0; --p2)
1670 if (*p2 == '\\')
1671 backslash = !backslash;
1672 else
1673 break;
1676 if (!backslash)
1678 p[-1] = '\0';
1679 break;
1682 if (end - p <= 1)
1684 /* Enlarge the buffer. */
1685 register unsigned int p_off = p - buffer;
1686 linebuffer->size *= 2;
1687 buffer = (char *) xrealloc (buffer, linebuffer->size);
1688 p = buffer + p_off;
1689 end = buffer + linebuffer->size;
1690 linebuffer->buffer = buffer;
1694 return nlines;
1697 /* Construct the list of include directories
1698 from the arguments and the default list. */
1700 void
1701 construct_include_path (arg_dirs)
1702 char **arg_dirs;
1704 register unsigned int i;
1705 struct stat stbuf;
1707 /* Table to hold the dirs. */
1709 register unsigned int defsize = (sizeof (default_include_directories)
1710 / sizeof (default_include_directories[0]));
1711 register unsigned int max = 5;
1712 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
1713 register unsigned int idx = 0;
1715 /* First consider any dirs specified with -I switches.
1716 Ignore dirs that don't exist. */
1718 if (arg_dirs != 0)
1719 while (*arg_dirs != 0)
1721 char *dir = *arg_dirs++;
1723 if (dir[0] == '~')
1725 char *expanded = tilde_expand (dir);
1726 if (expanded != 0)
1727 dir = expanded;
1730 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
1732 if (idx == max - 1)
1734 max += 5;
1735 dirs = (char **)
1736 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
1738 dirs[idx++] = dir;
1740 else if (dir != arg_dirs[-1])
1741 free (dir);
1744 /* Now add at the end the standard default dirs. */
1746 for (i = 0; default_include_directories[i] != 0; ++i)
1747 if (stat (default_include_directories[i], &stbuf) == 0
1748 && S_ISDIR (stbuf.st_mode))
1749 dirs[idx++] = default_include_directories[i];
1751 dirs[idx] = 0;
1753 /* Now compute the maximum length of any name in it. */
1755 max_incl_len = 0;
1756 for (i = 0; i < idx; ++i)
1758 unsigned int len = strlen (dirs[i]);
1759 /* If dir name is written with a trailing slash, discard it. */
1760 if (dirs[i][len - 1] == '/')
1761 /* We can't just clobber a null in because it may have come from
1762 a literal string and literal strings may not be writable. */
1763 dirs[i] = savestring (dirs[i], len - 1);
1764 if (len > max_incl_len)
1765 max_incl_len = len;
1768 include_directories = dirs;
1771 /* Expand ~ or ~USER at the beginning of NAME.
1772 Return a newly malloc'd string or 0. */
1774 char *
1775 tilde_expand (name)
1776 char *name;
1778 if (name[1] == '/' || name[1] == '\0')
1780 extern char *getenv ();
1781 char *home_dir;
1782 int is_variable;
1785 /* Turn off --warn-undefined-variables while we expand HOME. */
1786 int save = warn_undefined_variables_flag;
1787 warn_undefined_variables_flag = 0;
1789 home_dir = allocated_variable_expand ("$(HOME)");
1791 warn_undefined_variables_flag = save;
1794 is_variable = home_dir[0] != '\0';
1795 if (!is_variable)
1797 free (home_dir);
1798 home_dir = getenv ("HOME");
1800 if (home_dir == 0 || home_dir[0] == '\0')
1802 extern char *getlogin ();
1803 char *name = getlogin ();
1804 home_dir = 0;
1805 if (name != 0)
1807 struct passwd *p = getpwnam (name);
1808 if (p != 0)
1809 home_dir = p->pw_dir;
1812 if (home_dir != 0)
1814 char *new = concat (home_dir, "", name + 1);
1815 if (is_variable)
1816 free (home_dir);
1817 return new;
1820 else
1822 struct passwd *pwent;
1823 char *userend = index (name + 1, '/');
1824 if (userend != 0)
1825 *userend = '\0';
1826 pwent = getpwnam (name + 1);
1827 if (pwent != 0)
1829 if (userend == 0)
1830 return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
1831 else
1832 return concat (pwent->pw_dir, "/", userend + 1);
1834 else if (userend != 0)
1835 *userend = '/';
1838 return 0;
1841 /* Given a chain of struct nameseq's describing a sequence of filenames,
1842 in reverse of the intended order, return a new chain describing the
1843 result of globbing the filenames. The new chain is in forward order.
1844 The links of the old chain are freed or used in the new chain.
1845 Likewise for the names in the old chain.
1847 SIZE is how big to construct chain elements.
1848 This is useful if we want them actually to be other structures
1849 that have room for additional info. */
1851 struct nameseq *
1852 multi_glob (chain, size)
1853 struct nameseq *chain;
1854 unsigned int size;
1856 register struct nameseq *new = 0;
1857 register struct nameseq *old;
1858 struct nameseq *nexto;
1860 for (old = chain; old != 0; old = nexto)
1862 glob_t gl;
1863 #ifndef NO_ARCHIVES
1864 char *memname;
1865 #endif
1867 nexto = old->next;
1869 if (old->name[0] == '~')
1871 char *newname = tilde_expand (old->name);
1872 if (newname != 0)
1874 free (old->name);
1875 old->name = newname;
1879 #ifndef NO_ARCHIVES
1880 if (ar_name (old->name))
1882 /* OLD->name is an archive member reference.
1883 Replace it with the archive file name,
1884 and save the member name in MEMNAME.
1885 We will glob on the archive name and then
1886 reattach MEMNAME later. */
1887 char *arname;
1888 ar_parse_name (old->name, &arname, &memname);
1889 free (old->name);
1890 old->name = arname;
1892 else
1893 memname = 0;
1894 #endif
1896 switch (glob (old->name, GLOB_NOCHECK, NULL, &gl))
1898 case 0: /* Success. */
1900 register int i = gl.gl_pathc;
1901 while (i-- > 0)
1903 #ifndef NO_ARCHIVES
1904 if (memname != 0)
1906 /* Try to glob on MEMNAME within the archive. */
1907 struct nameseq *found
1908 = ar_glob (gl.gl_pathv[i], memname, size);
1909 if (found == 0)
1911 /* No matches. Use MEMNAME as-is. */
1912 struct nameseq *elt
1913 = (struct nameseq *) xmalloc (size);
1914 unsigned int alen = strlen (gl.gl_pathv[i]);
1915 unsigned int mlen = strlen (memname);
1916 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
1917 bcopy (gl.gl_pathv[i], elt->name, alen);
1918 elt->name[alen] = '(';
1919 bcopy (memname, &elt->name[alen + 1], mlen);
1920 elt->name[alen + 1 + mlen] = ')';
1921 elt->name[alen + 1 + mlen + 1] = '\0';
1922 elt->next = new;
1923 new = elt;
1925 else
1927 /* Find the end of the FOUND chain. */
1928 struct nameseq *f = found;
1929 while (f->next != 0)
1930 f = f->next;
1932 /* Attach the chain being built to the end of the FOUND
1933 chain, and make FOUND the new NEW chain. */
1934 f->next = new;
1935 new = found;
1938 free (memname);
1940 else
1941 #endif
1943 struct nameseq *elt = (struct nameseq *) xmalloc (size);
1944 elt->name = savestring (gl.gl_pathv[i],
1945 strlen (gl.gl_pathv[i]));
1946 elt->next = new;
1947 new = elt;
1950 globfree (&gl);
1951 free (old->name);
1952 free (old);
1953 break;
1956 case GLOB_NOSPACE:
1957 fatal ("virtual memory exhausted");
1958 break;
1960 default:
1961 old->next = new;
1962 new = old;
1963 break;
1967 return new;