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