source: trunk/src/gmake/read.c@ 529

Last change on this file since 529 was 528, checked in by bird, 19 years ago

includedep

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