Commit cd9a5361 cd9a5361ab0416c7be112c84758745120a8fc014 by Sergey Poznyakoff

(mh_getopt): Rewritten. The main purpose is

to expand old-style options to their double-dash equivalents.
(mh_argv_preproc): New function. Preprocess the argv array.
1 parent 8e105853
...@@ -25,9 +25,9 @@ ...@@ -25,9 +25,9 @@
25 #include <stdlib.h> 25 #include <stdlib.h>
26 #include <mh_getopt.h> 26 #include <mh_getopt.h>
27 27
28 int mh_optind = 1; 28 static int mh_optind = 1;
29 char *mh_optarg; 29 static char *mh_optarg;
30 char *mh_optptr; 30 static char *mh_optptr;
31 31
32 int 32 int
33 mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc) 33 mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc)
...@@ -46,8 +46,11 @@ mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc) ...@@ -46,8 +46,11 @@ mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc)
46 return '+'; 46 return '+';
47 } 47 }
48 48
49 if (mh_optptr[0] != '-') 49 if (mh_optptr[0] != '-' || mh_optptr[1] == '-')
50 return EOF; 50 {
51 mh_optind++;
52 return 0;
53 }
51 54
52 optlen = strlen (mh_optptr+1); 55 optlen = strlen (mh_optptr+1);
53 for (p = mh_opt; p->opt; p++) 56 for (p = mh_opt; p->opt; p++)
...@@ -64,6 +67,7 @@ mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc) ...@@ -64,6 +67,7 @@ mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc)
64 67
65 if (p->opt) 68 if (p->opt)
66 { 69 {
70 char *longopt = p->longopt ? p->longopt : p->opt;
67 switch (p->flags) 71 switch (p->flags)
68 { 72 {
69 case MH_OPT_BOOL: 73 case MH_OPT_BOOL:
...@@ -71,17 +75,20 @@ mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc) ...@@ -71,17 +75,20 @@ mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc)
71 mh_optarg = "no"; 75 mh_optarg = "no";
72 else 76 else
73 mh_optarg = "yes"; 77 mh_optarg = "yes";
78 asprintf (&argv[mh_optind], "--%s=%s", longopt, mh_optarg);
74 break; 79 break;
75 80
76 case MH_OPT_ARG: 81 case MH_OPT_ARG:
82 asprintf (&argv[mh_optind], "--%s", longopt);
77 mh_optarg = argv[++mh_optind]; 83 mh_optarg = argv[++mh_optind];
78 break; 84 break;
79 85
80 default: 86 default:
87 asprintf (&argv[mh_optind], "--%s", longopt);
81 mh_optarg = NULL; 88 mh_optarg = NULL;
82 } 89 }
83 mh_optind++; 90 mh_optind++;
84 return p->key; 91 return 1;
85 } 92 }
86 else if (!strcmp (mh_optptr+1, "help")) 93 else if (!strcmp (mh_optptr+1, "help"))
87 { 94 {
...@@ -92,6 +99,14 @@ mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc) ...@@ -92,6 +99,14 @@ mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc)
92 } 99 }
93 100
94 void 101 void
102 mh_argv_preproc (int argc, char **argv, struct mh_argp_data *data)
103 {
104 mh_optind = 1;
105 while (mh_getopt (argc, argv, data->mh_option, data->doc) != EOF)
106 ;
107 }
108
109 void
95 mh_help (struct mh_option *mh_opt, const char *doc) 110 mh_help (struct mh_option *mh_opt, const char *doc)
96 { 111 {
97 struct mh_option *p; 112 struct mh_option *p;
......