1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
|
/* cli/main.c - Main routine for all CLI programs
* Copyright (C) 2001,2005 Bruce Guenter <bruce@untroubled.org>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "iobuf.h"
#include "msg.h"
#include <time.h>
#include "systime.h"
#include <stdlib.h>
#include <string.h>
#include "cli.h"
#include "internal.h"
static int do_show_usage = 0;
const char* argv0;
const char* argv0base;
const char* argv0dir;
cli_option cli_help_option = { 'h', "help", CLI_FLAG,
1, &do_show_usage,
"Display this help and exit", 0 };
cli_option** cli_full_options;
unsigned cli_option_count;
static void build_options()
{
unsigned i;
for(cli_option_count = 0;
cli_options[cli_option_count].ch || cli_options[cli_option_count].name;
cli_option_count++) ;
cli_option_count++;
cli_full_options = malloc(cli_option_count * sizeof *cli_full_options);
for(i = 0; i < cli_option_count-1; i++)
cli_full_options[i] = &cli_options[i];
cli_full_options[cli_option_count-1] = &cli_help_option;
}
static void show_usage()
{
obuf_put5s(&outbuf, "usage: ", program, " [flags] ", cli_args_usage, "\n");
}
void usage(int exit_value, const char* errorstr)
{
if(errorstr)
error1(errorstr);
show_usage();
cli_show_help();
obuf_flush(&outbuf);
exit(exit_value);
}
static cli_stringlist* stringlist_append(cli_stringlist* node,
const char* newstr,
const cli_option* option)
{
cli_stringlist* newnode;
newnode = malloc(sizeof *newnode);
newnode->string = newstr;
newnode->option = option;
newnode->next = 0;
if(node) {
cli_stringlist* head = node;
while(node->next)
node = node->next;
node->next = newnode;
return head;
}
else
return newnode;
}
static int cli_option_set(cli_option* o, const char* arg)
{
char* endptr;
switch(o->type) {
case CLI_FLAG:
*(int*)o->dataptr = o->flag_value;
return 0;
case CLI_COUNTER:
*(int*)o->dataptr += o->flag_value;
return 0;
case CLI_INTEGER:
*(int*)o->dataptr = strtol(arg, &endptr, 10);
if(*endptr) {
error2("Invalid integer: ", arg);
return -1;
}
return 1;
case CLI_UINTEGER:
*(unsigned*)o->dataptr = strtoul(arg, &endptr, 10);
if(*endptr) {
error2("Invalid unsigned integer: ", arg);
return -1;
}
return 1;
case CLI_STRINGLIST:
*(cli_stringlist**)o->dataptr =
stringlist_append(*(cli_stringlist**)o->dataptr, arg, o);
return 1;
case CLI_FUNCTION:
((cli_function*)o->dataptr)(arg, o);
return 1;
default:
*(const char**)o->dataptr = arg;
return 1;
}
}
static int parse_short(int argc, char* argv[])
{
int i;
int end = strlen(argv[0]) - 1;
for(i = 1; i <= end; i++) {
int ch = argv[0][i];
unsigned j;
for(j = 0; j < cli_option_count; j++) {
cli_option* o = cli_full_options[j];
if(o->ch == ch) {
if(o->type != CLI_FLAG &&
o->type != CLI_COUNTER) {
if(i < end) {
if(cli_option_set(o, argv[0]+i+1) != -1)
return 0;
}
else if(argc <= 1) {
char tmp[] = "Option -X requires a value.";
tmp[8] = o->ch;
error1(tmp);
}
else
if(cli_option_set(o, argv[1]) != -1)
return 1;
}
else if(cli_option_set(o, 0) != -1)
break;
return -1;
}
}
if(j >= cli_option_count) {
char tmp[] = "Unknown option letter -X.";
tmp[23] = argv[0][i];
error1(tmp);
return -1;
}
}
return 0;
}
static int cli_option_parse_long_eq(cli_option* o, const char* arg)
{
if(o->type == CLI_FLAG || o->type == CLI_COUNTER) {
error3("Option --", o->name, " does not take a value.");
return -1;
}
else
return cli_option_set(o, arg) - 1;
}
static int cli_option_parse_long_noeq(cli_option* o, const char* arg)
{
if(o->type == CLI_FLAG || o->type == CLI_COUNTER)
return cli_option_set(o, 0);
else if(arg)
return cli_option_set(o, arg);
else {
error3("Option --", o->name, " requires a value.");
return -1;
}
}
static int parse_long(char* argv[])
{
unsigned j;
const char* arg = argv[0]+2;
for(j = 0; j < cli_option_count; j++) {
cli_option* o = cli_full_options[j];
if(o->name) {
size_t len = strlen(o->name);
if(!memcmp(arg, o->name, len)) {
if(arg[len] == '\0')
return cli_option_parse_long_noeq(o, argv[1]);
else if(arg[len] == '=')
return cli_option_parse_long_eq(o, arg+len+1);
}
}
}
error3("Unknown option string: '--", arg, "'");
return -1;
}
static int parse_args(int argc, char* argv[])
{
int i;
int j;
build_options();
for(i = 1; i < argc; i++) {
const char* arg = argv[i];
// Stop at the first non-option argument
if(arg[0] != '-')
break;
// Stop after the first "-" or "--"
if(arg[1] == '\0' ||
(arg[1] == '-' && arg[2] == '\0')) {
i++;
break;
}
j = (arg[1] != '-') ?
parse_short(argc-i, argv+i) :
parse_long(argv+i);
if(j < 0)
usage(1, 0);
else
i += j;
}
return i;
}
static void set_argv0(const char* p)
{
static const char* empty = "";
const char* s = strrchr(p, '/');
size_t length;
char* tmp;
argv0 = p;
if(s) {
++s;
argv0base = s;
length = s-p;
tmp = malloc(length+1);
memcpy(tmp, p, length);
tmp[length] = 0;
argv0dir = tmp;
}
else {
argv0base = p;
argv0dir = empty;
}
}
int main(int argc, char* argv[])
{
struct timeval tv;
int lastarg;
gettimeofday(&tv, 0);
srandom(tv.tv_usec ^ tv.tv_sec);
set_argv0(argv[0]);
lastarg = parse_args(argc, argv);
if(do_show_usage)
usage(0, 0);
argc -= lastarg;
argv += lastarg;
if(argc < cli_args_min)
usage(1, "Too few command-line arguments");
if(cli_args_max >= cli_args_min && argc > cli_args_max)
usage(1, "Too many command-line arguments");
return cli_main(argc, argv);
}
|