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