]>
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 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(qemuio_bs
, 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(qemuio_bs
, 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
;
129 static void prep_fetchline(void *opaque
)
131 int *fetchable
= opaque
;
133 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
137 static char *get_prompt(void);
139 void command_loop(void)
141 int i
, done
= 0, fetchable
= 0, prompted
= 0;
144 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
145 done
= qemuio_command(cmdline
[i
]);
154 printf("%s", get_prompt());
156 qemu_set_fd_handler(STDIN_FILENO
, prep_fetchline
, NULL
, &fetchable
);
160 main_loop_wait(false);
170 done
= qemuio_command(input
);
176 qemu_set_fd_handler(STDIN_FILENO
, NULL
, NULL
, NULL
);
179 /* from libxcmd/input.c */
181 #if defined(ENABLE_READLINE)
182 # include <readline/history.h>
183 # include <readline/readline.h>
184 #elif defined(ENABLE_EDITLINE)
185 # include <histedit.h>
191 static char prompt
[FILENAME_MAX
+ 2 /*"> "*/ + 1 /*"\0"*/ ];
194 snprintf(prompt
, sizeof(prompt
), "%s> ", progname
);
198 #if defined(ENABLE_READLINE)
204 line
= readline(get_prompt());
209 #elif defined(ENABLE_EDITLINE)
210 static char *el_get_prompt(EditLine
*e
) { return get_prompt(); }
215 static History
*hist
;
221 hist
= history_init();
222 history(hist
, &hevent
, H_SETSIZE
, 100);
223 el
= el_init(progname
, stdin
, stdout
, stderr
);
225 el_set(el
, EL_SIGNAL
, 1);
226 el_set(el
, EL_PROMPT
, el_get_prompt
);
227 el_set(el
, EL_HIST
, history
, (const char *)hist
);
229 line
= strdup(el_gets(el
, &count
));
232 line
[count
-1] = '\0';
234 history(hist
, &hevent
, H_ENTER
, line
);
239 # define MAXREADLINESZ 1024
243 char *p
, *line
= malloc(MAXREADLINESZ
);
247 if (!fgets(line
, MAXREADLINESZ
, stdin
)) {
251 p
= line
+ strlen(line
);
252 if (p
!= line
&& p
[-1] == '\n')
258 static char *qemu_strsep(char **input
, const char *delim
)
260 char *result
= *input
;
261 if (result
!= NULL
) {
264 for (p
= result
; *p
!= '\0'; p
++) {
265 if (strchr(delim
, *p
)) {
279 char **breakline(char *input
, int *count
)
283 char **rval
= calloc(sizeof(char *), 1);
286 while (rval
&& (p
= qemu_strsep(&input
, " ")) != NULL
) {
291 tmp
= realloc(rval
, sizeof(*rval
) * (c
+ 1));
307 #define EXABYTES(x) ((long long)(x) << 60)
308 #define PETABYTES(x) ((long long)(x) << 50)
309 #define TERABYTES(x) ((long long)(x) << 40)
310 #define GIGABYTES(x) ((long long)(x) << 30)
311 #define MEGABYTES(x) ((long long)(x) << 20)
312 #define KILOBYTES(x) ((long long)(x) << 10)
314 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
315 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
316 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
317 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
318 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
319 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
330 if (value
>= EXABYTES(1)) {
332 snprintf(str
, size
- 4, "%.3f", TO_EXABYTES(value
));
333 } else if (value
>= PETABYTES(1)) {
335 snprintf(str
, size
- 4, "%.3f", TO_PETABYTES(value
));
336 } else if (value
>= TERABYTES(1)) {
338 snprintf(str
, size
- 4, "%.3f", TO_TERABYTES(value
));
339 } else if (value
>= GIGABYTES(1)) {
341 snprintf(str
, size
- 4, "%.3f", TO_GIGABYTES(value
));
342 } else if (value
>= MEGABYTES(1)) {
344 snprintf(str
, size
- 4, "%.3f", TO_MEGABYTES(value
));
345 } else if (value
>= KILOBYTES(1)) {
347 snprintf(str
, size
- 4, "%.3f", TO_KILOBYTES(value
));
350 snprintf(str
, size
- 6, "%f", value
);
353 trim
= strstr(str
, ".000");
355 strcpy(trim
, suffix
);
362 tsub(struct timeval t1
, struct timeval t2
)
364 t1
.tv_usec
-= t2
.tv_usec
;
365 if (t1
.tv_usec
< 0) {
366 t1
.tv_usec
+= 1000000;
369 t1
.tv_sec
-= t2
.tv_sec
;
374 tdiv(double value
, struct timeval tv
)
376 return value
/ ((double)tv
.tv_sec
+ ((double)tv
.tv_usec
/ 1000000.0));
379 #define HOURS(sec) ((sec) / (60 * 60))
380 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
381 #define SECONDS(sec) ((sec) % 60)
390 double usec
= (double)tv
->tv_usec
/ 1000000.0;
392 if (format
& TERSE_FIXED_TIME
) {
393 if (!HOURS(tv
->tv_sec
)) {
394 snprintf(ts
, size
, "%u:%02u.%02u",
395 (unsigned int) MINUTES(tv
->tv_sec
),
396 (unsigned int) SECONDS(tv
->tv_sec
),
397 (unsigned int) (usec
* 100));
400 format
|= VERBOSE_FIXED_TIME
; /* fallback if hours needed */
403 if ((format
& VERBOSE_FIXED_TIME
) || tv
->tv_sec
) {
404 snprintf(ts
, size
, "%u:%02u:%02u.%02u",
405 (unsigned int) HOURS(tv
->tv_sec
),
406 (unsigned int) MINUTES(tv
->tv_sec
),
407 (unsigned int) SECONDS(tv
->tv_sec
),
408 (unsigned int) (usec
* 100));
410 snprintf(ts
, size
, "0.%04u sec", (unsigned int) (usec
* 10000));
415 /* from libxcmd/quit.c */
417 static cmdinfo_t quit_cmd
;
422 BlockDriverState
*bs
,
432 quit_cmd
.name
= _("quit");
433 quit_cmd
.altname
= _("q");
434 quit_cmd
.cfunc
= quit_f
;
435 quit_cmd
.argmin
= -1;
436 quit_cmd
.argmax
= -1;
437 quit_cmd
.flags
= CMD_FLAG_GLOBAL
;
438 quit_cmd
.oneline
= _("exit the program");
440 add_command(&quit_cmd
);
443 /* from libxcmd/help.c */
445 static cmdinfo_t help_cmd
;
446 static void help_onecmd(const char *cmd
, const cmdinfo_t
*ct
);
447 static void help_oneline(const char *cmd
, const cmdinfo_t
*ct
);
454 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++)
455 help_oneline(ct
->name
, ct
);
456 printf(_("\nUse 'help commandname' for extended help.\n"));
461 BlockDriverState
*bs
,
471 ct
= find_command(argv
[1]);
473 printf(_("command %s not found\n"), argv
[1]);
476 help_onecmd(argv
[1], ct
);
485 help_oneline(cmd
, ct
);
498 printf("%s ", ct
->name
);
500 printf("(or %s) ", ct
->altname
);
503 printf("%s ", ct
->args
);
504 printf("-- %s\n", ct
->oneline
);
510 help_cmd
.name
= _("help");
511 help_cmd
.altname
= _("?");
512 help_cmd
.cfunc
= help_f
;
515 help_cmd
.flags
= CMD_FLAG_GLOBAL
;
516 help_cmd
.args
= _("[command]");
517 help_cmd
.oneline
= _("help for one or all commands");
519 add_command(&help_cmd
);