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

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

Check for '\0' in defines, just to keep the value_length sane.

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