Author: jilles
Date: Sat Sep 15 21:56:30 2012
New Revision: 240541
URL: http://svn.freebsd.org/changeset/base/240541

Log:
  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.

Modified:
  head/bin/sh/cd.c
  head/bin/sh/eval.c
  head/bin/sh/histedit.c
  head/bin/sh/jobs.c
  head/bin/sh/var.c
  head/usr.bin/printf/printf.c

Modified: head/bin/sh/cd.c
==============================================================================
--- head/bin/sh/cd.c    Sat Sep 15 20:09:08 2012        (r240540)
+++ head/bin/sh/cd.c    Sat Sep 15 21:56:30 2012        (r240541)
@@ -79,7 +79,7 @@ static char *prevdir;         /* previous worki
 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()) {

Modified: head/bin/sh/eval.c
==============================================================================
--- head/bin/sh/eval.c  Sat Sep 15 20:09:08 2012        (r240540)
+++ head/bin/sh/eval.c  Sat Sep 15 21:56:30 2012        (r240541)
@@ -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");
 
        /*

Modified: head/bin/sh/histedit.c
==============================================================================
--- head/bin/sh/histedit.c      Sat Sep 15 20:09:08 2012        (r240540)
+++ head/bin/sh/histedit.c      Sat Sep 15 21:56:30 2012        (r240541)
@@ -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.
         */

Modified: head/bin/sh/jobs.c
==============================================================================
--- head/bin/sh/jobs.c  Sat Sep 15 20:09:08 2012        (r240540)
+++ head/bin/sh/jobs.c  Sat Sep 15 21:56:30 2012        (r240541)
@@ -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);

Modified: head/bin/sh/var.c
==============================================================================
--- head/bin/sh/var.c   Sat Sep 15 20:09:08 2012        (r240540)
+++ head/bin/sh/var.c   Sat Sep 15 21:56:30 2012        (r240541)
@@ -640,10 +640,11 @@ showvarscmd(int argc __unused, char **ar
  */
 
 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 {

Modified: head/usr.bin/printf/printf.c
==============================================================================
--- head/usr.bin/printf/printf.c        Sat Sep 15 20:09:08 2012        
(r240540)
+++ head/usr.bin/printf/printf.c        Sat Sep 15 21:56:30 2012        
(r240541)
@@ -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();
_______________________________________________
svn-src-all@freebsd.org mailing list
http://lists.freebsd.org/mailman/listinfo/svn-src-all
To unsubscribe, send any mail to "svn-src-all-unsubscr...@freebsd.org"

Reply via email to