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