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