]> git.proxmox.com Git - qemu.git/blob - cmd.c
qemu-io: Move 'quit' function
[qemu.git] / cmd.c
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
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
16 */
17
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>
25
26 #include "cmd.h"
27 #include "block/aio.h"
28 #include "qemu/main-loop.h"
29
30 #define _(x) x /* not gettext support yet */
31
32 /* from libxcmd/command.c */
33
34 cmdinfo_t *cmdtab;
35 int ncmds;
36
37 static checkfunc_t check_func;
38 static int ncmdline;
39 static char **cmdline;
40
41 static int
42 compare(const void *a, const void *b)
43 {
44 return strcmp(((const cmdinfo_t *)a)->name,
45 ((const cmdinfo_t *)b)->name);
46 }
47
48 void add_command(const cmdinfo_t *ci)
49 {
50 cmdtab = g_realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
51 cmdtab[ncmds - 1] = *ci;
52 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
53 }
54
55 static int
56 check_command(
57 const cmdinfo_t *ci)
58 {
59 if (check_func)
60 return check_func(qemuio_bs, ci);
61 return 1;
62 }
63
64 void
65 add_check_command(
66 checkfunc_t cf)
67 {
68 check_func = cf;
69 }
70
71 int
72 command_usage(
73 const cmdinfo_t *ci)
74 {
75 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
76 return 0;
77 }
78
79 int
80 command(
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;
106 return ct->cfunc(qemuio_bs, argc, argv);
107 }
108
109 const cmdinfo_t *
110 find_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
123 void add_user_command(char *optarg)
124 {
125 cmdline = g_realloc(cmdline, ++ncmdline * sizeof(char *));
126 cmdline[ncmdline-1] = optarg;
127 }
128
129 static void prep_fetchline(void *opaque)
130 {
131 int *fetchable = opaque;
132
133 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
134 *fetchable= 1;
135 }
136
137 static char *get_prompt(void);
138
139 void command_loop(void)
140 {
141 int i, done = 0, fetchable = 0, prompted = 0;
142 char *input;
143
144 for (i = 0; !done && i < ncmdline; i++) {
145 done = qemuio_command(cmdline[i]);
146 }
147 if (cmdline) {
148 g_free(cmdline);
149 return;
150 }
151
152 while (!done) {
153 if (!prompted) {
154 printf("%s", get_prompt());
155 fflush(stdout);
156 qemu_set_fd_handler(STDIN_FILENO, prep_fetchline, NULL, &fetchable);
157 prompted = 1;
158 }
159
160 main_loop_wait(false);
161
162 if (!fetchable) {
163 continue;
164 }
165
166 input = fetchline();
167 if (input == NULL) {
168 break;
169 }
170 done = qemuio_command(input);
171 free(input);
172
173 prompted = 0;
174 fetchable = 0;
175 }
176 qemu_set_fd_handler(STDIN_FILENO, NULL, NULL, NULL);
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
188 static char *
189 get_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)
199 char *
200 fetchline(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)
210 static char *el_get_prompt(EditLine *e) { return get_prompt(); }
211 char *
212 fetchline(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
240 char *
241 fetchline(void)
242 {
243 char *p, *line = malloc(MAXREADLINESZ);
244
245 if (!line)
246 return NULL;
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
258 static char *qemu_strsep(char **input, const char *delim)
259 {
260 char *result = *input;
261 if (result != NULL) {
262 char *p;
263
264 for (p = result; *p != '\0'; p++) {
265 if (strchr(delim, *p)) {
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
279 char **breakline(char *input, int *count)
280 {
281 int c = 0;
282 char *p;
283 char **rval = calloc(sizeof(char *), 1);
284 char **tmp;
285
286 while (rval && (p = qemu_strsep(&input, " ")) != NULL) {
287 if (!*p) {
288 continue;
289 }
290 c++;
291 tmp = realloc(rval, sizeof(*rval) * (c + 1));
292 if (!tmp) {
293 free(rval);
294 rval = NULL;
295 c = 0;
296 break;
297 } else {
298 rval = tmp;
299 }
300 rval[c - 1] = p;
301 rval[c] = NULL;
302 }
303 *count = c;
304 return rval;
305 }
306
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
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
321 void
322 cvtstr(
323 double value,
324 char *str,
325 size_t size)
326 {
327 char *trim;
328 const char *suffix;
329
330 if (value >= EXABYTES(1)) {
331 suffix = " EiB";
332 snprintf(str, size - 4, "%.3f", TO_EXABYTES(value));
333 } else if (value >= PETABYTES(1)) {
334 suffix = " PiB";
335 snprintf(str, size - 4, "%.3f", TO_PETABYTES(value));
336 } else if (value >= TERABYTES(1)) {
337 suffix = " TiB";
338 snprintf(str, size - 4, "%.3f", TO_TERABYTES(value));
339 } else if (value >= GIGABYTES(1)) {
340 suffix = " GiB";
341 snprintf(str, size - 4, "%.3f", TO_GIGABYTES(value));
342 } else if (value >= MEGABYTES(1)) {
343 suffix = " MiB";
344 snprintf(str, size - 4, "%.3f", TO_MEGABYTES(value));
345 } else if (value >= KILOBYTES(1)) {
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);
356 } else {
357 strcat(str, suffix);
358 }
359 }
360
361 struct timeval
362 tsub(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
373 double
374 tdiv(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
383 void
384 timestr(
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),
397 (unsigned int) (usec * 100));
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),
408 (unsigned int) (usec * 100));
409 } else {
410 snprintf(ts, size, "0.%04u sec", (unsigned int) (usec * 10000));
411 }
412 }