VirtualBox

source: kBuild/trunk/src/kmk/read.c@ 1169

Last change on this file since 1169 was 1169, checked in by bird, 17 years ago

reworte it to use a whole file buffer so we can use mmap later...

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette