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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2008 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#ifndef _CMDPARSE_H
#define _CMDPARSE_H
#ifdef __cplusplus
extern "C" {
#endif
#include <getopt.h>
/* subcommands must have a single bit on and must have exclusive values */
#define SUBCOMMAND_BASE 1
#define SUBCOMMAND(x) (SUBCOMMAND_BASE << x)
#define OBJECT_BASE 1
#define OBJECT(x) (OBJECT_BASE << x)
/* maximum length of an option argument */
#define MAXOPTARGLEN 256
/*
* Add objects here
*
* EXAMPLE:
* object_t object[] = {
* {"target", TARGET},
* {NULL, 0}
* };
*/
typedef struct _object {
char *name;
uint_t value;
} object_t;
/*
* This structure is passed into the caller's callback function and
* will contain a list of all options entered and their associated
* option arguments if applicable
*/
typedef struct _cmdOptions {
int optval;
char optarg[MAXOPTARGLEN + 1];
} cmdOptions_t;
/*
* list of objects, subcommands, valid short options, required flag and
* exlusive option string
*
* objectValue -> object
* subcommandValue -> subcommand value
* optionProp.optionString -> short options that are valid
* optionProp.required -> flag indicating whether at least one option is
* required
* optionProp.exclusive -> short options that are required to be exclusively
* entered
*
*
* If it's not here, there are no options for that object.
*
* The long options table specifies whether an option argument is required.
*
*
* EXAMPLE:
*
* Based on DISCOVERY entry below:
*
* MODIFY DISCOVERY accepts -i, -s, -t and -l
* MODIFY DISCOVERY requires at least one option
* MODIFY DISCOVERY has no exclusive options
*
*
* optionRules_t optionRules[] = {
* {DISCOVERY, MODIFY, "istl", B_TRUE, NULL},
* {0, 0, NULL, 0, NULL}
* };
*/
typedef struct _optionProp {
char *optionString;
boolean_t required;
char *exclusive;
} optionProp_t;
typedef struct _optionRules {
uint_t objectValue;
uint_t subcommandValue;
optionProp_t optionProp;
} optionRules_t;
/*
* Rules for subcommands and object operands
*
* Every object requires an entry
*
* value, reqOpCmd, optOpCmd, noOpCmd, invCmd, multOpCmd
*
* value -> numeric value of object
*
* The following five fields are comprised of values that are
* a bitwise OR of the subcommands related to the object
*
* reqOpCmd -> subcommands that must have an operand
* optOpCmd -> subcommands that may have an operand
* noOpCmd -> subcommands that will have no operand
* invCmd -> subcommands that are invalid
* multOpCmd -> subcommands that can accept multiple operands
* operandDefinition -> Usage definition for the operand of this object
*
*
* EXAMPLE:
*
* based on TARGET entry below:
* MODIFY and DELETE subcomamnds require an operand
* LIST optionally requires an operand
* There are no subcommands that requires that no operand is specified
* ADD and REMOVE are invalid subcommands for this operand
* DELETE can accept multiple operands
*
* objectRules_t objectRules[] = {
* {TARGET, MODIFY|DELETE, LIST, 0, ADD|REMOVE, DELETE,
* "target-name"},
* {0, 0, 0, 0, 0, NULL}
* };
*/
typedef struct _opCmd {
uint_t reqOpCmd;
uint_t optOpCmd;
uint_t noOpCmd;
uint_t invOpCmd;
uint_t multOpCmd;
} opCmd_t;
typedef struct _objectRules {
uint_t value;
opCmd_t opCmd;
char *operandDefinition;
} objectRules_t;
/*
* subcommand callback function
*
* argc - number of arguments in argv
* argv - operand arguments
* options - options entered on command line
* callData - pointer to caller data to be passed to subcommand function
*/
typedef int (*handler_t)(int argc, char *argv[], int, cmdOptions_t *options,
void *callData);
/*
* Add new subcommands here
*
* EXAMPLE:
* subcommand_t subcommands[] = {
* {"add", ADD, addFunc},
* {NULL, 0, NULL}
* };
*/
typedef struct _subcommand {
char *name;
uint_t value;
handler_t handler;
} subcommand_t;
#define required_arg required_argument
#define no_arg no_argument
/*
* Add short options and long options here
*
* name -> long option name
* has_arg -> required_arg, no_arg
* val -> short option character
* argDesc -> description of option argument
*
* Note: This structure may not be used if your CLI has no
* options. However, -?, --help and -V, --version will still be supported
* as they are standard for every CLI.
*
* EXAMPLE:
*
* optionTbl_t options[] = {
* {"filename", arg_required, 'f', "out-filename"},
* {NULL, 0, 0}
* };
*
*/
typedef struct _optionTbl {
char *name;
int has_arg;
int val;
char *argDesc;
} optionTbl_t;
/*
* After tables are set, assign them to this structure
* for passing into cmdparse()
*/
typedef struct _synTables {
char *versionString;
optionTbl_t *longOptionTbl;
subcommand_t *subcommandTbl;
object_t *objectTbl;
objectRules_t *objectRulesTbl;
optionRules_t *optionRulesTbl;
} synTables_t;
/*
* cmdParse is a parser that checks syntax of the input command against
* various rules tables.
*
* When syntax is successfully validated, the function associated with the
* subcommand is called using the subcommands table functions.
*
* Syntax for the command is as follows:
*
* command subcommand [<options>] object [<operand ...>]
*
*
* There are two standard short and long options assumed:
* -?, --help Provides usage on a command or subcommand
* and stops further processing of the arguments
*
* -V, --version Provides version information on the command
* and stops further processing of the arguments
*
* These options are loaded by this function.
*
* input:
* argc, argv from main
* syntax rules tables (synTables_t structure)
* callArgs - void * passed by caller to be passed to subcommand function
*
* output:
* funcRet - pointer to int that holds subcommand function return value
*
* Returns:
*
* zero on successful syntax parse and function call
*
* 1 on unsuccessful syntax parse (no function has been called)
* This could be due to a version or help call or simply a
* general usage call.
*
* -1 check errno, call failed
*
*/
int cmdParse(int numOperands, char *operands[], synTables_t synTables,
void *callerArgs, int *funcRet);
#ifdef __cplusplus
}
#endif
#endif /* _CMDPARSE_H */
|