]>
git.proxmox.com Git - qemu.git/blob - cmd.c
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
= g_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
)
125 cmdline
= g_realloc(cmdline
, ++ncmdline
* sizeof(char *));
126 cmdline
[ncmdline
-1] = optarg
;
134 return args_func(index
);
145 static void prep_fetchline(void *opaque
)
147 int *fetchable
= opaque
;
149 qemu_aio_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
, NULL
, NULL
);
153 static char *get_prompt(void);
155 void command_loop(void)
157 int c
, i
, j
= 0, done
= 0, fetchable
= 0, prompted
= 0;
162 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
163 input
= strdup(cmdline
[i
]);
165 fprintf(stderr
, _("cannot strdup command '%s': %s\n"),
166 cmdline
[i
], strerror(errno
));
169 v
= breakline(input
, &c
);
171 ct
= find_command(v
[0]);
173 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
174 done
= command(ct
, c
, v
);
177 while (!done
&& (j
= args_command(j
))) {
178 done
= command(ct
, c
, v
);
182 fprintf(stderr
, _("command \"%s\" not found\n"), v
[0]);
194 printf("%s", get_prompt());
196 qemu_aio_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, NULL
,
210 v
= breakline(input
, &c
);
212 ct
= find_command(v
[0]);
214 done
= command(ct
, c
, v
);
216 fprintf(stderr
, _("command \"%s\" not found\n"), v
[0]);
224 qemu_aio_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
, NULL
, NULL
);
227 /* from libxcmd/input.c */
229 #if defined(ENABLE_READLINE)
230 # include <readline/history.h>
231 # include <readline/readline.h>
232 #elif defined(ENABLE_EDITLINE)
233 # include <histedit.h>
239 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
242 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
246 #if defined(ENABLE_READLINE)
252 line
= readline(get_prompt());
257 #elif defined(ENABLE_EDITLINE)
258 static char *el_get_prompt(EditLine
*e
) { return get_prompt(); }
263 static History
*hist
;
269 hist
= history_init();
270 history(hist
, &hevent
, H_SETSIZE
, 100);
271 el
= el_init(progname
, stdin
, stdout
, stderr
);
273 el_set(el
, EL_SIGNAL
, 1);
274 el_set(el
, EL_PROMPT
, el_get_prompt
);
275 el_set(el
, EL_HIST
, history
, (const char *)hist
);
277 line
= strdup(el_gets(el
, &count
));
280 line
[count
-1] = '\0';
282 history(hist
, &hevent
, H_ENTER
, line
);
287 # define MAXREADLINESZ 1024
291 char *p
, *line
= malloc(MAXREADLINESZ
);
295 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
299 p
= line
+ strlen(line
);
300 if (p
!= line
&& p
[-1] == '\n')
306 static char *qemu_strsep(char **input
, const char *delim
)
308 char *result
= *input
;
309 if (result
!= NULL
) {
312 for (p
= result
; *p
!= '\0'; p
++) {
313 if (strchr(delim
, *p
)) {
327 char **breakline(char *input
, int *count
)
331 char **rval
= calloc(sizeof(char *), 1);
333 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
338 rval
= realloc(rval
, sizeof(*rval
) * (c
+ 1));
359 #define EXABYTES(x) ((long long)(x) << 60)
360 #define PETABYTES(x) ((long long)(x) << 50)
361 #define TERABYTES(x) ((long long)(x) << 40)
362 #define GIGABYTES(x) ((long long)(x) << 30)
363 #define MEGABYTES(x) ((long long)(x) << 20)
364 #define KILOBYTES(x) ((long long)(x) << 10)
374 i
= strtoll(s
, &sp
, 0);
375 if (i
== 0 && sp
== s
)
383 c
= qemu_tolower(*sp
);
403 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
404 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
405 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
406 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
407 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
408 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
419 precise
= ((double)value
* 1000 == (double)(int)value
* 1000);
421 if (value
>= EXABYTES(1)) {
422 fmt
= precise
? "%.f EiB" : "%.3f EiB";
423 snprintf(str
, size
, fmt
, TO_EXABYTES(value
));
424 } else if (value
>= PETABYTES(1)) {
425 fmt
= precise
? "%.f PiB" : "%.3f PiB";
426 snprintf(str
, size
, fmt
, TO_PETABYTES(value
));
427 } else if (value
>= TERABYTES(1)) {
428 fmt
= precise
? "%.f TiB" : "%.3f TiB";
429 snprintf(str
, size
, fmt
, TO_TERABYTES(value
));
430 } else if (value
>= GIGABYTES(1)) {
431 fmt
= precise
? "%.f GiB" : "%.3f GiB";
432 snprintf(str
, size
, fmt
, TO_GIGABYTES(value
));
433 } else if (value
>= MEGABYTES(1)) {
434 fmt
= precise
? "%.f MiB" : "%.3f MiB";
435 snprintf(str
, size
, fmt
, TO_MEGABYTES(value
));
436 } else if (value
>= KILOBYTES(1)) {
437 fmt
= precise
? "%.f KiB" : "%.3f KiB";
438 snprintf(str
, size
, fmt
, TO_KILOBYTES(value
));
440 snprintf(str
, size
, "%f bytes", value
);
445 tsub(struct timeval t1
, struct timeval t2
)
447 t1
.tv_usec
-= t2
.tv_usec
;
448 if (t1
.tv_usec
< 0) {
449 t1
.tv_usec
+= 1000000;
452 t1
.tv_sec
-= t2
.tv_sec
;
457 tdiv(double value
, struct timeval tv
)
459 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
462 #define HOURS(sec) ((sec) / (60 * 60))
463 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
464 #define SECONDS(sec) ((sec) % 60)
473 double usec
= (double)tv
->tv_usec
/ 1000000.0;
475 if (format
& TERSE_FIXED_TIME
) {
476 if (!HOURS(tv
->tv_sec
)) {
477 snprintf(ts
, size
, "%u:%02u.%02u",
478 (unsigned int) MINUTES(tv
->tv_sec
),
479 (unsigned int) SECONDS(tv
->tv_sec
),
480 (unsigned int) (usec
* 100));
483 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
486 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
487 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
488 (unsigned int) HOURS(tv
->tv_sec
),
489 (unsigned int) MINUTES(tv
->tv_sec
),
490 (unsigned int) SECONDS(tv
->tv_sec
),
491 (unsigned int) (usec
* 100));
493 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
498 /* from libxcmd/quit.c */
500 static cmdinfo_t quit_cmd
;
514 quit_cmd
.name
= _("quit");
515 quit_cmd
.altname
= _("q");
516 quit_cmd
.cfunc
= quit_f
;
517 quit_cmd
.argmin
= -1;
518 quit_cmd
.argmax
= -1;
519 quit_cmd
.flags
= CMD_FLAG_GLOBAL
;
520 quit_cmd
.oneline
= _("exit the program");
522 add_command(&quit_cmd
);
525 /* from libxcmd/help.c */
527 static cmdinfo_t help_cmd
;
528 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
);
529 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
);
536 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++)
537 help_oneline(ct
->name
, ct
);
538 printf(_("\nUse 'help commandname' for extended help.\n"));
552 ct
= find_command(argv
[1]);
554 printf(_("command %s not found\n"), argv
[1]);
557 help_onecmd(argv
[1], ct
);
566 help_oneline(cmd
, ct
);
579 printf("%s ", ct
->name
);
581 printf("(or %s) ", ct
->altname
);
584 printf("%s ", ct
->args
);
585 printf("-- %s\n", ct
->oneline
);
591 help_cmd
.name
= _("help");
592 help_cmd
.altname
= _("?");
593 help_cmd
.cfunc
= help_f
;
596 help_cmd
.flags
= CMD_FLAG_GLOBAL
;
597 help_cmd
.args
= _("[command]");
598 help_cmd
.oneline
= _("help for one or all commands");
600 add_command(&help_cmd
);