VirtualBox

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

Last change on this file since 3154 was 3145, checked in by bird, 7 years ago

kmk: warnings found by gcc 7.3.0

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

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