VirtualBox

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

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

another warning.

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