]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxcmd/command.c
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
19 #include "platform_defs.h"
26 static iterfunc_t iter_func
;
27 static checkfunc_t check_func
;
35 struct cmdline
*cmdline
;
38 compare(const void *a
, const void *b
)
40 return strcmp(((const cmdinfo_t
*)a
)->name
,
41 ((const cmdinfo_t
*)b
)->name
);
48 cmdtab
= realloc((void *)cmdtab
, ++ncmds
* sizeof(*cmdtab
));
49 cmdtab
[ncmds
- 1] = *ci
;
50 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare
);
57 /* always run internal library supplied commands */
58 if (ci
->flags
& CMD_FLAG_LIBRARY
)
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
);
107 platform_getoptreset();
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(struct cmdline
) * (ncmdline
));
134 cmdline
[ncmdline
-1].cmdline
= optarg
;
135 cmdline
[ncmdline
-1].iterate
= true;
140 add_oneshot_user_command(char *optarg
)
143 cmdline
= realloc(cmdline
, sizeof(struct cmdline
) * (ncmdline
));
148 cmdline
[ncmdline
-1].cmdline
= optarg
;
149 cmdline
[ncmdline
-1].iterate
= false;
153 * Run a command, iterating as necessary. Return 0 for success, non-zero
154 * if an error occurred. Errors terminate loop iteration immediately.
165 /* if there's nothing to iterate, we're done! */
169 for (j
= iter_func(0); j
; j
= iter_func(j
)) {
170 error
= command(ct
, argc
, argv
);
180 add_command_iterator(
196 v
= breakline(input
, &c
);
200 ct
= find_command(v
[0]);
202 fprintf(stderr
, _("command \"%s\" not found\n"), v
[0]);
206 /* oneshot commands don't iterate */
207 if (!iterate
|| (ct
->flags
& CMD_FLAG_ONESHOT
))
208 error
= command(ct
, c
, v
);
210 error
= iterate_command(ct
, c
, v
);
224 /* interactive mode */
229 done
= process_input(input
, false);
234 /* command line mode */
235 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
236 input
= strdup(cmdline
[i
].cmdline
);
239 _("cannot strdup command '%s': %s\n"),
240 cmdline
[i
].cmdline
, strerror(errno
));
243 done
= process_input(input
, cmdline
[i
].iterate
);
259 char s1
[64], s2
[64], ts
[64];
261 timestr(t2
, ts
, sizeof(ts
), compact
? VERBOSE_FIXED_TIME
: 0);
263 cvtstr((double)total
, s1
, sizeof(s1
));
264 cvtstr(tdiv((double)total
, *t2
), s2
, sizeof(s2
));
265 printf(_("%s %lld/%lld bytes at offset %lld\n"),
266 verb
, total
, count
, (long long)offset
);
267 printf(_("%s, %d ops; %s (%s/sec and %.4f ops/sec)\n"),
268 s1
, ops
, ts
, s2
, tdiv((double)ops
, *t2
));
269 } else {/* bytes,ops,time,bytes/sec,ops/sec */
270 printf("%lld,%d,%s,%.3f,%.3f\n",
272 tdiv((double)total
, *t2
), tdiv((double)ops
, *t2
));