Changes for GNU make 3.76
[make.git] / read.c
blobbf7f80ce462c1e996e165ef8a871ca694bb2f7c5
1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96,97 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 "dep.h"
21 #include "filedef.h"
22 #include "job.h"
23 #include "commands.h"
24 #include "variable.h"
26 /* This is POSIX.2, but most systems using -DPOSIX probably don't have it. */
27 #ifdef HAVE_GLOB_H
28 #include <glob.h>
29 #else
30 #include "glob/glob.h"
31 #endif
33 #ifndef WINDOWS32
34 #ifndef _AMIGA
35 #ifndef VMS
36 #include <pwd.h>
37 #else
38 struct passwd *getpwnam PARAMS ((char *name));
39 #endif
40 #endif
41 #endif /* !WINDOWS32 */
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 #if defined(WINDOWS32) && !defined(INCLUDEDIR)
86 * This completly up to the user when they install MSVC or other packages.
87 * This is defined as a placeholder.
89 #define INCLUDEDIR "."
90 #endif
91 INCLUDEDIR,
92 #ifndef _AMIGA
93 "/usr/gnu/include",
94 "/usr/local/include",
95 "/usr/include",
96 #endif
100 /* List of directories to search for include files in */
102 static char **include_directories;
104 /* Maximum length of an element of the above. */
106 static unsigned int max_incl_len;
108 /* The filename and pointer to line number of the
109 makefile currently being read in. */
111 char *reading_filename;
112 unsigned int *reading_lineno_ptr;
114 /* The chain of makefiles read by read_makefile. */
116 static struct dep *read_makefiles = 0;
118 static int read_makefile PARAMS ((char *filename, int flags));
119 static unsigned int readline PARAMS ((struct linebuffer *linebuffer, FILE *stream,
120 char *filename, unsigned int lineno));
121 static unsigned int do_define PARAMS ((char *name, unsigned int namelen, enum variable_origin origin,
122 unsigned int lineno, FILE *infile, char *filename));
123 static int conditional_line PARAMS ((char *line, char *filename, unsigned int lineno));
124 static void record_files PARAMS ((struct nameseq *filenames, char *pattern, char *pattern_percent,
125 struct dep *deps, unsigned int commands_started, char *commands,
126 unsigned int commands_idx, int two_colon, char *filename,
127 unsigned int lineno, int set_default));
129 /* Read in all the makefiles and return the chain of their names. */
131 struct dep *
132 read_all_makefiles (makefiles)
133 char **makefiles;
135 unsigned int num_makefiles = 0;
137 if (debug_flag)
138 puts ("Reading makefiles...");
140 /* If there's a non-null variable MAKEFILES, its value is a list of
141 files to read first thing. But don't let it prevent reading the
142 default makefiles and don't let the default goal come from there. */
145 char *value;
146 char *name, *p;
147 unsigned int length;
150 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
151 int save = warn_undefined_variables_flag;
152 warn_undefined_variables_flag = 0;
154 value = allocated_variable_expand ("$(MAKEFILES)");
156 warn_undefined_variables_flag = save;
159 /* Set NAME to the start of next token and LENGTH to its length.
160 MAKEFILES is updated for finding remaining tokens. */
161 p = value;
163 while ((name = find_next_token (&p, &length)) != 0)
165 if (*p != '\0')
166 *p++ = '\0';
167 (void) read_makefile (name,
168 RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
171 free (value);
174 /* Read makefiles specified with -f switches. */
176 if (makefiles != 0)
177 while (*makefiles != 0)
179 struct dep *tail = read_makefiles;
180 register struct dep *d;
182 if (! read_makefile (*makefiles, 0))
183 perror_with_name ("", *makefiles);
185 /* Find the right element of read_makefiles. */
186 d = read_makefiles;
187 while (d->next != tail)
188 d = d->next;
190 /* Use the storage read_makefile allocates. */
191 *makefiles = dep_name (d);
192 ++num_makefiles;
193 ++makefiles;
196 /* If there were no -f switches, try the default names. */
198 if (num_makefiles == 0)
200 static char *default_makefiles[] =
201 #ifdef VMS
202 /* all lower case since readdir() (the vms version) 'lowercasifies' */
203 { "makefile.vms", "gnumakefile", "makefile", 0 };
204 #else
205 #ifdef _AMIGA
206 { "GNUmakefile", "Makefile", "SMakefile", 0 };
207 #else /* !Amiga && !VMS */
208 { "GNUmakefile", "makefile", "Makefile", 0 };
209 #endif /* AMIGA */
210 #endif /* VMS */
211 register char **p = default_makefiles;
212 while (*p != 0 && !file_exists_p (*p))
213 ++p;
215 if (*p != 0)
217 if (! read_makefile (*p, 0))
218 perror_with_name ("", *p);
220 else
222 /* No default makefile was found. Add the default makefiles to the
223 `read_makefiles' chain so they will be updated if possible. */
224 struct dep *tail = read_makefiles;
225 /* Add them to the tail, after any MAKEFILES variable makefiles. */
226 while (tail != 0 && tail->next != 0)
227 tail = tail->next;
228 for (p = default_makefiles; *p != 0; ++p)
230 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
231 d->name = 0;
232 d->file = enter_file (*p);
233 d->file->dontcare = 1;
234 /* Tell update_goal_chain to bail out as soon as this file is
235 made, and main not to die if we can't make this file. */
236 d->changed = RM_DONTCARE;
237 if (tail == 0)
238 read_makefiles = d;
239 else
240 tail->next = d;
241 tail = d;
243 if (tail != 0)
244 tail->next = 0;
248 return read_makefiles;
251 /* Read file FILENAME as a makefile and add its contents to the data base.
253 FLAGS contains bits as above.
255 FILENAME is added to the `read_makefiles' chain.
257 Returns 1 if a file was found and read, 0 if not. */
259 static int
260 read_makefile (filename, flags)
261 char *filename;
262 int flags;
264 static char *collapsed = 0;
265 static unsigned int collapsed_length = 0;
266 register FILE *infile;
267 struct linebuffer lb;
268 unsigned int commands_len = 200;
269 char *commands = (char *) xmalloc (200);
270 unsigned int commands_idx = 0;
271 unsigned int commands_started;
272 register char *p;
273 char *p2;
274 int len;
275 int ignoring = 0, in_ignored_define = 0;
276 int no_targets = 0; /* Set when reading a rule without targets. */
277 char *passed_filename = filename;
279 struct nameseq *filenames = 0;
280 struct dep *deps;
281 unsigned int lineno = 1;
282 unsigned int nlines = 0;
283 int two_colon;
284 char *pattern = 0, *pattern_percent;
286 int makefile_errno;
287 #if defined (WINDOWS32) || defined (__MSDOS__)
288 int check_again;
289 #endif
291 #define record_waiting_files() \
292 do \
294 if (filenames != 0) \
295 record_files (filenames, pattern, pattern_percent, deps, \
296 commands_started, commands, commands_idx, \
297 two_colon, filename, lineno, \
298 !(flags & RM_NO_DEFAULT_GOAL)); \
299 filenames = 0; \
300 commands_idx = 0; \
301 pattern = 0; \
302 } while (0)
304 #ifdef lint /* Suppress `used before set' messages. */
305 two_colon = 0;
306 #endif
307 pattern_percent = 0;
308 commands_started = lineno;
310 if (debug_flag)
312 printf ("Reading makefile `%s'", filename);
313 if (flags & RM_NO_DEFAULT_GOAL)
314 printf (" (no default goal)");
315 if (flags & RM_INCLUDED)
316 printf (" (search path)");
317 if (flags & RM_DONTCARE)
318 printf (" (don't care)");
319 if (flags & RM_NO_TILDE)
320 printf (" (no ~ expansion)");
321 puts ("...");
324 /* First, get a stream to read. */
326 /* Expand ~ in FILENAME unless it came from `include',
327 in which case it was already done. */
328 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
330 char *expanded = tilde_expand (filename);
331 if (expanded != 0)
332 filename = expanded;
335 infile = fopen (filename, "r");
336 /* Save the error code so we print the right message later. */
337 makefile_errno = errno;
339 /* If the makefile wasn't found and it's either a makefile from
340 the `MAKEFILES' variable or an included makefile,
341 search the included makefile search path for this makefile. */
342 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
344 register unsigned int i;
345 for (i = 0; include_directories[i] != 0; ++i)
347 char *name = concat (include_directories[i], "/", filename);
348 infile = fopen (name, "r");
349 if (infile == 0)
350 free (name);
351 else
353 filename = name;
354 break;
359 /* Add FILENAME to the chain of read makefiles. */
360 deps = (struct dep *) xmalloc (sizeof (struct dep));
361 deps->next = read_makefiles;
362 read_makefiles = deps;
363 deps->name = 0;
364 deps->file = lookup_file (filename);
365 if (deps->file == 0)
367 deps->file = enter_file (savestring (filename, strlen (filename)));
368 if (flags & RM_DONTCARE)
369 deps->file->dontcare = 1;
371 if (filename != passed_filename)
372 free (filename);
373 filename = deps->file->name;
374 deps->changed = flags;
375 deps = 0;
377 /* If the makefile can't be found at all, give up entirely. */
379 if (infile == 0)
381 /* If we did some searching, errno has the error from the last
382 attempt, rather from FILENAME itself. Restore it in case the
383 caller wants to use it in a message. */
384 errno = makefile_errno;
385 return 0;
388 reading_filename = filename;
389 reading_lineno_ptr = &lineno;
391 /* Loop over lines in the file.
392 The strategy is to accumulate target names in FILENAMES, dependencies
393 in DEPS and commands in COMMANDS. These are used to define a rule
394 when the start of the next rule (or eof) is encountered. */
396 initbuffer (&lb);
398 while (!feof (infile))
400 lineno += nlines;
401 nlines = readline (&lb, infile, filename, lineno);
403 /* Check for a shell command line first.
404 If it is not one, we can stop treating tab specially. */
405 if (lb.buffer[0] == '\t')
407 /* This line is a probably shell command. */
408 unsigned int len;
410 if (no_targets)
411 /* Ignore the commands in a rule with no targets. */
412 continue;
414 /* If there is no preceding rule line, don't treat this line
415 as a command, even though it begins with a tab character.
416 SunOS 4 make appears to behave this way. */
418 if (filenames != 0)
420 if (ignoring)
421 /* Yep, this is a shell command, and we don't care. */
422 continue;
424 /* Append this command line to the line being accumulated. */
425 p = lb.buffer;
426 if (commands_idx == 0)
427 commands_started = lineno;
428 len = strlen (p);
429 if (len + 1 + commands_idx > commands_len)
431 commands_len = (len + 1 + commands_idx) * 2;
432 commands = (char *) xrealloc (commands, commands_len);
434 bcopy (p, &commands[commands_idx], len);
435 commands_idx += len;
436 commands[commands_idx++] = '\n';
438 continue;
442 /* This line is not a shell command line. Don't worry about tabs. */
444 if (collapsed_length < lb.size)
446 collapsed_length = lb.size;
447 if (collapsed != 0)
448 free (collapsed);
449 collapsed = (char *) xmalloc (collapsed_length);
451 strcpy (collapsed, lb.buffer);
452 /* Collapse continuation lines. */
453 collapse_continuations (collapsed);
454 remove_comments (collapsed);
456 /* strncmp is first to avoid dereferencing out into space. */
457 #define word1eq(s, l) (len == l && !strncmp (s, p, l))
458 p = collapsed;
459 while (isspace (*p))
460 ++p;
461 if (*p == '\0')
462 /* This line is completely empty. */
463 continue;
465 /* Find the end of the first token */
466 for (p2 = p+1; *p2 != '\0' && !isspace(*p2); ++p2)
468 len = p2 - p;
470 /* Find the start of the second token. If it's a `:', jump past
471 preprocessor stuff since it can't be that--this allows targets named
472 `export', etc. */
473 while (isspace (*p2))
474 ++p2;
475 if (*p2 == '\0')
476 p2 = NULL;
477 else if (p2[0] == ':' && p2[1] == '\0')
478 goto check_var;
480 /* We must first check for conditional and `define' directives before
481 ignoring anything, since they control what we will do with
482 following lines. */
484 if (!in_ignored_define
485 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
486 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
487 || word1eq ("else", 4) || word1eq ("endif", 5)))
489 int i = conditional_line (p, filename, lineno);
490 if (i >= 0)
491 ignoring = i;
492 else
493 makefile_fatal (filename, lineno,
494 "invalid syntax in conditional");
495 continue;
497 else if (word1eq ("endef", 5))
499 if (in_ignored_define)
500 in_ignored_define = 0;
501 else
502 makefile_fatal (filename, lineno, "extraneous `endef'");
503 continue;
505 else if (word1eq ("define", 6))
507 if (ignoring)
508 in_ignored_define = 1;
509 else
511 p2 = next_token (p + 6);
512 /* Let the variable name be the whole rest of the line,
513 with trailing blanks stripped (comments have already been
514 removed), so it could be a complex variable/function
515 reference that might contain blanks. */
516 p = index (p2, '\0');
517 while (isblank (p[-1]))
518 --p;
519 lineno = do_define (p2, p - p2, o_file,
520 lineno, infile, filename);
522 continue;
524 else if (word1eq ("override", 8))
526 p2 = next_token (p + 8);
527 if (p2 == 0)
528 makefile_error (filename, lineno, "empty `override' directive");
529 if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
531 if (ignoring)
532 in_ignored_define = 1;
533 else
535 p2 = next_token (p2 + 6);
536 /* Let the variable name be the whole rest of the line,
537 with trailing blanks stripped (comments have already been
538 removed), so it could be a complex variable/function
539 reference that might contain blanks. */
540 p = index (p2, '\0');
541 while (isblank (p[-1]))
542 --p;
543 lineno = do_define (p2, p - p2, o_override,
544 lineno, infile, filename);
547 else if (!ignoring
548 && !try_variable_definition (filename, lineno,
549 p2, o_override))
550 makefile_error (filename, lineno, "empty `override' directive");
552 continue;
555 if (ignoring)
556 /* Ignore the line. We continue here so conditionals
557 can appear in the middle of a rule. */
558 continue;
559 else if (word1eq ("export", 6))
561 struct variable *v;
562 p2 = next_token (p + 6);
563 if (*p2 == '\0')
564 export_all_variables = 1;
565 v = try_variable_definition (filename, lineno, p2, o_file);
566 if (v != 0)
567 v->export = v_export;
568 else
570 unsigned int len;
571 for (p = find_next_token (&p2, &len); p != 0;
572 p = find_next_token (&p2, &len))
574 v = lookup_variable (p, len);
575 if (v == 0)
576 v = define_variable (p, len, "", o_file, 0);
577 v->export = v_export;
581 else if (word1eq ("unexport", 8))
583 unsigned int len;
584 struct variable *v;
585 p2 = next_token (p + 8);
586 if (*p2 == '\0')
587 export_all_variables = 0;
588 for (p = find_next_token (&p2, &len); p != 0;
589 p = find_next_token (&p2, &len))
591 v = lookup_variable (p, len);
592 if (v == 0)
593 v = define_variable (p, len, "", o_file, 0);
594 v->export = v_noexport;
597 else if (word1eq ("vpath", 5))
599 char *pattern;
600 unsigned int len;
601 p2 = variable_expand (p + 5);
602 p = find_next_token (&p2, &len);
603 if (p != 0)
605 pattern = savestring (p, len);
606 p = find_next_token (&p2, &len);
607 /* No searchpath means remove all previous
608 selective VPATH's with the same pattern. */
610 else
611 /* No pattern means remove all previous selective VPATH's. */
612 pattern = 0;
613 construct_vpath_list (pattern, p);
614 if (pattern != 0)
615 free (pattern);
617 else
618 check_var:
619 if (word1eq ("include", 7) || word1eq ("-include", 8)
620 || word1eq ("sinclude", 8))
622 /* We have found an `include' line specifying a nested
623 makefile to be read at this point. */
624 struct conditionals *save, new_conditionals;
625 struct nameseq *files;
626 /* "-include" (vs "include") says no error if the file does not
627 exist. "sinclude" is an alias for this from SGI. */
628 int noerror = p[0] != 'i';
630 p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
631 if (*p == '\0')
633 makefile_error (filename, lineno,
634 "no file name for `%sinclude'",
635 noerror ? "-" : "");
636 continue;
639 /* Parse the list of file names. */
640 p2 = p;
641 files = multi_glob (parse_file_seq (&p2, '\0',
642 sizeof (struct nameseq),
644 sizeof (struct nameseq));
645 free (p);
647 /* Save the state of conditionals and start
648 the included makefile with a clean slate. */
649 save = conditionals;
650 bzero ((char *) &new_conditionals, sizeof new_conditionals);
651 conditionals = &new_conditionals;
653 /* Record the rules that are waiting so they will determine
654 the default goal before those in the included makefile. */
655 record_waiting_files ();
657 /* Read each included makefile. */
658 while (files != 0)
660 struct nameseq *next = files->next;
661 char *name = files->name;
662 free ((char *)files);
663 files = next;
665 if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
666 | (noerror ? RM_DONTCARE : 0)))
667 && ! noerror)
668 makefile_error (filename, lineno,
669 "%s: %s", name, strerror (errno));
672 /* Free any space allocated by conditional_line. */
673 if (conditionals->ignoring)
674 free (conditionals->ignoring);
675 if (conditionals->seen_else)
676 free (conditionals->seen_else);
678 /* Restore state. */
679 conditionals = save;
680 reading_filename = filename;
681 reading_lineno_ptr = &lineno;
683 #undef word1eq
684 else if (try_variable_definition (filename, lineno, p, o_file))
685 /* This line has been dealt with. */
687 else if (lb.buffer[0] == '\t')
689 p = collapsed; /* Ignore comments. */
690 while (isblank (*p))
691 ++p;
692 if (*p == '\0')
693 /* The line is completely blank; that is harmless. */
694 continue;
695 /* This line starts with a tab but was not caught above
696 because there was no preceding target, and the line
697 might have been usable as a variable definition.
698 But now it is definitely lossage. */
699 makefile_fatal (filename, lineno,
700 "commands commence before first target");
702 else
704 /* This line describes some target files. */
706 char *cmdleft;
708 /* Record the previous rule. */
710 record_waiting_files ();
712 /* Search the line for an unquoted ; that is not after an
713 unquoted #. */
714 cmdleft = find_char_unquote (lb.buffer, ";#", 0);
715 if (cmdleft != 0 && *cmdleft == '#')
717 /* We found a comment before a semicolon. */
718 *cmdleft = '\0';
719 cmdleft = 0;
721 else if (cmdleft != 0)
722 /* Found one. Cut the line short there before expanding it. */
723 *cmdleft = '\0';
725 collapse_continuations (lb.buffer);
727 /* Expand variable and function references before doing anything
728 else so that special characters can be inside variables. */
729 p = variable_expand (lb.buffer);
731 if (cmdleft == 0)
732 /* Look for a semicolon in the expanded line. */
733 cmdleft = find_char_unquote (p, ";", 0);
735 if (cmdleft != 0)
736 /* Cut the line short at the semicolon. */
737 *cmdleft = '\0';
739 p2 = next_token (p);
740 if (*p2 == '\0')
742 if (cmdleft != 0)
743 makefile_fatal (filename, lineno,
744 "missing rule before commands");
745 else
746 /* This line contained a variable reference that
747 expanded to nothing but whitespace. */
748 continue;
750 else if (*p2 == ':')
752 /* We accept and ignore rules without targets for
753 compatibility with SunOS 4 make. */
754 no_targets = 1;
755 continue;
758 filenames = multi_glob (parse_file_seq (&p2, ':',
759 sizeof (struct nameseq),
761 sizeof (struct nameseq));
762 if (*p2++ == '\0')
763 makefile_fatal (filename, lineno, "missing separator");
764 /* Is this a one-colon or two-colon entry? */
765 two_colon = *p2 == ':';
766 if (two_colon)
767 p2++;
769 /* We have some targets, so don't ignore the following commands. */
770 no_targets = 0;
772 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
773 p = index (p2, ':');
774 while (p != 0 && p[-1] == '\\')
776 register char *q = &p[-1];
777 register int backslash = 0;
778 while (*q-- == '\\')
779 backslash = !backslash;
780 if (backslash)
781 p = index (p + 1, ':');
782 else
783 break;
785 #ifdef _AMIGA
786 /* Here, the situation is quite complicated. Let's have a look
787 at a couple of targets:
789 install: dev:make
791 dev:make: make
793 dev:make:: xyz
795 The rule is that it's only a target, if there are TWO :'s
796 OR a space around the :.
798 if (p && !(isspace(p[1]) || !p[1] || isspace(p[-1])))
799 p = 0;
800 #endif
801 #if defined (WINDOWS32) || defined (__MSDOS__)
802 do {
803 check_again = 0;
804 /* For MSDOS and WINDOWS32, skip a "C:\..." or a "C:/..." */
805 if (p != 0 && (p[1] == '\\' || p[1] == '/') && isalpha (p[-1])) {
806 p = index(p + 1, ':');
807 check_again = 1;
809 } while (check_again);
810 #endif
811 if (p != 0)
813 struct nameseq *target;
814 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
815 ++p2;
816 if (target == 0)
817 makefile_fatal (filename, lineno, "missing target pattern");
818 else if (target->next != 0)
819 makefile_fatal (filename, lineno, "multiple target patterns");
820 pattern = target->name;
821 pattern_percent = find_percent (pattern);
822 if (pattern_percent == 0)
823 makefile_fatal (filename, lineno,
824 "target pattern contains no `%%'");
826 else
827 pattern = 0;
829 /* Parse the dependencies. */
830 deps = (struct dep *)
831 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
832 sizeof (struct dep));
834 commands_idx = 0;
835 if (cmdleft != 0)
837 /* Semicolon means rest of line is a command. */
838 unsigned int len = strlen (cmdleft + 1);
840 commands_started = lineno;
842 /* Add this command line to the buffer. */
843 if (len + 2 > commands_len)
845 commands_len = (len + 2) * 2;
846 commands = (char *) xrealloc (commands, commands_len);
848 bcopy (cmdleft + 1, commands, len);
849 commands_idx += len;
850 commands[commands_idx++] = '\n';
853 continue;
856 /* We get here except in the case that we just read a rule line.
857 Record now the last rule we read, so following spurious
858 commands are properly diagnosed. */
859 record_waiting_files ();
860 no_targets = 0;
863 if (conditionals->if_cmds)
864 makefile_fatal (filename, lineno, "missing `endif'");
866 /* At eof, record the last rule. */
867 record_waiting_files ();
869 freebuffer (&lb);
870 free ((char *) commands);
871 fclose (infile);
873 reading_filename = 0;
874 reading_lineno_ptr = 0;
876 return 1;
879 /* Execute a `define' directive.
880 The first line has already been read, and NAME is the name of
881 the variable to be defined. The following lines remain to be read.
882 LINENO, INFILE and FILENAME refer to the makefile being read.
883 The value returned is LINENO, updated for lines read here. */
885 static unsigned int
886 do_define (name, namelen, origin, lineno, infile, filename)
887 char *name;
888 unsigned int namelen;
889 enum variable_origin origin;
890 unsigned int lineno;
891 FILE *infile;
892 char *filename;
894 struct linebuffer lb;
895 unsigned int nlines = 0;
896 unsigned int length = 100;
897 char *definition = (char *) xmalloc (100);
898 register unsigned int idx = 0;
899 register char *p;
901 /* Expand the variable name. */
902 char *var = (char *) alloca (namelen + 1);
903 bcopy (name, var, namelen);
904 var[namelen] = '\0';
905 var = variable_expand (var);
907 initbuffer (&lb);
908 while (!feof (infile))
910 unsigned int len;
912 lineno += nlines;
913 nlines = readline (&lb, infile, filename, lineno);
915 collapse_continuations (lb.buffer);
917 p = next_token (lb.buffer);
918 len = strlen (p);
919 if ((len == 5 || (len > 5 && isblank (p[5])))
920 && !strncmp (p, "endef", 5))
922 p += 5;
923 remove_comments (p);
924 if (*next_token (p) != '\0')
925 makefile_error (filename, lineno,
926 "Extraneous text after `endef' directive");
927 /* Define the variable. */
928 if (idx == 0)
929 definition[0] = '\0';
930 else
931 definition[idx - 1] = '\0';
932 (void) define_variable (var, strlen (var), definition, origin, 1);
933 free (definition);
934 freebuffer (&lb);
935 return (lineno + nlines);
937 else
939 len = strlen (lb.buffer);
940 /* Increase the buffer size if necessary. */
941 if (idx + len + 1 > length)
943 length = (idx + len) * 2;
944 definition = (char *) xrealloc (definition, length + 1);
947 bcopy (lb.buffer, &definition[idx], len);
948 idx += len;
949 /* Separate lines with a newline. */
950 definition[idx++] = '\n';
954 /* No `endef'!! */
955 makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
957 /* NOTREACHED */
958 return 0;
961 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
962 "ifneq", "else" and "endif".
963 LINE is the input line, with the command as its first word.
965 FILENAME and LINENO are the filename and line number in the
966 current makefile. They are used for error messages.
968 Value is -1 if the line is invalid,
969 0 if following text should be interpreted,
970 1 if following text should be ignored. */
972 static int
973 conditional_line (line, filename, lineno)
974 char *line;
975 char *filename;
976 unsigned int lineno;
978 int notdef;
979 char *cmdname;
980 register unsigned int i;
982 if (*line == 'i')
984 /* It's an "if..." command. */
985 notdef = line[2] == 'n';
986 if (notdef)
988 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
989 line += cmdname[3] == 'd' ? 7 : 6;
991 else
993 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
994 line += cmdname[2] == 'd' ? 6 : 5;
997 else
999 /* It's an "else" or "endif" command. */
1000 notdef = line[1] == 'n';
1001 cmdname = notdef ? "endif" : "else";
1002 line += notdef ? 5 : 4;
1005 line = next_token (line);
1007 if (*cmdname == 'e')
1009 if (*line != '\0')
1010 makefile_error (filename, lineno,
1011 "Extraneous text after `%s' directive",
1012 cmdname);
1013 /* "Else" or "endif". */
1014 if (conditionals->if_cmds == 0)
1015 makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
1016 /* NOTDEF indicates an `endif' command. */
1017 if (notdef)
1018 --conditionals->if_cmds;
1019 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1020 makefile_fatal (filename, lineno, "only one `else' per conditional");
1021 else
1023 /* Toggle the state of ignorance. */
1024 conditionals->ignoring[conditionals->if_cmds - 1]
1025 = !conditionals->ignoring[conditionals->if_cmds - 1];
1026 /* Record that we have seen an `else' in this conditional.
1027 A second `else' will be erroneous. */
1028 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1030 for (i = 0; i < conditionals->if_cmds; ++i)
1031 if (conditionals->ignoring[i])
1032 return 1;
1033 return 0;
1036 if (conditionals->allocated == 0)
1038 conditionals->allocated = 5;
1039 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1040 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1043 ++conditionals->if_cmds;
1044 if (conditionals->if_cmds > conditionals->allocated)
1046 conditionals->allocated += 5;
1047 conditionals->ignoring = (char *)
1048 xrealloc (conditionals->ignoring, conditionals->allocated);
1049 conditionals->seen_else = (char *)
1050 xrealloc (conditionals->seen_else, conditionals->allocated);
1053 /* Record that we have seen an `if...' but no `else' so far. */
1054 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1056 /* Search through the stack to see if we're already ignoring. */
1057 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1058 if (conditionals->ignoring[i])
1060 /* We are already ignoring, so just push a level
1061 to match the next "else" or "endif", and keep ignoring.
1062 We don't want to expand variables in the condition. */
1063 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1064 return 1;
1067 if (cmdname[notdef ? 3 : 2] == 'd')
1069 /* "Ifdef" or "ifndef". */
1070 struct variable *v;
1071 register char *p = end_of_token (line);
1072 i = p - line;
1073 p = next_token (p);
1074 if (*p != '\0')
1075 return -1;
1076 v = lookup_variable (line, i);
1077 conditionals->ignoring[conditionals->if_cmds - 1]
1078 = (v != 0 && *v->value != '\0') == notdef;
1080 else
1082 /* "Ifeq" or "ifneq". */
1083 char *s1, *s2;
1084 unsigned int len;
1085 char termin = *line == '(' ? ',' : *line;
1087 if (termin != ',' && termin != '"' && termin != '\'')
1088 return -1;
1090 s1 = ++line;
1091 /* Find the end of the first string. */
1092 if (termin == ',')
1094 register int count = 0;
1095 for (; *line != '\0'; ++line)
1096 if (*line == '(')
1097 ++count;
1098 else if (*line == ')')
1099 --count;
1100 else if (*line == ',' && count <= 0)
1101 break;
1103 else
1104 while (*line != '\0' && *line != termin)
1105 ++line;
1107 if (*line == '\0')
1108 return -1;
1110 if (termin == ',')
1112 /* Strip blanks after the first string. */
1113 char *p = line++;
1114 while (isblank (p[-1]))
1115 --p;
1116 *p = '\0';
1118 else
1119 *line++ = '\0';
1121 s2 = variable_expand (s1);
1122 /* We must allocate a new copy of the expanded string because
1123 variable_expand re-uses the same buffer. */
1124 len = strlen (s2);
1125 s1 = (char *) alloca (len + 1);
1126 bcopy (s2, s1, len + 1);
1128 if (termin != ',')
1129 /* Find the start of the second string. */
1130 line = next_token (line);
1132 termin = termin == ',' ? ')' : *line;
1133 if (termin != ')' && termin != '"' && termin != '\'')
1134 return -1;
1136 /* Find the end of the second string. */
1137 if (termin == ')')
1139 register int count = 0;
1140 s2 = next_token (line);
1141 for (line = s2; *line != '\0'; ++line)
1143 if (*line == '(')
1144 ++count;
1145 else if (*line == ')')
1146 if (count <= 0)
1147 break;
1148 else
1149 --count;
1152 else
1154 ++line;
1155 s2 = line;
1156 while (*line != '\0' && *line != termin)
1157 ++line;
1160 if (*line == '\0')
1161 return -1;
1163 *line = '\0';
1164 line = next_token (++line);
1165 if (*line != '\0')
1166 makefile_error (filename, lineno,
1167 "Extraneous text after `%s' directive",
1168 cmdname);
1170 s2 = variable_expand (s2);
1171 conditionals->ignoring[conditionals->if_cmds - 1]
1172 = streq (s1, s2) == notdef;
1175 /* Search through the stack to see if we're ignoring. */
1176 for (i = 0; i < conditionals->if_cmds; ++i)
1177 if (conditionals->ignoring[i])
1178 return 1;
1179 return 0;
1182 /* Remove duplicate dependencies in CHAIN. */
1184 void
1185 uniquize_deps (chain)
1186 struct dep *chain;
1188 register struct dep *d;
1190 /* Make sure that no dependencies are repeated. This does not
1191 really matter for the purpose of updating targets, but it
1192 might make some names be listed twice for $^ and $?. */
1194 for (d = chain; d != 0; d = d->next)
1196 struct dep *last, *next;
1198 last = d;
1199 next = d->next;
1200 while (next != 0)
1201 if (streq (dep_name (d), dep_name (next)))
1203 struct dep *n = next->next;
1204 last->next = n;
1205 if (next->name != 0 && next->name != d->name)
1206 free (next->name);
1207 if (next != d)
1208 free ((char *) next);
1209 next = n;
1211 else
1213 last = next;
1214 next = next->next;
1219 /* Record a description line for files FILENAMES,
1220 with dependencies DEPS, commands to execute described
1221 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1222 TWO_COLON is nonzero if a double colon was used.
1223 If not nil, PATTERN is the `%' pattern to make this
1224 a static pattern rule, and PATTERN_PERCENT is a pointer
1225 to the `%' within it.
1227 The links of FILENAMES are freed, and so are any names in it
1228 that are not incorporated into other data structures. */
1230 static void
1231 record_files (filenames, pattern, pattern_percent, deps, commands_started,
1232 commands, commands_idx, two_colon, filename, lineno, set_default)
1233 struct nameseq *filenames;
1234 char *pattern, *pattern_percent;
1235 struct dep *deps;
1236 unsigned int commands_started;
1237 char *commands;
1238 unsigned int commands_idx;
1239 int two_colon;
1240 char *filename;
1241 unsigned int lineno;
1242 int set_default;
1244 struct nameseq *nextf;
1245 int implicit = 0;
1246 unsigned int max_targets, target_idx;
1247 char **targets = 0, **target_percents = 0;
1248 struct commands *cmds;
1250 if (commands_idx > 0)
1252 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1253 cmds->filename = filename;
1254 cmds->lineno = commands_started;
1255 cmds->commands = savestring (commands, commands_idx);
1256 cmds->command_lines = 0;
1258 else
1259 cmds = 0;
1261 for (; filenames != 0; filenames = nextf)
1263 register char *name = filenames->name;
1264 register struct file *f;
1265 register struct dep *d;
1266 struct dep *this;
1267 char *implicit_percent;
1269 nextf = filenames->next;
1270 free ((char *) filenames);
1272 implicit_percent = find_percent (name);
1273 implicit |= implicit_percent != 0;
1275 if (implicit && pattern != 0)
1276 makefile_fatal (filename, lineno,
1277 "mixed implicit and static pattern rules");
1279 if (implicit && implicit_percent == 0)
1280 makefile_fatal (filename, lineno, "mixed implicit and normal rules");
1282 if (implicit)
1284 if (targets == 0)
1286 max_targets = 5;
1287 targets = (char **) xmalloc (5 * sizeof (char *));
1288 target_percents = (char **) xmalloc (5 * sizeof (char *));
1289 target_idx = 0;
1291 else if (target_idx == max_targets - 1)
1293 max_targets += 5;
1294 targets = (char **) xrealloc ((char *) targets,
1295 max_targets * sizeof (char *));
1296 target_percents
1297 = (char **) xrealloc ((char *) target_percents,
1298 max_targets * sizeof (char *));
1300 targets[target_idx] = name;
1301 target_percents[target_idx] = implicit_percent;
1302 ++target_idx;
1303 continue;
1306 /* If there are multiple filenames, copy the chain DEPS
1307 for all but the last one. It is not safe for the same deps
1308 to go in more than one place in the data base. */
1309 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1311 if (pattern != 0)
1312 /* If this is an extended static rule:
1313 `targets: target%pattern: dep%pattern; cmds',
1314 translate each dependency pattern into a plain filename
1315 using the target pattern and this target's name. */
1316 if (!pattern_matches (pattern, pattern_percent, name))
1318 /* Give a warning if the rule is meaningless. */
1319 makefile_error (filename, lineno,
1320 "target `%s' doesn't match the target pattern",
1321 name);
1322 this = 0;
1324 else
1326 /* We use patsubst_expand to do the work of translating
1327 the target pattern, the target's name and the dependencies'
1328 patterns into plain dependency names. */
1329 char *buffer = variable_expand ("");
1331 for (d = this; d != 0; d = d->next)
1333 char *o;
1334 char *percent = find_percent (d->name);
1335 if (percent == 0)
1336 continue;
1337 o = patsubst_expand (buffer, name, pattern, d->name,
1338 pattern_percent, percent);
1339 free (d->name);
1340 d->name = savestring (buffer, o - buffer);
1344 if (!two_colon)
1346 /* Single-colon. Combine these dependencies
1347 with others in file's existing record, if any. */
1348 f = enter_file (name);
1350 if (f->double_colon)
1351 makefile_fatal (filename, lineno,
1352 "target file `%s' has both : and :: entries",
1353 f->name);
1355 /* If CMDS == F->CMDS, this target was listed in this rule
1356 more than once. Just give a warning since this is harmless. */
1357 if (cmds != 0 && cmds == f->cmds)
1358 makefile_error
1359 (filename, lineno,
1360 "target `%s' given more than once in the same rule.",
1361 f->name);
1363 /* Check for two single-colon entries both with commands.
1364 Check is_target so that we don't lose on files such as .c.o
1365 whose commands were preinitialized. */
1366 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1368 makefile_error (cmds->filename, cmds->lineno,
1369 "warning: overriding commands for target `%s'",
1370 f->name);
1371 makefile_error (f->cmds->filename, f->cmds->lineno,
1372 "warning: ignoring old commands for target `%s'",
1373 f->name);
1376 f->is_target = 1;
1378 /* Defining .DEFAULT with no deps or cmds clears it. */
1379 if (f == default_file && this == 0 && cmds == 0)
1380 f->cmds = 0;
1381 if (cmds != 0)
1382 f->cmds = cmds;
1383 /* Defining .SUFFIXES with no dependencies
1384 clears out the list of suffixes. */
1385 if (f == suffix_file && this == 0)
1387 d = f->deps;
1388 while (d != 0)
1390 struct dep *nextd = d->next;
1391 free (d->name);
1392 free ((char *)d);
1393 d = nextd;
1395 f->deps = 0;
1397 else if (f->deps != 0)
1399 /* Add the file's old deps and the new ones in THIS together. */
1401 struct dep *firstdeps, *moredeps;
1402 if (cmds != 0)
1404 /* This is the rule with commands, so put its deps first.
1405 The rationale behind this is that $< expands to the
1406 first dep in the chain, and commands use $< expecting
1407 to get the dep that rule specifies. */
1408 firstdeps = this;
1409 moredeps = f->deps;
1411 else
1413 /* Append the new deps to the old ones. */
1414 firstdeps = f->deps;
1415 moredeps = this;
1418 if (firstdeps == 0)
1419 firstdeps = moredeps;
1420 else
1422 d = firstdeps;
1423 while (d->next != 0)
1424 d = d->next;
1425 d->next = moredeps;
1428 f->deps = firstdeps;
1430 else
1431 f->deps = this;
1433 /* If this is a static pattern rule, set the file's stem to
1434 the part of its name that matched the `%' in the pattern,
1435 so you can use $* in the commands. */
1436 if (pattern != 0)
1438 static char *percent = "%";
1439 char *buffer = variable_expand ("");
1440 char *o = patsubst_expand (buffer, name, pattern, percent,
1441 pattern_percent, percent);
1442 f->stem = savestring (buffer, o - buffer);
1445 else
1447 /* Double-colon. Make a new record
1448 even if the file already has one. */
1449 f = lookup_file (name);
1450 /* Check for both : and :: rules. Check is_target so
1451 we don't lose on default suffix rules or makefiles. */
1452 if (f != 0 && f->is_target && !f->double_colon)
1453 makefile_fatal (filename, lineno,
1454 "target file `%s' has both : and :: entries",
1455 f->name);
1456 f = enter_file (name);
1457 /* If there was an existing entry and it was a double-colon
1458 entry, enter_file will have returned a new one, making it the
1459 prev pointer of the old one, and setting its double_colon
1460 pointer to the first one. */
1461 if (f->double_colon == 0)
1462 /* This is the first entry for this name, so we must
1463 set its double_colon pointer to itself. */
1464 f->double_colon = f;
1465 f->is_target = 1;
1466 f->deps = this;
1467 f->cmds = cmds;
1470 /* Free name if not needed further. */
1471 if (f != 0 && name != f->name
1472 && (name < f->name || name > f->name + strlen (f->name)))
1474 free (name);
1475 name = f->name;
1478 /* See if this is first target seen whose name does
1479 not start with a `.', unless it contains a slash. */
1480 if (default_goal_file == 0 && set_default
1481 && (*name != '.' || index (name, '/') != 0
1482 #ifdef __MSDOS__
1483 || index (name, '\\') != 0
1484 #endif
1487 int reject = 0;
1489 /* If this file is a suffix, don't
1490 let it be the default goal file. */
1492 for (d = suffix_file->deps; d != 0; d = d->next)
1494 register struct dep *d2;
1495 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1497 reject = 1;
1498 break;
1500 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1502 register unsigned int len = strlen (dep_name (d2));
1503 if (strncmp (name, dep_name (d2), len))
1504 continue;
1505 if (streq (name + len, dep_name (d)))
1507 reject = 1;
1508 break;
1511 if (reject)
1512 break;
1515 if (!reject)
1516 default_goal_file = f;
1520 if (implicit)
1522 targets[target_idx] = 0;
1523 target_percents[target_idx] = 0;
1524 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1525 free ((char *) target_percents);
1529 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1530 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1531 Quoting backslashes are removed from STRING by compacting it into
1532 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1533 one, or nil if there are none. */
1535 char *
1536 find_char_unquote (string, stopchars, blank)
1537 char *string;
1538 char *stopchars;
1539 int blank;
1541 unsigned int string_len = 0;
1542 register char *p = string;
1544 while (1)
1546 while (*p != '\0' && index (stopchars, *p) == 0
1547 && (!blank || !isblank (*p)))
1548 ++p;
1549 if (*p == '\0')
1550 break;
1552 if (p > string && p[-1] == '\\')
1554 /* Search for more backslashes. */
1555 register int i = -2;
1556 while (&p[i] >= string && p[i] == '\\')
1557 --i;
1558 ++i;
1559 /* Only compute the length if really needed. */
1560 if (string_len == 0)
1561 string_len = strlen (string);
1562 /* The number of backslashes is now -I.
1563 Copy P over itself to swallow half of them. */
1564 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1565 p += i / 2;
1566 if (i % 2 == 0)
1567 /* All the backslashes quoted each other; the STOPCHAR was
1568 unquoted. */
1569 return p;
1571 /* The STOPCHAR was quoted by a backslash. Look for another. */
1573 else
1574 /* No backslash in sight. */
1575 return p;
1578 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1579 return 0;
1582 /* Search PATTERN for an unquoted %. */
1584 char *
1585 find_percent (pattern)
1586 char *pattern;
1588 return find_char_unquote (pattern, "%", 0);
1591 /* Parse a string into a sequence of filenames represented as a
1592 chain of struct nameseq's in reverse order and return that chain.
1594 The string is passed as STRINGP, the address of a string pointer.
1595 The string pointer is updated to point at the first character
1596 not parsed, which either is a null char or equals STOPCHAR.
1598 SIZE is how big to construct chain elements.
1599 This is useful if we want them actually to be other structures
1600 that have room for additional info.
1602 If STRIP is nonzero, strip `./'s off the beginning. */
1604 struct nameseq *
1605 parse_file_seq (stringp, stopchar, size, strip)
1606 char **stringp;
1607 int stopchar;
1608 unsigned int size;
1609 int strip;
1611 register struct nameseq *new = 0;
1612 register struct nameseq *new1, *lastnew1;
1613 register char *p = *stringp;
1614 char *q;
1615 char *name;
1616 char stopchars[3];
1618 #ifdef VMS
1619 stopchars[0] = ',';
1620 stopchars[1] = stopchar;
1621 stopchars[2] = '\0';
1622 #else
1623 stopchars[0] = stopchar;
1624 stopchars[1] = '\0';
1625 #endif
1627 while (1)
1629 /* Skip whitespace; see if any more names are left. */
1630 p = next_token (p);
1631 if (*p == '\0')
1632 break;
1633 if (*p == stopchar)
1634 break;
1636 /* Yes, find end of next name. */
1637 q = p;
1638 p = find_char_unquote (q, stopchars, 1);
1639 #ifdef VMS
1640 /* convert comma separated list to space separated */
1641 if (p && *p == ',')
1642 *p =' ';
1643 #endif
1644 #ifdef __MSDOS__
1645 /* For MS-DOS, skip a "C:\..." or a "C:/..." until we find a
1646 first colon which isn't followed by a slash or a backslash. */
1647 if (stopchar == ':')
1648 while (p != 0 && (p[1] == '\\' || p[1] == '/') && isalpha (p[-1]))
1649 p = find_char_unquote (p + 1, stopchars, 1);
1650 #endif
1651 #ifdef _AMIGA
1652 if (stopchar == ':' && p && *p == ':' &&
1653 !(isspace(p[1]) || !p[1] || isspace(p[-1])))
1655 p = find_char_unquote (p+1, stopchars, 1);
1657 #endif
1658 #ifdef WINDOWS32
1659 /* For WINDOWS32, skip a "C:\..." or "C:/...". */
1660 if (stopchar == ':' &&
1661 p != 0 &&
1662 (p[1] == '\\' || p[1] == '/') &&
1663 isalpha (p[-1])) {
1664 p = end_of_token_w32(++p, ':');
1665 if (*p == '\0' && p[-1] == ':')
1666 p--;
1668 #endif
1669 if (p == 0)
1670 p = q + strlen (q);
1672 if (strip)
1673 #ifdef VMS
1674 /* Skip leading `[]'s. */
1675 while (p - q > 2 && q[0] == '[' && q[1] == ']')
1676 #else
1677 /* Skip leading `./'s. */
1678 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1679 #endif
1681 q += 2; /* Skip "./". */
1682 while (q < p && *q == '/')
1683 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1684 ++q;
1687 /* Extract the filename just found, and skip it. */
1689 if (q == p)
1690 /* ".///" was stripped to "". */
1691 #ifdef VMS
1692 continue;
1693 #else
1694 #ifdef _AMIGA
1695 name = savestring ("", 0);
1696 #else
1697 name = savestring ("./", 2);
1698 #endif
1699 #endif
1700 else
1701 #ifdef VMS
1702 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
1703 * to remove this '\' before we can use the filename.
1704 * Savestring called because q may be read-only string constant.
1707 char *qbase = savestring(q, strlen(q));
1708 char *pbase = qbase + (p-q);
1709 char *q1 = qbase;
1710 char *q2 = q1;
1711 char *p1 = pbase;
1713 while (q1 != pbase)
1715 if (*q1 == '\\' && *(q1+1) == ':')
1717 q1++;
1718 p1--;
1720 *q2++ = *q1++;
1722 name = savestring (qbase, p1 - qbase);
1723 free (qbase);
1725 #else
1726 name = savestring (q, p - q);
1727 #endif
1729 /* Add it to the front of the chain. */
1730 new1 = (struct nameseq *) xmalloc (size);
1731 new1->name = name;
1732 new1->next = new;
1733 new = new1;
1736 #ifndef NO_ARCHIVES
1738 /* Look for multi-word archive references.
1739 They are indicated by a elt ending with an unmatched `)' and
1740 an elt further down the chain (i.e., previous in the file list)
1741 with an unmatched `(' (e.g., "lib(mem"). */
1743 new1 = new;
1744 lastnew1 = 0;
1745 while (new1 != 0)
1746 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1747 && new1->name[strlen (new1->name) - 1] == ')'
1748 && index (new1->name, '(') == 0)
1750 /* NEW1 ends with a `)' but does not contain a `('.
1751 Look back for an elt with an opening `(' but no closing `)'. */
1753 struct nameseq *n = new1->next, *lastn = new1;
1754 char *paren;
1755 while (n != 0 && (paren = index (n->name, '(')) == 0)
1757 lastn = n;
1758 n = n->next;
1760 if (n != 0
1761 /* Ignore something starting with `(', as that cannot actually
1762 be an archive-member reference (and treating it as such
1763 results in an empty file name, which causes much lossage). */
1764 && n->name[0] != '(')
1766 /* N is the first element in the archive group.
1767 Its name looks like "lib(mem" (with no closing `)'). */
1769 char *libname;
1771 /* Copy "lib(" into LIBNAME. */
1772 ++paren;
1773 libname = (char *) alloca (paren - n->name + 1);
1774 bcopy (n->name, libname, paren - n->name);
1775 libname[paren - n->name] = '\0';
1777 if (*paren == '\0')
1779 /* N was just "lib(", part of something like "lib( a b)".
1780 Edit it out of the chain and free its storage. */
1781 lastn->next = n->next;
1782 free (n->name);
1783 free ((char *) n);
1784 /* LASTN->next is the new stopping elt for the loop below. */
1785 n = lastn->next;
1787 else
1789 /* Replace N's name with the full archive reference. */
1790 name = concat (libname, paren, ")");
1791 free (n->name);
1792 n->name = name;
1795 if (new1->name[1] == '\0')
1797 /* NEW1 is just ")", part of something like "lib(a b )".
1798 Omit it from the chain and free its storage. */
1799 if (lastnew1 == 0)
1800 new = new1->next;
1801 else
1802 lastnew1->next = new1->next;
1803 lastn = new1;
1804 new1 = new1->next;
1805 free (lastn->name);
1806 free ((char *) lastn);
1808 else
1810 /* Replace also NEW1->name, which already has closing `)'. */
1811 name = concat (libname, new1->name, "");
1812 free (new1->name);
1813 new1->name = name;
1814 new1 = new1->next;
1817 /* Trace back from NEW1 (the end of the list) until N
1818 (the beginning of the list), rewriting each name
1819 with the full archive reference. */
1821 while (new1 != n)
1823 name = concat (libname, new1->name, ")");
1824 free (new1->name);
1825 new1->name = name;
1826 lastnew1 = new1;
1827 new1 = new1->next;
1830 else
1832 /* No frobnication happening. Just step down the list. */
1833 lastnew1 = new1;
1834 new1 = new1->next;
1837 else
1839 lastnew1 = new1;
1840 new1 = new1->next;
1843 #endif
1845 *stringp = p;
1846 return new;
1849 /* Read a line of text from STREAM into LINEBUFFER.
1850 Combine continuation lines into one line.
1851 Return the number of actual lines read (> 1 if hacked continuation lines).
1854 static unsigned int
1855 readline (linebuffer, stream, filename, lineno)
1856 struct linebuffer *linebuffer;
1857 FILE *stream;
1858 char *filename;
1859 unsigned int lineno;
1861 char *buffer = linebuffer->buffer;
1862 register char *p = linebuffer->buffer;
1863 register char *end = p + linebuffer->size;
1864 register int len, lastlen = 0;
1865 register char *p2;
1866 register unsigned int nlines = 0;
1867 register int backslash;
1869 *p = '\0';
1871 while (fgets (p, end - p, stream) != 0)
1873 len = strlen (p);
1874 if (len == 0)
1876 /* This only happens when the first thing on the line is a '\0'.
1877 It is a pretty hopeless case, but (wonder of wonders) Athena
1878 lossage strikes again! (xmkmf puts NULs in its makefiles.)
1879 There is nothing really to be done; we synthesize a newline so
1880 the following line doesn't appear to be part of this line. */
1881 makefile_error (filename, lineno,
1882 "warning: NUL character seen; rest of line ignored");
1883 p[0] = '\n';
1884 len = 1;
1887 p += len;
1888 if (p[-1] != '\n')
1890 /* Probably ran out of buffer space. */
1891 register unsigned int p_off = p - buffer;
1892 linebuffer->size *= 2;
1893 buffer = (char *) xrealloc (buffer, linebuffer->size);
1894 p = buffer + p_off;
1895 end = buffer + linebuffer->size;
1896 linebuffer->buffer = buffer;
1897 *p = '\0';
1898 lastlen = len;
1899 continue;
1902 ++nlines;
1904 if (len == 1 && p > buffer)
1905 /* P is pointing at a newline and it's the beginning of
1906 the buffer returned by the last fgets call. However,
1907 it is not necessarily the beginning of a line if P is
1908 pointing past the beginning of the holding buffer.
1909 If the buffer was just enlarged (right before the newline),
1910 we must account for that, so we pretend that the two lines
1911 were one line. */
1912 len += lastlen;
1913 lastlen = len;
1914 backslash = 0;
1915 for (p2 = p - 2; --len > 0; --p2)
1917 if (*p2 == '\\')
1918 backslash = !backslash;
1919 else
1920 break;
1923 if (!backslash)
1925 p[-1] = '\0';
1926 break;
1929 if (end - p <= 1)
1931 /* Enlarge the buffer. */
1932 register unsigned int p_off = p - buffer;
1933 linebuffer->size *= 2;
1934 buffer = (char *) xrealloc (buffer, linebuffer->size);
1935 p = buffer + p_off;
1936 end = buffer + linebuffer->size;
1937 linebuffer->buffer = buffer;
1941 if (ferror (stream))
1942 pfatal_with_name (filename);
1944 return nlines;
1947 /* Construct the list of include directories
1948 from the arguments and the default list. */
1950 void
1951 construct_include_path (arg_dirs)
1952 char **arg_dirs;
1954 register unsigned int i;
1955 #ifdef VAXC /* just don't ask ... */
1956 stat_t stbuf;
1957 #else
1958 struct stat stbuf;
1959 #endif
1960 /* Table to hold the dirs. */
1962 register unsigned int defsize = (sizeof (default_include_directories)
1963 / sizeof (default_include_directories[0]));
1964 register unsigned int max = 5;
1965 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
1966 register unsigned int idx = 0;
1968 #ifdef __MSDOS__
1969 defsize++;
1970 #endif
1972 /* First consider any dirs specified with -I switches.
1973 Ignore dirs that don't exist. */
1975 if (arg_dirs != 0)
1976 while (*arg_dirs != 0)
1978 char *dir = *arg_dirs++;
1980 if (dir[0] == '~')
1982 char *expanded = tilde_expand (dir);
1983 if (expanded != 0)
1984 dir = expanded;
1987 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
1989 if (idx == max - 1)
1991 max += 5;
1992 dirs = (char **)
1993 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
1995 dirs[idx++] = dir;
1997 else if (dir != arg_dirs[-1])
1998 free (dir);
2001 /* Now add at the end the standard default dirs. */
2003 #ifdef __MSDOS__
2005 /* The environment variable $DJDIR holds the root of the
2006 DJGPP directory tree; add ${DJDIR}/include. */
2007 struct variable *djdir = lookup_variable ("DJDIR", 5);
2009 if (djdir)
2011 char *defdir = (char *) xmalloc (strlen (djdir->value) + 8 + 1);
2013 strcat (strcpy (defdir, djdir->value), "/include");
2014 dirs[idx++] = defdir;
2017 #endif
2019 for (i = 0; default_include_directories[i] != 0; ++i)
2020 if (stat (default_include_directories[i], &stbuf) == 0
2021 && S_ISDIR (stbuf.st_mode))
2022 dirs[idx++] = default_include_directories[i];
2024 dirs[idx] = 0;
2026 /* Now compute the maximum length of any name in it. */
2028 max_incl_len = 0;
2029 for (i = 0; i < idx; ++i)
2031 unsigned int len = strlen (dirs[i]);
2032 /* If dir name is written with a trailing slash, discard it. */
2033 if (dirs[i][len - 1] == '/')
2034 /* We can't just clobber a null in because it may have come from
2035 a literal string and literal strings may not be writable. */
2036 dirs[i] = savestring (dirs[i], len - 1);
2037 if (len > max_incl_len)
2038 max_incl_len = len;
2041 include_directories = dirs;
2044 /* Expand ~ or ~USER at the beginning of NAME.
2045 Return a newly malloc'd string or 0. */
2047 char *
2048 tilde_expand (name)
2049 char *name;
2051 #ifndef VMS
2052 if (name[1] == '/' || name[1] == '\0')
2054 extern char *getenv ();
2055 char *home_dir;
2056 int is_variable;
2059 /* Turn off --warn-undefined-variables while we expand HOME. */
2060 int save = warn_undefined_variables_flag;
2061 warn_undefined_variables_flag = 0;
2063 home_dir = allocated_variable_expand ("$(HOME)");
2065 warn_undefined_variables_flag = save;
2068 is_variable = home_dir[0] != '\0';
2069 if (!is_variable)
2071 free (home_dir);
2072 home_dir = getenv ("HOME");
2074 #if !defined(_AMIGA) && !defined(WINDOWS32)
2075 if (home_dir == 0 || home_dir[0] == '\0')
2077 extern char *getlogin ();
2078 char *name = getlogin ();
2079 home_dir = 0;
2080 if (name != 0)
2082 struct passwd *p = getpwnam (name);
2083 if (p != 0)
2084 home_dir = p->pw_dir;
2087 #endif /* !AMIGA && !WINDOWS32 */
2088 if (home_dir != 0)
2090 char *new = concat (home_dir, "", name + 1);
2091 if (is_variable)
2092 free (home_dir);
2093 return new;
2096 #if !defined(_AMIGA) && !defined(WINDOWS32)
2097 else
2099 struct passwd *pwent;
2100 char *userend = index (name + 1, '/');
2101 if (userend != 0)
2102 *userend = '\0';
2103 pwent = getpwnam (name + 1);
2104 if (pwent != 0)
2106 if (userend == 0)
2107 return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
2108 else
2109 return concat (pwent->pw_dir, "/", userend + 1);
2111 else if (userend != 0)
2112 *userend = '/';
2114 #endif /* !AMIGA && !WINDOWS32 */
2115 #endif /* !VMS */
2116 return 0;
2119 /* Given a chain of struct nameseq's describing a sequence of filenames,
2120 in reverse of the intended order, return a new chain describing the
2121 result of globbing the filenames. The new chain is in forward order.
2122 The links of the old chain are freed or used in the new chain.
2123 Likewise for the names in the old chain.
2125 SIZE is how big to construct chain elements.
2126 This is useful if we want them actually to be other structures
2127 that have room for additional info. */
2129 struct nameseq *
2130 multi_glob (chain, size)
2131 struct nameseq *chain;
2132 unsigned int size;
2134 extern void dir_setup_glob ();
2135 register struct nameseq *new = 0;
2136 register struct nameseq *old;
2137 struct nameseq *nexto;
2138 glob_t gl;
2140 dir_setup_glob (&gl);
2142 for (old = chain; old != 0; old = nexto)
2144 #ifndef NO_ARCHIVES
2145 char *memname;
2146 #endif
2148 nexto = old->next;
2150 if (old->name[0] == '~')
2152 char *newname = tilde_expand (old->name);
2153 if (newname != 0)
2155 free (old->name);
2156 old->name = newname;
2160 #ifndef NO_ARCHIVES
2161 if (ar_name (old->name))
2163 /* OLD->name is an archive member reference.
2164 Replace it with the archive file name,
2165 and save the member name in MEMNAME.
2166 We will glob on the archive name and then
2167 reattach MEMNAME later. */
2168 char *arname;
2169 ar_parse_name (old->name, &arname, &memname);
2170 free (old->name);
2171 old->name = arname;
2173 else
2174 memname = 0;
2175 #endif /* !NO_ARCHIVES */
2177 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
2179 case 0: /* Success. */
2181 register int i = gl.gl_pathc;
2182 while (i-- > 0)
2184 #ifndef NO_ARCHIVES
2185 if (memname != 0)
2187 /* Try to glob on MEMNAME within the archive. */
2188 struct nameseq *found
2189 = ar_glob (gl.gl_pathv[i], memname, size);
2190 if (found == 0)
2192 /* No matches. Use MEMNAME as-is. */
2193 struct nameseq *elt
2194 = (struct nameseq *) xmalloc (size);
2195 unsigned int alen = strlen (gl.gl_pathv[i]);
2196 unsigned int mlen = strlen (memname);
2197 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2198 bcopy (gl.gl_pathv[i], elt->name, alen);
2199 elt->name[alen] = '(';
2200 bcopy (memname, &elt->name[alen + 1], mlen);
2201 elt->name[alen + 1 + mlen] = ')';
2202 elt->name[alen + 1 + mlen + 1] = '\0';
2203 elt->next = new;
2204 new = elt;
2206 else
2208 /* Find the end of the FOUND chain. */
2209 struct nameseq *f = found;
2210 while (f->next != 0)
2211 f = f->next;
2213 /* Attach the chain being built to the end of the FOUND
2214 chain, and make FOUND the new NEW chain. */
2215 f->next = new;
2216 new = found;
2219 free (memname);
2221 else
2222 #endif /* !NO_ARCHIVES */
2224 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2225 elt->name = savestring (gl.gl_pathv[i],
2226 strlen (gl.gl_pathv[i]));
2227 elt->next = new;
2228 new = elt;
2231 globfree (&gl);
2232 free (old->name);
2233 free ((char *)old);
2234 break;
2237 case GLOB_NOSPACE:
2238 fatal ("virtual memory exhausted");
2239 break;
2241 default:
2242 old->next = new;
2243 new = old;
2244 break;
2248 return new;