Sun May 12 19:19:43 1996 Aaron Digulla <digulla@fh-konstanz.de>
[make.git] / read.c
blob97af2caf69305cfb5f394b1a31b69f285294cfc0
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 _AMIGA
34 #ifndef VMS
35 #include <pwd.h>
36 #else
37 struct passwd *getpwnam PARAMS ((char *name));
38 #endif
39 #endif
41 /* A `struct linebuffer' is a structure which holds a line of text.
42 `readline' reads a line from a stream into a linebuffer
43 and works regardless of the length of the line. */
45 struct linebuffer
47 /* Note: This is the number of bytes malloc'ed for `buffer'
48 It does not indicate `buffer's real length.
49 Instead, a null char indicates end-of-string. */
50 unsigned int size;
51 char *buffer;
54 #define initbuffer(lb) (lb)->buffer = (char *) xmalloc ((lb)->size = 200)
55 #define freebuffer(lb) free ((lb)->buffer)
58 /* A `struct conditionals' contains the information describing
59 all the active conditionals in a makefile.
61 The global variable `conditionals' contains the conditionals
62 information for the current makefile. It is initialized from
63 the static structure `toplevel_conditionals' and is later changed
64 to new structures for included makefiles. */
66 struct conditionals
68 unsigned int if_cmds; /* Depth of conditional nesting. */
69 unsigned int allocated; /* Elts allocated in following arrays. */
70 char *ignoring; /* Are we ignoring or interepreting? */
71 char *seen_else; /* Have we already seen an `else'? */
74 static struct conditionals toplevel_conditionals;
75 static struct conditionals *conditionals = &toplevel_conditionals;
78 /* Default directories to search for include files in */
80 static char *default_include_directories[] =
82 INCLUDEDIR,
83 #ifndef _AMIGA
84 "/usr/gnu/include",
85 "/usr/local/include",
86 "/usr/include",
87 #endif
91 /* List of directories to search for include files in */
93 static char **include_directories;
95 /* Maximum length of an element of the above. */
97 static unsigned int max_incl_len;
99 /* The filename and pointer to line number of the
100 makefile currently being read in. */
102 char *reading_filename;
103 unsigned int *reading_lineno_ptr;
105 /* The chain of makefiles read by read_makefile. */
107 static struct dep *read_makefiles = 0;
109 static int read_makefile PARAMS ((char *filename, int flags));
110 static unsigned int readline PARAMS ((struct linebuffer *linebuffer, FILE *stream,
111 char *filename, unsigned int lineno));
112 static unsigned int do_define PARAMS ((char *name, unsigned int namelen, enum variable_origin origin,
113 unsigned int lineno, FILE *infile, char *filename));
114 static int conditional_line PARAMS ((char *line, char *filename, unsigned int lineno));
115 static void record_files PARAMS ((struct nameseq *filenames, char *pattern, char *pattern_percent,
116 struct dep *deps, unsigned int commands_started, char *commands,
117 unsigned int commands_idx, int two_colon, char *filename,
118 unsigned int lineno, int set_default));
120 /* Read in all the makefiles and return the chain of their names. */
122 struct dep *
123 read_all_makefiles (makefiles)
124 char **makefiles;
126 unsigned int num_makefiles = 0;
128 if (debug_flag)
129 puts ("Reading makefiles...");
131 /* If there's a non-null variable MAKEFILES, its value is a list of
132 files to read first thing. But don't let it prevent reading the
133 default makefiles and don't let the default goal come from there. */
136 char *value;
137 char *name, *p;
138 unsigned int length;
141 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
142 int save = warn_undefined_variables_flag;
143 warn_undefined_variables_flag = 0;
145 value = allocated_variable_expand ("$(MAKEFILES)");
147 warn_undefined_variables_flag = save;
150 /* Set NAME to the start of next token and LENGTH to its length.
151 MAKEFILES is updated for finding remaining tokens. */
152 p = value;
154 while ((name = find_next_token (&p, &length)) != 0)
156 if (*p != '\0')
157 *p++ = '\0';
158 (void) read_makefile (name,
159 RM_NO_DEFAULT_GOAL | RM_INCLUDED | RM_DONTCARE);
162 free (value);
165 /* Read makefiles specified with -f switches. */
167 if (makefiles != 0)
168 while (*makefiles != 0)
170 struct dep *tail = read_makefiles;
171 register struct dep *d;
173 if (! read_makefile (*makefiles, 0))
174 perror_with_name ("", *makefiles);
176 /* Find the right element of read_makefiles. */
177 d = read_makefiles;
178 while (d->next != tail)
179 d = d->next;
181 /* Use the storage read_makefile allocates. */
182 *makefiles = dep_name (d);
183 ++num_makefiles;
184 ++makefiles;
187 /* If there were no -f switches, try the default names. */
189 if (num_makefiles == 0)
191 static char *default_makefiles[] =
192 #ifdef VMS
193 /* all lower case since readdir() (the vms version) 'lowercasifies' */
194 { "makefile.vms", "gnumakefile", "makefile", 0 };
195 #else
196 #ifdef _AMIGA
197 { "GNUmakefile", "Makefile", "SMakefile", 0 };
198 #else /* !Amiga && !VMS */
199 { "GNUmakefile", "makefile", "Makefile", 0 };
200 #endif /* AMIGA */
201 #endif /* VMS */
202 register char **p = default_makefiles;
203 while (*p != 0 && !file_exists_p (*p))
204 ++p;
206 if (*p != 0)
208 if (! read_makefile (*p, 0))
209 perror_with_name ("", *p);
211 else
213 /* No default makefile was found. Add the default makefiles to the
214 `read_makefiles' chain so they will be updated if possible. */
215 struct dep *tail = read_makefiles;
216 /* Add them to the tail, after any MAKEFILES variable makefiles. */
217 while (tail != 0 && tail->next != 0)
218 tail = tail->next;
219 for (p = default_makefiles; *p != 0; ++p)
221 struct dep *d = (struct dep *) xmalloc (sizeof (struct dep));
222 d->name = 0;
223 d->file = enter_file (*p);
224 d->file->dontcare = 1;
225 /* Tell update_goal_chain to bail out as soon as this file is
226 made, and main not to die if we can't make this file. */
227 d->changed = RM_DONTCARE;
228 if (tail == 0)
229 read_makefiles = d;
230 else
231 tail->next = d;
232 tail = d;
234 if (tail != 0)
235 tail->next = 0;
239 return read_makefiles;
242 /* Read file FILENAME as a makefile and add its contents to the data base.
244 FLAGS contains bits as above.
246 FILENAME is added to the `read_makefiles' chain.
248 Returns 1 if a file was found and read, 0 if not. */
250 static int
251 read_makefile (filename, flags)
252 char *filename;
253 int flags;
255 static char *collapsed = 0;
256 static unsigned int collapsed_length = 0;
257 register FILE *infile;
258 struct linebuffer lb;
259 unsigned int commands_len = 200;
260 char *commands = (char *) xmalloc (200);
261 unsigned int commands_idx = 0;
262 unsigned int commands_started;
263 register char *p;
264 char *p2;
265 int ignoring = 0, in_ignored_define = 0;
266 int no_targets = 0; /* Set when reading a rule without targets. */
267 char *passed_filename = filename;
269 struct nameseq *filenames = 0;
270 struct dep *deps;
271 unsigned int lineno = 1;
272 unsigned int nlines = 0;
273 int two_colon;
274 char *pattern = 0, *pattern_percent;
276 int makefile_errno;
278 #define record_waiting_files() \
279 do \
281 if (filenames != 0) \
282 record_files (filenames, pattern, pattern_percent, deps, \
283 commands_started, commands, commands_idx, \
284 two_colon, filename, lineno, \
285 !(flags & RM_NO_DEFAULT_GOAL)); \
286 filenames = 0; \
287 commands_idx = 0; \
288 pattern = 0; \
289 } while (0)
291 #ifdef lint /* Suppress `used before set' messages. */
292 two_colon = 0;
293 pattern_percent = 0;
294 #endif
296 if (debug_flag)
298 printf ("Reading makefile `%s'", filename);
299 if (flags & RM_NO_DEFAULT_GOAL)
300 printf (" (no default goal)");
301 if (flags & RM_INCLUDED)
302 printf (" (search path)");
303 if (flags & RM_DONTCARE)
304 printf (" (don't care)");
305 if (flags & RM_NO_TILDE)
306 printf (" (no ~ expansion)");
307 puts ("...");
310 /* First, get a stream to read. */
312 /* Expand ~ in FILENAME unless it came from `include',
313 in which case it was already done. */
314 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
316 char *expanded = tilde_expand (filename);
317 if (expanded != 0)
318 filename = expanded;
321 infile = fopen (filename, "r");
322 /* Save the error code so we print the right message later. */
323 makefile_errno = errno;
325 /* If the makefile wasn't found and it's either a makefile from
326 the `MAKEFILES' variable or an included makefile,
327 search the included makefile search path for this makefile. */
328 if (infile == 0 && (flags & RM_INCLUDED) && *filename != '/')
330 register unsigned int i;
331 for (i = 0; include_directories[i] != 0; ++i)
333 char *name = concat (include_directories[i], "/", filename);
334 infile = fopen (name, "r");
335 if (infile == 0)
336 free (name);
337 else
339 filename = name;
340 break;
345 /* Add FILENAME to the chain of read makefiles. */
346 deps = (struct dep *) xmalloc (sizeof (struct dep));
347 deps->next = read_makefiles;
348 read_makefiles = deps;
349 deps->name = 0;
350 deps->file = lookup_file (filename);
351 if (deps->file == 0)
353 deps->file = enter_file (savestring (filename, strlen (filename)));
354 if (flags & RM_DONTCARE)
355 deps->file->dontcare = 1;
357 if (filename != passed_filename)
358 free (filename);
359 filename = deps->file->name;
360 deps->changed = flags;
361 deps = 0;
363 /* If the makefile can't be found at all, give up entirely. */
365 if (infile == 0)
367 /* If we did some searching, errno has the error from the last
368 attempt, rather from FILENAME itself. Restore it in case the
369 caller wants to use it in a message. */
370 errno = makefile_errno;
371 return 0;
374 reading_filename = filename;
375 reading_lineno_ptr = &lineno;
377 /* Loop over lines in the file.
378 The strategy is to accumulate target names in FILENAMES, dependencies
379 in DEPS and commands in COMMANDS. These are used to define a rule
380 when the start of the next rule (or eof) is encountered. */
382 initbuffer (&lb);
384 while (!feof (infile))
386 lineno += nlines;
387 nlines = readline (&lb, infile, filename, lineno);
389 /* Check for a shell command line first.
390 If it is not one, we can stop treating tab specially. */
391 if (lb.buffer[0] == '\t')
393 /* This line is a probably shell command. */
394 unsigned int len;
396 if (no_targets)
397 /* Ignore the commands in a rule with no targets. */
398 continue;
400 /* If there is no preceding rule line, don't treat this line
401 as a command, even though it begins with a tab character.
402 SunOS 4 make appears to behave this way. */
404 if (filenames != 0)
406 if (ignoring)
407 /* Yep, this is a shell command, and we don't care. */
408 continue;
410 /* Append this command line to the line being accumulated. */
411 p = lb.buffer;
412 if (commands_idx == 0)
413 commands_started = lineno;
414 len = strlen (p);
415 if (len + 1 + commands_idx > commands_len)
417 commands_len = (len + 1 + commands_idx) * 2;
418 commands = (char *) xrealloc (commands, commands_len);
420 bcopy (p, &commands[commands_idx], len);
421 commands_idx += len;
422 commands[commands_idx++] = '\n';
424 continue;
428 /* This line is not a shell command line. Don't worry about tabs. */
430 if (collapsed_length < lb.size)
432 collapsed_length = lb.size;
433 if (collapsed != 0)
434 free (collapsed);
435 collapsed = (char *) xmalloc (collapsed_length);
437 strcpy (collapsed, lb.buffer);
438 /* Collapse continuation lines. */
439 collapse_continuations (collapsed);
440 remove_comments (collapsed);
442 /* strncmp is first to avoid dereferencing out into space. */
443 #define word1eq(s, l) (!strncmp (s, p, l) \
444 && (p[l] == '\0' || isblank (p[l])))
445 p = collapsed;
446 while (isspace (*p))
447 ++p;
448 if (*p == '\0')
449 /* This line is completely empty. */
450 continue;
452 /* We must first check for conditional and `define' directives before
453 ignoring anything, since they control what we will do with
454 following lines. */
456 if (!in_ignored_define
457 && (word1eq ("ifdef", 5) || word1eq ("ifndef", 6)
458 || word1eq ("ifeq", 4) || word1eq ("ifneq", 5)
459 || word1eq ("else", 4) || word1eq ("endif", 5)))
461 int i = conditional_line (p, filename, lineno);
462 if (i >= 0)
463 ignoring = i;
464 else
465 makefile_fatal (filename, lineno,
466 "invalid syntax in conditional");
467 continue;
469 else if (word1eq ("endef", 5))
471 if (in_ignored_define)
472 in_ignored_define = 0;
473 else
474 makefile_fatal (filename, lineno, "extraneous `endef'");
475 continue;
477 else if (word1eq ("define", 6))
479 if (ignoring)
480 in_ignored_define = 1;
481 else
483 p2 = next_token (p + 6);
484 /* Let the variable name be the whole rest of the line,
485 with trailing blanks stripped (comments have already been
486 removed), so it could be a complex variable/function
487 reference that might contain blanks. */
488 p = index (p2, '\0');
489 while (isblank (p[-1]))
490 --p;
491 lineno = do_define (p2, p - p2, o_file,
492 lineno, infile, filename);
494 continue;
496 else if (word1eq ("override", 8))
498 p2 = next_token (p + 8);
499 if (p2 == 0)
500 makefile_error (filename, lineno, "empty `override' directive");
501 if (!strncmp (p2, "define", 6) && (isblank (p2[6]) || p2[6] == '\0'))
503 if (ignoring)
504 in_ignored_define = 1;
505 else
507 p2 = next_token (p2 + 6);
508 /* Let the variable name be the whole rest of the line,
509 with trailing blanks stripped (comments have already been
510 removed), so it could be a complex variable/function
511 reference that might contain blanks. */
512 p = index (p2, '\0');
513 while (isblank (p[-1]))
514 --p;
515 lineno = do_define (p2, p - p2, o_override,
516 lineno, infile, filename);
519 else if (!ignoring
520 && !try_variable_definition (filename, lineno,
521 p2, o_override))
522 makefile_error (filename, lineno, "empty `override' directive");
524 continue;
527 if (ignoring)
528 /* Ignore the line. We continue here so conditionals
529 can appear in the middle of a rule. */
530 continue;
531 else if (word1eq ("export", 6))
533 struct variable *v;
534 p2 = next_token (p + 6);
535 if (*p2 == '\0')
536 export_all_variables = 1;
537 v = try_variable_definition (filename, lineno, p2, o_file);
538 if (v != 0)
539 v->export = v_export;
540 else
542 unsigned int len;
543 for (p = find_next_token (&p2, &len); p != 0;
544 p = find_next_token (&p2, &len))
546 v = lookup_variable (p, len);
547 if (v == 0)
548 v = define_variable (p, len, "", o_file, 0);
549 v->export = v_export;
553 else if (word1eq ("unexport", 8))
555 unsigned int len;
556 struct variable *v;
557 p2 = next_token (p + 8);
558 if (*p2 == '\0')
559 export_all_variables = 0;
560 for (p = find_next_token (&p2, &len); p != 0;
561 p = find_next_token (&p2, &len))
563 v = lookup_variable (p, len);
564 if (v == 0)
565 v = define_variable (p, len, "", o_file, 0);
566 v->export = v_noexport;
569 else if (word1eq ("include", 7) || word1eq ("-include", 8)
570 || word1eq ("sinclude", 8))
572 /* We have found an `include' line specifying a nested
573 makefile to be read at this point. */
574 struct conditionals *save, new_conditionals;
575 struct nameseq *files;
576 /* "-include" (vs "include") says no error if the file does not
577 exist. "sinclude" is an alias for this from SGI. */
578 int noerror = p[0] != 'i';
580 p = allocated_variable_expand (next_token (p + (noerror ? 9 : 8)));
581 if (*p == '\0')
583 makefile_error (filename, lineno,
584 "no file name for `%sinclude'",
585 noerror ? "-" : "");
586 continue;
589 /* Parse the list of file names. */
590 p2 = p;
591 files = multi_glob (parse_file_seq (&p2, '\0',
592 sizeof (struct nameseq),
594 sizeof (struct nameseq));
595 free (p);
597 /* Save the state of conditionals and start
598 the included makefile with a clean slate. */
599 save = conditionals;
600 bzero ((char *) &new_conditionals, sizeof new_conditionals);
601 conditionals = &new_conditionals;
603 /* Record the rules that are waiting so they will determine
604 the default goal before those in the included makefile. */
605 record_waiting_files ();
607 /* Read each included makefile. */
608 while (files != 0)
610 struct nameseq *next = files->next;
611 char *name = files->name;
612 free ((char *)files);
613 files = next;
615 if (! read_makefile (name, (RM_INCLUDED | RM_NO_TILDE
616 | (noerror ? RM_DONTCARE : 0)))
617 && ! noerror)
618 makefile_error (filename, lineno,
619 "%s: %s", name, strerror (errno));
622 /* Free any space allocated by conditional_line. */
623 if (conditionals->ignoring)
624 free (conditionals->ignoring);
625 if (conditionals->seen_else)
626 free (conditionals->seen_else);
628 /* Restore state. */
629 conditionals = save;
630 reading_filename = filename;
631 reading_lineno_ptr = &lineno;
633 else if (word1eq ("vpath", 5))
635 char *pattern;
636 unsigned int len;
637 p2 = variable_expand (p + 5);
638 p = find_next_token (&p2, &len);
639 if (p != 0)
641 pattern = savestring (p, len);
642 p = find_next_token (&p2, &len);
643 /* No searchpath means remove all previous
644 selective VPATH's with the same pattern. */
646 else
647 /* No pattern means remove all previous selective VPATH's. */
648 pattern = 0;
649 construct_vpath_list (pattern, p);
650 if (pattern != 0)
651 free (pattern);
653 #undef word1eq
654 else if (try_variable_definition (filename, lineno, p, o_file))
655 /* This line has been dealt with. */
657 else if (lb.buffer[0] == '\t')
659 p = collapsed; /* Ignore comments. */
660 while (isblank (*p))
661 ++p;
662 if (*p == '\0')
663 /* The line is completely blank; that is harmless. */
664 continue;
665 /* This line starts with a tab but was not caught above
666 because there was no preceding target, and the line
667 might have been usable as a variable definition.
668 But now it is definitely lossage. */
669 makefile_fatal (filename, lineno,
670 "commands commence before first target");
672 else
674 /* This line describes some target files. */
676 char *cmdleft;
678 /* Record the previous rule. */
680 record_waiting_files ();
682 /* Search the line for an unquoted ; that is not after an
683 unquoted #. */
684 cmdleft = find_char_unquote (lb.buffer, ";#", 0);
685 if (cmdleft != 0 && *cmdleft == '#')
687 /* We found a comment before a semicolon. */
688 *cmdleft = '\0';
689 cmdleft = 0;
691 else if (cmdleft != 0)
692 /* Found one. Cut the line short there before expanding it. */
693 *cmdleft = '\0';
695 collapse_continuations (lb.buffer);
697 /* Expand variable and function references before doing anything
698 else so that special characters can be inside variables. */
699 p = variable_expand (lb.buffer);
701 if (cmdleft == 0)
702 /* Look for a semicolon in the expanded line. */
703 cmdleft = find_char_unquote (p, ";", 0);
705 if (cmdleft != 0)
706 /* Cut the line short at the semicolon. */
707 *cmdleft = '\0';
709 p2 = next_token (p);
710 if (*p2 == '\0')
712 if (cmdleft != 0)
713 makefile_fatal (filename, lineno,
714 "missing rule before commands");
715 else
716 /* This line contained a variable reference that
717 expanded to nothing but whitespace. */
718 continue;
720 else if (*p2 == ':')
722 /* We accept and ignore rules without targets for
723 compatibility with SunOS 4 make. */
724 no_targets = 1;
725 continue;
728 filenames = multi_glob (parse_file_seq (&p2, ':',
729 sizeof (struct nameseq),
731 sizeof (struct nameseq));
732 if (*p2++ == '\0')
733 makefile_fatal (filename, lineno, "missing separator");
734 /* Is this a one-colon or two-colon entry? */
735 two_colon = *p2 == ':';
736 if (two_colon)
737 p2++;
739 /* We have some targets, so don't ignore the following commands. */
740 no_targets = 0;
742 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
743 p = index (p2, ':');
744 while (p != 0 && p[-1] == '\\')
746 register char *q = &p[-1];
747 register int backslash = 0;
748 while (*q-- == '\\')
749 backslash = !backslash;
750 if (backslash)
751 p = index (p + 1, ':');
752 else
753 break;
755 #ifdef __MSDOS__
756 /* For MS-DOS, skip a "C:\...". */
757 if (p != 0 && p[1] == '\\' && isalpha (p[-1]))
758 p = 0;
759 #endif
760 #ifdef _AMIGA
761 /* Here, the situation is quite complicated. Let's have a look
762 at a couple of targets:
764 install: dev:make
766 dev:make: make
768 dev:make:: xyz
770 The rule is that it's only a target, if there are TWO :'s
771 OR a space around the :.
773 if (p && !(isspace(p[1]) || !p[1] || isspace(p[-1])))
774 p = 0;
775 #endif
776 if (p != 0)
778 struct nameseq *target;
779 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
780 ++p2;
781 if (target == 0)
782 makefile_fatal (filename, lineno, "missing target pattern");
783 else if (target->next != 0)
784 makefile_fatal (filename, lineno, "multiple target patterns");
785 pattern = target->name;
786 pattern_percent = find_percent (pattern);
787 if (pattern_percent == 0)
788 makefile_fatal (filename, lineno,
789 "target pattern contains no `%%'");
791 else
792 pattern = 0;
794 /* Parse the dependencies. */
795 deps = (struct dep *)
796 multi_glob (parse_file_seq (&p2, '\0', sizeof (struct dep), 1),
797 sizeof (struct dep));
799 commands_idx = 0;
800 if (cmdleft != 0)
802 /* Semicolon means rest of line is a command. */
803 unsigned int len = strlen (cmdleft + 1);
805 commands_started = lineno;
807 /* Add this command line to the buffer. */
808 if (len + 2 > commands_len)
810 commands_len = (len + 2) * 2;
811 commands = (char *) xrealloc (commands, commands_len);
813 bcopy (cmdleft + 1, commands, len);
814 commands_idx += len;
815 commands[commands_idx++] = '\n';
818 continue;
821 /* We get here except in the case that we just read a rule line.
822 Record now the last rule we read, so following spurious
823 commands are properly diagnosed. */
824 record_waiting_files ();
825 no_targets = 0;
828 if (conditionals->if_cmds)
829 makefile_fatal (filename, lineno, "missing `endif'");
831 /* At eof, record the last rule. */
832 record_waiting_files ();
834 freebuffer (&lb);
835 free ((char *) commands);
836 fclose (infile);
838 reading_filename = 0;
839 reading_lineno_ptr = 0;
841 return 1;
844 /* Execute a `define' directive.
845 The first line has already been read, and NAME is the name of
846 the variable to be defined. The following lines remain to be read.
847 LINENO, INFILE and FILENAME refer to the makefile being read.
848 The value returned is LINENO, updated for lines read here. */
850 static unsigned int
851 do_define (name, namelen, origin, lineno, infile, filename)
852 char *name;
853 unsigned int namelen;
854 enum variable_origin origin;
855 unsigned int lineno;
856 FILE *infile;
857 char *filename;
859 struct linebuffer lb;
860 unsigned int nlines = 0;
861 unsigned int length = 100;
862 char *definition = (char *) xmalloc (100);
863 register unsigned int idx = 0;
864 register char *p;
866 /* Expand the variable name. */
867 char *var = (char *) alloca (namelen + 1);
868 bcopy (name, var, namelen);
869 var[namelen] = '\0';
870 var = variable_expand (var);
872 initbuffer (&lb);
873 while (!feof (infile))
875 lineno += nlines;
876 nlines = readline (&lb, infile, filename, lineno);
878 collapse_continuations (lb.buffer);
880 p = next_token (lb.buffer);
881 if ((p[5] == '\0' || isblank (p[5])) && !strncmp (p, "endef", 5))
883 p += 5;
884 remove_comments (p);
885 if (*next_token (p) != '\0')
886 makefile_error (filename, lineno,
887 "Extraneous text after `endef' directive");
888 /* Define the variable. */
889 if (idx == 0)
890 definition[0] = '\0';
891 else
892 definition[idx - 1] = '\0';
893 (void) define_variable (var, strlen (var), definition, origin, 1);
894 free (definition);
895 freebuffer (&lb);
896 return lineno;
898 else
900 unsigned int len = strlen (lb.buffer);
902 /* Increase the buffer size if necessary. */
903 if (idx + len + 1 > length)
905 length = (idx + len) * 2;
906 definition = (char *) xrealloc (definition, length + 1);
909 bcopy (lb.buffer, &definition[idx], len);
910 idx += len;
911 /* Separate lines with a newline. */
912 definition[idx++] = '\n';
916 /* No `endef'!! */
917 makefile_fatal (filename, lineno, "missing `endef', unterminated `define'");
919 /* NOTREACHED */
920 return 0;
923 /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
924 "ifneq", "else" and "endif".
925 LINE is the input line, with the command as its first word.
927 FILENAME and LINENO are the filename and line number in the
928 current makefile. They are used for error messages.
930 Value is -1 if the line is invalid,
931 0 if following text should be interpreted,
932 1 if following text should be ignored. */
934 static int
935 conditional_line (line, filename, lineno)
936 char *line;
937 char *filename;
938 unsigned int lineno;
940 int notdef;
941 char *cmdname;
942 register unsigned int i;
944 if (*line == 'i')
946 /* It's an "if..." command. */
947 notdef = line[2] == 'n';
948 if (notdef)
950 cmdname = line[3] == 'd' ? "ifndef" : "ifneq";
951 line += cmdname[3] == 'd' ? 7 : 6;
953 else
955 cmdname = line[2] == 'd' ? "ifdef" : "ifeq";
956 line += cmdname[2] == 'd' ? 6 : 5;
959 else
961 /* It's an "else" or "endif" command. */
962 notdef = line[1] == 'n';
963 cmdname = notdef ? "endif" : "else";
964 line += notdef ? 5 : 4;
967 line = next_token (line);
969 if (*cmdname == 'e')
971 if (*line != '\0')
972 makefile_error (filename, lineno,
973 "Extraneous text after `%s' directive",
974 cmdname);
975 /* "Else" or "endif". */
976 if (conditionals->if_cmds == 0)
977 makefile_fatal (filename, lineno, "extraneous `%s'", cmdname);
978 /* NOTDEF indicates an `endif' command. */
979 if (notdef)
980 --conditionals->if_cmds;
981 else if (conditionals->seen_else[conditionals->if_cmds - 1])
982 makefile_fatal (filename, lineno, "only one `else' per conditional");
983 else
985 /* Toggle the state of ignorance. */
986 conditionals->ignoring[conditionals->if_cmds - 1]
987 = !conditionals->ignoring[conditionals->if_cmds - 1];
988 /* Record that we have seen an `else' in this conditional.
989 A second `else' will be erroneous. */
990 conditionals->seen_else[conditionals->if_cmds - 1] = 1;
992 for (i = 0; i < conditionals->if_cmds; ++i)
993 if (conditionals->ignoring[i])
994 return 1;
995 return 0;
998 if (conditionals->allocated == 0)
1000 conditionals->allocated = 5;
1001 conditionals->ignoring = (char *) xmalloc (conditionals->allocated);
1002 conditionals->seen_else = (char *) xmalloc (conditionals->allocated);
1005 ++conditionals->if_cmds;
1006 if (conditionals->if_cmds > conditionals->allocated)
1008 conditionals->allocated += 5;
1009 conditionals->ignoring = (char *)
1010 xrealloc (conditionals->ignoring, conditionals->allocated);
1011 conditionals->seen_else = (char *)
1012 xrealloc (conditionals->seen_else, conditionals->allocated);
1015 /* Record that we have seen an `if...' but no `else' so far. */
1016 conditionals->seen_else[conditionals->if_cmds - 1] = 0;
1018 /* Search through the stack to see if we're already ignoring. */
1019 for (i = 0; i < conditionals->if_cmds - 1; ++i)
1020 if (conditionals->ignoring[i])
1022 /* We are already ignoring, so just push a level
1023 to match the next "else" or "endif", and keep ignoring.
1024 We don't want to expand variables in the condition. */
1025 conditionals->ignoring[conditionals->if_cmds - 1] = 1;
1026 return 1;
1029 if (cmdname[notdef ? 3 : 2] == 'd')
1031 /* "Ifdef" or "ifndef". */
1032 struct variable *v;
1033 register char *p = end_of_token (line);
1034 i = p - line;
1035 p = next_token (p);
1036 if (*p != '\0')
1037 return -1;
1038 v = lookup_variable (line, i);
1039 conditionals->ignoring[conditionals->if_cmds - 1]
1040 = (v != 0 && *v->value != '\0') == notdef;
1042 else
1044 /* "Ifeq" or "ifneq". */
1045 char *s1, *s2;
1046 unsigned int len;
1047 char termin = *line == '(' ? ',' : *line;
1049 if (termin != ',' && termin != '"' && termin != '\'')
1050 return -1;
1052 s1 = ++line;
1053 /* Find the end of the first string. */
1054 if (termin == ',')
1056 register int count = 0;
1057 for (; *line != '\0'; ++line)
1058 if (*line == '(')
1059 ++count;
1060 else if (*line == ')')
1061 --count;
1062 else if (*line == ',' && count <= 0)
1063 break;
1065 else
1066 while (*line != '\0' && *line != termin)
1067 ++line;
1069 if (*line == '\0')
1070 return -1;
1072 *line++ = '\0';
1074 s2 = variable_expand (s1);
1075 /* We must allocate a new copy of the expanded string because
1076 variable_expand re-uses the same buffer. */
1077 len = strlen (s2);
1078 s1 = (char *) alloca (len + 1);
1079 bcopy (s2, s1, len + 1);
1081 if (termin != ',')
1082 /* Find the start of the second string. */
1083 line = next_token (line);
1085 termin = termin == ',' ? ')' : *line;
1086 if (termin != ')' && termin != '"' && termin != '\'')
1087 return -1;
1089 /* Find the end of the second string. */
1090 if (termin == ')')
1092 register int count = 0;
1093 s2 = next_token (line);
1094 for (line = s2; *line != '\0'; ++line)
1096 if (*line == '(')
1097 ++count;
1098 else if (*line == ')')
1099 if (count <= 0)
1100 break;
1101 else
1102 --count;
1105 else
1107 ++line;
1108 s2 = line;
1109 while (*line != '\0' && *line != termin)
1110 ++line;
1113 if (*line == '\0')
1114 return -1;
1116 *line = '\0';
1117 line = next_token (++line);
1118 if (*line != '\0')
1119 makefile_error (filename, lineno,
1120 "Extraneous text after `%s' directive",
1121 cmdname);
1123 s2 = variable_expand (s2);
1124 conditionals->ignoring[conditionals->if_cmds - 1]
1125 = streq (s1, s2) == notdef;
1128 /* Search through the stack to see if we're ignoring. */
1129 for (i = 0; i < conditionals->if_cmds; ++i)
1130 if (conditionals->ignoring[i])
1131 return 1;
1132 return 0;
1135 /* Remove duplicate dependencies in CHAIN. */
1137 void
1138 uniquize_deps (chain)
1139 struct dep *chain;
1141 register struct dep *d;
1143 /* Make sure that no dependencies are repeated. This does not
1144 really matter for the purpose of updating targets, but it
1145 might make some names be listed twice for $^ and $?. */
1147 for (d = chain; d != 0; d = d->next)
1149 struct dep *last, *next;
1151 last = d;
1152 next = d->next;
1153 while (next != 0)
1154 if (streq (dep_name (d), dep_name (next)))
1156 struct dep *n = next->next;
1157 last->next = n;
1158 if (next->name != 0 && next->name != d->name)
1159 free (next->name);
1160 if (next != d)
1161 free ((char *) next);
1162 next = n;
1164 else
1166 last = next;
1167 next = next->next;
1172 /* Record a description line for files FILENAMES,
1173 with dependencies DEPS, commands to execute described
1174 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1175 TWO_COLON is nonzero if a double colon was used.
1176 If not nil, PATTERN is the `%' pattern to make this
1177 a static pattern rule, and PATTERN_PERCENT is a pointer
1178 to the `%' within it.
1180 The links of FILENAMES are freed, and so are any names in it
1181 that are not incorporated into other data structures. */
1183 static void
1184 record_files (filenames, pattern, pattern_percent, deps, commands_started,
1185 commands, commands_idx, two_colon, filename, lineno, set_default)
1186 struct nameseq *filenames;
1187 char *pattern, *pattern_percent;
1188 struct dep *deps;
1189 unsigned int commands_started;
1190 char *commands;
1191 unsigned int commands_idx;
1192 int two_colon;
1193 char *filename;
1194 unsigned int lineno;
1195 int set_default;
1197 struct nameseq *nextf;
1198 int implicit = 0;
1199 unsigned int max_targets, target_idx;
1200 char **targets = 0, **target_percents = 0;
1201 struct commands *cmds;
1203 if (commands_idx > 0)
1205 cmds = (struct commands *) xmalloc (sizeof (struct commands));
1206 cmds->filename = filename;
1207 cmds->lineno = commands_started;
1208 cmds->commands = savestring (commands, commands_idx);
1209 cmds->command_lines = 0;
1211 else
1212 cmds = 0;
1214 for (; filenames != 0; filenames = nextf)
1216 register char *name = filenames->name;
1217 register struct file *f;
1218 register struct dep *d;
1219 struct dep *this;
1220 char *implicit_percent;
1222 nextf = filenames->next;
1223 free ((char *) filenames);
1225 implicit_percent = find_percent (name);
1226 implicit |= implicit_percent != 0;
1228 if (implicit && pattern != 0)
1229 makefile_fatal (filename, lineno,
1230 "mixed implicit and static pattern rules");
1232 if (implicit && implicit_percent == 0)
1233 makefile_fatal (filename, lineno, "mixed implicit and normal rules");
1235 if (implicit)
1237 if (targets == 0)
1239 max_targets = 5;
1240 targets = (char **) xmalloc (5 * sizeof (char *));
1241 target_percents = (char **) xmalloc (5 * sizeof (char *));
1242 target_idx = 0;
1244 else if (target_idx == max_targets - 1)
1246 max_targets += 5;
1247 targets = (char **) xrealloc ((char *) targets,
1248 max_targets * sizeof (char *));
1249 target_percents
1250 = (char **) xrealloc ((char *) target_percents,
1251 max_targets * sizeof (char *));
1253 targets[target_idx] = name;
1254 target_percents[target_idx] = implicit_percent;
1255 ++target_idx;
1256 continue;
1259 /* If there are multiple filenames, copy the chain DEPS
1260 for all but the last one. It is not safe for the same deps
1261 to go in more than one place in the data base. */
1262 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1264 if (pattern != 0)
1265 /* If this is an extended static rule:
1266 `targets: target%pattern: dep%pattern; cmds',
1267 translate each dependency pattern into a plain filename
1268 using the target pattern and this target's name. */
1269 if (!pattern_matches (pattern, pattern_percent, name))
1271 /* Give a warning if the rule is meaningless. */
1272 makefile_error (filename, lineno,
1273 "target `%s' doesn't match the target pattern",
1274 name);
1275 this = 0;
1277 else
1279 /* We use patsubst_expand to do the work of translating
1280 the target pattern, the target's name and the dependencies'
1281 patterns into plain dependency names. */
1282 char *buffer = variable_expand ("");
1284 for (d = this; d != 0; d = d->next)
1286 char *o;
1287 char *percent = find_percent (d->name);
1288 if (percent == 0)
1289 continue;
1290 o = patsubst_expand (buffer, name, pattern, d->name,
1291 pattern_percent, percent);
1292 free (d->name);
1293 d->name = savestring (buffer, o - buffer);
1297 if (!two_colon)
1299 /* Single-colon. Combine these dependencies
1300 with others in file's existing record, if any. */
1301 f = enter_file (name);
1303 if (f->double_colon)
1304 makefile_fatal (filename, lineno,
1305 "target file `%s' has both : and :: entries",
1306 f->name);
1308 /* If CMDS == F->CMDS, this target was listed in this rule
1309 more than once. Just give a warning since this is harmless. */
1310 if (cmds != 0 && cmds == f->cmds)
1311 makefile_error
1312 (filename, lineno,
1313 "target `%s' given more than once in the same rule.",
1314 f->name);
1316 /* Check for two single-colon entries both with commands.
1317 Check is_target so that we don't lose on files such as .c.o
1318 whose commands were preinitialized. */
1319 else if (cmds != 0 && f->cmds != 0 && f->is_target)
1321 makefile_error (cmds->filename, cmds->lineno,
1322 "warning: overriding commands for target `%s'",
1323 f->name);
1324 makefile_error (f->cmds->filename, f->cmds->lineno,
1325 "warning: ignoring old commands for target `%s'",
1326 f->name);
1329 f->is_target = 1;
1331 /* Defining .DEFAULT with no deps or cmds clears it. */
1332 if (f == default_file && this == 0 && cmds == 0)
1333 f->cmds = 0;
1334 if (cmds != 0)
1335 f->cmds = cmds;
1336 /* Defining .SUFFIXES with no dependencies
1337 clears out the list of suffixes. */
1338 if (f == suffix_file && this == 0)
1340 d = f->deps;
1341 while (d != 0)
1343 struct dep *nextd = d->next;
1344 free (d->name);
1345 free ((char *)d);
1346 d = nextd;
1348 f->deps = 0;
1350 else if (f->deps != 0)
1352 /* Add the file's old deps and the new ones in THIS together. */
1354 struct dep *firstdeps, *moredeps;
1355 if (cmds != 0)
1357 /* This is the rule with commands, so put its deps first.
1358 The rationale behind this is that $< expands to the
1359 first dep in the chain, and commands use $< expecting
1360 to get the dep that rule specifies. */
1361 firstdeps = this;
1362 moredeps = f->deps;
1364 else
1366 /* Append the new deps to the old ones. */
1367 firstdeps = f->deps;
1368 moredeps = this;
1371 if (firstdeps == 0)
1372 firstdeps = moredeps;
1373 else
1375 d = firstdeps;
1376 while (d->next != 0)
1377 d = d->next;
1378 d->next = moredeps;
1381 f->deps = firstdeps;
1383 else
1384 f->deps = this;
1386 /* If this is a static pattern rule, set the file's stem to
1387 the part of its name that matched the `%' in the pattern,
1388 so you can use $* in the commands. */
1389 if (pattern != 0)
1391 static char *percent = "%";
1392 char *buffer = variable_expand ("");
1393 char *o = patsubst_expand (buffer, name, pattern, percent,
1394 pattern_percent, percent);
1395 f->stem = savestring (buffer, o - buffer);
1398 else
1400 /* Double-colon. Make a new record
1401 even if the file already has one. */
1402 f = lookup_file (name);
1403 /* Check for both : and :: rules. Check is_target so
1404 we don't lose on default suffix rules or makefiles. */
1405 if (f != 0 && f->is_target && !f->double_colon)
1406 makefile_fatal (filename, lineno,
1407 "target file `%s' has both : and :: entries",
1408 f->name);
1409 f = enter_file (name);
1410 /* If there was an existing entry and it was a double-colon
1411 entry, enter_file will have returned a new one, making it the
1412 prev pointer of the old one, and setting its double_colon
1413 pointer to the first one. */
1414 if (f->double_colon == 0)
1415 /* This is the first entry for this name, so we must
1416 set its double_colon pointer to itself. */
1417 f->double_colon = f;
1418 f->is_target = 1;
1419 f->deps = this;
1420 f->cmds = cmds;
1423 /* Free name if not needed further. */
1424 if (f != 0 && name != f->name
1425 && (name < f->name || name > f->name + strlen (f->name)))
1427 free (name);
1428 name = f->name;
1431 /* See if this is first target seen whose name does
1432 not start with a `.', unless it contains a slash. */
1433 if (default_goal_file == 0 && set_default
1434 && (*name != '.' || index (name, '/') != 0))
1436 int reject = 0;
1438 /* If this file is a suffix, don't
1439 let it be the default goal file. */
1441 for (d = suffix_file->deps; d != 0; d = d->next)
1443 register struct dep *d2;
1444 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1446 reject = 1;
1447 break;
1449 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1451 register unsigned int len = strlen (dep_name (d2));
1452 if (strncmp (name, dep_name (d2), len))
1453 continue;
1454 if (streq (name + len, dep_name (d)))
1456 reject = 1;
1457 break;
1460 if (reject)
1461 break;
1464 if (!reject)
1465 default_goal_file = f;
1469 if (implicit)
1471 targets[target_idx] = 0;
1472 target_percents[target_idx] = 0;
1473 create_pattern_rule (targets, target_percents, two_colon, deps, cmds, 1);
1474 free ((char *) target_percents);
1478 /* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
1479 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
1480 Quoting backslashes are removed from STRING by compacting it into
1481 itself. Returns a pointer to the first unquoted STOPCHAR if there is
1482 one, or nil if there are none. */
1484 char *
1485 find_char_unquote (string, stopchars, blank)
1486 char *string;
1487 char *stopchars;
1488 int blank;
1490 unsigned int string_len = strlen (string);
1491 register char *p = string;
1493 while (1)
1495 while (*p != '\0' && index (stopchars, *p) == 0
1496 && (!blank || !isblank (*p)))
1497 ++p;
1498 if (*p == '\0')
1499 break;
1501 if (p > string && p[-1] == '\\')
1503 /* Search for more backslashes. */
1504 register int i = -2;
1505 while (&p[i] >= string && p[i] == '\\')
1506 --i;
1507 ++i;
1508 /* The number of backslashes is now -I.
1509 Copy P over itself to swallow half of them. */
1510 bcopy (&p[i / 2], &p[i], (string_len - (p - string)) - (i / 2) + 1);
1511 p += i / 2;
1512 if (i % 2 == 0)
1513 /* All the backslashes quoted each other; the STOPCHAR was
1514 unquoted. */
1515 return p;
1517 /* The STOPCHAR was quoted by a backslash. Look for another. */
1519 else
1520 /* No backslash in sight. */
1521 return p;
1524 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
1525 return 0;
1528 /* Search PATTERN for an unquoted %. */
1530 char *
1531 find_percent (pattern)
1532 char *pattern;
1534 return find_char_unquote (pattern, "%", 0);
1537 /* Parse a string into a sequence of filenames represented as a
1538 chain of struct nameseq's in reverse order and return that chain.
1540 The string is passed as STRINGP, the address of a string pointer.
1541 The string pointer is updated to point at the first character
1542 not parsed, which either is a null char or equals STOPCHAR.
1544 SIZE is how big to construct chain elements.
1545 This is useful if we want them actually to be other structures
1546 that have room for additional info.
1548 If STRIP is nonzero, strip `./'s off the beginning. */
1550 struct nameseq *
1551 parse_file_seq (stringp, stopchar, size, strip)
1552 char **stringp;
1553 int stopchar;
1554 unsigned int size;
1555 int strip;
1557 register struct nameseq *new = 0;
1558 register struct nameseq *new1, *lastnew1;
1559 register char *p = *stringp;
1560 char *q;
1561 char *name;
1562 char stopchars[3];
1564 #ifdef VMS
1565 stopchars[0] = ',';
1566 stopchars[1] = stopchar;
1567 stopchars[2] = '\0';
1568 #else
1569 stopchars[0] = stopchar;
1570 stopchars[1] = '\0';
1571 #endif
1573 while (1)
1575 /* Skip whitespace; see if any more names are left. */
1576 p = next_token (p);
1577 if (*p == '\0')
1578 break;
1579 if (*p == stopchar)
1580 break;
1582 /* Yes, find end of next name. */
1583 q = p;
1584 p = find_char_unquote (q, stopchars, 1);
1585 #ifdef VMS
1586 /* convert comma separated list to space separated */
1587 if (p && *p == ',')
1588 *p =' ';
1589 #endif
1590 #ifdef __MSDOS__
1591 /* For MS-DOS, skip a "C:\...". */
1592 if (stopchar == ':' && p != 0 && p[1] == '\\' && isalpha (p[-1]))
1593 p = 0;
1594 #endif
1595 #ifdef _AMIGA
1596 if (stopchar == ':' && p && *p == ':' &&
1597 !(isspace(p[1]) || !p[1] || isspace(p[-1])))
1599 p = find_char_unquote (p+1, stopchars, 1);
1601 #endif
1602 if (p == 0)
1603 p = q + strlen (q);
1605 if (strip)
1606 #ifdef VMS
1607 /* Skip leading `[]'s. */
1608 while (p - q > 2 && q[0] == '[' && q[1] == ']')
1609 #else
1610 /* Skip leading `./'s. */
1611 while (p - q > 2 && q[0] == '.' && q[1] == '/')
1612 #endif
1614 q += 2; /* Skip "./". */
1615 while (q < p && *q == '/')
1616 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
1617 ++q;
1620 /* Extract the filename just found, and skip it. */
1622 if (q == p)
1623 /* ".///" was stripped to "". */
1624 #ifdef VMS
1625 continue;
1626 #else
1627 #ifdef _AMIGA
1628 name = savestring ("", 0);
1629 #else
1630 name = savestring ("./", 2);
1631 #endif
1632 #endif
1633 else
1634 #ifdef VMS
1635 /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
1636 * to remove this '\' before we can use the filename.
1637 * Savestring called because q may be read-only string constant.
1640 char *qbase = savestring(q, strlen(q));
1641 char *pbase = qbase + (p-q);
1642 char *q1 = qbase;
1643 char *q2 = q1;
1644 char *p1 = pbase;
1646 while (q1 != pbase)
1648 if (*q1 == '\\' && *(q1+1) == ':')
1650 q1++;
1651 p1--;
1653 *q2++ = *q1++;
1655 name = savestring (qbase, p1 - qbase);
1656 free (qbase);
1658 #else
1659 name = savestring (q, p - q);
1660 #endif
1662 /* Add it to the front of the chain. */
1663 new1 = (struct nameseq *) xmalloc (size);
1664 new1->name = name;
1665 new1->next = new;
1666 new = new1;
1669 #ifndef NO_ARCHIVES
1671 /* Look for multi-word archive references.
1672 They are indicated by a elt ending with an unmatched `)' and
1673 an elt further down the chain (i.e., previous in the file list)
1674 with an unmatched `(' (e.g., "lib(mem"). */
1676 new1 = new;
1677 lastnew1 = 0;
1678 while (new1 != 0)
1679 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
1680 && new1->name[strlen (new1->name) - 1] == ')'
1681 && index (new1->name, '(') == 0)
1683 /* NEW1 ends with a `)' but does not contain a `('.
1684 Look back for an elt with an opening `(' but no closing `)'. */
1686 struct nameseq *n = new1->next, *lastn = new1;
1687 char *paren;
1688 while (n != 0 && (paren = index (n->name, '(')) == 0)
1690 lastn = n;
1691 n = n->next;
1693 if (n != 0
1694 /* Ignore something starting with `(', as that cannot actually
1695 be an archive-member reference (and treating it as such
1696 results in an empty file name, which causes much lossage). */
1697 && n->name[0] != '(')
1699 /* N is the first element in the archive group.
1700 Its name looks like "lib(mem" (with no closing `)'). */
1702 char *libname;
1704 /* Copy "lib(" into LIBNAME. */
1705 ++paren;
1706 libname = (char *) alloca (paren - n->name + 1);
1707 bcopy (n->name, libname, paren - n->name);
1708 libname[paren - n->name] = '\0';
1710 if (*paren == '\0')
1712 /* N was just "lib(", part of something like "lib( a b)".
1713 Edit it out of the chain and free its storage. */
1714 lastn->next = n->next;
1715 free (n->name);
1716 free ((char *) n);
1717 /* LASTN->next is the new stopping elt for the loop below. */
1718 n = lastn->next;
1720 else
1722 /* Replace N's name with the full archive reference. */
1723 name = concat (libname, paren, ")");
1724 free (n->name);
1725 n->name = name;
1728 if (new1->name[1] == '\0')
1730 /* NEW1 is just ")", part of something like "lib(a b )".
1731 Omit it from the chain and free its storage. */
1732 if (lastnew1 == 0)
1733 new = new1->next;
1734 else
1735 lastnew1->next = new1->next;
1736 lastn = new1;
1737 new1 = new1->next;
1738 free (lastn->name);
1739 free ((char *) lastn);
1741 else
1743 /* Replace also NEW1->name, which already has closing `)'. */
1744 name = concat (libname, new1->name, "");
1745 free (new1->name);
1746 new1->name = name;
1747 new1 = new1->next;
1750 /* Trace back from NEW1 (the end of the list) until N
1751 (the beginning of the list), rewriting each name
1752 with the full archive reference. */
1754 while (new1 != n)
1756 name = concat (libname, new1->name, ")");
1757 free (new1->name);
1758 new1->name = name;
1759 lastnew1 = new1;
1760 new1 = new1->next;
1763 else
1765 /* No frobnication happening. Just step down the list. */
1766 lastnew1 = new1;
1767 new1 = new1->next;
1770 else
1772 lastnew1 = new1;
1773 new1 = new1->next;
1776 #endif
1778 *stringp = p;
1779 return new;
1782 /* Read a line of text from STREAM into LINEBUFFER.
1783 Combine continuation lines into one line.
1784 Return the number of actual lines read (> 1 if hacked continuation lines).
1787 static unsigned int
1788 readline (linebuffer, stream, filename, lineno)
1789 struct linebuffer *linebuffer;
1790 FILE *stream;
1791 char *filename;
1792 unsigned int lineno;
1794 char *buffer = linebuffer->buffer;
1795 register char *p = linebuffer->buffer;
1796 register char *end = p + linebuffer->size;
1797 register int len, lastlen = 0;
1798 register char *p2;
1799 register unsigned int nlines = 0;
1800 register int backslash;
1802 *p = '\0';
1804 while (fgets (p, end - p, stream) != 0)
1806 len = strlen (p);
1807 if (len == 0)
1809 /* This only happens when the first thing on the line is a '\0'.
1810 It is a pretty hopeless case, but (wonder of wonders) Athena
1811 lossage strikes again! (xmkmf puts NULs in its makefiles.)
1812 There is nothing really to be done; we synthesize a newline so
1813 the following line doesn't appear to be part of this line. */
1814 makefile_error (filename, lineno,
1815 "warning: NUL character seen; rest of line ignored");
1816 p[0] = '\n';
1817 len = 1;
1820 p += len;
1821 if (p[-1] != '\n')
1823 /* Probably ran out of buffer space. */
1824 register unsigned int p_off = p - buffer;
1825 linebuffer->size *= 2;
1826 buffer = (char *) xrealloc (buffer, linebuffer->size);
1827 p = buffer + p_off;
1828 end = buffer + linebuffer->size;
1829 linebuffer->buffer = buffer;
1830 *p = '\0';
1831 lastlen = len;
1832 continue;
1835 ++nlines;
1837 if (len == 1 && p > buffer)
1838 /* P is pointing at a newline and it's the beginning of
1839 the buffer returned by the last fgets call. However,
1840 it is not necessarily the beginning of a line if P is
1841 pointing past the beginning of the holding buffer.
1842 If the buffer was just enlarged (right before the newline),
1843 we must account for that, so we pretend that the two lines
1844 were one line. */
1845 len += lastlen;
1846 lastlen = len;
1847 backslash = 0;
1848 for (p2 = p - 2; --len > 0; --p2)
1850 if (*p2 == '\\')
1851 backslash = !backslash;
1852 else
1853 break;
1856 if (!backslash)
1858 p[-1] = '\0';
1859 break;
1862 if (end - p <= 1)
1864 /* Enlarge the buffer. */
1865 register unsigned int p_off = p - buffer;
1866 linebuffer->size *= 2;
1867 buffer = (char *) xrealloc (buffer, linebuffer->size);
1868 p = buffer + p_off;
1869 end = buffer + linebuffer->size;
1870 linebuffer->buffer = buffer;
1874 if (ferror (stream))
1875 pfatal_with_name (filename);
1877 return nlines;
1880 /* Construct the list of include directories
1881 from the arguments and the default list. */
1883 void
1884 construct_include_path (arg_dirs)
1885 char **arg_dirs;
1887 register unsigned int i;
1888 #ifdef VAXC /* just don't ask ... */
1889 stat_t stbuf;
1890 #else
1891 struct stat stbuf;
1892 #endif
1893 /* Table to hold the dirs. */
1895 register unsigned int defsize = (sizeof (default_include_directories)
1896 / sizeof (default_include_directories[0]));
1897 register unsigned int max = 5;
1898 register char **dirs = (char **) xmalloc ((5 + defsize) * sizeof (char *));
1899 register unsigned int idx = 0;
1901 /* First consider any dirs specified with -I switches.
1902 Ignore dirs that don't exist. */
1904 if (arg_dirs != 0)
1905 while (*arg_dirs != 0)
1907 char *dir = *arg_dirs++;
1909 if (dir[0] == '~')
1911 char *expanded = tilde_expand (dir);
1912 if (expanded != 0)
1913 dir = expanded;
1916 if (stat (dir, &stbuf) == 0 && S_ISDIR (stbuf.st_mode))
1918 if (idx == max - 1)
1920 max += 5;
1921 dirs = (char **)
1922 xrealloc ((char *) dirs, (max + defsize) * sizeof (char *));
1924 dirs[idx++] = dir;
1926 else if (dir != arg_dirs[-1])
1927 free (dir);
1930 /* Now add at the end the standard default dirs. */
1932 for (i = 0; default_include_directories[i] != 0; ++i)
1933 if (stat (default_include_directories[i], &stbuf) == 0
1934 && S_ISDIR (stbuf.st_mode))
1935 dirs[idx++] = default_include_directories[i];
1937 dirs[idx] = 0;
1939 /* Now compute the maximum length of any name in it. */
1941 max_incl_len = 0;
1942 for (i = 0; i < idx; ++i)
1944 unsigned int len = strlen (dirs[i]);
1945 /* If dir name is written with a trailing slash, discard it. */
1946 if (dirs[i][len - 1] == '/')
1947 /* We can't just clobber a null in because it may have come from
1948 a literal string and literal strings may not be writable. */
1949 dirs[i] = savestring (dirs[i], len - 1);
1950 if (len > max_incl_len)
1951 max_incl_len = len;
1954 include_directories = dirs;
1957 /* Expand ~ or ~USER at the beginning of NAME.
1958 Return a newly malloc'd string or 0. */
1960 char *
1961 tilde_expand (name)
1962 char *name;
1964 #ifndef VMS
1965 if (name[1] == '/' || name[1] == '\0')
1967 extern char *getenv ();
1968 char *home_dir;
1969 int is_variable;
1972 /* Turn off --warn-undefined-variables while we expand HOME. */
1973 int save = warn_undefined_variables_flag;
1974 warn_undefined_variables_flag = 0;
1976 home_dir = allocated_variable_expand ("$(HOME)");
1978 warn_undefined_variables_flag = save;
1981 is_variable = home_dir[0] != '\0';
1982 if (!is_variable)
1984 free (home_dir);
1985 home_dir = getenv ("HOME");
1987 #ifndef _AMIGA
1988 if (home_dir == 0 || home_dir[0] == '\0')
1990 extern char *getlogin ();
1991 char *name = getlogin ();
1992 home_dir = 0;
1993 if (name != 0)
1995 struct passwd *p = getpwnam (name);
1996 if (p != 0)
1997 home_dir = p->pw_dir;
2000 #endif /* !AMIGA */
2001 if (home_dir != 0)
2003 char *new = concat (home_dir, "", name + 1);
2004 if (is_variable)
2005 free (home_dir);
2006 return new;
2009 #ifndef _AMIGA
2010 else
2012 struct passwd *pwent;
2013 char *userend = index (name + 1, '/');
2014 if (userend != 0)
2015 *userend = '\0';
2016 pwent = getpwnam (name + 1);
2017 if (pwent != 0)
2019 if (userend == 0)
2020 return savestring (pwent->pw_dir, strlen (pwent->pw_dir));
2021 else
2022 return concat (pwent->pw_dir, "/", userend + 1);
2024 else if (userend != 0)
2025 *userend = '/';
2027 #endif /* !AMIGA */
2028 #endif /* !VMS */
2029 return 0;
2032 /* Given a chain of struct nameseq's describing a sequence of filenames,
2033 in reverse of the intended order, return a new chain describing the
2034 result of globbing the filenames. The new chain is in forward order.
2035 The links of the old chain are freed or used in the new chain.
2036 Likewise for the names in the old chain.
2038 SIZE is how big to construct chain elements.
2039 This is useful if we want them actually to be other structures
2040 that have room for additional info. */
2042 struct nameseq *
2043 multi_glob (chain, size)
2044 struct nameseq *chain;
2045 unsigned int size;
2047 extern void dir_setup_glob ();
2048 register struct nameseq *new = 0;
2049 register struct nameseq *old;
2050 struct nameseq *nexto;
2051 glob_t gl;
2053 dir_setup_glob (&gl);
2055 for (old = chain; old != 0; old = nexto)
2057 #ifndef NO_ARCHIVES
2058 char *memname;
2059 #endif
2061 nexto = old->next;
2063 if (old->name[0] == '~')
2065 char *newname = tilde_expand (old->name);
2066 if (newname != 0)
2068 free (old->name);
2069 old->name = newname;
2073 #ifndef NO_ARCHIVES
2074 if (ar_name (old->name))
2076 /* OLD->name is an archive member reference.
2077 Replace it with the archive file name,
2078 and save the member name in MEMNAME.
2079 We will glob on the archive name and then
2080 reattach MEMNAME later. */
2081 char *arname;
2082 ar_parse_name (old->name, &arname, &memname);
2083 free (old->name);
2084 old->name = arname;
2086 else
2087 memname = 0;
2088 #endif /* !NO_ARCHIVES */
2090 switch (glob (old->name, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
2092 case 0: /* Success. */
2094 register int i = gl.gl_pathc;
2095 while (i-- > 0)
2097 #ifndef NO_ARCHIVES
2098 if (memname != 0)
2100 /* Try to glob on MEMNAME within the archive. */
2101 struct nameseq *found
2102 = ar_glob (gl.gl_pathv[i], memname, size);
2103 if (found == 0)
2105 /* No matches. Use MEMNAME as-is. */
2106 struct nameseq *elt
2107 = (struct nameseq *) xmalloc (size);
2108 unsigned int alen = strlen (gl.gl_pathv[i]);
2109 unsigned int mlen = strlen (memname);
2110 elt->name = (char *) xmalloc (alen + 1 + mlen + 2);
2111 bcopy (gl.gl_pathv[i], elt->name, alen);
2112 elt->name[alen] = '(';
2113 bcopy (memname, &elt->name[alen + 1], mlen);
2114 elt->name[alen + 1 + mlen] = ')';
2115 elt->name[alen + 1 + mlen + 1] = '\0';
2116 elt->next = new;
2117 new = elt;
2119 else
2121 /* Find the end of the FOUND chain. */
2122 struct nameseq *f = found;
2123 while (f->next != 0)
2124 f = f->next;
2126 /* Attach the chain being built to the end of the FOUND
2127 chain, and make FOUND the new NEW chain. */
2128 f->next = new;
2129 new = found;
2132 free (memname);
2134 else
2135 #endif /* !NO_ARCHIVES */
2137 struct nameseq *elt = (struct nameseq *) xmalloc (size);
2138 elt->name = savestring (gl.gl_pathv[i],
2139 strlen (gl.gl_pathv[i]));
2140 elt->next = new;
2141 new = elt;
2144 globfree (&gl);
2145 free (old->name);
2146 free ((char *)old);
2147 break;
2150 case GLOB_NOSPACE:
2151 fatal ("virtual memory exhausted");
2152 break;
2154 default:
2155 old->next = new;
2156 new = old;
2157 break;
2161 return new;