-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmdline.c
195 lines (135 loc) · 4.47 KB
/
cmdline.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
* Name: cmdline.c
* Author: Pietro Belotti
* Purpose: procedures for reading/managing command line arguments
*
* This code is published under the Eclipse Public License (EPL).
* See http://www.eclipse.org/legal/epl-v10.html
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "cmdline.h"
#define MALLOC_BLOCK 20
/*
* Action to be taken for each parameter
*/
int act (char ***argv, int *argc, enum etype tact, void *par) {
switch (tact) {
case TTOGGLE: *(char *) par = 1; break; /**< Toggle some parameter */
case TCHARA: if (!(--(*argc))) return -1; *(char *) par = **++*argv; break; /**< Char param */
case TINT: if (!(--(*argc))) return -1; *(int *) par = atoi (*++*argv); break; /**< Integer param */
case TDOUBLE: if (!(--(*argc))) return -1; *(double *) par = atof (*++*argv); break; /**< Double param */
case TSTRING: if (!(--(*argc))) return -1;
*(char **) par = (char *) realloc (*(char **) par, (1 + strlen (*++*argv)) * sizeof (char));
Strcpy (* (char **) par, strlen (**argv) + 1, **argv); break; /**< String param */
default: printf ("switch{} mismatch in act ()\n"); return -1;
}
return 0;
}
/*
* Set default value of the command line arguments
*/
void set_default_args (tpar *options) {
for (; options -> shortopt; options++)
switch (options -> type) {
case TINT: * (int *) (options -> par) = (int) options -> initial; break;
case TTOGGLE: * (char *) (options -> par) = (char) options -> initial; break;
case TCHARA: * (char *) (options -> par) = (char) options -> initial; break;
case TDOUBLE: * (double *) (options -> par) = (double) options -> initial; break;
case TSTRING: Strcpy (*(char **) options -> par, 1, ""); break;
default: ;
}
}
/*
* Read argument line
*/
char **readargs (int argc, char **argv, tpar *options) {
char **filenames = NULL;
char *cumul;
char *pname = *argv;
int nfiles = 0;
tpar *p2;
/*
* parse arguments, set corresponding parameters
*/
while (--argc) {
++argv;
if ((options) &&
(** argv == '-') && /* arg starts with a "-" */
((*argv) [1])) { /* not a single "-" (meaning I should read from stdin) */
cumul = *argv + 1;
if (*cumul == '-') { /** long option ("--something [val]") */
cumul++;
for (p2 = options; p2 -> shortopt; p2++) {
if (!strcmp (cumul, p2->longopt)) {
if (!(p2 -> par)) {
if (p2 -> type != TTOGGLE) argv++;
printf ("%s: not yet implemented\n", *argv);
}
else
if (act (&argv, &argc, p2->type, (p2->par))) {
printf ("%s: missing value\n", *(argv));
return NULL;
}
break;
}
}
if (!(p2 -> shortopt)) {
printf ("%s: unrecognized option \"--%s\"\n", pname, cumul);
}
}
else { /** short option ("-s [val]") */
for (; *cumul; cumul++) {
for (p2 = options; p2 -> shortopt; p2++)
if (*cumul == p2->shortopt) {
if (act (&argv, &argc, p2->type, (p2->par))) {
printf ("%s: missing value\n", *(argv));
return NULL;
}
break;
}
if (!(p2 -> shortopt)) {
printf ("%s: unrecognized option \"-%c\"\n", pname, *cumul);
}
}
}
}
else {
if (!(nfiles % MALLOC_BLOCK)) {
filenames = (char **) realloc (filenames, (nfiles + MALLOC_BLOCK) * sizeof (char *));
filenames [0] = NULL;
}
filenames [nfiles] = (char *) malloc ((strlen (*argv) + 1) * sizeof (char));
Strcpy (filenames [nfiles++], 1 + strlen (*argv), *argv);
}
}
if (filenames) {
if (!(nfiles % MALLOC_BLOCK))
filenames = (char **) realloc (filenames, (nfiles + 1) * sizeof (char *));
filenames [nfiles] = NULL;
}
return filenames;
}
/*
* Print help message
*/
void print_help (char *pname, tpar *opt) {
int i;
char arg [5];
char helpline [80];
printf ("Usage: %s [options] file...\nOptions are:\n", pname);
for (i=0; opt [i]. shortopt; i++) {
switch (opt [i]. type) {
case TTOGGLE: *arg=0; break; /*Sprintf (arg, " "); break;*/
case TCHARA: Sprintf (arg, " <c>"); break;
case TSTRING: Sprintf (arg, " <s>"); break;
case TDOUBLE: Sprintf (arg, " <x>"); break;
case TINT: Sprintf (arg, " <n>"); break;
}
Sprintf (helpline," -%c%s, --%s%s:", opt [i].shortopt, arg,
opt [i].longopt, arg);
printf ("%-40s %s\n", helpline, opt [i].help);
}
}