4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
35 /* subcommands must have a single bit on and must have exclusive values */
36 #define SUBCOMMAND_BASE 1
37 #define SUBCOMMAND(x) (SUBCOMMAND_BASE << x)
40 #define OBJECT(x) (OBJECT_BASE << x)
42 /* maximum length of an option argument */
43 #define MAXOPTARGLEN 256
50 * object_t object[] = {
55 typedef struct _object
{
61 * This structure is passed into the caller's callback function and
62 * will contain a list of all options entered and their associated
63 * option arguments if applicable
65 typedef struct _cmdOptions
{
67 char optarg
[MAXOPTARGLEN
+ 1];
72 * list of objects, subcommands, valid short options, required flag and
73 * exlusive option string
75 * objectValue -> object
76 * subcommandValue -> subcommand value
77 * optionProp.optionString -> short options that are valid
78 * optionProp.required -> flag indicating whether at least one option is
80 * optionProp.exclusive -> short options that are required to be exclusively
84 * If it's not here, there are no options for that object.
86 * The long options table specifies whether an option argument is required.
91 * Based on DISCOVERY entry below:
93 * MODIFY DISCOVERY accepts -i, -s, -t and -l
94 * MODIFY DISCOVERY requires at least one option
95 * MODIFY DISCOVERY has no exclusive options
98 * optionRules_t optionRules[] = {
99 * {DISCOVERY, MODIFY, "istl", B_TRUE, NULL},
100 * {0, 0, NULL, 0, NULL}
103 typedef struct _optionProp
{
109 typedef struct _optionRules
{
111 uint_t subcommandValue
;
112 optionProp_t optionProp
;
116 * Rules for subcommands and object operands
118 * Every object requires an entry
120 * value, reqOpCmd, optOpCmd, noOpCmd, invCmd, multOpCmd
122 * value -> numeric value of object
124 * The following five fields are comprised of values that are
125 * a bitwise OR of the subcommands related to the object
127 * reqOpCmd -> subcommands that must have an operand
128 * optOpCmd -> subcommands that may have an operand
129 * noOpCmd -> subcommands that will have no operand
130 * invCmd -> subcommands that are invalid
131 * multOpCmd -> subcommands that can accept multiple operands
132 * operandDefinition -> Usage definition for the operand of this object
137 * based on TARGET entry below:
138 * MODIFY and DELETE subcomamnds require an operand
139 * LIST optionally requires an operand
140 * There are no subcommands that requires that no operand is specified
141 * ADD and REMOVE are invalid subcommands for this operand
142 * DELETE can accept multiple operands
144 * objectRules_t objectRules[] = {
145 * {TARGET, MODIFY|DELETE, LIST, 0, ADD|REMOVE, DELETE,
147 * {0, 0, 0, 0, 0, NULL}
150 typedef struct _opCmd
{
158 typedef struct _objectRules
{
161 char *operandDefinition
;
166 * subcommand callback function
168 * argc - number of arguments in argv
169 * argv - operand arguments
170 * options - options entered on command line
171 * callData - pointer to caller data to be passed to subcommand function
173 typedef int (*handler_t
)(int argc
, char *argv
[], int, cmdOptions_t
*options
,
177 * Add new subcommands here
180 * subcommand_t subcommands[] = {
181 * {"add", ADD, addFunc},
185 typedef struct _subcommand
{
191 #define required_arg required_argument
192 #define no_arg no_argument
195 * Add short options and long options here
197 * name -> long option name
198 * has_arg -> required_arg, no_arg
199 * val -> short option character
200 * argDesc -> description of option argument
202 * Note: This structure may not be used if your CLI has no
203 * options. However, -?, --help and -V, --version will still be supported
204 * as they are standard for every CLI.
208 * optionTbl_t options[] = {
209 * {"filename", arg_required, 'f', "out-filename"},
214 typedef struct _optionTbl
{
222 * After tables are set, assign them to this structure
223 * for passing into cmdparse()
225 typedef struct _synTables
{
227 optionTbl_t
*longOptionTbl
;
228 subcommand_t
*subcommandTbl
;
230 objectRules_t
*objectRulesTbl
;
231 optionRules_t
*optionRulesTbl
;
235 * cmdParse is a parser that checks syntax of the input command against
236 * various rules tables.
238 * When syntax is successfully validated, the function associated with the
239 * subcommand is called using the subcommands table functions.
241 * Syntax for the command is as follows:
243 * command subcommand [<options>] object [<operand ...>]
246 * There are two standard short and long options assumed:
247 * -?, --help Provides usage on a command or subcommand
248 * and stops further processing of the arguments
250 * -V, --version Provides version information on the command
251 * and stops further processing of the arguments
253 * These options are loaded by this function.
256 * argc, argv from main
257 * syntax rules tables (synTables_t structure)
258 * callArgs - void * passed by caller to be passed to subcommand function
261 * funcRet - pointer to int that holds subcommand function return value
265 * zero on successful syntax parse and function call
267 * 1 on unsuccessful syntax parse (no function has been called)
268 * This could be due to a version or help call or simply a
269 * general usage call.
271 * -1 check errno, call failed
274 int cmdParse(int numOperands
, char *operands
[], synTables_t synTables
,
275 void *callerArgs
, int *funcRet
);
281 #endif /* _CMDPARSE_H */