Updated from libc
[make.git] / read.c
blob8d42aa818a5b60875c054dcd6004345453ba6950
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 VMS
34 #include <pwd.h>
35 #else
36 struct passwd *getpwnam PARAMS ((char *name));
37 #endif
39 /* A `struct linebuffer' is a structure which holds a line of text.
40 `readline' reads a line from a stream into a linebuffer
41 and works regardless of the length of the line. */
43 struct linebuffer
45 /* Note: This is the number of bytes malloc'ed for `buffer'
46 It does not indicate `buffer's real length.
47 Instead, a null char indicates end-of-string. */
48 unsigned int size;
49 char *buffer;
52 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
53 #define freebuffer(lb) free ((lb)->buffer)
56 /* A `struct conditionals' contains the information describing
57 all the active conditionals in a makefile.
59 The global variable `conditionals' contains the conditionals
60 information for the current makefile. It is initialized from
61 the static structure `toplevel_conditionals' and is later changed
62 to new structures for included makefiles. */
64 struct conditionals
66 unsigned int if_cmds; /* Depth of conditional nesting. */
67 unsigned int allocated; /* Elts allocated in following arrays. */
68 char *ignoring; /* Are we ignoring or interepreting? */
69 char *seen_else; /* Have we already seen an `else'? */
72 static struct conditionals toplevel_conditionals;
73 static struct conditionals *conditionals = &toplevel_conditionals;
76 /* Default directories to search for include files in */
78 static char *default_include_directories[] =
80 INCLUDEDIR,
81 #ifndef _AMIGA
82 "/usr/gnu/include",
83 "/usr/local/include",
84 "/usr/include",
85 #endif
89 /* List of directories to search for include files in */
91 static char **include_directories;
93 /* Maximum length of an element of the above. */
95 static unsigned int max_incl_len;
97 /* The filename and pointer to line number of the
98 makefile currently being read in. */
100 char *reading_filename;
101 unsigned int *reading_lineno_ptr;
103 /* The chain of makefiles read by read_makefile. */
105 static struct dep *read_makefiles = 0;
107 static int read_makefile PARAMS ((char *filename, int flags));
108 static unsigned int readline PARAMS ((struct linebuffer *linebuffer, FILE *stream,
109 char *filename, unsigned int lineno));
110 static unsigned int do_define PARAMS ((char *name, unsigned int namelen, enum variable_origin origin,
111 unsigned int lineno, FILE *infile, char *filename));
112 static int conditional_line PARAMS ((char *line, char *filename, unsigned int lineno));
113 static void record_files PARAMS ((struct nameseq *filenames, char *pattern, char *pattern_percent,
114 struct dep *deps, unsigned int commands_started, char *commands,
115 unsigned int commands_idx, int two_colon, char *filename,
116 unsigned int lineno, int set_default));
117 static char *find_semicolon PARAMS ((char *s));
119 /* Read in all the makefiles and return the chain of their names. */
121 struct dep *
122 read_all_makefiles (makefiles)
123 char **makefiles;
125 unsigned int num_makefiles = 0;
127 if (debug_flag)
128 puts ("Reading makefiles...");
130 /* If there's a non-null variable MAKEFILES, its value is a list of
131 files to read first thing. But don't let it prevent reading the
132 default makefiles and don't let the default goal come from there. */
135 char *value;
136 char *name, *p;
137 unsigned int length;
140 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
141 int save = warn_undefined_variables_flag;
142 warn_undefined_variables_flag = 0;
144 value = allocated_variable_expand ("$(MAKEFILES)");
146 warn_undefined_variables_flag = save;
149 /* Set NAME to the start of next token and LENGTH to its length.
150 MAKEFILES is updated for finding remaining tokens. */
151 p = value;
153 while ((name = find_next_token (&p, &length)) != 0)
155 if (*p != '\0')
156 *p++ = '\0';
157 (void) read_makefile (name,
158 RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
161 free (value);
164 /* Read makefiles specified with -f switches. */
166 if (makefiles != 0)
167 while (*makefiles != 0)
169 struct dep *tail = read_makefiles;
170 register struct dep *d;
172 if (! read_makefile (*makefiles, 0))
173 perror_with_name ("", *makefiles);
175 /* Find the right element of read_makefiles. */
176 d = read_makefiles;
177 while (d->next != tail)
178 d = d->next;
180 /* Use the storage read_makefile allocates. */
181 *makefiles = dep_name (d);
182 ++num_makefiles;
183 ++makefiles;
186 /* If there were no -f switches, try the default names. */
188 if (num_makefiles == 0)
190 static char *default_makefiles[] =
191 #if VMS
192 /* all lower case since readdir() (the vms version) 'lowercasifies' */
193 { "makefile.vms", "gnumakefile", "makefile", 0 };
194 #else
195 { "GNUmakefile", "makefile", "Makefile", 0 };
196 #endif
197 register char **p = default_makefiles;
198 while (*p != 0 && !file_exists_p (*p))
199 ++p;
201 if (*p != 0)
203 if (! read_makefile (*p, 0))
204 perror_with_name ("", *p);
206 else
208 /* No default makefile was found. Add the default makefiles to the
209 `read_makefiles' chain so they will be updated if possible. */
210 struct dep *tail = read_makefiles;
211 /* Add them to the tail, after any MAKEFILES variable makefiles. */
212 while (tail != 0 && tail->next != 0)
213 tail = tail->next;
214 for (p = default_makefiles; *p != 0; ++p)
216 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
217 d->name = 0;
218 d->file = enter_file (*p);
219 d->file->dontcare = 1;
220 /* Tell update_goal_chain to bail out as soon as this file is
221 made, and main not to die if we can't make this file. */
222 d->changed = RM_DONTCARE;
223 if (tail == 0)
224 read_makefiles = d;
225 else
226 tail->next = d;
227 tail = d;
229 if (tail != 0)
230 tail->next = 0;
234 return read_makefiles;
237 /* Read file FILENAME as a makefile and add its contents to the data base.
239 FLAGS contains bits as above.
241 FILENAME is added to the `read_makefiles' chain.
243 Returns 1 if a file was found and read, 0 if not. */
245 static int
246 read_makefile (filename, flags)
247 char *filename;
248 int flags;
250 static char *collapsed = 0;
251 static unsigned int collapsed_length = 0;
252 register FILE *infile;
253 struct linebuffer lb;
254 unsigned int commands_len = 200;
255 char *commands = (char *) xmalloc (200);
256 unsigned int commands_idx = 0;
257 unsigned int commands_started;
258 register char *p;
259 char *p2;
260 int ignoring = 0, in_ignored_define = 0;
261 int no_targets = 0; /* Set when reading a rule without targets. */
262 char *passed_filename = filename;
264 struct nameseq *filenames = 0;
265 struct dep *deps;
266 unsigned int lineno = 1;
267 unsigned int nlines = 0;
268 int two_colon;
269 char *pattern = 0, *pattern_percent;
271 int makefile_errno;
273 #define record_waiting_files() \
274 do \
276 if (filenames != 0) \
277 record_files (filenames, pattern, pattern_percent, deps, \
278 commands_started, commands, commands_idx, \
279 two_colon, filename, lineno, \
280 !(flags & RM_NO_DEFAULT_GOAL)); \
281 filenames = 0; \
282 commands_idx = 0; \
283 pattern = 0; \
284 } while (0)
286 #ifdef lint /* Suppress `used before set' messages. */
287 two_colon = 0;
288 #endif
290 if (debug_flag)
292 printf ("Reading makefile `%s'", filename);
293 if (flags & RM_NO_DEFAULT_GOAL)
294 printf (" (no default goal)");
295 if (flags & RM_INCLUDED)
296 printf (" (search path)");
297 if (flags & RM_DONTCARE)
298 printf (" (don't care)");
299 if (flags & RM_NO_TILDE)
300 printf (" (no ~ expansion)");
301 puts ("...");
304 /* First, get a stream to read. */
306 /* Expand ~ in FILENAME unless it came from `include',
307 in which case it was already done. */
308 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
310 char *expanded = tilde_expand (filename);
311 if (expanded != 0)
312 filename = expanded;
315 infile = fopen (filename, "r");
316 /* Save the error code so we print the right message later. */
317 makefile_errno = errno;
319 /* If the makefile wasn't found and it's either a makefile from
320 the `MAKEFILES' variable or an included makefile,
321 search the included makefile search path for this makefile. */
322 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
324 register unsigned int i;
325 for (i = 0; include_directories[i] != 0; ++i)
327 char *name = concat (include_directories[i], "/", filename);
328 infile = fopen (name, "r");
329 if (infile == 0)
330 free (name);
331 else
333 filename = name;
334 break;
339 /* Add FILENAME to the chain of read makefiles. */
340 deps = (struct dep *) xmalloc (sizeof (struct dep));
341 deps->next = read_makefiles;
342 read_makefiles = deps;
343 deps->name = 0;
344 deps->file = lookup_file (filename);
345 if (deps->file == 0)
347 deps->file = enter_file (savestring (filename, strlen (filename)));
348 if (flags & RM_DONTCARE)
349 deps->file->dontcare = 1;
351 if (filename != passed_filename)
352 free (filename);
353 filename = deps->file->name;
354 deps->changed = flags;
355 deps = 0;
357 /* If the makefile can't be found at all, give up entirely. */
359 if (infile == 0)
361 /* If we did some searching, errno has the error from the last
362 attempt, rather from FILENAME itself. Restore it in case the
363 caller wants to use it in a message. */
364 errno = makefile_errno;
365 return 0;
368 reading_filename = filename;
369 reading_lineno_ptr = &lineno;
371 /* Loop over lines in the file.
372 The strategy is to accumulate target names in FILENAMES, dependencies
373 in DEPS and commands in COMMANDS. These are used to define a rule
374 when the start of the next rule (or eof) is encountered. */
376 initbuffer (&lb);
378 while (!feof (infile))
380 lineno += nlines;
381 nlines = readline (&lb, infile, filename, lineno);
383 /* Check for a shell command line first.
384 If it is not one, we can stop treating tab specially. */
385 if (lb.buffer[0] == '\t')
387 /* This line is a probably shell command. */
388 unsigned int len;
390 if (no_targets)
391 /* Ignore the commands in a rule with no targets. */
392 continue;
394 /* If there is no preceding rule line, don't treat this line
395 as a command, even though it begins with a tab character.
396 SunOS 4 make appears to behave this way. */
398 if (filenames != 0)
400 if (ignoring)
401 /* Yep, this is a shell command, and we don't care. */
402 continue;
404 /* Append this command line to the line being accumulated. */
405 p = lb.buffer;
406 if (commands_idx == 0)
407 commands_started = lineno;
408 len = strlen (p);
409 if (len + 1 + commands_idx > commands_len)
411 commands_len = (len + 1 + commands_idx) * 2;
412 commands = (char *) xrealloc (commands, commands_len);
414 bcopy (p, &commands[commands_idx], len);
415 commands_idx += len;
416 commands[commands_idx++] = '\n';
418 continue;
422 /* This line is not a shell command line. Don't worry about tabs. */
424 if (collapsed_length < lb.size)
426 collapsed_length = lb.size;
427 if (collapsed != 0)
428 free (collapsed);
429 collapsed = (char *) xmalloc (collapsed_length);
431 strcpy (collapsed, lb.buffer);
432 /* Collapse continuation lines. */
433 collapse_continuations (collapsed);
434 remove_comments (collapsed);
436 /* strncmp is first to avoid dereferencing out into space. */
437 #define word1eq(s, l) (!strncmp (s, p, l) \
438 && (p[l] == '\0' || isblank (p[l])))
439 p = collapsed;
440 while (isspace (*p))
441 ++p;
442 if (*p == '\0')
443 /* This line is completely empty. */
444 continue;
446 /* We must first check for conditional and `define' directives before
447 ignoring anything, since they control what we will do with
448 following lines. */
450 if (!in_ignored_define
451 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
452 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
453 || word1eq ("else", 4) || word1eq ("endif", 5)))
455 int i = conditional_line (p, filename, lineno);
456 if (i >= 0)
457 ignoring = i;
458 else
459 makefile_fatal (filename, lineno,
460 "invalid syntax in conditional");
461 continue;
463 else if (word1eq ("endef", 5))
465 if (in_ignored_define)
466 in_ignored_define = 0;
467 else
468 makefile_fatal (filename, lineno, "extraneous `endef'");
469 continue;
471 else if (word1eq ("define", 6))
473 if (ignoring)
474 in_ignored_define = 1;
475 else
477 p2 = next_token (p + 6);
478 /* Let the variable name be the whole rest of the line,
479 with trailing blanks stripped (comments have already been
480 removed), so it could be a complex variable/function
481 reference that might contain blanks. */
482 p = index (p2, '\0');
483 while (isblank (p[-1]))
484 --p;
485 lineno = do_define (p2, p - p2, o_file,
486 lineno, infile, filename);
488 continue;
490 else if (word1eq ("override", 8))
492 p2 = next_token (p + 8);
493 if (p2 == 0)
494 makefile_error (filename, lineno, "empty `override' directive");
495 if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
497 if (ignoring)
498 in_ignored_define = 1;
499 else
501 p2 = next_token (p2 + 6);
502 /* Let the variable name be the whole rest of the line,
503 with trailing blanks stripped (comments have already been
504 removed), so it could be a complex variable/function
505 reference that might contain blanks. */
506 p = index (p2, '\0');
507 while (isblank (p[-1]))
508 --p;
509 lineno = do_define (p2, p - p2, o_override,
510 lineno, infile, filename);
513 else if (!ignoring
514 && !try_variable_definition (filename, lineno,
515 p2, o_override))
516 makefile_error (filename, lineno, "empty `override' directive");
518 continue;
521 if (ignoring)
522 /* Ignore the line. We continue here so conditionals
523 can appear in the middle of a rule. */
524 continue;
525 else if (word1eq ("export", 6))
527 struct variable *v;
528 p2 = next_token (p + 6);
529 if (*p2 == '\0')
530 export_all_variables = 1;
531 v = try_variable_definition (filename, lineno, p2, o_file);
532 if (v != 0)
533 v->export = v_export;
534 else
536 unsigned int len;
537 for (p = find_next_token (&p2, &len); p != 0;
538 p = find_next_token (&p2, &len))
540 v = lookup_variable (p, len);
541 if (v == 0)
542 v = define_variable (p, len, "", o_file, 0);
543 v->export = v_export;
547 else if (word1eq ("unexport", 8))
549 unsigned int len;
550 struct variable *v;
551 p2 = next_token (p + 8);
552 if (*p2 == '\0')
553 export_all_variables = 0;
554 for (p = find_next_token (&p2, &len); p != 0;
555 p = find_next_token (&p2, &len))
557 v = lookup_variable (p, len);
558 if (v == 0)
559 v = define_variable (p, len, "", o_file, 0);
560 v->export = v_noexport;
563 else if (word1eq ("include", 7) || word1eq ("-include", 8)
564 || word1eq ("sinclude", 8))
566 /* We have found an `include' line specifying a nested
567 makefile to be read at this point. */
568 struct conditionals *save, new_conditionals;
569 struct nameseq *files;
570 /* "-include" (vs "include") says no error if the file does not
571 exist. "sinclude" is an alias for this from SGI. */
572 int noerror = p[0] != 'i';
574 p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
575 if (*p == '\0')
577 makefile_error (filename, lineno,
578 "no file name for `%sinclude'",
579 noerror ? "-" : "");
580 continue;
583 /* Parse the list of file names. */
584 p2 = p;
585 files = multi_glob (parse_file_seq (&p2, '\0',
586 sizeof (struct nameseq),
588 sizeof (struct nameseq));
589 free (p);
591 /* Save the state of conditionals and start
592 the included makefile with a clean slate. */
593 save = conditionals;
594 bzero ((char *) &new_conditionals, sizeof new_conditionals);
595 conditionals = &new_conditionals;
597 /* Record the rules that are waiting so they will determine
598 the default goal before those in the included makefile. */
599 record_waiting_files ();
601 /* Read each included makefile. */
602 while (files != 0)
604 struct nameseq *next = files->next;
605 char *name = files->name;
606 free ((char *)files);
607 files = next;
609 if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
610 | (noerror ? RM_DONTCARE : 0)))
611 && ! noerror)
612 makefile_error (filename, lineno,
613 "%s: %s", name, strerror (errno));
616 /* Free any space allocated by conditional_line. */
617 if (conditionals->ignoring)
618 free (conditionals->ignoring);
619 if (conditionals->seen_else)
620 free (conditionals->seen_else);
622 /* Restore state. */
623 conditionals = save;
624 reading_filename = filename;
625 reading_lineno_ptr = &lineno;
627 else if (word1eq ("vpath", 5))
629 char *pattern;
630 unsigned int len;
631 p2 = variable_expand (p + 5);
632 p = find_next_token (&p2, &len);
633 if (p != 0)
635 pattern = savestring (p, len);
636 p = find_next_token (&p2, &len);
637 /* No searchpath means remove all previous
638 selective VPATH's with the same pattern. */
640 else
641 /* No pattern means remove all previous selective VPATH's. */
642 pattern = 0;
643 construct_vpath_list (pattern, p);
644 if (pattern != 0)
645 free (pattern);
647 #undef word1eq
648 else if (try_variable_definition (filename, lineno, p, o_file))
649 /* This line has been dealt with. */
651 else if (lb.buffer[0] == '\t')
653 p = collapsed; /* Ignore comments. */
654 while (isblank (*p))
655 ++p;
656 if (*p == '\0')
657 /* The line is completely blank; that is harmless. */
658 continue;
659 /* This line starts with a tab but was not caught above
660 because there was no preceding target, and the line
661 might have been usable as a variable definition.
662 But now it is definitely lossage. */
663 makefile_fatal (filename, lineno,
664 "commands commence before first target");
666 else
668 /* This line describes some target files. */
670 char *cmdleft;
672 /* Record the previous rule. */
674 record_waiting_files ();
676 /* Search the line for an unquoted ; that is not after an
677 unquoted #. */
678 cmdleft = find_char_unquote (lb.buffer, ";#", 0);
679 if (cmdleft != 0 && *cmdleft == '#')
681 /* We found a comment before a semicolon. */
682 *cmdleft = '\0';
683 cmdleft = 0;
685 else if (cmdleft != 0)
686 /* Found one. Cut the line short there before expanding it. */
687 *cmdleft = '\0';
689 collapse_continuations (lb.buffer);
691 /* Expand variable and function references before doing anything
692 else so that special characters can be inside variables. */
693 p = variable_expand (lb.buffer);
695 if (cmdleft == 0)
696 /* Look for a semicolon in the expanded line. */
697 cmdleft = find_char_unquote (p, ";", 0);
699 if (cmdleft != 0)
700 /* Cut the line short at the semicolon. */
701 *cmdleft = '\0';
703 p2 = next_token (p);
704 if (*p2 == '\0')
706 if (cmdleft != 0)
707 makefile_fatal (filename, lineno,
708 "missing rule before commands");
709 else
710 /* This line contained a variable reference that
711 expanded to nothing but whitespace. */
712 continue;
714 else if (*p2 == ':')
716 /* We accept and ignore rules without targets for
717 compatibility with SunOS 4 make. */
718 no_targets = 1;
719 continue;
722 filenames = multi_glob (parse_file_seq (&p2, ':',
723 sizeof (struct nameseq),
725 sizeof (struct nameseq));
726 if (*p2++ == '\0')
727 makefile_fatal (filename, lineno, "missing separator");
728 /* Is this a one-colon or two-colon entry? */
729 two_colon = *p2 == ':';
730 if (two_colon)
731 p2++;
733 /* We have some targets, so don't ignore the following commands. */
734 no_targets = 0;
736 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
737 p = index (p2, ':');
738 while (p != 0 && p[-1] == '\\')
740 register char *q = &p[-1];
741 register int backslash = 0;
742 while (*q-- == '\\')
743 backslash = !backslash;
744 if (backslash)
745 p = index (p + 1, ':');
746 else
747 break;
749 #ifdef __MSDOS__
750 /* For MS-DOS, skip a "C:\...". */
751 if (p != 0 && p[1] == '\\' && isalpha (p[-1]))
752 p = 0;
753 #endif
754 #ifdef _AMIGA
755 /* Here, the situation is quite complicated. Let's have a look
756 at a couple of targets:
758 install: dev:make
760 dev:make: make
762 dev:make:: xyz
764 The rule is that it's only a target, if there are TWO :'s
765 OR a space around the :.
767 if (p && !(isspace(p[1]) || !p[1] || isspace(p[-1])))
768 p = 0;
769 #endif
770 if (p != 0)
772 struct nameseq *target;
773 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
774 ++p2;
775 if (target == 0)
776 makefile_fatal (filename, lineno, "missing target pattern");
777 else if (target->next != 0)
778 makefile_fatal (filename, lineno, "multiple target patterns");
779 pattern = target->name;
780 pattern_percent = find_percent (pattern);
781 if (pattern_percent == 0)
782 makefile_fatal (filename, lineno,
783 "target pattern contains no `%%'");
785 else
786 pattern = 0;
788 /* Parse the dependencies. */
789 deps = (struct dep *)
790 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
791 sizeof (struct dep));
793 commands_idx = 0;
794 if (cmdleft != 0)
796 /* Semicolon means rest of line is a command. */
797 unsigned int len = strlen (cmdleft + 1);
799 commands_started = lineno;
801 /* Add this command line to the buffer. */
802 if (len + 2 > commands_len)
804 commands_len = (len + 2) * 2;
805 commands = (char *) xrealloc (commands, commands_len);
807 bcopy (cmdleft + 1, commands, len);
808 commands_idx += len;
809 commands[commands_idx++] = '\n';
812 continue;
815 /* We get here except in the case that we just read a rule line.
816 Record now the last rule we read, so following spurious
817 commands are properly diagnosed. */
818 record_waiting_files ();
819 no_targets = 0;
822 if (conditionals->if_cmds)
823 makefile_fatal (filename, lineno, "missing `endif'");
825 /* At eof, record the last rule. */
826 record_waiting_files ();
828 freebuffer (&lb);
829 free ((char *) commands);
830 fclose (infile);
832 reading_filename = 0;
833 reading_lineno_ptr = 0;
835 return 1;
838 /* Execute a `define' directive.
839 The first line has already been read, and NAME is the name of
840 the variable to be defined. The following lines remain to be read.
841 LINENO, INFILE and FILENAME refer to the makefile being read.
842 The value returned is LINENO, updated for lines read here. */
844 static unsigned int
845 do_define (name, namelen, origin, lineno, infile, filename)
846 char *name;
847 unsigned int namelen;
848 enum variable_origin origin;
849 unsigned int lineno;
850 FILE *infile;
851 char *filename;
853 struct linebuffer lb;
854 unsigned int nlines = 0;
855 unsigned int length = 100;
856 char *definition = (char *) xmalloc (100);
857 register unsigned int idx = 0;
858 register char *p;
860 /* Expand the variable name. */
861 char *var = (char *) alloca (namelen + 1);
862 bcopy (name, var, namelen);
863 var[namelen] = '\0';
864 var = variable_expand (var);
866 initbuffer (&lb);
867 while (!feof (infile))
869 lineno += nlines;
870 nlines = readline (&lb, infile, filename, lineno);
872 collapse_continuations (lb.buffer);
874 p = next_token (lb.buffer);
875 if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
877 p += 5;
878 remove_comments (p);
879 if (*next_token (p) != '\0')
880 makefile_error (filename, lineno,
881 "Extraneous text after `endef' directive");
882 /* Define the variable. */
883 if (idx == 0)
884 definition[0] = '\0';
885 else
886 definition[idx - 1] = '\0';
887 (void) define_variable (var, strlen (var), definition, origin, 1);
888 free (definition);
889 freebuffer (&lb);
890 return lineno;
892 else
894 unsigned int len = strlen (lb.buffer);
896 /* Increase the buffer size if necessary. */
897 if (idx + len + 1 > length)
899 length = (idx + len) * 2;
900 definition = (char *) xrealloc (definition, length + 1);
903 bcopy (lb.buffer, &definition[idx], len);
904 idx += len;
905 /* Separate lines with a newline. */
906 definition[idx++] = '\n';
910 /* No `endef'!! */
911 makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
913 /* NOTREACHED */
914 return 0;
917 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
918 "ifneq", "else" and "endif".
919 LINE is the input line, with the command as its first word.
921 FILENAME and LINENO are the filename and line number in the
922 current makefile. They are used for error messages.
924 Value is -1 if the line is invalid,
925 0 if following text should be interpreted,
926 1 if following text should be ignored. */
928 static int
929 conditional_line (line, filename, lineno)
930 char *line;
931 char *filename;
932 unsigned int lineno;
934 int notdef;
935 char *cmdname;
936 register unsigned int i;
938 if (*line == 'i')
940 /* It's an "if..." command. */
941 notdef = line[2] == 'n';
942 if (notdef)
944 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
945 line += cmdname[3] == 'd' ? 7 : 6;
947 else
949 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
950 line += cmdname[2] == 'd' ? 6 : 5;
953 else
955 /* It's an "else" or "endif" command. */
956 notdef = line[1] == 'n';
957 cmdname = notdef ? "endif" : "else";
958 line += notdef ? 5 : 4;
961 line = next_token (line);
963 if (*cmdname == 'e')
965 if (*line != '\0')
966 makefile_error (filename, lineno,
967 "Extraneous text after `%s' directive",
968 cmdname);
969 /* "Else" or "endif". */
970 if (conditionals->if_cmds == 0)
971 makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
972 /* NOTDEF indicates an `endif' command. */
973 if (notdef)
974 --conditionals->if_cmds;
975 else if (conditionals->seen_else[conditionals->if_cmds - 1])
976 makefile_fatal (filename, lineno, "only one `else' per conditional");
977 else
979 /* Toggle the state of ignorance. */
980 conditionals->ignoring[conditionals->if_cmds - 1]
981 = !conditionals->ignoring[conditionals->if_cmds - 1];
982 /* Record that we have seen an `else' in this conditional.
983 A second `else' will be erroneous. */
984 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
986 for (i = 0; i < conditionals->if_cmds; ++i)
987 if (conditionals->ignoring[i])
988 return 1;
989 return 0;
992 if (conditionals->allocated == 0)
994 conditionals->allocated = 5;
995 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
996 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
999 ++conditionals->if_cmds;
1000 if (conditionals->if_cmds > conditionals->allocated)
1002 conditionals->allocated += 5;
1003 conditionals->ignoring = (char *)
1004 xrealloc (conditionals->ignoring, conditionals->allocated);
1005 conditionals->seen_else = (char *)
1006 xrealloc (conditionals->seen_else, conditionals->allocated);
1009 /* Record that we have seen an `if...' but no `else' so far. */
1010 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1012 /* Search through the stack to see if we're already ignoring. */
1013 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1014 if (conditionals->ignoring[i])
1016 /* We are already ignoring, so just push a level
1017 to match the next "else" or "endif", and keep ignoring.
1018 We don't want to expand variables in the condition. */
1019 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1020 return 1;
1023 if (cmdname[notdef ? 3 : 2] == 'd')
1025 /* "Ifdef" or "ifndef". */
1026 struct variable *v;
1027 register char *p = end_of_token (line);
1028 i = p - line;
1029 p = next_token (p);
1030 if (*p != '\0')
1031 return -1;
1032 v = lookup_variable (line, i);
1033 conditionals->ignoring[conditionals->if_cmds - 1]
1034 = (v != 0 && *v->value != '\0') == notdef;
1036 else
1038 /* "Ifeq" or "ifneq". */
1039 char *s1, *s2;
1040 unsigned int len;
1041 char termin = *line == '(' ? ',' : *line;
1043 if (termin != ',' && termin != '"' && termin != '\'')
1044 return -1;
1046 s1 = ++line;
1047 /* Find the end of the first string. */
1048 if (termin == ',')
1050 register int count = 0;
1051 for (; *line != '\0'; ++line)
1052 if (*line == '(')
1053 ++count;
1054 else if (*line == ')')
1055 --count;
1056 else if (*line == ',' && count <= 0)
1057 break;
1059 else
1060 while (*line != '\0' && *line != termin)
1061 ++line;
1063 if (*line == '\0')
1064 return -1;
1066 *line++ = '\0';
1068 s2 = variable_expand (s1);
1069 /* We must allocate a new copy of the expanded string because
1070 variable_expand re-uses the same buffer. */
1071 len = strlen (s2);
1072 s1 = (char *) alloca (len + 1);
1073 bcopy (s2, s1, len + 1);
1075 if (termin != ',')
1076 /* Find the start of the second string. */
1077 line = next_token (line);
1079 termin = termin == ',' ? ')' : *line;
1080 if (termin != ')' && termin != '"' && termin != '\'')
1081 return -1;
1083 /* Find the end of the second string. */
1084 if (termin == ')')
1086 register int count = 0;
1087 s2 = next_token (line);
1088 for (line = s2; *line != '\0'; ++line)
1090 if (*line == '(')
1091 ++count;
1092 else if (*line == ')')
1093 if (count <= 0)
1094 break;
1095 else
1096 --count;
1099 else
1101 ++line;
1102 s2 = line;
1103 while (*line != '\0' && *line != termin)
1104 ++line;
1107 if (*line == '\0')
1108 return -1;
1110 *line = '\0';
1111 line = next_token (++line);
1112 if (*line != '\0')
1113 makefile_error (filename, lineno,
1114 "Extraneous text after `%s' directive",
1115 cmdname);
1117 s2 = variable_expand (s2);
1118 conditionals->ignoring[conditionals->if_cmds - 1]
1119 = streq (s1, s2) == notdef;
1122 /* Search through the stack to see if we're ignoring. */
1123 for (i = 0; i < conditionals->if_cmds; ++i)
1124 if (conditionals->ignoring[i])
1125 return 1;
1126 return 0;
1129 /* Remove duplicate dependencies in CHAIN. */
1131 void
1132 uniquize_deps (chain)
1133 struct dep *chain;
1135 register struct dep *d;
1137 /* Make sure that no dependencies are repeated. This does not
1138 really matter for the purpose of updating targets, but it
1139 might make some names be listed twice for $^ and $?. */
1141 for (d = chain; d != 0; d = d->next)
1143 struct dep *last, *next;
1145 last = d;
1146 next = d->next;
1147 while (next != 0)
1148 if (streq (dep_name (d), dep_name (next)))
1150 struct dep *n = next->next;
1151 last->next = n;
1152 if (next->name != 0 && next->name != d->name)
1153 free (next->name);
1154 if (next != d)
1155 free ((char *) next);
1156 next = n;
1158 else
1160 last = next;
1161 next = next->next;
1166 /* Record a description line for files FILENAMES,
1167 with dependencies DEPS, commands to execute described
1168 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1169 TWO_COLON is nonzero if a double colon was used.
1170 If not nil, PATTERN is the `%' pattern to make this
1171 a static pattern rule, and PATTERN_PERCENT is a pointer
1172 to the `%' within it.
1174 The links of FILENAMES are freed, and so are any names in it
1175 that are not incorporated into other data structures. */
1177 static void
1178 record_files (filenames, pattern, pattern_percent, deps, commands_started,
1179 commands, commands_idx, two_colon, filename, lineno, set_default)
1180 struct nameseq *filenames;
1181 char *pattern, *pattern_percent;
1182 struct dep *deps;
1183 unsigned int commands_started;
1184 char *commands;
1185 unsigned int commands_idx;
1186 int two_colon;
1187 char *filename;
1188 unsigned int lineno;
1189 int set_default;
1191 struct nameseq *nextf;
1192 int implicit = 0;
1193 unsigned int max_targets, target_idx;
1194 char **targets = 0, **target_percents = 0;
1195 struct commands *cmds;
1197 if (commands_idx > 0)
1199 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1200 cmds->filename = filename;
1201 cmds->lineno = commands_started;
1202 cmds->commands = savestring (commands, commands_idx);
1203 cmds->command_lines = 0;
1205 else
1206 cmds = 0;
1208 for (; filenames != 0; filenames = nextf)
1210 register char *name = filenames->name;
1211 register struct file *f;
1212 register struct dep *d;
1213 struct dep *this;
1214 char *implicit_percent;
1216 nextf = filenames->next;
1217 free ((char *) filenames);
1219 implicit_percent = find_percent (name);
1220 implicit |= implicit_percent != 0;
1222 if (implicit && pattern != 0)
1223 makefile_fatal (filename, lineno,
1224 "mixed implicit and static pattern rules");
1226 if (implicit && implicit_percent == 0)
1227 makefile_fatal (filename, lineno, "mixed implicit and normal rules");
1229 if (implicit)
1231 if (targets == 0)
1233 max_targets = 5;
1234 targets = (char **) xmalloc (5 * sizeof (char *));
1235 target_percents = (char **) xmalloc (5 * sizeof (char *));
1236 target_idx = 0;
1238 else if (target_idx == max_targets - 1)
1240 max_targets += 5;
1241 targets = (char **) xrealloc ((char *) targets,
1242 max_targets * sizeof (char *));
1243 target_percents
1244 = (char **) xrealloc ((char *) target_percents,
1245 max_targets * sizeof (char *));
1247 targets[target_idx] = name;
1248 target_percents[target_idx] = implicit_percent;
1249 ++target_idx;
1250 continue;
1253 /* If there are multiple filenames, copy the chain DEPS
1254 for all but the last one. It is not safe for the same deps
1255 to go in more than one place in the data base. */
1256 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1258 if (pattern != 0)
1259 /* If this is an extended static rule:
1260 `targets: target%pattern: dep%pattern; cmds',
1261 translate each dependency pattern into a plain filename
1262 using the target pattern and this target's name. */
1263 if (!pattern_matches (pattern, pattern_percent, name))
1265 /* Give a warning if the rule is meaningless. */
1266 makefile_error (filename, lineno,
1267 "target `%s' doesn't match the target pattern",
1268 name);
1269 this = 0;
1271 else
1273 /* We use patsubst_expand to do the work of translating
1274 the target pattern, the target's name and the dependencies'
1275 patterns into plain dependency names. */
1276 char *buffer = variable_expand ("");
1278 for (d = this; d != 0; d = d->next)
1280 char *o;
1281 char *percent = find_percent (d->name);
1282 if (percent == 0)
1283 continue;
1284 o = patsubst_expand (buffer, name, pattern, d->name,
1285 pattern_percent, percent);
1286 free (d->name);
1287 d->name = savestring (buffer, o - buffer);
1291 if (!two_colon)
1293 /* Single-colon. Combine these dependencies
1294 with others in file's existing record, if any. */
1295 f = enter_file (name);
1297 if (f->double_colon)
1298 makefile_fatal (filename, lineno,
1299 "target file `%s' has both : and :: entries",
1300 f->name);
1302 /* If CMDS == F->CMDS, this target was listed in this rule
1303 more than once. Just give a warning since this is harmless. */
1304 if (cmds != 0 && cmds == f->cmds)
1305 makefile_error
1306 (filename, lineno,
1307 "target `%s' given more than once in the same rule.",
1308 f->name);
1310 /* Check for two single-colon entries both with commands.
1311 Check is_target so that we don't lose on files such as .c.o
1312 whose commands were preinitialized. */
1313 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1315 makefile_error (cmds->filename, cmds->lineno,
1316 "warning: overriding commands for target `%s'",
1317 f->name);
1318 makefile_error (f->cmds->filename, f->cmds->lineno,
1319 "warning: ignoring old commands for target `%s'",
1320 f->name);
1323 f->is_target = 1;
1325 /* Defining .DEFAULT with no deps or cmds clears it. */
1326 if (f == default_file && this == 0 && cmds == 0)
1327 f->cmds = 0;
1328 if (cmds != 0)
1329 f->cmds = cmds;
1330 /* Defining .SUFFIXES with no dependencies
1331 clears out the list of suffixes. */
1332 if (f == suffix_file && this == 0)
1334 d = f->deps;
1335 while (d != 0)
1337 struct dep *nextd = d->next;
1338 free (d->name);
1339 free ((char *)d);
1340 d = nextd;
1342 f->deps = 0;
1344 else if (f->deps != 0)
1346 /* Add the file's old deps and the new ones in THIS together. */
1348 struct dep *firstdeps, *moredeps;
1349 if (cmds != 0)
1351 /* This is the rule with commands, so put its deps first.
1352 The rationale behind this is that $< expands to the
1353 first dep in the chain, and commands use $< expecting
1354 to get the dep that rule specifies. */
1355 firstdeps = this;
1356 moredeps = f->deps;
1358 else
1360 /* Append the new deps to the old ones. */
1361 firstdeps = f->deps;
1362 moredeps = this;
1365 if (firstdeps == 0)
1366 firstdeps = moredeps;
1367 else
1369 d = firstdeps;
1370 while (d->next != 0)
1371 d = d->next;
1372 d->next = moredeps;
1375 f->deps = firstdeps;
1377 else
1378 f->deps = this;
1380 /* If this is a static pattern rule, set the file's stem to
1381 the part of its name that matched the `%' in the pattern,
1382 so you can use $* in the commands. */
1383 if (pattern != 0)
1385 static char *percent = "%";
1386 char *buffer = variable_expand ("");
1387 char *o = patsubst_expand (buffer, name, pattern, percent,
1388 pattern_percent, percent);
1389 f->stem = savestring (buffer, o - buffer);
1392 else
1394 /* Double-colon. Make a new record
1395 even if the file already has one. */
1396 f = lookup_file (name);
1397 /* Check for both : and :: rules. Check is_target so
1398 we don't lose on default suffix rules or makefiles. */
1399 if (f != 0 && f->is_target && !f->double_colon)
1400 makefile_fatal (filename, lineno,
1401 "target file `%s' has both : and :: entries",
1402 f->name);
1403 f = enter_file (name);
1404 /* If there was an existing entry and it was a double-colon
1405 entry, enter_file will have returned a new one, making it the
1406 prev pointer of the old one, and setting its double_colon
1407 pointer to the first one. */
1408 if (f->double_colon == 0)
1409 /* This is the first entry for this name, so we must
1410 set its double_colon pointer to itself. */
1411 f->double_colon = f;
1412 f->is_target = 1;
1413 f->deps = this;
1414 f->cmds = cmds;
1417 /* Free name if not needed further. */
1418 if (f != 0 && name != f->name
1419 && (name < f->name || name > f->name + strlen (f->name)))
1421 free (name);
1422 name = f->name;
1425 /* See if this is first target seen whose name does
1426 not start with a `.', unless it contains a slash. */
1427 if (default_goal_file == 0 && set_default
1428 && (*name != '.' || index (name, '/') != 0))
1430 int reject = 0;
1432 /* If this file is a suffix, don't
1433 let it be the default goal file. */
1435 for (d = suffix_file->deps; d != 0; d = d->next)
1437 register struct dep *d2;
1438 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1440 reject = 1;
1441 break;
1443 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1445 register unsigned int len = strlen (dep_name (d2));
1446 if (strncmp (name, dep_name (d2), len))
1447 continue;
1448 if (streq (name + len, dep_name (d)))
1450 reject = 1;
1451 break;
1454 if (reject)
1455 break;
1458 if (!reject)
1459 default_goal_file = f;
1463 if (implicit)
1465 targets[target_idx] = 0;
1466 target_percents[target_idx] = 0;
1467 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1468 free ((char *) target_percents);
1472 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1473 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1474 Quoting backslashes are removed from STRING by compacting it into
1475 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1476 one, or nil if there are none. */
1478 char *
1479 find_char_unquote (string, stopchars, blank)
1480 char *string;
1481 char *stopchars;
1482 int blank;
1484 unsigned int string_len = strlen (string);
1485 register char *p = string;
1487 while (1)
1489 while (*p != '\0' && index (stopchars, *p) == 0
1490 && (!blank || !isblank (*p)))
1491 ++p;
1492 if (*p == '\0')
1493 break;
1495 if (p > string && p[-1] == '\\')
1497 /* Search for more backslashes. */
1498 register int i = -2;
1499 while (&p[i] >= string && p[i] == '\\')
1500 --i;
1501 ++i;
1502 /* The number of backslashes is now -I.
1503 Copy P over itself to swallow half of them. */
1504 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1505 p += i / 2;
1506 if (i % 2 == 0)
1507 /* All the backslashes quoted each other; the STOPCHAR was
1508 unquoted. */
1509 return p;
1511 /* The STOPCHAR was quoted by a backslash. Look for another. */
1513 else
1514 /* No backslash in sight. */
1515 return p;
1518 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1519 return 0;
1522 /* Search PATTERN for an unquoted %. */
1524 char *
1525 find_percent (pattern)
1526 char *pattern;
1528 return find_char_unquote (pattern, "%", 0);
1531 /* Parse a string into a sequence of filenames represented as a
1532 chain of struct nameseq's in reverse order and return that chain.
1534 The string is passed as STRINGP, the address of a string pointer.
1535 The string pointer is updated to point at the first character
1536 not parsed, which either is a null char or equals STOPCHAR.
1538 SIZE is how big to construct chain elements.
1539 This is useful if we want them actually to be other structures
1540 that have room for additional info.
1542 If STRIP is nonzero, strip `./'s off the beginning. */
1544 struct nameseq *
1545 parse_file_seq (stringp, stopchar, size, strip)
1546 char **stringp;
1547 int stopchar;
1548 unsigned int size;
1549 int strip;
1551 register struct nameseq *new = 0;
1552 register struct nameseq *new1, *lastnew1;
1553 register char *p = *stringp;
1554 char *q;
1555 char *name;
1556 char stopchars[3];
1558 #ifdef VMS
1559 stopchars[0] = ',';
1560 stopchars[1] = stopchar;
1561 stopchars[2] = '\0';
1562 #else
1563 stopchars[0] = stopchar;
1564 stopchars[1] = '\0';
1565 #endif
1567 while (1)
1569 /* Skip whitespace; see if any more names are left. */
1570 p = next_token (p);
1571 if (*p == '\0')
1572 break;
1573 if (*p == stopchar)
1574 break;
1576 /* Yes, find end of next name. */
1577 q = p;
1578 p = find_char_unquote (q, stopchars, 1);
1579 #ifdef VMS
1580 /* convert comma separated list to space separated */
1581 if (p && *p == ',')
1582 *p =' ';
1583 #endif
1585 #ifdef __MSDOS__
1586 /* For MS-DOS, skip a "C:\...". */
1587 if (stopchar == ':' && p != 0 && p[1] == '\\' && isalpha (p[-1]))
1588 p = 0;
1589 #endif
1590 #ifdef _AMIGA
1591 if (stopchar == ':' && p && *p == ':' &&
1592 !(isspace(p[1]) || !p[1] || isspace(p[-1])))
1594 p = find_char_unquote (p+1, stopchars, 1);
1596 #endif
1597 if (p == 0)
1598 p = q + strlen (q);
1600 if (strip)
1601 #ifdef VMS
1602 /* Skip leading `[]'s. */
1603 while (p - q > 2 && q[0] == '[' && q[1] == ']')
1604 #else
1605 /* Skip leading `./'s. */
1606 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1607 #endif
1609 q += 2; /* Skip "./". */
1610 while (q < p && *q == '/')
1611 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1612 ++q;
1615 /* Extract the filename just found, and skip it. */
1617 if (q == p)
1618 /* ".///" was stripped to "". */
1619 #ifdef VMS
1620 continue;
1621 #else
1622 name = savestring ("./", 2);
1623 #endif
1624 else
1625 #ifdef VMS
1626 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
1627 * to remove this '\' before we can use the filename.
1628 * Savestring called because q may be read-only string constant.
1631 char *qbase = savestring(q, strlen(q));
1632 char *pbase = qbase + (p-q);
1633 char *q1 = qbase;
1634 char *q2 = q1;
1635 char *p1 = pbase;
1637 while (q1 != pbase)
1639 if (*q1 == '\\' && *(q1+1) == ':')
1641 q1++;
1642 p1--;
1644 *q2++ = *q1++;
1646 name = savestring (qbase, p1 - qbase);
1647 free (qbase);
1649 #else
1650 name = savestring (q, p - q);
1651 #endif
1653 /* Add it to the front of the chain. */
1654 new1 = (struct nameseq *) xmalloc (size);
1655 new1->name = name;
1656 new1->next = new;
1657 new = new1;
1660 #ifndef NO_ARCHIVES
1662 /* Look for multi-word archive references.
1663 They are indicated by a elt ending with an unmatched `)' and
1664 an elt further down the chain (i.e., previous in the file list)
1665 with an unmatched `(' (e.g., "lib(mem"). */
1667 new1 = new;
1668 lastnew1 = 0;
1669 while (new1 != 0)
1670 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1671 && new1->name[strlen (new1->name) - 1] == ')'
1672 && index (new1->name, '(') == 0)
1674 /* NEW1 ends with a `)' but does not contain a `('.
1675 Look back for an elt with an opening `(' but no closing `)'. */
1677 struct nameseq *n = new1->next, *lastn = new1;
1678 char *paren;
1679 while (n != 0 && (paren = index (n->name, '(')) == 0)
1681 lastn = n;
1682 n = n->next;
1684 if (n != 0
1685 /* Ignore something starting with `(', as that cannot actually
1686 be an archive-member reference (and treating it as such
1687 results in an empty file name, which causes much lossage). */
1688 && n->name[0] != '(')
1690 /* N is the first element in the archive group.
1691 Its name looks like "lib(mem" (with no closing `)'). */
1693 char *libname;
1695 /* Copy "lib(" into LIBNAME. */
1696 ++paren;
1697 libname = (char *) alloca (paren - n->name + 1);
1698 bcopy (n->name, libname, paren - n->name);
1699 libname[paren - n->name] = '\0';
1701 if (*paren == '\0')
1703 /* N was just "lib(", part of something like "lib( a b)".
1704 Edit it out of the chain and free its storage. */
1705 lastn->next = n->next;
1706 free (n->name);
1707 free ((char *) n);
1708 /* LASTN->next is the new stopping elt for the loop below. */
1709 n = lastn->next;
1711 else
1713 /* Replace N's name with the full archive reference. */
1714 name = concat (libname, paren, ")");
1715 free (n->name);
1716 n->name = name;
1719 if (new1->name[1] == '\0')
1721 /* NEW1 is just ")", part of something like "lib(a b )".
1722 Omit it from the chain and free its storage. */
1723 if (lastnew1 == 0)
1724 new = new1->next;
1725 else
1726 lastnew1->next = new1->next;
1727 lastn = new1;
1728 new1 = new1->next;
1729 free (lastn->name);
1730 free ((char *) lastn);
1732 else
1734 /* Replace also NEW1->name, which already has closing `)'. */
1735 name = concat (libname, new1->name, "");
1736 free (new1->name);
1737 new1->name = name;
1738 new1 = new1->next;
1741 /* Trace back from NEW1 (the end of the list) until N
1742 (the beginning of the list), rewriting each name
1743 with the full archive reference. */
1745 while (new1 != n)
1747 name = concat (libname, new1->name, ")");
1748 free (new1->name);
1749 new1->name = name;
1750 lastnew1 = new1;
1751 new1 = new1->next;
1754 else
1756 /* No frobnication happening. Just step down the list. */
1757 lastnew1 = new1;
1758 new1 = new1->next;
1761 else
1763 lastnew1 = new1;
1764 new1 = new1->next;
1767 #endif
1769 *stringp = p;
1770 return new;
1773 /* Read a line of text from STREAM into LINEBUFFER.
1774 Combine continuation lines into one line.
1775 Return the number of actual lines read (> 1 if hacked continuation lines).
1778 static unsigned int
1779 readline (linebuffer, stream, filename, lineno)
1780 struct linebuffer *linebuffer;
1781 FILE *stream;
1782 char *filename;
1783 unsigned int lineno;
1785 char *buffer = linebuffer->buffer;
1786 register char *p = linebuffer->buffer;
1787 register char *end = p + linebuffer->size;
1788 register int len, lastlen = 0;
1789 register char *p2;
1790 register unsigned int nlines = 0;
1791 register int backslash;
1793 *p = '\0';
1795 while (fgets (p, end - p, stream) != 0)
1797 len = strlen (p);
1798 if (len == 0)
1800 /* This only happens when the first thing on the line is a '\0'.
1801 It is a pretty hopeless case, but (wonder of wonders) Athena
1802 lossage strikes again! (xmkmf puts NULs in its makefiles.)
1803 There is nothing really to be done; we synthesize a newline so
1804 the following line doesn't appear to be part of this line. */
1805 makefile_error (filename, lineno,
1806 "warning: NUL character seen; rest of line ignored");
1807 p[0] = '\n';
1808 len = 1;
1811 p += len;
1812 if (p[-1] != '\n')
1814 /* Probably ran out of buffer space. */
1815 register unsigned int p_off = p - buffer;
1816 linebuffer->size *= 2;
1817 buffer = (char *) xrealloc (buffer, linebuffer->size);
1818 p = buffer + p_off;
1819 end = buffer + linebuffer->size;
1820 linebuffer->buffer = buffer;
1821 *p = '\0';
1822 lastlen = len;
1823 continue;
1826 ++nlines;
1828 if (len == 1 && p > buffer)
1829 /* P is pointing at a newline and it's the beginning of
1830 the buffer returned by the last fgets call. However,
1831 it is not necessarily the beginning of a line if P is
1832 pointing past the beginning of the holding buffer.
1833 If the buffer was just enlarged (right before the newline),
1834 we must account for that, so we pretend that the two lines
1835 were one line. */
1836 len += lastlen;
1837 lastlen = len;
1838 backslash = 0;
1839 for (p2 = p - 2; --len > 0; --p2)
1841 if (*p2 == '\\')
1842 backslash = !backslash;
1843 else
1844 break;
1847 if (!backslash)
1849 p[-1] = '\0';
1850 break;
1853 if (end - p <= 1)
1855 /* Enlarge the buffer. */
1856 register unsigned int p_off = p - buffer;
1857 linebuffer->size *= 2;
1858 buffer = (char *) xrealloc (buffer, linebuffer->size);
1859 p = buffer + p_off;
1860 end = buffer + linebuffer->size;
1861 linebuffer->buffer = buffer;
1865 if (ferror (stream))
1866 pfatal_with_name (filename);
1868 return nlines;
1871 /* Construct the list of include directories
1872 from the arguments and the default list. */
1874 void
1875 construct_include_path (arg_dirs)
1876 char **arg_dirs;
1878 register unsigned int i;
1879 #ifdef VAXC /* just don't ask ... */
1880 stat_t stbuf;
1881 #else
1882 struct stat stbuf;
1883 #endif
1884 /* Table to hold the dirs. */
1886 register unsigned int defsize = (sizeof (default_include_directories)
1887 / sizeof (default_include_directories[0]));
1888 register unsigned int max = 5;
1889 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
1890 register unsigned int idx = 0;
1892 /* First consider any dirs specified with -I switches.
1893 Ignore dirs that don't exist. */
1895 if (arg_dirs != 0)
1896 while (*arg_dirs != 0)
1898 char *dir = *arg_dirs++;
1900 if (dir[0] == '~')
1902 char *expanded = tilde_expand (dir);
1903 if (expanded != 0)
1904 dir = expanded;
1907 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
1909 if (idx == max - 1)
1911 max += 5;
1912 dirs = (char **)
1913 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
1915 dirs[idx++] = dir;
1917 else if (dir != arg_dirs[-1])
1918 free (dir);
1921 /* Now add at the end the standard default dirs. */
1923 for (i = 0; default_include_directories[i] != 0; ++i)
1924 if (stat (default_include_directories[i], &stbuf) == 0
1925 && S_ISDIR (stbuf.st_mode))
1926 dirs[idx++] = default_include_directories[i];
1928 dirs[idx] = 0;
1930 /* Now compute the maximum length of any name in it. */
1932 max_incl_len = 0;
1933 for (i = 0; i < idx; ++i)
1935 unsigned int len = strlen (dirs[i]);
1936 /* If dir name is written with a trailing slash, discard it. */
1937 if (dirs[i][len - 1] == '/')
1938 /* We can't just clobber a null in because it may have come from
1939 a literal string and literal strings may not be writable. */
1940 dirs[i] = savestring (dirs[i], len - 1);
1941 if (len > max_incl_len)
1942 max_incl_len = len;
1945 include_directories = dirs;
1948 /* Expand ~ or ~USER at the beginning of NAME.
1949 Return a newly malloc'd string or 0. */
1951 char *
1952 tilde_expand (name)
1953 char *name;
1955 #ifndef VMS
1956 if (name[1] == '/' || name[1] == '\0')
1958 extern char *getenv ();
1959 char *home_dir;
1960 int is_variable;
1963 /* Turn off --warn-undefined-variables while we expand HOME. */
1964 int save = warn_undefined_variables_flag;
1965 warn_undefined_variables_flag = 0;
1967 home_dir = allocated_variable_expand ("$(HOME)");
1969 warn_undefined_variables_flag = save;
1972 is_variable = home_dir[0] != '\0';
1973 if (!is_variable)
1975 free (home_dir);
1976 home_dir = getenv ("HOME");
1978 #ifndef _AMIGA
1979 if (home_dir == 0 || home_dir[0] == '\0')
1981 extern char *getlogin ();
1982 char *name = getlogin ();
1983 home_dir = 0;
1984 if (name != 0)
1986 struct passwd *p = getpwnam (name);
1987 if (p != 0)
1988 home_dir = p->pw_dir;
1991 #endif
1992 if (home_dir != 0)
1994 char *new = concat (home_dir, "", name + 1);
1995 if (is_variable)
1996 free (home_dir);
1997 return new;
2000 #ifndef _AMIGA
2001 else
2003 struct passwd *pwent;
2004 char *userend = index (name + 1, '/');
2005 if (userend != 0)
2006 *userend = '\0';
2007 pwent = getpwnam (name + 1);
2008 if (pwent != 0)
2010 if (userend == 0)
2011 return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
2012 else
2013 return concat (pwent->pw_dir, "/", userend + 1);
2015 else if (userend != 0)
2016 *userend = '/';
2018 #endif /* !VMS */
2019 return 0;
2022 /* Given a chain of struct nameseq's describing a sequence of filenames,
2023 in reverse of the intended order, return a new chain describing the
2024 result of globbing the filenames. The new chain is in forward order.
2025 The links of the old chain are freed or used in the new chain.
2026 Likewise for the names in the old chain.
2028 SIZE is how big to construct chain elements.
2029 This is useful if we want them actually to be other structures
2030 that have room for additional info. */
2032 struct nameseq *
2033 multi_glob (chain, size)
2034 struct nameseq *chain;
2035 unsigned int size;
2037 extern void dir_setup_glob ();
2038 register struct nameseq *new = 0;
2039 register struct nameseq *old;
2040 struct nameseq *nexto;
2041 glob_t gl;
2043 dir_setup_glob (&gl);
2045 for (old = chain; old != 0; old = nexto)
2047 #ifndef NO_ARCHIVES
2048 char *memname;
2049 #endif
2051 nexto = old->next;
2053 if (old->name[0] == '~')
2055 char *newname = tilde_expand (old->name);
2056 if (newname != 0)
2058 free (old->name);
2059 old->name = newname;
2063 #ifndef NO_ARCHIVES
2064 if (ar_name (old->name))
2066 /* OLD->name is an archive member reference.
2067 Replace it with the archive file name,
2068 and save the member name in MEMNAME.
2069 We will glob on the archive name and then
2070 reattach MEMNAME later. */
2071 char *arname;
2072 ar_parse_name (old->name, &arname, &memname);
2073 free (old->name);
2074 old->name = arname;
2076 else
2077 memname = 0;
2078 #endif
2080 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
2082 case 0: /* Success. */
2084 register int i = gl.gl_pathc;
2085 while (i-- > 0)
2087 #ifndef NO_ARCHIVES
2088 if (memname != 0)
2090 /* Try to glob on MEMNAME within the archive. */
2091 struct nameseq *found
2092 = ar_glob (gl.gl_pathv[i], memname, size);
2093 if (found == 0)
2095 /* No matches. Use MEMNAME as-is. */
2096 struct nameseq *elt
2097 = (struct nameseq *) xmalloc (size);
2098 unsigned int alen = strlen (gl.gl_pathv[i]);
2099 unsigned int mlen = strlen (memname);
2100 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2101 bcopy (gl.gl_pathv[i], elt->name, alen);
2102 elt->name[alen] = '(';
2103 bcopy (memname, &elt->name[alen + 1], mlen);
2104 elt->name[alen + 1 + mlen] = ')';
2105 elt->name[alen + 1 + mlen + 1] = '\0';
2106 elt->next = new;
2107 new = elt;
2109 else
2111 /* Find the end of the FOUND chain. */
2112 struct nameseq *f = found;
2113 while (f->next != 0)
2114 f = f->next;
2116 /* Attach the chain being built to the end of the FOUND
2117 chain, and make FOUND the new NEW chain. */
2118 f->next = new;
2119 new = found;
2122 free (memname);
2124 else
2125 #endif
2127 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2128 elt->name = savestring (gl.gl_pathv[i],
2129 strlen (gl.gl_pathv[i]));
2130 elt->next = new;
2131 new = elt;
2134 globfree (&gl);
2135 free (old->name);
2136 free ((char *)old);
2137 break;
2140 case GLOB_NOSPACE:
2141 fatal ("virtual memory exhausted");
2142 break;
2144 default:
2145 old->next = new;
2146 new = old;
2147 break;
2150 #endif
2152 return new;