=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/make/main.c,v retrieving revision 1.8 retrieving revision 1.9 diff -c -r1.8 -r1.9 *** src/usr.bin/make/main.c 1996/09/02 16:04:13 1.8 --- src/usr.bin/make/main.c 1996/11/30 21:08:59 1.9 *************** *** 1,9 **** ! /* $OpenBSD: main.c,v 1.8 1996/09/02 16:04:13 briggs Exp $ */ ! /* $NetBSD: main.c,v 1.30 1996/08/13 16:42:08 christos Exp $ */ /* ! * Copyright (c) 1988, 1989, 1990 The Regents of the University of California. ! * Copyright (c) 1988, 1989 by Adam de Boor * Copyright (c) 1989 by Berkeley Softworks * All rights reserved. * --- 1,9 ---- ! /* $OpenBSD: main.c,v 1.9 1996/11/30 21:08:59 millert Exp $ */ ! /* $NetBSD: main.c,v 1.31 1996/11/06 17:59:12 christos Exp $ */ /* ! * Copyright (c) 1988, 1989, 1990, 1993 ! * The Regents of the University of California. All rights reserved. * Copyright (c) 1989 by Berkeley Softworks * All rights reserved. * *************** *** 40,56 **** */ #ifndef lint ! char copyright[] = ! "@(#) Copyright (c) 1989 The Regents of the University of California.\n\ ! All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 ! static char sccsid[] = "@(#)main.c 5.25 (Berkeley) 4/1/91"; ! static char rcsid[] = "$NetBSD: main.c,v 1.26 1996/03/11 13:45:33 christos Exp $"; #else ! static char rcsid[] = "$OpenBSD: main.c,v 1.8 1996/09/02 16:04:13 briggs Exp $"; #endif #endif /* not lint */ --- 40,55 ---- */ #ifndef lint ! static char copyright[] = ! "@(#) Copyright (c) 1988, 1989, 1990, 1993\n\ ! The Regents of the University of California. All rights reserved.\n"; #endif /* not lint */ #ifndef lint #if 0 ! static char sccsid[] = "@(#)main.c 8.3 (Berkeley) 3/19/94"; #else ! static char rcsid[] = "$OpenBSD: main.c,v 1.9 1996/11/30 21:08:59 millert Exp $"; #endif #endif /* not lint */ *************** *** 87,93 **** --- 86,94 ---- #include #include #include + #ifndef MACHINE #include + #endif #include #include #include *************** *** 116,121 **** --- 117,124 ---- static Boolean noBuiltins; /* -r flag */ static Lst makefiles; /* ordered list of makefiles to read */ + static Boolean printVars; /* print value of one or more vars */ + static Lst variables; /* list of variables to print */ int maxJobs; /* -j argument */ static int maxLocal; /* -L argument */ Boolean compatMake; /* -B argument */ *************** *** 131,138 **** Boolean checkEnvFirst; /* -e flag */ static Boolean jobsRunning; /* TRUE if the jobs might be running */ ! static Boolean ReadMakefile(); ! static void usage(); static char *curdir; /* startup directory */ static char *objdir; /* where we chdir'ed to */ --- 134,143 ---- Boolean checkEnvFirst; /* -e flag */ static Boolean jobsRunning; /* TRUE if the jobs might be running */ ! static void MainParseArgs __P((int, char **)); ! char * chdir_verify_path __P((char *, char *)); ! static int ReadMakefile __P((ClientData, ClientData)); ! static void usage __P((void)); static char *curdir; /* startup directory */ static char *objdir; /* where we chdir'ed to */ *************** *** 163,171 **** optind = 1; /* since we're called more than once */ #ifdef REMOTE ! # define OPTFLAGS "BD:I:L:PSd:ef:ij:km:nqrst" #else ! # define OPTFLAGS "BD:I:PSd:ef:ij:km:nqrst" #endif rearg: while((c = getopt(argc, argv, OPTFLAGS)) != EOF) { switch(c) { --- 168,176 ---- optind = 1; /* since we're called more than once */ #ifdef REMOTE ! # define OPTFLAGS "BD:I:L:PSV:d:ef:ij:km:nqrst" #else ! # define OPTFLAGS "BD:I:PSV:d:ef:ij:km:nqrst" #endif rearg: while((c = getopt(argc, argv, OPTFLAGS)) != EOF) { switch(c) { *************** *** 179,184 **** --- 184,195 ---- Var_Append(MAKEFLAGS, "-I", VAR_GLOBAL); Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); break; + case 'V': + printVars = TRUE; + (void)Lst_AtEnd(variables, (ClientData)optarg); + Var_Append(MAKEFLAGS, "-V", VAR_GLOBAL); + Var_Append(MAKEFLAGS, optarg, VAR_GLOBAL); + break; case 'B': compatMake = TRUE; break; *************** *** 324,330 **** */ for (argv += optind, argc -= optind; *argv; ++argv, --argc) if (Parse_IsVar(*argv)) { ! char *var = strdup(*argv); Parse_DoVar(var, VAR_CMD); free(var); --- 335,341 ---- */ for (argv += optind, argc -= optind; *argv; ++argv, --argc) if (Parse_IsVar(*argv)) { ! char *var = estrdup(*argv); Parse_DoVar(var, VAR_CMD); free(var); *************** *** 375,380 **** --- 386,419 ---- MainParseArgs(argc, argv); } + char * + chdir_verify_path(path, obpath) + char *path; + char *obpath; + { + struct stat sb; + + if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { + if (chdir(path)) { + (void)fprintf(stderr, "make warning: %s: %s.\n", + path, strerror(errno)); + return 0; + } + else { + if (path[0] != '/') { + (void) snprintf(obpath, MAXPATHLEN, "%s/%s", + curdir, path); + return obpath; + } + else + return path; + } + } + + return 0; + } + + /*- * main -- * The main function, for obvious reasons. Initializes variables *************** *** 400,410 **** Lst targs; /* target nodes to create -- passed to Make_Init */ Boolean outOfDate = TRUE; /* FALSE if all targets up to date */ struct stat sb, sa; ! char *p, *p1, *path, *pwd, *getenv(), *getwd(); char mdpath[MAXPATHLEN + 1]; char obpath[MAXPATHLEN + 1]; char cdpath[MAXPATHLEN + 1]; - struct utsname utsname; char *machine = getenv("MACHINE"); Lst sysMkPath; /* Path of sys.mk */ char *cp = NULL, *start; --- 439,448 ---- Lst targs; /* target nodes to create -- passed to Make_Init */ Boolean outOfDate = TRUE; /* FALSE if all targets up to date */ struct stat sb, sa; ! char *p, *p1, *path, *pathp, *pwd; char mdpath[MAXPATHLEN + 1]; char obpath[MAXPATHLEN + 1]; char cdpath[MAXPATHLEN + 1]; char *machine = getenv("MACHINE"); Lst sysMkPath; /* Path of sys.mk */ char *cp = NULL, *start; *************** *** 443,449 **** if ((pwd = getenv("PWD")) != NULL) { if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino && ! sa.st_dev == sb.st_dev) (void) strcpy(curdir, pwd); } --- 481,487 ---- if ((pwd = getenv("PWD")) != NULL) { if (stat(pwd, &sb) == 0 && sa.st_ino == sb.st_ino && ! sa.st_dev == sb.st_dev) (void) strcpy(curdir, pwd); } *************** *** 457,462 **** --- 495,502 ---- */ if (!machine) { #ifndef MACHINE + struct utsname utsname; + if (uname(&utsname) == -1) { perror("make: uname"); exit(2); *************** *** 468,521 **** } /* ! * if the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory ! * exists, change into it and build there. Once things are ! * initted, have to add the original directory to the search path, * and modify the paths for the Makefiles apropriately. The * current directory is also placed as a variable for make scripts. */ ! if (!(path = getenv("MAKEOBJDIR"))) { ! path = _PATH_OBJDIR; ! (void) sprintf(mdpath, "%s.%s", path, machine); ! } ! else ! (void) strncpy(mdpath, path, MAXPATHLEN + 1); ! ! if (stat(mdpath, &sb) == 0 && S_ISDIR(sb.st_mode)) { ! ! if (chdir(mdpath)) { ! (void)fprintf(stderr, "make warning: %s: %s.\n", ! mdpath, strerror(errno)); ! objdir = curdir; } ! else { ! if (mdpath[0] != '/') { ! (void) sprintf(obpath, "%s/%s", curdir, mdpath); ! objdir = obpath; ! } ! else ! objdir = mdpath; ! } } else { ! if (stat(path, &sb) == 0 && S_ISDIR(sb.st_mode)) { ! ! if (chdir(path)) { ! (void)fprintf(stderr, "make warning: %s: %s.\n", ! path, strerror(errno)); ! objdir = curdir; ! } ! else { ! if (path[0] != '/') { ! (void) sprintf(obpath, "%s/%s", curdir, ! path); ! objdir = obpath; ! } ! else ! objdir = obpath; ! } ! } ! else objdir = curdir; } --- 508,546 ---- } /* ! * If the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory ! * exists, change into it and build there. (If a .${MACHINE} suffix ! * exists, use that directory instead). ! * Otherwise check MAKEOBJDIRPREFIX`cwd` (or by default, ! * _PATH_OBJDIRPREFIX`cwd`) and build there if it exists. ! * If all fails, use the current directory to build. ! * ! * Once things are initted, ! * have to add the original directory to the search path, * and modify the paths for the Makefiles apropriately. The * current directory is also placed as a variable for make scripts. */ ! if (!(pathp = getenv("MAKEOBJDIRPREFIX"))) { ! if (!(path = getenv("MAKEOBJDIR"))) { ! path = _PATH_OBJDIR; ! pathp = _PATH_OBJDIRPREFIX; ! (void) snprintf(mdpath, MAXPATHLEN, "%s.%s", ! path, machine); ! if (!(objdir = chdir_verify_path(mdpath, obpath))) ! if (!(objdir=chdir_verify_path(path, obpath))) { ! (void) snprintf(mdpath, MAXPATHLEN, ! "%s%s", pathp, curdir); ! if (!(objdir=chdir_verify_path(mdpath, ! obpath))) ! objdir = curdir; ! } } ! else if (!(objdir = chdir_verify_path(path, obpath))) ! objdir = curdir; } else { ! (void) snprintf(mdpath, MAXPATHLEN, "%s%s", pathp, curdir); ! if (!(objdir = chdir_verify_path(mdpath, obpath))) objdir = curdir; } *************** *** 523,528 **** --- 548,555 ---- create = Lst_Init(FALSE); makefiles = Lst_Init(FALSE); + printVars = FALSE; + variables = Lst_Init(FALSE); beSilent = FALSE; /* Print commands as executed */ ignoreErrors = FALSE; /* Pay attention to non-zero returns */ noExecute = FALSE; /* Execute all commands */ *************** *** 542,549 **** maxJobs = maxLocal; #endif compatMake = FALSE; /* No compat mode */ - /* * Initialize the parsing, directory and variable modules to prepare * for the reading of inclusion paths and variable settings on the --- 569,576 ---- maxJobs = maxLocal; #endif compatMake = FALSE; /* No compat mode */ + /* * Initialize the parsing, directory and variable modules to prepare * for the reading of inclusion paths and variable settings on the *************** *** 585,591 **** #else Main_ParseArgLine(getenv("MAKE")); #endif ! MainParseArgs(argc, argv); /* --- 612,618 ---- #else Main_ParseArgLine(getenv("MAKE")); #endif ! MainParseArgs(argc, argv); /* *************** *** 624,630 **** */ if (Lst_IsEmpty(sysIncPath)) { for (start = syspath; *start != '\0'; start = cp) { ! for (cp = start; *cp != '\0' && *cp != ':'; cp++) continue; if (*cp == '\0') { Dir_AddDir(sysIncPath, start); --- 651,657 ---- */ if (Lst_IsEmpty(sysIncPath)) { for (start = syspath; *start != '\0'; start = cp) { ! for (cp = start; *cp != '\0' && *cp != ':'; cp++) continue; if (*cp == '\0') { Dir_AddDir(sysIncPath, start); *************** *** 658,667 **** ln = Lst_Find(makefiles, (ClientData)NULL, ReadMakefile); if (ln != NILLNODE) Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); ! } else if (!ReadMakefile("makefile")) ! (void)ReadMakefile("Makefile"); ! (void)ReadMakefile(".depend"); Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL); if (p1) --- 685,694 ---- ln = Lst_Find(makefiles, (ClientData)NULL, ReadMakefile); if (ln != NILLNODE) Fatal("make: cannot open %s.", (char *)Lst_Datum(ln)); ! } else if (!ReadMakefile("makefile", NULL)) ! (void)ReadMakefile("Makefile", NULL); ! (void)ReadMakefile(".depend", NULL); Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL, &p1), VAR_GLOBAL); if (p1) *************** *** 719,724 **** --- 746,766 ---- if (DEBUG(GRAPH1)) Targ_PrintGraph(1); + /* print the values of any variables requested by the user */ + if (printVars) { + LstNode ln; + + for (ln = Lst_First(variables); ln != NILLNODE; + ln = Lst_Succ(ln)) { + char *value = Var_Value((char *)Lst_Datum(ln), + VAR_GLOBAL, &p1); + + printf("%s\n", value ? value : ""); + if (p1) + free(p1); + } + } + /* * Have now read the entire graph and need to make a list of targets * to create. If none was given on the command line, we consult the *************** *** 729,735 **** else targs = Targ_FindList(create, TARG_CREATE); ! if (!compatMake) { /* * Initialize job module before traversing the graph, now that * any .BEGIN and .END targets have been read. This is done --- 771,777 ---- else targs = Targ_FindList(create, TARG_CREATE); ! if (!compatMake && !printVars) { /* * Initialize job module before traversing the graph, now that * any .BEGIN and .END targets have been read. This is done *************** *** 745,758 **** /* Traverse the graph, checking on all the targets */ outOfDate = Make_Run(targs); ! } else /* * Compat_Init will take care of creating all the targets as * well as initializing the module. */ Compat_Run(targs); ! Lst_Destroy(targs, NOFREE); Lst_Destroy(makefiles, NOFREE); Lst_Destroy(create, (void (*) __P((ClientData))) free); --- 787,802 ---- /* Traverse the graph, checking on all the targets */ outOfDate = Make_Run(targs); ! } else if (!printVars) { /* * Compat_Init will take care of creating all the targets as * well as initializing the module. */ Compat_Run(targs); ! } ! Lst_Destroy(targs, NOFREE); + Lst_Destroy(variables, NOFREE); Lst_Destroy(makefiles, NOFREE); Lst_Destroy(create, (void (*) __P((ClientData))) free); *************** *** 785,793 **** * lots */ static Boolean ! ReadMakefile(fname) ! char *fname; /* makefile to read */ { extern Lst parseIncPath; FILE *stream; char *name, path[MAXPATHLEN + 1]; --- 829,838 ---- * lots */ static Boolean ! ReadMakefile(p, q) ! ClientData p, q; { + char *fname = p; /* makefile to read */ extern Lst parseIncPath; FILE *stream; char *name, path[MAXPATHLEN + 1]; *************** *** 888,894 **** */ (void) dup2(fds[1], 1); (void) close(fds[1]); ! (void) execv("/bin/sh", args); _exit(1); /*NOTREACHED*/ --- 933,939 ---- */ (void) dup2(fds[1], 1); (void) close(fds[1]); ! (void) execv("/bin/sh", args); _exit(1); /*NOTREACHED*/ *************** *** 902,914 **** * No need for the writing half */ (void) close(fds[1]); ! buf = Buf_Init (MAKE_BSIZE); do { char result[BUFSIZ]; cc = read(fds[0], result, sizeof(result)); ! if (cc > 0) Buf_AddBytes(buf, cc, (Byte *) result); } while (cc > 0 || (cc == -1 && errno == EINTR)); --- 947,959 ---- * No need for the writing half */ (void) close(fds[1]); ! buf = Buf_Init (MAKE_BSIZE); do { char result[BUFSIZ]; cc = read(fds[0], result, sizeof(result)); ! if (cc > 0) Buf_AddBytes(buf, cc, (Byte *) result); } while (cc > 0 || (cc == -1 && errno == EINTR)); *************** *** 927,933 **** res = (char *)Buf_GetAll (buf, &cc); Buf_Destroy (buf, FALSE); ! if (cc == 0) *err = "Couldn't read shell's output for \"%s\""; if (status) --- 972,978 ---- res = (char *)Buf_GetAll (buf, &cc); Buf_Destroy (buf, FALSE); ! if (cc == 0) *err = "Couldn't read shell's output for \"%s\""; if (status) *************** *** 1043,1049 **** * a message and exits. * * Results: ! * None * * Side Effects: * All children are killed indiscriminately and the program Lib_Exits --- 1088,1094 ---- * a message and exits. * * Results: ! * None * * Side Effects: * All children are killed indiscriminately and the program Lib_Exits *************** *** 1099,1108 **** /* * Finish -- * Called when aborting due to errors in child shell to signal ! * abnormal exit. * * Results: ! * None * * Side Effects: * The program exits --- 1144,1153 ---- /* * Finish -- * Called when aborting due to errors in child shell to signal ! * abnormal exit. * * Results: ! * None * * Side Effects: * The program exits *************** *** 1130,1136 **** } /* ! * emalloc -- * strdup, but die on error. */ char * --- 1175,1181 ---- } /* ! * estrdup -- * strdup, but die on error. */ char * *************** *** 1197,1204 **** usage() { (void)fprintf(stderr, ! "usage: make [-eiknqrst] [-D variable] [-d flags] [-f makefile ]\n\ ! [-I directory] [-j max_jobs] [-m directory] [variable=value]\n"); exit(2); } --- 1242,1250 ---- usage() { (void)fprintf(stderr, ! "usage: make [-Beiknqrst] [-D variable] [-d flags] [-f makefile ]\n\ ! [-I directory] [-j max_jobs] [-m directory] [-V variable]\n\ ! [variable=value] [target ...]\n"); exit(2); }