bug-sysutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Bug-sysutils] argp patch for add-shell


From: Barry deFreese
Subject: [Bug-sysutils] argp patch for add-shell
Date: Thu, 27 May 2004 20:20:07 -0700
User-agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.6) Gecko/20040413 Debian/1.6-5

OK, here is argp for add-shell.  Builds and works.

Thanks,

--
Barry deFreese
Debian 3.0r1 "Woody"
GNU/Hurd
Registered Linux "Newbie" #302256 - Hurd H4XX0r wannabe

"Programming today is a race between software engineers striving
to build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots. So far, the Universe is
winning." Rich Cook.



Index: src/add-shell.c
===================================================================
RCS file: /cvsroot/sysutils/sysutils/src/add-shell.c,v
retrieving revision 1.2
diff -u -p -r1.2 add-shell.c
--- src/add-shell.c     27 May 2004 07:28:26 -0000      1.2
+++ src/add-shell.c     28 May 2004 04:09:15 -0000
@@ -19,6 +19,7 @@
  *  Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  */
 
+#include <argp.h>
 #include <errno.h>
 #include <stdio.h>
 #include <string.h>
@@ -30,25 +31,60 @@
 
 #define PRG_NAME "add-shell"
 
-extern int optind;
-extern char *optarg;
 extern const char *progname;
 
-/**
- * Show usage information
- */
-static void usage(void)
+const char *argp_program_bug_address = PACKAGE_BUGREPORT;
+
+static char args_doc[] = "SHELL";
+
+static char doc[] = 
+       N_("Add a shell to the list of valid shells.\n");
+
+static struct argp_option options[] = {
+       {"force", 'f', 0, 0,
+         N_("allow non-existing shells to be added\n"), 0},
+       {"verbose", 'v', 0, 0,
+         N_("warn if the shell is already listed as a valid shell\n"), 0},
+       { 0, 0, 0, 0, 0, 0}
+};
+
+struct arguments {
+       char *args;
+       long force;
+       long verbose;
+};
+
+/* Parse a single option */
+static error_t parse_opt(int key, char *arg, struct argp_state *state)
 {
-       fprintf(stdout,
-               _("Usage: %s [OPTION]... SHELL\n"
-                 "Add a shell to the list of valid shells.\n"
-                 "\n"
-                 "      --force            allow non-existing shells to be "
-                 "added\n"
-                 "      --verbose          warn if the shell is already "
-                 "listed as a valid shell\n"),
-               progname);
-       usage_help();
+       struct arguments *args = state->input;
+
+       switch(key) {
+       case 'f':
+               args->force = 1;
+               break;
+
+       case 'v':
+               args->verbose = 1;
+               break;
+
+       case ARGP_KEY_INIT:
+               args->force = 0;
+               args->verbose = 0;
+               break;
+
+       case ARGP_KEY_ARG:
+               if (state->arg_num >= 2)
+                       argp_usage(state);
+
+               args->args = arg;
+
+               break;
+
+       default:
+               return ARGP_ERR_UNKNOWN;
+       }
+       return 0;
 }
 
 /**
@@ -62,54 +98,27 @@ int main(int argc, char *argv[])
 {
        FILE *afp = NULL;
 
-       int force = 0;
-       int verbose = 0;
-
-       int optc;
-       int opt_index;
-
        int status = 0;
 
-       struct option const options[] = {
-               { "force", no_argument, 0, 'f' },
-               { "verbose", no_argument, 0, 'v' },
-               { "help", no_argument, 0, 'h' },
-               { "version", no_argument, 0, 'V' },
-               { 0, 0, 0, 0 }
+       /* argp parser */
+       struct argp argp = {
+               options, parse_opt, args_doc, doc, NULL, NULL, NULL
        };
 
+       struct arguments args;
+
+       argp_program_version_hook = version;
+
        errno = 0;
 
        /* Initialise support for locales, and set the program-name */
        if ((status = init_locales(PRG_NAME)))
                goto EXIT;
 
-       /* Parse the command-line options */
-       while ((optc = getopt_long(argc, argv, "",
-                                  options, &opt_index)) != -1) {
-               switch (optc) {
-               case 'f':
-                       force = 1;
-                       break;
-
-               case 'v':
-                       verbose = 1;
-                       break;
-
-               case 'h':
-                       usage();
-                       goto EXIT;
-
-               case 'V':
-                       version();
-                       goto EXIT;
-
-               default:
-                       usage();
-                       status = EINVAL;
-                       goto EXIT;
-               }
-       }
+       set_author_information(_("Written by David Weinhall.\n"));
+
+       /* Parse command line */
+       argp_parse(&argp, argc, argv, 0, 0, &args);
 
        /* Make sure the caller has root privileges */
        if ((status = is_root())) {
@@ -128,26 +137,7 @@ int main(int argc, char *argv[])
                goto EXIT;
        }
 
-       /* Make sure we get the correct number of arguments */
-       if (argc - optind < 1) {
-               fprintf(stderr,
-                       _("%s: too few arguments\n"
-                         "Try `%s --help' for more information.\n"),
-                       progname, progname);
-               status = EINVAL;
-               goto EXIT;
-       }
-
-       if ((argc - optind) > 1) {
-               fprintf(stderr,
-                       _("%s: too many arguments\n"
-                         "Try `%s --help' for more information.\n"),
-                       progname, progname);
-               status = EINVAL;
-               goto EXIT;
-       }
-
-       if (!force && (status = is_valid_shell(argv[optind]))) {
+       if (!args.force && (status = is_valid_shell(args.args))) {
                if (errno)
                        fprintf(stderr,
                                _("%s: invalid shell specified\n"),
@@ -155,8 +145,8 @@ int main(int argc, char *argv[])
                goto EXIT;
        }
 
-       if (!is_listed_shell(argv[optind])) {
-               if (verbose) {
+       if (!is_listed_shell(args.args)) {
+               if (args.verbose) {
                        fprintf(stderr,
                                _("%s: warning: `%s' already exists "
                                  "in the list of valid shells\n"),
@@ -172,7 +162,7 @@ int main(int argc, char *argv[])
                goto EXIT;
        }
 
-       if ((status = fprintf(afp, "%s\n", argv[optind])) < 0) {
+       if ((status = fprintf(afp, "%s\n", args.args)) < 0) {
                fprintf(stderr,
                        _("%s: `%s' failed; %s\n"),
                        progname, "fprintf()", strerror(errno));

reply via email to

[Prev in Thread] Current Thread [Next in Thread]