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/>.
29 #define _(x) x /* not gettext support yet */
31 /* from libxcmd/command.c */
36 static argsfunc_t args_func;
37 static checkfunc_t check_func;
39 static char **cmdline;
42 compare(const void *a, const void *b)
44 return strcmp(((const cmdinfo_t *)a)->name,
45 ((const cmdinfo_t *)b)->name);
48 void add_command(const cmdinfo_t *ci)
50 cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
51 cmdtab[ncmds - 1] = *ci;
52 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
60 return check_func(ci);
75 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
87 if (!check_command(ct))
90 if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
93 _("bad argument count %d to %s, expected at least %d arguments\n"),
94 argc-1, cmd, ct->argmin);
95 else if (ct->argmin == ct->argmax)
97 _("bad argument count %d to %s, expected %d arguments\n"),
98 argc-1, cmd, ct->argmin);
101 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
102 argc-1, cmd, ct->argmin, ct->argmax);
106 return ct->cfunc(argc, argv);
115 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
116 if (strcmp(ct->name, cmd) == 0 ||
117 (ct->altname && strcmp(ct->altname, cmd) == 0))
118 return (const cmdinfo_t *)ct;
123 void add_user_command(char *optarg)
126 cmdline = realloc(cmdline, ncmdline * sizeof(char *));
131 cmdline[ncmdline-1] = optarg;
139 return args_func(index);
150 static void prep_fetchline(void *opaque)
152 int *fetchable = opaque;
154 qemu_aio_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL, NULL, NULL);
158 static char *get_prompt(void);
160 void command_loop(void)
162 int c, i, j = 0, done = 0, fetchable = 0, prompted = 0;
167 for (i = 0; !done && i < ncmdline; i++) {
168 input = strdup(cmdline[i]);
170 fprintf(stderr, _("cannot strdup command '%s': %s\n"),
171 cmdline[i], strerror(errno));
174 v = breakline(input, &c);
176 ct = find_command(v[0]);
178 if (ct->flags & CMD_FLAG_GLOBAL) {
179 done = command(ct, c, v);
182 while (!done && (j = args_command(j))) {
183 done = command(ct, c, v);
187 fprintf(stderr, _("command \"%s\" not found\n"), v[0]);
199 printf("%s", get_prompt());
201 qemu_aio_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, NULL,
215 v = breakline(input, &c);
217 ct = find_command(v[0]);
219 done = command(ct, c, v);
221 fprintf(stderr, _("command \"%s\" not found\n"), v[0]);
229 qemu_aio_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL, NULL, NULL);
232 /* from libxcmd/input.c */
234 #if defined(ENABLE_READLINE)
235 # include <readline/history.h>
236 # include <readline/readline.h>
237 #elif defined(ENABLE_EDITLINE)
238 # include <histedit.h>
244 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
247 snprintf(prompt, sizeof(prompt), "%s> ", progname);
251 #if defined(ENABLE_READLINE)
257 line = readline(get_prompt());
262 #elif defined(ENABLE_EDITLINE)
263 static char *el_get_prompt(EditLine *e) { return get_prompt(); }
268 static History *hist;
274 hist = history_init();
275 history(hist, &hevent, H_SETSIZE, 100);
276 el = el_init(progname, stdin, stdout, stderr);
278 el_set(el, EL_SIGNAL, 1);
279 el_set(el, EL_PROMPT, el_get_prompt);
280 el_set(el, EL_HIST, history, (const char *)hist);
282 line = strdup(el_gets(el, &count));
285 line[count-1] = '\0';
287 history(hist, &hevent, H_ENTER, line);
292 # define MAXREADLINESZ 1024
296 char *p, *line = malloc(MAXREADLINESZ);
300 if (!fgets(line, MAXREADLINESZ, stdin)) {
304 p = line + strlen(line);
305 if (p != line && p[-1] == '\n')
311 static char *qemu_strsep(char **input, const char *delim)
313 char *result = *input;
314 if (result != NULL) {
317 for (p = result; *p != '\0'; p++) {
318 if (strchr(delim, *p)) {
332 char **breakline(char *input, int *count)
336 char **rval = calloc(sizeof(char *), 1);
338 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
343 rval = realloc(rval, sizeof(*rval) * (c + 1));
364 #define EXABYTES(x) ((long long)(x) << 60)
365 #define PETABYTES(x) ((long long)(x) << 50)
366 #define TERABYTES(x) ((long long)(x) << 40)
367 #define GIGABYTES(x) ((long long)(x) << 30)
368 #define MEGABYTES(x) ((long long)(x) << 20)
369 #define KILOBYTES(x) ((long long)(x) << 10)
379 i = strtoll(s, &sp, 0);
380 if (i == 0 && sp == s)
388 c = qemu_tolower(*sp);
408 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
409 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
410 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
411 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
412 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
413 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
424 precise = ((double)value * 1000 == (double)(int)value * 1000);
426 if (value >= EXABYTES(1)) {
427 fmt = precise ? "%.f EiB" : "%.3f EiB";
428 snprintf(str, size, fmt, TO_EXABYTES(value));
429 } else if (value >= PETABYTES(1)) {
430 fmt = precise ? "%.f PiB" : "%.3f PiB";
431 snprintf(str, size, fmt, TO_PETABYTES(value));
432 } else if (value >= TERABYTES(1)) {
433 fmt = precise ? "%.f TiB" : "%.3f TiB";
434 snprintf(str, size, fmt, TO_TERABYTES(value));
435 } else if (value >= GIGABYTES(1)) {
436 fmt = precise ? "%.f GiB" : "%.3f GiB";
437 snprintf(str, size, fmt, TO_GIGABYTES(value));
438 } else if (value >= MEGABYTES(1)) {
439 fmt = precise ? "%.f MiB" : "%.3f MiB";
440 snprintf(str, size, fmt, TO_MEGABYTES(value));
441 } else if (value >= KILOBYTES(1)) {
442 fmt = precise ? "%.f KiB" : "%.3f KiB";
443 snprintf(str, size, fmt, TO_KILOBYTES(value));
445 snprintf(str, size, "%f bytes", value);
450 tsub(struct timeval t1, struct timeval t2)
452 t1.tv_usec -= t2.tv_usec;
453 if (t1.tv_usec < 0) {
454 t1.tv_usec += 1000000;
457 t1.tv_sec -= t2.tv_sec;
462 tdiv(double value, struct timeval tv)
464 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
467 #define HOURS(sec) ((sec) / (60 * 60))
468 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
469 #define SECONDS(sec) ((sec) % 60)
478 double usec = (double)tv->tv_usec / 1000000.0;
480 if (format & TERSE_FIXED_TIME) {
481 if (!HOURS(tv->tv_sec)) {
482 snprintf(ts, size, "%u:%02u.%02u",
483 (unsigned int) MINUTES(tv->tv_sec),
484 (unsigned int) SECONDS(tv->tv_sec),
485 (unsigned int) (usec * 100));
488 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
491 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
492 snprintf(ts, size, "%u:%02u:%02u.%02u",
493 (unsigned int) HOURS(tv->tv_sec),
494 (unsigned int) MINUTES(tv->tv_sec),
495 (unsigned int) SECONDS(tv->tv_sec),
496 (unsigned int) (usec * 100));
498 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
503 /* from libxcmd/quit.c */
505 static cmdinfo_t quit_cmd;
519 quit_cmd.name = _("quit");
520 quit_cmd.altname = _("q");
521 quit_cmd.cfunc = quit_f;
522 quit_cmd.argmin = -1;
523 quit_cmd.argmax = -1;
524 quit_cmd.flags = CMD_FLAG_GLOBAL;
525 quit_cmd.oneline = _("exit the program");
527 add_command(&quit_cmd);
530 /* from libxcmd/help.c */
532 static cmdinfo_t help_cmd;
533 static void help_onecmd(const char *cmd, const cmdinfo_t *ct);
534 static void help_oneline(const char *cmd, const cmdinfo_t *ct);
541 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++)
542 help_oneline(ct->name, ct);
543 printf(_("\nUse 'help commandname' for extended help.\n"));
557 ct = find_command(argv[1]);
559 printf(_("command %s not found\n"), argv[1]);
562 help_onecmd(argv[1], ct);
571 help_oneline(cmd, ct);
584 printf("%s ", ct->name);
586 printf("(or %s) ", ct->altname);
589 printf("%s ", ct->args);
590 printf("-- %s\n", ct->oneline);
596 help_cmd.name = _("help");
597 help_cmd.altname = _("?");
598 help_cmd.cfunc = help_f;
601 help_cmd.flags = CMD_FLAG_GLOBAL;
602 help_cmd.args = _("[command]");
603 help_cmd.oneline = _("help for one or all commands");
605 add_command(&help_cmd);