Formerly configure.in.~30~
[make.git] / read.c
blob0ad6c5bca9e05c0c7ec497cceca87a98b70311d8
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 int 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 (void) read_makefile (name,
147 RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
150 free (value);
153 /* Read makefiles specified with -f switches. */
155 if (makefiles != 0)
156 while (*makefiles != 0)
158 struct dep *tail = read_makefiles;
159 register struct dep *d;
161 if (! read_makefile (*makefiles, 0))
162 perror_with_name ("", *makefiles);
164 /* Find the right element of read_makefiles. */
165 d = read_makefiles;
166 while (d->next != tail)
167 d = d->next;
169 /* Use the storage read_makefile allocates. */
170 *makefiles = dep_name (d);
171 ++num_makefiles;
172 ++makefiles;
175 /* If there were no -f switches, try the default names. */
177 if (num_makefiles == 0)
179 static char *default_makefiles[] =
180 { "GNUmakefile", "makefile", "Makefile", 0 };
181 register char **p = default_makefiles;
182 while (*p != 0 && !file_exists_p (*p))
183 ++p;
185 if (*p != 0)
187 if (! read_makefile (*p, 0))
188 perror_with_name ("", *p);
190 else
192 /* No default makefile was found. Add the default makefiles to the
193 `read_makefiles' chain so they will be updated if possible. */
194 struct dep *tail = read_makefiles;
195 for (p = default_makefiles; *p != 0; ++p)
197 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
198 d->name = 0;
199 d->file = enter_file (*p);
200 d->file->dontcare = 1;
201 /* Tell update_goal_chain to bail out as soon as this file is
202 made, and main not to die if we can't make this file. */
203 d->changed = RM_DONTCARE;
204 if (tail == 0)
205 read_makefiles = d;
206 else
207 tail->next = d;
208 tail = d;
210 if (tail != 0)
211 tail->next = 0;
215 return read_makefiles;
218 /* Read file FILENAME as a makefile and add its contents to the data base.
220 FLAGS contains bits as above.
222 FILENAME is added to the `read_makefiles' chain.
224 Returns 1 if a file was found and read, 0 if not. */
226 static int
227 read_makefile (filename, flags)
228 char *filename;
229 int flags;
231 static char *collapsed = 0;
232 static unsigned int collapsed_length = 0;
233 register FILE *infile;
234 struct linebuffer lb;
235 unsigned int commands_len = 200;
236 char *commands = (char *) xmalloc (200);
237 unsigned int commands_idx = 0;
238 unsigned int commands_started;
239 register char *p;
240 char *p2;
241 int ignoring = 0, in_ignored_define = 0;
242 int no_targets = 0; /* Set when reading a rule without targets. */
244 struct nameseq *filenames = 0;
245 struct dep *deps;
246 unsigned int lineno = 1;
247 unsigned int nlines = 0;
248 int two_colon;
249 char *pattern = 0, *pattern_percent;
251 int makefile_errno;
253 #define record_waiting_files() \
254 do \
256 if (filenames != 0) \
257 record_files (filenames, pattern, pattern_percent, deps, \
258 commands_started, commands, commands_idx, \
259 two_colon, filename, lineno, \
260 !(flags & RM_NO_DEFAULT_GOAL)); \
261 filenames = 0; \
262 commands_idx = 0; \
263 pattern = 0; \
264 } while (0)
266 #ifdef lint /* Suppress `used before set' messages. */
267 two_colon = 0;
268 #endif
270 /* First, get a stream to read. */
272 /* Expand ~ in FILENAME unless it came from `include',
273 in which case it was already done. */
274 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
276 char *expanded = tilde_expand (filename);
277 /* This is a possible memory leak, but I don't care. */
278 if (expanded != 0)
279 filename = expanded;
282 infile = fopen (filename, "r");
283 /* Save the error code so we print the right message later. */
284 makefile_errno = errno;
286 /* If the makefile wasn't found and it's either a makefile from
287 the `MAKEFILES' variable or an included makefile,
288 search the included makefile search path for this makefile. */
290 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
292 register unsigned int i;
293 for (i = 0; include_directories[i] != 0; ++i)
295 char *name = concat (include_directories[i], "/", filename);
296 infile = fopen (name, "r");
297 if (infile == 0)
298 free (name);
299 else
301 filename = name;
302 break;
307 /* Add FILENAME to the chain of read makefiles. */
308 deps = (struct dep *) xmalloc (sizeof (struct dep));
309 deps->next = read_makefiles;
310 read_makefiles = deps;
311 deps->name = 0;
312 deps->file = lookup_file (filename);
313 if (deps->file == 0)
315 deps->file = enter_file (savestring (filename, strlen (filename)));
316 if (flags & RM_DONTCARE)
317 deps->file->dontcare = 1;
319 filename = deps->file->name;
320 deps->file->precious = 1;
321 deps->changed = flags;
322 deps = 0;
324 /* If the makefile can't be found at all, give up entirely. */
326 if (infile == 0)
328 /* If we did some searching, errno has the error from the last
329 attempt, rather from FILENAME itself. Restore it in case the
330 caller wants to use it in a message. */
331 errno = makefile_errno;
332 return 0;
335 reading_filename = filename;
336 reading_lineno_ptr = &lineno;
338 /* Loop over lines in the file.
339 The strategy is to accumulate target names in FILENAMES, dependencies
340 in DEPS and commands in COMMANDS. These are used to define a rule
341 when the start of the next rule (or eof) is encountered. */
343 initbuffer (&lb);
345 while (!feof (infile))
347 lineno += nlines;
348 nlines = readline (&lb, infile, filename, lineno);
350 if (collapsed_length < lb.size)
352 collapsed_length = lb.size;
353 if (collapsed != 0)
354 free (collapsed);
355 collapsed = (char *) xmalloc (collapsed_length);
357 strcpy (collapsed, lb.buffer);
358 /* Collapse continuation lines. */
359 collapse_continuations (collapsed);
360 remove_comments (collapsed);
362 p = collapsed;
363 while (isspace (*p))
364 ++p;
365 /* We cannot consider a line containing just a tab to be empty
366 because it might constitute an empty command for a target. */
367 if (*p == '\0' && lb.buffer[0] != '\t')
368 continue;
370 /* strncmp is first to avoid dereferencing out into space. */
371 #define word1eq(s, l) (!strncmp (s, p, l) \
372 && (p[l] == '\0' || isblank (p[l])))
373 if (!in_ignored_define
374 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
375 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
376 || word1eq ("else", 4) || word1eq ("endif", 5)))
378 int i = conditional_line (p, filename, lineno);
379 if (i >= 0)
380 ignoring = i;
381 else
382 makefile_fatal (filename, lineno,
383 "invalid syntax in conditional");
384 continue;
386 else if (word1eq ("endef", 5))
388 if (in_ignored_define)
389 in_ignored_define = 0;
390 else
391 makefile_fatal (filename, lineno, "extraneous `endef'");
392 continue;
394 else if (word1eq ("define", 6))
396 if (ignoring)
397 in_ignored_define = 1;
398 else
400 p2 = next_token (p + 6);
401 p = end_of_token (p2);
402 lineno = do_define (p2, p - p2, o_file,
403 lineno, infile, filename);
405 continue;
407 else if (word1eq ("override", 8))
409 p2 = next_token (p + 8);
410 if (p2 == 0)
411 makefile_error (filename, lineno, "empty `override' directive");
412 if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
414 if (ignoring)
415 in_ignored_define = 1;
416 else
418 unsigned int len;
419 p2 = end_of_token (p2);
420 p = find_next_token (&p2, &len);
421 lineno = do_define (p, len, o_override,
422 lineno, infile, filename);
425 else if (!ignoring
426 && !try_variable_definition (filename, lineno,
427 p2, o_override))
428 makefile_error (filename, lineno, "empty `override' directive");
430 continue;
434 if (ignoring)
435 /* Ignore the line. We continue here so conditionals
436 can appear in the middle of a rule. */
437 continue;
438 else if (lb.buffer[0] == '\t')
440 /* This line is a shell command. */
441 unsigned int len;
443 if (no_targets)
444 /* Ignore the commands in a rule with no targets. */
445 continue;
447 /* If there is no preceding rule line, don't treat this line
448 as a command, even though it begins with a tab character.
449 SunOS 4 make appears to behave this way. */
451 if (filenames != 0)
453 /* Append this command line to the line being accumulated. */
454 p = lb.buffer;
455 if (commands_idx == 0)
456 commands_started = lineno;
457 len = strlen (p);
458 if (len + 1 + commands_idx > commands_len)
460 commands_len = (len + 1 + commands_idx) * 2;
461 commands = (char *) xrealloc (commands, commands_len);
463 bcopy (p, &commands[commands_idx], len);
464 commands_idx += len;
465 commands[commands_idx++] = '\n';
467 continue;
471 if (word1eq ("export", 6))
473 struct variable *v;
474 p2 = next_token (p + 6);
475 if (*p2 == '\0')
476 export_all_variables = 1;
477 v = try_variable_definition (filename, lineno, p2, o_file);
478 if (v != 0)
479 v->export = v_export;
480 else
482 unsigned int len;
483 for (p = find_next_token (&p2, &len); p != 0;
484 p = find_next_token (&p2, &len))
486 v = lookup_variable (p, len);
487 if (v == 0)
488 v = define_variable (p, len, "", o_file, 0);
489 v->export = v_export;
493 else if (word1eq ("unexport", 8))
495 unsigned int len;
496 struct variable *v;
497 p2 = next_token (p + 8);
498 if (*p2 == '\0')
499 export_all_variables = 0;
500 for (p = find_next_token (&p2, &len); p != 0;
501 p = find_next_token (&p2, &len))
503 v = lookup_variable (p, len);
504 if (v == 0)
505 v = define_variable (p, len, "", o_file, 0);
506 v->export = v_noexport;
509 else if (word1eq ("include", 7) || word1eq ("-include", 8))
511 /* We have found an `include' line specifying a nested
512 makefile to be read at this point. */
513 struct conditionals *save, new_conditionals;
514 struct nameseq *files;
515 /* "-include" (vs "include") says no
516 error if the file does not exist. */
517 int noerror = p[0] == '-';
519 p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
520 if (*p == '\0')
522 makefile_error (filename, lineno,
523 "no file name for `%sinclude'",
524 noerror ? "-" : "");
525 continue;
528 /* Parse the list of file names. */
529 p2 = p;
530 files = multi_glob (parse_file_seq (&p2, '\0',
531 sizeof (struct nameseq),
533 sizeof (struct nameseq));
534 free (p);
536 /* Save the state of conditionals and start
537 the included makefile with a clean slate. */
538 save = conditionals;
539 bzero ((char *) &new_conditionals, sizeof new_conditionals);
540 conditionals = &new_conditionals;
542 /* Record the rules that are waiting so they will determine
543 the default goal before those in the included makefile. */
544 record_waiting_files ();
546 /* Read each included makefile. */
547 while (files != 0)
549 struct nameseq *next = files->next;
550 char *name = files->name;
551 free (files);
552 files = next;
554 if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
555 | (noerror ? RM_DONTCARE : 0))))
556 makefile_error (filename, lineno,
557 "%s: %s", name, strerror (errno));
560 /* Free any space allocated by conditional_line. */
561 if (conditionals->ignoring)
562 free (conditionals->ignoring);
563 if (conditionals->seen_else)
564 free (conditionals->seen_else);
566 /* Restore state. */
567 conditionals = save;
568 reading_filename = filename;
569 reading_lineno_ptr = &lineno;
571 else if (word1eq ("vpath", 5))
573 char *pattern;
574 unsigned int len;
575 p2 = variable_expand (p + 5);
576 p = find_next_token (&p2, &len);
577 if (p != 0)
579 pattern = savestring (p, len);
580 p = find_next_token (&p2, &len);
581 /* No searchpath means remove all previous
582 selective VPATH's with the same pattern. */
584 else
585 /* No pattern means remove all previous selective VPATH's. */
586 pattern = 0;
587 construct_vpath_list (pattern, p);
588 if (pattern != 0)
589 free (pattern);
591 #undef word1eq
592 else if (try_variable_definition (filename, lineno, p, o_file))
593 /* This line has been dealt with. */
595 else if (lb.buffer[0] == '\t')
596 /* This line starts with a tab but was not caught above
597 because there was no preceding target, and the line
598 might have been usable as a variable definition.
599 But now it is definitely lossage. */
600 makefile_fatal (filename, lineno,
601 "commands commence before first target");
602 else
604 /* This line describes some target files. */
606 char *cmdleft;
608 /* Record the previous rule. */
610 record_waiting_files ();
612 /* Look for a semicolon in the unexpanded line. */
613 cmdleft = find_semicolon (lb.buffer);
614 if (cmdleft != 0)
615 /* Found one. Cut the line short there before expanding it. */
616 *cmdleft = '\0';
618 collapse_continuations (lb.buffer);
620 /* Expand variable and function references before doing anything
621 else so that special characters can be inside variables. */
622 p = variable_expand (lb.buffer);
624 if (cmdleft == 0)
625 /* Look for a semicolon in the expanded line. */
626 cmdleft = find_semicolon (p);
628 if (cmdleft != 0)
629 /* Cut the line short at the semicolon. */
630 *cmdleft = '\0';
632 /* Remove comments from the line. */
633 remove_comments (p);
635 p2 = next_token (p);
636 if (*p2 == '\0')
638 if (cmdleft != 0)
639 makefile_fatal (filename, lineno,
640 "missing rule before commands");
641 else
642 /* This line contained a variable reference that
643 expanded to nothing but whitespace. */
644 continue;
646 else if (*p2 == ':')
648 /* We accept and ignore rules without targets for
649 compatibility with SunOS 4 make. */
650 no_targets = 1;
651 continue;
654 filenames = multi_glob (parse_file_seq (&p2, ':',
655 sizeof (struct nameseq),
657 sizeof (struct nameseq));
658 if (*p2++ == '\0')
659 makefile_fatal (filename, lineno, "missing separator");
660 /* Is this a one-colon or two-colon entry? */
661 two_colon = *p2 == ':';
662 if (two_colon)
663 p2++;
665 /* We have some targets, so don't ignore the following commands. */
666 no_targets = 0;
668 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
669 p = index (p2, ':');
670 while (p != 0 && p[-1] == '\\')
672 register char *q = &p[-1];
673 register int backslash = 0;
674 while (*q-- == '\\')
675 backslash = !backslash;
676 if (backslash)
677 p = index (p + 1, ':');
678 else
679 break;
681 if (p != 0)
683 struct nameseq *target;
684 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
685 ++p2;
686 if (target == 0)
687 makefile_fatal (filename, lineno, "missing target pattern");
688 else if (target->next != 0)
689 makefile_fatal (filename, lineno, "multiple target patterns");
690 pattern = target->name;
691 pattern_percent = find_percent (pattern);
692 if (pattern_percent == 0)
693 makefile_fatal (filename, lineno,
694 "target pattern contains no `%%'");
696 else
697 pattern = 0;
699 /* Parse the dependencies. */
700 deps = (struct dep *)
701 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
702 sizeof (struct dep));
704 commands_idx = 0;
705 if (cmdleft != 0)
707 /* Semicolon means rest of line is a command. */
708 unsigned int len = strlen (cmdleft + 1);
710 commands_started = lineno;
712 /* Add this command line to the buffer. */
713 if (len + 2 > commands_len)
715 commands_len = (len + 2) * 2;
716 commands = (char *) xrealloc (commands, commands_len);
718 bcopy (cmdleft + 1, commands, len);
719 commands_idx += len;
720 commands[commands_idx++] = '\n';
723 continue;
726 /* We get here except in the case that we just read a rule line.
727 Record now the last rule we read, so following spurious
728 commands are properly diagnosed. */
729 record_waiting_files ();
730 no_targets = 0;
733 if (conditionals->if_cmds)
734 makefile_fatal (filename, lineno, "missing `endif'");
736 /* At eof, record the last rule. */
737 record_waiting_files ();
739 freebuffer (&lb);
740 free ((char *) commands);
741 fclose (infile);
743 reading_filename = 0;
744 reading_lineno_ptr = 0;
746 return 1;
749 /* Execute a `define' directive.
750 The first line has already been read, and NAME is the name of
751 the variable to be defined. The following lines remain to be read.
752 LINENO, INFILE and FILENAME refer to the makefile being read.
753 The value returned is LINENO, updated for lines read here. */
755 static unsigned int
756 do_define (name, namelen, origin, lineno, infile, filename)
757 char *name;
758 unsigned int namelen;
759 enum variable_origin origin;
760 unsigned int lineno;
761 FILE *infile;
762 char *filename;
764 struct linebuffer lb;
765 unsigned int nlines = 0;
766 unsigned int length = 100;
767 char *definition = (char *) xmalloc (100);
768 register unsigned int idx = 0;
769 register char *p;
771 /* Expand the variable name. */
772 char *var = (char *) alloca (namelen + 1);
773 bcopy (name, var, namelen);
774 var[namelen] = '\0';
775 var = variable_expand (var);
777 initbuffer (&lb);
778 while (!feof (infile))
780 lineno += nlines;
781 nlines = readline (&lb, infile, filename, lineno);
782 p = next_token (lb.buffer);
784 if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
786 p += 5;
787 collapse_continuations (p);
788 remove_comments (p);
789 if (*next_token (p) != '\0')
790 makefile_error (filename, lineno,
791 "Extraneous text after `endef' directive");
792 /* Define the variable. */
793 if (idx == 0)
794 definition[0] = '\0';
795 else
796 definition[idx - 1] = '\0';
797 (void) define_variable (var, strlen (var), definition, origin, 1);
798 free (definition);
799 freebuffer (&lb);
800 return lineno;
802 else
804 unsigned int len = strlen (p);
806 /* Increase the buffer size if necessary. */
807 if (idx + len + 1 > length)
809 length = (idx + len) * 2;
810 definition = (char *) xrealloc (definition, length + 1);
813 bcopy (p, &definition[idx], len);
814 idx += len;
815 /* Separate lines with a newline. */
816 definition[idx++] = '\n';
820 /* No `endef'!! */
821 makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
823 /* NOTREACHED */
824 return 0;
827 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
828 "ifneq", "else" and "endif".
829 LINE is the input line, with the command as its first word.
831 FILENAME and LINENO are the filename and line number in the
832 current makefile. They are used for error messages.
834 Value is -1 if the line is invalid,
835 0 if following text should be interpreted,
836 1 if following text should be ignored. */
838 static int
839 conditional_line (line, filename, lineno)
840 char *line;
841 char *filename;
842 unsigned int lineno;
844 int notdef;
845 char *cmdname;
846 register unsigned int i;
848 if (*line == 'i')
850 /* It's an "if..." command. */
851 notdef = line[2] == 'n';
852 if (notdef)
854 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
855 line += cmdname[3] == 'd' ? 7 : 6;
857 else
859 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
860 line += cmdname[2] == 'd' ? 6 : 5;
863 else
865 /* It's an "else" or "endif" command. */
866 notdef = line[1] == 'n';
867 cmdname = notdef ? "endif" : "else";
868 line += notdef ? 5 : 4;
871 line = next_token (line);
873 if (*cmdname == 'e')
875 if (*line != '\0')
876 makefile_error (filename, lineno,
877 "Extraneous text after `%s' directive",
878 cmdname);
879 /* "Else" or "endif". */
880 if (conditionals->if_cmds == 0)
881 makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
882 /* NOTDEF indicates an `endif' command. */
883 if (notdef)
884 --conditionals->if_cmds;
885 else if (conditionals->seen_else[conditionals->if_cmds - 1])
886 makefile_fatal (filename, lineno, "only one `else' per conditional");
887 else
889 /* Toggle the state of ignorance. */
890 conditionals->ignoring[conditionals->if_cmds - 1]
891 = !conditionals->ignoring[conditionals->if_cmds - 1];
892 /* Record that we have seen an `else' in this conditional.
893 A second `else' will be erroneous. */
894 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
896 for (i = 0; i < conditionals->if_cmds; ++i)
897 if (conditionals->ignoring[i])
898 return 1;
899 return 0;
902 if (conditionals->allocated == 0)
904 conditionals->allocated = 5;
905 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
906 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
909 ++conditionals->if_cmds;
910 if (conditionals->if_cmds > conditionals->allocated)
912 conditionals->allocated += 5;
913 conditionals->ignoring = (char *)
914 xrealloc (conditionals->ignoring, conditionals->allocated);
915 conditionals->seen_else = (char *)
916 xrealloc (conditionals->seen_else, conditionals->allocated);
919 /* Record that we have seen an `if...' but no `else' so far. */
920 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
922 /* Search through the stack to see if we're already ignoring. */
923 for (i = 0; i < conditionals->if_cmds - 1; ++i)
924 if (conditionals->ignoring[i])
926 /* We are already ignoring, so just push a level
927 to match the next "else" or "endif", and keep ignoring.
928 We don't want to expand variables in the condition. */
929 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
930 return 1;
933 if (cmdname[notdef ? 3 : 2] == 'd')
935 /* "Ifdef" or "ifndef". */
936 struct variable *v;
937 register char *p = end_of_token (line);
938 i = p - line;
939 p = next_token (p);
940 if (*p != '\0')
941 return -1;
942 v = lookup_variable (line, i);
943 conditionals->ignoring[conditionals->if_cmds - 1]
944 = (v != 0 && *v->value != '\0') == notdef;
946 else
948 /* "Ifeq" or "ifneq". */
949 char *s1, *s2;
950 unsigned int len;
951 char termin = *line == '(' ? ',' : *line;
953 if (termin != ',' && termin != '"' && termin != '\'')
954 return -1;
956 s1 = ++line;
957 /* Find the end of the first string. */
958 if (termin == ',')
960 register int count = 0;
961 for (; *line != '\0'; ++line)
962 if (*line == '(')
963 ++count;
964 else if (*line == ')')
965 --count;
966 else if (*line == ',' && count <= 0)
967 break;
969 else
970 while (*line != '\0' && *line != termin)
971 ++line;
973 if (*line == '\0')
974 return -1;
976 *line++ = '\0';
978 s2 = variable_expand (s1);
979 /* We must allocate a new copy of the expanded string because
980 variable_expand re-uses the same buffer. */
981 len = strlen (s2);
982 s1 = (char *) alloca (len + 1);
983 bcopy (s2, s1, len + 1);
985 if (termin != ',')
986 /* Find the start of the second string. */
987 line = next_token (line);
989 termin = termin == ',' ? ')' : *line;
990 if (termin != ')' && termin != '"' && termin != '\'')
991 return -1;
993 /* Find the end of the second string. */
994 if (termin == ')')
996 register int count = 0;
997 s2 = next_token (line);
998 for (line = s2; *line != '\0'; ++line)
1000 if (*line == '(')
1001 ++count;
1002 else if (*line == ')')
1003 if (count <= 0)
1004 break;
1005 else
1006 --count;
1009 else
1011 ++line;
1012 s2 = line;
1013 while (*line != '\0' && *line != termin)
1014 ++line;
1017 if (*line == '\0')
1018 return -1;
1020 *line = '\0';
1021 line = next_token (++line);
1022 if (*line != '\0')
1023 makefile_error (filename, lineno,
1024 "Extraneous text after `%s' directive",
1025 cmdname);
1027 s2 = variable_expand (s2);
1028 conditionals->ignoring[conditionals->if_cmds - 1]
1029 = streq (s1, s2) == notdef;
1032 /* Search through the stack to see if we're ignoring. */
1033 for (i = 0; i < conditionals->if_cmds; ++i)
1034 if (conditionals->ignoring[i])
1035 return 1;
1036 return 0;
1039 /* Remove duplicate dependencies in CHAIN. */
1041 void
1042 uniquize_deps (chain)
1043 struct dep *chain;
1045 register struct dep *d;
1047 /* Make sure that no dependencies are repeated. This does not
1048 really matter for the purpose of updating targets, but it
1049 might make some names be listed twice for $^ and $?. */
1051 for (d = chain; d != 0; d = d->next)
1053 struct dep *last, *next;
1055 last = d;
1056 next = d->next;
1057 while (next != 0)
1058 if (streq (dep_name (d), dep_name (next)))
1060 struct dep *n = next->next;
1061 last->next = n;
1062 if (next->name != 0 && next->name != d->name)
1063 free (next->name);
1064 if (next != d)
1065 free ((char *) next);
1066 next = n;
1068 else
1070 last = next;
1071 next = next->next;
1076 /* Record a description line for files FILENAMES,
1077 with dependencies DEPS, commands to execute described
1078 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1079 TWO_COLON is nonzero if a double colon was used.
1080 If not nil, PATTERN is the `%' pattern to make this
1081 a static pattern rule, and PATTERN_PERCENT is a pointer
1082 to the `%' within it.
1084 The links of FILENAMES are freed, and so are any names in it
1085 that are not incorporated into other data structures. */
1087 static void
1088 record_files (filenames, pattern, pattern_percent, deps, commands_started,
1089 commands, commands_idx, two_colon, filename, lineno, set_default)
1090 struct nameseq *filenames;
1091 char *pattern, *pattern_percent;
1092 struct dep *deps;
1093 unsigned int commands_started;
1094 char *commands;
1095 unsigned int commands_idx;
1096 int two_colon;
1097 char *filename;
1098 unsigned int lineno;
1099 int set_default;
1101 struct nameseq *nextf;
1102 int implicit = 0;
1103 unsigned int max_targets, target_idx;
1104 char **targets = 0, **target_percents = 0;
1105 struct commands *cmds;
1107 if (commands_idx > 0)
1109 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1110 cmds->filename = filename;
1111 cmds->lineno = commands_started;
1112 cmds->commands = savestring (commands, commands_idx);
1113 cmds->command_lines = 0;
1115 else
1116 cmds = 0;
1118 for (; filenames != 0; filenames = nextf)
1120 register char *name = filenames->name;
1121 register struct file *f;
1122 register struct dep *d;
1123 struct dep *this;
1124 char *implicit_percent;
1126 nextf = filenames->next;
1127 free ((char *) filenames);
1129 implicit_percent = find_percent (name);
1130 implicit |= implicit_percent != 0;
1132 if (implicit && pattern != 0)
1133 makefile_fatal (filename, lineno,
1134 "mixed implicit and static pattern rules");
1136 if (implicit && implicit_percent == 0)
1137 makefile_fatal (filename, lineno, "mixed implicit and normal rules");
1139 if (implicit)
1141 if (targets == 0)
1143 max_targets = 5;
1144 targets = (char **) xmalloc (5 * sizeof (char *));
1145 target_percents = (char **) xmalloc (5 * sizeof (char *));
1146 target_idx = 0;
1148 else if (target_idx == max_targets - 1)
1150 max_targets += 5;
1151 targets = (char **) xrealloc ((char *) targets,
1152 max_targets * sizeof (char *));
1153 target_percents
1154 = (char **) xrealloc ((char *) target_percents,
1155 max_targets * sizeof (char *));
1157 targets[target_idx] = name;
1158 target_percents[target_idx] = implicit_percent;
1159 ++target_idx;
1160 continue;
1163 /* If there are multiple filenames, copy the chain DEPS
1164 for all but the last one. It is not safe for the same deps
1165 to go in more than one place in the data base. */
1166 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1168 if (pattern != 0)
1169 /* If this is an extended static rule:
1170 `targets: target%pattern: dep%pattern; cmds',
1171 translate each dependency pattern into a plain filename
1172 using the target pattern and this target's name. */
1173 if (!pattern_matches (pattern, pattern_percent, name))
1175 /* Give a warning if the rule is meaningless. */
1176 makefile_error (filename, lineno,
1177 "target `%s' doesn't match the target pattern",
1178 name);
1179 this = 0;
1181 else
1183 /* We use patsubst_expand to do the work of translating
1184 the target pattern, the target's name and the dependencies'
1185 patterns into plain dependency names. */
1186 char *buffer = variable_expand ("");
1188 for (d = this; d != 0; d = d->next)
1190 char *o;
1191 char *percent = find_percent (d->name);
1192 if (percent == 0)
1193 continue;
1194 o = patsubst_expand (buffer, name, pattern, d->name,
1195 pattern_percent, percent);
1196 free (d->name);
1197 d->name = savestring (buffer, o - buffer);
1201 if (!two_colon)
1203 /* Single-colon. Combine these dependencies
1204 with others in file's existing record, if any. */
1205 f = enter_file (name);
1207 if (f->double_colon)
1208 makefile_fatal (filename, lineno,
1209 "target file `%s' has both : and :: entries",
1210 f->name);
1212 /* If CMDS == F->CMDS, this target was listed in this rule
1213 more than once. Just give a warning since this is harmless. */
1214 if (cmds != 0 && cmds == f->cmds)
1215 makefile_error
1216 (filename, lineno,
1217 "target `%s' given more than once in the same rule.",
1218 f->name);
1220 /* Check for two single-colon entries both with commands.
1221 Check is_target so that we don't lose on files such as .c.o
1222 whose commands were preinitialized. */
1223 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1225 makefile_error (cmds->filename, cmds->lineno,
1226 "warning: overriding commands for target `%s'",
1227 f->name);
1228 makefile_error (f->cmds->filename, f->cmds->lineno,
1229 "warning: ignoring old commands for target `%s'",
1230 f->name);
1233 f->is_target = 1;
1235 /* Defining .DEFAULT with no deps or cmds clears it. */
1236 if (f == default_file && this == 0 && cmds == 0)
1237 f->cmds = 0;
1238 if (cmds != 0)
1239 f->cmds = cmds;
1240 /* Defining .SUFFIXES with no dependencies
1241 clears out the list of suffixes. */
1242 if (f == suffix_file && this == 0)
1244 d = f->deps;
1245 while (d != 0)
1247 struct dep *nextd = d->next;
1248 free (d->name);
1249 free (d);
1250 d = nextd;
1252 f->deps = 0;
1254 else if (f->deps != 0)
1256 /* Add the file's old deps and the new ones in THIS together. */
1258 struct dep *firstdeps, *moredeps;
1259 if (cmds != 0)
1261 /* This is the rule with commands, so put its deps first.
1262 The rationale behind this is that $< expands to the
1263 first dep in the chain, and commands use $< expecting
1264 to get the dep that rule specifies. */
1265 firstdeps = this;
1266 moredeps = f->deps;
1268 else
1270 /* Append the new deps to the old ones. */
1271 firstdeps = f->deps;
1272 moredeps = this;
1275 if (firstdeps == 0)
1276 firstdeps = moredeps;
1277 else
1279 d = firstdeps;
1280 while (d->next != 0)
1281 d = d->next;
1282 d->next = moredeps;
1285 f->deps = firstdeps;
1287 else
1288 f->deps = this;
1290 /* If this is a static pattern rule, set the file's stem to
1291 the part of its name that matched the `%' in the pattern,
1292 so you can use $* in the commands. */
1293 if (pattern != 0)
1295 static char *percent = "%";
1296 char *buffer = variable_expand ("");
1297 char *o = patsubst_expand (buffer, name, pattern, percent,
1298 pattern_percent, percent);
1299 f->stem = savestring (buffer, o - buffer);
1302 else
1304 /* Double-colon. Make a new record
1305 even if the file already has one. */
1306 f = lookup_file (name);
1307 /* Check for both : and :: rules. Check is_target so
1308 we don't lose on default suffix rules or makefiles. */
1309 if (f != 0 && f->is_target && !f->double_colon)
1310 makefile_fatal (filename, lineno,
1311 "target file `%s' has both : and :: entries",
1312 f->name);
1313 f = enter_file (name);
1314 /* If there was an existing entry and it was a
1315 double-colon entry, enter_file will have returned a
1316 new one, making it the prev pointer of the old one. */
1317 f->is_target = 1;
1318 f->double_colon = 1;
1319 f->deps = this;
1320 f->cmds = cmds;
1323 /* Free name if not needed further. */
1324 if (f != 0 && name != f->name
1325 && (name < f->name || name > f->name + strlen (f->name)))
1327 free (name);
1328 name = f->name;
1331 /* See if this is first target seen whose name does
1332 not start with a `.', unless it contains a slash. */
1333 if (default_goal_file == 0 && set_default
1334 && (*name != '.' || index (name, '/') != 0))
1336 int reject = 0;
1338 /* If this file is a suffix, don't
1339 let it be the default goal file. */
1341 for (d = suffix_file->deps; d != 0; d = d->next)
1343 register struct dep *d2;
1344 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1346 reject = 1;
1347 break;
1349 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1351 register unsigned int len = strlen (dep_name (d2));
1352 if (strncmp (name, dep_name (d2), len))
1353 continue;
1354 if (streq (name + len, dep_name (d)))
1356 reject = 1;
1357 break;
1360 if (reject)
1361 break;
1364 if (!reject)
1365 default_goal_file = f;
1369 if (implicit)
1371 targets[target_idx] = 0;
1372 target_percents[target_idx] = 0;
1373 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1374 free ((char *) target_percents);
1378 /* Search STRING for an unquoted ; that is not after an unquoted #. */
1380 static char *
1381 find_semicolon (string)
1382 char *string;
1384 char *found, *p;
1386 found = index (string, ';');
1387 while (found != 0 && found[-1] == '\\')
1389 register char *q = &found[-1];
1390 register int backslash = 0;
1391 while (*q-- == '\\')
1392 backslash = !backslash;
1393 if (backslash)
1394 found = index (found + 1, ';');
1395 else
1396 break;
1398 if (found == 0)
1399 return 0;
1401 /* Look for a comment character (#) before the ; we found. */
1402 p = lindex (string, found, '#');
1403 while (p != 0 && p[-1] == '\\')
1405 register char *q = &p[-1];
1406 register int backslash = 0;
1407 while (*q-- == '\\')
1408 backslash = !backslash;
1409 if (backslash)
1410 p = lindex (p + 1, found, '#');
1411 else
1412 break;
1414 if (p == 0)
1415 return found;
1416 return 0;
1419 /* Search PATTERN for an unquoted %. Backslashes quote % and backslash.
1420 Quoting backslashes are removed from PATTERN by compacting it into
1421 itself. Returns a pointer to the first unquoted % if there is one,
1422 or nil if there are none. */
1424 char *
1425 find_percent (pattern)
1426 char *pattern;
1428 unsigned int pattern_len = strlen (pattern);
1429 register char *p = pattern;
1431 while ((p = index (p, '%')) != 0)
1432 if (p > pattern && p[-1] == '\\')
1434 /* Search for more backslashes. */
1435 register int i = -2;
1436 while (&p[i] >= pattern && p[i] == '\\')
1437 --i;
1438 ++i;
1439 /* The number of backslashes is now -I.
1440 Copy P over itself to swallow half of them. */
1441 bcopy (&p[i / 2], &p[i], (pattern_len - (p - pattern)) - (i / 2) + 1);
1442 p += i / 2;
1443 if (i % 2 == 0)
1444 /* All the backslashes quoted each other; the % was unquoted. */
1445 return p;
1447 /* The % was quoted by a backslash. Look for another. */
1449 else
1450 /* No backslash in sight. */
1451 return p;
1453 /* Never hit a %. */
1454 return 0;
1457 /* Parse a string into a sequence of filenames represented as a
1458 chain of struct nameseq's in reverse order and return that chain.
1460 The string is passed as STRINGP, the address of a string pointer.
1461 The string pointer is updated to point at the first character
1462 not parsed, which either is a null char or equals STOPCHAR.
1464 SIZE is how big to construct chain elements.
1465 This is useful if we want them actually to be other structures
1466 that have room for additional info.
1468 If STRIP is nonzero, strip `./'s off the beginning. */
1470 struct nameseq *
1471 parse_file_seq (stringp, stopchar, size, strip)
1472 char **stringp;
1473 char stopchar;
1474 unsigned int size;
1475 int strip;
1477 register struct nameseq *new = 0;
1478 register struct nameseq *new1;
1479 register char *p = *stringp;
1480 char *q;
1481 char *name;
1482 register int c;
1484 while (1)
1486 /* Skip whitespace; see if any more names are left. */
1487 p = next_token (p);
1488 if (*p == '\0')
1489 break;
1490 if (*p == stopchar)
1491 break;
1492 /* Yes, find end of next name. */
1493 q = p;
1494 while (1)
1496 c = *p++;
1497 if (c == '\0')
1498 break;
1499 else if (c == '\\' &&
1500 (*p == '\\' || isblank (*p) || *p == stopchar))
1501 ++p;
1502 else if (isblank (c) || c == stopchar)
1503 break;
1505 p--;
1507 if (strip)
1508 /* Skip leading `./'s. */
1509 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1511 q += 2; /* Skip "./". */
1512 while (q < p && *q == '/')
1513 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1514 ++q;
1517 /* Extract the filename just found, and skip it. */
1519 if (q == p)
1520 /* ".///" was stripped to "". */
1521 name = savestring ("./", 2);
1522 else
1523 name = savestring (q, p - q);
1525 /* Add it to the front of the chain. */
1526 new1 = (struct nameseq *) xmalloc (size);
1527 new1->name = name;
1528 new1->next = new;
1529 new = new1;
1532 #ifndef NO_ARCHIVES
1534 /* Look for multi-word archive references.
1535 They are indicated by a elt ending with an unmatched `)' and
1536 an elt further down the chain (i.e., previous in the file list)
1537 with an unmatched `(' (e.g., "lib(mem"). */
1539 for (new1 = new; new1 != 0; new1 = new1->next)
1540 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1541 && new1->name[strlen (new1->name) - 1] == ')'
1542 && index (new1->name, '(') == 0)
1544 /* NEW1 ends with a `)' but does not contain a `('.
1545 Look back for an elt with an opening `(' but no closing `)'. */
1547 struct nameseq *n = new1->next, *lastn = new1;
1548 char *paren;
1549 while (n != 0 && (paren = index (n->name, '(')) == 0)
1551 lastn = n;
1552 n = n->next;
1554 if (n != 0
1555 /* Ignore something starting with `(', as that cannot actually
1556 be an archive-member reference (and treating it as such
1557 results in an empty file name, which causes much lossage). */
1558 && n->name[0] != '(')
1560 /* N is the first element in the archive group.
1561 Its name looks like "lib(mem" (with no closing `)'). */
1563 char *libname;
1565 /* Copy "lib(" into LIBNAME. */
1566 ++paren;
1567 libname = (char *) alloca (paren - n->name + 1);
1568 bcopy (n->name, libname, paren - n->name);
1569 libname[paren - n->name] = '\0';
1571 if (*paren == '\0')
1573 /* N was just "lib(", part of something like "lib( a b)".
1574 Edit it out of the chain and free its storage. */
1575 lastn->next = n->next;
1576 free (n->name);
1577 free ((char *) n);
1578 /* LASTN->next is the new stopping elt for the loop below. */
1579 n = lastn->next;
1581 else
1583 /* Replace N's name with the full archive reference. */
1584 name = concat (libname, paren, ")");
1585 free (n->name);
1586 n->name = name;
1589 if (new1->name[1] == '\0')
1591 /* NEW1 is just ")", part of something like "lib(a b )".
1592 Omit it from the chain and free its storage. */
1593 lastn = new1;
1594 new1 = new1->next;
1595 if (new == lastn)
1596 new = new1;
1597 free (lastn->name);
1598 free ((char *) lastn);
1600 else
1602 /* Replace also NEW1->name, which already has closing `)'. */
1603 name = concat (libname, new1->name, "");
1604 free (new1->name);
1605 new1->name = name;
1606 new1 = new1->next;
1609 /* Trace back from NEW1 (the end of the list) until N
1610 (the beginning of the list), rewriting each name
1611 with the full archive reference. */
1613 while (new1 != n)
1615 name = concat (libname, new1->name, ")");
1616 free (new1->name);
1617 new1->name = name;
1618 new1 = new1->next;
1623 #endif
1625 *stringp = p;
1626 return new;
1629 /* Read a line of text from STREAM into LINEBUFFER.
1630 Combine continuation lines into one line.
1631 Return the number of actual lines read (> 1 if hacked continuation lines).
1634 static unsigned int
1635 readline (linebuffer, stream, filename, lineno)
1636 struct linebuffer *linebuffer;
1637 FILE *stream;
1638 char *filename;
1639 unsigned int lineno;
1641 char *buffer = linebuffer->buffer;
1642 register char *p = linebuffer->buffer;
1643 register char *end = p + linebuffer->size;
1644 register int len, lastlen = 0;
1645 register char *p2;
1646 register unsigned int nlines = 0;
1647 register int backslash;
1649 *p = '\0';
1651 while (fgets (p, end - p, stream) != 0)
1653 len = strlen (p);
1654 if (len == 0)
1656 /* This only happens when the first thing on the line is a '\0'.
1657 It is a pretty hopeless case, but (wonder of wonders) Athena
1658 lossage strikes again! (xmkmf puts NULs in its makefiles.)
1659 There is nothing really to be done; we synthesize a newline so
1660 the following line doesn't appear to be part of this line. */
1661 makefile_error (filename, lineno,
1662 "warning: NUL character seen; rest of line ignored");
1663 p[0] = '\n';
1664 len = 1;
1667 p += len;
1668 if (p[-1] != '\n')
1670 /* Probably ran out of buffer space. */
1671 register unsigned int p_off = p - buffer;
1672 linebuffer->size *= 2;
1673 buffer = (char *) xrealloc (buffer, linebuffer->size);
1674 p = buffer + p_off;
1675 end = buffer + linebuffer->size;
1676 linebuffer->buffer = buffer;
1677 *p = '\0';
1678 lastlen = len;
1679 continue;
1682 ++nlines;
1684 if (len == 1 && p > buffer)
1685 /* P is pointing at a newline and it's the beginning of
1686 the buffer returned by the last fgets call. However,
1687 it is not necessarily the beginning of a line if P is
1688 pointing past the beginning of the holding buffer.
1689 If the buffer was just enlarged (right before the newline),
1690 we must account for that, so we pretend that the two lines
1691 were one line. */
1692 len += lastlen;
1693 lastlen = len;
1694 backslash = 0;
1695 for (p2 = p - 2; --len > 0; --p2)
1697 if (*p2 == '\\')
1698 backslash = !backslash;
1699 else
1700 break;
1703 if (!backslash)
1705 p[-1] = '\0';
1706 break;
1709 if (end - p <= 1)
1711 /* Enlarge the buffer. */
1712 register unsigned int p_off = p - buffer;
1713 linebuffer->size *= 2;
1714 buffer = (char *) xrealloc (buffer, linebuffer->size);
1715 p = buffer + p_off;
1716 end = buffer + linebuffer->size;
1717 linebuffer->buffer = buffer;
1721 if (ferror (stream))
1722 pfatal_with_name (filename);
1724 return nlines;
1727 /* Construct the list of include directories
1728 from the arguments and the default list. */
1730 void
1731 construct_include_path (arg_dirs)
1732 char **arg_dirs;
1734 register unsigned int i;
1735 struct stat stbuf;
1737 /* Table to hold the dirs. */
1739 register unsigned int defsize = (sizeof (default_include_directories)
1740 / sizeof (default_include_directories[0]));
1741 register unsigned int max = 5;
1742 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
1743 register unsigned int idx = 0;
1745 /* First consider any dirs specified with -I switches.
1746 Ignore dirs that don't exist. */
1748 if (arg_dirs != 0)
1749 while (*arg_dirs != 0)
1751 char *dir = *arg_dirs++;
1753 if (dir[0] == '~')
1755 char *expanded = tilde_expand (dir);
1756 if (expanded != 0)
1757 dir = expanded;
1760 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
1762 if (idx == max - 1)
1764 max += 5;
1765 dirs = (char **)
1766 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
1768 dirs[idx++] = dir;
1770 else if (dir != arg_dirs[-1])
1771 free (dir);
1774 /* Now add at the end the standard default dirs. */
1776 for (i = 0; default_include_directories[i] != 0; ++i)
1777 if (stat (default_include_directories[i], &stbuf) == 0
1778 && S_ISDIR (stbuf.st_mode))
1779 dirs[idx++] = default_include_directories[i];
1781 dirs[idx] = 0;
1783 /* Now compute the maximum length of any name in it. */
1785 max_incl_len = 0;
1786 for (i = 0; i < idx; ++i)
1788 unsigned int len = strlen (dirs[i]);
1789 /* If dir name is written with a trailing slash, discard it. */
1790 if (dirs[i][len - 1] == '/')
1791 /* We can't just clobber a null in because it may have come from
1792 a literal string and literal strings may not be writable. */
1793 dirs[i] = savestring (dirs[i], len - 1);
1794 if (len > max_incl_len)
1795 max_incl_len = len;
1798 include_directories = dirs;
1801 /* Expand ~ or ~USER at the beginning of NAME.
1802 Return a newly malloc'd string or 0. */
1804 char *
1805 tilde_expand (name)
1806 char *name;
1808 if (name[1] == '/' || name[1] == '\0')
1810 extern char *getenv ();
1811 char *home_dir;
1812 int is_variable;
1815 /* Turn off --warn-undefined-variables while we expand HOME. */
1816 int save = warn_undefined_variables_flag;
1817 warn_undefined_variables_flag = 0;
1819 home_dir = allocated_variable_expand ("$(HOME)");
1821 warn_undefined_variables_flag = save;
1824 is_variable = home_dir[0] != '\0';
1825 if (!is_variable)
1827 free (home_dir);
1828 home_dir = getenv ("HOME");
1830 if (home_dir == 0 || home_dir[0] == '\0')
1832 extern char *getlogin ();
1833 char *name = getlogin ();
1834 home_dir = 0;
1835 if (name != 0)
1837 struct passwd *p = getpwnam (name);
1838 if (p != 0)
1839 home_dir = p->pw_dir;
1842 if (home_dir != 0)
1844 char *new = concat (home_dir, "", name + 1);
1845 if (is_variable)
1846 free (home_dir);
1847 return new;
1850 else
1852 struct passwd *pwent;
1853 char *userend = index (name + 1, '/');
1854 if (userend != 0)
1855 *userend = '\0';
1856 pwent = getpwnam (name + 1);
1857 if (pwent != 0)
1859 if (userend == 0)
1860 return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
1861 else
1862 return concat (pwent->pw_dir, "/", userend + 1);
1864 else if (userend != 0)
1865 *userend = '/';
1868 return 0;
1871 /* Given a chain of struct nameseq's describing a sequence of filenames,
1872 in reverse of the intended order, return a new chain describing the
1873 result of globbing the filenames. The new chain is in forward order.
1874 The links of the old chain are freed or used in the new chain.
1875 Likewise for the names in the old chain.
1877 SIZE is how big to construct chain elements.
1878 This is useful if we want them actually to be other structures
1879 that have room for additional info. */
1881 struct nameseq *
1882 multi_glob (chain, size)
1883 struct nameseq *chain;
1884 unsigned int size;
1886 register struct nameseq *new = 0;
1887 register struct nameseq *old;
1888 struct nameseq *nexto;
1890 for (old = chain; old != 0; old = nexto)
1892 glob_t gl;
1893 #ifndef NO_ARCHIVES
1894 char *memname;
1895 #endif
1897 nexto = old->next;
1899 if (old->name[0] == '~')
1901 char *newname = tilde_expand (old->name);
1902 if (newname != 0)
1904 free (old->name);
1905 old->name = newname;
1909 #ifndef NO_ARCHIVES
1910 if (ar_name (old->name))
1912 /* OLD->name is an archive member reference.
1913 Replace it with the archive file name,
1914 and save the member name in MEMNAME.
1915 We will glob on the archive name and then
1916 reattach MEMNAME later. */
1917 char *arname;
1918 ar_parse_name (old->name, &arname, &memname);
1919 free (old->name);
1920 old->name = arname;
1922 else
1923 memname = 0;
1924 #endif
1926 switch (glob (old->name, GLOB_NOCHECK, NULL, &gl))
1928 case 0: /* Success. */
1930 register int i = gl.gl_pathc;
1931 while (i-- > 0)
1933 #ifndef NO_ARCHIVES
1934 if (memname != 0)
1936 /* Try to glob on MEMNAME within the archive. */
1937 struct nameseq *found
1938 = ar_glob (gl.gl_pathv[i], memname, size);
1939 if (found == 0)
1941 /* No matches. Use MEMNAME as-is. */
1942 struct nameseq *elt
1943 = (struct nameseq *) xmalloc (size);
1944 unsigned int alen = strlen (gl.gl_pathv[i]);
1945 unsigned int mlen = strlen (memname);
1946 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
1947 bcopy (gl.gl_pathv[i], elt->name, alen);
1948 elt->name[alen] = '(';
1949 bcopy (memname, &elt->name[alen + 1], mlen);
1950 elt->name[alen + 1 + mlen] = ')';
1951 elt->name[alen + 1 + mlen + 1] = '\0';
1952 elt->next = new;
1953 new = elt;
1955 else
1957 /* Find the end of the FOUND chain. */
1958 struct nameseq *f = found;
1959 while (f->next != 0)
1960 f = f->next;
1962 /* Attach the chain being built to the end of the FOUND
1963 chain, and make FOUND the new NEW chain. */
1964 f->next = new;
1965 new = found;
1968 free (memname);
1970 else
1971 #endif
1973 struct nameseq *elt = (struct nameseq *) xmalloc (size);
1974 elt->name = savestring (gl.gl_pathv[i],
1975 strlen (gl.gl_pathv[i]));
1976 elt->next = new;
1977 new = elt;
1980 globfree (&gl);
1981 free (old->name);
1982 free (old);
1983 break;
1986 case GLOB_NOSPACE:
1987 fatal ("virtual memory exhausted");
1988 break;
1990 default:
1991 old->next = new;
1992 new = old;
1993 break;
1997 return new;