Move include of config.h before all others.
[make.git] / read.c
blob5c49b8ca838002da67f8b48c3b6c7ab6bf27ade7
1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988, 89, 90, 91, 92, 93, 94, 1995 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. */
243 char *passed_filename = filename;
245 struct nameseq *filenames = 0;
246 struct dep *deps;
247 unsigned int lineno = 1;
248 unsigned int nlines = 0;
249 int two_colon;
250 char *pattern = 0, *pattern_percent;
252 int makefile_errno;
254 #define record_waiting_files() \
255 do \
257 if (filenames != 0) \
258 record_files (filenames, pattern, pattern_percent, deps, \
259 commands_started, commands, commands_idx, \
260 two_colon, filename, lineno, \
261 !(flags & RM_NO_DEFAULT_GOAL)); \
262 filenames = 0; \
263 commands_idx = 0; \
264 pattern = 0; \
265 } while (0)
267 #ifdef lint /* Suppress `used before set' messages. */
268 two_colon = 0;
269 #endif
271 if (debug_flag)
273 printf ("Reading makefile `%s'", filename);
274 if (flags & RM_NO_DEFAULT_GOAL)
275 printf (" (no default goal)");
276 if (flags & RM_INCLUDED)
277 printf (" (search path)");
278 if (flags & RM_DONTCARE)
279 printf (" (don't care)");
280 if (flags & RM_NO_TILDE)
281 printf (" (no ~ expansion)");
282 puts ("...");
285 /* First, get a stream to read. */
287 /* Expand ~ in FILENAME unless it came from `include',
288 in which case it was already done. */
289 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
291 char *expanded = tilde_expand (filename);
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 if (filename != passed_filename)
334 free (filename);
335 filename = deps->file->name;
336 deps->changed = flags;
337 deps = 0;
339 /* If the makefile can't be found at all, give up entirely. */
341 if (infile == 0)
343 /* If we did some searching, errno has the error from the last
344 attempt, rather from FILENAME itself. Restore it in case the
345 caller wants to use it in a message. */
346 errno = makefile_errno;
347 return 0;
350 reading_filename = filename;
351 reading_lineno_ptr = &lineno;
353 /* Loop over lines in the file.
354 The strategy is to accumulate target names in FILENAMES, dependencies
355 in DEPS and commands in COMMANDS. These are used to define a rule
356 when the start of the next rule (or eof) is encountered. */
358 initbuffer (&lb);
360 while (!feof (infile))
362 lineno += nlines;
363 nlines = readline (&lb, infile, filename, lineno);
365 /* Check for a shell command line first.
366 If it is not one, we can stop treating tab specially. */
367 if (lb.buffer[0] == '\t')
369 /* This line is a probably shell command. */
370 unsigned int len;
372 if (no_targets)
373 /* Ignore the commands in a rule with no targets. */
374 continue;
376 /* If there is no preceding rule line, don't treat this line
377 as a command, even though it begins with a tab character.
378 SunOS 4 make appears to behave this way. */
380 if (filenames != 0)
382 if (ignoring)
383 /* Yep, this is a shell command, and we don't care. */
384 continue;
386 /* Append this command line to the line being accumulated. */
387 p = lb.buffer;
388 if (commands_idx == 0)
389 commands_started = lineno;
390 len = strlen (p);
391 if (len + 1 + commands_idx > commands_len)
393 commands_len = (len + 1 + commands_idx) * 2;
394 commands = (char *) xrealloc (commands, commands_len);
396 bcopy (p, &commands[commands_idx], len);
397 commands_idx += len;
398 commands[commands_idx++] = '\n';
400 continue;
404 /* This line is not a shell command line. Don't worry about tabs. */
406 if (collapsed_length < lb.size)
408 collapsed_length = lb.size;
409 if (collapsed != 0)
410 free (collapsed);
411 collapsed = (char *) xmalloc (collapsed_length);
413 strcpy (collapsed, lb.buffer);
414 /* Collapse continuation lines. */
415 collapse_continuations (collapsed);
416 remove_comments (collapsed);
418 /* strncmp is first to avoid dereferencing out into space. */
419 #define word1eq(s, l) (!strncmp (s, p, l) \
420 && (p[l] == '\0' || isblank (p[l])))
421 p = collapsed;
422 while (isspace (*p))
423 ++p;
424 if (*p == '\0')
425 /* This line is completely empty. */
426 continue;
428 /* We must first check for conditional and `define' directives before
429 ignoring anything, since they control what we will do with
430 following lines. */
432 if (!in_ignored_define
433 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
434 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
435 || word1eq ("else", 4) || word1eq ("endif", 5)))
437 int i = conditional_line (p, filename, lineno);
438 if (i >= 0)
439 ignoring = i;
440 else
441 makefile_fatal (filename, lineno,
442 "invalid syntax in conditional");
443 continue;
445 else if (word1eq ("endef", 5))
447 if (in_ignored_define)
448 in_ignored_define = 0;
449 else
450 makefile_fatal (filename, lineno, "extraneous `endef'");
451 continue;
453 else if (word1eq ("define", 6))
455 if (ignoring)
456 in_ignored_define = 1;
457 else
459 p2 = next_token (p + 6);
460 /* Let the variable name be the whole rest of the line,
461 with trailing blanks stripped (comments have already been
462 removed), so it could be a complex variable/function
463 reference that might contain blanks. */
464 p = index (p2, '\0');
465 while (isblank (p[-1]))
466 --p;
467 lineno = do_define (p2, p - p2, o_file,
468 lineno, infile, filename);
470 continue;
472 else if (word1eq ("override", 8))
474 p2 = next_token (p + 8);
475 if (p2 == 0)
476 makefile_error (filename, lineno, "empty `override' directive");
477 if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
479 if (ignoring)
480 in_ignored_define = 1;
481 else
483 p2 = next_token (p2 + 6);
484 /* Let the variable name be the whole rest of the line,
485 with trailing blanks stripped (comments have already been
486 removed), so it could be a complex variable/function
487 reference that might contain blanks. */
488 p = index (p2, '\0');
489 while (isblank (p[-1]))
490 --p;
491 lineno = do_define (p2, p - p2, o_override,
492 lineno, infile, filename);
495 else if (!ignoring
496 && !try_variable_definition (filename, lineno,
497 p2, o_override))
498 makefile_error (filename, lineno, "empty `override' directive");
500 continue;
503 if (ignoring)
504 /* Ignore the line. We continue here so conditionals
505 can appear in the middle of a rule. */
506 continue;
507 else if (word1eq ("export", 6))
509 struct variable *v;
510 p2 = next_token (p + 6);
511 if (*p2 == '\0')
512 export_all_variables = 1;
513 v = try_variable_definition (filename, lineno, p2, o_file);
514 if (v != 0)
515 v->export = v_export;
516 else
518 unsigned int len;
519 for (p = find_next_token (&p2, &len); p != 0;
520 p = find_next_token (&p2, &len))
522 v = lookup_variable (p, len);
523 if (v == 0)
524 v = define_variable (p, len, "", o_file, 0);
525 v->export = v_export;
529 else if (word1eq ("unexport", 8))
531 unsigned int len;
532 struct variable *v;
533 p2 = next_token (p + 8);
534 if (*p2 == '\0')
535 export_all_variables = 0;
536 for (p = find_next_token (&p2, &len); p != 0;
537 p = find_next_token (&p2, &len))
539 v = lookup_variable (p, len);
540 if (v == 0)
541 v = define_variable (p, len, "", o_file, 0);
542 v->export = v_noexport;
545 else if (word1eq ("include", 7) || word1eq ("-include", 8))
547 /* We have found an `include' line specifying a nested
548 makefile to be read at this point. */
549 struct conditionals *save, new_conditionals;
550 struct nameseq *files;
551 /* "-include" (vs "include") says no
552 error if the file does not exist. */
553 int noerror = p[0] == '-';
555 p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
556 if (*p == '\0')
558 makefile_error (filename, lineno,
559 "no file name for `%sinclude'",
560 noerror ? "-" : "");
561 continue;
564 /* Parse the list of file names. */
565 p2 = p;
566 files = multi_glob (parse_file_seq (&p2, '\0',
567 sizeof (struct nameseq),
569 sizeof (struct nameseq));
570 free (p);
572 /* Save the state of conditionals and start
573 the included makefile with a clean slate. */
574 save = conditionals;
575 bzero ((char *) &new_conditionals, sizeof new_conditionals);
576 conditionals = &new_conditionals;
578 /* Record the rules that are waiting so they will determine
579 the default goal before those in the included makefile. */
580 record_waiting_files ();
582 /* Read each included makefile. */
583 while (files != 0)
585 struct nameseq *next = files->next;
586 char *name = files->name;
587 free (files);
588 files = next;
590 if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
591 | (noerror ? RM_DONTCARE : 0)))
592 && ! noerror)
593 makefile_error (filename, lineno,
594 "%s: %s", name, strerror (errno));
597 /* Free any space allocated by conditional_line. */
598 if (conditionals->ignoring)
599 free (conditionals->ignoring);
600 if (conditionals->seen_else)
601 free (conditionals->seen_else);
603 /* Restore state. */
604 conditionals = save;
605 reading_filename = filename;
606 reading_lineno_ptr = &lineno;
608 else if (word1eq ("vpath", 5))
610 char *pattern;
611 unsigned int len;
612 p2 = variable_expand (p + 5);
613 p = find_next_token (&p2, &len);
614 if (p != 0)
616 pattern = savestring (p, len);
617 p = find_next_token (&p2, &len);
618 /* No searchpath means remove all previous
619 selective VPATH's with the same pattern. */
621 else
622 /* No pattern means remove all previous selective VPATH's. */
623 pattern = 0;
624 construct_vpath_list (pattern, p);
625 if (pattern != 0)
626 free (pattern);
628 #undef word1eq
629 else if (try_variable_definition (filename, lineno, p, o_file))
630 /* This line has been dealt with. */
632 else if (lb.buffer[0] == '\t')
634 p = collapsed; /* Ignore comments. */
635 while (isblank (*p))
636 ++p;
637 if (*p == '\0')
638 /* The line is completely blank; that is harmless. */
639 continue;
640 /* This line starts with a tab but was not caught above
641 because there was no preceding target, and the line
642 might have been usable as a variable definition.
643 But now it is definitely lossage. */
644 makefile_fatal (filename, lineno,
645 "commands commence before first target");
647 else
649 /* This line describes some target files. */
651 char *cmdleft;
653 /* Record the previous rule. */
655 record_waiting_files ();
657 /* Look for a semicolon in the unexpanded line. */
658 cmdleft = find_semicolon (lb.buffer);
659 if (cmdleft != 0)
660 /* Found one. Cut the line short there before expanding it. */
661 *cmdleft = '\0';
663 collapse_continuations (lb.buffer);
665 /* Expand variable and function references before doing anything
666 else so that special characters can be inside variables. */
667 p = variable_expand (lb.buffer);
669 if (cmdleft == 0)
670 /* Look for a semicolon in the expanded line. */
671 cmdleft = find_semicolon (p);
673 if (cmdleft != 0)
674 /* Cut the line short at the semicolon. */
675 *cmdleft = '\0';
677 /* Remove comments from the line. */
678 remove_comments (p);
680 p2 = next_token (p);
681 if (*p2 == '\0')
683 if (cmdleft != 0)
684 makefile_fatal (filename, lineno,
685 "missing rule before commands");
686 else
687 /* This line contained a variable reference that
688 expanded to nothing but whitespace. */
689 continue;
691 else if (*p2 == ':')
693 /* We accept and ignore rules without targets for
694 compatibility with SunOS 4 make. */
695 no_targets = 1;
696 continue;
699 filenames = multi_glob (parse_file_seq (&p2, ':',
700 sizeof (struct nameseq),
702 sizeof (struct nameseq));
703 if (*p2++ == '\0')
704 makefile_fatal (filename, lineno, "missing separator");
705 /* Is this a one-colon or two-colon entry? */
706 two_colon = *p2 == ':';
707 if (two_colon)
708 p2++;
710 /* We have some targets, so don't ignore the following commands. */
711 no_targets = 0;
713 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
714 p = index (p2, ':');
715 while (p != 0 && p[-1] == '\\')
717 register char *q = &p[-1];
718 register int backslash = 0;
719 while (*q-- == '\\')
720 backslash = !backslash;
721 if (backslash)
722 p = index (p + 1, ':');
723 else
724 break;
726 #ifdef __MSDOS__
727 /* For MS-DOS, skip a "C:\...". */
728 if (p != 0 && p[1] == '\\' && isalpha (p[-1]))
729 p = 0;
730 #endif
731 if (p != 0)
733 struct nameseq *target;
734 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
735 ++p2;
736 if (target == 0)
737 makefile_fatal (filename, lineno, "missing target pattern");
738 else if (target->next != 0)
739 makefile_fatal (filename, lineno, "multiple target patterns");
740 pattern = target->name;
741 pattern_percent = find_percent (pattern);
742 if (pattern_percent == 0)
743 makefile_fatal (filename, lineno,
744 "target pattern contains no `%%'");
746 else
747 pattern = 0;
749 /* Parse the dependencies. */
750 deps = (struct dep *)
751 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
752 sizeof (struct dep));
754 commands_idx = 0;
755 if (cmdleft != 0)
757 /* Semicolon means rest of line is a command. */
758 unsigned int len = strlen (cmdleft + 1);
760 commands_started = lineno;
762 /* Add this command line to the buffer. */
763 if (len + 2 > commands_len)
765 commands_len = (len + 2) * 2;
766 commands = (char *) xrealloc (commands, commands_len);
768 bcopy (cmdleft + 1, commands, len);
769 commands_idx += len;
770 commands[commands_idx++] = '\n';
773 continue;
776 /* We get here except in the case that we just read a rule line.
777 Record now the last rule we read, so following spurious
778 commands are properly diagnosed. */
779 record_waiting_files ();
780 no_targets = 0;
783 if (conditionals->if_cmds)
784 makefile_fatal (filename, lineno, "missing `endif'");
786 /* At eof, record the last rule. */
787 record_waiting_files ();
789 freebuffer (&lb);
790 free ((char *) commands);
791 fclose (infile);
793 reading_filename = 0;
794 reading_lineno_ptr = 0;
796 return 1;
799 /* Execute a `define' directive.
800 The first line has already been read, and NAME is the name of
801 the variable to be defined. The following lines remain to be read.
802 LINENO, INFILE and FILENAME refer to the makefile being read.
803 The value returned is LINENO, updated for lines read here. */
805 static unsigned int
806 do_define (name, namelen, origin, lineno, infile, filename)
807 char *name;
808 unsigned int namelen;
809 enum variable_origin origin;
810 unsigned int lineno;
811 FILE *infile;
812 char *filename;
814 struct linebuffer lb;
815 unsigned int nlines = 0;
816 unsigned int length = 100;
817 char *definition = (char *) xmalloc (100);
818 register unsigned int idx = 0;
819 register char *p;
821 /* Expand the variable name. */
822 char *var = (char *) alloca (namelen + 1);
823 bcopy (name, var, namelen);
824 var[namelen] = '\0';
825 var = variable_expand (var);
827 initbuffer (&lb);
828 while (!feof (infile))
830 lineno += nlines;
831 nlines = readline (&lb, infile, filename, lineno);
833 collapse_continuations (lb.buffer);
835 p = next_token (lb.buffer);
836 if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
838 p += 5;
839 remove_comments (p);
840 if (*next_token (p) != '\0')
841 makefile_error (filename, lineno,
842 "Extraneous text after `endef' directive");
843 /* Define the variable. */
844 if (idx == 0)
845 definition[0] = '\0';
846 else
847 definition[idx - 1] = '\0';
848 (void) define_variable (var, strlen (var), definition, origin, 1);
849 free (definition);
850 freebuffer (&lb);
851 return lineno;
853 else
855 unsigned int len = strlen (lb.buffer);
857 /* Increase the buffer size if necessary. */
858 if (idx + len + 1 > length)
860 length = (idx + len) * 2;
861 definition = (char *) xrealloc (definition, length + 1);
864 bcopy (lb.buffer, &definition[idx], len);
865 idx += len;
866 /* Separate lines with a newline. */
867 definition[idx++] = '\n';
871 /* No `endef'!! */
872 makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
874 /* NOTREACHED */
875 return 0;
878 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
879 "ifneq", "else" and "endif".
880 LINE is the input line, with the command as its first word.
882 FILENAME and LINENO are the filename and line number in the
883 current makefile. They are used for error messages.
885 Value is -1 if the line is invalid,
886 0 if following text should be interpreted,
887 1 if following text should be ignored. */
889 static int
890 conditional_line (line, filename, lineno)
891 char *line;
892 char *filename;
893 unsigned int lineno;
895 int notdef;
896 char *cmdname;
897 register unsigned int i;
899 if (*line == 'i')
901 /* It's an "if..." command. */
902 notdef = line[2] == 'n';
903 if (notdef)
905 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
906 line += cmdname[3] == 'd' ? 7 : 6;
908 else
910 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
911 line += cmdname[2] == 'd' ? 6 : 5;
914 else
916 /* It's an "else" or "endif" command. */
917 notdef = line[1] == 'n';
918 cmdname = notdef ? "endif" : "else";
919 line += notdef ? 5 : 4;
922 line = next_token (line);
924 if (*cmdname == 'e')
926 if (*line != '\0')
927 makefile_error (filename, lineno,
928 "Extraneous text after `%s' directive",
929 cmdname);
930 /* "Else" or "endif". */
931 if (conditionals->if_cmds == 0)
932 makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
933 /* NOTDEF indicates an `endif' command. */
934 if (notdef)
935 --conditionals->if_cmds;
936 else if (conditionals->seen_else[conditionals->if_cmds - 1])
937 makefile_fatal (filename, lineno, "only one `else' per conditional");
938 else
940 /* Toggle the state of ignorance. */
941 conditionals->ignoring[conditionals->if_cmds - 1]
942 = !conditionals->ignoring[conditionals->if_cmds - 1];
943 /* Record that we have seen an `else' in this conditional.
944 A second `else' will be erroneous. */
945 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
947 for (i = 0; i < conditionals->if_cmds; ++i)
948 if (conditionals->ignoring[i])
949 return 1;
950 return 0;
953 if (conditionals->allocated == 0)
955 conditionals->allocated = 5;
956 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
957 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
960 ++conditionals->if_cmds;
961 if (conditionals->if_cmds > conditionals->allocated)
963 conditionals->allocated += 5;
964 conditionals->ignoring = (char *)
965 xrealloc (conditionals->ignoring, conditionals->allocated);
966 conditionals->seen_else = (char *)
967 xrealloc (conditionals->seen_else, conditionals->allocated);
970 /* Record that we have seen an `if...' but no `else' so far. */
971 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
973 /* Search through the stack to see if we're already ignoring. */
974 for (i = 0; i < conditionals->if_cmds - 1; ++i)
975 if (conditionals->ignoring[i])
977 /* We are already ignoring, so just push a level
978 to match the next "else" or "endif", and keep ignoring.
979 We don't want to expand variables in the condition. */
980 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
981 return 1;
984 if (cmdname[notdef ? 3 : 2] == 'd')
986 /* "Ifdef" or "ifndef". */
987 struct variable *v;
988 register char *p = end_of_token (line);
989 i = p - line;
990 p = next_token (p);
991 if (*p != '\0')
992 return -1;
993 v = lookup_variable (line, i);
994 conditionals->ignoring[conditionals->if_cmds - 1]
995 = (v != 0 && *v->value != '\0') == notdef;
997 else
999 /* "Ifeq" or "ifneq". */
1000 char *s1, *s2;
1001 unsigned int len;
1002 char termin = *line == '(' ? ',' : *line;
1004 if (termin != ',' && termin != '"' && termin != '\'')
1005 return -1;
1007 s1 = ++line;
1008 /* Find the end of the first string. */
1009 if (termin == ',')
1011 register int count = 0;
1012 for (; *line != '\0'; ++line)
1013 if (*line == '(')
1014 ++count;
1015 else if (*line == ')')
1016 --count;
1017 else if (*line == ',' && count <= 0)
1018 break;
1020 else
1021 while (*line != '\0' && *line != termin)
1022 ++line;
1024 if (*line == '\0')
1025 return -1;
1027 *line++ = '\0';
1029 s2 = variable_expand (s1);
1030 /* We must allocate a new copy of the expanded string because
1031 variable_expand re-uses the same buffer. */
1032 len = strlen (s2);
1033 s1 = (char *) alloca (len + 1);
1034 bcopy (s2, s1, len + 1);
1036 if (termin != ',')
1037 /* Find the start of the second string. */
1038 line = next_token (line);
1040 termin = termin == ',' ? ')' : *line;
1041 if (termin != ')' && termin != '"' && termin != '\'')
1042 return -1;
1044 /* Find the end of the second string. */
1045 if (termin == ')')
1047 register int count = 0;
1048 s2 = next_token (line);
1049 for (line = s2; *line != '\0'; ++line)
1051 if (*line == '(')
1052 ++count;
1053 else if (*line == ')')
1054 if (count <= 0)
1055 break;
1056 else
1057 --count;
1060 else
1062 ++line;
1063 s2 = line;
1064 while (*line != '\0' && *line != termin)
1065 ++line;
1068 if (*line == '\0')
1069 return -1;
1071 *line = '\0';
1072 line = next_token (++line);
1073 if (*line != '\0')
1074 makefile_error (filename, lineno,
1075 "Extraneous text after `%s' directive",
1076 cmdname);
1078 s2 = variable_expand (s2);
1079 conditionals->ignoring[conditionals->if_cmds - 1]
1080 = streq (s1, s2) == notdef;
1083 /* Search through the stack to see if we're ignoring. */
1084 for (i = 0; i < conditionals->if_cmds; ++i)
1085 if (conditionals->ignoring[i])
1086 return 1;
1087 return 0;
1090 /* Remove duplicate dependencies in CHAIN. */
1092 void
1093 uniquize_deps (chain)
1094 struct dep *chain;
1096 register struct dep *d;
1098 /* Make sure that no dependencies are repeated. This does not
1099 really matter for the purpose of updating targets, but it
1100 might make some names be listed twice for $^ and $?. */
1102 for (d = chain; d != 0; d = d->next)
1104 struct dep *last, *next;
1106 last = d;
1107 next = d->next;
1108 while (next != 0)
1109 if (streq (dep_name (d), dep_name (next)))
1111 struct dep *n = next->next;
1112 last->next = n;
1113 if (next->name != 0 && next->name != d->name)
1114 free (next->name);
1115 if (next != d)
1116 free ((char *) next);
1117 next = n;
1119 else
1121 last = next;
1122 next = next->next;
1127 /* Record a description line for files FILENAMES,
1128 with dependencies DEPS, commands to execute described
1129 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1130 TWO_COLON is nonzero if a double colon was used.
1131 If not nil, PATTERN is the `%' pattern to make this
1132 a static pattern rule, and PATTERN_PERCENT is a pointer
1133 to the `%' within it.
1135 The links of FILENAMES are freed, and so are any names in it
1136 that are not incorporated into other data structures. */
1138 static void
1139 record_files (filenames, pattern, pattern_percent, deps, commands_started,
1140 commands, commands_idx, two_colon, filename, lineno, set_default)
1141 struct nameseq *filenames;
1142 char *pattern, *pattern_percent;
1143 struct dep *deps;
1144 unsigned int commands_started;
1145 char *commands;
1146 unsigned int commands_idx;
1147 int two_colon;
1148 char *filename;
1149 unsigned int lineno;
1150 int set_default;
1152 struct nameseq *nextf;
1153 int implicit = 0;
1154 unsigned int max_targets, target_idx;
1155 char **targets = 0, **target_percents = 0;
1156 struct commands *cmds;
1158 if (commands_idx > 0)
1160 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1161 cmds->filename = filename;
1162 cmds->lineno = commands_started;
1163 cmds->commands = savestring (commands, commands_idx);
1164 cmds->command_lines = 0;
1166 else
1167 cmds = 0;
1169 for (; filenames != 0; filenames = nextf)
1171 register char *name = filenames->name;
1172 register struct file *f;
1173 register struct dep *d;
1174 struct dep *this;
1175 char *implicit_percent;
1177 nextf = filenames->next;
1178 free ((char *) filenames);
1180 implicit_percent = find_percent (name);
1181 implicit |= implicit_percent != 0;
1183 if (implicit && pattern != 0)
1184 makefile_fatal (filename, lineno,
1185 "mixed implicit and static pattern rules");
1187 if (implicit && implicit_percent == 0)
1188 makefile_fatal (filename, lineno, "mixed implicit and normal rules");
1190 if (implicit)
1192 if (targets == 0)
1194 max_targets = 5;
1195 targets = (char **) xmalloc (5 * sizeof (char *));
1196 target_percents = (char **) xmalloc (5 * sizeof (char *));
1197 target_idx = 0;
1199 else if (target_idx == max_targets - 1)
1201 max_targets += 5;
1202 targets = (char **) xrealloc ((char *) targets,
1203 max_targets * sizeof (char *));
1204 target_percents
1205 = (char **) xrealloc ((char *) target_percents,
1206 max_targets * sizeof (char *));
1208 targets[target_idx] = name;
1209 target_percents[target_idx] = implicit_percent;
1210 ++target_idx;
1211 continue;
1214 /* If there are multiple filenames, copy the chain DEPS
1215 for all but the last one. It is not safe for the same deps
1216 to go in more than one place in the data base. */
1217 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1219 if (pattern != 0)
1220 /* If this is an extended static rule:
1221 `targets: target%pattern: dep%pattern; cmds',
1222 translate each dependency pattern into a plain filename
1223 using the target pattern and this target's name. */
1224 if (!pattern_matches (pattern, pattern_percent, name))
1226 /* Give a warning if the rule is meaningless. */
1227 makefile_error (filename, lineno,
1228 "target `%s' doesn't match the target pattern",
1229 name);
1230 this = 0;
1232 else
1234 /* We use patsubst_expand to do the work of translating
1235 the target pattern, the target's name and the dependencies'
1236 patterns into plain dependency names. */
1237 char *buffer = variable_expand ("");
1239 for (d = this; d != 0; d = d->next)
1241 char *o;
1242 char *percent = find_percent (d->name);
1243 if (percent == 0)
1244 continue;
1245 o = patsubst_expand (buffer, name, pattern, d->name,
1246 pattern_percent, percent);
1247 free (d->name);
1248 d->name = savestring (buffer, o - buffer);
1252 if (!two_colon)
1254 /* Single-colon. Combine these dependencies
1255 with others in file's existing record, if any. */
1256 f = enter_file (name);
1258 if (f->double_colon)
1259 makefile_fatal (filename, lineno,
1260 "target file `%s' has both : and :: entries",
1261 f->name);
1263 /* If CMDS == F->CMDS, this target was listed in this rule
1264 more than once. Just give a warning since this is harmless. */
1265 if (cmds != 0 && cmds == f->cmds)
1266 makefile_error
1267 (filename, lineno,
1268 "target `%s' given more than once in the same rule.",
1269 f->name);
1271 /* Check for two single-colon entries both with commands.
1272 Check is_target so that we don't lose on files such as .c.o
1273 whose commands were preinitialized. */
1274 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1276 makefile_error (cmds->filename, cmds->lineno,
1277 "warning: overriding commands for target `%s'",
1278 f->name);
1279 makefile_error (f->cmds->filename, f->cmds->lineno,
1280 "warning: ignoring old commands for target `%s'",
1281 f->name);
1284 f->is_target = 1;
1286 /* Defining .DEFAULT with no deps or cmds clears it. */
1287 if (f == default_file && this == 0 && cmds == 0)
1288 f->cmds = 0;
1289 if (cmds != 0)
1290 f->cmds = cmds;
1291 /* Defining .SUFFIXES with no dependencies
1292 clears out the list of suffixes. */
1293 if (f == suffix_file && this == 0)
1295 d = f->deps;
1296 while (d != 0)
1298 struct dep *nextd = d->next;
1299 free (d->name);
1300 free (d);
1301 d = nextd;
1303 f->deps = 0;
1305 else if (f->deps != 0)
1307 /* Add the file's old deps and the new ones in THIS together. */
1309 struct dep *firstdeps, *moredeps;
1310 if (cmds != 0)
1312 /* This is the rule with commands, so put its deps first.
1313 The rationale behind this is that $< expands to the
1314 first dep in the chain, and commands use $< expecting
1315 to get the dep that rule specifies. */
1316 firstdeps = this;
1317 moredeps = f->deps;
1319 else
1321 /* Append the new deps to the old ones. */
1322 firstdeps = f->deps;
1323 moredeps = this;
1326 if (firstdeps == 0)
1327 firstdeps = moredeps;
1328 else
1330 d = firstdeps;
1331 while (d->next != 0)
1332 d = d->next;
1333 d->next = moredeps;
1336 f->deps = firstdeps;
1338 else
1339 f->deps = this;
1341 /* If this is a static pattern rule, set the file's stem to
1342 the part of its name that matched the `%' in the pattern,
1343 so you can use $* in the commands. */
1344 if (pattern != 0)
1346 static char *percent = "%";
1347 char *buffer = variable_expand ("");
1348 char *o = patsubst_expand (buffer, name, pattern, percent,
1349 pattern_percent, percent);
1350 f->stem = savestring (buffer, o - buffer);
1353 else
1355 /* Double-colon. Make a new record
1356 even if the file already has one. */
1357 f = lookup_file (name);
1358 /* Check for both : and :: rules. Check is_target so
1359 we don't lose on default suffix rules or makefiles. */
1360 if (f != 0 && f->is_target && !f->double_colon)
1361 makefile_fatal (filename, lineno,
1362 "target file `%s' has both : and :: entries",
1363 f->name);
1364 f = enter_file (name);
1365 /* If there was an existing entry and it was a double-colon
1366 entry, enter_file will have returned a new one, making it the
1367 prev pointer of the old one, and setting its double_colon
1368 pointer to the first one. */
1369 if (f->double_colon == 0)
1370 /* This is the first entry for this name, so we must
1371 set its double_colon pointer to itself. */
1372 f->double_colon = f;
1373 f->is_target = 1;
1374 f->deps = this;
1375 f->cmds = cmds;
1378 /* Free name if not needed further. */
1379 if (f != 0 && name != f->name
1380 && (name < f->name || name > f->name + strlen (f->name)))
1382 free (name);
1383 name = f->name;
1386 /* See if this is first target seen whose name does
1387 not start with a `.', unless it contains a slash. */
1388 if (default_goal_file == 0 && set_default
1389 && (*name != '.' || index (name, '/') != 0))
1391 int reject = 0;
1393 /* If this file is a suffix, don't
1394 let it be the default goal file. */
1396 for (d = suffix_file->deps; d != 0; d = d->next)
1398 register struct dep *d2;
1399 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1401 reject = 1;
1402 break;
1404 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1406 register unsigned int len = strlen (dep_name (d2));
1407 if (strncmp (name, dep_name (d2), len))
1408 continue;
1409 if (streq (name + len, dep_name (d)))
1411 reject = 1;
1412 break;
1415 if (reject)
1416 break;
1419 if (!reject)
1420 default_goal_file = f;
1424 if (implicit)
1426 targets[target_idx] = 0;
1427 target_percents[target_idx] = 0;
1428 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1429 free ((char *) target_percents);
1433 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1434 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1435 Quoting backslashes are removed from STRING by compacting it into
1436 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1437 one, or nil if there are none. */
1439 char *
1440 find_char_unquote (string, stopchars, blank)
1441 char *string;
1442 char *stopchars;
1443 int blank;
1445 unsigned int string_len = strlen (string);
1446 register char *p = string;
1448 while (1)
1450 while (*p != '\0' && index (stopchars, *p) == 0
1451 && (!blank || !isblank (*p)))
1452 ++p;
1453 if (*p == '\0')
1454 break;
1456 if (p > string && p[-1] == '\\')
1458 /* Search for more backslashes. */
1459 register int i = -2;
1460 while (&p[i] >= string && p[i] == '\\')
1461 --i;
1462 ++i;
1463 /* The number of backslashes is now -I.
1464 Copy P over itself to swallow half of them. */
1465 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1466 p += i / 2;
1467 if (i % 2 == 0)
1468 /* All the backslashes quoted each other; the STOPCHAR was
1469 unquoted. */
1470 return p;
1472 /* The STOPCHAR was quoted by a backslash. Look for another. */
1474 else
1475 /* No backslash in sight. */
1476 return p;
1479 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1480 return 0;
1483 /* Search PATTERN for an unquoted %. */
1485 char *
1486 find_percent (pattern)
1487 char *pattern;
1489 return find_char_unquote (pattern, "%", 0);
1492 /* Search STRING for an unquoted ; that is not after an unquoted #. */
1494 static char *
1495 find_semicolon (string)
1496 char *string;
1498 char *match = find_char_unquote (string, ";#", 0);
1499 if (match != 0 && *match == '#')
1500 /* We found a comment before a semicolon. No match. */
1501 match = 0;
1502 return match;
1505 /* Parse a string into a sequence of filenames represented as a
1506 chain of struct nameseq's in reverse order and return that chain.
1508 The string is passed as STRINGP, the address of a string pointer.
1509 The string pointer is updated to point at the first character
1510 not parsed, which either is a null char or equals STOPCHAR.
1512 SIZE is how big to construct chain elements.
1513 This is useful if we want them actually to be other structures
1514 that have room for additional info.
1516 If STRIP is nonzero, strip `./'s off the beginning. */
1518 struct nameseq *
1519 parse_file_seq (stringp, stopchar, size, strip)
1520 char **stringp;
1521 char stopchar;
1522 unsigned int size;
1523 int strip;
1525 register struct nameseq *new = 0;
1526 register struct nameseq *new1, *lastnew1;
1527 register char *p = *stringp;
1528 char *q;
1529 char *name;
1530 char stopchars[2];
1531 stopchars[0] = stopchar;
1532 stopchars[1] = '\0';
1534 while (1)
1536 /* Skip whitespace; see if any more names are left. */
1537 p = next_token (p);
1538 if (*p == '\0')
1539 break;
1540 if (*p == stopchar)
1541 break;
1542 /* Yes, find end of next name. */
1543 q = p;
1544 p = find_char_unquote (q, stopchars, 1);
1545 #ifdef __MSDOS__
1546 /* For MS-DOS, skip a "C:\...". */
1547 if (stopchar == ':' && p != 0 && p[1] == '\\' && isalpha (p[-1]))
1548 p = 0;
1549 #endif
1550 if (p == 0)
1551 p = q + strlen (q);
1553 if (strip)
1554 /* Skip leading `./'s. */
1555 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1557 q += 2; /* Skip "./". */
1558 while (q < p && *q == '/')
1559 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1560 ++q;
1563 /* Extract the filename just found, and skip it. */
1565 if (q == p)
1566 /* ".///" was stripped to "". */
1567 name = savestring ("./", 2);
1568 else
1569 name = savestring (q, p - q);
1571 /* Add it to the front of the chain. */
1572 new1 = (struct nameseq *) xmalloc (size);
1573 new1->name = name;
1574 new1->next = new;
1575 new = new1;
1578 #ifndef NO_ARCHIVES
1580 /* Look for multi-word archive references.
1581 They are indicated by a elt ending with an unmatched `)' and
1582 an elt further down the chain (i.e., previous in the file list)
1583 with an unmatched `(' (e.g., "lib(mem"). */
1585 new1 = new;
1586 lastnew1 = 0;
1587 while (new1 != 0)
1588 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1589 && new1->name[strlen (new1->name) - 1] == ')'
1590 && index (new1->name, '(') == 0)
1592 /* NEW1 ends with a `)' but does not contain a `('.
1593 Look back for an elt with an opening `(' but no closing `)'. */
1595 struct nameseq *n = new1->next, *lastn = new1;
1596 char *paren;
1597 while (n != 0 && (paren = index (n->name, '(')) == 0)
1599 lastn = n;
1600 n = n->next;
1602 if (n != 0
1603 /* Ignore something starting with `(', as that cannot actually
1604 be an archive-member reference (and treating it as such
1605 results in an empty file name, which causes much lossage). */
1606 && n->name[0] != '(')
1608 /* N is the first element in the archive group.
1609 Its name looks like "lib(mem" (with no closing `)'). */
1611 char *libname;
1613 /* Copy "lib(" into LIBNAME. */
1614 ++paren;
1615 libname = (char *) alloca (paren - n->name + 1);
1616 bcopy (n->name, libname, paren - n->name);
1617 libname[paren - n->name] = '\0';
1619 if (*paren == '\0')
1621 /* N was just "lib(", part of something like "lib( a b)".
1622 Edit it out of the chain and free its storage. */
1623 lastn->next = n->next;
1624 free (n->name);
1625 free ((char *) n);
1626 /* LASTN->next is the new stopping elt for the loop below. */
1627 n = lastn->next;
1629 else
1631 /* Replace N's name with the full archive reference. */
1632 name = concat (libname, paren, ")");
1633 free (n->name);
1634 n->name = name;
1637 if (new1->name[1] == '\0')
1639 /* NEW1 is just ")", part of something like "lib(a b )".
1640 Omit it from the chain and free its storage. */
1641 if (lastnew1 == 0)
1642 new = new1->next;
1643 else
1644 lastnew1->next = new1->next;
1645 lastn = new1;
1646 new1 = new1->next;
1647 free (lastn->name);
1648 free ((char *) lastn);
1650 else
1652 /* Replace also NEW1->name, which already has closing `)'. */
1653 name = concat (libname, new1->name, "");
1654 free (new1->name);
1655 new1->name = name;
1656 new1 = new1->next;
1659 /* Trace back from NEW1 (the end of the list) until N
1660 (the beginning of the list), rewriting each name
1661 with the full archive reference. */
1663 while (new1 != n)
1665 name = concat (libname, new1->name, ")");
1666 free (new1->name);
1667 new1->name = name;
1668 lastnew1 = new1;
1669 new1 = new1->next;
1672 else
1674 /* No frobnication happening. Just step down the list. */
1675 lastnew1 = new1;
1676 new1 = new1->next;
1679 else
1681 lastnew1 = new1;
1682 new1 = new1->next;
1685 #endif
1687 *stringp = p;
1688 return new;
1691 /* Read a line of text from STREAM into LINEBUFFER.
1692 Combine continuation lines into one line.
1693 Return the number of actual lines read (> 1 if hacked continuation lines).
1696 static unsigned int
1697 readline (linebuffer, stream, filename, lineno)
1698 struct linebuffer *linebuffer;
1699 FILE *stream;
1700 char *filename;
1701 unsigned int lineno;
1703 char *buffer = linebuffer->buffer;
1704 register char *p = linebuffer->buffer;
1705 register char *end = p + linebuffer->size;
1706 register int len, lastlen = 0;
1707 register char *p2;
1708 register unsigned int nlines = 0;
1709 register int backslash;
1711 *p = '\0';
1713 while (fgets (p, end - p, stream) != 0)
1715 len = strlen (p);
1716 if (len == 0)
1718 /* This only happens when the first thing on the line is a '\0'.
1719 It is a pretty hopeless case, but (wonder of wonders) Athena
1720 lossage strikes again! (xmkmf puts NULs in its makefiles.)
1721 There is nothing really to be done; we synthesize a newline so
1722 the following line doesn't appear to be part of this line. */
1723 makefile_error (filename, lineno,
1724 "warning: NUL character seen; rest of line ignored");
1725 p[0] = '\n';
1726 len = 1;
1729 p += len;
1730 if (p[-1] != '\n')
1732 /* Probably ran out of buffer space. */
1733 register unsigned int p_off = p - buffer;
1734 linebuffer->size *= 2;
1735 buffer = (char *) xrealloc (buffer, linebuffer->size);
1736 p = buffer + p_off;
1737 end = buffer + linebuffer->size;
1738 linebuffer->buffer = buffer;
1739 *p = '\0';
1740 lastlen = len;
1741 continue;
1744 ++nlines;
1746 if (len == 1 && p > buffer)
1747 /* P is pointing at a newline and it's the beginning of
1748 the buffer returned by the last fgets call. However,
1749 it is not necessarily the beginning of a line if P is
1750 pointing past the beginning of the holding buffer.
1751 If the buffer was just enlarged (right before the newline),
1752 we must account for that, so we pretend that the two lines
1753 were one line. */
1754 len += lastlen;
1755 lastlen = len;
1756 backslash = 0;
1757 for (p2 = p - 2; --len > 0; --p2)
1759 if (*p2 == '\\')
1760 backslash = !backslash;
1761 else
1762 break;
1765 if (!backslash)
1767 p[-1] = '\0';
1768 break;
1771 if (end - p <= 1)
1773 /* Enlarge the buffer. */
1774 register unsigned int p_off = p - buffer;
1775 linebuffer->size *= 2;
1776 buffer = (char *) xrealloc (buffer, linebuffer->size);
1777 p = buffer + p_off;
1778 end = buffer + linebuffer->size;
1779 linebuffer->buffer = buffer;
1783 if (ferror (stream))
1784 pfatal_with_name (filename);
1786 return nlines;
1789 /* Construct the list of include directories
1790 from the arguments and the default list. */
1792 void
1793 construct_include_path (arg_dirs)
1794 char **arg_dirs;
1796 register unsigned int i;
1797 struct stat stbuf;
1799 /* Table to hold the dirs. */
1801 register unsigned int defsize = (sizeof (default_include_directories)
1802 / sizeof (default_include_directories[0]));
1803 register unsigned int max = 5;
1804 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
1805 register unsigned int idx = 0;
1807 /* First consider any dirs specified with -I switches.
1808 Ignore dirs that don't exist. */
1810 if (arg_dirs != 0)
1811 while (*arg_dirs != 0)
1813 char *dir = *arg_dirs++;
1815 if (dir[0] == '~')
1817 char *expanded = tilde_expand (dir);
1818 if (expanded != 0)
1819 dir = expanded;
1822 if (safe_stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
1824 if (idx == max - 1)
1826 max += 5;
1827 dirs = (char **)
1828 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
1830 dirs[idx++] = dir;
1832 else if (dir != arg_dirs[-1])
1833 free (dir);
1836 /* Now add at the end the standard default dirs. */
1838 for (i = 0; default_include_directories[i] != 0; ++i)
1839 if (safe_stat (default_include_directories[i], &stbuf) == 0
1840 && S_ISDIR (stbuf.st_mode))
1841 dirs[idx++] = default_include_directories[i];
1843 dirs[idx] = 0;
1845 /* Now compute the maximum length of any name in it. */
1847 max_incl_len = 0;
1848 for (i = 0; i < idx; ++i)
1850 unsigned int len = strlen (dirs[i]);
1851 /* If dir name is written with a trailing slash, discard it. */
1852 if (dirs[i][len - 1] == '/')
1853 /* We can't just clobber a null in because it may have come from
1854 a literal string and literal strings may not be writable. */
1855 dirs[i] = savestring (dirs[i], len - 1);
1856 if (len > max_incl_len)
1857 max_incl_len = len;
1860 include_directories = dirs;
1863 /* Expand ~ or ~USER at the beginning of NAME.
1864 Return a newly malloc'd string or 0. */
1866 char *
1867 tilde_expand (name)
1868 char *name;
1870 if (name[1] == '/' || name[1] == '\0')
1872 extern char *getenv ();
1873 char *home_dir;
1874 int is_variable;
1877 /* Turn off --warn-undefined-variables while we expand HOME. */
1878 int save = warn_undefined_variables_flag;
1879 warn_undefined_variables_flag = 0;
1881 home_dir = allocated_variable_expand ("$(HOME)");
1883 warn_undefined_variables_flag = save;
1886 is_variable = home_dir[0] != '\0';
1887 if (!is_variable)
1889 free (home_dir);
1890 home_dir = getenv ("HOME");
1892 if (home_dir == 0 || home_dir[0] == '\0')
1894 extern char *getlogin ();
1895 char *name = getlogin ();
1896 home_dir = 0;
1897 if (name != 0)
1899 struct passwd *p = getpwnam (name);
1900 if (p != 0)
1901 home_dir = p->pw_dir;
1904 if (home_dir != 0)
1906 char *new = concat (home_dir, "", name + 1);
1907 if (is_variable)
1908 free (home_dir);
1909 return new;
1912 else
1914 struct passwd *pwent;
1915 char *userend = index (name + 1, '/');
1916 if (userend != 0)
1917 *userend = '\0';
1918 pwent = getpwnam (name + 1);
1919 if (pwent != 0)
1921 if (userend == 0)
1922 return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
1923 else
1924 return concat (pwent->pw_dir, "/", userend + 1);
1926 else if (userend != 0)
1927 *userend = '/';
1930 return 0;
1933 /* Given a chain of struct nameseq's describing a sequence of filenames,
1934 in reverse of the intended order, return a new chain describing the
1935 result of globbing the filenames. The new chain is in forward order.
1936 The links of the old chain are freed or used in the new chain.
1937 Likewise for the names in the old chain.
1939 SIZE is how big to construct chain elements.
1940 This is useful if we want them actually to be other structures
1941 that have room for additional info. */
1943 struct nameseq *
1944 multi_glob (chain, size)
1945 struct nameseq *chain;
1946 unsigned int size;
1948 register struct nameseq *new = 0;
1949 register struct nameseq *old;
1950 struct nameseq *nexto;
1952 for (old = chain; old != 0; old = nexto)
1954 glob_t gl;
1955 #ifndef NO_ARCHIVES
1956 char *memname;
1957 #endif
1959 nexto = old->next;
1961 if (old->name[0] == '~')
1963 char *newname = tilde_expand (old->name);
1964 if (newname != 0)
1966 free (old->name);
1967 old->name = newname;
1971 #ifndef NO_ARCHIVES
1972 if (ar_name (old->name))
1974 /* OLD->name is an archive member reference.
1975 Replace it with the archive file name,
1976 and save the member name in MEMNAME.
1977 We will glob on the archive name and then
1978 reattach MEMNAME later. */
1979 char *arname;
1980 ar_parse_name (old->name, &arname, &memname);
1981 free (old->name);
1982 old->name = arname;
1984 else
1985 memname = 0;
1986 #endif
1988 switch (glob (old->name, GLOB_NOCHECK, NULL, &gl))
1990 case 0: /* Success. */
1992 register int i = gl.gl_pathc;
1993 while (i-- > 0)
1995 #ifndef NO_ARCHIVES
1996 if (memname != 0)
1998 /* Try to glob on MEMNAME within the archive. */
1999 struct nameseq *found
2000 = ar_glob (gl.gl_pathv[i], memname, size);
2001 if (found == 0)
2003 /* No matches. Use MEMNAME as-is. */
2004 struct nameseq *elt
2005 = (struct nameseq *) xmalloc (size);
2006 unsigned int alen = strlen (gl.gl_pathv[i]);
2007 unsigned int mlen = strlen (memname);
2008 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2009 bcopy (gl.gl_pathv[i], elt->name, alen);
2010 elt->name[alen] = '(';
2011 bcopy (memname, &elt->name[alen + 1], mlen);
2012 elt->name[alen + 1 + mlen] = ')';
2013 elt->name[alen + 1 + mlen + 1] = '\0';
2014 elt->next = new;
2015 new = elt;
2017 else
2019 /* Find the end of the FOUND chain. */
2020 struct nameseq *f = found;
2021 while (f->next != 0)
2022 f = f->next;
2024 /* Attach the chain being built to the end of the FOUND
2025 chain, and make FOUND the new NEW chain. */
2026 f->next = new;
2027 new = found;
2030 free (memname);
2032 else
2033 #endif
2035 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2036 elt->name = savestring (gl.gl_pathv[i],
2037 strlen (gl.gl_pathv[i]));
2038 elt->next = new;
2039 new = elt;
2042 globfree (&gl);
2043 free (old->name);
2044 free (old);
2045 break;
2048 case GLOB_NOSPACE:
2049 fatal ("virtual memory exhausted");
2050 break;
2052 default:
2053 old->next = new;
2054 new = old;
2055 break;
2059 return new;