(GETOPT, GETOPT_SRC, GLOB): Variables removed.
[make.git] / read.c
blobedbaf48e42a0d14c92f85014519dfca27280215d
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 ();
42 /* A `struct linebuffer' is a structure which holds a line of text.
43 `readline' reads a line from a stream into a linebuffer
44 and works regardless of the length of the line. */
46 struct linebuffer
48 /* Note: This is the number of bytes malloc'ed for `buffer'
49 It does not indicate `buffer's real length.
50 Instead, a null char indicates end-of-string. */
51 unsigned int size;
52 char *buffer;
55 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
56 #define freebuffer(lb) free ((lb)->buffer)
59 /* A `struct conditionals' contains the information describing
60 all the active conditionals in a makefile.
62 The global variable `conditionals' contains the conditionals
63 information for the current makefile. It is initialized from
64 the static structure `toplevel_conditionals' and is later changed
65 to new structures for included makefiles. */
67 struct conditionals
69 unsigned int if_cmds; /* Depth of conditional nesting. */
70 unsigned int allocated; /* Elts allocated in following arrays. */
71 char *ignoring; /* Are we ignoring or interepreting? */
72 char *seen_else; /* Have we already seen an `else'? */
75 static struct conditionals toplevel_conditionals;
76 static struct conditionals *conditionals = &toplevel_conditionals;
79 /* Default directories to search for include files in */
81 static char *default_include_directories[] =
83 INCLUDEDIR,
84 "/usr/gnu/include",
85 "/usr/local/include",
86 "/usr/include",
90 /* List of directories to search for include files in */
92 static char **include_directories;
94 /* Maximum length of an element of the above. */
96 static unsigned int max_incl_len;
98 /* The filename and pointer to line number of the
99 makefile currently being read in. */
101 char *reading_filename;
102 unsigned int *reading_lineno_ptr;
104 /* The chain of makefiles read by read_makefile. */
106 static struct dep *read_makefiles = 0;
108 /* Read in all the makefiles and return the chain of their names. */
110 struct dep *
111 read_all_makefiles (makefiles)
112 char **makefiles;
114 unsigned int num_makefiles = 0;
116 if (debug_flag)
117 puts ("Reading makefiles...");
119 /* If there's a non-null variable MAKEFILES, its value is a list of
120 files to read first thing. But don't let it prevent reading the
121 default makefiles and don't let the default goal come from there. */
124 char *value;
125 char *name, *p;
126 unsigned int length;
129 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
130 int save = warn_undefined_variables_flag;
131 warn_undefined_variables_flag = 0;
133 value = allocated_variable_expand ("$(MAKEFILES)");
135 warn_undefined_variables_flag = save;
138 /* Set NAME to the start of next token and LENGTH to its length.
139 MAKEFILES is updated for finding remaining tokens. */
140 p = value;
141 while ((name = find_next_token (&p, &length)) != 0)
143 if (*p != '\0')
144 *p++ = '\0';
145 (void) read_makefile (name,
146 RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
149 free (value);
152 /* Read makefiles specified with -f switches. */
154 if (makefiles != 0)
155 while (*makefiles != 0)
157 struct dep *tail = read_makefiles;
158 register struct dep *d;
160 if (! read_makefile (*makefiles, 0))
161 perror_with_name ("", *makefiles);
163 /* Find the right element of read_makefiles. */
164 d = read_makefiles;
165 while (d->next != tail)
166 d = d->next;
168 /* Use the storage read_makefile allocates. */
169 *makefiles = dep_name (d);
170 ++num_makefiles;
171 ++makefiles;
174 /* If there were no -f switches, try the default names. */
176 if (num_makefiles == 0)
178 static char *default_makefiles[] =
179 { "GNUmakefile", "makefile", "Makefile", 0 };
180 register char **p = default_makefiles;
181 while (*p != 0 && !file_exists_p (*p))
182 ++p;
184 if (*p != 0)
186 if (! read_makefile (*p, 0))
187 perror_with_name ("", *p);
189 else
191 /* No default makefile was found. Add the default makefiles to the
192 `read_makefiles' chain so they will be updated if possible. */
193 struct dep *tail = read_makefiles;
194 /* Add them to the tail, after any MAKEFILES variable makefiles. */
195 while (tail != 0 && tail->next != 0)
196 tail = tail->next;
197 for (p = default_makefiles; *p != 0; ++p)
199 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
200 d->name = 0;
201 d->file = enter_file (*p);
202 d->file->dontcare = 1;
203 /* Tell update_goal_chain to bail out as soon as this file is
204 made, and main not to die if we can't make this file. */
205 d->changed = RM_DONTCARE;
206 if (tail == 0)
207 read_makefiles = d;
208 else
209 tail->next = d;
210 tail = d;
212 if (tail != 0)
213 tail->next = 0;
217 return read_makefiles;
220 /* Read file FILENAME as a makefile and add its contents to the data base.
222 FLAGS contains bits as above.
224 FILENAME is added to the `read_makefiles' chain.
226 Returns 1 if a file was found and read, 0 if not. */
228 static int
229 read_makefile (filename, flags)
230 char *filename;
231 int flags;
233 static char *collapsed = 0;
234 static unsigned int collapsed_length = 0;
235 register FILE *infile;
236 struct linebuffer lb;
237 unsigned int commands_len = 200;
238 char *commands = (char *) xmalloc (200);
239 unsigned int commands_idx = 0;
240 unsigned int commands_started;
241 register char *p;
242 char *p2;
243 int ignoring = 0, in_ignored_define = 0;
244 int no_targets = 0; /* Set when reading a rule without targets. */
245 char *passed_filename = filename;
247 struct nameseq *filenames = 0;
248 struct dep *deps;
249 unsigned int lineno = 1;
250 unsigned int nlines = 0;
251 int two_colon;
252 char *pattern = 0, *pattern_percent;
254 int makefile_errno;
256 #define record_waiting_files() \
257 do \
259 if (filenames != 0) \
260 record_files (filenames, pattern, pattern_percent, deps, \
261 commands_started, commands, commands_idx, \
262 two_colon, filename, lineno, \
263 !(flags & RM_NO_DEFAULT_GOAL)); \
264 filenames = 0; \
265 commands_idx = 0; \
266 pattern = 0; \
267 } while (0)
269 #ifdef lint /* Suppress `used before set' messages. */
270 two_colon = 0;
271 #endif
273 if (debug_flag)
275 printf ("Reading makefile `%s'", filename);
276 if (flags & RM_NO_DEFAULT_GOAL)
277 printf (" (no default goal)");
278 if (flags & RM_INCLUDED)
279 printf (" (search path)");
280 if (flags & RM_DONTCARE)
281 printf (" (don't care)");
282 if (flags & RM_NO_TILDE)
283 printf (" (no ~ expansion)");
284 puts ("...");
287 /* First, get a stream to read. */
289 /* Expand ~ in FILENAME unless it came from `include',
290 in which case it was already done. */
291 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
293 char *expanded = tilde_expand (filename);
294 if (expanded != 0)
295 filename = expanded;
298 infile = fopen (filename, "r");
299 /* Save the error code so we print the right message later. */
300 makefile_errno = errno;
302 /* If the makefile wasn't found and it's either a makefile from
303 the `MAKEFILES' variable or an included makefile,
304 search the included makefile search path for this makefile. */
306 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
308 register unsigned int i;
309 for (i = 0; include_directories[i] != 0; ++i)
311 char *name = concat (include_directories[i], "/", filename);
312 infile = fopen (name, "r");
313 if (infile == 0)
314 free (name);
315 else
317 filename = name;
318 break;
323 /* Add FILENAME to the chain of read makefiles. */
324 deps = (struct dep *) xmalloc (sizeof (struct dep));
325 deps->next = read_makefiles;
326 read_makefiles = deps;
327 deps->name = 0;
328 deps->file = lookup_file (filename);
329 if (deps->file == 0)
331 deps->file = enter_file (savestring (filename, strlen (filename)));
332 if (flags & RM_DONTCARE)
333 deps->file->dontcare = 1;
335 if (filename != passed_filename)
336 free (filename);
337 filename = deps->file->name;
338 deps->changed = flags;
339 deps = 0;
341 /* If the makefile can't be found at all, give up entirely. */
343 if (infile == 0)
345 /* If we did some searching, errno has the error from the last
346 attempt, rather from FILENAME itself. Restore it in case the
347 caller wants to use it in a message. */
348 errno = makefile_errno;
349 return 0;
352 reading_filename = filename;
353 reading_lineno_ptr = &lineno;
355 /* Loop over lines in the file.
356 The strategy is to accumulate target names in FILENAMES, dependencies
357 in DEPS and commands in COMMANDS. These are used to define a rule
358 when the start of the next rule (or eof) is encountered. */
360 initbuffer (&lb);
362 while (!feof (infile))
364 lineno += nlines;
365 nlines = readline (&lb, infile, filename, lineno);
367 /* Check for a shell command line first.
368 If it is not one, we can stop treating tab specially. */
369 if (lb.buffer[0] == '\t')
371 /* This line is a probably shell command. */
372 unsigned int len;
374 if (no_targets)
375 /* Ignore the commands in a rule with no targets. */
376 continue;
378 /* If there is no preceding rule line, don't treat this line
379 as a command, even though it begins with a tab character.
380 SunOS 4 make appears to behave this way. */
382 if (filenames != 0)
384 if (ignoring)
385 /* Yep, this is a shell command, and we don't care. */
386 continue;
388 /* Append this command line to the line being accumulated. */
389 p = lb.buffer;
390 if (commands_idx == 0)
391 commands_started = lineno;
392 len = strlen (p);
393 if (len + 1 + commands_idx > commands_len)
395 commands_len = (len + 1 + commands_idx) * 2;
396 commands = (char *) xrealloc (commands, commands_len);
398 bcopy (p, &commands[commands_idx], len);
399 commands_idx += len;
400 commands[commands_idx++] = '\n';
402 continue;
406 /* This line is not a shell command line. Don't worry about tabs. */
408 if (collapsed_length < lb.size)
410 collapsed_length = lb.size;
411 if (collapsed != 0)
412 free (collapsed);
413 collapsed = (char *) xmalloc (collapsed_length);
415 strcpy (collapsed, lb.buffer);
416 /* Collapse continuation lines. */
417 collapse_continuations (collapsed);
418 remove_comments (collapsed);
420 /* strncmp is first to avoid dereferencing out into space. */
421 #define word1eq(s, l) (!strncmp (s, p, l) \
422 && (p[l] == '\0' || isblank (p[l])))
423 p = collapsed;
424 while (isspace (*p))
425 ++p;
426 if (*p == '\0')
427 /* This line is completely empty. */
428 continue;
430 /* We must first check for conditional and `define' directives before
431 ignoring anything, since they control what we will do with
432 following lines. */
434 if (!in_ignored_define
435 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
436 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
437 || word1eq ("else", 4) || word1eq ("endif", 5)))
439 int i = conditional_line (p, filename, lineno);
440 if (i >= 0)
441 ignoring = i;
442 else
443 makefile_fatal (filename, lineno,
444 "invalid syntax in conditional");
445 continue;
447 else if (word1eq ("endef", 5))
449 if (in_ignored_define)
450 in_ignored_define = 0;
451 else
452 makefile_fatal (filename, lineno, "extraneous `endef'");
453 continue;
455 else if (word1eq ("define", 6))
457 if (ignoring)
458 in_ignored_define = 1;
459 else
461 p2 = next_token (p + 6);
462 /* Let the variable name be the whole rest of the line,
463 with trailing blanks stripped (comments have already been
464 removed), so it could be a complex variable/function
465 reference that might contain blanks. */
466 p = index (p2, '\0');
467 while (isblank (p[-1]))
468 --p;
469 lineno = do_define (p2, p - p2, o_file,
470 lineno, infile, filename);
472 continue;
474 else if (word1eq ("override", 8))
476 p2 = next_token (p + 8);
477 if (p2 == 0)
478 makefile_error (filename, lineno, "empty `override' directive");
479 if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
481 if (ignoring)
482 in_ignored_define = 1;
483 else
485 p2 = next_token (p2 + 6);
486 /* Let the variable name be the whole rest of the line,
487 with trailing blanks stripped (comments have already been
488 removed), so it could be a complex variable/function
489 reference that might contain blanks. */
490 p = index (p2, '\0');
491 while (isblank (p[-1]))
492 --p;
493 lineno = do_define (p2, p - p2, o_override,
494 lineno, infile, filename);
497 else if (!ignoring
498 && !try_variable_definition (filename, lineno,
499 p2, o_override))
500 makefile_error (filename, lineno, "empty `override' directive");
502 continue;
505 if (ignoring)
506 /* Ignore the line. We continue here so conditionals
507 can appear in the middle of a rule. */
508 continue;
509 else if (word1eq ("export", 6))
511 struct variable *v;
512 p2 = next_token (p + 6);
513 if (*p2 == '\0')
514 export_all_variables = 1;
515 v = try_variable_definition (filename, lineno, p2, o_file);
516 if (v != 0)
517 v->export = v_export;
518 else
520 unsigned int len;
521 for (p = find_next_token (&p2, &len); p != 0;
522 p = find_next_token (&p2, &len))
524 v = lookup_variable (p, len);
525 if (v == 0)
526 v = define_variable (p, len, "", o_file, 0);
527 v->export = v_export;
531 else if (word1eq ("unexport", 8))
533 unsigned int len;
534 struct variable *v;
535 p2 = next_token (p + 8);
536 if (*p2 == '\0')
537 export_all_variables = 0;
538 for (p = find_next_token (&p2, &len); p != 0;
539 p = find_next_token (&p2, &len))
541 v = lookup_variable (p, len);
542 if (v == 0)
543 v = define_variable (p, len, "", o_file, 0);
544 v->export = v_noexport;
547 else if (word1eq ("include", 7) || word1eq ("-include", 8))
549 /* We have found an `include' line specifying a nested
550 makefile to be read at this point. */
551 struct conditionals *save, new_conditionals;
552 struct nameseq *files;
553 /* "-include" (vs "include") says no
554 error if the file does not exist. */
555 int noerror = p[0] == '-';
557 p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
558 if (*p == '\0')
560 makefile_error (filename, lineno,
561 "no file name for `%sinclude'",
562 noerror ? "-" : "");
563 continue;
566 /* Parse the list of file names. */
567 p2 = p;
568 files = multi_glob (parse_file_seq (&p2, '\0',
569 sizeof (struct nameseq),
571 sizeof (struct nameseq));
572 free (p);
574 /* Save the state of conditionals and start
575 the included makefile with a clean slate. */
576 save = conditionals;
577 bzero ((char *) &new_conditionals, sizeof new_conditionals);
578 conditionals = &new_conditionals;
580 /* Record the rules that are waiting so they will determine
581 the default goal before those in the included makefile. */
582 record_waiting_files ();
584 /* Read each included makefile. */
585 while (files != 0)
587 struct nameseq *next = files->next;
588 char *name = files->name;
589 free (files);
590 files = next;
592 if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
593 | (noerror ? RM_DONTCARE : 0)))
594 && ! noerror)
595 makefile_error (filename, lineno,
596 "%s: %s", name, strerror (errno));
599 /* Free any space allocated by conditional_line. */
600 if (conditionals->ignoring)
601 free (conditionals->ignoring);
602 if (conditionals->seen_else)
603 free (conditionals->seen_else);
605 /* Restore state. */
606 conditionals = save;
607 reading_filename = filename;
608 reading_lineno_ptr = &lineno;
610 else if (word1eq ("vpath", 5))
612 char *pattern;
613 unsigned int len;
614 p2 = variable_expand (p + 5);
615 p = find_next_token (&p2, &len);
616 if (p != 0)
618 pattern = savestring (p, len);
619 p = find_next_token (&p2, &len);
620 /* No searchpath means remove all previous
621 selective VPATH's with the same pattern. */
623 else
624 /* No pattern means remove all previous selective VPATH's. */
625 pattern = 0;
626 construct_vpath_list (pattern, p);
627 if (pattern != 0)
628 free (pattern);
630 #undef word1eq
631 else if (try_variable_definition (filename, lineno, p, o_file))
632 /* This line has been dealt with. */
634 else if (lb.buffer[0] == '\t')
636 p = collapsed; /* Ignore comments. */
637 while (isblank (*p))
638 ++p;
639 if (*p == '\0')
640 /* The line is completely blank; that is harmless. */
641 continue;
642 /* This line starts with a tab but was not caught above
643 because there was no preceding target, and the line
644 might have been usable as a variable definition.
645 But now it is definitely lossage. */
646 makefile_fatal (filename, lineno,
647 "commands commence before first target");
649 else
651 /* This line describes some target files. */
653 char *cmdleft;
655 /* Record the previous rule. */
657 record_waiting_files ();
659 /* Search the line for an unquoted ; that is not after an
660 unquoted #. */
661 cmdleft = find_char_unquote (lb.buffer, ";#", 0);
662 if (cmdleft != 0 && *cmdleft == '#')
664 /* We found a comment before a semicolon. */
665 *cmdleft = '\0';
666 cmdleft = 0;
668 else if (cmdleft != 0)
669 /* Found one. Cut the line short there before expanding it. */
670 *cmdleft = '\0';
672 collapse_continuations (lb.buffer);
674 /* Expand variable and function references before doing anything
675 else so that special characters can be inside variables. */
676 p = variable_expand (lb.buffer);
678 if (cmdleft == 0)
679 /* Look for a semicolon in the expanded line. */
680 cmdleft = find_char_unquote (p, ";", 0);
682 if (cmdleft != 0)
683 /* Cut the line short at the semicolon. */
684 *cmdleft = '\0';
686 p2 = next_token (p);
687 if (*p2 == '\0')
689 if (cmdleft != 0)
690 makefile_fatal (filename, lineno,
691 "missing rule before commands");
692 else
693 /* This line contained a variable reference that
694 expanded to nothing but whitespace. */
695 continue;
697 else if (*p2 == ':')
699 /* We accept and ignore rules without targets for
700 compatibility with SunOS 4 make. */
701 no_targets = 1;
702 continue;
705 filenames = multi_glob (parse_file_seq (&p2, ':',
706 sizeof (struct nameseq),
708 sizeof (struct nameseq));
709 if (*p2++ == '\0')
710 makefile_fatal (filename, lineno, "missing separator");
711 /* Is this a one-colon or two-colon entry? */
712 two_colon = *p2 == ':';
713 if (two_colon)
714 p2++;
716 /* We have some targets, so don't ignore the following commands. */
717 no_targets = 0;
719 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
720 p = index (p2, ':');
721 while (p != 0 && p[-1] == '\\')
723 register char *q = &p[-1];
724 register int backslash = 0;
725 while (*q-- == '\\')
726 backslash = !backslash;
727 if (backslash)
728 p = index (p + 1, ':');
729 else
730 break;
732 #ifdef __MSDOS__
733 /* For MS-DOS, skip a "C:\...". */
734 if (p != 0 && p[1] == '\\' && isalpha (p[-1]))
735 p = 0;
736 #endif
737 if (p != 0)
739 struct nameseq *target;
740 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
741 ++p2;
742 if (target == 0)
743 makefile_fatal (filename, lineno, "missing target pattern");
744 else if (target->next != 0)
745 makefile_fatal (filename, lineno, "multiple target patterns");
746 pattern = target->name;
747 pattern_percent = find_percent (pattern);
748 if (pattern_percent == 0)
749 makefile_fatal (filename, lineno,
750 "target pattern contains no `%%'");
752 else
753 pattern = 0;
755 /* Parse the dependencies. */
756 deps = (struct dep *)
757 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
758 sizeof (struct dep));
760 commands_idx = 0;
761 if (cmdleft != 0)
763 /* Semicolon means rest of line is a command. */
764 unsigned int len = strlen (cmdleft + 1);
766 commands_started = lineno;
768 /* Add this command line to the buffer. */
769 if (len + 2 > commands_len)
771 commands_len = (len + 2) * 2;
772 commands = (char *) xrealloc (commands, commands_len);
774 bcopy (cmdleft + 1, commands, len);
775 commands_idx += len;
776 commands[commands_idx++] = '\n';
779 continue;
782 /* We get here except in the case that we just read a rule line.
783 Record now the last rule we read, so following spurious
784 commands are properly diagnosed. */
785 record_waiting_files ();
786 no_targets = 0;
789 if (conditionals->if_cmds)
790 makefile_fatal (filename, lineno, "missing `endif'");
792 /* At eof, record the last rule. */
793 record_waiting_files ();
795 freebuffer (&lb);
796 free ((char *) commands);
797 fclose (infile);
799 reading_filename = 0;
800 reading_lineno_ptr = 0;
802 return 1;
805 /* Execute a `define' directive.
806 The first line has already been read, and NAME is the name of
807 the variable to be defined. The following lines remain to be read.
808 LINENO, INFILE and FILENAME refer to the makefile being read.
809 The value returned is LINENO, updated for lines read here. */
811 static unsigned int
812 do_define (name, namelen, origin, lineno, infile, filename)
813 char *name;
814 unsigned int namelen;
815 enum variable_origin origin;
816 unsigned int lineno;
817 FILE *infile;
818 char *filename;
820 struct linebuffer lb;
821 unsigned int nlines = 0;
822 unsigned int length = 100;
823 char *definition = (char *) xmalloc (100);
824 register unsigned int idx = 0;
825 register char *p;
827 /* Expand the variable name. */
828 char *var = (char *) alloca (namelen + 1);
829 bcopy (name, var, namelen);
830 var[namelen] = '\0';
831 var = variable_expand (var);
833 initbuffer (&lb);
834 while (!feof (infile))
836 lineno += nlines;
837 nlines = readline (&lb, infile, filename, lineno);
839 collapse_continuations (lb.buffer);
841 p = next_token (lb.buffer);
842 if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
844 p += 5;
845 remove_comments (p);
846 if (*next_token (p) != '\0')
847 makefile_error (filename, lineno,
848 "Extraneous text after `endef' directive");
849 /* Define the variable. */
850 if (idx == 0)
851 definition[0] = '\0';
852 else
853 definition[idx - 1] = '\0';
854 (void) define_variable (var, strlen (var), definition, origin, 1);
855 free (definition);
856 freebuffer (&lb);
857 return lineno;
859 else
861 unsigned int len = strlen (lb.buffer);
863 /* Increase the buffer size if necessary. */
864 if (idx + len + 1 > length)
866 length = (idx + len) * 2;
867 definition = (char *) xrealloc (definition, length + 1);
870 bcopy (lb.buffer, &definition[idx], len);
871 idx += len;
872 /* Separate lines with a newline. */
873 definition[idx++] = '\n';
877 /* No `endef'!! */
878 makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
880 /* NOTREACHED */
881 return 0;
884 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
885 "ifneq", "else" and "endif".
886 LINE is the input line, with the command as its first word.
888 FILENAME and LINENO are the filename and line number in the
889 current makefile. They are used for error messages.
891 Value is -1 if the line is invalid,
892 0 if following text should be interpreted,
893 1 if following text should be ignored. */
895 static int
896 conditional_line (line, filename, lineno)
897 char *line;
898 char *filename;
899 unsigned int lineno;
901 int notdef;
902 char *cmdname;
903 register unsigned int i;
905 if (*line == 'i')
907 /* It's an "if..." command. */
908 notdef = line[2] == 'n';
909 if (notdef)
911 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
912 line += cmdname[3] == 'd' ? 7 : 6;
914 else
916 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
917 line += cmdname[2] == 'd' ? 6 : 5;
920 else
922 /* It's an "else" or "endif" command. */
923 notdef = line[1] == 'n';
924 cmdname = notdef ? "endif" : "else";
925 line += notdef ? 5 : 4;
928 line = next_token (line);
930 if (*cmdname == 'e')
932 if (*line != '\0')
933 makefile_error (filename, lineno,
934 "Extraneous text after `%s' directive",
935 cmdname);
936 /* "Else" or "endif". */
937 if (conditionals->if_cmds == 0)
938 makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
939 /* NOTDEF indicates an `endif' command. */
940 if (notdef)
941 --conditionals->if_cmds;
942 else if (conditionals->seen_else[conditionals->if_cmds - 1])
943 makefile_fatal (filename, lineno, "only one `else' per conditional");
944 else
946 /* Toggle the state of ignorance. */
947 conditionals->ignoring[conditionals->if_cmds - 1]
948 = !conditionals->ignoring[conditionals->if_cmds - 1];
949 /* Record that we have seen an `else' in this conditional.
950 A second `else' will be erroneous. */
951 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
953 for (i = 0; i < conditionals->if_cmds; ++i)
954 if (conditionals->ignoring[i])
955 return 1;
956 return 0;
959 if (conditionals->allocated == 0)
961 conditionals->allocated = 5;
962 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
963 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
966 ++conditionals->if_cmds;
967 if (conditionals->if_cmds > conditionals->allocated)
969 conditionals->allocated += 5;
970 conditionals->ignoring = (char *)
971 xrealloc (conditionals->ignoring, conditionals->allocated);
972 conditionals->seen_else = (char *)
973 xrealloc (conditionals->seen_else, conditionals->allocated);
976 /* Record that we have seen an `if...' but no `else' so far. */
977 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
979 /* Search through the stack to see if we're already ignoring. */
980 for (i = 0; i < conditionals->if_cmds - 1; ++i)
981 if (conditionals->ignoring[i])
983 /* We are already ignoring, so just push a level
984 to match the next "else" or "endif", and keep ignoring.
985 We don't want to expand variables in the condition. */
986 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
987 return 1;
990 if (cmdname[notdef ? 3 : 2] == 'd')
992 /* "Ifdef" or "ifndef". */
993 struct variable *v;
994 register char *p = end_of_token (line);
995 i = p - line;
996 p = next_token (p);
997 if (*p != '\0')
998 return -1;
999 v = lookup_variable (line, i);
1000 conditionals->ignoring[conditionals->if_cmds - 1]
1001 = (v != 0 && *v->value != '\0') == notdef;
1003 else
1005 /* "Ifeq" or "ifneq". */
1006 char *s1, *s2;
1007 unsigned int len;
1008 char termin = *line == '(' ? ',' : *line;
1010 if (termin != ',' && termin != '"' && termin != '\'')
1011 return -1;
1013 s1 = ++line;
1014 /* Find the end of the first string. */
1015 if (termin == ',')
1017 register int count = 0;
1018 for (; *line != '\0'; ++line)
1019 if (*line == '(')
1020 ++count;
1021 else if (*line == ')')
1022 --count;
1023 else if (*line == ',' && count <= 0)
1024 break;
1026 else
1027 while (*line != '\0' && *line != termin)
1028 ++line;
1030 if (*line == '\0')
1031 return -1;
1033 *line++ = '\0';
1035 s2 = variable_expand (s1);
1036 /* We must allocate a new copy of the expanded string because
1037 variable_expand re-uses the same buffer. */
1038 len = strlen (s2);
1039 s1 = (char *) alloca (len + 1);
1040 bcopy (s2, s1, len + 1);
1042 if (termin != ',')
1043 /* Find the start of the second string. */
1044 line = next_token (line);
1046 termin = termin == ',' ? ')' : *line;
1047 if (termin != ')' && termin != '"' && termin != '\'')
1048 return -1;
1050 /* Find the end of the second string. */
1051 if (termin == ')')
1053 register int count = 0;
1054 s2 = next_token (line);
1055 for (line = s2; *line != '\0'; ++line)
1057 if (*line == '(')
1058 ++count;
1059 else if (*line == ')')
1060 if (count <= 0)
1061 break;
1062 else
1063 --count;
1066 else
1068 ++line;
1069 s2 = line;
1070 while (*line != '\0' && *line != termin)
1071 ++line;
1074 if (*line == '\0')
1075 return -1;
1077 *line = '\0';
1078 line = next_token (++line);
1079 if (*line != '\0')
1080 makefile_error (filename, lineno,
1081 "Extraneous text after `%s' directive",
1082 cmdname);
1084 s2 = variable_expand (s2);
1085 conditionals->ignoring[conditionals->if_cmds - 1]
1086 = streq (s1, s2) == notdef;
1089 /* Search through the stack to see if we're ignoring. */
1090 for (i = 0; i < conditionals->if_cmds; ++i)
1091 if (conditionals->ignoring[i])
1092 return 1;
1093 return 0;
1096 /* Remove duplicate dependencies in CHAIN. */
1098 void
1099 uniquize_deps (chain)
1100 struct dep *chain;
1102 register struct dep *d;
1104 /* Make sure that no dependencies are repeated. This does not
1105 really matter for the purpose of updating targets, but it
1106 might make some names be listed twice for $^ and $?. */
1108 for (d = chain; d != 0; d = d->next)
1110 struct dep *last, *next;
1112 last = d;
1113 next = d->next;
1114 while (next != 0)
1115 if (streq (dep_name (d), dep_name (next)))
1117 struct dep *n = next->next;
1118 last->next = n;
1119 if (next->name != 0 && next->name != d->name)
1120 free (next->name);
1121 if (next != d)
1122 free ((char *) next);
1123 next = n;
1125 else
1127 last = next;
1128 next = next->next;
1133 /* Record a description line for files FILENAMES,
1134 with dependencies DEPS, commands to execute described
1135 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1136 TWO_COLON is nonzero if a double colon was used.
1137 If not nil, PATTERN is the `%' pattern to make this
1138 a static pattern rule, and PATTERN_PERCENT is a pointer
1139 to the `%' within it.
1141 The links of FILENAMES are freed, and so are any names in it
1142 that are not incorporated into other data structures. */
1144 static void
1145 record_files (filenames, pattern, pattern_percent, deps, commands_started,
1146 commands, commands_idx, two_colon, filename, lineno, set_default)
1147 struct nameseq *filenames;
1148 char *pattern, *pattern_percent;
1149 struct dep *deps;
1150 unsigned int commands_started;
1151 char *commands;
1152 unsigned int commands_idx;
1153 int two_colon;
1154 char *filename;
1155 unsigned int lineno;
1156 int set_default;
1158 struct nameseq *nextf;
1159 int implicit = 0;
1160 unsigned int max_targets, target_idx;
1161 char **targets = 0, **target_percents = 0;
1162 struct commands *cmds;
1164 if (commands_idx > 0)
1166 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1167 cmds->filename = filename;
1168 cmds->lineno = commands_started;
1169 cmds->commands = savestring (commands, commands_idx);
1170 cmds->command_lines = 0;
1172 else
1173 cmds = 0;
1175 for (; filenames != 0; filenames = nextf)
1177 register char *name = filenames->name;
1178 register struct file *f;
1179 register struct dep *d;
1180 struct dep *this;
1181 char *implicit_percent;
1183 nextf = filenames->next;
1184 free ((char *) filenames);
1186 implicit_percent = find_percent (name);
1187 implicit |= implicit_percent != 0;
1189 if (implicit && pattern != 0)
1190 makefile_fatal (filename, lineno,
1191 "mixed implicit and static pattern rules");
1193 if (implicit && implicit_percent == 0)
1194 makefile_fatal (filename, lineno, "mixed implicit and normal rules");
1196 if (implicit)
1198 if (targets == 0)
1200 max_targets = 5;
1201 targets = (char **) xmalloc (5 * sizeof (char *));
1202 target_percents = (char **) xmalloc (5 * sizeof (char *));
1203 target_idx = 0;
1205 else if (target_idx == max_targets - 1)
1207 max_targets += 5;
1208 targets = (char **) xrealloc ((char *) targets,
1209 max_targets * sizeof (char *));
1210 target_percents
1211 = (char **) xrealloc ((char *) target_percents,
1212 max_targets * sizeof (char *));
1214 targets[target_idx] = name;
1215 target_percents[target_idx] = implicit_percent;
1216 ++target_idx;
1217 continue;
1220 /* If there are multiple filenames, copy the chain DEPS
1221 for all but the last one. It is not safe for the same deps
1222 to go in more than one place in the data base. */
1223 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1225 if (pattern != 0)
1226 /* If this is an extended static rule:
1227 `targets: target%pattern: dep%pattern; cmds',
1228 translate each dependency pattern into a plain filename
1229 using the target pattern and this target's name. */
1230 if (!pattern_matches (pattern, pattern_percent, name))
1232 /* Give a warning if the rule is meaningless. */
1233 makefile_error (filename, lineno,
1234 "target `%s' doesn't match the target pattern",
1235 name);
1236 this = 0;
1238 else
1240 /* We use patsubst_expand to do the work of translating
1241 the target pattern, the target's name and the dependencies'
1242 patterns into plain dependency names. */
1243 char *buffer = variable_expand ("");
1245 for (d = this; d != 0; d = d->next)
1247 char *o;
1248 char *percent = find_percent (d->name);
1249 if (percent == 0)
1250 continue;
1251 o = patsubst_expand (buffer, name, pattern, d->name,
1252 pattern_percent, percent);
1253 free (d->name);
1254 d->name = savestring (buffer, o - buffer);
1258 if (!two_colon)
1260 /* Single-colon. Combine these dependencies
1261 with others in file's existing record, if any. */
1262 f = enter_file (name);
1264 if (f->double_colon)
1265 makefile_fatal (filename, lineno,
1266 "target file `%s' has both : and :: entries",
1267 f->name);
1269 /* If CMDS == F->CMDS, this target was listed in this rule
1270 more than once. Just give a warning since this is harmless. */
1271 if (cmds != 0 && cmds == f->cmds)
1272 makefile_error
1273 (filename, lineno,
1274 "target `%s' given more than once in the same rule.",
1275 f->name);
1277 /* Check for two single-colon entries both with commands.
1278 Check is_target so that we don't lose on files such as .c.o
1279 whose commands were preinitialized. */
1280 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1282 makefile_error (cmds->filename, cmds->lineno,
1283 "warning: overriding commands for target `%s'",
1284 f->name);
1285 makefile_error (f->cmds->filename, f->cmds->lineno,
1286 "warning: ignoring old commands for target `%s'",
1287 f->name);
1290 f->is_target = 1;
1292 /* Defining .DEFAULT with no deps or cmds clears it. */
1293 if (f == default_file && this == 0 && cmds == 0)
1294 f->cmds = 0;
1295 if (cmds != 0)
1296 f->cmds = cmds;
1297 /* Defining .SUFFIXES with no dependencies
1298 clears out the list of suffixes. */
1299 if (f == suffix_file && this == 0)
1301 d = f->deps;
1302 while (d != 0)
1304 struct dep *nextd = d->next;
1305 free (d->name);
1306 free (d);
1307 d = nextd;
1309 f->deps = 0;
1311 else if (f->deps != 0)
1313 /* Add the file's old deps and the new ones in THIS together. */
1315 struct dep *firstdeps, *moredeps;
1316 if (cmds != 0)
1318 /* This is the rule with commands, so put its deps first.
1319 The rationale behind this is that $< expands to the
1320 first dep in the chain, and commands use $< expecting
1321 to get the dep that rule specifies. */
1322 firstdeps = this;
1323 moredeps = f->deps;
1325 else
1327 /* Append the new deps to the old ones. */
1328 firstdeps = f->deps;
1329 moredeps = this;
1332 if (firstdeps == 0)
1333 firstdeps = moredeps;
1334 else
1336 d = firstdeps;
1337 while (d->next != 0)
1338 d = d->next;
1339 d->next = moredeps;
1342 f->deps = firstdeps;
1344 else
1345 f->deps = this;
1347 /* If this is a static pattern rule, set the file's stem to
1348 the part of its name that matched the `%' in the pattern,
1349 so you can use $* in the commands. */
1350 if (pattern != 0)
1352 static char *percent = "%";
1353 char *buffer = variable_expand ("");
1354 char *o = patsubst_expand (buffer, name, pattern, percent,
1355 pattern_percent, percent);
1356 f->stem = savestring (buffer, o - buffer);
1359 else
1361 /* Double-colon. Make a new record
1362 even if the file already has one. */
1363 f = lookup_file (name);
1364 /* Check for both : and :: rules. Check is_target so
1365 we don't lose on default suffix rules or makefiles. */
1366 if (f != 0 && f->is_target && !f->double_colon)
1367 makefile_fatal (filename, lineno,
1368 "target file `%s' has both : and :: entries",
1369 f->name);
1370 f = enter_file (name);
1371 /* If there was an existing entry and it was a double-colon
1372 entry, enter_file will have returned a new one, making it the
1373 prev pointer of the old one, and setting its double_colon
1374 pointer to the first one. */
1375 if (f->double_colon == 0)
1376 /* This is the first entry for this name, so we must
1377 set its double_colon pointer to itself. */
1378 f->double_colon = f;
1379 f->is_target = 1;
1380 f->deps = this;
1381 f->cmds = cmds;
1384 /* Free name if not needed further. */
1385 if (f != 0 && name != f->name
1386 && (name < f->name || name > f->name + strlen (f->name)))
1388 free (name);
1389 name = f->name;
1392 /* See if this is first target seen whose name does
1393 not start with a `.', unless it contains a slash. */
1394 if (default_goal_file == 0 && set_default
1395 && (*name != '.' || index (name, '/') != 0))
1397 int reject = 0;
1399 /* If this file is a suffix, don't
1400 let it be the default goal file. */
1402 for (d = suffix_file->deps; d != 0; d = d->next)
1404 register struct dep *d2;
1405 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1407 reject = 1;
1408 break;
1410 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1412 register unsigned int len = strlen (dep_name (d2));
1413 if (strncmp (name, dep_name (d2), len))
1414 continue;
1415 if (streq (name + len, dep_name (d)))
1417 reject = 1;
1418 break;
1421 if (reject)
1422 break;
1425 if (!reject)
1426 default_goal_file = f;
1430 if (implicit)
1432 targets[target_idx] = 0;
1433 target_percents[target_idx] = 0;
1434 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1435 free ((char *) target_percents);
1439 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1440 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1441 Quoting backslashes are removed from STRING by compacting it into
1442 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1443 one, or nil if there are none. */
1445 char *
1446 find_char_unquote (string, stopchars, blank)
1447 char *string;
1448 char *stopchars;
1449 int blank;
1451 unsigned int string_len = strlen (string);
1452 register char *p = string;
1454 while (1)
1456 while (*p != '\0' && index (stopchars, *p) == 0
1457 && (!blank || !isblank (*p)))
1458 ++p;
1459 if (*p == '\0')
1460 break;
1462 if (p > string && p[-1] == '\\')
1464 /* Search for more backslashes. */
1465 register int i = -2;
1466 while (&p[i] >= string && p[i] == '\\')
1467 --i;
1468 ++i;
1469 /* The number of backslashes is now -I.
1470 Copy P over itself to swallow half of them. */
1471 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1472 p += i / 2;
1473 if (i % 2 == 0)
1474 /* All the backslashes quoted each other; the STOPCHAR was
1475 unquoted. */
1476 return p;
1478 /* The STOPCHAR was quoted by a backslash. Look for another. */
1480 else
1481 /* No backslash in sight. */
1482 return p;
1485 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1486 return 0;
1489 /* Search PATTERN for an unquoted %. */
1491 char *
1492 find_percent (pattern)
1493 char *pattern;
1495 return find_char_unquote (pattern, "%", 0);
1498 /* Parse a string into a sequence of filenames represented as a
1499 chain of struct nameseq's in reverse order and return that chain.
1501 The string is passed as STRINGP, the address of a string pointer.
1502 The string pointer is updated to point at the first character
1503 not parsed, which either is a null char or equals STOPCHAR.
1505 SIZE is how big to construct chain elements.
1506 This is useful if we want them actually to be other structures
1507 that have room for additional info.
1509 If STRIP is nonzero, strip `./'s off the beginning. */
1511 struct nameseq *
1512 parse_file_seq (stringp, stopchar, size, strip)
1513 char **stringp;
1514 char stopchar;
1515 unsigned int size;
1516 int strip;
1518 register struct nameseq *new = 0;
1519 register struct nameseq *new1, *lastnew1;
1520 register char *p = *stringp;
1521 char *q;
1522 char *name;
1523 char stopchars[2];
1524 stopchars[0] = stopchar;
1525 stopchars[1] = '\0';
1527 while (1)
1529 /* Skip whitespace; see if any more names are left. */
1530 p = next_token (p);
1531 if (*p == '\0')
1532 break;
1533 if (*p == stopchar)
1534 break;
1535 /* Yes, find end of next name. */
1536 q = p;
1537 p = find_char_unquote (q, stopchars, 1);
1538 #ifdef __MSDOS__
1539 /* For MS-DOS, skip a "C:\...". */
1540 if (stopchar == ':' && p != 0 && p[1] == '\\' && isalpha (p[-1]))
1541 p = 0;
1542 #endif
1543 if (p == 0)
1544 p = q + strlen (q);
1546 if (strip)
1547 /* Skip leading `./'s. */
1548 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1550 q += 2; /* Skip "./". */
1551 while (q < p && *q == '/')
1552 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1553 ++q;
1556 /* Extract the filename just found, and skip it. */
1558 if (q == p)
1559 /* ".///" was stripped to "". */
1560 name = savestring ("./", 2);
1561 else
1562 name = savestring (q, p - q);
1564 /* Add it to the front of the chain. */
1565 new1 = (struct nameseq *) xmalloc (size);
1566 new1->name = name;
1567 new1->next = new;
1568 new = new1;
1571 #ifndef NO_ARCHIVES
1573 /* Look for multi-word archive references.
1574 They are indicated by a elt ending with an unmatched `)' and
1575 an elt further down the chain (i.e., previous in the file list)
1576 with an unmatched `(' (e.g., "lib(mem"). */
1578 new1 = new;
1579 lastnew1 = 0;
1580 while (new1 != 0)
1581 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1582 && new1->name[strlen (new1->name) - 1] == ')'
1583 && index (new1->name, '(') == 0)
1585 /* NEW1 ends with a `)' but does not contain a `('.
1586 Look back for an elt with an opening `(' but no closing `)'. */
1588 struct nameseq *n = new1->next, *lastn = new1;
1589 char *paren;
1590 while (n != 0 && (paren = index (n->name, '(')) == 0)
1592 lastn = n;
1593 n = n->next;
1595 if (n != 0
1596 /* Ignore something starting with `(', as that cannot actually
1597 be an archive-member reference (and treating it as such
1598 results in an empty file name, which causes much lossage). */
1599 && n->name[0] != '(')
1601 /* N is the first element in the archive group.
1602 Its name looks like "lib(mem" (with no closing `)'). */
1604 char *libname;
1606 /* Copy "lib(" into LIBNAME. */
1607 ++paren;
1608 libname = (char *) alloca (paren - n->name + 1);
1609 bcopy (n->name, libname, paren - n->name);
1610 libname[paren - n->name] = '\0';
1612 if (*paren == '\0')
1614 /* N was just "lib(", part of something like "lib( a b)".
1615 Edit it out of the chain and free its storage. */
1616 lastn->next = n->next;
1617 free (n->name);
1618 free ((char *) n);
1619 /* LASTN->next is the new stopping elt for the loop below. */
1620 n = lastn->next;
1622 else
1624 /* Replace N's name with the full archive reference. */
1625 name = concat (libname, paren, ")");
1626 free (n->name);
1627 n->name = name;
1630 if (new1->name[1] == '\0')
1632 /* NEW1 is just ")", part of something like "lib(a b )".
1633 Omit it from the chain and free its storage. */
1634 if (lastnew1 == 0)
1635 new = new1->next;
1636 else
1637 lastnew1->next = new1->next;
1638 lastn = new1;
1639 new1 = new1->next;
1640 free (lastn->name);
1641 free ((char *) lastn);
1643 else
1645 /* Replace also NEW1->name, which already has closing `)'. */
1646 name = concat (libname, new1->name, "");
1647 free (new1->name);
1648 new1->name = name;
1649 new1 = new1->next;
1652 /* Trace back from NEW1 (the end of the list) until N
1653 (the beginning of the list), rewriting each name
1654 with the full archive reference. */
1656 while (new1 != n)
1658 name = concat (libname, new1->name, ")");
1659 free (new1->name);
1660 new1->name = name;
1661 lastnew1 = new1;
1662 new1 = new1->next;
1665 else
1667 /* No frobnication happening. Just step down the list. */
1668 lastnew1 = new1;
1669 new1 = new1->next;
1672 else
1674 lastnew1 = new1;
1675 new1 = new1->next;
1678 #endif
1680 *stringp = p;
1681 return new;
1684 /* Read a line of text from STREAM into LINEBUFFER.
1685 Combine continuation lines into one line.
1686 Return the number of actual lines read (> 1 if hacked continuation lines).
1689 static unsigned int
1690 readline (linebuffer, stream, filename, lineno)
1691 struct linebuffer *linebuffer;
1692 FILE *stream;
1693 char *filename;
1694 unsigned int lineno;
1696 char *buffer = linebuffer->buffer;
1697 register char *p = linebuffer->buffer;
1698 register char *end = p + linebuffer->size;
1699 register int len, lastlen = 0;
1700 register char *p2;
1701 register unsigned int nlines = 0;
1702 register int backslash;
1704 *p = '\0';
1706 while (fgets (p, end - p, stream) != 0)
1708 len = strlen (p);
1709 if (len == 0)
1711 /* This only happens when the first thing on the line is a '\0'.
1712 It is a pretty hopeless case, but (wonder of wonders) Athena
1713 lossage strikes again! (xmkmf puts NULs in its makefiles.)
1714 There is nothing really to be done; we synthesize a newline so
1715 the following line doesn't appear to be part of this line. */
1716 makefile_error (filename, lineno,
1717 "warning: NUL character seen; rest of line ignored");
1718 p[0] = '\n';
1719 len = 1;
1722 p += len;
1723 if (p[-1] != '\n')
1725 /* Probably ran out of buffer space. */
1726 register unsigned int p_off = p - buffer;
1727 linebuffer->size *= 2;
1728 buffer = (char *) xrealloc (buffer, linebuffer->size);
1729 p = buffer + p_off;
1730 end = buffer + linebuffer->size;
1731 linebuffer->buffer = buffer;
1732 *p = '\0';
1733 lastlen = len;
1734 continue;
1737 ++nlines;
1739 if (len == 1 && p > buffer)
1740 /* P is pointing at a newline and it's the beginning of
1741 the buffer returned by the last fgets call. However,
1742 it is not necessarily the beginning of a line if P is
1743 pointing past the beginning of the holding buffer.
1744 If the buffer was just enlarged (right before the newline),
1745 we must account for that, so we pretend that the two lines
1746 were one line. */
1747 len += lastlen;
1748 lastlen = len;
1749 backslash = 0;
1750 for (p2 = p - 2; --len > 0; --p2)
1752 if (*p2 == '\\')
1753 backslash = !backslash;
1754 else
1755 break;
1758 if (!backslash)
1760 p[-1] = '\0';
1761 break;
1764 if (end - p <= 1)
1766 /* Enlarge the buffer. */
1767 register unsigned int p_off = p - buffer;
1768 linebuffer->size *= 2;
1769 buffer = (char *) xrealloc (buffer, linebuffer->size);
1770 p = buffer + p_off;
1771 end = buffer + linebuffer->size;
1772 linebuffer->buffer = buffer;
1776 if (ferror (stream))
1777 pfatal_with_name (filename);
1779 return nlines;
1782 /* Construct the list of include directories
1783 from the arguments and the default list. */
1785 void
1786 construct_include_path (arg_dirs)
1787 char **arg_dirs;
1789 register unsigned int i;
1790 struct stat stbuf;
1792 /* Table to hold the dirs. */
1794 register unsigned int defsize = (sizeof (default_include_directories)
1795 / sizeof (default_include_directories[0]));
1796 register unsigned int max = 5;
1797 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
1798 register unsigned int idx = 0;
1800 /* First consider any dirs specified with -I switches.
1801 Ignore dirs that don't exist. */
1803 if (arg_dirs != 0)
1804 while (*arg_dirs != 0)
1806 char *dir = *arg_dirs++;
1808 if (dir[0] == '~')
1810 char *expanded = tilde_expand (dir);
1811 if (expanded != 0)
1812 dir = expanded;
1815 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
1817 if (idx == max - 1)
1819 max += 5;
1820 dirs = (char **)
1821 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
1823 dirs[idx++] = dir;
1825 else if (dir != arg_dirs[-1])
1826 free (dir);
1829 /* Now add at the end the standard default dirs. */
1831 for (i = 0; default_include_directories[i] != 0; ++i)
1832 if (stat (default_include_directories[i], &stbuf) == 0
1833 && S_ISDIR (stbuf.st_mode))
1834 dirs[idx++] = default_include_directories[i];
1836 dirs[idx] = 0;
1838 /* Now compute the maximum length of any name in it. */
1840 max_incl_len = 0;
1841 for (i = 0; i < idx; ++i)
1843 unsigned int len = strlen (dirs[i]);
1844 /* If dir name is written with a trailing slash, discard it. */
1845 if (dirs[i][len - 1] == '/')
1846 /* We can't just clobber a null in because it may have come from
1847 a literal string and literal strings may not be writable. */
1848 dirs[i] = savestring (dirs[i], len - 1);
1849 if (len > max_incl_len)
1850 max_incl_len = len;
1853 include_directories = dirs;
1856 /* Expand ~ or ~USER at the beginning of NAME.
1857 Return a newly malloc'd string or 0. */
1859 char *
1860 tilde_expand (name)
1861 char *name;
1863 if (name[1] == '/' || name[1] == '\0')
1865 extern char *getenv ();
1866 char *home_dir;
1867 int is_variable;
1870 /* Turn off --warn-undefined-variables while we expand HOME. */
1871 int save = warn_undefined_variables_flag;
1872 warn_undefined_variables_flag = 0;
1874 home_dir = allocated_variable_expand ("$(HOME)");
1876 warn_undefined_variables_flag = save;
1879 is_variable = home_dir[0] != '\0';
1880 if (!is_variable)
1882 free (home_dir);
1883 home_dir = getenv ("HOME");
1885 if (home_dir == 0 || home_dir[0] == '\0')
1887 extern char *getlogin ();
1888 char *name = getlogin ();
1889 home_dir = 0;
1890 if (name != 0)
1892 struct passwd *p = getpwnam (name);
1893 if (p != 0)
1894 home_dir = p->pw_dir;
1897 if (home_dir != 0)
1899 char *new = concat (home_dir, "", name + 1);
1900 if (is_variable)
1901 free (home_dir);
1902 return new;
1905 else
1907 struct passwd *pwent;
1908 char *userend = index (name + 1, '/');
1909 if (userend != 0)
1910 *userend = '\0';
1911 pwent = getpwnam (name + 1);
1912 if (pwent != 0)
1914 if (userend == 0)
1915 return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
1916 else
1917 return concat (pwent->pw_dir, "/", userend + 1);
1919 else if (userend != 0)
1920 *userend = '/';
1923 return 0;
1926 /* Given a chain of struct nameseq's describing a sequence of filenames,
1927 in reverse of the intended order, return a new chain describing the
1928 result of globbing the filenames. The new chain is in forward order.
1929 The links of the old chain are freed or used in the new chain.
1930 Likewise for the names in the old chain.
1932 SIZE is how big to construct chain elements.
1933 This is useful if we want them actually to be other structures
1934 that have room for additional info. */
1936 struct nameseq *
1937 multi_glob (chain, size)
1938 struct nameseq *chain;
1939 unsigned int size;
1941 extern void dir_setup_glob ();
1942 register struct nameseq *new = 0;
1943 register struct nameseq *old;
1944 struct nameseq *nexto;
1945 glob_t gl;
1947 dir_setup_glob (&gl);
1949 for (old = chain; old != 0; old = nexto)
1951 #ifndef NO_ARCHIVES
1952 char *memname;
1953 #endif
1955 nexto = old->next;
1957 if (old->name[0] == '~')
1959 char *newname = tilde_expand (old->name);
1960 if (newname != 0)
1962 free (old->name);
1963 old->name = newname;
1967 #ifndef NO_ARCHIVES
1968 if (ar_name (old->name))
1970 /* OLD->name is an archive member reference.
1971 Replace it with the archive file name,
1972 and save the member name in MEMNAME.
1973 We will glob on the archive name and then
1974 reattach MEMNAME later. */
1975 char *arname;
1976 ar_parse_name (old->name, &arname, &memname);
1977 free (old->name);
1978 old->name = arname;
1980 else
1981 memname = 0;
1982 #endif
1984 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
1986 case 0: /* Success. */
1988 register int i = gl.gl_pathc;
1989 while (i-- > 0)
1991 #ifndef NO_ARCHIVES
1992 if (memname != 0)
1994 /* Try to glob on MEMNAME within the archive. */
1995 struct nameseq *found
1996 = ar_glob (gl.gl_pathv[i], memname, size);
1997 if (found == 0)
1999 /* No matches. Use MEMNAME as-is. */
2000 struct nameseq *elt
2001 = (struct nameseq *) xmalloc (size);
2002 unsigned int alen = strlen (gl.gl_pathv[i]);
2003 unsigned int mlen = strlen (memname);
2004 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2005 bcopy (gl.gl_pathv[i], elt->name, alen);
2006 elt->name[alen] = '(';
2007 bcopy (memname, &elt->name[alen + 1], mlen);
2008 elt->name[alen + 1 + mlen] = ')';
2009 elt->name[alen + 1 + mlen + 1] = '\0';
2010 elt->next = new;
2011 new = elt;
2013 else
2015 /* Find the end of the FOUND chain. */
2016 struct nameseq *f = found;
2017 while (f->next != 0)
2018 f = f->next;
2020 /* Attach the chain being built to the end of the FOUND
2021 chain, and make FOUND the new NEW chain. */
2022 f->next = new;
2023 new = found;
2026 free (memname);
2028 else
2029 #endif
2031 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2032 elt->name = savestring (gl.gl_pathv[i],
2033 strlen (gl.gl_pathv[i]));
2034 elt->next = new;
2035 new = elt;
2038 globfree (&gl);
2039 free (old->name);
2040 free (old);
2041 break;
2044 case GLOB_NOSPACE:
2045 fatal ("virtual memory exhausted");
2046 break;
2048 default:
2049 old->next = new;
2050 new = old;
2051 break;
2055 return new;