VirtualBox

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

Last change on this file since 225 was 191, checked in by bird, 20 years ago

Makefile.kmk

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