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