VirtualBox

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

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

Implemented local variable definitions - CONFIG_WITH_LOCAL_VARIABLES.

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

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