VirtualBox

source: kBuild/vendor/gnumake/3.82/read.c

Last change on this file was 2579, checked in by bird, 12 years ago

Importing make-3.82.tar.bz2 (md5sum 1a11100f3c63fcf5753818e59d63088f) with --auto-props but no keywords.

  • Property svn:eol-style set to native
File size: 91.2 KB
Line 
1/* Reading and parsing of makefiles for GNU Make.
2Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
31998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
42010 Free Software Foundation, Inc.
5This file is part of GNU Make.
6
7GNU Make is free software; you can redistribute it and/or modify it under the
8terms of the GNU General Public License as published by the Free Software
9Foundation; either version 3 of the License, or (at your option) any later
10version.
11
12GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
13WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14A PARTICULAR PURPOSE. See the GNU General Public License for more details.
15
16You should have received a copy of the GNU General Public License along with
17this program. If not, see <http://www.gnu.org/licenses/>. */
18
19#include "make.h"
20
21#include <assert.h>
22
23#include <glob.h>
24
25#include "dep.h"
26#include "filedef.h"
27#include "job.h"
28#include "commands.h"
29#include "variable.h"
30#include "rule.h"
31#include "debug.h"
32#include "hash.h"
33
34
35#ifndef WINDOWS32
36#ifndef _AMIGA
37#ifndef VMS
38#include <pwd.h>
39#else
40struct passwd *getpwnam (char *name);
41#endif
42#endif
43#endif /* !WINDOWS32 */
44
45/* A 'struct ebuffer' controls the origin of the makefile we are currently
46 eval'ing.
47*/
48
49struct ebuffer
50 {
51 char *buffer; /* Start of the current line in the buffer. */
52 char *bufnext; /* Start of the next line in the buffer. */
53 char *bufstart; /* Start of the entire buffer. */
54 unsigned int size; /* Malloc'd size of buffer. */
55 FILE *fp; /* File, or NULL if this is an internal buffer. */
56 struct floc floc; /* Info on the file in fp (if any). */
57 };
58
59/* Track the modifiers we can have on variable assignments */
60
61struct vmodifiers
62 {
63 unsigned int assign_v:1;
64 unsigned int define_v:1;
65 unsigned int undefine_v:1;
66 unsigned int export_v:1;
67 unsigned int override_v:1;
68 unsigned int private_v:1;
69 };
70
71/* Types of "words" that can be read in a makefile. */
72enum make_word_type
73 {
74 w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
75 w_varassign
76 };
77
78
79/* A `struct conditionals' contains the information describing
80 all the active conditionals in a makefile.
81
82 The global variable `conditionals' contains the conditionals
83 information for the current makefile. It is initialized from
84 the static structure `toplevel_conditionals' and is later changed
85 to new structures for included makefiles. */
86
87struct conditionals
88 {
89 unsigned int if_cmds; /* Depth of conditional nesting. */
90 unsigned int allocated; /* Elts allocated in following arrays. */
91 char *ignoring; /* Are we ignoring or interpreting?
92 0=interpreting, 1=not yet interpreted,
93 2=already interpreted */
94 char *seen_else; /* Have we already seen an `else'? */
95 };
96
97static struct conditionals toplevel_conditionals;
98static struct conditionals *conditionals = &toplevel_conditionals;
99
100
101/* Default directories to search for include files in */
102
103static const char *default_include_directories[] =
104 {
105#if defined(WINDOWS32) && !defined(INCLUDEDIR)
106/* This completely up to the user when they install MSVC or other packages.
107 This is defined as a placeholder. */
108# define INCLUDEDIR "."
109#endif
110 INCLUDEDIR,
111#ifndef _AMIGA
112 "/usr/gnu/include",
113 "/usr/local/include",
114 "/usr/include",
115#endif
116 0
117 };
118
119/* List of directories to search for include files in */
120
121static const char **include_directories;
122
123/* Maximum length of an element of the above. */
124
125static unsigned int max_incl_len;
126
127/* The filename and pointer to line number of the
128 makefile currently being read in. */
129
130const struct floc *reading_file = 0;
131
132/* The chain of makefiles read by read_makefile. */
133
134static struct dep *read_makefiles = 0;
135
136static int eval_makefile (const char *filename, int flags);
137static void eval (struct ebuffer *buffer, int flags);
138
139static long readline (struct ebuffer *ebuf);
140static void do_undefine (char *name, enum variable_origin origin,
141 struct ebuffer *ebuf);
142static struct variable *do_define (char *name, enum variable_origin origin,
143 struct ebuffer *ebuf);
144static int conditional_line (char *line, int len, const struct floc *flocp);
145static void record_files (struct nameseq *filenames, const char *pattern,
146 const char *pattern_percent, char *depstr,
147 unsigned int cmds_started, char *commands,
148 unsigned int commands_idx, int two_colon,
149 const struct floc *flocp);
150static void record_target_var (struct nameseq *filenames, char *defn,
151 enum variable_origin origin,
152 struct vmodifiers *vmod,
153 const struct floc *flocp);
154static enum make_word_type get_next_mword (char *buffer, char *delim,
155 char **startp, unsigned int *length);
156static void remove_comments (char *line);
157static char *find_char_unquote (char *string, int stop1, int stop2,
158 int blank, int ignorevars);
159
160
161/* Compare a word, both length and contents.
162 P must point to the word to be tested, and WLEN must be the length.
163*/
164#define word1eq(s) (wlen == sizeof(s)-1 && strneq (s, p, sizeof(s)-1))
165
166
167
168/* Read in all the makefiles and return the chain of their names. */
169
170struct dep *
171read_all_makefiles (const char **makefiles)
172{
173 unsigned int num_makefiles = 0;
174
175 /* Create *_LIST variables, to hold the makefiles, targets, and variables
176 we will be reading. */
177
178 define_variable_cname ("MAKEFILE_LIST", "", o_file, 0);
179
180 DB (DB_BASIC, (_("Reading makefiles...\n")));
181
182 /* If there's a non-null variable MAKEFILES, its value is a list of
183 files to read first thing. But don't let it prevent reading the
184 default makefiles and don't let the default goal come from there. */
185
186 {
187 char *value;
188 char *name, *p;
189 unsigned int length;
190
191 {
192 /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
193 int save = warn_undefined_variables_flag;
194 warn_undefined_variables_flag = 0;
195
196 value = allocated_variable_expand ("$(MAKEFILES)");
197
198 warn_undefined_variables_flag = save;
199 }
200
201 /* Set NAME to the start of next token and LENGTH to its length.
202 MAKEFILES is updated for finding remaining tokens. */
203 p = value;
204
205 while ((name = find_next_token ((const char **)&p, &length)) != 0)
206 {
207 if (*p != '\0')
208 *p++ = '\0';
209 eval_makefile (name, RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE);
210 }
211
212 free (value);
213 }
214
215 /* Read makefiles specified with -f switches. */
216
217 if (makefiles != 0)
218 while (*makefiles != 0)
219 {
220 struct dep *tail = read_makefiles;
221 register struct dep *d;
222
223 if (! eval_makefile (*makefiles, 0))
224 perror_with_name ("", *makefiles);
225
226 /* Find the right element of read_makefiles. */
227 d = read_makefiles;
228 while (d->next != tail)
229 d = d->next;
230
231 /* Use the storage read_makefile allocates. */
232 *makefiles = dep_name (d);
233 ++num_makefiles;
234 ++makefiles;
235 }
236
237 /* If there were no -f switches, try the default names. */
238
239 if (num_makefiles == 0)
240 {
241 static char *default_makefiles[] =
242#ifdef VMS
243 /* all lower case since readdir() (the vms version) 'lowercasifies' */
244 { "makefile.vms", "gnumakefile.", "makefile.", 0 };
245#else
246#ifdef _AMIGA
247 { "GNUmakefile", "Makefile", "SMakefile", 0 };
248#else /* !Amiga && !VMS */
249 { "GNUmakefile", "makefile", "Makefile", 0 };
250#endif /* AMIGA */
251#endif /* VMS */
252 register char **p = default_makefiles;
253 while (*p != 0 && !file_exists_p (*p))
254 ++p;
255
256 if (*p != 0)
257 {
258 if (! eval_makefile (*p, 0))
259 perror_with_name ("", *p);
260 }
261 else
262 {
263 /* No default makefile was found. Add the default makefiles to the
264 `read_makefiles' chain so they will be updated if possible. */
265 struct dep *tail = read_makefiles;
266 /* Add them to the tail, after any MAKEFILES variable makefiles. */
267 while (tail != 0 && tail->next != 0)
268 tail = tail->next;
269 for (p = default_makefiles; *p != 0; ++p)
270 {
271 struct dep *d = alloc_dep ();
272 d->file = enter_file (strcache_add (*p));
273 d->dontcare = 1;
274 /* Tell update_goal_chain to bail out as soon as this file is
275 made, and main not to die if we can't make this file. */
276 d->changed = RM_DONTCARE;
277 if (tail == 0)
278 read_makefiles = d;
279 else
280 tail->next = d;
281 tail = d;
282 }
283 if (tail != 0)
284 tail->next = 0;
285 }
286 }
287
288 return read_makefiles;
289}
290
291
292/* Install a new conditional and return the previous one. */
293
294static struct conditionals *
295install_conditionals (struct conditionals *new)
296{
297 struct conditionals *save = conditionals;
298
299 memset (new, '\0', sizeof (*new));
300 conditionals = new;
301
302 return save;
303}
304
305/* Free the current conditionals and reinstate a saved one. */
306
307static void
308restore_conditionals (struct conditionals *saved)
309{
310 /* Free any space allocated by conditional_line. */
311 if (conditionals->ignoring)
312 free (conditionals->ignoring);
313 if (conditionals->seen_else)
314 free (conditionals->seen_else);
315
316 /* Restore state. */
317 conditionals = saved;
318}
319
320
321static int
322eval_makefile (const char *filename, int flags)
323{
324 struct dep *deps;
325 struct ebuffer ebuf;
326 const struct floc *curfile;
327 char *expanded = 0;
328 int makefile_errno;
329
330 filename = strcache_add (filename);
331 ebuf.floc.filenm = filename;
332 ebuf.floc.lineno = 1;
333
334 if (ISDB (DB_VERBOSE))
335 {
336 printf (_("Reading makefile `%s'"), filename);
337 if (flags & RM_NO_DEFAULT_GOAL)
338 printf (_(" (no default goal)"));
339 if (flags & RM_INCLUDED)
340 printf (_(" (search path)"));
341 if (flags & RM_DONTCARE)
342 printf (_(" (don't care)"));
343 if (flags & RM_NO_TILDE)
344 printf (_(" (no ~ expansion)"));
345 puts ("...");
346 }
347
348 /* First, get a stream to read. */
349
350 /* Expand ~ in FILENAME unless it came from `include',
351 in which case it was already done. */
352 if (!(flags & RM_NO_TILDE) && filename[0] == '~')
353 {
354 expanded = tilde_expand (filename);
355 if (expanded != 0)
356 filename = expanded;
357 }
358
359 ebuf.fp = fopen (filename, "r");
360 /* Save the error code so we print the right message later. */
361 makefile_errno = errno;
362
363 /* If the makefile wasn't found and it's either a makefile from
364 the `MAKEFILES' variable or an included makefile,
365 search the included makefile search path for this makefile. */
366 if (ebuf.fp == 0 && (flags & RM_INCLUDED) && *filename != '/')
367 {
368 unsigned int i;
369 for (i = 0; include_directories[i] != 0; ++i)
370 {
371 const char *included = concat (3, include_directories[i],
372 "/", filename);
373 ebuf.fp = fopen (included, "r");
374 if (ebuf.fp)
375 {
376 filename = strcache_add (included);
377 break;
378 }
379 }
380 }
381
382 /* Add FILENAME to the chain of read makefiles. */
383 deps = alloc_dep ();
384 deps->next = read_makefiles;
385 read_makefiles = deps;
386 deps->file = lookup_file (filename);
387 if (deps->file == 0)
388 deps->file = enter_file (filename);
389 filename = deps->file->name;
390 deps->changed = flags;
391 if (flags & RM_DONTCARE)
392 deps->dontcare = 1;
393
394 if (expanded)
395 free (expanded);
396
397 /* If the makefile can't be found at all, give up entirely. */
398
399 if (ebuf.fp == 0)
400 {
401 /* If we did some searching, errno has the error from the last
402 attempt, rather from FILENAME itself. Restore it in case the
403 caller wants to use it in a message. */
404 errno = makefile_errno;
405 return 0;
406 }
407
408 /* Set close-on-exec to avoid leaking the makefile to children, such as
409 $(shell ...). */
410#ifdef HAVE_FILENO
411 CLOSE_ON_EXEC (fileno (ebuf.fp));
412#endif
413
414 /* Add this makefile to the list. */
415 do_variable_definition (&ebuf.floc, "MAKEFILE_LIST", filename, o_file,
416 f_append, 0);
417
418 /* Evaluate the makefile */
419
420 ebuf.size = 200;
421 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = xmalloc (ebuf.size);
422
423 curfile = reading_file;
424 reading_file = &ebuf.floc;
425
426 eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
427
428 reading_file = curfile;
429
430 fclose (ebuf.fp);
431
432 free (ebuf.bufstart);
433 alloca (0);
434
435 return 1;
436}
437
438void
439eval_buffer (char *buffer)
440{
441 struct ebuffer ebuf;
442 struct conditionals *saved;
443 struct conditionals new;
444 const struct floc *curfile;
445
446 /* Evaluate the buffer */
447
448 ebuf.size = strlen (buffer);
449 ebuf.buffer = ebuf.bufnext = ebuf.bufstart = buffer;
450 ebuf.fp = NULL;
451
452 if (reading_file)
453 ebuf.floc = *reading_file;
454 else
455 ebuf.floc.filenm = NULL;
456
457 curfile = reading_file;
458 reading_file = &ebuf.floc;
459
460 saved = install_conditionals (&new);
461
462 eval (&ebuf, 1);
463
464 restore_conditionals (saved);
465
466 reading_file = curfile;
467
468 alloca (0);
469}
470
471
472/* Check LINE to see if it's a variable assignment or undefine.
473
474 It might use one of the modifiers "export", "override", "private", or it
475 might be one of the conditional tokens like "ifdef", "include", etc.
476
477 If it's not a variable assignment or undefine, VMOD.V_ASSIGN is 0.
478 Returns LINE.
479
480 Returns a pointer to the first non-modifier character, and sets VMOD
481 based on the modifiers found if any, plus V_ASSIGN is 1.
482 */
483static char *
484parse_var_assignment (const char *line, struct vmodifiers *vmod)
485{
486 const char *p;
487 memset (vmod, '\0', sizeof (*vmod));
488
489 /* Find the start of the next token. If there isn't one we're done. */
490 line = next_token (line);
491 if (*line == '\0')
492 return (char *)line;
493
494 p = line;
495 while (1)
496 {
497 int wlen;
498 const char *p2;
499 enum variable_flavor flavor;
500
501 p2 = parse_variable_definition (p, &flavor);
502
503 /* If this is a variable assignment, we're done. */
504 if (p2)
505 break;
506
507 /* It's not a variable; see if it's a modifier. */
508 p2 = end_of_token (p);
509 wlen = p2 - p;
510
511 if (word1eq ("export"))
512 vmod->export_v = 1;
513 else if (word1eq ("override"))
514 vmod->override_v = 1;
515 else if (word1eq ("private"))
516 vmod->private_v = 1;
517 else if (word1eq ("define"))
518 {
519 /* We can't have modifiers after 'define' */
520 vmod->define_v = 1;
521 p = next_token (p2);
522 break;
523 }
524 else if (word1eq ("undefine"))
525 {
526 /* We can't have modifiers after 'undefine' */
527 vmod->undefine_v = 1;
528 p = next_token (p2);
529 break;
530 }
531 else
532 /* Not a variable or modifier: this is not a variable assignment. */
533 return (char *)line;
534
535 /* It was a modifier. Try the next word. */
536 p = next_token (p2);
537 if (*p == '\0')
538 return (char *)line;
539 }
540
541 /* Found a variable assignment or undefine. */
542 vmod->assign_v = 1;
543 return (char *)p;
544}
545
546
547
548/* Read file FILENAME as a makefile and add its contents to the data base.
549
550 SET_DEFAULT is true if we are allowed to set the default goal. */
551
552static void
553eval (struct ebuffer *ebuf, int set_default)
554{
555 char *collapsed = 0;
556 unsigned int collapsed_length = 0;
557 unsigned int commands_len = 200;
558 char *commands;
559 unsigned int commands_idx = 0;
560 unsigned int cmds_started, tgts_started;
561 int ignoring = 0, in_ignored_define = 0;
562 int no_targets = 0; /* Set when reading a rule without targets. */
563 struct nameseq *filenames = 0;
564 char *depstr = 0;
565 long nlines = 0;
566 int two_colon = 0;
567 const char *pattern = 0;
568 const char *pattern_percent;
569 struct floc *fstart;
570 struct floc fi;
571
572#define record_waiting_files() \
573 do \
574 { \
575 if (filenames != 0) \
576 { \
577 fi.lineno = tgts_started; \
578 record_files (filenames, pattern, pattern_percent, depstr, \
579 cmds_started, commands, commands_idx, two_colon, \
580 &fi); \
581 filenames = 0; \
582 } \
583 commands_idx = 0; \
584 no_targets = 0; \
585 pattern = 0; \
586 } while (0)
587
588 pattern_percent = 0;
589 cmds_started = tgts_started = 1;
590
591 fstart = &ebuf->floc;
592 fi.filenm = ebuf->floc.filenm;
593
594 /* Loop over lines in the file.
595 The strategy is to accumulate target names in FILENAMES, dependencies
596 in DEPS and commands in COMMANDS. These are used to define a rule
597 when the start of the next rule (or eof) is encountered.
598
599 When you see a "continue" in the loop below, that means we are moving on
600 to the next line _without_ ending any rule that we happen to be working
601 with at the moment. If you see a "goto rule_complete", then the
602 statement we just parsed also finishes the previous rule. */
603
604 commands = xmalloc (200);
605
606 while (1)
607 {
608 unsigned int linelen;
609 char *line;
610 unsigned int wlen;
611 char *p;
612 char *p2;
613 struct vmodifiers vmod;
614
615 /* At the top of this loop, we are starting a brand new line. */
616 /* Grab the next line to be evaluated */
617 ebuf->floc.lineno += nlines;
618 nlines = readline (ebuf);
619
620 /* If there is nothing left to eval, we're done. */
621 if (nlines < 0)
622 break;
623
624 /* If this line is empty, skip it. */
625 line = ebuf->buffer;
626 if (line[0] == '\0')
627 continue;
628
629 linelen = strlen (line);
630
631 /* Check for a shell command line first.
632 If it is not one, we can stop treating tab specially. */
633 if (line[0] == cmd_prefix)
634 {
635 if (no_targets)
636 /* Ignore the commands in a rule with no targets. */
637 continue;
638
639 /* If there is no preceding rule line, don't treat this line
640 as a command, even though it begins with a recipe prefix.
641 SunOS 4 make appears to behave this way. */
642
643 if (filenames != 0)
644 {
645 if (ignoring)
646 /* Yep, this is a shell command, and we don't care. */
647 continue;
648
649 /* Append this command line to the line being accumulated.
650 Strip command prefix chars that appear after newlines. */
651 if (commands_idx == 0)
652 cmds_started = ebuf->floc.lineno;
653
654 if (linelen + commands_idx > commands_len)
655 {
656 commands_len = (linelen + commands_idx) * 2;
657 commands = xrealloc (commands, commands_len);
658 }
659 p = &commands[commands_idx];
660 p2 = line + 1;
661 while (--linelen)
662 {
663 ++commands_idx;
664 *(p++) = *p2;
665 if (p2[0] == '\n' && p2[1] == cmd_prefix)
666 {
667 ++p2;
668 --linelen;
669 }
670 ++p2;
671 }
672 *p = '\n';
673 ++commands_idx;
674
675 continue;
676 }
677 }
678
679 /* This line is not a shell command line. Don't worry about whitespace.
680 Get more space if we need it; we don't need to preserve the current
681 contents of the buffer. */
682
683 if (collapsed_length < linelen+1)
684 {
685 collapsed_length = linelen+1;
686 if (collapsed)
687 free (collapsed);
688 /* Don't need xrealloc: we don't need to preserve the content. */
689 collapsed = xmalloc (collapsed_length);
690 }
691 strcpy (collapsed, line);
692 /* Collapse continuation lines. */
693 collapse_continuations (collapsed);
694 remove_comments (collapsed);
695
696 /* Get rid if starting space (including formfeed, vtab, etc.) */
697 p = collapsed;
698 while (isspace ((unsigned char)*p))
699 ++p;
700
701 /* See if this is a variable assignment. We need to do this early, to
702 allow variables with names like 'ifdef', 'export', 'private', etc. */
703 p = parse_var_assignment(p, &vmod);
704 if (vmod.assign_v)
705 {
706 struct variable *v;
707 enum variable_origin origin = vmod.override_v ? o_override : o_file;
708
709 /* If we're ignoring then we're done now. */
710 if (ignoring)
711 {
712 if (vmod.define_v)
713 in_ignored_define = 1;
714 continue;
715 }
716
717 if (vmod.undefine_v)
718 {
719 do_undefine (p, origin, ebuf);
720
721 /* This line has been dealt with. */
722 goto rule_complete;
723 }
724 else if (vmod.define_v)
725 v = do_define (p, origin, ebuf);
726 else
727 v = try_variable_definition (fstart, p, origin, 0);
728
729 assert (v != NULL);
730
731 if (vmod.export_v)
732 v->export = v_export;
733 if (vmod.private_v)
734 v->private_var = 1;
735
736 /* This line has been dealt with. */
737 goto rule_complete;
738 }
739
740 /* If this line is completely empty, ignore it. */
741 if (*p == '\0')
742 continue;
743
744 p2 = end_of_token (p);
745 wlen = p2 - p;
746 p2 = next_token (p2);
747
748 /* If we're in an ignored define, skip this line (but maybe get out). */
749 if (in_ignored_define)
750 {
751 /* See if this is an endef line (plus optional comment). */
752 if (word1eq ("endef") && (*p2 == '\0' || *p2 == '#'))
753 in_ignored_define = 0;
754
755 continue;
756 }
757
758 /* Check for conditional state changes. */
759 {
760 int i = conditional_line (p, wlen, fstart);
761 if (i != -2)
762 {
763 if (i == -1)
764 fatal (fstart, _("invalid syntax in conditional"));
765
766 ignoring = i;
767 continue;
768 }
769 }
770
771 /* Nothing to see here... move along. */
772 if (ignoring)
773 continue;
774
775 /* Manage the "export" keyword used outside of variable assignment
776 as well as "unexport". */
777 if (word1eq ("export") || word1eq ("unexport"))
778 {
779 int exporting = *p == 'u' ? 0 : 1;
780
781 /* (un)export by itself causes everything to be (un)exported. */
782 if (*p2 == '\0')
783 export_all_variables = exporting;
784 else
785 {
786 unsigned int l;
787 const char *cp;
788 char *ap;
789
790 /* Expand the line so we can use indirect and constructed
791 variable names in an (un)export command. */
792 cp = ap = allocated_variable_expand (p2);
793
794 for (p = find_next_token (&cp, &l); p != 0;
795 p = find_next_token (&cp, &l))
796 {
797 struct variable *v = lookup_variable (p, l);
798 if (v == 0)
799 v = define_variable_loc (p, l, "", o_file, 0, fstart);
800 v->export = exporting ? v_export : v_noexport;
801 }
802
803 free (ap);
804 }
805 goto rule_complete;
806 }
807
808 /* Handle the special syntax for vpath. */
809 if (word1eq ("vpath"))
810 {
811 const char *cp;
812 char *vpat;
813 unsigned int l;
814 cp = variable_expand (p2);
815 p = find_next_token (&cp, &l);
816 if (p != 0)
817 {
818 vpat = xstrndup (p, l);
819 p = find_next_token (&cp, &l);
820 /* No searchpath means remove all previous
821 selective VPATH's with the same pattern. */
822 }
823 else
824 /* No pattern means remove all previous selective VPATH's. */
825 vpat = 0;
826 construct_vpath_list (vpat, p);
827 if (vpat != 0)
828 free (vpat);
829
830 goto rule_complete;
831 }
832
833 /* Handle include and variants. */
834 if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
835 {
836 /* We have found an `include' line specifying a nested
837 makefile to be read at this point. */
838 struct conditionals *save;
839 struct conditionals new_conditionals;
840 struct nameseq *files;
841 /* "-include" (vs "include") says no error if the file does not
842 exist. "sinclude" is an alias for this from SGI. */
843 int noerror = (p[0] != 'i');
844
845 p = allocated_variable_expand (p2);
846
847 /* If no filenames, it's a no-op. */
848 if (*p == '\0')
849 {
850 free (p);
851 continue;
852 }
853
854 /* Parse the list of file names. Don't expand archive references! */
855 p2 = p;
856 files = PARSE_FILE_SEQ (&p2, struct nameseq, '\0', NULL,
857 PARSEFS_NOAR);
858 free (p);
859
860 /* Save the state of conditionals and start
861 the included makefile with a clean slate. */
862 save = install_conditionals (&new_conditionals);
863
864 /* Record the rules that are waiting so they will determine
865 the default goal before those in the included makefile. */
866 record_waiting_files ();
867
868 /* Read each included makefile. */
869 while (files != 0)
870 {
871 struct nameseq *next = files->next;
872 const char *name = files->name;
873 int r;
874
875 free_ns (files);
876 files = next;
877
878 r = eval_makefile (name,
879 (RM_INCLUDED | RM_NO_TILDE
880 | (noerror ? RM_DONTCARE : 0)
881 | (set_default ? 0 : RM_NO_DEFAULT_GOAL)));
882 if (!r && !noerror)
883 error (fstart, "%s: %s", name, strerror (errno));
884 }
885
886 /* Restore conditional state. */
887 restore_conditionals (save);
888
889 goto rule_complete;
890 }
891
892 /* This line starts with a tab but was not caught above because there
893 was no preceding target, and the line might have been usable as a
894 variable definition. But now we know it is definitely lossage. */
895 if (line[0] == cmd_prefix)
896 fatal(fstart, _("recipe commences before first target"));
897
898 /* This line describes some target files. This is complicated by
899 the existence of target-specific variables, because we can't
900 expand the entire line until we know if we have one or not. So
901 we expand the line word by word until we find the first `:',
902 then check to see if it's a target-specific variable.
903
904 In this algorithm, `lb_next' will point to the beginning of the
905 unexpanded parts of the input buffer, while `p2' points to the
906 parts of the expanded buffer we haven't searched yet. */
907
908 {
909 enum make_word_type wtype;
910 char *cmdleft, *semip, *lb_next;
911 unsigned int plen = 0;
912 char *colonp;
913 const char *end, *beg; /* Helpers for whitespace stripping. */
914
915 /* Record the previous rule. */
916
917 record_waiting_files ();
918 tgts_started = fstart->lineno;
919
920 /* Search the line for an unquoted ; that is not after an
921 unquoted #. */
922 cmdleft = find_char_unquote (line, ';', '#', 0, 1);
923 if (cmdleft != 0 && *cmdleft == '#')
924 {
925 /* We found a comment before a semicolon. */
926 *cmdleft = '\0';
927 cmdleft = 0;
928 }
929 else if (cmdleft != 0)
930 /* Found one. Cut the line short there before expanding it. */
931 *(cmdleft++) = '\0';
932 semip = cmdleft;
933
934 collapse_continuations (line);
935
936 /* We can't expand the entire line, since if it's a per-target
937 variable we don't want to expand it. So, walk from the
938 beginning, expanding as we go, and looking for "interesting"
939 chars. The first word is always expandable. */
940 wtype = get_next_mword(line, NULL, &lb_next, &wlen);
941 switch (wtype)
942 {
943 case w_eol:
944 if (cmdleft != 0)
945 fatal(fstart, _("missing rule before recipe"));
946 /* This line contained something but turned out to be nothing
947 but whitespace (a comment?). */
948 continue;
949
950 case w_colon:
951 case w_dcolon:
952 /* We accept and ignore rules without targets for
953 compatibility with SunOS 4 make. */
954 no_targets = 1;
955 continue;
956
957 default:
958 break;
959 }
960
961 p2 = variable_expand_string(NULL, lb_next, wlen);
962
963 while (1)
964 {
965 lb_next += wlen;
966 if (cmdleft == 0)
967 {
968 /* Look for a semicolon in the expanded line. */
969 cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
970
971 if (cmdleft != 0)
972 {
973 unsigned long p2_off = p2 - variable_buffer;
974 unsigned long cmd_off = cmdleft - variable_buffer;
975 char *pend = p2 + strlen(p2);
976
977 /* Append any remnants of lb, then cut the line short
978 at the semicolon. */
979 *cmdleft = '\0';
980
981 /* One school of thought says that you shouldn't expand
982 here, but merely copy, since now you're beyond a ";"
983 and into a command script. However, the old parser
984 expanded the whole line, so we continue that for
985 backwards-compatiblity. Also, it wouldn't be
986 entirely consistent, since we do an unconditional
987 expand below once we know we don't have a
988 target-specific variable. */
989 (void)variable_expand_string(pend, lb_next, (long)-1);
990 lb_next += strlen(lb_next);
991 p2 = variable_buffer + p2_off;
992 cmdleft = variable_buffer + cmd_off + 1;
993 }
994 }
995
996 colonp = find_char_unquote(p2, ':', 0, 0, 0);
997#ifdef HAVE_DOS_PATHS
998 /* The drive spec brain-damage strikes again... */
999 /* Note that the only separators of targets in this context
1000 are whitespace and a left paren. If others are possible,
1001 they should be added to the string in the call to index. */
1002 while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
1003 colonp > p2 && isalpha ((unsigned char)colonp[-1]) &&
1004 (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
1005 colonp = find_char_unquote(colonp + 1, ':', 0, 0, 0);
1006#endif
1007 if (colonp != 0)
1008 break;
1009
1010 wtype = get_next_mword(lb_next, NULL, &lb_next, &wlen);
1011 if (wtype == w_eol)
1012 break;
1013
1014 p2 += strlen(p2);
1015 *(p2++) = ' ';
1016 p2 = variable_expand_string(p2, lb_next, wlen);
1017 /* We don't need to worry about cmdleft here, because if it was
1018 found in the variable_buffer the entire buffer has already
1019 been expanded... we'll never get here. */
1020 }
1021
1022 p2 = next_token (variable_buffer);
1023
1024 /* If the word we're looking at is EOL, see if there's _anything_
1025 on the line. If not, a variable expanded to nothing, so ignore
1026 it. If so, we can't parse this line so punt. */
1027 if (wtype == w_eol)
1028 {
1029 if (*p2 != '\0')
1030 /* There's no need to be ivory-tower about this: check for
1031 one of the most common bugs found in makefiles... */
1032 fatal (fstart, _("missing separator%s"),
1033 (cmd_prefix == '\t' && !strneq(line, " ", 8))
1034 ? "" : _(" (did you mean TAB instead of 8 spaces?)"));
1035 continue;
1036 }
1037
1038 /* Make the colon the end-of-string so we know where to stop
1039 looking for targets. */
1040 *colonp = '\0';
1041 filenames = PARSE_FILE_SEQ (&p2, struct nameseq, '\0', NULL, 0);
1042 *p2 = ':';
1043
1044 if (!filenames)
1045 {
1046 /* We accept and ignore rules without targets for
1047 compatibility with SunOS 4 make. */
1048 no_targets = 1;
1049 continue;
1050 }
1051 /* This should never be possible; we handled it above. */
1052 assert (*p2 != '\0');
1053 ++p2;
1054
1055 /* Is this a one-colon or two-colon entry? */
1056 two_colon = *p2 == ':';
1057 if (two_colon)
1058 p2++;
1059
1060 /* Test to see if it's a target-specific variable. Copy the rest
1061 of the buffer over, possibly temporarily (we'll expand it later
1062 if it's not a target-specific variable). PLEN saves the length
1063 of the unparsed section of p2, for later. */
1064 if (*lb_next != '\0')
1065 {
1066 unsigned int l = p2 - variable_buffer;
1067 plen = strlen (p2);
1068 variable_buffer_output (p2+plen, lb_next, strlen (lb_next)+1);
1069 p2 = variable_buffer + l;
1070 }
1071
1072 p2 = parse_var_assignment (p2, &vmod);
1073 if (vmod.assign_v)
1074 {
1075 /* If there was a semicolon found, add it back, plus anything
1076 after it. */
1077 if (semip)
1078 {
1079 unsigned int l = p - variable_buffer;
1080 *(--semip) = ';';
1081 collapse_continuations (semip);
1082 variable_buffer_output (p2 + strlen (p2),
1083 semip, strlen (semip)+1);
1084 p = variable_buffer + l;
1085 }
1086 record_target_var (filenames, p2,
1087 vmod.override_v ? o_override : o_file,
1088 &vmod, fstart);
1089 filenames = 0;
1090 continue;
1091 }
1092
1093 /* This is a normal target, _not_ a target-specific variable.
1094 Unquote any = in the dependency list. */
1095 find_char_unquote (lb_next, '=', 0, 0, 0);
1096
1097 /* We have some targets, so don't ignore the following commands. */
1098 no_targets = 0;
1099
1100 /* Expand the dependencies, etc. */
1101 if (*lb_next != '\0')
1102 {
1103 unsigned int l = p2 - variable_buffer;
1104 (void) variable_expand_string (p2 + plen, lb_next, (long)-1);
1105 p2 = variable_buffer + l;
1106
1107 /* Look for a semicolon in the expanded line. */
1108 if (cmdleft == 0)
1109 {
1110 cmdleft = find_char_unquote (p2, ';', 0, 0, 0);
1111 if (cmdleft != 0)
1112 *(cmdleft++) = '\0';
1113 }
1114 }
1115
1116 /* Is this a static pattern rule: `target: %targ: %dep; ...'? */
1117 p = strchr (p2, ':');
1118 while (p != 0 && p[-1] == '\\')
1119 {
1120 char *q = &p[-1];
1121 int backslash = 0;
1122 while (*q-- == '\\')
1123 backslash = !backslash;
1124 if (backslash)
1125 p = strchr (p + 1, ':');
1126 else
1127 break;
1128 }
1129#ifdef _AMIGA
1130 /* Here, the situation is quite complicated. Let's have a look
1131 at a couple of targets:
1132
1133 install: dev:make
1134
1135 dev:make: make
1136
1137 dev:make:: xyz
1138
1139 The rule is that it's only a target, if there are TWO :'s
1140 OR a space around the :.
1141 */
1142 if (p && !(isspace ((unsigned char)p[1]) || !p[1]
1143 || isspace ((unsigned char)p[-1])))
1144 p = 0;
1145#endif
1146#ifdef HAVE_DOS_PATHS
1147 {
1148 int check_again;
1149 do {
1150 check_again = 0;
1151 /* For DOS-style paths, skip a "C:\..." or a "C:/..." */
1152 if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
1153 isalpha ((unsigned char)p[-1]) &&
1154 (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
1155 p = strchr (p + 1, ':');
1156 check_again = 1;
1157 }
1158 } while (check_again);
1159 }
1160#endif
1161 if (p != 0)
1162 {
1163 struct nameseq *target;
1164 target = PARSE_FILE_SEQ (&p2, struct nameseq, ':', NULL,
1165 PARSEFS_NOGLOB);
1166 ++p2;
1167 if (target == 0)
1168 fatal (fstart, _("missing target pattern"));
1169 else if (target->next != 0)
1170 fatal (fstart, _("multiple target patterns"));
1171 pattern_percent = find_percent_cached (&target->name);
1172 pattern = target->name;
1173 if (pattern_percent == 0)
1174 fatal (fstart, _("target pattern contains no `%%'"));
1175 free_ns (target);
1176 }
1177 else
1178 pattern = 0;
1179
1180 /* Strip leading and trailing whitespaces. */
1181 beg = p2;
1182 end = beg + strlen (beg) - 1;
1183 strip_whitespace (&beg, &end);
1184
1185 /* Put all the prerequisites here; they'll be parsed later. */
1186 if (beg <= end && *beg != '\0')
1187 depstr = xstrndup (beg, end - beg + 1);
1188 else
1189 depstr = 0;
1190
1191 commands_idx = 0;
1192 if (cmdleft != 0)
1193 {
1194 /* Semicolon means rest of line is a command. */
1195 unsigned int l = strlen (cmdleft);
1196
1197 cmds_started = fstart->lineno;
1198
1199 /* Add this command line to the buffer. */
1200 if (l + 2 > commands_len)
1201 {
1202 commands_len = (l + 2) * 2;
1203 commands = xrealloc (commands, commands_len);
1204 }
1205 memcpy (commands, cmdleft, l);
1206 commands_idx += l;
1207 commands[commands_idx++] = '\n';
1208 }
1209
1210 /* Determine if this target should be made default. We used to do
1211 this in record_files() but because of the delayed target recording
1212 and because preprocessor directives are legal in target's commands
1213 it is too late. Consider this fragment for example:
1214
1215 foo:
1216
1217 ifeq ($(.DEFAULT_GOAL),foo)
1218 ...
1219 endif
1220
1221 Because the target is not recorded until after ifeq directive is
1222 evaluated the .DEFAULT_GOAL does not contain foo yet as one
1223 would expect. Because of this we have to move the logic here. */
1224
1225 if (set_default && default_goal_var->value[0] == '\0')
1226 {
1227 const char *name;
1228 struct dep *d;
1229 struct nameseq *t = filenames;
1230
1231 for (; t != 0; t = t->next)
1232 {
1233 int reject = 0;
1234 name = t->name;
1235
1236 /* We have nothing to do if this is an implicit rule. */
1237 if (strchr (name, '%') != 0)
1238 break;
1239
1240 /* See if this target's name does not start with a `.',
1241 unless it contains a slash. */
1242 if (*name == '.' && strchr (name, '/') == 0
1243#ifdef HAVE_DOS_PATHS
1244 && strchr (name, '\\') == 0
1245#endif
1246 )
1247 continue;
1248
1249
1250 /* If this file is a suffix, don't let it be
1251 the default goal file. */
1252 for (d = suffix_file->deps; d != 0; d = d->next)
1253 {
1254 register struct dep *d2;
1255 if (*dep_name (d) != '.' && streq (name, dep_name (d)))
1256 {
1257 reject = 1;
1258 break;
1259 }
1260 for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
1261 {
1262 unsigned int l = strlen (dep_name (d2));
1263 if (!strneq (name, dep_name (d2), l))
1264 continue;
1265 if (streq (name + l, dep_name (d)))
1266 {
1267 reject = 1;
1268 break;
1269 }
1270 }
1271
1272 if (reject)
1273 break;
1274 }
1275
1276 if (!reject)
1277 {
1278 define_variable_global (".DEFAULT_GOAL", 13, t->name,
1279 o_file, 0, NILF);
1280 break;
1281 }
1282 }
1283 }
1284
1285 continue;
1286 }
1287
1288 /* We get here except in the case that we just read a rule line.
1289 Record now the last rule we read, so following spurious
1290 commands are properly diagnosed. */
1291 rule_complete:
1292 record_waiting_files ();
1293 }
1294
1295#undef word1eq
1296
1297 if (conditionals->if_cmds)
1298 fatal (fstart, _("missing `endif'"));
1299
1300 /* At eof, record the last rule. */
1301 record_waiting_files ();
1302
1303 if (collapsed)
1304 free (collapsed);
1305 free (commands);
1306}
1307
1308
1309
1310/* Remove comments from LINE.
1311 This is done by copying the text at LINE onto itself. */
1312
1313static void
1314remove_comments (char *line)
1315{
1316 char *comment;
1317
1318 comment = find_char_unquote (line, '#', 0, 0, 0);
1319
1320 if (comment != 0)
1321 /* Cut off the line at the #. */
1322 *comment = '\0';
1323}
1324
1325/* Execute a `undefine' directive.
1326 The undefine line has already been read, and NAME is the name of
1327 the variable to be undefined. */
1328
1329static void
1330do_undefine (char *name, enum variable_origin origin, struct ebuffer *ebuf)
1331{
1332 char *p, *var;
1333
1334 /* Expand the variable name and find the beginning (NAME) and end. */
1335 var = allocated_variable_expand (name);
1336 name = next_token (var);
1337 if (*name == '\0')
1338 fatal (&ebuf->floc, _("empty variable name"));
1339 p = name + strlen (name) - 1;
1340 while (p > name && isblank ((unsigned char)*p))
1341 --p;
1342 p[1] = '\0';
1343
1344 undefine_variable_global (name, p - name + 1, origin);
1345 free (var);
1346}
1347
1348/* Execute a `define' directive.
1349 The first line has already been read, and NAME is the name of
1350 the variable to be defined. The following lines remain to be read. */
1351
1352static struct variable *
1353do_define (char *name, enum variable_origin origin, struct ebuffer *ebuf)
1354{
1355 struct variable *v;
1356 enum variable_flavor flavor;
1357 struct floc defstart;
1358 int nlevels = 1;
1359 unsigned int length = 100;
1360 char *definition = xmalloc (length);
1361 unsigned int idx = 0;
1362 char *p, *var;
1363
1364 defstart = ebuf->floc;
1365
1366 p = parse_variable_definition (name, &flavor);
1367 if (p == NULL)
1368 /* No assignment token, so assume recursive. */
1369 flavor = f_recursive;
1370 else
1371 {
1372 if (*(next_token (p)) != '\0')
1373 error (&defstart, _("extraneous text after `define' directive"));
1374
1375 /* Chop the string before the assignment token to get the name. */
1376 p[flavor == f_recursive ? -1 : -2] = '\0';
1377 }
1378
1379 /* Expand the variable name and find the beginning (NAME) and end. */
1380 var = allocated_variable_expand (name);
1381 name = next_token (var);
1382 if (*name == '\0')
1383 fatal (&defstart, _("empty variable name"));
1384 p = name + strlen (name) - 1;
1385 while (p > name && isblank ((unsigned char)*p))
1386 --p;
1387 p[1] = '\0';
1388
1389 /* Now read the value of the variable. */
1390 while (1)
1391 {
1392 unsigned int len;
1393 char *line;
1394 long nlines = readline (ebuf);
1395
1396 /* If there is nothing left to be eval'd, there's no 'endef'!! */
1397 if (nlines < 0)
1398 fatal (&defstart, _("missing `endef', unterminated `define'"));
1399
1400 ebuf->floc.lineno += nlines;
1401 line = ebuf->buffer;
1402
1403 collapse_continuations (line);
1404
1405 /* If the line doesn't begin with a tab, test to see if it introduces
1406 another define, or ends one. Stop if we find an 'endef' */
1407 if (line[0] != cmd_prefix)
1408 {
1409 p = next_token (line);
1410 len = strlen (p);
1411
1412 /* If this is another 'define', increment the level count. */
1413 if ((len == 6 || (len > 6 && isblank ((unsigned char)p[6])))
1414 && strneq (p, "define", 6))
1415 ++nlevels;
1416
1417 /* If this is an 'endef', decrement the count. If it's now 0,
1418 we've found the last one. */
1419 else if ((len == 5 || (len > 5 && isblank ((unsigned char)p[5])))
1420 && strneq (p, "endef", 5))
1421 {
1422 p += 5;
1423 remove_comments (p);
1424 if (*(next_token (p)) != '\0')
1425 error (&ebuf->floc,
1426 _("extraneous text after `endef' directive"));
1427
1428 if (--nlevels == 0)
1429 break;
1430 }
1431 }
1432
1433 /* Add this line to the variable definition. */
1434 len = strlen (line);
1435 if (idx + len + 1 > length)
1436 {
1437 length = (idx + len) * 2;
1438 definition = xrealloc (definition, length + 1);
1439 }
1440
1441 memcpy (&definition[idx], line, len);
1442 idx += len;
1443 /* Separate lines with a newline. */
1444 definition[idx++] = '\n';
1445 }
1446
1447 /* We've got what we need; define the variable. */
1448 if (idx == 0)
1449 definition[0] = '\0';
1450 else
1451 definition[idx - 1] = '\0';
1452
1453 v = do_variable_definition (&defstart, name, definition, origin, flavor, 0);
1454 free (definition);
1455 free (var);
1456 return (v);
1457}
1458
1459
1460/* Interpret conditional commands "ifdef", "ifndef", "ifeq",
1461 "ifneq", "else" and "endif".
1462 LINE is the input line, with the command as its first word.
1463
1464 FILENAME and LINENO are the filename and line number in the
1465 current makefile. They are used for error messages.
1466
1467 Value is -2 if the line is not a conditional at all,
1468 -1 if the line is an invalid conditional,
1469 0 if following text should be interpreted,
1470 1 if following text should be ignored. */
1471
1472static int
1473conditional_line (char *line, int len, const struct floc *flocp)
1474{
1475 char *cmdname;
1476 enum { c_ifdef, c_ifndef, c_ifeq, c_ifneq, c_else, c_endif } cmdtype;
1477 unsigned int i;
1478 unsigned int o;
1479
1480 /* Compare a word, both length and contents. */
1481#define word1eq(s) (len == sizeof(s)-1 && strneq (s, line, sizeof(s)-1))
1482#define chkword(s, t) if (word1eq (s)) { cmdtype = (t); cmdname = (s); }
1483
1484 /* Make sure this line is a conditional. */
1485 chkword ("ifdef", c_ifdef)
1486 else chkword ("ifndef", c_ifndef)
1487 else chkword ("ifeq", c_ifeq)
1488 else chkword ("ifneq", c_ifneq)
1489 else chkword ("else", c_else)
1490 else chkword ("endif", c_endif)
1491 else
1492 return -2;
1493
1494 /* Found one: skip past it and any whitespace after it. */
1495 line = next_token (line + len);
1496
1497#define EXTRANEOUS() error (flocp, _("Extraneous text after `%s' directive"), cmdname)
1498
1499 /* An 'endif' cannot contain extra text, and reduces the if-depth by 1 */
1500 if (cmdtype == c_endif)
1501 {
1502 if (*line != '\0')
1503 EXTRANEOUS ();
1504
1505 if (!conditionals->if_cmds)
1506 fatal (flocp, _("extraneous `%s'"), cmdname);
1507
1508 --conditionals->if_cmds;
1509
1510 goto DONE;
1511 }
1512
1513 /* An 'else' statement can either be simple, or it can have another
1514 conditional after it. */
1515 if (cmdtype == c_else)
1516 {
1517 const char *p;
1518
1519 if (!conditionals->if_cmds)
1520 fatal (flocp, _("extraneous `%s'"), cmdname);
1521
1522 o = conditionals->if_cmds - 1;
1523
1524 if (conditionals->seen_else[o])
1525 fatal (flocp, _("only one `else' per conditional"));
1526
1527 /* Change the state of ignorance. */
1528 switch (conditionals->ignoring[o])
1529 {
1530 case 0:
1531 /* We've just been interpreting. Never do it again. */
1532 conditionals->ignoring[o] = 2;
1533 break;
1534 case 1:
1535 /* We've never interpreted yet. Maybe this time! */
1536 conditionals->ignoring[o] = 0;
1537 break;
1538 }
1539
1540 /* It's a simple 'else'. */
1541 if (*line == '\0')
1542 {
1543 conditionals->seen_else[o] = 1;
1544 goto DONE;
1545 }
1546
1547 /* The 'else' has extra text. That text must be another conditional
1548 and cannot be an 'else' or 'endif'. */
1549
1550 /* Find the length of the next word. */
1551 for (p = line+1; *p != '\0' && !isspace ((unsigned char)*p); ++p)
1552 ;
1553 len = p - line;
1554
1555 /* If it's 'else' or 'endif' or an illegal conditional, fail. */
1556 if (word1eq("else") || word1eq("endif")
1557 || conditional_line (line, len, flocp) < 0)
1558 EXTRANEOUS ();
1559 else
1560 {
1561 /* conditional_line() created a new level of conditional.
1562 Raise it back to this level. */
1563 if (conditionals->ignoring[o] < 2)
1564 conditionals->ignoring[o] = conditionals->ignoring[o+1];
1565 --conditionals->if_cmds;
1566 }
1567
1568 goto DONE;
1569 }
1570
1571 if (conditionals->allocated == 0)
1572 {
1573 conditionals->allocated = 5;
1574 conditionals->ignoring = xmalloc (conditionals->allocated);
1575 conditionals->seen_else = xmalloc (conditionals->allocated);
1576 }
1577
1578 o = conditionals->if_cmds++;
1579 if (conditionals->if_cmds > conditionals->allocated)
1580 {
1581 conditionals->allocated += 5;
1582 conditionals->ignoring = xrealloc (conditionals->ignoring,
1583 conditionals->allocated);
1584 conditionals->seen_else = xrealloc (conditionals->seen_else,
1585 conditionals->allocated);
1586 }
1587
1588 /* Record that we have seen an `if...' but no `else' so far. */
1589 conditionals->seen_else[o] = 0;
1590
1591 /* Search through the stack to see if we're already ignoring. */
1592 for (i = 0; i < o; ++i)
1593 if (conditionals->ignoring[i])
1594 {
1595 /* We are already ignoring, so just push a level to match the next
1596 "else" or "endif", and keep ignoring. We don't want to expand
1597 variables in the condition. */
1598 conditionals->ignoring[o] = 1;
1599 return 1;
1600 }
1601
1602 if (cmdtype == c_ifdef || cmdtype == c_ifndef)
1603 {
1604 char *var;
1605 struct variable *v;
1606 char *p;
1607
1608 /* Expand the thing we're looking up, so we can use indirect and
1609 constructed variable names. */
1610 var = allocated_variable_expand (line);
1611
1612 /* Make sure there's only one variable name to test. */
1613 p = end_of_token (var);
1614 i = p - var;
1615 p = next_token (p);
1616 if (*p != '\0')
1617 return -1;
1618
1619 var[i] = '\0';
1620 v = lookup_variable (var, i);
1621
1622 conditionals->ignoring[o] =
1623 ((v != 0 && *v->value != '\0') == (cmdtype == c_ifndef));
1624
1625 free (var);
1626 }
1627 else
1628 {
1629 /* "ifeq" or "ifneq". */
1630 char *s1, *s2;
1631 unsigned int l;
1632 char termin = *line == '(' ? ',' : *line;
1633
1634 if (termin != ',' && termin != '"' && termin != '\'')
1635 return -1;
1636
1637 s1 = ++line;
1638 /* Find the end of the first string. */
1639 if (termin == ',')
1640 {
1641 int count = 0;
1642 for (; *line != '\0'; ++line)
1643 if (*line == '(')
1644 ++count;
1645 else if (*line == ')')
1646 --count;
1647 else if (*line == ',' && count <= 0)
1648 break;
1649 }
1650 else
1651 while (*line != '\0' && *line != termin)
1652 ++line;
1653
1654 if (*line == '\0')
1655 return -1;
1656
1657 if (termin == ',')
1658 {
1659 /* Strip blanks after the first string. */
1660 char *p = line++;
1661 while (isblank ((unsigned char)p[-1]))
1662 --p;
1663 *p = '\0';
1664 }
1665 else
1666 *line++ = '\0';
1667
1668 s2 = variable_expand (s1);
1669 /* We must allocate a new copy of the expanded string because
1670 variable_expand re-uses the same buffer. */
1671 l = strlen (s2);
1672 s1 = alloca (l + 1);
1673 memcpy (s1, s2, l + 1);
1674
1675 if (termin != ',')
1676 /* Find the start of the second string. */
1677 line = next_token (line);
1678
1679 termin = termin == ',' ? ')' : *line;
1680 if (termin != ')' && termin != '"' && termin != '\'')
1681 return -1;
1682
1683 /* Find the end of the second string. */
1684 if (termin == ')')
1685 {
1686 int count = 0;
1687 s2 = next_token (line);
1688 for (line = s2; *line != '\0'; ++line)
1689 {
1690 if (*line == '(')
1691 ++count;
1692 else if (*line == ')')
1693 {
1694 if (count <= 0)
1695 break;
1696 else
1697 --count;
1698 }
1699 }
1700 }
1701 else
1702 {
1703 ++line;
1704 s2 = line;
1705 while (*line != '\0' && *line != termin)
1706 ++line;
1707 }
1708
1709 if (*line == '\0')
1710 return -1;
1711
1712 *line = '\0';
1713 line = next_token (++line);
1714 if (*line != '\0')
1715 EXTRANEOUS ();
1716
1717 s2 = variable_expand (s2);
1718 conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
1719 }
1720
1721 DONE:
1722 /* Search through the stack to see if we're ignoring. */
1723 for (i = 0; i < conditionals->if_cmds; ++i)
1724 if (conditionals->ignoring[i])
1725 return 1;
1726 return 0;
1727}
1728
1729
1730
1731/* Record target-specific variable values for files FILENAMES.
1732 TWO_COLON is nonzero if a double colon was used.
1733
1734 The links of FILENAMES are freed, and so are any names in it
1735 that are not incorporated into other data structures.
1736
1737 If the target is a pattern, add the variable to the pattern-specific
1738 variable value list. */
1739
1740static void
1741record_target_var (struct nameseq *filenames, char *defn,
1742 enum variable_origin origin, struct vmodifiers *vmod,
1743 const struct floc *flocp)
1744{
1745 struct nameseq *nextf;
1746 struct variable_set_list *global;
1747
1748 global = current_variable_set_list;
1749
1750 /* If the variable is an append version, store that but treat it as a
1751 normal recursive variable. */
1752
1753 for (; filenames != 0; filenames = nextf)
1754 {
1755 struct variable *v;
1756 const char *name = filenames->name;
1757 const char *fname;
1758 const char *percent;
1759 struct pattern_var *p;
1760
1761 nextf = filenames->next;
1762 free_ns (filenames);
1763
1764 /* If it's a pattern target, then add it to the pattern-specific
1765 variable list. */
1766 percent = find_percent_cached (&name);
1767 if (percent)
1768 {
1769 /* Get a reference for this pattern-specific variable struct. */
1770 p = create_pattern_var (name, percent);
1771 p->variable.fileinfo = *flocp;
1772 /* I don't think this can fail since we already determined it was a
1773 variable definition. */
1774 v = assign_variable_definition (&p->variable, defn);
1775 assert (v != 0);
1776
1777 v->origin = origin;
1778 if (v->flavor == f_simple)
1779 v->value = allocated_variable_expand (v->value);
1780 else
1781 v->value = xstrdup (v->value);
1782
1783 fname = p->target;
1784 }
1785 else
1786 {
1787 struct file *f;
1788
1789 /* Get a file reference for this file, and initialize it.
1790 We don't want to just call enter_file() because that allocates a
1791 new entry if the file is a double-colon, which we don't want in
1792 this situation. */
1793 f = lookup_file (name);
1794 if (!f)
1795 f = enter_file (strcache_add (name));
1796 else if (f->double_colon)
1797 f = f->double_colon;
1798
1799 initialize_file_variables (f, 1);
1800 fname = f->name;
1801
1802 current_variable_set_list = f->variables;
1803 v = try_variable_definition (flocp, defn, origin, 1);
1804 if (!v)
1805 fatal (flocp, _("Malformed target-specific variable definition"));
1806 current_variable_set_list = global;
1807 }
1808
1809 /* Set up the variable to be *-specific. */
1810 v->per_target = 1;
1811 v->private_var = vmod->private_v;
1812 v->export = vmod->export_v ? v_export : v_default;
1813
1814 /* If it's not an override, check to see if there was a command-line
1815 setting. If so, reset the value. */
1816 if (v->origin != o_override)
1817 {
1818 struct variable *gv;
1819 int len = strlen(v->name);
1820
1821 gv = lookup_variable (v->name, len);
1822 if (gv && (gv->origin == o_env_override || gv->origin == o_command))
1823 {
1824 if (v->value != 0)
1825 free (v->value);
1826 v->value = xstrdup (gv->value);
1827 v->origin = gv->origin;
1828 v->recursive = gv->recursive;
1829 v->append = 0;
1830 }
1831 }
1832 }
1833}
1834
1835
1836/* Record a description line for files FILENAMES,
1837 with dependencies DEPS, commands to execute described
1838 by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
1839 TWO_COLON is nonzero if a double colon was used.
1840 If not nil, PATTERN is the `%' pattern to make this
1841 a static pattern rule, and PATTERN_PERCENT is a pointer
1842 to the `%' within it.
1843
1844 The links of FILENAMES are freed, and so are any names in it
1845 that are not incorporated into other data structures. */
1846
1847static void
1848record_files (struct nameseq *filenames, const char *pattern,
1849 const char *pattern_percent, char *depstr,
1850 unsigned int cmds_started, char *commands,
1851 unsigned int commands_idx, int two_colon,
1852 const struct floc *flocp)
1853{
1854 struct commands *cmds;
1855 struct dep *deps;
1856 const char *implicit_percent;
1857 const char *name;
1858
1859 /* If we've already snapped deps, that means we're in an eval being
1860 resolved after the makefiles have been read in. We can't add more rules
1861 at this time, since they won't get snapped and we'll get core dumps.
1862 See Savannah bug # 12124. */
1863 if (snapped_deps)
1864 fatal (flocp, _("prerequisites cannot be defined in recipes"));
1865
1866 /* Determine if this is a pattern rule or not. */
1867 name = filenames->name;
1868 implicit_percent = find_percent_cached (&name);
1869
1870 /* If there's a recipe, set up a struct for it. */
1871 if (commands_idx > 0)
1872 {
1873 cmds = xmalloc (sizeof (struct commands));
1874 cmds->fileinfo.filenm = flocp->filenm;
1875 cmds->fileinfo.lineno = cmds_started;
1876 cmds->commands = xstrndup (commands, commands_idx);
1877 cmds->command_lines = 0;
1878 }
1879 else
1880 cmds = 0;
1881
1882 /* If there's a prereq string then parse it--unless it's eligible for 2nd
1883 expansion: if so, snap_deps() will do it. */
1884 if (depstr == 0)
1885 deps = 0;
1886 else if (second_expansion && strchr (depstr, '$'))
1887 {
1888 deps = alloc_dep ();
1889 deps->name = depstr;
1890 deps->need_2nd_expansion = 1;
1891 deps->staticpattern = pattern != 0;
1892 }
1893 else
1894 {
1895 deps = split_prereqs (depstr);
1896 free (depstr);
1897
1898 /* We'll enter static pattern prereqs later when we have the stem. We
1899 don't want to enter pattern rules at all so that we don't think that
1900 they ought to exist (make manual "Implicit Rule Search Algorithm",
1901 item 5c). */
1902 if (! pattern && ! implicit_percent)
1903 deps = enter_prereqs (deps, NULL);
1904 }
1905
1906 /* For implicit rules, _all_ the targets must have a pattern. That means we
1907 can test the first one to see if we're working with an implicit rule; if
1908 so we handle it specially. */
1909
1910 if (implicit_percent)
1911 {
1912 struct nameseq *nextf;
1913 const char **targets, **target_pats;
1914 unsigned int c;
1915
1916 if (pattern != 0)
1917 fatal (flocp, _("mixed implicit and static pattern rules"));
1918
1919 /* Count the targets to create an array of target names.
1920 We already have the first one. */
1921 nextf = filenames->next;
1922 free_ns (filenames);
1923 filenames = nextf;
1924
1925 for (c = 1; nextf; ++c, nextf = nextf->next)
1926 ;
1927 targets = xmalloc (c * sizeof (const char *));
1928 target_pats = xmalloc (c * sizeof (const char *));
1929
1930 targets[0] = name;
1931 target_pats[0] = implicit_percent;
1932
1933 c = 1;
1934 while (filenames)
1935 {
1936 name = filenames->name;
1937 implicit_percent = find_percent_cached (&name);
1938
1939 if (implicit_percent == 0)
1940 fatal (flocp, _("mixed implicit and normal rules"));
1941
1942 targets[c] = name;
1943 target_pats[c] = implicit_percent;
1944 ++c;
1945
1946 nextf = filenames->next;
1947 free_ns (filenames);
1948 filenames = nextf;
1949 }
1950
1951 create_pattern_rule (targets, target_pats, c, two_colon, deps, cmds, 1);
1952
1953 return;
1954 }
1955
1956
1957 /* Walk through each target and create it in the database.
1958 We already set up the first target, above. */
1959 while (1)
1960 {
1961 struct nameseq *nextf = filenames->next;
1962 struct file *f;
1963 struct dep *this = 0;
1964
1965 free_ns (filenames);
1966
1967 /* Check for special targets. Do it here instead of, say, snap_deps()
1968 so that we can immediately use the value. */
1969 if (streq (name, ".POSIX"))
1970 {
1971 posix_pedantic = 1;
1972 define_variable_cname (".SHELLFLAGS", "-ec", o_default, 0);
1973 }
1974 else if (streq (name, ".SECONDEXPANSION"))
1975 second_expansion = 1;
1976#if !defined(WINDOWS32) && !defined (__MSDOS__) && !defined (__EMX__)
1977 else if (streq (name, ".ONESHELL"))
1978 one_shell = 1;
1979#endif
1980
1981 /* If this is a static pattern rule:
1982 `targets: target%pattern: prereq%pattern; recipe',
1983 make sure the pattern matches this target name. */
1984 if (pattern && !pattern_matches (pattern, pattern_percent, name))
1985 error (flocp, _("target `%s' doesn't match the target pattern"), name);
1986 else if (deps)
1987 /* If there are multiple targets, copy the chain DEPS for all but the
1988 last one. It is not safe for the same deps to go in more than one
1989 place in the database. */
1990 this = nextf != 0 ? copy_dep_chain (deps) : deps;
1991
1992 /* Find or create an entry in the file database for this target. */
1993 if (!two_colon)
1994 {
1995 /* Single-colon. Combine this rule with the file's existing record,
1996 if any. */
1997 f = enter_file (strcache_add (name));
1998 if (f->double_colon)
1999 fatal (flocp,
2000 _("target file `%s' has both : and :: entries"), f->name);
2001
2002 /* If CMDS == F->CMDS, this target was listed in this rule
2003 more than once. Just give a warning since this is harmless. */
2004 if (cmds != 0 && cmds == f->cmds)
2005 error (flocp,
2006 _("target `%s' given more than once in the same rule."),
2007 f->name);
2008
2009 /* Check for two single-colon entries both with commands.
2010 Check is_target so that we don't lose on files such as .c.o
2011 whose commands were preinitialized. */
2012 else if (cmds != 0 && f->cmds != 0 && f->is_target)
2013 {
2014 error (&cmds->fileinfo,
2015 _("warning: overriding recipe for target `%s'"),
2016 f->name);
2017 error (&f->cmds->fileinfo,
2018 _("warning: ignoring old recipe for target `%s'"),
2019 f->name);
2020 }
2021
2022 /* Defining .DEFAULT with no deps or cmds clears it. */
2023 if (f == default_file && this == 0 && cmds == 0)
2024 f->cmds = 0;
2025 if (cmds != 0)
2026 f->cmds = cmds;
2027
2028 /* Defining .SUFFIXES with no dependencies clears out the list of
2029 suffixes. */
2030 if (f == suffix_file && this == 0)
2031 {
2032 free_dep_chain (f->deps);
2033 f->deps = 0;
2034 }
2035 }
2036 else
2037 {
2038 /* Double-colon. Make a new record even if there already is one. */
2039 f = lookup_file (name);
2040
2041 /* Check for both : and :: rules. Check is_target so we don't lose
2042 on default suffix rules or makefiles. */
2043 if (f != 0 && f->is_target && !f->double_colon)
2044 fatal (flocp,
2045 _("target file `%s' has both : and :: entries"), f->name);
2046
2047 f = enter_file (strcache_add (name));
2048 /* If there was an existing entry and it was a double-colon entry,
2049 enter_file will have returned a new one, making it the prev
2050 pointer of the old one, and setting its double_colon pointer to
2051 the first one. */
2052 if (f->double_colon == 0)
2053 /* This is the first entry for this name, so we must set its
2054 double_colon pointer to itself. */
2055 f->double_colon = f;
2056
2057 f->cmds = cmds;
2058 }
2059
2060 f->is_target = 1;
2061
2062 /* If this is a static pattern rule, set the stem to the part of its
2063 name that matched the `%' in the pattern, so you can use $* in the
2064 commands. If we didn't do it before, enter the prereqs now. */
2065 if (pattern)
2066 {
2067 static const char *percent = "%";
2068 char *buffer = variable_expand ("");
2069 char *o = patsubst_expand_pat (buffer, name, pattern, percent,
2070 pattern_percent+1, percent+1);
2071 f->stem = strcache_add_len (buffer, o - buffer);
2072 if (this)
2073 {
2074 if (! this->need_2nd_expansion)
2075 this = enter_prereqs (this, f->stem);
2076 else
2077 this->stem = f->stem;
2078 }
2079 }
2080
2081 /* Add the dependencies to this file entry. */
2082 if (this != 0)
2083 {
2084 /* Add the file's old deps and the new ones in THIS together. */
2085 if (f->deps == 0)
2086 f->deps = this;
2087 else if (cmds != 0)
2088 {
2089 struct dep *d = this;
2090
2091 /* If this rule has commands, put these deps first. */
2092 while (d->next != 0)
2093 d = d->next;
2094
2095 d->next = f->deps;
2096 f->deps = this;
2097 }
2098 else
2099 {
2100 struct dep *d = f->deps;
2101
2102 /* A rule without commands: put its prereqs at the end. */
2103 while (d->next != 0)
2104 d = d->next;
2105
2106 d->next = this;
2107 }
2108 }
2109
2110 name = f->name;
2111
2112 /* All done! Set up for the next one. */
2113 if (nextf == 0)
2114 break;
2115
2116 filenames = nextf;
2117
2118 /* Reduce escaped percents. If there are any unescaped it's an error */
2119 name = filenames->name;
2120 if (find_percent_cached (&name))
2121 fatal (flocp, _("mixed implicit and normal rules"));
2122 }
2123}
2124
2125
2126/* Search STRING for an unquoted STOPCHAR or blank (if BLANK is nonzero).
2127 Backslashes quote STOPCHAR, blanks if BLANK is nonzero, and backslash.
2128 Quoting backslashes are removed from STRING by compacting it into
2129 itself. Returns a pointer to the first unquoted STOPCHAR if there is
2130 one, or nil if there are none. STOPCHARs inside variable references are
2131 ignored if IGNOREVARS is true.
2132
2133 STOPCHAR _cannot_ be '$' if IGNOREVARS is true. */
2134
2135static char *
2136find_char_unquote (char *string, int stop1, int stop2, int blank,
2137 int ignorevars)
2138{
2139 unsigned int string_len = 0;
2140 char *p = string;
2141
2142 if (ignorevars)
2143 ignorevars = '$';
2144
2145 while (1)
2146 {
2147 if (stop2 && blank)
2148 while (*p != '\0' && *p != ignorevars && *p != stop1 && *p != stop2
2149 && ! isblank ((unsigned char) *p))
2150 ++p;
2151 else if (stop2)
2152 while (*p != '\0' && *p != ignorevars && *p != stop1 && *p != stop2)
2153 ++p;
2154 else if (blank)
2155 while (*p != '\0' && *p != ignorevars && *p != stop1
2156 && ! isblank ((unsigned char) *p))
2157 ++p;
2158 else
2159 while (*p != '\0' && *p != ignorevars && *p != stop1)
2160 ++p;
2161
2162 if (*p == '\0')
2163 break;
2164
2165 /* If we stopped due to a variable reference, skip over its contents. */
2166 if (*p == ignorevars)
2167 {
2168 char openparen = p[1];
2169
2170 p += 2;
2171
2172 /* Skip the contents of a non-quoted, multi-char variable ref. */
2173 if (openparen == '(' || openparen == '{')
2174 {
2175 unsigned int pcount = 1;
2176 char closeparen = (openparen == '(' ? ')' : '}');
2177
2178 while (*p)
2179 {
2180 if (*p == openparen)
2181 ++pcount;
2182 else if (*p == closeparen)
2183 if (--pcount == 0)
2184 {
2185 ++p;
2186 break;
2187 }
2188 ++p;
2189 }
2190 }
2191
2192 /* Skipped the variable reference: look for STOPCHARS again. */
2193 continue;
2194 }
2195
2196 if (p > string && p[-1] == '\\')
2197 {
2198 /* Search for more backslashes. */
2199 int i = -2;
2200 while (&p[i] >= string && p[i] == '\\')
2201 --i;
2202 ++i;
2203 /* Only compute the length if really needed. */
2204 if (string_len == 0)
2205 string_len = strlen (string);
2206 /* The number of backslashes is now -I.
2207 Copy P over itself to swallow half of them. */
2208 memmove (&p[i], &p[i/2], (string_len - (p - string)) - (i/2) + 1);
2209 p += i/2;
2210 if (i % 2 == 0)
2211 /* All the backslashes quoted each other; the STOPCHAR was
2212 unquoted. */
2213 return p;
2214
2215 /* The STOPCHAR was quoted by a backslash. Look for another. */
2216 }
2217 else
2218 /* No backslash in sight. */
2219 return p;
2220 }
2221
2222 /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
2223 return 0;
2224}
2225
2226/* Search PATTERN for an unquoted % and handle quoting. */
2227
2228char *
2229find_percent (char *pattern)
2230{
2231 return find_char_unquote (pattern, '%', 0, 0, 0);
2232}
2233
2234/* Search STRING for an unquoted % and handle quoting. Returns a pointer to
2235 the % or NULL if no % was found.
2236 This version is used with strings in the string cache: if there's a need to
2237 modify the string a new version will be added to the string cache and
2238 *STRING will be set to that. */
2239
2240const char *
2241find_percent_cached (const char **string)
2242{
2243 const char *p = *string;
2244 char *new = 0;
2245 int slen = 0;
2246
2247 /* If the first char is a % return now. This lets us avoid extra tests
2248 inside the loop. */
2249 if (*p == '%')
2250 return p;
2251
2252 while (1)
2253 {
2254 while (*p != '\0' && *p != '%')
2255 ++p;
2256
2257 if (*p == '\0')
2258 break;
2259
2260 /* See if this % is escaped with a backslash; if not we're done. */
2261 if (p[-1] != '\\')
2262 break;
2263
2264 {
2265 /* Search for more backslashes. */
2266 char *pv;
2267 int i = -2;
2268
2269 while (&p[i] >= *string && p[i] == '\\')
2270 --i;
2271 ++i;
2272
2273 /* At this point we know we'll need to allocate a new string.
2274 Make a copy if we haven't yet done so. */
2275 if (! new)
2276 {
2277 slen = strlen (*string);
2278 new = alloca (slen + 1);
2279 memcpy (new, *string, slen + 1);
2280 p = new + (p - *string);
2281 *string = new;
2282 }
2283
2284 /* At this point *string, p, and new all point into the same string.
2285 Get a non-const version of p so we can modify new. */
2286 pv = new + (p - *string);
2287
2288 /* The number of backslashes is now -I.
2289 Copy P over itself to swallow half of them. */
2290 memmove (&pv[i], &pv[i/2], (slen - (pv - new)) - (i/2) + 1);
2291 p += i/2;
2292
2293 /* If the backslashes quoted each other; the % was unquoted. */
2294 if (i % 2 == 0)
2295 break;
2296 }
2297 }
2298
2299 /* If we had to change STRING, add it to the strcache. */
2300 if (new)
2301 {
2302 *string = strcache_add (*string);
2303 p = *string + (p - new);
2304 }
2305
2306 /* If we didn't find a %, return NULL. Otherwise return a ptr to it. */
2307 return (*p == '\0') ? NULL : p;
2308}
2309
2310
2311/* Find the next line of text in an eval buffer, combining continuation lines
2312 into one line.
2313 Return the number of actual lines read (> 1 if continuation lines).
2314 Returns -1 if there's nothing left in the buffer.
2315
2316 After this function, ebuf->buffer points to the first character of the
2317 line we just found.
2318 */
2319
2320/* Read a line of text from a STRING.
2321 Since we aren't really reading from a file, don't bother with linenumbers.
2322 */
2323
2324static unsigned long
2325readstring (struct ebuffer *ebuf)
2326{
2327 char *eol;
2328
2329 /* If there is nothing left in this buffer, return 0. */
2330 if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
2331 return -1;
2332
2333 /* Set up a new starting point for the buffer, and find the end of the
2334 next logical line (taking into account backslash/newline pairs). */
2335
2336 eol = ebuf->buffer = ebuf->bufnext;
2337
2338 while (1)
2339 {
2340 int backslash = 0;
2341 const char *bol = eol;
2342 const char *p;
2343
2344 /* Find the next newline. At EOS, stop. */
2345 p = eol = strchr (eol , '\n');
2346 if (!eol)
2347 {
2348 ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
2349 return 0;
2350 }
2351
2352 /* Found a newline; if it's escaped continue; else we're done. */
2353 while (p > bol && *(--p) == '\\')
2354 backslash = !backslash;
2355 if (!backslash)
2356 break;
2357 ++eol;
2358 }
2359
2360 /* Overwrite the newline char. */
2361 *eol = '\0';
2362 ebuf->bufnext = eol+1;
2363
2364 return 0;
2365}
2366
2367static long
2368readline (struct ebuffer *ebuf)
2369{
2370 char *p;
2371 char *end;
2372 char *start;
2373 long nlines = 0;
2374
2375 /* The behaviors between string and stream buffers are different enough to
2376 warrant different functions. Do the Right Thing. */
2377
2378 if (!ebuf->fp)
2379 return readstring (ebuf);
2380
2381 /* When reading from a file, we always start over at the beginning of the
2382 buffer for each new line. */
2383
2384 p = start = ebuf->bufstart;
2385 end = p + ebuf->size;
2386 *p = '\0';
2387
2388 while (fgets (p, end - p, ebuf->fp) != 0)
2389 {
2390 char *p2;
2391 unsigned long len;
2392 int backslash;
2393
2394 len = strlen (p);
2395 if (len == 0)
2396 {
2397 /* This only happens when the first thing on the line is a '\0'.
2398 It is a pretty hopeless case, but (wonder of wonders) Athena
2399 lossage strikes again! (xmkmf puts NULs in its makefiles.)
2400 There is nothing really to be done; we synthesize a newline so
2401 the following line doesn't appear to be part of this line. */
2402 error (&ebuf->floc,
2403 _("warning: NUL character seen; rest of line ignored"));
2404 p[0] = '\n';
2405 len = 1;
2406 }
2407
2408 /* Jump past the text we just read. */
2409 p += len;
2410
2411 /* If the last char isn't a newline, the whole line didn't fit into the
2412 buffer. Get some more buffer and try again. */
2413 if (p[-1] != '\n')
2414 goto more_buffer;
2415
2416 /* We got a newline, so add one to the count of lines. */
2417 ++nlines;
2418
2419#if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
2420 /* Check to see if the line was really ended with CRLF; if so ignore
2421 the CR. */
2422 if ((p - start) > 1 && p[-2] == '\r')
2423 {
2424 --p;
2425 p[-1] = '\n';
2426 }
2427#endif
2428
2429 backslash = 0;
2430 for (p2 = p - 2; p2 >= start; --p2)
2431 {
2432 if (*p2 != '\\')
2433 break;
2434 backslash = !backslash;
2435 }
2436
2437 if (!backslash)
2438 {
2439 p[-1] = '\0';
2440 break;
2441 }
2442
2443 /* It was a backslash/newline combo. If we have more space, read
2444 another line. */
2445 if (end - p >= 80)
2446 continue;
2447
2448 /* We need more space at the end of our buffer, so realloc it.
2449 Make sure to preserve the current offset of p. */
2450 more_buffer:
2451 {
2452 unsigned long off = p - start;
2453 ebuf->size *= 2;
2454 start = ebuf->buffer = ebuf->bufstart = xrealloc (start, ebuf->size);
2455 p = start + off;
2456 end = start + ebuf->size;
2457 *p = '\0';
2458 }
2459 }
2460
2461 if (ferror (ebuf->fp))
2462 pfatal_with_name (ebuf->floc.filenm);
2463
2464 /* If we found some lines, return how many.
2465 If we didn't, but we did find _something_, that indicates we read the last
2466 line of a file with no final newline; return 1.
2467 If we read nothing, we're at EOF; return -1. */
2468
2469 return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
2470}
2471
2472
2473/* Parse the next "makefile word" from the input buffer, and return info
2474 about it.
2475
2476 A "makefile word" is one of:
2477
2478 w_bogus Should never happen
2479 w_eol End of input
2480 w_static A static word; cannot be expanded
2481 w_variable A word containing one or more variables/functions
2482 w_colon A colon
2483 w_dcolon A double-colon
2484 w_semicolon A semicolon
2485 w_varassign A variable assignment operator (=, :=, +=, or ?=)
2486
2487 Note that this function is only used when reading certain parts of the
2488 makefile. Don't use it where special rules hold sway (RHS of a variable,
2489 in a command list, etc.) */
2490
2491static enum make_word_type
2492get_next_mword (char *buffer, char *delim, char **startp, unsigned int *length)
2493{
2494 enum make_word_type wtype = w_bogus;
2495 char *p = buffer, *beg;
2496 char c;
2497
2498 /* Skip any leading whitespace. */
2499 while (isblank ((unsigned char)*p))
2500 ++p;
2501
2502 beg = p;
2503 c = *(p++);
2504 switch (c)
2505 {
2506 case '\0':
2507 wtype = w_eol;
2508 break;
2509
2510 case ';':
2511 wtype = w_semicolon;
2512 break;
2513
2514 case '=':
2515 wtype = w_varassign;
2516 break;
2517
2518 case ':':
2519 wtype = w_colon;
2520 switch (*p)
2521 {
2522 case ':':
2523 ++p;
2524 wtype = w_dcolon;
2525 break;
2526
2527 case '=':
2528 ++p;
2529 wtype = w_varassign;
2530 break;
2531 }
2532 break;
2533
2534 case '+':
2535 case '?':
2536 if (*p == '=')
2537 {
2538 ++p;
2539 wtype = w_varassign;
2540 break;
2541 }
2542
2543 default:
2544 if (delim && strchr (delim, c))
2545 wtype = w_static;
2546 break;
2547 }
2548
2549 /* Did we find something? If so, return now. */
2550 if (wtype != w_bogus)
2551 goto done;
2552
2553 /* This is some non-operator word. A word consists of the longest
2554 string of characters that doesn't contain whitespace, one of [:=#],
2555 or [?+]=, or one of the chars in the DELIM string. */
2556
2557 /* We start out assuming a static word; if we see a variable we'll
2558 adjust our assumptions then. */
2559 wtype = w_static;
2560
2561 /* We already found the first value of "c", above. */
2562 while (1)
2563 {
2564 char closeparen;
2565 int count;
2566
2567 switch (c)
2568 {
2569 case '\0':
2570 case ' ':
2571 case '\t':
2572 case '=':
2573 goto done_word;
2574
2575 case ':':
2576#ifdef HAVE_DOS_PATHS
2577 /* A word CAN include a colon in its drive spec. The drive
2578 spec is allowed either at the beginning of a word, or as part
2579 of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
2580 if (!(p - beg >= 2
2581 && (*p == '/' || *p == '\\') && isalpha ((unsigned char)p[-2])
2582 && (p - beg == 2 || p[-3] == '(')))
2583#endif
2584 goto done_word;
2585
2586 case '$':
2587 c = *(p++);
2588 if (c == '$')
2589 break;
2590
2591 /* This is a variable reference, so note that it's expandable.
2592 Then read it to the matching close paren. */
2593 wtype = w_variable;
2594
2595 if (c == '(')
2596 closeparen = ')';
2597 else if (c == '{')
2598 closeparen = '}';
2599 else
2600 /* This is a single-letter variable reference. */
2601 break;
2602
2603 for (count=0; *p != '\0'; ++p)
2604 {
2605 if (*p == c)
2606 ++count;
2607 else if (*p == closeparen && --count < 0)
2608 {
2609 ++p;
2610 break;
2611 }
2612 }
2613 break;
2614
2615 case '?':
2616 case '+':
2617 if (*p == '=')
2618 goto done_word;
2619 break;
2620
2621 case '\\':
2622 switch (*p)
2623 {
2624 case ':':
2625 case ';':
2626 case '=':
2627 case '\\':
2628 ++p;
2629 break;
2630 }
2631 break;
2632
2633 default:
2634 if (delim && strchr (delim, c))
2635 goto done_word;
2636 break;
2637 }
2638
2639 c = *(p++);
2640 }
2641 done_word:
2642 --p;
2643
2644 done:
2645 if (startp)
2646 *startp = beg;
2647 if (length)
2648 *length = p - beg;
2649 return wtype;
2650}
2651
2652
2653/* Construct the list of include directories
2654 from the arguments and the default list. */
2655
2656void
2657construct_include_path (const char **arg_dirs)
2658{
2659#ifdef VAXC /* just don't ask ... */
2660 stat_t stbuf;
2661#else
2662 struct stat stbuf;
2663#endif
2664 const char **dirs;
2665 const char **cpp;
2666 unsigned int idx;
2667
2668 /* Compute the number of pointers we need in the table. */
2669 idx = sizeof (default_include_directories) / sizeof (const char *);
2670 if (arg_dirs)
2671 for (cpp = arg_dirs; *cpp != 0; ++cpp)
2672 ++idx;
2673
2674#ifdef __MSDOS__
2675 /* Add one for $DJDIR. */
2676 ++idx;
2677#endif
2678
2679 dirs = xmalloc (idx * sizeof (const char *));
2680
2681 idx = 0;
2682 max_incl_len = 0;
2683
2684 /* First consider any dirs specified with -I switches.
2685 Ignore any that don't exist. Remember the maximum string length. */
2686
2687 if (arg_dirs)
2688 while (*arg_dirs != 0)
2689 {
2690 const char *dir = *(arg_dirs++);
2691 char *expanded = 0;
2692 int e;
2693
2694 if (dir[0] == '~')
2695 {
2696 expanded = tilde_expand (dir);
2697 if (expanded != 0)
2698 dir = expanded;
2699 }
2700
2701 EINTRLOOP (e, stat (dir, &stbuf));
2702 if (e == 0 && S_ISDIR (stbuf.st_mode))
2703 {
2704 unsigned int len = strlen (dir);
2705 /* If dir name is written with trailing slashes, discard them. */
2706 while (len > 1 && dir[len - 1] == '/')
2707 --len;
2708 if (len > max_incl_len)
2709 max_incl_len = len;
2710 dirs[idx++] = strcache_add_len (dir, len);
2711 }
2712
2713 if (expanded)
2714 free (expanded);
2715 }
2716
2717 /* Now add the standard default dirs at the end. */
2718
2719#ifdef __MSDOS__
2720 {
2721 /* The environment variable $DJDIR holds the root of the DJGPP directory
2722 tree; add ${DJDIR}/include. */
2723 struct variable *djdir = lookup_variable ("DJDIR", 5);
2724
2725 if (djdir)
2726 {
2727 unsigned int len = strlen (djdir->value) + 8;
2728 char *defdir = alloca (len + 1);
2729
2730 strcat (strcpy (defdir, djdir->value), "/include");
2731 dirs[idx++] = strcache_add (defdir);
2732
2733 if (len > max_incl_len)
2734 max_incl_len = len;
2735 }
2736 }
2737#endif
2738
2739 for (cpp = default_include_directories; *cpp != 0; ++cpp)
2740 {
2741 int e;
2742
2743 EINTRLOOP (e, stat (*cpp, &stbuf));
2744 if (e == 0 && S_ISDIR (stbuf.st_mode))
2745 {
2746 unsigned int len = strlen (*cpp);
2747 /* If dir name is written with trailing slashes, discard them. */
2748 while (len > 1 && (*cpp)[len - 1] == '/')
2749 --len;
2750 if (len > max_incl_len)
2751 max_incl_len = len;
2752 dirs[idx++] = strcache_add_len (*cpp, len);
2753 }
2754 }
2755
2756 dirs[idx] = 0;
2757
2758 /* Now add each dir to the .INCLUDE_DIRS variable. */
2759
2760 for (cpp = dirs; *cpp != 0; ++cpp)
2761 do_variable_definition (NILF, ".INCLUDE_DIRS", *cpp,
2762 o_default, f_append, 0);
2763
2764 include_directories = dirs;
2765}
2766
2767
2768/* Expand ~ or ~USER at the beginning of NAME.
2769 Return a newly malloc'd string or 0. */
2770
2771char *
2772tilde_expand (const char *name)
2773{
2774#ifndef VMS
2775 if (name[1] == '/' || name[1] == '\0')
2776 {
2777 extern char *getenv ();
2778 char *home_dir;
2779 int is_variable;
2780
2781 {
2782 /* Turn off --warn-undefined-variables while we expand HOME. */
2783 int save = warn_undefined_variables_flag;
2784 warn_undefined_variables_flag = 0;
2785
2786 home_dir = allocated_variable_expand ("$(HOME)");
2787
2788 warn_undefined_variables_flag = save;
2789 }
2790
2791 is_variable = home_dir[0] != '\0';
2792 if (!is_variable)
2793 {
2794 free (home_dir);
2795 home_dir = getenv ("HOME");
2796 }
2797# if !defined(_AMIGA) && !defined(WINDOWS32)
2798 if (home_dir == 0 || home_dir[0] == '\0')
2799 {
2800 extern char *getlogin ();
2801 char *logname = getlogin ();
2802 home_dir = 0;
2803 if (logname != 0)
2804 {
2805 struct passwd *p = getpwnam (logname);
2806 if (p != 0)
2807 home_dir = p->pw_dir;
2808 }
2809 }
2810# endif /* !AMIGA && !WINDOWS32 */
2811 if (home_dir != 0)
2812 {
2813 char *new = xstrdup (concat (2, home_dir, name + 1));
2814 if (is_variable)
2815 free (home_dir);
2816 return new;
2817 }
2818 }
2819# if !defined(_AMIGA) && !defined(WINDOWS32)
2820 else
2821 {
2822 struct passwd *pwent;
2823 char *userend = strchr (name + 1, '/');
2824 if (userend != 0)
2825 *userend = '\0';
2826 pwent = getpwnam (name + 1);
2827 if (pwent != 0)
2828 {
2829 if (userend == 0)
2830 return xstrdup (pwent->pw_dir);
2831 else
2832 return xstrdup (concat (3, pwent->pw_dir, "/", userend + 1));
2833 }
2834 else if (userend != 0)
2835 *userend = '/';
2836 }
2837# endif /* !AMIGA && !WINDOWS32 */
2838#endif /* !VMS */
2839 return 0;
2840}
2841
2842
2843/* Parse a string into a sequence of filenames represented as a chain of
2844 struct nameseq's and return that chain. Optionally expand the strings via
2845 glob().
2846
2847 The string is passed as STRINGP, the address of a string pointer.
2848 The string pointer is updated to point at the first character
2849 not parsed, which either is a null char or equals STOPCHAR.
2850
2851 SIZE is how big to construct chain elements.
2852 This is useful if we want them actually to be other structures
2853 that have room for additional info.
2854
2855 PREFIX, if non-null, is added to the beginning of each filename.
2856
2857 FLAGS allows one or more of the following bitflags to be set:
2858 PARSEFS_NOSTRIP - Do no strip './'s off the beginning
2859 PARSEFS_NOAR - Do not check filenames for archive references
2860 PARSEFS_NOGLOB - Do not expand globbing characters
2861 PARSEFS_EXISTS - Only return globbed files that actually exist
2862 (cannot also set NOGLOB)
2863 PARSEFS_NOCACHE - Do not add filenames to the strcache (caller frees)
2864 */
2865
2866void *
2867parse_file_seq (char **stringp, unsigned int size, int stopchar,
2868 const char *prefix, int flags)
2869{
2870 extern void dir_setup_glob (glob_t *glob);
2871
2872 /* tmp points to tmpbuf after the prefix, if any.
2873 tp is the end of the buffer. */
2874 static char *tmpbuf = NULL;
2875 static int tmpbuf_len = 0;
2876
2877 int cachep = (! (flags & PARSEFS_NOCACHE));
2878
2879 struct nameseq *new = 0;
2880 struct nameseq **newp = &new;
2881#define NEWELT(_n) do { \
2882 const char *__n = (_n); \
2883 *newp = xcalloc (size); \
2884 (*newp)->name = (cachep ? strcache_add (__n) : xstrdup (__n)); \
2885 newp = &(*newp)->next; \
2886 } while(0)
2887
2888 char *p;
2889 glob_t gl;
2890 char *tp;
2891
2892#ifdef VMS
2893# define VMS_COMMA ','
2894#else
2895# define VMS_COMMA 0
2896#endif
2897
2898 if (size < sizeof (struct nameseq))
2899 size = sizeof (struct nameseq);
2900
2901 if (! (flags & PARSEFS_NOGLOB))
2902 dir_setup_glob (&gl);
2903
2904 /* Get enough temporary space to construct the largest possible target. */
2905 {
2906 int l = strlen (*stringp) + 1;
2907 if (l > tmpbuf_len)
2908 {
2909 tmpbuf = xrealloc (tmpbuf, l);
2910 tmpbuf_len = l;
2911 }
2912 }
2913 tp = tmpbuf;
2914
2915 /* Parse STRING. P will always point to the end of the parsed content. */
2916 p = *stringp;
2917 while (1)
2918 {
2919 const char *name;
2920 const char **nlist = 0;
2921 char *tildep = 0;
2922#ifndef NO_ARCHIVES
2923 char *arname = 0;
2924 char *memname = 0;
2925#endif
2926 char *s;
2927 int nlen;
2928 int i;
2929
2930 /* Skip whitespace; at the end of the string or STOPCHAR we're done. */
2931 p = next_token (p);
2932 if (*p == '\0' || *p == stopchar)
2933 break;
2934
2935 /* There are names left, so find the end of the next name.
2936 Throughout this iteration S points to the start. */
2937 s = p;
2938 p = find_char_unquote (p, stopchar, VMS_COMMA, 1, 0);
2939#ifdef VMS
2940 /* convert comma separated list to space separated */
2941 if (p && *p == ',')
2942 *p =' ';
2943#endif
2944#ifdef _AMIGA
2945 if (stopchar == ':' && p && *p == ':'
2946 && !(isspace ((unsigned char)p[1]) || !p[1]
2947 || isspace ((unsigned char)p[-1])))
2948 p = find_char_unquote (p+1, stopchar, VMS_COMMA, 1, 0);
2949#endif
2950#ifdef HAVE_DOS_PATHS
2951 /* For DOS paths, skip a "C:\..." or a "C:/..." until we find the
2952 first colon which isn't followed by a slash or a backslash.
2953 Note that tokens separated by spaces should be treated as separate
2954 tokens since make doesn't allow path names with spaces */
2955 if (stopchar == ':')
2956 while (p != 0 && !isspace ((unsigned char)*p) &&
2957 (p[1] == '\\' || p[1] == '/') && isalpha ((unsigned char)p[-1]))
2958 p = find_char_unquote (p + 1, stopchar, VMS_COMMA, 1, 0);
2959#endif
2960 if (p == 0)
2961 p = s + strlen (s);
2962
2963 /* Strip leading "this directory" references. */
2964 if (! (flags & PARSEFS_NOSTRIP))
2965#ifdef VMS
2966 /* Skip leading `[]'s. */
2967 while (p - s > 2 && s[0] == '[' && s[1] == ']')
2968#else
2969 /* Skip leading `./'s. */
2970 while (p - s > 2 && s[0] == '.' && s[1] == '/')
2971#endif
2972 {
2973 /* Skip "./" and all following slashes. */
2974 s += 2;
2975 while (*s == '/')
2976 ++s;
2977 }
2978
2979 /* Extract the filename just found, and skip it.
2980 Set NAME to the string, and NLEN to its length. */
2981
2982 if (s == p)
2983 {
2984 /* The name was stripped to empty ("./"). */
2985#if defined(VMS)
2986 continue;
2987#elif defined(_AMIGA)
2988 /* PDS-- This cannot be right!! */
2989 tp[0] = '\0';
2990 nlen = 0;
2991#else
2992 tp[0] = '.';
2993 tp[1] = '/';
2994 tp[2] = '\0';
2995 nlen = 2;
2996#endif
2997 }
2998 else
2999 {
3000#ifdef VMS
3001/* VMS filenames can have a ':' in them but they have to be '\'ed but we need
3002 * to remove this '\' before we can use the filename.
3003 * xstrdup called because S may be read-only string constant.
3004 */
3005 char *n = tp;
3006 while (s < p)
3007 {
3008 if (s[0] == '\\' && s[1] == ':')
3009 ++s;
3010 *(n++) = *(s++);
3011 }
3012 n[0] = '\0';
3013 nlen = strlen (tp);
3014#else
3015 nlen = p - s;
3016 memcpy (tp, s, nlen);
3017 tp[nlen] = '\0';
3018#endif
3019 }
3020
3021 /* At this point, TP points to the element and NLEN is its length. */
3022
3023#ifndef NO_ARCHIVES
3024 /* If this is the start of an archive group that isn't complete, set up
3025 to add the archive prefix for future files. A file list like:
3026 "libf.a(x.o y.o z.o)" needs to be expanded as:
3027 "libf.a(x.o) libf.a(y.o) libf.a(z.o)"
3028
3029 TP == TMP means we're not already in an archive group. Ignore
3030 something starting with `(', as that cannot actually be an
3031 archive-member reference (and treating it as such results in an empty
3032 file name, which causes much lossage). Also if it ends in ")" then
3033 it's a complete reference so we don't need to treat it specially.
3034
3035 Finally, note that archive groups must end with ')' as the last
3036 character, so ensure there's some word ending like that before
3037 considering this an archive group. */
3038 if (! (flags & PARSEFS_NOAR)
3039 && tp == tmpbuf && tp[0] != '(' && tp[nlen-1] != ')')
3040 {
3041 char *n = strchr (tp, '(');
3042 if (n)
3043 {
3044 /* This looks like the first element in an open archive group.
3045 A valid group MUST have ')' as the last character. */
3046 const char *e = p + nlen;
3047 do
3048 {
3049 e = next_token (e);
3050 /* Find the end of this word. We don't want to unquote and
3051 we don't care about quoting since we're looking for the
3052 last char in the word. */
3053 while (*e != '\0' && *e != stopchar && *e != VMS_COMMA
3054 && ! isblank ((unsigned char) *e))
3055 ++e;
3056 if (e[-1] == ')')
3057 {
3058 /* Found the end, so this is the first element in an
3059 open archive group. It looks like "lib(mem".
3060 Reset TP past the open paren. */
3061 nlen -= (n + 1) - tp;
3062 tp = n + 1;
3063
3064 /* If we have just "lib(", part of something like
3065 "lib( a b)", go to the next item. */
3066 if (! nlen)
3067 continue;
3068
3069 /* We can stop looking now. */
3070 break;
3071 }
3072 }
3073 while (*e != '\0');
3074 }
3075 }
3076
3077 /* If we are inside an archive group, make sure it has an end. */
3078 if (tp > tmpbuf)
3079 {
3080 if (tp[nlen-1] == ')')
3081 {
3082 /* This is the natural end; reset TP. */
3083 tp = tmpbuf;
3084
3085 /* This is just ")", something like "lib(a b )": skip it. */
3086 if (nlen == 1)
3087 continue;
3088 }
3089 else
3090 {
3091 /* Not the end, so add a "fake" end. */
3092 tp[nlen++] = ')';
3093 tp[nlen] = '\0';
3094 }
3095 }
3096#endif
3097
3098 /* If we're not globbing we're done: add it to the end of the chain.
3099 Go to the next item in the string. */
3100 if (flags & PARSEFS_NOGLOB)
3101 {
3102 NEWELT (concat (2, prefix, tp));
3103 continue;
3104 }
3105
3106 /* If we get here we know we're doing glob expansion.
3107 TP is a string in tmpbuf. NLEN is no longer used.
3108 We may need to do more work: after this NAME will be set. */
3109 name = tp;
3110
3111 /* Expand tilde if applicable. */
3112 if (tp[0] == '~')
3113 {
3114 tildep = tilde_expand (tp);
3115 if (tildep != 0)
3116 name = tildep;
3117 }
3118
3119#ifndef NO_ARCHIVES
3120 /* If NAME is an archive member reference replace it with the archive
3121 file name, and save the member name in MEMNAME. We will glob on the
3122 archive name and then reattach MEMNAME later. */
3123 if (! (flags & PARSEFS_NOAR) && ar_name (name))
3124 {
3125 ar_parse_name (name, &arname, &memname);
3126 name = arname;
3127 }
3128#endif /* !NO_ARCHIVES */
3129
3130 switch (glob (name, GLOB_NOSORT|GLOB_ALTDIRFUNC, NULL, &gl))
3131 {
3132 case GLOB_NOSPACE:
3133 fatal (NILF, _("virtual memory exhausted"));
3134
3135 case 0:
3136 /* Success. */
3137 i = gl.gl_pathc;
3138 nlist = (const char **)gl.gl_pathv;
3139 break;
3140
3141 case GLOB_NOMATCH:
3142 /* If we want only existing items, skip this one. */
3143 if (flags & PARSEFS_EXISTS)
3144 {
3145 i = 0;
3146 break;
3147 }
3148 /* FALLTHROUGH */
3149
3150 default:
3151 /* By default keep this name. */
3152 i = 1;
3153 nlist = &name;
3154 break;
3155 }
3156
3157 /* For each matched element, add it to the list. */
3158 while (i-- > 0)
3159#ifndef NO_ARCHIVES
3160 if (memname != 0)
3161 {
3162 /* Try to glob on MEMNAME within the archive. */
3163 struct nameseq *found = ar_glob (nlist[i], memname, size);
3164 if (! found)
3165 /* No matches. Use MEMNAME as-is. */
3166 NEWELT (concat (5, prefix, nlist[i], "(", memname, ")"));
3167 else
3168 {
3169 /* We got a chain of items. Attach them. */
3170 (*newp)->next = found;
3171
3172 /* Find and set the new end. Massage names if necessary. */
3173 while (1)
3174 {
3175 if (! cachep)
3176 found->name = xstrdup (concat (2, prefix, name));
3177 else if (prefix)
3178 found->name = strcache_add (concat (2, prefix, name));
3179
3180 if (found->next == 0)
3181 break;
3182
3183 found = found->next;
3184 }
3185 newp = &found->next;
3186 }
3187 }
3188 else
3189#endif /* !NO_ARCHIVES */
3190 NEWELT (concat (2, prefix, nlist[i]));
3191
3192 globfree (&gl);
3193
3194#ifndef NO_ARCHIVES
3195 if (arname)
3196 free (arname);
3197#endif
3198
3199 if (tildep)
3200 free (tildep);
3201 }
3202
3203 *stringp = p;
3204 return new;
3205}
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