Added rcsid.
[make.git] / read.c
blobfdb8be2f7c1429ff80c64c876549550914e38f9a
1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94 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 if (debug_flag)
272 printf ("Reading makefile `%s'", filename);
273 if (flags & RM_NO_DEFAULT_GOAL)
274 printf (" (no default goal)");
275 if (flags & RM_INCLUDED)
276 printf (" (search path)");
277 if (flags & RM_DONTCARE)
278 printf (" (don't care)");
279 if (flags & RM_NO_TILDE)
280 printf (" (no ~ expansion)");
281 puts ("...");
284 /* First, get a stream to read. */
286 /* Expand ~ in FILENAME unless it came from `include',
287 in which case it was already done. */
288 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
290 char *expanded = tilde_expand (filename);
291 /* This is a possible memory leak, but I don't care. */
292 if (expanded != 0)
293 filename = expanded;
296 infile = fopen (filename, "r");
297 /* Save the error code so we print the right message later. */
298 makefile_errno = errno;
300 /* If the makefile wasn't found and it's either a makefile from
301 the `MAKEFILES' variable or an included makefile,
302 search the included makefile search path for this makefile. */
304 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
306 register unsigned int i;
307 for (i = 0; include_directories[i] != 0; ++i)
309 char *name = concat (include_directories[i], "/", filename);
310 infile = fopen (name, "r");
311 if (infile == 0)
312 free (name);
313 else
315 filename = name;
316 break;
321 /* Add FILENAME to the chain of read makefiles. */
322 deps = (struct dep *) xmalloc (sizeof (struct dep));
323 deps->next = read_makefiles;
324 read_makefiles = deps;
325 deps->name = 0;
326 deps->file = lookup_file (filename);
327 if (deps->file == 0)
329 deps->file = enter_file (savestring (filename, strlen (filename)));
330 if (flags & RM_DONTCARE)
331 deps->file->dontcare = 1;
333 filename = deps->file->name;
334 deps->file->precious = 1;
335 deps->changed = flags;
336 deps = 0;
338 /* If the makefile can't be found at all, give up entirely. */
340 if (infile == 0)
342 /* If we did some searching, errno has the error from the last
343 attempt, rather from FILENAME itself. Restore it in case the
344 caller wants to use it in a message. */
345 errno = makefile_errno;
346 return 0;
349 reading_filename = filename;
350 reading_lineno_ptr = &lineno;
352 /* Loop over lines in the file.
353 The strategy is to accumulate target names in FILENAMES, dependencies
354 in DEPS and commands in COMMANDS. These are used to define a rule
355 when the start of the next rule (or eof) is encountered. */
357 initbuffer (&lb);
359 while (!feof (infile))
361 lineno += nlines;
362 nlines = readline (&lb, infile, filename, lineno);
364 if (collapsed_length < lb.size)
366 collapsed_length = lb.size;
367 if (collapsed != 0)
368 free (collapsed);
369 collapsed = (char *) xmalloc (collapsed_length);
371 strcpy (collapsed, lb.buffer);
372 /* Collapse continuation lines. */
373 collapse_continuations (collapsed);
374 remove_comments (collapsed);
376 p = collapsed;
377 while (isspace (*p) && *p != '\t')
378 ++p;
379 /* We cannot consider a line containing just a tab to be empty
380 because it might constitute an empty command for a target. */
381 if (*p == '\0' && lb.buffer[0] != '\t')
382 continue;
384 /* strncmp is first to avoid dereferencing out into space. */
385 #define word1eq(s, l) (!strncmp (s, p, l) \
386 && (p[l] == '\0' || isblank (p[l])))
387 if (!in_ignored_define
388 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
389 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
390 || word1eq ("else", 4) || word1eq ("endif", 5)))
392 int i = conditional_line (p, filename, lineno);
393 if (i >= 0)
394 ignoring = i;
395 else
396 makefile_fatal (filename, lineno,
397 "invalid syntax in conditional");
398 continue;
400 else if (word1eq ("endef", 5))
402 if (in_ignored_define)
403 in_ignored_define = 0;
404 else
405 makefile_fatal (filename, lineno, "extraneous `endef'");
406 continue;
408 else if (word1eq ("define", 6))
410 if (ignoring)
411 in_ignored_define = 1;
412 else
414 p2 = next_token (p + 6);
415 /* Let the variable name be the whole rest of the line,
416 with trailing blanks stripped (comments have already been
417 removed), so it could be a complex variable/function
418 reference that might contain blanks. */
419 p = index (p2, '\0');
420 while (isblank (p[-1]))
421 --p;
422 lineno = do_define (p2, p - p2, o_file,
423 lineno, infile, filename);
425 continue;
427 else if (word1eq ("override", 8))
429 p2 = next_token (p + 8);
430 if (p2 == 0)
431 makefile_error (filename, lineno, "empty `override' directive");
432 if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
434 if (ignoring)
435 in_ignored_define = 1;
436 else
438 unsigned int len;
439 p2 = end_of_token (p2);
440 /* Let the variable name be the whole rest of the line,
441 with trailing blanks stripped (comments have already been
442 removed), so it could be a complex variable/function
443 reference that might contain blanks. */
444 p = index (p2, '\0');
445 while (isblank (p[-1]))
446 --p;
447 lineno = do_define (p2, p - p2, o_override,
448 lineno, infile, filename);
451 else if (!ignoring
452 && !try_variable_definition (filename, lineno,
453 p2, o_override))
454 makefile_error (filename, lineno, "empty `override' directive");
456 continue;
459 if (ignoring)
460 /* Ignore the line. We continue here so conditionals
461 can appear in the middle of a rule. */
462 continue;
463 else if (lb.buffer[0] == '\t')
465 /* This line is a shell command. */
466 unsigned int len;
468 if (no_targets)
469 /* Ignore the commands in a rule with no targets. */
470 continue;
472 /* If there is no preceding rule line, don't treat this line
473 as a command, even though it begins with a tab character.
474 SunOS 4 make appears to behave this way. */
476 if (filenames != 0)
478 /* Append this command line to the line being accumulated. */
479 p = lb.buffer;
480 if (commands_idx == 0)
481 commands_started = lineno;
482 len = strlen (p);
483 if (len + 1 + commands_idx > commands_len)
485 commands_len = (len + 1 + commands_idx) * 2;
486 commands = (char *) xrealloc (commands, commands_len);
488 bcopy (p, &commands[commands_idx], len);
489 commands_idx += len;
490 commands[commands_idx++] = '\n';
492 continue;
496 if (word1eq ("export", 6))
498 struct variable *v;
499 p2 = next_token (p + 6);
500 if (*p2 == '\0')
501 export_all_variables = 1;
502 v = try_variable_definition (filename, lineno, p2, o_file);
503 if (v != 0)
504 v->export = v_export;
505 else
507 unsigned int len;
508 for (p = find_next_token (&p2, &len); p != 0;
509 p = find_next_token (&p2, &len))
511 v = lookup_variable (p, len);
512 if (v == 0)
513 v = define_variable (p, len, "", o_file, 0);
514 v->export = v_export;
518 else if (word1eq ("unexport", 8))
520 unsigned int len;
521 struct variable *v;
522 p2 = next_token (p + 8);
523 if (*p2 == '\0')
524 export_all_variables = 0;
525 for (p = find_next_token (&p2, &len); p != 0;
526 p = find_next_token (&p2, &len))
528 v = lookup_variable (p, len);
529 if (v == 0)
530 v = define_variable (p, len, "", o_file, 0);
531 v->export = v_noexport;
534 else if (word1eq ("include", 7) || word1eq ("-include", 8))
536 /* We have found an `include' line specifying a nested
537 makefile to be read at this point. */
538 struct conditionals *save, new_conditionals;
539 struct nameseq *files;
540 /* "-include" (vs "include") says no
541 error if the file does not exist. */
542 int noerror = p[0] == '-';
544 p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
545 if (*p == '\0')
547 makefile_error (filename, lineno,
548 "no file name for `%sinclude'",
549 noerror ? "-" : "");
550 continue;
553 /* Parse the list of file names. */
554 p2 = p;
555 files = multi_glob (parse_file_seq (&p2, '\0',
556 sizeof (struct nameseq),
558 sizeof (struct nameseq));
559 free (p);
561 /* Save the state of conditionals and start
562 the included makefile with a clean slate. */
563 save = conditionals;
564 bzero ((char *) &new_conditionals, sizeof new_conditionals);
565 conditionals = &new_conditionals;
567 /* Record the rules that are waiting so they will determine
568 the default goal before those in the included makefile. */
569 record_waiting_files ();
571 /* Read each included makefile. */
572 while (files != 0)
574 struct nameseq *next = files->next;
575 char *name = files->name;
576 free (files);
577 files = next;
579 if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
580 | (noerror ? RM_DONTCARE : 0))))
581 makefile_error (filename, lineno,
582 "%s: %s", name, strerror (errno));
585 /* Free any space allocated by conditional_line. */
586 if (conditionals->ignoring)
587 free (conditionals->ignoring);
588 if (conditionals->seen_else)
589 free (conditionals->seen_else);
591 /* Restore state. */
592 conditionals = save;
593 reading_filename = filename;
594 reading_lineno_ptr = &lineno;
596 else if (word1eq ("vpath", 5))
598 char *pattern;
599 unsigned int len;
600 p2 = variable_expand (p + 5);
601 p = find_next_token (&p2, &len);
602 if (p != 0)
604 pattern = savestring (p, len);
605 p = find_next_token (&p2, &len);
606 /* No searchpath means remove all previous
607 selective VPATH's with the same pattern. */
609 else
610 /* No pattern means remove all previous selective VPATH's. */
611 pattern = 0;
612 construct_vpath_list (pattern, p);
613 if (pattern != 0)
614 free (pattern);
616 #undef word1eq
617 else if (try_variable_definition (filename, lineno, p, o_file))
618 /* This line has been dealt with. */
620 else if (lb.buffer[0] == '\t')
622 p = lb.buffer;
623 while (isblank (*p))
624 ++p;
625 if (*p == '\0')
626 /* The line is completely blank; that is harmless. */
627 continue;
628 /* This line starts with a tab but was not caught above
629 because there was no preceding target, and the line
630 might have been usable as a variable definition.
631 But now it is definitely lossage. */
632 makefile_fatal (filename, lineno,
633 "commands commence before first target");
635 else
637 /* This line describes some target files. */
639 char *cmdleft;
641 /* Record the previous rule. */
643 record_waiting_files ();
645 /* Look for a semicolon in the unexpanded line. */
646 cmdleft = find_semicolon (lb.buffer);
647 if (cmdleft != 0)
648 /* Found one. Cut the line short there before expanding it. */
649 *cmdleft = '\0';
651 collapse_continuations (lb.buffer);
653 /* Expand variable and function references before doing anything
654 else so that special characters can be inside variables. */
655 p = variable_expand (lb.buffer);
657 if (cmdleft == 0)
658 /* Look for a semicolon in the expanded line. */
659 cmdleft = find_semicolon (p);
661 if (cmdleft != 0)
662 /* Cut the line short at the semicolon. */
663 *cmdleft = '\0';
665 /* Remove comments from the line. */
666 remove_comments (p);
668 p2 = next_token (p);
669 if (*p2 == '\0')
671 if (cmdleft != 0)
672 makefile_fatal (filename, lineno,
673 "missing rule before commands");
674 else
675 /* This line contained a variable reference that
676 expanded to nothing but whitespace. */
677 continue;
679 else if (*p2 == ':')
681 /* We accept and ignore rules without targets for
682 compatibility with SunOS 4 make. */
683 no_targets = 1;
684 continue;
687 filenames = multi_glob (parse_file_seq (&p2, ':',
688 sizeof (struct nameseq),
690 sizeof (struct nameseq));
691 if (*p2++ == '\0')
692 makefile_fatal (filename, lineno, "missing separator");
693 /* Is this a one-colon or two-colon entry? */
694 two_colon = *p2 == ':';
695 if (two_colon)
696 p2++;
698 /* We have some targets, so don't ignore the following commands. */
699 no_targets = 0;
701 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
702 p = index (p2, ':');
703 while (p != 0 && p[-1] == '\\')
705 register char *q = &p[-1];
706 register int backslash = 0;
707 while (*q-- == '\\')
708 backslash = !backslash;
709 if (backslash)
710 p = index (p + 1, ':');
711 else
712 break;
714 if (p != 0)
716 struct nameseq *target;
717 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
718 ++p2;
719 if (target == 0)
720 makefile_fatal (filename, lineno, "missing target pattern");
721 else if (target->next != 0)
722 makefile_fatal (filename, lineno, "multiple target patterns");
723 pattern = target->name;
724 pattern_percent = find_percent (pattern);
725 if (pattern_percent == 0)
726 makefile_fatal (filename, lineno,
727 "target pattern contains no `%%'");
729 else
730 pattern = 0;
732 /* Parse the dependencies. */
733 deps = (struct dep *)
734 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
735 sizeof (struct dep));
737 commands_idx = 0;
738 if (cmdleft != 0)
740 /* Semicolon means rest of line is a command. */
741 unsigned int len = strlen (cmdleft + 1);
743 commands_started = lineno;
745 /* Add this command line to the buffer. */
746 if (len + 2 > commands_len)
748 commands_len = (len + 2) * 2;
749 commands = (char *) xrealloc (commands, commands_len);
751 bcopy (cmdleft + 1, commands, len);
752 commands_idx += len;
753 commands[commands_idx++] = '\n';
756 continue;
759 /* We get here except in the case that we just read a rule line.
760 Record now the last rule we read, so following spurious
761 commands are properly diagnosed. */
762 record_waiting_files ();
763 no_targets = 0;
766 if (conditionals->if_cmds)
767 makefile_fatal (filename, lineno, "missing `endif'");
769 /* At eof, record the last rule. */
770 record_waiting_files ();
772 freebuffer (&lb);
773 free ((char *) commands);
774 fclose (infile);
776 reading_filename = 0;
777 reading_lineno_ptr = 0;
779 return 1;
782 /* Execute a `define' directive.
783 The first line has already been read, and NAME is the name of
784 the variable to be defined. The following lines remain to be read.
785 LINENO, INFILE and FILENAME refer to the makefile being read.
786 The value returned is LINENO, updated for lines read here. */
788 static unsigned int
789 do_define (name, namelen, origin, lineno, infile, filename)
790 char *name;
791 unsigned int namelen;
792 enum variable_origin origin;
793 unsigned int lineno;
794 FILE *infile;
795 char *filename;
797 struct linebuffer lb;
798 unsigned int nlines = 0;
799 unsigned int length = 100;
800 char *definition = (char *) xmalloc (100);
801 register unsigned int idx = 0;
802 register char *p;
804 /* Expand the variable name. */
805 char *var = (char *) alloca (namelen + 1);
806 bcopy (name, var, namelen);
807 var[namelen] = '\0';
808 var = variable_expand (var);
810 initbuffer (&lb);
811 while (!feof (infile))
813 lineno += nlines;
814 nlines = readline (&lb, infile, filename, lineno);
815 p = next_token (lb.buffer);
817 if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
819 p += 5;
820 collapse_continuations (p);
821 remove_comments (p);
822 if (*next_token (p) != '\0')
823 makefile_error (filename, lineno,
824 "Extraneous text after `endef' directive");
825 /* Define the variable. */
826 if (idx == 0)
827 definition[0] = '\0';
828 else
829 definition[idx - 1] = '\0';
830 (void) define_variable (var, strlen (var), definition, origin, 1);
831 free (definition);
832 freebuffer (&lb);
833 return lineno;
835 else
837 unsigned int len = strlen (p);
839 /* Increase the buffer size if necessary. */
840 if (idx + len + 1 > length)
842 length = (idx + len) * 2;
843 definition = (char *) xrealloc (definition, length + 1);
846 bcopy (p, &definition[idx], len);
847 idx += len;
848 /* Separate lines with a newline. */
849 definition[idx++] = '\n';
853 /* No `endef'!! */
854 makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
856 /* NOTREACHED */
857 return 0;
860 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
861 "ifneq", "else" and "endif".
862 LINE is the input line, with the command as its first word.
864 FILENAME and LINENO are the filename and line number in the
865 current makefile. They are used for error messages.
867 Value is -1 if the line is invalid,
868 0 if following text should be interpreted,
869 1 if following text should be ignored. */
871 static int
872 conditional_line (line, filename, lineno)
873 char *line;
874 char *filename;
875 unsigned int lineno;
877 int notdef;
878 char *cmdname;
879 register unsigned int i;
881 if (*line == 'i')
883 /* It's an "if..." command. */
884 notdef = line[2] == 'n';
885 if (notdef)
887 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
888 line += cmdname[3] == 'd' ? 7 : 6;
890 else
892 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
893 line += cmdname[2] == 'd' ? 6 : 5;
896 else
898 /* It's an "else" or "endif" command. */
899 notdef = line[1] == 'n';
900 cmdname = notdef ? "endif" : "else";
901 line += notdef ? 5 : 4;
904 line = next_token (line);
906 if (*cmdname == 'e')
908 if (*line != '\0')
909 makefile_error (filename, lineno,
910 "Extraneous text after `%s' directive",
911 cmdname);
912 /* "Else" or "endif". */
913 if (conditionals->if_cmds == 0)
914 makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
915 /* NOTDEF indicates an `endif' command. */
916 if (notdef)
917 --conditionals->if_cmds;
918 else if (conditionals->seen_else[conditionals->if_cmds - 1])
919 makefile_fatal (filename, lineno, "only one `else' per conditional");
920 else
922 /* Toggle the state of ignorance. */
923 conditionals->ignoring[conditionals->if_cmds - 1]
924 = !conditionals->ignoring[conditionals->if_cmds - 1];
925 /* Record that we have seen an `else' in this conditional.
926 A second `else' will be erroneous. */
927 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
929 for (i = 0; i < conditionals->if_cmds; ++i)
930 if (conditionals->ignoring[i])
931 return 1;
932 return 0;
935 if (conditionals->allocated == 0)
937 conditionals->allocated = 5;
938 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
939 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
942 ++conditionals->if_cmds;
943 if (conditionals->if_cmds > conditionals->allocated)
945 conditionals->allocated += 5;
946 conditionals->ignoring = (char *)
947 xrealloc (conditionals->ignoring, conditionals->allocated);
948 conditionals->seen_else = (char *)
949 xrealloc (conditionals->seen_else, conditionals->allocated);
952 /* Record that we have seen an `if...' but no `else' so far. */
953 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
955 /* Search through the stack to see if we're already ignoring. */
956 for (i = 0; i < conditionals->if_cmds - 1; ++i)
957 if (conditionals->ignoring[i])
959 /* We are already ignoring, so just push a level
960 to match the next "else" or "endif", and keep ignoring.
961 We don't want to expand variables in the condition. */
962 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
963 return 1;
966 if (cmdname[notdef ? 3 : 2] == 'd')
968 /* "Ifdef" or "ifndef". */
969 struct variable *v;
970 register char *p = end_of_token (line);
971 i = p - line;
972 p = next_token (p);
973 if (*p != '\0')
974 return -1;
975 v = lookup_variable (line, i);
976 conditionals->ignoring[conditionals->if_cmds - 1]
977 = (v != 0 && *v->value != '\0') == notdef;
979 else
981 /* "Ifeq" or "ifneq". */
982 char *s1, *s2;
983 unsigned int len;
984 char termin = *line == '(' ? ',' : *line;
986 if (termin != ',' && termin != '"' && termin != '\'')
987 return -1;
989 s1 = ++line;
990 /* Find the end of the first string. */
991 if (termin == ',')
993 register int count = 0;
994 for (; *line != '\0'; ++line)
995 if (*line == '(')
996 ++count;
997 else if (*line == ')')
998 --count;
999 else if (*line == ',' && count <= 0)
1000 break;
1002 else
1003 while (*line != '\0' && *line != termin)
1004 ++line;
1006 if (*line == '\0')
1007 return -1;
1009 *line++ = '\0';
1011 s2 = variable_expand (s1);
1012 /* We must allocate a new copy of the expanded string because
1013 variable_expand re-uses the same buffer. */
1014 len = strlen (s2);
1015 s1 = (char *) alloca (len + 1);
1016 bcopy (s2, s1, len + 1);
1018 if (termin != ',')
1019 /* Find the start of the second string. */
1020 line = next_token (line);
1022 termin = termin == ',' ? ')' : *line;
1023 if (termin != ')' && termin != '"' && termin != '\'')
1024 return -1;
1026 /* Find the end of the second string. */
1027 if (termin == ')')
1029 register int count = 0;
1030 s2 = next_token (line);
1031 for (line = s2; *line != '\0'; ++line)
1033 if (*line == '(')
1034 ++count;
1035 else if (*line == ')')
1036 if (count <= 0)
1037 break;
1038 else
1039 --count;
1042 else
1044 ++line;
1045 s2 = line;
1046 while (*line != '\0' && *line != termin)
1047 ++line;
1050 if (*line == '\0')
1051 return -1;
1053 *line = '\0';
1054 line = next_token (++line);
1055 if (*line != '\0')
1056 makefile_error (filename, lineno,
1057 "Extraneous text after `%s' directive",
1058 cmdname);
1060 s2 = variable_expand (s2);
1061 conditionals->ignoring[conditionals->if_cmds - 1]
1062 = streq (s1, s2) == notdef;
1065 /* Search through the stack to see if we're ignoring. */
1066 for (i = 0; i < conditionals->if_cmds; ++i)
1067 if (conditionals->ignoring[i])
1068 return 1;
1069 return 0;
1072 /* Remove duplicate dependencies in CHAIN. */
1074 void
1075 uniquize_deps (chain)
1076 struct dep *chain;
1078 register struct dep *d;
1080 /* Make sure that no dependencies are repeated. This does not
1081 really matter for the purpose of updating targets, but it
1082 might make some names be listed twice for $^ and $?. */
1084 for (d = chain; d != 0; d = d->next)
1086 struct dep *last, *next;
1088 last = d;
1089 next = d->next;
1090 while (next != 0)
1091 if (streq (dep_name (d), dep_name (next)))
1093 struct dep *n = next->next;
1094 last->next = n;
1095 if (next->name != 0 && next->name != d->name)
1096 free (next->name);
1097 if (next != d)
1098 free ((char *) next);
1099 next = n;
1101 else
1103 last = next;
1104 next = next->next;
1109 /* Record a description line for files FILENAMES,
1110 with dependencies DEPS, commands to execute described
1111 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1112 TWO_COLON is nonzero if a double colon was used.
1113 If not nil, PATTERN is the `%' pattern to make this
1114 a static pattern rule, and PATTERN_PERCENT is a pointer
1115 to the `%' within it.
1117 The links of FILENAMES are freed, and so are any names in it
1118 that are not incorporated into other data structures. */
1120 static void
1121 record_files (filenames, pattern, pattern_percent, deps, commands_started,
1122 commands, commands_idx, two_colon, filename, lineno, set_default)
1123 struct nameseq *filenames;
1124 char *pattern, *pattern_percent;
1125 struct dep *deps;
1126 unsigned int commands_started;
1127 char *commands;
1128 unsigned int commands_idx;
1129 int two_colon;
1130 char *filename;
1131 unsigned int lineno;
1132 int set_default;
1134 struct nameseq *nextf;
1135 int implicit = 0;
1136 unsigned int max_targets, target_idx;
1137 char **targets = 0, **target_percents = 0;
1138 struct commands *cmds;
1140 if (commands_idx > 0)
1142 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1143 cmds->filename = filename;
1144 cmds->lineno = commands_started;
1145 cmds->commands = savestring (commands, commands_idx);
1146 cmds->command_lines = 0;
1148 else
1149 cmds = 0;
1151 for (; filenames != 0; filenames = nextf)
1153 register char *name = filenames->name;
1154 register struct file *f;
1155 register struct dep *d;
1156 struct dep *this;
1157 char *implicit_percent;
1159 nextf = filenames->next;
1160 free ((char *) filenames);
1162 implicit_percent = find_percent (name);
1163 implicit |= implicit_percent != 0;
1165 if (implicit && pattern != 0)
1166 makefile_fatal (filename, lineno,
1167 "mixed implicit and static pattern rules");
1169 if (implicit && implicit_percent == 0)
1170 makefile_fatal (filename, lineno, "mixed implicit and normal rules");
1172 if (implicit)
1174 if (targets == 0)
1176 max_targets = 5;
1177 targets = (char **) xmalloc (5 * sizeof (char *));
1178 target_percents = (char **) xmalloc (5 * sizeof (char *));
1179 target_idx = 0;
1181 else if (target_idx == max_targets - 1)
1183 max_targets += 5;
1184 targets = (char **) xrealloc ((char *) targets,
1185 max_targets * sizeof (char *));
1186 target_percents
1187 = (char **) xrealloc ((char *) target_percents,
1188 max_targets * sizeof (char *));
1190 targets[target_idx] = name;
1191 target_percents[target_idx] = implicit_percent;
1192 ++target_idx;
1193 continue;
1196 /* If there are multiple filenames, copy the chain DEPS
1197 for all but the last one. It is not safe for the same deps
1198 to go in more than one place in the data base. */
1199 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1201 if (pattern != 0)
1202 /* If this is an extended static rule:
1203 `targets: target%pattern: dep%pattern; cmds',
1204 translate each dependency pattern into a plain filename
1205 using the target pattern and this target's name. */
1206 if (!pattern_matches (pattern, pattern_percent, name))
1208 /* Give a warning if the rule is meaningless. */
1209 makefile_error (filename, lineno,
1210 "target `%s' doesn't match the target pattern",
1211 name);
1212 this = 0;
1214 else
1216 /* We use patsubst_expand to do the work of translating
1217 the target pattern, the target's name and the dependencies'
1218 patterns into plain dependency names. */
1219 char *buffer = variable_expand ("");
1221 for (d = this; d != 0; d = d->next)
1223 char *o;
1224 char *percent = find_percent (d->name);
1225 if (percent == 0)
1226 continue;
1227 o = patsubst_expand (buffer, name, pattern, d->name,
1228 pattern_percent, percent);
1229 free (d->name);
1230 d->name = savestring (buffer, o - buffer);
1234 if (!two_colon)
1236 /* Single-colon. Combine these dependencies
1237 with others in file's existing record, if any. */
1238 f = enter_file (name);
1240 if (f->double_colon)
1241 makefile_fatal (filename, lineno,
1242 "target file `%s' has both : and :: entries",
1243 f->name);
1245 /* If CMDS == F->CMDS, this target was listed in this rule
1246 more than once. Just give a warning since this is harmless. */
1247 if (cmds != 0 && cmds == f->cmds)
1248 makefile_error
1249 (filename, lineno,
1250 "target `%s' given more than once in the same rule.",
1251 f->name);
1253 /* Check for two single-colon entries both with commands.
1254 Check is_target so that we don't lose on files such as .c.o
1255 whose commands were preinitialized. */
1256 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1258 makefile_error (cmds->filename, cmds->lineno,
1259 "warning: overriding commands for target `%s'",
1260 f->name);
1261 makefile_error (f->cmds->filename, f->cmds->lineno,
1262 "warning: ignoring old commands for target `%s'",
1263 f->name);
1266 f->is_target = 1;
1268 /* Defining .DEFAULT with no deps or cmds clears it. */
1269 if (f == default_file && this == 0 && cmds == 0)
1270 f->cmds = 0;
1271 if (cmds != 0)
1272 f->cmds = cmds;
1273 /* Defining .SUFFIXES with no dependencies
1274 clears out the list of suffixes. */
1275 if (f == suffix_file && this == 0)
1277 d = f->deps;
1278 while (d != 0)
1280 struct dep *nextd = d->next;
1281 free (d->name);
1282 free (d);
1283 d = nextd;
1285 f->deps = 0;
1287 else if (f->deps != 0)
1289 /* Add the file's old deps and the new ones in THIS together. */
1291 struct dep *firstdeps, *moredeps;
1292 if (cmds != 0)
1294 /* This is the rule with commands, so put its deps first.
1295 The rationale behind this is that $< expands to the
1296 first dep in the chain, and commands use $< expecting
1297 to get the dep that rule specifies. */
1298 firstdeps = this;
1299 moredeps = f->deps;
1301 else
1303 /* Append the new deps to the old ones. */
1304 firstdeps = f->deps;
1305 moredeps = this;
1308 if (firstdeps == 0)
1309 firstdeps = moredeps;
1310 else
1312 d = firstdeps;
1313 while (d->next != 0)
1314 d = d->next;
1315 d->next = moredeps;
1318 f->deps = firstdeps;
1320 else
1321 f->deps = this;
1323 /* If this is a static pattern rule, set the file's stem to
1324 the part of its name that matched the `%' in the pattern,
1325 so you can use $* in the commands. */
1326 if (pattern != 0)
1328 static char *percent = "%";
1329 char *buffer = variable_expand ("");
1330 char *o = patsubst_expand (buffer, name, pattern, percent,
1331 pattern_percent, percent);
1332 f->stem = savestring (buffer, o - buffer);
1335 else
1337 /* Double-colon. Make a new record
1338 even if the file already has one. */
1339 f = lookup_file (name);
1340 /* Check for both : and :: rules. Check is_target so
1341 we don't lose on default suffix rules or makefiles. */
1342 if (f != 0 && f->is_target && !f->double_colon)
1343 makefile_fatal (filename, lineno,
1344 "target file `%s' has both : and :: entries",
1345 f->name);
1346 f = enter_file (name);
1347 /* If there was an existing entry and it was a
1348 double-colon entry, enter_file will have returned a
1349 new one, making it the prev pointer of the old one. */
1350 f->is_target = 1;
1351 f->double_colon = 1;
1352 f->deps = this;
1353 f->cmds = cmds;
1356 /* Free name if not needed further. */
1357 if (f != 0 && name != f->name
1358 && (name < f->name || name > f->name + strlen (f->name)))
1360 free (name);
1361 name = f->name;
1364 /* See if this is first target seen whose name does
1365 not start with a `.', unless it contains a slash. */
1366 if (default_goal_file == 0 && set_default
1367 && (*name != '.' || index (name, '/') != 0))
1369 int reject = 0;
1371 /* If this file is a suffix, don't
1372 let it be the default goal file. */
1374 for (d = suffix_file->deps; d != 0; d = d->next)
1376 register struct dep *d2;
1377 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1379 reject = 1;
1380 break;
1382 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1384 register unsigned int len = strlen (dep_name (d2));
1385 if (strncmp (name, dep_name (d2), len))
1386 continue;
1387 if (streq (name + len, dep_name (d)))
1389 reject = 1;
1390 break;
1393 if (reject)
1394 break;
1397 if (!reject)
1398 default_goal_file = f;
1402 if (implicit)
1404 targets[target_idx] = 0;
1405 target_percents[target_idx] = 0;
1406 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1407 free ((char *) target_percents);
1411 /* Search STRING for an unquoted ; that is not after an unquoted #. */
1413 static char *
1414 find_semicolon (string)
1415 char *string;
1417 char *found, *p;
1419 found = index (string, ';');
1420 while (found != 0 && found[-1] == '\\')
1422 register char *q = &found[-1];
1423 register int backslash = 0;
1424 while (*q-- == '\\')
1425 backslash = !backslash;
1426 if (backslash)
1427 found = index (found + 1, ';');
1428 else
1429 break;
1431 if (found == 0)
1432 return 0;
1434 /* Look for a comment character (#) before the ; we found. */
1435 p = lindex (string, found, '#');
1436 while (p != 0 && p[-1] == '\\')
1438 register char *q = &p[-1];
1439 register int backslash = 0;
1440 while (*q-- == '\\')
1441 backslash = !backslash;
1442 if (backslash)
1443 p = lindex (p + 1, found, '#');
1444 else
1445 break;
1447 if (p == 0)
1448 return found;
1449 return 0;
1452 /* Search PATTERN for an unquoted %. Backslashes quote % and backslash.
1453 Quoting backslashes are removed from PATTERN by compacting it into
1454 itself. Returns a pointer to the first unquoted % if there is one,
1455 or nil if there are none. */
1457 char *
1458 find_percent (pattern)
1459 char *pattern;
1461 unsigned int pattern_len = strlen (pattern);
1462 register char *p = pattern;
1464 while ((p = index (p, '%')) != 0)
1465 if (p > pattern && p[-1] == '\\')
1467 /* Search for more backslashes. */
1468 register int i = -2;
1469 while (&p[i] >= pattern && p[i] == '\\')
1470 --i;
1471 ++i;
1472 /* The number of backslashes is now -I.
1473 Copy P over itself to swallow half of them. */
1474 bcopy (&p[i / 2], &p[i], (pattern_len - (p - pattern)) - (i / 2) + 1);
1475 p += i / 2;
1476 if (i % 2 == 0)
1477 /* All the backslashes quoted each other; the % was unquoted. */
1478 return p;
1480 /* The % was quoted by a backslash. Look for another. */
1482 else
1483 /* No backslash in sight. */
1484 return p;
1486 /* Never hit a %. */
1487 return 0;
1490 /* Parse a string into a sequence of filenames represented as a
1491 chain of struct nameseq's in reverse order and return that chain.
1493 The string is passed as STRINGP, the address of a string pointer.
1494 The string pointer is updated to point at the first character
1495 not parsed, which either is a null char or equals STOPCHAR.
1497 SIZE is how big to construct chain elements.
1498 This is useful if we want them actually to be other structures
1499 that have room for additional info.
1501 If STRIP is nonzero, strip `./'s off the beginning. */
1503 struct nameseq *
1504 parse_file_seq (stringp, stopchar, size, strip)
1505 char **stringp;
1506 char stopchar;
1507 unsigned int size;
1508 int strip;
1510 register struct nameseq *new = 0;
1511 register struct nameseq *new1;
1512 register char *p = *stringp;
1513 char *q;
1514 char *name;
1515 register int c;
1517 while (1)
1519 /* Skip whitespace; see if any more names are left. */
1520 p = next_token (p);
1521 if (*p == '\0')
1522 break;
1523 if (*p == stopchar)
1524 break;
1525 /* Yes, find end of next name. */
1526 q = p;
1527 while (1)
1529 c = *p++;
1530 if (c == '\0')
1531 break;
1532 else if (c == '\\' &&
1533 (*p == '\\' || isblank (*p) || *p == stopchar))
1534 ++p;
1535 else if (isblank (c) || c == stopchar)
1536 break;
1538 p--;
1540 if (strip)
1541 /* Skip leading `./'s. */
1542 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1544 q += 2; /* Skip "./". */
1545 while (q < p && *q == '/')
1546 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1547 ++q;
1550 /* Extract the filename just found, and skip it. */
1552 if (q == p)
1553 /* ".///" was stripped to "". */
1554 name = savestring ("./", 2);
1555 else
1556 name = savestring (q, p - q);
1558 /* Add it to the front of the chain. */
1559 new1 = (struct nameseq *) xmalloc (size);
1560 new1->name = name;
1561 new1->next = new;
1562 new = new1;
1565 #ifndef NO_ARCHIVES
1567 /* Look for multi-word archive references.
1568 They are indicated by a elt ending with an unmatched `)' and
1569 an elt further down the chain (i.e., previous in the file list)
1570 with an unmatched `(' (e.g., "lib(mem"). */
1572 for (new1 = new; new1 != 0; new1 = new1->next)
1573 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1574 && new1->name[strlen (new1->name) - 1] == ')'
1575 && index (new1->name, '(') == 0)
1577 /* NEW1 ends with a `)' but does not contain a `('.
1578 Look back for an elt with an opening `(' but no closing `)'. */
1580 struct nameseq *n = new1->next, *lastn = new1;
1581 char *paren;
1582 while (n != 0 && (paren = index (n->name, '(')) == 0)
1584 lastn = n;
1585 n = n->next;
1587 if (n != 0
1588 /* Ignore something starting with `(', as that cannot actually
1589 be an archive-member reference (and treating it as such
1590 results in an empty file name, which causes much lossage). */
1591 && n->name[0] != '(')
1593 /* N is the first element in the archive group.
1594 Its name looks like "lib(mem" (with no closing `)'). */
1596 char *libname;
1598 /* Copy "lib(" into LIBNAME. */
1599 ++paren;
1600 libname = (char *) alloca (paren - n->name + 1);
1601 bcopy (n->name, libname, paren - n->name);
1602 libname[paren - n->name] = '\0';
1604 if (*paren == '\0')
1606 /* N was just "lib(", part of something like "lib( a b)".
1607 Edit it out of the chain and free its storage. */
1608 lastn->next = n->next;
1609 free (n->name);
1610 free ((char *) n);
1611 /* LASTN->next is the new stopping elt for the loop below. */
1612 n = lastn->next;
1614 else
1616 /* Replace N's name with the full archive reference. */
1617 name = concat (libname, paren, ")");
1618 free (n->name);
1619 n->name = name;
1622 if (new1->name[1] == '\0')
1624 /* NEW1 is just ")", part of something like "lib(a b )".
1625 Omit it from the chain and free its storage. */
1626 lastn = new1;
1627 new1 = new1->next;
1628 if (new == lastn)
1629 new = new1;
1630 free (lastn->name);
1631 free ((char *) lastn);
1633 else
1635 /* Replace also NEW1->name, which already has closing `)'. */
1636 name = concat (libname, new1->name, "");
1637 free (new1->name);
1638 new1->name = name;
1639 new1 = new1->next;
1642 /* Trace back from NEW1 (the end of the list) until N
1643 (the beginning of the list), rewriting each name
1644 with the full archive reference. */
1646 while (new1 != n)
1648 name = concat (libname, new1->name, ")");
1649 free (new1->name);
1650 new1->name = name;
1651 new1 = new1->next;
1654 if (new1 == 0)
1655 /* We might have slurped up the whole list,
1656 and continuing the loop would dereference NEW1. */
1657 break;
1661 #endif
1663 *stringp = p;
1664 return new;
1667 /* Read a line of text from STREAM into LINEBUFFER.
1668 Combine continuation lines into one line.
1669 Return the number of actual lines read (> 1 if hacked continuation lines).
1672 static unsigned int
1673 readline (linebuffer, stream, filename, lineno)
1674 struct linebuffer *linebuffer;
1675 FILE *stream;
1676 char *filename;
1677 unsigned int lineno;
1679 char *buffer = linebuffer->buffer;
1680 register char *p = linebuffer->buffer;
1681 register char *end = p + linebuffer->size;
1682 register int len, lastlen = 0;
1683 register char *p2;
1684 register unsigned int nlines = 0;
1685 register int backslash;
1687 *p = '\0';
1689 while (fgets (p, end - p, stream) != 0)
1691 len = strlen (p);
1692 if (len == 0)
1694 /* This only happens when the first thing on the line is a '\0'.
1695 It is a pretty hopeless case, but (wonder of wonders) Athena
1696 lossage strikes again! (xmkmf puts NULs in its makefiles.)
1697 There is nothing really to be done; we synthesize a newline so
1698 the following line doesn't appear to be part of this line. */
1699 makefile_error (filename, lineno,
1700 "warning: NUL character seen; rest of line ignored");
1701 p[0] = '\n';
1702 len = 1;
1705 p += len;
1706 if (p[-1] != '\n')
1708 /* Probably ran out of buffer space. */
1709 register unsigned int p_off = p - buffer;
1710 linebuffer->size *= 2;
1711 buffer = (char *) xrealloc (buffer, linebuffer->size);
1712 p = buffer + p_off;
1713 end = buffer + linebuffer->size;
1714 linebuffer->buffer = buffer;
1715 *p = '\0';
1716 lastlen = len;
1717 continue;
1720 ++nlines;
1722 if (len == 1 && p > buffer)
1723 /* P is pointing at a newline and it's the beginning of
1724 the buffer returned by the last fgets call. However,
1725 it is not necessarily the beginning of a line if P is
1726 pointing past the beginning of the holding buffer.
1727 If the buffer was just enlarged (right before the newline),
1728 we must account for that, so we pretend that the two lines
1729 were one line. */
1730 len += lastlen;
1731 lastlen = len;
1732 backslash = 0;
1733 for (p2 = p - 2; --len > 0; --p2)
1735 if (*p2 == '\\')
1736 backslash = !backslash;
1737 else
1738 break;
1741 if (!backslash)
1743 p[-1] = '\0';
1744 break;
1747 if (end - p <= 1)
1749 /* Enlarge the buffer. */
1750 register unsigned int p_off = p - buffer;
1751 linebuffer->size *= 2;
1752 buffer = (char *) xrealloc (buffer, linebuffer->size);
1753 p = buffer + p_off;
1754 end = buffer + linebuffer->size;
1755 linebuffer->buffer = buffer;
1759 if (ferror (stream))
1760 pfatal_with_name (filename);
1762 return nlines;
1765 /* Construct the list of include directories
1766 from the arguments and the default list. */
1768 void
1769 construct_include_path (arg_dirs)
1770 char **arg_dirs;
1772 register unsigned int i;
1773 struct stat stbuf;
1775 /* Table to hold the dirs. */
1777 register unsigned int defsize = (sizeof (default_include_directories)
1778 / sizeof (default_include_directories[0]));
1779 register unsigned int max = 5;
1780 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
1781 register unsigned int idx = 0;
1783 /* First consider any dirs specified with -I switches.
1784 Ignore dirs that don't exist. */
1786 if (arg_dirs != 0)
1787 while (*arg_dirs != 0)
1789 char *dir = *arg_dirs++;
1791 if (dir[0] == '~')
1793 char *expanded = tilde_expand (dir);
1794 if (expanded != 0)
1795 dir = expanded;
1798 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
1800 if (idx == max - 1)
1802 max += 5;
1803 dirs = (char **)
1804 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
1806 dirs[idx++] = dir;
1808 else if (dir != arg_dirs[-1])
1809 free (dir);
1812 /* Now add at the end the standard default dirs. */
1814 for (i = 0; default_include_directories[i] != 0; ++i)
1815 if (stat (default_include_directories[i], &stbuf) == 0
1816 && S_ISDIR (stbuf.st_mode))
1817 dirs[idx++] = default_include_directories[i];
1819 dirs[idx] = 0;
1821 /* Now compute the maximum length of any name in it. */
1823 max_incl_len = 0;
1824 for (i = 0; i < idx; ++i)
1826 unsigned int len = strlen (dirs[i]);
1827 /* If dir name is written with a trailing slash, discard it. */
1828 if (dirs[i][len - 1] == '/')
1829 /* We can't just clobber a null in because it may have come from
1830 a literal string and literal strings may not be writable. */
1831 dirs[i] = savestring (dirs[i], len - 1);
1832 if (len > max_incl_len)
1833 max_incl_len = len;
1836 include_directories = dirs;
1839 /* Expand ~ or ~USER at the beginning of NAME.
1840 Return a newly malloc'd string or 0. */
1842 char *
1843 tilde_expand (name)
1844 char *name;
1846 if (name[1] == '/' || name[1] == '\0')
1848 extern char *getenv ();
1849 char *home_dir;
1850 int is_variable;
1853 /* Turn off --warn-undefined-variables while we expand HOME. */
1854 int save = warn_undefined_variables_flag;
1855 warn_undefined_variables_flag = 0;
1857 home_dir = allocated_variable_expand ("$(HOME)");
1859 warn_undefined_variables_flag = save;
1862 is_variable = home_dir[0] != '\0';
1863 if (!is_variable)
1865 free (home_dir);
1866 home_dir = getenv ("HOME");
1868 if (home_dir == 0 || home_dir[0] == '\0')
1870 extern char *getlogin ();
1871 char *name = getlogin ();
1872 home_dir = 0;
1873 if (name != 0)
1875 struct passwd *p = getpwnam (name);
1876 if (p != 0)
1877 home_dir = p->pw_dir;
1880 if (home_dir != 0)
1882 char *new = concat (home_dir, "", name + 1);
1883 if (is_variable)
1884 free (home_dir);
1885 return new;
1888 else
1890 struct passwd *pwent;
1891 char *userend = index (name + 1, '/');
1892 if (userend != 0)
1893 *userend = '\0';
1894 pwent = getpwnam (name + 1);
1895 if (pwent != 0)
1897 if (userend == 0)
1898 return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
1899 else
1900 return concat (pwent->pw_dir, "/", userend + 1);
1902 else if (userend != 0)
1903 *userend = '/';
1906 return 0;
1909 /* Given a chain of struct nameseq's describing a sequence of filenames,
1910 in reverse of the intended order, return a new chain describing the
1911 result of globbing the filenames. The new chain is in forward order.
1912 The links of the old chain are freed or used in the new chain.
1913 Likewise for the names in the old chain.
1915 SIZE is how big to construct chain elements.
1916 This is useful if we want them actually to be other structures
1917 that have room for additional info. */
1919 struct nameseq *
1920 multi_glob (chain, size)
1921 struct nameseq *chain;
1922 unsigned int size;
1924 register struct nameseq *new = 0;
1925 register struct nameseq *old;
1926 struct nameseq *nexto;
1928 for (old = chain; old != 0; old = nexto)
1930 glob_t gl;
1931 #ifndef NO_ARCHIVES
1932 char *memname;
1933 #endif
1935 nexto = old->next;
1937 if (old->name[0] == '~')
1939 char *newname = tilde_expand (old->name);
1940 if (newname != 0)
1942 free (old->name);
1943 old->name = newname;
1947 #ifndef NO_ARCHIVES
1948 if (ar_name (old->name))
1950 /* OLD->name is an archive member reference.
1951 Replace it with the archive file name,
1952 and save the member name in MEMNAME.
1953 We will glob on the archive name and then
1954 reattach MEMNAME later. */
1955 char *arname;
1956 ar_parse_name (old->name, &arname, &memname);
1957 free (old->name);
1958 old->name = arname;
1960 else
1961 memname = 0;
1962 #endif
1964 switch (glob (old->name, GLOB_NOCHECK, NULL, &gl))
1966 case 0: /* Success. */
1968 register int i = gl.gl_pathc;
1969 while (i-- > 0)
1971 #ifndef NO_ARCHIVES
1972 if (memname != 0)
1974 /* Try to glob on MEMNAME within the archive. */
1975 struct nameseq *found
1976 = ar_glob (gl.gl_pathv[i], memname, size);
1977 if (found == 0)
1979 /* No matches. Use MEMNAME as-is. */
1980 struct nameseq *elt
1981 = (struct nameseq *) xmalloc (size);
1982 unsigned int alen = strlen (gl.gl_pathv[i]);
1983 unsigned int mlen = strlen (memname);
1984 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
1985 bcopy (gl.gl_pathv[i], elt->name, alen);
1986 elt->name[alen] = '(';
1987 bcopy (memname, &elt->name[alen + 1], mlen);
1988 elt->name[alen + 1 + mlen] = ')';
1989 elt->name[alen + 1 + mlen + 1] = '\0';
1990 elt->next = new;
1991 new = elt;
1993 else
1995 /* Find the end of the FOUND chain. */
1996 struct nameseq *f = found;
1997 while (f->next != 0)
1998 f = f->next;
2000 /* Attach the chain being built to the end of the FOUND
2001 chain, and make FOUND the new NEW chain. */
2002 f->next = new;
2003 new = found;
2006 free (memname);
2008 else
2009 #endif
2011 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2012 elt->name = savestring (gl.gl_pathv[i],
2013 strlen (gl.gl_pathv[i]));
2014 elt->next = new;
2015 new = elt;
2018 globfree (&gl);
2019 free (old->name);
2020 free (old);
2021 break;
2024 case GLOB_NOSPACE:
2025 fatal ("virtual memory exhausted");
2026 break;
2028 default:
2029 old->next = new;
2030 new = old;
2031 break;
2035 return new;