8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / mpathadm / cmdparse.h
blob7b7bb640caf8efad5b94a97dcee1f711e7a79174
1 /*
2 * CDDL HEADER START
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]
19 * CDDL HEADER END
22 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
23 * Use is subject to license terms.
26 #ifndef _CMDPARSE_H
27 #define _CMDPARSE_H
29 #ifdef __cplusplus
30 extern "C" {
31 #endif
33 #include <getopt.h>
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)
39 #define OBJECT_BASE 1
40 #define OBJECT(x) (OBJECT_BASE << x)
42 /* maximum length of an option argument */
43 #define MAXOPTARGLEN 256
47 * Add objects here
49 * EXAMPLE:
50 * object_t object[] = {
51 * {"target", TARGET},
52 * {NULL, 0}
53 * };
55 typedef struct _object {
56 char *name;
57 uint_t value;
58 } object_t;
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 {
66 int optval;
67 char optarg[MAXOPTARGLEN + 1];
68 } cmdOptions_t;
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
79 * required
80 * optionProp.exclusive -> short options that are required to be exclusively
81 * entered
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.
89 * EXAMPLE:
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}
101 * };
103 typedef struct _optionProp {
104 char *optionString;
105 boolean_t required;
106 char *exclusive;
107 } optionProp_t;
109 typedef struct _optionRules {
110 uint_t objectValue;
111 uint_t subcommandValue;
112 optionProp_t optionProp;
113 } optionRules_t;
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
135 * EXAMPLE:
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,
146 * "target-name"},
147 * {0, 0, 0, 0, 0, NULL}
148 * };
150 typedef struct _opCmd {
151 uint_t reqOpCmd;
152 uint_t optOpCmd;
153 uint_t noOpCmd;
154 uint_t invOpCmd;
155 uint_t multOpCmd;
156 } opCmd_t;
158 typedef struct _objectRules {
159 uint_t value;
160 opCmd_t opCmd;
161 char *operandDefinition;
162 } objectRules_t;
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,
174 void *callData);
177 * Add new subcommands here
179 * EXAMPLE:
180 * subcommand_t subcommands[] = {
181 * {"add", ADD, addFunc},
182 * {NULL, 0, NULL}
183 * };
185 typedef struct _subcommand {
186 char *name;
187 uint_t value;
188 handler_t handler;
189 } subcommand_t;
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.
206 * EXAMPLE:
208 * optionTbl_t options[] = {
209 * {"filename", arg_required, 'f', "out-filename"},
210 * {NULL, 0, 0}
211 * };
214 typedef struct _optionTbl {
215 char *name;
216 int has_arg;
217 int val;
218 char *argDesc;
219 } optionTbl_t;
222 * After tables are set, assign them to this structure
223 * for passing into cmdparse()
225 typedef struct _synTables {
226 char *versionString;
227 optionTbl_t *longOptionTbl;
228 subcommand_t *subcommandTbl;
229 object_t *objectTbl;
230 objectRules_t *objectRulesTbl;
231 optionRules_t *optionRulesTbl;
232 } synTables_t;
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.
255 * input:
256 * argc, argv from main
257 * syntax rules tables (synTables_t structure)
258 * callArgs - void * passed by caller to be passed to subcommand function
260 * output:
261 * funcRet - pointer to int that holds subcommand function return value
263 * Returns:
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);
277 #ifdef __cplusplus
279 #endif
281 #endif /* _CMDPARSE_H */