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