VirtualBox

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

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

added variable assignment to the includedep parser.

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