Ignore:
Timestamp:
Mar 12, 2018, 8:32:29 PM (7 years ago)
Author:
bird
Message:

Imported make 4.2.1 (2e55f5e4abdc0e38c1d64be703b446695e70b3b6) from https://git.savannah.gnu.org/git/make.git.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vendor/gnumake/current/main.c

    r2596 r3138  
    11/* Argument parsing and main program of GNU Make.
    2 Copyright (C) 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997,
    3 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
    4 2010 Free Software Foundation, Inc.
     2Copyright (C) 1988-2016 Free Software Foundation, Inc.
    53This file is part of GNU Make.
    64
     
    1715this program.  If not, see <http://www.gnu.org/licenses/>.  */
    1816
    19 #include "make.h"
     17#include "makeint.h"
     18#include "os.h"
     19#include "filedef.h"
    2020#include "dep.h"
    21 #include "filedef.h"
    2221#include "variable.h"
    2322#include "job.h"
     
    3332#endif
    3433#ifdef WINDOWS32
    35 #include <windows.h>
    36 #include <io.h>
    37 #include "pathstuff.h"
     34# include <windows.h>
     35# include <io.h>
     36# include "pathstuff.h"
     37# include "sub_proc.h"
     38# include "w32err.h"
    3839#endif
    3940#ifdef __EMX__
     
    4849int __stack = 20000; /* Make sure we have 20K of stack space */
    4950#endif
    50 
    51 void init_dir (void);
    52 void remote_setup (void);
    53 void remote_cleanup (void);
    54 RETSIGTYPE fatal_error_signal (int sig);
    55 
    56 void print_variable_data_base (void);
    57 void print_dir_data_base (void);
    58 void print_rule_data_base (void);
    59 void print_vpath_data_base (void);
    60 
    61 void verify_file_data_base (void);
     51#ifdef VMS
     52int vms_use_mcr_command = 0;
     53int vms_always_use_cmd_file = 0;
     54int vms_gnv_shell = 0;
     55int vms_legacy_behavior = 0;
     56int vms_comma_separator = 0;
     57int vms_unix_simulation = 0;
     58int vms_report_unix_paths = 0;
     59
     60/* Evaluates if a VMS environment option is set, only look at first character */
     61static int
     62get_vms_env_flag (const char *name, int default_value)
     63{
     64char * value;
     65char x;
     66
     67  value = getenv (name);
     68  if (value == NULL)
     69    return default_value;
     70
     71  x = toupper (value[0]);
     72  switch (x)
     73    {
     74    case '1':
     75    case 'T':
     76    case 'E':
     77      return 1;
     78      break;
     79    case '0':
     80    case 'F':
     81    case 'D':
     82      return 0;
     83    }
     84}
     85#endif
    6286
    6387#if defined HAVE_WAITPID || defined HAVE_WAIT3
     
    6589#endif
    6690
    67 #ifndef HAVE_UNISTD_H
     91#ifndef HAVE_UNISTD_H
    6892int chdir ();
    6993#endif
    70 #ifndef STDC_HEADERS
    71 # ifndef sun                    /* Sun has an incorrect decl in a header.  */
     94#ifndef STDC_HEADERS
     95# ifndef sun                    /* Sun has an incorrect decl in a header.  */
    7296void exit (int) __attribute__ ((noreturn));
    7397# endif
     
    78102static void print_data_base (void);
    79103static void print_version (void);
    80 static void decode_switches (int argc, char **argv, int env);
    81 static void decode_env_switches (char *envar, unsigned int len);
    82 static const char *define_makeflags (int all, int makefile);
     104static void decode_switches (int argc, const char **argv, int env);
     105static void decode_env_switches (const char *envar, unsigned int len);
     106static struct variable *define_makeflags (int all, int makefile);
    83107static char *quote_for_env (char *out, const char *in);
    84108static void initialize_global_hash_tables (void);
     
    90114struct command_switch
    91115  {
    92     int c;                      /* The switch character.  */
    93 
    94     enum                        /* Type of the value.  */
     116    int c;                      /* The switch character.  */
     117
     118    enum                        /* Type of the value.  */
    95119      {
    96         flag,                   /* Turn int flag on.  */
    97         flag_off,               /* Turn int flag off.  */
    98         string,                 /* One string per switch.  */
    99         filename,               /* A string containing a file name.  */
    100         positive_int,           /* A positive integer.  */
    101         floating,               /* A floating-point number (double).  */
    102         ignore                  /* Ignored.  */
     120        flag,                   /* Turn int flag on.  */
     121        flag_off,               /* Turn int flag off.  */
     122        string,                 /* One string per invocation.  */
     123        strlist,                /* One string per switch.  */
     124        filename,               /* A string containing a file name.  */
     125        positive_int,           /* A positive integer.  */
     126        floating,               /* A floating-point number (double).  */
     127        ignore                  /* Ignored.  */
    103128      } type;
    104129
    105     void *value_ptr;    /* Pointer to the value-holding variable.  */
    106 
    107     unsigned int env:1;         /* Can come from MAKEFLAGS.  */
    108     unsigned int toenv:1;       /* Should be put in MAKEFLAGS.  */
    109     unsigned int no_makefile:1; /* Don't propagate when remaking makefiles.  */
    110 
    111     const void *noarg_value;    /* Pointer to value used if no arg given.  */
    112     const void *default_value;  /* Pointer to default value.  */
    113 
    114     char *long_name;            /* Long option name.  */
     130    void *value_ptr;    /* Pointer to the value-holding variable.  */
     131
     132    unsigned int env:1;         /* Can come from MAKEFLAGS.  */
     133    unsigned int toenv:1;       /* Should be put in MAKEFLAGS.  */
     134    unsigned int no_makefile:1; /* Don't propagate when remaking makefiles.  */
     135
     136    const void *noarg_value;    /* Pointer to value used if no arg given.  */
     137    const void *default_value;  /* Pointer to default value.  */
     138
     139    const char *long_name;      /* Long option name.  */
    115140  };
    116141
     
    120145
    121146/* The structure used to hold the list of strings given
    122    in command switches of a type that takes string arguments.  */
     147   in command switches of a type that takes strlist arguments.  */
    123148
    124149struct stringlist
    125150  {
    126     const char **list;  /* Nil-terminated list of strings.  */
    127     unsigned int idx;   /* Index into above.  */
    128     unsigned int max;   /* Number of pointers allocated.  */
     151    const char **list;  /* Nil-terminated list of strings.  */
     152    unsigned int idx;   /* Index into above.  */
     153    unsigned int max;   /* Number of pointers allocated.  */
    129154  };
    130155
    131156
    132157/* The recognized command switches.  */
     158
     159/* Nonzero means do extra verification (that may slow things down).  */
     160
     161int verify_flag;
    133162
    134163/* Nonzero means do not print commands to be executed (-s).  */
     
    148177/* Print debugging info (--debug).  */
    149178
    150 static struct stringlist *db_flags;
     179static struct stringlist *db_flags = 0;
    151180static int debug_flag = 0;
    152181
    153182int db_level = 0;
    154183
    155 /* Output level (--verbosity).  */
    156 
    157 static struct stringlist *verbosity_flags;
     184/* Synchronize output (--output-sync).  */
     185
     186char *output_sync_option = 0;
    158187
    159188#ifdef WINDOWS32
     
    220249
    221250
    222 /* Number of job slots (commands that can be run at once).  */
    223 
    224 unsigned int job_slots = 1;
    225 unsigned int default_job_slots = 1;
     251/* Number of job slots for parallelism.  */
     252
     253unsigned int job_slots;
     254
     255#define INVALID_JOB_SLOTS (-1)
    226256static unsigned int master_job_slots = 0;
     257static int arg_job_slots = INVALID_JOB_SLOTS;
     258
     259static const int default_job_slots = INVALID_JOB_SLOTS;
    227260
    228261/* Value of job_slots that means no limit.  */
    229262
    230 static unsigned int inf_jobs = 0;
    231 
    232 /* File descriptors for the jobs pipe.  */
    233 
    234 static struct stringlist *jobserver_fds = 0;
    235 
    236 int job_fds[2] = { -1, -1 };
    237 int job_rfd = -1;
     263static const int inf_jobs = 0;
     264
     265/* Authorization for the jobserver.  */
     266
     267static char *jobserver_auth = NULL;
     268
     269/* Handle for the mutex used on Windows to synchronize output of our
     270   children under -O.  */
     271
     272char *sync_mutex = NULL;
    238273
    239274/* Maximum load average at which multiple jobs will be run.
     
    346381                              Consider FILE to be very old and don't remake it.\n"),
    347382    N_("\
     383  -O[TYPE], --output-sync[=TYPE]\n\
     384                              Synchronize output of parallel jobs by TYPE.\n"),
     385    N_("\
    348386  -p, --print-data-base       Print make's internal database.\n"),
    349387    N_("\
     
    361399  -t, --touch                 Touch targets instead of remaking them.\n"),
    362400    N_("\
     401  --trace                     Print tracing information.\n"),
     402    N_("\
    363403  -v, --version               Print the version number of make and exit.\n"),
    364404    N_("\
     
    374414  };
    375415
    376 /* The table of command switches.  */
     416/* The table of command switches.
     417   Order matters here: this is the order MAKEFLAGS will be constructed.
     418   So be sure all simple flags (single char, no argument) come first.  */
    377419
    378420static const struct command_switch switches[] =
     
    380422    { 'b', ignore, 0, 0, 0, 0, 0, 0, 0 },
    381423    { 'B', flag, &always_make_set, 1, 1, 0, 0, 0, "always-make" },
    382     { 'C', filename, &directories, 0, 0, 0, 0, 0, "directory" },
    383424    { 'd', flag, &debug_flag, 1, 1, 0, 0, 0, 0 },
    384     { CHAR_MAX+1, string, &db_flags, 1, 1, 0, "basic", 0, "debug" },
    385425#ifdef WINDOWS32
    386426    { 'D', flag, &suspend_flag, 1, 1, 0, 0, 0, "suspend-for-debug" },
    387427#endif
    388428    { 'e', flag, &env_overrides, 1, 1, 0, 0, 0, "environment-overrides", },
    389     { 'f', filename, &makefiles, 0, 0, 0, 0, 0, "file" },
    390429    { 'h', flag, &print_usage_flag, 0, 0, 0, 0, 0, "help" },
    391430    { 'i', flag, &ignore_errors_flag, 1, 1, 0, 0, 0, "ignore-errors" },
    392     { 'I', filename, &include_directories, 1, 1, 0, 0, 0,
    393       "include-dir" },
    394     { 'j', positive_int, &job_slots, 1, 1, 0, &inf_jobs, &default_job_slots,
    395       "jobs" },
    396     { CHAR_MAX+2, string, &jobserver_fds, 1, 1, 0, 0, 0, "jobserver-fds" },
    397431    { 'k', flag, &keep_going_flag, 1, 1, 0, 0, &default_keep_going_flag,
    398432      "keep-going" },
    399 #ifndef NO_FLOAT
    400     { 'l', floating, &max_load_average, 1, 1, 0, &default_load_average,
    401       &default_load_average, "load-average" },
    402 #else
    403     { 'l', positive_int, &max_load_average, 1, 1, 0, &default_load_average,
    404       &default_load_average, "load-average" },
    405 #endif
    406433    { 'L', flag, &check_symlink_flag, 1, 1, 0, 0, 0, "check-symlink-times" },
    407434    { 'm', ignore, 0, 0, 0, 0, 0, 0, 0 },
    408435    { 'n', flag, &just_print_flag, 1, 1, 1, 0, 0, "just-print" },
    409     { 'o', filename, &old_files, 0, 0, 0, 0, 0, "old-file" },
    410436    { 'p', flag, &print_data_base_flag, 1, 1, 0, 0, 0, "print-data-base" },
    411437    { 'q', flag, &question_flag, 1, 1, 1, 0, 0, "question" },
     
    418444    { 't', flag, &touch_flag, 1, 1, 1, 0, 0, "touch" },
    419445    { 'v', flag, &print_version_flag, 1, 1, 0, 0, 0, "version" },
    420     { CHAR_MAX+3, string, &verbosity_flags, 1, 1, 0, 0, 0,
    421       "verbosity" },
    422446    { 'w', flag, &print_directory_flag, 1, 1, 0, 0, 0, "print-directory" },
     447
     448    /* These options take arguments.  */
     449    { 'C', filename, &directories, 0, 0, 0, 0, 0, "directory" },
     450    { 'f', filename, &makefiles, 0, 0, 0, 0, 0, "file" },
     451    { 'I', filename, &include_directories, 1, 1, 0, 0, 0,
     452      "include-dir" },
     453    { 'j', positive_int, &arg_job_slots, 1, 1, 0, &inf_jobs, &default_job_slots,
     454      "jobs" },
     455#ifndef NO_FLOAT
     456    { 'l', floating, &max_load_average, 1, 1, 0, &default_load_average,
     457      &default_load_average, "load-average" },
     458#else
     459    { 'l', positive_int, &max_load_average, 1, 1, 0, &default_load_average,
     460      &default_load_average, "load-average" },
     461#endif
     462    { 'o', filename, &old_files, 0, 0, 0, 0, 0, "old-file" },
     463    { 'O', string, &output_sync_option, 1, 1, 0, "target", 0, "output-sync" },
     464    { 'W', filename, &new_files, 0, 0, 0, 0, 0, "what-if" },
     465
     466    /* These are long-style options.  */
     467    { CHAR_MAX+1, strlist, &db_flags, 1, 1, 0, "basic", 0, "debug" },
     468    { CHAR_MAX+2, string, &jobserver_auth, 1, 1, 0, 0, 0, "jobserver-auth" },
     469    { CHAR_MAX+3, flag, &trace_flag, 1, 1, 0, 0, 0, "trace" },
    423470    { CHAR_MAX+4, flag, &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
    424471      "no-print-directory" },
    425     { 'W', filename, &new_files, 0, 0, 0, 0, 0, "what-if" },
    426472    { CHAR_MAX+5, flag, &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
    427473      "warn-undefined-variables" },
    428     { CHAR_MAX+6, string, &eval_strings, 1, 0, 0, 0, 0, "eval" },
     474    { CHAR_MAX+6, strlist, &eval_strings, 1, 0, 0, 0, 0, "eval" },
     475    { CHAR_MAX+7, string, &sync_mutex, 1, 1, 0, 0, 0, "sync-mutex" },
    429476    { 0, 0, 0, 0, 0, 0, 0, 0, 0 }
    430477  };
     
    434481static struct option long_option_aliases[] =
    435482  {
    436     { "quiet",          no_argument,            0, 's' },
    437     { "stop",           no_argument,            0, 'S' },
    438     { "new-file",       required_argument,      0, 'W' },
    439     { "assume-new",     required_argument,      0, 'W' },
    440     { "assume-old",     required_argument,      0, 'o' },
    441     { "max-load",       optional_argument,      0, 'l' },
    442     { "dry-run",        no_argument,            0, 'n' },
    443     { "recon",          no_argument,            0, 'n' },
    444     { "makefile",       required_argument,      0, 'f' },
     483    { "quiet",          no_argument,            0, 's' },
     484    { "stop",           no_argument,            0, 'S' },
     485    { "new-file",       required_argument,      0, 'W' },
     486    { "assume-new",     required_argument,      0, 'W' },
     487    { "assume-old",     required_argument,      0, 'o' },
     488    { "max-load",       optional_argument,      0, 'l' },
     489    { "dry-run",        no_argument,            0, 'n' },
     490    { "recon",          no_argument,            0, 'n' },
     491    { "makefile",       required_argument,      0, 'f' },
    445492  };
    446493
    447494/* List of goal targets.  */
    448495
    449 static struct dep *goals, *lastgoal;
     496static struct goaldep *goals, *lastgoal;
    450497
    451498/* List of variables which were defined on the command line
     
    462509/* The name we were invoked with.  */
    463510
     511#ifdef WINDOWS32
     512/* On MS-Windows, we chop off the .exe suffix in 'main', so this
     513   cannot be 'const'.  */
    464514char *program;
     515#else
     516const char *program;
     517#endif
    465518
    466519/* Our current directory before processing any -C options.  */
     
    490543struct file *default_file;
    491544
    492 /* Nonzero if we have seen the magic `.POSIX' target.
     545/* Nonzero if we have seen the magic '.POSIX' target.
    493546   This turns on pedantic compliance with POSIX.2.  */
    494547
     
    506559int one_shell;
    507560
    508 /* Nonzero if we have seen the `.NOTPARALLEL' target.
     561/* One of OUTPUT_SYNC_* if the "--output-sync" option was given.  This
     562   attempts to synchronize the output of parallel jobs such that the results
     563   of each job stay together.  */
     564
     565int output_sync = OUTPUT_SYNC_NONE;
     566
     567/* Nonzero if the "--trace" option was given.  */
     568
     569int trace_flag = 0;
     570
     571/* Nonzero if we have seen the '.NOTPARALLEL' target.
    509572   This turns off parallel builds for this invocation of make.  */
    510573
     
    517580int clock_skew_detected;
    518581
     582/* Map of possible stop characters for searching strings.  */
     583#ifndef UCHAR_MAX
     584# define UCHAR_MAX 255
     585#endif
     586unsigned short stopchar_map[UCHAR_MAX + 1] = {0};
     587
     588/* If output-sync is enabled we'll collect all the output generated due to
     589   options, while reading makefiles, etc.  */
     590
     591struct output make_sync;
     592
     593
    519594
    520595/* Mask of signals that are being caught with fatal_error_signal.  */
    521596
    522 #ifdef  POSIX
     597#ifdef POSIX
    523598sigset_t fatal_signal_set;
    524599#else
    525 # ifdef HAVE_SIGSETMASK
     600# ifdef HAVE_SIGSETMASK
    526601int fatal_signal_mask;
    527602# endif
     
    559634}
    560635
     636/* This character map locate stop chars when parsing GNU makefiles.
     637   Each element is true if we should stop parsing on that character.  */
     638
     639static void
     640initialize_stopchar_map (void)
     641{
     642  int i;
     643
     644  stopchar_map[(int)'\0'] = MAP_NUL;
     645  stopchar_map[(int)'#'] = MAP_COMMENT;
     646  stopchar_map[(int)';'] = MAP_SEMI;
     647  stopchar_map[(int)'='] = MAP_EQUALS;
     648  stopchar_map[(int)':'] = MAP_COLON;
     649  stopchar_map[(int)'%'] = MAP_PERCENT;
     650  stopchar_map[(int)'|'] = MAP_PIPE;
     651  stopchar_map[(int)'.'] = MAP_DOT | MAP_USERFUNC;
     652  stopchar_map[(int)','] = MAP_COMMA;
     653  stopchar_map[(int)'$'] = MAP_VARIABLE;
     654
     655  stopchar_map[(int)'-'] = MAP_USERFUNC;
     656  stopchar_map[(int)'_'] = MAP_USERFUNC;
     657
     658  stopchar_map[(int)' '] = MAP_BLANK;
     659  stopchar_map[(int)'\t'] = MAP_BLANK;
     660
     661  stopchar_map[(int)'/'] = MAP_DIRSEP;
     662#if defined(VMS)
     663  stopchar_map[(int)':'] |= MAP_DIRSEP;
     664  stopchar_map[(int)']'] |= MAP_DIRSEP;
     665  stopchar_map[(int)'>'] |= MAP_DIRSEP;
     666#elif defined(HAVE_DOS_PATHS)
     667  stopchar_map[(int)'\\'] |= MAP_DIRSEP;
     668#endif
     669
     670  for (i = 1; i <= UCHAR_MAX; ++i)
     671    {
     672      if (isspace (i) && NONE_SET (stopchar_map[i], MAP_BLANK))
     673        /* Don't mark blank characters as newline characters.  */
     674        stopchar_map[i] |= MAP_NEWLINE;
     675      else if (isalnum (i))
     676        stopchar_map[i] |= MAP_USERFUNC;
     677    }
     678}
     679
    561680static const char *
    562 expand_command_line_file (char *name)
     681expand_command_line_file (const char *name)
    563682{
    564683  const char *cp;
     
    566685
    567686  if (name[0] == '\0')
    568     fatal (NILF, _("empty string invalid as file name"));
     687    O (fatal, NILF, _("empty string invalid as file name"));
    569688
    570689  if (name[0] == '~')
    571690    {
    572691      expanded = tilde_expand (name);
    573       if (expanded != 0)
    574         name = expanded;
     692      if (expanded && expanded[0] != '\0')
     693        name = expanded;
    575694    }
    576695
     
    578697     for names read from makefiles.  It is here for names passed
    579698     on the command line.  */
    580   while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
     699  while (name[0] == '.' && name[1] == '/')
    581700    {
    582701      name += 2;
    583       while (*name == '/')
    584         /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
    585         ++name;
    586     }
    587 
    588   if (*name == '\0')
    589     {
    590       /* It was all slashes!  Move back to the dot and truncate
    591          it after the first slash, so it becomes just "./".  */
    592       do
    593         --name;
    594       while (name[0] != '.');
    595       name[2] = '\0';
     702      while (name[0] == '/')
     703        /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
     704        ++name;
     705    }
     706
     707  if (name[0] == '\0')
     708    {
     709      /* Nothing else but one or more "./", maybe plus slashes!  */
     710      name = "./";
    596711    }
    597712
    598713  cp = strcache_add (name);
    599714
    600   if (expanded)
    601     free (expanded);
     715  free (expanded);
    602716
    603717  return cp;
     
    622736    db_level = DB_ALL;
    623737
    624   if (!db_flags)
    625     return;
    626 
    627   for (pp=db_flags->list; *pp; ++pp)
    628     {
    629       const char *p = *pp;
    630 
    631       while (1)
    632         {
    633           switch (tolower (p[0]))
    634             {
    635             case 'a':
    636               db_level |= DB_ALL;
     738  if (db_flags)
     739    for (pp=db_flags->list; *pp; ++pp)
     740      {
     741        const char *p = *pp;
     742
     743        while (1)
     744          {
     745            switch (tolower (p[0]))
     746              {
     747              case 'a':
     748                db_level |= DB_ALL;
     749                break;
     750              case 'b':
     751                db_level |= DB_BASIC;
     752                break;
     753              case 'i':
     754                db_level |= DB_BASIC | DB_IMPLICIT;
     755                break;
     756              case 'j':
     757                db_level |= DB_JOBS;
     758                break;
     759              case 'm':
     760                db_level |= DB_BASIC | DB_MAKEFILES;
     761                break;
     762              case 'n':
     763                db_level = 0;
     764                break;
     765              case 'v':
     766                db_level |= DB_BASIC | DB_VERBOSE;
     767                break;
     768              default:
     769                OS (fatal, NILF,
     770                    _("unknown debug level specification '%s'"), p);
     771              }
     772
     773            while (*(++p) != '\0')
     774              if (*p == ',' || *p == ' ')
     775                {
     776                  ++p;
     777                  break;
     778                }
     779
     780            if (*p == '\0')
    637781              break;
    638             case 'b':
    639               db_level |= DB_BASIC;
    640               break;
    641             case 'i':
    642               db_level |= DB_BASIC | DB_IMPLICIT;
    643               break;
    644             case 'j':
    645               db_level |= DB_JOBS;
    646               break;
    647             case 'm':
    648               db_level |= DB_BASIC | DB_MAKEFILES;
    649               break;
    650             case 'v':
    651               db_level |= DB_BASIC | DB_VERBOSE;
    652               break;
    653             default:
    654               fatal (NILF, _("unknown debug level specification `%s'"), p);
    655             }
    656 
    657           while (*(++p) != '\0')
    658             if (*p == ',' || *p == ' ')
    659               break;
    660 
    661           if (*p == '\0')
    662             break;
    663 
    664           ++p;
    665         }
    666     }
     782          }
     783      }
     784
     785  if (db_level)
     786    verify_flag = 1;
     787
     788  if (! db_level)
     789    debug_flag = 0;
    667790}
    668791
     792static void
     793decode_output_sync_flags (void)
     794{
     795#ifdef NO_OUTPUT_SYNC
     796  output_sync = OUTPUT_SYNC_NONE;
     797#else
     798  if (output_sync_option)
     799    {
     800      if (streq (output_sync_option, "none"))
     801        output_sync = OUTPUT_SYNC_NONE;
     802      else if (streq (output_sync_option, "line"))
     803        output_sync = OUTPUT_SYNC_LINE;
     804      else if (streq (output_sync_option, "target"))
     805        output_sync = OUTPUT_SYNC_TARGET;
     806      else if (streq (output_sync_option, "recurse"))
     807        output_sync = OUTPUT_SYNC_RECURSE;
     808      else
     809        OS (fatal, NILF,
     810            _("unknown output-sync type '%s'"), output_sync_option);
     811    }
     812
     813  if (sync_mutex)
     814    RECORD_SYNC_MUTEX (sync_mutex);
     815#endif
     816}
     817
    669818#ifdef WINDOWS32
     819
     820#ifndef NO_OUTPUT_SYNC
     821
     822/* This is called from start_job_command when it detects that
     823   output_sync option is in effect.  The handle to the synchronization
     824   mutex is passed, as a string, to sub-makes via the --sync-mutex
     825   command-line argument.  */
     826void
     827prepare_mutex_handle_string (sync_handle_t handle)
     828{
     829  if (!sync_mutex)
     830    {
     831      /* Prepare the mutex handle string for our children.  */
     832      /* 2 hex digits per byte + 2 characters for "0x" + null.  */
     833      sync_mutex = xmalloc ((2 * sizeof (sync_handle_t)) + 2 + 1);
     834      sprintf (sync_mutex, "0x%Ix", handle);
     835      define_makeflags (1, 0);
     836    }
     837}
     838
     839#endif  /* NO_OUTPUT_SYNC */
     840
    670841/*
    671842 * HANDLE runtime exceptions by avoiding a requestor on the GUI. Capture
     
    678849 */
    679850LONG WINAPI
    680 handle_runtime_exceptions( struct _EXCEPTION_POINTERS *exinfo )
     851handle_runtime_exceptions (struct _EXCEPTION_POINTERS *exinfo)
    681852{
    682853  PEXCEPTION_RECORD exrec = exinfo->ExceptionRecord;
    683   LPSTR cmdline = GetCommandLine();
    684   LPSTR prg = strtok(cmdline, " ");
     854  LPSTR cmdline = GetCommandLine ();
     855  LPSTR prg = strtok (cmdline, " ");
    685856  CHAR errmsg[1024];
    686857#ifdef USE_EVENT_LOG
     
    691862  if (! ISDB (DB_VERBOSE))
    692863    {
    693       sprintf(errmsg,
    694               _("%s: Interrupt/Exception caught (code = 0x%lx, addr = 0x%p)\n"),
    695               prg, exrec->ExceptionCode, exrec->ExceptionAddress);
    696       fprintf(stderr, errmsg);
    697       exit(255);
    698     }
    699 
    700   sprintf(errmsg,
    701           _("\nUnhandled exception filter called from program %s\nExceptionCode = %lx\nExceptionFlags = %lx\nExceptionAddress = 0x%p\n"),
    702           prg, exrec->ExceptionCode, exrec->ExceptionFlags,
    703           exrec->ExceptionAddress);
     864      sprintf (errmsg,
     865               _("%s: Interrupt/Exception caught (code = 0x%lx, addr = 0x%p)\n"),
     866               prg, exrec->ExceptionCode, exrec->ExceptionAddress);
     867      fprintf (stderr, errmsg);
     868      exit (255);
     869    }
     870
     871  sprintf (errmsg,
     872           _("\nUnhandled exception filter called from program %s\nExceptionCode = %lx\nExceptionFlags = %lx\nExceptionAddress = 0x%p\n"),
     873           prg, exrec->ExceptionCode, exrec->ExceptionFlags,
     874           exrec->ExceptionAddress);
    704875
    705876  if (exrec->ExceptionCode == EXCEPTION_ACCESS_VIOLATION
    706877      && exrec->NumberParameters >= 2)
    707     sprintf(&errmsg[strlen(errmsg)],
    708             (exrec->ExceptionInformation[0]
    709              ? _("Access violation: write operation at address 0x%p\n")
    710              : _("Access violation: read operation at address 0x%p\n")),
    711             (PVOID)exrec->ExceptionInformation[1]);
     878    sprintf (&errmsg[strlen(errmsg)],
     879             (exrec->ExceptionInformation[0]
     880              ? _("Access violation: write operation at address 0x%p\n")
     881              : _("Access violation: read operation at address 0x%p\n")),
     882             (PVOID)exrec->ExceptionInformation[1]);
    712883
    713884  /* turn this on if we want to put stuff in the event log too */
    714885#ifdef USE_EVENT_LOG
    715   hEventSource = RegisterEventSource(NULL, "GNU Make");
     886  hEventSource = RegisterEventSource (NULL, "GNU Make");
    716887  lpszStrings[0] = errmsg;
    717888
    718889  if (hEventSource != NULL)
    719890    {
    720       ReportEvent(hEventSource,         /* handle of event source */
    721                   EVENTLOG_ERROR_TYPE,  /* event type */
    722                   0,                    /* event category */
    723                   0,                    /* event ID */
    724                   NULL,                 /* current user's SID */
    725                   1,                    /* strings in lpszStrings */
    726                   0,                    /* no bytes of raw data */
    727                   lpszStrings,          /* array of error strings */
    728                   NULL);                /* no raw data */
    729 
    730       (VOID) DeregisterEventSource(hEventSource);
     891      ReportEvent (hEventSource,         /* handle of event source */
     892                   EVENTLOG_ERROR_TYPE,  /* event type */
     893                   0,                    /* event category */
     894                   0,                    /* event ID */
     895                   NULL,                 /* current user's SID */
     896                   1,                    /* strings in lpszStrings */
     897                   0,                    /* no bytes of raw data */
     898                   lpszStrings,          /* array of error strings */
     899                   NULL);                /* no raw data */
     900
     901      (VOID) DeregisterEventSource (hEventSource);
    731902    }
    732903#endif
    733904
    734905  /* Write the error to stderr too */
    735   fprintf(stderr, errmsg);
     906  fprintf (stderr, errmsg);
    736907
    737908#ifdef DEBUG
    738909  return EXCEPTION_CONTINUE_SEARCH;
    739910#else
    740   exit(255);
     911  exit (255);
    741912  return (255); /* not reached */
    742913#endif
     
    746917 * On WIN32 systems we don't have the luxury of a /bin directory that
    747918 * is mapped globally to every drive mounted to the system. Since make could
    748  * be invoked from any drive, and we don't want to propogate /bin/sh
     919 * be invoked from any drive, and we don't want to propagate /bin/sh
    749920 * to every single drive. Allow ourselves a chance to search for
    750921 * a value for default shell here (if the default path does not exist).
     
    756927  int sh_found = 0;
    757928  char *atoken = 0;
    758   char *search_token;
    759   char *tokend;
     929  const char *search_token;
     930  const char *tokend;
    760931  PATH_VAR(sh_path);
    761   extern char *default_shell;
     932  extern const char *default_shell;
    762933
    763934  if (!token)
    764935    search_token = default_shell;
    765936  else
    766     atoken = search_token = xstrdup (token);
     937    search_token = atoken = xstrdup (token);
    767938
    768939  /* If the user explicitly requests the DOS cmd shell, obey that request.
     
    778949           || (tokend - 4 > search_token
    779950               && (tokend[-5] == '/' || tokend[-5] == '\\')))
    780           && !strcasecmp (tokend - 4, "cmd.exe"))) {
    781     batch_mode_shell = 1;
    782     unixy_shell = 0;
    783     sprintf (sh_path, "%s", search_token);
    784     default_shell = xstrdup (w32ify (sh_path, 0));
    785     DB (DB_VERBOSE, (_("find_and_set_shell() setting default_shell = %s\n"),
    786                      default_shell));
    787     sh_found = 1;
    788   } else if (!no_default_sh_exe &&
    789              (token == NULL || !strcmp (search_token, default_shell))) {
    790     /* no new information, path already set or known */
    791     sh_found = 1;
    792   } else if (file_exists_p (search_token)) {
    793     /* search token path was found */
    794     sprintf (sh_path, "%s", search_token);
    795     default_shell = xstrdup (w32ify (sh_path, 0));
    796     DB (DB_VERBOSE, (_("find_and_set_shell() setting default_shell = %s\n"),
    797                      default_shell));
    798     sh_found = 1;
    799   } else {
    800     char *p;
    801     struct variable *v = lookup_variable (STRING_SIZE_TUPLE ("PATH"));
    802 
    803     /* Search Path for shell */
    804     if (v && v->value) {
    805       char *ep;
    806 
    807       p  = v->value;
    808       ep = strchr (p, PATH_SEPARATOR_CHAR);
    809 
    810       while (ep && *ep) {
    811         *ep = '\0';
    812 
    813         if (dir_file_exists_p (p, search_token)) {
    814           sprintf (sh_path, "%s/%s", p, search_token);
    815           default_shell = xstrdup (w32ify (sh_path, 0));
    816           sh_found = 1;
    817           *ep = PATH_SEPARATOR_CHAR;
    818 
    819           /* terminate loop */
    820           p += strlen (p);
    821         } else {
    822           *ep = PATH_SEPARATOR_CHAR;
    823            p = ++ep;
     951          && !strcasecmp (tokend - 4, "cmd.exe")))
     952    {
     953      batch_mode_shell = 1;
     954      unixy_shell = 0;
     955      sprintf (sh_path, "%s", search_token);
     956      default_shell = xstrdup (w32ify (sh_path, 0));
     957      DB (DB_VERBOSE, (_("find_and_set_shell() setting default_shell = %s\n"),
     958                       default_shell));
     959      sh_found = 1;
     960    }
     961  else if (!no_default_sh_exe
     962           && (token == NULL || !strcmp (search_token, default_shell)))
     963    {
     964      /* no new information, path already set or known */
     965      sh_found = 1;
     966    }
     967  else if (_access (search_token, 0) == 0)
     968    {
     969      /* search token path was found */
     970      sprintf (sh_path, "%s", search_token);
     971      default_shell = xstrdup (w32ify (sh_path, 0));
     972      DB (DB_VERBOSE, (_("find_and_set_shell() setting default_shell = %s\n"),
     973                       default_shell));
     974      sh_found = 1;
     975    }
     976  else
     977    {
     978      char *p;
     979      struct variable *v = lookup_variable (STRING_SIZE_TUPLE ("PATH"));
     980
     981      /* Search Path for shell */
     982      if (v && v->value)
     983        {
     984          char *ep;
     985
     986          p  = v->value;
     987          ep = strchr (p, PATH_SEPARATOR_CHAR);
     988
     989          while (ep && *ep)
     990            {
     991              *ep = '\0';
     992
     993              sprintf (sh_path, "%s/%s", p, search_token);
     994              if (_access (sh_path, 0) == 0)
     995                {
     996                  default_shell = xstrdup (w32ify (sh_path, 0));
     997                  sh_found = 1;
     998                  *ep = PATH_SEPARATOR_CHAR;
     999
     1000                  /* terminate loop */
     1001                  p += strlen (p);
     1002                }
     1003              else
     1004                {
     1005                  *ep = PATH_SEPARATOR_CHAR;
     1006                  p = ++ep;
     1007                }
     1008
     1009              ep = strchr (p, PATH_SEPARATOR_CHAR);
     1010            }
     1011
     1012          /* be sure to check last element of Path */
     1013          if (p && *p)
     1014            {
     1015              sprintf (sh_path, "%s/%s", p, search_token);
     1016              if (_access (sh_path, 0) == 0)
     1017                {
     1018                  default_shell = xstrdup (w32ify (sh_path, 0));
     1019                  sh_found = 1;
     1020                }
     1021            }
     1022
     1023          if (sh_found)
     1024            DB (DB_VERBOSE,
     1025                (_("find_and_set_shell() path search set default_shell = %s\n"),
     1026                 default_shell));
    8241027        }
    825 
    826         ep = strchr (p, PATH_SEPARATOR_CHAR);
    827       }
    828 
    829       /* be sure to check last element of Path */
    830       if (p && *p && dir_file_exists_p (p, search_token)) {
    831           sprintf (sh_path, "%s/%s", p, search_token);
    832           default_shell = xstrdup (w32ify (sh_path, 0));
    833           sh_found = 1;
    834       }
    835 
    836       if (sh_found)
    837         DB (DB_VERBOSE,
    838             (_("find_and_set_shell() path search set default_shell = %s\n"),
    839              default_shell));
    840     }
    841   }
     1028    }
    8421029
    8431030  /* naive test */
    844   if (!unixy_shell && sh_found &&
    845       (strstr (default_shell, "sh") || strstr (default_shell, "SH"))) {
    846     unixy_shell = 1;
    847     batch_mode_shell = 0;
    848   }
     1031  if (!unixy_shell && sh_found
     1032      && (strstr (default_shell, "sh") || strstr (default_shell, "SH")))
     1033    {
     1034      unixy_shell = 1;
     1035      batch_mode_shell = 0;
     1036    }
    8491037
    8501038#ifdef BATCH_MODE_ONLY_SHELL
     
    8521040#endif
    8531041
    854   if (atoken)
    855     free (atoken);
     1042  free (atoken);
    8561043
    8571044  return (sh_found);
     
    8681055#endif  /* __MSDOS__ */
    8691056
    870 char *mktemp (char *template);
    871 int mkstemp (char *template);
    872 
    873 FILE *
    874 open_tmpfile(char **name, const char *template)
     1057static void
     1058reset_jobserver (void)
    8751059{
    876 #ifdef HAVE_FDOPEN
    877   int fd;
    878 #endif
    879 
    880 #if defined HAVE_MKSTEMP || defined HAVE_MKTEMP
    881 # define TEMPLATE_LEN   strlen (template)
    882 #else
    883 # define TEMPLATE_LEN   L_tmpnam
    884 #endif
    885   *name = xmalloc (TEMPLATE_LEN + 1);
    886   strcpy (*name, template);
    887 
    888 #if defined HAVE_MKSTEMP && defined HAVE_FDOPEN
    889   /* It's safest to use mkstemp(), if we can.  */
    890   fd = mkstemp (*name);
    891   if (fd == -1)
    892     return 0;
    893   return fdopen (fd, "w");
    894 #else
    895 # ifdef HAVE_MKTEMP
    896   (void) mktemp (*name);
    897 # else
    898   (void) tmpnam (*name);
    899 # endif
    900 
    901 # ifdef HAVE_FDOPEN
    902   /* Can't use mkstemp(), but guard against a race condition.  */
    903   fd = open (*name, O_CREAT|O_EXCL|O_WRONLY, 0600);
    904   if (fd == -1)
    905     return 0;
    906   return fdopen (fd, "w");
    907 # else
    908   /* Not secure, but what can we do?  */
    909   return fopen (*name, "w");
    910 # endif
    911 #endif
     1060  jobserver_clear ();
     1061  free (jobserver_auth);
     1062  jobserver_auth = NULL;
    9121063}
    913 
    9141064
    9151065#ifdef _AMIGA
     
    9231073  static char *stdin_nm = 0;
    9241074  int makefile_status = MAKE_SUCCESS;
    925   struct dep *read_makefiles;
     1075  struct goaldep *read_files;
    9261076  PATH_VAR (current_directory);
    9271077  unsigned int restarts = 0;
     1078  unsigned int syncing = 0;
     1079  int argv_slots;
    9281080#ifdef WINDOWS32
    929   char *unix_path = NULL;
    930   char *windows32_path = NULL;
    931 
    932   SetUnhandledExceptionFilter(handle_runtime_exceptions);
     1081  const char *unix_path = NULL;
     1082  const char *windows32_path = NULL;
     1083
     1084  SetUnhandledExceptionFilter (handle_runtime_exceptions);
    9331085
    9341086  /* start off assuming we have no shell */
     
    9361088  no_default_sh_exe = 1;
    9371089#endif
     1090
     1091  output_init (&make_sync);
     1092
     1093  initialize_stopchar_map();
    9381094
    9391095#ifdef SET_STACK_SIZE
     
    9551111#endif
    9561112
    957 #ifdef HAVE_ATEXIT
    958   atexit (close_stdout);
    959 #endif
    960 
    9611113  /* Needed for OS/2 */
    962   initialize_main(&argc, &argv);
    963 
    964   reading_file = 0;
     1114  initialize_main (&argc, &argv);
     1115
     1116#ifdef MAKE_MAINTAINER_MODE
     1117  /* In maintainer mode we always enable verification.  */
     1118  verify_flag = 1;
     1119#endif
    9651120
    9661121#if defined (__MSDOS__) && !defined (_POSIX_SOURCE)
    967   /* Request the most powerful version of `system', to
     1122  /* Request the most powerful version of 'system', to
    9681123     make up for the dumb default shell.  */
    9691124  __system_flags = (__system_redirect
    970                     | __system_use_shell
    971                     | __system_allow_multiple_cmds
    972                     | __system_allow_long_cmds
    973                     | __system_handle_null_commands
    974                     | __system_emulate_chdir);
     1125                    | __system_use_shell
     1126                    | __system_allow_multiple_cmds
     1127                    | __system_allow_long_cmds
     1128                    | __system_handle_null_commands
     1129                    | __system_emulate_chdir);
    9751130
    9761131#endif
     
    9831138  (void)textdomain (PACKAGE);
    9841139
    985 #ifdef  POSIX
     1140#ifdef  POSIX
    9861141  sigemptyset (&fatal_signal_set);
    987 #define ADD_SIG(sig)    sigaddset (&fatal_signal_set, sig)
     1142#define ADD_SIG(sig)    sigaddset (&fatal_signal_set, sig)
    9881143#else
    989 #ifdef  HAVE_SIGSETMASK
     1144#ifdef  HAVE_SIGSETMASK
    9901145  fatal_signal_mask = 0;
    991 #define ADD_SIG(sig)    fatal_signal_mask |= sigmask (sig)
     1146#define ADD_SIG(sig)    fatal_signal_mask |= sigmask (sig)
    9921147#else
    993 #define ADD_SIG(sig)    (void)sig      /* Needed to avoid warnings in MSVC.  */
    994 #endif
    995 #endif
    996 
    997 #define FATAL_SIG(sig)                                                        \
    998   if (bsd_signal (sig, fatal_error_signal) == SIG_IGN)                        \
    999     bsd_signal (sig, SIG_IGN);                                                \
    1000   else                                                                        \
     1148#define ADD_SIG(sig)    (void)sig
     1149#endif
     1150#endif
     1151
     1152#define FATAL_SIG(sig)                                                        \
     1153  if (bsd_signal (sig, fatal_error_signal) == SIG_IGN)                        \
     1154    bsd_signal (sig, SIG_IGN);                                                \
     1155  else                                                                        \
    10011156    ADD_SIG (sig);
    10021157
     
    10171172#endif
    10181173
    1019 #ifdef  SIGDANGER
     1174#ifdef  SIGDANGER
    10201175  FATAL_SIG (SIGDANGER);
    10211176#endif
     
    10271182#endif
    10281183
    1029 #undef  FATAL_SIG
     1184#undef  FATAL_SIG
    10301185
    10311186  /* Do not ignore the child-death signal.  This must be done before
     
    10431198#endif
    10441199
    1045   /* Make sure stdout is line-buffered.  */
    1046 
    1047 #ifdef HAVE_SETVBUF
    1048 # ifdef SETVBUF_REVERSED
    1049   setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
    1050 # else  /* setvbuf not reversed.  */
    1051   /* Some buggy systems lose if we pass 0 instead of allocating ourselves.  */
    1052   setvbuf (stdout, 0, _IOLBF, BUFSIZ);
    1053 # endif /* setvbuf reversed.  */
    1054 #elif HAVE_SETLINEBUF
    1055   setlinebuf (stdout);
    1056 #endif  /* setlinebuf missing.  */
     1200  output_init (NULL);
    10571201
    10581202  /* Figure out where this program lives.  */
    10591203
    10601204  if (argv[0] == 0)
    1061     argv[0] = "";
     1205    argv[0] = (char *)"";
    10621206  if (argv[0][0] == '\0')
    10631207    program = "make";
    10641208  else
    10651209    {
    1066 #ifdef VMS
    1067       program = strrchr (argv[0], ']');
    1068 #else
    10691210      program = strrchr (argv[0], '/');
    1070 #endif
    10711211#if defined(__MSDOS__) || defined(__EMX__)
    10721212      if (program == 0)
    1073         program = strrchr (argv[0], '\\');
     1213        program = strrchr (argv[0], '\\');
    10741214      else
    1075         {
    1076           /* Some weird environments might pass us argv[0] with
    1077              both kinds of slashes; we must find the rightmost.  */
    1078           char *p = strrchr (argv[0], '\\');
    1079           if (p && p > program)
    1080             program = p;
    1081         }
     1215        {
     1216          /* Some weird environments might pass us argv[0] with
     1217             both kinds of slashes; we must find the rightmost.  */
     1218          char *p = strrchr (argv[0], '\\');
     1219          if (p && p > program)
     1220            program = p;
     1221        }
    10821222      if (program == 0 && argv[0][1] == ':')
    1083         program = argv[0] + 1;
     1223        program = argv[0] + 1;
    10841224#endif
    10851225#ifdef WINDOWS32
     
    10871227        {
    10881228          /* Extract program from full path */
    1089           int argv0_len;
    10901229          program = strrchr (argv[0], '\\');
    10911230          if (program)
    10921231            {
    1093               argv0_len = strlen(program);
     1232              int argv0_len = strlen (program);
    10941233              if (argv0_len > 4 && streq (&program[argv0_len - 4], ".exe"))
    10951234                /* Remove .exe extension */
     
    10981237        }
    10991238#endif
     1239#ifdef VMS
     1240      set_program_name (argv[0]);
     1241      program = program_name;
     1242      {
     1243        const char *shell;
     1244        char pwdbuf[256];
     1245        char *pwd;
     1246        shell = getenv ("SHELL");
     1247        if (shell != NULL)
     1248          vms_gnv_shell = 1;
     1249
     1250        /* Need to know if CRTL set to report UNIX paths.  Use getcwd as
     1251           it works on all versions of VMS. */
     1252        pwd = getcwd(pwdbuf, 256);
     1253        if (pwd[0] == '/')
     1254          vms_report_unix_paths = 1;
     1255
     1256        vms_use_mcr_command = get_vms_env_flag ("GNV$MAKE_USE_MCR", 0);
     1257
     1258        vms_always_use_cmd_file = get_vms_env_flag ("GNV$MAKE_USE_CMD_FILE", 0);
     1259
     1260        /* Legacy behavior is on VMS is older behavior that needed to be
     1261           changed to be compatible with standard make behavior.
     1262           For now only completely disable when running under a Bash shell.
     1263           TODO: Update VMS built in recipes and macros to not need this
     1264           behavior, at which time the default may change. */
     1265        vms_legacy_behavior = get_vms_env_flag ("GNV$MAKE_OLD_VMS",
     1266                                                !vms_gnv_shell);
     1267
     1268        /* VMS was changed to use a comma separator in the past, but that is
     1269           incompatible with built in functions that expect space separated
     1270           lists.  Allow this to be selectively turned off. */
     1271        vms_comma_separator = get_vms_env_flag ("GNV$MAKE_COMMA",
     1272                                                vms_legacy_behavior);
     1273
     1274        /* Some Posix shell syntax options are incompatible with VMS syntax.
     1275           VMS requires double quotes for strings and escapes quotes
     1276           differently.  When this option is active, VMS will try
     1277           to simulate Posix shell simulations instead of using
     1278           VMS DCL behavior. */
     1279        vms_unix_simulation = get_vms_env_flag ("GNV$MAKE_SHELL_SIM",
     1280                                                !vms_legacy_behavior);
     1281
     1282      }
     1283      if (need_vms_symbol () && !vms_use_mcr_command)
     1284        create_foreign_command (program_name, argv[0]);
     1285#else
    11001286      if (program == 0)
    1101         program = argv[0];
     1287        program = argv[0];
    11021288      else
    1103         ++program;
     1289        ++program;
     1290#endif
    11041291    }
    11051292
     
    11171304#endif
    11181305    {
    1119 #ifdef  HAVE_GETCWD
     1306#ifdef  HAVE_GETCWD
    11201307      perror_with_name ("getcwd", "");
    11211308#else
    1122       error (NILF, "getwd: %s", current_directory);
     1309      OS (error, NILF, "getwd: %s", current_directory);
    11231310#endif
    11241311      current_directory[0] = '\0';
     
    11271314  else
    11281315    directory_before_chdir = xstrdup (current_directory);
     1316
    11291317#ifdef  __MSDOS__
    11301318  /* Make sure we will return to the initial directory, come what may.  */
     
    11371325  define_variable_cname (".RECIPEPREFIX", "", o_default, 0)->special = 1;
    11381326  define_variable_cname (".SHELLFLAGS", "-c", o_default, 0);
     1327  define_variable_cname (".LOADED", "", o_default, 0);
    11391328
    11401329  /* Set up .FEATURES
    1141      We must do this in multiple calls because define_variable_cname() is
    1142      a macro and some compilers (MSVC) don't like conditionals in macros.  */
     1330     Use a separate variable because define_variable_cname() is a macro and
     1331     some compilers (MSVC) don't like conditionals in macros.  */
    11431332  {
    11441333    const char *features = "target-specific order-only second-expansion"
    1145                            " else-if shortest-stem undefine"
     1334                           " else-if shortest-stem undefine oneshell"
    11461335#ifndef NO_ARCHIVES
    11471336                           " archives"
     
    11501339                           " jobserver"
    11511340#endif
     1341#ifndef NO_OUTPUT_SYNC
     1342                           " output-sync"
     1343#endif
    11521344#ifdef MAKE_SYMLINKS
    11531345                           " check-symlink"
    11541346#endif
     1347#ifdef HAVE_GUILE
     1348                           " guile"
     1349#endif
     1350#ifdef MAKE_LOAD
     1351                           " load"
     1352#endif
    11551353                           ;
    11561354
    11571355    define_variable_cname (".FEATURES", features, o_default, 0);
    11581356  }
     1357
     1358  /* Configure GNU Guile support */
     1359  guile_gmake_setup (NILF);
    11591360
    11601361  /* Read in variables from the environment.  It is important that this be
     
    11681369    for (i = 0; envp[i] != 0; ++i)
    11691370      {
    1170         int do_not_define = 0;
    1171         char *ep = envp[i];
    1172 
    1173         while (*ep != '\0' && *ep != '=')
     1371        struct variable *v;
     1372        const char *ep = envp[i];
     1373        /* By default, export all variables culled from the environment.  */
     1374        enum variable_export export = v_export;
     1375        unsigned int len;
     1376
     1377        while (! STOP_SET (*ep, MAP_EQUALS))
    11741378          ++ep;
     1379
     1380        /* If there's no equals sign it's a malformed environment.  Ignore.  */
     1381        if (*ep == '\0')
     1382          continue;
     1383
    11751384#ifdef WINDOWS32
    1176         if (!unix_path && strneq(envp[i], "PATH=", 5))
     1385        if (!unix_path && strneq (envp[i], "PATH=", 5))
    11771386          unix_path = ep+1;
    1178         else if (!strnicmp(envp[i], "Path=", 5)) {
    1179           do_not_define = 1; /* it gets defined after loop exits */
    1180           if (!windows32_path)
    1181             windows32_path = ep+1;
    1182         }
    1183 #endif
    1184         /* The result of pointer arithmetic is cast to unsigned int for
    1185            machines where ptrdiff_t is a different size that doesn't widen
    1186            the same.  */
    1187         if (!do_not_define)
     1387        else if (!strnicmp (envp[i], "Path=", 5))
    11881388          {
    1189             struct variable *v;
    1190 
    1191             v = define_variable (envp[i], (unsigned int) (ep - envp[i]),
    1192                                  ep + 1, o_env, 1);
    1193             /* Force exportation of every variable culled from the
    1194                environment.  We used to rely on target_environment's
    1195                v_default code to do this.  But that does not work for the
    1196                case where an environment variable is redefined in a makefile
    1197                with `override'; it should then still be exported, because it
    1198                was originally in the environment.  */
    1199             v->export = v_export;
    1200 
    1201             /* Another wrinkle is that POSIX says the value of SHELL set in
    1202                the makefile won't change the value of SHELL given to
    1203                subprocesses.  */
    1204             if (streq (v->name, "SHELL"))
     1389            if (!windows32_path)
     1390              windows32_path = ep+1;
     1391            /* PATH gets defined after the loop exits.  */
     1392            continue;
     1393          }
     1394#endif
     1395
     1396        /* Length of the variable name, and skip the '='.  */
     1397        len = ep++ - envp[i];
     1398
     1399        /* If this is MAKE_RESTARTS, check to see if the "already printed
     1400           the enter statement" flag is set.  */
     1401        if (len == 13 && strneq (envp[i], "MAKE_RESTARTS", 13))
     1402          {
     1403            if (*ep == '-')
    12051404              {
     1405                OUTPUT_TRACED ();
     1406                ++ep;
     1407              }
     1408            restarts = (unsigned int) atoi (ep);
     1409            export = v_noexport;
     1410          }
     1411
     1412        v = define_variable (envp[i], len, ep, o_env, 1);
     1413
     1414        /* POSIX says the value of SHELL set in the makefile won't change the
     1415           value of SHELL given to subprocesses.  */
     1416        if (streq (v->name, "SHELL"))
     1417          {
    12061418#ifndef __MSDOS__
    1207                 v->export = v_noexport;
    1208 #endif
    1209                 shell_var.name = "SHELL";
    1210                 shell_var.length = 5;
    1211                 shell_var.value = xstrdup (ep + 1);
    1212               }
    1213 
    1214             /* If MAKE_RESTARTS is set, remember it but don't export it.  */
    1215             if (streq (v->name, "MAKE_RESTARTS"))
    1216               {
    1217                 v->export = v_noexport;
    1218                 restarts = (unsigned int) atoi (ep + 1);
    1219               }
     1419            export = v_noexport;
     1420#endif
     1421            shell_var.name = xstrdup ("SHELL");
     1422            shell_var.length = 5;
     1423            shell_var.value = xstrdup (ep);
    12201424          }
     1425
     1426        v->export = export;
    12211427      }
    12221428  }
    12231429#ifdef WINDOWS32
    12241430    /* If we didn't find a correctly spelled PATH we define PATH as
    1225      * either the first mispelled value or an empty string
     1431     * either the first misspelled value or an empty string
    12261432     */
    12271433    if (!unix_path)
     
    12311437#else /* For Amiga, read the ENV: device, ignoring all dirs */
    12321438    {
    1233         BPTR env, file, old;
    1234         char buffer[1024];
    1235         int len;
    1236         __aligned struct FileInfoBlock fib;
    1237 
    1238         env = Lock ("ENV:", ACCESS_READ);
    1239         if (env)
    1240         {
    1241             old = CurrentDir (DupLock(env));
    1242             Examine (env, &fib);
    1243 
    1244             while (ExNext (env, &fib))
    1245             {
    1246                 if (fib.fib_DirEntryType < 0) /* File */
    1247                 {
    1248                     /* Define an empty variable. It will be filled in
    1249                         variable_lookup(). Makes startup quite a bit
    1250                         faster. */
    1251                         define_variable (fib.fib_FileName,
    1252                             strlen (fib.fib_FileName),
    1253                         "", o_env, 1)->export = v_export;
    1254                 }
    1255             }
    1256             UnLock (env);
    1257             UnLock(CurrentDir(old));
    1258         }
     1439        BPTR env, file, old;
     1440        char buffer[1024];
     1441        int len;
     1442        __aligned struct FileInfoBlock fib;
     1443
     1444        env = Lock ("ENV:", ACCESS_READ);
     1445        if (env)
     1446          {
     1447            old = CurrentDir (DupLock (env));
     1448            Examine (env, &fib);
     1449
     1450            while (ExNext (env, &fib))
     1451              {
     1452                if (fib.fib_DirEntryType < 0) /* File */
     1453                  {
     1454                    /* Define an empty variable. It will be filled in
     1455                       variable_lookup(). Makes startup quite a bit faster. */
     1456                    define_variable (fib.fib_FileName,
     1457                                     strlen (fib.fib_FileName),
     1458                                     "", o_env, 1)->export = v_export;
     1459                  }
     1460              }
     1461            UnLock (env);
     1462            UnLock (CurrentDir (old));
     1463          }
    12591464    }
    12601465#endif
    12611466
    12621467  /* Decode the switches.  */
     1468  decode_env_switches (STRING_SIZE_TUPLE ("GNUMAKEFLAGS"));
     1469
     1470  /* Clear GNUMAKEFLAGS to avoid duplication.  */
     1471  define_variable_cname ("GNUMAKEFLAGS", "", o_env, 0);
    12631472
    12641473  decode_env_switches (STRING_SIZE_TUPLE ("MAKEFLAGS"));
     1474
    12651475#if 0
    12661476  /* People write things like:
     
    12701480#endif
    12711481
    1272   decode_switches (argc, argv, 0);
     1482  /* In output sync mode we need to sync any output generated by reading the
     1483     makefiles, such as in $(info ...) or stderr from $(shell ...) etc.  */
     1484
     1485  syncing = make_sync.syncout = (output_sync == OUTPUT_SYNC_LINE
     1486                                 || output_sync == OUTPUT_SYNC_TARGET);
     1487  OUTPUT_SET (&make_sync);
     1488
     1489  /* Remember the job slots set through the environment vs. command line.  */
     1490  {
     1491    int env_slots = arg_job_slots;
     1492    arg_job_slots = INVALID_JOB_SLOTS;
     1493
     1494    decode_switches (argc, (const char **)argv, 0);
     1495    argv_slots = arg_job_slots;
     1496
     1497    if (arg_job_slots == INVALID_JOB_SLOTS)
     1498      arg_job_slots = env_slots;
     1499  }
     1500
     1501  /* Set a variable specifying whether stdout/stdin is hooked to a TTY.  */
     1502#ifdef HAVE_ISATTY
     1503  if (isatty (fileno (stdout)))
     1504    if (! lookup_variable (STRING_SIZE_TUPLE ("MAKE_TERMOUT")))
     1505      {
     1506        const char *tty = TTYNAME (fileno (stdout));
     1507        define_variable_cname ("MAKE_TERMOUT", tty ? tty : DEFAULT_TTYNAME,
     1508                               o_default, 0)->export = v_export;
     1509      }
     1510  if (isatty (fileno (stderr)))
     1511    if (! lookup_variable (STRING_SIZE_TUPLE ("MAKE_TERMERR")))
     1512      {
     1513        const char *tty = TTYNAME (fileno (stderr));
     1514        define_variable_cname ("MAKE_TERMERR", tty ? tty : DEFAULT_TTYNAME,
     1515                               o_default, 0)->export = v_export;
     1516      }
     1517#endif
     1518
     1519  /* Reset in case the switches changed our minds.  */
     1520  syncing = (output_sync == OUTPUT_SYNC_LINE
     1521             || output_sync == OUTPUT_SYNC_TARGET);
     1522
     1523  if (make_sync.syncout && ! syncing)
     1524    output_close (&make_sync);
     1525
     1526  make_sync.syncout = syncing;
     1527  OUTPUT_SET (&make_sync);
     1528
     1529  /* Figure out the level of recursion.  */
     1530  {
     1531    struct variable *v = lookup_variable (STRING_SIZE_TUPLE (MAKELEVEL_NAME));
     1532    if (v && v->value[0] != '\0' && v->value[0] != '-')
     1533      makelevel = (unsigned int) atoi (v->value);
     1534    else
     1535      makelevel = 0;
     1536  }
    12731537
    12741538#ifdef WINDOWS32
    1275   if (suspend_flag) {
    1276         fprintf(stderr, "%s (pid = %ld)\n", argv[0], GetCurrentProcessId());
    1277         fprintf(stderr, _("%s is suspending for 30 seconds..."), argv[0]);
    1278         Sleep(30 * 1000);
    1279         fprintf(stderr, _("done sleep(30). Continuing.\n"));
    1280   }
    1281 #endif
    1282 
    1283   decode_debug_flags ();
     1539  if (suspend_flag)
     1540    {
     1541      fprintf (stderr, "%s (pid = %ld)\n", argv[0], GetCurrentProcessId ());
     1542      fprintf (stderr, _("%s is suspending for 30 seconds..."), argv[0]);
     1543      Sleep (30 * 1000);
     1544      fprintf (stderr, _("done sleep(30). Continuing.\n"));
     1545    }
     1546#endif
    12841547
    12851548  /* Set always_make_flag if -B was given and we've not restarted already.  */
    12861549  always_make_flag = always_make_set && (restarts == 0);
    12871550
    1288   /* Print version information.  */
    1289   if (print_version_flag || print_data_base_flag || db_level)
     1551  /* Print version information, and exit.  */
     1552  if (print_version_flag)
    12901553    {
    12911554      print_version ();
    1292 
    1293       /* `make --version' is supposed to just print the version and exit.  */
    1294       if (print_version_flag)
    1295         die (0);
    1296     }
     1555      die (MAKE_SUCCESS);
     1556    }
     1557
     1558  if (ISDB (DB_BASIC))
     1559    print_version ();
    12971560
    12981561#ifndef VMS
     
    13091572   * CreateProcess().
    13101573   */
    1311   if (strpbrk(argv[0], "/:\\") ||
    1312       strstr(argv[0], "..") ||
    1313       strneq(argv[0], "//", 2))
    1314     argv[0] = xstrdup(w32ify(argv[0],1));
     1574  if (strpbrk (argv[0], "/:\\") || strstr (argv[0], "..")
     1575      || strneq (argv[0], "//", 2))
     1576    argv[0] = xstrdup (w32ify (argv[0], 1));
    13151577#else /* WINDOWS32 */
    13161578#if defined (__MSDOS__) || defined (__EMX__)
     
    13211583      argv[0] = xstrdup (argv[0]);
    13221584      for (p = argv[0]; *p; p++)
    1323         if (*p == '\\')
    1324           *p = '/';
     1585        if (*p == '\\')
     1586          *p = '/';
    13251587    }
    13261588  /* If argv[0] is not in absolute form, prepend the current
     
    13491611#endif
    13501612
     1613  /* We may move, but until we do, here we are.  */
     1614  starting_directory = current_directory;
     1615
     1616  /* Set up the job_slots value and the jobserver.  This can't be usefully set
     1617     in the makefile, and we want to verify the authorization is valid before
     1618     make has a chance to start using it for something else.  */
     1619
     1620  if (jobserver_auth)
     1621    {
     1622      if (argv_slots == INVALID_JOB_SLOTS)
     1623        {
     1624          if (jobserver_parse_auth (jobserver_auth))
     1625            {
     1626              /* Success!  Use the jobserver.  */
     1627              job_slots = 0;
     1628              goto job_setup_complete;
     1629            }
     1630
     1631          O (error, NILF, _("warning: jobserver unavailable: using -j1.  Add '+' to parent make rule."));
     1632          arg_job_slots = 1;
     1633        }
     1634
     1635      /* The user provided a -j setting on the command line: use it.  */
     1636      else if (!restarts)
     1637        /* If restarts is >0 we already printed this message.  */
     1638        O (error, NILF,
     1639           _("warning: -jN forced in submake: disabling jobserver mode."));
     1640
     1641      /* We failed to use our parent's jobserver.  */
     1642      reset_jobserver ();
     1643      job_slots = (unsigned int)arg_job_slots;
     1644    }
     1645  else if (arg_job_slots == INVALID_JOB_SLOTS)
     1646    /* The default is one job at a time.  */
     1647    job_slots = 1;
     1648  else
     1649    /* Use whatever was provided.  */
     1650    job_slots = (unsigned int)arg_job_slots;
     1651
     1652 job_setup_complete:
     1653
    13511654  /* The extra indirection through $(MAKE_COMMAND) is done
    13521655     for hysterical raisins.  */
     1656
     1657#ifdef VMS
     1658  if (vms_use_mcr_command)
     1659    define_variable_cname ("MAKE_COMMAND", vms_command (argv[0]), o_default, 0);
     1660  else
     1661    define_variable_cname ("MAKE_COMMAND", program, o_default, 0);
     1662#else
    13531663  define_variable_cname ("MAKE_COMMAND", argv[0], o_default, 0);
     1664#endif
    13541665  define_variable_cname ("MAKE", "$(MAKE_COMMAND)", o_default, 1);
    13551666
     
    13621673
    13631674      /* Figure out how much space will be taken up by the command-line
    1364         variable definitions.  */
     1675        variable definitions.  */
    13651676      for (cv = command_variables; cv != 0; cv = cv->next)
    1366         {
    1367           v = cv->variable;
    1368           len += 2 * strlen (v->name);
    1369           if (! v->recursive)
    1370             ++len;
    1371           ++len;
    1372           len += 2 * strlen (v->value);
    1373           ++len;
    1374         }
     1677        {
     1678          v = cv->variable;
     1679          len += 2 * strlen (v->name);
     1680          if (! v->recursive)
     1681            ++len;
     1682          ++len;
     1683          len += 2 * strlen (v->value);
     1684          ++len;
     1685        }
    13751686
    13761687      /* Now allocate a buffer big enough and fill it.  */
    13771688      p = value = alloca (len);
    13781689      for (cv = command_variables; cv != 0; cv = cv->next)
    1379         {
    1380           v = cv->variable;
    1381           p = quote_for_env (p, v->name);
    1382           if (! v->recursive)
    1383             *p++ = ':';
    1384           *p++ = '=';
    1385           p = quote_for_env (p, v->value);
    1386           *p++ = ' ';
    1387         }
    1388       p[-1] = '\0';             /* Kill the final space and terminate.  */
     1690        {
     1691          v = cv->variable;
     1692          p = quote_for_env (p, v->name);
     1693          if (! v->recursive)
     1694            *p++ = ':';
     1695          *p++ = '=';
     1696          p = quote_for_env (p, v->value);
     1697          *p++ = ' ';
     1698        }
     1699      p[-1] = '\0';             /* Kill the final space and terminate.  */
    13891700
    13901701      /* Define an unchangeable variable with a name that no POSIX.2
    1391         makefile could validly use for its own variable.  */
     1702        makefile could validly use for its own variable.  */
    13921703      define_variable_cname ("-*-command-variables-*-", value, o_automatic, 0);
    13931704
     
    14001711      define_variable_cname ("MAKEOVERRIDES", "${-*-command-variables-*-}",
    14011712                             o_env, 1);
     1713#ifdef VMS
     1714      vms_export_dcl_symbol ("MAKEOVERRIDES", "${-*-command-variables-*-}");
     1715#endif
    14021716    }
    14031717
     
    14341748   * at the wrong place when it was first evaluated.
    14351749   */
    1436    no_default_sh_exe = !find_and_set_default_shell(NULL);
    1437 
     1750   no_default_sh_exe = !find_and_set_default_shell (NULL);
    14381751#endif /* WINDOWS32 */
    1439   /* Figure out the level of recursion.  */
    1440   {
    1441     struct variable *v = lookup_variable (STRING_SIZE_TUPLE (MAKELEVEL_NAME));
    1442     if (v != 0 && v->value[0] != '\0' && v->value[0] != '-')
    1443       makelevel = (unsigned int) atoi (v->value);
    1444     else
    1445       makelevel = 0;
    1446   }
    14471752
    14481753  /* Except under -s, always do -w in sub-makes and under -C.  */
     
    14631768                          ? 0 : include_directories->list);
    14641769
    1465   /* Figure out where we are now, after chdir'ing.  */
    1466   if (directories == 0)
    1467     /* We didn't move, so we're still in the same place.  */
    1468     starting_directory = current_directory;
    1469   else
     1770  /* If we chdir'ed, figure out where we are now.  */
     1771  if (directories)
    14701772    {
    14711773#ifdef WINDOWS32
     
    14741776      if (getcwd (current_directory, GET_PATH_MAX) == 0)
    14751777#endif
    1476         {
    1477 #ifdef  HAVE_GETCWD
    1478           perror_with_name ("getcwd", "");
     1778        {
     1779#ifdef  HAVE_GETCWD
     1780          perror_with_name ("getcwd", "");
    14791781#else
    1480           error (NILF, "getwd: %s", current_directory);
    1481 #endif
    1482           starting_directory = 0;
    1483         }
     1782          OS (error, NILF, "getwd: %s", current_directory);
     1783#endif
     1784          starting_directory = 0;
     1785        }
    14841786      else
    1485         starting_directory = current_directory;
     1787        starting_directory = current_directory;
    14861788    }
    14871789
     
    14941796      unsigned int i;
    14951797      for (i = 0; i < makefiles->idx; ++i)
    1496         if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
    1497           {
    1498             /* This makefile is standard input.  Since we may re-exec
    1499                and thus re-read the makefiles, we read standard input
    1500                into a temporary file and read from that.  */
    1501             FILE *outfile;
    1502             char *template, *tmpdir;
     1798        if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
     1799          {
     1800            /* This makefile is standard input.  Since we may re-exec
     1801               and thus re-read the makefiles, we read standard input
     1802               into a temporary file and read from that.  */
     1803            FILE *outfile;
     1804            char *template;
     1805            const char *tmpdir;
    15031806
    15041807            if (stdin_nm)
    1505               fatal (NILF, _("Makefile from standard input specified twice."));
     1808              O (fatal, NILF,
     1809                 _("Makefile from standard input specified twice."));
    15061810
    15071811#ifdef VMS
    1508 # define DEFAULT_TMPDIR     "sys$scratch:"
     1812# define DEFAULT_TMPDIR     "/sys$scratch/"
    15091813#else
    15101814# ifdef P_tmpdir
     
    15161820#define DEFAULT_TMPFILE     "GmXXXXXX"
    15171821
    1518             if (((tmpdir = getenv ("TMPDIR")) == NULL || *tmpdir == '\0')
     1822            if (((tmpdir = getenv ("TMPDIR")) == NULL || *tmpdir == '\0')
    15191823#if defined (__MSDOS__) || defined (WINDOWS32) || defined (__EMX__)
    15201824                /* These are also used commonly on these platforms.  */
     
    15231827#endif
    15241828               )
    1525               tmpdir = DEFAULT_TMPDIR;
    1526 
    1527             template = alloca (strlen (tmpdir) + sizeof (DEFAULT_TMPFILE) + 1);
    1528             strcpy (template, tmpdir);
     1829              tmpdir = DEFAULT_TMPDIR;
     1830
     1831            template = alloca (strlen (tmpdir) + CSTRLEN (DEFAULT_TMPFILE) + 2);
     1832            strcpy (template, tmpdir);
    15291833
    15301834#ifdef HAVE_DOS_PATHS
    1531             if (strchr ("/\\", template[strlen (template) - 1]) == NULL)
    1532               strcat (template, "/");
     1835            if (strchr ("/\\", template[strlen (template) - 1]) == NULL)
     1836              strcat (template, "/");
    15331837#else
    15341838# ifndef VMS
    1535             if (template[strlen (template) - 1] != '/')
    1536               strcat (template, "/");
     1839            if (template[strlen (template) - 1] != '/')
     1840              strcat (template, "/");
    15371841# endif /* !VMS */
    15381842#endif /* !HAVE_DOS_PATHS */
    15391843
    1540             strcat (template, DEFAULT_TMPFILE);
    1541             outfile = open_tmpfile (&stdin_nm, template);
    1542             if (outfile == 0)
    1543               pfatal_with_name (_("fopen (temporary file)"));
    1544             while (!feof (stdin) && ! ferror (stdin))
    1545               {
    1546                 char buf[2048];
    1547                 unsigned int n = fread (buf, 1, sizeof (buf), stdin);
    1548                 if (n > 0 && fwrite (buf, 1, n, outfile) != n)
    1549                   pfatal_with_name (_("fwrite (temporary file)"));
    1550               }
    1551             fclose (outfile);
    1552 
    1553             /* Replace the name that read_all_makefiles will
    1554                see with the name of the temporary file.  */
     1844            strcat (template, DEFAULT_TMPFILE);
     1845            outfile = output_tmpfile (&stdin_nm, template);
     1846            if (outfile == 0)
     1847              pfatal_with_name (_("fopen (temporary file)"));
     1848            while (!feof (stdin) && ! ferror (stdin))
     1849              {
     1850                char buf[2048];
     1851                unsigned int n = fread (buf, 1, sizeof (buf), stdin);
     1852                if (n > 0 && fwrite (buf, 1, n, outfile) != n)
     1853                  pfatal_with_name (_("fwrite (temporary file)"));
     1854              }
     1855            fclose (outfile);
     1856
     1857            /* Replace the name that read_all_makefiles will
     1858               see with the name of the temporary file.  */
    15551859            makefiles->list[i] = strcache_add (stdin_nm);
    15561860
    1557             /* Make sure the temporary file will not be remade.  */
     1861            /* Make sure the temporary file will not be remade.  */
    15581862            {
    15591863              struct file *f = enter_file (strcache_add (stdin_nm));
    15601864              f->updated = 1;
    1561               f->update_status = 0;
     1865              f->update_status = us_success;
    15621866              f->command_state = cs_finished;
    15631867              /* Can't be intermediate, or it'll be removed too early for
     
    15661870              f->dontcare = 0;
    15671871            }
    1568           }
     1872          }
    15691873    }
    15701874
    15711875#ifndef __EMX__ /* Don't use a SIGCHLD handler for OS/2 */
    1572 #if defined(MAKE_JOBSERVER) || !defined(HAVE_WAIT_NOHANG)
     1876#if !defined(HAVE_WAIT_NOHANG) || defined(MAKE_JOBSERVER)
    15731877  /* Set up to handle children dying.  This must be done before
    1574      reading in the makefiles so that `shell' function calls will work.
     1878     reading in the makefiles so that 'shell' function calls will work.
    15751879
    15761880     If we don't have a hanging wait we have to fall back to old, broken
     
    15781882     children.
    15791883
    1580      If we're using the jobs pipe we need a signal handler so that
    1581      SIGCHLD is not ignored; we need it to interrupt the read(2) of the
    1582      jobserver pipe in job.c if we're waiting for a token.
     1884     If we're using the jobs pipe we need a signal handler so that SIGCHLD is
     1885     not ignored; we need it to interrupt the read(2) of the jobserver pipe if
     1886     we're waiting for a token.
    15831887
    15841888     If none of these are true, we don't need a signal handler at all.  */
    15851889  {
    1586     RETSIGTYPE child_handler (int sig);
    15871890# if defined SIGCHLD
    15881891    bsd_signal (SIGCHLD, child_handler);
     
    15921895# endif
    15931896  }
     1897
     1898#ifdef HAVE_PSELECT
     1899  /* If we have pselect() then we need to block SIGCHLD so it's deferred.  */
     1900  {
     1901    sigset_t block;
     1902    sigemptyset (&block);
     1903    sigaddset (&block, SIGCHLD);
     1904    if (sigprocmask (SIG_SETMASK, &block, NULL) < 0)
     1905      pfatal_with_name ("sigprocmask(SIG_SETMASK, SIGCHLD)");
     1906  }
     1907#endif
     1908
    15941909#endif
    15951910#endif
     
    16011916
    16021917  /* Define the initial list of suffixes for old-style rules.  */
    1603 
    16041918  set_default_suffixes ();
    16051919
     
    16091923     makefiles, it results in the built-in pattern rules taking precedence
    16101924     over makefile-specified suffix rules, which is wrong.  */
    1611 
    16121925  install_default_suffix_rules ();
    16131926
    16141927  /* Define some internal and special variables.  */
    1615 
    16161928  define_automatic_variables ();
    16171929
    1618   /* Set up the MAKEFLAGS and MFLAGS variables
    1619      so makefiles can look at them.  */
    1620 
    1621   define_makeflags (0, 0);
     1930  /* Set up the MAKEFLAGS and MFLAGS variables for makefiles to see.
     1931     Initialize it to be exported but allow the makefile to reset it.  */
     1932  define_makeflags (0, 0)->export = v_export;
    16221933
    16231934  /* Define the default variables.  */
     
    16351946      char *p, *value;
    16361947      unsigned int i;
    1637       unsigned int len = sizeof ("--eval=") * eval_strings->idx;
     1948      unsigned int len = (CSTRLEN ("--eval=") + 1) * eval_strings->idx;
    16381949
    16391950      for (i = 0; i < eval_strings->idx; ++i)
     
    16411952          p = xstrdup (eval_strings->list[i]);
    16421953          len += 2 * strlen (p);
    1643           eval_buffer (p);
     1954          eval_buffer (p, NULL);
    16441955          free (p);
    16451956        }
     
    16491960        {
    16501961          strcpy (p, "--eval=");
    1651           p += strlen (p);
     1962          p += CSTRLEN ("--eval=");
    16521963          p = quote_for_env (p, eval_strings->list[i]);
    16531964          *(p++) = ' ';
     
    16601971  /* Read all the makefiles.  */
    16611972
    1662   read_makefiles
    1663     = read_all_makefiles (makefiles == 0 ? 0 : makefiles->list);
     1973  read_files = read_all_makefiles (makefiles == 0 ? 0 : makefiles->list);
    16641974
    16651975#ifdef WINDOWS32
    16661976  /* look one last time after reading all Makefiles */
    16671977  if (no_default_sh_exe)
    1668     no_default_sh_exe = !find_and_set_default_shell(NULL);
     1978    no_default_sh_exe = !find_and_set_default_shell (NULL);
    16691979#endif /* WINDOWS32 */
    16701980
    1671 #if defined (__MSDOS__) || defined (__EMX__)
     1981#if defined (__MSDOS__) || defined (__EMX__) || defined (VMS)
    16721982  /* We need to know what kind of shell we will be using.  */
    16731983  {
     
    16751985    struct variable *shv = lookup_variable (STRING_SIZE_TUPLE ("SHELL"));
    16761986    extern int unixy_shell;
    1677     extern char *default_shell;
     1987    extern const char *default_shell;
    16781988
    16791989    if (shv && *shv->value)
    16801990      {
    1681         char *shell_path = recursively_expand(shv);
    1682 
    1683         if (shell_path && _is_unixy_shell (shell_path))
    1684           unixy_shell = 1;
    1685         else
    1686           unixy_shell = 0;
    1687         if (shell_path)
    1688           default_shell = shell_path;
     1991        char *shell_path = recursively_expand (shv);
     1992
     1993        if (shell_path && _is_unixy_shell (shell_path))
     1994          unixy_shell = 1;
     1995        else
     1996          unixy_shell = 0;
     1997        if (shell_path)
     1998          default_shell = shell_path;
    16891999      }
    16902000  }
    16912001#endif /* __MSDOS__ || __EMX__ */
    16922002
    1693   /* Decode switches again, in case the variables were set by the makefile.  */
    1694   decode_env_switches (STRING_SIZE_TUPLE ("MAKEFLAGS"));
     2003  {
     2004    int old_builtin_rules_flag = no_builtin_rules_flag;
     2005    int old_builtin_variables_flag = no_builtin_variables_flag;
     2006
     2007    /* Decode switches again, for variables set by the makefile.  */
     2008    decode_env_switches (STRING_SIZE_TUPLE ("GNUMAKEFLAGS"));
     2009
     2010    /* Clear GNUMAKEFLAGS to avoid duplication.  */
     2011    define_variable_cname ("GNUMAKEFLAGS", "", o_override, 0);
     2012
     2013    decode_env_switches (STRING_SIZE_TUPLE ("MAKEFLAGS"));
    16952014#if 0
    1696   decode_env_switches (STRING_SIZE_TUPLE ("MFLAGS"));
    1697 #endif
    1698 
    1699 #if defined (__MSDOS__) || defined (__EMX__)
    1700   if (job_slots != 1
     2015    decode_env_switches (STRING_SIZE_TUPLE ("MFLAGS"));
     2016#endif
     2017
     2018    /* Reset in case the switches changed our mind.  */
     2019    syncing = (output_sync == OUTPUT_SYNC_LINE
     2020               || output_sync == OUTPUT_SYNC_TARGET);
     2021
     2022    if (make_sync.syncout && ! syncing)
     2023      output_close (&make_sync);
     2024
     2025    make_sync.syncout = syncing;
     2026    OUTPUT_SET (&make_sync);
     2027
     2028    /* If we've disabled builtin rules, get rid of them.  */
     2029    if (no_builtin_rules_flag && ! old_builtin_rules_flag)
     2030      {
     2031        if (suffix_file->builtin)
     2032          {
     2033            free_dep_chain (suffix_file->deps);
     2034            suffix_file->deps = 0;
     2035          }
     2036        define_variable_cname ("SUFFIXES", "", o_default, 0);
     2037      }
     2038
     2039    /* If we've disabled builtin variables, get rid of them.  */
     2040    if (no_builtin_variables_flag && ! old_builtin_variables_flag)
     2041      undefine_default_variables ();
     2042  }
     2043
     2044#if defined (__MSDOS__) || defined (__EMX__) || defined (VMS)
     2045  if (arg_job_slots != 1
    17012046# ifdef __EMX__
    17022047      && _osmode != OS2_MODE /* turn off -j if we are in DOS mode */
     
    17042049      )
    17052050    {
    1706       error (NILF,
    1707              _("Parallel jobs (-j) are not supported on this platform."));
    1708       error (NILF, _("Resetting to single job (-j1) mode."));
    1709       job_slots = 1;
    1710     }
    1711 #endif
    1712 
    1713 #ifdef MAKE_JOBSERVER
    1714   /* If the jobserver-fds option is seen, make sure that -j is reasonable.  */
    1715 
    1716   if (jobserver_fds)
    1717     {
    1718       const char *cp;
    1719       unsigned int ui;
    1720 
    1721       for (ui=1; ui < jobserver_fds->idx; ++ui)
    1722         if (!streq (jobserver_fds->list[0], jobserver_fds->list[ui]))
    1723           fatal (NILF, _("internal error: multiple --jobserver-fds options"));
    1724 
    1725       /* Now parse the fds string and make sure it has the proper format.  */
    1726 
    1727       cp = jobserver_fds->list[0];
    1728 
    1729       if (sscanf (cp, "%d,%d", &job_fds[0], &job_fds[1]) != 2)
    1730         fatal (NILF,
    1731                _("internal error: invalid --jobserver-fds string `%s'"), cp);
    1732 
    1733       DB (DB_JOBS,
    1734           (_("Jobserver client (fds %d,%d)\n"), job_fds[0], job_fds[1]));
    1735 
    1736       /* The combination of a pipe + !job_slots means we're using the
    1737          jobserver.  If !job_slots and we don't have a pipe, we can start
    1738          infinite jobs.  If we see both a pipe and job_slots >0 that means the
    1739          user set -j explicitly.  This is broken; in this case obey the user
    1740          (ignore the jobserver pipe for this make) but print a message.  */
    1741 
    1742       if (job_slots > 0)
    1743         error (NILF,
    1744                _("warning: -jN forced in submake: disabling jobserver mode."));
    1745 
    1746       /* Create a duplicate pipe, that will be closed in the SIGCHLD
    1747          handler.  If this fails with EBADF, the parent has closed the pipe
    1748          on us because it didn't think we were a submake.  If so, print a
    1749          warning then default to -j1.  */
    1750 
    1751       else if ((job_rfd = dup (job_fds[0])) < 0)
     2051      O (error, NILF,
     2052         _("Parallel jobs (-j) are not supported on this platform."));
     2053      O (error, NILF, _("Resetting to single job (-j1) mode."));
     2054      arg_job_slots = job_slots = 1;
     2055    }
     2056#endif
     2057
     2058  /* If we have >1 slot at this point, then we're a top-level make.
     2059     Set up the jobserver.
     2060
     2061     Every make assumes that it always has one job it can run.  For the
     2062     submakes it's the token they were given by their parent.  For the top
     2063     make, we just subtract one from the number the user wants.  */
     2064
     2065  if (job_slots > 1 && jobserver_setup (job_slots - 1))
     2066    {
     2067      /* Fill in the jobserver_auth for our children.  */
     2068      jobserver_auth = jobserver_get_auth ();
     2069
     2070      if (jobserver_auth)
    17522071        {
    1753           if (errno != EBADF)
    1754             pfatal_with_name (_("dup jobserver"));
    1755 
    1756           error (NILF,
    1757                  _("warning: jobserver unavailable: using -j1.  Add `+' to parent make rule."));
    1758           job_slots = 1;
     2072          /* We're using the jobserver so set job_slots to 0.  */
     2073          master_job_slots = job_slots;
     2074          job_slots = 0;
    17592075        }
    1760 
    1761       if (job_slots > 0)
    1762         {
    1763           close (job_fds[0]);
    1764           close (job_fds[1]);
    1765           job_fds[0] = job_fds[1] = -1;
    1766           free (jobserver_fds->list);
    1767           free (jobserver_fds);
    1768           jobserver_fds = 0;
    1769         }
    1770     }
    1771 
    1772   /* If we have >1 slot but no jobserver-fds, then we're a top-level make.
    1773      Set up the pipe and install the fds option for our children.  */
    1774 
    1775   if (job_slots > 1)
    1776     {
    1777       char *cp;
    1778       char c = '+';
    1779 
    1780       if (pipe (job_fds) < 0 || (job_rfd = dup (job_fds[0])) < 0)
    1781         pfatal_with_name (_("creating jobs pipe"));
    1782 
    1783       /* Every make assumes that it always has one job it can run.  For the
    1784          submakes it's the token they were given by their parent.  For the
    1785          top make, we just subtract one from the number the user wants.  We
    1786          want job_slots to be 0 to indicate we're using the jobserver.  */
    1787 
    1788       master_job_slots = job_slots;
    1789 
    1790       while (--job_slots)
    1791         {
    1792           int r;
    1793 
    1794           EINTRLOOP (r, write (job_fds[1], &c, 1));
    1795           if (r != 1)
    1796             pfatal_with_name (_("init jobserver pipe"));
    1797         }
    1798 
    1799       /* Fill in the jobserver_fds struct for our children.  */
    1800 
    1801       cp = xmalloc ((sizeof ("1024")*2)+1);
    1802       sprintf (cp, "%d,%d", job_fds[0], job_fds[1]);
    1803 
    1804       jobserver_fds = (struct stringlist *)
    1805                         xmalloc (sizeof (struct stringlist));
    1806       jobserver_fds->list = xmalloc (sizeof (char *));
    1807       jobserver_fds->list[0] = cp;
    1808       jobserver_fds->idx = 1;
    1809       jobserver_fds->max = 1;
    1810     }
    1811 #endif
     2076    }
     2077
     2078  /* If we're not using parallel jobs, then we don't need output sync.
     2079     This is so people can enable output sync in GNUMAKEFLAGS or similar, but
     2080     not have it take effect unless parallel builds are enabled.  */
     2081  if (syncing && job_slots == 1)
     2082    {
     2083      OUTPUT_UNSET ();
     2084      output_close (&make_sync);
     2085      syncing = 0;
     2086      output_sync = OUTPUT_SYNC_NONE;
     2087    }
    18122088
    18132089#ifndef MAKE_SYMLINKS
    18142090  if (check_symlink_flag)
    18152091    {
    1816       error (NILF, _("Symbolic links not supported: disabling -L."));
     2092      O (error, NILF, _("Symbolic links not supported: disabling -L."));
    18172093      check_symlink_flag = 0;
    18182094    }
     
    18232099  define_makeflags (1, 0);
    18242100
    1825   /* Make each `struct dep' point at the `struct file' for the file
     2101  /* Make each 'struct goaldep' point at the 'struct file' for the file
    18262102     depended on.  Also do magic for special targets.  */
    18272103
     
    18622138          f->last_mtime = f->mtime_before_update = OLD_MTIME;
    18632139          f->updated = 1;
    1864           f->update_status = 0;
     2140          f->update_status = us_success;
    18652141          f->command_state = cs_finished;
    18662142        }
     
    18712147      const char **p;
    18722148      for (p = new_files->list; *p != 0; ++p)
    1873         {
    1874           struct file *f = enter_file (*p);
    1875           f->last_mtime = f->mtime_before_update = NEW_MTIME;
    1876         }
     2149        {
     2150          struct file *f = enter_file (*p);
     2151          f->last_mtime = f->mtime_before_update = NEW_MTIME;
     2152        }
    18772153    }
    18782154
     
    18802156  remote_setup ();
    18812157
    1882   if (read_makefiles != 0)
     2158  /* Dump any output we've collected.  */
     2159
     2160  OUTPUT_UNSET ();
     2161  output_close (&make_sync);
     2162
     2163  if (read_files != 0)
    18832164    {
    18842165      /* Update any makefiles if necessary.  */
     
    18862167      FILE_TIMESTAMP *makefile_mtimes = 0;
    18872168      unsigned int mm_idx = 0;
    1888       char **nargv;
     2169      char **aargv = NULL;
     2170      const char **nargv;
    18892171      int nargc;
    1890       int orig_db_level = db_level;
    1891       int status;
    1892 
    1893       if (! ISDB (DB_MAKEFILES))
    1894         db_level = DB_NONE;
     2172      enum update_status status;
    18952173
    18962174      DB (DB_BASIC, (_("Updating makefiles....\n")));
    18972175
    18982176      /* Remove any makefiles we don't want to try to update.
    1899         Also record the current modtimes so we can compare them later.  */
     2177        Also record the current modtimes so we can compare them later.  */
    19002178      {
    1901         register struct dep *d, *last;
    1902         last = 0;
    1903         d = read_makefiles;
    1904         while (d != 0)
    1905           {
    1906             struct file *f = d->file;
    1907             if (f->double_colon)
    1908               for (f = f->double_colon; f != NULL; f = f->prev)
    1909                 {
    1910                   if (f->deps == 0 && f->cmds != 0)
    1911                     {
    1912                       /* This makefile is a :: target with commands, but
    1913                         no dependencies.  So, it will always be remade.
    1914                         This might well cause an infinite loop, so don't
    1915                         try to remake it.  (This will only happen if
    1916                         your makefiles are written exceptionally
    1917                         stupidly; but if you work for Athena, that's how
    1918                         you write your makefiles.)  */
    1919 
    1920                       DB (DB_VERBOSE,
    1921                           (_("Makefile `%s' might loop; not remaking it.\n"),
     2179        register struct goaldep *d, *last;
     2180        last = 0;
     2181        d = read_files;
     2182        while (d != 0)
     2183          {
     2184            struct file *f = d->file;
     2185            if (f->double_colon)
     2186              for (f = f->double_colon; f != NULL; f = f->prev)
     2187                {
     2188                  if (f->deps == 0 && f->cmds != 0)
     2189                    {
     2190                      /* This makefile is a :: target with commands, but
     2191                        no dependencies.  So, it will always be remade.
     2192                        This might well cause an infinite loop, so don't
     2193                        try to remake it.  (This will only happen if
     2194                        your makefiles are written exceptionally
     2195                        stupidly; but if you work for Athena, that's how
     2196                        you write your makefiles.)  */
     2197
     2198                      DB (DB_VERBOSE,
     2199                          (_("Makefile '%s' might loop; not remaking it.\n"),
    19222200                           f->name));
    19232201
    1924                       if (last == 0)
    1925                         read_makefiles = d->next;
    1926                       else
    1927                         last->next = d->next;
    1928 
    1929                       /* Free the storage.  */
    1930                       free_dep (d);
    1931 
    1932                       d = last == 0 ? read_makefiles : last->next;
    1933 
    1934                       break;
    1935                     }
    1936                 }
    1937             if (f == NULL || !f->double_colon)
    1938               {
     2202                      if (last == 0)
     2203                        read_files = d->next;
     2204                      else
     2205                        last->next = d->next;
     2206
     2207                      /* Free the storage.  */
     2208                      free_goaldep (d);
     2209
     2210                      d = last == 0 ? read_files : last->next;
     2211
     2212                      break;
     2213                    }
     2214                }
     2215
     2216            if (f == NULL || !f->double_colon)
     2217              {
    19392218                makefile_mtimes = xrealloc (makefile_mtimes,
    19402219                                            (mm_idx+1)
    19412220                                            * sizeof (FILE_TIMESTAMP));
    1942                 makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
    1943                 last = d;
    1944                 d = d->next;
    1945               }
    1946           }
     2221                makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
     2222                last = d;
     2223                d = d->next;
     2224              }
     2225          }
    19472226      }
    19482227
    1949       /* Set up `MAKEFLAGS' specially while remaking makefiles.  */
     2228      /* Set up 'MAKEFLAGS' specially while remaking makefiles.  */
    19502229      define_makeflags (1, 1);
    19512230
    1952       rebuilding_makefiles = 1;
    1953       status = update_goal_chain (read_makefiles);
    1954       rebuilding_makefiles = 0;
     2231      {
     2232        int orig_db_level = db_level;
     2233
     2234        if (! ISDB (DB_MAKEFILES))
     2235          db_level = DB_NONE;
     2236
     2237        rebuilding_makefiles = 1;
     2238        status = update_goal_chain (read_files);
     2239        rebuilding_makefiles = 0;
     2240
     2241        db_level = orig_db_level;
     2242      }
    19552243
    19562244      switch (status)
    1957         {
    1958         case 1:
     2245        {
     2246        case us_question:
    19592247          /* The only way this can happen is if the user specified -q and asked
    1960            * for one of the makefiles to be remade as a target on the command
    1961            * line.  Since we're not actually updating anything with -q we can
    1962            * treat this as "did nothing".
    1963            */
    1964 
    1965         case -1:
    1966           /* Did nothing.  */
    1967           break;
    1968 
    1969         case 2:
    1970           /* Failed to update.  Figure out if we care.  */
    1971           {
    1972             /* Nonzero if any makefile was successfully remade.  */
    1973             int any_remade = 0;
    1974             /* Nonzero if any makefile we care about failed
    1975                in updating or could not be found at all.  */
    1976             int any_failed = 0;
    1977             unsigned int i;
    1978             struct dep *d;
    1979 
    1980             for (i = 0, d = read_makefiles; d != 0; ++i, d = d->next)
     2248             for one of the makefiles to be remade as a target on the command
     2249             line.  Since we're not actually updating anything with -q we can
     2250             treat this as "did nothing".  */
     2251
     2252        case us_none:
     2253          /* Did nothing.  */
     2254          break;
     2255
     2256        case us_failed:
     2257          /* Failed to update.  Figure out if we care.  */
     2258          {
     2259            /* Nonzero if any makefile was successfully remade.  */
     2260            int any_remade = 0;
     2261            /* Nonzero if any makefile we care about failed
     2262               in updating or could not be found at all.  */
     2263            int any_failed = 0;
     2264            unsigned int i;
     2265            struct goaldep *d;
     2266
     2267            for (i = 0, d = read_files; d != 0; ++i, d = d->next)
    19812268              {
    1982                 /* Reset the considered flag; we may need to look at the file
    1983                    again to print an error.  */
    1984                 d->file->considered = 0;
    1985 
    19862269                if (d->file->updated)
    19872270                  {
    19882271                    /* This makefile was updated.  */
    1989                     if (d->file->update_status == 0)
     2272                    if (d->file->update_status == us_success)
    19902273                      {
    19912274                        /* It was successfully updated.  */
     
    19932276                                       != makefile_mtimes[i]);
    19942277                      }
    1995                     else if (! (d->changed & RM_DONTCARE))
     2278                    else if (! (d->flags & RM_DONTCARE))
    19962279                      {
    19972280                        FILE_TIMESTAMP mtime;
    19982281                        /* The update failed and this makefile was not
    19992282                           from the MAKEFILES variable, so we care.  */
    2000                         error (NILF, _("Failed to remake makefile `%s'."),
    2001                                d->file->name);
     2283                        OS (error, NILF, _("Failed to remake makefile '%s'."),
     2284                            d->file->name);
    20022285                        mtime = file_mtime_no_search (d->file);
    20032286                        any_remade |= (mtime != NONEXISTENT_MTIME
     
    20082291                else
    20092292                  /* This makefile was not found at all.  */
    2010                   if (! (d->changed & RM_DONTCARE))
     2293                  if (! (d->flags & RM_DONTCARE))
    20112294                    {
     2295                      const char *dnm = dep_name (d);
     2296                      size_t l = strlen (dnm);
     2297
    20122298                      /* This is a makefile we care about.  See how much.  */
    2013                       if (d->changed & RM_INCLUDED)
    2014                         /* An included makefile.  We don't need
    2015                            to die, but we do want to complain.  */
    2016                         error (NILF,
    2017                                _("Included makefile `%s' was not found."),
    2018                                dep_name (d));
     2299                      if (d->flags & RM_INCLUDED)
     2300                        /* An included makefile.  We don't need to die, but we
     2301                           do want to complain.  */
     2302                        error (NILF, l,
     2303                               _("Included makefile '%s' was not found."), dnm);
    20192304                      else
    20202305                        {
    20212306                          /* A normal makefile.  We must die later.  */
    2022                           error (NILF, _("Makefile `%s' was not found"),
    2023                                  dep_name (d));
     2307                          error (NILF, l,
     2308                                 _("Makefile '%s' was not found"), dnm);
    20242309                          any_failed = 1;
    20252310                        }
     
    20272312              }
    20282313            /* Reset this to empty so we get the right error message below.  */
    2029             read_makefiles = 0;
    2030 
    2031             if (any_remade)
    2032               goto re_exec;
    2033             if (any_failed)
    2034               die (2);
     2314            read_files = 0;
     2315
     2316            if (any_remade)
     2317              goto re_exec;
     2318            if (any_failed)
     2319              die (MAKE_FAILURE);
    20352320            break;
    2036           }
    2037 
    2038         case 0:
    2039         re_exec:
    2040           /* Updated successfully.  Re-exec ourselves.  */
    2041 
    2042           remove_intermediates (0);
    2043 
    2044           if (print_data_base_flag)
    2045             print_data_base ();
    2046 
    2047           log_working_directory (0);
     2321          }
     2322
     2323        case us_success:
     2324        re_exec:
     2325          /* Updated successfully.  Re-exec ourselves.  */
     2326
     2327          remove_intermediates (0);
     2328
     2329          if (print_data_base_flag)
     2330            print_data_base ();
    20482331
    20492332          clean_jobserver (0);
    20502333
    2051           if (makefiles != 0)
    2052             {
    2053               /* These names might have changed.  */
    2054               int i, j = 0;
    2055               for (i = 1; i < argc; ++i)
    2056                 if (strneq (argv[i], "-f", 2)) /* XXX */
    2057                   {
    2058                     if (argv[i][2] == '\0')
     2334          if (makefiles != 0)
     2335            {
     2336              /* These names might have changed.  */
     2337              int i, j = 0;
     2338              for (i = 1; i < argc; ++i)
     2339                if (strneq (argv[i], "-f", 2)) /* XXX */
     2340                  {
     2341                    if (argv[i][2] == '\0')
    20592342                      /* This cast is OK since we never modify argv.  */
    2060                       argv[++i] = (char *) makefiles->list[j];
    2061                     else
    2062                       argv[i] = xstrdup (concat (2, "-f", makefiles->list[j]));
    2063                     ++j;
    2064                   }
    2065             }
     2343                      argv[++i] = (char *) makefiles->list[j];
     2344                    else
     2345                      argv[i] = xstrdup (concat (2, "-f", makefiles->list[j]));
     2346                    ++j;
     2347                  }
     2348            }
    20662349
    20672350          /* Add -o option for the stdin temporary file, if necessary.  */
     
    20692352          if (stdin_nm)
    20702353            {
    2071               nargv = xmalloc ((nargc + 2) * sizeof (char *));
    2072               memcpy (nargv, argv, argc * sizeof (char *));
    2073               nargv[nargc++] = xstrdup (concat (2, "-o", stdin_nm));
    2074               nargv[nargc] = 0;
     2354              void *m = xmalloc ((nargc + 2) * sizeof (char *));
     2355              aargv = m;
     2356              memcpy (aargv, argv, argc * sizeof (char *));
     2357              aargv[nargc++] = xstrdup (concat (2, "-o", stdin_nm));
     2358              aargv[nargc] = 0;
     2359              nargv = m;
    20752360            }
    20762361          else
    2077             nargv = argv;
    2078 
    2079           if (directories != 0 && directories->idx > 0)
    2080             {
    2081               int bad = 1;
    2082               if (directory_before_chdir != 0)
    2083                 {
    2084                   if (chdir (directory_before_chdir) < 0)
    2085                       perror_with_name ("chdir", "");
    2086                   else
    2087                     bad = 0;
    2088                 }
    2089               if (bad)
    2090                 fatal (NILF, _("Couldn't change back to original directory."));
    2091             }
     2362            nargv = (const char**)argv;
     2363
     2364          if (directories != 0 && directories->idx > 0)
     2365            {
     2366              int bad = 1;
     2367              if (directory_before_chdir != 0)
     2368                {
     2369                  if (chdir (directory_before_chdir) < 0)
     2370                      perror_with_name ("chdir", "");
     2371                  else
     2372                    bad = 0;
     2373                }
     2374              if (bad)
     2375                O (fatal, NILF,
     2376                   _("Couldn't change back to original directory."));
     2377            }
    20922378
    20932379          ++restarts;
    20942380
    2095           /* Reset makeflags in case they were changed.  */
    2096           {
    2097             const char *pv = define_makeflags (1, 1);
    2098             char *p = alloca (sizeof ("MAKEFLAGS=") + strlen (pv) + 1);
    2099             sprintf (p, "MAKEFLAGS=%s", pv);
    2100             putenv (p);
    2101           }
    2102 
    2103           if (ISDB (DB_BASIC))
    2104             {
    2105               char **p;
    2106               printf (_("Re-executing[%u]:"), restarts);
    2107               for (p = nargv; *p != 0; ++p)
    2108                 printf (" %s", *p);
    2109               putchar ('\n');
    2110             }
     2381          if (ISDB (DB_BASIC))
     2382            {
     2383              const char **p;
     2384              printf (_("Re-executing[%u]:"), restarts);
     2385              for (p = nargv; *p != 0; ++p)
     2386                printf (" %s", *p);
     2387              putchar ('\n');
     2388              fflush (stdout);
     2389            }
    21112390
    21122391#ifndef _AMIGA
     
    21152394            for (p = environ; *p != 0; ++p)
    21162395              {
    2117                 if (strneq (*p, MAKELEVEL_NAME, MAKELEVEL_LENGTH)
    2118                     && (*p)[MAKELEVEL_LENGTH] == '=')
     2396                if (strneq (*p, MAKELEVEL_NAME "=", MAKELEVEL_LENGTH+1))
    21192397                  {
    21202398                    *p = alloca (40);
    21212399                    sprintf (*p, "%s=%u", MAKELEVEL_NAME, makelevel);
     2400#ifdef VMS
     2401                    vms_putenv_symbol (*p);
     2402#endif
    21222403                  }
    2123                 if (strneq (*p, "MAKE_RESTARTS=", 14))
     2404                else if (strneq (*p, "MAKE_RESTARTS=", CSTRLEN ("MAKE_RESTARTS=")))
    21242405                  {
    21252406                    *p = alloca (40);
    2126                     sprintf (*p, "MAKE_RESTARTS=%u", restarts);
     2407                    sprintf (*p, "MAKE_RESTARTS=%s%u",
     2408                             OUTPUT_IS_TRACED () ? "-" : "", restarts);
    21272409                    restarts = 0;
    21282410                  }
     
    21302412          }
    21312413#else /* AMIGA */
    2132           {
    2133             char buffer[256];
     2414          {
     2415            char buffer[256];
    21342416
    21352417            sprintf (buffer, "%u", makelevel);
    21362418            SetVar (MAKELEVEL_NAME, buffer, -1, GVF_GLOBAL_ONLY);
    21372419
    2138             sprintf (buffer, "%u", restarts);
     2420            sprintf (buffer, "%s%u", OUTPUT_IS_TRACED () ? "-" : "", restarts);
    21392421            SetVar ("MAKE_RESTARTS", buffer, -1, GVF_GLOBAL_ONLY);
    21402422            restarts = 0;
    2141           }
     2423          }
    21422424#endif
    21432425
     
    21462428            {
    21472429              char *b = alloca (40);
    2148               sprintf (b, "MAKE_RESTARTS=%u", restarts);
     2430              sprintf (b, "MAKE_RESTARTS=%s%u",
     2431                       OUTPUT_IS_TRACED () ? "-" : "", restarts);
    21492432              putenv (b);
    21502433            }
    21512434
    2152           fflush (stdout);
    2153           fflush (stderr);
    2154 
    2155           /* Close the dup'd jobserver pipe if we opened one.  */
    2156           if (job_rfd >= 0)
    2157             close (job_rfd);
     2435          fflush (stdout);
     2436          fflush (stderr);
    21582437
    21592438#ifdef _AMIGA
    2160           exec_command (nargv);
    2161           exit (0);
     2439          exec_command (nargv);
     2440          exit (0);
    21622441#elif defined (__EMX__)
    2163           {
    2164             /* It is not possible to use execve() here because this
    2165                would cause the parent process to be terminated with
    2166                exit code 0 before the child process has been terminated.
    2167                Therefore it may be the best solution simply to spawn the
    2168                child process including all file handles and to wait for its
    2169                termination. */
    2170             int pid;
    2171             int status;
    2172             pid = child_execute_job (0, 1, nargv, environ);
    2173 
    2174             /* is this loop really necessary? */
    2175             do {
    2176               pid = wait (&status);
    2177             } while (pid <= 0);
    2178             /* use the exit code of the child process */
    2179             exit (WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE);
    2180           }
     2442          {
     2443            /* It is not possible to use execve() here because this
     2444               would cause the parent process to be terminated with
     2445               exit code 0 before the child process has been terminated.
     2446               Therefore it may be the best solution simply to spawn the
     2447               child process including all file handles and to wait for its
     2448               termination. */
     2449            int pid;
     2450            int r;
     2451            pid = child_execute_job (NULL, 1, nargv, environ);
     2452
     2453            /* is this loop really necessary? */
     2454            do {
     2455              pid = wait (&r);
     2456            } while (pid <= 0);
     2457            /* use the exit code of the child process */
     2458            exit (WIFEXITED(r) ? WEXITSTATUS(r) : EXIT_FAILURE);
     2459          }
    21812460#else
    2182           exec_command (nargv, environ);
    2183 #endif
    2184           /* NOTREACHED */
    2185 
    2186         default:
    2187 #define BOGUS_UPDATE_STATUS 0
    2188           assert (BOGUS_UPDATE_STATUS);
    2189           break;
    2190         }
    2191 
    2192       db_level = orig_db_level;
    2193 
    2194       /* Free the makefile mtimes (if we allocated any).  */
    2195       if (makefile_mtimes)
    2196         free (makefile_mtimes);
    2197     }
    2198 
    2199   /* Set up `MAKEFLAGS' again for the normal targets.  */
     2461#ifdef SET_STACK_SIZE
     2462          /* Reset limits, if necessary.  */
     2463          if (stack_limit.rlim_cur)
     2464            setrlimit (RLIMIT_STACK, &stack_limit);
     2465#endif
     2466          exec_command ((char **)nargv, environ);
     2467#endif
     2468          free (aargv);
     2469          break;
     2470        }
     2471
     2472      /* Free the makefile mtimes.  */
     2473      free (makefile_mtimes);
     2474    }
     2475
     2476  /* Set up 'MAKEFLAGS' again for the normal targets.  */
    22002477  define_makeflags (1, 0);
    22012478
     
    22082485      const char **p;
    22092486      for (p = new_files->list; *p != 0; ++p)
    2210         {
    2211           struct file *f = enter_file (*p);
    2212           f->last_mtime = f->mtime_before_update = NEW_MTIME;
    2213         }
     2487        {
     2488          struct file *f = enter_file (*p);
     2489          f->last_mtime = f->mtime_before_update = NEW_MTIME;
     2490        }
    22142491    }
    22152492
     
    22442521              struct nameseq *ns;
    22452522
    2246               ns = PARSE_FILE_SEQ (&p, struct nameseq, '\0', NULL, 0);
     2523              ns = PARSE_SIMPLE_SEQ (&p, struct nameseq);
    22472524              if (ns)
    22482525                {
    22492526                  /* .DEFAULT_GOAL should contain one target. */
    22502527                  if (ns->next != 0)
    2251                     fatal (NILF, _(".DEFAULT_GOAL contains more than one target"));
     2528                    O (fatal, NILF,
     2529                       _(".DEFAULT_GOAL contains more than one target"));
    22522530
    22532531                  f = enter_file (strcache_add (ns->name));
     
    22602538          if (f)
    22612539            {
    2262               goals = alloc_dep ();
     2540              goals = alloc_goaldep ();
    22632541              goals->file = f;
    22642542            }
     
    22712549  if (!goals)
    22722550    {
    2273       if (read_makefiles == 0)
    2274         fatal (NILF, _("No targets specified and no makefile found"));
    2275 
    2276       fatal (NILF, _("No targets"));
     2551      if (read_files == 0)
     2552        O (fatal, NILF, _("No targets specified and no makefile found"));
     2553
     2554      O (fatal, NILF, _("No targets"));
    22772555    }
    22782556
     
    22822560
    22832561  {
    2284     int status;
    2285 
    22862562    switch (update_goal_chain (goals))
    22872563    {
    2288       case -1:
     2564      case us_none:
    22892565        /* Nothing happened.  */
    2290       case 0:
    2291         /* Updated successfully.  */
    2292         status = makefile_status;
     2566        /* FALLTHROUGH */
     2567      case us_success:
     2568        /* Keep the previous result.  */
    22932569        break;
    2294       case 1:
     2570      case us_question:
    22952571        /* We are under -q and would run some commands.  */
    2296         status = MAKE_TROUBLE;
     2572        makefile_status = MAKE_TROUBLE;
    22972573        break;
    2298       case 2:
    2299         /* Updating failed.  POSIX.2 specifies exit status >1 for this;
    2300            but in VMS, there is only success and failure.  */
    2301         status = MAKE_FAILURE;
     2574      case us_failed:
     2575        /* Updating failed.  POSIX.2 specifies exit status >1 for this; */
     2576        makefile_status = MAKE_FAILURE;
    23022577        break;
    2303       default:
    2304         abort ();
    23052578    }
    23062579
    23072580    /* If we detected some clock skew, generate one last warning */
    23082581    if (clock_skew_detected)
    2309       error (NILF,
    2310              _("warning:  Clock skew detected.  Your build may be incomplete."));
     2582      O (error, NILF,
     2583         _("warning:  Clock skew detected.  Your build may be incomplete."));
    23112584
    23122585    /* Exit.  */
    2313     die (status);
     2586    die (makefile_status);
    23142587  }
    23152588
    23162589  /* NOTREACHED */
    2317   return 0;
     2590  exit (MAKE_SUCCESS);
    23182591}
    23192592
     
    23232596static char options[1 + sizeof (switches) / sizeof (switches[0]) * 3];
    23242597static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
    2325                                   (sizeof (long_option_aliases) /
    2326                                    sizeof (long_option_aliases[0]))];
     2598                                  (sizeof (long_option_aliases) /
     2599                                   sizeof (long_option_aliases[0]))];
    23272600
    23282601/* Fill in the string and vector for getopt.  */
     
    23472620    {
    23482621      long_options[i].name = (switches[i].long_name == 0 ? "" :
    2349                               switches[i].long_name);
     2622                              switches[i].long_name);
    23502623      long_options[i].flag = 0;
    23512624      long_options[i].val = switches[i].c;
    23522625      if (short_option (switches[i].c))
    2353         *p++ = switches[i].c;
     2626        *p++ = switches[i].c;
    23542627      switch (switches[i].type)
    2355         {
    2356         case flag:
    2357         case flag_off:
    2358         case ignore:
    2359           long_options[i].has_arg = no_argument;
    2360           break;
    2361 
    2362         case string:
     2628        {
     2629        case flag:
     2630        case flag_off:
     2631        case ignore:
     2632          long_options[i].has_arg = no_argument;
     2633          break;
     2634
     2635        case string:
     2636        case strlist:
    23632637        case filename:
    2364         case positive_int:
    2365         case floating:
    2366           if (short_option (switches[i].c))
    2367             *p++ = ':';
    2368           if (switches[i].noarg_value != 0)
    2369             {
    2370               if (short_option (switches[i].c))
    2371                 *p++ = ':';
    2372               long_options[i].has_arg = optional_argument;
    2373             }
    2374           else
    2375             long_options[i].has_arg = required_argument;
    2376           break;
    2377         }
     2638        case positive_int:
     2639        case floating:
     2640          if (short_option (switches[i].c))
     2641            *p++ = ':';
     2642          if (switches[i].noarg_value != 0)
     2643            {
     2644              if (short_option (switches[i].c))
     2645                *p++ = ':';
     2646              long_options[i].has_arg = optional_argument;
     2647            }
     2648          else
     2649            long_options[i].has_arg = required_argument;
     2650          break;
     2651        }
    23782652    }
    23792653  *p = '\0';
    23802654  for (c = 0; c < (sizeof (long_option_aliases) /
    2381                    sizeof (long_option_aliases[0]));
     2655                   sizeof (long_option_aliases[0]));
    23822656       ++c)
    23832657    long_options[i++] = long_option_aliases[c];
     
    23852659}
    23862660
     2661
     2662/* Non-option argument.  It might be a variable definition.  */
    23872663static void
    2388 handle_non_switch_argument (char *arg, int env)
     2664handle_non_switch_argument (const char *arg, int env)
    23892665{
    2390   /* Non-option argument.  It might be a variable definition.  */
    23912666  struct variable *v;
     2667
    23922668  if (arg[0] == '-' && arg[1] == '\0')
    2393     /* Ignore plain `-' for compatibility.  */
     2669    /* Ignore plain '-' for compatibility.  */
    23942670    return;
     2671
     2672#ifdef VMS
     2673  {
     2674    /* VMS DCL quoting can result in foo="bar baz" showing up here.
     2675       Need to remove the double quotes from the value. */
     2676    char * eq_ptr;
     2677    char * new_arg;
     2678    eq_ptr = strchr (arg, '=');
     2679    if ((eq_ptr != NULL) && (eq_ptr[1] == '"'))
     2680      {
     2681         int len;
     2682         int seg1;
     2683         int seg2;
     2684         len = strlen(arg);
     2685         new_arg = alloca(len);
     2686         seg1 = eq_ptr - arg + 1;
     2687         strncpy(new_arg, arg, (seg1));
     2688         seg2 = len - seg1 - 1;
     2689         strncpy(&new_arg[seg1], &eq_ptr[2], seg2);
     2690         new_arg[seg1 + seg2] = 0;
     2691         if (new_arg[seg1 + seg2 - 1] == '"')
     2692           new_arg[seg1 + seg2 - 1] = 0;
     2693         arg = new_arg;
     2694      }
     2695  }
     2696#endif
    23952697  v = try_variable_definition (0, arg, o_command, 0);
    23962698  if (v != 0)
    23972699    {
    23982700      /* It is indeed a variable definition.  If we don't already have this
    2399         one, record a pointer to the variable for later use in
    2400         define_makeflags.  */
     2701        one, record a pointer to the variable for later use in
     2702        define_makeflags.  */
    24012703      struct command_variable *cv;
    24022704
     
    24052707          break;
    24062708
    2407       if (! cv) {
    2408         cv = xmalloc (sizeof (*cv));
    2409         cv->variable = v;
    2410         cv->next = command_variables;
    2411         command_variables = cv;
    2412       }
     2709      if (! cv)
     2710        {
     2711          cv = xmalloc (sizeof (*cv));
     2712          cv->variable = v;
     2713          cv->next = command_variables;
     2714          command_variables = cv;
     2715        }
    24132716    }
    24142717  else if (! env)
    24152718    {
    24162719      /* Not an option or variable definition; it must be a goal
    2417         target!  Enter it as a file and add it to the dep chain of
    2418         goals.  */
     2720        target!  Enter it as a file and add it to the dep chain of
     2721        goals.  */
    24192722      struct file *f = enter_file (strcache_add (expand_command_line_file (arg)));
    24202723      f->cmd_target = 1;
    24212724
    24222725      if (goals == 0)
    2423         {
    2424           goals = alloc_dep ();
    2425           lastgoal = goals;
    2426         }
     2726        {
     2727          goals = alloc_goaldep ();
     2728          lastgoal = goals;
     2729        }
    24272730      else
    2428         {
    2429           lastgoal->next = alloc_dep ();
    2430           lastgoal = lastgoal->next;
    2431         }
     2731        {
     2732          lastgoal->next = alloc_goaldep ();
     2733          lastgoal = lastgoal->next;
     2734        }
    24322735
    24332736      lastgoal->file = f;
     
    24912794
    24922795static void
    2493 decode_switches (int argc, char **argv, int env)
     2796decode_switches (int argc, const char **argv, int env)
    24942797{
    24952798  int bad = 0;
     
    25112814  while (optind < argc)
    25122815    {
     2816      const char *coptarg;
     2817
    25132818      /* Parse the next argument.  */
    2514       c = getopt_long (argc, argv, options, long_options, (int *) 0);
     2819      c = getopt_long (argc, (char*const*)argv, options, long_options, NULL);
     2820      coptarg = optarg;
    25152821      if (c == EOF)
    2516         /* End of arguments, or "--" marker seen.  */
    2517         break;
     2822        /* End of arguments, or "--" marker seen.  */
     2823        break;
    25182824      else if (c == 1)
    2519         /* An argument not starting with a dash.  */
    2520         handle_non_switch_argument (optarg, env);
     2825        /* An argument not starting with a dash.  */
     2826        handle_non_switch_argument (coptarg, env);
    25212827      else if (c == '?')
    2522         /* Bad option.  We will print a usage message and die later.
    2523            But continue to parse the other options so the user can
    2524            see all he did wrong.  */
    2525         bad = 1;
     2828        /* Bad option.  We will print a usage message and die later.
     2829           But continue to parse the other options so the user can
     2830           see all he did wrong.  */
     2831        bad = 1;
    25262832      else
    2527         for (cs = switches; cs->c != '\0'; ++cs)
    2528           if (cs->c == c)
    2529             {
    2530               /* Whether or not we will actually do anything with
    2531                  this switch.  We test this individually inside the
    2532                  switch below rather than just once outside it, so that
    2533                  options which are to be ignored still consume args.  */
    2534               int doit = !env || cs->env;
    2535 
    2536               switch (cs->type)
    2537                 {
    2538                 default:
    2539                   abort ();
    2540 
    2541                 case ignore:
    2542                   break;
    2543 
    2544                 case flag:
    2545                 case flag_off:
    2546                   if (doit)
    2547                     *(int *) cs->value_ptr = cs->type == flag;
    2548                   break;
    2549 
    2550                 case string:
    2551                 case filename:
    2552                   if (!doit)
    2553                     break;
    2554 
    2555                   if (optarg == 0)
    2556                     optarg = xstrdup (cs->noarg_value);
    2557                   else if (*optarg == '\0')
     2833        for (cs = switches; cs->c != '\0'; ++cs)
     2834          if (cs->c == c)
     2835            {
     2836              /* Whether or not we will actually do anything with
     2837                 this switch.  We test this individually inside the
     2838                 switch below rather than just once outside it, so that
     2839                 options which are to be ignored still consume args.  */
     2840              int doit = !env || cs->env;
     2841
     2842              switch (cs->type)
     2843                {
     2844                default:
     2845                  abort ();
     2846
     2847                case ignore:
     2848                  break;
     2849
     2850                case flag:
     2851                case flag_off:
     2852                  if (doit)
     2853                    *(int *) cs->value_ptr = cs->type == flag;
     2854                  break;
     2855
     2856                case string:
     2857                case strlist:
     2858                case filename:
     2859                  if (!doit)
     2860                    break;
     2861
     2862                  if (! coptarg)
     2863                    coptarg = xstrdup (cs->noarg_value);
     2864                  else if (*coptarg == '\0')
    25582865                    {
    25592866                      char opt[2] = "c";
     
    25652872                        op = cs->long_name;
    25662873
    2567                       error (NILF, _("the `%s%s' option requires a non-empty string argument"),
     2874                      error (NILF, strlen (op),
     2875                             _("the '%s%s' option requires a non-empty string argument"),
    25682876                             short_option (cs->c) ? "-" : "--", op);
    25692877                      bad = 1;
     2878                      break;
    25702879                    }
    25712880
    2572                   sl = *(struct stringlist **) cs->value_ptr;
    2573                   if (sl == 0)
    2574                     {
    2575                       sl = (struct stringlist *)
    2576                         xmalloc (sizeof (struct stringlist));
    2577                       sl->max = 5;
    2578                       sl->idx = 0;
    2579                       sl->list = xmalloc (5 * sizeof (char *));
    2580                       *(struct stringlist **) cs->value_ptr = sl;
    2581                     }
    2582                   else if (sl->idx == sl->max - 1)
    2583                     {
    2584                       sl->max += 5;
     2881                  if (cs->type == string)
     2882                    {
     2883                      char **val = (char **)cs->value_ptr;
     2884                      free (*val);
     2885                      *val = xstrdup (coptarg);
     2886                      break;
     2887                    }
     2888
     2889                  sl = *(struct stringlist **) cs->value_ptr;
     2890                  if (sl == 0)
     2891                    {
     2892                      sl = xmalloc (sizeof (struct stringlist));
     2893                      sl->max = 5;
     2894                      sl->idx = 0;
     2895                      sl->list = xmalloc (5 * sizeof (char *));
     2896                      *(struct stringlist **) cs->value_ptr = sl;
     2897                    }
     2898                  else if (sl->idx == sl->max - 1)
     2899                    {
     2900                      sl->max += 5;
    25852901                      /* MSVC erroneously warns without a cast here.  */
    2586                       sl->list = xrealloc ((void *)sl->list,
     2902                      sl->list = xrealloc ((void *)sl->list,
    25872903                                           sl->max * sizeof (char *));
    2588                     }
     2904                    }
    25892905                  if (cs->type == filename)
    2590                     sl->list[sl->idx++] = expand_command_line_file (optarg);
     2906                    sl->list[sl->idx++] = expand_command_line_file (coptarg);
    25912907                  else
    2592                     sl->list[sl->idx++] = optarg;
    2593                   sl->list[sl->idx] = 0;
    2594                   break;
    2595 
    2596                 case positive_int:
     2908                    sl->list[sl->idx++] = xstrdup (coptarg);
     2909                  sl->list[sl->idx] = 0;
     2910                  break;
     2911
     2912                case positive_int:
    25972913                  /* See if we have an option argument; if we do require that
    25982914                     it's all digits, not something like "10foo".  */
    2599                   if (optarg == 0 && argc > optind)
     2915                  if (coptarg == 0 && argc > optind)
    26002916                    {
    26012917                      const char *cp;
     
    26032919                        ;
    26042920                      if (cp[0] == '\0')
    2605                         optarg = argv[optind++];
     2921                        coptarg = argv[optind++];
    26062922                    }
    26072923
    2608                   if (!doit)
    2609                     break;
    2610 
    2611                   if (optarg != 0)
    2612                     {
    2613                       int i = atoi (optarg);
     2924                  if (!doit)
     2925                    break;
     2926
     2927                  if (coptarg)
     2928                    {
     2929                      int i = atoi (coptarg);
    26142930                      const char *cp;
    26152931
    26162932                      /* Yes, I realize we're repeating this in some cases.  */
    2617                       for (cp = optarg; ISDIGIT (cp[0]); ++cp)
     2933                      for (cp = coptarg; ISDIGIT (cp[0]); ++cp)
    26182934                        ;
    26192935
    2620                       if (i < 1 || cp[0] != '\0')
    2621                         {
    2622                           error (NILF, _("the `-%c' option requires a positive integral argument"),
     2936                      if (i < 1 || cp[0] != '\0')
     2937                        {
     2938                          error (NILF, 0,
     2939                                 _("the '-%c' option requires a positive integer argument"),
    26232940                                 cs->c);
    2624                           bad = 1;
    2625                         }
    2626                       else
    2627                         *(unsigned int *) cs->value_ptr = i;
    2628                     }
    2629                   else
    2630                     *(unsigned int *) cs->value_ptr
    2631                       = *(unsigned int *) cs->noarg_value;
    2632                   break;
     2941                          bad = 1;
     2942                        }
     2943                      else
     2944                        *(unsigned int *) cs->value_ptr = i;
     2945                    }
     2946                  else
     2947                    *(unsigned int *) cs->value_ptr
     2948                      = *(unsigned int *) cs->noarg_value;
     2949                  break;
    26332950
    26342951#ifndef NO_FLOAT
    2635                 case floating:
    2636                   if (optarg == 0 && optind < argc
    2637                       && (ISDIGIT (argv[optind][0]) || argv[optind][0] == '.'))
    2638                     optarg = argv[optind++];
    2639 
    2640                   if (doit)
    2641                     *(double *) cs->value_ptr
    2642                       = (optarg != 0 ? atof (optarg)
    2643                         : *(double *) cs->noarg_value);
    2644 
    2645                   break;
    2646 #endif
    2647                 }
    2648 
    2649               /* We've found the switch.  Stop looking.  */
    2650               break;
    2651             }
     2952                case floating:
     2953                  if (coptarg == 0 && optind < argc
     2954                      && (ISDIGIT (argv[optind][0]) || argv[optind][0] == '.'))
     2955                    coptarg = argv[optind++];
     2956
     2957                  if (doit)
     2958                    *(double *) cs->value_ptr
     2959                      = (coptarg != 0 ? atof (coptarg)
     2960                        : *(double *) cs->noarg_value);
     2961
     2962                  break;
     2963#endif
     2964                }
     2965
     2966              /* We've found the switch.  Stop looking.  */
     2967              break;
     2968            }
    26522969    }
    26532970
     
    26592976    handle_non_switch_argument (argv[optind++], env);
    26602977
    2661 
    26622978  if (!env && (bad || print_usage_flag))
    26632979    {
    26642980      print_usage (bad);
    2665       die (bad ? 2 : 0);
    2666     }
     2981      die (bad ? MAKE_FAILURE : MAKE_SUCCESS);
     2982    }
     2983
     2984  /* If there are any options that need to be decoded do it now.  */
     2985  decode_debug_flags ();
     2986  decode_output_sync_flags ();
    26672987}
    26682988
     
    26732993
    26742994static void
    2675 decode_env_switches (char *envar, unsigned int len)
     2995decode_env_switches (const char *envar, unsigned int len)
    26762996{
    26772997  char *varref = alloca (2 + len + 2);
    2678   char *value, *p;
     2998  char *value, *p, *buf;
    26792999  int argc;
    2680   char **argv;
     3000  const char **argv;
    26813001
    26823002  /* Get the variable's value.  */
     
    26893009
    26903010  /* Skip whitespace, and check for an empty value.  */
    2691   value = next_token (value);
     3011  NEXT_TOKEN (value);
    26923012  len = strlen (value);
    26933013  if (len == 0)
     
    26963016  /* Allocate a vector that is definitely big enough.  */
    26973017  argv = alloca ((1 + len + 1) * sizeof (char *));
    2698 
    2699   /* Allocate a buffer to copy the value into while we split it into words
    2700      and unquote it.  We must use permanent storage for this because
    2701      decode_switches may store pointers into the passed argument words.  */
    2702   p = xmalloc (2 * len);
    27033018
    27043019  /* getopt will look at the arguments starting at ARGV[1].
     
    27063021  argv[0] = 0;
    27073022  argc = 1;
     3023
     3024  /* We need a buffer to copy the value into while we split it into words
     3025     and unquote it.  Set up in case we need to prepend a dash later.  */
     3026  buf = alloca (1 + len + 1);
     3027  buf[0] = '-';
     3028  p = buf+1;
    27083029  argv[argc] = p;
    27093030  while (*value != '\0')
    27103031    {
    27113032      if (*value == '\\' && value[1] != '\0')
    2712         ++value;                /* Skip the backslash.  */
    2713       else if (isblank ((unsigned char)*value))
    2714         {
    2715           /* End of the word.  */
    2716           *p++ = '\0';
    2717           argv[++argc] = p;
    2718           do
    2719             ++value;
    2720           while (isblank ((unsigned char)*value));
    2721           continue;
    2722         }
     3033        ++value;                /* Skip the backslash.  */
     3034      else if (ISBLANK (*value))
     3035        {
     3036          /* End of the word.  */
     3037          *p++ = '\0';
     3038          argv[++argc] = p;
     3039          do
     3040            ++value;
     3041          while (ISBLANK (*value));
     3042          continue;
     3043        }
    27233044      *p++ = *value++;
    27243045    }
    27253046  *p = '\0';
    27263047  argv[++argc] = 0;
     3048  assert (p < buf + len + 2);
    27273049
    27283050  if (argv[1][0] != '-' && strchr (argv[1], '=') == 0)
    27293051    /* The first word doesn't start with a dash and isn't a variable
    2730        definition.  Add a dash and pass it along to decode_switches.  We
    2731        need permanent storage for this in case decode_switches saves
    2732        pointers into the value.  */
    2733     argv[1] = xstrdup (concat (2, "-", argv[1]));
     3052       definition, so add a dash.  */
     3053    argv[1] = buf;
    27343054
    27353055  /* Parse those words.  */
     
    27503070    {
    27513071      if (*in == '$')
    2752         *out++ = '$';
    2753       else if (isblank ((unsigned char)*in) || *in == '\\')
     3072        *out++ = '$';
     3073      else if (ISBLANK (*in) || *in == '\\')
    27543074        *out++ = '\\';
    27553075      *out++ = *in++;
     
    27613081/* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
    27623082   command switches.  Include options with args if ALL is nonzero.
    2763    Don't include options with the `no_makefile' flag set if MAKEFILE.  */
    2764 
    2765 static const char *
     3083   Don't include options with the 'no_makefile' flag set if MAKEFILE.  */
     3084
     3085static struct variable *
    27663086define_makeflags (int all, int makefile)
    27673087{
     
    27713091  const struct command_switch *cs;
    27723092  char *flagstring;
    2773   register char *p;
    2774   unsigned int words;
    2775   struct variable *v;
    2776 
    2777   /* We will construct a linked list of `struct flag's describing
     3093  char *p;
     3094
     3095  /* We will construct a linked list of 'struct flag's describing
    27783096     all the flags which need to go in MAKEFLAGS.  Then, once we
    27793097     know how many there are and their lengths, we can put them all
     
    27873105    };
    27883106  struct flag *flags = 0;
     3107  struct flag *last = 0;
    27893108  unsigned int flagslen = 0;
    2790 #define ADD_FLAG(ARG, LEN) \
    2791   do {                                                                        \
    2792     struct flag *new = alloca (sizeof (struct flag));                         \
    2793     new->cs = cs;                                                             \
    2794     new->arg = (ARG);                                                         \
    2795     new->next = flags;                                                        \
    2796     flags = new;                                                              \
    2797     if (new->arg == 0)                                                        \
    2798       ++flagslen;               /* Just a single flag letter.  */             \
    2799     else                                                                      \
    2800       /* " -x foo", plus space to expand "foo".  */                           \
    2801       flagslen += 1 + 1 + 1 + 1 + (3 * (LEN));                                \
    2802     if (!short_option (cs->c))                                                \
     3109#define ADD_FLAG(ARG, LEN) \
     3110  do {                                                                        \
     3111    struct flag *new = alloca (sizeof (struct flag));                         \
     3112    new->cs = cs;                                                             \
     3113    new->arg = (ARG);                                                         \
     3114    new->next = 0;                                                            \
     3115    if (! flags)                                                              \
     3116      flags = new;                                                            \
     3117    else                                                                      \
     3118      last->next = new;                                                       \
     3119    last = new;                                                               \
     3120    if (new->arg == 0)                                                        \
     3121      /* Just a single flag letter: " -x"  */                                 \
     3122      flagslen += 3;                                                          \
     3123    else                                                                      \
     3124      /* " -xfoo", plus space to escape "foo".  */                            \
     3125      flagslen += 1 + 1 + 1 + (3 * (LEN));                                    \
     3126    if (!short_option (cs->c))                                                \
    28033127      /* This switch has no single-letter version, so we use the long.  */    \
    2804       flagslen += 2 + strlen (cs->long_name);                                 \
     3128      flagslen += 2 + strlen (cs->long_name);                                 \
    28053129  } while (0)
    28063130
     
    28083132    if (cs->toenv && (!makefile || !cs->no_makefile))
    28093133      switch (cs->type)
    2810         {
    2811         case ignore:
    2812           break;
    2813 
    2814         case flag:
    2815         case flag_off:
    2816           if (!*(int *) cs->value_ptr == (cs->type == flag_off)
    2817               && (cs->default_value == 0
    2818                   || *(int *) cs->value_ptr != *(int *) cs->default_value))
    2819             ADD_FLAG (0, 0);
    2820           break;
    2821 
    2822         case positive_int:
    2823           if (all)
    2824             {
    2825               if ((cs->default_value != 0
    2826                    && (*(unsigned int *) cs->value_ptr
    2827                        == *(unsigned int *) cs->default_value)))
    2828                 break;
    2829               else if (cs->noarg_value != 0
    2830                        && (*(unsigned int *) cs->value_ptr ==
    2831                            *(unsigned int *) cs->noarg_value))
    2832                 ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
    2833               else if (cs->c == 'j')
    2834                 /* Special case for `-j'.  */
    2835                 ADD_FLAG ("1", 1);
    2836               else
    2837                 {
    2838                   char *buf = alloca (30);
    2839                   sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
    2840                   ADD_FLAG (buf, strlen (buf));
    2841                 }
    2842             }
    2843           break;
     3134        {
     3135        case ignore:
     3136          break;
     3137
     3138        case flag:
     3139        case flag_off:
     3140          if ((!*(int *) cs->value_ptr) == (cs->type == flag_off)
     3141              && (cs->default_value == 0
     3142                  || *(int *) cs->value_ptr != *(int *) cs->default_value))
     3143            ADD_FLAG (0, 0);
     3144          break;
     3145
     3146        case positive_int:
     3147          if (all)
     3148            {
     3149              if ((cs->default_value != 0
     3150                   && (*(unsigned int *) cs->value_ptr
     3151                       == *(unsigned int *) cs->default_value)))
     3152                break;
     3153              else if (cs->noarg_value != 0
     3154                       && (*(unsigned int *) cs->value_ptr ==
     3155                           *(unsigned int *) cs->noarg_value))
     3156                ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
     3157              else
     3158                {
     3159                  char *buf = alloca (30);
     3160                  sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
     3161                  ADD_FLAG (buf, strlen (buf));
     3162                }
     3163            }
     3164          break;
    28443165
    28453166#ifndef NO_FLOAT
    2846         case floating:
    2847           if (all)
    2848             {
    2849               if (cs->default_value != 0
    2850                   && (*(double *) cs->value_ptr
    2851                       == *(double *) cs->default_value))
    2852                 break;
    2853               else if (cs->noarg_value != 0
    2854                        && (*(double *) cs->value_ptr
    2855                            == *(double *) cs->noarg_value))
    2856                 ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
    2857               else
    2858                 {
    2859                   char *buf = alloca (100);
    2860                   sprintf (buf, "%g", *(double *) cs->value_ptr);
    2861                   ADD_FLAG (buf, strlen (buf));
    2862                 }
    2863             }
    2864           break;
    2865 #endif
    2866 
    2867         case filename:
    2868         case string:
    2869           if (all)
    2870             {
    2871               struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
    2872               if (sl != 0)
    2873                 {
    2874                   /* Add the elements in reverse order, because all the flags
    2875                      get reversed below; and the order matters for some
    2876                      switches (like -I).  */
    2877                   unsigned int i = sl->idx;
    2878                   while (i-- > 0)
    2879                     ADD_FLAG (sl->list[i], strlen (sl->list[i]));
    2880                 }
    2881             }
    2882           break;
    2883 
    2884         default:
    2885           abort ();
    2886         }
    2887 
    2888   /* Four more for the possible " -- ".  */
    2889   flagslen += 4 + sizeof (posixref) + sizeof (evalref);
    2890 
    2891 #undef  ADD_FLAG
     3167        case floating:
     3168          if (all)
     3169            {
     3170              if (cs->default_value != 0
     3171                  && (*(double *) cs->value_ptr
     3172                      == *(double *) cs->default_value))
     3173                break;
     3174              else if (cs->noarg_value != 0
     3175                       && (*(double *) cs->value_ptr
     3176                           == *(double *) cs->noarg_value))
     3177                ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
     3178              else
     3179                {
     3180                  char *buf = alloca (100);
     3181                  sprintf (buf, "%g", *(double *) cs->value_ptr);
     3182                  ADD_FLAG (buf, strlen (buf));
     3183                }
     3184            }
     3185          break;
     3186#endif
     3187
     3188        case string:
     3189          if (all)
     3190            {
     3191              p = *((char **)cs->value_ptr);
     3192              if (p)
     3193                ADD_FLAG (p, strlen (p));
     3194            }
     3195          break;
     3196
     3197        case filename:
     3198        case strlist:
     3199          if (all)
     3200            {
     3201              struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
     3202              if (sl != 0)
     3203                {
     3204                  unsigned int i;
     3205                  for (i = 0; i < sl->idx; ++i)
     3206                    ADD_FLAG (sl->list[i], strlen (sl->list[i]));
     3207                }
     3208            }
     3209          break;
     3210
     3211        default:
     3212          abort ();
     3213        }
     3214
     3215#undef  ADD_FLAG
     3216
     3217  /* Four more for the possible " -- ", plus variable references.  */
     3218  flagslen += 4 + CSTRLEN (posixref) + 1 + CSTRLEN (evalref) + 1;
    28923219
    28933220  /* Construct the value in FLAGSTRING.
     
    28963223  memset (flagstring, '\0', 1 + flagslen + 1);
    28973224  p = flagstring;
    2898   words = 1;
     3225
     3226  /* Start with a dash, for MFLAGS.  */
    28993227  *p++ = '-';
    2900   while (flags != 0)
    2901     {
     3228
     3229  /* Add simple options as a group.  */
     3230  while (flags != 0 && !flags->arg && short_option (flags->cs->c))
     3231    {
     3232      *p++ = flags->cs->c;
     3233      flags = flags->next;
     3234    }
     3235
     3236  /* Now add more complex flags: ones with options and/or long names.  */
     3237  while (flags)
     3238    {
     3239      *p++ = ' ';
     3240      *p++ = '-';
     3241
    29023242      /* Add the flag letter or name to the string.  */
    29033243      if (short_option (flags->cs->c))
    2904         *p++ = flags->cs->c;
     3244        *p++ = flags->cs->c;
    29053245      else
    2906         {
    2907           if (*p != '-')
    2908             {
    2909               *p++ = ' ';
    2910               *p++ = '-';
    2911             }
    2912           *p++ = '-';
    2913           strcpy (p, flags->cs->long_name);
    2914           p += strlen (p);
    2915         }
    2916       if (flags->arg != 0)
    2917         {
    2918           /* A flag that takes an optional argument which in this case is
    2919              omitted is specified by ARG being "".  We must distinguish
    2920              because a following flag appended without an intervening " -"
    2921              is considered the arg for the first.  */
    2922           if (flags->arg[0] != '\0')
    2923             {
    2924               /* Add its argument too.  */
    2925               *p++ = !short_option (flags->cs->c) ? '=' : ' ';
    2926               p = quote_for_env (p, flags->arg);
    2927             }
    2928           ++words;
    2929           /* Write a following space and dash, for the next flag.  */
    2930           *p++ = ' ';
    2931           *p++ = '-';
    2932         }
    2933       else if (!short_option (flags->cs->c))
    2934         {
    2935           ++words;
    2936           /* Long options must each go in their own word,
    2937              so we write the following space and dash.  */
    2938           *p++ = ' ';
    2939           *p++ = '-';
    2940         }
     3246        {
     3247          /* Long options require a double-dash.  */
     3248          *p++ = '-';
     3249          strcpy (p, flags->cs->long_name);
     3250          p += strlen (p);
     3251        }
     3252      /* An omitted optional argument has an ARG of "".  */
     3253      if (flags->arg && flags->arg[0] != '\0')
     3254        {
     3255          if (!short_option (flags->cs->c))
     3256            /* Long options require '='.  */
     3257            *p++ = '=';
     3258          p = quote_for_env (p, flags->arg);
     3259        }
    29413260      flags = flags->next;
    29423261    }
    29433262
    2944   /* Define MFLAGS before appending variable definitions.  */
    2945 
     3263  /* If no flags at all, get rid of the initial dash.  */
    29463264  if (p == &flagstring[1])
    2947     /* No flags.  */
    2948     flagstring[0] = '\0';
    2949   else if (p[-1] == '-')
    2950     {
    2951       /* Kill the final space and dash.  */
    2952       p -= 2;
    2953       *p = '\0';
    2954     }
    2955   else
    2956     /* Terminate the string.  */
    2957     *p = '\0';
    2958 
    2959   /* Since MFLAGS is not parsed for flags, there is no reason to
     3265    {
     3266      flagstring[0] = '\0';
     3267      p = flagstring;
     3268    }
     3269
     3270  /* Define MFLAGS before appending variable definitions.  Omit an initial
     3271     empty dash.  Since MFLAGS is not parsed for flags, there is no reason to
    29603272     override any makefile redefinition.  */
    2961   define_variable_cname ("MFLAGS", flagstring, o_env, 1);
     3273  define_variable_cname ("MFLAGS",
     3274                         flagstring + (flagstring[0] == '-' && flagstring[1] == ' ' ? 2 : 0),
     3275                         o_env, 1);
    29623276
    29633277  /* Write a reference to -*-eval-flags-*-, which contains all the --eval
     
    29653279  if (eval_strings)
    29663280    {
    2967       if (p == &flagstring[1])
    2968         /* No flags written, so elide the leading dash already written.  */
    2969         p = flagstring;
    2970       else
    2971         *p++ = ' ';
    2972       memcpy (p, evalref, sizeof (evalref) - 1);
    2973       p += sizeof (evalref) - 1;
    2974     }
    2975 
    2976   if (all && command_variables != 0)
    2977     {
    2978       /* Now write a reference to $(MAKEOVERRIDES), which contains all the
    2979          command-line variable definitions.  */
    2980 
    2981       if (p == &flagstring[1])
    2982         /* No flags written, so elide the leading dash already written.  */
    2983         p = flagstring;
    2984       else
    2985         {
    2986           /* Separate the variables from the switches with a "--" arg.  */
    2987           if (p[-1] != '-')
    2988             {
    2989               /* We did not already write a trailing " -".  */
    2990               *p++ = ' ';
    2991               *p++ = '-';
    2992             }
    2993           /* There is a trailing " -"; fill it out to " -- ".  */
    2994           *p++ = '-';
    2995           *p++ = ' ';
    2996         }
     3281      *p++ = ' ';
     3282      memcpy (p, evalref, CSTRLEN (evalref));
     3283      p += CSTRLEN (evalref);
     3284    }
     3285
     3286  if (all && command_variables)
     3287    {
     3288      /* Write a reference to $(MAKEOVERRIDES), which contains all the
     3289         command-line variable definitions.  Separate the variables from the
     3290         switches with a "--" arg.  */
     3291
     3292      strcpy (p, " -- ");
     3293      p += 4;
    29973294
    29983295      /* Copy in the string.  */
    29993296      if (posix_pedantic)
    3000         {
    3001           memcpy (p, posixref, sizeof (posixref) - 1);
    3002           p += sizeof (posixref) - 1;
    3003         }
     3297        {
     3298          memcpy (p, posixref, CSTRLEN (posixref));
     3299          p += CSTRLEN (posixref);
     3300        }
    30043301      else
    3005         {
    3006           memcpy (p, ref, sizeof (ref) - 1);
    3007           p += sizeof (ref) - 1;
    3008         }
    3009     }
    3010   else if (p == &flagstring[1])
    3011     {
    3012       words = 0;
    3013       --p;
    3014     }
    3015   else if (p[-1] == '-')
    3016     /* Kill the final space and dash.  */
    3017     p -= 2;
    3018   /* Terminate the string.  */
    3019   *p = '\0';
    3020 
    3021   /* If there are switches, omit the leading dash unless it is a single long
    3022      option with two leading dashes.  */
    3023   if (flagstring[0] == '-' && flagstring[1] != '-')
     3302        {
     3303          memcpy (p, ref, CSTRLEN (ref));
     3304          p += CSTRLEN (ref);
     3305        }
     3306    }
     3307
     3308  /* If there is a leading dash, omit it.  */
     3309  if (flagstring[0] == '-')
    30243310    ++flagstring;
    30253311
    3026   v = define_variable_cname ("MAKEFLAGS", flagstring,
    3027                              /* This used to use o_env, but that lost when a
    3028                                 makefile defined MAKEFLAGS.  Makefiles set
    3029                                 MAKEFLAGS to add switches, but we still want
    3030                                 to redefine its value with the full set of
    3031                                 switches.  Of course, an override or command
    3032                                 definition will still take precedence.  */
    3033                              o_file, 1);
    3034 
    3035   if (! all)
    3036     /* The first time we are called, set MAKEFLAGS to always be exported.
    3037        We should not do this again on the second call, because that is
    3038        after reading makefiles which might have done `unexport MAKEFLAGS'. */
    3039     v->export = v_export;
    3040 
    3041   return v->value;
     3312  /* This used to use o_env, but that lost when a makefile defined MAKEFLAGS.
     3313     Makefiles set MAKEFLAGS to add switches, but we still want to redefine
     3314     its value with the full set of switches.  Then we used o_file, but that
     3315     lost when users added -e, causing a previous MAKEFLAGS env. var. to take
     3316     precedence over the new one.  Of course, an override or command
     3317     definition will still take precedence.  */
     3318  return define_variable_cname ("MAKEFLAGS", flagstring,
     3319                                env_overrides ? o_env_override : o_file, 1);
    30423320}
    30433321
     
    30503328  static int printed_version = 0;
    30513329
    3052   char *precede = print_data_base_flag ? "# " : "";
     3330  const char *precede = print_data_base_flag ? "# " : "";
    30533331
    30543332  if (printed_version)
     
    30673345     (C) to the copyright symbol, but this string is going to change every
    30683346     year, and none of the rest of it should be translated (including the
    3069      word "Copyright", so it hardly seems worth it.  */
    3070 
    3071   printf ("%sCopyright (C) 2010  Free Software Foundation, Inc.\n", precede);
     3347     word "Copyright"), so it hardly seems worth it.  */
     3348
     3349  printf ("%sCopyright (C) 1988-2016 Free Software Foundation, Inc.\n",
     3350          precede);
    30723351
    30733352  printf (_("%sLicense GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>\n\
     
    30793358
    30803359  /* Flush stdout so the user doesn't have to wait to see the
    3081      version information while things are thought about.  */
     3360     version information while make thinks about things.  */
    30823361  fflush (stdout);
    30833362}
     
    30863365
    30873366static void
    3088 print_data_base ()
     3367print_data_base (void)
    30893368{
    3090   time_t when;
    3091 
    3092   when = time ((time_t *) 0);
     3369  time_t when = time ((time_t *) 0);
     3370
     3371  print_version ();
     3372
    30933373  printf (_("\n# Make data base, printed on %s"), ctime (&when));
    30943374
     
    31073387clean_jobserver (int status)
    31083388{
    3109   char token = '+';
    3110 
    31113389  /* Sanity: have we written all our jobserver tokens back?  If our
    31123390     exit status is 2 that means some kind of syntax error; we might not
     
    31143392     after any other error code, that's bad.  */
    31153393
    3116   if (job_fds[0] != -1 && jobserver_tokens)
     3394  if (jobserver_enabled() && jobserver_tokens)
    31173395    {
    31183396      if (status != 2)
    3119         error (NILF,
    3120                "INTERNAL: Exiting with %u jobserver tokens (should be 0)!",
    3121                jobserver_tokens);
     3397        ON (error, NILF,
     3398            "INTERNAL: Exiting with %u jobserver tokens (should be 0)!",
     3399            jobserver_tokens);
    31223400      else
    3123         while (jobserver_tokens--)
    3124           {
    3125             int r;
    3126 
    3127             EINTRLOOP (r, write (job_fds[1], &token, 1));
    3128             if (r != 1)
    3129               perror_with_name ("write", "");
    3130           }
     3401        /* Don't write back the "free" token */
     3402        while (--jobserver_tokens)
     3403          jobserver_release (0);
    31313404    }
    31323405
     
    31373410    {
    31383411      /* We didn't write one for ourself, so start at 1.  */
    3139       unsigned int tcnt = 1;
    3140 
    3141       /* Close the write side, so the read() won't hang.  */
    3142       close (job_fds[1]);
    3143 
    3144       while (read (job_fds[0], &token, 1) == 1)
    3145         ++tcnt;
    3146 
    3147       if (tcnt != master_job_slots)
    3148         error (NILF,
    3149                "INTERNAL: Exiting with %u jobserver tokens available; should be %u!",
    3150                tcnt, master_job_slots);
    3151 
    3152       close (job_fds[0]);
    3153 
    3154       /* Clean out jobserver_fds so we don't pass this information to any
    3155          sub-makes.  Also reset job_slots since it will be put on the command
    3156          line, not in MAKEFLAGS.  */
    3157       job_slots = default_job_slots;
    3158       if (jobserver_fds)
    3159         {
    3160           /* MSVC erroneously warns without a cast here.  */
    3161           free ((void *)jobserver_fds->list);
    3162           free (jobserver_fds);
    3163           jobserver_fds = 0;
    3164         }
     3412      unsigned int tokens = 1 + jobserver_acquire_all ();
     3413
     3414      if (tokens != master_job_slots)
     3415        ONN (error, NILF,
     3416             "INTERNAL: Exiting with %u jobserver tokens available; should be %u!",
     3417             tokens, master_job_slots);
     3418
     3419      reset_jobserver ();
    31653420    }
    31663421}
     
    31813436
    31823437      if (print_version_flag)
    3183         print_version ();
     3438        print_version ();
    31843439
    31853440      /* Wait for children to die.  */
    31863441      err = (status != 0);
    31873442      while (job_slots_used > 0)
    3188         reap_children (1, err);
     3443        reap_children (1, err);
    31893444
    31903445      /* Let the remote job module clean up its state.  */
     
    31953450
    31963451      if (print_data_base_flag)
    3197         print_data_base ();
    3198 
    3199       verify_file_data_base ();
     3452        print_data_base ();
     3453
     3454      if (verify_flag)
     3455        verify_file_data_base ();
    32003456
    32013457      clean_jobserver (status);
    32023458
     3459      if (output_context)
     3460        {
     3461          /* die() might be called in a recipe output context due to an
     3462             $(error ...) function.  */
     3463          output_close (output_context);
     3464
     3465          if (output_context != &make_sync)
     3466            output_close (&make_sync);
     3467
     3468          OUTPUT_UNSET ();
     3469        }
     3470
     3471      output_close (NULL);
     3472
    32033473      /* Try to move back to the original directory.  This is essential on
    3204         MS-DOS (where there is really only one process), and on Unix it
    3205         puts core files in the original directory instead of the -C
    3206         directory.  Must wait until after remove_intermediates(), or unlinks
     3474        MS-DOS (where there is really only one process), and on Unix it
     3475        puts core files in the original directory instead of the -C
     3476        directory.  Must wait until after remove_intermediates(), or unlinks
    32073477         of relative pathnames fail.  */
    32083478      if (directory_before_chdir != 0)
    32093479        {
    32103480          /* If it fails we don't care: shut up GCC.  */
    3211           int _x;
     3481          int _x UNUSED;
    32123482          _x = chdir (directory_before_chdir);
    32133483        }
    3214 
    3215       log_working_directory (0);
    32163484    }
    32173485
    32183486  exit (status);
    32193487}
    3220 
    3221 
    3222 /* Write a message indicating that we've just entered or
    3223    left (according to ENTERING) the current directory.  */
    3224 
    3225 void
    3226 log_working_directory (int entering)
    3227 {
    3228   static int entered = 0;
    3229 
    3230   /* Print nothing without the flag.  Don't print the entering message
    3231      again if we already have.  Don't print the leaving message if we
    3232      haven't printed the entering message.  */
    3233   if (! print_directory_flag || entering == entered)
    3234     return;
    3235 
    3236   entered = entering;
    3237 
    3238   if (print_data_base_flag)
    3239     fputs ("# ", stdout);
    3240 
    3241   /* Use entire sentences to give the translators a fighting chance.  */
    3242 
    3243   if (makelevel == 0)
    3244     if (starting_directory == 0)
    3245       if (entering)
    3246         printf (_("%s: Entering an unknown directory\n"), program);
    3247       else
    3248         printf (_("%s: Leaving an unknown directory\n"), program);
    3249     else
    3250       if (entering)
    3251         printf (_("%s: Entering directory `%s'\n"),
    3252                 program, starting_directory);
    3253       else
    3254         printf (_("%s: Leaving directory `%s'\n"),
    3255                 program, starting_directory);
    3256   else
    3257     if (starting_directory == 0)
    3258       if (entering)
    3259         printf (_("%s[%u]: Entering an unknown directory\n"),
    3260                 program, makelevel);
    3261       else
    3262         printf (_("%s[%u]: Leaving an unknown directory\n"),
    3263                 program, makelevel);
    3264     else
    3265       if (entering)
    3266         printf (_("%s[%u]: Entering directory `%s'\n"),
    3267                 program, makelevel, starting_directory);
    3268       else
    3269         printf (_("%s[%u]: Leaving directory `%s'\n"),
    3270                 program, makelevel, starting_directory);
    3271 
    3272   /* Flush stdout to be sure this comes before any stderr output.  */
    3273   fflush (stdout);
    3274 }
Note: See TracChangeset for help on using the changeset viewer.