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, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
27 #define _(x) x /* not gettext support yet */
31 /* from libxcmd/command.c */
36 static argsfunc_t args_func;
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);
52 cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
53 cmdtab[ncmds - 1] = *ci;
54 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
62 return check_func(ci);
77 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
89 if (!check_command(ct))
92 if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
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)
99 _("bad argument count %d to %s, expected %d arguments\n"),
100 argc-1, cmd, ct->argmin);
103 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
104 argc-1, cmd, ct->argmin, ct->argmax);
108 return ct->cfunc(argc, argv);
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;
126 add_user_command(char *optarg)
129 cmdline = realloc(cmdline, sizeof(char*) * (ncmdline));
134 cmdline[ncmdline-1] = optarg;
142 return args_func(index);
156 int c, i, j = 0, done = 0;
161 for (i = 0; !done && i < ncmdline; i++) {
162 input = strdup(cmdline[i]);
165 _("cannot strdup command '%s': %s\n"),
166 cmdline[i], strerror(errno));
169 v = breakline(input, &c);
171 ct = find_command(v[0]);
173 if (ct->flags & CMD_FLAG_GLOBAL)
174 done = command(ct, c, v);
177 while (!done && (j = args_command(j)))
178 done = command(ct, c, v);
181 fprintf(stderr, _("command \"%s\" not found\n"),
191 if ((input = fetchline()) == NULL)
193 v = breakline(input, &c);
195 ct = find_command(v[0]);
197 done = command(ct, c, v);
199 fprintf(stderr, _("command \"%s\" not found\n"),
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>
215 extern char *progname;
220 static char prompt[FILENAME_MAX + 2 /*"> "*/ + 1 /*"\0"*/ ];
223 snprintf(prompt, sizeof(prompt), "%s> ", progname);
227 #if defined(ENABLE_READLINE)
233 line = readline(get_prompt());
238 #elif defined(ENABLE_EDITLINE)
239 static char *el_get_prompt(EditLine *e) { return get_prompt(); }
244 static History *hist;
250 hist = history_init();
251 history(hist, &hevent, H_SETSIZE, 100);
252 el = el_init(progname, stdin, stdout, stderr);
254 el_set(el, EL_SIGNAL, 1);
255 el_set(el, EL_PROMPT, el_get_prompt);
256 el_set(el, EL_HIST, history, (const char *)hist);
258 line = strdup(el_gets(el, &count));
261 line[count-1] = '\0';
263 history(hist, &hevent, H_ENTER, line);
268 # define MAXREADLINESZ 1024
272 char *p, *line = malloc(MAXREADLINESZ);
276 printf("%s", get_prompt());
278 if (!fgets(line, MAXREADLINESZ, stdin)) {
282 p = line + strlen(line);
283 if (p != line && p[-1] == '\n')
296 char **rval = calloc(sizeof(char *), 1);
298 while (rval && (p = strsep(&input, " ")) != NULL) {
302 rval = realloc(rval, sizeof(*rval) * (c + 1));
323 #define EXABYTES(x) ((long long)(x) << 60)
324 #define PETABYTES(x) ((long long)(x) << 50)
325 #define TERABYTES(x) ((long long)(x) << 40)
326 #define GIGABYTES(x) ((long long)(x) << 30)
327 #define MEGABYTES(x) ((long long)(x) << 20)
328 #define KILOBYTES(x) ((long long)(x) << 10)
338 i = strtoll(s, &sp, 0);
339 if (i == 0 && sp == s)
367 #define TO_EXABYTES(x) ((x) / EXABYTES(1))
368 #define TO_PETABYTES(x) ((x) / PETABYTES(1))
369 #define TO_TERABYTES(x) ((x) / TERABYTES(1))
370 #define TO_GIGABYTES(x) ((x) / GIGABYTES(1))
371 #define TO_MEGABYTES(x) ((x) / MEGABYTES(1))
372 #define TO_KILOBYTES(x) ((x) / KILOBYTES(1))
383 precise = ((double)value * 1000 == (double)(int)value * 1000);
385 if (value >= EXABYTES(1)) {
386 fmt = precise ? "%.f EiB" : "%.3f EiB";
387 snprintf(str, size, fmt, TO_EXABYTES(value));
388 } else if (value >= PETABYTES(1)) {
389 fmt = precise ? "%.f PiB" : "%.3f PiB";
390 snprintf(str, size, fmt, TO_PETABYTES(value));
391 } else if (value >= TERABYTES(1)) {
392 fmt = precise ? "%.f TiB" : "%.3f TiB";
393 snprintf(str, size, fmt, TO_TERABYTES(value));
394 } else if (value >= GIGABYTES(1)) {
395 fmt = precise ? "%.f GiB" : "%.3f GiB";
396 snprintf(str, size, fmt, TO_GIGABYTES(value));
397 } else if (value >= MEGABYTES(1)) {
398 fmt = precise ? "%.f MiB" : "%.3f MiB";
399 snprintf(str, size, fmt, TO_MEGABYTES(value));
400 } else if (value >= KILOBYTES(1)) {
401 fmt = precise ? "%.f KiB" : "%.3f KiB";
402 snprintf(str, size, fmt, TO_KILOBYTES(value));
404 snprintf(str, size, "%f bytes", value);
409 tsub(struct timeval t1, struct timeval t2)
411 t1.tv_usec -= t2.tv_usec;
412 if (t1.tv_usec < 0) {
413 t1.tv_usec += 1000000;
416 t1.tv_sec -= t2.tv_sec;
421 tdiv(double value, struct timeval tv)
423 return value / ((double)tv.tv_sec + ((double)tv.tv_usec / 1000000.0));
426 #define HOURS(sec) ((sec) / (60 * 60))
427 #define MINUTES(sec) (((sec) % (60 * 60)) / 60)
428 #define SECONDS(sec) ((sec) % 60)
437 double usec = (double)tv->tv_usec / 1000000.0;
439 if (format & TERSE_FIXED_TIME) {
440 if (!HOURS(tv->tv_sec)) {
441 snprintf(ts, size, "%u:%02u.%02u",
442 (unsigned int) MINUTES(tv->tv_sec),
443 (unsigned int) SECONDS(tv->tv_sec),
444 (unsigned int) usec * 100);
447 format |= VERBOSE_FIXED_TIME; /* fallback if hours needed */
450 if ((format & VERBOSE_FIXED_TIME) || tv->tv_sec) {
451 snprintf(ts, size, "%u:%02u:%02u.%02u",
452 (unsigned int) HOURS(tv->tv_sec),
453 (unsigned int) MINUTES(tv->tv_sec),
454 (unsigned int) SECONDS(tv->tv_sec),
455 (unsigned int) usec * 100);
457 snprintf(ts, size, "0.%04u sec", (unsigned int) usec * 10000);
462 /* from libxcmd/quit.c */
464 static cmdinfo_t quit_cmd;
478 quit_cmd.name = _("quit");
479 quit_cmd.altname = _("q");
480 quit_cmd.cfunc = quit_f;
481 quit_cmd.argmin = -1;
482 quit_cmd.argmax = -1;
483 quit_cmd.flags = CMD_FLAG_GLOBAL;
484 quit_cmd.oneline = _("exit the program");
486 add_command(&quit_cmd);
489 /* from libxcmd/help.c */
491 static cmdinfo_t help_cmd;
492 static void help_onecmd(const char *cmd, const cmdinfo_t *ct);
493 static void help_oneline(const char *cmd, const cmdinfo_t *ct);
500 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++)
501 help_oneline(ct->name, ct);
502 printf(_("\nUse 'help commandname' for extended help.\n"));
516 ct = find_command(argv[1]);
518 printf(_("command %s not found\n"), argv[1]);
521 help_onecmd(argv[1], ct);
530 help_oneline(cmd, ct);
543 printf("%s ", ct->name);
545 printf("(or %s) ", ct->altname);
548 printf("%s ", ct->args);
549 printf("-- %s\n", ct->oneline);
555 help_cmd.name = _("help");
556 help_cmd.altname = _("?");
557 help_cmd.cfunc = help_f;
560 help_cmd.flags = CMD_FLAG_GLOBAL;
561 help_cmd.args = _("[command]");
562 help_cmd.oneline = _("help for one or all commands");
564 add_command(&help_cmd);