xref: /onnv-gate/usr/src/cmd/iscsiadm/cmdparse.h (revision 7836:4e95154b5b7a)
1*7836SJohn.Forte@Sun.COM /*
2*7836SJohn.Forte@Sun.COM  * CDDL HEADER START
3*7836SJohn.Forte@Sun.COM  *
4*7836SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
5*7836SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
6*7836SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
7*7836SJohn.Forte@Sun.COM  *
8*7836SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7836SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*7836SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
11*7836SJohn.Forte@Sun.COM  * and limitations under the License.
12*7836SJohn.Forte@Sun.COM  *
13*7836SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*7836SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7836SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*7836SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*7836SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7836SJohn.Forte@Sun.COM  *
19*7836SJohn.Forte@Sun.COM  * CDDL HEADER END
20*7836SJohn.Forte@Sun.COM  */
21*7836SJohn.Forte@Sun.COM /*
22*7836SJohn.Forte@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*7836SJohn.Forte@Sun.COM  * Use is subject to license terms.
24*7836SJohn.Forte@Sun.COM  */
25*7836SJohn.Forte@Sun.COM 
26*7836SJohn.Forte@Sun.COM #ifndef	_CMDPARSE_H
27*7836SJohn.Forte@Sun.COM #define	_CMDPARSE_H
28*7836SJohn.Forte@Sun.COM 
29*7836SJohn.Forte@Sun.COM 
30*7836SJohn.Forte@Sun.COM #ifdef	__cplusplus
31*7836SJohn.Forte@Sun.COM extern "C" {
32*7836SJohn.Forte@Sun.COM #endif
33*7836SJohn.Forte@Sun.COM 
34*7836SJohn.Forte@Sun.COM #include <getopt.h>
35*7836SJohn.Forte@Sun.COM 
36*7836SJohn.Forte@Sun.COM /* subcommands must have a single bit on and must have exclusive values */
37*7836SJohn.Forte@Sun.COM #define	SUBCOMMAND_BASE  1
38*7836SJohn.Forte@Sun.COM #define	SUBCOMMAND(x)  (SUBCOMMAND_BASE << x)
39*7836SJohn.Forte@Sun.COM 
40*7836SJohn.Forte@Sun.COM #define	OBJECT_BASE  1
41*7836SJohn.Forte@Sun.COM #define	OBJECT(x) (OBJECT_BASE << x)
42*7836SJohn.Forte@Sun.COM 
43*7836SJohn.Forte@Sun.COM /* maximum length of an option argument */
44*7836SJohn.Forte@Sun.COM #define	MAXOPTARGLEN   256
45*7836SJohn.Forte@Sun.COM 
46*7836SJohn.Forte@Sun.COM 
47*7836SJohn.Forte@Sun.COM /*
48*7836SJohn.Forte@Sun.COM  * Add objects here
49*7836SJohn.Forte@Sun.COM  *
50*7836SJohn.Forte@Sun.COM  * EXAMPLE:
51*7836SJohn.Forte@Sun.COM  *	object_t object[] = {
52*7836SJohn.Forte@Sun.COM  *	    {"target", TARGET},
53*7836SJohn.Forte@Sun.COM  *	    {NULL, 0}
54*7836SJohn.Forte@Sun.COM  *	};
55*7836SJohn.Forte@Sun.COM  */
56*7836SJohn.Forte@Sun.COM typedef struct _object {
57*7836SJohn.Forte@Sun.COM 	char *name;
58*7836SJohn.Forte@Sun.COM 	uint_t value;
59*7836SJohn.Forte@Sun.COM } object_t;
60*7836SJohn.Forte@Sun.COM 
61*7836SJohn.Forte@Sun.COM /*
62*7836SJohn.Forte@Sun.COM  * This structure is passed into the caller's callback function and
63*7836SJohn.Forte@Sun.COM  * will contain a list of all options entered and their associated
64*7836SJohn.Forte@Sun.COM  * option arguments if applicable
65*7836SJohn.Forte@Sun.COM  */
66*7836SJohn.Forte@Sun.COM typedef struct _cmdOptions {
67*7836SJohn.Forte@Sun.COM 	int optval;
68*7836SJohn.Forte@Sun.COM 	char optarg[MAXOPTARGLEN + 1];
69*7836SJohn.Forte@Sun.COM } cmdOptions_t;
70*7836SJohn.Forte@Sun.COM 
71*7836SJohn.Forte@Sun.COM 
72*7836SJohn.Forte@Sun.COM /*
73*7836SJohn.Forte@Sun.COM  * list of objects, subcommands, valid short options, required flag and
74*7836SJohn.Forte@Sun.COM  * exlusive option string
75*7836SJohn.Forte@Sun.COM  *
76*7836SJohn.Forte@Sun.COM  * objectValue -> object
77*7836SJohn.Forte@Sun.COM  * subcommandValue -> subcommand value
78*7836SJohn.Forte@Sun.COM  * optionProp.optionString -> short options that are valid
79*7836SJohn.Forte@Sun.COM  * optionProp.required -> flag indicating whether at least one option is
80*7836SJohn.Forte@Sun.COM  *                        required
81*7836SJohn.Forte@Sun.COM  * optionProp.exclusive -> short options that are required to be exclusively
82*7836SJohn.Forte@Sun.COM  *                         entered
83*7836SJohn.Forte@Sun.COM  *
84*7836SJohn.Forte@Sun.COM  *
85*7836SJohn.Forte@Sun.COM  * If it's not here, there are no options for that object.
86*7836SJohn.Forte@Sun.COM  *
87*7836SJohn.Forte@Sun.COM  * The long options table specifies whether an option argument is required.
88*7836SJohn.Forte@Sun.COM  *
89*7836SJohn.Forte@Sun.COM  *
90*7836SJohn.Forte@Sun.COM  * EXAMPLE:
91*7836SJohn.Forte@Sun.COM  *
92*7836SJohn.Forte@Sun.COM  * Based on DISCOVERY entry below:
93*7836SJohn.Forte@Sun.COM  *
94*7836SJohn.Forte@Sun.COM  *  MODIFY DISCOVERY accepts -i, -s, -t and -l
95*7836SJohn.Forte@Sun.COM  *  MODIFY DISCOVERY requires at least one option
96*7836SJohn.Forte@Sun.COM  *  MODIFY DISCOVERY has no exclusive options
97*7836SJohn.Forte@Sun.COM  *
98*7836SJohn.Forte@Sun.COM  *
99*7836SJohn.Forte@Sun.COM  *	optionRules_t optionRules[] = {
100*7836SJohn.Forte@Sun.COM  *	    {DISCOVERY, MODIFY, "istl", B_TRUE, NULL},
101*7836SJohn.Forte@Sun.COM  *	    {0, 0, NULL, 0, NULL}
102*7836SJohn.Forte@Sun.COM  *	};
103*7836SJohn.Forte@Sun.COM  */
104*7836SJohn.Forte@Sun.COM typedef struct _optionProp {
105*7836SJohn.Forte@Sun.COM 	char *optionString;
106*7836SJohn.Forte@Sun.COM 	boolean_t required;
107*7836SJohn.Forte@Sun.COM 	char *exclusive;
108*7836SJohn.Forte@Sun.COM } optionProp_t;
109*7836SJohn.Forte@Sun.COM 
110*7836SJohn.Forte@Sun.COM typedef struct _optionRules {
111*7836SJohn.Forte@Sun.COM 	uint_t objectValue;
112*7836SJohn.Forte@Sun.COM 	uint_t subcommandValue;
113*7836SJohn.Forte@Sun.COM 	optionProp_t	optionProp;
114*7836SJohn.Forte@Sun.COM } optionRules_t;
115*7836SJohn.Forte@Sun.COM 
116*7836SJohn.Forte@Sun.COM /*
117*7836SJohn.Forte@Sun.COM  * Rules for subcommands and object operands
118*7836SJohn.Forte@Sun.COM  *
119*7836SJohn.Forte@Sun.COM  * Every object requires an entry
120*7836SJohn.Forte@Sun.COM  *
121*7836SJohn.Forte@Sun.COM  * value, reqOpCmd, optOpCmd, noOpCmd, invCmd, multOpCmd
122*7836SJohn.Forte@Sun.COM  *
123*7836SJohn.Forte@Sun.COM  * value -> numeric value of object
124*7836SJohn.Forte@Sun.COM  *
125*7836SJohn.Forte@Sun.COM  * The following five fields are comprised of values that are
126*7836SJohn.Forte@Sun.COM  * a bitwise OR of the subcommands related to the object
127*7836SJohn.Forte@Sun.COM  *
128*7836SJohn.Forte@Sun.COM  * reqOpCmd -> subcommands that must have an operand
129*7836SJohn.Forte@Sun.COM  * optOpCmd -> subcommands that may have an operand
130*7836SJohn.Forte@Sun.COM  * noOpCmd -> subcommands that will have no operand
131*7836SJohn.Forte@Sun.COM  * invCmd -> subcommands that are invalid
132*7836SJohn.Forte@Sun.COM  * multOpCmd -> subcommands that can accept multiple operands
133*7836SJohn.Forte@Sun.COM  * operandDefinition -> Usage definition for the operand of this object
134*7836SJohn.Forte@Sun.COM  *
135*7836SJohn.Forte@Sun.COM  *
136*7836SJohn.Forte@Sun.COM  * EXAMPLE:
137*7836SJohn.Forte@Sun.COM  *
138*7836SJohn.Forte@Sun.COM  * based on TARGET entry below:
139*7836SJohn.Forte@Sun.COM  *  MODIFY and DELETE subcomamnds require an operand
140*7836SJohn.Forte@Sun.COM  *  LIST optionally requires an operand
141*7836SJohn.Forte@Sun.COM  *  There are no subcommands that requires that no operand is specified
142*7836SJohn.Forte@Sun.COM  *  ADD and REMOVE are invalid subcommands for this operand
143*7836SJohn.Forte@Sun.COM  *  DELETE can accept multiple operands
144*7836SJohn.Forte@Sun.COM  *
145*7836SJohn.Forte@Sun.COM  *	objectRules_t objectRules[] = {
146*7836SJohn.Forte@Sun.COM  *	    {TARGET, MODIFY|DELETE, LIST, 0, ADD|REMOVE, DELETE,
147*7836SJohn.Forte@Sun.COM  *	    "target-name"},
148*7836SJohn.Forte@Sun.COM  *	    {0, 0, 0, 0, 0, NULL}
149*7836SJohn.Forte@Sun.COM  *	};
150*7836SJohn.Forte@Sun.COM  */
151*7836SJohn.Forte@Sun.COM typedef struct _opCmd {
152*7836SJohn.Forte@Sun.COM 	uint_t reqOpCmd;
153*7836SJohn.Forte@Sun.COM 	uint_t optOpCmd;
154*7836SJohn.Forte@Sun.COM 	uint_t noOpCmd;
155*7836SJohn.Forte@Sun.COM 	uint_t invOpCmd;
156*7836SJohn.Forte@Sun.COM 	uint_t multOpCmd;
157*7836SJohn.Forte@Sun.COM } opCmd_t;
158*7836SJohn.Forte@Sun.COM 
159*7836SJohn.Forte@Sun.COM typedef struct _objectRules {
160*7836SJohn.Forte@Sun.COM 	uint_t value;
161*7836SJohn.Forte@Sun.COM 	opCmd_t opCmd;
162*7836SJohn.Forte@Sun.COM 	char *operandDefinition;
163*7836SJohn.Forte@Sun.COM } objectRules_t;
164*7836SJohn.Forte@Sun.COM 
165*7836SJohn.Forte@Sun.COM 
166*7836SJohn.Forte@Sun.COM /*
167*7836SJohn.Forte@Sun.COM  * subcommand callback function
168*7836SJohn.Forte@Sun.COM  *
169*7836SJohn.Forte@Sun.COM  * argc - number of arguments in argv
170*7836SJohn.Forte@Sun.COM  * argv - operand arguments
171*7836SJohn.Forte@Sun.COM  * options - options entered on command line
172*7836SJohn.Forte@Sun.COM  * callData - pointer to caller data to be passed to subcommand function
173*7836SJohn.Forte@Sun.COM  */
174*7836SJohn.Forte@Sun.COM typedef int (*handler_t)(int argc, char *argv[], int, cmdOptions_t *options,
175*7836SJohn.Forte@Sun.COM     void *callData, int *funtRet);
176*7836SJohn.Forte@Sun.COM 
177*7836SJohn.Forte@Sun.COM /*
178*7836SJohn.Forte@Sun.COM  * Add new subcommands here
179*7836SJohn.Forte@Sun.COM  *
180*7836SJohn.Forte@Sun.COM  * EXAMPLE:
181*7836SJohn.Forte@Sun.COM  *	subcommand_t subcommands[] = {
182*7836SJohn.Forte@Sun.COM  *	    {"add", ADD, addFunc},
183*7836SJohn.Forte@Sun.COM  *	    {NULL, 0, NULL}
184*7836SJohn.Forte@Sun.COM  *	};
185*7836SJohn.Forte@Sun.COM  */
186*7836SJohn.Forte@Sun.COM typedef struct _subcommand {
187*7836SJohn.Forte@Sun.COM 	char *name;
188*7836SJohn.Forte@Sun.COM 	uint_t value;
189*7836SJohn.Forte@Sun.COM 	handler_t handler;
190*7836SJohn.Forte@Sun.COM } subcommand_t;
191*7836SJohn.Forte@Sun.COM 
192*7836SJohn.Forte@Sun.COM #define	required_arg	required_argument
193*7836SJohn.Forte@Sun.COM #define	no_arg		no_argument
194*7836SJohn.Forte@Sun.COM 
195*7836SJohn.Forte@Sun.COM /*
196*7836SJohn.Forte@Sun.COM  * Add short options and long options here
197*7836SJohn.Forte@Sun.COM  *
198*7836SJohn.Forte@Sun.COM  *  name -> long option name
199*7836SJohn.Forte@Sun.COM  *  has_arg -> required_arg, no_arg
200*7836SJohn.Forte@Sun.COM  *  val -> short option character
201*7836SJohn.Forte@Sun.COM  *  argDesc -> description of option argument
202*7836SJohn.Forte@Sun.COM  *
203*7836SJohn.Forte@Sun.COM  * Note: This structure may not be used if your CLI has no
204*7836SJohn.Forte@Sun.COM  * options. However, -?, --help and -V, --version will still be supported
205*7836SJohn.Forte@Sun.COM  * as they are standard for every CLI.
206*7836SJohn.Forte@Sun.COM  *
207*7836SJohn.Forte@Sun.COM  * EXAMPLE:
208*7836SJohn.Forte@Sun.COM  *
209*7836SJohn.Forte@Sun.COM  *	optionTbl_t options[] = {
210*7836SJohn.Forte@Sun.COM  *	    {"filename", arg_required, 'f', "out-filename"},
211*7836SJohn.Forte@Sun.COM  *	    {NULL, 0, 0}
212*7836SJohn.Forte@Sun.COM  *	};
213*7836SJohn.Forte@Sun.COM  *
214*7836SJohn.Forte@Sun.COM  */
215*7836SJohn.Forte@Sun.COM typedef struct _optionTbl {
216*7836SJohn.Forte@Sun.COM 	char *name;
217*7836SJohn.Forte@Sun.COM 	int has_arg;
218*7836SJohn.Forte@Sun.COM 	int val;
219*7836SJohn.Forte@Sun.COM 	char *argDesc;
220*7836SJohn.Forte@Sun.COM } optionTbl_t;
221*7836SJohn.Forte@Sun.COM 
222*7836SJohn.Forte@Sun.COM /*
223*7836SJohn.Forte@Sun.COM  * After tables are set, assign them to this structure
224*7836SJohn.Forte@Sun.COM  * for passing into cmdparse()
225*7836SJohn.Forte@Sun.COM  */
226*7836SJohn.Forte@Sun.COM typedef struct _synTables {
227*7836SJohn.Forte@Sun.COM 	char *versionString;
228*7836SJohn.Forte@Sun.COM 	optionTbl_t *longOptionTbl;
229*7836SJohn.Forte@Sun.COM 	subcommand_t *subcommandTbl;
230*7836SJohn.Forte@Sun.COM 	object_t *objectTbl;
231*7836SJohn.Forte@Sun.COM 	objectRules_t *objectRulesTbl;
232*7836SJohn.Forte@Sun.COM 	optionRules_t *optionRulesTbl;
233*7836SJohn.Forte@Sun.COM } synTables_t;
234*7836SJohn.Forte@Sun.COM 
235*7836SJohn.Forte@Sun.COM /*
236*7836SJohn.Forte@Sun.COM  * cmdParse is a parser that checks syntax of the input command against
237*7836SJohn.Forte@Sun.COM  * various rules tables.
238*7836SJohn.Forte@Sun.COM  *
239*7836SJohn.Forte@Sun.COM  * When syntax is successfully validated, the function associated with the
240*7836SJohn.Forte@Sun.COM  * subcommand is called using the subcommands table functions.
241*7836SJohn.Forte@Sun.COM  *
242*7836SJohn.Forte@Sun.COM  * Syntax for the command is as follows:
243*7836SJohn.Forte@Sun.COM  *
244*7836SJohn.Forte@Sun.COM  *	command subcommand [<options>] object [<operand ...>]
245*7836SJohn.Forte@Sun.COM  *
246*7836SJohn.Forte@Sun.COM  *
247*7836SJohn.Forte@Sun.COM  * There are two standard short and long options assumed:
248*7836SJohn.Forte@Sun.COM  *	-?, --help	Provides usage on a command or subcommand
249*7836SJohn.Forte@Sun.COM  *			and stops further processing of the arguments
250*7836SJohn.Forte@Sun.COM  *
251*7836SJohn.Forte@Sun.COM  *	-V, --version	Provides version information on the command
252*7836SJohn.Forte@Sun.COM  *			and stops further processing of the arguments
253*7836SJohn.Forte@Sun.COM  *
254*7836SJohn.Forte@Sun.COM  *	These options are loaded by this function.
255*7836SJohn.Forte@Sun.COM  *
256*7836SJohn.Forte@Sun.COM  * input:
257*7836SJohn.Forte@Sun.COM  *  argc, argv from main
258*7836SJohn.Forte@Sun.COM  *  syntax rules tables (synTables_t structure)
259*7836SJohn.Forte@Sun.COM  *  callArgs - void * passed by caller to be passed to subcommand function
260*7836SJohn.Forte@Sun.COM  *
261*7836SJohn.Forte@Sun.COM  * output:
262*7836SJohn.Forte@Sun.COM  *  funcRet - pointer to int that holds subcommand function return value
263*7836SJohn.Forte@Sun.COM  *
264*7836SJohn.Forte@Sun.COM  * Returns:
265*7836SJohn.Forte@Sun.COM  *
266*7836SJohn.Forte@Sun.COM  *     zero on successful syntax parse and function call
267*7836SJohn.Forte@Sun.COM  *
268*7836SJohn.Forte@Sun.COM  *     1 on unsuccessful syntax parse (no function has been called)
269*7836SJohn.Forte@Sun.COM  *		This could be due to a version or help call or simply a
270*7836SJohn.Forte@Sun.COM  *		general usage call.
271*7836SJohn.Forte@Sun.COM  *
272*7836SJohn.Forte@Sun.COM  *     -1 check errno, call failed
273*7836SJohn.Forte@Sun.COM  *
274*7836SJohn.Forte@Sun.COM  */
275*7836SJohn.Forte@Sun.COM int cmdParse(int numOperands, char *operands[], synTables_t synTables,
276*7836SJohn.Forte@Sun.COM     void *callerArgs, int *funcRet);
277*7836SJohn.Forte@Sun.COM 
278*7836SJohn.Forte@Sun.COM #ifdef	__cplusplus
279*7836SJohn.Forte@Sun.COM }
280*7836SJohn.Forte@Sun.COM #endif
281*7836SJohn.Forte@Sun.COM 
282*7836SJohn.Forte@Sun.COM #endif	/* _CMDPARSE_H */
283