stdio.h defines BUFSIZ
[unix-history] / usr / src / usr.bin / make / main.c
index 0bdb085..913f6b5 100644 (file)
@@ -7,17 +7,7 @@
  * This code is derived from software contributed to Berkeley by
  * Adam de Boor.
  *
  * This code is derived from software contributed to Berkeley by
  * Adam de Boor.
  *
- * Redistribution and use in source and binary forms are permitted
- * provided that the above copyright notice and this paragraph are
- * duplicated in all such forms and that any documentation,
- * advertising materials, and other materials related to such
- * distribution and use acknowledge that the software was developed
- * by the University of California, Berkeley.  The name of the
- * University may not be used to endorse or promote products derived
- * from this software without specific prior written permission.
- * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
- * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
- * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
+ * %sccs.include.redist.c%
  */
 
 #ifndef lint
  */
 
 #ifndef lint
@@ -27,7 +17,7 @@ char copyright[] =
 #endif /* not lint */
 
 #ifndef lint
 #endif /* not lint */
 
 #ifndef lint
-static char sccsid[] = "@(#)main.c     5.14 (Berkeley) %G%";
+static char sccsid[] = "@(#)main.c     5.24 (Berkeley) %G%";
 #endif /* not lint */
 
 /*-
 #endif /* not lint */
 
 /*-
@@ -60,6 +50,7 @@ static char sccsid[] = "@(#)main.c    5.14 (Berkeley) %G%";
 #include <sys/param.h>
 #include <sys/signal.h>
 #include <sys/stat.h>
 #include <sys/param.h>
 #include <sys/signal.h>
 #include <sys/stat.h>
+#include <errno.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <varargs.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <varargs.h>
@@ -122,7 +113,8 @@ MainParseArgs(argc, argv)
        register char *cp;
        char c;
 
        register char *cp;
        char c;
 
-       while((c = getopt(argc, argv, "D:I:d:ef:ij:knqrstv")) != -1) {
+       optind = 1;     /* since we're called more than once */
+rearg: while((c = getopt(argc, argv, "D:I:d:ef:ij:knqrst")) != EOF) {
                switch(c) {
                case 'D':
                        Var_Set(optarg, "1", VAR_GLOBAL);
                switch(c) {
                case 'D':
                        Var_Set(optarg, "1", VAR_GLOBAL);
@@ -182,9 +174,6 @@ MainParseArgs(argc, argv)
                                case 'm':
                                        debug |= DEBUG_MAKE;
                                        break;
                                case 'm':
                                        debug |= DEBUG_MAKE;
                                        break;
-                               case 'p':
-                                       debug |= DEBUG_PARSE;
-                                       break;
                                case 's':
                                        debug |= DEBUG_SUFF;
                                        break;
                                case 's':
                                        debug |= DEBUG_SUFF;
                                        break;
@@ -253,12 +242,16 @@ MainParseArgs(argc, argv)
         * perform them if so. Else take them to be targets and stuff them
         * on the end of the "create" list.
         */
         * perform them if so. Else take them to be targets and stuff them
         * on the end of the "create" list.
         */
-       for (argv += optind; *argv; ++argv)
+       for (argv += optind, argc -= optind; *argv; ++argv, --argc)
                if (Parse_IsVar(*argv))
                        Parse_DoVar(*argv, VAR_CMD);
                else {
                        if (!**argv)
                if (Parse_IsVar(*argv))
                        Parse_DoVar(*argv, VAR_CMD);
                else {
                        if (!**argv)
-                               Punt("Bogus argument in MainParseArgs");
+                               Punt("illegal (null) argument.");
+                       if (**argv == '-') {
+                               optind = 0;
+                               goto rearg;
+                       }
                        (void)Lst_AtEnd(create, (ClientData)*argv);
                }
 }
                        (void)Lst_AtEnd(create, (ClientData)*argv);
                }
 }
@@ -288,6 +281,8 @@ Main_ParseArgLine(line)
        if (line == NULL)
                return;
        for (; *line == ' '; ++line);
        if (line == NULL)
                return;
        for (; *line == ' '; ++line);
+       if (!*line)
+               return;
 
        argv = brk_string(line, &argc);
        MainParseArgs(argc, argv);
 
        argv = brk_string(line, &argc);
        MainParseArgs(argc, argv);
@@ -317,7 +312,7 @@ main(argc, argv)
        Lst targs;      /* target nodes to create -- passed to Make_Init */
        Boolean outOfDate;      /* FALSE if all targets up to date */
        struct stat sb;
        Lst targs;      /* target nodes to create -- passed to Make_Init */
        Boolean outOfDate;      /* FALSE if all targets up to date */
        struct stat sb;
-       char *path, *getenv();
+       char *p, *path, *getenv();
 
        /*
         * if the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory
 
        /*
         * if the MAKEOBJDIR (or by default, the _PATH_OBJDIR) directory
@@ -339,8 +334,6 @@ main(argc, argv)
                        }
                }
                if (chdir(path)) {
                        }
                }
                if (chdir(path)) {
-                       extern int errno;
-
                        (void)fprintf(stderr, "make: %s: %s.\n",
                            path, strerror(errno));
                        exit(2);
                        (void)fprintf(stderr, "make: %s: %s.\n",
                            path, strerror(errno));
                        exit(2);
@@ -384,26 +377,24 @@ main(argc, argv)
 
        /*
         * Initialize various variables.
 
        /*
         * Initialize various variables.
-        *      .PMAKE gets how we were executed.
         *      MAKE also gets this name, for compatibility
         *      .MAKEFLAGS gets set to the empty string just in case.
         *      MFLAGS also gets initialized empty, for compatibility.
         */
         *      MAKE also gets this name, for compatibility
         *      .MAKEFLAGS gets set to the empty string just in case.
         *      MFLAGS also gets initialized empty, for compatibility.
         */
-       Var_Set(".PMAKE", argv[0], VAR_GLOBAL);
        Var_Set("MAKE", argv[0], VAR_GLOBAL);
        Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
        Var_Set("MFLAGS", "", VAR_GLOBAL);
        Var_Set("MACHINE", MACHINE, VAR_GLOBAL);
 
        /*
        Var_Set("MAKE", argv[0], VAR_GLOBAL);
        Var_Set(MAKEFLAGS, "", VAR_GLOBAL);
        Var_Set("MFLAGS", "", VAR_GLOBAL);
        Var_Set("MACHINE", MACHINE, VAR_GLOBAL);
 
        /*
-        * First snag any flags out of the PMAKE environment variable.
+        * First snag any flags out of the MAKE environment variable.
         * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
         * in a different format).
         */
 #ifdef POSIX
        Main_ParseArgLine(getenv("MAKEFLAGS"));
 #else
         * (Note this is *not* MAKEFLAGS since /bin/make uses that and it's
         * in a different format).
         */
 #ifdef POSIX
        Main_ParseArgLine(getenv("MAKEFLAGS"));
 #else
-       Main_ParseArgLine(getenv("PMAKE"));
+       Main_ParseArgLine(getenv("MAKE"));
 #endif
     
        MainParseArgs(argc, argv);
 #endif
     
        MainParseArgs(argc, argv);
@@ -453,13 +444,16 @@ main(argc, argv)
        } else if (!ReadMakefile("makefile"))
                (void)ReadMakefile("Makefile");
 
        } else if (!ReadMakefile("makefile"))
                (void)ReadMakefile("Makefile");
 
