Fix in file qemu-sockets.c
[qemu/aliguori-queue.git] / cmd.c
blob1dc5dbd6875f56e078c126a950553d39a3fbd6e7
1 /*
2 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
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, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ctype.h>
23 #include <errno.h>
25 #include "cmd.h"
27 #define _(x) x /* not gettext support yet */
29 extern int optind;
31 /* from libxcmd/command.c */
33 cmdinfo_t *cmdtab;
34 int ncmds;
36 static argsfunc_t args_func;
37 static checkfunc_t check_func;
38 static int ncmdline;
39 static char **cmdline;
41 static int
42 compare(const void *a, const void *b)
44 return strcmp(((const cmdinfo_t *)a)->name,
45 ((const cmdinfo_t *)b)->name);
48 void
49 add_command(
50 const cmdinfo_t *ci)
52 cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
53 cmdtab[ncmds - 1] = *ci;
54 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
57 static int
58 check_command(
59 const cmdinfo_t *ci)
61 if (check_func)
62 return check_func(ci);
63 return 1;
66 void
67 add_check_command(
68 checkfunc_t cf)
70 check_func = cf;
73 int
74 command_usage(
75 const cmdinfo_t *ci)
77 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
78 return 0;
81 int
82 command(
83 const cmdinfo_t *ct,
84 int argc,
85 char **argv)
87 char *cmd = argv[0];
89 if (!check_command(ct))
90 return 0;
92 if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
93 if (ct->argmax == -1)
94 fprintf(stderr,
95 _("bad argument count %d to %s, expected at least %d arguments\n"),
96 argc-1, cmd, ct->argmin);
97 else if (ct->argmin == ct->argmax)
98 fprintf(stderr,
99 _("bad argument count %d to %s, expected %d arguments\n"),
100 argc-1, cmd, ct->argmin);
101 else
102 fprintf(stderr,
103 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
104 argc-1, cmd, ct->argmin, ct->argmax);
105 return 0;
107 optind = 0;
108 return ct->cfunc(argc, argv);
111 const cmdinfo_t *
112 find_command(
113 const char *cmd)
115 cmdinfo_t *ct;
117 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
118 if (strcmp(ct->name, cmd) == 0 ||
119 (ct->altname && strcmp(ct->altname, cmd) == 0))
120 return (const cmdinfo_t *)ct;
122 return NULL;
125 void
126 add_user_command(char *optarg)
128 ncmdline++;
129 cmdline = realloc(cmdline, sizeof(char*) * (ncmdline));
130 if (!cmdline) {
131 perror("realloc");
132 exit(1);
134 cmdline[ncmdline-1] = optarg;
137 static int
138 args_command(
139 int index)
141 if (args_func)
142 return args_func(index);
143 return 0;
146 void
147 add_args_command(
148 argsfunc_t af)
150 args_func = af;
153 void
154 command_loop(void)
156 int c, i, j = 0, done = 0;
157 char *input;
158 char **v;
159 const cmdinfo_t *ct;
161 for (i = 0; !done && i < ncmdline; i++) {
162 input = strdup(cmdline[i]);
163 if (!input) {
164 fprintf(stderr,
165 _("cannot strdup command '%s': %s\n"),
166 cmdline[i], strerror(errno));
167 exit(1);
169 v = breakline(input, &c);
170 if (c) {
171 ct = find_command(v[0]);
172 if (ct) {
173 if (ct->flags & CMD_FLAG_GLOBAL)
174 done = command(ct, c, v);
175 else {
176 j = 0;
177 while (!done && (j = args_command(j)))
178 done = command(ct, c, v);
180 } else
181 fprintf(stderr, _("command \"%s\" not found\n"),
182 v[0]);
184 doneline(input, v);
186 if (cmdline) {
187 free(cmdline);
188 return;
190 while (!done) {
191 if ((input = fetchline()) == NULL)
192 break;
193 v = breakline(input, &c);
194 if (c) {
195 ct = find_command(v[0]);
196 if (ct)
197 done = command(ct, c, v);
198 else
199 fprintf(stderr, _("command \"%s\" not found\n"),
200 v[0]);
202 doneline(input, v);
206 /* from libxcmd/input.c */
208 #if defined(ENABLE_READLINE)
209 # include <readline/history.h>
210 # include <readline/readline.h>
211 #elif defined(ENABLE_EDITLINE)
212 # include <histedit.h>
213 #endif
215 static char *
216 get_prompt(void)
218 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
220 if (!prompt[0])
221 snprintf(prompt, sizeof(prompt), "%s> ", progname);
222 return prompt;
225 #if defined(ENABLE_READLINE)
226 char *
227 fetchline(void)
229 char *line;
231 line = readline(get_prompt());
232 if (line && *line)
233 add_history(line);
234 return line;
236 #elif defined(ENABLE_EDITLINE)
237 static char *el_get_prompt(EditLine *e) { return get_prompt(); }
238 char *
239 fetchline(void)
241 static EditLine *el;
242 static History *hist;
243 HistEvent hevent;
244 char *line;
245 int count;
247 if (!el) {
248 hist = history_init();
249 history(hist, &hevent, H_SETSIZE, 100);
250 el = el_init(progname, stdin, stdout, stderr);
251 el_source(el, NULL);
252 el_set(el, EL_SIGNAL, 1);
253 el_set(el, EL_PROMPT, el_get_prompt);
254 el_set(el, EL_HIST, history, (const char *)hist);
256 line = strdup(el_gets(el, &count));
257 if (line) {
258 if (count > 0)
259 line[count-1] = '\0';
260 if (*line)
261 history(hist, &hevent, H_ENTER, line);
263 return line;
265 #else
266 # define MAXREADLINESZ 1024
267 char *
268 fetchline(void)
270 char *p, *line = malloc(MAXREADLINESZ);
272 if (!line)
273 return NULL;
274 printf("%s", get_prompt());
275 fflush(stdout);
276 if (!fgets(line, MAXREADLINESZ, stdin)) {
277 free(line);
278 return NULL;
280 p = line + strlen(line);
281 if (p != line && p[-1] == '\n')
282 p[-1] = '\0';
283 return line;
285 #endif
287 char **
288 breakline(
289 char *input,
290 int *count)
292 int c = 0;
293 char *p;
294 char **rval = calloc(sizeof(char *), 1);
296 while (rval && (p = strsep(&input, " ")) != NULL) {
297 if (!*p)
298 continue;
299 c++;
300 rval = realloc(rval, sizeof(*rval) * (c + 1));
301 if (!rval) {
302 c = 0;
303 break;
305 rval[c - 1] = p;
306 rval[c] = NULL;
308 *count = c;
309 return rval;
312 void
313 doneline(
314 char *input,
315 char **vec)
317 free(input);
318 free(vec);
321 #define EXABYTES(x) ((long long)(x) << 60)
322 #define PETABYTES(x) ((long long)(x) << 50)
323 #define TERABYTES(x) ((long long)(x) << 40)
324 #define GIGABYTES(x) ((long long)(x) << 30)
325 #define MEGABYTES(x) ((long long)(x) << 20)
326 #define KILOBYTES(x) ((long long)(x) << 10)
328 long long
329 cvtnum(
330 char *s)
332 long long i;
333 char *sp;
334 int c;
336 i = strtoll(s, &sp, 0);
337 if (i == 0 && sp == s)
338 return -1LL;
339 if (*sp == '\0')
340 return i;
342 if (sp[1] != '\0')
343 return -1LL;
345 c = tolower(*sp);
346 switch (c) {
347 default:
348 return i;
349 case 'k':
350 return KILOBYTES(i);
351 case 'm':
352 return MEGABYTES(i);
353 case 'g':
354 return GIGABYTES(i);
355 case 't':
356 return TERABYTES(i);
357 case 'p':
358 return PETABYTES(i);
359 case 'e':
360 return EXABYTES(i);
362 return -1LL;
365 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
366 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
367 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
368 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
369 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
370 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
372 void
373 cvtstr(
374 double value,
375 char *str,
376 size_t size)
378 const char *fmt;
379 int precise;
381 precise = ((double)value * 1000 == (double)(int)value * 1000);
383 if (value >= EXABYTES(1)) {
384 fmt = precise ? "%.f EiB" : "%.3f EiB";
385 snprintf(str, size, fmt, TO_EXABYTES(value));
386 } else if (value >= PETABYTES(1)) {
387 fmt = precise ? "%.f PiB" : "%.3f PiB";
388 snprintf(str, size, fmt, TO_PETABYTES(value));
389 } else if (value >= TERABYTES(1)) {
390 fmt = precise ? "%.f TiB" : "%.3f TiB";
391 snprintf(str, size, fmt, TO_TERABYTES(value));
392 } else if (value >= GIGABYTES(1)) {
393 fmt = precise ? "%.f GiB" : "%.3f GiB";
394 snprintf(str, size, fmt, TO_GIGABYTES(value));
395 } else if (value >= MEGABYTES(1)) {
396 fmt = precise ? "%.f MiB" : "%.3f MiB";
397 snprintf(str, size, fmt, TO_MEGABYTES(value));
398 } else if (value >= KILOBYTES(1)) {
399 fmt = precise ? "%.f KiB" : "%.3f KiB";
400 snprintf(str, size, fmt, TO_KILOBYTES(value));
401 } else {
402 snprintf(str, size, "%f bytes", value);
406 struct timeval
407 tsub(struct timeval t1, struct timeval t2)
409 t1.tv_usec -= t2.tv_usec;
410 if (t1.tv_usec < 0) {
411 t1.tv_usec += 1000000;
412 t1.tv_sec--;
414 t1.tv_sec -= t2.tv_sec;
415 return t1;
418 double
419 tdiv(double value, struct timeval tv)
421 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
424 #define HOURS(sec) ((sec) / (60 * 60))
425 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
426 #define SECONDS(sec) ((sec) % 60)
428 void
429 timestr(
430 struct timeval *tv,
431 char *ts,
432 size_t size,
433 int format)
435 double usec = (double)tv->tv_usec / 1000000.0;
437 if (format & TERSE_FIXED_TIME) {
438 if (!HOURS(tv->tv_sec)) {
439 snprintf(ts, size, "%u:%02u.%02u",
440 (unsigned int) MINUTES(tv->tv_sec),
441 (unsigned int) SECONDS(tv->tv_sec),
442 (unsigned int) usec * 100);
443 return;
445 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
448 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
449 snprintf(ts, size, "%u:%02u:%02u.%02u",
450 (unsigned int) HOURS(tv->tv_sec),
451 (unsigned int) MINUTES(tv->tv_sec),
452 (unsigned int) SECONDS(tv->tv_sec),
453 (unsigned int) usec * 100);
454 } else {
455 snprintf(ts, size, "0.%04u sec", (unsigned int) usec * 10000);
460 /* from libxcmd/quit.c */
462 static cmdinfo_t quit_cmd;
464 /* ARGSUSED */
465 static int
466 quit_f(
467 int argc,
468 char **argv)
470 return 1;
473 void
474 quit_init(void)
476 quit_cmd.name = _("quit");
477 quit_cmd.altname = _("q");
478 quit_cmd.cfunc = quit_f;
479 quit_cmd.argmin = -1;
480 quit_cmd.argmax = -1;
481 quit_cmd.flags = CMD_FLAG_GLOBAL;
482 quit_cmd.oneline = _("exit the program");
484 add_command(&quit_cmd);
487 /* from libxcmd/help.c */
489 static cmdinfo_t help_cmd;
490 static void help_onecmd(const char *cmd, const cmdinfo_t *ct);
491 static void help_oneline(const char *cmd, const cmdinfo_t *ct);
493 static void
494 help_all(void)
496 const cmdinfo_t *ct;
498 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++)
499 help_oneline(ct->name, ct);
500 printf(_("\nUse 'help commandname' for extended help.\n"));
503 static int
504 help_f(
505 int argc,
506 char **argv)
508 const cmdinfo_t *ct;
510 if (argc == 1) {
511 help_all();
512 return 0;
514 ct = find_command(argv[1]);
515 if (ct == NULL) {
516 printf(_("command %s not found\n"), argv[1]);
517 return 0;
519 help_onecmd(argv[1], ct);
520 return 0;
523 static void
524 help_onecmd(
525 const char *cmd,
526 const cmdinfo_t *ct)
528 help_oneline(cmd, ct);
529 if (ct->help)
530 ct->help();
533 static void
534 help_oneline(
535 const char *cmd,
536 const cmdinfo_t *ct)
538 if (cmd)
539 printf("%s ", cmd);
540 else {
541 printf("%s ", ct->name);
542 if (ct->altname)
543 printf("(or %s) ", ct->altname);
545 if (ct->args)
546 printf("%s ", ct->args);
547 printf("-- %s\n", ct->oneline);
550 void
551 help_init(void)
553 help_cmd.name = _("help");
554 help_cmd.altname = _("?");
555 help_cmd.cfunc = help_f;
556 help_cmd.argmin = 0;
557 help_cmd.argmax = 1;
558 help_cmd.flags = CMD_FLAG_GLOBAL;
559 help_cmd.args = _("[command]");
560 help_cmd.oneline = _("help for one or all commands");
562 add_command(&help_cmd);