-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoptions.c
119 lines (99 loc) · 2.95 KB
/
options.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
// command line options parser
//
// Copyright (C) 2010 Melchior FRANZ <[email protected]>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License as
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
//
#include <string.h>
#include "options.h"
void init_options_context(struct option_parser_context *ctx, int argc, const char *argv[],
const struct command_line_option *options)
{
ctx->argc = argc;
ctx->argv = argv;
ctx->options = options;
ctx->index = 1;
ctx->option = ctx->argument = ctx->aggregate = "";
ctx->buf[0] = '-', ctx->buf[1] = ' ', ctx->buf[2] = '\0';
}
int get_option(struct option_parser_context *ctx)
{
const struct command_line_option *opt;
int grab_arg, found;
ctx->argument = "";
if (ctx->argc < 2)
return OPTIONS_DONE;
if (!ctx->argv[ctx->index] && !*ctx->aggregate)
return OPTIONS_DONE;
if (ctx->buf[1] == '-') {
ctx->option = ctx->argv[ctx->index++];
return OPTIONS_ARGUMENT;
}
if (*ctx->aggregate) {
ctx->buf[1] = *ctx->aggregate++;
ctx->option = ctx->buf;
} else {
ctx->option = ctx->argv[ctx->index++];
if (ctx->option[0] != '-')
return OPTIONS_ARGUMENT;
if (!ctx->option[1])
return OPTIONS_ARGUMENT;
if (ctx->option[1] == '-') {
if (!ctx->option[2]) {
ctx->buf[1] = '-';
return OPTIONS_TERMINATOR;
}
} else {
ctx->aggregate = &ctx->option[1];
return get_option(ctx);
}
}
for (grab_arg = found = 0, opt = ctx->options; opt->long_opt || opt->short_opt; opt++, found++) {
int len;
if (opt->short_opt && !strcmp(opt->short_opt, ctx->option)) {
if (opt->has_arg) {
if (*ctx->aggregate) {
ctx->argument = ctx->aggregate;
ctx->aggregate = "";
} else {
grab_arg = 1;
}
}
break;
} else if (opt->long_opt && !strncmp(opt->long_opt, ctx->option, len = strlen(opt->long_opt))
&& ctx->option[len] == '=') {
ctx->argument = ctx->option + strlen(opt->long_opt) + 1;
ctx->option = opt->long_opt;
if (!opt->has_arg)
return OPTIONS_EXCESS_ARGUMENT;
break;
} else if (opt->long_opt && !strcmp(opt->long_opt, ctx->option)) {
if (opt->has_arg)
grab_arg = 1;
break;
}
}
if (!opt->long_opt && !opt->short_opt)
return OPTIONS_UNKNOWN_OPTION;
if (grab_arg) {
if (ctx->argv[ctx->index]) {
ctx->argument = ctx->argv[ctx->index++];
} else {
ctx->argument = "";
return OPTIONS_MISSING_ARGUMENT;
}
}
return found;
}