Blame SOURCES/popt-1.13-alias-equal-arg.patch

96b077
diff -up popt-1.13/popt.c.alias-equal-arg popt-1.13/popt.c
96b077
--- popt-1.13/popt.c.alias-equal-arg	2007-12-11 20:04:06.000000000 +0200
96b077
+++ popt-1.13/popt.c	2011-06-14 11:57:21.000000000 +0300
96b077
@@ -308,8 +308,9 @@ static int handleExec(/*@special@*/ popt
96b077
 
96b077
 /* Only one of longName, shortName may be set at a time */
96b077
 static int handleAlias(/*@special@*/ poptContext con,
96b077
-		/*@null@*/ const char * longName, char shortName,
96b077
-		/*@exposed@*/ /*@null@*/ const char * nextCharArg)
96b077
+		/*@null@*/ const char * longName, size_t longNameLen,
96b077
+		char shortName,
96b077
+		/*@exposed@*/ /*@null@*/ const char * nextArg)
96b077
 	/*@uses con->aliases, con->numAliases, con->optionStack, con->os,
96b077
 		con->os->currAlias, con->os->currAlias->option.longName @*/
96b077
 	/*@modifies con @*/
96b077
@@ -319,9 +320,11 @@ static int handleAlias(/*@special@*/ pop
96b077
     int i;
96b077
 
96b077
     if (item) {
96b077
-	if (longName && (item->option.longName &&
96b077
-		!strcmp(longName, item->option.longName)))
96b077
+	if (longName && item->option.longName
96b077
+	 && longNameLen == strlen(item->option.longName)
96b077
+	 && !strncmp(longName, item->option.longName, longNameLen))
96b077
 	    return 0;
96b077
+	else
96b077
 	if (shortName && shortName == item->option.shortName)
96b077
 	    return 0;
96b077
     }
96b077
@@ -331,10 +334,14 @@ static int handleAlias(/*@special@*/ pop
96b077
 
96b077
     for (i = con->numAliases - 1; i >= 0; i--) {
96b077
 	item = con->aliases + i;
96b077
-	if (longName && !(item->option.longName &&
96b077
-			!strcmp(longName, item->option.longName)))
96b077
-	    continue;
96b077
-	else if (shortName != item->option.shortName)
96b077
+	if (longName) {
96b077
+	    if (item->option.longName == NULL)
96b077
+		continue;
96b077
+	    if (longNameLen != strlen(item->option.longName))
96b077
+		continue;
96b077
+	    if (strncmp(longName, item->option.longName, longNameLen))
96b077
+		continue;
96b077
+	} else if (shortName != item->option.shortName)
96b077
 	    continue;
96b077
 	break;
96b077
     }
96b077
@@ -343,8 +350,8 @@ static int handleAlias(/*@special@*/ pop
96b077
     if ((con->os - con->optionStack + 1) == POPT_OPTION_DEPTH)
96b077
 	return POPT_ERROR_OPTSTOODEEP;
96b077
 
96b077
-    if (nextCharArg && *nextCharArg)
96b077
-	con->os->nextCharArg = nextCharArg;
96b077
+    if (longName == NULL && nextArg && *nextArg)
96b077
+	con->os->nextCharArg = nextArg;
96b077
 
96b077
     con->os++;
96b077
     con->os->next = 0;
96b077
@@ -352,8 +359,20 @@ static int handleAlias(/*@special@*/ pop
96b077
     con->os->nextArg = NULL;
96b077
     con->os->nextCharArg = NULL;
96b077
     con->os->currAlias = con->aliases + i;
96b077
-    rc = poptDupArgv(con->os->currAlias->argc, con->os->currAlias->argv,
96b077
-		&con->os->argc, &con->os->argv);
96b077
+    {	const char ** av;
96b077
+	int ac = con->os->currAlias->argc;
96b077
+	/* Append --foo=bar arg to alias argv array (if present). */ 
96b077
+	if (longName && nextArg && *nextArg) {
96b077
+	    int i;
96b077
+	    av = alloca((ac + 1 + 1) * sizeof(*av));
96b077
+	    for (i = 0; i < ac; i++)
96b077
+		av[i] = con->os->currAlias->argv[i];
96b077
+	    av[ac++] = nextArg;
96b077
+	    av[ac] = NULL;
96b077
+	} else
96b077
+	    av = con->os->currAlias->argv;
96b077
+	rc = poptDupArgv(ac, av, &con->os->argc, &con->os->argv);
96b077
+    }
96b077
     con->os->argb = NULL;
96b077
 
96b077
     return (rc ? rc : 1);
96b077
@@ -795,13 +814,6 @@ int poptGetNextOpt(poptContext con)
96b077
 		else
96b077
 		    singleDash = 1;
96b077
 
96b077
-		/* XXX aliases with arg substitution need "--alias=arg" */
96b077
-		if (handleAlias(con, optString, '\0', NULL))
96b077
-		    continue;
96b077
-
96b077
-		if (handleExec(con, optString, '\0'))
96b077
-		    continue;
96b077
-
96b077
 		/* Check for "--long=arg" option. */
96b077
 		for (oe = optString; *oe && *oe != '='; oe++)
96b077
 		    {};
96b077
@@ -809,6 +821,15 @@ int poptGetNextOpt(poptContext con)
96b077
 		if (*oe == '=')
96b077
 		    longArg = oe + 1;
96b077
 
96b077
+		/* XXX aliases with arg substitution need "--alias=arg" */
96b077
+		if (handleAlias(con, optString, optStringLen, '\0', longArg)) {
96b077
+		    longArg = NULL;
96b077
+		    continue;
96b077
+		}
96b077
+
96b077
+		if (handleExec(con, optString, '\0'))
96b077
+		    continue;
96b077
+
96b077
 		opt = findOption(con->options, optString, optStringLen, '\0', &cb, &cbData,
96b077
 				 singleDash);
96b077
 		if (!opt && !singleDash)
96b077
@@ -834,7 +855,7 @@ int poptGetNextOpt(poptContext con)
96b077
 
96b077
 	    con->os->nextCharArg = NULL;
96b077
 
96b077
-	    if (handleAlias(con, NULL, *origOptString, origOptString + 1))
96b077
+	    if (handleAlias(con, NULL, 0, *origOptString, origOptString + 1))
96b077
 		continue;
96b077
 
96b077
 	    if (handleExec(con, NULL, *origOptString)) {