tcg-i386: fix a typo
[qemu.git] / cmd.c
blob2336334eea8597caf4bf34b3e5b84c8e6d729c0d
1 /*
2 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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/>.
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <sys/time.h>
24 #include <getopt.h>
26 #include "cmd.h"
28 #define _(x) x /* not gettext support yet */
30 /* from libxcmd/command.c */
32 cmdinfo_t *cmdtab;
33 int ncmds;
35 static argsfunc_t args_func;
36 static checkfunc_t check_func;
37 static int ncmdline;
38 static char **cmdline;
40 static int
41 compare(const void *a, const void *b)
43 return strcmp(((const cmdinfo_t *)a)->name,
44 ((const cmdinfo_t *)b)->name);
47 void
48 add_command(
49 const cmdinfo_t *ci)
51 cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
52 cmdtab[ncmds - 1] = *ci;
53 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
56 static int
57 check_command(
58 const cmdinfo_t *ci)
60 if (check_func)
61 return check_func(ci);
62 return 1;
65 void
66 add_check_command(
67 checkfunc_t cf)
69 check_func = cf;
72 int
73 command_usage(
74 const cmdinfo_t *ci)
76 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
77 return 0;
80 int
81 command(
82 const cmdinfo_t *ct,
83 int argc,
84 char **argv)
86 char *cmd = argv[0];
88 if (!check_command(ct))
89 return 0;
91 if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
92 if (ct->argmax == -1)
93 fprintf(stderr,
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)
97 fprintf(stderr,
98 _("bad argument count %d to %s, expected %d arguments\n"),
99 argc-1, cmd, ct->argmin);
100 else
101 fprintf(stderr,
102 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
103 argc-1, cmd, ct->argmin, ct->argmax);
104 return 0;
106 optind = 0;
107 return ct->cfunc(argc, argv);
110 const cmdinfo_t *
111 find_command(
112 const char *cmd)
114 cmdinfo_t *ct;
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;
121 return NULL;
124 void
125 add_user_command(char *optarg)
127 ncmdline++;
128 cmdline = realloc(cmdline, sizeof(char*) * (ncmdline));
129 if (!cmdline) {
130 perror("realloc");
131 exit(1);
133 cmdline[ncmdline-1] = optarg;
136 static int
137 args_command(
138 int index)
140 if (args_func)
141 return args_func(index);
142 return 0;
145 void
146 add_args_command(
147 argsfunc_t af)
149 args_func = af;
152 void
153 command_loop(void)
155 int c, i, j = 0, done = 0;
156 char *input;
157 char **v;
158 const cmdinfo_t *ct;
160 for (i = 0; !done && i < ncmdline; i++) {
161 input = strdup(cmdline[i]);
162 if (!input) {
163 fprintf(stderr,
164 _("cannot strdup command '%s': %s\n"),
165 cmdline[i], strerror(errno));
166 exit(1);
168 v = breakline(input, &c);
169 if (c) {
170 ct = find_command(v[0]);
171 if (ct) {
172 if (ct->flags & CMD_FLAG_GLOBAL)
173 done = command(ct, c, v);
174 else {
175 j = 0;
176 while (!done && (j = args_command(j)))
177 done = command(ct, c, v);
179 } else
180 fprintf(stderr, _("command \"%s\" not found\n"),
181 v[0]);
183 doneline(input, v);
185 if (cmdline) {
186 free(cmdline);
187 return;
189 while (!done) {
190 if ((input = fetchline()) == NULL)
191 break;
192 v = breakline(input, &c);
193 if (c) {
194 ct = find_command(v[0]);
195 if (ct)
196 done = command(ct, c, v);
197 else
198 fprintf(stderr, _("command \"%s\" not found\n"),
199 v[0]);
201 doneline(input, v);
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>
212 #endif
214 static char *
215 get_prompt(void)
217 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
219 if (!prompt[0])
220 snprintf(prompt, sizeof(prompt), "%s> ", progname);
221 return prompt;
224 #if defined(ENABLE_READLINE)
225 char *
226 fetchline(void)
228 char *line;
230 line = readline(get_prompt());
231 if (line && *line)
232 add_history(line);
233 return line;
235 #elif defined(ENABLE_EDITLINE)
236 static char *el_get_prompt(EditLine *e) { return get_prompt(); }
237 char *
238 fetchline(void)
240 static EditLine *el;
241 static History *hist;
242 HistEvent hevent;
243 char *line;
244 int count;
246 if (!el) {
247 hist = history_init();
248 history(hist, &hevent, H_SETSIZE, 100);
249 el = el_init(progname, stdin, stdout, stderr);
250 el_source(el, NULL);
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));
256 if (line) {
257 if (count > 0)
258 line[count-1] = '\0';
259 if (*line)
260 history(hist, &hevent, H_ENTER, line);
262 return line;
264 #else
265 # define MAXREADLINESZ 1024
266 char *
267 fetchline(void)
269 char *p, *line = malloc(MAXREADLINESZ);
271 if (!line)
272 return NULL;
273 printf("%s", get_prompt());
274 fflush(stdout);
275 if (!fgets(line, MAXREADLINESZ, stdin)) {
276 free(line);
277 return NULL;
279 p = line + strlen(line);
280 if (p != line && p[-1] == '\n')
281 p[-1] = '\0';
282 return line;
284 #endif
286 static char *qemu_strsep(char **input, const char *delim)
288 char *result = *input;
289 if (result != NULL) {
290 char *p;
292 for (p = result; *p != '\0'; p++) {
293 if (strchr(delim, *p)) {
294 break;
297 if (*p == '\0') {
298 *input = NULL;
299 } else {
300 *p = '\0';
301 *input = p + 1;
304 return result;
307 char **
308 breakline(
309 char *input,
310 int *count)
312 int c = 0;
313 char *p;
314 char **rval = calloc(sizeof(char *), 1);
316 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
317 if (!*p)
318 continue;
319 c++;
320 rval = realloc(rval, sizeof(*rval) * (c + 1));
321 if (!rval) {
322 c = 0;
323 break;
325 rval[c - 1] = p;
326 rval[c] = NULL;
328 *count = c;
329 return rval;
332 void
333 doneline(
334 char *input,
335 char **vec)
337 free(input);
338 free(vec);
341 #define EXABYTES(x) ((long long)(x) << 60)
342 #define PETABYTES(x) ((long long)(x) << 50)
343 #define TERABYTES(x) ((long long)(x) << 40)
344 #define GIGABYTES(x) ((long long)(x) << 30)
345 #define MEGABYTES(x) ((long long)(x) << 20)
346 #define KILOBYTES(x) ((long long)(x) << 10)
348 long long
349 cvtnum(
350 char *s)
352 long long i;
353 char *sp;
354 int c;
356 i = strtoll(s, &sp, 0);
357 if (i == 0 && sp == s)
358 return -1LL;
359 if (*sp == '\0')
360 return i;
362 if (sp[1] != '\0')
363 return -1LL;
365 c = tolower(*sp);
366 switch (c) {
367 default:
368 return i;
369 case 'k':
370 return KILOBYTES(i);
371 case 'm':
372 return MEGABYTES(i);
373 case 'g':
374 return GIGABYTES(i);
375 case 't':
376 return TERABYTES(i);
377 case 'p':
378 return PETABYTES(i);
379 case 'e':
380 return EXABYTES(i);
382 return -1LL;
385 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
386 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
387 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
388 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
389 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
390 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
392 void
393 cvtstr(
394 double value,
395 char *str,
396 size_t size)
398 const char *fmt;
399 int precise;
401 precise = ((double)value * 1000 == (double)(int)value * 1000);
403 if (value >= EXABYTES(1)) {
404 fmt = precise ? "%.f EiB" : "%.3f EiB";
405 snprintf(str, size, fmt, TO_EXABYTES(value));
406 } else if (value >= PETABYTES(1)) {
407 fmt = precise ? "%.f PiB" : "%.3f PiB";
408 snprintf(str, size, fmt, TO_PETABYTES(value));
409 } else if (value >= TERABYTES(1)) {
410 fmt = precise ? "%.f TiB" : "%.3f TiB";
411 snprintf(str, size, fmt, TO_TERABYTES(value));
412 } else if (value >= GIGABYTES(1)) {
413 fmt = precise ? "%.f GiB" : "%.3f GiB";
414 snprintf(str, size, fmt, TO_GIGABYTES(value));
415 } else if (value >= MEGABYTES(1)) {
416 fmt = precise ? "%.f MiB" : "%.3f MiB";
417 snprintf(str, size, fmt, TO_MEGABYTES(value));
418 } else if (value >= KILOBYTES(1)) {
419 fmt = precise ? "%.f KiB" : "%.3f KiB";
420 snprintf(str, size, fmt, TO_KILOBYTES(value));
421 } else {
422 snprintf(str, size, "%f bytes", value);
426 struct timeval
427 tsub(struct timeval t1, struct timeval t2)
429 t1.tv_usec -= t2.tv_usec;
430 if (t1.tv_usec < 0) {
431 t1.tv_usec += 1000000;
432 t1.tv_sec--;
434 t1.tv_sec -= t2.tv_sec;
435 return t1;
438 double
439 tdiv(double value, struct timeval tv)
441 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
444 #define HOURS(sec) ((sec) / (60 * 60))
445 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
446 #define SECONDS(sec) ((sec) % 60)
448 void
449 timestr(
450 struct timeval *tv,
451 char *ts,
452 size_t size,
453 int format)
455 double usec = (double)tv->tv_usec / 1000000.0;
457 if (format & TERSE_FIXED_TIME) {
458 if (!HOURS(tv->tv_sec)) {
459 snprintf(ts, size, "%u:%02u.%02u",
460 (unsigned int) MINUTES(tv->tv_sec),
461 (unsigned int) SECONDS(tv->tv_sec),
462 (unsigned int) usec * 100);
463 return;
465 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
468 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
469 snprintf(ts, size, "%u:%02u:%02u.%02u",
470 (unsigned int) HOURS(tv->tv_sec),
471 (unsigned int) MINUTES(tv->tv_sec),
472 (unsigned int) SECONDS(tv->tv_sec),
473 (unsigned int) usec * 100);
474 } else {
475 snprintf(ts, size, "0.%04u sec", (unsigned int) usec * 10000);
480 /* from libxcmd/quit.c */
482 static cmdinfo_t quit_cmd;
484 /* ARGSUSED */
485 static int
486 quit_f(
487 int argc,
488 char **argv)
490 return 1;
493 void
494 quit_init(void)
496 quit_cmd.name = _("quit");
497 quit_cmd.altname = _("q");
498 quit_cmd.cfunc = quit_f;
499 quit_cmd.argmin = -1;
500 quit_cmd.argmax = -1;
501 quit_cmd.flags = CMD_FLAG_GLOBAL;
502 quit_cmd.oneline = _("exit the program");
504 add_command(&quit_cmd);
507 /* from libxcmd/help.c */
509 static cmdinfo_t help_cmd;
510 static void help_onecmd(const char *cmd, const cmdinfo_t *ct);
511 static void help_oneline(const char *cmd, const cmdinfo_t *ct);
513 static void
514 help_all(void)
516 const cmdinfo_t *ct;
518 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++)
519 help_oneline(ct->name, ct);
520 printf(_("\nUse 'help commandname' for extended help.\n"));
523 static int
524 help_f(
525 int argc,
526 char **argv)
528 const cmdinfo_t *ct;
530 if (argc == 1) {
531 help_all();
532 return 0;
534 ct = find_command(argv[1]);
535 if (ct == NULL) {
536 printf(_("command %s not found\n"), argv[1]);
537 return 0;
539 help_onecmd(argv[1], ct);
540 return 0;
543 static void
544 help_onecmd(
545 const char *cmd,
546 const cmdinfo_t *ct)
548 help_oneline(cmd, ct);
549 if (ct->help)
550 ct->help();
553 static void
554 help_oneline(
555 const char *cmd,
556 const cmdinfo_t *ct)
558 if (cmd)
559 printf("%s ", cmd);
560 else {
561 printf("%s ", ct->name);
562 if (ct->altname)
563 printf("(or %s) ", ct->altname);
565 if (ct->args)
566 printf("%s ", ct->args);
567 printf("-- %s\n", ct->oneline);
570 void
571 help_init(void)
573 help_cmd.name = _("help");
574 help_cmd.altname = _("?");
575 help_cmd.cfunc = help_f;
576 help_cmd.argmin = 0;
577 help_cmd.argmax = 1;
578 help_cmd.flags = CMD_FLAG_GLOBAL;
579 help_cmd.args = _("[command]");
580 help_cmd.oneline = _("help for one or all commands");
582 add_command(&help_cmd);