+       (void)ReadMakefile(".depend");
+
        Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL), VAR_GLOBAL);
 
        Var_Append("MFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL), VAR_GLOBAL);
 
-       /* Install all the flags into the PMAKE envariable. */
+       /* Install all the flags into the MAKE envariable. */
+       if ((p = Var_Value(MAKEFLAGS, VAR_GLOBAL)) && *p)
 #ifdef POSIX
 #ifdef POSIX
-       setenv("MAKEFLAGS", Var_Value(MAKEFLAGS, VAR_GLOBAL));
+               setenv("MAKEFLAGS", p, 1);
 #else
 #else
-       setenv("PMAKE", Var_Value(MAKEFLAGS, VAR_GLOBAL));
+               setenv("MAKE", p, 1);
 #endif
 
        /*
 #endif
 
        /*
@@ -572,25 +566,29 @@ ReadMakefile(fname)
                Parse_File("(stdin)", stdin);
                Var_Set("MAKEFILE", "", VAR_GLOBAL);
        } else {
                Parse_File("(stdin)", stdin);
                Var_Set("MAKEFILE", "", VAR_GLOBAL);
        } else {
+               if (stream = fopen(fname, "r"))
+                       goto found;
                /* if we've chdir'd, rebuild the path name */
                if (curdir && *fname != '/') {
                        (void)sprintf(path, "%s/%s", curdir, fname);
                /* if we've chdir'd, rebuild the path name */
                if (curdir && *fname != '/') {
                        (void)sprintf(path, "%s/%s", curdir, fname);
-                       fname = path;
-               }
-               if (!(stream = fopen(fname, "r"))) {
-                       /* look in -I and system include directories. */
-                       if (!(name = Dir_FindFile(fname, parseIncPath)) &&
-                           !(name = Dir_FindFile(fname, sysIncPath)) ||
-                           !(stream = fopen(name, "r")))
-                               return(FALSE);
-                       fname = name;
+                       if (stream = fopen(path, "r")) {
+                               fname = path;
+                               goto found;
+                       }
                }
                }
+               /* look in -I and system include directories. */
+               name = Dir_FindFile(fname, parseIncPath);
+               if (!name)
+                       name = Dir_FindFile(fname, sysIncPath);
+               if (!name || !(stream = fopen(name, "r")))
+                       return(FALSE);
+               fname = name;
                /*
                 * set the MAKEFILE variable desired by System V fans -- the
                 * placement of the setting here means it gets set to the last
                 * makefile specified, as it is set by SysV make.
                 */
                /*
                 * set the MAKEFILE variable desired by System V fans -- the
                 * placement of the setting here means it gets set to the last
                 * makefile specified, as it is set by SysV make.
                 */
-               Var_Set("MAKEFILE", fname, VAR_GLOBAL);
+found:         Var_Set("MAKEFILE", fname, VAR_GLOBAL);
                Parse_File(fname, stream);
                (void)fclose(stream);
        }
                Parse_File(fname, stream);
                (void)fclose(stream);
        }
