2 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
28 #define _(x) x /* not gettext support yet */
30 /* from libxcmd/command.c */
35 static argsfunc_t args_func;
36 static checkfunc_t check_func;
38 static char **cmdline;
41 compare(const void *a, const void *b)
43 return strcmp(((const cmdinfo_t *)a)->name,
44 ((const cmdinfo_t *)b)->name);
51 cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
52 cmdtab[ncmds - 1] = *ci;
53 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
61 return check_func(ci);
76 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
88 if (!check_command(ct))
91 if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
94 _("bad argument count %d to %s, expected at least %d arguments\n"),
95 argc-1, cmd, ct->argmin);
96 else if (ct->argmin == ct->argmax)
98 _("bad argument count %d to %s, expected %d arguments\n"),
99 argc-1, cmd, ct->argmin);
102 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
103 argc-1, cmd, ct->argmin, ct->argmax);
107 return ct->cfunc(argc, argv);
116 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
117 if (strcmp(ct->name, cmd) == 0 ||
118 (ct->altname && strcmp(ct->altname, cmd) == 0))
119 return (const cmdinfo_t *)ct;
125 add_user_command(char *optarg)
128 cmdline = realloc(cmdline, sizeof(char*) * (ncmdline));
133 cmdline[ncmdline-1] = optarg;
141 return args_func(index);
155 int c, i, j = 0, done = 0;
160 for (i = 0; !done && i < ncmdline; i++) {
161 input = strdup(cmdline[i]);
164 _("cannot strdup command '%s': %s\n"),
165 cmdline[i], strerror(errno));
168 v = breakline(input, &c);
170 ct = find_command(v[0]);
172 if (ct->flags & CMD_FLAG_GLOBAL)
173 done = command(ct, c, v);
176 while (!done && (j = args_command(j)))
177 done = command(ct, c, v);
180 fprintf(stderr, _("command \"%s\" not found\n"),
190 if ((input = fetchline()) == NULL)
192 v = breakline(input, &c);
194 ct = find_command(v[0]);
196 done = command(ct, c, v);
198 fprintf(stderr, _("command \"%s\" not found\n"),
205 /* from libxcmd/input.c */
207 #if defined(ENABLE_READLINE)
208 # include <readline/history.h>
209 # include <readline/readline.h>
210 #elif defined(ENABLE_EDITLINE)
211 # include <histedit.h>
217 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
220 snprintf(prompt, sizeof(prompt), "%s> ", progname);
224 #if defined(ENABLE_READLINE)
230 line = readline(get_prompt());
235 #elif defined(ENABLE_EDITLINE)
236 static char *el_get_prompt(EditLine *e) { return get_prompt(); }
241 static History *hist;
247 hist = history_init();
248 history(hist, &hevent, H_SETSIZE, 100);
249 el = el_init(progname, stdin, stdout, stderr);
251 el_set(el, EL_SIGNAL, 1);
252 el_set(el, EL_PROMPT, el_get_prompt);
253 el_set(el, EL_HIST, history, (const char *)hist);
255 line = strdup(el_gets(el, &count));
258 line[count-1] = '\0';
260 history(hist, &hevent, H_ENTER, line);
265 # define MAXREADLINESZ 1024
269 char *p, *line = malloc(MAXREADLINESZ);
273 printf("%s", get_prompt());
275 if (!fgets(line, MAXREADLINESZ, stdin)) {
279 p = line + strlen(line);
280 if (p != line && p[-1] == '\n')
286 static char *qemu_strsep(char **input, const char *delim)
288 char *result = *input;
289 if (result != NULL) {
291 for (p = result; *p != '\0'; p++) {
292 if (strchr(delim, *p)) {
313 char **rval = calloc(sizeof(char *), 1);
315 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
319 rval = realloc(rval, sizeof(*rval) * (c + 1));
340 #define EXABYTES(x) ((long long)(x) << 60)
341 #define PETABYTES(x) ((long long)(x) << 50)
342 #define TERABYTES(x) ((long long)(x) << 40)
343 #define GIGABYTES(x) ((long long)(x) << 30)
344 #define MEGABYTES(x) ((long long)(x) << 20)
345 #define KILOBYTES(x) ((long long)(x) << 10)
355 i = strtoll(s, &sp, 0);
356 if (i == 0 && sp == s)
384 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
385 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
386 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
387 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
388 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
389 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
400 precise = ((double)value * 1000 == (double)(int)value * 1000);
402 if (value >= EXABYTES(1)) {
403 fmt = precise ? "%.f EiB" : "%.3f EiB";
404 snprintf(str, size, fmt, TO_EXABYTES(value));
405 } else if (value >= PETABYTES(1)) {
406 fmt = precise ? "%.f PiB" : "%.3f PiB";
407 snprintf(str, size, fmt, TO_PETABYTES(value));
408 } else if (value >= TERABYTES(1)) {
409 fmt = precise ? "%.f TiB" : "%.3f TiB";
410 snprintf(str, size, fmt, TO_TERABYTES(value));
411 } else if (value >= GIGABYTES(1)) {
412 fmt = precise ? "%.f GiB" : "%.3f GiB";
413 snprintf(str, size, fmt, TO_GIGABYTES(value));
414 } else if (value >= MEGABYTES(1)) {
415 fmt = precise ? "%.f MiB" : "%.3f MiB";
416 snprintf(str, size, fmt, TO_MEGABYTES(value));
417 } else if (value >= KILOBYTES(1)) {
418 fmt = precise ? "%.f KiB" : "%.3f KiB";
419 snprintf(str, size, fmt, TO_KILOBYTES(value));
421 snprintf(str, size, "%f bytes", value);
426 tsub(struct timeval t1, struct timeval t2)
428 t1.tv_usec -= t2.tv_usec;
429 if (t1.tv_usec < 0) {
430 t1.tv_usec += 1000000;
433 t1.tv_sec -= t2.tv_sec;
438 tdiv(double value, struct timeval tv)
440 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
443 #define HOURS(sec) ((sec) / (60 * 60))
444 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
445 #define SECONDS(sec) ((sec) % 60)
454 double usec = (double)tv->tv_usec / 1000000.0;
456 if (format & TERSE_FIXED_TIME) {
457 if (!HOURS(tv->tv_sec)) {
458 snprintf(ts, size, "%u:%02u.%02u",
459 (unsigned int) MINUTES(tv->tv_sec),
460 (unsigned int) SECONDS(tv->tv_sec),
461 (unsigned int) usec * 100);
464 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
467 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
468 snprintf(ts, size, "%u:%02u:%02u.%02u",
469 (unsigned int) HOURS(tv->tv_sec),
470 (unsigned int) MINUTES(tv->tv_sec),
471 (unsigned int) SECONDS(tv->tv_sec),
472 (unsigned int) usec * 100);
474 snprintf(ts, size, "0.%04u sec", (unsigned int) usec * 10000);
479 /* from libxcmd/quit.c */
481 static cmdinfo_t quit_cmd;
495 quit_cmd.name = _("quit");
496 quit_cmd.altname = _("q");
497 quit_cmd.cfunc = quit_f;
498 quit_cmd.argmin = -1;
499 quit_cmd.argmax = -1;
500 quit_cmd.flags = CMD_FLAG_GLOBAL;
501 quit_cmd.oneline = _("exit the program");
503 add_command(&quit_cmd);
506 /* from libxcmd/help.c */
508 static cmdinfo_t help_cmd;
509 static void help_onecmd(const char *cmd, const cmdinfo_t *ct);
510 static void help_oneline(const char *cmd, const cmdinfo_t *ct);
517 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++)
518 help_oneline(ct->name, ct);
519 printf(_("\nUse 'help commandname' for extended help.\n"));
533 ct = find_command(argv[1]);
535 printf(_("command %s not found\n"), argv[1]);
538 help_onecmd(argv[1], ct);
547 help_oneline(cmd, ct);
560 printf("%s ", ct->name);
562 printf("(or %s) ", ct->altname);
565 printf("%s ", ct->args);
566 printf("-- %s\n", ct->oneline);
572 help_cmd.name = _("help");
573 help_cmd.altname = _("?");
574 help_cmd.cfunc = help_f;
577 help_cmd.flags = CMD_FLAG_GLOBAL;
578 help_cmd.args = _("[command]");
579 help_cmd.oneline = _("help for one or all commands");
581 add_command(&help_cmd);