xref: /illumos-gate/usr/src/cmd/isns/isnsadm/cmdparse.h (revision fcf3ce44)
1*fcf3ce44SJohn Forte /*
2*fcf3ce44SJohn Forte  * CDDL HEADER START
3*fcf3ce44SJohn Forte  *
4*fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5*fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6*fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7*fcf3ce44SJohn Forte  *
8*fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10*fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11*fcf3ce44SJohn Forte  * and limitations under the License.
12*fcf3ce44SJohn Forte  *
13*fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14*fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16*fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17*fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18*fcf3ce44SJohn Forte  *
19*fcf3ce44SJohn Forte  * CDDL HEADER END
20*fcf3ce44SJohn Forte  */
21*fcf3ce44SJohn Forte /*
22*fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*fcf3ce44SJohn Forte  * Use is subject to license terms.
24*fcf3ce44SJohn Forte  */
25*fcf3ce44SJohn Forte 
26*fcf3ce44SJohn Forte #ifndef	_CMDPARSE_H
27*fcf3ce44SJohn Forte #define	_CMDPARSE_H
28*fcf3ce44SJohn Forte 
29*fcf3ce44SJohn Forte #ifdef	__cplusplus
30*fcf3ce44SJohn Forte extern "C" {
31*fcf3ce44SJohn Forte #endif
32*fcf3ce44SJohn Forte 
33*fcf3ce44SJohn Forte #include <getopt.h>
34*fcf3ce44SJohn Forte 
35*fcf3ce44SJohn Forte #define	SUBCOMMAND_BASE  1
36*fcf3ce44SJohn Forte 
37*fcf3ce44SJohn Forte /* bit defines for operand macros */
38*fcf3ce44SJohn Forte #define	OPERAND_SINGLE		0x2
39*fcf3ce44SJohn Forte #define	OPERAND_MULTIPLE	0x4
40*fcf3ce44SJohn Forte #define	OPERAND_MANDATORY	0x8
41*fcf3ce44SJohn Forte #define	OPERAND_OPTIONAL	0x10
42*fcf3ce44SJohn Forte 
43*fcf3ce44SJohn Forte /* maximum length of an option argument */
44*fcf3ce44SJohn Forte #define	MAXOPTARGLEN   256
45*fcf3ce44SJohn Forte 
46*fcf3ce44SJohn Forte 
47*fcf3ce44SJohn Forte /* Following are used to express operand requirements */
48*fcf3ce44SJohn Forte #define	OPERAND_NONE		    0x1
49*fcf3ce44SJohn Forte #define	OPERAND_MANDATORY_SINGLE    (OPERAND_MANDATORY | OPERAND_SINGLE)
50*fcf3ce44SJohn Forte #define	OPERAND_OPTIONAL_SINGLE	    (OPERAND_OPTIONAL | OPERAND_SINGLE)
51*fcf3ce44SJohn Forte #define	OPERAND_MANDATORY_MULTIPLE  (OPERAND_MANDATORY | OPERAND_MULTIPLE)
52*fcf3ce44SJohn Forte #define	OPERAND_OPTIONAL_MULTIPLE   (OPERAND_OPTIONAL | OPERAND_MULTIPLE)
53*fcf3ce44SJohn Forte 
54*fcf3ce44SJohn Forte /* subcommands must have a single bit on and must have exclusive values */
55*fcf3ce44SJohn Forte #define	SUBCOMMAND(x)  (SUBCOMMAND_BASE << x)
56*fcf3ce44SJohn Forte 
57*fcf3ce44SJohn Forte /*
58*fcf3ce44SJohn Forte  * This structure is passed into the caller's callback function and
59*fcf3ce44SJohn Forte  * will contain a list of all options entered and their associated
60*fcf3ce44SJohn Forte  * option arguments if applicable
61*fcf3ce44SJohn Forte  */
62*fcf3ce44SJohn Forte typedef struct _cmdOptions {
63*fcf3ce44SJohn Forte 	int optval;
64*fcf3ce44SJohn Forte 	char optarg[MAXOPTARGLEN + 1];
65*fcf3ce44SJohn Forte } cmdOptions_t;
66*fcf3ce44SJohn Forte 
67*fcf3ce44SJohn Forte /*
68*fcf3ce44SJohn Forte  * subcommand callback function
69*fcf3ce44SJohn Forte  *
70*fcf3ce44SJohn Forte  * argc - number of arguments in argv
71*fcf3ce44SJohn Forte  * argv - operand arguments
72*fcf3ce44SJohn Forte  * options - options entered on command line
73*fcf3ce44SJohn Forte  * callData - pointer to caller data to be passed to subcommand function
74*fcf3ce44SJohn Forte  */
75*fcf3ce44SJohn Forte typedef int (*handler_t)(int argc, char *argv[], cmdOptions_t *options,
76*fcf3ce44SJohn Forte     void *callData);
77*fcf3ce44SJohn Forte 
78*fcf3ce44SJohn Forte /*
79*fcf3ce44SJohn Forte  * list of subcommands and associated properties
80*fcf3ce44SJohn Forte  *
81*fcf3ce44SJohn Forte  * name -> subcommand name
82*fcf3ce44SJohn Forte  * value -> subcommand value
83*fcf3ce44SJohn Forte  * handler -> function to call on successful syntax check
84*fcf3ce44SJohn Forte  * optionString -> short options that are valid
85*fcf3ce44SJohn Forte  * required -> Does it require at least one option?
86*fcf3ce44SJohn Forte  * exclusive -> short options that are required to be exclusively entered
87*fcf3ce44SJohn Forte  * operand -> Type of operand input. Can be:
88*fcf3ce44SJohn Forte  *
89*fcf3ce44SJohn Forte  *		NO_OPERAND
90*fcf3ce44SJohn Forte  *		OPERAND_MANDATORY_SINGLE
91*fcf3ce44SJohn Forte  *		OPERAND_OPTIONAL_SINGLE
92*fcf3ce44SJohn Forte  *		OPERAND_MANDATORY_MULTIPLE
93*fcf3ce44SJohn Forte  *		OPERAND_OPTIONAL_MULTIPLE
94*fcf3ce44SJohn Forte  *
95*fcf3ce44SJohn Forte  * operandDefinition -> char * definition of the operand
96*fcf3ce44SJohn Forte  *
97*fcf3ce44SJohn Forte  * The long options table specifies whether an option argument is required.
98*fcf3ce44SJohn Forte  *
99*fcf3ce44SJohn Forte  *
100*fcf3ce44SJohn Forte  * EXAMPLE:
101*fcf3ce44SJohn Forte  *
102*fcf3ce44SJohn Forte  * Based on "list-target" entry below:
103*fcf3ce44SJohn Forte  *
104*fcf3ce44SJohn Forte  *  "list-target" is expected as the subcommand input
105*fcf3ce44SJohn Forte  *  LIST-TARGET is the subcommand value
106*fcf3ce44SJohn Forte  *  listTarget is the function to be called on success
107*fcf3ce44SJohn Forte  *  LIST_TARGET accepts -i, -s, -t and -l
108*fcf3ce44SJohn Forte  *  LIST_TARGET requires at least one option
109*fcf3ce44SJohn Forte  *  LIST_TARGET has no exclusive options
110*fcf3ce44SJohn Forte  *  LIST_TARGET may have one or more operands
111*fcf3ce44SJohn Forte  *  LIST_TARGET operand description is "target-name"
112*fcf3ce44SJohn Forte  *
113*fcf3ce44SJohn Forte  *
114*fcf3ce44SJohn Forte  *	optionRules_t optionRules[] = {
115*fcf3ce44SJohn Forte  *	    {"list-target", LIST-TARGET, listTarget, "istl", B_TRUE, NULL,
116*fcf3ce44SJohn Forte  *		OPERAND_OPTIONAL_MULTIPLE, "target-name"},
117*fcf3ce44SJohn Forte  *	    {"modify-target", MODIFY-TARGET, modifyTarget, "t", B_TRUE, NULL,
118*fcf3ce44SJohn Forte  *		OPERAND_MANDATORY_MULTIPLE, "target-name"},
119*fcf3ce44SJohn Forte  *	    {"enable", ENABLE, enable, NULL, B_TRUE, NULL, NO_OPERAND, NULL},
120*fcf3ce44SJohn Forte  *	    {NULL, 0, 0, NULL, 0, NULL}
121*fcf3ce44SJohn Forte  *	};
122*fcf3ce44SJohn Forte  */
123*fcf3ce44SJohn Forte typedef struct _subCommandProps {
124*fcf3ce44SJohn Forte 	char *name;
125*fcf3ce44SJohn Forte 	uint_t value;
126*fcf3ce44SJohn Forte 	handler_t handler;
127*fcf3ce44SJohn Forte 	char *optionString;
128*fcf3ce44SJohn Forte 	boolean_t required;
129*fcf3ce44SJohn Forte 	char *exclusive;
130*fcf3ce44SJohn Forte 	int operand;
131*fcf3ce44SJohn Forte 	char *operandDefinition;
132*fcf3ce44SJohn Forte 	uint8_t reserved[64];
133*fcf3ce44SJohn Forte } subCommandProps_t;
134*fcf3ce44SJohn Forte 
135*fcf3ce44SJohn Forte 
136*fcf3ce44SJohn Forte 
137*fcf3ce44SJohn Forte #define	required_arg	required_argument
138*fcf3ce44SJohn Forte #define	no_arg		no_argument
139*fcf3ce44SJohn Forte 
140*fcf3ce44SJohn Forte /*
141*fcf3ce44SJohn Forte  * Add short options and long options here
142*fcf3ce44SJohn Forte  *
143*fcf3ce44SJohn Forte  *  name -> long option name
144*fcf3ce44SJohn Forte  *  has_arg -> required_arg, no_arg
145*fcf3ce44SJohn Forte  *  val -> short option character
146*fcf3ce44SJohn Forte  *  argDesc -> description of option argument
147*fcf3ce44SJohn Forte  *
148*fcf3ce44SJohn Forte  * Note: This structure may not be used if your CLI has no
149*fcf3ce44SJohn Forte  * options. However, -?, --help and -V, --version will still be supported
150*fcf3ce44SJohn Forte  * as they are standard for every CLI.
151*fcf3ce44SJohn Forte  *
152*fcf3ce44SJohn Forte  * EXAMPLE:
153*fcf3ce44SJohn Forte  *
154*fcf3ce44SJohn Forte  *	optionTbl_t options[] = {
155*fcf3ce44SJohn Forte  *	    {"filename", arg_required, 'f', "out-filename"},
156*fcf3ce44SJohn Forte  *	    {NULL, 0, 0}
157*fcf3ce44SJohn Forte  *	};
158*fcf3ce44SJohn Forte  *
159*fcf3ce44SJohn Forte  */
160*fcf3ce44SJohn Forte typedef struct _optionTbl {
161*fcf3ce44SJohn Forte 	char *name;
162*fcf3ce44SJohn Forte 	int has_arg;
163*fcf3ce44SJohn Forte 	int val;
164*fcf3ce44SJohn Forte 	char *argDesc;
165*fcf3ce44SJohn Forte } optionTbl_t;
166*fcf3ce44SJohn Forte 
167*fcf3ce44SJohn Forte /*
168*fcf3ce44SJohn Forte  * After tables are set, assign them to this structure
169*fcf3ce44SJohn Forte  * for passing into cmdparse()
170*fcf3ce44SJohn Forte  */
171*fcf3ce44SJohn Forte typedef struct _synTables {
172*fcf3ce44SJohn Forte 	char *versionString;
173*fcf3ce44SJohn Forte 	optionTbl_t *longOptionTbl;
174*fcf3ce44SJohn Forte 	subCommandProps_t *subCommandPropsTbl;
175*fcf3ce44SJohn Forte } synTables_t;
176*fcf3ce44SJohn Forte 
177*fcf3ce44SJohn Forte /*
178*fcf3ce44SJohn Forte  * cmdParse is a parser that checks syntax of the input command against
179*fcf3ce44SJohn Forte  * rules and property tables.
180*fcf3ce44SJohn Forte  *
181*fcf3ce44SJohn Forte  * When syntax is successfully validated, the function associated with the
182*fcf3ce44SJohn Forte  * subcommand is called using the subcommands table functions.
183*fcf3ce44SJohn Forte  *
184*fcf3ce44SJohn Forte  * Syntax for the command is as follows:
185*fcf3ce44SJohn Forte  *
186*fcf3ce44SJohn Forte  *	command [options] subcommand [<options>] [<operand ...>]
187*fcf3ce44SJohn Forte  *
188*fcf3ce44SJohn Forte  *
189*fcf3ce44SJohn Forte  * There are two standard short and long options assumed:
190*fcf3ce44SJohn Forte  *	-?, --help	Provides usage on a command or subcommand
191*fcf3ce44SJohn Forte  *			and stops further processing of the arguments
192*fcf3ce44SJohn Forte  *
193*fcf3ce44SJohn Forte  *	-V, --version	Provides version information on the command
194*fcf3ce44SJohn Forte  *			and stops further processing of the arguments
195*fcf3ce44SJohn Forte  *
196*fcf3ce44SJohn Forte  *	These options are loaded by this function.
197*fcf3ce44SJohn Forte  *
198*fcf3ce44SJohn Forte  * input:
199*fcf3ce44SJohn Forte  *  argc, argv from main
200*fcf3ce44SJohn Forte  *  syntax rules tables (synTables_t structure)
201*fcf3ce44SJohn Forte  *  callArgs - void * passed by caller to be passed to subcommand function
202*fcf3ce44SJohn Forte  *
203*fcf3ce44SJohn Forte  * output:
204*fcf3ce44SJohn Forte  *  funcRet - pointer to int that holds subcommand function return value
205*fcf3ce44SJohn Forte  *
206*fcf3ce44SJohn Forte  * Returns:
207*fcf3ce44SJohn Forte  *
208*fcf3ce44SJohn Forte  *     zero on successful syntax parse and function call
209*fcf3ce44SJohn Forte  *
210*fcf3ce44SJohn Forte  *     1 on unsuccessful syntax parse (no function has been called)
211*fcf3ce44SJohn Forte  *		This could be due to a version or help call or simply a
212*fcf3ce44SJohn Forte  *		general usage call.
213*fcf3ce44SJohn Forte  *
214*fcf3ce44SJohn Forte  *     -1 check errno, call failed
215*fcf3ce44SJohn Forte  *
216*fcf3ce44SJohn Forte  */
217*fcf3ce44SJohn Forte int cmdParse(int numOperands, char *operands[], synTables_t synTables,
218*fcf3ce44SJohn Forte     void *callerArgs, int *funcRet);
219*fcf3ce44SJohn Forte 
220*fcf3ce44SJohn Forte #ifdef	__cplusplus
221*fcf3ce44SJohn Forte }
222*fcf3ce44SJohn Forte #endif
223*fcf3ce44SJohn Forte 
224*fcf3ce44SJohn Forte #endif	/* _CMDPARSE_H */
225