(start_job_command): Pass null instead of "" to message; don't add newline.
[make.git] / read.c
blob3c51fa5014f20d66ddbb1270351c9967153538f0
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 /* Add them to the tail, after any MAKEFILES variable makefiles. */
196 while (tail != 0 && tail->next != 0)
197 tail = tail->next;
198 for (p = default_makefiles; *p != 0; ++p)
200 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
201 d->name = 0;
202 d->file = enter_file (*p);
203 d->file->dontcare = 1;
204 /* Tell update_goal_chain to bail out as soon as this file is
205 made, and main not to die if we can't make this file. */
206 d->changed = RM_DONTCARE;
207 if (tail == 0)
208 read_makefiles = d;
209 else
210 tail->next = d;
211 tail = d;
213 if (tail != 0)
214 tail->next = 0;
218 return read_makefiles;
221 /* Read file FILENAME as a makefile and add its contents to the data base.
223 FLAGS contains bits as above.
225 FILENAME is added to the `read_makefiles' chain.
227 Returns 1 if a file was found and read, 0 if not. */
229 static int
230 read_makefile (filename, flags)
231 char *filename;
232 int flags;
234 static char *collapsed = 0;
235 static unsigned int collapsed_length = 0;
236 register FILE *infile;
237 struct linebuffer lb;
238 unsigned int commands_len = 200;
239 char *commands = (char *) xmalloc (200);
240 unsigned int commands_idx = 0;
241 unsigned int commands_started;
242 register char *p;
243 char *p2;
244 int ignoring = 0, in_ignored_define = 0;
245 int no_targets = 0; /* Set when reading a rule without targets. */
246 char *passed_filename = filename;
248 struct nameseq *filenames = 0;
249 struct dep *deps;
250 unsigned int lineno = 1;
251 unsigned int nlines = 0;
252 int two_colon;
253 char *pattern = 0, *pattern_percent;
255 int makefile_errno;
257 #define record_waiting_files() \
258 do \
260 if (filenames != 0) \
261 record_files (filenames, pattern, pattern_percent, deps, \
262 commands_started, commands, commands_idx, \
263 two_colon, filename, lineno, \
264 !(flags & RM_NO_DEFAULT_GOAL)); \
265 filenames = 0; \
266 commands_idx = 0; \
267 pattern = 0; \
268 } while (0)
270 #ifdef lint /* Suppress `used before set' messages. */
271 two_colon = 0;
272 #endif
274 if (debug_flag)
276 printf ("Reading makefile `%s'", filename);
277 if (flags & RM_NO_DEFAULT_GOAL)
278 printf (" (no default goal)");
279 if (flags & RM_INCLUDED)
280 printf (" (search path)");
281 if (flags & RM_DONTCARE)
282 printf (" (don't care)");
283 if (flags & RM_NO_TILDE)
284 printf (" (no ~ expansion)");
285 puts ("...");
288 /* First, get a stream to read. */
290 /* Expand ~ in FILENAME unless it came from `include',
291 in which case it was already done. */
292 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
294 char *expanded = tilde_expand (filename);
295 if (expanded != 0)
296 filename = expanded;
299 infile = fopen (filename, "r");
300 /* Save the error code so we print the right message later. */
301 makefile_errno = errno;
303 /* If the makefile wasn't found and it's either a makefile from
304 the `MAKEFILES' variable or an included makefile,
305 search the included makefile search path for this makefile. */
307 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
309 register unsigned int i;
310 for (i = 0; include_directories[i] != 0; ++i)
312 char *name = concat (include_directories[i], "/", filename);
313 infile = fopen (name, "r");
314 if (infile == 0)
315 free (name);
316 else
318 filename = name;
319 break;
324 /* Add FILENAME to the chain of read makefiles. */
325 deps = (struct dep *) xmalloc (sizeof (struct dep));
326 deps->next = read_makefiles;
327 read_makefiles = deps;
328 deps->name = 0;
329 deps->file = lookup_file (filename);
330 if (deps->file == 0)
332 deps->file = enter_file (savestring (filename, strlen (filename)));
333 if (flags & RM_DONTCARE)
334 deps->file->dontcare = 1;
336 if (filename != passed_filename)
337 free (filename);
338 filename = deps->file->name;
339 deps->changed = flags;
340 deps = 0;
342 /* If the makefile can't be found at all, give up entirely. */
344 if (infile == 0)
346 /* If we did some searching, errno has the error from the last
347 attempt, rather from FILENAME itself. Restore it in case the
348 caller wants to use it in a message. */
349 errno = makefile_errno;
350 return 0;
353 reading_filename = filename;
354 reading_lineno_ptr = &lineno;
356 /* Loop over lines in the file.
357 The strategy is to accumulate target names in FILENAMES, dependencies
358 in DEPS and commands in COMMANDS. These are used to define a rule
359 when the start of the next rule (or eof) is encountered. */
361 initbuffer (&lb);
363 while (!feof (infile))
365 lineno += nlines;
366 nlines = readline (&lb, infile, filename, lineno);
368 /* Check for a shell command line first.
369 If it is not one, we can stop treating tab specially. */
370 if (lb.buffer[0] == '\t')
372 /* This line is a probably shell command. */
373 unsigned int len;
375 if (no_targets)
376 /* Ignore the commands in a rule with no targets. */
377 continue;
379 /* If there is no preceding rule line, don't treat this line
380 as a command, even though it begins with a tab character.
381 SunOS 4 make appears to behave this way. */
383 if (filenames != 0)
385 if (ignoring)
386 /* Yep, this is a shell command, and we don't care. */
387 continue;
389 /* Append this command line to the line being accumulated. */
390 p = lb.buffer;
391 if (commands_idx == 0)
392 commands_started = lineno;
393 len = strlen (p);
394 if (len + 1 + commands_idx > commands_len)
396 commands_len = (len + 1 + commands_idx) * 2;
397 commands = (char *) xrealloc (commands, commands_len);
399 bcopy (p, &commands[commands_idx], len);
400 commands_idx += len;
401 commands[commands_idx++] = '\n';
403 continue;
407 /* This line is not a shell command line. Don't worry about tabs. */
409 if (collapsed_length < lb.size)
411 collapsed_length = lb.size;
412 if (collapsed != 0)
413 free (collapsed);
414 collapsed = (char *) xmalloc (collapsed_length);
416 strcpy (collapsed, lb.buffer);
417 /* Collapse continuation lines. */
418 collapse_continuations (collapsed);
419 remove_comments (collapsed);
421 /* strncmp is first to avoid dereferencing out into space. */
422 #define word1eq(s, l) (!strncmp (s, p, l) \
423 && (p[l] == '\0' || isblank (p[l])))
424 p = collapsed;
425 while (isspace (*p))
426 ++p;
427 if (*p == '\0')
428 /* This line is completely empty. */
429 continue;
431 /* We must first check for conditional and `define' directives before
432 ignoring anything, since they control what we will do with
433 following lines. */
435 if (!in_ignored_define
436 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
437 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
438 || word1eq ("else", 4) || word1eq ("endif", 5)))
440 int i = conditional_line (p, filename, lineno);
441 if (i >= 0)
442 ignoring = i;
443 else
444 makefile_fatal (filename, lineno,
445 "invalid syntax in conditional");
446 continue;
448 else if (word1eq ("endef", 5))
450 if (in_ignored_define)
451 in_ignored_define = 0;
452 else
453 makefile_fatal (filename, lineno, "extraneous `endef'");
454 continue;
456 else if (word1eq ("define", 6))
458 if (ignoring)
459 in_ignored_define = 1;
460 else
462 p2 = next_token (p + 6);
463 /* Let the variable name be the whole rest of the line,
464 with trailing blanks stripped (comments have already been
465 removed), so it could be a complex variable/function
466 reference that might contain blanks. */
467 p = index (p2, '\0');
468 while (isblank (p[-1]))
469 --p;
470 lineno = do_define (p2, p - p2, o_file,
471 lineno, infile, filename);
473 continue;
475 else if (word1eq ("override", 8))
477 p2 = next_token (p + 8);
478 if (p2 == 0)
479 makefile_error (filename, lineno, "empty `override' directive");
480 if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
482 if (ignoring)
483 in_ignored_define = 1;
484 else
486 p2 = next_token (p2 + 6);
487 /* Let the variable name be the whole rest of the line,
488 with trailing blanks stripped (comments have already been
489 removed), so it could be a complex variable/function
490 reference that might contain blanks. */
491 p = index (p2, '\0');
492 while (isblank (p[-1]))
493 --p;
494 lineno = do_define (p2, p - p2, o_override,
495 lineno, infile, filename);
498 else if (!ignoring
499 && !try_variable_definition (filename, lineno,
500 p2, o_override))
501 makefile_error (filename, lineno, "empty `override' directive");
503 continue;
506 if (ignoring)
507 /* Ignore the line. We continue here so conditionals
508 can appear in the middle of a rule. */
509 continue;
510 else if (word1eq ("export", 6))
512 struct variable *v;
513 p2 = next_token (p + 6);
514 if (*p2 == '\0')
515 export_all_variables = 1;
516 v = try_variable_definition (filename, lineno, p2, o_file);
517 if (v != 0)
518 v->export = v_export;
519 else
521 unsigned int len;
522 for (p = find_next_token (&p2, &len); p != 0;
523 p = find_next_token (&p2, &len))
525 v = lookup_variable (p, len);
526 if (v == 0)
527 v = define_variable (p, len, "", o_file, 0);
528 v->export = v_export;
532 else if (word1eq ("unexport", 8))
534 unsigned int len;
535 struct variable *v;
536 p2 = next_token (p + 8);
537 if (*p2 == '\0')
538 export_all_variables = 0;
539 for (p = find_next_token (&p2, &len); p != 0;
540 p = find_next_token (&p2, &len))
542 v = lookup_variable (p, len);
543 if (v == 0)
544 v = define_variable (p, len, "", o_file, 0);
545 v->export = v_noexport;
548 else if (word1eq ("include", 7) || word1eq ("-include", 8))
550 /* We have found an `include' line specifying a nested
551 makefile to be read at this point. */
552 struct conditionals *save, new_conditionals;
553 struct nameseq *files;
554 /* "-include" (vs "include") says no
555 error if the file does not exist. */
556 int noerror = p[0] == '-';
558 p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
559 if (*p == '\0')
561 makefile_error (filename, lineno,
562 "no file name for `%sinclude'",
563 noerror ? "-" : "");
564 continue;
567 /* Parse the list of file names. */
568 p2 = p;
569 files = multi_glob (parse_file_seq (&p2, '\0',
570 sizeof (struct nameseq),
572 sizeof (struct nameseq));
573 free (p);
575 /* Save the state of conditionals and start
576 the included makefile with a clean slate. */
577 save = conditionals;
578 bzero ((char *) &new_conditionals, sizeof new_conditionals);
579 conditionals = &new_conditionals;
581 /* Record the rules that are waiting so they will determine
582 the default goal before those in the included makefile. */
583 record_waiting_files ();
585 /* Read each included makefile. */
586 while (files != 0)
588 struct nameseq *next = files->next;
589 char *name = files->name;
590 free (files);
591 files = next;
593 if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
594 | (noerror ? RM_DONTCARE : 0)))
595 && ! noerror)
596 makefile_error (filename, lineno,
597 "%s: %s", name, strerror (errno));
600 /* Free any space allocated by conditional_line. */
601 if (conditionals->ignoring)
602 free (conditionals->ignoring);
603 if (conditionals->seen_else)
604 free (conditionals->seen_else);
606 /* Restore state. */
607 conditionals = save;
608 reading_filename = filename;
609 reading_lineno_ptr = &lineno;
611 else if (word1eq ("vpath", 5))
613 char *pattern;
614 unsigned int len;
615 p2 = variable_expand (p + 5);
616 p = find_next_token (&p2, &len);
617 if (p != 0)
619 pattern = savestring (p, len);
620 p = find_next_token (&p2, &len);
621 /* No searchpath means remove all previous
622 selective VPATH's with the same pattern. */
624 else
625 /* No pattern means remove all previous selective VPATH's. */
626 pattern = 0;
627 construct_vpath_list (pattern, p);
628 if (pattern != 0)
629 free (pattern);
631 #undef word1eq
632 else if (try_variable_definition (filename, lineno, p, o_file))
633 /* This line has been dealt with. */
635 else if (lb.buffer[0] == '\t')
637 p = collapsed; /* Ignore comments. */
638 while (isblank (*p))
639 ++p;
640 if (*p == '\0')
641 /* The line is completely blank; that is harmless. */
642 continue;
643 /* This line starts with a tab but was not caught above
644 because there was no preceding target, and the line
645 might have been usable as a variable definition.
646 But now it is definitely lossage. */
647 makefile_fatal (filename, lineno,
648 "commands commence before first target");
650 else
652 /* This line describes some target files. */
654 char *cmdleft;
656 /* Record the previous rule. */
658 record_waiting_files ();
660 /* Look for a semicolon in the unexpanded line. */
661 cmdleft = find_semicolon (lb.buffer);
662 if (cmdleft != 0)
663 /* Found one. Cut the line short there before expanding it. */
664 *cmdleft = '\0';
666 collapse_continuations (lb.buffer);
668 /* Expand variable and function references before doing anything
669 else so that special characters can be inside variables. */
670 p = variable_expand (lb.buffer);
672 if (cmdleft == 0)
673 /* Look for a semicolon in the expanded line. */
674 cmdleft = find_semicolon (p);
676 if (cmdleft != 0)
677 /* Cut the line short at the semicolon. */
678 *cmdleft = '\0';
680 /* Remove comments from the line. */
681 remove_comments (p);
683 p2 = next_token (p);
684 if (*p2 == '\0')
686 if (cmdleft != 0)
687 makefile_fatal (filename, lineno,
688 "missing rule before commands");
689 else
690 /* This line contained a variable reference that
691 expanded to nothing but whitespace. */
692 continue;
694 else if (*p2 == ':')
696 /* We accept and ignore rules without targets for
697 compatibility with SunOS 4 make. */
698 no_targets = 1;
699 continue;
702 filenames = multi_glob (parse_file_seq (&p2, ':',
703 sizeof (struct nameseq),
705 sizeof (struct nameseq));
706 if (*p2++ == '\0')
707 makefile_fatal (filename, lineno, "missing separator");
708 /* Is this a one-colon or two-colon entry? */
709 two_colon = *p2 == ':';
710 if (two_colon)
711 p2++;
713 /* We have some targets, so don't ignore the following commands. */
714 no_targets = 0;
716 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
717 p = index (p2, ':');
718 while (p != 0 && p[-1] == '\\')
720 register char *q = &p[-1];
721 register int backslash = 0;
722 while (*q-- == '\\')
723 backslash = !backslash;
724 if (backslash)
725 p = index (p + 1, ':');
726 else
727 break;
729 #ifdef __MSDOS__
730 /* For MS-DOS, skip a "C:\...". */
731 if (p != 0 && p[1] == '\\' && isalpha (p[-1]))
732 p = 0;
733 #endif
734 if (p != 0)
736 struct nameseq *target;
737 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
738 ++p2;
739 if (target == 0)
740 makefile_fatal (filename, lineno, "missing target pattern");
741 else if (target->next != 0)
742 makefile_fatal (filename, lineno, "multiple target patterns");
743 pattern = target->name;
744 pattern_percent = find_percent (pattern);
745 if (pattern_percent == 0)
746 makefile_fatal (filename, lineno,
747 "target pattern contains no `%%'");
749 else
750 pattern = 0;
752 /* Parse the dependencies. */
753 deps = (struct dep *)
754 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
755 sizeof (struct dep));
757 commands_idx = 0;
758 if (cmdleft != 0)
760 /* Semicolon means rest of line is a command. */
761 unsigned int len = strlen (cmdleft + 1);
763 commands_started = lineno;
765 /* Add this command line to the buffer. */
766 if (len + 2 > commands_len)
768 commands_len = (len + 2) * 2;
769 commands = (char *) xrealloc (commands, commands_len);
771 bcopy (cmdleft + 1, commands, len);
772 commands_idx += len;
773 commands[commands_idx++] = '\n';
776 continue;
779 /* We get here except in the case that we just read a rule line.
780 Record now the last rule we read, so following spurious
781 commands are properly diagnosed. */
782 record_waiting_files ();
783 no_targets = 0;
786 if (conditionals->if_cmds)
787 makefile_fatal (filename, lineno, "missing `endif'");
789 /* At eof, record the last rule. */
790 record_waiting_files ();
792 freebuffer (&lb);
793 free ((char *) commands);
794 fclose (infile);
796 reading_filename = 0;
797 reading_lineno_ptr = 0;
799 return 1;
802 /* Execute a `define' directive.
803 The first line has already been read, and NAME is the name of
804 the variable to be defined. The following lines remain to be read.
805 LINENO, INFILE and FILENAME refer to the makefile being read.
806 The value returned is LINENO, updated for lines read here. */
808 static unsigned int
809 do_define (name, namelen, origin, lineno, infile, filename)
810 char *name;
811 unsigned int namelen;
812 enum variable_origin origin;
813 unsigned int lineno;
814 FILE *infile;
815 char *filename;
817 struct linebuffer lb;
818 unsigned int nlines = 0;
819 unsigned int length = 100;
820 char *definition = (char *) xmalloc (100);
821 register unsigned int idx = 0;
822 register char *p;
824 /* Expand the variable name. */
825 char *var = (char *) alloca (namelen + 1);
826 bcopy (name, var, namelen);
827 var[namelen] = '\0';
828 var = variable_expand (var);
830 initbuffer (&lb);
831 while (!feof (infile))
833 lineno += nlines;
834 nlines = readline (&lb, infile, filename, lineno);
836 collapse_continuations (lb.buffer);
838 p = next_token (lb.buffer);
839 if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
841 p += 5;
842 remove_comments (p);
843 if (*next_token (p) != '\0')
844 makefile_error (filename, lineno,
845 "Extraneous text after `endef' directive");
846 /* Define the variable. */
847 if (idx == 0)
848 definition[0] = '\0';
849 else
850 definition[idx - 1] = '\0';
851 (void) define_variable (var, strlen (var), definition, origin, 1);
852 free (definition);
853 freebuffer (&lb);
854 return lineno;
856 else
858 unsigned int len = strlen (lb.buffer);
860 /* Increase the buffer size if necessary. */
861 if (idx + len + 1 > length)
863 length = (idx + len) * 2;
864 definition = (char *) xrealloc (definition, length + 1);
867 bcopy (lb.buffer, &definition[idx], len);
868 idx += len;
869 /* Separate lines with a newline. */
870 definition[idx++] = '\n';
874 /* No `endef'!! */
875 makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
877 /* NOTREACHED */
878 return 0;
881 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
882 "ifneq", "else" and "endif".
883 LINE is the input line, with the command as its first word.
885 FILENAME and LINENO are the filename and line number in the
886 current makefile. They are used for error messages.
888 Value is -1 if the line is invalid,
889 0 if following text should be interpreted,
890 1 if following text should be ignored. */
892 static int
893 conditional_line (line, filename, lineno)
894 char *line;
895 char *filename;
896 unsigned int lineno;
898 int notdef;
899 char *cmdname;
900 register unsigned int i;
902 if (*line == 'i')
904 /* It's an "if..." command. */
905 notdef = line[2] == 'n';
906 if (notdef)
908 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
909 line += cmdname[3] == 'd' ? 7 : 6;
911 else
913 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
914 line += cmdname[2] == 'd' ? 6 : 5;
917 else
919 /* It's an "else" or "endif" command. */
920 notdef = line[1] == 'n';
921 cmdname = notdef ? "endif" : "else";
922 line += notdef ? 5 : 4;
925 line = next_token (line);
927 if (*cmdname == 'e')
929 if (*line != '\0')
930 makefile_error (filename, lineno,
931 "Extraneous text after `%s' directive",
932 cmdname);
933 /* "Else" or "endif". */
934 if (conditionals->if_cmds == 0)
935 makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
936 /* NOTDEF indicates an `endif' command. */
937 if (notdef)
938 --conditionals->if_cmds;
939 else if (conditionals->seen_else[conditionals->if_cmds - 1])
940 makefile_fatal (filename, lineno, "only one `else' per conditional");
941 else
943 /* Toggle the state of ignorance. */
944 conditionals->ignoring[conditionals->if_cmds - 1]
945 = !conditionals->ignoring[conditionals->if_cmds - 1];
946 /* Record that we have seen an `else' in this conditional.
947 A second `else' will be erroneous. */
948 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
950 for (i = 0; i < conditionals->if_cmds; ++i)
951 if (conditionals->ignoring[i])
952 return 1;
953 return 0;
956 if (conditionals->allocated == 0)
958 conditionals->allocated = 5;
959 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
960 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
963 ++conditionals->if_cmds;
964 if (conditionals->if_cmds > conditionals->allocated)
966 conditionals->allocated += 5;
967 conditionals->ignoring = (char *)
968 xrealloc (conditionals->ignoring, conditionals->allocated);
969 conditionals->seen_else = (char *)
970 xrealloc (conditionals->seen_else, conditionals->allocated);
973 /* Record that we have seen an `if...' but no `else' so far. */
974 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
976 /* Search through the stack to see if we're already ignoring. */
977 for (i = 0; i < conditionals->if_cmds - 1; ++i)
978 if (conditionals->ignoring[i])
980 /* We are already ignoring, so just push a level
981 to match the next "else" or "endif", and keep ignoring.
982 We don't want to expand variables in the condition. */
983 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
984 return 1;
987 if (cmdname[notdef ? 3 : 2] == 'd')
989 /* "Ifdef" or "ifndef". */
990 struct variable *v;
991 register char *p = end_of_token (line);
992 i = p - line;
993 p = next_token (p);
994 if (*p != '\0')
995 return -1;
996 v = lookup_variable (line, i);
997 conditionals->ignoring[conditionals->if_cmds - 1]
998 = (v != 0 && *v->value != '\0') == notdef;
1000 else
1002 /* "Ifeq" or "ifneq". */
1003 char *s1, *s2;
1004 unsigned int len;
1005 char termin = *line == '(' ? ',' : *line;
1007 if (termin != ',' && termin != '"' && termin != '\'')
1008 return -1;
1010 s1 = ++line;
1011 /* Find the end of the first string. */
1012 if (termin == ',')
1014 register int count = 0;
1015 for (; *line != '\0'; ++line)
1016 if (*line == '(')
1017 ++count;
1018 else if (*line == ')')
1019 --count;
1020 else if (*line == ',' && count <= 0)
1021 break;
1023 else
1024 while (*line != '\0' && *line != termin)
1025 ++line;
1027 if (*line == '\0')
1028 return -1;
1030 *line++ = '\0';
1032 s2 = variable_expand (s1);
1033 /* We must allocate a new copy of the expanded string because
1034 variable_expand re-uses the same buffer. */
1035 len = strlen (s2);
1036 s1 = (char *) alloca (len + 1);
1037 bcopy (s2, s1, len + 1);
1039 if (termin != ',')
1040 /* Find the start of the second string. */
1041 line = next_token (line);
1043 termin = termin == ',' ? ')' : *line;
1044 if (termin != ')' && termin != '"' && termin != '\'')
1045 return -1;
1047 /* Find the end of the second string. */
1048 if (termin == ')')
1050 register int count = 0;
1051 s2 = next_token (line);
1052 for (line = s2; *line != '\0'; ++line)
1054 if (*line == '(')
1055 ++count;
1056 else if (*line == ')')
1057 if (count <= 0)
1058 break;
1059 else
1060 --count;
1063 else
1065 ++line;
1066 s2 = line;
1067 while (*line != '\0' && *line != termin)
1068 ++line;
1071 if (*line == '\0')
1072 return -1;
1074 *line = '\0';
1075 line = next_token (++line);
1076 if (*line != '\0')
1077 makefile_error (filename, lineno,
1078 "Extraneous text after `%s' directive",
1079 cmdname);
1081 s2 = variable_expand (s2);
1082 conditionals->ignoring[conditionals->if_cmds - 1]
1083 = streq (s1, s2) == notdef;
1086 /* Search through the stack to see if we're ignoring. */
1087 for (i = 0; i < conditionals->if_cmds; ++i)
1088 if (conditionals->ignoring[i])
1089 return 1;
1090 return 0;
1093 /* Remove duplicate dependencies in CHAIN. */
1095 void
1096 uniquize_deps (chain)
1097 struct dep *chain;
1099 register struct dep *d;
1101 /* Make sure that no dependencies are repeated. This does not
1102 really matter for the purpose of updating targets, but it
1103 might make some names be listed twice for $^ and $?. */
1105 for (d = chain; d != 0; d = d->next)
1107 struct dep *last, *next;
1109 last = d;
1110 next = d->next;
1111 while (next != 0)
1112 if (streq (dep_name (d), dep_name (next)))
1114 struct dep *n = next->next;
1115 last->next = n;
1116 if (next->name != 0 && next->name != d->name)
1117 free (next->name);
1118 if (next != d)
1119 free ((char *) next);
1120 next = n;
1122 else
1124 last = next;
1125 next = next->next;
1130 /* Record a description line for files FILENAMES,
1131 with dependencies DEPS, commands to execute described
1132 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1133 TWO_COLON is nonzero if a double colon was used.
1134 If not nil, PATTERN is the `%' pattern to make this
1135 a static pattern rule, and PATTERN_PERCENT is a pointer
1136 to the `%' within it.
1138 The links of FILENAMES are freed, and so are any names in it
1139 that are not incorporated into other data structures. */
1141 static void
1142 record_files (filenames, pattern, pattern_percent, deps, commands_started,
1143 commands, commands_idx, two_colon, filename, lineno, set_default)
1144 struct nameseq *filenames;
1145 char *pattern, *pattern_percent;
1146 struct dep *deps;
1147 unsigned int commands_started;
1148 char *commands;
1149 unsigned int commands_idx;
1150 int two_colon;
1151 char *filename;
1152 unsigned int lineno;
1153 int set_default;
1155 struct nameseq *nextf;
1156 int implicit = 0;
1157 unsigned int max_targets, target_idx;
1158 char **targets = 0, **target_percents = 0;
1159 struct commands *cmds;
1161 if (commands_idx > 0)
1163 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1164 cmds->filename = filename;
1165 cmds->lineno = commands_started;
1166 cmds->commands = savestring (commands, commands_idx);
1167 cmds->command_lines = 0;
1169 else
1170 cmds = 0;
1172 for (; filenames != 0; filenames = nextf)
1174 register char *name = filenames->name;
1175 register struct file *f;
1176 register struct dep *d;
1177 struct dep *this;
1178 char *implicit_percent;
1180 nextf = filenames->next;
1181 free ((char *) filenames);
1183 implicit_percent = find_percent (name);
1184 implicit |= implicit_percent != 0;
1186 if (implicit && pattern != 0)
1187 makefile_fatal (filename, lineno,
1188 "mixed implicit and static pattern rules");
1190 if (implicit && implicit_percent == 0)
1191 makefile_fatal (filename, lineno, "mixed implicit and normal rules");
1193 if (implicit)
1195 if (targets == 0)
1197 max_targets = 5;
1198 targets = (char **) xmalloc (5 * sizeof (char *));
1199 target_percents = (char **) xmalloc (5 * sizeof (char *));
1200 target_idx = 0;
1202 else if (target_idx == max_targets - 1)
1204 max_targets += 5;
1205 targets = (char **) xrealloc ((char *) targets,
1206 max_targets * sizeof (char *));
1207 target_percents
1208 = (char **) xrealloc ((char *) target_percents,
1209 max_targets * sizeof (char *));
1211 targets[target_idx] = name;
1212 target_percents[target_idx] = implicit_percent;
1213 ++target_idx;
1214 continue;
1217 /* If there are multiple filenames, copy the chain DEPS
1218 for all but the last one. It is not safe for the same deps
1219 to go in more than one place in the data base. */
1220 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1222 if (pattern != 0)
1223 /* If this is an extended static rule:
1224 `targets: target%pattern: dep%pattern; cmds',
1225 translate each dependency pattern into a plain filename
1226 using the target pattern and this target's name. */
1227 if (!pattern_matches (pattern, pattern_percent, name))
1229 /* Give a warning if the rule is meaningless. */
1230 makefile_error (filename, lineno,
1231 "target `%s' doesn't match the target pattern",
1232 name);
1233 this = 0;
1235 else
1237 /* We use patsubst_expand to do the work of translating
1238 the target pattern, the target's name and the dependencies'
1239 patterns into plain dependency names. */
1240 char *buffer = variable_expand ("");
1242 for (d = this; d != 0; d = d->next)
1244 char *o;
1245 char *percent = find_percent (d->name);
1246 if (percent == 0)
1247 continue;
1248 o = patsubst_expand (buffer, name, pattern, d->name,
1249 pattern_percent, percent);
1250 free (d->name);
1251 d->name = savestring (buffer, o - buffer);
1255 if (!two_colon)
1257 /* Single-colon. Combine these dependencies
1258 with others in file's existing record, if any. */
1259 f = enter_file (name);
1261 if (f->double_colon)
1262 makefile_fatal (filename, lineno,
1263 "target file `%s' has both : and :: entries",
1264 f->name);
1266 /* If CMDS == F->CMDS, this target was listed in this rule
1267 more than once. Just give a warning since this is harmless. */
1268 if (cmds != 0 && cmds == f->cmds)
1269 makefile_error
1270 (filename, lineno,
1271 "target `%s' given more than once in the same rule.",
1272 f->name);
1274 /* Check for two single-colon entries both with commands.
1275 Check is_target so that we don't lose on files such as .c.o
1276 whose commands were preinitialized. */
1277 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1279 makefile_error (cmds->filename, cmds->lineno,
1280 "warning: overriding commands for target `%s'",
1281 f->name);
1282 makefile_error (f->cmds->filename, f->cmds->lineno,
1283 "warning: ignoring old commands for target `%s'",
1284 f->name);
1287 f->is_target = 1;
1289 /* Defining .DEFAULT with no deps or cmds clears it. */
1290 if (f == default_file && this == 0 && cmds == 0)
1291 f->cmds = 0;
1292 if (cmds != 0)
1293 f->cmds = cmds;
1294 /* Defining .SUFFIXES with no dependencies
1295 clears out the list of suffixes. */
1296 if (f == suffix_file && this == 0)
1298 d = f->deps;
1299 while (d != 0)
1301 struct dep *nextd = d->next;
1302 free (d->name);
1303 free (d);
1304 d = nextd;
1306 f->deps = 0;
1308 else if (f->deps != 0)
1310 /* Add the file's old deps and the new ones in THIS together. */
1312 struct dep *firstdeps, *moredeps;
1313 if (cmds != 0)
1315 /* This is the rule with commands, so put its deps first.
1316 The rationale behind this is that $< expands to the
1317 first dep in the chain, and commands use $< expecting
1318 to get the dep that rule specifies. */
1319 firstdeps = this;
1320 moredeps = f->deps;
1322 else
1324 /* Append the new deps to the old ones. */
1325 firstdeps = f->deps;
1326 moredeps = this;
1329 if (firstdeps == 0)
1330 firstdeps = moredeps;
1331 else
1333 d = firstdeps;
1334 while (d->next != 0)
1335 d = d->next;
1336 d->next = moredeps;
1339 f->deps = firstdeps;
1341 else
1342 f->deps = this;
1344 /* If this is a static pattern rule, set the file's stem to
1345 the part of its name that matched the `%' in the pattern,
1346 so you can use $* in the commands. */
1347 if (pattern != 0)
1349 static char *percent = "%";
1350 char *buffer = variable_expand ("");
1351 char *o = patsubst_expand (buffer, name, pattern, percent,
1352 pattern_percent, percent);
1353 f->stem = savestring (buffer, o - buffer);
1356 else
1358 /* Double-colon. Make a new record
1359 even if the file already has one. */
1360 f = lookup_file (name);
1361 /* Check for both : and :: rules. Check is_target so
1362 we don't lose on default suffix rules or makefiles. */
1363 if (f != 0 && f->is_target && !f->double_colon)
1364 makefile_fatal (filename, lineno,
1365 "target file `%s' has both : and :: entries",
1366 f->name);
1367 f = enter_file (name);
1368 /* If there was an existing entry and it was a double-colon
1369 entry, enter_file will have returned a new one, making it the
1370 prev pointer of the old one, and setting its double_colon
1371 pointer to the first one. */
1372 if (f->double_colon == 0)
1373 /* This is the first entry for this name, so we must
1374 set its double_colon pointer to itself. */
1375 f->double_colon = f;
1376 f->is_target = 1;
1377 f->deps = this;
1378 f->cmds = cmds;
1381 /* Free name if not needed further. */
1382 if (f != 0 && name != f->name
1383 && (name < f->name || name > f->name + strlen (f->name)))
1385 free (name);
1386 name = f->name;
1389 /* See if this is first target seen whose name does
1390 not start with a `.', unless it contains a slash. */
1391 if (default_goal_file == 0 && set_default
1392 && (*name != '.' || index (name, '/') != 0))
1394 int reject = 0;
1396 /* If this file is a suffix, don't
1397 let it be the default goal file. */
1399 for (d = suffix_file->deps; d != 0; d = d->next)
1401 register struct dep *d2;
1402 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1404 reject = 1;
1405 break;
1407 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1409 register unsigned int len = strlen (dep_name (d2));
1410 if (strncmp (name, dep_name (d2), len))
1411 continue;
1412 if (streq (name + len, dep_name (d)))
1414 reject = 1;
1415 break;
1418 if (reject)
1419 break;
1422 if (!reject)
1423 default_goal_file = f;
1427 if (implicit)
1429 targets[target_idx] = 0;
1430 target_percents[target_idx] = 0;
1431 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1432 free ((char *) target_percents);
1436 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1437 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1438 Quoting backslashes are removed from STRING by compacting it into
1439 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1440 one, or nil if there are none. */
1442 char *
1443 find_char_unquote (string, stopchars, blank)
1444 char *string;
1445 char *stopchars;
1446 int blank;
1448 unsigned int string_len = strlen (string);
1449 register char *p = string;
1451 while (1)
1453 while (*p != '\0' && index (stopchars, *p) == 0
1454 && (!blank || !isblank (*p)))
1455 ++p;
1456 if (*p == '\0')
1457 break;
1459 if (p > string && p[-1] == '\\')
1461 /* Search for more backslashes. */
1462 register int i = -2;
1463 while (&p[i] >= string && p[i] == '\\')
1464 --i;
1465 ++i;
1466 /* The number of backslashes is now -I.
1467 Copy P over itself to swallow half of them. */
1468 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1469 p += i / 2;
1470 if (i % 2 == 0)
1471 /* All the backslashes quoted each other; the STOPCHAR was
1472 unquoted. */
1473 return p;
1475 /* The STOPCHAR was quoted by a backslash. Look for another. */
1477 else
1478 /* No backslash in sight. */
1479 return p;
1482 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1483 return 0;
1486 /* Search PATTERN for an unquoted %. */
1488 char *
1489 find_percent (pattern)
1490 char *pattern;
1492 return find_char_unquote (pattern, "%", 0);
1495 /* Search STRING for an unquoted ; that is not after an unquoted #. */
1497 static char *
1498 find_semicolon (string)
1499 char *string;
1501 char *match = find_char_unquote (string, ";#", 0);
1502 if (match != 0 && *match == '#')
1503 /* We found a comment before a semicolon. No match. */
1504 match = 0;
1505 return match;
1508 /* Parse a string into a sequence of filenames represented as a
1509 chain of struct nameseq's in reverse order and return that chain.
1511 The string is passed as STRINGP, the address of a string pointer.
1512 The string pointer is updated to point at the first character
1513 not parsed, which either is a null char or equals STOPCHAR.
1515 SIZE is how big to construct chain elements.
1516 This is useful if we want them actually to be other structures
1517 that have room for additional info.
1519 If STRIP is nonzero, strip `./'s off the beginning. */
1521 struct nameseq *
1522 parse_file_seq (stringp, stopchar, size, strip)
1523 char **stringp;
1524 char stopchar;
1525 unsigned int size;
1526 int strip;
1528 register struct nameseq *new = 0;
1529 register struct nameseq *new1, *lastnew1;
1530 register char *p = *stringp;
1531 char *q;
1532 char *name;
1533 char stopchars[2];
1534 stopchars[0] = stopchar;
1535 stopchars[1] = '\0';
1537 while (1)
1539 /* Skip whitespace; see if any more names are left. */
1540 p = next_token (p);
1541 if (*p == '\0')
1542 break;
1543 if (*p == stopchar)
1544 break;
1545 /* Yes, find end of next name. */
1546 q = p;
1547 p = find_char_unquote (q, stopchars, 1);
1548 #ifdef __MSDOS__
1549 /* For MS-DOS, skip a "C:\...". */
1550 if (stopchar == ':' && p != 0 && p[1] == '\\' && isalpha (p[-1]))
1551 p = 0;
1552 #endif
1553 if (p == 0)
1554 p = q + strlen (q);
1556 if (strip)
1557 /* Skip leading `./'s. */
1558 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1560 q += 2; /* Skip "./". */
1561 while (q < p && *q == '/')
1562 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1563 ++q;
1566 /* Extract the filename just found, and skip it. */
1568 if (q == p)
1569 /* ".///" was stripped to "". */
1570 name = savestring ("./", 2);
1571 else
1572 name = savestring (q, p - q);
1574 /* Add it to the front of the chain. */
1575 new1 = (struct nameseq *) xmalloc (size);
1576 new1->name = name;
1577 new1->next = new;
1578 new = new1;
1581 #ifndef NO_ARCHIVES
1583 /* Look for multi-word archive references.
1584 They are indicated by a elt ending with an unmatched `)' and
1585 an elt further down the chain (i.e., previous in the file list)
1586 with an unmatched `(' (e.g., "lib(mem"). */
1588 new1 = new;
1589 lastnew1 = 0;
1590 while (new1 != 0)
1591 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1592 && new1->name[strlen (new1->name) - 1] == ')'
1593 && index (new1->name, '(') == 0)
1595 /* NEW1 ends with a `)' but does not contain a `('.
1596 Look back for an elt with an opening `(' but no closing `)'. */
1598 struct nameseq *n = new1->next, *lastn = new1;
1599 char *paren;
1600 while (n != 0 && (paren = index (n->name, '(')) == 0)
1602 lastn = n;
1603 n = n->next;
1605 if (n != 0
1606 /* Ignore something starting with `(', as that cannot actually
1607 be an archive-member reference (and treating it as such
1608 results in an empty file name, which causes much lossage). */
1609 && n->name[0] != '(')
1611 /* N is the first element in the archive group.
1612 Its name looks like "lib(mem" (with no closing `)'). */
1614 char *libname;
1616 /* Copy "lib(" into LIBNAME. */
1617 ++paren;
1618 libname = (char *) alloca (paren - n->name + 1);
1619 bcopy (n->name, libname, paren - n->name);
1620 libname[paren - n->name] = '\0';
1622 if (*paren == '\0')
1624 /* N was just "lib(", part of something like "lib( a b)".
1625 Edit it out of the chain and free its storage. */
1626 lastn->next = n->next;
1627 free (n->name);
1628 free ((char *) n);
1629 /* LASTN->next is the new stopping elt for the loop below. */
1630 n = lastn->next;
1632 else
1634 /* Replace N's name with the full archive reference. */
1635 name = concat (libname, paren, ")");
1636 free (n->name);
1637 n->name = name;
1640 if (new1->name[1] == '\0')
1642 /* NEW1 is just ")", part of something like "lib(a b )".
1643 Omit it from the chain and free its storage. */
1644 if (lastnew1 == 0)
1645 new = new1->next;
1646 else
1647 lastnew1->next = new1->next;
1648 lastn = new1;
1649 new1 = new1->next;
1650 free (lastn->name);
1651 free ((char *) lastn);
1653 else
1655 /* Replace also NEW1->name, which already has closing `)'. */
1656 name = concat (libname, new1->name, "");
1657 free (new1->name);
1658 new1->name = name;
1659 new1 = new1->next;
1662 /* Trace back from NEW1 (the end of the list) until N
1663 (the beginning of the list), rewriting each name
1664 with the full archive reference. */
1666 while (new1 != n)
1668 name = concat (libname, new1->name, ")");
1669 free (new1->name);
1670 new1->name = name;
1671 lastnew1 = new1;
1672 new1 = new1->next;
1675 else
1677 /* No frobnication happening. Just step down the list. */
1678 lastnew1 = new1;
1679 new1 = new1->next;
1682 else
1684 lastnew1 = new1;
1685 new1 = new1->next;
1688 #endif
1690 *stringp = p;
1691 return new;
1694 /* Read a line of text from STREAM into LINEBUFFER.
1695 Combine continuation lines into one line.
1696 Return the number of actual lines read (> 1 if hacked continuation lines).
1699 static unsigned int
1700 readline (linebuffer, stream, filename, lineno)
1701 struct linebuffer *linebuffer;
1702 FILE *stream;
1703 char *filename;
1704 unsigned int lineno;
1706 char *buffer = linebuffer->buffer;
1707 register char *p = linebuffer->buffer;
1708 register char *end = p + linebuffer->size;
1709 register int len, lastlen = 0;
1710 register char *p2;
1711 register unsigned int nlines = 0;
1712 register int backslash;
1714 *p = '\0';
1716 while (fgets (p, end - p, stream) != 0)
1718 len = strlen (p);
1719 if (len == 0)
1721 /* This only happens when the first thing on the line is a '\0'.
1722 It is a pretty hopeless case, but (wonder of wonders) Athena
1723 lossage strikes again! (xmkmf puts NULs in its makefiles.)
1724 There is nothing really to be done; we synthesize a newline so
1725 the following line doesn't appear to be part of this line. */
1726 makefile_error (filename, lineno,
1727 "warning: NUL character seen; rest of line ignored");
1728 p[0] = '\n';
1729 len = 1;
1732 p += len;
1733 if (p[-1] != '\n')
1735 /* Probably ran out of buffer space. */
1736 register unsigned int p_off = p - buffer;
1737 linebuffer->size *= 2;
1738 buffer = (char *) xrealloc (buffer, linebuffer->size);
1739 p = buffer + p_off;
1740 end = buffer + linebuffer->size;
1741 linebuffer->buffer = buffer;
1742 *p = '\0';
1743 lastlen = len;
1744 continue;
1747 ++nlines;
1749 if (len == 1 && p > buffer)
1750 /* P is pointing at a newline and it's the beginning of
1751 the buffer returned by the last fgets call. However,
1752 it is not necessarily the beginning of a line if P is
1753 pointing past the beginning of the holding buffer.
1754 If the buffer was just enlarged (right before the newline),
1755 we must account for that, so we pretend that the two lines
1756 were one line. */
1757 len += lastlen;
1758 lastlen = len;
1759 backslash = 0;
1760 for (p2 = p - 2; --len > 0; --p2)
1762 if (*p2 == '\\')
1763 backslash = !backslash;
1764 else
1765 break;
1768 if (!backslash)
1770 p[-1] = '\0';
1771 break;
1774 if (end - p <= 1)
1776 /* Enlarge the buffer. */
1777 register unsigned int p_off = p - buffer;
1778 linebuffer->size *= 2;
1779 buffer = (char *) xrealloc (buffer, linebuffer->size);
1780 p = buffer + p_off;
1781 end = buffer + linebuffer->size;
1782 linebuffer->buffer = buffer;
1786 if (ferror (stream))
1787 pfatal_with_name (filename);
1789 return nlines;
1792 /* Construct the list of include directories
1793 from the arguments and the default list. */
1795 void
1796 construct_include_path (arg_dirs)
1797 char **arg_dirs;
1799 register unsigned int i;
1800 struct stat stbuf;
1802 /* Table to hold the dirs. */
1804 register unsigned int defsize = (sizeof (default_include_directories)
1805 / sizeof (default_include_directories[0]));
1806 register unsigned int max = 5;
1807 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
1808 register unsigned int idx = 0;
1810 /* First consider any dirs specified with -I switches.
1811 Ignore dirs that don't exist. */
1813 if (arg_dirs != 0)
1814 while (*arg_dirs != 0)
1816 char *dir = *arg_dirs++;
1818 if (dir[0] == '~')
1820 char *expanded = tilde_expand (dir);
1821 if (expanded != 0)
1822 dir = expanded;
1825 if (safe_stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
1827 if (idx == max - 1)
1829 max += 5;
1830 dirs = (char **)
1831 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
1833 dirs[idx++] = dir;
1835 else if (dir != arg_dirs[-1])
1836 free (dir);
1839 /* Now add at the end the standard default dirs. */
1841 for (i = 0; default_include_directories[i] != 0; ++i)
1842 if (safe_stat (default_include_directories[i], &stbuf) == 0
1843 && S_ISDIR (stbuf.st_mode))
1844 dirs[idx++] = default_include_directories[i];
1846 dirs[idx] = 0;
1848 /* Now compute the maximum length of any name in it. */
1850 max_incl_len = 0;
1851 for (i = 0; i < idx; ++i)
1853 unsigned int len = strlen (dirs[i]);
1854 /* If dir name is written with a trailing slash, discard it. */
1855 if (dirs[i][len - 1] == '/')
1856 /* We can't just clobber a null in because it may have come from
1857 a literal string and literal strings may not be writable. */
1858 dirs[i] = savestring (dirs[i], len - 1);
1859 if (len > max_incl_len)
1860 max_incl_len = len;
1863 include_directories = dirs;
1866 /* Expand ~ or ~USER at the beginning of NAME.
1867 Return a newly malloc'd string or 0. */
1869 char *
1870 tilde_expand (name)
1871 char *name;
1873 if (name[1] == '/' || name[1] == '\0')
1875 extern char *getenv ();
1876 char *home_dir;
1877 int is_variable;
1880 /* Turn off --warn-undefined-variables while we expand HOME. */
1881 int save = warn_undefined_variables_flag;
1882 warn_undefined_variables_flag = 0;
1884 home_dir = allocated_variable_expand ("$(HOME)");
1886 warn_undefined_variables_flag = save;
1889 is_variable = home_dir[0] != '\0';
1890 if (!is_variable)
1892 free (home_dir);
1893 home_dir = getenv ("HOME");
1895 if (home_dir == 0 || home_dir[0] == '\0')
1897 extern char *getlogin ();
1898 char *name = getlogin ();
1899 home_dir = 0;
1900 if (name != 0)
1902 struct passwd *p = getpwnam (name);
1903 if (p != 0)
1904 home_dir = p->pw_dir;
1907 if (home_dir != 0)
1909 char *new = concat (home_dir, "", name + 1);
1910 if (is_variable)
1911 free (home_dir);
1912 return new;
1915 else
1917 struct passwd *pwent;
1918 char *userend = index (name + 1, '/');
1919 if (userend != 0)
1920 *userend = '\0';
1921 pwent = getpwnam (name + 1);
1922 if (pwent != 0)
1924 if (userend == 0)
1925 return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
1926 else
1927 return concat (pwent->pw_dir, "/", userend + 1);
1929 else if (userend != 0)
1930 *userend = '/';
1933 return 0;
1936 /* Given a chain of struct nameseq's describing a sequence of filenames,
1937 in reverse of the intended order, return a new chain describing the
1938 result of globbing the filenames. The new chain is in forward order.
1939 The links of the old chain are freed or used in the new chain.
1940 Likewise for the names in the old chain.
1942 SIZE is how big to construct chain elements.
1943 This is useful if we want them actually to be other structures
1944 that have room for additional info. */
1946 struct nameseq *
1947 multi_glob (chain, size)
1948 struct nameseq *chain;
1949 unsigned int size;
1951 register struct nameseq *new = 0;
1952 register struct nameseq *old;
1953 struct nameseq *nexto;
1955 for (old = chain; old != 0; old = nexto)
1957 glob_t gl;
1958 #ifndef NO_ARCHIVES
1959 char *memname;
1960 #endif
1962 nexto = old->next;
1964 if (old->name[0] == '~')
1966 char *newname = tilde_expand (old->name);
1967 if (newname != 0)
1969 free (old->name);
1970 old->name = newname;
1974 #ifndef NO_ARCHIVES
1975 if (ar_name (old->name))
1977 /* OLD->name is an archive member reference.
1978 Replace it with the archive file name,
1979 and save the member name in MEMNAME.
1980 We will glob on the archive name and then
1981 reattach MEMNAME later. */
1982 char *arname;
1983 ar_parse_name (old->name, &arname, &memname);
1984 free (old->name);
1985 old->name = arname;
1987 else
1988 memname = 0;
1989 #endif
1991 switch (glob (old->name, GLOB_NOCHECK, NULL, &gl))
1993 case 0: /* Success. */
1995 register int i = gl.gl_pathc;
1996 while (i-- > 0)
1998 #ifndef NO_ARCHIVES
1999 if (memname != 0)
2001 /* Try to glob on MEMNAME within the archive. */
2002 struct nameseq *found
2003 = ar_glob (gl.gl_pathv[i], memname, size);
2004 if (found == 0)
2006 /* No matches. Use MEMNAME as-is. */
2007 struct nameseq *elt
2008 = (struct nameseq *) xmalloc (size);
2009 unsigned int alen = strlen (gl.gl_pathv[i]);
2010 unsigned int mlen = strlen (memname);
2011 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2012 bcopy (gl.gl_pathv[i], elt->name, alen);
2013 elt->name[alen] = '(';
2014 bcopy (memname, &elt->name[alen + 1], mlen);
2015 elt->name[alen + 1 + mlen] = ')';
2016 elt->name[alen + 1 + mlen + 1] = '\0';
2017 elt->next = new;
2018 new = elt;
2020 else
2022 /* Find the end of the FOUND chain. */
2023 struct nameseq *f = found;
2024 while (f->next != 0)
2025 f = f->next;
2027 /* Attach the chain being built to the end of the FOUND
2028 chain, and make FOUND the new NEW chain. */
2029 f->next = new;
2030 new = found;
2033 free (memname);
2035 else
2036 #endif
2038 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2039 elt->name = savestring (gl.gl_pathv[i],
2040 strlen (gl.gl_pathv[i]));
2041 elt->next = new;
2042 new = elt;
2045 globfree (&gl);
2046 free (old->name);
2047 free (old);
2048 break;
2051 case GLOB_NOSPACE:
2052 fatal ("virtual memory exhausted");
2053 break;
2055 default:
2056 old->next = new;
2057 new = old;
2058 break;
2062 return new;