Fixed option parsing. Grep would continue grepping, even when given a bad /
[oweals/busybox.git] / getopt.c
index fb75790d9a102020b9f82699a6593b352b59b0bf..b74dd65a1ca187895723f5e70dbb1f245e9bf3b5 100644 (file)
--- a/getopt.c
+++ b/getopt.c
@@ -37,7 +37,7 @@
  *     <misiek@misiek.eu.org>)
  * Ported to Busybox - Alfred M. Szmidt <ams@trillian.itslinux.org>
  *  Removed --version/-V and --help/-h in
- *  Removed prase_error(), using errorMsg() from Busybox instead
+ *  Removed prase_error(), using error_msg() from Busybox instead
  *  Replaced our_malloc with xmalloc and our_realloc with xrealloc
  *
  */
 #include <ctype.h>
 #include <getopt.h>
 
-#include "internal.h"
+#include "busybox.h"
 
 /* NON_OPT is the code that is returned when a non-option is found in '+'
    mode */
-#define NON_OPT 1
+static const int NON_OPT = 1;
 /* LONG_OPT is the code that is returned when a long option is found. */
-#define LONG_OPT 2
+static const int LONG_OPT = 2;
 
 /* The shells recognized. */
 typedef enum {BASH,TCSH} shell_t;
 
 
 /* Some global variables that tells us how to parse. */
-shell_t shell=BASH; /* The shell we generate output for. */
-int quiet_errors=0; /* 0 is not quiet. */
-int quiet_output=0; /* 0 is not quiet. */
-int quote=1; /* 1 is do quote. */
-int alternative=0; /* 0 is getopt_long, 1 is getopt_long_only */
+static shell_t shell=BASH; /* The shell we generate output for. */
+static int quiet_errors=0; /* 0 is not quiet. */
+static int quiet_output=0; /* 0 is not quiet. */
+static int quote=1; /* 1 is do quote. */
+static int alternative=0; /* 0 is getopt_long, 1 is getopt_long_only */
 
 /* Function prototypes */
-const char *normalize(const char *arg);
-int generate_output(char * argv[],int argc,const char *optstr,
+static const char *normalize(const char *arg);
+static int generate_output(char * argv[],int argc,const char *optstr,
                     const struct option *longopts);
-void add_long_options(char *options);
-void add_longopt(const char *name,int has_arg);
-void set_shell(const char *new_shell);
-void set_initial_shell(void);
+static void add_long_options(char *options);
+static void add_longopt(const char *name,int has_arg);
+static void set_shell(const char *new_shell);
 
 
 /*
@@ -199,7 +198,7 @@ int generate_output(char * argv[],int argc,const char *optstr,
 static struct option *long_options=NULL;
 static int long_options_length=0; /* Length of array */
 static int long_options_nr=0; /* Nr of used elements in array */
-#define LONG_OPTIONS_INCR 10
+static const int LONG_OPTIONS_INCR = 10;
 #define init_longopt() add_longopt(NULL,0)
 
 /* Register a long option. The contents of name is copied. */
@@ -258,7 +257,7 @@ void add_long_options(char *options)
                                         arg_opt=required_argument;
                                 }
                                 if (strlen(tokptr) == 0)
-                                        errorMsg("empty long option after -l or --long argument\n");
+                                        error_msg("empty long option after -l or --long argument");
                         }
                         add_longopt(tokptr,arg_opt);
                 }
@@ -277,7 +276,7 @@ void set_shell(const char *new_shell)
         else if (!strcmp(new_shell,"csh"))
                 shell=TCSH;
         else
-                errorMsg("unknown shell after -s or --shell argument\n");
+                error_msg("unknown shell after -s or --shell argument");
 }
 
 
@@ -306,22 +305,6 @@ static struct option longopts[]=
 /* Stop scanning as soon as a non-option argument is found! */
 static const char *shortopts="+ao:l:n:qQs:Tu";
 
-static const char getopt_usage[] =
-"getopt [OPTIONS]...\n"
-#ifndef BB_FEATURE_TRIVIAL_HELP
-"Parse command options\n"
-"  -a, --alternative            Allow long options starting with single -\n"
-"  -l, --longoptions=longopts   Long options to be recognized\n"
-"  -n, --name=progname          The name under which errors are reported\n"
-"  -o, --options=optstring      Short options to be recognized\n"
-"  -q, --quiet                  Disable error reporting by getopt(3)\n"
-"  -Q, --quiet-output           No normal output\n"
-"  -s, --shell=shell            Set shell quoting conventions\n"
-"  -T, --test                   Test for getopt(1) version\n"
-"  -u, --unqote                 Do not quote the output\n"
-#endif
-;
-
 
 int getopt_main(int argc, char *argv[])
 {
@@ -342,7 +325,7 @@ int getopt_main(int argc, char *argv[])
                         printf(" --\n");
                         exit(0);
                 } else
-                        fatalError("missing optstring argument\n");
+                        error_msg_and_die("missing optstring argument");
         }
 
         if (argv[1][0] != '-' || compatible) {
@@ -388,12 +371,12 @@ int getopt_main(int argc, char *argv[])
                         quote=0;
                         break;
                 default:
-                        usage(getopt_usage);
+                        show_usage();
                 }
 
         if (!optstr) {
                 if (optind >= argc)
-                        fatalError("missing optstring argument\n");
+                        error_msg_and_die("missing optstring argument");
                 else {
                         optstr=xmalloc(strlen(argv[optind])+1);
                         strcpy(optstr,argv[optind]);