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 #define SUBCOMMAND_BASE 1 36*7836SJohn.Forte@Sun.COM 37*7836SJohn.Forte@Sun.COM /* bit defines for operand macros */ 38*7836SJohn.Forte@Sun.COM #define OPERAND_SINGLE 0x2 39*7836SJohn.Forte@Sun.COM #define OPERAND_MULTIPLE 0x4 40*7836SJohn.Forte@Sun.COM #define OPERAND_MANDATORY 0x8 41*7836SJohn.Forte@Sun.COM #define OPERAND_OPTIONAL 0x10 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 /* Following are used to express operand requirements */ 48*7836SJohn.Forte@Sun.COM #define OPERAND_NONE 0x1 49*7836SJohn.Forte@Sun.COM #define OPERAND_MANDATORY_SINGLE (OPERAND_MANDATORY | OPERAND_SINGLE) 50*7836SJohn.Forte@Sun.COM #define OPERAND_OPTIONAL_SINGLE (OPERAND_OPTIONAL | OPERAND_SINGLE) 51*7836SJohn.Forte@Sun.COM #define OPERAND_MANDATORY_MULTIPLE (OPERAND_MANDATORY | OPERAND_MULTIPLE) 52*7836SJohn.Forte@Sun.COM #define OPERAND_OPTIONAL_MULTIPLE (OPERAND_OPTIONAL | OPERAND_MULTIPLE) 53*7836SJohn.Forte@Sun.COM 54*7836SJohn.Forte@Sun.COM /* subcommands must have a single bit on and must have exclusive values */ 55*7836SJohn.Forte@Sun.COM #define SUBCOMMAND(x) (SUBCOMMAND_BASE << x) 56*7836SJohn.Forte@Sun.COM 57*7836SJohn.Forte@Sun.COM /* 58*7836SJohn.Forte@Sun.COM * This structure is passed into the caller's callback function and 59*7836SJohn.Forte@Sun.COM * will contain a list of all options entered and their associated 60*7836SJohn.Forte@Sun.COM * option arguments if applicable 61*7836SJohn.Forte@Sun.COM */ 62*7836SJohn.Forte@Sun.COM typedef struct _cmdOptions { 63*7836SJohn.Forte@Sun.COM int optval; 64*7836SJohn.Forte@Sun.COM char optarg[MAXOPTARGLEN + 1]; 65*7836SJohn.Forte@Sun.COM } cmdOptions_t; 66*7836SJohn.Forte@Sun.COM 67*7836SJohn.Forte@Sun.COM /* 68*7836SJohn.Forte@Sun.COM * subcommand callback function 69*7836SJohn.Forte@Sun.COM * 70*7836SJohn.Forte@Sun.COM * argc - number of arguments in argv 71*7836SJohn.Forte@Sun.COM * argv - operand arguments 72*7836SJohn.Forte@Sun.COM * options - options entered on command line 73*7836SJohn.Forte@Sun.COM * callData - pointer to caller data to be passed to subcommand function 74*7836SJohn.Forte@Sun.COM */ 75*7836SJohn.Forte@Sun.COM typedef int (*handler_t)(int argc, char *argv[], cmdOptions_t *options, 76*7836SJohn.Forte@Sun.COM void *callData); 77*7836SJohn.Forte@Sun.COM 78*7836SJohn.Forte@Sun.COM /* 79*7836SJohn.Forte@Sun.COM * list of subcommands and associated properties 80*7836SJohn.Forte@Sun.COM * 81*7836SJohn.Forte@Sun.COM * name -> subcommand name 82*7836SJohn.Forte@Sun.COM * handler -> function to call on successful syntax check 83*7836SJohn.Forte@Sun.COM * optionString -> short options that are valid 84*7836SJohn.Forte@Sun.COM * required -> Does it require at least one option? 85*7836SJohn.Forte@Sun.COM * exclusive -> short options that are required to be exclusively entered 86*7836SJohn.Forte@Sun.COM * operand -> Type of operand input. Can be: 87*7836SJohn.Forte@Sun.COM * 88*7836SJohn.Forte@Sun.COM * NO_OPERAND 89*7836SJohn.Forte@Sun.COM * OPERAND_MANDATORY_SINGLE 90*7836SJohn.Forte@Sun.COM * OPERAND_OPTIONAL_SINGLE 91*7836SJohn.Forte@Sun.COM * OPERAND_MANDATORY_MULTIPLE 92*7836SJohn.Forte@Sun.COM * OPERAND_OPTIONAL_MULTIPLE 93*7836SJohn.Forte@Sun.COM * 94*7836SJohn.Forte@Sun.COM * operandDefinition -> char * definition of the operand 95*7836SJohn.Forte@Sun.COM * 96*7836SJohn.Forte@Sun.COM * The long options table specifies whether an option argument is required. 97*7836SJohn.Forte@Sun.COM * 98*7836SJohn.Forte@Sun.COM * 99*7836SJohn.Forte@Sun.COM * EXAMPLE: 100*7836SJohn.Forte@Sun.COM * 101*7836SJohn.Forte@Sun.COM * Based on "list-target" entry below: 102*7836SJohn.Forte@Sun.COM * 103*7836SJohn.Forte@Sun.COM * "list-target" is expected as the subcommand input 104*7836SJohn.Forte@Sun.COM * listTarget is the function to be called on success 105*7836SJohn.Forte@Sun.COM * "list-target" accepts -i, -s, -t and -l 106*7836SJohn.Forte@Sun.COM * "list-target" requires the option 'i'. 107*7836SJohn.Forte@Sun.COM * "list-target" has no exclusive options 108*7836SJohn.Forte@Sun.COM * "list-target" may have one or more operands 109*7836SJohn.Forte@Sun.COM * "list-target" operand description is "target-name" 110*7836SJohn.Forte@Sun.COM * 111*7836SJohn.Forte@Sun.COM * 112*7836SJohn.Forte@Sun.COM * optionRules_t optionRules[] = { 113*7836SJohn.Forte@Sun.COM * {"list-target", listTarget, "istl", "i", NULL, 114*7836SJohn.Forte@Sun.COM * OPERAND_OPTIONAL_MULTIPLE, "target-name"}, 115*7836SJohn.Forte@Sun.COM * {"modify-target", modifyTarget, "t", "t", NULL, 116*7836SJohn.Forte@Sun.COM * OPERAND_MANDATORY_MULTIPLE, "target-name"}, 117*7836SJohn.Forte@Sun.COM * {"enable", enable, NULL, NULL, NULL, NO_OPERAND, NULL}, 118*7836SJohn.Forte@Sun.COM * {NULL, 0, 0, NULL, 0, NULL} 119*7836SJohn.Forte@Sun.COM * }; 120*7836SJohn.Forte@Sun.COM */ 121*7836SJohn.Forte@Sun.COM typedef struct _subCommandProps { 122*7836SJohn.Forte@Sun.COM char *name; 123*7836SJohn.Forte@Sun.COM handler_t handler; 124*7836SJohn.Forte@Sun.COM char *optionString; 125*7836SJohn.Forte@Sun.COM char *required; 126*7836SJohn.Forte@Sun.COM char *exclusive; 127*7836SJohn.Forte@Sun.COM int operand; 128*7836SJohn.Forte@Sun.COM char *operandDefinition; 129*7836SJohn.Forte@Sun.COM uint8_t reserved[64]; 130*7836SJohn.Forte@Sun.COM } subCommandProps_t; 131*7836SJohn.Forte@Sun.COM 132*7836SJohn.Forte@Sun.COM 133*7836SJohn.Forte@Sun.COM 134*7836SJohn.Forte@Sun.COM #define required_arg required_argument 135*7836SJohn.Forte@Sun.COM #define no_arg no_argument 136*7836SJohn.Forte@Sun.COM 137*7836SJohn.Forte@Sun.COM /* 138*7836SJohn.Forte@Sun.COM * Add short options and long options here 139*7836SJohn.Forte@Sun.COM * 140*7836SJohn.Forte@Sun.COM * name -> long option name 141*7836SJohn.Forte@Sun.COM * has_arg -> required_arg, no_arg 142*7836SJohn.Forte@Sun.COM * val -> short option character 143*7836SJohn.Forte@Sun.COM * argDesc -> description of option argument 144*7836SJohn.Forte@Sun.COM * 145*7836SJohn.Forte@Sun.COM * Note: This structure may not be used if your CLI has no 146*7836SJohn.Forte@Sun.COM * options. However, -?, --help and -V, --version will still be supported 147*7836SJohn.Forte@Sun.COM * as they are standard for every CLI. 148*7836SJohn.Forte@Sun.COM * 149*7836SJohn.Forte@Sun.COM * EXAMPLE: 150*7836SJohn.Forte@Sun.COM * 151*7836SJohn.Forte@Sun.COM * optionTbl_t options[] = { 152*7836SJohn.Forte@Sun.COM * {"filename", arg_required, 'f', "out-filename"}, 153*7836SJohn.Forte@Sun.COM * {NULL, 0, 0} 154*7836SJohn.Forte@Sun.COM * }; 155*7836SJohn.Forte@Sun.COM * 156*7836SJohn.Forte@Sun.COM */ 157*7836SJohn.Forte@Sun.COM typedef struct _optionTbl { 158*7836SJohn.Forte@Sun.COM char *name; 159*7836SJohn.Forte@Sun.COM int has_arg; 160*7836SJohn.Forte@Sun.COM int val; 161*7836SJohn.Forte@Sun.COM char *argDesc; 162*7836SJohn.Forte@Sun.COM } optionTbl_t; 163*7836SJohn.Forte@Sun.COM 164*7836SJohn.Forte@Sun.COM /* 165*7836SJohn.Forte@Sun.COM * After tables are set, assign them to this structure 166*7836SJohn.Forte@Sun.COM * for passing into cmdparse() 167*7836SJohn.Forte@Sun.COM */ 168*7836SJohn.Forte@Sun.COM typedef struct _synTables { 169*7836SJohn.Forte@Sun.COM char *versionString; 170*7836SJohn.Forte@Sun.COM optionTbl_t *longOptionTbl; 171*7836SJohn.Forte@Sun.COM subCommandProps_t *subCommandPropsTbl; 172*7836SJohn.Forte@Sun.COM } synTables_t; 173*7836SJohn.Forte@Sun.COM 174*7836SJohn.Forte@Sun.COM /* 175*7836SJohn.Forte@Sun.COM * cmdParse is a parser that checks syntax of the input command against 176*7836SJohn.Forte@Sun.COM * rules and property tables. 177*7836SJohn.Forte@Sun.COM * 178*7836SJohn.Forte@Sun.COM * When syntax is successfully validated, the function associated with the 179*7836SJohn.Forte@Sun.COM * subcommand is called using the subcommands table functions. 180*7836SJohn.Forte@Sun.COM * 181*7836SJohn.Forte@Sun.COM * Syntax for the command is as follows: 182*7836SJohn.Forte@Sun.COM * 183*7836SJohn.Forte@Sun.COM * command [options] subcommand [<options>] [<operand ...>] 184*7836SJohn.Forte@Sun.COM * 185*7836SJohn.Forte@Sun.COM * 186*7836SJohn.Forte@Sun.COM * There are two standard short and long options assumed: 187*7836SJohn.Forte@Sun.COM * -?, --help Provides usage on a command or subcommand 188*7836SJohn.Forte@Sun.COM * and stops further processing of the arguments 189*7836SJohn.Forte@Sun.COM * 190*7836SJohn.Forte@Sun.COM * -V, --version Provides version information on the command 191*7836SJohn.Forte@Sun.COM * and stops further processing of the arguments 192*7836SJohn.Forte@Sun.COM * 193*7836SJohn.Forte@Sun.COM * These options are loaded by this function. 194*7836SJohn.Forte@Sun.COM * 195*7836SJohn.Forte@Sun.COM * input: 196*7836SJohn.Forte@Sun.COM * argc, argv from main 197*7836SJohn.Forte@Sun.COM * syntax rules tables (synTables_t structure) 198*7836SJohn.Forte@Sun.COM * callArgs - void * passed by caller to be passed to subcommand function 199*7836SJohn.Forte@Sun.COM * 200*7836SJohn.Forte@Sun.COM * output: 201*7836SJohn.Forte@Sun.COM * funcRet - pointer to int that holds subcommand function return value 202*7836SJohn.Forte@Sun.COM * 203*7836SJohn.Forte@Sun.COM * Returns: 204*7836SJohn.Forte@Sun.COM * 205*7836SJohn.Forte@Sun.COM * zero on successful syntax parse and function call 206*7836SJohn.Forte@Sun.COM * 207*7836SJohn.Forte@Sun.COM * 1 on unsuccessful syntax parse (no function has been called) 208*7836SJohn.Forte@Sun.COM * This could be due to a version or help call or simply a 209*7836SJohn.Forte@Sun.COM * general usage call. 210*7836SJohn.Forte@Sun.COM * 211*7836SJohn.Forte@Sun.COM * -1 check errno, call failed 212*7836SJohn.Forte@Sun.COM * 213*7836SJohn.Forte@Sun.COM */ 214*7836SJohn.Forte@Sun.COM int cmdParse(int numOperands, char *operands[], synTables_t synTables, 215*7836SJohn.Forte@Sun.COM void *callerArgs, int *funcRet); 216*7836SJohn.Forte@Sun.COM 217*7836SJohn.Forte@Sun.COM #ifdef __cplusplus 218*7836SJohn.Forte@Sun.COM } 219*7836SJohn.Forte@Sun.COM #endif 220*7836SJohn.Forte@Sun.COM 221*7836SJohn.Forte@Sun.COM #endif /* _CMDPARSE_H */ 222