source: trunk/src/kmk/read.c@ 1173

Last change on this file since 1173 was 1173, checked in by bird, 18 years ago

oops.

  • Property svn:eol-style set to native
File size: 104.2 KB
Line 
1/* Reading and parsing of makefiles for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006 Free Software
4Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 2, or (at your option) any later version.
10
11GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
12WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
13A PARTICULAR PURPOSE. See the GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License along with
16GNU Make; see the file COPYING. If not, write to the Free Software
17Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. */
18
19#include "make.h"
20
21#include <assert.h>
22
23#include <glob.h>
24
25#include "dep.h"
26#include "filedef.h"
27#include "job.h"
28#include "commands.h"
29#include "variable.h"
30#include "rule.h"
31#include "debug.h"
32#include "hash.h"
33#ifdef KMK
34# include "kbuild.h"
35#endif
36#ifdef CONFIG_WITH_INCLUDEDEP
37# ifdef HAVE_FCNTL_H
38# include <fcntl.h>
39# else
40# include <sys/file.h>
41#endif
42# ifdef WINDOWS32
43# include <io.h>
44# endif
45#endif
46
47#ifndef WINDOWS32
48#ifndef _AMIGA
49#ifndef VMS
50#include <pwd.h>
51#else
52struct passwd *getpwnam (char *name);
53#endif
54#endif
55#endif /* !WINDOWS32 */
56
57/* A 'struct ebuffer' controls the origin of the makefile we are currently
58 eval'ing.
59*/
60
61struct ebuffer
62 {
63 char *buffer; /* Start of the current line in the buffer. */
64 char *bufnext; /* Start of the next line in the buffer. */
65 char *bufstart; /* Start of the entire buffer. */
66 unsigned int size; /* Malloc'd size of buffer. */
67 FILE *fp; /* File, or NULL if this is an internal buffer. */
68 struct floc floc; /* Info on the file in fp (if any). */
69 };
70
71/* Types of "words" that can be read in a makefile. */
72enum make_word_type
73 {
74 w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
75 w_varassign
76 };
77
78
79/* A `struct conditionals' contains the information describing
80 all the active conditionals in a makefile.
81
82 The global variable `conditionals' contains the conditionals
83 information for the current makefile. It is initialized from
84 the static structure `toplevel_conditionals' and is later changed
85 to new structures for included makefiles. */
86
87struct conditionals
88 {
89 unsigned int if_cmds; /* Depth of conditional nesting. */
90 unsigned int allocated; /* Elts allocated in following arrays. */
91 char *ignoring; /* Are we ignoring or interpreting?
92 0=interpreting, 1=not yet interpreted,
93 2=already interpreted */
94 char *seen_else; /* Have we already seen an `else'? */
95 };
96
97static struct conditionals toplevel_conditionals;
98static struct conditionals *conditionals = &toplevel_conditionals;
99
100
101/* Default directories to search for include files in */
102
103static const char *default_include_directories[] =
104 {
105#ifndef KMK
106#if defined(WINDOWS32) && !defined(INCLUDEDIR)
107/* This completely up to the user when they install MSVC or other packages.
108 This is defined as a placeholder. */
109# define INCLUDEDIR "."
110#endif
111# ifdef INCLUDEDIR /* bird */
112 INCLUDEDIR,
113# else /* bird */
114 ".", /* bird */
115# endif /* bird */
116#ifndef _AMIGA
117 "/usr/gnu/include",
118 "/usr/local/include",
119 "/usr/include",
120#endif
121#endif /* !KMK */
122 0
123 };
124
125/* List of directories to search for include files in */
126
127static const char **include_directories;
128
129/* Maximum length of an element of the above. */
130
131static unsigned int max_incl_len;
132
133/* The filename and pointer to line number of the
134 makefile currently being read in. */
135
136const struct floc *reading_file = 0;
137
138/* The chain of makefiles read by read_makefile. */
139
140static struct dep *read_makefiles = 0;
141
142static int eval_makefile (const char *filename, int flags);
143static int eval (struct ebuffer *buffer, int flags);
144
145static long readline (struct ebuffer *ebuf);
146static void do_define (char *name, unsigned int namelen,
147 enum variable_origin origin, struct ebuffer *ebuf);
148static int conditional_line (char *line, int len, const struct floc *flocp);
149static void record_files (struct nameseq *filenames, const char *pattern,
150 const char *pattern_percent, struct dep *deps,
151 unsigned int cmds_started, char *commands,
152 unsigned int commands_idx, int two_colon,
153 const struct floc *flocp);
154static void record_target_var (struct nameseq *filenames, char *defn,
155 enum variable_origin origin, int enabled,
156 const struct floc *flocp);
157static enum make_word_type get_next_mword (char *buffer, char *delim,
158 char **startp, unsigned int *length);
159static void remove_comments (char *line);
160static char *find_char_unquote (char *string, int stop1, int stop2,
161 int blank, int ignorevars);
162
163
164/* Read in all the makefiles and return the chain of their names. */
165
166struct dep *
167read_all_makefiles (const char **makefiles)
168{
169 unsigned int num_makefiles = 0;
170
171 /* Create *_LIST variables, to hold the makefiles, targets, and variables
172 we will be reading. */
173
174 define_variable ("MAKEFILE_LIST", sizeof ("MAKEFILE_LIST")-1, "", o_file, 0);
175
176 DB (DB_BASIC, (_("Reading makefiles...\n")));
177
178 /* If there's a non-null variable MAKEFILES, its value is a list of
179 files to read first thing. But don't let it prevent reading the
180 default makefiles and don't let the default goal come from there. */
181
182 {
183 char *value;
184 char *name, *p;
185 unsigned int length;
186
187 {
188 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
189 int save = warn_undefined_variables_flag;
190 warn_undefined_variables_flag = 0;
191
192 value = allocated_variable_expand ("$(MAKEFILES)");
193
194 warn_undefined_variables_flag = save;
195 }
196
197 /* Set NAME to the start of next token and LENGTH to its length.
198 MAKEFILES is updated for finding remaining tokens. */
199 p = value;
200
201 while ((name = find_next_token ((const char **)&p, &length)) != 0)
202 {
203 if (*p != '\0')
204 *p++ = '\0';
205 eval_makefile (name, RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE);
206 }
207
208 free (value);
209 }
210
211 /* Read makefiles specified with -f switches. */
212
213 if (makefiles != 0)
214 while (*makefiles != 0)
215 {
216 struct dep *tail = read_makefiles;
217 register struct dep *d;
218
219 if (! eval_makefile (*makefiles, 0))
220 perror_with_name ("", *makefiles);
221
222 /* Find the right element of read_makefiles. */
223 d = read_makefiles;
224 while (d->next != tail)
225 d = d->next;
226
227 /* Use the storage read_makefile allocates. */
228 *makefiles = dep_name (d);
229 ++num_makefiles;
230 ++makefiles;
231 }
232
233 /* If there were no -f switches, try the default names. */
234
235 if (num_makefiles == 0)
236 {
237 static char *default_makefiles[] =
238#ifdef VMS
239 /* all lower case since readdir() (the vms version) 'lowercasifies' */
240# ifdef KMK
241 { "makefile.kmk", "makefile.vms", "gnumakefile.", "makefile.", 0 };
242# else
243 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
244# endif
245#else
246#ifdef _AMIGA
247 /* what's the deal here? no dots? */
248# ifdef KMK
249 { "Makefile.kmk", "makefile.kmk", "GNUmakefile", "Makefile", "SMakefile", 0 };
250# else
251 { "GNUmakefile", "Makefile", "SMakefile", 0 };
252# endif
253#else /* !Amiga && !VMS */
254# ifdef KMK
255 { "Makefile.kmk", "makefile.kmk", "GNUmakefile", "makefile", "Makefile", 0 };
256# else
257 { "GNUmakefile", "makefile", "Makefile", 0 };
258# endif
259#endif /* AMIGA */
260#endif /* VMS */
261 register char **p = default_makefiles;
262 while (*p != 0 && !file_exists_p (*p))
263 ++p;
264
265 if (*p != 0)
266 {
267 if (! eval_makefile (*p, 0))
268 perror_with_name ("", *p);
269 }
270 else
271 {
272 /* No default makefile was found. Add the default makefiles to the
273 `read_makefiles' chain so they will be updated if possible. */
274 struct dep *tail = read_makefiles;
275 /* Add them to the tail, after any MAKEFILES variable makefiles. */
276 while (tail != 0 && tail->next != 0)
277 tail = tail->next;
278 for (p = default_makefiles; *p != 0; ++p)
279 {
280 struct dep *d = alloc_dep ();
281 d->file = enter_file (strcache_add (*p));
282 d->file->dontcare = 1;
283 /* Tell update_goal_chain to bail out as soon as this file is
284 made, and main not to die if we can't make this file. */
285 d->changed = RM_DONTCARE;
286 if (tail == 0)
287 read_makefiles = d;
288 else
289 tail->next = d;
290 tail = d;
291 }
292 if (tail != 0)
293 tail->next = 0;
294 }
295 }
296
297 return read_makefiles;
298}
299
300
301/* Install a new conditional and return the previous one. */
302
303static struct conditionals *
304install_conditionals (struct conditionals *new)
305{
306 struct conditionals *save = conditionals;
307
308 memset (new, '\0', sizeof (*new));
309 conditionals = new;
310
311 return save;
312}
313
314/* Free the current conditionals and reinstate a saved one. */
315
316static void
317restore_conditionals (struct conditionals *saved)
318{
319 /* Free any space allocated by conditional_line. */
320 if (conditionals->ignoring)
321 free (conditionals->ignoring);
322 if (conditionals->seen_else)
323 free (conditionals->seen_else);
324
325 /* Restore state. */
326 conditionals = saved;
327}
328
329
330static int
331eval_makefile (const char *filename, int flags)
332{
333 struct dep *deps;
334 struct ebuffer ebuf;
335 const struct floc *curfile;
336 char *expanded = 0;
337 int makefile_errno;
338 int r;
339
340 filename = strcache_add (filename);
341 ebuf.floc.filenm = filename;
342 ebuf.floc.lineno = 1;
343
344 if (ISDB (DB_VERBOSE))
345 {
346 printf (_("Reading makefile `%s'"), filename);
347 if (flags & RM_NO_DEFAULT_GOAL)
348 printf (_(" (no default goal)"));
349 if (flags & RM_INCLUDED)
350 printf (_(" (search path)"));
351 if (flags & RM_DONTCARE)
352 printf (_(" (don't care)"));
353 if (flags & RM_NO_TILDE)
354 printf (_(" (no ~ expansion)"));
355 puts ("...");
356 }
357
358 /* First, get a stream to read. */
359
360 /* Expand ~ in FILENAME unless it came from `include',
361 in which case it was already done. */
362 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
363 {
364 expanded = tilde_expand (filename);
365 if (expanded != 0)
366 filename = expanded;
367 }
368
369 ebuf.fp = fopen (filename, "r");
370 /* Save the error code so we print the right message later. */
371 makefile_errno = errno;
372
373 /* If the makefile wasn't found and it's either a makefile from
374 the `MAKEFILES' variable or an included makefile,
375 search the included makefile search path for this makefile. */
376 if (ebuf.fp == 0 && (flags & RM_INCLUDED) && *filename != '/')
377 {
378 unsigned int i;
379 for (i = 0; include_directories[i] != 0; ++i)
380 {
381 const char *included = concat (include_directories[i], "/", filename);
382 ebuf.fp = fopen (included, "r");
383 if (ebuf.fp)
384 {
385 filename = strcache_add (included);
386 break;
387 }
388 }
389 }
390
391 /* Add FILENAME to the chain of read makefiles. */
392 deps = alloc_dep ();
393 deps->next = read_makefiles;
394 read_makefiles = deps;
395 deps->file = lookup_file (filename);
396 if (deps->file == 0)
397 deps->file = enter_file (filename);
398 filename = deps->file->name;
399 deps->changed = flags;
400 if (flags & RM_DONTCARE)
401 deps->file->dontcare = 1;
402
403 if (expanded)
404 free (expanded);
405
406 /* If the makefile can't be found at all, give up entirely. */
407
408 if (ebuf.fp == 0)
409 {
410 /* If we did some searching, errno has the error from the last
411 attempt, rather from FILENAME itself. Restore it in case the
412 caller wants to use it in a message. */
413 errno = makefile_errno;
414 return 0;
415 }
416
417 /* Add this makefile to the list. */
418 do_variable_definition (&ebuf.floc, "MAKEFILE_LIST", filename, o_file,
419 f_append, 0);
420
421 /* Evaluate the makefile */
422
423 ebuf.size = 200;
424 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = xmalloc (ebuf.size);
425
426 curfile = reading_file;
427 reading_file = &ebuf.floc;
428
429 r = eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
430
431 reading_file = curfile;
432
433 fclose (ebuf.fp);
434
435 free (ebuf.bufstart);
436 alloca (0);
437 return r;
438}
439
440int
441eval_buffer (char *buffer)
442{
443 struct ebuffer ebuf;
444 struct conditionals *saved;
445 struct conditionals new;
446 const struct floc *curfile;
447 int r;
448
449 /* Evaluate the buffer */
450
451 ebuf.size = strlen (buffer);
452 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = buffer;
453 ebuf.fp = NULL;
454
455 ebuf.floc = *reading_file;
456
457 curfile = reading_file;
458 reading_file = &ebuf.floc;
459
460 saved = install_conditionals (&new);
461
462 r = eval (&ebuf, 1);
463
464 restore_conditionals (saved);
465
466 reading_file = curfile;
467
468 alloca (0);
469 return r;
470}
471
472#ifdef CONFIG_WITH_INCLUDEDEP
473/* no nonsense dependency file including.
474
475 Because nobody wants bogus dependency files to break their incremental
476 builds with hard to comprehend error messages, this function does not
477 use the normal eval routine but does all the parsing itself. This isn't,
478 as much work as it sounds, because the necessary feature set is very
479 limited. */
480void
481eval_include_dep (const char *name, struct floc *f)
482{
483 int fd;
484 char *file_base;
485 const char *file_end, *cur, *endp;
486 struct stat st;
487 unsigned line_no;
488
489 /* open and read it into memory. ignore non-existing dependency files
490 or that we can't read. */
491 errno = 0;
492#ifdef O_BINARY
493 fd = open (name, O_RDONLY | O_BINARY, 0);
494#else
495 fd = open (name, O_RDONLY, 0);
496#endif
497 if (fd < 0)
498 {
499 int err = errno;
500 if (err == ENOENT || !file_exists_p (name))
501 return;
502 error (f, "%s: %s", name, strerror (err));
503 return;
504 }
505 if (fstat (fd, &st))
506 {
507 error (f, "%s: fstat: %s", name, strerror (errno));
508 close (fd);
509 return;
510 }
511 file_base = xmalloc (st.st_size + 1);
512 if (read (fd, file_base, st.st_size) != st.st_size)
513 {
514 error (f, "%s: read: %s", name, strerror (errno));
515 close (fd);
516 free (file_base);
517 return;
518 }
519 close (fd);
520 file_end = file_base + st.st_size;
521 file_base[st.st_size] = '\0';
522
523 /* now parse the file. */
524 line_no = 1;
525 cur = file_base;
526 while (cur < file_end)
527 {
528 /* skip empty lines */
529 while (cur < file_end && isspace ((unsigned char)*cur) && *cur != '\n')
530 ++cur;
531 if (cur >= file_end)
532 break;
533 if (*cur == '#')
534 {
535 cur = memchr (cur, '\n', file_end - cur);
536 if (!cur)
537 break;
538 }
539 if (*cur == '\\')
540 {
541 unsigned eol_len = (file_end - cur > 1 && cur[1] == '\n') ? 2
542 : (file_end - cur > 2 && cur[1] == '\r' && cur[2] == '\n') ? 3
543 : (file_end - cur == 1) ? 1 : 0;
544 if (eol_len)
545 {
546 cur += eol_len;
547 line_no++;
548 continue;
549 }
550 }
551 if (*cur == '\n')
552 {
553 cur++;
554 line_no++;
555 continue;
556 }
557
558 /* define var ... endef for command tracking. */
559 if (strneq (cur, "define ", 7))
560 {
561 const char *var;
562 unsigned var_len;
563 const char *value_start;
564 const char *value_end;
565 char *value;
566 unsigned value_len;
567 int found_endef = 0;
568
569 /* extract the variable name. */
570 cur += 7;
571 while (isblank ((unsigned char)*cur))
572 ++cur;
573 value_start = endp = memchr (cur, '\n', file_end - cur);
574 if (!endp)
575 endp = cur;
576 while (endp > cur && isspace ((unsigned char)endp[-1]))
577 --endp;
578 var_len = endp - cur;
579 if (!var_len)
580 {
581 error (f, "%s(%d): bogus define statement.", name, line_no);
582 break;
583 }
584 var = strcache_add_len (cur, var_len);
585
586 /* find the end of the variable. */
587 cur = value_end = value_start = value_start + 1;
588 ++line_no;
589 while (cur < file_end)
590 {
591 /* check for endef, don't bother with skipping leading spaces. */
592 if ( file_end - cur >= 5
593 && strneq (cur, "endef", 5))
594 {
595 endp = cur + 5;
596 while (endp < file_end && isspace ((unsigned char)*endp) && *endp != '\n')
597 endp++;
598 if (endp >= file_end || *endp == '\n')
599 {
600 found_endef = 1;
601 cur = endp >= file_end ? file_end : endp + 1;
602 break;
603 }
604 }
605
606 /* skip a line ahead. */
607 cur = value_end = memchr (cur, '\n', file_end - cur);
608 if (cur != NULL)
609 ++cur;
610 else
611 cur = value_end = file_end;
612 ++line_no;
613 }
614
615 if (!found_endef)
616 {
617 error (f, "%s(%d): bogus define statement.", name, line_no);
618 break;
619 }
620
621 /* make a copy of the value, converting \r\n to \n, and define it. */
622 value_len = value_end - value_start;
623 value = xmalloc (value_len + 1);
624 endp = memchr (value_start, '\r', value_len);
625 if (endp)
626 {
627 const char *src = value_start;
628 char *dst = value;
629 for (;;)
630 {
631 size_t len = endp - src;
632 memcpy (dst, src, len);
633 dst += len;
634 src = endp;
635 if (src + 1 < file_end && src[1] == '\n')
636 src++; /* skip the '\r' */
637 if (src >= value_end)
638 break;
639 endp = memchr (endp + 1, '\r', src - value_end);
640 if (!endp)
641 endp = value_end;
642 }
643 value_len = dst - value;
644 }
645 else
646 memcpy (value, value_start, value_len);
647 value [value_len] = '\0';
648
649 define_variable_in_set (var, var_len, value, value_len,
650 0 /* don't duplicate */, o_file,
651 0 /* defines are recursive but this is faster */,
652 NULL /* global set */, f);
653 }
654 /* file: deps */
655 else
656 {
657 struct nameseq *filenames = 0;
658 struct dep *deps = 0;
659 struct dep **nextdep = &deps;
660 struct dep *dep;
661 int next_line = 1;
662 char *colonp;
663
664 /* look for a colon, ASSUME it's on the same line. */
665 colonp = memchr (cur, ':', file_end - cur);
666#ifdef HAVE_DOS_PATHS
667 while ( colonp
668 && (colonp[1] == '/' || colonp[1] == '\\')
669 && colonp > cur
670 && isalpha ((unsigned char)colonp[-1])
671 && ( colonp == cur + 1
672 || strchr (" \t(", colonp[-2]) != 0))
673 colonp = memchr (colonp + 1, ':', file_end - (colonp + 1));
674#endif
675 if (!colonp || memchr (cur, '\n', colonp - cur))
676 {
677 error (f, "%s(%d): no colon.", name, line_no);
678 break;
679 }
680
681 /* extract the filename, ASSUME a single one. */
682 endp = colonp;
683 while (endp > cur && isblank ((unsigned char)endp[-1]))
684 --endp;
685 if (cur == endp)
686 {
687 error (f, "%s(%d): empty filename.", name, line_no);
688 break;
689 }
690 filenames = xmalloc (sizeof (struct nameseq));
691 memset (filenames, 0, sizeof (*filenames));
692 filenames->name = strcache_add_len (cur, endp - cur);
693
694 /* parse any dependencies. */
695 cur = colonp + 1;
696 while (cur < file_end)
697 {
698 /* skip blanks and count lines. */
699 while (cur < file_end && isspace ((unsigned char)*cur) && *cur != '\n')
700 ++cur;
701 if (cur >= file_end)
702 break;
703 if (*cur == '\n')
704 {
705 cur++;
706 line_no++;
707 break;
708 }
709
710 /* continuation + eol? */
711 if (*cur == '\\')
712 {
713 unsigned eol_len = (file_end - cur > 1 && cur[1] == '\n') ? 2
714 : (file_end - cur > 2 && cur[1] == '\r' && cur[2] == '\n') ? 3
715 : (file_end - cur == 1) ? 1 : 0;
716 if (eol_len)
717 {
718 cur += eol_len;
719 line_no++;
720 continue;
721 }
722 }
723
724 /* find the end of the filename */
725 endp = cur;
726 while (endp < file_end && !isspace ((unsigned char)*endp))
727 ++endp;
728
729 /* add it to the list. */
730 *nextdep = dep = alloc_dep ();
731 dep->name = strcache_add_len (cur, endp - cur);
732 nextdep = &dep->next;
733
734 cur = endp;
735 }
736
737 /* enter the file with its dependencies. */
738 record_files (filenames, NULL, NULL, deps, 0, NULL, 0, 0, f);
739 }
740 }
741 free (file_base);
742}
743#endif /* CONFIG_WITH_INCLUDEDEP */
744
745
746
747/* Read file FILENAME as a makefile and add its contents to the data base.
748
749 SET_DEFAULT is true if we are allowed to set the default goal. */
750
751
752static int
753eval (struct ebuffer *ebuf, int set_default)
754{
755 char *collapsed = 0;
756 unsigned int collapsed_length = 0;
757 unsigned int commands_len = 200;
758 char *commands;
759 unsigned int commands_idx = 0;
760 unsigned int cmds_started, tgts_started;
761 int ignoring = 0, in_ignored_define = 0;
762 int no_targets = 0; /* Set when reading a rule without targets. */
763 struct nameseq *filenames = 0;
764 struct dep *deps = 0;
765 long nlines = 0;
766 int two_colon = 0;
767 const char *pattern = 0;
768 const char *pattern_percent;
769 struct floc *fstart;
770 struct floc fi;
771
772#define record_waiting_files() \
773 do \
774 { \
775 if (filenames != 0) \
776 { \
777 fi.lineno = tgts_started; \
778 record_files (filenames, pattern, pattern_percent, deps, \
779 cmds_started, commands, commands_idx, two_colon, \
780 &fi); \
781 } \
782 filenames = 0; \
783 commands_idx = 0; \
784 no_targets = 0; \
785 pattern = 0; \
786 } while (0)
787
788 pattern_percent = 0;
789 cmds_started = tgts_started = 1;
790
791 fstart = &ebuf->floc;
792 fi.filenm = ebuf->floc.filenm;
793
794 /* Loop over lines in the file.
795 The strategy is to accumulate target names in FILENAMES, dependencies
796 in DEPS and commands in COMMANDS. These are used to define a rule
797 when the start of the next rule (or eof) is encountered.
798
799 When you see a "continue" in the loop below, that means we are moving on
800 to the next line _without_ ending any rule that we happen to be working
801 with at the moment. If you see a "goto rule_complete", then the
802 statement we just parsed also finishes the previous rule. */
803
804 commands = xmalloc (200);
805
806 while (1)
807 {
808 unsigned int linelen;
809 char *line;
810 unsigned int wlen;
811 char *p;
812 char *p2;
813
814 /* Grab the next line to be evaluated */
815 ebuf->floc.lineno += nlines;
816 nlines = readline (ebuf);
817
818 /* If there is nothing left to eval, we're done. */
819 if (nlines < 0)
820 break;
821
822 /* If this line is empty, skip it. */
823 line = ebuf->buffer;
824 if (line[0] == '\0')
825 continue;
826
827 linelen = strlen (line);
828
829 /* Check for a shell command line first.
830 If it is not one, we can stop treating tab specially. */
831 if (line[0] == cmd_prefix)
832 {
833 if (no_targets)
834 /* Ignore the commands in a rule with no targets. */
835 continue;
836
837 /* If there is no preceding rule line, don't treat this line
838 as a command, even though it begins with a tab character.
839 SunOS 4 make appears to behave this way. */
840
841 if (filenames != 0)
842 {
843 if (ignoring)
844 /* Yep, this is a shell command, and we don't care. */
845 continue;
846
847 /* Append this command line to the line being accumulated. */
848 if (commands_idx == 0)
849 cmds_started = ebuf->floc.lineno;
850
851 if (linelen + 1 + commands_idx > commands_len)
852 {
853 commands_len = (linelen + 1 + commands_idx) * 2;
854 commands = xrealloc (commands, commands_len);
855 }
856 memcpy (&commands[commands_idx], line, linelen);
857 commands_idx += linelen;
858 commands[commands_idx++] = '\n';
859
860 continue;
861 }
862 }
863
864 /* This line is not a shell command line. Don't worry about tabs.
865 Get more space if we need it; we don't need to preserve the current
866 contents of the buffer. */
867
868 if (collapsed_length < linelen+1)
869 {
870 collapsed_length = linelen+1;
871 if (collapsed)
872 free (collapsed);
873 collapsed = xmalloc (collapsed_length);
874 }
875 strcpy (collapsed, line);
876 /* Collapse continuation lines. */
877 collapse_continuations (collapsed);
878 remove_comments (collapsed);
879
880 /* Compare a word, both length and contents. */
881#define word1eq(s) (wlen == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
882 p = collapsed;
883 while (isspace ((unsigned char)*p))
884 ++p;
885
886 if (*p == '\0')
887 /* This line is completely empty--ignore it. */
888 continue;
889
890 /* Find the end of the first token. Note we don't need to worry about
891 * ":" here since we compare tokens by length (so "export" will never
892 * be equal to "export:").
893 */
894 for (p2 = p+1; *p2 != '\0' && !isspace ((unsigned char)*p2); ++p2)
895 ;
896 wlen = p2 - p;
897
898 /* Find the start of the second token. If it looks like a target or
899 variable definition it can't be a preprocessor token so skip
900 them--this allows variables/targets named `ifdef', `export', etc. */
901 while (isspace ((unsigned char)*p2))
902 ++p2;
903
904 if ((p2[0] == ':' || p2[0] == '+' || p2[0] == '=') && p2[1] == '\0')
905 {
906 /* It can't be a preprocessor token so skip it if we're ignoring */
907 if (ignoring)
908 continue;
909
910 goto skip_conditionals;
911 }
912
913 /* We must first check for conditional and `define' directives before
914 ignoring anything, since they control what we will do with
915 following lines. */
916
917 if (!in_ignored_define)
918 {
919 int i = conditional_line (p, wlen, fstart);
920 if (i != -2)
921 {
922 if (i == -1)
923 fatal (fstart, _("invalid syntax in conditional"));
924
925 ignoring = i;
926 continue;
927 }
928 }
929
930 if (word1eq ("endef"))
931 {
932 if (!in_ignored_define)
933 fatal (fstart, _("extraneous `endef'"));
934 in_ignored_define = 0;
935 continue;
936 }
937
938 if (word1eq ("define"))
939 {
940 if (ignoring)
941 in_ignored_define = 1;
942 else
943 {
944 if (*p2 == '\0')
945 fatal (fstart, _("empty variable name"));
946
947 /* Let the variable name be the whole rest of the line,
948 with trailing blanks stripped (comments have already been
949 removed), so it could be a complex variable/function
950 reference that might contain blanks. */
951 p = strchr (p2, '\0');
952 while (isblank ((unsigned char)p[-1]))
953 --p;
954 do_define (p2, p - p2, o_file, ebuf);
955 }
956 continue;
957 }
958
959 if (word1eq ("override"))
960 {
961 if (*p2 == '\0')
962 error (fstart, _("empty `override' directive"));
963
964 if (strneq (p2, "define", 6)
965 && (isblank ((unsigned char)p2[6]) || p2[6] == '\0'))
966 {
967 if (ignoring)
968 in_ignored_define = 1;
969 else
970 {
971 p2 = next_token (p2 + 6);
972 if (*p2 == '\0')
973 fatal (fstart, _("empty variable name"));
974
975 /* Let the variable name be the whole rest of the line,
976 with trailing blanks stripped (comments have already been
977 removed), so it could be a complex variable/function
978 reference that might contain blanks. */
979 p = strchr (p2, '\0');
980 while (isblank ((unsigned char)p[-1]))
981 --p;
982 do_define (p2, p - p2, o_override, ebuf);
983 }
984 }
985 else if (!ignoring
986 && !try_variable_definition (fstart, p2, o_override, 0))
987 error (fstart, _("invalid `override' directive"));
988
989 continue;
990 }
991
992 if (ignoring)
993 /* Ignore the line. We continue here so conditionals
994 can appear in the middle of a rule. */
995 continue;
996
997 if (word1eq ("export"))
998 {
999 /* 'export' by itself causes everything to be exported. */
1000 if (*p2 == '\0')
1001 export_all_variables = 1;
1002 else
1003 {
1004 struct variable *v;
1005
1006 v = try_variable_definition (fstart, p2, o_file, 0);
1007 if (v != 0)
1008 v->export = v_export;
1009 else
1010 {
1011 unsigned int l;
1012 const char *cp;
1013 char *ap;
1014
1015 /* Expand the line so we can use indirect and constructed
1016 variable names in an export command. */
1017 cp = ap = allocated_variable_expand (p2);
1018
1019 for (p = find_next_token (&cp, &l); p != 0;
1020 p = find_next_token (&cp, &l))
1021 {
1022 v = lookup_variable (p, l);
1023 if (v == 0)
1024 v = define_variable_loc (p, l, "", o_file, 0, fstart);
1025 v->export = v_export;
1026 }
1027
1028 free (ap);
1029 }
1030 }
1031 goto rule_complete;
1032 }
1033
1034 if (word1eq ("unexport"))
1035 {
1036 if (*p2 == '\0')
1037 export_all_variables = 0;
1038 else
1039 {
1040 unsigned int l;
1041 struct variable *v;
1042 const char *cp;
1043 char *ap;
1044
1045 /* Expand the line so we can use indirect and constructed
1046 variable names in an unexport command. */
1047 cp = ap = allocated_variable_expand (p2);
1048
1049 for (p = find_next_token (&cp, &l); p != 0;
1050 p = find_next_token (&cp, &l))
1051 {
1052 v = lookup_variable (p, l);
1053 if (v == 0)
1054 v = define_variable_loc (p, l, "", o_file, 0, fstart);
1055
1056 v->export = v_noexport;
1057 }
1058
1059 free (ap);
1060 }
1061 goto rule_complete;
1062 }
1063
1064 skip_conditionals:
1065 if (word1eq ("vpath"))
1066 {
1067 const char *cp;
1068 char *vpat;
1069 unsigned int l;
1070 cp = variable_expand (p2);
1071 p = find_next_token (&cp, &l);
1072 if (p != 0)
1073 {
1074 vpat = savestring (p, l);
1075 p = find_next_token (&cp, &l);
1076 /* No searchpath means remove all previous
1077 selective VPATH's with the same pattern. */
1078 }
1079 else
1080 /* No pattern means remove all previous selective VPATH's. */
1081 vpat = 0;
1082 construct_vpath_list (vpat, p);
1083 if (vpat != 0)
1084 free (vpat);
1085
1086 goto rule_complete;
1087 }
1088
1089#ifdef CONFIG_WITH_INCLUDEDEP
1090 if (word1eq ("includedep"))
1091 {
1092 /* We have found an `includedep' line specifying a single nested
1093 makefile to be read at this point. This include variation
1094 does not globbing and doesn't support multiple names. It's
1095 trying to save time by being dead simple. */
1096 char *name = p2;
1097 char *end = strchr(name, '\0');
1098 char saved;
1099
1100 while (end > name && isspace ((unsigned char)end[-1]))
1101 --end;
1102
1103 saved = *end; /* not sure if this is required... */
1104 *end = '\0';
1105 eval_include_dep (name, fstart);
1106 *end = saved;
1107
1108 goto rule_complete;
1109 }
1110#endif /* CONFIG_WITH_INCLUDEDEP */
1111
1112 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
1113 {
1114 /* We have found an `include' line specifying a nested
1115 makefile to be read at this point. */
1116 struct conditionals *save;
1117 struct conditionals new_conditionals;
1118 struct nameseq *files;
1119 /* "-include" (vs "include") says no error if the file does not
1120 exist. "sinclude" is an alias for this from SGI. */
1121 int noerror = (p[0] != 'i');
1122
1123 p = allocated_variable_expand (p2);
1124
1125 /* If no filenames, it's a no-op. */
1126 if (*p == '\0')
1127 {
1128 free (p);
1129 continue;
1130 }
1131
1132 /* Parse the list of file names. */
1133 p2 = p;
1134 files = multi_glob (parse_file_seq (&p2, '\0',
1135 sizeof (struct nameseq),
1136 1),
1137 sizeof (struct nameseq));
1138 free (p);
1139
1140 /* Save the state of conditionals and start
1141 the included makefile with a clean slate. */
1142 save = install_conditionals (&new_conditionals);
1143
1144 /* Record the rules that are waiting so they will determine
1145 the default goal before those in the included makefile. */
1146 record_waiting_files ();
1147
1148 /* Read each included makefile. */
1149 while (files != 0)
1150 {
1151 struct nameseq *next = files->next;
1152 const char *name = files->name;
1153 int r;
1154
1155 free (files);
1156 files = next;
1157
1158 r = eval_makefile (name, (RM_INCLUDED | RM_NO_TILDE
1159 | (noerror ? RM_DONTCARE : 0)));
1160 if (!r && !noerror)
1161 error (fstart, "%s: %s", name, strerror (errno));
1162 }
1163
1164 /* Restore conditional state. */
1165 restore_conditionals (save);
1166
1167 goto rule_complete;
1168 }
1169
1170 if (try_variable_definition (fstart, p, o_file, 0))
1171 /* This line has been dealt with. */
1172 goto rule_complete;
1173
1174 /* This line starts with a tab but was not caught above because there
1175 was no preceding target, and the line might have been usable as a
1176 variable definition. But now we know it is definitely lossage. */
1177 if (line[0] == cmd_prefix)
1178 fatal(fstart, _("commands commence before first target"));
1179
1180 /* This line describes some target files. This is complicated by
1181 the existence of target-specific variables, because we can't
1182 expand the entire line until we know if we have one or not. So
1183 we expand the line word by word until we find the first `:',
1184 then check to see if it's a target-specific variable.
1185
1186 In this algorithm, `lb_next' will point to the beginning of the
1187 unexpanded parts of the input buffer, while `p2' points to the
1188 parts of the expanded buffer we haven't searched yet. */
1189
1190 {
1191 enum make_word_type wtype;
1192 enum variable_origin v_origin;
1193 int exported;
1194 char *cmdleft, *semip, *lb_next;
1195 unsigned int plen = 0;
1196 char *colonp;
1197 const char *end, *beg; /* Helpers for whitespace stripping. */
1198
1199 /* Record the previous rule. */
1200
1201 record_waiting_files ();
1202 tgts_started = fstart->lineno;
1203
1204 /* Search the line for an unquoted ; that is not after an
1205 unquoted #. */
1206 cmdleft = find_char_unquote (line, ';', '#', 0, 1);
1207 if (cmdleft != 0 && *cmdleft == '#')
1208 {
1209 /* We found a comment before a semicolon. */
1210 *cmdleft = '\0';
1211 cmdleft = 0;
1212 }
1213 else if (cmdleft != 0)
1214 /* Found one. Cut the line short there before expanding it. */
1215 *(cmdleft++) = '\0';
1216 semip = cmdleft;
1217
1218 collapse_continuations (line);
1219
1220 /* We can't expand the entire line, since if it's a per-target
1221 variable we don't want to expand it. So, walk from the
1222 beginning, expanding as we go, and looking for "interesting"
1223 chars. The first word is always expandable. */
1224 wtype = get_next_mword(line, NULL, &lb_next, &wlen);
1225 switch (wtype)
1226 {
1227 case w_eol:
1228 if (cmdleft != 0)
1229 fatal(fstart, _("missing rule before commands"));
1230 /* This line contained something but turned out to be nothing
1231 but whitespace (a comment?). */
1232 continue;
1233
1234 case w_colon:
1235 case w_dcolon:
1236 /* We accept and ignore rules without targets for
1237 compatibility with SunOS 4 make. */
1238 no_targets = 1;
1239 continue;
1240
1241 default:
1242 break;
1243 }
1244
1245 p2 = variable_expand_string(NULL, lb_next, wlen);
1246
1247 while (1)
1248 {
1249 lb_next += wlen;
1250 if (cmdleft == 0)
1251 {
1252 /* Look for a semicolon in the expanded line. */
1253 cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
1254
1255 if (cmdleft != 0)
1256 {
1257 unsigned long p2_off = p2 - variable_buffer;
1258 unsigned long cmd_off = cmdleft - variable_buffer;
1259 char *pend = p2 + strlen(p2);
1260
1261 /* Append any remnants of lb, then cut the line short
1262 at the semicolon. */
1263 *cmdleft = '\0';
1264
1265 /* One school of thought says that you shouldn't expand
1266 here, but merely copy, since now you're beyond a ";"
1267 and into a command script. However, the old parser
1268 expanded the whole line, so we continue that for
1269 backwards-compatiblity. Also, it wouldn't be
1270 entirely consistent, since we do an unconditional
1271 expand below once we know we don't have a
1272 target-specific variable. */
1273 (void)variable_expand_string(pend, lb_next, (long)-1);
1274 lb_next += strlen(lb_next);
1275 p2 = variable_buffer + p2_off;
1276 cmdleft = variable_buffer + cmd_off + 1;
1277 }
1278 }
1279
1280 colonp = find_char_unquote(p2, ':', 0, 0, 0);
1281#ifdef HAVE_DOS_PATHS
1282 /* The drive spec brain-damage strikes again... */
1283 /* Note that the only separators of targets in this context
1284 are whitespace and a left paren. If others are possible,
1285 they should be added to the string in the call to index. */
1286 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
1287 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
1288 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
1289 colonp = find_char_unquote(colonp + 1, ':', 0, 0, 0);
1290#endif
1291 if (colonp != 0)
1292 break;
1293
1294 wtype = get_next_mword(lb_next, NULL, &lb_next, &wlen);
1295 if (wtype == w_eol)
1296 break;
1297
1298 p2 += strlen(p2);
1299 *(p2++) = ' ';
1300 p2 = variable_expand_string(p2, lb_next, wlen);
1301 /* We don't need to worry about cmdleft here, because if it was
1302 found in the variable_buffer the entire buffer has already
1303 been expanded... we'll never get here. */
1304 }
1305
1306 p2 = next_token (variable_buffer);
1307
1308 /* If the word we're looking at is EOL, see if there's _anything_
1309 on the line. If not, a variable expanded to nothing, so ignore
1310 it. If so, we can't parse this line so punt. */
1311 if (wtype == w_eol)
1312 {
1313 if (*p2 != '\0')
1314 /* There's no need to be ivory-tower about this: check for
1315 one of the most common bugs found in makefiles... */
1316 fatal (fstart, _("missing separator%s"),
1317 !strneq(line, " ", 8) ? ""
1318 : _(" (did you mean TAB instead of 8 spaces?)"));
1319 continue;
1320 }
1321
1322 /* Make the colon the end-of-string so we know where to stop
1323 looking for targets. */
1324 *colonp = '\0';
1325 filenames = multi_glob (parse_file_seq (&p2, '\0',
1326 sizeof (struct nameseq),
1327 1),
1328 sizeof (struct nameseq));
1329 *p2 = ':';
1330
1331 if (!filenames)
1332 {
1333 /* We accept and ignore rules without targets for
1334 compatibility with SunOS 4 make. */
1335 no_targets = 1;
1336 continue;
1337 }
1338 /* This should never be possible; we handled it above. */
1339 assert (*p2 != '\0');
1340 ++p2;
1341
1342 /* Is this a one-colon or two-colon entry? */
1343 two_colon = *p2 == ':';
1344 if (two_colon)
1345 p2++;
1346
1347 /* Test to see if it's a target-specific variable. Copy the rest
1348 of the buffer over, possibly temporarily (we'll expand it later
1349 if it's not a target-specific variable). PLEN saves the length
1350 of the unparsed section of p2, for later. */
1351 if (*lb_next != '\0')
1352 {
1353 unsigned int l = p2 - variable_buffer;
1354 plen = strlen (p2);
1355 variable_buffer_output (p2+plen, lb_next, strlen (lb_next)+1);
1356 p2 = variable_buffer + l;
1357 }
1358
1359 /* See if it's an "override" or "export" keyword; if so see if what
1360 comes after it looks like a variable definition. */
1361
1362 wtype = get_next_mword (p2, NULL, &p, &wlen);
1363
1364 v_origin = o_file;
1365 exported = 0;
1366 if (wtype == w_static)
1367 {
1368 if (word1eq ("override"))
1369 {
1370 v_origin = o_override;
1371 wtype = get_next_mword (p+wlen, NULL, &p, &wlen);
1372 }
1373 else if (word1eq ("export"))
1374 {
1375 exported = 1;
1376 wtype = get_next_mword (p+wlen, NULL, &p, &wlen);
1377 }
1378 }
1379
1380 if (wtype != w_eol)
1381 wtype = get_next_mword (p+wlen, NULL, NULL, NULL);
1382
1383 if (wtype == w_varassign)
1384 {
1385 /* If there was a semicolon found, add it back, plus anything
1386 after it. */
1387 if (semip)
1388 {
1389 unsigned int l = p - variable_buffer;
1390 *(--semip) = ';';
1391 variable_buffer_output (p2 + strlen (p2),
1392 semip, strlen (semip)+1);
1393 p = variable_buffer + l;
1394 }
1395 record_target_var (filenames, p, v_origin, exported, fstart);
1396 filenames = 0;
1397 continue;
1398 }
1399
1400 /* This is a normal target, _not_ a target-specific variable.
1401 Unquote any = in the dependency list. */
1402 find_char_unquote (lb_next, '=', 0, 0, 0);
1403
1404 /* We have some targets, so don't ignore the following commands. */
1405 no_targets = 0;
1406
1407 /* Expand the dependencies, etc. */
1408 if (*lb_next != '\0')
1409 {
1410 unsigned int l = p2 - variable_buffer;
1411 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
1412 p2 = variable_buffer + l;
1413
1414 /* Look for a semicolon in the expanded line. */
1415 if (cmdleft == 0)
1416 {
1417 cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
1418 if (cmdleft != 0)
1419 *(cmdleft++) = '\0';
1420 }
1421 }
1422
1423 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
1424 p = strchr (p2, ':');
1425 while (p != 0 && p[-1] == '\\')
1426 {
1427 register char *q = &p[-1];
1428 register int backslash = 0;
1429 while (*q-- == '\\')
1430 backslash = !backslash;
1431 if (backslash)
1432 p = strchr (p + 1, ':');
1433 else
1434 break;
1435 }
1436#ifdef _AMIGA
1437 /* Here, the situation is quite complicated. Let's have a look
1438 at a couple of targets:
1439
1440 install: dev:make
1441
1442 dev:make: make
1443
1444 dev:make:: xyz
1445
1446 The rule is that it's only a target, if there are TWO :'s
1447 OR a space around the :.
1448 */
1449 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
1450 || isspace ((unsigned char)p[-1])))
1451 p = 0;
1452#endif
1453#ifdef HAVE_DOS_PATHS
1454 {
1455 int check_again;
1456 do {
1457 check_again = 0;
1458 /* For DOS-style paths, skip a "C:\..." or a "C:/..." */
1459 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
1460 isalpha ((unsigned char)p[-1]) &&
1461 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1462 p = strchr (p + 1, ':');
1463 check_again = 1;
1464 }
1465 } while (check_again);
1466 }
1467#endif
1468 if (p != 0)
1469 {
1470 struct nameseq *target;
1471 target = parse_file_seq (&p2, ':', sizeof (struct nameseq), 1);
1472 ++p2;
1473 if (target == 0)
1474 fatal (fstart, _("missing target pattern"));
1475 else if (target->next != 0)
1476 fatal (fstart, _("multiple target patterns (target `%s')"), target->name); /* bird */
1477 pattern_percent = find_percent_cached (&target->name);
1478 pattern = target->name;
1479 if (pattern_percent == 0)
1480 fatal (fstart, _("target pattern contains no `%%' (target `%s')"), target->name); /* bird */
1481 free (target);
1482 }
1483 else
1484 pattern = 0;
1485
1486 /* Strip leading and trailing whitespaces. */
1487 beg = p2;
1488 end = beg + strlen (beg) - 1;
1489 strip_whitespace (&beg, &end);
1490
1491 if (beg <= end && *beg != '\0')
1492 {
1493 /* Put all the prerequisites here; they'll be parsed later. */
1494 deps = alloc_dep ();
1495 deps->name = strcache_add_len (beg, end - beg + 1);
1496 }
1497 else
1498 deps = 0;
1499
1500 commands_idx = 0;
1501 if (cmdleft != 0)
1502 {
1503 /* Semicolon means rest of line is a command. */
1504 unsigned int l = strlen (cmdleft);
1505
1506 cmds_started = fstart->lineno;
1507
1508 /* Add this command line to the buffer. */
1509 if (l + 2 > commands_len)
1510 {
1511 commands_len = (l + 2) * 2;
1512 commands = xrealloc (commands, commands_len);
1513 }
1514 memcpy (commands, cmdleft, l);
1515 commands_idx += l;
1516 commands[commands_idx++] = '\n';
1517 }
1518
1519 /* Determine if this target should be made default. We used to do
1520 this in record_files() but because of the delayed target recording
1521 and because preprocessor directives are legal in target's commands
1522 it is too late. Consider this fragment for example:
1523
1524 foo:
1525
1526 ifeq ($(.DEFAULT_GOAL),foo)
1527 ...
1528 endif
1529
1530 Because the target is not recorded until after ifeq directive is
1531 evaluated the .DEFAULT_GOAL does not contain foo yet as one
1532 would expect. Because of this we have to move some of the logic
1533 here. */
1534
1535 if (**default_goal_name == '\0' && set_default)
1536 {
1537 const char *name;
1538 struct dep *d;
1539 struct nameseq *t = filenames;
1540
1541 for (; t != 0; t = t->next)
1542 {
1543 int reject = 0;
1544 name = t->name;
1545
1546 /* We have nothing to do if this is an implicit rule. */
1547 if (strchr (name, '%') != 0)
1548 break;
1549
1550 /* See if this target's name does not start with a `.',
1551 unless it contains a slash. */
1552 if (*name == '.' && strchr (name, '/') == 0
1553#ifdef HAVE_DOS_PATHS
1554 && strchr (name, '\\') == 0
1555#endif
1556 )
1557 continue;
1558
1559
1560 /* If this file is a suffix, don't let it be
1561 the default goal file. */
1562 for (d = suffix_file->deps; d != 0; d = d->next)
1563 {
1564 register struct dep *d2;
1565 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1566 {
1567 reject = 1;
1568 break;
1569 }
1570 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1571 {
1572 unsigned int l = strlen (dep_name (d2));
1573 if (!strneq (name, dep_name (d2), l))
1574 continue;
1575 if (streq (name + l, dep_name (d)))
1576 {
1577 reject = 1;
1578 break;
1579 }
1580 }
1581
1582 if (reject)
1583 break;
1584 }
1585
1586 if (!reject)
1587 {
1588 define_variable_global (".DEFAULT_GOAL", 13, t->name,
1589 o_file, 0, NILF);
1590 break;
1591 }
1592 }
1593 }
1594
1595 continue;
1596 }
1597
1598 /* We get here except in the case that we just read a rule line.
1599 Record now the last rule we read, so following spurious
1600 commands are properly diagnosed. */
1601 rule_complete:
1602 record_waiting_files ();
1603 }
1604
1605#undef word1eq
1606
1607 if (conditionals->if_cmds)
1608 fatal (fstart, _("missing `endif'"));
1609
1610 /* At eof, record the last rule. */
1611 record_waiting_files ();
1612
1613 if (collapsed)
1614 free (collapsed);
1615 free (commands);
1616
1617 return 1;
1618}
1619
1620
1621
1622/* Remove comments from LINE.
1623 This is done by copying the text at LINE onto itself. */
1624
1625static void
1626remove_comments (char *line)
1627{
1628 char *comment;
1629
1630 comment = find_char_unquote (line, '#', 0, 0, 0);
1631
1632 if (comment != 0)
1633 /* Cut off the line at the #. */
1634 *comment = '\0';
1635}
1636
1637/* Execute a `define' directive.
1638 The first line has already been read, and NAME is the name of
1639 the variable to be defined. The following lines remain to be read. */
1640
1641static void
1642do_define (char *name, unsigned int namelen,
1643 enum variable_origin origin, struct ebuffer *ebuf)
1644{
1645 struct floc defstart;
1646 long nlines = 0;
1647 int nlevels = 1;
1648 unsigned int length = 100;
1649 char *definition = xmalloc (length);
1650 unsigned int idx = 0;
1651 char *p;
1652
1653 /* Expand the variable name. */
1654 char *var = alloca (namelen + 1);
1655 memcpy (var, name, namelen);
1656 var[namelen] = '\0';
1657 var = variable_expand (var);
1658
1659 defstart = ebuf->floc;
1660
1661 while (1)
1662 {
1663 unsigned int len;
1664 char *line;
1665
1666 nlines = readline (ebuf);
1667 ebuf->floc.lineno += nlines;
1668
1669 /* If there is nothing left to eval, we're done. */
1670 if (nlines < 0)
1671 break;
1672
1673 line = ebuf->buffer;
1674
1675 collapse_continuations (line);
1676
1677 /* If the line doesn't begin with a tab, test to see if it introduces
1678 another define, or ends one. */
1679
1680 /* Stop if we find an 'endef' */
1681 if (line[0] != cmd_prefix)
1682 {
1683 p = next_token (line);
1684 len = strlen (p);
1685
1686 /* If this is another 'define', increment the level count. */
1687 if ((len == 6 || (len > 6 && isblank ((unsigned char)p[6])))
1688 && strneq (p, "define", 6))
1689 ++nlevels;
1690
1691 /* If this is an 'endef', decrement the count. If it's now 0,
1692 we've found the last one. */
1693 else if ((len == 5 || (len > 5 && isblank ((unsigned char)p[5])))
1694 && strneq (p, "endef", 5))
1695 {
1696 p += 5;
1697 remove_comments (p);
1698 if (*next_token (p) != '\0')
1699 error (&ebuf->floc,
1700 _("Extraneous text after `endef' directive"));
1701
1702 if (--nlevels == 0)
1703 {
1704 /* Define the variable. */
1705 if (idx == 0)
1706 definition[0] = '\0';
1707 else
1708 definition[idx - 1] = '\0';
1709
1710 /* Always define these variables in the global set. */
1711 define_variable_global (var, strlen (var), definition,
1712 origin, 1, &defstart);
1713 free (definition);
1714 return;
1715 }
1716 }
1717 }
1718
1719 /* Otherwise add this line to the variable definition. */
1720 len = strlen (line);
1721 if (idx + len + 1 > length)
1722 {
1723 length = (idx + len) * 2;
1724 definition = xrealloc (definition, length + 1);
1725 }
1726
1727 memcpy (&definition[idx], line, len);
1728 idx += len;
1729 /* Separate lines with a newline. */
1730 definition[idx++] = '\n';
1731 }
1732
1733 /* No `endef'!! */
1734 fatal (&defstart, _("missing `endef', unterminated `define'"));
1735
1736 /* NOTREACHED */
1737 return;
1738}
1739
1740
1741/* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1742 "ifneq", "if1of", "ifn1of", "else" and "endif".
1743 LINE is the input line, with the command as its first word.
1744
1745 FILENAME and LINENO are the filename and line number in the
1746 current makefile. They are used for error messages.
1747
1748 Value is -2 if the line is not a conditional at all,
1749 -1 if the line is an invalid conditional,
1750 0 if following text should be interpreted,
1751 1 if following text should be ignored. */
1752
1753static int
1754conditional_line (char *line, int len, const struct floc *flocp)
1755{
1756 char *cmdname;
1757#ifdef CONFIG_WITH_SET_CONDITIONALS
1758 enum { c_ifdef, c_ifndef, c_ifeq, c_ifneq, c_if1of, c_ifn1of, c_else, c_endif } cmdtype;
1759#else
1760 enum { c_ifdef, c_ifndef, c_ifeq, c_ifneq, c_else, c_endif } cmdtype;
1761#endif
1762 unsigned int i;
1763 unsigned int o;
1764
1765 /* Compare a word, both length and contents. */
1766#define word1eq(s) (len == sizeof(s)-1 && strneq (s, line, sizeof(s)-1))
1767#define chkword(s, t) if (word1eq (s)) { cmdtype = (t); cmdname = (s); }
1768
1769 /* Make sure this line is a conditional. */
1770 chkword ("ifdef", c_ifdef)
1771 else chkword ("ifndef", c_ifndef)
1772 else chkword ("ifeq", c_ifeq)
1773 else chkword ("ifneq", c_ifneq)
1774#ifdef CONFIG_WITH_SET_CONDITIONALS
1775 else chkword ("if1of", c_if1of)
1776 else chkword ("ifn1of", c_ifn1of)
1777#endif
1778 else chkword ("else", c_else)
1779 else chkword ("endif", c_endif)
1780 else
1781 return -2;
1782
1783 /* Found one: skip past it and any whitespace after it. */
1784 line = next_token (line + len);
1785
1786#define EXTRANEOUS() error (flocp, _("Extraneous text after `%s' directive"), cmdname)
1787
1788 /* An 'endif' cannot contain extra text, and reduces the if-depth by 1 */
1789 if (cmdtype == c_endif)
1790 {
1791 if (*line != '\0')
1792 EXTRANEOUS ();
1793
1794 if (!conditionals->if_cmds)
1795 fatal (flocp, _("extraneous `%s'"), cmdname);
1796
1797 --conditionals->if_cmds;
1798
1799 goto DONE;
1800 }
1801
1802 /* An 'else' statement can either be simple, or it can have another
1803 conditional after it. */
1804 if (cmdtype == c_else)
1805 {
1806 const char *p;
1807
1808 if (!conditionals->if_cmds)
1809 fatal (flocp, _("extraneous `%s'"), cmdname);
1810
1811 o = conditionals->if_cmds - 1;
1812
1813 if (conditionals->seen_else[o])
1814 fatal (flocp, _("only one `else' per conditional"));
1815
1816 /* Change the state of ignorance. */
1817 switch (conditionals->ignoring[o])
1818 {
1819 case 0:
1820 /* We've just been interpreting. Never do it again. */
1821 conditionals->ignoring[o] = 2;
1822 break;
1823 case 1:
1824 /* We've never interpreted yet. Maybe this time! */
1825 conditionals->ignoring[o] = 0;
1826 break;
1827 }
1828
1829 /* It's a simple 'else'. */
1830 if (*line == '\0')
1831 {
1832 conditionals->seen_else[o] = 1;
1833 goto DONE;
1834 }
1835
1836 /* The 'else' has extra text. That text must be another conditional
1837 and cannot be an 'else' or 'endif'. */
1838
1839 /* Find the length of the next word. */
1840 for (p = line+1; *p != '\0' && !isspace ((unsigned char)*p); ++p)
1841 ;
1842 len = p - line;
1843
1844 /* If it's 'else' or 'endif' or an illegal conditional, fail. */
1845 if (word1eq("else") || word1eq("endif")
1846 || conditional_line (line, len, flocp) < 0)
1847 EXTRANEOUS ();
1848 else
1849 {
1850 /* conditional_line() created a new level of conditional.
1851 Raise it back to this level. */
1852 if (conditionals->ignoring[o] < 2)
1853 conditionals->ignoring[o] = conditionals->ignoring[o+1];
1854 --conditionals->if_cmds;
1855 }
1856
1857 goto DONE;
1858 }
1859
1860 if (conditionals->allocated == 0)
1861 {
1862 conditionals->allocated = 5;
1863 conditionals->ignoring = xmalloc (conditionals->allocated);
1864 conditionals->seen_else = xmalloc (conditionals->allocated);
1865 }
1866
1867 o = conditionals->if_cmds++;
1868 if (conditionals->if_cmds > conditionals->allocated)
1869 {
1870 conditionals->allocated += 5;
1871 conditionals->ignoring = xrealloc (conditionals->ignoring,
1872 conditionals->allocated);
1873 conditionals->seen_else = xrealloc (conditionals->seen_else,
1874 conditionals->allocated);
1875 }
1876
1877 /* Record that we have seen an `if...' but no `else' so far. */
1878 conditionals->seen_else[o] = 0;
1879
1880 /* Search through the stack to see if we're already ignoring. */
1881 for (i = 0; i < o; ++i)
1882 if (conditionals->ignoring[i])
1883 {
1884 /* We are already ignoring, so just push a level to match the next
1885 "else" or "endif", and keep ignoring. We don't want to expand
1886 variables in the condition. */
1887 conditionals->ignoring[o] = 1;
1888 return 1;
1889 }
1890
1891 if (cmdtype == c_ifdef || cmdtype == c_ifndef)
1892 {
1893 char *var;
1894 struct variable *v;
1895 char *p;
1896
1897 /* Expand the thing we're looking up, so we can use indirect and
1898 constructed variable names. */
1899 var = allocated_variable_expand (line);
1900
1901 /* Make sure there's only one variable name to test. */
1902 p = end_of_token (var);
1903 i = p - var;
1904 p = next_token (p);
1905 if (*p != '\0')
1906 return -1;
1907
1908 var[i] = '\0';
1909 v = lookup_variable (var, i);
1910
1911 conditionals->ignoring[o] =
1912 ((v != 0 && *v->value != '\0') == (cmdtype == c_ifndef));
1913
1914 free (var);
1915 }
1916 else
1917 {
1918#ifdef CONFIG_WITH_SET_CONDITIONALS
1919 /* "ifeq", "ifneq", "if1of" or "ifn1of". */
1920#else
1921 /* "ifeq" or "ifneq". */
1922#endif
1923 char *s1, *s2;
1924 unsigned int l;
1925 char termin = *line == '(' ? ',' : *line;
1926
1927 if (termin != ',' && termin != '"' && termin != '\'')
1928 return -1;
1929
1930 s1 = ++line;
1931 /* Find the end of the first string. */
1932 if (termin == ',')
1933 {
1934 int count = 0;
1935 for (; *line != '\0'; ++line)
1936 if (*line == '(')
1937 ++count;
1938 else if (*line == ')')
1939 --count;
1940 else if (*line == ',' && count <= 0)
1941 break;
1942 }
1943 else
1944 while (*line != '\0' && *line != termin)
1945 ++line;
1946
1947 if (*line == '\0')
1948 return -1;
1949
1950 if (termin == ',')
1951 {
1952 /* Strip blanks after the first string. */
1953 char *p = line++;
1954 while (isblank ((unsigned char)p[-1]))
1955 --p;
1956 *p = '\0';
1957 }
1958 else
1959 *line++ = '\0';
1960
1961 s2 = variable_expand (s1);
1962 /* We must allocate a new copy of the expanded string because
1963 variable_expand re-uses the same buffer. */
1964 l = strlen (s2);
1965 s1 = alloca (l + 1);
1966 memcpy (s1, s2, l + 1);
1967
1968 if (termin != ',')
1969 /* Find the start of the second string. */
1970 line = next_token (line);
1971
1972 termin = termin == ',' ? ')' : *line;
1973 if (termin != ')' && termin != '"' && termin != '\'')
1974 return -1;
1975
1976 /* Find the end of the second string. */
1977 if (termin == ')')
1978 {
1979 int count = 0;
1980 s2 = next_token (line);
1981 for (line = s2; *line != '\0'; ++line)
1982 {
1983 if (*line == '(')
1984 ++count;
1985 else if (*line == ')')
1986 {
1987 if (count <= 0)
1988 break;
1989 else
1990 --count;
1991 }
1992 }
1993 }
1994 else
1995 {
1996 ++line;
1997 s2 = line;
1998 while (*line != '\0' && *line != termin)
1999 ++line;
2000 }
2001
2002 if (*line == '\0')
2003 return -1;
2004
2005 *line = '\0';
2006 line = next_token (++line);
2007 if (*line != '\0')
2008 EXTRANEOUS ();
2009
2010 s2 = variable_expand (s2);
2011#ifdef CONFIG_WITH_SET_CONDITIONALS
2012 if (cmdtype == c_if1of || cmdtype == c_ifn1of)
2013 {
2014 const char *s1_cur;
2015 unsigned int s1_len;
2016 const char *s1_iterator = s1;
2017
2018 conditionals->ignoring[o] = (cmdtype == c_if1of); /* if not found */
2019 while ((s1_cur = find_next_token (&s1_iterator, &s1_len)) != 0)
2020 {
2021 const char *s2_cur;
2022 unsigned int s2_len;
2023 const char *s2_iterator = s2;
2024 while ((s2_cur = find_next_token (&s2_iterator, &s2_len)) != 0)
2025 if (s2_len == s1_len
2026 && strneq (s2_cur, s1_cur, s1_len) )
2027 {
2028 conditionals->ignoring[o] = (cmdtype != c_if1of); /* found */
2029 break;
2030 }
2031 }
2032 }
2033 else
2034 conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
2035#else
2036 conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
2037#endif
2038 }
2039
2040 DONE:
2041 /* Search through the stack to see if we're ignoring. */
2042 for (i = 0; i < conditionals->if_cmds; ++i)
2043 if (conditionals->ignoring[i])
2044 return 1;
2045 return 0;
2046}
2047
2048
2049/* Remove duplicate dependencies in CHAIN. */
2050
2051static unsigned long
2052dep_hash_1 (const void *key)
2053{
2054 return_STRING_HASH_1 (dep_name ((struct dep const *) key));
2055}
2056
2057static unsigned long
2058dep_hash_2 (const void *key)
2059{
2060 return_STRING_HASH_2 (dep_name ((struct dep const *) key));
2061}
2062
2063static int
2064dep_hash_cmp (const void *x, const void *y)
2065{
2066 struct dep *dx = (struct dep *) x;
2067 struct dep *dy = (struct dep *) y;
2068 int cmp = strcmp (dep_name (dx), dep_name (dy));
2069
2070 /* If the names are the same but ignore_mtimes are not equal, one of these
2071 is an order-only prerequisite and one isn't. That means that we should
2072 remove the one that isn't and keep the one that is. */
2073
2074 if (!cmp && dx->ignore_mtime != dy->ignore_mtime)
2075 dx->ignore_mtime = dy->ignore_mtime = 0;
2076
2077 return cmp;
2078}
2079
2080
2081void
2082uniquize_deps (struct dep *chain)
2083{
2084 struct hash_table deps;
2085 register struct dep **depp;
2086
2087 hash_init (&deps, 500, dep_hash_1, dep_hash_2, dep_hash_cmp);
2088
2089 /* Make sure that no dependencies are repeated. This does not
2090 really matter for the purpose of updating targets, but it
2091 might make some names be listed twice for $^ and $?. */
2092
2093 depp = &chain;
2094 while (*depp)
2095 {
2096 struct dep *dep = *depp;
2097 struct dep **dep_slot = (struct dep **) hash_find_slot (&deps, dep);
2098 if (HASH_VACANT (*dep_slot))
2099 {
2100 hash_insert_at (&deps, dep, dep_slot);
2101 depp = &dep->next;
2102 }
2103 else
2104 {
2105 /* Don't bother freeing duplicates.
2106 It's dangerous and little benefit accrues. */
2107 *depp = dep->next;
2108 }
2109 }
2110
2111 hash_free (&deps, 0);
2112}
2113
2114
2115/* Record target-specific variable values for files FILENAMES.
2116 TWO_COLON is nonzero if a double colon was used.
2117
2118 The links of FILENAMES are freed, and so are any names in it
2119 that are not incorporated into other data structures.
2120
2121 If the target is a pattern, add the variable to the pattern-specific
2122 variable value list. */
2123
2124static void
2125record_target_var (struct nameseq *filenames, char *defn,
2126 enum variable_origin origin, int exported,
2127 const struct floc *flocp)
2128{
2129 struct nameseq *nextf;
2130 struct variable_set_list *global;
2131
2132 global = current_variable_set_list;
2133
2134 /* If the variable is an append version, store that but treat it as a
2135 normal recursive variable. */
2136
2137 for (; filenames != 0; filenames = nextf)
2138 {
2139 struct variable *v;
2140 const char *name = filenames->name;
2141 const char *fname;
2142 const char *percent;
2143 struct pattern_var *p;
2144
2145 nextf = filenames->next;
2146 free (filenames);
2147
2148 /* If it's a pattern target, then add it to the pattern-specific
2149 variable list. */
2150 percent = find_percent_cached (&name);
2151 if (percent)
2152 {
2153 /* Get a reference for this pattern-specific variable struct. */
2154 p = create_pattern_var (name, percent);
2155 p->variable.fileinfo = *flocp;
2156 /* I don't think this can fail since we already determined it was a
2157 variable definition. */
2158 v = parse_variable_definition (&p->variable, defn);
2159 assert (v != 0);
2160
2161 if (v->flavor == f_simple)
2162 v->value = allocated_variable_expand (v->value);
2163 else
2164 v->value = xstrdup (v->value);
2165
2166 fname = p->target;
2167 }
2168 else
2169 {
2170 struct file *f;
2171
2172 /* Get a file reference for this file, and initialize it.
2173 We don't want to just call enter_file() because that allocates a
2174 new entry if the file is a double-colon, which we don't want in
2175 this situation. */
2176 f = lookup_file (name);
2177 if (!f)
2178 f = enter_file (strcache_add (name));
2179 else if (f->double_colon)
2180 f = f->double_colon;
2181
2182 initialize_file_variables (f, 1);
2183 fname = f->name;
2184
2185 current_variable_set_list = f->variables;
2186 v = try_variable_definition (flocp, defn, origin, 1);
2187 if (!v)
2188 error (flocp, _("Malformed target-specific variable definition"));
2189 current_variable_set_list = global;
2190 }
2191
2192 /* Set up the variable to be *-specific. */
2193 v->origin = origin;
2194 v->per_target = 1;
2195 v->export = exported ? v_export : v_default;
2196
2197 /* If it's not an override, check to see if there was a command-line
2198 setting. If so, reset the value. */
2199 if (origin != o_override)
2200 {
2201 struct variable *gv;
2202 int len = strlen(v->name);
2203
2204 gv = lookup_variable (v->name, len);
2205 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
2206 {
2207 if (v->value != 0)
2208 free (v->value);
2209 v->value = xstrdup (gv->value);
2210 v->origin = gv->origin;
2211 v->recursive = gv->recursive;
2212 v->append = 0;
2213 }
2214 }
2215 }
2216}
2217
2218
2219/* Record a description line for files FILENAMES,
2220 with dependencies DEPS, commands to execute described
2221 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
2222 TWO_COLON is nonzero if a double colon was used.
2223 If not nil, PATTERN is the `%' pattern to make this
2224 a static pattern rule, and PATTERN_PERCENT is a pointer
2225 to the `%' within it.
2226
2227 The links of FILENAMES are freed, and so are any names in it
2228 that are not incorporated into other data structures. */
2229
2230static void
2231record_files (struct nameseq *filenames, const char *pattern,
2232 const char *pattern_percent, struct dep *deps,
2233 unsigned int cmds_started, char *commands,
2234 unsigned int commands_idx, int two_colon,
2235 const struct floc *flocp)
2236{
2237 struct nameseq *nextf;
2238 int implicit = 0;
2239 unsigned int max_targets = 0, target_idx = 0;
2240 const char **targets = 0, **target_percents = 0;
2241 struct commands *cmds;
2242#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2243 struct file *prev_file = 0;
2244 enum multitarget_mode { m_unsettled, m_no, m_yes, m_yes_maybe }
2245 multi_mode = !two_colon && !pattern ? m_unsettled : m_no;
2246#endif
2247
2248 /* If we've already snapped deps, that means we're in an eval being
2249 resolved after the makefiles have been read in. We can't add more rules
2250 at this time, since they won't get snapped and we'll get core dumps.
2251 See Savannah bug # 12124. */
2252 if (snapped_deps)
2253 fatal (flocp, _("prerequisites cannot be defined in command scripts"));
2254
2255 if (commands_idx > 0)
2256 {
2257 cmds = xmalloc (sizeof (struct commands));
2258 cmds->fileinfo.filenm = flocp->filenm;
2259 cmds->fileinfo.lineno = cmds_started;
2260 cmds->commands = savestring (commands, commands_idx);
2261 cmds->command_lines = 0;
2262 }
2263 else
2264 cmds = 0;
2265
2266 for (; filenames != 0; filenames = nextf)
2267 {
2268 const char *name = filenames->name;
2269 struct file *f;
2270 struct dep *this = 0;
2271 const char *implicit_percent;
2272
2273 nextf = filenames->next;
2274 free (filenames);
2275
2276 /* Check for special targets. Do it here instead of, say, snap_deps()
2277 so that we can immediately use the value. */
2278
2279 if (streq (name, ".POSIX"))
2280 posix_pedantic = 1;
2281 else if (streq (name, ".SECONDEXPANSION"))
2282 second_expansion = 1;
2283
2284 implicit_percent = find_percent_cached (&name);
2285 implicit |= implicit_percent != 0;
2286
2287 if (implicit)
2288 {
2289 if (pattern != 0)
2290 fatal (flocp, _("mixed implicit and static pattern rules"));
2291
2292 if (implicit_percent == 0)
2293 fatal (flocp, _("mixed implicit and normal rules"));
2294
2295 if (targets == 0)
2296 {
2297 max_targets = 5;
2298 targets = xmalloc (5 * sizeof (char *));
2299 target_percents = xmalloc (5 * sizeof (char *));
2300 target_idx = 0;
2301 }
2302 else if (target_idx == max_targets - 1)
2303 {
2304 max_targets += 5;
2305 targets = xrealloc ((void *)targets, max_targets * sizeof (char *));
2306 target_percents = xrealloc ((void *)target_percents,
2307 max_targets * sizeof (char *));
2308 }
2309 targets[target_idx] = name;
2310 target_percents[target_idx] = implicit_percent;
2311 ++target_idx;
2312 continue;
2313 }
2314
2315#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2316 /* Check for the explicit multitarget mode operators. For this to be
2317 identified as an explicit multiple target rule, the first + or +|
2318 operator *must* appear between the first two files. If not found as
2319 the 2nd file or if found as the 1st file, the rule will be rejected
2320 as a potential multiple first target rule. For the subsequent files
2321 the operator is only required to switch between maybe and non-maybe
2322 mode:
2323 `primary + 2nd 3rd +| 4th-maybe + 5th-for-sure: deps; cmds'
2324
2325 The whole idea of the maybe-updated files is this:
2326 timestamp +| maybe.h: src1.c src2.c
2327 grep goes-into-maybe.h $* > timestamp
2328 cmp timestamp maybe.h || cp -f timestamp maybe.h
2329
2330 This is implemented in remake.c where we don't consider the mtime of
2331 the maybe-updated targets. */
2332 if (multi_mode != m_no && name[0] == '+'
2333 && (name[1] == '\0' || (name[1] == '|' && name[2] == '\0')))
2334 {
2335 if (!prev_file)
2336 multi_mode = m_no; /* first */
2337 else
2338 {
2339 if (multi_mode == m_unsettled)
2340 {
2341 prev_file->multi_head = prev_file;
2342
2343 /* Only the primary file needs the dependencies. */
2344 if (deps)
2345 {
2346 free_dep_chain (deps);
2347 deps = NULL;
2348 }
2349 }
2350 multi_mode = name[1] == '\0' ? m_yes : m_yes_maybe;
2351 continue;
2352 }
2353 }
2354 else if (multi_mode == m_unsettled && prev_file)
2355 multi_mode = m_no;
2356#endif
2357
2358 /* If this is a static pattern rule:
2359 `targets: target%pattern: dep%pattern; cmds',
2360 make sure the pattern matches this target name. */
2361 if (pattern && !pattern_matches (pattern, pattern_percent, name))
2362 error (flocp, _("target `%s' doesn't match the target pattern"), name);
2363 else if (deps)
2364 {
2365 /* If there are multiple filenames, copy the chain DEPS for all but
2366 the last one. It is not safe for the same deps to go in more
2367 than one place in the database. */
2368 this = nextf != 0 ? copy_dep_chain (deps) : deps;
2369 this->need_2nd_expansion = (second_expansion
2370 && strchr (this->name, '$'));
2371 }
2372
2373 if (!two_colon)
2374 {
2375 /* Single-colon. Combine these dependencies
2376 with others in file's existing record, if any. */
2377 f = enter_file (strcache_add (name));
2378
2379 if (f->double_colon)
2380 fatal (flocp,
2381 _("target file `%s' has both : and :: entries"), f->name);
2382
2383 /* If CMDS == F->CMDS, this target was listed in this rule
2384 more than once. Just give a warning since this is harmless. */
2385 if (cmds != 0 && cmds == f->cmds)
2386 error (flocp,
2387 _("target `%s' given more than once in the same rule."),
2388 f->name);
2389
2390 /* Check for two single-colon entries both with commands.
2391 Check is_target so that we don't lose on files such as .c.o
2392 whose commands were preinitialized. */
2393 else if (cmds != 0 && f->cmds != 0 && f->is_target)
2394 {
2395 error (&cmds->fileinfo,
2396 _("warning: overriding commands for target `%s'"),
2397 f->name);
2398 error (&f->cmds->fileinfo,
2399 _("warning: ignoring old commands for target `%s'"),
2400 f->name);
2401 }
2402
2403 f->is_target = 1;
2404
2405 /* Defining .DEFAULT with no deps or cmds clears it. */
2406 if (f == default_file && this == 0 && cmds == 0)
2407 f->cmds = 0;
2408 if (cmds != 0)
2409 f->cmds = cmds;
2410
2411#ifdef CONFIG_WITH_EXPLICIT_MULTITARGET
2412 /* If this is an explicit multi target rule, add it to the
2413 target chain and set the multi_maybe flag according to
2414 the current mode. */
2415
2416 if (multi_mode >= m_yes)
2417 {
2418 f->multi_maybe = multi_mode == m_yes_maybe;
2419 prev_file->multi_next = f;
2420 assert (prev_file->multi_head != 0);
2421 f->multi_head = prev_file->multi_head;
2422
2423 if (f == suffix_file)
2424 error (flocp,
2425 _(".SUFFIXES encountered in an explicit multi target rule"));
2426 }
2427 prev_file = f;
2428#endif
2429
2430 /* Defining .SUFFIXES with no dependencies clears out the list of
2431 suffixes. */
2432 if (f == suffix_file && this == 0)
2433 {
2434 free_dep_chain (f->deps);
2435 f->deps = 0;
2436 }
2437 else if (this != 0)
2438 {
2439 /* Add the file's old deps and the new ones in THIS together. */
2440
2441 if (f->deps != 0)
2442 {
2443 struct dep **d_ptr = &f->deps;
2444
2445 while ((*d_ptr)->next != 0)
2446 d_ptr = &(*d_ptr)->next;
2447
2448 if (cmds != 0)
2449 /* This is the rule with commands, so put its deps
2450 last. The rationale behind this is that $< expands to
2451 the first dep in the chain, and commands use $<
2452 expecting to get the dep that rule specifies. However
2453 the second expansion algorithm reverses the order thus
2454 we need to make it last here. */
2455 (*d_ptr)->next = this;
2456 else
2457 {
2458 /* This is the rule without commands. Put its
2459 dependencies at the end but before dependencies from
2460 the rule with commands (if any). This way everything
2461 appears in makefile order. */
2462
2463 if (f->cmds != 0)
2464 {
2465 this->next = *d_ptr;
2466 *d_ptr = this;
2467 }
2468 else
2469 (*d_ptr)->next = this;
2470 }
2471 }
2472 else
2473 f->deps = this;
2474
2475 /* This is a hack. I need a way to communicate to snap_deps()
2476 that the last dependency line in this file came with commands
2477 (so that logic in snap_deps() can put it in front and all
2478 this $< -logic works). I cannot simply rely on file->cmds
2479 being not 0 because of the cases like the following:
2480
2481 foo: bar
2482 foo:
2483 ...
2484
2485 I am going to temporarily "borrow" UPDATING member in
2486 `struct file' for this. */
2487
2488 if (cmds != 0)
2489 f->updating = 1;
2490 }
2491 }
2492 else
2493 {
2494 /* Double-colon. Make a new record even if there already is one. */
2495 f = lookup_file (name);
2496
2497 /* Check for both : and :: rules. Check is_target so
2498 we don't lose on default suffix rules or makefiles. */
2499 if (f != 0 && f->is_target && !f->double_colon)
2500 fatal (flocp,
2501 _("target file `%s' has both : and :: entries"), f->name);
2502 f = enter_file (strcache_add (name));
2503 /* If there was an existing entry and it was a double-colon entry,
2504 enter_file will have returned a new one, making it the prev
2505 pointer of the old one, and setting its double_colon pointer to
2506 the first one. */
2507 if (f->double_colon == 0)
2508 /* This is the first entry for this name, so we must set its
2509 double_colon pointer to itself. */
2510 f->double_colon = f;
2511 f->is_target = 1;
2512 f->deps = this;
2513 f->cmds = cmds;
2514 }
2515
2516 /* If this is a static pattern rule, set the stem to the part of its
2517 name that matched the `%' in the pattern, so you can use $* in the
2518 commands. */
2519 if (pattern)
2520 {
2521 static const char *percent = "%";
2522 char *buffer = variable_expand ("");
2523 const size_t buffer_offset = buffer - variable_buffer; /* bird */
2524 char *o = patsubst_expand_pat (buffer, name, pattern, percent,
2525 pattern_percent+1, percent+1);
2526 buffer = variable_buffer + buffer_offset; /* bird - variable_buffer may have been reallocated. */
2527 f->stem = strcache_add_len (buffer, o - buffer);
2528 if (this)
2529 {
2530 this->staticpattern = 1;
2531 this->stem = f->stem;
2532 }
2533 }
2534
2535 name = f->name;
2536
2537 /* If this target is a default target, update DEFAULT_GOAL_FILE. */
2538 if (streq (*default_goal_name, name)
2539 && (default_goal_file == 0
2540 || ! streq (default_goal_file->name, name)))
2541 default_goal_file = f;
2542 }
2543
2544 if (implicit)
2545 {
2546 if (deps)
2547 deps->need_2nd_expansion = second_expansion;
2548 create_pattern_rule (targets, target_percents, target_idx,
2549 two_colon, deps, cmds, 1);
2550 }
2551}
2552
2553
2554/* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2555 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2556 Quoting backslashes are removed from STRING by compacting it into
2557 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2558 one, or nil if there are none. STOPCHARs inside variable references are
2559 ignored if IGNOREVARS is true.
2560
2561 STOPCHAR _cannot_ be '$' if IGNOREVARS is true. */
2562
2563static char *
2564find_char_unquote (char *string, int stop1, int stop2, int blank,
2565 int ignorevars)
2566{
2567 unsigned int string_len = 0;
2568 char *p = string;
2569 register int ch; /* bird: 'optimiziations' */
2570
2571 if (ignorevars)
2572 ignorevars = '$';
2573
2574 while (1)
2575 {
2576 if (stop2 && blank)
2577 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2
2578 && ! isblank ((unsigned char) ch))
2579 ++p;
2580 else if (stop2)
2581 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1 && ch != stop2)
2582 ++p;
2583 else if (blank)
2584 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1
2585 && ! isblank ((unsigned char) ch))
2586 ++p;
2587 else
2588 while ((ch = *p) != '\0' && ch != ignorevars && ch != stop1)
2589 ++p;
2590
2591 if (ch == '\0')
2592 break;
2593
2594 /* If we stopped due to a variable reference, skip over its contents. */
2595 if (ch == ignorevars)
2596 {
2597 char openparen = p[1];
2598
2599 p += 2;
2600
2601 /* Skip the contents of a non-quoted, multi-char variable ref. */
2602 if (openparen == '(' || openparen == '{')
2603 {
2604 unsigned int pcount = 1;
2605 char closeparen = (openparen == '(' ? ')' : '}');
2606
2607 while ((ch = *p))
2608 {
2609 if (ch == openparen)
2610 ++pcount;
2611 else if (ch == closeparen)
2612 if (--pcount == 0)
2613 {
2614 ++p;
2615 break;
2616 }
2617 ++p;
2618 }
2619 }
2620
2621 /* Skipped the variable reference: look for STOPCHARS again. */
2622 continue;
2623 }
2624
2625 if (p > string && p[-1] == '\\')
2626 {
2627 /* Search for more backslashes. */
2628 int i = -2;
2629 while (&p[i] >= string && p[i] == '\\')
2630 --i;
2631 ++i;
2632 /* Only compute the length if really needed. */
2633 if (string_len == 0)
2634 string_len = strlen (string);
2635 /* The number of backslashes is now -I.
2636 Copy P over itself to swallow half of them. */
2637 memmove (&p[i], &p[i/2], (string_len - (p - string)) - (i/2) + 1);
2638 p += i/2;
2639 if (i % 2 == 0)
2640 /* All the backslashes quoted each other; the STOPCHAR was
2641 unquoted. */
2642 return p;
2643
2644 /* The STOPCHAR was quoted by a backslash. Look for another. */
2645 }
2646 else
2647 /* No backslash in sight. */
2648 return p;
2649 }
2650
2651 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2652 return 0;
2653}
2654
2655/* Search PATTERN for an unquoted % and handle quoting. */
2656
2657char *
2658find_percent (char *pattern)
2659{
2660 return find_char_unquote (pattern, '%', 0, 0, 0);
2661}
2662
2663/* Search STRING for an unquoted % and handle quoting. Returns a pointer to
2664 the % or NULL if no % was found.
2665 This version is used with strings in the string cache: if there's a need to
2666 modify the string a new version will be added to the string cache and
2667 *STRING will be set to that. */
2668
2669const char *
2670find_percent_cached (const char **string)
2671{
2672 const char *p = *string;
2673 char *new = 0;
2674 int slen;
2675
2676 /* If the first char is a % return now. This lets us avoid extra tests
2677 inside the loop. */
2678 if (*p == '%')
2679 return p;
2680
2681 while (1)
2682 {
2683 while (*p != '\0' && *p != '%')
2684 ++p;
2685
2686 if (*p == '\0')
2687 break;
2688
2689 /* See if this % is escaped with a backslash; if not we're done. */
2690 if (p[-1] != '\\')
2691 break;
2692
2693 {
2694 /* Search for more backslashes. */
2695 char *pv;
2696 int i = -2;
2697
2698 while (&p[i] >= *string && p[i] == '\\')
2699 --i;
2700 ++i;
2701
2702 /* At this point we know we'll need to allocate a new string.
2703 Make a copy if we haven't yet done so. */
2704 if (! new)
2705 {
2706 slen = strlen (*string);
2707 new = alloca (slen + 1);
2708 memcpy (new, *string, slen + 1);
2709 p = new + (p - *string);
2710 *string = new;
2711 }
2712
2713 /* At this point *string, p, and new all point into the same string.
2714 Get a non-const version of p so we can modify new. */
2715 pv = new + (p - *string);
2716
2717 /* The number of backslashes is now -I.
2718 Copy P over itself to swallow half of them. */
2719 memmove (&pv[i], &pv[i/2], (slen - (pv - new)) - (i/2) + 1);
2720 p += i/2;
2721
2722 /* If the backslashes quoted each other; the % was unquoted. */
2723 if (i % 2 == 0)
2724 break;
2725 }
2726 }
2727
2728 /* If we had to change STRING, add it to the strcache. */
2729 if (new)
2730 {
2731 *string = strcache_add (*string);
2732 p = *string + (p - new);
2733 }
2734
2735 /* If we didn't find a %, return NULL. Otherwise return a ptr to it. */
2736 return (*p == '\0') ? NULL : p;
2737}
2738
2739
2740/* Parse a string into a sequence of filenames represented as a
2741 chain of struct nameseq's in reverse order and return that chain.
2742
2743 The string is passed as STRINGP, the address of a string pointer.
2744 The string pointer is updated to point at the first character
2745 not parsed, which either is a null char or equals STOPCHAR.
2746
2747 SIZE is how big to construct chain elements.
2748 This is useful if we want them actually to be other structures
2749 that have room for additional info.
2750
2751 If STRIP is nonzero, strip `./'s off the beginning. */
2752
2753struct nameseq *
2754parse_file_seq (char **stringp, int stopchar, unsigned int size, int strip)
2755{
2756 struct nameseq *new = 0;
2757 struct nameseq *new1;
2758#ifndef NO_ARCHIVES /* bird: MSC warning */
2759 struct nameseq *lastnew1;
2760#endif
2761 char *p = *stringp;
2762
2763#ifdef VMS
2764# define VMS_COMMA ','
2765#else
2766# define VMS_COMMA 0
2767#endif
2768
2769 while (1)
2770 {
2771 const char *name;
2772 char *q;
2773
2774 /* Skip whitespace; see if any more names are left. */
2775 p = next_token (p);
2776 if (*p == '\0')
2777 break;
2778 if (*p == stopchar)
2779 break;
2780
2781 /* There are, so find the end of the next name. */
2782 q = p;
2783 p = find_char_unquote (q, stopchar, VMS_COMMA, 1, 0);
2784#ifdef VMS
2785 /* convert comma separated list to space separated */
2786 if (p && *p == ',')
2787 *p =' ';
2788#endif
2789#ifdef _AMIGA
2790 if (stopchar == ':' && p && *p == ':'
2791 && !(isspace ((unsigned char)p[1]) || !p[1]
2792 || isspace ((unsigned char)p[-1])))
2793 p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1, 0);
2794#endif
2795#ifdef HAVE_DOS_PATHS
2796 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
2797 first colon which isn't followed by a slash or a backslash.
2798 Note that tokens separated by spaces should be treated as separate
2799 tokens since make doesn't allow path names with spaces */
2800 if (stopchar == ':')
2801 while (p != 0 && !isspace ((unsigned char)*p) &&
2802 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
2803 p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1, 0);
2804#endif
2805 if (p == 0)
2806 p = q + strlen (q);
2807
2808 if (strip)
2809#ifdef VMS
2810 /* Skip leading `[]'s. */
2811 while (p - q > 2 && q[0] == '[' && q[1] == ']')
2812#else
2813 /* Skip leading `./'s. */
2814 while (p - q > 2 && q[0] == '.' && q[1] == '/')
2815#endif
2816 {
2817 q += 2; /* Skip "./". */
2818 while (q < p && *q == '/')
2819 /* Skip following slashes: ".//foo" is "foo", not "/foo". */
2820 ++q;
2821 }
2822
2823 /* Extract the filename just found, and skip it. */
2824
2825 if (q == p)
2826 /* ".///" was stripped to "". */
2827#if defined(VMS)
2828 continue;
2829#elif defined(_AMIGA)
2830 name = "";
2831#else
2832 name = "./";
2833#endif
2834 else
2835#ifdef VMS
2836/* VMS filenames can have a ':' in them but they have to be '\'ed but we need
2837 * to remove this '\' before we can use the filename.
2838 * Savestring called because q may be read-only string constant.
2839 */
2840 {
2841 char *qbase = xstrdup (q);
2842 char *pbase = qbase + (p-q);
2843 char *q1 = qbase;
2844 char *q2 = q1;
2845 char *p1 = pbase;
2846
2847 while (q1 != pbase)
2848 {
2849 if (*q1 == '\\' && *(q1+1) == ':')
2850 {
2851 q1++;
2852 p1--;
2853 }
2854 *q2++ = *q1++;
2855 }
2856 name = strcache_add_len (qbase, p1 - qbase);
2857 free (qbase);
2858 }
2859#else
2860 name = strcache_add_len (q, p - q);
2861#endif
2862
2863 /* Add it to the front of the chain. */
2864 new1 = xmalloc (size);
2865 new1->name = name;
2866 new1->next = new;
2867 new = new1;
2868 }
2869
2870#ifndef NO_ARCHIVES
2871
2872 /* Look for multi-word archive references.
2873 They are indicated by a elt ending with an unmatched `)' and
2874 an elt further down the chain (i.e., previous in the file list)
2875 with an unmatched `(' (e.g., "lib(mem"). */
2876
2877 new1 = new;
2878 lastnew1 = 0;
2879 while (new1 != 0)
2880 if (new1->name[0] != '(' /* Don't catch "(%)" and suchlike. */
2881 && new1->name[strlen (new1->name) - 1] == ')'
2882 && strchr (new1->name, '(') == 0)
2883 {
2884 /* NEW1 ends with a `)' but does not contain a `('.
2885 Look back for an elt with an opening `(' but no closing `)'. */
2886
2887 struct nameseq *n = new1->next, *lastn = new1;
2888 char *paren = 0;
2889 while (n != 0 && (paren = strchr (n->name, '(')) == 0)
2890 {
2891 lastn = n;
2892 n = n->next;
2893 }
2894 if (n != 0
2895 /* Ignore something starting with `(', as that cannot actually
2896 be an archive-member reference (and treating it as such
2897 results in an empty file name, which causes much lossage). */
2898 && n->name[0] != '(')
2899 {
2900 /* N is the first element in the archive group.
2901 Its name looks like "lib(mem" (with no closing `)'). */
2902
2903 char *libname;
2904
2905 /* Copy "lib(" into LIBNAME. */
2906 ++paren;
2907 libname = alloca (paren - n->name + 1);
2908 memcpy (libname, n->name, paren - n->name);
2909 libname[paren - n->name] = '\0';
2910
2911 if (*paren == '\0')
2912 {
2913 /* N was just "lib(", part of something like "lib( a b)".
2914 Edit it out of the chain and free its storage. */
2915 lastn->next = n->next;
2916 free (n);
2917 /* LASTN->next is the new stopping elt for the loop below. */
2918 n = lastn->next;
2919 }
2920 else
2921 {
2922 /* Replace N's name with the full archive reference. */
2923 n->name = strcache_add (concat (libname, paren, ")"));
2924 }
2925
2926 if (new1->name[1] == '\0')
2927 {
2928 /* NEW1 is just ")", part of something like "lib(a b )".
2929 Omit it from the chain and free its storage. */
2930 if (lastnew1 == 0)
2931 new = new1->next;
2932 else
2933 lastnew1->next = new1->next;
2934 lastn = new1;
2935 new1 = new1->next;
2936 free (lastn);
2937 }
2938 else
2939 {
2940 /* Replace also NEW1->name, which already has closing `)'. */
2941 new1->name = strcache_add (concat (libname, new1->name, ""));
2942 new1 = new1->next;
2943 }
2944
2945 /* Trace back from NEW1 (the end of the list) until N
2946 (the beginning of the list), rewriting each name
2947 with the full archive reference. */
2948
2949 while (new1 != n)
2950 {
2951 new1->name = strcache_add (concat (libname, new1->name, ")"));
2952 lastnew1 = new1;
2953 new1 = new1->next;
2954 }
2955 }
2956 else
2957 {
2958 /* No frobnication happening. Just step down the list. */
2959 lastnew1 = new1;
2960 new1 = new1->next;
2961 }
2962 }
2963 else
2964 {
2965 lastnew1 = new1;
2966 new1 = new1->next;
2967 }
2968
2969#endif
2970
2971 *stringp = p;
2972 return new;
2973}
2974
2975
2976/* Find the next line of text in an eval buffer, combining continuation lines
2977 into one line.
2978 Return the number of actual lines read (> 1 if continuation lines).
2979 Returns -1 if there's nothing left in the buffer.
2980
2981 After this function, ebuf->buffer points to the first character of the
2982 line we just found.
2983 */
2984
2985/* Read a line of text from a STRING.
2986 Since we aren't really reading from a file, don't bother with linenumbers.
2987 */
2988
2989static unsigned long
2990readstring (struct ebuffer *ebuf)
2991{
2992 char *eol;
2993
2994 /* If there is nothing left in this buffer, return 0. */
2995 if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
2996 return -1;
2997
2998 /* Set up a new starting point for the buffer, and find the end of the
2999 next logical line (taking into account backslash/newline pairs). */
3000
3001 eol = ebuf->buffer = ebuf->bufnext;
3002
3003 while (1)
3004 {
3005 int backslash = 0;
3006 char *bol = eol;
3007 char *p;
3008
3009 /* Find the next newline. At EOS, stop. */
3010 eol = p = strchr (eol , '\n');
3011 if (!eol)
3012 {
3013 ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
3014 return 0;
3015 }
3016
3017 /* Found a newline; if it's escaped continue; else we're done. */
3018 while (p > bol && *(--p) == '\\')
3019 backslash = !backslash;
3020 if (!backslash)
3021 break;
3022 ++eol;
3023 }
3024
3025 /* Overwrite the newline char. */
3026 *eol = '\0';
3027 ebuf->bufnext = eol+1;
3028
3029 return 0;
3030}
3031
3032static long
3033readline (struct ebuffer *ebuf)
3034{
3035 char *p;
3036 char *end;
3037 char *start;
3038 long nlines = 0;
3039
3040 /* The behaviors between string and stream buffers are different enough to
3041 warrant different functions. Do the Right Thing. */
3042
3043 if (!ebuf->fp)
3044 return readstring (ebuf);
3045
3046 /* When reading from a file, we always start over at the beginning of the
3047 buffer for each new line. */
3048
3049 p = start = ebuf->bufstart;
3050 end = p + ebuf->size;
3051 *p = '\0';
3052
3053 while (fgets (p, end - p, ebuf->fp) != 0)
3054 {
3055 char *p2;
3056 unsigned long len;
3057 int backslash;
3058
3059 len = strlen (p);
3060 if (len == 0)
3061 {
3062 /* This only happens when the first thing on the line is a '\0'.
3063 It is a pretty hopeless case, but (wonder of wonders) Athena
3064 lossage strikes again! (xmkmf puts NULs in its makefiles.)
3065 There is nothing really to be done; we synthesize a newline so
3066 the following line doesn't appear to be part of this line. */
3067 error (&ebuf->floc,
3068 _("warning: NUL character seen; rest of line ignored"));
3069 p[0] = '\n';
3070 len = 1;
3071 }
3072
3073 /* Jump past the text we just read. */
3074 p += len;
3075
3076 /* If the last char isn't a newline, the whole line didn't fit into the
3077 buffer. Get some more buffer and try again. */
3078 if (p[-1] != '\n')
3079 goto more_buffer;
3080
3081 /* We got a newline, so add one to the count of lines. */
3082 ++nlines;
3083
3084#if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
3085 /* Check to see if the line was really ended with CRLF; if so ignore
3086 the CR. */
3087 if ((p - start) > 1 && p[-2] == '\r')
3088 {
3089 --p;
3090 p[-1] = '\n';
3091 }
3092#endif
3093
3094 backslash = 0;
3095 for (p2 = p - 2; p2 >= start; --p2)
3096 {
3097 if (*p2 != '\\')
3098 break;
3099 backslash = !backslash;
3100 }
3101
3102 if (!backslash)
3103 {
3104 p[-1] = '\0';
3105 break;
3106 }
3107
3108 /* It was a backslash/newline combo. If we have more space, read
3109 another line. */
3110 if (end - p >= 80)
3111 continue;
3112
3113 /* We need more space at the end of our buffer, so realloc it.
3114 Make sure to preserve the current offset of p. */
3115 more_buffer:
3116 {
3117 unsigned long off = p - start;
3118 ebuf->size *= 2;
3119 start = ebuf->buffer = ebuf->bufstart = xrealloc (start, ebuf->size);
3120 p = start + off;
3121 end = start + ebuf->size;
3122 *p = '\0';
3123 }
3124 }
3125
3126 if (ferror (ebuf->fp))
3127 pfatal_with_name (ebuf->floc.filenm);
3128
3129 /* If we found some lines, return how many.
3130 If we didn't, but we did find _something_, that indicates we read the last
3131 line of a file with no final newline; return 1.
3132 If we read nothing, we're at EOF; return -1. */
3133
3134 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
3135}
3136
3137
3138/* Parse the next "makefile word" from the input buffer, and return info
3139 about it.
3140
3141 A "makefile word" is one of:
3142
3143 w_bogus Should never happen
3144 w_eol End of input
3145 w_static A static word; cannot be expanded
3146 w_variable A word containing one or more variables/functions
3147 w_colon A colon
3148 w_dcolon A double-colon
3149 w_semicolon A semicolon
3150 w_varassign A variable assignment operator (=, :=, +=, >=, or ?=)
3151
3152 Note that this function is only used when reading certain parts of the
3153 makefile. Don't use it where special rules hold sway (RHS of a variable,
3154 in a command list, etc.) */
3155
3156static enum make_word_type
3157get_next_mword (char *buffer, char *delim, char **startp, unsigned int *length)
3158{
3159 enum make_word_type wtype = w_bogus;
3160 char *p = buffer, *beg;
3161 char c;
3162
3163 /* Skip any leading whitespace. */
3164 while (isblank ((unsigned char)*p))
3165 ++p;
3166
3167 beg = p;
3168 c = *(p++);
3169 switch (c)
3170 {
3171 case '\0':
3172 wtype = w_eol;
3173 break;
3174
3175 case ';':
3176 wtype = w_semicolon;
3177 break;
3178
3179 case '=':
3180 wtype = w_varassign;
3181 break;
3182
3183 case ':':
3184 wtype = w_colon;
3185 switch (*p)
3186 {
3187 case ':':
3188 ++p;
3189 wtype = w_dcolon;
3190 break;
3191
3192 case '=':
3193 ++p;
3194 wtype = w_varassign;
3195 break;
3196 }
3197 break;
3198
3199 case '+':
3200 case '?':
3201#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3202 case '>':
3203#endif
3204 if (*p == '=')
3205 {
3206 ++p;
3207 wtype = w_varassign;
3208 break;
3209 }
3210
3211 default:
3212 if (delim && strchr (delim, c))
3213 wtype = w_static;
3214 break;
3215 }
3216
3217 /* Did we find something? If so, return now. */
3218 if (wtype != w_bogus)
3219 goto done;
3220
3221 /* This is some non-operator word. A word consists of the longest
3222 string of characters that doesn't contain whitespace, one of [:=#],
3223 or [?+]=, or one of the chars in the DELIM string. */
3224
3225 /* We start out assuming a static word; if we see a variable we'll
3226 adjust our assumptions then. */
3227 wtype = w_static;
3228
3229 /* We already found the first value of "c", above. */
3230 while (1)
3231 {
3232 char closeparen;
3233 int count;
3234
3235 switch (c)
3236 {
3237 case '\0':
3238 case ' ':
3239 case '\t':
3240 case '=':
3241 goto done_word;
3242
3243 case ':':
3244#ifdef HAVE_DOS_PATHS
3245 /* A word CAN include a colon in its drive spec. The drive
3246 spec is allowed either at the beginning of a word, or as part
3247 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
3248 if (!(p - beg >= 2
3249 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
3250 && (p - beg == 2 || p[-3] == '(')))
3251#endif
3252 goto done_word;
3253
3254 case '$':
3255 c = *(p++);
3256 if (c == '$')
3257 break;
3258
3259 /* This is a variable reference, so note that it's expandable.
3260 Then read it to the matching close paren. */
3261 wtype = w_variable;
3262
3263 if (c == '(')
3264 closeparen = ')';
3265 else if (c == '{')
3266 closeparen = '}';
3267 else
3268 /* This is a single-letter variable reference. */
3269 break;
3270
3271 for (count=0; *p != '\0'; ++p)
3272 {
3273 if (*p == c)
3274 ++count;
3275 else if (*p == closeparen && --count < 0)
3276 {
3277 ++p;
3278 break;
3279 }
3280 }
3281 break;
3282
3283 case '?':
3284 case '+':
3285#ifdef CONFIG_WITH_PREPEND_ASSIGNMENT
3286 case '>':
3287#endif
3288 if (*p == '=')
3289 goto done_word;
3290 break;
3291
3292 case '\\':
3293 switch (*p)
3294 {
3295 case ':':
3296 case ';':
3297 case '=':
3298 case '\\':
3299 ++p;
3300 break;
3301 }
3302 break;
3303
3304 default:
3305 if (delim && strchr (delim, c))
3306 goto done_word;
3307 break;
3308 }
3309
3310 c = *(p++);
3311 }
3312 done_word:
3313 --p;
3314
3315 done:
3316 if (startp)
3317 *startp = beg;
3318 if (length)
3319 *length = p - beg;
3320 return wtype;
3321}
3322
3323
3324/* Construct the list of include directories
3325 from the arguments and the default list. */
3326
3327void
3328construct_include_path (const char **arg_dirs)
3329{
3330#ifdef VAXC /* just don't ask ... */
3331 stat_t stbuf;
3332#else
3333 struct stat stbuf;
3334#endif
3335 const char **dirs;
3336 const char **cpp;
3337 unsigned int idx;
3338
3339 /* Compute the number of pointers we need in the table. */
3340 idx = sizeof (default_include_directories) / sizeof (const char *);
3341 if (arg_dirs)
3342 for (cpp = arg_dirs; *cpp != 0; ++cpp)
3343 ++idx;
3344
3345#ifdef __MSDOS__
3346 /* Add one for $DJDIR. */
3347 ++idx;
3348#endif
3349#ifdef KMK
3350 /* Add one for the kBuild directory. */
3351 ++idx;
3352#endif
3353
3354 dirs = xmalloc (idx * sizeof (const char *));
3355
3356 idx = 0;
3357 max_incl_len = 0;
3358
3359 /* First consider any dirs specified with -I switches.
3360 Ignore any that don't exist. Remember the maximum string length. */
3361
3362 if (arg_dirs)
3363 while (*arg_dirs != 0)
3364 {
3365 const char *dir = *(arg_dirs++);
3366 char *expanded = 0;
3367 int e;
3368
3369 if (dir[0] == '~')
3370 {
3371 expanded = tilde_expand (dir);
3372 if (expanded != 0)
3373 dir = expanded;
3374 }
3375
3376 EINTRLOOP (e, stat (dir, &stbuf));
3377 if (e == 0 && S_ISDIR (stbuf.st_mode))
3378 {
3379 unsigned int len = strlen (dir);
3380 /* If dir name is written with trailing slashes, discard them. */
3381 while (len > 1 && dir[len - 1] == '/')
3382 --len;
3383 if (len > max_incl_len)
3384 max_incl_len = len;
3385 dirs[idx++] = strcache_add_len (dir, len);
3386 }
3387
3388 if (expanded)
3389 free (expanded);
3390 }
3391
3392 /* Now add the standard default dirs at the end. */
3393
3394#ifdef __MSDOS__
3395 {
3396 /* The environment variable $DJDIR holds the root of the DJGPP directory
3397 tree; add ${DJDIR}/include. */
3398 struct variable *djdir = lookup_variable ("DJDIR", 5);
3399
3400 if (djdir)
3401 {
3402 unsigned int len = strlen (djdir->value) + 8;
3403 char *defdir = alloca (len + 1);
3404
3405 strcat (strcpy (defdir, djdir->value), "/include");
3406 dirs[idx++] = strcache_add (defdir);
3407
3408 if (len > max_incl_len)
3409 max_incl_len = len;
3410 }
3411 }
3412#endif
3413#ifdef KMK
3414 /* Add $(PATH_KBUILD). */
3415 {
3416 size_t len = strlen (get_path_kbuild ());
3417 dirs[idx++] = strcache_add_len (get_path_kbuild (), len);
3418 if (len > max_incl_len)
3419 max_incl_len = len;
3420 }
3421#endif
3422
3423 for (cpp = default_include_directories; *cpp != 0; ++cpp)
3424 {
3425 int e;
3426
3427 EINTRLOOP (e, stat (*cpp, &stbuf));
3428 if (e == 0 && S_ISDIR (stbuf.st_mode))
3429 {
3430 unsigned int len = strlen (*cpp);
3431 /* If dir name is written with trailing slashes, discard them. */
3432 while (len > 1 && (*cpp)[len - 1] == '/')
3433 --len;
3434 if (len > max_incl_len)
3435 max_incl_len = len;
3436 dirs[idx++] = strcache_add_len (*cpp, len - 1);
3437 }
3438 }
3439
3440 dirs[idx] = 0;
3441
3442 /* Now add each dir to the .INCLUDE_DIRS variable. */
3443
3444 for (cpp = dirs; *cpp != 0; ++cpp)
3445 do_variable_definition (NILF, ".INCLUDE_DIRS", *cpp,
3446 o_default, f_append, 0);
3447
3448 include_directories = dirs;
3449}
3450
3451
3452/* Expand ~ or ~USER at the beginning of NAME.
3453 Return a newly malloc'd string or 0. */
3454
3455char *
3456tilde_expand (const char *name)
3457{
3458#ifndef VMS
3459 if (name[1] == '/' || name[1] == '\0')
3460 {
3461 extern char *getenv ();
3462 char *home_dir;
3463 int is_variable;
3464
3465 {
3466 /* Turn off --warn-undefined-variables while we expand HOME. */
3467 int save = warn_undefined_variables_flag;
3468 warn_undefined_variables_flag = 0;
3469
3470 home_dir = allocated_variable_expand ("$(HOME)");
3471
3472 warn_undefined_variables_flag = save;
3473 }
3474
3475 is_variable = home_dir[0] != '\0';
3476 if (!is_variable)
3477 {
3478 free (home_dir);
3479 home_dir = getenv ("HOME");
3480 }
3481# if !defined(_AMIGA) && !defined(WINDOWS32)
3482 if (home_dir == 0 || home_dir[0] == '\0')
3483 {
3484 extern char *getlogin ();
3485 char *logname = getlogin ();
3486 home_dir = 0;
3487 if (logname != 0)
3488 {
3489 struct passwd *p = getpwnam (logname);
3490 if (p != 0)
3491 home_dir = p->pw_dir;
3492 }
3493 }
3494# endif /* !AMIGA && !WINDOWS32 */
3495 if (home_dir != 0)
3496 {
3497 char *new = xstrdup (concat (home_dir, "", name + 1));
3498 if (is_variable)
3499 free (home_dir);
3500 return new;
3501 }
3502 }
3503# if !defined(_AMIGA) && !defined(WINDOWS32)
3504 else
3505 {
3506 struct passwd *pwent;
3507 char *userend = strchr (name + 1, '/');
3508 if (userend != 0)
3509 *userend = '\0';
3510 pwent = getpwnam (name + 1);
3511 if (pwent != 0)
3512 {
3513 if (userend == 0)
3514 return xstrdup (pwent->pw_dir);
3515 else
3516 return xstrdup (concat (pwent->pw_dir, "/", userend + 1));
3517 }
3518 else if (userend != 0)
3519 *userend = '/';
3520 }
3521# endif /* !AMIGA && !WINDOWS32 */
3522#endif /* !VMS */
3523 return 0;
3524}
3525
3526/* Given a chain of struct nameseq's describing a sequence of filenames,
3527 in reverse of the intended order, return a new chain describing the
3528 result of globbing the filenames. The new chain is in forward order.
3529 The links of the old chain are freed or used in the new chain.
3530 Likewise for the names in the old chain.
3531
3532 SIZE is how big to construct chain elements.
3533 This is useful if we want them actually to be other structures
3534 that have room for additional info. */
3535
3536struct nameseq *
3537multi_glob (struct nameseq *chain, unsigned int size)
3538{
3539 void dir_setup_glob (glob_t *);
3540 struct nameseq *new = 0;
3541 struct nameseq *old;
3542 struct nameseq *nexto;
3543 glob_t gl;
3544#if defined(KMK) || defined(__EMX__) /* speed optimization */
3545 int rc;
3546#endif
3547
3548 dir_setup_glob (&gl);
3549
3550 for (old = chain; old != 0; old = nexto)
3551 {
3552 const char *gname;
3553#ifndef NO_ARCHIVES
3554 char *arname = 0;
3555 char *memname = 0;
3556#endif
3557 nexto = old->next;
3558 gname = old->name;
3559
3560 if (gname[0] == '~')
3561 {
3562 char *newname = tilde_expand (old->name);
3563 if (newname != 0)
3564 gname = newname;
3565 }
3566
3567#ifndef NO_ARCHIVES
3568 if (ar_name (gname))
3569 {
3570 /* OLD->name is an archive member reference. Replace it with the
3571 archive file name, and save the member name in MEMNAME. We will
3572 glob on the archive name and then reattach MEMNAME later. */
3573 ar_parse_name (gname, &arname, &memname);
3574 gname = arname;
3575 }
3576#endif /* !NO_ARCHIVES */
3577
3578#if defined(KMK) || defined(__EMX__) /* speed optimization */
3579 if (!strpbrk(gname, "*?["))
3580 {
3581 gl.gl_pathc = 1;
3582 gl.gl_pathv = (char **)&gname;
3583 rc = 0;
3584 }
3585 else
3586 rc = glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl);
3587 switch (rc)
3588#else
3589 switch (glob (gname, GLOB_NOCHECK|GLOB_ALTDIRFUNC, NULL, &gl))
3590#endif
3591 {
3592 case 0: /* Success. */
3593 {
3594 int i = gl.gl_pathc;
3595 while (i-- > 0)
3596 {
3597#ifndef NO_ARCHIVES
3598 if (memname != 0)
3599 {
3600 /* Try to glob on MEMNAME within the archive. */
3601 struct nameseq *found
3602 = ar_glob (gl.gl_pathv[i], memname, size);
3603 if (! found)
3604 {
3605 /* No matches. Use MEMNAME as-is. */
3606 unsigned int alen = strlen (gl.gl_pathv[i]);
3607 unsigned int mlen = strlen (memname);
3608 char *name;
3609 struct nameseq *elt = xmalloc (size);
3610 memset (elt, '\0', size);
3611
3612 name = alloca (alen + 1 + mlen + 2);
3613 memcpy (name, gl.gl_pathv[i], alen);
3614 name[alen] = '(';
3615 memcpy (name+alen+1, memname, mlen);
3616 name[alen + 1 + mlen] = ')';
3617 name[alen + 1 + mlen + 1] = '\0';
3618 elt->name = strcache_add (name);
3619 elt->next = new;
3620 new = elt;
3621 }
3622 else
3623 {
3624 /* Find the end of the FOUND chain. */
3625 struct nameseq *f = found;
3626 while (f->next != 0)
3627 f = f->next;
3628
3629 /* Attach the chain being built to the end of the FOUND
3630 chain, and make FOUND the new NEW chain. */
3631 f->next = new;
3632 new = found;
3633 }
3634 }
3635 else
3636#endif /* !NO_ARCHIVES */
3637 {
3638 struct nameseq *elt = xmalloc (size);
3639 memset (elt, '\0', size);
3640 elt->name = strcache_add (gl.gl_pathv[i]);
3641 elt->next = new;
3642 new = elt;
3643 }
3644 }
3645#if defined(KMK) || defined(__EMX__) /* speed optimization */
3646 if (gl.gl_pathv != (char **)&gname)
3647#endif
3648 globfree (&gl);
3649 free (old);
3650 break;
3651 }
3652
3653 case GLOB_NOSPACE:
3654 fatal (NILF, _("virtual memory exhausted"));
3655 break;
3656
3657 default:
3658 old->next = new;
3659 new = old;
3660 break;
3661 }
3662
3663#ifndef NO_ARCHIVES
3664 if (arname)
3665 free (arname);
3666#endif
3667 }
3668
3669 return new;
3670}
Note: See TracBrowser for help on using the repository browser.