VirtualBox

source: kBuild/vendor/gnumake/3.81-beta1/read.c@ 1158

Last change on this file since 1158 was 153, checked in by bird, 20 years ago

GNU Make 3.81beta1.

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