@@ -676,6 +674,7 @@ Punt(va_alist)
        va_list ap;
        char *fmt;
 
        va_list ap;
        char *fmt;
 
+       (void)fprintf(stderr, "make: ");
        va_start(ap);
        fmt = va_arg(ap, char *);
        (void)vfprintf(stderr, fmt, ap);
        va_start(ap);
        fmt = va_arg(ap, char *);
        (void)vfprintf(stderr, fmt, ap);
@@ -732,7 +731,6 @@ char *
 emalloc(len)
        u_int len;
 {
 emalloc(len)
        u_int len;
 {
-       extern int errno;
        char *p, *malloc();
 
        if (!(p = malloc(len)))
        char *p, *malloc();
 
        if (!(p = malloc(len)))
@@ -757,7 +755,7 @@ enomem()
 usage()
 {
        (void)fprintf(stderr,
 usage()
 {
        (void)fprintf(stderr,
-"usage: make [-eiknqrstv] [-D variable] [-d flags] [-f makefile ]\n\t\
+"usage: make [-eiknqrst] [-D variable] [-d flags] [-f makefile ]\n\t\
 [-I directory] [-j max_jobs] [variable=value]\n");
        exit(2);
 }
 [-I directory] [-j max_jobs] [variable=value]\n");
        exit(2);
 }