home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 4 / FreshFish_May-June1994.bin / bbs / gnu / make-3.70-src.lha / src / amiga / make-3.70 / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-21  |  47.4 KB  |  1,808 lines

  1. /* Copyright (C) 1988, 1989, 1990, 1991 Free Software Foundation, Inc.
  2. This file is part of GNU Make.
  3.  
  4. GNU Make is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2, or (at your option)
  7. any later version.
  8.  
  9. GNU Make is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12. GNU General Public License for more details.
  13.  
  14. You should have received a copy of the GNU General Public License
  15. along with GNU Make; see the file COPYING.  If not, write to
  16. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #include "make.h"
  19. #include "commands.h"
  20. #include "dep.h"
  21. #include "file.h"
  22. #include "variable.h"
  23. #include "job.h"
  24. #include "getopt.h"
  25. #ifdef amigados
  26. #include <stdlib.h>
  27. #endif
  28.  
  29.  
  30. extern void print_variable_data_base ();
  31. extern void print_dir_data_base ();
  32. extern void print_rule_data_base ();
  33. extern void print_file_data_base ();
  34. extern void print_vpath_data_base ();
  35.  
  36. #ifndef    HAVE_UNISTD_H
  37. extern int chdir ();
  38. #endif
  39. #ifndef    STDC_HEADERS
  40. #ifndef    sun            /* Sun has an incorrect decl in a header.  */
  41. extern void exit ();
  42. #endif
  43. extern double atof ();
  44. #endif
  45. extern char *mktemp ();
  46.  
  47. static void log_working_directory ();
  48. static void print_data_base (), print_version ();
  49. static void decode_switches (), decode_env_switches ();
  50. static void define_makeflags ();
  51.  
  52. /* The structure that describes an accepted command switch.  */
  53.  
  54. struct command_switch
  55.   {
  56.     char c;            /* The switch character.  */
  57.  
  58.     enum            /* Type of the value.  */
  59.       {
  60.     flag,            /* Turn int flag on.  */
  61.     flag_off,        /* Turn int flag off.  */
  62.     string,            /* One string per switch.  */
  63.     positive_int,        /* A positive integer.  */
  64.     floating,        /* A floating-point number (double).  */
  65.     ignore            /* Ignored.  */
  66.       } type;
  67.  
  68.     char *value_ptr;    /* Pointer to the value-holding variable.  */
  69.  
  70.     unsigned int env:1;        /* Can come from MAKEFLAGS.  */
  71.     unsigned int toenv:1;    /* Should be put in MAKEFLAGS.  */
  72.     unsigned int no_makefile:1;    /* Don't propagate when remaking makefiles.  */
  73.  
  74.     char *noarg_value;    /* Pointer to value used if no argument is given.  */
  75.     char *default_value;/* Pointer to default value.  */
  76.  
  77.     char *long_name;        /* Long option name.  */
  78.     char *argdesc;        /* Descriptive word for argument.  */
  79.     char *description;        /* Description for usage message.  */
  80.   };
  81.  
  82.  
  83. /* The structure used to hold the list of strings given
  84.    in command switches of a type that takes string arguments.  */
  85.  
  86. struct stringlist
  87.   {
  88.     char **list;    /* Nil-terminated list of strings.  */
  89.     unsigned int idx;    /* Index into above.  */
  90.     unsigned int max;    /* Number of pointers allocated.  */
  91.   };
  92.  
  93.  
  94. /* The recognized command switches.  */
  95.  
  96. /* Nonzero means do not print commands to be executed (-s).  */
  97.  
  98. int silent_flag;
  99.  
  100. /* Nonzero means just touch the files
  101.    that would appear to need remaking (-t)  */
  102.  
  103. int touch_flag;
  104.  
  105. /* Nonzero means just print what commands would need to be executed,
  106.    don't actually execute them (-n).  */
  107.  
  108. int just_print_flag;
  109.  
  110. /* Print debugging trace info (-d).  */
  111.  
  112. int debug_flag = 0;
  113.  
  114. /* Environment variables override makefile definitions.  */
  115.  
  116. int env_overrides = 0;
  117.  
  118. /* Nonzero means ignore status codes returned by commands
  119.    executed to remake files.  Just treat them all as successful (-i).  */
  120.  
  121. int ignore_errors_flag = 0;
  122.  
  123. /* Nonzero means don't remake anything, just print the data base
  124.    that results from reading the makefile (-p).  */
  125.  
  126. int print_data_base_flag = 0;
  127.  
  128. /* Nonzero means don't remake anything; just return a nonzero status
  129.    if the specified targets are not up to date (-q).  */
  130.  
  131. int question_flag = 0;
  132.  
  133. /* Nonzero means do not use any of the builtin rules (-r).  */
  134.  
  135. int no_builtin_rules_flag = 0;
  136.  
  137. /* Nonzero means keep going even if remaking some file fails (-k).  */
  138.  
  139. int keep_going_flag;
  140. int default_keep_going_flag = 0;
  141.  
  142. /* Nonzero means print directory before starting and when done (-w).  */
  143.  
  144. int print_directory_flag = 0;
  145.  
  146. /* Nonzero means ignore print_directory_flag and never print the directory.
  147.    This is necessary because print_directory_flag is set implicitly.  */
  148.  
  149. int inhibit_print_directory_flag = 0;
  150.  
  151. /* Nonzero means print version information.  */
  152.  
  153. int print_version_flag = 0;
  154.  
  155. /* List of makefiles given with -f switches.  */
  156.  
  157. static struct stringlist *makefiles = 0;
  158.  
  159.  
  160. /* Number of job slots (commands that can be run at once).  */
  161.  
  162. unsigned int job_slots = 1;
  163. unsigned int default_job_slots = 1;
  164.  
  165. /* Value of job_slots that means no limit.  */
  166.  
  167. static unsigned int inf_jobs = 0;
  168.  
  169. /* Maximum load average at which multiple jobs will be run.
  170.    Negative values mean unlimited, while zero means limit to
  171.    zero load (which could be useful to start infinite jobs remotely
  172.    but one at a time locally).  */
  173.  
  174. double max_load_average = -1.0;
  175. double default_load_average = -1.0;
  176.  
  177. /* List of directories given with -C switches.  */
  178.  
  179. static struct stringlist *directories = 0;
  180.  
  181. /* List of include directories given with -I switches.  */
  182.  
  183. static struct stringlist *include_directories = 0;
  184.  
  185. /* List of files given with -o switches.  */
  186.  
  187. static struct stringlist *old_files = 0;
  188.  
  189. /* List of files given with -W switches.  */
  190.  
  191. static struct stringlist *new_files = 0;
  192.  
  193. /* If nonzero, we should just print usage and exit.  */
  194.  
  195. static int print_usage_flag = 0;
  196.  
  197. /* If nonzero, we should print a warning message
  198.    for each reference to an undefined variable.  */
  199.  
  200. int warn_undefined_variables_flag;
  201.  
  202. /* The table of command switches.  */
  203.  
  204. static const struct command_switch switches[] =
  205.   {
  206.     { 'b', ignore, 0, 0, 0, 0, 0, 0,
  207.     0, 0,
  208.     "Ignored for compatibility" },
  209.     { 'C', string, (char *) &directories, 0, 0, 0, 0, 0,
  210.     "directory", "DIRECTORY",
  211.     "Change to DIRECTORY before doing anything" },
  212.     { 'd', flag, (char *) &debug_flag, 1, 1, 0, 0, 0,
  213.     "debug", 0,
  214.     "Print lots of debugging information" },
  215.     { 'e', flag, (char *) &env_overrides, 1, 1, 0, 0, 0,
  216.     "environment-overrides", 0,
  217.     "Environment variables override makefiles" },
  218.     { 'f', string, (char *) &makefiles, 0, 0, 0, 0, 0,
  219.     "file", "FILE",
  220.     "Read FILE as a makefile" },
  221.     { 'h', flag, (char *) &print_usage_flag, 0, 0, 0, 0, 0,
  222.     "help", 0,
  223.     "Print this message and exit" },
  224.     { 'i', flag, (char *) &ignore_errors_flag, 1, 1, 0, 0, 0,
  225.     "ignore-errors", 0,
  226.     "Ignore errors from commands" },
  227.     { 'I', string, (char *) &include_directories, 1, 1, 0, 0, 0,
  228.     "include-dir", "DIRECTORY",
  229.     "Search DIRECTORY for included makefiles" },
  230.     { 'j', positive_int, (char *) &job_slots, 1, 1, 0,
  231.     (char *) &inf_jobs, (char *) &default_job_slots,
  232.     "jobs", "N",
  233.     "Allow N jobs at once; infinite jobs with no arg" },
  234.     { 'k', flag, (char *) &keep_going_flag, 1, 1, 0,
  235.     0, (char *) &default_keep_going_flag,
  236.     "keep-going", 0,
  237.     "Keep going when some targets can't be made" },
  238.     { 'l', floating, (char *) &max_load_average, 1, 1, 0,
  239.     (char *) &default_load_average, (char *) &default_load_average,
  240.     "load-average", "N",
  241.     "Don't start multiple jobs unless load is below N" },
  242.     { 'm', ignore, 0, 0, 0, 0, 0, 0,
  243.     0, 0,
  244.     "-b" },
  245.     { 'n', flag, (char *) &just_print_flag, 1, 1, 1, 0, 0,
  246.     "just-print", 0,
  247.     "Don't actually run any commands; just print them" },
  248.     { 'o', string, (char *) &old_files, 0, 0, 0, 0, 0,
  249.     "old-file", "FILE",
  250.     "Consider FILE to be very old and don't remake it" },
  251.     { 'p', flag, (char *) &print_data_base_flag, 1, 1, 0, 0, 0,
  252.     "print-data-base", 0,
  253.     "Print make's internal database" },
  254.     { 'q', flag, (char *) &question_flag, 1, 1, 1, 0, 0,
  255.     "question", 0,
  256.     "Run no commands; exit status says if up to date" },
  257.     { 'r', flag, (char *) &no_builtin_rules_flag, 1, 1, 0, 0, 0,
  258.     "no-builtin-rules", 0,
  259.     "Disable the built-in implicit rules" },
  260.     { 's', flag, (char *) &silent_flag, 1, 1, 0, 0, 0,
  261.     "silent", 0,
  262.     "Don't echo commands" },
  263.     { 'S', flag_off, (char *) &keep_going_flag, 1, 1, 0,
  264.     0, (char *) &default_keep_going_flag,
  265.     "no-keep-going", 0,
  266.     "Turns off -k" },
  267.     { 't', flag, (char *) &touch_flag, 1, 1, 1, 0, 0,
  268.     "touch", 0,
  269.     "Touch targets instead of remaking them" },
  270.     { 'v', flag, (char *) &print_version_flag, 1, 1, 0, 0, 0,
  271.     "version", 0,
  272.     "Print the version number of make and exit" },
  273.     { 'w', flag, (char *) &print_directory_flag, 1, 1, 0, 0, 0,
  274.     "print-directory", 0,
  275.     "Print the current directory" },
  276.     { 1, flag, (char *) &inhibit_print_directory_flag, 1, 1, 0, 0, 0,
  277.     "no-print-directory", 0,
  278.     "Turn off -w, even if it was turned on implicitly" },
  279.     { 'W', string, (char *) &new_files, 0, 0, 0, 0, 0,
  280.     "what-if", "FILE",
  281.     "Consider FILE to be infinitely new" },
  282.     { 2, flag, (char *) &warn_undefined_variables_flag, 1, 1, 0, 0, 0,
  283.     "warn-undefined-variables", 0,
  284.     "Warn when an undefined variable is referenced" },
  285.     { '\0', }
  286.   };
  287.  
  288. /* Secondary long names for options.  */
  289.  
  290. static struct option long_option_aliases[] =
  291.   {
  292.     { "quiet",        no_argument,        0, 's' },
  293.     { "stop",        no_argument,        0, 'S' },
  294.     { "new-file",    required_argument,    0, 'W' },
  295.     { "assume-new",    required_argument,    0, 'W' },
  296.     { "assume-old",    required_argument,    0, 'o' },
  297.     { "max-load",    optional_argument,    0, 'l' },
  298.     { "dry-run",    no_argument,        0, 'n' },
  299.     { "recon",        no_argument,        0, 'n' },
  300.     { "makefile",    required_argument,    0, 'f' },
  301.   };
  302.  
  303. /* The usage message prints the descriptions of options starting in
  304.    this column.  Make sure it leaves enough room for the longest
  305.    description to fit in less than 80 characters.  */
  306.  
  307. #define    DESCRIPTION_COLUMN    30
  308.  
  309. /* List of non-switch arguments.  */
  310.  
  311. struct stringlist *other_args = 0;
  312.  
  313. /* The name we were invoked with.  */
  314.  
  315. char *program;
  316.  
  317. /* Our current directory after processing all -C options.  */
  318.  
  319. char *starting_directory;
  320.  
  321. /* Value of the MAKELEVEL variable at startup (or 0).  */
  322.  
  323. unsigned int makelevel;
  324.  
  325. /* First file defined in the makefile whose name does not
  326.    start with `.'.  This is the default to remake if the
  327.    command line does not specify.  */
  328.  
  329. struct file *default_goal_file;
  330.  
  331. /* Pointer to structure for the file .DEFAULT
  332.    whose commands are used for any file that has none of its own.
  333.    This is zero if the makefiles do not define .DEFAULT.  */
  334.  
  335. struct file *default_file;
  336.  
  337. /* Mask of signals that are being caught with fatal_error_signal.  */
  338.  
  339. #ifdef    POSIX
  340. sigset_t fatal_signal_set;
  341. #else
  342. #ifdef    HAVE_SIGSETMASK
  343. int fatal_signal_mask;
  344. #endif
  345. #endif
  346.  
  347. static struct file *
  348. enter_command_line_file (name)
  349.      char *name;
  350. {
  351.   if (name[0] == '~')
  352.     {
  353.       char *expanded = tilde_expand (name);
  354.       if (expanded != 0)
  355.     name = expanded;    /* Memory leak; I don't care.  */
  356.     }
  357.  
  358.   /* This is also done in parse_file_seq, so this is redundant
  359.      for names read from makefiles.  It is here for names passed
  360.      on the command line.  */
  361.   while (name[0] == '.' && name[1] == '/' && name[2] != '\0')
  362.     {
  363.       name += 2;
  364.       while (*name == '/')
  365.     /* Skip following slashes: ".//foo" is "foo", not "/foo".  */
  366.     ++name;
  367.     }
  368.   
  369.   if (*name == '\0')
  370.     {
  371.       /* It was all slashes!  Move back to the dot and truncate
  372.      it after the first slash, so it becomes just "./".  */
  373.       do
  374.     --name;
  375.       while (name[0] != '.');
  376.       name[2] = '\0';
  377.     }
  378.  
  379.   return enter_file (savestring (name, strlen (name)));
  380. }
  381.  
  382. int
  383. main (argc, argv, envp)
  384.      int argc;
  385.      char **argv;
  386.      char **envp;
  387. {
  388.   extern void init_dir ();
  389.   extern RETSIGTYPE fatal_error_signal (), child_handler ();
  390.   register struct file *f;
  391.   register unsigned int i;
  392.   register char *cmd_defs;
  393.   register unsigned int cmd_defs_len, cmd_defs_idx;
  394.   char **p;
  395.   struct dep *goals = 0;
  396.   register struct dep *lastgoal;
  397.   struct dep *read_makefiles;
  398.   PATH_VAR (current_directory);
  399.   char *directory_before_chdir;
  400.  
  401.   default_goal_file = 0;
  402.   reading_filename = 0;
  403.   reading_lineno_ptr = 0;
  404.   
  405. #ifndef    HAVE_SYS_SIGLIST
  406.   signame_init ();
  407. #endif
  408.  
  409. #ifdef    POSIX
  410.   sigemptyset (&fatal_signal_set);
  411. #define    ADD_SIG(sig)    sigaddset (&fatal_signal_set, sig)
  412. #else
  413. #ifdef    HAVE_SIGSETMASK
  414.   fatal_signal_mask = 0;
  415. #define    ADD_SIG(sig)    fatal_signal_mask |= sigmask (sig)
  416. #else
  417. #define    ADD_SIG(sig)
  418. #endif
  419. #endif
  420.  
  421. #define    FATAL_SIG(sig)                                  \
  422.   if (signal ((sig), fatal_error_signal) == SIG_IGN)                  \
  423.     (void) signal ((sig), SIG_IGN);                          \
  424.   else                                          \
  425.     ADD_SIG (sig);
  426.  
  427.   FATAL_SIG (SIGHUP);
  428.   FATAL_SIG (SIGQUIT);
  429.   FATAL_SIG (SIGINT);
  430.   FATAL_SIG (SIGTERM);
  431.  
  432. #ifdef    SIGDANGER
  433.   FATAL_SIG (SIGDANGER);
  434. #endif
  435. #ifdef SIGXCPU
  436.   FATAL_SIG (SIGXCPU);
  437. #endif
  438. #ifdef SIGXFSZ
  439.   FATAL_SIG (SIGXFSZ);
  440. #endif
  441.  
  442. #undef    FATAL_SIG
  443.  
  444.   /* Make sure stdout is line-buffered.  */
  445.  
  446. #ifdef    HAVE_SETLINEBUF
  447.   setlinebuf (stdout);
  448. #else
  449. #ifndef    SETVBUF_REVERSED
  450.   setvbuf (stdout, (char *) 0, _IOLBF, BUFSIZ);
  451. #else    /* setvbuf not reversed.  */
  452.   /* Some buggy systems lose if we pass 0 instead of allocating ourselves.  */
  453.   setvbuf (stdout, _IOLBF, xmalloc (BUFSIZ), BUFSIZ);
  454. #endif    /* setvbuf reversed.  */
  455. #endif    /* setlinebuf missing.  */
  456.  
  457.   /* Initialize the directory hashing code.  */
  458.   init_dir ();
  459.  
  460.   /* Set up to access user data (files).  */
  461.   user_access ();
  462.  
  463.   /* Figure out where this program lives.  */
  464.  
  465.   if (argv[0] == 0)
  466.     argv[0] = "";
  467.   if (argv[0][0] == '\0')
  468.     program = "make";
  469.   else 
  470.     {
  471.       program = rindex (argv[0], '/');
  472.       if (program == 0)
  473.     program = argv[0];
  474.       else
  475.     ++program;
  476.     }
  477.  
  478.   /* Figure out where we are.  */
  479.  
  480.   if (getcwd (current_directory, GET_PATH_MAX) == 0)
  481.     {
  482. #ifdef    HAVE_GETCWD
  483.       perror_with_name ("getcwd: ", "");
  484. #else
  485.       error ("getwd: %s", current_directory);
  486. #endif
  487.       current_directory[0] = '\0';
  488.       directory_before_chdir = 0;
  489.     }
  490.   else
  491.     directory_before_chdir = savestring (current_directory,
  492.                      strlen (current_directory));
  493.  
  494.   /* Read in variables from the environment.  It is important that this be
  495.      done before `MAKE' and `MAKEOVERRIDES' are figured out so their
  496.      definitions will not be ones from the environment.  */
  497.  
  498.   for (i = 0; envp[i] != 0; ++i)
  499.     {
  500.       register char *ep = envp[i];
  501.       while (*ep != '=')
  502.     ++ep;
  503.       /* The result of pointer arithmetic is cast to unsigned int for
  504.      machines where ptrdiff_t is a different size that doesn't widen
  505.      the same.  */
  506.       define_variable (envp[i], (unsigned int) (ep - envp[i]),
  507.                ep + 1, o_env, 1)
  508.     /* Force exportation of every variable culled from the environment.
  509.        We used to rely on target_environment's v_default code to do this.
  510.        But that does not work for the case where an environment variable
  511.        is redefined in a makefile with `override'; it should then still
  512.        be exported, because it was originally in the environment.  */
  513.     ->export = v_export;
  514.     }
  515.  
  516.   /* Decode the switches.  */
  517.  
  518.   decode_env_switches ("MAKEFLAGS", 9);
  519. #if 0
  520.   /* People write things like:
  521.          MFLAGS="CC=gcc -pipe" "CFLAGS=-g"
  522.      and we set the -p, -i and -e switches.  Doesn't seem quite right.  */
  523.   decode_env_switches ("MFLAGS", 6);
  524. #endif
  525.   decode_switches (argc, argv, 0);
  526.  
  527.   /* Print version information.  */
  528.  
  529.   if (print_version_flag || print_data_base_flag || debug_flag)
  530.     print_version ();
  531.  
  532.   /* `make --version' is supposed to just print the version and exit.  */
  533.   if (print_version_flag)
  534.     die (0);
  535.  
  536.   /* Search for command line arguments that define variables,
  537.      and do the definitions.  Also save up the text of these
  538.      arguments in CMD_DEFS so we can put them into the values
  539.      of $(MAKEOVERRIDES) and $(MAKE).  */
  540.  
  541.   cmd_defs_len = 200;
  542.   cmd_defs = (char *) xmalloc (cmd_defs_len);
  543.   cmd_defs_idx = 0;
  544.  
  545.   for (i = 1; other_args->list[i] != 0; ++i)
  546.     {
  547.       if (other_args->list[i][0] == '\0')
  548.     /* Ignore empty arguments, so they can't kill enter_file.  */
  549.     continue;
  550.  
  551.       /* Try a variable definition.  */
  552.       if (try_variable_definition ((char *) 0, 0,
  553.                    other_args->list[i], o_command))
  554.     {
  555.       /* It succeeded.  The variable is already defined.
  556.          Backslash-quotify it and append it to CMD_DEFS, then clobber it
  557.          to 0 in the list so that it won't be taken for a goal target.  */
  558.       register char *p = other_args->list[i];
  559.       unsigned int l = strlen (p);
  560.       if (cmd_defs_idx + (l * 2) + 1 > cmd_defs_len)
  561.         {
  562.           if (l * 2 > cmd_defs_len)
  563.         cmd_defs_len += l * 2;
  564.           else
  565.         cmd_defs_len *= 2;
  566.           cmd_defs = (char *) xrealloc (cmd_defs, cmd_defs_len);
  567.         }
  568.       
  569.       while (*p != '\0')
  570.         {
  571.           if (index ("^;'\"*?[]$<>(){}|&~`\\ \t\r\n\f\v", *p) != 0)
  572.         cmd_defs[cmd_defs_idx++] = '\\';
  573.           cmd_defs[cmd_defs_idx++] = *p++;
  574.         }
  575.       cmd_defs[cmd_defs_idx++] = ' ';
  576.     }
  577.       else
  578.     {
  579.       /* It was not a variable definition, so it is a target to be made.
  580.          Enter it as a file and add it to the dep chain of goals.  */
  581.       f = enter_command_line_file (other_args->list[i]);
  582.       f->cmd_target = 1;
  583.       
  584.       if (goals == 0)
  585.         {
  586.           goals = (struct dep *) xmalloc (sizeof (struct dep));
  587.           lastgoal = goals;
  588.         }
  589.       else
  590.         {
  591.           lastgoal->next = (struct dep *) xmalloc (sizeof (struct dep));
  592.           lastgoal = lastgoal->next;
  593.         }
  594.       lastgoal->name = 0;
  595.       lastgoal->file = f;
  596.     }
  597.     }
  598.  
  599.   if (cmd_defs_idx > 0)
  600.     {
  601.       cmd_defs[cmd_defs_idx - 1] = '\0';
  602.       (void) define_variable ("MAKEOVERRIDES", 13, cmd_defs, o_default, 0);
  603.     }
  604.   free (cmd_defs);
  605.  
  606.   /* Set the "MAKE_COMMAND" variable to the name we were invoked with.
  607.      (If it is a relative pathname with a slash, prepend our directory name
  608.      so the result will run the same program regardless of the current dir.
  609.      If it is a name with no slash, we can only hope that PATH did not
  610.      find it in the current directory.)  */
  611.  
  612.   if (current_directory[0] != '\0'
  613.       && argv[0] != 0 && argv[0][0] != '/' && index (argv[0], '/') != 0)
  614.     argv[0] = concat (current_directory, "/", argv[0]);
  615.  
  616.   (void) define_variable ("MAKE_COMMAND", 12, argv[0], o_default, 0);
  617.  
  618.   /* Append the command-line variable definitions gathered above
  619.      so sub-makes will get them as command-line definitions.  */
  620.  
  621.   (void) define_variable ("MAKE", 4,
  622.               "$(MAKE_COMMAND) $(MAKEOVERRIDES)", o_default, 1);
  623.  
  624.   /* If there were -C flags, move ourselves about.  */
  625.  
  626.   if (directories != 0)
  627.     for (i = 0; directories->list[i] != 0; ++i)
  628.       {
  629.     char *dir = directories->list[i];
  630.     if (dir[0] == '~')
  631.       {
  632.         char *expanded = tilde_expand (dir);
  633.         if (expanded != 0)
  634.           dir = expanded;
  635.       }
  636.     if (chdir (dir) < 0)
  637.       pfatal_with_name (dir);
  638.     if (dir != directories->list[i])
  639.       free (dir);
  640.       }
  641.  
  642.   /* Figure out the level of recursion.  */
  643.   {
  644.     struct variable *v = lookup_variable ("MAKELEVEL", 9);
  645.     if (v != 0 && *v->value != '\0' && *v->value != '-')
  646.       makelevel = (unsigned int) atoi (v->value);
  647.     else
  648.       makelevel = 0;
  649.   }
  650.  
  651.   /* Except under -s, always do -w in sub-makes and under -C.  */
  652.   if (!silent_flag && (directories != 0 || makelevel > 0))
  653.     print_directory_flag = 1;
  654.  
  655.   /* Let the user disable that with --no-print-directory.  */
  656.   if (inhibit_print_directory_flag)
  657.     print_directory_flag = 0;
  658.  
  659.   /* Construct the list of include directories to search.  */
  660.  
  661.   construct_include_path (include_directories == 0 ? (char **) 0
  662.               : include_directories->list);
  663.  
  664.   /* Figure out where we are now, after chdir'ing.  */
  665.   if (directories == 0)
  666.     /* We didn't move, so we're still in the same place.  */
  667.     starting_directory = current_directory;
  668.   else
  669.     {
  670.       if (getcwd (current_directory, GET_PATH_MAX) == 0)
  671.     {
  672. #ifdef    HAVE_GETCWD
  673.       perror_with_name ("getcwd: ", "");
  674. #else
  675.       error ("getwd: %s", current_directory);
  676. #endif
  677.       starting_directory = 0;
  678.     }
  679.       else
  680.     starting_directory = current_directory;
  681.     }
  682.  
  683.   /* Tell the user where he is.  */
  684.  
  685.   if (print_directory_flag)
  686.     log_working_directory (1);
  687.  
  688.   /* Read any stdin makefiles into temporary files.  */
  689.  
  690.   if (makefiles != 0)
  691.     {
  692.       register unsigned int i;
  693.       for (i = 0; i < makefiles->idx; ++i)
  694.     if (makefiles->list[i][0] == '-' && makefiles->list[i][1] == '\0')
  695.       {
  696.         /* This makefile is standard input.  Since we may re-exec
  697.            and thus re-read the makefiles, we read standard input
  698.            into a temporary file and read from that.  */
  699.         static char name[] = "/tmp/GmXXXXXX";
  700.         FILE *outfile;
  701.  
  702.         /* Make a unique filename.  */
  703.         (void) mktemp (name);
  704.  
  705.         outfile = fopen (name, "w");
  706.         if (outfile == 0)
  707.           pfatal_with_name ("fopen (temporary file)");
  708.         while (!feof (stdin))
  709.           {
  710.         char buf[2048];
  711.         int n = fread (buf, 1, sizeof(buf), stdin);
  712.         if (n > 0 && fwrite (buf, 1, n, outfile) != n)
  713.           pfatal_with_name ("fwrite (temporary file)");
  714.           }
  715.         /* Try to make sure we won't remake the temporary
  716.            file when we are re-exec'd.  Kludge-o-matic!  */
  717.         fprintf (outfile, "%s:;\n", name);
  718.         (void) fclose (outfile);
  719.  
  720.         /* Replace the name that read_all_makefiles will
  721.            see with the name of the temporary file.  */
  722.         {
  723.           char *temp;
  724.           /* SGI compiler requires alloca's result be assigned simply.  */
  725.           temp = (char *) alloca (sizeof (name));
  726.           bcopy (name, temp, sizeof (name));
  727.           makefiles->list[i] = temp;
  728.         }
  729.  
  730.         /* Make sure the temporary file will not be remade.  */
  731.         f = enter_file (savestring (name, sizeof name - 1));
  732.         f->updated = 1;
  733.         f->update_status = 0;
  734.         f->command_state = cs_finished;
  735.         /* Let it be removed when we're done.  */
  736.         f->intermediate = 1;
  737.         /* But don't mention it.  */
  738.         f->dontcare = 1;
  739.       }
  740.     }
  741.  
  742.   /* Set up to handle children dying.  This must be done before
  743.      reading in the makefiles so that `shell' function calls will work.  */
  744.  
  745. #ifdef SIGCHLD
  746.   (void) signal (SIGCHLD, child_handler);
  747. #endif
  748. #ifdef SIGCLD
  749.   (void) signal (SIGCLD, child_handler);
  750. #endif
  751.  
  752.   /* Define the initial list of suffixes for old-style rules.  */
  753.  
  754.   set_default_suffixes ();
  755.  
  756.   /* Define the file rules for the built-in suffix rules.  These will later
  757.      be converted into pattern rules.  We used to do this in
  758.      install_default_implicit_rules, but since that happens after reading
  759.      makefiles, it results in the built-in pattern rules taking precedence
  760.      over makefile-specified suffix rules, which is wrong.  */
  761.  
  762.   install_default_suffix_rules ();
  763.  
  764.   /* Define some internal and special variables.  */
  765.  
  766.   define_automatic_variables ();
  767.  
  768.   /* Set up the MAKEFLAGS and MFLAGS variables
  769.      so makefiles can look at them.  */
  770.  
  771.   define_makeflags (0, 0);
  772.  
  773.   /* Define the default variables.  */
  774.   define_default_variables ();
  775.  
  776.   /* Read all the makefiles.  */
  777.  
  778.   default_file = enter_file (".DEFAULT");
  779.  
  780.   read_makefiles
  781.     = read_all_makefiles (makefiles == 0 ? (char **) 0 : makefiles->list);
  782.  
  783.   /* Decode switches again, in case the variables were set by the makefile.  */
  784.   decode_env_switches ("MAKEFLAGS", 9);
  785. #if 0
  786.   decode_env_switches ("MFLAGS", 6);
  787. #endif
  788.  
  789.   /* Set up MAKEFLAGS and MFLAGS again, so they will be right.  */
  790.  
  791.   define_makeflags (1, 0);
  792.  
  793.   ignore_errors_flag |= lookup_file (".IGNORE") != 0;
  794.  
  795.   silent_flag |= lookup_file (".SILENT") != 0;
  796.  
  797.   /* Make each `struct dep' point at the
  798.      `struct file' for the file depended on.  */
  799.  
  800.   snap_deps ();
  801.  
  802.   /* Convert old-style suffix rules to pattern rules.  It is important to
  803.      do this before installing the built-in pattern rules below, so that
  804.      makefile-specified suffix rules take precedence over built-in pattern
  805.      rules.  */
  806.  
  807.   convert_to_pattern ();
  808.  
  809.   /* Install the default implicit pattern rules.
  810.      This used to be done before reading the makefiles.
  811.      But in that case, built-in pattern rules were in the chain
  812.      before user-defined ones, so they matched first.  */
  813.  
  814.   install_default_implicit_rules ();
  815.  
  816.   /* Compute implicit rule limits.  */
  817.  
  818.   count_implicit_rule_limits ();
  819.  
  820.   /* Construct the listings of directories in VPATH lists.  */
  821.  
  822.   build_vpath_lists ();
  823.  
  824.   /* Mark files given with -o flags as very old (00:00:01.00 Jan 1, 1970)
  825.      and as having been updated already, and files given with -W flags as
  826.      brand new (time-stamp as far as possible into the future).  */
  827.  
  828.   if (old_files != 0)
  829.     for (p = old_files->list; *p != 0; ++p)
  830.       {
  831.     f = enter_command_line_file (*p);
  832.     f->last_mtime = (time_t) 1;
  833.     f->updated = 1;
  834.     f->update_status = 0;
  835.     f->command_state = cs_finished;
  836.       }
  837.  
  838.   if (new_files != 0)
  839.     {
  840.       for (p = new_files->list; *p != 0; ++p)
  841.     {
  842.       f = enter_command_line_file (*p);
  843.       f->last_mtime = NEW_MTIME;
  844.     }
  845.     }
  846.  
  847.   if (read_makefiles != 0)
  848.     {
  849.       /* Update any makefiles if necessary.  */
  850.  
  851.       time_t *makefile_mtimes = 0;
  852.       unsigned int mm_idx = 0;
  853.  
  854.       if (debug_flag)
  855.     puts ("Updating makefiles....");
  856.  
  857.       /* Remove any makefiles we don't want to try to update.
  858.      Also record the current modtimes so we can compare them later.  */
  859.       {
  860.     register struct dep *d, *last;
  861.     last = 0;
  862.     d = read_makefiles;
  863.     while (d != 0)
  864.       {
  865.         register struct file *f = d->file;
  866.         if (f->double_colon)
  867.           do
  868.         {
  869.           if (f->deps == 0 && f->cmds != 0)
  870.             {
  871.               /* This makefile is a :: target with commands, but
  872.              no dependencies.  So, it will always be remade.
  873.              This might well cause an infinite loop, so don't
  874.              try to remake it.  (This will only happen if
  875.              your makefiles are written exceptionally
  876.              stupidly; but if you work for Athena, that's how
  877.              you write your makefiles.)  */
  878.  
  879.               if (debug_flag)
  880.             printf ("Makefile `%s' might loop; not remaking it.\n",
  881.                 f->name);
  882.  
  883.               if (last == 0)
  884.             read_makefiles = d->next;
  885.               else
  886.             last->next = d->next;
  887.  
  888.               /* Free the storage.  */
  889.               free ((char *) d);
  890.  
  891.               d = last == 0 ? 0 : last->next;
  892.  
  893.               break;
  894.             }
  895.           f = f->prev;
  896.         }
  897.           while (f != NULL);
  898.         if (f == NULL || !f->double_colon)
  899.           {
  900.         if (makefile_mtimes == 0)
  901.           makefile_mtimes = (time_t *) xmalloc (sizeof (time_t));
  902.         else
  903.           makefile_mtimes = (time_t *)
  904.             xrealloc ((char *) makefile_mtimes,
  905.                   (mm_idx + 1) * sizeof (time_t));
  906.         makefile_mtimes[mm_idx++] = file_mtime_no_search (d->file);
  907.         last = d;
  908.         d = d->next;
  909.           }
  910.       }
  911.       }    
  912.  
  913.       /* Set up `MAKEFLAGS' specially while remaking makefiles.  */
  914.       define_makeflags (1, 1);
  915.  
  916.       switch (update_goal_chain (read_makefiles, 1))
  917.     {
  918.     default:
  919.       abort ();
  920.       
  921.     case -1:
  922.       /* Did nothing.  */
  923.       break;
  924.       
  925.     case 1:
  926.       /* Failed to update.  Figure out if we care.  */
  927.       {
  928.         /* Nonzero if any makefile was successfully remade.  */
  929.         int any_remade = 0;
  930.         /* Nonzero if any makefile we care about failed
  931.            in updating or could not be found at all.  */
  932.         int any_failed = 0;
  933.         register unsigned int i;
  934.  
  935.         for (i = 0; read_makefiles != 0; ++i)
  936.           {
  937.         struct dep *d = read_makefiles;
  938.         read_makefiles = d->next;
  939.         if (d->file->updated)
  940.           {
  941.             /* This makefile was updated.  */
  942.             if (d->file->update_status == 0)
  943.               {
  944.             /* It was successfully updated.  */
  945.             any_remade |= (file_mtime_no_search (d->file)
  946.                        != makefile_mtimes[i]);
  947.               }
  948.             else if (! (d->changed & RM_DONTCARE))
  949.               {
  950.             time_t mtime;
  951.             /* The update failed and this makefile was not
  952.                from the MAKEFILES variable, so we care.  */
  953.             error ("Failed to remake makefile `%s'.",
  954.                    d->file->name);
  955.             mtime = file_mtime_no_search (d->file);
  956.             any_remade |= (mtime != (time_t) -1
  957.                        && mtime != makefile_mtimes[i]);
  958.               }
  959.           }
  960.         else
  961.           /* This makefile was not found at all.  */
  962.           if (! (d->changed & RM_DONTCARE))
  963.             {
  964.               /* This is a makefile we care about.  See how much.  */
  965.               if (d->changed & RM_INCLUDED)
  966.             /* An included makefile.  We don't need
  967.                to die, but we do want to complain.  */
  968.             error ("Included makefile `%s' was not found.",
  969.                    dep_name (d));
  970.               else
  971.             {
  972.               /* A normal makefile.  We must die later.  */
  973.               error ("Makefile `%s' was not found", dep_name (d));
  974.               any_failed = 1;
  975.             }
  976.             }
  977.  
  978.         free ((char *) d);
  979.           }
  980.  
  981.         if (any_remade)
  982.           goto re_exec;
  983.         else if (any_failed)
  984.           die (1);
  985.         else
  986.           break;
  987.       }
  988.  
  989.     case 0:
  990.     re_exec:
  991.       /* Updated successfully.  Re-exec ourselves.  */
  992.  
  993.       remove_intermediates (0);
  994.  
  995.       if (print_data_base_flag)
  996.         print_data_base ();
  997.  
  998.       if (print_directory_flag)
  999.         log_working_directory (0);
  1000.  
  1001.       if (makefiles != 0)
  1002.         {
  1003.           /* These names might have changed.  */
  1004.           register unsigned int i, j = 0;
  1005.           for (i = 1; i < argc; ++i)
  1006.         if (!strcmp (argv[i], "-f")) /* XXX */
  1007.           {
  1008.             char *p = &argv[i][2];
  1009.             if (*p == '\0')
  1010.               argv[++i] = makefiles->list[j];
  1011.             else
  1012.               argv[i] = concat ("-f", makefiles->list[j], "");
  1013.             ++j;
  1014.           }
  1015.         }
  1016.  
  1017.       if (directories != 0 && directories->idx > 0)
  1018.         {
  1019.           char bad;
  1020.           if (directory_before_chdir != 0)
  1021.         {
  1022.           if (chdir (directory_before_chdir) < 0)
  1023.             {
  1024.               perror_with_name ("chdir", "");
  1025.               bad = 1;
  1026.             }
  1027.           else
  1028.             bad = 0;
  1029.         }
  1030.           else
  1031.         bad = 1;
  1032.           if (bad)
  1033.         fatal ("Couldn't change back to original directory.");
  1034.         }
  1035.  
  1036.       for (p = environ; *p != 0; ++p)
  1037.         if (!strncmp (*p, "MAKELEVEL=", 10))
  1038.           {
  1039.         /* The SGI compiler apparently can't understand
  1040.            the concept of storing the result of a function
  1041.            in something other than a local variable.  */
  1042.         char *sgi_loses;
  1043.         sgi_loses = (char *) alloca (40);
  1044.         *p = sgi_loses;
  1045.         sprintf (*p, "MAKELEVEL=%u", makelevel);
  1046.         break;
  1047.           }
  1048.  
  1049.       if (debug_flag)
  1050.         {
  1051.           char **p;
  1052.           fputs ("Re-executing:", stdout);
  1053.           for (p = argv; *p != 0; ++p)
  1054.         printf (" %s", *p);
  1055.           puts ("");
  1056.         }
  1057.  
  1058.       fflush (stdout);
  1059.       fflush (stderr);
  1060.  
  1061.       exec_command (argv, environ);
  1062.       /* NOTREACHED */
  1063.     }
  1064.     }
  1065.  
  1066.   /* Set up `MAKEFLAGS' again for the normal targets.  */
  1067.   define_makeflags (1, 0);
  1068.  
  1069.   {
  1070.     int status;
  1071.  
  1072.     /* If there were no command-line goals, use the default.  */
  1073.     if (goals == 0)
  1074.       {
  1075.     if (default_goal_file != 0)
  1076.       {
  1077.         goals = (struct dep *) xmalloc (sizeof (struct dep));
  1078.         goals->next = 0;
  1079.         goals->name = 0;
  1080.         goals->file = default_goal_file;
  1081.       }
  1082.       }
  1083.     else
  1084.       lastgoal->next = 0;
  1085.  
  1086.     if (goals != 0)
  1087.       {
  1088.     /* Update the goals.  */
  1089.  
  1090.     if (debug_flag)
  1091.       puts ("Updating goal targets....");
  1092.  
  1093.     switch (update_goal_chain (goals, 0))
  1094.       {
  1095.       case -1:
  1096.         /* Nothing happened.  */
  1097.       case 0:
  1098.         /* Updated successfully.  */
  1099.         status = 0;
  1100.         break;
  1101.       case 1:
  1102.         /* Updating failed.  */
  1103.         status = 1;
  1104.         break;
  1105.       default:
  1106.         abort ();
  1107.       }
  1108.       }
  1109.     else
  1110.       {
  1111.     if (read_makefiles == 0)
  1112.       fatal ("No targets specified and no makefile found");
  1113.     else
  1114.       fatal ("No targets");
  1115.       }
  1116.  
  1117.     /* Exit.  */
  1118.     die (status);
  1119.   }
  1120.  
  1121.   return 0;
  1122. }
  1123.  
  1124. /* Parsing of arguments, decoding of switches.  */
  1125.  
  1126. static char options[sizeof (switches) / sizeof (switches[0]) * 3];
  1127. static struct option long_options[(sizeof (switches) / sizeof (switches[0])) +
  1128.                   (sizeof (long_option_aliases) /
  1129.                    sizeof (long_option_aliases[0]))];
  1130.  
  1131. /* Fill in the string and vector for getopt.  */
  1132. static void
  1133. init_switches ()
  1134. {
  1135.   register char *p;
  1136.   register int c;
  1137.   register unsigned int i;
  1138.  
  1139.   if (options[0] != '\0')
  1140.     /* Already done.  */
  1141.     return;
  1142.  
  1143.   p = options;
  1144.   for (i = 0; switches[i].c != '\0'; ++i)
  1145.     {
  1146.       long_options[i].name = (switches[i].long_name == 0 ? "" :
  1147.                   switches[i].long_name);
  1148.       long_options[i].flag = 0;
  1149.       long_options[i].val = switches[i].c;
  1150.       if (isalnum (switches[i].c))
  1151.     *p++ = switches[i].c;
  1152.       switch (switches[i].type)
  1153.     {
  1154.     case flag:
  1155.     case flag_off:
  1156.     case ignore:
  1157.       long_options[i].has_arg = no_argument;
  1158.       break;
  1159.  
  1160.     case string:
  1161.     case positive_int:
  1162.     case floating:
  1163.       if (isalnum (switches[i].c))
  1164.         *p++ = ':';
  1165.       if (switches[i].noarg_value != 0)
  1166.         {
  1167.           if (isalnum (switches[i].c))
  1168.         *p++ = ':';
  1169.           long_options[i].has_arg = optional_argument;
  1170.         }
  1171.       else
  1172.         long_options[i].has_arg = required_argument;
  1173.       break;
  1174.     }
  1175.     }
  1176.   *p = '\0';
  1177.   for (c = 0; c < (sizeof (long_option_aliases) /
  1178.            sizeof (long_option_aliases[0]));
  1179.        ++c)
  1180.     long_options[i++] = long_option_aliases[c];
  1181.   long_options[i].name = 0;
  1182. }
  1183.  
  1184. /* Decode switches from ARGC and ARGV.
  1185.    They came from the environment if ENV is nonzero.  */
  1186.  
  1187. static void
  1188. decode_switches (argc, argv, env)
  1189.      int argc;
  1190.      char **argv;
  1191.      int env;
  1192. {
  1193.   int bad = 0;
  1194.   register const struct command_switch *cs;
  1195.   register struct stringlist *sl;
  1196.   register int c;
  1197.  
  1198.   if (!env)
  1199.     {
  1200.       other_args = (struct stringlist *) xmalloc (sizeof (struct stringlist));
  1201.       other_args->max = argc + 1;
  1202.       other_args->list = (char **) xmalloc ((argc + 1) * sizeof (char *));
  1203.       other_args->idx = 1;
  1204.       other_args->list[0] = argv[0];
  1205.     }
  1206.  
  1207.   /* getopt does most of the parsing for us.
  1208.      First, get its vectors set up.  */
  1209.  
  1210.   init_switches ();
  1211.  
  1212.   /* Let getopt produce error messages for the command line,
  1213.      but not for options from the environment.  */
  1214.   opterr = !env;
  1215.   /* Reset getopt's state.  */
  1216.   optind = 0;
  1217.  
  1218.   while ((c = getopt_long (argc, argv,
  1219.                options, long_options, (int *) 0)) != EOF)
  1220.     {
  1221.       if (c == '?')
  1222.     /* Bad option.  We will print a usage message and die later.
  1223.        But continue to parse the other options so the user can
  1224.        see all he did wrong.  */
  1225.     bad = 1;
  1226.       else
  1227.     for (cs = switches; cs->c != '\0'; ++cs)
  1228.       if (cs->c == c)
  1229.         {
  1230.           /* Whether or not we will actually do anything with
  1231.          this switch.  We test this individually inside the
  1232.          switch below rather than just once outside it, so that
  1233.          options which are to be ignored still consume args.  */
  1234.           int doit = !env || cs->env;
  1235.  
  1236.           switch (cs->type)
  1237.         {
  1238.         default:
  1239.           abort ();
  1240.  
  1241.         case ignore:
  1242.           break;
  1243.  
  1244.         case flag:
  1245.         case flag_off:
  1246.           if (doit)
  1247.             *(int *) cs->value_ptr = cs->type == flag;
  1248.           break;
  1249.  
  1250.         case string:
  1251.           if (!doit)
  1252.             break;
  1253.  
  1254.           if (optarg == 0)
  1255.             optarg = cs->noarg_value;
  1256.  
  1257.           sl = *(struct stringlist **) cs->value_ptr;
  1258.           if (sl == 0)
  1259.             {
  1260.               sl = (struct stringlist *)
  1261.             xmalloc (sizeof (struct stringlist));
  1262.               sl->max = 5;
  1263.               sl->idx = 0;
  1264.               sl->list = (char **) xmalloc (5 * sizeof (char *));
  1265.               *(struct stringlist **) cs->value_ptr = sl;
  1266.             }
  1267.           else if (sl->idx == sl->max - 1)
  1268.             {
  1269.               sl->max += 5;
  1270.               sl->list = (char **)
  1271.             xrealloc ((char *) sl->list,
  1272.                   sl->max * sizeof (char *));
  1273.             }
  1274.           sl->list[sl->idx++] = optarg;
  1275.           sl->list[sl->idx] = 0;
  1276.           break;
  1277.  
  1278.         case positive_int:
  1279.           if (optarg == 0 && argc > optind
  1280.               && isdigit (argv[optind][0]))
  1281.             optarg = argv[optind++];
  1282.  
  1283.           if (!doit)
  1284.             break;
  1285.  
  1286.           if (optarg != 0)
  1287.             {
  1288.               int i = atoi (optarg);
  1289.               if (i < 1)
  1290.             {
  1291.               if (doit)
  1292.                 error ("the `-%c' option requires a \
  1293. positive integral argument",
  1294.                    cs->c);
  1295.               bad = 1;
  1296.             }
  1297.               else
  1298.             *(unsigned int *) cs->value_ptr = i;
  1299.             }
  1300.           else
  1301.             *(unsigned int *) cs->value_ptr
  1302.               = *(unsigned int *) cs->noarg_value;
  1303.           break;
  1304.  
  1305.         case floating:
  1306.           if (optarg == 0 && optind < argc
  1307.               && (isdigit (argv[optind][0]) || argv[optind][0] == '.'))
  1308.             optarg = argv[optind++];
  1309.  
  1310.           if (doit)
  1311.             *(double *) cs->value_ptr            
  1312.               = (optarg != 0 ? atof (optarg)
  1313.              : *(double *) cs->noarg_value);
  1314.  
  1315.           break;
  1316.         }
  1317.         
  1318.           /* We've found the switch.  Stop looking.  */
  1319.           break;
  1320.         }
  1321.     }
  1322.  
  1323.   if (!env)
  1324.     {
  1325.       /* Collect the remaining args in the `other_args' string list.  */
  1326.  
  1327.       while (optind < argc)
  1328.     {
  1329.       char *arg = argv[optind++];
  1330.       if (arg[0] != '-' || arg[1] != '\0')
  1331.         other_args->list[other_args->idx++] = arg;
  1332.     }
  1333.       other_args->list[other_args->idx] = 0;
  1334.     }
  1335.  
  1336.   if (!env && (bad || print_usage_flag))
  1337.     {
  1338.       /* Print a nice usage message.  */
  1339.  
  1340.       if (print_version_flag)
  1341.     print_version ();
  1342.  
  1343.       fprintf (stderr, "Usage: %s [options] [target] ...\n", program);
  1344.  
  1345.       fputs ("Options:\n", stderr);
  1346.       for (cs = switches; cs->c != '\0'; ++cs)
  1347.     {
  1348.       char buf[1024], shortarg[50], longarg[50], *p;
  1349.  
  1350.       if (cs->description[0] == '-')
  1351.         continue;
  1352.  
  1353.       switch (long_options[cs - switches].has_arg)
  1354.         {
  1355.         case no_argument:
  1356.           shortarg[0] = longarg[0] = '\0';
  1357.           break;
  1358.         case required_argument:
  1359.           sprintf (longarg, "=%s", cs->argdesc);
  1360.           sprintf (shortarg, " %s", cs->argdesc);
  1361.           break;
  1362.         case optional_argument:
  1363.           sprintf (longarg, "[=%s]", cs->argdesc);
  1364.           sprintf (shortarg, " [%s]", cs->argdesc);
  1365.           break;
  1366.         }
  1367.  
  1368.       p = buf;
  1369.  
  1370.       if (isalnum (cs->c))
  1371.         {
  1372.           sprintf (buf, "  -%c%s", cs->c, shortarg);
  1373.           p += strlen (p);
  1374.         }
  1375.       if (cs->long_name != 0)
  1376.         {
  1377.           unsigned int i;
  1378.           sprintf (p, "%s--%s%s",
  1379.                !isalnum (cs->c) ? "  " : ", ",
  1380.                cs->long_name, longarg);
  1381.           p += strlen (p);
  1382.           for (i = 0; i < (sizeof (long_option_aliases) /
  1383.                    sizeof (long_option_aliases[0]));
  1384.            ++i)
  1385.         if (long_option_aliases[i].val == cs->c)
  1386.           {
  1387.             sprintf (p, ", --%s%s",
  1388.                  long_option_aliases[i].name, longarg);
  1389.             p += strlen (p);
  1390.           }
  1391.         }
  1392.       {
  1393.         const struct command_switch *ncs = cs;
  1394.         while ((++ncs)->c != '\0')
  1395.           if (ncs->description[0] == '-' &&
  1396.           ncs->description[1] == cs->c)
  1397.         {
  1398.           /* This is another switch that does the same
  1399.              one as the one we are processing.  We want
  1400.              to list them all together on one line.  */
  1401.           sprintf (p, ", -%c%s", ncs->c, shortarg);
  1402.           p += strlen (p);
  1403.           if (ncs->long_name != 0)
  1404.             {
  1405.               sprintf (p, ", --%s%s", ncs->long_name, longarg);
  1406.               p += strlen (p);
  1407.             }
  1408.         }
  1409.       }
  1410.  
  1411.       if (p - buf > DESCRIPTION_COLUMN - 2)
  1412.         /* The list of option names is too long to fit on the same
  1413.            line with the description, leaving at least two spaces.
  1414.            Print it on its own line instead.  */
  1415.         {
  1416.           fprintf (stderr, "%s\n", buf);
  1417.           buf[0] = '\0';
  1418.         }
  1419.  
  1420.       fprintf (stderr, "%*s%s.\n",
  1421.            - DESCRIPTION_COLUMN,
  1422.            buf, cs->description);
  1423.     }
  1424.  
  1425.       die (bad);
  1426.     }
  1427. }
  1428.  
  1429. /* Decode switches from environment variable ENVAR (which is LEN chars long).
  1430.    We do this by chopping the value into a vector of words, prepending a
  1431.    dash to the first word if it lacks one, and passing the vector to
  1432.    decode_switches.  */
  1433.  
  1434. static void
  1435. decode_env_switches (envar, len)
  1436.      char *envar;
  1437.      unsigned int len;
  1438. {
  1439.   char *varref = (char *) alloca (2 + len + 2);
  1440.   char *value, *args;
  1441.   int argc;
  1442.   char **argv;
  1443.  
  1444.   /* Get the variable's value.  */
  1445.   varref[0] = '$';
  1446.   varref[1] = '(';
  1447.   bcopy (envar, &varref[2], len);
  1448.   varref[2 + len] = ')';
  1449.   varref[2 + len + 1] = '\0';
  1450.   value = variable_expand (varref);
  1451.  
  1452.   /* Skip whitespace, and check for an empty value.  */
  1453.   value = next_token (value);
  1454.   len = strlen (value);
  1455.   if (len == 0)
  1456.     return;
  1457.  
  1458.   /* Make a copy of the value in ARGS, where we will munge it.
  1459.      If it does not begin with a dash, prepend one.
  1460.      We must allocate lasting storage for this (and we never free it) because
  1461.      decode_switches may save pointers into it for string-valued switches.  */
  1462.   args = (char *) xmalloc (1 + len + 2);
  1463.   if (value[0] != '-')
  1464.     args[0] = '-';
  1465.   bcopy (value, value[0] == '-' ? args : &args[1], len + 1);
  1466.   /* Write an extra null terminator so our loop below will
  1467.      never be in danger of looking past the end of the string.  */
  1468.   args[(value[0] == '-' ? 0 : 1) + len + 1] = '\0';
  1469.  
  1470.   /* Allocate a vector that is definitely big enough.  */
  1471.   argv = (char **) alloca ((1 + len + 1) * sizeof (char *));
  1472.  
  1473.   /* getopt will look at the arguments starting at ARGV[1].
  1474.      Prepend a spacer word.  */
  1475.   argv[0] = 0;
  1476.   argc = 1;
  1477.   do
  1478.     {
  1479.       argv[argc++] = args;
  1480.       args = end_of_token (args);
  1481.       *args++ = '\0';
  1482.     } while (*args != '\0');
  1483.   argv[argc] = 0;
  1484.  
  1485.   /* Parse those words.  */
  1486.   decode_switches (argc, argv, 1);
  1487. }
  1488.  
  1489. /* Define the MAKEFLAGS and MFLAGS variables to reflect the settings of the
  1490.    command switches.  Include options with args if ALL is nonzero.
  1491.    Don't include options with the `no_makefile' flag set if MAKEFILE.  */
  1492.  
  1493. static void
  1494. define_makeflags (all, makefile)
  1495.      int all, makefile;
  1496. {
  1497.   register const struct command_switch *cs;
  1498.   char *flagstring;
  1499.   struct variable *v;
  1500.  
  1501.   /* We will construct a linked list of `struct flag's describing
  1502.      all the flags which need to go in MAKEFLAGS.  Then, once we
  1503.      know how many there are and their lengths, we can put them all
  1504.      together in a string.  */
  1505.  
  1506.   struct flag
  1507.     {
  1508.       struct flag *next;
  1509.       const struct command_switch *cs;
  1510.       char *arg;
  1511.       unsigned int arglen;
  1512.     };
  1513.   struct flag *flags = 0;
  1514.   unsigned int flagslen = 0;
  1515. #define    ADD_FLAG(ARG, LEN) \
  1516.   do {                                          \
  1517.     struct flag *new = (struct flag *) alloca (sizeof (struct flag));          \
  1518.     new->cs = cs;                                  \
  1519.     new->arg = (ARG);                                  \
  1520.     new->arglen = (LEN);                              \
  1521.     new->next = flags;                                  \
  1522.     flags = new;                                  \
  1523.     if (new->arg == 0)                                  \
  1524.       ++flagslen;        /* Just a single flag letter.  */          \
  1525.     else                                      \
  1526.       flagslen += 1 + 1 + 1 + 1 + new->arglen; /* " -x foo" */              \
  1527.     if (!isalnum (cs->c))                              \
  1528.       /* This switch has no single-letter version, so we use the long.  */    \
  1529.       flagslen += 2 + strlen (cs->long_name);                      \
  1530.   } while (0)
  1531.  
  1532.   for (cs = switches; cs->c != '\0'; ++cs)
  1533.     if (cs->toenv && (!makefile || !cs->no_makefile))
  1534.       switch (cs->type)
  1535.     {
  1536.     default:
  1537.       abort ();
  1538.  
  1539.     case ignore:
  1540.       break;
  1541.  
  1542.     case flag:
  1543.     case flag_off:
  1544.       if (!*(int *) cs->value_ptr == (cs->type == flag_off)
  1545.           && (cs->default_value == 0
  1546.           || *(int *) cs->value_ptr != *(int *) cs->default_value))
  1547.         ADD_FLAG (0, 0);
  1548.       break;
  1549.  
  1550.     case positive_int:
  1551.       if (all)
  1552.         {
  1553.           if ((cs->default_value != 0
  1554.            && (*(unsigned int *) cs->value_ptr
  1555.                == *(unsigned int *) cs->default_value)))
  1556.         break;
  1557.           else if (cs->noarg_value != 0
  1558.                && (*(unsigned int *) cs->value_ptr ==
  1559.                *(unsigned int *) cs->noarg_value))
  1560.         ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
  1561.           else if (cs->c == 'j')
  1562.         /* Special case for `-j'.  */
  1563.         ADD_FLAG ("1", 1);
  1564.           else
  1565.         {
  1566.           char *buf = (char *) alloca (30);
  1567.           sprintf (buf, "%u", *(unsigned int *) cs->value_ptr);
  1568.           ADD_FLAG (buf, strlen (buf));
  1569.         }
  1570.         }
  1571.       break;
  1572.  
  1573.     case floating:
  1574.       if (all)
  1575.         {
  1576.           if (cs->default_value != 0
  1577.           && (*(double *) cs->value_ptr
  1578.               == *(double *) cs->default_value))
  1579.         break;
  1580.           else if (cs->noarg_value != 0
  1581.                && (*(double *) cs->value_ptr
  1582.                == *(double *) cs->noarg_value))
  1583.         ADD_FLAG ("", 0); /* Optional value omitted; see below.  */
  1584.           else
  1585.         {
  1586.           char *buf = (char *) alloca (100);
  1587.           sprintf (buf, "%g", *(double *) cs->value_ptr);
  1588.           ADD_FLAG (buf, strlen (buf));
  1589.         }
  1590.         }
  1591.       break;
  1592.  
  1593.     case string:
  1594.       if (all)
  1595.         {
  1596.           struct stringlist *sl = *(struct stringlist **) cs->value_ptr;
  1597.           if (sl != 0)
  1598.         {
  1599.           /* Add the elements in reverse order, because
  1600.              all the flags get reversed below; and the order
  1601.              matters for some switches (like -I).  */
  1602.           register unsigned int i = sl->idx;
  1603.           while (i-- > 0)
  1604.             ADD_FLAG (sl->list[i], strlen (sl->list[i]));
  1605.         }
  1606.         }
  1607.       break;
  1608.     }
  1609.  
  1610. #undef    ADD_FLAG
  1611.  
  1612.   if (flags == 0)
  1613.     /* No flags.  Use a string of two nulls so [1] works below.  */
  1614.     flagstring = "\0";
  1615.   else
  1616.     {
  1617.       /* Construct the value in FLAGSTRING.
  1618.      We allocate enough space for a preceding dash and trailing null.  */
  1619.       register char *p;
  1620.       flagstring = (char *) alloca (1 + flagslen + 1);
  1621.       p = flagstring;
  1622.       *p++ = '-';
  1623.       do
  1624.     {
  1625.       /* Add the flag letter or name to the string.  */
  1626.       if (!isalnum (flags->cs->c))
  1627.         {
  1628.           *p++ = '-';
  1629.           strcpy (p, flags->cs->long_name);
  1630.           p += strlen (p);
  1631.         }
  1632.       else
  1633.         *p++ = flags->cs->c;
  1634.       if (flags->arg != 0)
  1635.         {
  1636.           /* A flag that takes an optional argument which in this case
  1637.          is omitted is specified by ARG being "" and ARGLEN being 0.
  1638.          We must distinguish because a following flag appended without
  1639.          an intervening " -" is considered the arg for the first.  */
  1640.           if (flags->arglen > 0)
  1641.         {
  1642.           /* Add its argument too.  */
  1643.           *p++ = !isalnum (flags->cs->c) ? '=' : ' ';
  1644.           bcopy (flags->arg, p, flags->arglen);
  1645.           p += flags->arglen;
  1646.         }
  1647.           /* Write a following space and dash, for the next flag.  */
  1648.           *p++ = ' ';
  1649.           *p++ = '-';
  1650.         }
  1651.       else if (!isalnum (flags->cs->c))
  1652.         {
  1653.           /* Long options must each go in their own word,
  1654.          so we write the following space and dash.  */
  1655.           *p++ = ' ';
  1656.           *p++ = '-';
  1657.         }
  1658.       flags = flags->next;
  1659.     } while (flags != 0);
  1660.  
  1661.       if (p[-1] == '-')
  1662.     /* Kill the final space and dash.  */
  1663.     p -= 2;
  1664.  
  1665.       /* Terminate the string.  */
  1666.       *p = '\0';
  1667.     }
  1668.  
  1669.   v = define_variable ("MAKEFLAGS", 9,
  1670.                /* On Sun, the value of MFLAGS starts with a `-' but
  1671.               the value of MAKEFLAGS lacks the `-'.
  1672.               Be compatible with this unless FLAGSTRING starts
  1673.               with a long option `--foo', since removing the
  1674.               first dash would result in the bogus `-foo'.  */
  1675.                flagstring[1] == '-' ? flagstring : &flagstring[1],
  1676.                /* This used to use o_env, but that lost when a
  1677.               makefile defined MAKEFLAGS.  Makefiles set
  1678.               MAKEFLAGS to add switches, but we still want
  1679.               to redefine its value with the full set of
  1680.               switches.  Of course, an override or command
  1681.               definition will still take precedence.  */
  1682.                o_file, 0);
  1683.   if (! all)
  1684.     /* The first time we are called, set MAKEFLAGS to always be exported.
  1685.        We should not do this again on the second call, because that is
  1686.        after reading makefiles which might have done `unexport MAKEFLAGS'. */
  1687.     v->export = v_export;
  1688.   /* Since MFLAGS is not parsed for flags, there is no reason to
  1689.      override any makefile redefinition.  */
  1690.   (void) define_variable ("MFLAGS", 6, flagstring, o_env, 0);
  1691. }
  1692.  
  1693. /* Print version information.  */
  1694.  
  1695. static void
  1696. print_version ()
  1697. {
  1698.   static int printed_version = 0;
  1699.  
  1700.   char *precede = print_data_base_flag ? "# " : "";
  1701.  
  1702.   if (printed_version)
  1703.     /* Do it only once.  */
  1704.     return;
  1705.  
  1706.   printf ("%sGNU Make version %s", precede, version_string);
  1707.   if (remote_description != 0 && *remote_description != '\0')
  1708.     printf ("-%s", remote_description);
  1709.  
  1710.   printf (", by Richard Stallman and Roland McGrath.\n\
  1711. %sCopyright (C) 1988, 89, 90, 91, 92, 93 Free Software Foundation, Inc.\n\
  1712. %sThis is free software; see the source for copying conditions.\n\
  1713. %sThere is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n\
  1714. %sPARTICULAR PURPOSE.\n\n", precede, precede, precede, precede);
  1715.  
  1716.   printed_version = 1;
  1717.  
  1718.   /* Flush stdout so the user doesn't have to wait to see the
  1719.      version information while things are thought about.  */
  1720.   fflush (stdout);
  1721. }
  1722.  
  1723. /* Print a bunch of information about this and that.  */
  1724.  
  1725. static void
  1726. print_data_base ()
  1727. {
  1728.   extern char *ctime ();
  1729.   time_t when;
  1730.  
  1731.   when = time ((time_t *) 0);
  1732.   printf ("\n# Make data base, printed on %s", ctime (&when));
  1733.  
  1734.   print_variable_data_base ();
  1735.   print_dir_data_base ();
  1736.   print_rule_data_base ();
  1737.   print_file_data_base ();
  1738.   print_vpath_data_base ();
  1739.  
  1740.   when = time ((time_t *) 0);
  1741.   printf ("\n# Finished Make data base on %s\n", ctime (&when));
  1742. }
  1743.  
  1744. /* Exit with STATUS, cleaning up as necessary.  */
  1745.  
  1746. void
  1747. die (status)
  1748.      int status;
  1749. {
  1750.   static char dying = 0;
  1751.  
  1752.   if (!dying)
  1753.     {
  1754.       int err;
  1755.  
  1756.       dying = 1;
  1757.  
  1758.       if (print_version_flag)
  1759.     print_version ();
  1760.  
  1761.       /* Wait for children to die.  */
  1762.       for (err = status != 0; job_slots_used > 0; err = 0)
  1763.     reap_children (1, err);
  1764.  
  1765.       /* Remove the intermediate files.  */
  1766.       remove_intermediates (0);
  1767.  
  1768.       if (print_data_base_flag)
  1769.     print_data_base ();
  1770.  
  1771.       if (print_directory_flag)
  1772.     log_working_directory (0);
  1773.     }
  1774.  
  1775.   exit (status);
  1776. }
  1777.  
  1778. /* Write a message indicating that we've just entered or
  1779.    left (according to ENTERING) the current directory.  */
  1780.  
  1781. static void
  1782. log_working_directory (entering)
  1783.      int entering;
  1784. {
  1785.   static int entered = 0;
  1786.   char *message = entering ? "Entering" : "Leaving";
  1787.  
  1788.   if (entering)
  1789.     entered = 1;
  1790.   else if (!entered)
  1791.     /* Don't print the leaving message if we
  1792.        haven't printed the entering message.  */
  1793.     return;
  1794.  
  1795.   if (print_data_base_flag)
  1796.     fputs ("# ", stdout);
  1797.  
  1798.   if (makelevel == 0)
  1799.     printf ("%s: %s ", program, message);
  1800.   else
  1801.     printf ("%s[%u]: %s ", program, makelevel, message);
  1802.  
  1803.   if (starting_directory == 0)
  1804.     puts ("an unknown directory");
  1805.   else
  1806.     printf ("directory `%s'\n", starting_directory);
  1807. }
  1808.