sh: Prefer internal nextopt() to libc getopt().

This reduces code duplication and code size.

/usr/bin/printf is not affected.

Side effect: different error messages when certain builtins are passed
invalid options.
This commit is contained in:
Jilles Tjoelker 2012-09-15 21:56:30 +00:00
parent c99c0499e0
commit 7cbda73825
6 changed files with 47 additions and 87 deletions

View File

@ -79,7 +79,7 @@ static char *prevdir; /* previous working directory */
static char *cdcomppath;
int
cdcmd(int argc, char **argv)
cdcmd(int argc __unused, char **argv __unused)
{
const char *dest;
const char *path;
@ -89,9 +89,8 @@ cdcmd(int argc, char **argv)
int rc;
int errno1 = ENOENT;
optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
phys = Pflag;
while ((ch = getopt(argc, argv, "eLP")) != -1) {
while ((ch = nextopt("eLP")) != '\0') {
switch (ch) {
case 'e':
getcwderr = 1;
@ -102,18 +101,13 @@ cdcmd(int argc, char **argv)
case 'P':
phys = 1;
break;
default:
error("unknown option: -%c", optopt);
break;
}
}
argc -= optind;
argv += optind;
if (argc > 1)
if (*argptr != NULL && argptr[1] != NULL)
error("too many arguments");
if ((dest = *argv) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
if ((dest = *argptr) == NULL && (dest = bltinlookup("HOME", 1)) == NULL)
error("HOME not set");
if (*dest == '\0')
dest = ".";
@ -330,14 +324,13 @@ updatepwd(char *dir)
}
int
pwdcmd(int argc, char **argv)
pwdcmd(int argc __unused, char **argv __unused)
{
char *p;
int ch, phys;
optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
phys = Pflag;
while ((ch = getopt(argc, argv, "LP")) != -1) {
while ((ch = nextopt("LP")) != '\0') {
switch (ch) {
case 'L':
phys = 0;
@ -345,15 +338,10 @@ pwdcmd(int argc, char **argv)
case 'P':
phys = 1;
break;
default:
error("unknown option: -%c", optopt);
break;
}
}
argc -= optind;
argv += optind;
if (argc != 0)
if (*argptr != NULL)
error("too many arguments");
if (!phys && getpwd()) {

View File

@ -1223,7 +1223,7 @@ breakcmd(int argc, char **argv)
* The `command' command.
*/
int
commandcmd(int argc, char **argv)
commandcmd(int argc __unused, char **argv __unused)
{
const char *path;
int ch;
@ -1231,9 +1231,7 @@ commandcmd(int argc, char **argv)
path = bltinlookup("PATH", 1);
optind = optreset = 1;
opterr = 0;
while ((ch = getopt(argc, argv, "pvV")) != -1) {
while ((ch = nextopt("pvV")) != '\0') {
switch (ch) {
case 'p':
path = _PATH_STDPATH;
@ -1244,20 +1242,15 @@ commandcmd(int argc, char **argv)
case 'V':
cmd = TYPECMD_BIGV;
break;
case '?':
default:
error("unknown option: -%c", optopt);
}
}
argc -= optind;
argv += optind;
if (cmd != -1) {
if (argc != 1)
if (*argptr == NULL || argptr[1] != NULL)
error("wrong number of arguments");
return typecmd_impl(2, argv - 1, cmd, path);
return typecmd_impl(2, argptr - 1, cmd, path);
}
if (argc != 0)
if (*argptr != NULL)
error("commandcmd bad call");
/*

View File

@ -182,7 +182,7 @@ setterm(const char *term)
}
int
histcmd(int argc, char **argv)
histcmd(int argc, char **argv __unused)
{
int ch;
const char *editor = NULL;
@ -206,13 +206,10 @@ histcmd(int argc, char **argv)
if (argc == 1)
error("missing history argument");
optreset = 1; optind = 1; /* initialize getopt */
opterr = 0;
while (not_fcnumber(argv[optind]) &&
(ch = getopt(argc, argv, ":e:lnrs")) != -1)
while (not_fcnumber(*argptr) && (ch = nextopt("e:lnrs")) != '\0')
switch ((char)ch) {
case 'e':
editor = optarg;
editor = shoptarg;
break;
case 'l':
lflg = 1;
@ -226,13 +223,7 @@ histcmd(int argc, char **argv)
case 's':
sflg = 1;
break;
case ':':
error("option -%c expects argument", optopt);
case '?':
default:
error("unknown option: -%c", optopt);
}
argc -= optind, argv += optind;
savehandler = handler;
/*
@ -276,31 +267,26 @@ histcmd(int argc, char **argv)
/*
* If executing, parse [old=new] now
*/
if (lflg == 0 && argc > 0 &&
((repl = strchr(argv[0], '=')) != NULL)) {
pat = argv[0];
if (lflg == 0 && *argptr != NULL &&
((repl = strchr(*argptr, '=')) != NULL)) {
pat = *argptr;
*repl++ = '\0';
argc--, argv++;
argptr++;
}
/*
* determine [first] and [last]
*/
switch (argc) {
case 0:
if (*argptr == NULL) {
firststr = lflg ? "-16" : "-1";
laststr = "-1";
break;
case 1:
firststr = argv[0];
laststr = lflg ? "-1" : argv[0];
break;
case 2:
firststr = argv[0];
laststr = argv[1];
break;
default:
} else if (argptr[1] == NULL) {
firststr = argptr[0];
laststr = lflg ? "-1" : argptr[0];
} else if (argptr[2] == NULL) {
firststr = argptr[0];
laststr = argptr[1];
} else
error("too many arguments");
}
/*
* Turn into event numbers.
*/

View File

@ -250,15 +250,13 @@ restartjob(struct job *jp)
int
jobscmd(int argc, char *argv[])
jobscmd(int argc __unused, char *argv[] __unused)
{
char *id;
int ch, mode;
optind = optreset = 1;
opterr = 0;
mode = SHOWJOBS_DEFAULT;
while ((ch = getopt(argc, argv, "lps")) != -1) {
while ((ch = nextopt("lps")) != '\0') {
switch (ch) {
case 'l':
mode = SHOWJOBS_VERBOSE;
@ -269,18 +267,13 @@ jobscmd(int argc, char *argv[])
case 's':
mode = SHOWJOBS_PIDS;
break;
case '?':
default:
error("unknown option: -%c", optopt);
}
}
argc -= optind;
argv += optind;
if (argc == 0)
if (*argptr == NULL)
showjobs(0, mode);
else
while ((id = *argv++) != NULL)
while ((id = *argptr++) != NULL)
showjob(getjob(id), mode);
return (0);

View File

@ -640,10 +640,11 @@ showvarscmd(int argc __unused, char **argv __unused)
*/
int
exportcmd(int argc, char **argv)
exportcmd(int argc __unused, char **argv)
{
struct var **vpp;
struct var *vp;
char **ap;
char *name;
char *p;
char *cmdname;
@ -651,26 +652,19 @@ exportcmd(int argc, char **argv)
int flag = argv[0][0] == 'r'? VREADONLY : VEXPORT;
cmdname = argv[0];
optreset = optind = 1;
opterr = 0;
values = 0;
while ((ch = getopt(argc, argv, "p")) != -1) {
while ((ch = nextopt("p")) != '\0') {
switch (ch) {
case 'p':
values = 1;
break;
case '?':
default:
error("unknown option: -%c", optopt);
}
}
argc -= optind;
argv += optind;
if (values && argc != 0)
if (values && *argptr != NULL)
error("-p requires no arguments");
if (argc != 0) {
while ((name = *argv++) != NULL) {
if (*argptr != NULL) {
for (ap = argptr; (name = *ap) != NULL; ap++) {
if ((p = strchr(name, '=')) != NULL) {
p++;
} else {

View File

@ -64,6 +64,7 @@ static const char rcsid[] =
#define main printfcmd
#include "bltin/bltin.h"
#include "error.h"
#include "options.h"
#endif
#define PF(f, func) do { \
@ -101,15 +102,19 @@ int
main(int argc, char *argv[])
{
size_t len;
int ch, chopped, end, rval;
int chopped, end, rval;
char *format, *fmt, *start;
#ifndef SHELL
int ch;
(void) setlocale(LC_ALL, "");
#endif
#ifdef SHELL
optreset = 1; optind = 1; opterr = 0; /* initialize getopt */
#endif
nextopt("");
argc -= argptr - argv;
argv = argptr;
#else
while ((ch = getopt(argc, argv, "")) != -1)
switch (ch) {
case '?':
@ -119,6 +124,7 @@ main(int argc, char *argv[])
}
argc -= optind;
argv += optind;
#endif
if (argc < 1) {
usage();