autoconf
[make.git] / read.c
blob1986c76169134cb04dd9d2197eb9d36a4eb1f416
1 /* Reading and parsing of makefiles for GNU Make.
2 Copyright (C) 1988,89,90,91,92,93,94,95,96 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 WIN32
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 /* !WIN32 */
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(WIN32) && !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 ignoring = 0, in_ignored_define = 0;
275 int no_targets = 0; /* Set when reading a rule without targets. */
276 char *passed_filename = filename;
278 struct nameseq *filenames = 0;
279 struct dep *deps;
280 unsigned int lineno = 1;
281 unsigned int nlines = 0;
282 int two_colon;
283 char *pattern = 0, *pattern_percent;
285 int makefile_errno;
286 #ifdef WIN32
287 int check_again;
288 #endif
290 #define record_waiting_files() \
291 do \
293 if (filenames != 0) \
294 record_files (filenames, pattern, pattern_percent, deps, \
295 commands_started, commands, commands_idx, \
296 two_colon, filename, lineno, \
297 !(flags & RM_NO_DEFAULT_GOAL)); \
298 filenames = 0; \
299 commands_idx = 0; \
300 pattern = 0; \
301 } while (0)
303 #ifdef lint /* Suppress `used before set' messages. */
304 two_colon = 0;
305 pattern_percent = 0;
306 #endif
308 if (debug_flag)
310 printf ("Reading makefile `%s'", filename);
311 if (flags & RM_NO_DEFAULT_GOAL)
312 printf (" (no default goal)");
313 if (flags & RM_INCLUDED)
314 printf (" (search path)");
315 if (flags & RM_DONTCARE)
316 printf (" (don't care)");
317 if (flags & RM_NO_TILDE)
318 printf (" (no ~ expansion)");
319 puts ("...");
322 /* First, get a stream to read. */
324 /* Expand ~ in FILENAME unless it came from `include',
325 in which case it was already done. */
326 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
328 char *expanded = tilde_expand (filename);
329 if (expanded != 0)
330 filename = expanded;
333 infile = fopen (filename, "r");
334 /* Save the error code so we print the right message later. */
335 makefile_errno = errno;
337 /* If the makefile wasn't found and it's either a makefile from
338 the `MAKEFILES' variable or an included makefile,
339 search the included makefile search path for this makefile. */
340 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
342 register unsigned int i;
343 for (i = 0; include_directories[i] != 0; ++i)
345 char *name = concat (include_directories[i], "/", filename);
346 infile = fopen (name, "r");
347 if (infile == 0)
348 free (name);
349 else
351 filename = name;
352 break;
357 /* Add FILENAME to the chain of read makefiles. */
358 deps = (struct dep *) xmalloc (sizeof (struct dep));
359 deps->next = read_makefiles;
360 read_makefiles = deps;
361 deps->name = 0;
362 deps->file = lookup_file (filename);
363 if (deps->file == 0)
365 deps->file = enter_file (savestring (filename, strlen (filename)));
366 if (flags & RM_DONTCARE)
367 deps->file->dontcare = 1;
369 if (filename != passed_filename)
370 free (filename);
371 filename = deps->file->name;
372 deps->changed = flags;
373 deps = 0;
375 /* If the makefile can't be found at all, give up entirely. */
377 if (infile == 0)
379 /* If we did some searching, errno has the error from the last
380 attempt, rather from FILENAME itself. Restore it in case the
381 caller wants to use it in a message. */
382 errno = makefile_errno;
383 return 0;
386 reading_filename = filename;
387 reading_lineno_ptr = &lineno;
389 /* Loop over lines in the file.
390 The strategy is to accumulate target names in FILENAMES, dependencies
391 in DEPS and commands in COMMANDS. These are used to define a rule
392 when the start of the next rule (or eof) is encountered. */
394 initbuffer (&lb);
396 while (!feof (infile))
398 lineno += nlines;
399 nlines = readline (&lb, infile, filename, lineno);
401 /* Check for a shell command line first.
402 If it is not one, we can stop treating tab specially. */
403 if (lb.buffer[0] == '\t')
405 /* This line is a probably shell command. */
406 unsigned int len;
408 if (no_targets)
409 /* Ignore the commands in a rule with no targets. */
410 continue;
412 /* If there is no preceding rule line, don't treat this line
413 as a command, even though it begins with a tab character.
414 SunOS 4 make appears to behave this way. */
416 if (filenames != 0)
418 if (ignoring)
419 /* Yep, this is a shell command, and we don't care. */
420 continue;
422 /* Append this command line to the line being accumulated. */
423 p = lb.buffer;
424 if (commands_idx == 0)
425 commands_started = lineno;
426 len = strlen (p);
427 if (len + 1 + commands_idx > commands_len)
429 commands_len = (len + 1 + commands_idx) * 2;
430 commands = (char *) xrealloc (commands, commands_len);
432 bcopy (p, &commands[commands_idx], len);
433 commands_idx += len;
434 commands[commands_idx++] = '\n';
436 continue;
440 /* This line is not a shell command line. Don't worry about tabs. */
442 if (collapsed_length < lb.size)
444 collapsed_length = lb.size;
445 if (collapsed != 0)
446 free (collapsed);
447 collapsed = (char *) xmalloc (collapsed_length);
449 strcpy (collapsed, lb.buffer);
450 /* Collapse continuation lines. */
451 collapse_continuations (collapsed);
452 remove_comments (collapsed);
454 /* strncmp is first to avoid dereferencing out into space. */
455 #define word1eq(s, l) (!strncmp (s, p, l) \
456 && (p[l] == '\0' || isblank (p[l])))
457 p = collapsed;
458 while (isspace (*p))
459 ++p;
460 if (*p == '\0')
461 /* This line is completely empty. */
462 continue;
464 /* We must first check for conditional and `define' directives before
465 ignoring anything, since they control what we will do with
466 following lines. */
468 if (!in_ignored_define
469 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
470 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
471 || word1eq ("else", 4) || word1eq ("endif", 5)))
473 int i = conditional_line (p, filename, lineno);
474 if (i >= 0)
475 ignoring = i;
476 else
477 makefile_fatal (filename, lineno,
478 "invalid syntax in conditional");
479 continue;
481 else if (word1eq ("endef", 5))
483 if (in_ignored_define)
484 in_ignored_define = 0;
485 else
486 makefile_fatal (filename, lineno, "extraneous `endef'");
487 continue;
489 else if (word1eq ("define", 6))
491 if (ignoring)
492 in_ignored_define = 1;
493 else
495 p2 = next_token (p + 6);
496 /* Let the variable name be the whole rest of the line,
497 with trailing blanks stripped (comments have already been
498 removed), so it could be a complex variable/function
499 reference that might contain blanks. */
500 p = index (p2, '\0');
501 while (isblank (p[-1]))
502 --p;
503 lineno = do_define (p2, p - p2, o_file,
504 lineno, infile, filename);
506 continue;
508 else if (word1eq ("override", 8))
510 p2 = next_token (p + 8);
511 if (p2 == 0)
512 makefile_error (filename, lineno, "empty `override' directive");
513 if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
515 if (ignoring)
516 in_ignored_define = 1;
517 else
519 p2 = next_token (p2 + 6);
520 /* Let the variable name be the whole rest of the line,
521 with trailing blanks stripped (comments have already been
522 removed), so it could be a complex variable/function
523 reference that might contain blanks. */
524 p = index (p2, '\0');
525 while (isblank (p[-1]))
526 --p;
527 lineno = do_define (p2, p - p2, o_override,
528 lineno, infile, filename);
531 else if (!ignoring
532 && !try_variable_definition (filename, lineno,
533 p2, o_override))
534 makefile_error (filename, lineno, "empty `override' directive");
536 continue;
539 if (ignoring)
540 /* Ignore the line. We continue here so conditionals
541 can appear in the middle of a rule. */
542 continue;
543 else if (word1eq ("export", 6))
545 struct variable *v;
546 p2 = next_token (p + 6);
547 if (*p2 == '\0')
548 export_all_variables = 1;
549 v = try_variable_definition (filename, lineno, p2, o_file);
550 if (v != 0)
551 v->export = v_export;
552 else
554 unsigned int len;
555 for (p = find_next_token (&p2, &len); p != 0;
556 p = find_next_token (&p2, &len))
558 v = lookup_variable (p, len);
559 if (v == 0)
560 v = define_variable (p, len, "", o_file, 0);
561 v->export = v_export;
565 else if (word1eq ("unexport", 8))
567 unsigned int len;
568 struct variable *v;
569 p2 = next_token (p + 8);
570 if (*p2 == '\0')
571 export_all_variables = 0;
572 for (p = find_next_token (&p2, &len); p != 0;
573 p = find_next_token (&p2, &len))
575 v = lookup_variable (p, len);
576 if (v == 0)
577 v = define_variable (p, len, "", o_file, 0);
578 v->export = v_noexport;
581 else if (word1eq ("include", 7) || word1eq ("-include", 8)
582 || word1eq ("sinclude", 8))
584 /* We have found an `include' line specifying a nested
585 makefile to be read at this point. */
586 struct conditionals *save, new_conditionals;
587 struct nameseq *files;
588 /* "-include" (vs "include") says no error if the file does not
589 exist. "sinclude" is an alias for this from SGI. */
590 int noerror = p[0] != 'i';
592 p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
593 if (*p == '\0')
595 makefile_error (filename, lineno,
596 "no file name for `%sinclude'",
597 noerror ? "-" : "");
598 continue;
601 /* Parse the list of file names. */
602 p2 = p;
603 files = multi_glob (parse_file_seq (&p2, '\0',
604 sizeof (struct nameseq),
606 sizeof (struct nameseq));
607 free (p);
609 /* Save the state of conditionals and start
610 the included makefile with a clean slate. */
611 save = conditionals;
612 bzero ((char *) &new_conditionals, sizeof new_conditionals);
613 conditionals = &new_conditionals;
615 /* Record the rules that are waiting so they will determine
616 the default goal before those in the included makefile. */
617 record_waiting_files ();
619 /* Read each included makefile. */
620 while (files != 0)
622 struct nameseq *next = files->next;
623 char *name = files->name;
624 free ((char *)files);
625 files = next;
627 if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
628 | (noerror ? RM_DONTCARE : 0)))
629 && ! noerror)
630 makefile_error (filename, lineno,
631 "%s: %s", name, strerror (errno));
634 /* Free any space allocated by conditional_line. */
635 if (conditionals->ignoring)
636 free (conditionals->ignoring);
637 if (conditionals->seen_else)
638 free (conditionals->seen_else);
640 /* Restore state. */
641 conditionals = save;
642 reading_filename = filename;
643 reading_lineno_ptr = &lineno;
645 else if (word1eq ("vpath", 5))
647 char *pattern;
648 unsigned int len;
649 p2 = variable_expand (p + 5);
650 p = find_next_token (&p2, &len);
651 if (p != 0)
653 pattern = savestring (p, len);
654 p = find_next_token (&p2, &len);
655 /* No searchpath means remove all previous
656 selective VPATH's with the same pattern. */
658 else
659 /* No pattern means remove all previous selective VPATH's. */
660 pattern = 0;
661 construct_vpath_list (pattern, p);
662 if (pattern != 0)
663 free (pattern);
665 #undef word1eq
666 else if (try_variable_definition (filename, lineno, p, o_file))
667 /* This line has been dealt with. */
669 else if (lb.buffer[0] == '\t')
671 p = collapsed; /* Ignore comments. */
672 while (isblank (*p))
673 ++p;
674 if (*p == '\0')
675 /* The line is completely blank; that is harmless. */
676 continue;
677 /* This line starts with a tab but was not caught above
678 because there was no preceding target, and the line
679 might have been usable as a variable definition.
680 But now it is definitely lossage. */
681 makefile_fatal (filename, lineno,
682 "commands commence before first target");
684 else
686 /* This line describes some target files. */
688 char *cmdleft;
690 /* Record the previous rule. */
692 record_waiting_files ();
694 /* Search the line for an unquoted ; that is not after an
695 unquoted #. */
696 cmdleft = find_char_unquote (lb.buffer, ";#", 0);
697 if (cmdleft != 0 && *cmdleft == '#')
699 /* We found a comment before a semicolon. */
700 *cmdleft = '\0';
701 cmdleft = 0;
703 else if (cmdleft != 0)
704 /* Found one. Cut the line short there before expanding it. */
705 *cmdleft = '\0';
707 collapse_continuations (lb.buffer);
709 /* Expand variable and function references before doing anything
710 else so that special characters can be inside variables. */
711 p = variable_expand (lb.buffer);
713 if (cmdleft == 0)
714 /* Look for a semicolon in the expanded line. */
715 cmdleft = find_char_unquote (p, ";", 0);
717 if (cmdleft != 0)
718 /* Cut the line short at the semicolon. */
719 *cmdleft = '\0';
721 p2 = next_token (p);
722 if (*p2 == '\0')
724 if (cmdleft != 0)
725 makefile_fatal (filename, lineno,
726 "missing rule before commands");
727 else
728 /* This line contained a variable reference that
729 expanded to nothing but whitespace. */
730 continue;
732 else if (*p2 == ':')
734 /* We accept and ignore rules without targets for
735 compatibility with SunOS 4 make. */
736 no_targets = 1;
737 continue;
740 filenames = multi_glob (parse_file_seq (&p2, ':',
741 sizeof (struct nameseq),
743 sizeof (struct nameseq));
744 if (*p2++ == '\0')
745 makefile_fatal (filename, lineno, "missing separator");
746 /* Is this a one-colon or two-colon entry? */
747 two_colon = *p2 == ':';
748 if (two_colon)
749 p2++;
751 /* We have some targets, so don't ignore the following commands. */
752 no_targets = 0;
754 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
755 p = index (p2, ':');
756 while (p != 0 && p[-1] == '\\')
758 register char *q = &p[-1];
759 register int backslash = 0;
760 while (*q-- == '\\')
761 backslash = !backslash;
762 if (backslash)
763 p = index (p + 1, ':');
764 else
765 break;
767 #ifdef __MSDOS__
768 /* For MS-DOS, skip a "C:\...". */
769 if (p != 0 && p[1] == '\\' && isalpha (p[-1]))
770 p = 0;
771 #endif
772 #ifdef _AMIGA
773 /* Here, the situation is quite complicated. Let's have a look
774 at a couple of targets:
776 install: dev:make
778 dev:make: make
780 dev:make:: xyz
782 The rule is that it's only a target, if there are TWO :'s
783 OR a space around the :.
785 if (p && !(isspace(p[1]) || !p[1] || isspace(p[-1])))
786 p = 0;
787 #endif
788 #ifdef WIN32
789 do {
790 check_again = 0;
791 /* For WIN32, skip a "C:\..." or a "C:/..." */
792 if (p != 0 && (p[1] == '\\' || p[1] == '/') && isalpha (p[-1])) {
793 p = index(p + 1, ':');
794 check_again = 1;
796 } while (check_again);
797 #endif
798 if (p != 0)
800 struct nameseq *target;
801 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
802 ++p2;
803 if (target == 0)
804 makefile_fatal (filename, lineno, "missing target pattern");
805 else if (target->next != 0)
806 makefile_fatal (filename, lineno, "multiple target patterns");
807 pattern = target->name;
808 pattern_percent = find_percent (pattern);
809 if (pattern_percent == 0)
810 makefile_fatal (filename, lineno,
811 "target pattern contains no `%%'");
813 else
814 pattern = 0;
816 /* Parse the dependencies. */
817 deps = (struct dep *)
818 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
819 sizeof (struct dep));
821 commands_idx = 0;
822 if (cmdleft != 0)
824 /* Semicolon means rest of line is a command. */
825 unsigned int len = strlen (cmdleft + 1);
827 commands_started = lineno;
829 /* Add this command line to the buffer. */
830 if (len + 2 > commands_len)
832 commands_len = (len + 2) * 2;
833 commands = (char *) xrealloc (commands, commands_len);
835 bcopy (cmdleft + 1, commands, len);
836 commands_idx += len;
837 commands[commands_idx++] = '\n';
840 continue;
843 /* We get here except in the case that we just read a rule line.
844 Record now the last rule we read, so following spurious
845 commands are properly diagnosed. */
846 record_waiting_files ();
847 no_targets = 0;
850 if (conditionals->if_cmds)
851 makefile_fatal (filename, lineno, "missing `endif'");
853 /* At eof, record the last rule. */
854 record_waiting_files ();
856 freebuffer (&lb);
857 free ((char *) commands);
858 fclose (infile);
860 reading_filename = 0;
861 reading_lineno_ptr = 0;
863 return 1;
866 /* Execute a `define' directive.
867 The first line has already been read, and NAME is the name of
868 the variable to be defined. The following lines remain to be read.
869 LINENO, INFILE and FILENAME refer to the makefile being read.
870 The value returned is LINENO, updated for lines read here. */
872 static unsigned int
873 do_define (name, namelen, origin, lineno, infile, filename)
874 char *name;
875 unsigned int namelen;
876 enum variable_origin origin;
877 unsigned int lineno;
878 FILE *infile;
879 char *filename;
881 struct linebuffer lb;
882 unsigned int nlines = 0;
883 unsigned int length = 100;
884 char *definition = (char *) xmalloc (100);
885 register unsigned int idx = 0;
886 register char *p;
888 /* Expand the variable name. */
889 char *var = (char *) alloca (namelen + 1);
890 bcopy (name, var, namelen);
891 var[namelen] = '\0';
892 var = variable_expand (var);
894 initbuffer (&lb);
895 while (!feof (infile))
897 lineno += nlines;
898 nlines = readline (&lb, infile, filename, lineno);
900 collapse_continuations (lb.buffer);
902 p = next_token (lb.buffer);
903 if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
905 p += 5;
906 remove_comments (p);
907 if (*next_token (p) != '\0')
908 makefile_error (filename, lineno,
909 "Extraneous text after `endef' directive");
910 /* Define the variable. */
911 if (idx == 0)
912 definition[0] = '\0';
913 else
914 definition[idx - 1] = '\0';
915 (void) define_variable (var, strlen (var), definition, origin, 1);
916 free (definition);
917 freebuffer (&lb);
918 return lineno;
920 else
922 unsigned int len = strlen (lb.buffer);
924 /* Increase the buffer size if necessary. */
925 if (idx + len + 1 > length)
927 length = (idx + len) * 2;
928 definition = (char *) xrealloc (definition, length + 1);
931 bcopy (lb.buffer, &definition[idx], len);
932 idx += len;
933 /* Separate lines with a newline. */
934 definition[idx++] = '\n';
938 /* No `endef'!! */
939 makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
941 /* NOTREACHED */
942 return 0;
945 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
946 "ifneq", "else" and "endif".
947 LINE is the input line, with the command as its first word.
949 FILENAME and LINENO are the filename and line number in the
950 current makefile. They are used for error messages.
952 Value is -1 if the line is invalid,
953 0 if following text should be interpreted,
954 1 if following text should be ignored. */
956 static int
957 conditional_line (line, filename, lineno)
958 char *line;
959 char *filename;
960 unsigned int lineno;
962 int notdef;
963 char *cmdname;
964 register unsigned int i;
966 if (*line == 'i')
968 /* It's an "if..." command. */
969 notdef = line[2] == 'n';
970 if (notdef)
972 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
973 line += cmdname[3] == 'd' ? 7 : 6;
975 else
977 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
978 line += cmdname[2] == 'd' ? 6 : 5;
981 else
983 /* It's an "else" or "endif" command. */
984 notdef = line[1] == 'n';
985 cmdname = notdef ? "endif" : "else";
986 line += notdef ? 5 : 4;
989 line = next_token (line);
991 if (*cmdname == 'e')
993 if (*line != '\0')
994 makefile_error (filename, lineno,
995 "Extraneous text after `%s' directive",
996 cmdname);
997 /* "Else" or "endif". */
998 if (conditionals->if_cmds == 0)
999 makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
1000 /* NOTDEF indicates an `endif' command. */
1001 if (notdef)
1002 --conditionals->if_cmds;
1003 else if (conditionals->seen_else[conditionals->if_cmds - 1])
1004 makefile_fatal (filename, lineno, "only one `else' per conditional");
1005 else
1007 /* Toggle the state of ignorance. */
1008 conditionals->ignoring[conditionals->if_cmds - 1]
1009 = !conditionals->ignoring[conditionals->if_cmds - 1];
1010 /* Record that we have seen an `else' in this conditional.
1011 A second `else' will be erroneous. */
1012 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
1014 for (i = 0; i < conditionals->if_cmds; ++i)
1015 if (conditionals->ignoring[i])
1016 return 1;
1017 return 0;
1020 if (conditionals->allocated == 0)
1022 conditionals->allocated = 5;
1023 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1024 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1027 ++conditionals->if_cmds;
1028 if (conditionals->if_cmds > conditionals->allocated)
1030 conditionals->allocated += 5;
1031 conditionals->ignoring = (char *)
1032 xrealloc (conditionals->ignoring, conditionals->allocated);
1033 conditionals->seen_else = (char *)
1034 xrealloc (conditionals->seen_else, conditionals->allocated);
1037 /* Record that we have seen an `if...' but no `else' so far. */
1038 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1040 /* Search through the stack to see if we're already ignoring. */
1041 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1042 if (conditionals->ignoring[i])
1044 /* We are already ignoring, so just push a level
1045 to match the next "else" or "endif", and keep ignoring.
1046 We don't want to expand variables in the condition. */
1047 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1048 return 1;
1051 if (cmdname[notdef ? 3 : 2] == 'd')
1053 /* "Ifdef" or "ifndef". */
1054 struct variable *v;
1055 register char *p = end_of_token (line);
1056 i = p - line;
1057 p = next_token (p);
1058 if (*p != '\0')
1059 return -1;
1060 v = lookup_variable (line, i);
1061 conditionals->ignoring[conditionals->if_cmds - 1]
1062 = (v != 0 && *v->value != '\0') == notdef;
1064 else
1066 /* "Ifeq" or "ifneq". */
1067 char *s1, *s2;
1068 unsigned int len;
1069 char termin = *line == '(' ? ',' : *line;
1071 if (termin != ',' && termin != '"' && termin != '\'')
1072 return -1;
1074 s1 = ++line;
1075 /* Find the end of the first string. */
1076 if (termin == ',')
1078 register int count = 0;
1079 for (; *line != '\0'; ++line)
1080 if (*line == '(')
1081 ++count;
1082 else if (*line == ')')
1083 --count;
1084 else if (*line == ',' && count <= 0)
1085 break;
1087 else
1088 while (*line != '\0' && *line != termin)
1089 ++line;
1091 if (*line == '\0')
1092 return -1;
1094 if (termin == ',')
1096 /* Strip blanks after the first string. */
1097 char *p = line++;
1098 while (isblank (p[-1]))
1099 --p;
1100 *p = '\0';
1102 else
1103 *line++ = '\0';
1105 s2 = variable_expand (s1);
1106 /* We must allocate a new copy of the expanded string because
1107 variable_expand re-uses the same buffer. */
1108 len = strlen (s2);
1109 s1 = (char *) alloca (len + 1);
1110 bcopy (s2, s1, len + 1);
1112 if (termin != ',')
1113 /* Find the start of the second string. */
1114 line = next_token (line);
1116 termin = termin == ',' ? ')' : *line;
1117 if (termin != ')' && termin != '"' && termin != '\'')
1118 return -1;
1120 /* Find the end of the second string. */
1121 if (termin == ')')
1123 register int count = 0;
1124 s2 = next_token (line);
1125 for (line = s2; *line != '\0'; ++line)
1127 if (*line == '(')
1128 ++count;
1129 else if (*line == ')')
1130 if (count <= 0)
1131 break;
1132 else
1133 --count;
1136 else
1138 ++line;
1139 s2 = line;
1140 while (*line != '\0' && *line != termin)
1141 ++line;
1144 if (*line == '\0')
1145 return -1;
1147 *line = '\0';
1148 line = next_token (++line);
1149 if (*line != '\0')
1150 makefile_error (filename, lineno,
1151 "Extraneous text after `%s' directive",
1152 cmdname);
1154 s2 = variable_expand (s2);
1155 conditionals->ignoring[conditionals->if_cmds - 1]
1156 = streq (s1, s2) == notdef;
1159 /* Search through the stack to see if we're ignoring. */
1160 for (i = 0; i < conditionals->if_cmds; ++i)
1161 if (conditionals->ignoring[i])
1162 return 1;
1163 return 0;
1166 /* Remove duplicate dependencies in CHAIN. */
1168 void
1169 uniquize_deps (chain)
1170 struct dep *chain;
1172 register struct dep *d;
1174 /* Make sure that no dependencies are repeated. This does not
1175 really matter for the purpose of updating targets, but it
1176 might make some names be listed twice for $^ and $?. */
1178 for (d = chain; d != 0; d = d->next)
1180 struct dep *last, *next;
1182 last = d;
1183 next = d->next;
1184 while (next != 0)
1185 if (streq (dep_name (d), dep_name (next)))
1187 struct dep *n = next->next;
1188 last->next = n;
1189 if (next->name != 0 && next->name != d->name)
1190 free (next->name);
1191 if (next != d)
1192 free ((char *) next);
1193 next = n;
1195 else
1197 last = next;
1198 next = next->next;
1203 /* Record a description line for files FILENAMES,
1204 with dependencies DEPS, commands to execute described
1205 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1206 TWO_COLON is nonzero if a double colon was used.
1207 If not nil, PATTERN is the `%' pattern to make this
1208 a static pattern rule, and PATTERN_PERCENT is a pointer
1209 to the `%' within it.
1211 The links of FILENAMES are freed, and so are any names in it
1212 that are not incorporated into other data structures. */
1214 static void
1215 record_files (filenames, pattern, pattern_percent, deps, commands_started,
1216 commands, commands_idx, two_colon, filename, lineno, set_default)
1217 struct nameseq *filenames;
1218 char *pattern, *pattern_percent;
1219 struct dep *deps;
1220 unsigned int commands_started;
1221 char *commands;
1222 unsigned int commands_idx;
1223 int two_colon;
1224 char *filename;
1225 unsigned int lineno;
1226 int set_default;
1228 struct nameseq *nextf;
1229 int implicit = 0;
1230 unsigned int max_targets, target_idx;
1231 char **targets = 0, **target_percents = 0;
1232 struct commands *cmds;
1234 if (commands_idx > 0)
1236 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1237 cmds->filename = filename;
1238 cmds->lineno = commands_started;
1239 cmds->commands = savestring (commands, commands_idx);
1240 cmds->command_lines = 0;
1242 else
1243 cmds = 0;
1245 for (; filenames != 0; filenames = nextf)
1247 register char *name = filenames->name;
1248 register struct file *f;
1249 register struct dep *d;
1250 struct dep *this;
1251 char *implicit_percent;
1253 nextf = filenames->next;
1254 free ((char *) filenames);
1256 implicit_percent = find_percent (name);
1257 implicit |= implicit_percent != 0;
1259 if (implicit && pattern != 0)
1260 makefile_fatal (filename, lineno,
1261 "mixed implicit and static pattern rules");
1263 if (implicit && implicit_percent == 0)
1264 makefile_fatal (filename, lineno, "mixed implicit and normal rules");
1266 if (implicit)
1268 if (targets == 0)
1270 max_targets = 5;
1271 targets = (char **) xmalloc (5 * sizeof (char *));
1272 target_percents = (char **) xmalloc (5 * sizeof (char *));
1273 target_idx = 0;
1275 else if (target_idx == max_targets - 1)
1277 max_targets += 5;
1278 targets = (char **) xrealloc ((char *) targets,
1279 max_targets * sizeof (char *));
1280 target_percents
1281 = (char **) xrealloc ((char *) target_percents,
1282 max_targets * sizeof (char *));
1284 targets[target_idx] = name;
1285 target_percents[target_idx] = implicit_percent;
1286 ++target_idx;
1287 continue;
1290 /* If there are multiple filenames, copy the chain DEPS
1291 for all but the last one. It is not safe for the same deps
1292 to go in more than one place in the data base. */
1293 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1295 if (pattern != 0)
1296 /* If this is an extended static rule:
1297 `targets: target%pattern: dep%pattern; cmds',
1298 translate each dependency pattern into a plain filename
1299 using the target pattern and this target's name. */
1300 if (!pattern_matches (pattern, pattern_percent, name))
1302 /* Give a warning if the rule is meaningless. */
1303 makefile_error (filename, lineno,
1304 "target `%s' doesn't match the target pattern",
1305 name);
1306 this = 0;
1308 else
1310 /* We use patsubst_expand to do the work of translating
1311 the target pattern, the target's name and the dependencies'
1312 patterns into plain dependency names. */
1313 char *buffer = variable_expand ("");
1315 for (d = this; d != 0; d = d->next)
1317 char *o;
1318 char *percent = find_percent (d->name);
1319 if (percent == 0)
1320 continue;
1321 o = patsubst_expand (buffer, name, pattern, d->name,
1322 pattern_percent, percent);
1323 free (d->name);
1324 d->name = savestring (buffer, o - buffer);
1328 if (!two_colon)
1330 /* Single-colon. Combine these dependencies
1331 with others in file's existing record, if any. */
1332 f = enter_file (name);
1334 if (f->double_colon)
1335 makefile_fatal (filename, lineno,
1336 "target file `%s' has both : and :: entries",
1337 f->name);
1339 /* If CMDS == F->CMDS, this target was listed in this rule
1340 more than once. Just give a warning since this is harmless. */
1341 if (cmds != 0 && cmds == f->cmds)
1342 makefile_error
1343 (filename, lineno,
1344 "target `%s' given more than once in the same rule.",
1345 f->name);
1347 /* Check for two single-colon entries both with commands.
1348 Check is_target so that we don't lose on files such as .c.o
1349 whose commands were preinitialized. */
1350 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1352 makefile_error (cmds->filename, cmds->lineno,
1353 "warning: overriding commands for target `%s'",
1354 f->name);
1355 makefile_error (f->cmds->filename, f->cmds->lineno,
1356 "warning: ignoring old commands for target `%s'",
1357 f->name);
1360 f->is_target = 1;
1362 /* Defining .DEFAULT with no deps or cmds clears it. */
1363 if (f == default_file && this == 0 && cmds == 0)
1364 f->cmds = 0;
1365 if (cmds != 0)
1366 f->cmds = cmds;
1367 /* Defining .SUFFIXES with no dependencies
1368 clears out the list of suffixes. */
1369 if (f == suffix_file && this == 0)
1371 d = f->deps;
1372 while (d != 0)
1374 struct dep *nextd = d->next;
1375 free (d->name);
1376 free ((char *)d);
1377 d = nextd;
1379 f->deps = 0;
1381 else if (f->deps != 0)
1383 /* Add the file's old deps and the new ones in THIS together. */
1385 struct dep *firstdeps, *moredeps;
1386 if (cmds != 0)
1388 /* This is the rule with commands, so put its deps first.
1389 The rationale behind this is that $< expands to the
1390 first dep in the chain, and commands use $< expecting
1391 to get the dep that rule specifies. */
1392 firstdeps = this;
1393 moredeps = f->deps;
1395 else
1397 /* Append the new deps to the old ones. */
1398 firstdeps = f->deps;
1399 moredeps = this;
1402 if (firstdeps == 0)
1403 firstdeps = moredeps;
1404 else
1406 d = firstdeps;
1407 while (d->next != 0)
1408 d = d->next;
1409 d->next = moredeps;
1412 f->deps = firstdeps;
1414 else
1415 f->deps = this;
1417 /* If this is a static pattern rule, set the file's stem to
1418 the part of its name that matched the `%' in the pattern,
1419 so you can use $* in the commands. */
1420 if (pattern != 0)
1422 static char *percent = "%";
1423 char *buffer = variable_expand ("");
1424 char *o = patsubst_expand (buffer, name, pattern, percent,
1425 pattern_percent, percent);
1426 f->stem = savestring (buffer, o - buffer);
1429 else
1431 /* Double-colon. Make a new record
1432 even if the file already has one. */
1433 f = lookup_file (name);
1434 /* Check for both : and :: rules. Check is_target so
1435 we don't lose on default suffix rules or makefiles. */
1436 if (f != 0 && f->is_target && !f->double_colon)
1437 makefile_fatal (filename, lineno,
1438 "target file `%s' has both : and :: entries",
1439 f->name);
1440 f = enter_file (name);
1441 /* If there was an existing entry and it was a double-colon
1442 entry, enter_file will have returned a new one, making it the
1443 prev pointer of the old one, and setting its double_colon
1444 pointer to the first one. */
1445 if (f->double_colon == 0)
1446 /* This is the first entry for this name, so we must
1447 set its double_colon pointer to itself. */
1448 f->double_colon = f;
1449 f->is_target = 1;
1450 f->deps = this;
1451 f->cmds = cmds;
1454 /* Free name if not needed further. */
1455 if (f != 0 && name != f->name
1456 && (name < f->name || name > f->name + strlen (f->name)))
1458 free (name);
1459 name = f->name;
1462 /* See if this is first target seen whose name does
1463 not start with a `.', unless it contains a slash. */
1464 if (default_goal_file == 0 && set_default
1465 && (*name != '.' || index (name, '/') != 0))
1467 int reject = 0;
1469 /* If this file is a suffix, don't
1470 let it be the default goal file. */
1472 for (d = suffix_file->deps; d != 0; d = d->next)
1474 register struct dep *d2;
1475 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1477 reject = 1;
1478 break;
1480 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1482 register unsigned int len = strlen (dep_name (d2));
1483 if (strncmp (name, dep_name (d2), len))
1484 continue;
1485 if (streq (name + len, dep_name (d)))
1487 reject = 1;
1488 break;
1491 if (reject)
1492 break;
1495 if (!reject)
1496 default_goal_file = f;
1500 if (implicit)
1502 targets[target_idx] = 0;
1503 target_percents[target_idx] = 0;
1504 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1505 free ((char *) target_percents);
1509 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1510 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1511 Quoting backslashes are removed from STRING by compacting it into
1512 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1513 one, or nil if there are none. */
1515 char *
1516 find_char_unquote (string, stopchars, blank)
1517 char *string;
1518 char *stopchars;
1519 int blank;
1521 unsigned int string_len = 0;
1522 register char *p = string;
1524 while (1)
1526 while (*p != '\0' && index (stopchars, *p) == 0
1527 && (!blank || !isblank (*p)))
1528 ++p;
1529 if (*p == '\0')
1530 break;
1532 if (p > string && p[-1] == '\\')
1534 /* Search for more backslashes. */
1535 register int i = -2;
1536 while (&p[i] >= string && p[i] == '\\')
1537 --i;
1538 ++i;
1539 /* Only compute the length if really needed. */
1540 if (string_len == 0)
1541 string_len = strlen (string);
1542 /* The number of backslashes is now -I.
1543 Copy P over itself to swallow half of them. */
1544 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1545 p += i / 2;
1546 if (i % 2 == 0)
1547 /* All the backslashes quoted each other; the STOPCHAR was
1548 unquoted. */
1549 return p;
1551 /* The STOPCHAR was quoted by a backslash. Look for another. */
1553 else
1554 /* No backslash in sight. */
1555 return p;
1558 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1559 return 0;
1562 /* Search PATTERN for an unquoted %. */
1564 char *
1565 find_percent (pattern)
1566 char *pattern;
1568 return find_char_unquote (pattern, "%", 0);
1571 /* Parse a string into a sequence of filenames represented as a
1572 chain of struct nameseq's in reverse order and return that chain.
1574 The string is passed as STRINGP, the address of a string pointer.
1575 The string pointer is updated to point at the first character
1576 not parsed, which either is a null char or equals STOPCHAR.
1578 SIZE is how big to construct chain elements.
1579 This is useful if we want them actually to be other structures
1580 that have room for additional info.
1582 If STRIP is nonzero, strip `./'s off the beginning. */
1584 struct nameseq *
1585 parse_file_seq (stringp, stopchar, size, strip)
1586 char **stringp;
1587 int stopchar;
1588 unsigned int size;
1589 int strip;
1591 register struct nameseq *new = 0;
1592 register struct nameseq *new1, *lastnew1;
1593 register char *p = *stringp;
1594 char *q;
1595 char *name;
1596 char stopchars[3];
1598 #ifdef VMS
1599 stopchars[0] = ',';
1600 stopchars[1] = stopchar;
1601 stopchars[2] = '\0';
1602 #else
1603 stopchars[0] = stopchar;
1604 stopchars[1] = '\0';
1605 #endif
1607 while (1)
1609 /* Skip whitespace; see if any more names are left. */
1610 p = next_token (p);
1611 if (*p == '\0')
1612 break;
1613 if (*p == stopchar)
1614 break;
1616 /* Yes, find end of next name. */
1617 q = p;
1618 p = find_char_unquote (q, stopchars, 1);
1619 #ifdef VMS
1620 /* convert comma separated list to space separated */
1621 if (p && *p == ',')
1622 *p =' ';
1623 #endif
1624 #ifdef __MSDOS__
1625 /* For MS-DOS, skip a "C:\...". */
1626 if (stopchar == ':' && p != 0 && p[1] == '\\' && isalpha (p[-1]))
1627 p = 0;
1628 #endif
1629 #ifdef _AMIGA
1630 if (stopchar == ':' && p && *p == ':' &&
1631 !(isspace(p[1]) || !p[1] || isspace(p[-1])))
1633 p = find_char_unquote (p+1, stopchars, 1);
1635 #endif
1636 #ifdef WIN32
1637 /* For WIN32, skip a "C:\..." or "C:/...". */
1638 if (stopchar == ':' &&
1639 p != 0 &&
1640 (p[1] == '\\' || p[1] == '/') &&
1641 isalpha (p[-1])) {
1642 p = end_of_token_w32(++p, ':');
1643 if (*p == '\0' && p[-1] == ':')
1644 p--;
1646 #endif
1647 if (p == 0)
1648 p = q + strlen (q);
1650 if (strip)
1651 #ifdef VMS
1652 /* Skip leading `[]'s. */
1653 while (p - q > 2 && q[0] == '[' && q[1] == ']')
1654 #else
1655 /* Skip leading `./'s. */
1656 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1657 #endif
1659 q += 2; /* Skip "./". */
1660 while (q < p && *q == '/')
1661 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1662 ++q;
1665 /* Extract the filename just found, and skip it. */
1667 if (q == p)
1668 /* ".///" was stripped to "". */
1669 #ifdef VMS
1670 continue;
1671 #else
1672 #ifdef _AMIGA
1673 name = savestring ("", 0);
1674 #else
1675 name = savestring ("./", 2);
1676 #endif
1677 #endif
1678 else
1679 #ifdef VMS
1680 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
1681 * to remove this '\' before we can use the filename.
1682 * Savestring called because q may be read-only string constant.
1685 char *qbase = savestring(q, strlen(q));
1686 char *pbase = qbase + (p-q);
1687 char *q1 = qbase;
1688 char *q2 = q1;
1689 char *p1 = pbase;
1691 while (q1 != pbase)
1693 if (*q1 == '\\' && *(q1+1) == ':')
1695 q1++;
1696 p1--;
1698 *q2++ = *q1++;
1700 name = savestring (qbase, p1 - qbase);
1701 free (qbase);
1703 #else
1704 name = savestring (q, p - q);
1705 #endif
1707 /* Add it to the front of the chain. */
1708 new1 = (struct nameseq *) xmalloc (size);
1709 new1->name = name;
1710 new1->next = new;
1711 new = new1;
1714 #ifndef NO_ARCHIVES
1716 /* Look for multi-word archive references.
1717 They are indicated by a elt ending with an unmatched `)' and
1718 an elt further down the chain (i.e., previous in the file list)
1719 with an unmatched `(' (e.g., "lib(mem"). */
1721 new1 = new;
1722 lastnew1 = 0;
1723 while (new1 != 0)
1724 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1725 && new1->name[strlen (new1->name) - 1] == ')'
1726 && index (new1->name, '(') == 0)
1728 /* NEW1 ends with a `)' but does not contain a `('.
1729 Look back for an elt with an opening `(' but no closing `)'. */
1731 struct nameseq *n = new1->next, *lastn = new1;
1732 char *paren;
1733 while (n != 0 && (paren = index (n->name, '(')) == 0)
1735 lastn = n;
1736 n = n->next;
1738 if (n != 0
1739 /* Ignore something starting with `(', as that cannot actually
1740 be an archive-member reference (and treating it as such
1741 results in an empty file name, which causes much lossage). */
1742 && n->name[0] != '(')
1744 /* N is the first element in the archive group.
1745 Its name looks like "lib(mem" (with no closing `)'). */
1747 char *libname;
1749 /* Copy "lib(" into LIBNAME. */
1750 ++paren;
1751 libname = (char *) alloca (paren - n->name + 1);
1752 bcopy (n->name, libname, paren - n->name);
1753 libname[paren - n->name] = '\0';
1755 if (*paren == '\0')
1757 /* N was just "lib(", part of something like "lib( a b)".
1758 Edit it out of the chain and free its storage. */
1759 lastn->next = n->next;
1760 free (n->name);
1761 free ((char *) n);
1762 /* LASTN->next is the new stopping elt for the loop below. */
1763 n = lastn->next;
1765 else
1767 /* Replace N's name with the full archive reference. */
1768 name = concat (libname, paren, ")");
1769 free (n->name);
1770 n->name = name;
1773 if (new1->name[1] == '\0')
1775 /* NEW1 is just ")", part of something like "lib(a b )".
1776 Omit it from the chain and free its storage. */
1777 if (lastnew1 == 0)
1778 new = new1->next;
1779 else
1780 lastnew1->next = new1->next;
1781 lastn = new1;
1782 new1 = new1->next;
1783 free (lastn->name);
1784 free ((char *) lastn);
1786 else
1788 /* Replace also NEW1->name, which already has closing `)'. */
1789 name = concat (libname, new1->name, "");
1790 free (new1->name);
1791 new1->name = name;
1792 new1 = new1->next;
1795 /* Trace back from NEW1 (the end of the list) until N
1796 (the beginning of the list), rewriting each name
1797 with the full archive reference. */
1799 while (new1 != n)
1801 name = concat (libname, new1->name, ")");
1802 free (new1->name);
1803 new1->name = name;
1804 lastnew1 = new1;
1805 new1 = new1->next;
1808 else
1810 /* No frobnication happening. Just step down the list. */
1811 lastnew1 = new1;
1812 new1 = new1->next;
1815 else
1817 lastnew1 = new1;
1818 new1 = new1->next;
1821 #endif
1823 *stringp = p;
1824 return new;
1827 /* Read a line of text from STREAM into LINEBUFFER.
1828 Combine continuation lines into one line.
1829 Return the number of actual lines read (> 1 if hacked continuation lines).
1832 static unsigned int
1833 readline (linebuffer, stream, filename, lineno)
1834 struct linebuffer *linebuffer;
1835 FILE *stream;
1836 char *filename;
1837 unsigned int lineno;
1839 char *buffer = linebuffer->buffer;
1840 register char *p = linebuffer->buffer;
1841 register char *end = p + linebuffer->size;
1842 register int len, lastlen = 0;
1843 register char *p2;
1844 register unsigned int nlines = 0;
1845 register int backslash;
1847 *p = '\0';
1849 while (fgets (p, end - p, stream) != 0)
1851 len = strlen (p);
1852 if (len == 0)
1854 /* This only happens when the first thing on the line is a '\0'.
1855 It is a pretty hopeless case, but (wonder of wonders) Athena
1856 lossage strikes again! (xmkmf puts NULs in its makefiles.)
1857 There is nothing really to be done; we synthesize a newline so
1858 the following line doesn't appear to be part of this line. */
1859 makefile_error (filename, lineno,
1860 "warning: NUL character seen; rest of line ignored");
1861 p[0] = '\n';
1862 len = 1;
1865 p += len;
1866 if (p[-1] != '\n')
1868 /* Probably ran out of buffer space. */
1869 register unsigned int p_off = p - buffer;
1870 linebuffer->size *= 2;
1871 buffer = (char *) xrealloc (buffer, linebuffer->size);
1872 p = buffer + p_off;
1873 end = buffer + linebuffer->size;
1874 linebuffer->buffer = buffer;
1875 *p = '\0';
1876 lastlen = len;
1877 continue;
1880 ++nlines;
1882 if (len == 1 && p > buffer)
1883 /* P is pointing at a newline and it's the beginning of
1884 the buffer returned by the last fgets call. However,
1885 it is not necessarily the beginning of a line if P is
1886 pointing past the beginning of the holding buffer.
1887 If the buffer was just enlarged (right before the newline),
1888 we must account for that, so we pretend that the two lines
1889 were one line. */
1890 len += lastlen;
1891 lastlen = len;
1892 backslash = 0;
1893 for (p2 = p - 2; --len > 0; --p2)
1895 if (*p2 == '\\')
1896 backslash = !backslash;
1897 else
1898 break;
1901 if (!backslash)
1903 p[-1] = '\0';
1904 break;
1907 if (end - p <= 1)
1909 /* Enlarge the buffer. */
1910 register unsigned int p_off = p - buffer;
1911 linebuffer->size *= 2;
1912 buffer = (char *) xrealloc (buffer, linebuffer->size);
1913 p = buffer + p_off;
1914 end = buffer + linebuffer->size;
1915 linebuffer->buffer = buffer;
1919 if (ferror (stream))
1920 pfatal_with_name (filename);
1922 return nlines;
1925 /* Construct the list of include directories
1926 from the arguments and the default list. */
1928 void
1929 construct_include_path (arg_dirs)
1930 char **arg_dirs;
1932 register unsigned int i;
1933 #ifdef VAXC /* just don't ask ... */
1934 stat_t stbuf;
1935 #else
1936 struct stat stbuf;
1937 #endif
1938 /* Table to hold the dirs. */
1940 register unsigned int defsize = (sizeof (default_include_directories)
1941 / sizeof (default_include_directories[0]));
1942 register unsigned int max = 5;
1943 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
1944 register unsigned int idx = 0;
1946 /* First consider any dirs specified with -I switches.
1947 Ignore dirs that don't exist. */
1949 if (arg_dirs != 0)
1950 while (*arg_dirs != 0)
1952 char *dir = *arg_dirs++;
1954 if (dir[0] == '~')
1956 char *expanded = tilde_expand (dir);
1957 if (expanded != 0)
1958 dir = expanded;
1961 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
1963 if (idx == max - 1)
1965 max += 5;
1966 dirs = (char **)
1967 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
1969 dirs[idx++] = dir;
1971 else if (dir != arg_dirs[-1])
1972 free (dir);
1975 /* Now add at the end the standard default dirs. */
1977 for (i = 0; default_include_directories[i] != 0; ++i)
1978 if (stat (default_include_directories[i], &stbuf) == 0
1979 && S_ISDIR (stbuf.st_mode))
1980 dirs[idx++] = default_include_directories[i];
1982 dirs[idx] = 0;
1984 /* Now compute the maximum length of any name in it. */
1986 max_incl_len = 0;
1987 for (i = 0; i < idx; ++i)
1989 unsigned int len = strlen (dirs[i]);
1990 /* If dir name is written with a trailing slash, discard it. */
1991 if (dirs[i][len - 1] == '/')
1992 /* We can't just clobber a null in because it may have come from
1993 a literal string and literal strings may not be writable. */
1994 dirs[i] = savestring (dirs[i], len - 1);
1995 if (len > max_incl_len)
1996 max_incl_len = len;
1999 include_directories = dirs;
2002 /* Expand ~ or ~USER at the beginning of NAME.
2003 Return a newly malloc'd string or 0. */
2005 char *
2006 tilde_expand (name)
2007 char *name;
2009 #ifndef VMS
2010 if (name[1] == '/' || name[1] == '\0')
2012 extern char *getenv ();
2013 char *home_dir;
2014 int is_variable;
2017 /* Turn off --warn-undefined-variables while we expand HOME. */
2018 int save = warn_undefined_variables_flag;
2019 warn_undefined_variables_flag = 0;
2021 home_dir = allocated_variable_expand ("$(HOME)");
2023 warn_undefined_variables_flag = save;
2026 is_variable = home_dir[0] != '\0';
2027 if (!is_variable)
2029 free (home_dir);
2030 home_dir = getenv ("HOME");
2032 #if !defined(_AMIGA) && !defined(WIN32)
2033 if (home_dir == 0 || home_dir[0] == '\0')
2035 extern char *getlogin ();
2036 char *name = getlogin ();
2037 home_dir = 0;
2038 if (name != 0)
2040 struct passwd *p = getpwnam (name);
2041 if (p != 0)
2042 home_dir = p->pw_dir;
2045 #endif /* !AMIGA && !WIN32 */
2046 if (home_dir != 0)
2048 char *new = concat (home_dir, "", name + 1);
2049 if (is_variable)
2050 free (home_dir);
2051 return new;
2054 #if !defined(_AMIGA) && !defined(WIN32)
2055 else
2057 struct passwd *pwent;
2058 char *userend = index (name + 1, '/');
2059 if (userend != 0)
2060 *userend = '\0';
2061 pwent = getpwnam (name + 1);
2062 if (pwent != 0)
2064 if (userend == 0)
2065 return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
2066 else
2067 return concat (pwent->pw_dir, "/", userend + 1);
2069 else if (userend != 0)
2070 *userend = '/';
2072 #endif /* !AMIGA && !WIN32 */
2073 #endif /* !VMS */
2074 return 0;
2077 /* Given a chain of struct nameseq's describing a sequence of filenames,
2078 in reverse of the intended order, return a new chain describing the
2079 result of globbing the filenames. The new chain is in forward order.
2080 The links of the old chain are freed or used in the new chain.
2081 Likewise for the names in the old chain.
2083 SIZE is how big to construct chain elements.
2084 This is useful if we want them actually to be other structures
2085 that have room for additional info. */
2087 struct nameseq *
2088 multi_glob (chain, size)
2089 struct nameseq *chain;
2090 unsigned int size;
2092 extern void dir_setup_glob ();
2093 register struct nameseq *new = 0;
2094 register struct nameseq *old;
2095 struct nameseq *nexto;
2096 glob_t gl;
2098 dir_setup_glob (&gl);
2100 for (old = chain; old != 0; old = nexto)
2102 #ifndef NO_ARCHIVES
2103 char *memname;
2104 #endif
2106 nexto = old->next;
2108 if (old->name[0] == '~')
2110 char *newname = tilde_expand (old->name);
2111 if (newname != 0)
2113 free (old->name);
2114 old->name = newname;
2118 #ifndef NO_ARCHIVES
2119 if (ar_name (old->name))
2121 /* OLD->name is an archive member reference.
2122 Replace it with the archive file name,
2123 and save the member name in MEMNAME.
2124 We will glob on the archive name and then
2125 reattach MEMNAME later. */
2126 char *arname;
2127 ar_parse_name (old->name, &arname, &memname);
2128 free (old->name);
2129 old->name = arname;
2131 else
2132 memname = 0;
2133 #endif /* !NO_ARCHIVES */
2135 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
2137 case 0: /* Success. */
2139 register int i = gl.gl_pathc;
2140 while (i-- > 0)
2142 #ifndef NO_ARCHIVES
2143 if (memname != 0)
2145 /* Try to glob on MEMNAME within the archive. */
2146 struct nameseq *found
2147 = ar_glob (gl.gl_pathv[i], memname, size);
2148 if (found == 0)
2150 /* No matches. Use MEMNAME as-is. */
2151 struct nameseq *elt
2152 = (struct nameseq *) xmalloc (size);
2153 unsigned int alen = strlen (gl.gl_pathv[i]);
2154 unsigned int mlen = strlen (memname);
2155 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2156 bcopy (gl.gl_pathv[i], elt->name, alen);
2157 elt->name[alen] = '(';
2158 bcopy (memname, &elt->name[alen + 1], mlen);
2159 elt->name[alen + 1 + mlen] = ')';
2160 elt->name[alen + 1 + mlen + 1] = '\0';
2161 elt->next = new;
2162 new = elt;
2164 else
2166 /* Find the end of the FOUND chain. */
2167 struct nameseq *f = found;
2168 while (f->next != 0)
2169 f = f->next;
2171 /* Attach the chain being built to the end of the FOUND
2172 chain, and make FOUND the new NEW chain. */
2173 f->next = new;
2174 new = found;
2177 free (memname);
2179 else
2180 #endif /* !NO_ARCHIVES */
2182 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2183 elt->name = savestring (gl.gl_pathv[i],
2184 strlen (gl.gl_pathv[i]));
2185 elt->next = new;
2186 new = elt;
2189 globfree (&gl);
2190 free (old->name);
2191 free ((char *)old);
2192 break;
2195 case GLOB_NOSPACE:
2196 fatal ("virtual memory exhausted");
2197 break;
2199 default:
2200 old->next = new;
2201 new = old;
2202 break;
2206 return new;