VirtualBox

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

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

buffer the entire makefile / 256KB. saves a little bit of time on the mac at least...

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