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

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

includedep: expand variabels when present. Fixes #38.

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