source: trunk/src/kmk/read.c@ 1701

Last change on this file since 1701 was 1701, checked in by bird, 17 years ago

kmk: Implemented secondary target expansion. Fixes #42.

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