VirtualBox

source: kBuild/trunk/src/kmk/read.c@ 1831

Last change on this file since 1831 was 1831, checked in by bird, 16 years ago

kmk: find_char_unquote optimizations, some cleanup and a fix.

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

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette