2009-09-10 10:31:40 +00:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2009 Martin Willi
|
|
|
|
* Hochschule fuer Technik Rapperswil
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it
|
|
|
|
* under the terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation; either version 2 of the License, or (at your
|
|
|
|
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "command.h"
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Registered commands.
|
|
|
|
*/
|
2009-09-10 10:44:06 +00:00
|
|
|
command_t cmds[MAX_COMMANDS];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* active command.
|
|
|
|
*/
|
|
|
|
static int active = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* number of registered commands
|
|
|
|
*/
|
|
|
|
static int registered = 0;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* help command index
|
|
|
|
*/
|
|
|
|
static int help_idx;
|
2009-09-10 10:31:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Global options used by all subcommands
|
|
|
|
*/
|
2009-09-10 10:44:06 +00:00
|
|
|
struct option command_opts[MAX_COMMANDS > MAX_OPTIONS ?: MAX_OPTIONS];
|
2009-09-10 10:31:40 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Build long_opts for a specific command
|
|
|
|
*/
|
2009-09-10 10:44:06 +00:00
|
|
|
static void build_opts()
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
memset(command_opts, 0, sizeof(command_opts));
|
2009-09-10 10:44:06 +00:00
|
|
|
if (active == help_idx)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[i].cmd; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
command_opts[i].name = cmds[i].cmd;
|
|
|
|
command_opts[i].val = cmds[i].op;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[active].options[i].name; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
command_opts[i].name = cmds[active].options[i].name;
|
|
|
|
command_opts[i].has_arg = cmds[active].options[i].arg;
|
|
|
|
command_opts[i].val = cmds[active].options[i].op;
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a command
|
|
|
|
*/
|
2009-09-10 10:44:06 +00:00
|
|
|
void command_register(command_t command)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
cmds[registered++] = command;
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Print usage text, with an optional error
|
|
|
|
*/
|
2009-09-10 10:44:06 +00:00
|
|
|
int command_usage(char *error)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
FILE *out = stdout;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (error)
|
|
|
|
{
|
|
|
|
out = stderr;
|
|
|
|
fprintf(out, "Error: %s\n", error);
|
|
|
|
}
|
|
|
|
fprintf(out, "strongSwan %s PKI tool\n", VERSION);
|
|
|
|
fprintf(out, "usage:\n");
|
2009-09-10 10:44:06 +00:00
|
|
|
if (active == help_idx)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[i].cmd; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
fprintf(out, " pki --%-6s %s\n", cmds[i].cmd, cmds[i].description);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[active].line[i]; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
if (i == 0)
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
fprintf(out, " pki --%s %s\n",
|
|
|
|
cmds[active].cmd, cmds[active].line[i]);
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
fprintf(out, " %s\n", cmds[active].line[i]);
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[active].options[i].name; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
fprintf(out, " --%-8s %s\n",
|
2009-09-10 10:44:06 +00:00
|
|
|
cmds[active].options[i].name, cmds[active].options[i].desc);
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return error != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Show usage information
|
|
|
|
*/
|
|
|
|
static int help(int argc, char *argv[])
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
return command_usage(NULL);
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dispatch commands.
|
|
|
|
*/
|
|
|
|
int command_dispatch(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
int op, i;
|
|
|
|
|
2009-09-10 10:44:06 +00:00
|
|
|
active = help_idx = registered;
|
|
|
|
command_register((command_t){help, 'h', "help", "show usage information"});
|
|
|
|
|
|
|
|
build_opts();
|
2009-09-10 10:31:40 +00:00
|
|
|
op = getopt_long(argc, argv, "", command_opts, NULL);
|
2009-09-10 10:44:06 +00:00
|
|
|
for (i = 0; cmds[i].cmd; i++)
|
2009-09-10 10:31:40 +00:00
|
|
|
{
|
|
|
|
if (cmds[i].op == op)
|
|
|
|
{
|
2009-09-10 10:44:06 +00:00
|
|
|
active = i;
|
|
|
|
build_opts();
|
2009-09-10 10:31:40 +00:00
|
|
|
return cmds[i].call(argc, argv);
|
|
|
|
}
|
|
|
|
}
|
2009-09-10 10:44:06 +00:00
|
|
|
return command_usage("invalid operation");
|
2009-09-10 10:31:40 +00:00
|
|
|
}
|
|
|
|
|