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
|
// SPDX-License-Identifier: GPL-2.0
/*
* Copyright (c) 2003-2005 Silicon Graphics, Inc.
* All Rights Reserved.
*/
#include "platform_defs.h"
#include "command.h"
#include "input.h"
cmdinfo_t *cmdtab;
int ncmds;
static iterfunc_t iter_func;
static checkfunc_t check_func;
struct cmdline {
char *cmdline;
bool iterate;
};
static int ncmdline;
static struct cmdline *cmdline;
static int
compare(const void *a, const void *b)
{
return strcmp(((const cmdinfo_t *)a)->name,
((const cmdinfo_t *)b)->name);
}
void
add_command(
const cmdinfo_t *ci)
{
cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
if (!cmdtab) {
perror(_("adding libxcmd command"));
exit(1);
}
cmdtab[ncmds - 1] = *ci;
qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
}
static int
check_command(
const cmdinfo_t *ci)
{
/* always run internal library supplied commands */
if (ci->flags & CMD_FLAG_LIBRARY)
return 1;
if (check_func)
return check_func(ci);
return 1;
}
void
add_check_command(
checkfunc_t cf)
{
check_func = cf;
}
int
command_usage(
const cmdinfo_t *ci)
{
printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
return 0;
}
int
command(
const cmdinfo_t *ct,
int argc,
char **argv)
{
char *cmd = argv[0];
if (!check_command(ct))
return 0;
if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
if (ct->argmax == -1)
fprintf(stderr,
_("bad argument count %d to %s, expected at least %d arguments\n"),
argc-1, cmd, ct->argmin);
else if (ct->argmin == ct->argmax)
fprintf(stderr,
_("bad argument count %d to %s, expected %d arguments\n"),
argc-1, cmd, ct->argmin);
else
fprintf(stderr,
_("bad argument count %d to %s, expected between %d and %d arguments\n"),
argc-1, cmd, ct->argmin, ct->argmax);
return 0;
}
platform_getoptreset();
return ct->cfunc(argc, argv);
}
const cmdinfo_t *
find_command(
const char *cmd)
{
cmdinfo_t *ct;
for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
if (strcmp(ct->name, cmd) == 0 ||
(ct->altname && strcmp(ct->altname, cmd) == 0))
return (const cmdinfo_t *)ct;
}
return NULL;
}
void
add_user_command(char *optarg)
{
ncmdline++;
cmdline = realloc(cmdline, sizeof(struct cmdline) * (ncmdline));
if (!cmdline) {
perror("realloc");
exit(1);
}
cmdline[ncmdline-1].cmdline = optarg;
cmdline[ncmdline-1].iterate = true;
}
void
add_oneshot_user_command(char *optarg)
{
ncmdline++;
cmdline = realloc(cmdline, sizeof(struct cmdline) * (ncmdline));
if (!cmdline) {
perror("realloc");
exit(1);
}
cmdline[ncmdline-1].cmdline = optarg;
cmdline[ncmdline-1].iterate = false;
}
/*
* Run a command, iterating as necessary. Return 0 for success, non-zero
* if an error occurred. Errors terminate loop iteration immediately.
*/
static int
iterate_command(
const cmdinfo_t *ct,
int argc,
char **argv)
{
int error = 0;
int j;
/* if there's nothing to iterate, we're done! */
if (!iter_func)
return 0;
for (j = iter_func(0); j; j = iter_func(j)) {
error = command(ct, argc, argv);
if (error)
break;
}
return error;
}
void
add_command_iterator(
iterfunc_t func)
{
iter_func = func;
}
static int
process_input(
char *input,
bool iterate)
{
char **v;
const cmdinfo_t *ct;
int c = 0;
int error = 0;
v = breakline(input, &c);
if (!c)
goto out;
ct = find_command(v[0]);
if (!ct) {
fprintf(stderr, _("command \"%s\" not found\n"), v[0]);
goto out;
}
/* oneshot commands don't iterate */
if (!iterate || (ct->flags & CMD_FLAG_ONESHOT))
error = command(ct, c, v);
else
error = iterate_command(ct, c, v);
out:
doneline(input, v);
return error;
}
void
command_loop(void)
{
char *input;
int done = 0;
int i;
if (!cmdline) {
/* interactive mode */
while (!done) {
input = fetchline();
if (!input)
break;
done = process_input(input, false);
}
return;
}
/* command line mode */
for (i = 0; !done && i < ncmdline; i++) {
input = strdup(cmdline[i].cmdline);
if (!input) {
fprintf(stderr,
_("cannot strdup command '%s': %s\n"),
cmdline[i].cmdline, strerror(errno));
exit(1);
}
done = process_input(input, cmdline[i].iterate);
}
free(cmdline);
return;
}
void
report_io_times(
const char *verb,
struct timeval *t2,
long long offset,
long long count,
long long total,
int ops,
int compact)
{
char s1[64], s2[64], ts[64];
timestr(t2, ts, sizeof(ts), compact ? VERBOSE_FIXED_TIME : 0);
if (!compact) {
cvtstr((double)total, s1, sizeof(s1));
cvtstr(tdiv((double)total, *t2), s2, sizeof(s2));
printf(_("%s %lld/%lld bytes at offset %lld\n"),
verb, total, count, (long long)offset);
printf(_("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n"),
s1, ops, ts, s2, tdiv((double)ops, *t2));
} else {/* bytes,ops,time,bytes/sec,ops/sec */
printf("%lld,%d,%s,%.3f,%.3f\n",
total, ops, ts,
tdiv((double)total, *t2), tdiv((double)ops, *t2));
}
}
|