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 279 280 281
|
/*
* medussa - a distributed cracking system
* Copyright (C) 1999 Kostas Evangelinos <kos@bastard.net>
*
*
* 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.
*
* 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.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
*/
/*
* $Id: cli.c,v 1.11 2000/11/14 03:57:02 kos Exp $
*
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <unistd.h>
#if HAVE_LIBREADLINE
#include <readline/readline.h>
#include <readline/history.h>
#endif
#include "cli.h"
#include "xmalloc.h"
#include "array.h"
#include "common.h"
cli_t *
cli_init(void *up) {
cli_t *c;
c = (cli_t *)xcalloc(1, sizeof(cli_t));
c->up = up;
c->cmds = array_init(sizeof(cmd_t));
strncpy(c->prompt, CLI_DEF_PROMPT, CLI_LINELEN);
strncpy(c->parsesep, CLI_DEF_PARSESEP, CLI_LINELEN);
strncpy(c->lastcommand, "?", CLI_LINELEN);
return c;
}
int
cli_destroy(cli_t *c) {
array_nuke(c->cmds);
free(c);
return 0;
}
int
cli_register(cli_t *c, int type, cmd_proc proc, char *name, ...) {
cmd_t cmd;
va_list vl;
char *p;
va_start(vl, name);
strncpy(cmd.name, name, CLI_LINELEN);
strncpy(cmd.help, "No help", CLI_LINELEN);
cmd.proc = proc;
cmd.type = type;
switch(type) {
case CLI_PARSE:
cmd.match = 0;
cmd.argmin = va_arg(vl, int);
cmd.argmax = va_arg(vl, int);
if((p = va_arg(vl, char *)))
strncpy(cmd.help, p, CLI_LINELEN);
break;
case CLI_RAW:
break;
default:
va_end(vl);
return 2;
}
va_end(vl);
array_add(c->cmds, &cmd);
return 0;
}
int
cli_set(cli_t *c, int type, ...) {
va_list vl;
va_start(vl, type);
switch(type) {
case CLI_PROMPT:
strncpy(c->prompt, va_arg(vl, char *), CLI_LINELEN);
break;
case CLI_PARSESEP:
strncpy(c->parsesep, va_arg(vl, char *), CLI_LINELEN);
break;
default:
va_end(vl);
return 1;
break;
}
va_end(vl);
return 0;
}
int
cli_parse(cli_t *c, char *line, char **args, int *nargs) {
*nargs = 0;
for(args[(*nargs)++]=strtok(line, " \t\n"); (args[(*nargs)]=strtok(NULL, " \t\n")); (*nargs)++)
;
return 0;
}
int
cli_zap_matches(cli_t *c) {
int i;
cmd_t *cmd;
for(i=0; (cmd=array_get(c->cmds, i)); i++)
cmd->match = 0;
return 0;
}
int
cli_find_matches(cli_t *c, char *what) {
cmd_t *cmd;
int i;
int len;
int nmatches;
nmatches = 0;
len = strlen(what);
for(i=0; (cmd=array_get(c->cmds, i)); i++)
if(!strncmp(cmd->name, what, MIN(len, CLI_LINELEN))) {
cmd->match = 1;
nmatches++;
}
return nmatches;
}
int
cli_despatch(cli_t *c, char **args, int nargs) {
cmd_t *cmd;
int i;
if(!nargs)
return 2;
if(!strcmp(args[0], "?")) {
if(nargs==2) {
for(i=0; (cmd=array_get(c->cmds, i)); i++) {
if(!strcmp(cmd->name, args[1]))
fprintf(stdout, "%-12s %-67s\n", cmd->name, cmd->help);
}
} else {
for(i=0; (cmd=array_get(c->cmds, i)); i++) {
fprintf(stdout, "%-12s %-67s\n", cmd->name, cmd->help);
}
}
return 0;
}
cli_zap_matches(c);
switch(cli_find_matches(c, args[0])) {
case 0:
fprintf(stdout, "Unknown command \"%s\"\n", args[0]);
return 1;
break;
case 1:
for(i=0; (cmd=array_get(c->cmds, i)); i++) {
if(cmd->match) {
if(cmd->argmin && nargs < cmd->argmin) {
fprintf(stdout, "Command %s needs at least %d arguments\n", cmd->name, cmd->argmin);
return 2;
}
if(cmd->argmax && nargs > cmd->argmax) {
fprintf(stdout, "Command %s needs at most %d arguments\n", cmd->name, cmd->argmax);
return 3;
}
if(cmd->proc(c->up, nargs, args))
return -1;
return 0;
}
}
default:
fprintf(stdout, "Ambiguous command \"%s\": ", args[0]);
for(i=0; (cmd=array_get(c->cmds, i)); i++) {
if(cmd->match)
fprintf(stdout, "%s ", cmd->name);
}
fprintf(stdout, "\n");
return 4;
}
}
int
cli_main(cli_t *c) {
#if HAVE_LIBREADLINE
char *pline;
#endif
char line[CLI_LINELEN];
int nargs;
char *args[CLI_MAXARGS];
int result;
#if HAVE_LIBREADLINE
if(!(pline = readline(c->prompt)))
return 1;
strncpy(line, pline, CLI_LINELEN);
free(pline);
#else
if(isatty(0)) {
fwrite(c->prompt, strlen(c->prompt), 1, stdout);
fflush(stdout);
}
if(!fgets(line, CLI_LINELEN, stdin))
return 1;
#endif
zap_whitespace(line, CLI_LINELEN, ZAP_LEFT|ZAP_RIGHT);
if(line[0] == '\0')
strncpy(line, c->lastcommand, CLI_LINELEN);
else
strncpy(c->lastcommand, line, CLI_LINELEN);
#if HAVE_LIBREADLINE
add_history(line);
#endif
cli_parse(c, line, args, &nargs);
result = cli_despatch(c, args, nargs);
if(result < 0)
return 1;
else
return 0;
}
#ifdef _CLI_DEBUG
int
command1(int nargs, char **args) {
printf("This is command1\n");
}
int
command2(int nargs, char **args) {
printf("This is command2\n");
}
int
main(int argc, char **argv) {
cli_t cli;
cli_new(&cli);
cli_set(&cli, CLI_PROMPT, "test> ");
cli_set(&cli, CLI_PARSESEP, " ");
cli_register(&cli, CLI_PARSE, command1, "command1", 1, 1);
cli_register(&cli, CLI_RAW, command2, "command2");
cli_main(&cli);
cli_free(&cli);
}
#endif
|