Subject: small sync for bootstrap/make from src/usr.bin/make
To: None <tech-pkg@netbsd.org>
From: Joerg Sonnenberger <joerg@britannica.bec.de>
List: tech-pkg
Date: 09/13/2005 00:42:48
--7JfCtLOvnd9MIVvH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline

Hi all,
the attached patch is merges the getopt(3) elimination from
src/usr.bin/make and fixes pkg/30319. Is their a good reason not to
include it? Can this be included?

Joerg

--7JfCtLOvnd9MIVvH
Content-Type: text/plain; charset=us-ascii
Content-Disposition: attachment; filename="bmake-degetopt.diff"

Index: main.c
===================================================================
RCS file: /home/joerg/wd/repository/netbsd/pkgsrc/bootstrap/bmake/main.c,v
retrieving revision 1.2
diff -u -r1.2 main.c
--- main.c	5 Jan 2005 21:54:40 -0000	1.2
+++ main.c	11 Sep 2005 23:10:48 -0000
@@ -194,6 +194,11 @@
 	char **argv;
 {
 	char *p;
+	int arginc;
+	char *argvalue;
+	const char *getopt_def;
+	char *optscan;
+	Boolean inOption;
 	int c;
 
 	if (argv[0] == 0)
@@ -205,8 +210,49 @@
 #else
 # define OPTFLAGS "BD:I:J:NPST:V:Wd:ef:ij:km:nqrst"
 #endif
-rearg:	while((c = getopt(argc, argv, OPTFLAGS)) != -1) {
+#undef optarg
+#define optarg argvalue	
+/* Can't actually use getopt(3) because rescanning is not portable */
+
+	getopt_def = OPTFLAGS;
+rearg:	
+	inOption = FALSE;
+	while(argc > 1) {
+		char *getopt_spec;
+		if(!inOption)
+			optscan = argv[1];
+		c = *optscan++;
+		arginc = 0;
+		if(inOption) {
+			if(c == '\0') {
+				++argv;
+				--argc;
+				inOption = FALSE;
+				continue;
+			}
+		} else {
+			if (c != '-')
+				break;
+			inOption = TRUE;
+			c = *optscan++;
+		}
+		/* '-' found at some earlier point */
+		getopt_spec = strchr(getopt_def, c);
+		if(c != '\0' && getopt_spec != NULL && getopt_spec[1] == ':') {
+			/* -<something> found, and <something> should have an arg */
+			inOption = FALSE;
+			arginc = 1;
+			argvalue = optscan;
+			if(*argvalue == '\0') {
+				argvalue = argv[2];
+				arginc = 2;
+			}
+		}
 		switch(c) {
+		case '\0':
+			arginc = 1;
+			inOption = FALSE;
+			break;
 		case 'D':
 			Var_Set(optarg, "1", VAR_GLOBAL, 0);
 			Var_Append(MAKEFLAGS, "-D", VAR_GLOBAL);
@@ -404,6 +450,8 @@
 #endif
 			usage();
 		}
+		argv += arginc;
+		argc -= arginc;
 	}
 
 	oldVars = TRUE;
@@ -413,20 +461,15 @@
 	 * perform them if so. Else take them to be targets and stuff them
 	 * on the end of the "create" list.
 	 */
-	for (argv += optind, argc -= optind; *argv; ++argv, --argc)
-		if (Parse_IsVar(*argv)) {
-			Parse_DoVar(*argv, VAR_CMD);
+	for (; argc > 1; ++argv, --argc)
+		if (Parse_IsVar(argv[1])) {
+			Parse_DoVar(argv[1], VAR_CMD);
 		} else {
-			if (!**argv)
+			if (!*argv[1])
 				Punt("illegal (null) argument.");
-			if (**argv == '-') {
-				if ((*argv)[1])
-					optind = 0;     /* -flag... */
-				else
-					optind = 1;     /* - */
+			if (*argv[1] == '-')
 				goto rearg;
-			}
-			(void)Lst_AtEnd(create, (ClientData)estrdup(*argv));
+			(void)Lst_AtEnd(create, (ClientData)estrdup(argv[1]));
 		}
 }
 

--7JfCtLOvnd9MIVvH--