VirtualBox

source: kBuild/trunk/src/gmake/read.c@ 530

Last change on this file since 530 was 530, checked in by bird, 18 years ago

Added kBuild specific functions for speeding up source processing.

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