]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxcmd/command.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
7 #include "platform_defs.h"
14 static iterfunc_t iter_func
;
15 static checkfunc_t check_func
;
23 static struct cmdline
*cmdline
;
26 compare(const void *a
, const void *b
)
28 return strcmp(((const cmdinfo_t
*)a
)->name
,
29 ((const cmdinfo_t
*)b
)->name
);
36 cmdtab
= realloc((void *)cmdtab
, ++ncmds
* sizeof(*cmdtab
));
37 cmdtab
[ncmds
- 1] = *ci
;
38 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare
);
45 /* always run internal library supplied commands */
46 if (ci
->flags
& CMD_FLAG_LIBRARY
)
50 return check_func(ci
);
65 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
77 if (!check_command(ct
))
80 if (argc
-1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
-1 > ct
->argmax
)) {
83 _("bad argument count %d to %s, expected at least %d arguments\n"),
84 argc
-1, cmd
, ct
->argmin
);
85 else if (ct
->argmin
== ct
->argmax
)
87 _("bad argument count %d to %s, expected %d arguments\n"),
88 argc
-1, cmd
, ct
->argmin
);
91 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
92 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
95 platform_getoptreset();
96 return ct
->cfunc(argc
, argv
);
105 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
106 if (strcmp(ct
->name
, cmd
) == 0 ||
107 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
108 return (const cmdinfo_t
*)ct
;
114 add_user_command(char *optarg
)
117 cmdline
= realloc(cmdline
, sizeof(struct cmdline
) * (ncmdline
));
122 cmdline
[ncmdline
-1].cmdline
= optarg
;
123 cmdline
[ncmdline
-1].iterate
= true;
128 add_oneshot_user_command(char *optarg
)
131 cmdline
= realloc(cmdline
, sizeof(struct cmdline
) * (ncmdline
));
136 cmdline
[ncmdline
-1].cmdline
= optarg
;
137 cmdline
[ncmdline
-1].iterate
= false;
141 * Run a command, iterating as necessary. Return 0 for success, non-zero
142 * if an error occurred. Errors terminate loop iteration immediately.
153 /* if there's nothing to iterate, we're done! */
157 for (j
= iter_func(0); j
; j
= iter_func(j
)) {
158 error
= command(ct
, argc
, argv
);
168 add_command_iterator(
184 v
= breakline(input
, &c
);
188 ct
= find_command(v
[0]);
190 fprintf(stderr
, _("command \"%s\" not found\n"), v
[0]);
194 /* oneshot commands don't iterate */
195 if (!iterate
|| (ct
->flags
& CMD_FLAG_ONESHOT
))
196 error
= command(ct
, c
, v
);
198 error
= iterate_command(ct
, c
, v
);
212 /* interactive mode */
217 done
= process_input(input
, false);
222 /* command line mode */
223 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
224 input
= strdup(cmdline
[i
].cmdline
);
227 _("cannot strdup command '%s': %s\n"),
228 cmdline
[i
].cmdline
, strerror(errno
));
231 done
= process_input(input
, cmdline
[i
].iterate
);
247 char s1
[64], s2
[64], ts
[64];
249 timestr(t2
, ts
, sizeof(ts
), compact
? VERBOSE_FIXED_TIME
: 0);
251 cvtstr((double)total
, s1
, sizeof(s1
));
252 cvtstr(tdiv((double)total
, *t2
), s2
, sizeof(s2
));
253 printf(_("%s %lld/%lld bytes at offset %lld\n"),
254 verb
, total
, count
, (long long)offset
);
255 printf(_("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n"),
256 s1
, ops
, ts
, s2
, tdiv((double)ops
, *t2
));
257 } else {/* bytes,ops,time,bytes/sec,ops/sec */
258 printf("%lld,%d,%s,%.3f,%.3f\n",
260 tdiv((double)total
, *t2
), tdiv((double)ops
, *t2
));