fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License (the "License").
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You may not use this file except in compliance with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
7b506e25917c371db526f76d85b9b1d17c8c5d39srivijitha dugganapalli * Copyright 2010 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifndef _CMDPARSE_H
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define _CMDPARSE_H
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef __cplusplus
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteextern "C" {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <getopt.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define SUBCOMMAND_BASE 1
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* bit defines for operand macros */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define OPERAND_SINGLE 0x2
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define OPERAND_MULTIPLE 0x4
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define OPERAND_MANDATORY 0x8
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define OPERAND_OPTIONAL 0x10
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* maximum length of an option argument */
7b506e25917c371db526f76d85b9b1d17c8c5d39srivijitha dugganapalli#define MAXOPTARGLEN 512
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* Following are used to express operand requirements */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define OPERAND_NONE 0x1
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define OPERAND_MANDATORY_SINGLE (OPERAND_MANDATORY | OPERAND_SINGLE)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define OPERAND_OPTIONAL_SINGLE (OPERAND_OPTIONAL | OPERAND_SINGLE)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define OPERAND_MANDATORY_MULTIPLE (OPERAND_MANDATORY | OPERAND_MULTIPLE)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define OPERAND_OPTIONAL_MULTIPLE (OPERAND_OPTIONAL | OPERAND_MULTIPLE)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* subcommands must have a single bit on and must have exclusive values */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define SUBCOMMAND(x) (SUBCOMMAND_BASE << x)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This structure is passed into the caller's callback function and
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * will contain a list of all options entered and their associated
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * option arguments if applicable
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef struct _cmdOptions {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int optval;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char optarg[MAXOPTARGLEN + 1];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte} cmdOptions_t;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * subcommand callback function
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * argc - number of arguments in argv
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * argv - operand arguments
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * options - options entered on command line
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * callData - pointer to caller data to be passed to subcommand function
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef int (*handler_t)(int argc, char *argv[], cmdOptions_t *options,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *callData);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * list of subcommands and associated properties
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * name -> subcommand name
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * handler -> function to call on successful syntax check
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * optionString -> short options that are valid
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * required -> Does it require at least one option?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * exclusive -> short options that are required to be exclusively entered
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * operand -> Type of operand input. Can be:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * NO_OPERAND
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * OPERAND_MANDATORY_SINGLE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * OPERAND_OPTIONAL_SINGLE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * OPERAND_MANDATORY_MULTIPLE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * OPERAND_OPTIONAL_MULTIPLE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * operandDefinition -> char * definition of the operand
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The long options table specifies whether an option argument is required.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * EXAMPLE:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Based on "list-target" entry below:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * "list-target" is expected as the subcommand input
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * listTarget is the function to be called on success
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * "list-target" accepts -i, -s, -t and -l
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * "list-target" requires the option 'i'.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * "list-target" has no exclusive options
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * "list-target" may have one or more operands
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * "list-target" operand description is "target-name"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * optionRules_t optionRules[] = {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * {"list-target", listTarget, "istl", "i", NULL,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * OPERAND_OPTIONAL_MULTIPLE, "target-name"},
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * {"modify-target", modifyTarget, "t", "t", NULL,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * OPERAND_MANDATORY_MULTIPLE, "target-name"},
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * {"enable", enable, NULL, NULL, NULL, NO_OPERAND, NULL},
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * {NULL, 0, 0, NULL, 0, NULL}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * };
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef struct _subCommandProps {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *name;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte handler_t handler;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *optionString;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *required;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *exclusive;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int operand;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *operandDefinition;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto char *helpText;
8fe960854f0d52e2e8a80ba68e8621a5ac6a866dtim szeto uint8_t reserved[60];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte} subCommandProps_t;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define required_arg required_argument
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define no_arg no_argument
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Add short options and long options here
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * name -> long option name
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * has_arg -> required_arg, no_arg
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * val -> short option character
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * argDesc -> description of option argument
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Note: This structure may not be used if your CLI has no
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * options. However, -?, --help and -V, --version will still be supported
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * as they are standard for every CLI.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * EXAMPLE:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * optionTbl_t options[] = {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * {"filename", arg_required, 'f', "out-filename"},
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * {NULL, 0, 0}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * };
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef struct _optionTbl {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *name;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int has_arg;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte int val;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *argDesc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte} optionTbl_t;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * After tables are set, assign them to this structure
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * for passing into cmdparse()
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef struct _synTables {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte char *versionString;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte optionTbl_t *longOptionTbl;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte subCommandProps_t *subCommandPropsTbl;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte} synTables_t;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * cmdParse is a parser that checks syntax of the input command against
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * rules and property tables.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When syntax is successfully validated, the function associated with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * subcommand is called using the subcommands table functions.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Syntax for the command is as follows:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * command [options] subcommand [<options>] [<operand ...>]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * There are two standard short and long options assumed:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -?, --help Provides usage on a command or subcommand
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and stops further processing of the arguments
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -V, --version Provides version information on the command
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and stops further processing of the arguments
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * These options are loaded by this function.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * input:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * argc, argv from main
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * syntax rules tables (synTables_t structure)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * callArgs - void * passed by caller to be passed to subcommand function
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * output:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * funcRet - pointer to int that holds subcommand function return value
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Returns:
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * zero on successful syntax parse and function call
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * 1 on unsuccessful syntax parse (no function has been called)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This could be due to a version or help call or simply a
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * general usage call.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * -1 check errno, call failed
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteint cmdParse(int numOperands, char *operands[], synTables_t synTables,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *callerArgs, int *funcRet);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#ifdef __cplusplus
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif /* _CMDPARSE_H */