]> git.proxmox.com Git - qemu.git/blame - cmd.c
qemu-io: Move 'quit' function
[qemu.git] / cmd.c
CommitLineData
e3aff4f6
AL
1/*
2 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
4 *
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.
8 *
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.
13 *
14 * You should have received a copy of the GNU General Public License
8167ee88 15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
e3aff4f6
AL
16 */
17
18#include <stdio.h>
19#include <stdlib.h>
20#include <string.h>
21#include <ctype.h>
22#include <errno.h>
c32d766a 23#include <sys/time.h>
22a2bdcb 24#include <getopt.h>
e3aff4f6
AL
25
26#include "cmd.h"
737e150e 27#include "block/aio.h"
1de7afc9 28#include "qemu/main-loop.h"
e3aff4f6
AL
29
30#define _(x) x /* not gettext support yet */
31
e3aff4f6
AL
32/* from libxcmd/command.c */
33
34cmdinfo_t *cmdtab;
35int ncmds;
36
e3aff4f6
AL
37static checkfunc_t check_func;
38static int ncmdline;
39static char **cmdline;
40
41static int
42compare(const void *a, const void *b)
43{
44 return strcmp(((const cmdinfo_t *)a)->name,
45 ((const cmdinfo_t *)b)->name);
46}
47
81beeec4 48void add_command(const cmdinfo_t *ci)
e3aff4f6 49{
ba7806ad 50 cmdtab = g_realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
81beeec4
PB
51 cmdtab[ncmds - 1] = *ci;
52 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
e3aff4f6
AL
53}
54
55static int
56check_command(
57 const cmdinfo_t *ci)
58{
59 if (check_func)
734c3b85 60 return check_func(qemuio_bs, ci);
e3aff4f6
AL
61 return 1;
62}
63
64void
65add_check_command(
66 checkfunc_t cf)
67{
68 check_func = cf;
69}
70
71int
72command_usage(
73 const cmdinfo_t *ci)
74{
75 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
76 return 0;
77}
78
79int
80command(
81 const cmdinfo_t *ct,
82 int argc,
83 char **argv)
84{
85 char *cmd = argv[0];
86
87 if (!check_command(ct))
88 return 0;
89
90 if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
91 if (ct->argmax == -1)
92 fprintf(stderr,
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)
96 fprintf(stderr,
97 _("bad argument count %d to %s, expected %d arguments\n"),
98 argc-1, cmd, ct->argmin);
99 else
100 fprintf(stderr,
101 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
102 argc-1, cmd, ct->argmin, ct->argmax);
103 return 0;
104 }
105 optind = 0;
734c3b85 106 return ct->cfunc(qemuio_bs, argc, argv);
e3aff4f6
AL
107}
108
109const cmdinfo_t *
110find_command(
111 const char *cmd)
112{
113 cmdinfo_t *ct;
114
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;
119 }
120 return NULL;
121}
122
81beeec4 123void add_user_command(char *optarg)
e3aff4f6 124{
ba7806ad 125 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
81beeec4 126 cmdline[ncmdline-1] = optarg;
e3aff4f6
AL
127}
128
7d7d975c
MK
129static void prep_fetchline(void *opaque)
130{
131 int *fetchable = opaque;
132
a5a5238e 133 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
7d7d975c
MK
134 *fetchable= 1;
135}
136
137static char *get_prompt(void);
138
81beeec4 139void command_loop(void)
e3aff4f6 140{
dd583296 141 int i, done = 0, fetchable = 0, prompted = 0;
81beeec4 142 char *input;
81beeec4
PB
143
144 for (i = 0; !done && i < ncmdline; i++) {
dd583296 145 done = qemuio_command(cmdline[i]);
81beeec4
PB
146 }
147 if (cmdline) {
ba7806ad 148 g_free(cmdline);
81beeec4
PB
149 return;
150 }
7d7d975c 151
81beeec4 152 while (!done) {
7d7d975c
MK
153 if (!prompted) {
154 printf("%s", get_prompt());
155 fflush(stdout);
a5a5238e 156 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
7d7d975c
MK
157 prompted = 1;
158 }
159
a5a5238e 160 main_loop_wait(false);
7d7d975c
MK
161
162 if (!fetchable) {
163 continue;
164 }
dd583296 165
81beeec4
PB
166 input = fetchline();
167 if (input == NULL) {
168 break;
169 }
dd583296
KW
170 done = qemuio_command(input);
171 free(input);
7d7d975c
MK
172
173 prompted = 0;
174 fetchable = 0;
81beeec4 175 }
a5a5238e 176 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
e3aff4f6
AL
177}
178
179/* from libxcmd/input.c */
180
181#if defined(ENABLE_READLINE)
182# include <readline/history.h>
183# include <readline/readline.h>
184#elif defined(ENABLE_EDITLINE)
185# include <histedit.h>
186#endif
187
e3aff4f6
AL
188static char *
189get_prompt(void)
190{
191 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
192
193 if (!prompt[0])
194 snprintf(prompt, sizeof(prompt), "%s> ", progname);
195 return prompt;
196}
197
198#if defined(ENABLE_READLINE)
199char *
200fetchline(void)
201{
202 char *line;
203
204 line = readline(get_prompt());
205 if (line && *line)
206 add_history(line);
207 return line;
208}
209#elif defined(ENABLE_EDITLINE)
210static char *el_get_prompt(EditLine *e) { return get_prompt(); }
211char *
212fetchline(void)
213{
214 static EditLine *el;
215 static History *hist;
216 HistEvent hevent;
217 char *line;
218 int count;
219
220 if (!el) {
221 hist = history_init();
222 history(hist, &hevent, H_SETSIZE, 100);
223 el = el_init(progname, stdin, stdout, stderr);
224 el_source(el, NULL);
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);
228 }
229 line = strdup(el_gets(el, &count));
230 if (line) {
231 if (count > 0)
232 line[count-1] = '\0';
233 if (*line)
234 history(hist, &hevent, H_ENTER, line);
235 }
236 return line;
237}
238#else
239# define MAXREADLINESZ 1024
240char *
241fetchline(void)
242{
243 char *p, *line = malloc(MAXREADLINESZ);
244
245 if (!line)
246 return NULL;
e3aff4f6
AL
247 if (!fgets(line, MAXREADLINESZ, stdin)) {
248 free(line);
249 return NULL;
250 }
251 p = line + strlen(line);
252 if (p != line && p[-1] == '\n')
253 p[-1] = '\0';
254 return line;
255}
256#endif
257
c32d766a
SW
258static char *qemu_strsep(char **input, const char *delim)
259{
260 char *result = *input;
261 if (result != NULL) {
88bf7950
BS
262 char *p;
263
50da01ed
SW
264 for (p = result; *p != '\0'; p++) {
265 if (strchr(delim, *p)) {
c32d766a
SW
266 break;
267 }
268 }
269 if (*p == '\0') {
270 *input = NULL;
271 } else {
272 *p = '\0';
273 *input = p + 1;
274 }
275 }
276 return result;
277}
278
81beeec4 279char **breakline(char *input, int *count)
e3aff4f6 280{
81beeec4
PB
281 int c = 0;
282 char *p;
283 char **rval = calloc(sizeof(char *), 1);
47e8dd8f 284 char **tmp;
81beeec4
PB
285
286 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
287 if (!*p) {
288 continue;
289 }
290 c++;
47e8dd8f
PB
291 tmp = realloc(rval, sizeof(*rval) * (c + 1));
292 if (!tmp) {
293 free(rval);
294 rval = NULL;
81beeec4
PB
295 c = 0;
296 break;
47e8dd8f
PB
297 } else {
298 rval = tmp;
81beeec4
PB
299 }
300 rval[c - 1] = p;
301 rval[c] = NULL;
302 }
303 *count = c;
304 return rval;
e3aff4f6
AL
305}
306
e3aff4f6
AL
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)
313
e3aff4f6
AL
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))
320
321void
322cvtstr(
323 double value,
324 char *str,
325 size_t size)
326{
8655d2de
PB
327 char *trim;
328 const char *suffix;
e3aff4f6
AL
329
330 if (value >= EXABYTES(1)) {
8655d2de
PB
331 suffix = " EiB";
332 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
e3aff4f6 333 } else if (value >= PETABYTES(1)) {
8655d2de
PB
334 suffix = " PiB";
335 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
e3aff4f6 336 } else if (value >= TERABYTES(1)) {
8655d2de
PB
337 suffix = " TiB";
338 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
e3aff4f6 339 } else if (value >= GIGABYTES(1)) {
8655d2de
PB
340 suffix = " GiB";
341 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
e3aff4f6 342 } else if (value >= MEGABYTES(1)) {
8655d2de
PB
343 suffix = " MiB";
344 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
e3aff4f6 345 } else if (value >= KILOBYTES(1)) {
8655d2de
PB
346 suffix = " KiB";
347 snprintf(str, size - 4, "%.3f", TO_KILOBYTES(value));
348 } else {
349 suffix = " bytes";
350 snprintf(str, size - 6, "%f", value);
351 }
352
353 trim = strstr(str, ".000");
354 if (trim) {
355 strcpy(trim, suffix);
e3aff4f6 356 } else {
8655d2de 357 strcat(str, suffix);
e3aff4f6
AL
358 }
359}
360
361struct timeval
362tsub(struct timeval t1, struct timeval t2)
363{
364 t1.tv_usec -= t2.tv_usec;
365 if (t1.tv_usec < 0) {
366 t1.tv_usec += 1000000;
367 t1.tv_sec--;
368 }
369 t1.tv_sec -= t2.tv_sec;
370 return t1;
371}
372
373double
374tdiv(double value, struct timeval tv)
375{
376 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
377}
378
379#define HOURS(sec) ((sec) / (60 * 60))
380#define MINUTES(sec) (((sec) % (60 * 60)) / 60)
381#define SECONDS(sec) ((sec) % 60)
382
383void
384timestr(
385 struct timeval *tv,
386 char *ts,
387 size_t size,
388 int format)
389{
390 double usec = (double)tv->tv_usec / 1000000.0;
391
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),
bcd2491a 397 (unsigned int) (usec * 100));
e3aff4f6
AL
398 return;
399 }
400 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
401 }
402
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),
bcd2491a 408 (unsigned int) (usec * 100));
e3aff4f6 409 } else {
bcd2491a 410 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
e3aff4f6
AL
411 }
412}