#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <unistd.h>
#include <libintl.h>
#include <errno.h>
#include <string.h>
#include <assert.h>
#include <getopt.h>
#include "cmdparse.h"
#define GENERAL_USAGE 1
#define HELP_USAGE 2
#define DETAIL_USAGE 3
#define MAXOPTIONS (uint_t)('~' - '!' + 1)
#define MAXOPTIONSTRING MAXOPTIONS * 2
struct option standardCmdOptions[] = {
{"help", no_argument, NULL, '?'},
{"version", no_argument, NULL, 'V'},
{NULL, 0, NULL, 0}
};
struct option standardSubCmdOptions[] = {
{"help", no_argument, NULL, '?'},
{NULL, 0, NULL, 0}
};
static int getSubcommand(char *, subcommand_t **);
static char *getExecBasename(char *);
static void usage(uint_t);
static void subUsage(uint_t, subcommand_t *);
static void subUsageObject(uint_t, subcommand_t *, object_t *);
static int getObject(char *, object_t **);
static int getObjectRules(uint_t, objectRules_t **);
static const char *getLongOption(int);
static optionProp_t *getOptions(uint_t, uint_t);
static char *getOptionArgDesc(int);
extern void seeMan(void);
static struct option *_longOptions;
static subcommand_t *_subcommands;
static object_t *_objects;
static objectRules_t *_objectRules;
static optionRules_t *_optionRules;
static optionTbl_t *_clientOptionTbl;
static char *commandName;
static int
getObjectRules(uint_t object, objectRules_t **objectRules)
{
objectRules_t *sp;
for (sp = _objectRules; sp->value; sp++) {
if (sp->value == object) {
*objectRules = sp;
return (0);
}
}
return (1);
}
static int
getObject(char *arg, object_t **object)
{
object_t *op;
int len;
for (op = _objects; op->name; op++) {
len = strlen(arg);
if (len == strlen(op->name) &&
strncasecmp(arg, op->name, len) == 0) {
*object = op;
return (0);
}
}
return (1);
}
static int
getSubcommand(char *arg, subcommand_t **subcommand)
{
subcommand_t *sp;
int len;
for (sp = _subcommands; sp->name; sp++) {
len = strlen(arg);
if (len == strlen(sp->name) &&
strncasecmp(arg, sp->name, len) == 0) {
*subcommand = sp;
return (0);
}
}
return (1);
}
static optionProp_t *
getOptions(uint_t object, uint_t subcommand)
{
uint_t currObject;
optionRules_t *op = _optionRules;
while (op && ((currObject = op->objectValue) != 0)) {
if ((currObject == object) &&
(op->subcommandValue == subcommand)) {
return (&(op->optionProp));
}
op++;
}
return (NULL);
}
static const char *
getLongOption(int shortOption)
{
struct option *op;
for (op = _longOptions; op->name; op++) {
if (shortOption == op->val) {
return (op->name);
}
}
return (NULL);
}
static char *
getOptionArgDesc(int shortOption)
{
optionTbl_t *op;
for (op = _clientOptionTbl; op->name; op++) {
if (op->val == shortOption &&
op->has_arg == required_argument) {
return (op->argDesc);
}
}
return (NULL);
}
static void
subUsage(uint_t usageType, subcommand_t *subcommand)
{
int i;
object_t *objp;
(void) fprintf(stdout, "%s:\t%s %s [",
gettext("Usage"), commandName, subcommand->name);
for (i = 0; standardSubCmdOptions[i].name; i++) {
(void) fprintf(stdout, "-%c",
standardSubCmdOptions[i].val);
if (standardSubCmdOptions[i+1].name)
(void) fprintf(stdout, ",");
}
(void) fprintf(stdout, "] %s [", "<OBJECT>");
for (i = 0; standardSubCmdOptions[i].name; i++) {
(void) fprintf(stdout, "-%c",
standardSubCmdOptions[i].val);
if (standardSubCmdOptions[i+1].name)
(void) fprintf(stdout, ",");
}
(void) fprintf(stdout, "] %s", "[<OPERAND>]");
(void) fprintf(stdout, "\n");
if (usageType == GENERAL_USAGE) {
return;
}
(void) fprintf(stdout, "%s:\n", gettext("Usage by OBJECT"));
for (objp = _objects; objp->value; objp++) {
subUsageObject(usageType, subcommand, objp);
}
(void) atexit(seeMan);
}
static void
subUsageObject(uint_t usageType, subcommand_t *subcommand, object_t *objp)
{
int i;
objectRules_t *objRules = NULL;
opCmd_t *opCmd = NULL;
optionProp_t *options;
char *optionArgDesc;
const char *longOpt;
if (getObjectRules(objp->value, &objRules) != 0) {
assert(0);
}
opCmd = &(objRules->opCmd);
if (opCmd->invOpCmd & subcommand->value) {
return;
}
options = getOptions(objp->value, subcommand->value);
(void) fprintf(stdout, "\t%s %s ", commandName, subcommand->name);
(void) fprintf(stdout, "%s ", objp->name);
if (options != NULL) {
if (options->required) {
(void) fprintf(stdout, "%s", gettext("<"));
} else {
(void) fprintf(stdout, "%s", gettext("["));
}
(void) fprintf(stdout, "%s", gettext("OPTIONS"));
if (options->required) {
(void) fprintf(stdout, "%s ", gettext(">"));
} else {
(void) fprintf(stdout, "%s ", gettext("]"));
}
}
if (opCmd->optOpCmd & subcommand->value) {
(void) fprintf(stdout, gettext("["));
}
if (!(opCmd->noOpCmd & subcommand->value)) {
(void) fprintf(stdout, gettext("<"));
if (objRules->operandDefinition) {
(void) fprintf(stdout, "%s",
objRules->operandDefinition);
} else {
assert(0);
}
}
if (opCmd->multOpCmd & subcommand->value) {
(void) fprintf(stdout, gettext(" ..."));
}
if (!(opCmd->noOpCmd & subcommand->value)) {
(void) fprintf(stdout, gettext(">"));
}
if (opCmd->optOpCmd & subcommand->value) {
(void) fprintf(stdout, gettext("]"));
}
if (usageType == HELP_USAGE) {
(void) fprintf(stdout, "\n");
return;
}
if (options != NULL && options->optionString != NULL) {
(void) fprintf(stdout, "\n\t%s:", gettext("OPTIONS"));
for (i = 0; i < strlen(options->optionString); i++) {
if ((longOpt = getLongOption(
options->optionString[i]))
== NULL) {
assert(0);
}
(void) fprintf(stdout, "\n\t\t-%c, --%s ",
options->optionString[i], longOpt);
optionArgDesc =
getOptionArgDesc(options->optionString[i]);
if (optionArgDesc != NULL) {
(void) fprintf(stdout, "<%s>", optionArgDesc);
}
if (options->exclusive &&
strchr(options->exclusive,
options->optionString[i])) {
(void) fprintf(stdout, " (%s)",
gettext("exclusive"));
}
}
}
(void) fprintf(stdout, "\n");
(void) atexit(seeMan);
}
static void
usage(uint_t usageType)
{
int i;
subcommand_t subcommand;
subcommand_t *sp;
(void) fprintf(stdout, "%s:\t%s ",
gettext("Usage"), commandName);
for (i = 0; standardCmdOptions[i].name; i++) {
(void) fprintf(stdout, "-%c",
standardCmdOptions[i].val);
if (standardCmdOptions[i+1].name)
(void) fprintf(stdout, ",");
}
if (usageType == HELP_USAGE || usageType == GENERAL_USAGE) {
for (i = 0; standardCmdOptions[i].name; i++) {
}
}
(void) fprintf(stdout, "\n");
for (sp = _subcommands; sp->name; sp++) {
subcommand.name = sp->name;
subcommand.value = sp->value;
if (usageType == HELP_USAGE) {
(void) fprintf(stdout, "\n");
}
subUsage(usageType, &subcommand);
}
(void) atexit(seeMan);
}
static char *
getExecBasename(char *execFullname)
{
char *lastSlash, *execBasename;
for (;;) {
lastSlash = strrchr(execFullname, '/');
if (lastSlash == NULL) {
execBasename = execFullname;
break;
} else {
execBasename = lastSlash + 1;
if (*execBasename == '\0') {
*lastSlash = '\0';
continue;
}
break;
}
}
return (execBasename);
}
int
cmdParse(int argc, char *argv[], synTables_t synTable, void *callArgs,
int *funcRet)
{
int getoptargc;
char **getoptargv;
int opt;
int operInd;
int i, j;
int len;
char *versionString;
char optionStringAll[MAXOPTIONSTRING + 1];
optionProp_t *availOptions;
objectRules_t *objRules = NULL;
opCmd_t *opCmd = NULL;
subcommand_t *subcommand;
object_t *object;
cmdOptions_t cmdOptions[MAXOPTIONS + 1];
struct option *lp;
optionTbl_t *optionTbl;
struct option intLongOpt[MAXOPTIONS + 1];
if (synTable.versionString == NULL ||
synTable.subcommandTbl == NULL ||
synTable.objectRulesTbl == NULL ||
synTable.objectTbl == NULL ||
funcRet == NULL) {
assert(0);
}
versionString = synTable.versionString;
commandName = getExecBasename(argv[0]);
setbuf(stdout, NULL);
_subcommands = synTable.subcommandTbl;
_objectRules = synTable.objectRulesTbl;
_optionRules = synTable.optionRulesTbl;
_objects = synTable.objectTbl;
_clientOptionTbl = synTable.longOptionTbl;
if (argc < 2) {
usage(GENERAL_USAGE);
return (1);
}
(void) memset(&intLongOpt[0], 0, sizeof (intLongOpt));
for (i = 0; standardSubCmdOptions[i].name; i++) {
intLongOpt[i].name = standardSubCmdOptions[i].name;
intLongOpt[i].has_arg = standardSubCmdOptions[i].has_arg;
intLongOpt[i].flag = standardSubCmdOptions[i].flag;
intLongOpt[i].val = standardSubCmdOptions[i].val;
}
for (optionTbl = synTable.longOptionTbl;
optionTbl && optionTbl->name; optionTbl++, i++) {
if (i > MAXOPTIONS - 1) {
assert(0);
}
intLongOpt[i].name = optionTbl->name;
intLongOpt[i].has_arg = optionTbl->has_arg;
intLongOpt[i].flag = NULL;
intLongOpt[i].val = optionTbl->val;
}
_longOptions = &intLongOpt[0];
while ((opt = getopt_long(argc, argv, "+?V", standardCmdOptions,
NULL)) != EOF) {
switch (opt) {
case '?':
if (optopt == '?') {
usage(HELP_USAGE);
return (0);
} else {
usage(GENERAL_USAGE);
return (0);
}
case 'V':
(void) fprintf(stdout, "%s: %s %s\n",
commandName, gettext("Version"),
versionString);
(void) atexit(seeMan);
return (0);
default:
break;
}
}
if (getSubcommand(argv[1], &subcommand) != 0) {
(void) fprintf(stderr, "%s: %s\n",
commandName, gettext("invalid subcommand"));
usage(GENERAL_USAGE);
return (1);
}
if (argc == 2) {
(void) fprintf(stderr, "%s: %s\n",
commandName, gettext("missing object"));
subUsage(GENERAL_USAGE, subcommand);
(void) atexit(seeMan);
return (1);
}
getoptargv = argv;
getoptargv++;
getoptargc = argc;
getoptargc -= 1;
while ((opt = getopt_long(getoptargc, getoptargv, "+?",
standardSubCmdOptions, NULL)) != EOF) {
switch (opt) {
case '?':
if (optopt == '?') {
subUsage(HELP_USAGE, subcommand);
return (0);
} else {
subUsage(GENERAL_USAGE, subcommand);
return (0);
}
default:
break;
}
}
if (getObject(argv[2], &object) != 0) {
(void) fprintf(stderr, "%s: %s\n",
commandName, gettext("invalid object"));
subUsage(HELP_USAGE, subcommand);
return (1);
}
if (getObjectRules(object->value, &objRules) != 0) {
assert(0);
}
opCmd = &(objRules->opCmd);
if (opCmd->invOpCmd & subcommand->value) {
(void) fprintf(stderr, "%s: %s %s\n", commandName,
gettext("invalid subcommand for"), object->name);
subUsage(HELP_USAGE, subcommand);
return (1);
}
getoptargv = argv;
getoptargv++;
getoptargv++;
getoptargc = argc;
getoptargc -= 2;
(void) memset(optionStringAll, 0, sizeof (optionStringAll));
(void) memset(&cmdOptions[0], 0, sizeof (cmdOptions));
j = 0;
for (lp = _longOptions; lp->name; lp++, j++) {
if (j + 1 >= sizeof (optionStringAll)) {
assert(0);
}
optionStringAll[j] = lp->val;
if (lp->has_arg == required_argument) {
optionStringAll[++j] = ':';
}
}
i = 0;
while ((opt = getopt_long(getoptargc, getoptargv, optionStringAll,
_longOptions, NULL)) != EOF) {
switch (opt) {
case '?':
if (optopt == '?') {
subUsageObject(DETAIL_USAGE,
subcommand, object);
return (0);
} else {
subUsage(GENERAL_USAGE, subcommand);
return (0);
}
default:
cmdOptions[i].optval = opt;
if (optarg) {
len = strlen(optarg);
if (len > sizeof (cmdOptions[i].optarg)
- 1) {
(void) fprintf(stderr,
"%s: %s\n",
commandName,
gettext("option too long"));
errno = EINVAL;
return (-1);
}
(void) strncpy(cmdOptions[i].optarg,
optarg, len);
}
i++;
break;
}
}
operInd = optind + 2;
availOptions = getOptions(object->value, subcommand->value);
if (cmdOptions[0].optval != 0) {
if (availOptions == NULL) {
(void) fprintf(stderr, "%s: %s\n",
commandName, gettext("no options permitted"));
subUsageObject(HELP_USAGE, subcommand, object);
return (1);
}
for (i = 0; cmdOptions[i].optval; i++) {
if (availOptions->optionString == NULL) {
assert(0);
}
if (!(strchr(availOptions->optionString,
cmdOptions[i].optval))) {
(void) fprintf(stderr,
"%s: '-%c': %s\n",
commandName, cmdOptions[i].optval,
gettext("invalid option"));
subUsageObject(DETAIL_USAGE, subcommand,
object);
return (1);
} else if (cmdOptions[1].optval != 0 &&
availOptions->exclusive &&
strchr(availOptions->exclusive,
cmdOptions[i].optval)) {
(void) fprintf(stderr,
"%s: '-%c': %s\n",
commandName, cmdOptions[i].optval,
gettext("is an exclusive option"));
subUsageObject(DETAIL_USAGE, subcommand,
object);
return (1);
}
}
} else {
if (availOptions != NULL &&
(availOptions->required)) {
(void) fprintf(stderr, "%s: %s\n",
commandName,
gettext("at least one option required"));
subUsageObject(DETAIL_USAGE, subcommand,
object);
return (1);
}
}
if ((operInd == argc) &&
(opCmd->reqOpCmd & subcommand->value)) {
(void) fprintf(stderr, "%s: %s %s %s\n",
commandName, subcommand->name,
object->name, gettext("requires an operand"));
subUsageObject(HELP_USAGE, subcommand, object);
(void) atexit(seeMan);
return (1);
}
if ((argc > operInd) &&
(opCmd->noOpCmd & subcommand->value)) {
(void) fprintf(stderr, "%s: %s %s %s\n",
commandName, subcommand->name,
object->name, gettext("takes no operands"));
subUsageObject(HELP_USAGE, subcommand, object);
return (1);
}
if ((argc > operInd) && ((argc - operInd) != 1) &&
!(opCmd->multOpCmd & subcommand->value)) {
(void) fprintf(stderr, "%s: %s %s %s\n",
commandName, subcommand->name, object->name,
gettext("accepts only a single operand"));
subUsageObject(HELP_USAGE, subcommand, object);
return (1);
}
return (subcommand->handler(argc - operInd, &argv[operInd],
object->value, &cmdOptions[0], callArgs, funcRet));
}