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

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

GCC starts with an empty ' \' line.

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