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