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);
52 cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
53 cmdtab[ncmds - 1] = *ci;
54 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
62 return check_func(ci);
77 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
89 if (!check_command(ct))
92 if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
95 _("bad argument count %d to %s, expected at least %d arguments\n"),
96 argc-1, cmd, ct->argmin);
97 else if (ct->argmin == ct->argmax)
99 _("bad argument count %d to %s, expected %d arguments\n"),
100 argc-1, cmd, ct->argmin);
103 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
104 argc-1, cmd, ct->argmin, ct->argmax);
108 return ct->cfunc(argc, argv);
117 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
118 if (strcmp(ct->name, cmd) == 0 ||
119 (ct->altname && strcmp(ct->altname, cmd) == 0))
120 return (const cmdinfo_t *)ct;
126 add_user_command(char *optarg)
129 cmdline = realloc(cmdline, sizeof(char*) * (ncmdline));
134 cmdline[ncmdline-1] = optarg;
142 return args_func(index);
153 static void prep_fetchline(void *opaque)
155 int *fetchable = opaque;
157 qemu_aio_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL, NULL, NULL);
161 static char *get_prompt(void);
166 int c, i, j = 0, done = 0, fetchable = 0, prompted = 0;
171 for (i = 0; !done && i < ncmdline; i++) {
172 input = strdup(cmdline[i]);
175 _("cannot strdup command '%s': %s\n"),
176 cmdline[i], strerror(errno));
179 v = breakline(input, &c);
181 ct = find_command(v[0]);
183 if (ct->flags & CMD_FLAG_GLOBAL)
184 done = command(ct, c, v);
187 while (!done && (j = args_command(j)))
188 done = command(ct, c, v);
191 fprintf(stderr, _("command \"%s\" not found\n"),
203 printf("%s", get_prompt());
205 qemu_aio_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, NULL,
215 if ((input = fetchline()) == NULL)
217 v = breakline(input, &c);
219 ct = find_command(v[0]);
221 done = command(ct, c, v);
223 fprintf(stderr, _("command \"%s\" not found\n"),
231 qemu_aio_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL, NULL, NULL);
234 /* from libxcmd/input.c */
236 #if defined(ENABLE_READLINE)
237 # include <readline/history.h>
238 # include <readline/readline.h>
239 #elif defined(ENABLE_EDITLINE)
240 # include <histedit.h>
246 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
249 snprintf(prompt, sizeof(prompt), "%s> ", progname);
253 #if defined(ENABLE_READLINE)
259 line = readline(get_prompt());
264 #elif defined(ENABLE_EDITLINE)
265 static char *el_get_prompt(EditLine *e) { return get_prompt(); }
270 static History *hist;
276 hist = history_init();
277 history(hist, &hevent, H_SETSIZE, 100);
278 el = el_init(progname, stdin, stdout, stderr);
280 el_set(el, EL_SIGNAL, 1);
281 el_set(el, EL_PROMPT, el_get_prompt);
282 el_set(el, EL_HIST, history, (const char *)hist);
284 line = strdup(el_gets(el, &count));
287 line[count-1] = '\0';
289 history(hist, &hevent, H_ENTER, line);
294 # define MAXREADLINESZ 1024
298 char *p, *line = malloc(MAXREADLINESZ);
302 if (!fgets(line, MAXREADLINESZ, stdin)) {
306 p = line + strlen(line);
307 if (p != line && p[-1] == '\n')
313 static char *qemu_strsep(char **input, const char *delim)
315 char *result = *input;
316 if (result != NULL) {
319 for (p = result; *p != '\0'; p++) {
320 if (strchr(delim, *p)) {
341 char **rval = calloc(sizeof(char *), 1);
343 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
347 rval = realloc(rval, sizeof(*rval) * (c + 1));
368 #define EXABYTES(x) ((long long)(x) << 60)
369 #define PETABYTES(x) ((long long)(x) << 50)
370 #define TERABYTES(x) ((long long)(x) << 40)
371 #define GIGABYTES(x) ((long long)(x) << 30)
372 #define MEGABYTES(x) ((long long)(x) << 20)
373 #define KILOBYTES(x) ((long long)(x) << 10)
383 i = strtoll(s, &sp, 0);
384 if (i == 0 && sp == s)
412 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
413 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
414 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
415 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
416 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
417 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
428 precise = ((double)value * 1000 == (double)(int)value * 1000);
430 if (value >= EXABYTES(1)) {
431 fmt = precise ? "%.f EiB" : "%.3f EiB";
432 snprintf(str, size, fmt, TO_EXABYTES(value));
433 } else if (value >= PETABYTES(1)) {
434 fmt = precise ? "%.f PiB" : "%.3f PiB";
435 snprintf(str, size, fmt, TO_PETABYTES(value));
436 } else if (value >= TERABYTES(1)) {
437 fmt = precise ? "%.f TiB" : "%.3f TiB";
438 snprintf(str, size, fmt, TO_TERABYTES(value));
439 } else if (value >= GIGABYTES(1)) {
440 fmt = precise ? "%.f GiB" : "%.3f GiB";
441 snprintf(str, size, fmt, TO_GIGABYTES(value));
442 } else if (value >= MEGABYTES(1)) {
443 fmt = precise ? "%.f MiB" : "%.3f MiB";
444 snprintf(str, size, fmt, TO_MEGABYTES(value));
445 } else if (value >= KILOBYTES(1)) {
446 fmt = precise ? "%.f KiB" : "%.3f KiB";
447 snprintf(str, size, fmt, TO_KILOBYTES(value));
449 snprintf(str, size, "%f bytes", value);
454 tsub(struct timeval t1, struct timeval t2)
456 t1.tv_usec -= t2.tv_usec;
457 if (t1.tv_usec < 0) {
458 t1.tv_usec += 1000000;
461 t1.tv_sec -= t2.tv_sec;
466 tdiv(double value, struct timeval tv)
468 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
471 #define HOURS(sec) ((sec) / (60 * 60))
472 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
473 #define SECONDS(sec) ((sec) % 60)
482 double usec = (double)tv->tv_usec / 1000000.0;
484 if (format & TERSE_FIXED_TIME) {
485 if (!HOURS(tv->tv_sec)) {
486 snprintf(ts, size, "%u:%02u.%02u",
487 (unsigned int) MINUTES(tv->tv_sec),
488 (unsigned int) SECONDS(tv->tv_sec),
489 (unsigned int) usec * 100);
492 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
495 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
496 snprintf(ts, size, "%u:%02u:%02u.%02u",
497 (unsigned int) HOURS(tv->tv_sec),
498 (unsigned int) MINUTES(tv->tv_sec),
499 (unsigned int) SECONDS(tv->tv_sec),
500 (unsigned int) usec * 100);
502 snprintf(ts, size, "0.%04u sec", (unsigned int) usec * 10000);
507 /* from libxcmd/quit.c */
509 static cmdinfo_t quit_cmd;
523 quit_cmd.name = _("quit");
524 quit_cmd.altname = _("q");
525 quit_cmd.cfunc = quit_f;
526 quit_cmd.argmin = -1;
527 quit_cmd.argmax = -1;
528 quit_cmd.flags = CMD_FLAG_GLOBAL;
529 quit_cmd.oneline = _("exit the program");
531 add_command(&quit_cmd);
534 /* from libxcmd/help.c */
536 static cmdinfo_t help_cmd;
537 static void help_onecmd(const char *cmd, const cmdinfo_t *ct);
538 static void help_oneline(const char *cmd, const cmdinfo_t *ct);
545 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++)
546 help_oneline(ct->name, ct);
547 printf(_("\nUse 'help commandname' for extended help.\n"));
561 ct = find_command(argv[1]);
563 printf(_("command %s not found\n"), argv[1]);
566 help_onecmd(argv[1], ct);
575 help_oneline(cmd, ct);
588 printf("%s ", ct->name);
590 printf("(or %s) ", ct->altname);
593 printf("%s ", ct->args);
594 printf("-- %s\n", ct->oneline);
600 help_cmd.name = _("help");
601 help_cmd.altname = _("?");
602 help_cmd.cfunc = help_f;
605 help_cmd.flags = CMD_FLAG_GLOBAL;
606 help_cmd.args = _("[command]");
607 help_cmd.oneline = _("help for one or all commands");
609 add_command(&help_cmd);