]>
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/>.
27 #include "block/aio.h"
28 #include "qemu/main-loop.h"
30 #define _(x) x /* not gettext support yet */
32 /* from libxcmd/command.c */
37 static argsfunc_t args_func
;
38 static checkfunc_t check_func
;
40 static char **cmdline
;
43 compare(const void *a
, const void *b
)
45 return strcmp(((const cmdinfo_t
*)a
)->name
,
46 ((const cmdinfo_t
*)b
)->name
);
49 void add_command(const cmdinfo_t
*ci
)
51 cmdtab
= g_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
;
124 void add_user_command(char *optarg
)
126 cmdline
= g_realloc(cmdline
, ++ncmdline
* sizeof(char *));
127 cmdline
[ncmdline
-1] = optarg
;
135 return args_func(index
);
146 static void prep_fetchline(void *opaque
)
148 int *fetchable
= opaque
;
150 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
154 static char *get_prompt(void);
156 void command_loop(void)
158 int c
, i
, j
= 0, done
= 0, fetchable
= 0, prompted
= 0;
163 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
164 input
= strdup(cmdline
[i
]);
166 fprintf(stderr
, _("cannot strdup command '%s': %s\n"),
167 cmdline
[i
], strerror(errno
));
170 v
= breakline(input
, &c
);
172 ct
= find_command(v
[0]);
174 if (ct
->flags
& CMD_FLAG_GLOBAL
) {
175 done
= command(ct
, c
, v
);
178 while (!done
&& (j
= args_command(j
))) {
179 done
= command(ct
, c
, v
);
183 fprintf(stderr
, _("command \"%s\" not found\n"), v
[0]);
195 printf("%s", get_prompt());
197 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
201 main_loop_wait(false);
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_set_fd_handler(STDIN_FILENO
, 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);
334 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
339 tmp
= 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 if (value
>= EXABYTES(1)) {
426 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
427 } else if (value
>= PETABYTES(1)) {
429 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
430 } else if (value
>= TERABYTES(1)) {
432 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
433 } else if (value
>= GIGABYTES(1)) {
435 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
436 } else if (value
>= MEGABYTES(1)) {
438 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
439 } else if (value
>= KILOBYTES(1)) {
441 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
444 snprintf(str
, size
- 6, "%f", value
);
447 trim
= strstr(str
, ".000");
449 strcpy(trim
, suffix
);
456 tsub(struct timeval t1
, struct timeval t2
)
458 t1
.tv_usec
-= t2
.tv_usec
;
459 if (t1
.tv_usec
< 0) {
460 t1
.tv_usec
+= 1000000;
463 t1
.tv_sec
-= t2
.tv_sec
;
468 tdiv(double value
, struct timeval tv
)
470 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
473 #define HOURS(sec) ((sec) / (60 * 60))
474 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
475 #define SECONDS(sec) ((sec) % 60)
484 double usec
= (double)tv
->tv_usec
/ 1000000.0;
486 if (format
& TERSE_FIXED_TIME
) {
487 if (!HOURS(tv
->tv_sec
)) {
488 snprintf(ts
, size
, "%u:%02u.%02u",
489 (unsigned int) MINUTES(tv
->tv_sec
),
490 (unsigned int) SECONDS(tv
->tv_sec
),
491 (unsigned int) (usec
* 100));
494 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
497 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
498 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
499 (unsigned int) HOURS(tv
->tv_sec
),
500 (unsigned int) MINUTES(tv
->tv_sec
),
501 (unsigned int) SECONDS(tv
->tv_sec
),
502 (unsigned int) (usec
* 100));
504 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
509 /* from libxcmd/quit.c */
511 static cmdinfo_t quit_cmd
;
525 quit_cmd
.name
= _("quit");
526 quit_cmd
.altname
= _("q");
527 quit_cmd
.cfunc
= quit_f
;
528 quit_cmd
.argmin
= -1;
529 quit_cmd
.argmax
= -1;
530 quit_cmd
.flags
= CMD_FLAG_GLOBAL
;
531 quit_cmd
.oneline
= _("exit the program");
533 add_command(&quit_cmd
);
536 /* from libxcmd/help.c */
538 static cmdinfo_t help_cmd
;
539 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
);
540 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
);
547 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++)
548 help_oneline(ct
->name
, ct
);
549 printf(_("\nUse 'help commandname' for extended help.\n"));
563 ct
= find_command(argv
[1]);
565 printf(_("command %s not found\n"), argv
[1]);
568 help_onecmd(argv
[1], ct
);
577 help_oneline(cmd
, ct
);
590 printf("%s ", ct
->name
);
592 printf("(or %s) ", ct
->altname
);
595 printf("%s ", ct
->args
);
596 printf("-- %s\n", ct
->oneline
);
602 help_cmd
.name
= _("help");
603 help_cmd
.altname
= _("?");
604 help_cmd
.cfunc
= help_f
;
607 help_cmd
.flags
= CMD_FLAG_GLOBAL
;
608 help_cmd
.args
= _("[command]");
609 help_cmd
.oneline
= _("help for one or all commands");
611 add_command(&help_cmd
);