1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 /*
  27  *      main.cc
  28  *
  29  *      make program main routine plus some helper routines
  30  */
  31  
  32 /*
  33  * Included files
  34  */
  35 #if defined(TEAMWARE_MAKE_CMN)
  36 #       include <avo/intl.h>
  37 #endif
  38 
  39 #include <bsd/bsd.h>              /* bsd_signal() */
  40 
  41 
  42 #include <locale.h>               /* setlocale() */
  43 #include <mk/defs.h>
  44 #include <mksdmsi18n/mksdmsi18n.h>        /* libmksdmsi18n_init() */
  45 #include <mksh/macro.h>           /* getvar() */
  46 #include <mksh/misc.h>            /* getmem(), setup_char_semantics() */
  47 
  48 #if defined(TEAMWARE_MAKE_CMN)
  49 #endif
  50 
  51 #include <pwd.h>          /* getpwnam() */
  52 #include <setjmp.h>
  53 #include <signal.h>
  54 #include <stdlib.h>
  55 #include <sys/errno.h>            /* ENOENT */
  56 #include <sys/stat.h>             /* fstat() */
  57 #include <fcntl.h>                /* open() */
  58 
  59 #       include <sys/systeminfo.h>        /* sysinfo() */
  60 
  61 #include <sys/types.h>            /* stat() */
  62 #include <sys/wait.h>             /* wait() */
  63 #include <unistd.h>               /* execv(), unlink(), access() */
  64 #include <vroot/report.h> /* report_dependency(), get_report_file() */
  65 
  66 // From read2.cc
  67 extern  Name            normalize_name(register wchar_t *name_string, register int length);
  68 
  69 // From parallel.cc
  70 #define MAXJOBS_ADJUST_RFE4694000
  71 
  72 #ifdef MAXJOBS_ADJUST_RFE4694000
  73 extern void job_adjust_fini();
  74 #endif /* MAXJOBS_ADJUST_RFE4694000 */
  75 
  76 
  77 /*
  78  * Defined macros
  79  */
  80 #define MAKE_PREFIX             NOCATGETS("/usr")
  81 #define LD_SUPPORT_ENV_VAR      NOCATGETS("SGS_SUPPORT_32")
  82 #define LD_SUPPORT_ENV_VAR_32   NOCATGETS("SGS_SUPPORT_32")
  83 #define LD_SUPPORT_ENV_VAR_64   NOCATGETS("SGS_SUPPORT_64")
  84 #define LD_SUPPORT_MAKE_LIB     NOCATGETS("libmakestate.so.1")
  85 #define LD_SUPPORT_MAKE_LIB_DIR NOCATGETS("/lib")
  86 #define LD_SUPPORT_MAKE_LIB_DIR_64      NOCATGETS("/64")
  87 
  88 /*
  89  * typedefs & structs
  90  */
  91 
  92 /*
  93  * Static variables
  94  */
  95 static  char            *argv_zero_string;
  96 static  Boolean         build_failed_ever_seen;
  97 static  Boolean         continue_after_error_ever_seen; /* `-k' */
  98 static  Boolean         dmake_group_specified;          /* `-g' */
  99 static  Boolean         dmake_max_jobs_specified;       /* `-j' */
 100 static  Boolean         dmake_mode_specified;           /* `-m' */
 101 static  Boolean         dmake_add_mode_specified;       /* `-x' */
 102 static  Boolean         dmake_output_mode_specified;    /* `-x DMAKE_OUTPUT_MODE=' */
 103 static  Boolean         dmake_compat_mode_specified;    /* `-x SUN_MAKE_COMPAT_MODE=' */
 104 static  Boolean         dmake_odir_specified;           /* `-o' */
 105 static  Boolean         dmake_rcfile_specified;         /* `-c' */
 106 static  Boolean         env_wins;                       /* `-e' */
 107 static  Boolean         ignore_default_mk;              /* `-r' */
 108 static  Boolean         list_all_targets;               /* `-T' */
 109 static  int             mf_argc;
 110 static  char            **mf_argv;
 111 static  Dependency_rec  not_auto_depen_struct;
 112 static  Dependency      not_auto_depen = &not_auto_depen_struct;
 113 static  Boolean         pmake_cap_r_specified;          /* `-R' */
 114 static  Boolean         pmake_machinesfile_specified;   /* `-M' */
 115 static  Boolean         stop_after_error_ever_seen;     /* `-S' */
 116 static  Boolean         trace_status;                   /* `-p' */
 117 
 118 #ifdef DMAKE_STATISTICS
 119 static  Boolean         getname_stat = false;
 120 #endif
 121 
 122         static  time_t          start_time;
 123         static  int             g_argc;
 124         static  char            **g_argv;
 125 
 126 /*
 127  * File table of contents
 128  */
 129         extern "C" void         cleanup_after_exit(void);
 130 
 131 extern "C" {
 132         extern  void            dmake_exit_callback(void);
 133         extern  void            dmake_message_callback(char *);
 134 }
 135 
 136 extern  Name            normalize_name(register wchar_t *name_string, register int length);
 137 
 138 extern  int             main(int, char * []);
 139 
 140 static  void            append_makeflags_string(Name, String);
 141 static  void            doalarm(int);
 142 static  void            enter_argv_values(int , char **, ASCII_Dyn_Array *);
 143 static  void            make_targets(int, char **, Boolean);
 144 static  int             parse_command_option(char);
 145 static  void            read_command_options(int, char **);
 146 static  void            read_environment(Boolean);
 147 static  void            read_files_and_state(int, char **);
 148 static  Boolean         read_makefile(Name, Boolean, Boolean, Boolean);
 149 static  void            report_recursion(Name);
 150 static  void            set_sgs_support(void);
 151 static  void            setup_for_projectdir(void);
 152 static  void            setup_makeflags_argv(void);
 153 static  void            report_dir_enter_leave(Boolean entering);
 154 
 155 extern void expand_value(Name, register String , Boolean);
 156 
 157 static const char       verstring[] = "illumos make";
 158 
 159 jmp_buf jmpbuffer;
 160 extern nl_catd catd;
 161 
 162 /*
 163  *      main(argc, argv)
 164  *
 165  *      Parameters:
 166  *              argc                    You know what this is
 167  *              argv                    You know what this is
 168  *
 169  *      Static variables used:
 170  *              list_all_targets        make -T seen
 171  *              trace_status            make -p seen
 172  *
 173  *      Global variables used:
 174  *              debug_level             Should we trace make actions?
 175  *              keep_state              Set if .KEEP_STATE seen
 176  *              makeflags               The Name "MAKEFLAGS", used to get macro
 177  *              remote_command_name     Name of remote invocation cmd ("on")
 178  *              running_list            List of parallel running processes
 179  *              stdout_stderr_same      true if stdout and stderr are the same
 180  *              auto_dependencies       The Name "SUNPRO_DEPENDENCIES"
 181  *              temp_file_directory     Set to the dir where we create tmp file
 182  *              trace_reader            Set to reflect tracing status
 183  *              working_on_targets      Set when building user targets
 184  */
 185 int
 186 main(int argc, char *argv[])
 187 {
 188         /*
 189          * cp is a -> to the value of the MAKEFLAGS env var,
 190          * which has to be regular chars.
 191          */
 192         register char           *cp;
 193         char                    make_state_dir[MAXPATHLEN];
 194         Boolean                 parallel_flag = false;
 195         char                    *prognameptr;
 196         char                    *slash_ptr;
 197         mode_t                  um;
 198         int                     i;
 199         struct itimerval        value;
 200         char                    def_dmakerc_path[MAXPATHLEN];
 201         Name                    dmake_name, dmake_name2;
 202         Name                    dmake_value, dmake_value2;
 203         Property                prop, prop2;
 204         struct stat             statbuf;
 205         int                     statval;
 206 
 207         struct stat             out_stat, err_stat;
 208         hostid = gethostid();
 209         bsd_signals();
 210 
 211         (void) setlocale(LC_ALL, "");
 212 
 213 
 214 #ifdef DMAKE_STATISTICS
 215         if (getenv(NOCATGETS("DMAKE_STATISTICS"))) {
 216                 getname_stat = true;
 217         }
 218 #endif
 219 
 220         catd = catopen(AVO_DOMAIN_DMAKE, NL_CAT_LOCALE);
 221 
 222 // ---> fprintf(stderr, catgets(catd, 15, 666, "--- SUN make ---\n"));
 223 
 224 
 225 /*
 226  * I put libmksdmsi18n_init() under #ifdef because it requires avo_i18n_init()
 227  * from avo_util library. 
 228  */
 229         libmksdmsi18n_init();
 230 
 231 
 232         textdomain(NOCATGETS("SUNW_SPRO_MAKE"));
 233 
 234         g_argc = argc;
 235         g_argv = (char **) malloc((g_argc + 1) * sizeof(char *));
 236         for (i = 0; i < argc; i++) {
 237                 g_argv[i] = argv[i];
 238         }
 239         g_argv[i] = NULL;
 240 
 241         /*
 242          * Set argv_zero_string to some form of argv[0] for
 243          * recursive MAKE builds.
 244          */
 245 
 246         if (*argv[0] == (int) slash_char) {
 247                 /* argv[0] starts with a slash */
 248                 argv_zero_string = strdup(argv[0]);
 249         } else if (strchr(argv[0], (int) slash_char) == NULL) {
 250                 /* argv[0] contains no slashes */
 251                 argv_zero_string = strdup(argv[0]);
 252         } else {
 253                 /*
 254                  * argv[0] contains at least one slash,
 255                  * but doesn't start with a slash
 256                  */
 257                 char    *tmp_current_path;
 258                 char    *tmp_string;
 259 
 260                 tmp_current_path = get_current_path();
 261                 tmp_string = getmem(strlen(tmp_current_path) + 1 +
 262                                     strlen(argv[0]) + 1);
 263                 (void) sprintf(tmp_string,
 264                                "%s/%s",
 265                                tmp_current_path,
 266                                argv[0]);
 267                 argv_zero_string = strdup(tmp_string);
 268                 retmem_mb(tmp_string);
 269         }
 270 
 271         /* 
 272          * The following flags are reset if we don't have the 
 273          * (.nse_depinfo or .make.state) files locked and only set 
 274          * AFTER the file has been locked. This ensures that if the user
 275          * interrupts the program while file_lock() is waiting to lock
 276          * the file, the interrupt handler doesn't remove a lock 
 277          * that doesn't belong to us.
 278          */
 279         make_state_lockfile = NULL;
 280         make_state_locked = false;
 281 
 282 
 283         /*
 284          * look for last slash char in the path to look at the binary 
 285          * name. This is to resolve the hard link and invoke make
 286          * in svr4 mode.
 287          */
 288 
 289         /* Sun OS make standart */
 290         svr4 = false;  
 291         posix = false;
 292         if(!strcmp(argv_zero_string, NOCATGETS("/usr/xpg4/bin/make"))) {
 293                 svr4 = false;
 294                 posix = true;
 295         } else {
 296                 prognameptr = strrchr(argv[0], '/');
 297                 if(prognameptr) {
 298                         prognameptr++;
 299                 } else {
 300                         prognameptr = argv[0];
 301                 }
 302                 if(!strcmp(prognameptr, NOCATGETS("svr4.make"))) {
 303                         svr4 = true;
 304                         posix = false;
 305                 }
 306         }
 307         if (getenv(USE_SVR4_MAKE) || getenv(NOCATGETS("USE_SVID"))){
 308            svr4 = true;
 309            posix = false;
 310         }
 311 
 312         /*
 313          * Find the dmake_compat_mode: posix, sun, svr4, or gnu_style, .
 314          */
 315         char * dmake_compat_mode_var = getenv(NOCATGETS("SUN_MAKE_COMPAT_MODE"));
 316         if (dmake_compat_mode_var != NULL) {
 317                 if (0 == strcasecmp(dmake_compat_mode_var, NOCATGETS("GNU"))) {
 318                         gnu_style = true;
 319                 }
 320                 //svr4 = false;
 321                 //posix = false;
 322         }
 323 
 324         /*
 325          * Temporary directory set up.
 326          */
 327         char * tmpdir_var = getenv(NOCATGETS("TMPDIR"));
 328         if (tmpdir_var != NULL && *tmpdir_var == '/' && strlen(tmpdir_var) < MAXPATHLEN) {
 329                 strcpy(mbs_buffer, tmpdir_var);
 330                 for (tmpdir_var = mbs_buffer+strlen(mbs_buffer);
 331                         *(--tmpdir_var) == '/' && tmpdir_var > mbs_buffer;
 332                         *tmpdir_var = '\0');
 333                 if (strlen(mbs_buffer) + 32 < MAXPATHLEN) { /* 32 = strlen("/dmake.stdout.%d.%d.XXXXXX") */
 334                         sprintf(mbs_buffer2, NOCATGETS("%s/dmake.tst.%d.XXXXXX"),
 335                                 mbs_buffer, getpid());
 336                         int fd = mkstemp(mbs_buffer2);
 337                         if (fd >= 0) {
 338                                 close(fd);
 339                                 unlink(mbs_buffer2);
 340                                 tmpdir = strdup(mbs_buffer);
 341                         }
 342                 }
 343         }
 344 
 345         /* find out if stdout and stderr point to the same place */
 346         if (fstat(1, &out_stat) < 0) {
 347                 fatal(catgets(catd, 1, 165, "fstat of standard out failed: %s"), errmsg(errno));
 348         }
 349         if (fstat(2, &err_stat) < 0) {
 350                 fatal(catgets(catd, 1, 166, "fstat of standard error failed: %s"), errmsg(errno));
 351         }
 352         if ((out_stat.st_dev == err_stat.st_dev) &&
 353             (out_stat.st_ino == err_stat.st_ino)) {
 354                 stdout_stderr_same = true;
 355         } else {
 356                 stdout_stderr_same = false;
 357         }
 358         /* Make the vroot package scan the path using shell semantics */
 359         set_path_style(0);
 360 
 361         setup_char_semantics();
 362 
 363         setup_for_projectdir();
 364 
 365         /*
 366          * If running with .KEEP_STATE, curdir will be set with
 367          * the connected directory.
 368          */
 369         (void) atexit(cleanup_after_exit);
 370 
 371         load_cached_names();
 372 
 373 /*
 374  *      Set command line flags
 375  */
 376         setup_makeflags_argv();
 377         read_command_options(mf_argc, mf_argv);
 378         read_command_options(argc, argv);
 379         if (debug_level > 0) {
 380                 cp = getenv(makeflags->string_mb);
 381                 (void) printf(catgets(catd, 1, 167, "MAKEFLAGS value: %s\n"), cp == NULL ? "" : cp);
 382         }
 383 
 384         setup_interrupt(handle_interrupt);
 385 
 386         read_files_and_state(argc, argv);
 387 
 388         /*
 389          * Find the dmake_output_mode: TXT1, TXT2 or HTML1.
 390          */
 391         MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_OUTPUT_MODE"));
 392         dmake_name2 = GETNAME(wcs_buffer, FIND_LENGTH);
 393         prop2 = get_prop(dmake_name2->prop, macro_prop);
 394         if (prop2 == NULL) {
 395                 /* DMAKE_OUTPUT_MODE not defined, default to TXT1 mode */
 396                 output_mode = txt1_mode;
 397         } else {
 398                 dmake_value2 = prop2->body.macro.value;
 399                 if ((dmake_value2 == NULL) ||
 400                     (IS_EQUAL(dmake_value2->string_mb, NOCATGETS("TXT1")))) {
 401                         output_mode = txt1_mode;
 402                 } else if (IS_EQUAL(dmake_value2->string_mb, NOCATGETS("TXT2"))) {
 403                         output_mode = txt2_mode;
 404                 } else if (IS_EQUAL(dmake_value2->string_mb, NOCATGETS("HTML1"))) {
 405                         output_mode = html1_mode;
 406                 } else {
 407                         warning(catgets(catd, 1, 352, "Unsupported value `%s' for DMAKE_OUTPUT_MODE after -x flag (ignored)"),
 408                               dmake_value2->string_mb);
 409                 }
 410         }
 411         /*
 412          * Find the dmake_mode: distributed, parallel, or serial.
 413          */
 414     if ((!pmake_cap_r_specified) &&
 415         (!pmake_machinesfile_specified)) {
 416         MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_MODE"));
 417         dmake_name2 = GETNAME(wcs_buffer, FIND_LENGTH);
 418         prop2 = get_prop(dmake_name2->prop, macro_prop);
 419         if (prop2 == NULL) {
 420                 /* DMAKE_MODE not defined, default to distributed mode */
 421                 dmake_mode_type = distributed_mode;
 422                 no_parallel = false;
 423         } else {
 424                 dmake_value2 = prop2->body.macro.value;
 425                 if ((dmake_value2 == NULL) ||
 426                     (IS_EQUAL(dmake_value2->string_mb, NOCATGETS("distributed")))) {
 427                         dmake_mode_type = distributed_mode;
 428                         no_parallel = false;
 429                 } else if (IS_EQUAL(dmake_value2->string_mb, NOCATGETS("parallel"))) {
 430                         dmake_mode_type = parallel_mode;
 431                         no_parallel = false;
 432                 } else if (IS_EQUAL(dmake_value2->string_mb, NOCATGETS("serial"))) {
 433                         dmake_mode_type = serial_mode;
 434                         no_parallel = true;
 435                 } else {
 436                         fatal(catgets(catd, 1, 307, "Unknown dmake mode argument `%s' after -m flag"), dmake_value2->string_mb);
 437                 }
 438         }
 439 
 440         if ((!list_all_targets) &&
 441             (report_dependencies_level == 0)) {
 442                 /*
 443                  * Check to see if either DMAKE_RCFILE or DMAKE_MODE is defined.
 444                  * They could be defined in the env, in the makefile, or on the
 445                  * command line.
 446                  * If neither is defined, and $(HOME)/.dmakerc does not exists,
 447                  * then print a message, and default to parallel mode.
 448                  */
 449                 if(dmake_mode_type == distributed_mode) {
 450                         dmake_mode_type = parallel_mode;
 451                         no_parallel = false;
 452                 }
 453         }
 454     }
 455 
 456         parallel_flag = true;
 457         putenv(strdup(NOCATGETS("DMAKE_CHILD=TRUE")));
 458 
 459 //
 460 // If dmake is running with -t option, set dmake_mode_type to serial.
 461 // This is done because doname() calls touch_command() that runs serially.
 462 // If we do not do that, maketool will have problems. 
 463 //
 464         if(touch) {
 465                 dmake_mode_type = serial_mode;
 466                 no_parallel = true;
 467         }
 468 
 469         /*
 470          * Check whether stdout and stderr are physically same.
 471          * This is in order to decide whether we need to redirect
 472          * stderr separately from stdout.
 473          * This check is performed only if __DMAKE_SEPARATE_STDERR
 474          * is not set. This variable may be used in order to preserve
 475          * the 'old' behaviour.
 476          */
 477         out_err_same = true;
 478         char * dmake_sep_var = getenv(NOCATGETS("__DMAKE_SEPARATE_STDERR"));
 479         if (dmake_sep_var == NULL || (0 != strcasecmp(dmake_sep_var, NOCATGETS("NO")))) {
 480                 struct stat stdout_stat;
 481                 struct stat stderr_stat;
 482                 if( (fstat(1, &stdout_stat) == 0)
 483                  && (fstat(2, &stderr_stat) == 0) )
 484                 {
 485                         if( (stdout_stat.st_dev != stderr_stat.st_dev)
 486                          || (stdout_stat.st_ino != stderr_stat.st_ino) )
 487                         {
 488                                 out_err_same = false;
 489                         }
 490                 }
 491         }
 492 
 493                 
 494 /*
 495  *      Enable interrupt handler for alarms
 496  */
 497         (void) bsd_signal(SIGALRM, (SIG_PF)doalarm);
 498 
 499 /*
 500  *      Check if make should report
 501  */
 502         if (getenv(sunpro_dependencies->string_mb) != NULL) {
 503                 FILE    *report_file;
 504 
 505                 report_dependency("");
 506                 report_file = get_report_file();
 507                 if ((report_file != NULL) && (report_file != (FILE*)-1)) {
 508                         (void) fprintf(report_file, "\n");
 509                 }
 510         }
 511 
 512 /*
 513  *      Make sure SUNPRO_DEPENDENCIES is exported (or not) properly.
 514  */
 515         if (keep_state) {
 516                 maybe_append_prop(sunpro_dependencies, macro_prop)->
 517                   body.macro.exported = true;
 518         } else {
 519                 maybe_append_prop(sunpro_dependencies, macro_prop)->
 520                   body.macro.exported = false;
 521         }
 522 
 523         working_on_targets = true;
 524         if (trace_status) {
 525                 dump_make_state();
 526                 fclose(stdout);
 527                 fclose(stderr);
 528                 exit_status = 0;
 529                 exit(0);
 530         }
 531         if (list_all_targets) {
 532                 dump_target_list();
 533                 fclose(stdout);
 534                 fclose(stderr);
 535                 exit_status = 0;
 536                 exit(0);
 537         }
 538         trace_reader = false;
 539 
 540         /*
 541          * Set temp_file_directory to the directory the .make.state
 542          * file is written to.
 543          */
 544         if ((slash_ptr = strrchr(make_state->string_mb, (int) slash_char)) == NULL) {
 545                 temp_file_directory = strdup(get_current_path());
 546         } else {
 547                 *slash_ptr = (int) nul_char;
 548                 (void) strcpy(make_state_dir, make_state->string_mb);
 549                 *slash_ptr = (int) slash_char;
 550                    /* when there is only one slash and it's the first
 551                    ** character, make_state_dir should point to '/'.
 552                    */
 553                 if(make_state_dir[0] == '\0') {
 554                    make_state_dir[0] = '/';
 555                    make_state_dir[1] = '\0';
 556                 }
 557                 if (make_state_dir[0] == (int) slash_char) {
 558                         temp_file_directory = strdup(make_state_dir);
 559                 } else {
 560                         char    tmp_current_path2[MAXPATHLEN];
 561  
 562                         (void) sprintf(tmp_current_path2,
 563                                        "%s/%s",
 564                                        get_current_path(),
 565                                        make_state_dir);
 566                         temp_file_directory = strdup(tmp_current_path2);
 567                 }
 568         }
 569 
 570 
 571         report_dir_enter_leave(true);
 572 
 573         make_targets(argc, argv, parallel_flag);
 574 
 575         report_dir_enter_leave(false);
 576 
 577         if (build_failed_ever_seen) {
 578                 if (posix) {
 579                         exit_status = 1;
 580                 }
 581                 exit(1);
 582         }
 583         exit_status = 0;
 584         exit(0);
 585         /* NOTREACHED */
 586 }
 587 
 588 /*
 589  *      cleanup_after_exit()
 590  *
 591  *      Called from exit(), performs cleanup actions.
 592  *
 593  *      Parameters:
 594  *              status          The argument exit() was called with
 595  *              arg             Address of an argument vector to
 596  *                              cleanup_after_exit()
 597  *
 598  *      Global variables used:
 599  *              command_changed Set if we think .make.state should be rewritten
 600  *              current_line    Is set we set commands_changed
 601  *              do_not_exec_rule
 602  *                              True if -n flag on
 603  *              done            The Name ".DONE", rule we run
 604  *              keep_state      Set if .KEEP_STATE seen
 605  *              parallel        True if building in parallel
 606  *              quest           If -q is on we do not run .DONE
 607  *              report_dependencies
 608  *                              True if -P flag on
 609  *              running_list    List of parallel running processes
 610  *              temp_file_name  The temp file is removed, if any
 611  *              catd    the message catalog file
 612  */
 613 extern "C" void
 614 cleanup_after_exit(void)
 615 {
 616         Running         rp;
 617 
 618 extern long     getname_bytes_count;
 619 extern long     getname_names_count;
 620 extern long     getname_struct_count;
 621 extern long     freename_bytes_count;
 622 extern long     freename_names_count;
 623 extern long     freename_struct_count;
 624 extern long     other_alloc;
 625 
 626 extern long     env_alloc_num;
 627 extern long     env_alloc_bytes;
 628 
 629 
 630 #ifdef DMAKE_STATISTICS
 631 if(getname_stat) {
 632         printf(NOCATGETS(">>> Getname statistics:\n"));
 633         printf(NOCATGETS("  Allocated:\n"));
 634         printf(NOCATGETS("        Names: %ld\n"), getname_names_count);
 635         printf(NOCATGETS("      Strings: %ld Kb (%ld bytes)\n"), getname_bytes_count/1000, getname_bytes_count);
 636         printf(NOCATGETS("      Structs: %ld Kb (%ld bytes)\n"), getname_struct_count/1000, getname_struct_count);
 637         printf(NOCATGETS("  Total bytes: %ld Kb (%ld bytes)\n"), getname_struct_count/1000 + getname_bytes_count/1000, getname_struct_count + getname_bytes_count);
 638 
 639         printf(NOCATGETS("\n  Unallocated: %ld\n"), freename_names_count);
 640         printf(NOCATGETS("        Names: %ld\n"), freename_names_count);
 641         printf(NOCATGETS("      Strings: %ld Kb (%ld bytes)\n"), freename_bytes_count/1000, freename_bytes_count);
 642         printf(NOCATGETS("      Structs: %ld Kb (%ld bytes)\n"), freename_struct_count/1000, freename_struct_count);
 643         printf(NOCATGETS("  Total bytes: %ld Kb (%ld bytes)\n"), freename_struct_count/1000 + freename_bytes_count/1000, freename_struct_count + freename_bytes_count);
 644 
 645         printf(NOCATGETS("\n  Total used: %ld Kb (%ld bytes)\n"), (getname_struct_count/1000 + getname_bytes_count/1000) - (freename_struct_count/1000 + freename_bytes_count/1000), (getname_struct_count + getname_bytes_count) - (freename_struct_count + freename_bytes_count));
 646 
 647         printf(NOCATGETS("\n>>> Other:\n"));
 648         printf(
 649                 NOCATGETS("       Env (%ld): %ld Kb (%ld bytes)\n"),
 650                 env_alloc_num,
 651                 env_alloc_bytes/1000,
 652                 env_alloc_bytes
 653         );
 654 
 655 }
 656 #endif
 657 
 658         parallel = false;
 659         /* If we used the SVR4_MAKE, don't build .DONE or .FAILED */
 660         if (!getenv(USE_SVR4_MAKE)){
 661             /* Build the target .DONE or .FAILED if we caught an error */
 662             if (!quest && !list_all_targets) {
 663                 Name            failed_name;
 664 
 665                 MBSTOWCS(wcs_buffer, NOCATGETS(".FAILED"));
 666                 failed_name = GETNAME(wcs_buffer, FIND_LENGTH);
 667                 if ((exit_status != 0) && (failed_name->prop != NULL)) {
 668                         /*
 669                          * [tolik] switch DMake to serial mode
 670                          */
 671                         dmake_mode_type = serial_mode;
 672                         no_parallel = true;
 673                         (void) doname(failed_name, false, true);
 674                 } else {
 675                     if (!trace_status) {
 676                         /*
 677                          * Switch DMake to serial mode
 678                          */
 679                         dmake_mode_type = serial_mode;
 680                         no_parallel = true;
 681                         (void) doname(done, false, true);
 682                     }
 683                 }
 684             }
 685         }
 686         /*
 687          * Remove the temp file utilities report dependencies thru if it
 688          * is still around
 689          */
 690         if (temp_file_name != NULL) {
 691                 (void) unlink(temp_file_name->string_mb);
 692         }
 693         /*
 694          * Do not save the current command in .make.state if make
 695          * was interrupted.
 696          */
 697         if (current_line != NULL) {
 698                 command_changed = true;
 699                 current_line->body.line.command_used = NULL;
 700         }
 701         /*
 702          * For each parallel build process running, remove the temp files
 703          * and zap the command line so it won't be put in .make.state
 704          */
 705         for (rp = running_list; rp != NULL; rp = rp->next) {
 706                 if (rp->temp_file != NULL) {
 707                         (void) unlink(rp->temp_file->string_mb);
 708                 }
 709                 if (rp->stdout_file != NULL) {
 710                         (void) unlink(rp->stdout_file);
 711                         retmem_mb(rp->stdout_file);
 712                         rp->stdout_file = NULL;
 713                 }
 714                 if (rp->stderr_file != NULL) {
 715                         (void) unlink(rp->stderr_file);
 716                         retmem_mb(rp->stderr_file);
 717                         rp->stderr_file = NULL;
 718                 }
 719                 command_changed = true;
 720 /*
 721                 line = get_prop(rp->target->prop, line_prop);
 722                 if (line != NULL) {
 723                         line->body.line.command_used = NULL;
 724                 }
 725  */
 726         }
 727         /* Remove the statefile lock file if the file has been locked */
 728         if ((make_state_lockfile != NULL) && (make_state_locked)) {
 729                 (void) unlink(make_state_lockfile);
 730                 make_state_lockfile = NULL;
 731                 make_state_locked = false;
 732         }
 733         /* Write .make.state */
 734         write_state_file(1, (Boolean) 1);
 735 
 736 #if defined (TEAMWARE_MAKE_CMN) && defined (MAXJOBS_ADJUST_RFE4694000)
 737         job_adjust_fini();
 738 #endif
 739 
 740 #ifdef TEAMWARE_MAKE_CMN
 741         catclose(catd);
 742 #endif
 743 }
 744 
 745 /*
 746  *      handle_interrupt()
 747  *
 748  *      This is where C-C traps are caught.
 749  *
 750  *      Parameters:
 751  *
 752  *      Global variables used (except DMake 1.0):
 753  *              current_target          Sometimes the current target is removed
 754  *              do_not_exec_rule        But not if -n is on
 755  *              quest                   or -q
 756  *              running_list            List of parallel running processes
 757  *              touch                   Current target is not removed if -t on
 758  */
 759 void
 760 handle_interrupt(int)
 761 {
 762         Property                member;
 763         Running                 rp;
 764 
 765         (void) fflush(stdout);
 766         if (childPid > 0) {
 767                 kill(childPid, SIGTERM);
 768                 childPid = -1;
 769         }
 770         for (rp = running_list; rp != NULL; rp = rp->next) {
 771                 if (rp->state != build_running) {
 772                         continue;
 773                 }
 774                 if (rp->pid > 0) {
 775                         kill(rp->pid, SIGTERM);
 776                         rp->pid = -1;
 777                 }
 778         }
 779         if (getpid() == getpgrp()) {
 780                 bsd_signal(SIGTERM, SIG_IGN);
 781                 kill (-getpid(), SIGTERM);
 782         }
 783         /* Clean up all parallel/distributed children already finished */
 784         finish_children(false);
 785 
 786         /* Make sure the processes running under us terminate first */
 787 
 788         while (wait((int *) NULL) != -1);
 789         /* Delete the current targets unless they are precious */
 790         if ((current_target != NULL) &&
 791             current_target->is_member &&
 792             ((member = get_prop(current_target->prop, member_prop)) != NULL)) {
 793                 current_target = member->body.member.library;
 794         }
 795         if (!do_not_exec_rule &&
 796             !touch &&
 797             !quest &&
 798             (current_target != NULL) &&
 799             !(current_target->stat.is_precious || all_precious)) {
 800 
 801 /* BID_1030811 */
 802 /* azv 16 Oct 95 */
 803                 current_target->stat.time = file_no_time; 
 804 
 805                 if (exists(current_target) != file_doesnt_exist) {
 806                         (void) fprintf(stderr,
 807                                        "\n*** %s ",
 808                                        current_target->string_mb);
 809                         if (current_target->stat.is_dir) {
 810                                 (void) fprintf(stderr,
 811                                                catgets(catd, 1, 168, "not removed.\n"),
 812                                                current_target->string_mb);
 813                         } else if (unlink(current_target->string_mb) == 0) {
 814                                 (void) fprintf(stderr,
 815                                                catgets(catd, 1, 169, "removed.\n"),
 816                                                current_target->string_mb);
 817                         } else {
 818                                 (void) fprintf(stderr,
 819                                                catgets(catd, 1, 170, "could not be removed: %s.\n"),
 820                                                current_target->string_mb,
 821                                                errmsg(errno));
 822                         }
 823                 }
 824         }
 825         for (rp = running_list; rp != NULL; rp = rp->next) {
 826                 if (rp->state != build_running) {
 827                         continue;
 828                 }
 829                 if (rp->target->is_member &&
 830                     ((member = get_prop(rp->target->prop, member_prop)) !=
 831                      NULL)) {
 832                         rp->target = member->body.member.library;
 833                 }
 834                 if (!do_not_exec_rule &&
 835                     !touch &&
 836                     !quest &&
 837                     !(rp->target->stat.is_precious || all_precious)) {
 838 
 839                         rp->target->stat.time = file_no_time; 
 840                         if (exists(rp->target) != file_doesnt_exist) {
 841                                 (void) fprintf(stderr,
 842                                                "\n*** %s ",
 843                                                rp->target->string_mb);
 844                                 if (rp->target->stat.is_dir) {
 845                                         (void) fprintf(stderr,
 846                                                        catgets(catd, 1, 171, "not removed.\n"),
 847                                                        rp->target->string_mb);
 848                                 } else if (unlink(rp->target->string_mb) == 0) {
 849                                         (void) fprintf(stderr,
 850                                                        catgets(catd, 1, 172, "removed.\n"),
 851                                                        rp->target->string_mb);
 852                                 } else {
 853                                         (void) fprintf(stderr,
 854                                                        catgets(catd, 1, 173, "could not be removed: %s.\n"),
 855                                                        rp->target->string_mb,
 856                                                        errmsg(errno));
 857                                 }
 858                         }
 859                 }
 860         }
 861 
 862 
 863         /* Have we locked .make.state or .nse_depinfo? */
 864         if ((make_state_lockfile != NULL) && (make_state_locked)) {
 865                 unlink(make_state_lockfile);
 866                 make_state_lockfile = NULL;
 867                 make_state_locked = false;
 868         }
 869         /*
 870          * Re-read .make.state file (it might be changed by recursive make)
 871          */
 872         check_state(NULL);
 873 
 874         report_dir_enter_leave(false);
 875 
 876         exit_status = 2;
 877         exit(2);
 878 }
 879 
 880 /*
 881  *      doalarm(sig, ...)
 882  *
 883  *      Handle the alarm interrupt but do nothing.  Side effect is to
 884  *      cause return from wait3.
 885  *
 886  *      Parameters:
 887  *              sig
 888  *
 889  *      Global variables used:
 890  */
 891 /*ARGSUSED*/
 892 static void
 893 doalarm(int)
 894 {
 895         return;
 896 }
 897 
 898 
 899 /*
 900  *      read_command_options(argc, argv)
 901  *
 902  *      Scan the cmd line options and process the ones that start with "-"
 903  *
 904  *      Return value:
 905  *                              -M argument, if any
 906  *
 907  *      Parameters:
 908  *              argc            You know what this is
 909  *              argv            You know what this is
 910  *
 911  *      Global variables used:
 912  */
 913 static void
 914 read_command_options(register int argc, register char **argv)
 915 {
 916         register int            ch;
 917         int                     current_optind = 1;
 918         int                     last_optind_with_double_hyphen = 0;
 919         int                     last_optind;
 920         int                     last_current_optind;
 921         register int            i;
 922         register int            j;
 923         register int            k;
 924         register int            makefile_next = 0; /*
 925                                                     * flag to note options:
 926                                                     * -c, f, g, j, m, o
 927                                                     */
 928         const char              *tptr;
 929         const char              *CMD_OPTS;
 930 
 931         extern char             *optarg;
 932         extern int              optind, opterr, optopt;
 933 
 934 #define SUNPRO_CMD_OPTS "-~Bbc:Ddef:g:ij:K:kM:m:NnO:o:PpqRrSsTtuVvwx:"
 935 
 936 #       define SVR4_CMD_OPTS   "-c:ef:g:ij:km:nO:o:pqrsTtVv"
 937 
 938         /*
 939          * Added V in SVR4_CMD_OPTS also, which is going to be a hidden
 940          * option, just to make sure that the getopt doesn't fail when some
 941          * users leave their USE_SVR4_MAKE set and try to use the makefiles
 942          * that are designed to issue commands like $(MAKE) -V. Anyway it
 943          * sets the same flag but ensures that getopt doesn't fail.
 944          */
 945 
 946         opterr = 0;
 947         optind = 1;
 948         while (1) {
 949                 last_optind=optind;                     /* Save optind and current_optind values */
 950                 last_current_optind=current_optind;     /* in case we have to repeat this round. */
 951                 if (svr4) {
 952                         CMD_OPTS=SVR4_CMD_OPTS;
 953                         ch = getopt(argc, argv, SVR4_CMD_OPTS);
 954                 } else {
 955                         CMD_OPTS=SUNPRO_CMD_OPTS;
 956                         ch = getopt(argc, argv, SUNPRO_CMD_OPTS);
 957                 }
 958                 if (ch == EOF) {
 959                         if(optind < argc) {
 960                                 /*
 961                                  * Fixing bug 4102537:
 962                                  *    Strange behaviour of command make using -- option.
 963                                  * Not all argv have been processed
 964                                  * Skip non-flag argv and continue processing.
 965                                  */
 966                                 optind++;
 967                                 current_optind++;
 968                                 continue;
 969                         } else {
 970                                 break;
 971                         }
 972 
 973                 }
 974                 if (ch == '?') {
 975                         if (optopt == '-') {
 976                                 /* Bug 5060758: getopt() changed behavior (s10_60),
 977                                  * and now we have to deal with cases when options
 978                                  * with double hyphen appear here, from -$(MAKEFLAGS)
 979                                  */
 980                                 i = current_optind;
 981                                 if (argv[i][0] == '-') {
 982                                   if (argv[i][1] == '-') {
 983                                     if (argv[i][2] != '\0') {
 984                                       /* Check if this option is allowed */
 985                                       tptr = strchr(CMD_OPTS, argv[i][2]);
 986                                       if (tptr) {
 987                                         if (last_optind_with_double_hyphen != current_optind) {
 988                                           /* This is first time we are trying to fix "--"
 989                                            * problem with this option. If we come here second 
 990                                            * time, we will go to fatal error.
 991                                            */
 992                                           last_optind_with_double_hyphen = current_optind;
 993                                           
 994                                           /* Eliminate first hyphen character */
 995                                           for (j=0; argv[i][j] != '\0'; j++) {
 996                                             argv[i][j] = argv[i][j+1];
 997                                           }
 998                                           
 999                                           /* Repeat the processing of this argument */
1000                                           optind=last_optind;
1001                                           current_optind=last_current_optind;
1002                                           continue;
1003                                         }
1004                                       }
1005                                     }
1006                                   }
1007                                 }
1008                         }
1009                 }
1010 
1011                 if (ch == '?') {
1012                         if (svr4) {
1013                                 fprintf(stderr,
1014                                         catgets(catd, 1, 267, "Usage : dmake [ -f makefile ][ -c dmake_rcfile ][ -g dmake_group ]\n"));
1015                                 fprintf(stderr,
1016                                         catgets(catd, 1, 268, "              [ -j dmake_max_jobs ][ -m dmake_mode ][ -o dmake_odir ]...\n"));
1017                                 fprintf(stderr,
1018                                         catgets(catd, 1, 269, "              [ -e ][ -i ][ -k ][ -n ][ -p ][ -q ][ -r ][ -s ][ -t ][ -v ]\n"));
1019                                 tptr = strchr(SVR4_CMD_OPTS, optopt);
1020                         } else {
1021                                 fprintf(stderr,
1022                                         catgets(catd, 1, 272, "Usage : dmake [ -f makefile ][ -c dmake_rcfile ][ -g dmake_group ]\n"));
1023                                 fprintf(stderr,
1024                                         catgets(catd, 1, 273, "              [ -j dmake_max_jobs ][ -K statefile ][ -m dmake_mode ][ -x MODE_NAME=VALUE ][ -o dmake_odir ]...\n"));
1025                                 fprintf(stderr,
1026                                         catgets(catd, 1, 274, "              [ -d ][ -dd ][ -D ][ -DD ][ -e ][ -i ][ -k ][ -n ][ -p ][ -P ][ -u ][ -w ]\n"));
1027                                 fprintf(stderr,
1028                                         catgets(catd, 1, 275, "              [ -q ][ -r ][ -s ][ -S ][ -t ][ -v ][ -V ][ target... ][ macro=value... ][ \"macro +=value\"... ]\n"));
1029                                 tptr = strchr(SUNPRO_CMD_OPTS, optopt);
1030                         }
1031                         if (!tptr) {
1032                                 fatal(catgets(catd, 1, 279, "Unknown option `-%c'"), optopt);
1033                         } else {
1034                                 fatal(catgets(catd, 1, 280, "Missing argument after `-%c'"), optopt);
1035                         }
1036                 }
1037 
1038 
1039 
1040                 makefile_next |= parse_command_option(ch);
1041                 /*
1042                  * If we're done processing all of the options of
1043                  * ONE argument string...
1044                  */
1045                 if (current_optind < optind) {
1046                         i = current_optind;
1047                         k = 0;
1048                         /* If there's an argument for an option... */
1049                         if ((optind - current_optind) > 1) {
1050                                 k = i + 1;
1051                         }
1052                         switch (makefile_next) {
1053                         case 0:
1054                                 argv[i] = NULL;
1055                                 /* This shouldn't happen */
1056                                 if (k) {
1057                                         argv[k] = NULL;
1058                                 }
1059                                 break;
1060                         case 1: /* -f seen */
1061                                 argv[i] = (char *)NOCATGETS("-f");
1062                                 break;
1063                         case 2: /* -c seen */
1064                                 argv[i] = (char *)NOCATGETS("-c");
1065                                 break;
1066                         case 4: /* -g seen */
1067                                 argv[i] = (char *)NOCATGETS("-g");
1068                                 break;
1069                         case 8: /* -j seen */
1070                                 argv[i] = (char *)NOCATGETS("-j");
1071                                 break;
1072                         case 16: /* -M seen */
1073                                 argv[i] = (char *)NOCATGETS("-M");
1074                                 break;
1075                         case 32: /* -m seen */
1076                                 argv[i] = (char *)NOCATGETS("-m");
1077                                 break;
1078                         case 128: /* -O seen */
1079                                 argv[i] = (char *)NOCATGETS("-O");
1080                                 break;
1081                         case 256: /* -K seen */
1082                                 argv[i] = (char *)NOCATGETS("-K");
1083                                 break;
1084                         case 512:       /* -o seen */
1085                                 argv[i] = (char *)NOCATGETS("-o");
1086                                 break;
1087                         case 1024: /* -x seen */
1088                                 argv[i] = (char *)NOCATGETS("-x");
1089                                 break;
1090                         default: /* > 1 of -c, f, g, j, K, M, m, O, o, x seen */
1091                                 fatal(catgets(catd, 1, 286, "Illegal command line. More than one option requiring\nan argument given in the same argument group"));
1092                         }
1093 
1094                         makefile_next = 0;
1095                         current_optind = optind;
1096                 }
1097         }
1098 }
1099 
1100 static void
1101 quote_str(char *str, char *qstr)
1102 {
1103         char            *to;
1104         char            *from;
1105 
1106         to = qstr;
1107         for (from = str; *from; from++) {
1108                 switch (*from) {
1109                 case ';':       /* End of command */
1110                 case '(':       /* Start group */
1111                 case ')':       /* End group */
1112                 case '{':       /* Start group */
1113                 case '}':       /* End group */
1114                 case '[':       /* Reg expr - any of a set of chars */
1115                 case ']':       /* End of set of chars */
1116                 case '|':       /* Pipe or logical-or */
1117                 case '^':       /* Old-fashioned pipe */
1118                 case '&':   /* Background or logical-and */
1119                 case '<':    /* Redirect stdin */
1120                 case '>':    /* Redirect stdout */
1121                 case '*':       /* Reg expr - any sequence of chars */
1122                 case '?':       /* Reg expr - any single char */
1123                 case '$':       /* Variable substitution */
1124                 case '\'':      /* Singe quote - turn off all magic */
1125                 case '"':       /* Double quote - span whitespace */
1126                 case '`':       /* Backquote - run a command */
1127                 case '#':       /* Comment */
1128                 case ' ':       /* Space (for MACRO=value1 value2  */
1129                 case '\\':      /* Escape char - turn off magic of next char */
1130                         *to++ = '\\';
1131                         break;
1132 
1133                 default:
1134                         break;
1135                 }
1136                 *to++ = *from;
1137         }
1138         *to = '\0';
1139 }
1140 
1141 static void
1142 unquote_str(char *str, char *qstr)
1143 {
1144         char            *to;
1145         char            *from;
1146 
1147         to = qstr;
1148         for (from = str; *from; from++) {
1149                 if (*from == '\\') {
1150                         from++;
1151                 }
1152                 *to++ = *from;
1153         }
1154         *to = '\0';
1155 }
1156 
1157 /*
1158  * Convert the MAKEFLAGS string value into a vector of char *, similar
1159  * to argv.
1160  */
1161 static void
1162 setup_makeflags_argv()
1163 {
1164         char            *cp;
1165         char            *cp1;
1166         char            *cp2;
1167         char            *cp3;
1168         char            *cp_orig;
1169         Boolean         add_hyphen;
1170         int             i;
1171         char            tmp_char;
1172 
1173         mf_argc = 1;
1174         cp = getenv(makeflags->string_mb);
1175         cp_orig = cp;
1176 
1177         if (cp) {
1178                 /*
1179                  * If new MAKEFLAGS format, no need to add hyphen.
1180                  * If old MAKEFLAGS format, add hyphen before flags.
1181                  */
1182 
1183                 if ((strchr(cp, (int) hyphen_char) != NULL) ||
1184                     (strchr(cp, (int) equal_char) != NULL)) {
1185 
1186                         /* New MAKEFLAGS format */
1187 
1188                         add_hyphen = false;
1189 #ifdef ADDFIX5060758                    
1190                         /* Check if MAKEFLAGS value begins with multiple
1191                          * hyphen characters, and remove all duplicates.
1192                          * Usually it happens when the next command is
1193                          * used: $(MAKE) -$(MAKEFLAGS)
1194                          * This is a workaround for BugID 5060758.
1195                          */
1196                         while (*cp) {
1197                                 if (*cp != (int) hyphen_char) {
1198                                         break;
1199                                 }
1200                                 cp++;
1201                                 if (*cp == (int) hyphen_char) {
1202                                         /* There are two hyphens. Skip one */
1203                                         cp_orig = cp;
1204                                         cp++;
1205                                 }
1206                                 if (!(*cp)) {
1207                                         /* There are hyphens only. Skip all */
1208                                         cp_orig = cp;
1209                                         break;
1210                                 }
1211                         }
1212 #endif
1213                 } else {
1214 
1215                         /* Old MAKEFLAGS format */
1216 
1217                         add_hyphen = true;
1218                 }
1219         }
1220 
1221         /* Find the number of arguments in MAKEFLAGS */
1222         while (cp && *cp) {
1223                 /* Skip white spaces */
1224                 while (cp && *cp && isspace(*cp)) {
1225                         cp++;
1226                 }
1227                 if (cp && *cp) {
1228                         /* Increment arg count */
1229                         mf_argc++;
1230                         /* Go to next white space */
1231                         while (cp && *cp && !isspace(*cp)) {
1232                                 if(*cp == (int) backslash_char) {
1233                                         cp++;
1234                                 }
1235                                 cp++;
1236                         }
1237                 }
1238         }
1239         /* Allocate memory for the new MAKEFLAGS argv */
1240         mf_argv = (char **) malloc((mf_argc + 1) * sizeof(char *));
1241         mf_argv[0] = (char *)NOCATGETS("MAKEFLAGS");
1242         /*
1243          * Convert the MAKEFLAGS string value into a vector of char *,
1244          * similar to argv.
1245          */
1246         cp = cp_orig;
1247         for (i = 1; i < mf_argc; i++) {
1248                 /* Skip white spaces */
1249                 while (cp && *cp && isspace(*cp)) {
1250                         cp++;
1251                 }
1252                 if (cp && *cp) {
1253                         cp_orig = cp;
1254                         /* Go to next white space */
1255                         while (cp && *cp && !isspace(*cp)) {
1256                                 if(*cp == (int) backslash_char) {
1257                                         cp++;
1258                                 }
1259                                 cp++;
1260                         }
1261                         tmp_char = *cp;
1262                         *cp = (int) nul_char;
1263                         if (add_hyphen) {
1264                                 mf_argv[i] = getmem(2 + strlen(cp_orig));
1265                                 mf_argv[i][0] = '\0';
1266                                 (void) strcat(mf_argv[i], "-");
1267                                 // (void) strcat(mf_argv[i], cp_orig);
1268                                 unquote_str(cp_orig, mf_argv[i]+1);
1269                         } else {
1270                                 mf_argv[i] = getmem(2 + strlen(cp_orig));
1271                                 //mf_argv[i] = strdup(cp_orig);
1272                                 unquote_str(cp_orig, mf_argv[i]);
1273                         }
1274                         *cp = tmp_char;
1275                 }
1276         }
1277         mf_argv[i] = NULL;
1278 }
1279 
1280 /*
1281  *      parse_command_option(ch)
1282  *
1283  *      Parse make command line options.
1284  *
1285  *      Return value:
1286  *                              Indicates if any -f -c or -M were seen
1287  *
1288  *      Parameters:
1289  *              ch              The character to parse
1290  *
1291  *      Static variables used:
1292  *              dmake_group_specified   Set for make -g
1293  *              dmake_max_jobs_specified        Set for make -j
1294  *              dmake_mode_specified    Set for make -m
1295  *              dmake_add_mode_specified        Set for make -x
1296  *              dmake_compat_mode_specified     Set for make -x SUN_MAKE_COMPAT_MODE=
1297  *              dmake_output_mode_specified     Set for make -x DMAKE_OUTPUT_MODE=
1298  *              dmake_odir_specified    Set for make -o
1299  *              dmake_rcfile_specified  Set for make -c
1300  *              env_wins                Set for make -e
1301  *              ignore_default_mk       Set for make -r
1302  *              trace_status            Set for make -p
1303  *
1304  *      Global variables used:
1305  *              .make.state path & name set for make -K
1306  *              continue_after_error    Set for make -k
1307  *              debug_level             Set for make -d
1308  *              do_not_exec_rule        Set for make -n
1309  *              filter_stderr           Set for make -X
1310  *              ignore_errors_all       Set for make -i
1311  *              no_parallel             Set for make -R
1312  *              quest                   Set for make -q
1313  *              read_trace_level        Set for make -D
1314  *              report_dependencies     Set for make -P
1315  *              send_mtool_msgs         Set for make -K
1316  *              silent_all              Set for make -s
1317  *              touch                   Set for make -t
1318  */
1319 static int
1320 parse_command_option(register char ch)
1321 {
1322         static int              invert_next = 0;
1323         int                     invert_this = invert_next;
1324 
1325         invert_next = 0;
1326         switch (ch) {
1327         case '-':                        /* Ignore "--" */
1328                 return 0;
1329         case '~':                        /* Invert next option */
1330                 invert_next = 1;
1331                 return 0;
1332         case 'B':                        /* Obsolete */
1333                 return 0;
1334         case 'b':                        /* Obsolete */
1335                 return 0;
1336         case 'c':                        /* Read alternative dmakerc file */
1337                 if (invert_this) {
1338                         dmake_rcfile_specified = false;
1339                 } else {
1340                         dmake_rcfile_specified = true;
1341                 }
1342                 return 2;
1343         case 'D':                        /* Show lines read */
1344                 if (invert_this) {
1345                         read_trace_level--;
1346                 } else {
1347                         read_trace_level++;
1348                 }
1349                 return 0;
1350         case 'd':                        /* Debug flag */
1351                 if (invert_this) {
1352                         debug_level--;
1353                 } else {
1354                         debug_level++;
1355                 }
1356                 return 0;
1357         case 'e':                        /* Environment override flag */
1358                 if (invert_this) {
1359                         env_wins = false;
1360                 } else {
1361                         env_wins = true;
1362                 }
1363                 return 0;
1364         case 'f':                        /* Read alternative makefile(s) */
1365                 return 1;
1366         case 'g':                        /* Use alternative DMake group */
1367                 if (invert_this) {
1368                         dmake_group_specified = false;
1369                 } else {
1370                         dmake_group_specified = true;
1371                 }
1372                 return 4;
1373         case 'i':                        /* Ignore errors */
1374                 if (invert_this) {
1375                         ignore_errors_all = false;
1376                 } else {
1377                         ignore_errors_all = true;
1378                 }
1379                 return 0;
1380         case 'j':                        /* Use alternative DMake max jobs */
1381                 if (invert_this) {
1382                         dmake_max_jobs_specified = false;
1383                 } else {
1384                         dmake_max_jobs_specified = true;
1385                 }
1386                 return 8;
1387         case 'K':                        /* Read alternative .make.state */
1388                 return 256;
1389         case 'k':                        /* Keep making even after errors */
1390                 if (invert_this) {
1391                         continue_after_error = false;
1392                 } else {
1393                         continue_after_error = true;
1394                         continue_after_error_ever_seen = true;
1395                 }
1396                 return 0;
1397         case 'M':                        /* Read alternative make.machines file */
1398                 if (invert_this) {
1399                         pmake_machinesfile_specified = false;
1400                 } else {
1401                         pmake_machinesfile_specified = true;
1402                         dmake_mode_type = parallel_mode;
1403                         no_parallel = false;
1404                 }
1405                 return 16;
1406         case 'm':                        /* Use alternative DMake build mode */
1407                 if (invert_this) {
1408                         dmake_mode_specified = false;
1409                 } else {
1410                         dmake_mode_specified = true;
1411                 }
1412                 return 32;
1413         case 'x':                        /* Use alternative DMake mode */
1414                 if (invert_this) {
1415                         dmake_add_mode_specified = false;
1416                 } else {
1417                         dmake_add_mode_specified = true;
1418                 }
1419                 return 1024;
1420         case 'N':                        /* Reverse -n */
1421                 if (invert_this) {
1422                         do_not_exec_rule = true;
1423                 } else {
1424                         do_not_exec_rule = false;
1425                 }
1426                 return 0;
1427         case 'n':                        /* Print, not exec commands */
1428                 if (invert_this) {
1429                         do_not_exec_rule = false;
1430                 } else {
1431                         do_not_exec_rule = true;
1432                 }
1433                 return 0;
1434         case 'O':                        /* Send job start & result msgs */
1435                 if (invert_this) {
1436                         send_mtool_msgs = false;
1437                 } else {
1438                 }
1439                 return 128;
1440         case 'o':                        /* Use alternative dmake output dir */
1441                 if (invert_this) {
1442                         dmake_odir_specified = false;
1443                 } else {
1444                         dmake_odir_specified = true;
1445                 }
1446                 return 512;
1447         case 'P':                        /* Print for selected targets */
1448                 if (invert_this) {
1449                         report_dependencies_level--;
1450                 } else {
1451                         report_dependencies_level++;
1452                 }
1453                 return 0;
1454         case 'p':                        /* Print description */
1455                 if (invert_this) {
1456                         trace_status = false;
1457                         do_not_exec_rule = false;
1458                 } else {
1459                         trace_status = true;
1460                         do_not_exec_rule = true;
1461                 }
1462                 return 0;
1463         case 'q':                        /* Question flag */
1464                 if (invert_this) {
1465                         quest = false;
1466                 } else {
1467                         quest = true;
1468                 }
1469                 return 0;
1470         case 'R':                        /* Don't run in parallel */
1471                 if (invert_this) {
1472                         pmake_cap_r_specified = false;
1473                         no_parallel = false;
1474                 } else {
1475                         pmake_cap_r_specified = true;
1476                         dmake_mode_type = serial_mode;
1477                         no_parallel = true;
1478                 }
1479                 return 0;
1480         case 'r':                        /* Turn off internal rules */
1481                 if (invert_this) {
1482                         ignore_default_mk = false;
1483                 } else {
1484                         ignore_default_mk = true;
1485                 }
1486                 return 0;
1487         case 'S':                        /* Reverse -k */
1488                 if (invert_this) {
1489                         continue_after_error = true;
1490                 } else {
1491                         continue_after_error = false;
1492                         stop_after_error_ever_seen = true;
1493                 }
1494                 return 0;
1495         case 's':                        /* Silent flag */
1496                 if (invert_this) {
1497                         silent_all = false;
1498                 } else {
1499                         silent_all = true;
1500                 }
1501                 return 0;
1502         case 'T':                        /* Print target list */
1503                 if (invert_this) {
1504                         list_all_targets = false;
1505                         do_not_exec_rule = false;
1506                 } else {
1507                         list_all_targets = true;
1508                         do_not_exec_rule = true;
1509                 }
1510                 return 0;
1511         case 't':                        /* Touch flag */
1512                 if (invert_this) {
1513                         touch = false;
1514                 } else {
1515                         touch = true;
1516                 }
1517                 return 0;
1518         case 'u':                        /* Unconditional flag */
1519                 if (invert_this) {
1520                         build_unconditional = false;
1521                 } else {
1522                         build_unconditional = true;
1523                 }
1524                 return 0;
1525         case 'V':                       /* SVR4 mode */
1526                 svr4 = true;
1527                 return 0;
1528         case 'v':                       /* Version flag */
1529                 if (invert_this) {
1530                 } else {
1531                         fprintf(stdout, NOCATGETS("dmake: %s\n"), verstring);
1532                         exit_status = 0;
1533                         exit(0);
1534                 }
1535                 return 0;
1536         case 'w':                        /* Unconditional flag */
1537                 if (invert_this) {
1538                         report_cwd = false;
1539                 } else {
1540                         report_cwd = true;
1541                 }
1542                 return 0;
1543 #if 0
1544         case 'X':                       /* Filter stdout */
1545                 if (invert_this) {
1546                         filter_stderr = false;
1547                 } else {
1548                         filter_stderr = true;
1549                 }
1550                 return 0;
1551 #endif
1552         default:
1553                 break;
1554         }
1555         return 0;
1556 }
1557 
1558 /*
1559  *      setup_for_projectdir()
1560  *
1561  *      Read the PROJECTDIR variable, if defined, and set the sccs path
1562  *
1563  *      Parameters:
1564  *
1565  *      Global variables used:
1566  *              sccs_dir_path   Set to point to SCCS dir to use
1567  */
1568 static void
1569 setup_for_projectdir(void)
1570 {
1571 static char     path[MAXPATHLEN];
1572 char            cwdpath[MAXPATHLEN];
1573 uid_t uid;
1574 int   done=0;
1575 
1576         /* Check if we should use PROJECTDIR when reading the SCCS dir. */
1577         sccs_dir_path = getenv(NOCATGETS("PROJECTDIR"));
1578         if ((sccs_dir_path != NULL) &&
1579             (sccs_dir_path[0] != (int) slash_char)) {
1580                 struct passwd *pwent;
1581 
1582              {
1583                 uid = getuid();
1584                 pwent = getpwuid(uid);
1585                 if (pwent == NULL) {
1586                    fatal(catgets(catd, 1, 188, "Bogus USERID "));
1587                 }
1588                 if ((pwent = getpwnam(sccs_dir_path)) == NULL) {
1589                         /*empty block : it'll go & check cwd  */
1590                 }
1591                 else {
1592                   (void) sprintf(path, NOCATGETS("%s/src"), pwent->pw_dir);
1593                   if (access(path, F_OK) == 0) {
1594                         sccs_dir_path = path;
1595                         done = 1;
1596                   } else {
1597                         (void) sprintf(path, NOCATGETS("%s/source"), pwent->pw_dir);
1598                         if (access(path, F_OK) == 0) {
1599                                 sccs_dir_path = path;
1600                                 done = 1;
1601                         }
1602                      }
1603                 }
1604                 if (!done) {
1605                     if (getcwd(cwdpath, MAXPATHLEN - 1 )) {
1606 
1607                        (void) sprintf(path, NOCATGETS("%s/%s"), cwdpath,sccs_dir_path);
1608                        if (access(path, F_OK) == 0) {
1609                                 sccs_dir_path = path;
1610                                 done = 1;
1611                         } else {
1612                                 fatal(catgets(catd, 1, 189, "Bogus PROJECTDIR '%s'"), sccs_dir_path);
1613                         }
1614                     }
1615                 }
1616            }
1617         }
1618 }
1619 
1620 /*
1621  *      set_sgs_support()
1622  *
1623  *      Add the libmakestate.so.1 lib to the env var SGS_SUPPORT
1624  *        if it's not already in there.
1625  *      The SGS_SUPPORT env var and libmakestate.so.1 is used by
1626  *        the linker ld to report .make.state info back to make.
1627  *
1628  * In the new world we always will set the 32-bit and 64-bit versions of this
1629  * variable explicitly so that we can take into account the correct isa and our
1630  * prefix. So say that the prefix was /opt/local. Then we would want to search
1631  * /opt/local/lib/libmakestate.so.1:libmakestate.so.1. We still want to search
1632  * the original location just as a safety measure.
1633  */
1634 static void
1635 set_sgs_support()
1636 {
1637         int             len;
1638         char            *newpath, *newpath64;
1639         char            *oldpath, *oldpath64;
1640         static char     *prev_path, *prev_path64;
1641 
1642         oldpath = getenv(LD_SUPPORT_ENV_VAR_32);
1643         if (oldpath == NULL) {
1644                 len = snprintf(NULL, 0, "%s=%s/%s/%s:%s",
1645                     LD_SUPPORT_ENV_VAR_32,
1646                     MAKE_PREFIX,
1647                     LD_SUPPORT_MAKE_LIB_DIR,
1648                     LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB) + 1;
1649                 newpath = (char *) malloc(len);
1650                 sprintf(newpath, "%s=%s/%s/%s:%s",
1651                     LD_SUPPORT_ENV_VAR_32,
1652                     MAKE_PREFIX,
1653                     LD_SUPPORT_MAKE_LIB_DIR,
1654                     LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB);
1655         } else {
1656                 len = snprintf(NULL, 0, "%s=%s:%s/%s/%s:%s",
1657                     LD_SUPPORT_ENV_VAR_32, oldpath, MAKE_PREFIX,
1658                     LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB,
1659                     LD_SUPPORT_MAKE_LIB) + 1;
1660                 newpath = (char *) malloc(len);
1661                 sprintf(newpath, "%s=%s:%s/%s/%s:%s",
1662                     LD_SUPPORT_ENV_VAR_32, oldpath, MAKE_PREFIX,
1663                     LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB,
1664                     LD_SUPPORT_MAKE_LIB);
1665         }
1666 
1667         oldpath64 = getenv(LD_SUPPORT_ENV_VAR_64);
1668         if (oldpath64 == NULL) {
1669                 len = snprintf(NULL, 0, "%s=%s/%s/%s/%s:%s",
1670                     LD_SUPPORT_ENV_VAR_64, MAKE_PREFIX, LD_SUPPORT_MAKE_LIB_DIR,
1671                     LD_SUPPORT_MAKE_LIB_DIR_64, LD_SUPPORT_MAKE_LIB,
1672                     LD_SUPPORT_MAKE_LIB) + 1;
1673                 newpath64 = (char *) malloc(len);
1674                 sprintf(newpath64, "%s=%s/%s/%s/%s:%s",
1675                     LD_SUPPORT_ENV_VAR_64, MAKE_PREFIX, LD_SUPPORT_MAKE_LIB_DIR,
1676                     LD_SUPPORT_MAKE_LIB_DIR_64, LD_SUPPORT_MAKE_LIB,
1677                     LD_SUPPORT_MAKE_LIB);
1678         } else {
1679                 len = snprintf(NULL, 0, "%s=%s:%s/%s/%s/%s:%s",
1680                     LD_SUPPORT_ENV_VAR_64, oldpath64, MAKE_PREFIX,
1681                     LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB_DIR_64,
1682                     LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB) + 1;
1683                 newpath64 = (char *) malloc(len);
1684                 sprintf(newpath64, "%s=%s:%s/%s/%s/%s:%s",
1685                     LD_SUPPORT_ENV_VAR_64, oldpath64, MAKE_PREFIX,
1686                     LD_SUPPORT_MAKE_LIB_DIR, LD_SUPPORT_MAKE_LIB_DIR_64,
1687                     LD_SUPPORT_MAKE_LIB, LD_SUPPORT_MAKE_LIB);
1688         }
1689 
1690         putenv(newpath);
1691         if (prev_path) {
1692                 free(prev_path);
1693         }
1694         prev_path = newpath;
1695 
1696         putenv(newpath64);
1697         if (prev_path64) {
1698                 free(prev_path64);
1699         }
1700         prev_path64 = newpath64;
1701 }
1702 
1703 /*
1704  *      read_files_and_state(argc, argv)
1705  *
1706  *      Read the makefiles we care about and the environment
1707  *      Also read the = style command line options
1708  *
1709  *      Parameters:
1710  *              argc            You know what this is
1711  *              argv            You know what this is
1712  *
1713  *      Static variables used:
1714  *              env_wins        make -e, determines if env vars are RO
1715  *              ignore_default_mk make -r, determines if make.rules is read
1716  *              not_auto_depen  dwight
1717  *
1718  *      Global variables used:
1719  *              default_target_to_build Set to first proper target from file
1720  *              do_not_exec_rule Set to false when makfile is made
1721  *              dot             The Name ".", used to read current dir
1722  *              empty_name      The Name "", use as macro value
1723  *              keep_state      Set if KEEP_STATE is in environment
1724  *              make_state      The Name ".make.state", used to read file
1725  *              makefile_type   Set to type of file being read
1726  *              makeflags       The Name "MAKEFLAGS", used to set macro value
1727  *              not_auto        dwight
1728  *              read_trace_level Checked to se if the reader should trace
1729  *              report_dependencies If -P is on we do not read .make.state
1730  *              trace_reader    Set if reader should trace
1731  *              virtual_root    The Name "VIRTUAL_ROOT", used to check value
1732  */
1733 static void
1734 read_files_and_state(int argc, char **argv)
1735 {
1736         wchar_t                 buffer[1000];
1737         wchar_t                 buffer_posix[1000];
1738         register char           ch;
1739         register char           *cp;
1740         Property                def_make_macro = NULL;
1741         Name                    def_make_name;
1742         Name                    default_makefile;
1743         String_rec              dest;
1744         wchar_t                 destbuffer[STRING_BUFFER_LENGTH];
1745         register int            i;
1746         register int            j;
1747         Name                    keep_state_name;
1748         int                     length;
1749         Name                    Makefile;
1750         register Property       macro;
1751         struct stat             make_state_stat;
1752         Name                    makefile_name;
1753         register int            makefile_next = 0;
1754         register Boolean        makefile_read = false;
1755         String_rec              makeflags_string;
1756         String_rec              makeflags_string_posix;
1757         String_rec *            makeflags_string_current;
1758         Name                    makeflags_value_saved;
1759         register Name           name;
1760         Name                    new_make_value;
1761         Boolean                 save_do_not_exec_rule;
1762         Name                    sdotMakefile;
1763         Name                    sdotmakefile_name;
1764         static wchar_t          state_file_str;
1765         static char             state_file_str_mb[MAXPATHLEN];
1766         static struct _Name     state_filename;
1767         Boolean                 temp;
1768         char                    tmp_char;
1769         wchar_t                 *tmp_wcs_buffer;
1770         register Name           value;
1771         ASCII_Dyn_Array         makeflags_and_macro;
1772         Boolean                 is_xpg4;
1773 
1774 /*
1775  *      Remember current mode. It may be changed after reading makefile
1776  *      and we will have to correct MAKEFLAGS variable.
1777  */
1778         is_xpg4 = posix;
1779 
1780         MBSTOWCS(wcs_buffer, NOCATGETS("KEEP_STATE"));
1781         keep_state_name = GETNAME(wcs_buffer, FIND_LENGTH);
1782         MBSTOWCS(wcs_buffer, NOCATGETS("Makefile"));
1783         Makefile = GETNAME(wcs_buffer, FIND_LENGTH);
1784         MBSTOWCS(wcs_buffer, NOCATGETS("makefile"));
1785         makefile_name = GETNAME(wcs_buffer, FIND_LENGTH);
1786         MBSTOWCS(wcs_buffer, NOCATGETS("s.makefile"));
1787         sdotmakefile_name = GETNAME(wcs_buffer, FIND_LENGTH);
1788         MBSTOWCS(wcs_buffer, NOCATGETS("s.Makefile"));
1789         sdotMakefile = GETNAME(wcs_buffer, FIND_LENGTH);
1790 
1791 /*
1792  *      Set flag if NSE is active
1793  */
1794 
1795 /*
1796  *      initialize global dependency entry for .NOT_AUTO
1797  */
1798         not_auto_depen->next = NULL;
1799         not_auto_depen->name = not_auto;
1800         not_auto_depen->automatic = not_auto_depen->stale = false;
1801 
1802 /*
1803  *      Read internal definitions and rules.
1804  */
1805         if (read_trace_level > 1) {
1806                 trace_reader = true;
1807         }
1808         if (!ignore_default_mk) {
1809                 if (svr4) {
1810                         MBSTOWCS(wcs_buffer, NOCATGETS("svr4.make.rules"));
1811                         default_makefile = GETNAME(wcs_buffer, FIND_LENGTH);
1812                 } else {
1813                         MBSTOWCS(wcs_buffer, NOCATGETS("make.rules"));
1814                         default_makefile = GETNAME(wcs_buffer, FIND_LENGTH);
1815                 }
1816                 default_makefile->stat.is_file = true;
1817 
1818                 (void) read_makefile(default_makefile,
1819                                      true,
1820                                      false,
1821                                      true);
1822         }
1823 
1824         /*
1825          * If the user did not redefine the MAKE macro in the
1826          * default makefile (make.rules), then we'd like to
1827          * change the macro value of MAKE to be some form
1828          * of argv[0] for recursive MAKE builds.
1829          */
1830         MBSTOWCS(wcs_buffer, NOCATGETS("MAKE"));
1831         def_make_name = GETNAME(wcs_buffer, wslen(wcs_buffer));
1832         def_make_macro = get_prop(def_make_name->prop, macro_prop);
1833         if ((def_make_macro != NULL) &&
1834             (IS_EQUAL(def_make_macro->body.macro.value->string_mb,
1835                       NOCATGETS("make")))) {
1836                 MBSTOWCS(wcs_buffer, argv_zero_string);
1837                 new_make_value = GETNAME(wcs_buffer, wslen(wcs_buffer));
1838                 (void) SETVAR(def_make_name,
1839                               new_make_value,
1840                               false);
1841         }
1842 
1843         default_target_to_build = NULL;
1844         trace_reader = false;
1845 
1846 /*
1847  *      Read environment args. Let file args which follow override unless
1848  *      -e option seen. If -e option is not mentioned.
1849  */
1850         read_environment(env_wins);
1851         if (getvar(virtual_root)->hash.length == 0) {
1852                 maybe_append_prop(virtual_root, macro_prop)
1853                   ->body.macro.exported = true;
1854                 MBSTOWCS(wcs_buffer, "/");
1855                 (void) SETVAR(virtual_root,
1856                               GETNAME(wcs_buffer, FIND_LENGTH),
1857                               false);
1858         }
1859 
1860 /*
1861  * We now scan mf_argv and argv to see if we need to set
1862  * any of the DMake-added options/variables in MAKEFLAGS.
1863  */
1864 
1865         makeflags_and_macro.start = 0;
1866         makeflags_and_macro.size = 0;
1867         enter_argv_values(mf_argc, mf_argv, &makeflags_and_macro);
1868         enter_argv_values(argc, argv, &makeflags_and_macro);
1869 
1870 /*
1871  *      Set MFLAGS and MAKEFLAGS
1872  *      
1873  *      Before reading makefile we do not know exactly which mode
1874  *      (posix or not) is used. So prepare two MAKEFLAGS strings
1875  *      for both posix and solaris modes because they are different.
1876  */
1877         INIT_STRING_FROM_STACK(makeflags_string, buffer);
1878         INIT_STRING_FROM_STACK(makeflags_string_posix, buffer_posix);
1879         append_char((int) hyphen_char, &makeflags_string);
1880         append_char((int) hyphen_char, &makeflags_string_posix);
1881 
1882         switch (read_trace_level) {
1883         case 2:
1884                 append_char('D', &makeflags_string);
1885                 append_char('D', &makeflags_string_posix);
1886         case 1:
1887                 append_char('D', &makeflags_string);
1888                 append_char('D', &makeflags_string_posix);
1889         }
1890         switch (debug_level) {
1891         case 2:
1892                 append_char('d', &makeflags_string);
1893                 append_char('d', &makeflags_string_posix);
1894         case 1:
1895                 append_char('d', &makeflags_string);
1896                 append_char('d', &makeflags_string_posix);
1897         }
1898         if (env_wins) {
1899                 append_char('e', &makeflags_string);
1900                 append_char('e', &makeflags_string_posix);
1901         }
1902         if (ignore_errors_all) {
1903                 append_char('i', &makeflags_string);
1904                 append_char('i', &makeflags_string_posix);
1905         }
1906         if (continue_after_error) {
1907                 if (stop_after_error_ever_seen) {
1908                         append_char('S', &makeflags_string_posix);
1909                         append_char((int) space_char, &makeflags_string_posix);
1910                         append_char((int) hyphen_char, &makeflags_string_posix);
1911                 }
1912                 append_char('k', &makeflags_string);
1913                 append_char('k', &makeflags_string_posix);
1914         } else {
1915                 if (stop_after_error_ever_seen 
1916                     && continue_after_error_ever_seen) {
1917                         append_char('k', &makeflags_string_posix);
1918                         append_char((int) space_char, &makeflags_string_posix);
1919                         append_char((int) hyphen_char, &makeflags_string_posix);
1920                         append_char('S', &makeflags_string_posix);
1921                 }
1922         }
1923         if (do_not_exec_rule) {
1924                 append_char('n', &makeflags_string);
1925                 append_char('n', &makeflags_string_posix);
1926         }
1927         switch (report_dependencies_level) {
1928         case 4:
1929                 append_char('P', &makeflags_string);
1930                 append_char('P', &makeflags_string_posix);
1931         case 3:
1932                 append_char('P', &makeflags_string);
1933                 append_char('P', &makeflags_string_posix);
1934         case 2:
1935                 append_char('P', &makeflags_string);
1936                 append_char('P', &makeflags_string_posix);
1937         case 1:
1938                 append_char('P', &makeflags_string);
1939                 append_char('P', &makeflags_string_posix);
1940         }
1941         if (trace_status) {
1942                 append_char('p', &makeflags_string);
1943                 append_char('p', &makeflags_string_posix);
1944         }
1945         if (quest) {
1946                 append_char('q', &makeflags_string);
1947                 append_char('q', &makeflags_string_posix);
1948         }
1949         if (silent_all) {
1950                 append_char('s', &makeflags_string);
1951                 append_char('s', &makeflags_string_posix);
1952         }
1953         if (touch) {
1954                 append_char('t', &makeflags_string);
1955                 append_char('t', &makeflags_string_posix);
1956         }
1957         if (build_unconditional) {
1958                 append_char('u', &makeflags_string);
1959                 append_char('u', &makeflags_string_posix);
1960         }
1961         if (report_cwd) {
1962                 append_char('w', &makeflags_string);
1963                 append_char('w', &makeflags_string_posix);
1964         }
1965         /* -c dmake_rcfile */
1966         if (dmake_rcfile_specified) {
1967                 MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_RCFILE"));
1968                 dmake_rcfile = GETNAME(wcs_buffer, FIND_LENGTH);
1969                 append_makeflags_string(dmake_rcfile, &makeflags_string);
1970                 append_makeflags_string(dmake_rcfile, &makeflags_string_posix);
1971         }
1972         /* -g dmake_group */
1973         if (dmake_group_specified) {
1974                 MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_GROUP"));
1975                 dmake_group = GETNAME(wcs_buffer, FIND_LENGTH);
1976                 append_makeflags_string(dmake_group, &makeflags_string);
1977                 append_makeflags_string(dmake_group, &makeflags_string_posix);
1978         }
1979         /* -j dmake_max_jobs */
1980         if (dmake_max_jobs_specified) {
1981                 MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_MAX_JOBS"));
1982                 dmake_max_jobs = GETNAME(wcs_buffer, FIND_LENGTH);
1983                 append_makeflags_string(dmake_max_jobs, &makeflags_string);
1984                 append_makeflags_string(dmake_max_jobs, &makeflags_string_posix);
1985         }
1986         /* -m dmake_mode */
1987         if (dmake_mode_specified) {
1988                 MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_MODE"));
1989                 dmake_mode = GETNAME(wcs_buffer, FIND_LENGTH);
1990                 append_makeflags_string(dmake_mode, &makeflags_string);
1991                 append_makeflags_string(dmake_mode, &makeflags_string_posix);
1992         }
1993         /* -x dmake_compat_mode */
1994 //      if (dmake_compat_mode_specified) {
1995 //              MBSTOWCS(wcs_buffer, NOCATGETS("SUN_MAKE_COMPAT_MODE"));
1996 //              dmake_compat_mode = GETNAME(wcs_buffer, FIND_LENGTH);
1997 //              append_makeflags_string(dmake_compat_mode, &makeflags_string);
1998 //              append_makeflags_string(dmake_compat_mode, &makeflags_string_posix);
1999 //      }
2000         /* -x dmake_output_mode */
2001         if (dmake_output_mode_specified) {
2002                 MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_OUTPUT_MODE"));
2003                 dmake_output_mode = GETNAME(wcs_buffer, FIND_LENGTH);
2004                 append_makeflags_string(dmake_output_mode, &makeflags_string);
2005                 append_makeflags_string(dmake_output_mode, &makeflags_string_posix);
2006         }
2007         /* -o dmake_odir */
2008         if (dmake_odir_specified) {
2009                 MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_ODIR"));
2010                 dmake_odir = GETNAME(wcs_buffer, FIND_LENGTH);
2011                 append_makeflags_string(dmake_odir, &makeflags_string);
2012                 append_makeflags_string(dmake_odir, &makeflags_string_posix);
2013         }
2014         /* -M pmake_machinesfile */
2015         if (pmake_machinesfile_specified) {
2016                 MBSTOWCS(wcs_buffer, NOCATGETS("PMAKE_MACHINESFILE"));
2017                 pmake_machinesfile = GETNAME(wcs_buffer, FIND_LENGTH);
2018                 append_makeflags_string(pmake_machinesfile, &makeflags_string);
2019                 append_makeflags_string(pmake_machinesfile, &makeflags_string_posix);
2020         }
2021         /* -R */
2022         if (pmake_cap_r_specified) {
2023                 append_char((int) space_char, &makeflags_string);
2024                 append_char((int) hyphen_char, &makeflags_string);
2025                 append_char('R', &makeflags_string);
2026                 append_char((int) space_char, &makeflags_string_posix);
2027                 append_char((int) hyphen_char, &makeflags_string_posix);
2028                 append_char('R', &makeflags_string_posix);
2029         }
2030 
2031 /*
2032  *      Make sure MAKEFLAGS is exported
2033  */
2034         maybe_append_prop(makeflags, macro_prop)->
2035           body.macro.exported = true;
2036 
2037         if (makeflags_string.buffer.start[1] != (int) nul_char) {
2038                 if (makeflags_string.buffer.start[1] != (int) space_char) {
2039                         MBSTOWCS(wcs_buffer, NOCATGETS("MFLAGS"));
2040                         (void) SETVAR(GETNAME(wcs_buffer, FIND_LENGTH),
2041                                       GETNAME(makeflags_string.buffer.start,
2042                                               FIND_LENGTH),
2043                                       false);
2044                 } else {
2045                         MBSTOWCS(wcs_buffer, NOCATGETS("MFLAGS"));
2046                         (void) SETVAR(GETNAME(wcs_buffer, FIND_LENGTH),
2047                                       GETNAME(makeflags_string.buffer.start + 2,
2048                                               FIND_LENGTH),
2049                                       false);
2050                 }
2051         }
2052 
2053 /* 
2054  *      Add command line macro to POSIX makeflags_string  
2055  */
2056         if (makeflags_and_macro.start) {
2057                 tmp_char = (char) space_char;
2058                 cp = makeflags_and_macro.start;
2059                 do {
2060                         append_char(tmp_char, &makeflags_string_posix);
2061                 } while ( tmp_char = *cp++ ); 
2062                 retmem_mb(makeflags_and_macro.start);
2063         }
2064 
2065 /*
2066  *      Now set the value of MAKEFLAGS macro in accordance
2067  *      with current mode.
2068  */
2069         macro = maybe_append_prop(makeflags, macro_prop);
2070         temp = (Boolean) macro->body.macro.read_only;
2071         macro->body.macro.read_only = false;
2072         if(posix || gnu_style) {
2073                 makeflags_string_current = &makeflags_string_posix;
2074         } else {
2075                 makeflags_string_current = &makeflags_string;
2076         }
2077         if (makeflags_string_current->buffer.start[1] == (int) nul_char) {
2078                 makeflags_value_saved =
2079                         GETNAME( makeflags_string_current->buffer.start + 1
2080                                , FIND_LENGTH
2081                                );
2082         } else {
2083                 if (makeflags_string_current->buffer.start[1] != (int) space_char) {
2084                         makeflags_value_saved =
2085                                 GETNAME( makeflags_string_current->buffer.start
2086                                        , FIND_LENGTH
2087                                        );
2088                 } else {
2089                         makeflags_value_saved =
2090                                 GETNAME( makeflags_string_current->buffer.start + 2
2091                                        , FIND_LENGTH
2092                                        );
2093                 }
2094         }
2095         (void) SETVAR( makeflags
2096                      , makeflags_value_saved
2097                      , false
2098                      );
2099         macro->body.macro.read_only = temp;
2100 
2101 /*
2102  *      Read command line "-f" arguments and ignore -c, g, j, K, M, m, O and o args.
2103  */
2104         save_do_not_exec_rule = do_not_exec_rule;
2105         do_not_exec_rule = false;
2106         if (read_trace_level > 0) {
2107                 trace_reader = true;
2108         }
2109 
2110         for (i = 1; i < argc; i++) {
2111                 if (argv[i] &&
2112                     (argv[i][0] == (int) hyphen_char) &&
2113                     (argv[i][1] == 'f') &&
2114                     (argv[i][2] == (int) nul_char)) {
2115                         argv[i] = NULL;         /* Remove -f */
2116                         if (i >= argc - 1) {
2117                                 fatal(catgets(catd, 1, 190, "No filename argument after -f flag"));
2118                         }
2119                         MBSTOWCS(wcs_buffer, argv[++i]);
2120                         primary_makefile = GETNAME(wcs_buffer, FIND_LENGTH);
2121                         (void) read_makefile(primary_makefile, true, true, true);
2122                         argv[i] = NULL;         /* Remove filename */
2123                         makefile_read = true;
2124                 } else if (argv[i] &&
2125                            (argv[i][0] == (int) hyphen_char) &&
2126                            (argv[i][1] == 'c' ||
2127                             argv[i][1] == 'g' ||
2128                             argv[i][1] == 'j' ||
2129                             argv[i][1] == 'K' ||
2130                             argv[i][1] == 'M' ||
2131                             argv[i][1] == 'm' ||
2132                             argv[i][1] == 'O' ||
2133                             argv[i][1] == 'o') &&
2134                            (argv[i][2] == (int) nul_char)) {
2135                         argv[i] = NULL;
2136                         argv[++i] = NULL;
2137                 }
2138         }
2139 
2140 /*
2141  *      If no command line "-f" args then look for "makefile", and then for
2142  *      "Makefile" if "makefile" isn't found.
2143  */
2144         if (!makefile_read) {
2145                 (void) read_dir(dot,
2146                                 (wchar_t *) NULL,
2147                                 (Property) NULL,
2148                                 (wchar_t *) NULL);
2149             if (!posix) {
2150                 if (makefile_name->stat.is_file) {
2151                         if (Makefile->stat.is_file) {
2152                                 warning(catgets(catd, 1, 310, "Both `makefile' and `Makefile' exist"));
2153                         }
2154                         primary_makefile = makefile_name;
2155                         makefile_read = read_makefile(makefile_name,
2156                                                       false,
2157                                                       false,
2158                                                       true);
2159                 }
2160                 if (!makefile_read &&
2161                     Makefile->stat.is_file) {
2162                         primary_makefile = Makefile;
2163                         makefile_read = read_makefile(Makefile,
2164                                                       false,
2165                                                       false,
2166                                                       true);
2167                 }
2168             } else {
2169 
2170                 enum sccs_stat save_m_has_sccs = NO_SCCS;
2171                 enum sccs_stat save_M_has_sccs = NO_SCCS;
2172 
2173                 if (makefile_name->stat.is_file) {
2174                         if (Makefile->stat.is_file) {
2175                                 warning(catgets(catd, 1, 191, "Both `makefile' and `Makefile' exist"));
2176                         }
2177                 }
2178                 if (makefile_name->stat.is_file) {
2179                         if (makefile_name->stat.has_sccs == NO_SCCS) {
2180                            primary_makefile = makefile_name;
2181                            makefile_read = read_makefile(makefile_name,
2182                                                       false,
2183                                                       false,
2184                                                       true);
2185                         } else {
2186                           save_m_has_sccs = makefile_name->stat.has_sccs;
2187                           makefile_name->stat.has_sccs = NO_SCCS;
2188                           primary_makefile = makefile_name;
2189                           makefile_read = read_makefile(makefile_name,
2190                                                       false,
2191                                                       false,
2192                                                       true);
2193                         }
2194                 }
2195                 if (!makefile_read &&
2196                     Makefile->stat.is_file) {
2197                         if (Makefile->stat.has_sccs == NO_SCCS) {
2198                            primary_makefile = Makefile;
2199                            makefile_read = read_makefile(Makefile,
2200                                                       false,
2201                                                       false,
2202                                                       true);
2203                         } else {
2204                           save_M_has_sccs = Makefile->stat.has_sccs;
2205                           Makefile->stat.has_sccs = NO_SCCS;
2206                           primary_makefile = Makefile;
2207                           makefile_read = read_makefile(Makefile,
2208                                                       false,
2209                                                       false,
2210                                                       true);
2211                         }
2212                 }
2213                 if (!makefile_read &&
2214                         makefile_name->stat.is_file) {
2215                            makefile_name->stat.has_sccs = save_m_has_sccs;
2216                            primary_makefile = makefile_name;
2217                            makefile_read = read_makefile(makefile_name,
2218                                                       false,
2219                                                       false,
2220                                                       true);
2221                 }
2222                 if (!makefile_read &&
2223                     Makefile->stat.is_file) {
2224                            Makefile->stat.has_sccs = save_M_has_sccs;
2225                            primary_makefile = Makefile;
2226                            makefile_read = read_makefile(Makefile,
2227                                                       false,
2228                                                       false,
2229                                                       true);
2230                 }
2231             }
2232         }
2233         do_not_exec_rule = save_do_not_exec_rule;
2234         allrules_read = makefile_read;
2235         trace_reader = false;
2236 
2237 /*
2238  *      Now get current value of MAKEFLAGS and compare it with
2239  *      the saved value we set before reading makefile.
2240  *      If they are different then MAKEFLAGS is subsequently set by
2241  *      makefile, just leave it there. Otherwise, if make mode
2242  *      is changed by using .POSIX target in makefile we need
2243  *      to correct MAKEFLAGS value.
2244  */
2245         Name mf_val = getvar(makeflags);
2246         if( (posix != is_xpg4)
2247          && (!strcmp(mf_val->string_mb, makeflags_value_saved->string_mb)))
2248         {
2249                 if (makeflags_string_posix.buffer.start[1] == (int) nul_char) {
2250                         (void) SETVAR(makeflags,
2251                                       GETNAME(makeflags_string_posix.buffer.start + 1,
2252                                               FIND_LENGTH),
2253                                       false);
2254                 } else {
2255                         if (makeflags_string_posix.buffer.start[1] != (int) space_char) {
2256                                 (void) SETVAR(makeflags,
2257                                               GETNAME(makeflags_string_posix.buffer.start,
2258                                                       FIND_LENGTH),
2259                                               false);
2260                         } else {
2261                                 (void) SETVAR(makeflags,
2262                                               GETNAME(makeflags_string_posix.buffer.start + 2,
2263                                                       FIND_LENGTH),
2264                                               false);
2265                         }
2266                 }
2267         }
2268 
2269         if (makeflags_string.free_after_use) {
2270                 retmem(makeflags_string.buffer.start);
2271         }
2272         if (makeflags_string_posix.free_after_use) {
2273                 retmem(makeflags_string_posix.buffer.start);
2274         }
2275         makeflags_string.buffer.start = NULL;
2276         makeflags_string_posix.buffer.start = NULL;
2277 
2278         if (posix) {
2279                 /*
2280                  * If the user did not redefine the ARFLAGS macro in the
2281                  * default makefile (make.rules), then we'd like to
2282                  * change the macro value of ARFLAGS to be in accordance
2283                  * with "POSIX" requirements.
2284                  */
2285                 MBSTOWCS(wcs_buffer, NOCATGETS("ARFLAGS"));
2286                 name = GETNAME(wcs_buffer, wslen(wcs_buffer));
2287                 macro = get_prop(name->prop, macro_prop);
2288                 if ((macro != NULL) && /* Maybe (macro == NULL) || ? */
2289                     (IS_EQUAL(macro->body.macro.value->string_mb,
2290                               NOCATGETS("rv")))) {
2291                         MBSTOWCS(wcs_buffer, NOCATGETS("-rv"));
2292                         value = GETNAME(wcs_buffer, wslen(wcs_buffer));
2293                         (void) SETVAR(name,
2294                                       value,
2295                                       false);
2296                 }
2297         }
2298 
2299         if (!posix && !svr4) {
2300                 set_sgs_support();
2301         }
2302 
2303 
2304 /*
2305  *      Make sure KEEP_STATE is in the environment if KEEP_STATE is on.
2306  */
2307         macro = get_prop(keep_state_name->prop, macro_prop);
2308         if ((macro != NULL) &&
2309             macro->body.macro.exported) {
2310                 keep_state = true;
2311         }
2312         if (keep_state) {
2313                 if (macro == NULL) {
2314                         macro = maybe_append_prop(keep_state_name,
2315                                                   macro_prop);
2316                 }
2317                 macro->body.macro.exported = true;
2318                 (void) SETVAR(keep_state_name,
2319                               empty_name,
2320                               false);
2321 
2322                 /*
2323                  *      Read state file
2324                  */
2325 
2326                 /* Before we read state, let's make sure we have
2327                 ** right state file.
2328                 */
2329                 /* just in case macro references are used in make_state file
2330                 ** name, we better expand them at this stage using expand_value.
2331                 */
2332                 INIT_STRING_FROM_STACK(dest, destbuffer);
2333                 expand_value(make_state, &dest, false);
2334 
2335                 make_state = GETNAME(dest.buffer.start, FIND_LENGTH);
2336 
2337                 if(!stat(make_state->string_mb, &make_state_stat)) {
2338                    if(!(make_state_stat.st_mode & S_IFREG) ) {
2339                         /* copy the make_state structure to the other
2340                         ** and then let make_state point to the new
2341                         ** one.
2342                         */
2343                       memcpy(&state_filename, make_state,sizeof(state_filename));
2344                       state_filename.string_mb = state_file_str_mb;
2345                 /* Just a kludge to avoid two slashes back to back */                   
2346                       if((make_state->hash.length == 1)&&
2347                                 (make_state->string_mb[0] == '/')) {
2348                          make_state->hash.length = 0;
2349                          make_state->string_mb[0] = '\0';
2350                       }
2351                       sprintf(state_file_str_mb,NOCATGETS("%s%s"),
2352                        make_state->string_mb,NOCATGETS("/.make.state"));
2353                       make_state = &state_filename;
2354                         /* adjust the length to reflect the appended string */
2355                       make_state->hash.length += 12;
2356                    }
2357                 } else { /* the file doesn't exist or no permission */
2358                    char tmp_path[MAXPATHLEN];
2359                    char *slashp;
2360 
2361                    if (slashp = strrchr(make_state->string_mb, '/')) {
2362                       strncpy(tmp_path, make_state->string_mb, 
2363                                 (slashp - make_state->string_mb));
2364                         tmp_path[slashp - make_state->string_mb]=0;
2365                       if(strlen(tmp_path)) {
2366                         if(stat(tmp_path, &make_state_stat)) {
2367                           warning(catgets(catd, 1, 192, "directory %s for .KEEP_STATE_FILE does not exist"),tmp_path);
2368                         }
2369                         if (access(tmp_path, F_OK) != 0) {
2370                           warning(catgets(catd, 1, 193, "can't access dir %s"),tmp_path);
2371                         }
2372                       }
2373                    }
2374                 }
2375                 if (report_dependencies_level != 1) {
2376                         Makefile_type   makefile_type_temp = makefile_type;
2377                         makefile_type = reading_statefile;
2378                         if (read_trace_level > 1) {
2379                                 trace_reader = true;
2380                         }
2381                         (void) read_simple_file(make_state,
2382                                                 false,
2383                                                 false,
2384                                                 false,
2385                                                 false,
2386                                                 false,
2387                                                 true);
2388                         trace_reader = false;
2389                         makefile_type = makefile_type_temp;
2390                 }
2391         }
2392 }
2393 
2394 /*
2395  * Scan the argv for options and "=" type args and make them readonly.
2396  */
2397 static void
2398 enter_argv_values(int argc, char *argv[], ASCII_Dyn_Array *makeflags_and_macro)
2399 {
2400         register char           *cp;
2401         register int            i;
2402         int                     length;
2403         register Name           name;
2404         int                     opt_separator = argc; 
2405         char                    tmp_char;
2406         wchar_t                 *tmp_wcs_buffer;
2407         register Name           value;
2408         Boolean                 append = false;
2409         Property                macro;
2410         struct stat             statbuf;
2411 
2412 
2413         /* Read argv options and "=" type args and make them readonly. */
2414         makefile_type = reading_nothing;
2415         for (i = 1; i < argc; ++i) {
2416                 append = false;
2417                 if (argv[i] == NULL) {
2418                         continue;
2419                 } else if (((argv[i][0] == '-') && (argv[i][1] == '-')) ||
2420                            ((argv[i][0] == (int) ' ') &&
2421                             (argv[i][1] == (int) '-') &&
2422                             (argv[i][2] == (int) ' ') &&
2423                             (argv[i][3] == (int) '-'))) {
2424                         argv[i] = NULL;
2425                         opt_separator = i;
2426                         continue;
2427                 } else if ((i < opt_separator) && (argv[i][0] == (int) hyphen_char)) {
2428                         switch (parse_command_option(argv[i][1])) {
2429                         case 1: /* -f seen */
2430                                 ++i;
2431                                 continue;
2432                         case 2: /* -c seen */
2433                                 if (argv[i+1] == NULL) {
2434                                         fatal(catgets(catd, 1, 194, "No dmake rcfile argument after -c flag"));
2435                                 }
2436                                 MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_RCFILE"));
2437                                 name = GETNAME(wcs_buffer, FIND_LENGTH);
2438                                 break;
2439                         case 4: /* -g seen */
2440                                 if (argv[i+1] == NULL) {
2441                                         fatal(catgets(catd, 1, 195, "No dmake group argument after -g flag"));
2442                                 }
2443                                 MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_GROUP"));
2444                                 name = GETNAME(wcs_buffer, FIND_LENGTH);
2445                                 break;
2446                         case 8: /* -j seen */
2447                                 if (argv[i+1] == NULL) {
2448                                         fatal(catgets(catd, 1, 196, "No dmake max jobs argument after -j flag"));
2449                                 }
2450                                 MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_MAX_JOBS"));
2451                                 name = GETNAME(wcs_buffer, FIND_LENGTH);
2452                                 break;
2453                         case 16: /* -M seen */
2454                                 if (argv[i+1] == NULL) {
2455                                         fatal(catgets(catd, 1, 323, "No pmake machinesfile argument after -M flag"));
2456                                 }
2457                                 MBSTOWCS(wcs_buffer, NOCATGETS("PMAKE_MACHINESFILE"));
2458                                 name = GETNAME(wcs_buffer, FIND_LENGTH);
2459                                 break;
2460                         case 32: /* -m seen */
2461                                 if (argv[i+1] == NULL) {
2462                                         fatal(catgets(catd, 1, 197, "No dmake mode argument after -m flag"));
2463                                 }
2464                                 MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_MODE"));
2465                                 name = GETNAME(wcs_buffer, FIND_LENGTH);
2466                                 break;
2467                         case 128: /* -O seen */
2468                                 if (argv[i+1] == NULL) {
2469                                         fatal(catgets(catd, 1, 287, "No file descriptor argument after -O flag"));
2470                                 }
2471                                 mtool_msgs_fd = atoi(argv[i+1]);
2472                                 /* find out if mtool_msgs_fd is a valid file descriptor */
2473                                 if (fstat(mtool_msgs_fd, &statbuf) < 0) {
2474                                         fatal(catgets(catd, 1, 355, "Invalid file descriptor %d after -O flag"), mtool_msgs_fd);
2475                                 }
2476                                 argv[i] = NULL;
2477                                 argv[i+1] = NULL;
2478                                 continue;
2479                         case 256: /* -K seen */
2480                                 if (argv[i+1] == NULL) {
2481                                         fatal(catgets(catd, 1, 288, "No makestate filename argument after -K flag"));
2482                                 }
2483                                 MBSTOWCS(wcs_buffer, argv[i+1]);
2484                                 make_state = GETNAME(wcs_buffer, FIND_LENGTH);
2485                                 keep_state = true;
2486                                 argv[i] = NULL;
2487                                 argv[i+1] = NULL;
2488                                 continue;
2489                         case 512:       /* -o seen */
2490                                 if (argv[i+1] == NULL) {
2491                                         fatal(catgets(catd, 1, 312, "No dmake output dir argument after -o flag"));
2492                                 }
2493                                 MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_ODIR"));
2494                                 name = GETNAME(wcs_buffer, FIND_LENGTH);
2495                                 break;
2496                         case 1024: /* -x seen */
2497                                 if (argv[i+1] == NULL) {
2498                                         fatal(catgets(catd, 1, 351, "No argument after -x flag"));
2499                                 }
2500                                 length = strlen( NOCATGETS("SUN_MAKE_COMPAT_MODE="));
2501                                 if (strncmp(argv[i+1], NOCATGETS("SUN_MAKE_COMPAT_MODE="), length) == 0) {
2502                                         argv[i+1] = &argv[i+1][length];
2503                                         MBSTOWCS(wcs_buffer, NOCATGETS("SUN_MAKE_COMPAT_MODE"));
2504                                         name = GETNAME(wcs_buffer, FIND_LENGTH);
2505                                         dmake_compat_mode_specified = dmake_add_mode_specified;
2506                                         break;
2507                                 }
2508                                 length = strlen( NOCATGETS("DMAKE_OUTPUT_MODE="));
2509                                 if (strncmp(argv[i+1], NOCATGETS("DMAKE_OUTPUT_MODE="), length) == 0) {
2510                                         argv[i+1] = &argv[i+1][length];
2511                                         MBSTOWCS(wcs_buffer, NOCATGETS("DMAKE_OUTPUT_MODE"));
2512                                         name = GETNAME(wcs_buffer, FIND_LENGTH);
2513                                         dmake_output_mode_specified = dmake_add_mode_specified;
2514                                 } else {
2515                                         warning(catgets(catd, 1, 354, "Unknown argument `%s' after -x flag (ignored)"),
2516                                               argv[i+1]);
2517                                         argv[i] = argv[i + 1] = NULL;
2518                                         continue;
2519                                 }
2520                                 break;
2521                         default: /* Shouldn't reach here */
2522                                 argv[i] = NULL;
2523                                 continue;
2524                         }
2525                         argv[i] = NULL;
2526                         if (i == (argc - 1)) {
2527                                 break;
2528                         }
2529                         if ((length = strlen(argv[i+1])) >= MAXPATHLEN) {
2530                                 tmp_wcs_buffer = ALLOC_WC(length + 1);
2531                                 (void) mbstowcs(tmp_wcs_buffer, argv[i+1], length + 1);
2532                                 value = GETNAME(tmp_wcs_buffer, FIND_LENGTH);
2533                                 retmem(tmp_wcs_buffer);
2534                         } else {
2535                                 MBSTOWCS(wcs_buffer, argv[i+1]);
2536                                 value = GETNAME(wcs_buffer, FIND_LENGTH);
2537                         }
2538                         argv[i+1] = NULL;
2539                 } else if ((cp = strchr(argv[i], (int) equal_char)) != NULL) {
2540 /* 
2541  * Combine all macro in dynamic array
2542  */
2543                         if(*(cp-1) == (int) plus_char)
2544                         {
2545                                 if(isspace(*(cp-2))) {
2546                                         append = true;
2547                                         cp--;
2548                                 }
2549                         }
2550                         if(!append)
2551                                 append_or_replace_macro_in_dyn_array(makeflags_and_macro, argv[i]);
2552 
2553                         while (isspace(*(cp-1))) {
2554                                 cp--;
2555                         }
2556                         tmp_char = *cp;
2557                         *cp = (int) nul_char;
2558                         MBSTOWCS(wcs_buffer, argv[i]);
2559                         *cp = tmp_char;
2560                         name = GETNAME(wcs_buffer, wslen(wcs_buffer));
2561                         while (*cp != (int) equal_char) {
2562                                 cp++;
2563                         }
2564                         cp++;
2565                         while (isspace(*cp) && (*cp != (int) nul_char)) {
2566                                 cp++;
2567                         }
2568                         if ((length = strlen(cp)) >= MAXPATHLEN) {
2569                                 tmp_wcs_buffer = ALLOC_WC(length + 1);
2570                                 (void) mbstowcs(tmp_wcs_buffer, cp, length + 1);
2571                                 value = GETNAME(tmp_wcs_buffer, FIND_LENGTH);
2572                                 retmem(tmp_wcs_buffer);
2573                         } else {
2574                                 MBSTOWCS(wcs_buffer, cp);
2575                                 value = GETNAME(wcs_buffer, FIND_LENGTH);
2576                         }
2577                         argv[i] = NULL;
2578                 } else {
2579                         /* Illegal MAKEFLAGS argument */
2580                         continue;
2581                 }
2582                 if(append) {
2583                         setvar_append(name, value);
2584                         append = false;
2585                 } else {
2586                         macro = maybe_append_prop(name, macro_prop);
2587                         macro->body.macro.exported = true;
2588                         SETVAR(name, value, false)->body.macro.read_only = true;
2589                 }
2590         }
2591 }
2592 
2593 /*
2594  * Append the DMake option and value to the MAKEFLAGS string.
2595  */
2596 static void
2597 append_makeflags_string(Name name, register String makeflags_string)
2598 {
2599         const char      *option;
2600 
2601         if (strcmp(name->string_mb, NOCATGETS("DMAKE_GROUP")) == 0) {
2602                 option = NOCATGETS(" -g ");
2603         } else if (strcmp(name->string_mb, NOCATGETS("DMAKE_MAX_JOBS")) == 0) {
2604                 option = NOCATGETS(" -j ");
2605         } else if (strcmp(name->string_mb, NOCATGETS("DMAKE_MODE")) == 0) {
2606                 option = NOCATGETS(" -m ");
2607         } else if (strcmp(name->string_mb, NOCATGETS("DMAKE_ODIR")) == 0) {
2608                 option = NOCATGETS(" -o ");
2609         } else if (strcmp(name->string_mb, NOCATGETS("DMAKE_RCFILE")) == 0) {
2610                 option = NOCATGETS(" -c ");
2611         } else if (strcmp(name->string_mb, NOCATGETS("PMAKE_MACHINESFILE")) == 0) {
2612                 option = NOCATGETS(" -M ");
2613         } else if (strcmp(name->string_mb, NOCATGETS("DMAKE_OUTPUT_MODE")) == 0) {
2614                 option = NOCATGETS(" -x DMAKE_OUTPUT_MODE=");
2615         } else if (strcmp(name->string_mb, NOCATGETS("SUN_MAKE_COMPAT_MODE")) == 0) {
2616                 option = NOCATGETS(" -x SUN_MAKE_COMPAT_MODE=");
2617         } else {
2618                 fatal(catgets(catd, 1, 289, "Internal error: name not recognized in append_makeflags_string()"));
2619         }
2620         Property prop = maybe_append_prop(name, macro_prop);
2621         if( prop == 0 || prop->body.macro.value == 0 ||
2622             prop->body.macro.value->string_mb == 0 ) {
2623                 return;
2624         }
2625         char mbs_value[MAXPATHLEN + 100];
2626         strcpy(mbs_value, option);
2627         strcat(mbs_value, prop->body.macro.value->string_mb);
2628         MBSTOWCS(wcs_buffer, mbs_value);
2629         append_string(wcs_buffer, makeflags_string, FIND_LENGTH);
2630 }
2631 
2632 /*
2633  *      read_environment(read_only)
2634  *
2635  *      This routine reads the process environment when make starts and enters
2636  *      it as make macros. The environment variable SHELL is ignored.
2637  *
2638  *      Parameters:
2639  *              read_only       Should we make env vars read only?
2640  *
2641  *      Global variables used:
2642  *              report_pwd      Set if this make was started by other make
2643  */
2644 static void
2645 read_environment(Boolean read_only)
2646 {
2647         register char           **environment;
2648         int                     length;
2649         wchar_t                 *tmp_wcs_buffer;
2650         Boolean                 alloced_tmp_wcs_buffer = false;
2651         register wchar_t        *name;
2652         register wchar_t        *value;
2653         register Name           macro;
2654         Property                val;
2655         Boolean                 read_only_saved;
2656 
2657         reading_environment = true;
2658         environment = environ;
2659         for (; *environment; environment++) {
2660                 read_only_saved = read_only;
2661                 if ((length = strlen(*environment)) >= MAXPATHLEN) {
2662                         tmp_wcs_buffer = ALLOC_WC(length + 1);
2663                         alloced_tmp_wcs_buffer = true;
2664                         (void) mbstowcs(tmp_wcs_buffer, *environment, length + 1);
2665                         name = tmp_wcs_buffer;
2666                 } else {
2667                         MBSTOWCS(wcs_buffer, *environment);
2668                         name = wcs_buffer;
2669                 }
2670                 value = (wchar_t *) wschr(name, (int) equal_char);
2671 
2672                 /*
2673                  * Looks like there's a bug in the system, but sometimes
2674                  * you can get blank lines in *environment.
2675                  */
2676                 if (!value) {
2677                         continue;
2678                 }
2679                 MBSTOWCS(wcs_buffer2, NOCATGETS("SHELL="));
2680                 if (IS_WEQUALN(name, wcs_buffer2, wslen(wcs_buffer2))) {
2681                         continue;
2682                 }
2683                 MBSTOWCS(wcs_buffer2, NOCATGETS("MAKEFLAGS="));
2684                 if (IS_WEQUALN(name, wcs_buffer2, wslen(wcs_buffer2))) {
2685                         report_pwd = true;
2686                         /*
2687                          * In POSIX mode we do not want MAKEFLAGS to be readonly.
2688                          * If the MAKEFLAGS macro is subsequently set by the makefile,
2689                          * it replaces the MAKEFLAGS variable currently found in the
2690                          * environment.
2691                          * See Assertion 50 in section 6.2.5.3 of standard P1003.3.2/D8.
2692                          */
2693                         if(posix) {
2694                                 read_only_saved = false;
2695                         }
2696                 }
2697 
2698                 /*
2699                  * We ignore SUNPRO_DEPENDENCIES. This environment variable is
2700                  * set by make and read by cpp which then writes info to
2701                  * .make.dependency.xxx.  When make is invoked by another make
2702                  * (recursive make), we don't want to read this because then
2703                  * the child make will end up writing to the parent
2704                  * directory's .make.state and clobbering them.
2705                  */
2706                 MBSTOWCS(wcs_buffer2, NOCATGETS("SUNPRO_DEPENDENCIES"));
2707                 if (IS_WEQUALN(name, wcs_buffer2, wslen(wcs_buffer2))) {
2708                         continue;
2709                 }
2710 
2711                 macro = GETNAME(name, value - name);
2712                 maybe_append_prop(macro, macro_prop)->body.macro.exported =
2713                   true;
2714                 if ((value == NULL) || ((value + 1)[0] == (int) nul_char)) {
2715                         val = setvar_daemon(macro,
2716                                             (Name) NULL,
2717                                             false, no_daemon, false, debug_level);
2718                 } else {
2719                         val = setvar_daemon(macro,
2720                                             GETNAME(value + 1, FIND_LENGTH),
2721                                             false, no_daemon, false, debug_level);
2722                 }
2723                 val->body.macro.read_only = read_only_saved;
2724                 if (alloced_tmp_wcs_buffer) {
2725                         retmem(tmp_wcs_buffer);
2726                         alloced_tmp_wcs_buffer = false;
2727                 }
2728         }
2729         reading_environment = false;
2730 }
2731 
2732 /*
2733  *      read_makefile(makefile, complain, must_exist, report_file)
2734  *
2735  *      Read one makefile and check the result
2736  *
2737  *      Return value:
2738  *                              false is the read failed
2739  *
2740  *      Parameters:
2741  *              makefile        The file to read
2742  *              complain        Passed thru to read_simple_file()
2743  *              must_exist      Passed thru to read_simple_file()
2744  *              report_file     Passed thru to read_simple_file()
2745  *
2746  *      Global variables used:
2747  *              makefile_type   Set to indicate we are reading main file
2748  *              recursion_level Initialized
2749  */
2750 static Boolean
2751 read_makefile(register Name makefile, Boolean complain, Boolean must_exist, Boolean report_file)
2752 {
2753         Boolean                 b;
2754         
2755         makefile_type = reading_makefile;
2756         recursion_level = 0;
2757         reading_dependencies = true;
2758         b = read_simple_file(makefile, true, true, complain,
2759                              must_exist, report_file, false);
2760         reading_dependencies = false;
2761         return b;
2762 }
2763 
2764 /*
2765  *      make_targets(argc, argv, parallel_flag)
2766  *
2767  *      Call doname on the specified targets
2768  *
2769  *      Parameters:
2770  *              argc            You know what this is
2771  *              argv            You know what this is
2772  *              parallel_flag   True if building in parallel
2773  *
2774  *      Global variables used:
2775  *              build_failed_seen Used to generated message after failed -k
2776  *              commands_done   Used to generate message "Up to date"
2777  *              default_target_to_build First proper target in makefile
2778  *              init            The Name ".INIT", use to run command
2779  *              parallel        Global parallel building flag
2780  *              quest           make -q, suppresses messages
2781  *              recursion_level Initialized, used for tracing
2782  *              report_dependencies make -P, regroves whole process
2783  */
2784 static void
2785 make_targets(int argc, char **argv, Boolean parallel_flag)
2786 {
2787         int                     i;
2788         char                    *cp;
2789         Doname                  result;
2790         register Boolean        target_to_make_found = false;
2791 
2792         (void) doname(init, true, true);
2793         recursion_level = 1;
2794         parallel = parallel_flag;
2795 /*
2796  *      make remaining args
2797  */
2798 /*
2799         if ((report_dependencies_level == 0) && parallel) {
2800  */
2801         if (parallel) {
2802                 /*
2803                  * If building targets in parallel, start all of the
2804                  * remaining args to build in parallel.
2805                  */
2806                 for (i = 1; i < argc; i++) {
2807                         if ((cp = argv[i]) != NULL) {
2808                                 commands_done = false;
2809                                 if ((cp[0] == (int) period_char) &&
2810                                     (cp[1] == (int) slash_char)) {
2811                                         cp += 2;
2812                                 }
2813                                  if((cp[0] == (int) ' ') &&
2814                                     (cp[1] == (int) '-') &&
2815                                     (cp[2] == (int) ' ') &&
2816                                     (cp[3] == (int) '-')) {
2817                                     argv[i] = NULL;
2818                                         continue;
2819                                 }
2820                                 MBSTOWCS(wcs_buffer, cp);
2821                                 //default_target_to_build = GETNAME(wcs_buffer,
2822                                 //                                FIND_LENGTH);
2823                                 default_target_to_build = normalize_name(wcs_buffer,
2824                                                                   wslen(wcs_buffer));
2825                                 if (default_target_to_build == wait_name) {
2826                                         if (parallel_process_cnt > 0) {
2827                                                 finish_running();
2828                                         }
2829                                         continue;
2830                                 }
2831                                 top_level_target = get_wstring(default_target_to_build->string_mb);
2832                                 /*
2833                                  * If we can't execute the current target in
2834                                  * parallel, hold off the target processing
2835                                  * to preserve the order of the targets as they appeared
2836                                  * in command line.
2837                                  */
2838                                 if (!parallel_ok(default_target_to_build, false)
2839                                                 && parallel_process_cnt > 0) {
2840                                         finish_running();
2841                                 }
2842                                 result = doname_check(default_target_to_build,
2843                                                       true,
2844                                                       false,
2845                                                       false);
2846                                 gather_recursive_deps();
2847                                 if (/* !commands_done && */
2848                                     (result == build_ok) &&
2849                                     !quest &&
2850                                     (report_dependencies_level == 0) /*  &&
2851                                     (exists(default_target_to_build) > file_doesnt_exist)  */) {
2852                                         if (posix) {
2853                                                 if (!commands_done) {
2854                                                         (void) printf(catgets(catd, 1, 293, "`%s' is updated.\n"),
2855                                                                       default_target_to_build->string_mb);
2856                                                 } else {
2857                                                         if (no_action_was_taken) {
2858                                                                 (void) printf(catgets(catd, 1, 294, "`%s': no action was taken.\n"),
2859                                                                               default_target_to_build->string_mb);
2860                                                         }
2861                                                 }
2862                                         } else {
2863                                                 default_target_to_build->stat.time = file_no_time;
2864                                                 if (!commands_done &&
2865                                                     (exists(default_target_to_build) > file_doesnt_exist)) {
2866                                                         (void) printf(catgets(catd, 1, 295, "`%s' is up to date.\n"),
2867                                                                       default_target_to_build->string_mb);
2868                                                 }
2869                                         }
2870                                 }
2871                         }
2872                 }
2873                 /* Now wait for all of the targets to finish running */
2874                 finish_running();
2875                 //              setjmp(jmpbuffer);
2876                 
2877         }
2878         for (i = 1; i < argc; i++) {
2879                 if ((cp = argv[i]) != NULL) {
2880                         target_to_make_found = true;
2881                         if ((cp[0] == (int) period_char) &&
2882                             (cp[1] == (int) slash_char)) {
2883                                 cp += 2;
2884                         }
2885                                  if((cp[0] == (int) ' ') &&
2886                                     (cp[1] == (int) '-') &&
2887                                     (cp[2] == (int) ' ') &&
2888                                     (cp[3] == (int) '-')) {
2889                                     argv[i] = NULL;
2890                                         continue;
2891                                 }
2892                         MBSTOWCS(wcs_buffer, cp);
2893                         default_target_to_build = normalize_name(wcs_buffer, wslen(wcs_buffer));
2894                         top_level_target = get_wstring(default_target_to_build->string_mb);
2895                         report_recursion(default_target_to_build);
2896                         commands_done = false;
2897                         if (parallel) {
2898                                 result = (Doname) default_target_to_build->state;
2899                         } else {
2900                                 result = doname_check(default_target_to_build,
2901                                                       true,
2902                                                       false,
2903                                                       false);
2904                         }
2905                         gather_recursive_deps();
2906                         if (build_failed_seen) {
2907                                 build_failed_ever_seen = true;
2908                                 warning(catgets(catd, 1, 200, "Target `%s' not remade because of errors"),
2909                                         default_target_to_build->string_mb);
2910                         }
2911                         build_failed_seen = false;
2912                         if (report_dependencies_level > 0) {
2913                                 print_dependencies(default_target_to_build,
2914                                                    get_prop(default_target_to_build->prop,
2915                                                             line_prop));
2916                         }
2917                         default_target_to_build->stat.time =
2918                           file_no_time;
2919                         if (default_target_to_build->colon_splits > 0) {
2920                                 default_target_to_build->state =
2921                                   build_dont_know;
2922                         }
2923                         if (!parallel &&
2924                             /* !commands_done && */
2925                             (result == build_ok) &&
2926                             !quest &&
2927                             (report_dependencies_level == 0) /*  &&
2928                             (exists(default_target_to_build) > file_doesnt_exist)  */) {
2929                                 if (posix) {
2930                                         if (!commands_done) {
2931                                                 (void) printf(catgets(catd, 1, 296, "`%s' is updated.\n"),
2932                                                               default_target_to_build->string_mb);
2933                                         } else {
2934                                                 if (no_action_was_taken) {
2935                                                         (void) printf(catgets(catd, 1, 297, "`%s': no action was taken.\n"),
2936                                                                       default_target_to_build->string_mb);
2937                                                 }
2938                                         }
2939                                 } else {
2940                                         if (!commands_done &&
2941                                             (exists(default_target_to_build) > file_doesnt_exist)) {
2942                                                 (void) printf(catgets(catd, 1, 298, "`%s' is up to date.\n"),
2943                                                               default_target_to_build->string_mb);
2944                                         }
2945                                 }
2946                         }
2947                 }
2948         }
2949 
2950 /*
2951  *      If no file arguments have been encountered,
2952  *      make the first name encountered that doesnt start with a dot
2953  */
2954         if (!target_to_make_found) {
2955                 if (default_target_to_build == NULL) {
2956                         fatal(catgets(catd, 1, 202, "No arguments to build"));
2957                 }
2958                 commands_done = false;
2959                 top_level_target = get_wstring(default_target_to_build->string_mb);
2960                 report_recursion(default_target_to_build);
2961 
2962 
2963                 if (getenv(NOCATGETS("SPRO_EXPAND_ERRORS"))){
2964                         (void) printf(NOCATGETS("::(%s)\n"),
2965                                       default_target_to_build->string_mb);
2966                 }
2967 
2968 
2969                 result = doname_parallel(default_target_to_build, true, false);
2970                 gather_recursive_deps();
2971                 if (build_failed_seen) {
2972                         build_failed_ever_seen = true;
2973                         warning(catgets(catd, 1, 203, "Target `%s' not remade because of errors"),
2974                                 default_target_to_build->string_mb);
2975                 }
2976                 build_failed_seen = false;
2977                 if (report_dependencies_level > 0) {
2978                         print_dependencies(default_target_to_build,
2979                                            get_prop(default_target_to_build->
2980                                                     prop,
2981                                                     line_prop));
2982                 }
2983                 default_target_to_build->stat.time = file_no_time;
2984                 if (default_target_to_build->colon_splits > 0) {
2985                         default_target_to_build->state = build_dont_know;
2986                 }
2987                 if (/* !commands_done && */
2988                     (result == build_ok) &&
2989                     !quest &&
2990                     (report_dependencies_level == 0) /*  &&
2991                     (exists(default_target_to_build) > file_doesnt_exist)  */) {
2992                         if (posix) {
2993                                 if (!commands_done) {
2994                                         (void) printf(catgets(catd, 1, 299, "`%s' is updated.\n"),
2995                                                       default_target_to_build->string_mb);
2996                                 } else {
2997                                         if (no_action_was_taken) {
2998                                                 (void) printf(catgets(catd, 1, 300, "`%s': no action was taken.\n"),
2999                                                               default_target_to_build->string_mb);
3000                                         }
3001                                 }
3002                         } else {
3003                                 if (!commands_done &&
3004                                     (exists(default_target_to_build) > file_doesnt_exist)) {
3005                                         (void) printf(catgets(catd, 1, 301, "`%s' is up to date.\n"),
3006                                                       default_target_to_build->string_mb);
3007                                 }
3008                         }
3009                 }
3010         }
3011 }
3012 
3013 /*
3014  *      report_recursion(target)
3015  *
3016  *      If this is a recursive make and the parent make has KEEP_STATE on
3017  *      this routine reports the dependency to the parent make
3018  *
3019  *      Parameters:
3020  *              target          Target to report
3021  *
3022  *      Global variables used:
3023  *              makefiles_used          List of makefiles read
3024  *              recursive_name          The Name ".RECURSIVE", printed
3025  *              report_dependency       dwight
3026  */
3027 static void
3028 report_recursion(register Name target)
3029 {
3030         register FILE           *report_file = get_report_file();
3031 
3032         if ((report_file == NULL) || (report_file == (FILE*)-1)) {
3033                 return;
3034         }
3035         if (primary_makefile == NULL) {
3036                 /*
3037                  * This can happen when there is no makefile and
3038                  * only implicit rules are being used.
3039                  */
3040                 return;
3041         }
3042         (void) fprintf(report_file,
3043                        "%s: %s ",
3044                        get_target_being_reported_for(),
3045                        recursive_name->string_mb);
3046         report_dependency(get_current_path());
3047         report_dependency(target->string_mb);
3048         report_dependency(primary_makefile->string_mb);
3049         (void) fprintf(report_file, "\n");
3050 }
3051 
3052 /* Next function "append_or_replace_macro_in_dyn_array" must be in "misc.cc". */
3053 /* NIKMOL */
3054 extern void
3055 append_or_replace_macro_in_dyn_array(ASCII_Dyn_Array *Ar, char *macro)
3056 {
3057         register char   *cp0;   /* work pointer in macro */
3058         register char   *cp1;   /* work pointer in array */
3059         register char   *cp2;   /* work pointer in array */
3060         register char   *cp3;   /* work pointer in array */
3061         register char   *name;  /* macro name */
3062         register char   *value; /* macro value */
3063         register int    len_array;
3064         register int    len_macro;
3065 
3066         char * esc_value = NULL;
3067         int esc_len;
3068 
3069         if (!(len_macro = strlen(macro))) return;
3070         name = macro;
3071         while (isspace(*(name))) {
3072                 name++;
3073         }
3074         if (!(value = strchr(name, (int) equal_char))) {
3075                 /* no '=' in macro */
3076                 goto ERROR_MACRO;
3077         }
3078         cp0 = value;
3079         value++;
3080         while (isspace(*(value))) {
3081                 value++;
3082         }
3083         while (isspace(*(cp0-1))) {
3084                 cp0--;
3085         }
3086         if (cp0 <= name) goto ERROR_MACRO; /* no name */
3087         if (!(Ar->size)) goto ALLOC_ARRAY;
3088         cp1 = Ar->start;
3089 
3090 LOOK_FOR_NAME:
3091         if (!(cp1 = strchr(cp1, name[0]))) goto APPEND_MACRO;
3092         if (!(cp2 = strchr(cp1, (int) equal_char))) goto APPEND_MACRO;
3093         if (strncmp(cp1, name, (size_t)(cp0-name))) {
3094                 /* another name */
3095                 cp1++;
3096                 goto LOOK_FOR_NAME;
3097         }
3098         if (cp1 != Ar->start) {
3099                 if (!isspace(*(cp1-1))) {
3100                         /* another name */
3101                         cp1++;
3102                         goto LOOK_FOR_NAME;
3103                 }
3104         }
3105         for (cp3 = cp1 + (cp0-name); cp3 < cp2; cp3++) {
3106                 if (isspace(*cp3)) continue;
3107                 /* else: another name */
3108                 cp1++;
3109                 goto LOOK_FOR_NAME;
3110         }
3111         /* Look for the next macro name in array */
3112         cp3 = cp2+1;
3113         if (*cp3 != (int) doublequote_char) {
3114                 /* internal error */
3115                 goto ERROR_MACRO;
3116         }
3117         if (!(cp3 = strchr(cp3+1, (int) doublequote_char))) {
3118                 /* internal error */
3119                 goto ERROR_MACRO;
3120         }
3121         cp3++;
3122         while (isspace(*cp3)) {
3123                 cp3++;
3124         }
3125         
3126         cp2 = cp1;  /* remove old macro */
3127         if ((*cp3) && (cp3 < Ar->start + Ar->size)) {
3128                 for (; cp3 < Ar->start + Ar->size; cp3++) {
3129                         *cp2++ = *cp3;
3130                 }
3131         } 
3132         for (; cp2 < Ar->start + Ar->size; cp2++) {
3133                 *cp2 = 0;
3134         }
3135         if (*cp1) {
3136                 /* check next name */
3137                 goto LOOK_FOR_NAME;
3138         }
3139         goto APPEND_MACRO;
3140 
3141 ALLOC_ARRAY:
3142         if (Ar->size) {
3143                 cp1 = Ar->start;
3144         } else {
3145                 cp1 = 0;
3146         }
3147         Ar->size += 128;
3148         Ar->start = getmem(Ar->size);
3149         for (len_array=0; len_array < Ar->size; len_array++) {
3150                 Ar->start[len_array] = 0;
3151         }
3152         if (cp1) {
3153                 strcpy(Ar->start, cp1);
3154                 retmem((wchar_t *) cp1);
3155         }
3156 
3157 APPEND_MACRO:
3158         len_array = strlen(Ar->start);
3159         esc_value = (char*)malloc(strlen(value)*2 + 1);
3160         quote_str(value, esc_value);
3161         esc_len = strlen(esc_value) - strlen(value);
3162         if (len_array + len_macro + esc_len + 5 >= Ar->size) goto  ALLOC_ARRAY;
3163         strcat(Ar->start, " ");
3164         strncat(Ar->start, name, cp0-name);
3165         strcat(Ar->start, "=");
3166         strncat(Ar->start, esc_value, strlen(esc_value));
3167         free(esc_value);
3168         return;
3169 ERROR_MACRO:    
3170         /* Macro without '=' or with invalid left/right part */
3171         return;
3172 }
3173 
3174 #ifdef TEAMWARE_MAKE_CMN
3175 /*
3176  * This function, if registered w/ avo_cli_get_license(), will be called
3177  * if the application is about to exit because:
3178  *   1) there has been certain unrecoverable error(s) that cause the
3179  *      application to exit immediately.
3180  *   2) the user has lost a license while the application is running.
3181  */
3182 extern "C" void
3183 dmake_exit_callback(void)
3184 {
3185         fatal(catgets(catd, 1, 306, "can not get a license, exiting..."));
3186         exit(1);
3187 }
3188 
3189 /*
3190  * This function, if registered w/ avo_cli_get_license(), will be called
3191  * if the application can not get a license.
3192  */
3193 extern "C" void
3194 dmake_message_callback(char *err_msg)
3195 {
3196         static Boolean  first = true;
3197 
3198         if (!first) {
3199                 return;
3200         }
3201         first = false;
3202         if ((!list_all_targets) &&
3203             (report_dependencies_level == 0) &&
3204             (dmake_mode_type != serial_mode)) {
3205                 warning(catgets(catd, 1, 313, "can not get a TeamWare license, defaulting to serial mode..."));
3206         }
3207 }
3208 #endif
3209 
3210 
3211 static void
3212 report_dir_enter_leave(Boolean entering)
3213 {
3214         char    rcwd[MAXPATHLEN];
3215 static  char *  mlev = NULL;
3216         char *  make_level_str = NULL;
3217         int     make_level_val = 0;
3218 
3219         make_level_str = getenv(NOCATGETS("MAKELEVEL"));
3220         if(make_level_str) {
3221                 make_level_val = atoi(make_level_str);
3222         }
3223         if(mlev == NULL) {
3224                 mlev = (char*) malloc(MAXPATHLEN);
3225         }
3226         if(entering) {
3227                 sprintf(mlev, NOCATGETS("MAKELEVEL=%d"), make_level_val + 1);
3228         } else {
3229                 make_level_val--;
3230                 sprintf(mlev, NOCATGETS("MAKELEVEL=%d"), make_level_val);
3231         }
3232         putenv(mlev);
3233 
3234         if(report_cwd) {
3235                 if(make_level_val <= 0) {
3236                         if(entering) {
3237                                 sprintf( rcwd
3238                                        , catgets(catd, 1, 329, "dmake: Entering directory `%s'\n")
3239                                        , get_current_path());
3240                         } else {
3241                                 sprintf( rcwd
3242                                        , catgets(catd, 1, 331, "dmake: Leaving directory `%s'\n")
3243                                        , get_current_path());
3244                         }
3245                 } else {
3246                         if(entering) {
3247                                 sprintf( rcwd
3248                                        , catgets(catd, 1, 333, "dmake[%d]: Entering directory `%s'\n")
3249                                        , make_level_val, get_current_path());
3250                         } else {
3251                                 sprintf( rcwd
3252                                        , catgets(catd, 1, 335, "dmake[%d]: Leaving directory `%s'\n")
3253                                        , make_level_val, get_current_path());
3254                         }
3255                 }
3256                 printf(NOCATGETS("%s"), rcwd);
3257         }
3258 }