25 |
#include <stdlib.h> |
#include <stdlib.h> |
26 |
#include <mh_getopt.h> |
#include <mh_getopt.h> |
27 |
|
|
28 |
int mh_optind = 1; |
static int mh_optind = 1; |
29 |
char *mh_optarg; |
static char *mh_optarg; |
30 |
char *mh_optptr; |
static char *mh_optptr; |
31 |
|
|
32 |
int |
int |
33 |
mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc) |
mh_getopt (int argc, char **argv, struct mh_option *mh_opt, const char *doc) |
46 |
return '+'; |
return '+'; |
47 |
} |
} |
48 |
|
|
49 |
if (mh_optptr[0] != '-') |
if (mh_optptr[0] != '-' || mh_optptr[1] == '-') |
50 |
return EOF; |
{ |
51 |
|
mh_optind++; |
52 |
|
return 0; |
53 |
|
} |
54 |
|
|
55 |
optlen = strlen (mh_optptr+1); |
optlen = strlen (mh_optptr+1); |
56 |
for (p = mh_opt; p->opt; p++) |
for (p = mh_opt; p->opt; p++) |
67 |
|
|
68 |
if (p->opt) |
if (p->opt) |
69 |
{ |
{ |
70 |
|
char *longopt = p->longopt ? p->longopt : p->opt; |
71 |
switch (p->flags) |
switch (p->flags) |
72 |
{ |
{ |
73 |
case MH_OPT_BOOL: |
case MH_OPT_BOOL: |
75 |
mh_optarg = "no"; |
mh_optarg = "no"; |
76 |
else |
else |
77 |
mh_optarg = "yes"; |
mh_optarg = "yes"; |
78 |
|
asprintf (&argv[mh_optind], "--%s=%s", longopt, mh_optarg); |
79 |
break; |
break; |
80 |
|
|
81 |
case MH_OPT_ARG: |
case MH_OPT_ARG: |
82 |
|
asprintf (&argv[mh_optind], "--%s", longopt); |
83 |
mh_optarg = argv[++mh_optind]; |
mh_optarg = argv[++mh_optind]; |
84 |
break; |
break; |
85 |
|
|
86 |
default: |
default: |
87 |
|
asprintf (&argv[mh_optind], "--%s", longopt); |
88 |
mh_optarg = NULL; |
mh_optarg = NULL; |
89 |
} |
} |
90 |
mh_optind++; |
mh_optind++; |
91 |
return p->key; |
return 1; |
92 |
} |
} |
93 |
else if (!strcmp (mh_optptr+1, "help")) |
else if (!strcmp (mh_optptr+1, "help")) |
94 |
{ |
{ |
99 |
} |
} |
100 |
|
|
101 |
void |
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 |
110 |
mh_help (struct mh_option *mh_opt, const char *doc) |
mh_help (struct mh_option *mh_opt, const char *doc) |
111 |
{ |
{ |
112 |
struct mh_option *p; |
struct mh_option *p; |