]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxcmd/command.c
e260309710a466144680d8e047d1260d049ce60b
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
));
38 perror(_("adding libxcmd command"));
41 cmdtab
[ncmds
- 1] = *ci
;
42 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare
);
49 /* always run internal library supplied commands */
50 if (ci
->flags
& CMD_FLAG_LIBRARY
)
54 return check_func(ci
);
69 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
81 if (!check_command(ct
))
84 if (argc
-1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
-1 > ct
->argmax
)) {
87 _("bad argument count %d to %s, expected at least %d arguments\n"),
88 argc
-1, cmd
, ct
->argmin
);
89 else if (ct
->argmin
== ct
->argmax
)
91 _("bad argument count %d to %s, expected %d arguments\n"),
92 argc
-1, cmd
, ct
->argmin
);
95 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
96 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
99 platform_getoptreset();
100 return ct
->cfunc(argc
, argv
);
109 for (ct
= cmdtab
; ct
< &cmdtab
[ncmds
]; ct
++) {
110 if (strcmp(ct
->name
, cmd
) == 0 ||
111 (ct
->altname
&& strcmp(ct
->altname
, cmd
) == 0))
112 return (const cmdinfo_t
*)ct
;
118 add_user_command(char *optarg
)
121 cmdline
= realloc(cmdline
, sizeof(struct cmdline
) * (ncmdline
));
126 cmdline
[ncmdline
-1].cmdline
= optarg
;
127 cmdline
[ncmdline
-1].iterate
= true;
132 add_oneshot_user_command(char *optarg
)
135 cmdline
= realloc(cmdline
, sizeof(struct cmdline
) * (ncmdline
));
140 cmdline
[ncmdline
-1].cmdline
= optarg
;
141 cmdline
[ncmdline
-1].iterate
= false;
145 * Run a command, iterating as necessary. Return 0 for success, non-zero
146 * if an error occurred. Errors terminate loop iteration immediately.
157 /* if there's nothing to iterate, we're done! */
161 for (j
= iter_func(0); j
; j
= iter_func(j
)) {
162 error
= command(ct
, argc
, argv
);
172 add_command_iterator(
188 v
= breakline(input
, &c
);
192 ct
= find_command(v
[0]);
194 fprintf(stderr
, _("command \"%s\" not found\n"), v
[0]);
198 /* oneshot commands don't iterate */
199 if (!iterate
|| (ct
->flags
& CMD_FLAG_ONESHOT
))
200 error
= command(ct
, c
, v
);
202 error
= iterate_command(ct
, c
, v
);
216 /* interactive mode */
221 done
= process_input(input
, false);
226 /* command line mode */
227 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
228 input
= strdup(cmdline
[i
].cmdline
);
231 _("cannot strdup command '%s': %s\n"),
232 cmdline
[i
].cmdline
, strerror(errno
));
235 done
= process_input(input
, cmdline
[i
].iterate
);
251 char s1
[64], s2
[64], ts
[64];
253 timestr(t2
, ts
, sizeof(ts
), compact
? VERBOSE_FIXED_TIME
: 0);
255 cvtstr((double)total
, s1
, sizeof(s1
));
256 cvtstr(tdiv((double)total
, *t2
), s2
, sizeof(s2
));
257 printf(_("%s %lld/%lld bytes at offset %lld\n"),
258 verb
, total
, count
, (long long)offset
);
259 printf(_("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n"),
260 s1
, ops
, ts
, s2
, tdiv((double)ops
, *t2
));
261 } else {/* bytes,ops,time,bytes/sec,ops/sec */
262 printf("%lld,%d,%s,%.3f,%.3f\n",
264 tdiv((double)total
, *t2
), tdiv((double)ops
, *t2
));