5 kx /* Reading and parsing of makefiles for GNU Make.
5 kx Copyright (C) 1988-2020 Free Software Foundation, Inc.
5 kx This file is part of GNU Make.
5 kx
5 kx GNU Make is free software; you can redistribute it and/or modify it under the
5 kx terms of the GNU General Public License as published by the Free Software
5 kx Foundation; either version 3 of the License, or (at your option) any later
5 kx version.
5 kx
5 kx GNU Make is distributed in the hope that it will be useful, but WITHOUT ANY
5 kx WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
5 kx A PARTICULAR PURPOSE. See the GNU General Public License for more details.
5 kx
5 kx You should have received a copy of the GNU General Public License along with
5 kx this program. If not, see <http://www.gnu.org/licenses/>. */
5 kx
5 kx #include "makeint.h"
5 kx
5 kx #include <assert.h>
5 kx
5 kx #include "filedef.h"
5 kx #include "dep.h"
5 kx #include "job.h"
5 kx #include "os.h"
5 kx #include "commands.h"
5 kx #include "variable.h"
5 kx #include "rule.h"
5 kx #include "debug.h"
5 kx #include "hash.h"
5 kx
5 kx
5 kx #ifdef WINDOWS32
5 kx #include <windows.h>
5 kx #include "sub_proc.h"
5 kx #else /* !WINDOWS32 */
5 kx #ifndef _AMIGA
5 kx #ifndef VMS
5 kx #include <pwd.h>
5 kx #else
5 kx struct passwd *getpwnam (char *name);
5 kx #endif
5 kx #endif
5 kx #endif /* !WINDOWS32 */
5 kx
5 kx /* A 'struct ebuffer' controls the origin of the makefile we are currently
5 kx eval'ing.
5 kx */
5 kx
5 kx struct ebuffer
5 kx {
5 kx char *buffer; /* Start of the current line in the buffer. */
5 kx char *bufnext; /* Start of the next line in the buffer. */
5 kx char *bufstart; /* Start of the entire buffer. */
5 kx size_t size; /* Malloc'd size of buffer. */
5 kx FILE *fp; /* File, or NULL if this is an internal buffer. */
5 kx floc floc; /* Info on the file in fp (if any). */
5 kx };
5 kx
5 kx /* Track the modifiers we can have on variable assignments */
5 kx
5 kx struct vmodifiers
5 kx {
5 kx unsigned int assign_v:1;
5 kx unsigned int define_v:1;
5 kx unsigned int undefine_v:1;
5 kx unsigned int override_v:1;
5 kx unsigned int private_v:1;
5 kx enum variable_export export_v ENUM_BITFIELD (2);
5 kx };
5 kx
5 kx /* Types of "words" that can be read in a makefile. */
5 kx enum make_word_type
5 kx {
5 kx w_bogus, w_eol, w_static, w_variable, w_colon, w_dcolon, w_semicolon,
5 kx w_varassign, w_ampcolon, w_ampdcolon
5 kx };
5 kx
5 kx
5 kx /* A 'struct conditionals' contains the information describing
5 kx all the active conditionals in a makefile.
5 kx
5 kx The global variable 'conditionals' contains the conditionals
5 kx information for the current makefile. It is initialized from
5 kx the static structure 'toplevel_conditionals' and is later changed
5 kx to new structures for included makefiles. */
5 kx
5 kx struct conditionals
5 kx {
5 kx unsigned int if_cmds; /* Depth of conditional nesting. */
5 kx unsigned int allocated; /* Elts allocated in following arrays. */
5 kx char *ignoring; /* Are we ignoring or interpreting?
5 kx 0=interpreting, 1=not yet interpreted,
5 kx 2=already interpreted */
5 kx char *seen_else; /* Have we already seen an 'else'? */
5 kx };
5 kx
5 kx static struct conditionals toplevel_conditionals;
5 kx static struct conditionals *conditionals = &toplevel_conditionals;
5 kx
5 kx
5 kx /* Default directories to search for include files in */
5 kx
5 kx static const char *default_include_directories[] =
5 kx {
5 kx #if defined(WINDOWS32) && !defined(INCLUDEDIR)
5 kx /* This completely up to the user when they install MSVC or other packages.
5 kx This is defined as a placeholder. */
5 kx # define INCLUDEDIR "."
5 kx #endif
5 kx INCLUDEDIR,
5 kx #ifndef _AMIGA
5 kx "/usr/gnu/include",
5 kx "/usr/local/include",
5 kx "/usr/include",
5 kx #endif
5 kx 0
5 kx };
5 kx
5 kx /* List of directories to search for include files in */
5 kx
5 kx static const char **include_directories;
5 kx
5 kx /* Maximum length of an element of the above. */
5 kx
5 kx static size_t max_incl_len;
5 kx
5 kx /* The filename and pointer to line number of the
5 kx makefile currently being read in. */
5 kx
5 kx const floc *reading_file = 0;
5 kx
5 kx /* The chain of files read by read_all_makefiles. */
5 kx
5 kx static struct goaldep *read_files = 0;
5 kx
5 kx static struct goaldep *eval_makefile (const char *filename, unsigned short flags);
5 kx static void eval (struct ebuffer *buffer, int flags);
5 kx
5 kx static long readline (struct ebuffer *ebuf);
5 kx static void do_undefine (char *name, enum variable_origin origin,
5 kx struct ebuffer *ebuf);
5 kx static struct variable *do_define (char *name, enum variable_origin origin,
5 kx struct ebuffer *ebuf);
5 kx static int conditional_line (char *line, size_t len, const floc *flocp);
5 kx static void check_specials (const struct nameseq *file, int set_default);
5 kx static void record_files (struct nameseq *filenames, int are_also_makes,
5 kx const char *pattern,
5 kx const char *pattern_percent, char *depstr,
5 kx unsigned int cmds_started, char *commands,
5 kx size_t commands_idx, int two_colon,
5 kx char prefix, const floc *flocp);
5 kx static void record_target_var (struct nameseq *filenames, char *defn,
5 kx enum variable_origin origin,
5 kx struct vmodifiers *vmod,
5 kx const floc *flocp);
5 kx static enum make_word_type get_next_mword (char *buffer,
5 kx char **startp, size_t *length);
5 kx static void remove_comments (char *line);
5 kx static char *find_map_unquote (char *string, int map);
5 kx static char *find_char_unquote (char *string, int stop);
5 kx static char *unescape_char (char *string, int c);
5 kx
5 kx
5 kx /* Compare a word, both length and contents.
5 kx P must point to the word to be tested, and WLEN must be the length.
5 kx */
5 kx #define word1eq(s) (wlen == CSTRLEN (s) && strneq (s, p, CSTRLEN (s)))
5 kx
5 kx
5 kx /* Read in all the makefiles and return a chain of targets to rebuild. */
5 kx
5 kx struct goaldep *
5 kx read_all_makefiles (const char **makefiles)
5 kx {
5 kx unsigned int num_makefiles = 0;
5 kx
5 kx /* Create *_LIST variables, to hold the makefiles, targets, and variables
5 kx we will be reading. */
5 kx
5 kx define_variable_cname ("MAKEFILE_LIST", "", o_file, 0);
5 kx
5 kx DB (DB_BASIC, (_("Reading makefiles...\n")));
5 kx
5 kx /* If there's a non-null variable MAKEFILES, its value is a list of
5 kx files to read first thing. But don't let it prevent reading the
5 kx default makefiles and don't let the default goal come from there. */
5 kx
5 kx {
5 kx char *value;
5 kx char *name, *p;
5 kx size_t length;
5 kx
5 kx {
5 kx /* Turn off --warn-undefined-variables while we expand MAKEFILES. */
5 kx int save = warn_undefined_variables_flag;
5 kx warn_undefined_variables_flag = 0;
5 kx
5 kx value = allocated_variable_expand ("$(MAKEFILES)");
5 kx
5 kx warn_undefined_variables_flag = save;
5 kx }
5 kx
5 kx /* Set NAME to the start of next token and LENGTH to its length.
5 kx MAKEFILES is updated for finding remaining tokens. */
5 kx p = value;
5 kx
5 kx while ((name = find_next_token ((const char **)&p, &length)) != 0)
5 kx {
5 kx if (*p != '\0')
5 kx *p++ = '\0';
5 kx eval_makefile (strcache_add (name), RM_NO_DEFAULT_GOAL|RM_INCLUDED|RM_DONTCARE);
5 kx }
5 kx
5 kx free (value);
5 kx }
5 kx
5 kx /* Read makefiles specified with -f switches. */
5 kx
5 kx if (makefiles != 0)
5 kx while (*makefiles != 0)
5 kx {
5 kx struct goaldep *d = eval_makefile (*makefiles, 0);
5 kx
5 kx if (errno)
5 kx perror_with_name ("", *makefiles);
5 kx
5 kx /* Reuse the storage allocated for the read_file. */
5 kx *makefiles = dep_name (d);
5 kx ++num_makefiles;
5 kx ++makefiles;
5 kx }
5 kx
5 kx /* If there were no -f switches, try the default names. */
5 kx
5 kx if (num_makefiles == 0)
5 kx {
5 kx static const char *default_makefiles[] =
5 kx #ifdef VMS
5 kx /* all lower case since readdir() (the vms version) 'lowercasifies' */
5 kx /* TODO: Above is not always true, this needs more work */
5 kx { "makefile.vms", "gnumakefile", "makefile", 0 };
5 kx #else
5 kx #ifdef _AMIGA
5 kx { "GNUmakefile", "Makefile", "SMakefile", 0 };
5 kx #else /* !Amiga && !VMS */
5 kx #ifdef WINDOWS32
5 kx { "GNUmakefile", "makefile", "Makefile", "makefile.mak", 0 };
5 kx #else /* !Amiga && !VMS && !WINDOWS32 */
5 kx { "GNUmakefile", "makefile", "Makefile", 0 };
5 kx #endif /* !Amiga && !VMS && !WINDOWS32 */
5 kx #endif /* AMIGA */
5 kx #endif /* VMS */
5 kx const char **p = default_makefiles;
5 kx while (*p != 0 && !file_exists_p (*p))
5 kx ++p;
5 kx
5 kx if (*p != 0)
5 kx {
5 kx eval_makefile (*p, 0);
5 kx if (errno)
5 kx perror_with_name ("", *p);
5 kx }
5 kx else
5 kx {
5 kx /* No default makefile was found. Add the default makefiles to the
5 kx 'read_files' chain so they will be updated if possible. */
5 kx for (p = default_makefiles; *p != 0; ++p)
5 kx {
5 kx struct goaldep *d = alloc_goaldep ();
5 kx d->file = enter_file (strcache_add (*p));
5 kx /* Tell update_goal_chain to bail out as soon as this file is
5 kx made, and main not to die if we can't make this file. */
5 kx d->flags = RM_DONTCARE;
5 kx d->next = read_files;
5 kx read_files = d;
5 kx }
5 kx }
5 kx }
5 kx
5 kx return read_files;
5 kx }
5 kx
5 kx /* Install a new conditional and return the previous one. */
5 kx
5 kx static struct conditionals *
5 kx install_conditionals (struct conditionals *new)
5 kx {
5 kx struct conditionals *save = conditionals;
5 kx
5 kx memset (new, '\0', sizeof (*new));
5 kx conditionals = new;
5 kx
5 kx return save;
5 kx }
5 kx
5 kx /* Free the current conditionals and reinstate a saved one. */
5 kx
5 kx static void
5 kx restore_conditionals (struct conditionals *saved)
5 kx {
5 kx /* Free any space allocated by conditional_line. */
5 kx free (conditionals->ignoring);
5 kx free (conditionals->seen_else);
5 kx
5 kx /* Restore state. */
5 kx conditionals = saved;
5 kx }
5 kx
5 kx static struct goaldep *
5 kx eval_makefile (const char *filename, unsigned short flags)
5 kx {
5 kx struct goaldep *deps;
5 kx struct ebuffer ebuf;
5 kx const floc *curfile;
5 kx char *expanded = 0;
5 kx
5 kx /* Create a new goaldep entry. */
5 kx deps = alloc_goaldep ();
5 kx deps->next = read_files;
5 kx read_files = deps;
5 kx
5 kx ebuf.floc.filenm = filename; /* Use the original file name. */
5 kx ebuf.floc.lineno = 1;
5 kx ebuf.floc.offset = 0;
5 kx
5 kx if (ISDB (DB_VERBOSE))
5 kx {
5 kx printf (_("Reading makefile '%s'"), filename);
5 kx if (flags & RM_NO_DEFAULT_GOAL)
5 kx printf (_(" (no default goal)"));
5 kx if (flags & RM_INCLUDED)
5 kx printf (_(" (search path)"));
5 kx if (flags & RM_DONTCARE)
5 kx printf (_(" (don't care)"));
5 kx if (flags & RM_NO_TILDE)
5 kx printf (_(" (no ~ expansion)"));
5 kx puts ("...");
5 kx }
5 kx
5 kx /* First, get a stream to read. */
5 kx
5 kx /* Expand ~ in FILENAME unless it came from 'include',
5 kx in which case it was already done. */
5 kx if (!(flags & RM_NO_TILDE) && filename[0] == '~')
5 kx {
5 kx expanded = tilde_expand (filename);
5 kx if (expanded != 0)
5 kx filename = expanded;
5 kx }
5 kx
5 kx errno = 0;
5 kx ENULLLOOP (ebuf.fp, fopen (filename, "r"));
5 kx deps->error = errno;
5 kx
5 kx /* Check for unrecoverable errors: out of mem or FILE slots. */
5 kx switch (deps->error)
5 kx {
5 kx #ifdef EMFILE
5 kx case EMFILE:
5 kx #endif
5 kx #ifdef ENFILE
5 kx case ENFILE:
5 kx #endif
5 kx case ENOMEM:
5 kx {
5 kx const char *err = strerror (deps->error);
5 kx OS (fatal, reading_file, "%s", err);
5 kx }
5 kx }
5 kx
5 kx /* If the makefile wasn't found and it's either a makefile from
5 kx the 'MAKEFILES' variable or an included makefile,
5 kx search the included makefile search path for this makefile. */
5 kx if (ebuf.fp == 0 && (flags & RM_INCLUDED) && *filename != '/')
5 kx {
5 kx unsigned int i;
5 kx for (i = 0; include_directories[i] != 0; ++i)
5 kx {
5 kx const char *included = concat (3, include_directories[i],
5 kx "/", filename);
5 kx ebuf.fp = fopen (included, "r");
5 kx if (ebuf.fp)
5 kx {
5 kx filename = included;
5 kx break;
5 kx }
5 kx }
5 kx }
5 kx
5 kx /* Enter the final name for this makefile as a goaldep. */
5 kx filename = strcache_add (filename);
5 kx deps->file = lookup_file (filename);
5 kx if (deps->file == 0)
5 kx deps->file = enter_file (filename);
5 kx filename = deps->file->name;
5 kx deps->flags = flags;
5 kx
5 kx free (expanded);
5 kx
5 kx if (ebuf.fp == 0)
5 kx {
5 kx /* The makefile can't be read at all, give up entirely.
5 kx If we did some searching errno has the error from the last attempt,
5 kx rather from FILENAME itself: recover the more accurate one. */
5 kx errno = deps->error;
5 kx deps->file->last_mtime = NONEXISTENT_MTIME;
5 kx return deps;
5 kx }
5 kx
5 kx /* Success; clear errno. */
5 kx deps->error = 0;
5 kx
5 kx /* If we tried and failed to read the included file before but this
5 kx time we succeeded, reset the last mtime. */
5 kx if (deps->file->last_mtime == NONEXISTENT_MTIME)
5 kx deps->file->last_mtime = 0;
5 kx
5 kx /* Avoid leaking the makefile to children. */
5 kx fd_noinherit (fileno (ebuf.fp));
5 kx
5 kx /* Add this makefile to the list. */
5 kx do_variable_definition (&ebuf.floc, "MAKEFILE_LIST", filename, o_file,
5 kx f_append_value, 0);
5 kx
5 kx /* Evaluate the makefile */
5 kx
5 kx ebuf.size = 200;
5 kx ebuf.buffer = ebuf.bufnext = ebuf.bufstart = xmalloc (ebuf.size);
5 kx
5 kx curfile = reading_file;
5 kx reading_file = &ebuf.floc;
5 kx
5 kx eval (&ebuf, !(flags & RM_NO_DEFAULT_GOAL));
5 kx
5 kx reading_file = curfile;
5 kx
5 kx fclose (ebuf.fp);
5 kx
5 kx free (ebuf.bufstart);
5 kx free_alloca ();
5 kx
5 kx errno = 0;
5 kx return deps;
5 kx }
5 kx
5 kx void
5 kx eval_buffer (char *buffer, const floc *flocp)
5 kx {
5 kx struct ebuffer ebuf;
5 kx struct conditionals *saved;
5 kx struct conditionals new;
5 kx const floc *curfile;
5 kx
5 kx /* Evaluate the buffer */
5 kx
5 kx ebuf.size = strlen (buffer);
5 kx ebuf.buffer = ebuf.bufnext = ebuf.bufstart = buffer;
5 kx ebuf.fp = NULL;
5 kx
5 kx if (flocp)
5 kx ebuf.floc = *flocp;
5 kx else if (reading_file)
5 kx ebuf.floc = *reading_file;
5 kx else
5 kx {
5 kx ebuf.floc.filenm = NULL;
5 kx ebuf.floc.lineno = 1;
5 kx ebuf.floc.offset = 0;
5 kx }
5 kx
5 kx curfile = reading_file;
5 kx reading_file = &ebuf.floc;
5 kx
5 kx saved = install_conditionals (&new);
5 kx
5 kx eval (&ebuf, 1);
5 kx
5 kx restore_conditionals (saved);
5 kx
5 kx reading_file = curfile;
5 kx
5 kx free_alloca ();
5 kx }
5 kx
5 kx /* Check LINE to see if it's a variable assignment or undefine.
5 kx
5 kx It might use one of the modifiers "export", "override", "private", or it
5 kx might be one of the conditional tokens like "ifdef", "include", etc.
5 kx
5 kx If it's not a variable assignment or undefine, VMOD.V_ASSIGN is 0.
5 kx Returns LINE.
5 kx
5 kx Returns a pointer to the first non-modifier character, and sets VMOD
5 kx based on the modifiers found if any, plus V_ASSIGN is 1.
5 kx */
5 kx static char *
5 kx parse_var_assignment (const char *line, int targvar, struct vmodifiers *vmod)
5 kx {
5 kx const char *p;
5 kx memset (vmod, '\0', sizeof (*vmod));
5 kx
5 kx /* Find the start of the next token. If there isn't one we're done. */
5 kx NEXT_TOKEN (line);
5 kx if (*line == '\0')
5 kx return (char *) line;
5 kx
5 kx p = line;
5 kx while (1)
5 kx {
5 kx size_t wlen;
5 kx const char *p2;
5 kx struct variable v;
5 kx
5 kx p2 = parse_variable_definition (p, &v);
5 kx
5 kx /* If this is a variable assignment, we're done. */
5 kx if (p2)
5 kx break;
5 kx
5 kx /* It's not a variable; see if it's a modifier. */
5 kx p2 = end_of_token (p);
5 kx wlen = p2 - p;
5 kx
5 kx if (word1eq ("export"))
5 kx vmod->export_v = v_export;
5 kx else if (word1eq ("unexport"))
5 kx vmod->export_v = v_noexport;
5 kx else if (word1eq ("override"))
5 kx vmod->override_v = 1;
5 kx else if (word1eq ("private"))
5 kx vmod->private_v = 1;
5 kx else if (!targvar && word1eq ("define"))
5 kx {
5 kx /* We can't have modifiers after 'define' */
5 kx vmod->define_v = 1;
5 kx p = next_token (p2);
5 kx break;
5 kx }
5 kx else if (!targvar && word1eq ("undefine"))
5 kx {
5 kx /* We can't have modifiers after 'undefine' */
5 kx vmod->undefine_v = 1;
5 kx p = next_token (p2);
5 kx break;
5 kx }
5 kx else
5 kx /* Not a variable or modifier: this is not a variable assignment. */
5 kx return (char *) line;
5 kx
5 kx /* It was a modifier. Try the next word. */
5 kx p = next_token (p2);
5 kx if (*p == '\0')
5 kx return (char *) line;
5 kx }
5 kx
5 kx /* Found a variable assignment or undefine. */
5 kx vmod->assign_v = 1;
5 kx return (char *)p;
5 kx }
5 kx
5 kx
5 kx /* Read file FILENAME as a makefile and add its contents to the data base.
5 kx
5 kx SET_DEFAULT is true if we are allowed to set the default goal. */
5 kx
5 kx static void
5 kx eval (struct ebuffer *ebuf, int set_default)
5 kx {
5 kx char *collapsed = 0;
5 kx size_t collapsed_length = 0;
5 kx size_t commands_len = 200;
5 kx char *commands;
5 kx size_t commands_idx = 0;
5 kx unsigned int cmds_started, tgts_started;
5 kx int ignoring = 0, in_ignored_define = 0;
5 kx int no_targets = 0; /* Set when reading a rule without targets. */
5 kx int also_make_targets = 0; /* Set when reading grouped targets. */
5 kx struct nameseq *filenames = 0;
5 kx char *depstr = 0;
5 kx long nlines = 0;
5 kx int two_colon = 0;
5 kx char prefix = cmd_prefix;
5 kx const char *pattern = 0;
5 kx const char *pattern_percent;
5 kx floc *fstart;
5 kx floc fi;
5 kx
5 kx #define record_waiting_files() \
5 kx do \
5 kx { \
5 kx if (filenames != 0) \
5 kx { \
5 kx fi.lineno = tgts_started; \
5 kx fi.offset = 0; \
5 kx record_files (filenames, also_make_targets, pattern, \
5 kx pattern_percent, depstr, \
5 kx cmds_started, commands, commands_idx, two_colon, \
5 kx prefix, &fi); \
5 kx filenames = 0; \
5 kx } \
5 kx commands_idx = 0; \
5 kx no_targets = 0; \
5 kx pattern = 0; \
5 kx also_make_targets = 0; \
5 kx } while (0)
5 kx
5 kx pattern_percent = 0;
5 kx cmds_started = tgts_started = 1;
5 kx
5 kx fstart = &ebuf->floc;
5 kx fi.filenm = ebuf->floc.filenm;
5 kx
5 kx /* Loop over lines in the file.
5 kx The strategy is to accumulate target names in FILENAMES, dependencies
5 kx in DEPS and commands in COMMANDS. These are used to define a rule
5 kx when the start of the next rule (or eof) is encountered.
5 kx
5 kx When you see a "continue" in the loop below, that means we are moving on
5 kx to the next line. If you see record_waiting_files(), then the statement
5 kx we are parsing also finishes the previous rule. */
5 kx
5 kx commands = xmalloc (200);
5 kx
5 kx while (1)
5 kx {
5 kx size_t linelen;
5 kx char *line;
5 kx size_t wlen;
5 kx char *p;
5 kx char *p2;
5 kx struct vmodifiers vmod;
5 kx
5 kx /* At the top of this loop, we are starting a brand new line. */
5 kx /* Grab the next line to be evaluated */
5 kx ebuf->floc.lineno += nlines;
5 kx nlines = readline (ebuf);
5 kx
5 kx /* If there is nothing left to eval, we're done. */
5 kx if (nlines < 0)
5 kx break;
5 kx
5 kx line = ebuf->buffer;
5 kx
5 kx /* If this is the first line, check for a UTF-8 BOM and skip it. */
5 kx if (ebuf->floc.lineno == 1)
5 kx {
5 kx unsigned char *ul = (unsigned char *) line;
5 kx if (ul[0] == 0xEF && ul[1] == 0xBB && ul[2] == 0xBF)
5 kx {
5 kx line += 3;
5 kx if (ISDB(DB_BASIC))
5 kx {
5 kx if (ebuf->floc.filenm)
5 kx printf (_("Skipping UTF-8 BOM in makefile '%s'\n"),
5 kx ebuf->floc.filenm);
5 kx else
5 kx printf (_("Skipping UTF-8 BOM in makefile buffer\n"));
5 kx }
5 kx }
5 kx }
5 kx /* If this line is empty, skip it. */
5 kx if (line[0] == '\0')
5 kx continue;
5 kx
5 kx linelen = strlen (line);
5 kx
5 kx /* Check for a shell command line first.
5 kx If it is not one, we can stop treating cmd_prefix specially. */
5 kx if (line[0] == cmd_prefix)
5 kx {
5 kx if (no_targets)
5 kx /* Ignore the commands in a rule with no targets. */
5 kx continue;
5 kx
5 kx /* If there is no preceding rule line, don't treat this line
5 kx as a command, even though it begins with a recipe prefix.
5 kx SunOS 4 make appears to behave this way. */
5 kx
5 kx if (filenames != 0)
5 kx {
5 kx if (ignoring)
5 kx /* Yep, this is a shell command, and we don't care. */
5 kx continue;
5 kx
5 kx if (commands_idx == 0)
5 kx cmds_started = ebuf->floc.lineno;
5 kx
5 kx /* Append this command line to the line being accumulated.
5 kx Skip the initial command prefix character. */
5 kx if (linelen + commands_idx > commands_len)
5 kx {
5 kx commands_len = (linelen + commands_idx) * 2;
5 kx commands = xrealloc (commands, commands_len);
5 kx }
5 kx memcpy (&commands[commands_idx], line + 1, linelen - 1);
5 kx commands_idx += linelen - 1;
5 kx commands[commands_idx++] = '\n';
5 kx continue;
5 kx }
5 kx }
5 kx
5 kx /* This line is not a shell command line. Don't worry about whitespace.
5 kx Get more space if we need it; we don't need to preserve the current
5 kx contents of the buffer. */
5 kx
5 kx if (collapsed_length < linelen+1)
5 kx {
5 kx collapsed_length = linelen+1;
5 kx free (collapsed);
5 kx /* Don't need xrealloc: we don't need to preserve the content. */
5 kx collapsed = xmalloc (collapsed_length);
5 kx }
5 kx strcpy (collapsed, line);
5 kx /* Collapse continuation lines. */
5 kx collapse_continuations (collapsed);
5 kx remove_comments (collapsed);
5 kx
5 kx /* Get rid if starting space (including formfeed, vtab, etc.) */
5 kx p = collapsed;
5 kx NEXT_TOKEN (p);
5 kx
5 kx /* See if this is a variable assignment. We need to do this early, to
5 kx allow variables with names like 'ifdef', 'export', 'private', etc. */
5 kx p = parse_var_assignment (p, 0, &vmod);
5 kx if (vmod.assign_v)
5 kx {
5 kx struct variable *v;
5 kx enum variable_origin origin = vmod.override_v ? o_override : o_file;
5 kx
5 kx /* If we're ignoring then we're done now. */
5 kx if (ignoring)
5 kx {
5 kx if (vmod.define_v)
5 kx in_ignored_define = 1;
5 kx continue;
5 kx }
5 kx
5 kx /* Variable assignment ends the previous rule. */
5 kx record_waiting_files ();
5 kx
5 kx if (vmod.undefine_v)
5 kx {
5 kx do_undefine (p, origin, ebuf);
5 kx continue;
5 kx }
5 kx else if (vmod.define_v)
5 kx v = do_define (p, origin, ebuf);
5 kx else
5 kx v = try_variable_definition (fstart, p, origin, 0);
5 kx
5 kx assert (v != NULL);
5 kx
5 kx if (vmod.export_v != v_default)
5 kx v->export = vmod.export_v;
5 kx if (vmod.private_v)
5 kx v->private_var = 1;
5 kx
5 kx /* This line has been dealt with. */
5 kx continue;
5 kx }
5 kx
5 kx /* If this line is completely empty, ignore it. */
5 kx if (*p == '\0')
5 kx continue;
5 kx
5 kx p2 = end_of_token (p);
5 kx wlen = p2 - p;
5 kx NEXT_TOKEN (p2);
5 kx
5 kx /* If we're in an ignored define, skip this line (but maybe get out). */
5 kx if (in_ignored_define)
5 kx {
5 kx /* See if this is an endef line (plus optional comment). */
5 kx if (word1eq ("endef") && STOP_SET (*p2, MAP_COMMENT|MAP_NUL))
5 kx in_ignored_define = 0;
5 kx
5 kx continue;
5 kx }
5 kx
5 kx /* Check for conditional state changes. */
5 kx {
5 kx int i = conditional_line (p, wlen, fstart);
5 kx if (i != -2)
5 kx {
5 kx if (i == -1)
5 kx O (fatal, fstart, _("invalid syntax in conditional"));
5 kx
5 kx ignoring = i;
5 kx continue;
5 kx }
5 kx }
5 kx
5 kx /* Nothing to see here... move along. */
5 kx if (ignoring)
5 kx continue;
5 kx
5 kx /* Manage the "export" keyword used outside of variable assignment
5 kx as well as "unexport". */
5 kx if (word1eq ("export") || word1eq ("unexport"))
5 kx {
5 kx int exporting = *p == 'u' ? 0 : 1;
5 kx
5 kx /* Export/unexport ends the previous rule. */
5 kx record_waiting_files ();
5 kx
5 kx /* (un)export by itself causes everything to be (un)exported. */
5 kx if (*p2 == '\0')
5 kx export_all_variables = exporting;
5 kx else
5 kx {
5 kx size_t l;
5 kx const char *cp;
5 kx char *ap;
5 kx
5 kx /* Expand the line so we can use indirect and constructed
5 kx variable names in an (un)export command. */
5 kx cp = ap = allocated_variable_expand (p2);
5 kx
5 kx for (p = find_next_token (&cp, &l); p != 0;
5 kx p = find_next_token (&cp, &l))
5 kx {
5 kx struct variable *v = lookup_variable (p, l);
5 kx if (v == 0)
5 kx v = define_variable_global (p, l, "", o_file, 0, fstart);
5 kx v->export = exporting ? v_export : v_noexport;
5 kx }
5 kx
5 kx free (ap);
5 kx }
5 kx continue;
5 kx }
5 kx
5 kx /* Handle the special syntax for vpath. */
5 kx if (word1eq ("vpath"))
5 kx {
5 kx const char *cp;
5 kx char *vpat;
5 kx size_t l;
5 kx
5 kx /* vpath ends the previous rule. */
5 kx record_waiting_files ();
5 kx
5 kx cp = variable_expand (p2);
5 kx p = find_next_token (&cp, &l);
5 kx if (p != 0)
5 kx {
5 kx vpat = xstrndup (p, l);
5 kx p = find_next_token (&cp, &l);
5 kx /* No searchpath means remove all previous
5 kx selective VPATH's with the same pattern. */
5 kx }
5 kx else
5 kx /* No pattern means remove all previous selective VPATH's. */
5 kx vpat = 0;
5 kx construct_vpath_list (vpat, p);
5 kx free (vpat);
5 kx
5 kx continue;
5 kx }
5 kx
5 kx /* Handle include and variants. */
5 kx if (word1eq ("include") || word1eq ("-include") || word1eq ("sinclude"))
5 kx {
5 kx /* We have found an 'include' line specifying a nested
5 kx makefile to be read at this point. */
5 kx struct conditionals *save;
5 kx struct conditionals new_conditionals;
5 kx struct nameseq *files;
5 kx /* "-include" (vs "include") says no error if the file does not
5 kx exist. "sinclude" is an alias for this from SGI. */
5 kx int noerror = (p[0] != 'i');
5 kx
5 kx /* Include ends the previous rule. */
5 kx record_waiting_files ();
5 kx
5 kx p = allocated_variable_expand (p2);
5 kx
5 kx /* If no filenames, it's a no-op. */
5 kx if (*p == '\0')
5 kx {
5 kx free (p);
5 kx continue;
5 kx }
5 kx
5 kx /* Parse the list of file names. Don't expand archive references! */
5 kx p2 = p;
5 kx files = PARSE_FILE_SEQ (&p2, struct nameseq, MAP_NUL, NULL,
5 kx PARSEFS_NOAR);
5 kx free (p);
5 kx
5 kx /* Save the state of conditionals and start
5 kx the included makefile with a clean slate. */
5 kx save = install_conditionals (&new_conditionals);
5 kx
5 kx /* Record the rules that are waiting so they will determine
5 kx the default goal before those in the included makefile. */
5 kx record_waiting_files ();
5 kx
5 kx /* Read each included makefile. */
5 kx while (files != 0)
5 kx {
5 kx struct nameseq *next = files->next;
5 kx unsigned short flags = (RM_INCLUDED | RM_NO_TILDE
5 kx | (noerror ? RM_DONTCARE : 0)
5 kx | (set_default ? 0 : RM_NO_DEFAULT_GOAL));
5 kx
5 kx struct goaldep *d = eval_makefile (files->name, flags);
5 kx
5 kx if (errno)
5 kx d->floc = *fstart;
5 kx
5 kx free_ns (files);
5 kx files = next;
5 kx }
5 kx
5 kx /* Restore conditional state. */
5 kx restore_conditionals (save);
5 kx
5 kx continue;
5 kx }
5 kx
5 kx /* Handle the load operations. */
5 kx if (word1eq ("load") || word1eq ("-load"))
5 kx {
5 kx /* A 'load' line specifies a dynamic object to load. */
5 kx struct nameseq *files;
5 kx int noerror = (p[0] == '-');
5 kx
5 kx /* Load ends the previous rule. */
5 kx record_waiting_files ();
5 kx
5 kx p = allocated_variable_expand (p2);
5 kx
5 kx /* If no filenames, it's a no-op. */
5 kx if (*p == '\0')
5 kx {
5 kx free (p);
5 kx continue;
5 kx }
5 kx
5 kx /* Parse the list of file names.
5 kx Don't expand archive references or strip "./" */
5 kx p2 = p;
5 kx files = PARSE_FILE_SEQ (&p2, struct nameseq, MAP_NUL, NULL,
5 kx PARSEFS_NOAR);
5 kx free (p);
5 kx
5 kx /* Load each file. */
5 kx while (files != 0)
5 kx {
5 kx struct nameseq *next = files->next;
5 kx const char *name = files->name;
5 kx struct goaldep *deps;
5 kx int r;
5 kx
5 kx /* Load the file. 0 means failure. */
5 kx r = load_file (&ebuf->floc, &name, noerror);
5 kx if (! r && ! noerror)
5 kx OS (fatal, &ebuf->floc, _("%s: failed to load"), name);
5 kx
5 kx free_ns (files);
5 kx files = next;
5 kx
5 kx /* Return of -1 means a special load: don't rebuild it. */
5 kx if (r == -1)
5 kx continue;
5 kx
5 kx /* It succeeded, so add it to the list "to be rebuilt". */
5 kx deps = alloc_goaldep ();
5 kx deps->next = read_files;
5 kx read_files = deps;
5 kx deps->file = lookup_file (name);
5 kx if (deps->file == 0)
5 kx deps->file = enter_file (name);
5 kx deps->file->loaded = 1;
5 kx }
5 kx
5 kx continue;
5 kx }
5 kx
5 kx /* This line starts with a tab but was not caught above because there
5 kx was no preceding target, and the line might have been usable as a
5 kx variable definition. But now we know it is definitely lossage. */
5 kx if (line[0] == cmd_prefix)
5 kx O (fatal, fstart, _("recipe commences before first target"));
5 kx
5 kx /* This line describes some target files. This is complicated by
5 kx the existence of target-specific variables, because we can't
5 kx expand the entire line until we know if we have one or not. So
5 kx we expand the line word by word until we find the first ':',
5 kx then check to see if it's a target-specific variable.
5 kx
5 kx In this algorithm, 'lb_next' will point to the beginning of the
5 kx unexpanded parts of the input buffer, while 'p2' points to the
5 kx parts of the expanded buffer we haven't searched yet. */
5 kx
5 kx {
5 kx enum make_word_type wtype;
5 kx char *cmdleft, *semip, *lb_next;
5 kx size_t plen = 0;
5 kx char *colonp;
5 kx const char *end, *beg; /* Helpers for whitespace stripping. */
5 kx
5 kx /* Record the previous rule. */
5 kx
5 kx record_waiting_files ();
5 kx tgts_started = fstart->lineno;
5 kx
5 kx /* Search the line for an unquoted ; that is not after an
5 kx unquoted #. */
5 kx cmdleft = find_map_unquote (line, MAP_SEMI|MAP_COMMENT|MAP_VARIABLE);
5 kx if (cmdleft != 0 && *cmdleft == '#')
5 kx {
5 kx /* We found a comment before a semicolon. */
5 kx *cmdleft = '\0';
5 kx cmdleft = 0;
5 kx }
5 kx else if (cmdleft != 0)
5 kx /* Found one. Cut the line short there before expanding it. */
5 kx *(cmdleft++) = '\0';
5 kx semip = cmdleft;
5 kx
5 kx collapse_continuations (line);
5 kx
5 kx /* We can't expand the entire line, since if it's a per-target
5 kx variable we don't want to expand it. So, walk from the
5 kx beginning, expanding as we go, and looking for "interesting"
5 kx chars. The first word is always expandable. */
5 kx wtype = get_next_mword (line, &lb_next, &wlen);
5 kx switch (wtype)
5 kx {
5 kx case w_eol:
5 kx if (cmdleft != 0)
5 kx O (fatal, fstart, _("missing rule before recipe"));
5 kx /* This line contained something but turned out to be nothing
5 kx but whitespace (a comment?). */
5 kx continue;
5 kx
5 kx case w_colon:
5 kx case w_dcolon:
5 kx case w_ampcolon:
5 kx case w_ampdcolon:
5 kx /* We accept and ignore rules without targets for
5 kx compatibility with SunOS 4 make. */
5 kx no_targets = 1;
5 kx continue;
5 kx
5 kx default:
5 kx break;
5 kx }
5 kx
5 kx p2 = variable_expand_string (NULL, lb_next, wlen);
5 kx
5 kx while (1)
5 kx {
5 kx lb_next += wlen;
5 kx if (cmdleft == 0)
5 kx {
5 kx /* Look for a semicolon in the expanded line. */
5 kx cmdleft = find_char_unquote (p2, ';');
5 kx
5 kx if (cmdleft != 0)
5 kx {
5 kx size_t p2_off = p2 - variable_buffer;
5 kx size_t cmd_off = cmdleft - variable_buffer;
5 kx char *pend = p2 + strlen (p2);
5 kx
5 kx /* Append any remnants of lb, then cut the line short
5 kx at the semicolon. */
5 kx *cmdleft = '\0';
5 kx
5 kx /* One school of thought says that you shouldn't expand
5 kx here, but merely copy, since now you're beyond a ";"
5 kx and into a command script. However, the old parser
5 kx expanded the whole line, so we continue that for
5 kx backwards-compatibility. Also, it wouldn't be
5 kx entirely consistent, since we do an unconditional
5 kx expand below once we know we don't have a
5 kx target-specific variable. */
5 kx variable_expand_string (pend, lb_next, SIZE_MAX);
5 kx lb_next += strlen (lb_next);
5 kx p2 = variable_buffer + p2_off;
5 kx cmdleft = variable_buffer + cmd_off + 1;
5 kx }
5 kx }
5 kx
5 kx colonp = find_char_unquote (p2, ':');
5 kx
5 kx #ifdef HAVE_DOS_PATHS
5 kx if (colonp > p2)
5 kx /* The drive spec brain-damage strikes again...
5 kx Note that the only separators of targets in this context are
5 kx whitespace and a left paren. If others are possible, add them
5 kx to the string in the call to strchr. */
5 kx while (colonp && (colonp[1] == '/' || colonp[1] == '\\') &&
5 kx isalpha ((unsigned char) colonp[-1]) &&
5 kx (colonp == p2 + 1 || strchr (" \t(", colonp[-2]) != 0))
5 kx colonp = find_char_unquote (colonp + 1, ':');
5 kx #endif
5 kx
5 kx if (colonp)
5 kx {
5 kx /* If the previous character is '&', back up before '&:' */
5 kx if (colonp > p2 && colonp[-1] == '&')
5 kx --colonp;
5 kx
5 kx break;
5 kx }
5 kx
5 kx wtype = get_next_mword (lb_next, &lb_next, &wlen);
5 kx if (wtype == w_eol)
5 kx break;
5 kx
5 kx p2 += strlen (p2);
5 kx *(p2++) = ' ';
5 kx p2 = variable_expand_string (p2, lb_next, wlen);
5 kx /* We don't need to worry about cmdleft here, because if it was
5 kx found in the variable_buffer the entire buffer has already
5 kx been expanded... we'll never get here. */
5 kx }
5 kx
5 kx p2 = next_token (variable_buffer);
5 kx
5 kx /* If the word we're looking at is EOL, see if there's _anything_
5 kx on the line. If not, a variable expanded to nothing, so ignore
5 kx it. If so, we can't parse this line so punt. */
5 kx if (wtype == w_eol)
5 kx {
5 kx if (*p2 == '\0')
5 kx continue;
5 kx
5 kx /* There's no need to be ivory-tower about this: check for
5 kx one of the most common bugs found in makefiles... */
5 kx if (cmd_prefix == '\t' && strneq (line, " ", 8))
5 kx O (fatal, fstart, _("missing separator (did you mean TAB instead of 8 spaces?)"));
5 kx else
5 kx O (fatal, fstart, _("missing separator"));
5 kx }
5 kx
5 kx {
5 kx char save = *colonp;
5 kx
5 kx /* If we have &:, it specifies that the targets are understood to be
5 kx updated/created together by a single invocation of the recipe. */
5 kx if (save == '&')
5 kx also_make_targets = 1;
5 kx
5 kx /* Make the colon the end-of-string so we know where to stop
5 kx looking for targets. Start there again once we're done. */
5 kx *colonp = '\0';
5 kx filenames = PARSE_SIMPLE_SEQ (&p2, struct nameseq);
5 kx *colonp = save;
5 kx p2 = colonp + (save == '&');
5 kx }
5 kx
5 kx if (!filenames)
5 kx {
5 kx /* We accept and ignore rules without targets for
5 kx compatibility with SunOS 4 make. */
5 kx no_targets = 1;
5 kx continue;
5 kx }
5 kx /* This should never be possible; we handled it above. */
5 kx assert (*p2 != '\0');
5 kx ++p2;
5 kx
5 kx /* Is this a one-colon or two-colon entry? */
5 kx two_colon = *p2 == ':';
5 kx if (two_colon)
5 kx p2++;
5 kx
5 kx /* Test to see if it's a target-specific variable. Copy the rest
5 kx of the buffer over, possibly temporarily (we'll expand it later
5 kx if it's not a target-specific variable). PLEN saves the length
5 kx of the unparsed section of p2, for later. */
5 kx if (*lb_next != '\0')
5 kx {
5 kx size_t l = p2 - variable_buffer;
5 kx plen = strlen (p2);
5 kx variable_buffer_output (p2+plen, lb_next, strlen (lb_next)+1);
5 kx p2 = variable_buffer + l;
5 kx }
5 kx
5 kx p2 = parse_var_assignment (p2, 1, &vmod);
5 kx if (vmod.assign_v)
5 kx {
5 kx /* If there was a semicolon found, add it back, plus anything
5 kx after it. */
5 kx if (semip)
5 kx {
5 kx size_t l = p2 - variable_buffer;
5 kx *(--semip) = ';';
5 kx collapse_continuations (semip);
5 kx variable_buffer_output (p2 + strlen (p2),
5 kx semip, strlen (semip)+1);
5 kx p2 = variable_buffer + l;
5 kx }
5 kx record_target_var (filenames, p2,
5 kx vmod.override_v ? o_override : o_file,
5 kx &vmod, fstart);
5 kx filenames = 0;
5 kx continue;
5 kx }
5 kx
5 kx /* This is a normal target, _not_ a target-specific variable.
5 kx Unquote any = in the dependency list. */
5 kx find_char_unquote (lb_next, '=');
5 kx
5 kx /* Remember the command prefix for this target. */
5 kx prefix = cmd_prefix;
5 kx
5 kx /* We have some targets, so don't ignore the following commands. */
5 kx no_targets = 0;
5 kx
5 kx /* Expand the dependencies, etc. */
5 kx if (*lb_next != '\0')
5 kx {
5 kx size_t l = p2 - variable_buffer;
5 kx variable_expand_string (p2 + plen, lb_next, SIZE_MAX);
5 kx p2 = variable_buffer + l;
5 kx
5 kx /* Look for a semicolon in the expanded line. */
5 kx if (cmdleft == 0)
5 kx {
5 kx cmdleft = find_char_unquote (p2, ';');
5 kx if (cmdleft != 0)
5 kx *(cmdleft++) = '\0';
5 kx }
5 kx }
5 kx
5 kx /* Is this a static pattern rule: 'target: %targ: %dep; ...'? */
5 kx p = strchr (p2, ':');
5 kx while (p != 0 && p[-1] == '\\')
5 kx {
5 kx char *q = &p[-1];
5 kx int backslash = 0;
5 kx while (*q-- == '\\')
5 kx backslash = !backslash;
5 kx if (backslash)
5 kx p = strchr (p + 1, ':');
5 kx else
5 kx break;
5 kx }
5 kx #ifdef _AMIGA
5 kx /* Here, the situation is quite complicated. Let's have a look
5 kx at a couple of targets:
5 kx
5 kx install: dev:make
5 kx
5 kx dev:make: make
5 kx
5 kx dev:make:: xyz
5 kx
5 kx The rule is that it's only a target, if there are TWO :'s
5 kx OR a space around the :.
5 kx */
5 kx if (p && !(ISSPACE (p[1]) || !p[1] || ISSPACE (p[-1])))
5 kx p = 0;
5 kx #endif
5 kx #ifdef HAVE_DOS_PATHS
5 kx {
5 kx int check_again;
5 kx do {
5 kx check_again = 0;
5 kx /* For DOS-style paths, skip a "C:\..." or a "C:/..." */
5 kx if (p != 0 && (p[1] == '\\' || p[1] == '/') &&
5 kx isalpha ((unsigned char)p[-1]) &&
5 kx (p == p2 + 1 || strchr (" \t:(", p[-2]) != 0)) {
5 kx p = strchr (p + 1, ':');
5 kx check_again = 1;
5 kx }
5 kx } while (check_again);
5 kx }
5 kx #endif
5 kx if (p != 0)
5 kx {
5 kx struct nameseq *target;
5 kx target = PARSE_FILE_SEQ (&p2, struct nameseq, MAP_COLON, NULL,
5 kx PARSEFS_NOGLOB);
5 kx ++p2;
5 kx if (target == 0)
5 kx O (fatal, fstart, _("missing target pattern"));
5 kx else if (target->next != 0)
5 kx O (fatal, fstart, _("multiple target patterns"));
5 kx pattern_percent = find_percent_cached (&target->name);
5 kx pattern = target->name;
5 kx if (pattern_percent == 0)
5 kx O (fatal, fstart, _("target pattern contains no '%%'"));
5 kx free_ns (target);
5 kx }
5 kx else
5 kx pattern = 0;
5 kx
5 kx /* Strip leading and trailing whitespaces. */
5 kx beg = p2;
5 kx end = beg + strlen (beg) - 1;
5 kx strip_whitespace (&beg, &end);
5 kx
5 kx /* Put all the prerequisites here; they'll be parsed later. */
5 kx if (beg <= end && *beg != '\0')
5 kx depstr = xstrndup (beg, end - beg + 1);
5 kx else
5 kx depstr = 0;
5 kx
5 kx commands_idx = 0;
5 kx if (cmdleft != 0)
5 kx {
5 kx /* Semicolon means rest of line is a command. */
5 kx size_t l = strlen (cmdleft);
5 kx
5 kx cmds_started = fstart->lineno;
5 kx
5 kx /* Add this command line to the buffer. */
5 kx if (l + 2 > commands_len)
5 kx {
5 kx commands_len = (l + 2) * 2;
5 kx commands = xrealloc (commands, commands_len);
5 kx }
5 kx memcpy (commands, cmdleft, l);
5 kx commands_idx += l;
5 kx commands[commands_idx++] = '\n';
5 kx }
5 kx
5 kx check_specials (filenames, set_default);
5 kx }
5 kx }
5 kx
5 kx #undef word1eq
5 kx
5 kx if (conditionals->if_cmds)
5 kx O (fatal, fstart, _("missing 'endif'"));
5 kx
5 kx /* At eof, record the last rule. */
5 kx record_waiting_files ();
5 kx
5 kx free (collapsed);
5 kx free (commands);
5 kx }
5 kx
5 kx
5 kx /* Remove comments from LINE.
5 kx This will also remove backslashes that escape things.
5 kx It ignores comment characters that appear inside variable references. */
5 kx
5 kx static void
5 kx remove_comments (char *line)
5 kx {
5 kx char *comment;
5 kx
5 kx comment = find_map_unquote (line, MAP_COMMENT|MAP_VARIABLE);
5 kx
5 kx if (comment != 0)
5 kx /* Cut off the line at the #. */
5 kx *comment = '\0';
5 kx }
5 kx
5 kx /* Execute a 'undefine' directive.
5 kx The undefine line has already been read, and NAME is the name of
5 kx the variable to be undefined. */
5 kx
5 kx static void
5 kx do_undefine (char *name, enum variable_origin origin, struct ebuffer *ebuf)
5 kx {
5 kx char *p, *var;
5 kx
5 kx /* Expand the variable name and find the beginning (NAME) and end. */
5 kx var = allocated_variable_expand (name);
5 kx name = next_token (var);
5 kx if (*name == '\0')
5 kx O (fatal, &ebuf->floc, _("empty variable name"));
5 kx p = name + strlen (name) - 1;
5 kx while (p > name && ISBLANK (*p))
5 kx --p;
5 kx p[1] = '\0';
5 kx
5 kx undefine_variable_global (name, p - name + 1, origin);
5 kx free (var);
5 kx }
5 kx
5 kx /* Execute a 'define' directive.
5 kx The first line has already been read, and NAME is the name of
5 kx the variable to be defined. The following lines remain to be read. */
5 kx
5 kx static struct variable *
5 kx do_define (char *name, enum variable_origin origin, struct ebuffer *ebuf)
5 kx {
5 kx struct variable *v;
5 kx struct variable var;
5 kx floc defstart;
5 kx int nlevels = 1;
5 kx size_t length = 100;
5 kx char *definition = xmalloc (length);
5 kx size_t idx = 0;
5 kx char *p, *n;
5 kx
5 kx defstart = ebuf->floc;
5 kx
5 kx p = parse_variable_definition (name, &var);
5 kx if (p == NULL)
5 kx /* No assignment token, so assume recursive. */
5 kx var.flavor = f_recursive;
5 kx else
5 kx {
5 kx if (var.value[0] != '\0')
5 kx O (error, &defstart, _("extraneous text after 'define' directive"));
5 kx
5 kx /* Chop the string before the assignment token to get the name. */
5 kx var.name[var.length] = '\0';
5 kx }
5 kx
5 kx /* Expand the variable name and find the beginning (NAME) and end. */
5 kx n = allocated_variable_expand (name);
5 kx name = next_token (n);
5 kx if (name[0] == '\0')
5 kx O (fatal, &defstart, _("empty variable name"));
5 kx p = name + strlen (name) - 1;
5 kx while (p > name && ISBLANK (*p))
5 kx --p;
5 kx p[1] = '\0';
5 kx
5 kx /* Now read the value of the variable. */
5 kx while (1)
5 kx {
5 kx size_t len;
5 kx char *line;
5 kx long nlines = readline (ebuf);
5 kx
5 kx /* If there is nothing left to be eval'd, there's no 'endef'!! */
5 kx if (nlines < 0)
5 kx O (fatal, &defstart, _("missing 'endef', unterminated 'define'"));
5 kx
5 kx ebuf->floc.lineno += nlines;
5 kx line = ebuf->buffer;
5 kx
5 kx collapse_continuations (line);
5 kx
5 kx /* If the line doesn't begin with a tab, test to see if it introduces
5 kx another define, or ends one. Stop if we find an 'endef' */
5 kx if (line[0] != cmd_prefix)
5 kx {
5 kx p = next_token (line);
5 kx len = strlen (p);
5 kx
5 kx /* If this is another 'define', increment the level count. */
5 kx if ((len == 6 || (len > 6 && ISBLANK (p[6])))
5 kx && strneq (p, "define", 6))
5 kx ++nlevels;
5 kx
5 kx /* If this is an 'endef', decrement the count. If it's now 0,
5 kx we've found the last one. */
5 kx else if ((len == 5 || (len > 5 && ISBLANK (p[5])))
5 kx && strneq (p, "endef", 5))
5 kx {
5 kx p += 5;
5 kx remove_comments (p);
5 kx if (*(next_token (p)) != '\0')
5 kx O (error, &ebuf->floc,
5 kx _("extraneous text after 'endef' directive"));
5 kx
5 kx if (--nlevels == 0)
5 kx break;
5 kx }
5 kx }
5 kx
5 kx /* Add this line to the variable definition. */
5 kx len = strlen (line);
5 kx if (idx + len + 1 > length)
5 kx {
5 kx length = (idx + len) * 2;
5 kx definition = xrealloc (definition, length + 1);
5 kx }
5 kx
5 kx memcpy (&definition[idx], line, len);
5 kx idx += len;
5 kx /* Separate lines with a newline. */
5 kx definition[idx++] = '\n';
5 kx }
5 kx
5 kx /* We've got what we need; define the variable. */
5 kx if (idx == 0)
5 kx definition[0] = '\0';
5 kx else
5 kx definition[idx - 1] = '\0';
5 kx
5 kx v = do_variable_definition (&defstart, name,
5 kx definition, origin, var.flavor, 0);
5 kx free (definition);
5 kx free (n);
5 kx return (v);
5 kx }
5 kx
5 kx /* Interpret conditional commands "ifdef", "ifndef", "ifeq",
5 kx "ifneq", "else" and "endif".
5 kx LINE is the input line, with the command as its first word.
5 kx
5 kx FILENAME and LINENO are the filename and line number in the
5 kx current makefile. They are used for error messages.
5 kx
5 kx Value is -2 if the line is not a conditional at all,
5 kx -1 if the line is an invalid conditional,
5 kx 0 if following text should be interpreted,
5 kx 1 if following text should be ignored. */
5 kx
5 kx static int
5 kx conditional_line (char *line, size_t len, const floc *flocp)
5 kx {
5 kx const char *cmdname;
5 kx enum { c_ifdef, c_ifndef, c_ifeq, c_ifneq, c_else, c_endif } cmdtype;
5 kx unsigned int i;
5 kx unsigned int o;
5 kx
5 kx /* Compare a word, both length and contents. */
5 kx #define word1eq(s) (len == CSTRLEN (s) && strneq (s, line, CSTRLEN (s)))
5 kx #define chkword(s, t) if (word1eq (s)) { cmdtype = (t); cmdname = (s); }
5 kx
5 kx /* Make sure this line is a conditional. */
5 kx chkword ("ifdef", c_ifdef)
5 kx else chkword ("ifndef", c_ifndef)
5 kx else chkword ("ifeq", c_ifeq)
5 kx else chkword ("ifneq", c_ifneq)
5 kx else chkword ("else", c_else)
5 kx else chkword ("endif", c_endif)
5 kx else
5 kx return -2;
5 kx
5 kx /* Found one: skip past it and any whitespace after it. */
5 kx line += len;
5 kx NEXT_TOKEN (line);
5 kx
5 kx #define EXTRATEXT() OS (error, flocp, _("extraneous text after '%s' directive"), cmdname)
5 kx #define EXTRACMD() OS (fatal, flocp, _("extraneous '%s'"), cmdname)
5 kx
5 kx /* An 'endif' cannot contain extra text, and reduces the if-depth by 1 */
5 kx if (cmdtype == c_endif)
5 kx {
5 kx if (*line != '\0')
5 kx EXTRATEXT ();
5 kx
5 kx if (!conditionals->if_cmds)
5 kx EXTRACMD ();
5 kx
5 kx --conditionals->if_cmds;
5 kx
5 kx goto DONE;
5 kx }
5 kx
5 kx /* An 'else' statement can either be simple, or it can have another
5 kx conditional after it. */
5 kx if (cmdtype == c_else)
5 kx {
5 kx const char *p;
5 kx
5 kx if (!conditionals->if_cmds)
5 kx EXTRACMD ();
5 kx
5 kx o = conditionals->if_cmds - 1;
5 kx
5 kx if (conditionals->seen_else[o])
5 kx O (fatal, flocp, _("only one 'else' per conditional"));
5 kx
5 kx /* Change the state of ignorance. */
5 kx switch (conditionals->ignoring[o])
5 kx {
5 kx case 0:
5 kx /* We've just been interpreting. Never do it again. */
5 kx conditionals->ignoring[o] = 2;
5 kx break;
5 kx case 1:
5 kx /* We've never interpreted yet. Maybe this time! */
5 kx conditionals->ignoring[o] = 0;
5 kx break;
5 kx }
5 kx
5 kx /* It's a simple 'else'. */
5 kx if (*line == '\0')
5 kx {
5 kx conditionals->seen_else[o] = 1;
5 kx goto DONE;
5 kx }
5 kx
5 kx /* The 'else' has extra text. That text must be another conditional
5 kx and cannot be an 'else' or 'endif'. */
5 kx
5 kx /* Find the length of the next word. */
5 kx for (p = line+1; ! STOP_SET (*p, MAP_SPACE|MAP_NUL); ++p)
5 kx ;
5 kx len = p - line;
5 kx
5 kx /* If it's 'else' or 'endif' or an illegal conditional, fail. */
5 kx if (word1eq ("else") || word1eq ("endif")
5 kx || conditional_line (line, len, flocp) < 0)
5 kx EXTRATEXT ();
5 kx else
5 kx {
5 kx /* conditional_line() created a new level of conditional.
5 kx Raise it back to this level. */
5 kx if (conditionals->ignoring[o] < 2)
5 kx conditionals->ignoring[o] = conditionals->ignoring[o+1];
5 kx --conditionals->if_cmds;
5 kx }
5 kx
5 kx goto DONE;
5 kx }
5 kx
5 kx if (conditionals->allocated == 0)
5 kx {
5 kx conditionals->allocated = 5;
5 kx conditionals->ignoring = xmalloc (conditionals->allocated);
5 kx conditionals->seen_else = xmalloc (conditionals->allocated);
5 kx }
5 kx
5 kx o = conditionals->if_cmds++;
5 kx if (conditionals->if_cmds > conditionals->allocated)
5 kx {
5 kx conditionals->allocated += 5;
5 kx conditionals->ignoring = xrealloc (conditionals->ignoring,
5 kx conditionals->allocated);
5 kx conditionals->seen_else = xrealloc (conditionals->seen_else,
5 kx conditionals->allocated);
5 kx }
5 kx
5 kx /* Record that we have seen an 'if...' but no 'else' so far. */
5 kx conditionals->seen_else[o] = 0;
5 kx
5 kx /* Search through the stack to see if we're already ignoring. */
5 kx for (i = 0; i < o; ++i)
5 kx if (conditionals->ignoring[i])
5 kx {
5 kx /* We are already ignoring, so just push a level to match the next
5 kx "else" or "endif", and keep ignoring. We don't want to expand
5 kx variables in the condition. */
5 kx conditionals->ignoring[o] = 1;
5 kx return 1;
5 kx }
5 kx
5 kx if (cmdtype == c_ifdef || cmdtype == c_ifndef)
5 kx {
5 kx size_t l;
5 kx char *var;
5 kx struct variable *v;
5 kx char *p;
5 kx
5 kx /* Expand the thing we're looking up, so we can use indirect and
5 kx constructed variable names. */
5 kx var = allocated_variable_expand (line);
5 kx
5 kx /* Make sure there's only one variable name to test. */
5 kx p = end_of_token (var);
5 kx l = p - var;
5 kx NEXT_TOKEN (p);
5 kx if (*p != '\0')
5 kx return -1;
5 kx
5 kx var[l] = '\0';
5 kx v = lookup_variable (var, l);
5 kx
5 kx conditionals->ignoring[o] =
5 kx ((v != 0 && *v->value != '\0') == (cmdtype == c_ifndef));
5 kx
5 kx free (var);
5 kx }
5 kx else
5 kx {
5 kx /* "ifeq" or "ifneq". */
5 kx char *s1, *s2;
5 kx size_t l;
5 kx char termin = *line == '(' ? ',' : *line;
5 kx
5 kx if (termin != ',' && termin != '"' && termin != '\'')
5 kx return -1;
5 kx
5 kx s1 = ++line;
5 kx /* Find the end of the first string. */
5 kx if (termin == ',')
5 kx {
5 kx int count = 0;
5 kx for (; *line != '\0'; ++line)
5 kx if (*line == '(')
5 kx ++count;
5 kx else if (*line == ')')
5 kx --count;
5 kx else if (*line == ',' && count <= 0)
5 kx break;
5 kx }
5 kx else
5 kx while (*line != '\0' && *line != termin)
5 kx ++line;
5 kx
5 kx if (*line == '\0')
5 kx return -1;
5 kx
5 kx if (termin == ',')
5 kx {
5 kx /* Strip blanks after the first string. */
5 kx char *p = line++;
5 kx while (ISBLANK (p[-1]))
5 kx --p;
5 kx *p = '\0';
5 kx }
5 kx else
5 kx *line++ = '\0';
5 kx
5 kx s2 = variable_expand (s1);
5 kx /* We must allocate a new copy of the expanded string because
5 kx variable_expand re-uses the same buffer. */
5 kx l = strlen (s2);
5 kx s1 = alloca (l + 1);
5 kx memcpy (s1, s2, l + 1);
5 kx
5 kx if (termin != ',')
5 kx /* Find the start of the second string. */
5 kx NEXT_TOKEN (line);
5 kx
5 kx termin = termin == ',' ? ')' : *line;
5 kx if (termin != ')' && termin != '"' && termin != '\'')
5 kx return -1;
5 kx
5 kx /* Find the end of the second string. */
5 kx if (termin == ')')
5 kx {
5 kx int count = 0;
5 kx s2 = next_token (line);
5 kx for (line = s2; *line != '\0'; ++line)
5 kx {
5 kx if (*line == '(')
5 kx ++count;
5 kx else if (*line == ')')
5 kx {
5 kx if (count <= 0)
5 kx break;
5 kx else
5 kx --count;
5 kx }
5 kx }
5 kx }
5 kx else
5 kx {
5 kx ++line;
5 kx s2 = line;
5 kx while (*line != '\0' && *line != termin)
5 kx ++line;
5 kx }
5 kx
5 kx if (*line == '\0')
5 kx return -1;
5 kx
5 kx *(line++) = '\0';
5 kx NEXT_TOKEN (line);
5 kx if (*line != '\0')
5 kx EXTRATEXT ();
5 kx
5 kx s2 = variable_expand (s2);
5 kx conditionals->ignoring[o] = (streq (s1, s2) == (cmdtype == c_ifneq));
5 kx }
5 kx
5 kx DONE:
5 kx /* Search through the stack to see if we're ignoring. */
5 kx for (i = 0; i < conditionals->if_cmds; ++i)
5 kx if (conditionals->ignoring[i])
5 kx return 1;
5 kx return 0;
5 kx }
5 kx
5 kx
5 kx /* Record target-specific variable values for files FILENAMES.
5 kx TWO_COLON is nonzero if a double colon was used.
5 kx
5 kx The links of FILENAMES are freed, and so are any names in it
5 kx that are not incorporated into other data structures.
5 kx
5 kx If the target is a pattern, add the variable to the pattern-specific
5 kx variable value list. */
5 kx
5 kx static void
5 kx record_target_var (struct nameseq *filenames, char *defn,
5 kx enum variable_origin origin, struct vmodifiers *vmod,
5 kx const floc *flocp)
5 kx {
5 kx struct nameseq *nextf;
5 kx struct variable_set_list *global;
5 kx
5 kx global = current_variable_set_list;
5 kx
5 kx /* If the variable is an append version, store that but treat it as a
5 kx normal recursive variable. */
5 kx
5 kx for (; filenames != 0; filenames = nextf)
5 kx {
5 kx struct variable *v;
5 kx const char *name = filenames->name;
5 kx const char *percent;
5 kx struct pattern_var *p;
5 kx
5 kx nextf = filenames->next;
5 kx free_ns (filenames);
5 kx
5 kx /* If it's a pattern target, then add it to the pattern-specific
5 kx variable list. */
5 kx percent = find_percent_cached (&name);
5 kx if (percent)
5 kx {
5 kx /* Get a reference for this pattern-specific variable struct. */
5 kx p = create_pattern_var (name, percent);
5 kx p->variable.fileinfo = *flocp;
5 kx /* I don't think this can fail since we already determined it was a
5 kx variable definition. */
5 kx v = assign_variable_definition (&p->variable, defn);
5 kx assert (v != 0);
5 kx
5 kx v->origin = origin;
5 kx if (v->flavor == f_simple)
5 kx v->value = allocated_variable_expand (v->value);
5 kx else
5 kx v->value = xstrdup (v->value);
5 kx }
5 kx else
5 kx {
5 kx struct file *f;
5 kx
5 kx /* Get a file reference for this file, and initialize it.
5 kx We don't want to just call enter_file() because that allocates a
5 kx new entry if the file is a double-colon, which we don't want in
5 kx this situation. */
5 kx f = lookup_file (name);
5 kx if (!f)
5 kx f = enter_file (strcache_add (name));
5 kx else if (f->double_colon)
5 kx f = f->double_colon;
5 kx
5 kx initialize_file_variables (f, 1);
5 kx
5 kx current_variable_set_list = f->variables;
5 kx v = try_variable_definition (flocp, defn, origin, 1);
5 kx if (!v)
5 kx O (fatal, flocp, _("Malformed target-specific variable definition"));
5 kx current_variable_set_list = global;
5 kx }
5 kx
5 kx /* Set up the variable to be *-specific. */
5 kx v->per_target = 1;
5 kx v->private_var = vmod->private_v;
5 kx if (vmod->export_v != v_default)
5 kx v->export = vmod->export_v;
5 kx
5 kx /* If it's not an override, check to see if there was a command-line
5 kx setting. If so, reset the value. */
5 kx if (v->origin != o_override)
5 kx {
5 kx struct variable *gv;
5 kx size_t len = strlen (v->name);
5 kx
5 kx gv = lookup_variable (v->name, len);
5 kx if (gv && v != gv
5 kx && (gv->origin == o_env_override || gv->origin == o_command))
5 kx {
5 kx free (v->value);
5 kx v->value = xstrdup (gv->value);
5 kx v->origin = gv->origin;
5 kx v->recursive = gv->recursive;
5 kx v->append = 0;
5 kx }
5 kx }
5 kx }
5 kx }
5 kx
5 kx
5 kx /* Check for special targets. We used to do this in record_files() but that's
5 kx too late: by the time we get there we'll have already parsed the next line
5 kx and it have been mis-parsed because these special targets haven't been
5 kx considered yet. */
5 kx
5 kx static void check_specials (const struct nameseq* files, int set_default)
5 kx {
5 kx const struct nameseq *t = files;
5 kx
5 kx /* Unlikely but ... */
5 kx if (posix_pedantic && second_expansion && one_shell
5 kx && (!set_default || default_goal_var->value[0] == '\0'))
5 kx return;
5 kx
5 kx for (; t != 0; t = t->next)
5 kx {
5 kx const char* nm = t->name;
5 kx
5 kx if (!posix_pedantic && streq (nm, ".POSIX"))
5 kx {
5 kx posix_pedantic = 1;
5 kx define_variable_cname (".SHELLFLAGS", "-ec", o_default, 0);
5 kx /* These default values are based on IEEE Std 1003.1-2008.
5 kx It requires '-O 1' for [CF]FLAGS, but GCC doesn't allow
5 kx space between -O and the number so omit it here. */
5 kx define_variable_cname ("CC", "c99", o_default, 0);
5 kx define_variable_cname ("CFLAGS", "-O1", o_default, 0);
5 kx define_variable_cname ("FC", "fort77", o_default, 0);
5 kx define_variable_cname ("FFLAGS", "-O1", o_default, 0);
5 kx define_variable_cname ("SCCSGETFLAGS", "-s", o_default, 0);
5 kx continue;
5 kx }
5 kx
5 kx if (!second_expansion && streq (nm, ".SECONDEXPANSION"))
5 kx {
5 kx second_expansion = 1;
5 kx continue;
5 kx }
5 kx
5 kx #if !defined (__MSDOS__) && !defined (__EMX__)
5 kx if (!one_shell && streq (nm, ".ONESHELL"))
5 kx {
5 kx one_shell = 1;
5 kx continue;
5 kx }
5 kx #endif
5 kx
5 kx /* Determine if this target should be made default. */
5 kx
5 kx if (set_default && default_goal_var->value[0] == '\0')
5 kx {
5 kx struct dep *d;
5 kx int reject = 0;
5 kx
5 kx /* We have nothing to do if this is an implicit rule. */
5 kx if (strchr (nm, '%') != 0)
5 kx break;
5 kx
5 kx /* See if this target's name does not start with a '.',
5 kx unless it contains a slash. */
5 kx if (*nm == '.' && strchr (nm, '/') == 0
5 kx #ifdef HAVE_DOS_PATHS
5 kx && strchr (nm, '\\') == 0
5 kx #endif
5 kx )
5 kx continue;
5 kx
5 kx /* If this file is a suffix, it can't be the default goal file. */
5 kx for (d = suffix_file->deps; d != 0; d = d->next)
5 kx {
5 kx struct dep *d2;
5 kx if (*dep_name (d) != '.' && streq (nm, dep_name (d)))
5 kx {
5 kx reject = 1;
5 kx break;
5 kx }
5 kx for (d2 = suffix_file->deps; d2 != 0; d2 = d2->next)
5 kx {
5 kx size_t l = strlen (dep_name (d2));
5 kx if (!strneq (nm, dep_name (d2), l))
5 kx continue;
5 kx if (streq (nm + l, dep_name (d)))
5 kx {
5 kx reject = 1;
5 kx break;
5 kx }
5 kx }
5 kx
5 kx if (reject)
5 kx break;
5 kx }
5 kx
5 kx if (!reject)
5 kx define_variable_global (".DEFAULT_GOAL", 13, t->name,
5 kx o_file, 0, NILF);
5 kx }
5 kx }
5 kx }
5 kx
5 kx /* Record a description line for files FILENAMES,
5 kx with dependencies DEPS, commands to execute described
5 kx by COMMANDS and COMMANDS_IDX, coming from FILENAME:COMMANDS_STARTED.
5 kx TWO_COLON is nonzero if a double colon was used.
5 kx If not nil, PATTERN is the '%' pattern to make this
5 kx a static pattern rule, and PATTERN_PERCENT is a pointer
5 kx to the '%' within it.
5 kx
5 kx The links of FILENAMES are freed, and so are any names in it
5 kx that are not incorporated into other data structures. */
5 kx
5 kx static void
5 kx record_files (struct nameseq *filenames, int are_also_makes,
5 kx const char *pattern,
5 kx const char *pattern_percent, char *depstr,
5 kx unsigned int cmds_started, char *commands,
5 kx size_t commands_idx, int two_colon,
5 kx char prefix, const floc *flocp)
5 kx {
5 kx struct commands *cmds;
5 kx struct dep *deps;
5 kx struct dep *also_make = NULL;
5 kx const char *implicit_percent;
5 kx const char *name;
5 kx
5 kx /* If we've already snapped deps, that means we're in an eval being
5 kx resolved after the makefiles have been read in. We can't add more rules
5 kx at this time, since they won't get snapped and we'll get core dumps.
5 kx See Savannah bug # 12124. */
5 kx if (snapped_deps)
5 kx O (fatal, flocp, _("prerequisites cannot be defined in recipes"));
5 kx
5 kx /* Determine if this is a pattern rule or not. */
5 kx name = filenames->name;
5 kx implicit_percent = find_percent_cached (&name);
5 kx
5 kx /* If there's a recipe, set up a struct for it. */
5 kx if (commands_idx > 0)
5 kx {
5 kx cmds = xmalloc (sizeof (struct commands));
5 kx cmds->fileinfo.filenm = flocp->filenm;
5 kx cmds->fileinfo.lineno = cmds_started;
5 kx cmds->fileinfo.offset = 0;
5 kx cmds->commands = xstrndup (commands, commands_idx);
5 kx cmds->command_lines = 0;
5 kx cmds->recipe_prefix = prefix;
5 kx }
5 kx else if (are_also_makes)
5 kx O (fatal, flocp, _("grouped targets must provide a recipe"));
5 kx else
5 kx cmds = NULL;
5 kx
5 kx /* If there's a prereq string then parse it--unless it's eligible for 2nd
5 kx expansion: if so, snap_deps() will do it. */
5 kx if (depstr == 0)
5 kx deps = 0;
5 kx else
5 kx {
5 kx depstr = unescape_char (depstr, ':');
5 kx if (second_expansion && strchr (depstr, '$'))
5 kx {
5 kx deps = alloc_dep ();
5 kx deps->name = depstr;
5 kx deps->need_2nd_expansion = 1;
5 kx deps->staticpattern = pattern != 0;
5 kx }
5 kx else
5 kx {
5 kx deps = split_prereqs (depstr);
5 kx free (depstr);
5 kx
5 kx /* We'll enter static pattern prereqs later when we have the stem.
5 kx We don't want to enter pattern rules at all so that we don't
5 kx think that they ought to exist (make manual "Implicit Rule Search
5 kx Algorithm", item 5c). */
5 kx if (! pattern && ! implicit_percent)
5 kx deps = enter_prereqs (deps, NULL);
5 kx }
5 kx }
5 kx
5 kx /* For implicit rules, _all_ the targets must have a pattern. That means we
5 kx can test the first one to see if we're working with an implicit rule; if
5 kx so we handle it specially. */
5 kx
5 kx if (implicit_percent)
5 kx {
5 kx struct nameseq *nextf;
5 kx const char **targets, **target_pats;
5 kx unsigned short c;
5 kx
5 kx if (pattern != 0)
5 kx O (fatal, flocp, _("mixed implicit and static pattern rules"));
5 kx
5 kx /* Count the targets to create an array of target names.
5 kx We already have the first one. */
5 kx nextf = filenames->next;
5 kx free_ns (filenames);
5 kx filenames = nextf;
5 kx
5 kx for (c = 1; nextf; ++c, nextf = nextf->next)
5 kx ;
5 kx targets = xmalloc (c * sizeof (const char *));
5 kx target_pats = xmalloc (c * sizeof (const char *));
5 kx
5 kx targets[0] = name;
5 kx target_pats[0] = implicit_percent;
5 kx
5 kx c = 1;
5 kx while (filenames)
5 kx {
5 kx name = filenames->name;
5 kx implicit_percent = find_percent_cached (&name);
5 kx
5 kx if (implicit_percent == 0)
5 kx O (fatal, flocp, _("mixed implicit and normal rules"));
5 kx
5 kx targets[c] = name;
5 kx target_pats[c] = implicit_percent;
5 kx ++c;
5 kx
5 kx nextf = filenames->next;
5 kx free_ns (filenames);
5 kx filenames = nextf;
5 kx }
5 kx
5 kx create_pattern_rule (targets, target_pats, c, two_colon, deps, cmds, 1);
5 kx
5 kx return;
5 kx }
5 kx
5 kx
5 kx /* Walk through each target and create it in the database.
5 kx We already set up the first target, above. */
5 kx while (1)
5 kx {
5 kx struct nameseq *nextf = filenames->next;
5 kx struct file *f;
5 kx struct dep *this = 0;
5 kx
5 kx free_ns (filenames);
5 kx
5 kx /* If this is a static pattern rule:
5 kx 'targets: target%pattern: prereq%pattern; recipe',
5 kx make sure the pattern matches this target name. */
5 kx if (pattern && !pattern_matches (pattern, pattern_percent, name))
5 kx OS (error, flocp,
5 kx _("target '%s' doesn't match the target pattern"), name);
5 kx else if (deps)
5 kx /* If there are multiple targets, copy the chain DEPS for all but the
5 kx last one. It is not safe for the same deps to go in more than one
5 kx place in the database. */
5 kx this = nextf != 0 ? copy_dep_chain (deps) : deps;
5 kx
5 kx /* Find or create an entry in the file database for this target. */
5 kx if (!two_colon)
5 kx {
5 kx /* Single-colon. Combine this rule with the file's existing record,
5 kx if any. */
5 kx f = enter_file (strcache_add (name));
5 kx if (f->double_colon)
5 kx OS (fatal, flocp,
5 kx _("target file '%s' has both : and :: entries"), f->name);
5 kx
5 kx /* If CMDS == F->CMDS, this target was listed in this rule
5 kx more than once. Just give a warning since this is harmless. */
5 kx if (cmds != 0 && cmds == f->cmds)
5 kx OS (error, flocp,
5 kx _("target '%s' given more than once in the same rule"),
5 kx f->name);
5 kx
5 kx /* Check for two single-colon entries both with commands.
5 kx Check is_target so that we don't lose on files such as .c.o
5 kx whose commands were preinitialized. */
5 kx else if (cmds != 0 && f->cmds != 0 && f->is_target)
5 kx {
5 kx size_t l = strlen (f->name);
5 kx error (&cmds->fileinfo, l,
5 kx _("warning: overriding recipe for target '%s'"),
5 kx f->name);
5 kx error (&f->cmds->fileinfo, l,
5 kx _("warning: ignoring old recipe for target '%s'"),
5 kx f->name);
5 kx }
5 kx
5 kx /* Defining .DEFAULT with no deps or cmds clears it. */
5 kx if (f == default_file && this == 0 && cmds == 0)
5 kx f->cmds = 0;
5 kx if (cmds != 0)
5 kx f->cmds = cmds;
5 kx
5 kx /* Defining .SUFFIXES with no dependencies clears out the list of
5 kx suffixes. */
5 kx if (f == suffix_file && this == 0)
5 kx {
5 kx free_dep_chain (f->deps);
5 kx f->deps = 0;
5 kx }
5 kx }
5 kx else
5 kx {
5 kx /* Double-colon. Make a new record even if there already is one. */
5 kx f = lookup_file (name);
5 kx
5 kx /* Check for both : and :: rules. Check is_target so we don't lose
5 kx on default suffix rules or makefiles. */
5 kx if (f != 0 && f->is_target && !f->double_colon)
5 kx OS (fatal, flocp,
5 kx _("target file '%s' has both : and :: entries"), f->name);
5 kx
5 kx f = enter_file (strcache_add (name));
5 kx /* If there was an existing entry and it was a double-colon entry,
5 kx enter_file will have returned a new one, making it the prev
5 kx pointer of the old one, and setting its double_colon pointer to
5 kx the first one. */
5 kx if (f->double_colon == 0)
5 kx /* This is the first entry for this name, so we must set its
5 kx double_colon pointer to itself. */
5 kx f->double_colon = f;
5 kx
5 kx f->cmds = cmds;
5 kx }
5 kx
5 kx if (are_also_makes)
5 kx {
5 kx struct dep *also = alloc_dep();
5 kx also->name = f->name;
5 kx also->file = f;
5 kx also->next = also_make;
5 kx also_make = also;
5 kx }
5 kx
5 kx f->is_target = 1;
5 kx
5 kx /* If this is a static pattern rule, set the stem to the part of its
5 kx name that matched the '%' in the pattern, so you can use $* in the
5 kx commands. If we didn't do it before, enter the prereqs now. */
5 kx if (pattern)
5 kx {
5 kx static const char *percent = "%";
5 kx char *o = patsubst_expand_pat (variable_buffer, name, pattern,
5 kx percent, pattern_percent+1, percent+1);
5 kx f->stem = strcache_add_len (variable_buffer, o - variable_buffer);
5 kx if (this)
5 kx {
5 kx if (! this->need_2nd_expansion)
5 kx this = enter_prereqs (this, f->stem);
5 kx else
5 kx this->stem = f->stem;
5 kx }
5 kx }
5 kx
5 kx /* Add the dependencies to this file entry. */
5 kx if (this != 0)
5 kx {
5 kx /* Add the file's old deps and the new ones in THIS together. */
5 kx if (f->deps == 0)
5 kx f->deps = this;
5 kx else if (cmds != 0)
5 kx {
5 kx struct dep *d = this;
5 kx
5 kx /* If this rule has commands, put these deps first. */
5 kx while (d->next != 0)
5 kx d = d->next;
5 kx
5 kx d->next = f->deps;
5 kx f->deps = this;
5 kx }
5 kx else
5 kx {
5 kx struct dep *d = f->deps;
5 kx
5 kx /* A rule without commands: put its prereqs at the end. */
5 kx while (d->next != 0)
5 kx d = d->next;
5 kx
5 kx d->next = this;
5 kx }
5 kx }
5 kx
5 kx name = f->name;
5 kx
5 kx /* All done! Set up for the next one. */
5 kx if (nextf == 0)
5 kx break;
5 kx
5 kx filenames = nextf;
5 kx
5 kx /* Reduce escaped percents. If there are any unescaped it's an error */
5 kx name = filenames->name;
5 kx if (find_percent_cached (&name))
5 kx O (error, flocp,
5 kx _("*** mixed implicit and normal rules: deprecated syntax"));
5 kx }
5 kx
5 kx /* If there are also-makes, then populate a copy of the also-make list into
5 kx each one. For the last file, we take our original also_make list instead
5 kx wastefully copying it one more time and freeing it. */
5 kx {
5 kx struct dep *i;
5 kx
5 kx for (i = also_make; i != NULL; i = i->next)
5 kx {
5 kx struct file *f = i->file;
5 kx struct dep *cpy = i->next ? copy_dep_chain (also_make) : also_make;
5 kx
5 kx if (f->also_make)
5 kx {
5 kx OS (error, &cmds->fileinfo,
5 kx _("warning: overriding group membership for target '%s'"),
5 kx f->name);
5 kx free_dep_chain (f->also_make);
5 kx }
5 kx
5 kx f->also_make = cpy;
5 kx }
5 kx }
5 kx }
5 kx
5 kx /* Search STRING for an unquoted STOPMAP.
5 kx Backslashes quote elements from STOPMAP and backslash.
5 kx Quoting backslashes are removed from STRING by compacting it into itself.
5 kx Returns a pointer to the first unquoted STOPCHAR if there is one, or nil if
5 kx there are none.
5 kx
5 kx If MAP_VARIABLE is set, then the complete contents of variable references
5 kx are skipped, even if the contain STOPMAP characters. */
5 kx
5 kx static char *
5 kx find_map_unquote (char *string, int stopmap)
5 kx {
5 kx size_t string_len = 0;
5 kx char *p = string;
5 kx
5 kx /* Always stop on NUL. */
5 kx stopmap |= MAP_NUL;
5 kx
5 kx while (1)
5 kx {
5 kx while (! STOP_SET (*p, stopmap))
5 kx ++p;
5 kx
5 kx if (*p == '\0')
5 kx break;
5 kx
5 kx /* If we stopped due to a variable reference, skip over its contents. */
5 kx if (*p == '$')
5 kx {
5 kx char openparen = p[1];
5 kx
5 kx /* Check if '$' is the last character in the string. */
5 kx if (openparen == '\0')
5 kx break;
5 kx
5 kx p += 2;
5 kx
5 kx /* Skip the contents of a non-quoted, multi-char variable ref. */
5 kx if (openparen == '(' || openparen == '{')
5 kx {
5 kx unsigned int pcount = 1;
5 kx char closeparen = (openparen == '(' ? ')' : '}');
5 kx
5 kx while (*p)
5 kx {
5 kx if (*p == openparen)
5 kx ++pcount;
5 kx else if (*p == closeparen)
5 kx if (--pcount == 0)
5 kx {
5 kx ++p;
5 kx break;
5 kx }
5 kx ++p;
5 kx }
5 kx }
5 kx
5 kx /* Skipped the variable reference: look for STOPCHARS again. */
5 kx continue;
5 kx }
5 kx
5 kx if (p > string && p[-1] == '\\')
5 kx {
5 kx /* Search for more backslashes. */
5 kx int i = -2;
5 kx while (&p[i] >= string && p[i] == '\\')
5 kx --i;
5 kx ++i;
5 kx /* Only compute the length if really needed. */
5 kx if (string_len == 0)
5 kx string_len = strlen (string);
5 kx /* The number of backslashes is now -I.
5 kx Copy P over itself to swallow half of them. */
5 kx memmove (&p[i], &p[i/2], (string_len - (p - string)) - (i/2) + 1);
5 kx p += i/2;
5 kx if (i % 2 == 0)
5 kx /* All the backslashes quoted each other; the STOPCHAR was
5 kx unquoted. */
5 kx return p;
5 kx
5 kx /* The STOPCHAR was quoted by a backslash. Look for another. */
5 kx }
5 kx else
5 kx /* No backslash in sight. */
5 kx return p;
5 kx }
5 kx
5 kx /* Never hit a STOPCHAR or blank (with BLANK nonzero). */
5 kx return 0;
5 kx }
5 kx
5 kx static char *
5 kx find_char_unquote (char *string, int stop)
5 kx {
5 kx size_t string_len = 0;
5 kx char *p = string;
5 kx
5 kx while (1)
5 kx {
5 kx p = strchr(p, stop);
5 kx
5 kx if (!p)
5 kx return NULL;
5 kx
5 kx if (p > string && p[-1] == '\\')
5 kx {
5 kx /* Search for more backslashes. */
5 kx int i = -2;
5 kx while (&p[i] >= string && p[i] == '\\')
5 kx --i;
5 kx ++i;
5 kx /* Only compute the length if really needed. */
5 kx if (string_len == 0)
5 kx string_len = strlen (string);
5 kx /* The number of backslashes is now -I.
5 kx Copy P over itself to swallow half of them. */
5 kx memmove (&p[i], &p[i/2], (string_len - (p - string)) - (i/2) + 1);
5 kx p += i/2;
5 kx if (i % 2 == 0)
5 kx /* All the backslashes quoted each other; the STOPCHAR was
5 kx unquoted. */
5 kx return p;
5 kx
5 kx /* The STOPCHAR was quoted by a backslash. Look for another. */
5 kx }
5 kx else
5 kx /* No backslash in sight. */
5 kx return p;
5 kx }
5 kx }
5 kx
5 kx /* Unescape a character in a string. The string is compressed onto itself. */
5 kx
5 kx static char *
5 kx unescape_char (char *string, int c)
5 kx {
5 kx char *p = string;
5 kx char *s = string;
5 kx
5 kx while (*s != '\0')
5 kx {
5 kx if (*s == '\\')
5 kx {
5 kx char *e = s;
5 kx size_t l;
5 kx
5 kx /* We found a backslash. See if it's escaping our character. */
5 kx while (*e == '\\')
5 kx ++e;
5 kx l = e - s;
5 kx
5 kx if (*e != c || l%2 == 0)
5 kx {
5 kx /* It's not; just take it all without unescaping. */
5 kx memmove (p, s, l);
5 kx p += l;
5 kx
5 kx /* If we hit the end of the string, we're done. */
5 kx if (*e == '\0')
5 kx break;
5 kx }
5 kx else if (l > 1)
5 kx {
5 kx /* It is, and there's >1 backslash. Take half of them. */
5 kx l /= 2;
5 kx memmove (p, s, l);
5 kx p += l;
5 kx }
5 kx
5 kx s = e;
5 kx }
5 kx
5 kx *(p++) = *(s++);
5 kx }
5 kx
5 kx *p = '\0';
5 kx return string;
5 kx }
5 kx
5 kx /* Search PATTERN for an unquoted % and handle quoting. */
5 kx
5 kx char *
5 kx find_percent (char *pattern)
5 kx {
5 kx return find_char_unquote (pattern, '%');
5 kx }
5 kx
5 kx /* Search STRING for an unquoted % and handle quoting. Returns a pointer to
5 kx the % or NULL if no % was found.
5 kx This version is used with strings in the string cache: if there's a need to
5 kx modify the string a new version will be added to the string cache and
5 kx *STRING will be set to that. */
5 kx
5 kx const char *
5 kx find_percent_cached (const char **string)
5 kx {
5 kx const char *p = *string;
5 kx char *new = 0;
5 kx size_t slen = 0;
5 kx
5 kx /* If the first char is a % return now. This lets us avoid extra tests
5 kx inside the loop. */
5 kx if (*p == '%')
5 kx return p;
5 kx
5 kx while (1)
5 kx {
5 kx p = strchr(p, '%');
5 kx
5 kx if (!p)
5 kx break;
5 kx
5 kx /* See if this % is escaped with a backslash; if not we're done. */
5 kx if (p[-1] != '\\')
5 kx break;
5 kx
5 kx {
5 kx /* Search for more backslashes. */
5 kx char *pv;
5 kx int i = -2;
5 kx
5 kx while (&p[i] >= *string && p[i] == '\\')
5 kx --i;
5 kx ++i;
5 kx
5 kx /* At this point we know we'll need to allocate a new string.
5 kx Make a copy if we haven't yet done so. */
5 kx if (! new)
5 kx {
5 kx slen = strlen (*string);
5 kx new = xmalloc (slen + 1);
5 kx memcpy (new, *string, slen + 1);
5 kx p = new + (p - *string);
5 kx *string = new;
5 kx }
5 kx
5 kx /* At this point *string, p, and new all point into the same string.
5 kx Get a non-const version of p so we can modify new. */
5 kx pv = new + (p - *string);
5 kx
5 kx /* The number of backslashes is now -I.
5 kx Copy P over itself to swallow half of them. */
5 kx memmove (&pv[i], &pv[i/2], (slen - (pv - new)) - (i/2) + 1);
5 kx p += i/2;
5 kx
5 kx /* If the backslashes quoted each other; the % was unquoted. */
5 kx if (i % 2 == 0)
5 kx break;
5 kx }
5 kx }
5 kx
5 kx /* If we had to change STRING, add it to the strcache. */
5 kx if (new)
5 kx {
5 kx *string = strcache_add (*string);
5 kx if (p)
5 kx p = *string + (p - new);
5 kx free (new);
5 kx }
5 kx
5 kx /* If we didn't find a %, return NULL. Otherwise return a ptr to it. */
5 kx return p;
5 kx }
5 kx
5 kx /* Find the next line of text in an eval buffer, combining continuation lines
5 kx into one line.
5 kx Return the number of actual lines read (> 1 if continuation lines).
5 kx Returns -1 if there's nothing left in the buffer.
5 kx
5 kx After this function, ebuf->buffer points to the first character of the
5 kx line we just found.
5 kx */
5 kx
5 kx /* Read a line of text from a STRING.
5 kx Since we aren't really reading from a file, don't bother with linenumbers.
5 kx */
5 kx
5 kx static long
5 kx readstring (struct ebuffer *ebuf)
5 kx {
5 kx char *eol;
5 kx
5 kx /* If there is nothing left in this buffer, return 0. */
5 kx if (ebuf->bufnext >= ebuf->bufstart + ebuf->size)
5 kx return -1;
5 kx
5 kx /* Set up a new starting point for the buffer, and find the end of the
5 kx next logical line (taking into account backslash/newline pairs). */
5 kx
5 kx eol = ebuf->buffer = ebuf->bufnext;
5 kx
5 kx while (1)
5 kx {
5 kx int backslash = 0;
5 kx const char *bol = eol;
5 kx const char *p;
5 kx
5 kx /* Find the next newline. At EOS, stop. */
5 kx p = eol = strchr (eol , '\n');
5 kx if (!eol)
5 kx {
5 kx ebuf->bufnext = ebuf->bufstart + ebuf->size + 1;
5 kx return 0;
5 kx }
5 kx
5 kx /* Found a newline; if it's escaped continue; else we're done. */
5 kx while (p > bol && *(--p) == '\\')
5 kx backslash = !backslash;
5 kx if (!backslash)
5 kx break;
5 kx ++eol;
5 kx }
5 kx
5 kx /* Overwrite the newline char. */
5 kx *eol = '\0';
5 kx ebuf->bufnext = eol+1;
5 kx
5 kx return 0;
5 kx }
5 kx
5 kx static long
5 kx readline (struct ebuffer *ebuf)
5 kx {
5 kx char *p;
5 kx char *end;
5 kx char *start;
5 kx long nlines = 0;
5 kx
5 kx /* The behaviors between string and stream buffers are different enough to
5 kx warrant different functions. Do the Right Thing. */
5 kx
5 kx if (!ebuf->fp)
5 kx return readstring (ebuf);
5 kx
5 kx /* When reading from a file, we always start over at the beginning of the
5 kx buffer for each new line. */
5 kx
5 kx p = start = ebuf->bufstart;
5 kx end = p + ebuf->size;
5 kx *p = '\0';
5 kx
5 kx while (fgets (p, (int) (end - p), ebuf->fp) != 0)
5 kx {
5 kx char *p2;
5 kx size_t len;
5 kx int backslash;
5 kx
5 kx len = strlen (p);
5 kx if (len == 0)
5 kx {
5 kx /* This only happens when the first thing on the line is a '\0'.
5 kx It is a pretty hopeless case, but (wonder of wonders) Athena
5 kx lossage strikes again! (xmkmf puts NULs in its makefiles.)
5 kx There is nothing really to be done; we synthesize a newline so
5 kx the following line doesn't appear to be part of this line. */
5 kx O (error, &ebuf->floc,
5 kx _("warning: NUL character seen; rest of line ignored"));
5 kx p[0] = '\n';
5 kx len = 1;
5 kx }
5 kx
5 kx /* Jump past the text we just read. */
5 kx p += len;
5 kx
5 kx /* If the last char isn't a newline, the whole line didn't fit into the
5 kx buffer. Get some more buffer and try again. */
5 kx if (p[-1] != '\n')
5 kx goto more_buffer;
5 kx
5 kx /* We got a newline, so add one to the count of lines. */
5 kx ++nlines;
5 kx
5 kx #if !defined(WINDOWS32) && !defined(__MSDOS__) && !defined(__EMX__)
5 kx /* Check to see if the line was really ended with CRLF; if so ignore
5 kx the CR. */
5 kx if ((p - start) > 1 && p[-2] == '\r')
5 kx {
5 kx --p;
5 kx memmove (p-1, p, strlen (p) + 1);
5 kx }
5 kx #endif
5 kx
5 kx backslash = 0;
5 kx for (p2 = p - 2; p2 >= start; --p2)
5 kx {
5 kx if (*p2 != '\\')
5 kx break;
5 kx backslash = !backslash;
5 kx }
5 kx
5 kx if (!backslash)
5 kx {
5 kx p[-1] = '\0';
5 kx break;
5 kx }
5 kx
5 kx /* It was a backslash/newline combo. If we have more space, read
5 kx another line. */
5 kx if (end - p >= 80)
5 kx continue;
5 kx
5 kx /* We need more space at the end of our buffer, so realloc it.
5 kx Make sure to preserve the current offset of p. */
5 kx more_buffer:
5 kx {
5 kx size_t off = p - start;
5 kx ebuf->size *= 2;
5 kx start = ebuf->buffer = ebuf->bufstart = xrealloc (start, ebuf->size);
5 kx p = start + off;
5 kx end = start + ebuf->size;
5 kx *p = '\0';
5 kx }
5 kx }
5 kx
5 kx if (ferror (ebuf->fp))
5 kx pfatal_with_name (ebuf->floc.filenm);
5 kx
5 kx /* If we found some lines, return how many.
5 kx If we didn't, but we did find _something_, that indicates we read the last
5 kx line of a file with no final newline; return 1.
5 kx If we read nothing, we're at EOF; return -1. */
5 kx
5 kx return nlines ? nlines : p == ebuf->bufstart ? -1 : 1;
5 kx }
5 kx
5 kx /* Parse the next "makefile word" from the input buffer, and return info
5 kx about it.
5 kx
5 kx A "makefile word" is one of:
5 kx
5 kx w_bogus Should never happen
5 kx w_eol End of input
5 kx w_static A static word; cannot be expanded
5 kx w_variable A word containing one or more variables/functions
5 kx w_colon A colon
5 kx w_dcolon A double-colon
5 kx w_ampcolon An ampersand-colon (&:) token
5 kx w_ampdcolon An ampersand-double-colon (&::) token
5 kx w_semicolon A semicolon
5 kx w_varassign A variable assignment operator (=, :=, ::=, +=, ?=, or !=)
5 kx
5 kx Note that this function is only used when reading certain parts of the
5 kx makefile. Don't use it where special rules hold sway (RHS of a variable,
5 kx in a command list, etc.) */
5 kx
5 kx static enum make_word_type
5 kx get_next_mword (char *buffer, char **startp, size_t *length)
5 kx {
5 kx enum make_word_type wtype;
5 kx char *p = buffer, *beg;
5 kx char c;
5 kx
5 kx /* Skip any leading whitespace. */
5 kx while (ISSPACE (*p))
5 kx ++p;
5 kx
5 kx beg = p;
5 kx c = *(p++);
5 kx
5 kx /* Look at the start of the word to see if it's simple. */
5 kx switch (c)
5 kx {
5 kx case '\0':
5 kx wtype = w_eol;
5 kx goto done;
5 kx
5 kx case ';':
5 kx wtype = w_semicolon;
5 kx goto done;
5 kx
5 kx case '=':
5 kx wtype = w_varassign;
5 kx goto done;
5 kx
5 kx case ':':
5 kx if (*p == '=')
5 kx {
5 kx ++p;
5 kx wtype = w_varassign; /* := */
5 kx }
5 kx else if (*p == ':')
5 kx {
5 kx ++p;
5 kx if (p[1] == '=')
5 kx {
5 kx ++p;
5 kx wtype = w_varassign; /* ::= */
5 kx }
5 kx else
5 kx wtype = w_dcolon;
5 kx }
5 kx else
5 kx wtype = w_colon;
5 kx goto done;
5 kx
5 kx case '&':
5 kx if (*p == ':')
5 kx {
5 kx ++p;
5 kx if (*p != ':')
5 kx wtype = w_ampcolon; /* &: */
5 kx else
5 kx {
5 kx ++p;
5 kx wtype = w_ampdcolon; /* &:: */
5 kx }
5 kx goto done;
5 kx }
5 kx break;
5 kx
5 kx case '+':
5 kx case '?':
5 kx case '!':
5 kx if (*p == '=')
5 kx {
5 kx ++p;
5 kx wtype = w_varassign; /* += or ?= or != */
5 kx goto done;
5 kx }
5 kx break;
5 kx
5 kx default:
5 kx break;
5 kx }
5 kx
5 kx /* This is some non-operator word. A word consists of the longest
5 kx string of characters that doesn't contain whitespace, one of [:=#],
5 kx or [?+!]=, or &:. */
5 kx
5 kx /* We start out assuming a static word; if we see a variable we'll
5 kx adjust our assumptions then. */
5 kx wtype = w_static;
5 kx
5 kx /* We already found the first value of "c", above. */
5 kx while (1)
5 kx {
5 kx char closeparen;
5 kx int count;
5 kx
5 kx if (END_OF_TOKEN (c))
5 kx goto done_word;
5 kx
5 kx switch (c)
5 kx {
5 kx case '=':
5 kx goto done_word;
5 kx
5 kx case ':':
5 kx #ifdef HAVE_DOS_PATHS
5 kx /* A word CAN include a colon in its drive spec. The drive
5 kx spec is allowed either at the beginning of a word, or as part
5 kx of the archive member name, like in "libfoo.a(d:/foo/bar.o)". */
5 kx if ((p - beg == 2 || (p - beg > 2 && p[-3] == '('))
5 kx && isalpha ((unsigned char)p[-2]))
5 kx break;
5 kx #endif
5 kx goto done_word;
5 kx
5 kx case '$':
5 kx c = *(p++);
5 kx if (c == '$')
5 kx break;
5 kx if (c == '\0')
5 kx goto done_word;
5 kx
5 kx /* This is a variable reference, so note that it's expandable.
5 kx Then read it to the matching close paren. */
5 kx wtype = w_variable;
5 kx
5 kx if (c == '(')
5 kx closeparen = ')';
5 kx else if (c == '{')
5 kx closeparen = '}';
5 kx else
5 kx /* This is a single-letter variable reference. */
5 kx break;
5 kx
5 kx for (count=0; *p != '\0'; ++p)
5 kx {
5 kx if (*p == c)
5 kx ++count;
5 kx else if (*p == closeparen && --count < 0)
5 kx {
5 kx ++p;
5 kx break;
5 kx }
5 kx }
5 kx break;
5 kx
5 kx case '?':
5 kx case '+':
5 kx if (*p == '=')
5 kx goto done_word;
5 kx break;
5 kx
5 kx case '\\':
5 kx switch (*p)
5 kx {
5 kx case ':':
5 kx case ';':
5 kx case '=':
5 kx case '\\':
5 kx ++p;
5 kx break;
5 kx }
5 kx break;
5 kx
5 kx case '&':
5 kx if (*p == ':')
5 kx goto done_word;
5 kx break;
5 kx
5 kx default:
5 kx break;
5 kx }
5 kx
5 kx c = *(p++);
5 kx }
5 kx done_word:
5 kx --p;
5 kx
5 kx done:
5 kx if (startp)
5 kx *startp = beg;
5 kx if (length)
5 kx *length = p - beg;
5 kx return wtype;
5 kx }
5 kx
5 kx /* Construct the list of include directories
5 kx from the arguments and the default list. */
5 kx
5 kx void
5 kx construct_include_path (const char **arg_dirs)
5 kx {
5 kx #ifdef VAXC /* just don't ask ... */
5 kx stat_t stbuf;
5 kx #else
5 kx struct stat stbuf;
5 kx #endif
5 kx const char **dirs;
5 kx const char **cpp;
5 kx size_t idx;
5 kx
5 kx /* Compute the number of pointers we need in the table. */
5 kx idx = sizeof (default_include_directories) / sizeof (const char *);
5 kx if (arg_dirs)
5 kx for (cpp = arg_dirs; *cpp != 0; ++cpp)
5 kx ++idx;
5 kx
5 kx #ifdef __MSDOS__
5 kx /* Add one for $DJDIR. */
5 kx ++idx;
5 kx #endif
5 kx
5 kx dirs = xmalloc (idx * sizeof (const char *));
5 kx
5 kx idx = 0;
5 kx max_incl_len = 0;
5 kx
5 kx /* First consider any dirs specified with -I switches.
5 kx Ignore any that don't exist. Remember the maximum string length. */
5 kx
5 kx if (arg_dirs)
5 kx while (*arg_dirs != 0)
5 kx {
5 kx const char *dir = *(arg_dirs++);
5 kx char *expanded = 0;
5 kx int e;
5 kx
5 kx if (dir[0] == '~')
5 kx {
5 kx expanded = tilde_expand (dir);
5 kx if (expanded != 0)
5 kx dir = expanded;
5 kx }
5 kx
5 kx EINTRLOOP (e, stat (dir, &stbuf));
5 kx if (e == 0 && S_ISDIR (stbuf.st_mode))
5 kx {
5 kx size_t len = strlen (dir);
5 kx /* If dir name is written with trailing slashes, discard them. */
5 kx while (len > 1 && dir[len - 1] == '/')
5 kx --len;
5 kx if (len > max_incl_len)
5 kx max_incl_len = len;
5 kx dirs[idx++] = strcache_add_len (dir, len);
5 kx }
5 kx
5 kx free (expanded);
5 kx }
5 kx
5 kx /* Now add the standard default dirs at the end. */
5 kx
5 kx #ifdef __MSDOS__
5 kx {
5 kx /* The environment variable $DJDIR holds the root of the DJGPP directory
5 kx tree; add ${DJDIR}/include. */
5 kx struct variable *djdir = lookup_variable ("DJDIR", 5);
5 kx
5 kx if (djdir)
5 kx {
5 kx size_t len = strlen (djdir->value) + 8;
5 kx char *defdir = alloca (len + 1);
5 kx
5 kx strcat (strcpy (defdir, djdir->value), "/include");
5 kx dirs[idx++] = strcache_add (defdir);
5 kx
5 kx if (len > max_incl_len)
5 kx max_incl_len = len;
5 kx }
5 kx }
5 kx #endif
5 kx
5 kx for (cpp = default_include_directories; *cpp != 0; ++cpp)
5 kx {
5 kx int e;
5 kx
5 kx EINTRLOOP (e, stat (*cpp, &stbuf));
5 kx if (e == 0 && S_ISDIR (stbuf.st_mode))
5 kx {
5 kx size_t len = strlen (*cpp);
5 kx /* If dir name is written with trailing slashes, discard them. */
5 kx while (len > 1 && (*cpp)[len - 1] == '/')
5 kx --len;
5 kx if (len > max_incl_len)
5 kx max_incl_len = len;
5 kx dirs[idx++] = strcache_add_len (*cpp, len);
5 kx }
5 kx }
5 kx
5 kx dirs[idx] = 0;
5 kx
5 kx /* Now add each dir to the .INCLUDE_DIRS variable. */
5 kx
5 kx for (cpp = dirs; *cpp != 0; ++cpp)
5 kx do_variable_definition (NILF, ".INCLUDE_DIRS", *cpp,
5 kx o_default, f_append, 0);
5 kx
5 kx include_directories = dirs;
5 kx }
5 kx
5 kx /* Expand ~ or ~USER at the beginning of NAME.
5 kx Return a newly malloc'd string or 0. */
5 kx
5 kx char *
5 kx tilde_expand (const char *name)
5 kx {
5 kx #ifndef VMS
5 kx if (name[1] == '/' || name[1] == '\0')
5 kx {
5 kx char *home_dir;
5 kx int is_variable;
5 kx
5 kx {
5 kx /* Turn off --warn-undefined-variables while we expand HOME. */
5 kx int save = warn_undefined_variables_flag;
5 kx warn_undefined_variables_flag = 0;
5 kx
5 kx home_dir = allocated_variable_expand ("$(HOME)");
5 kx
5 kx warn_undefined_variables_flag = save;
5 kx }
5 kx
5 kx is_variable = home_dir[0] != '\0';
5 kx if (!is_variable)
5 kx {
5 kx free (home_dir);
5 kx home_dir = getenv ("HOME");
5 kx }
5 kx # if !defined(_AMIGA) && !defined(WINDOWS32)
5 kx if (home_dir == 0 || home_dir[0] == '\0')
5 kx {
5 kx char *logname = getlogin ();
5 kx home_dir = 0;
5 kx if (logname != 0)
5 kx {
5 kx struct passwd *p = getpwnam (logname);
5 kx if (p != 0)
5 kx home_dir = p->pw_dir;
5 kx }
5 kx }
5 kx # endif /* !AMIGA && !WINDOWS32 */
5 kx if (home_dir != 0)
5 kx {
5 kx char *new = xstrdup (concat (2, home_dir, name + 1));
5 kx if (is_variable)
5 kx free (home_dir);
5 kx return new;
5 kx }
5 kx }
5 kx # if !defined(_AMIGA) && !defined(WINDOWS32)
5 kx else
5 kx {
5 kx struct passwd *pwent;
5 kx char *userend = strchr (name + 1, '/');
5 kx if (userend != 0)
5 kx *userend = '\0';
5 kx pwent = getpwnam (name + 1);
5 kx if (pwent != 0)
5 kx {
5 kx if (userend == 0)
5 kx return xstrdup (pwent->pw_dir);
5 kx else
5 kx return xstrdup (concat (3, pwent->pw_dir, "/", userend + 1));
5 kx }
5 kx else if (userend != 0)
5 kx *userend = '/';
5 kx }
5 kx # endif /* !AMIGA && !WINDOWS32 */
5 kx #endif /* !VMS */
5 kx return 0;
5 kx }
5 kx
5 kx /* Parse a string into a sequence of filenames represented as a chain of
5 kx struct nameseq's and return that chain. Optionally expand the strings via
5 kx glob().
5 kx
5 kx The string is passed as STRINGP, the address of a string pointer.
5 kx The string pointer is updated to point at the first character
5 kx not parsed, which either is a null char or equals STOPMAP.
5 kx
5 kx SIZE is how large (in bytes) each element in the new chain should be.
5 kx This is useful if we want them actually to be other structures
5 kx that have room for additional info.
5 kx
5 kx STOPMAP is a map of characters that tell us to stop parsing.
5 kx
5 kx PREFIX, if non-null, is added to the beginning of each filename.
5 kx
5 kx FLAGS allows one or more of the following bitflags to be set:
5 kx PARSEFS_NOSTRIP - Do no strip './'s off the beginning
5 kx PARSEFS_NOAR - Do not check filenames for archive references
5 kx PARSEFS_NOGLOB - Do not expand globbing characters
5 kx PARSEFS_EXISTS - Only return globbed files that actually exist
5 kx (cannot also set NOGLOB)
5 kx PARSEFS_NOCACHE - Do not add filenames to the strcache (caller frees)
5 kx */
5 kx
5 kx void *
5 kx parse_file_seq (char **stringp, size_t size, int stopmap,
5 kx const char *prefix, int flags)
5 kx {
5 kx /* tmp points to tmpbuf after the prefix, if any.
5 kx tp is the end of the buffer. */
5 kx static char *tmpbuf = NULL;
5 kx
5 kx int cachep = NONE_SET (flags, PARSEFS_NOCACHE);
5 kx
5 kx struct nameseq *new = 0;
5 kx struct nameseq **newp = &new;
5 kx #define NEWELT(_n) do { \
5 kx const char *__n = (_n); \
5 kx *newp = xcalloc (size); \
5 kx (*newp)->name = (cachep ? strcache_add (__n) : xstrdup (__n)); \
5 kx newp = &(*newp)->next; \
5 kx } while(0)
5 kx
5 kx char *p;
5 kx glob_t gl;
5 kx char *tp;
5 kx int findmap = stopmap|MAP_VMSCOMMA|MAP_NUL;
5 kx
5 kx if (NONE_SET (flags, PARSEFS_ONEWORD))
5 kx findmap |= MAP_BLANK;
5 kx
5 kx /* Always stop on NUL. */
5 kx stopmap |= MAP_NUL;
5 kx
5 kx if (size < sizeof (struct nameseq))
5 kx size = sizeof (struct nameseq);
5 kx
5 kx if (NONE_SET (flags, PARSEFS_NOGLOB))
5 kx dir_setup_glob (&gl);
5 kx
5 kx /* Get enough temporary space to construct the largest possible target. */
5 kx {
5 kx static size_t tmpbuf_len = 0;
5 kx size_t l = strlen (*stringp) + 1;
5 kx if (l > tmpbuf_len)
5 kx {
5 kx tmpbuf = xrealloc (tmpbuf, l);
5 kx tmpbuf_len = l;
5 kx }
5 kx }
5 kx tp = tmpbuf;
5 kx
5 kx /* Parse STRING. P will always point to the end of the parsed content. */
5 kx p = *stringp;
5 kx while (1)
5 kx {
5 kx const char *name;
5 kx const char **nlist = 0;
5 kx char *tildep = 0;
5 kx int globme = 1;
5 kx #ifndef NO_ARCHIVES
5 kx char *arname = 0;
5 kx char *memname = 0;
5 kx #endif
5 kx char *s;
5 kx size_t nlen;
5 kx int tot, i;
5 kx
5 kx /* Skip whitespace; at the end of the string or STOPCHAR we're done. */
5 kx NEXT_TOKEN (p);
5 kx if (STOP_SET (*p, stopmap))
5 kx break;
5 kx
5 kx /* There are names left, so find the end of the next name.
5 kx Throughout this iteration S points to the start. */
5 kx s = p;
5 kx p = find_map_unquote (p, findmap);
5 kx
5 kx #ifdef VMS
5 kx /* convert comma separated list to space separated */
5 kx if (p && *p == ',')
5 kx *p =' ';
5 kx #endif
5 kx #ifdef _AMIGA
5 kx /* If we stopped due to a device name, skip it. */
5 kx if (p && p != s+1 && p[0] == ':')
5 kx p = find_map_unquote (p+1, findmap);
5 kx #endif
5 kx #ifdef HAVE_DOS_PATHS
5 kx /* If we stopped due to a drive specifier, skip it.
5 kx Tokens separated by spaces are treated as separate paths since make
5 kx doesn't allow path names with spaces. */
5 kx if (p && p == s+1 && p[0] == ':'
5 kx && isalpha ((unsigned char)s[0]) && STOP_SET (p[1], MAP_DIRSEP))
5 kx p = find_map_unquote (p+1, findmap);
5 kx #endif
5 kx
5 kx if (!p)
5 kx p = s + strlen (s);
5 kx
5 kx /* Strip leading "this directory" references. */
5 kx if (NONE_SET (flags, PARSEFS_NOSTRIP))
5 kx #ifdef VMS
5 kx /* Skip leading '[]'s. should only be one set or bug somewhere else */
5 kx if (p - s > 2 && s[0] == '[' && s[1] == ']')
5 kx s += 2;
5 kx /* Skip leading '<>'s. should only be one set or bug somewhere else */
5 kx if (p - s > 2 && s[0] == '<' && s[1] == '>')
5 kx s += 2;
5 kx #endif
5 kx /* Skip leading './'s. */
5 kx while (p - s > 2 && s[0] == '.' && s[1] == '/')
5 kx {
5 kx /* Skip "./" and all following slashes. */
5 kx s += 2;
5 kx while (*s == '/')
5 kx ++s;
5 kx }
5 kx
5 kx /* Extract the filename just found, and skip it.
5 kx Set NAME to the string, and NLEN to its length. */
5 kx
5 kx if (s == p)
5 kx {
5 kx /* The name was stripped to empty ("./"). */
5 kx #if defined(_AMIGA)
5 kx /* PDS-- This cannot be right!! */
5 kx tp[0] = '\0';
5 kx nlen = 0;
5 kx #else
5 kx tp[0] = '.';
5 kx tp[1] = '/';
5 kx tp[2] = '\0';
5 kx nlen = 2;
5 kx #endif
5 kx }
5 kx else
5 kx {
5 kx #ifdef VMS
5 kx /* VMS filenames can have a ':' in them but they have to be '\'ed but we need
5 kx * to remove this '\' before we can use the filename.
5 kx * xstrdup called because S may be read-only string constant.
5 kx */
5 kx char *n = tp;
5 kx while (s < p)
5 kx {
5 kx if (s[0] == '\\' && s[1] == ':')
5 kx ++s;
5 kx *(n++) = *(s++);
5 kx }
5 kx n[0] = '\0';
5 kx nlen = strlen (tp);
5 kx #else
5 kx nlen = p - s;
5 kx memcpy (tp, s, nlen);
5 kx tp[nlen] = '\0';
5 kx #endif
5 kx }
5 kx
5 kx /* At this point, TP points to the element and NLEN is its length. */
5 kx
5 kx #ifndef NO_ARCHIVES
5 kx /* If this is the start of an archive group that isn't complete, set up
5 kx to add the archive prefix for future files. A file list like:
5 kx "libf.a(x.o y.o z.o)" needs to be expanded as:
5 kx "libf.a(x.o) libf.a(y.o) libf.a(z.o)"
5 kx
5 kx TP == TMP means we're not already in an archive group. Ignore
5 kx something starting with '(', as that cannot actually be an
5 kx archive-member reference (and treating it as such results in an empty
5 kx file name, which causes much lossage). Also if it ends in ")" then
5 kx it's a complete reference so we don't need to treat it specially.
5 kx
5 kx Finally, note that archive groups must end with ')' as the last
5 kx character, so ensure there's some word ending like that before
5 kx considering this an archive group. */
5 kx if (NONE_SET (flags, PARSEFS_NOAR)
5 kx && tp == tmpbuf && tp[0] != '(' && tp[nlen-1] != ')')
5 kx {
5 kx char *n = strchr (tp, '(');
5 kx if (n)
5 kx {
5 kx /* This looks like the first element in an open archive group.
5 kx A valid group MUST have ')' as the last character. */
5 kx const char *e = p;
5 kx do
5 kx {
5 kx const char *o = e;
5 kx NEXT_TOKEN (e);
5 kx /* Find the end of this word. We don't want to unquote and
5 kx we don't care about quoting since we're looking for the
5 kx last char in the word. */
5 kx while (! STOP_SET (*e, findmap))
5 kx ++e;
5 kx /* If we didn't move, we're done now. */
5 kx if (e == o)
5 kx break;
5 kx if (e[-1] == ')')
5 kx {
5 kx /* Found the end, so this is the first element in an
5 kx open archive group. It looks like "lib(mem".
5 kx Reset TP past the open paren. */
5 kx nlen -= (n + 1) - tp;
5 kx tp = n + 1;
5 kx
5 kx /* We can stop looking now. */
5 kx break;
5 kx }
5 kx }
5 kx while (*e != '\0');
5 kx
5 kx /* If we have just "lib(", part of something like "lib( a b)",
5 kx go to the next item. */
5 kx if (! nlen)
5 kx continue;
5 kx }
5 kx }
5 kx
5 kx /* If we are inside an archive group, make sure it has an end. */
5 kx if (tp > tmpbuf)
5 kx {
5 kx if (tp[nlen-1] == ')')
5 kx {
5 kx /* This is the natural end; reset TP. */
5 kx tp = tmpbuf;
5 kx
5 kx /* This is just ")", something like "lib(a b )": skip it. */
5 kx if (nlen == 1)
5 kx continue;
5 kx }
5 kx else
5 kx {
5 kx /* Not the end, so add a "fake" end. */
5 kx tp[nlen++] = ')';
5 kx tp[nlen] = '\0';
5 kx }
5 kx }
5 kx #endif
5 kx
5 kx /* If we're not globbing we're done: add it to the end of the chain.
5 kx Go to the next item in the string. */
5 kx if (ANY_SET (flags, PARSEFS_NOGLOB))
5 kx {
5 kx NEWELT (concat (2, prefix, tmpbuf));
5 kx continue;
5 kx }
5 kx
5 kx /* If we get here we know we're doing glob expansion.
5 kx TP is a string in tmpbuf. NLEN is no longer used.
5 kx We may need to do more work: after this NAME will be set. */
5 kx name = tmpbuf;
5 kx
5 kx /* Expand tilde if applicable. */
5 kx if (tmpbuf[0] == '~')
5 kx {
5 kx tildep = tilde_expand (tmpbuf);
5 kx if (tildep != 0)
5 kx name = tildep;
5 kx }
5 kx
5 kx #ifndef NO_ARCHIVES
5 kx /* If NAME is an archive member reference replace it with the archive
5 kx file name, and save the member name in MEMNAME. We will glob on the
5 kx archive name and then reattach MEMNAME later. */
5 kx if (NONE_SET (flags, PARSEFS_NOAR) && ar_name (name))
5 kx {
5 kx ar_parse_name (name, &arname, &memname);
5 kx name = arname;
5 kx }
5 kx #endif /* !NO_ARCHIVES */
5 kx
5 kx /* glob() is expensive: don't call it unless we need to. */
5 kx if (NONE_SET (flags, PARSEFS_EXISTS) && strpbrk (name, "?*[") == NULL)
5 kx {
5 kx globme = 0;
5 kx tot = 1;
5 kx nlist = &name;
5 kx }
5 kx else
5 kx switch (glob (name, GLOB_ALTDIRFUNC, NULL, &gl))
5 kx {
5 kx case GLOB_NOSPACE:
5 kx out_of_memory ();
5 kx
5 kx case 0:
5 kx /* Success. */
5 kx tot = gl.gl_pathc;
5 kx nlist = (const char **)gl.gl_pathv;
5 kx break;
5 kx
5 kx case GLOB_NOMATCH:
5 kx /* If we want only existing items, skip this one. */
5 kx if (ANY_SET (flags, PARSEFS_EXISTS))
5 kx {
5 kx tot = 0;
5 kx break;
5 kx }
5 kx /* FALLTHROUGH */
5 kx
5 kx default:
5 kx /* By default keep this name. */
5 kx tot = 1;
5 kx nlist = &name;
5 kx break;
5 kx }
5 kx
5 kx /* For each matched element, add it to the list. */
5 kx for (i = 0; i < tot; ++i)
5 kx #ifndef NO_ARCHIVES
5 kx if (memname != 0)
5 kx {
5 kx /* Try to glob on MEMNAME within the archive. */
5 kx struct nameseq *found = ar_glob (nlist[i], memname, size);
5 kx if (! found)
5 kx /* No matches. Use MEMNAME as-is. */
5 kx NEWELT (concat (5, prefix, nlist[i], "(", memname, ")"));
5 kx else
5 kx {
5 kx /* We got a chain of items. Attach them. */
5 kx if (*newp)
5 kx (*newp)->next = found;
5 kx else
5 kx *newp = found;
5 kx
5 kx /* Find and set the new end. Massage names if necessary. */
5 kx while (1)
5 kx {
5 kx if (! cachep)
5 kx found->name = xstrdup (concat (2, prefix, name));
5 kx else if (prefix)
5 kx found->name = strcache_add (concat (2, prefix, name));
5 kx
5 kx if (found->next == 0)
5 kx break;
5 kx
5 kx found = found->next;
5 kx }
5 kx newp = &found->next;
5 kx }
5 kx }
5 kx else
5 kx #endif /* !NO_ARCHIVES */
5 kx NEWELT (concat (2, prefix, nlist[i]));
5 kx
5 kx if (globme)
5 kx globfree (&gl);
5 kx
5 kx #ifndef NO_ARCHIVES
5 kx free (arname);
5 kx #endif
5 kx
5 kx free (tildep);
5 kx }
5 kx
5 kx *stringp = p;
5 kx return new;
5 kx }