VirtualBox

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

Last change on this file since 3633 was 3519, checked in by bird, 3 years ago

kmk: Added functions: qone-unquoted, qtarget, qdeps, qdeps-all, qdeps-newer, qdeps-oo, qfilter and qfilter-out.

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

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