-
Notifications
You must be signed in to change notification settings - Fork 25
/
cmdline-utils.c
146 lines (126 loc) · 2.94 KB
/
cmdline-utils.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* (c) 2007-2016 Mark Hamilton Wright */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <alloca.h>
#include <unistd.h>
#include "cmdline-utils.h"
static char const* typestring[N_OPTION_TYPE] = { "string", "int", "float", "flag" };
static void setoption(option_t *option, char *optarg) {
switch(option->type) {
case OPTION_STRINGTYPE:
*((char **) option->var) = strdup(optarg);
break;
case OPTION_INTTYPE:
*((int *) option->var) = atoi(optarg);
break;
case OPTION_DOUBLETYPE:
*((double *) option->var) = atof(optarg);
break;
case OPTION_FLAGTYPE:
*((int *) option->var) ^= 0x1;
break;
case OPT_CHAR:
*((char *) option->var) = optarg[0];
break;
}
}
int cmdline_getoptions(option_t *options, int argc, char *argv[]) {
int max_optsize;
char *optname, *optarg;
int longopt;
int i,j,k;
int error;
if (argc == 1 || strcmp(argv[1],"--help") == 0) {
cmdline_printoptions(options);
exit(0);
}
max_optsize = 0;
for(i=1;i<argc;i++) {
if (strlen(argv[i]) > max_optsize) {
max_optsize = strlen(argv[i]);
}
}
optname = alloca(max_optsize+1);
error = 0;
i = 1;
while(i<argc) {
if (argv[i][0] == '-') {
if (argv[i][1] == '-') {
longopt = 1;
k = 2;
while(argv[i][k] != 0 && argv[i][k]!='=') {
optname[k-2] = argv[i][k];
k++;
}
optname[k-2] = 0;
if (argv[i][k] == 0) {
optarg = NULL;
} else {
optarg = argv[i] + k + 1;
}
} else {
longopt = 0;
optname[0] = argv[i][1];
optarg = argv[i+1];
}
} else {
/* Not an option then... */
i++;
continue;
}
j = 0;
while(options[j].var != NULL) {
if (longopt) {
if (strcmp(optname, options[j].longopt)==0) {
setoption(options + j, optarg);
break;
}
} else {
if (options[j].shortopt == optname[0]) {
setoption(options + j, optarg);
break;
}
}
j++;
}
if (options[j].var == NULL) {
fprintf(stderr,"Unrecognized option \"%s\"\n",argv[i]);
error = 1;
}
if (longopt || options[j].type == OPT_FLAG) {
i++;
} else {
i+=2;
}
}
return error;
}
void cmdline_printoptions(option_t *options) {
FILE *f;
int i;
f = isatty(1) ? stdout : stderr;
fprintf(f,"Summary of command line options - see manual for details\n\n");
i = 0;
while(options[i].var != NULL) {
fprintf(f," ");
if (options[i].shortopt) {
fprintf(f,"-%c", options[i].shortopt);
if (options[i].argument_required)
fprintf(f," <%s>", typestring[options[i].type]);
if (options[i].longopt) fprintf(f,", ");
}
if (options[i].longopt) {
fprintf(f,"--%s", options[i].longopt);
if (options[i].argument_required)
fprintf(f,"=<%s>", typestring[options[i].type]);
}
if (options[i].required)
fprintf(f,"\t(required)");
fprintf(f,"\n");
if (options[i].docstring)
fprintf(f," %s\n", options[i].docstring);
i++;
}
fprintf(f,"\n\n");
}