]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - io/command.c
Merge whitespace changes over
[thirdparty/xfsprogs-dev.git] / io / command.c
CommitLineData
e246ba5f
NS
1/*
2 * Copyright (c) 2003 Silicon Graphics, Inc. All Rights Reserved.
dfc130f3 3 *
e246ba5f
NS
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
dfc130f3 7 *
e246ba5f
NS
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
dfc130f3 11 *
e246ba5f
NS
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
dfc130f3 18 *
e246ba5f
NS
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
dfc130f3 22 *
e246ba5f
NS
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
dfc130f3
RC
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
e246ba5f
NS
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
dfc130f3 32
e246ba5f
NS
33#include <libxfs.h>
34#include "command.h"
35
36cmdinfo_t *cmdtab;
37int ncmds;
38
39static int
40cmd_compare(const void *a, const void *b)
41{
42 return strcmp(((const cmdinfo_t *)a)->name,
43 ((const cmdinfo_t *)b)->name);
44}
45
46void
47add_command(
48 const cmdinfo_t *ci)
49{
50 cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
51 cmdtab[ncmds - 1] = *ci;
52 qsort(cmdtab, ncmds, sizeof(*cmdtab), cmd_compare);
53}
54
55int
56command(
57 int argc,
58 char **argv)
59{
60 char *cmd;
61 const cmdinfo_t *ct;
62
63 cmd = argv[0];
64 ct = find_command(cmd);
65 if (!ct) {
66 fprintf(stderr, _("command \"%s\" not found\n"), cmd);
67 return 0;
68 }
69 if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
70 if (ct->argmax == -1)
71 fprintf(stderr,
72 _("bad argument count %d to %s, expected at least %d arguments\n"),
73 argc-1, cmd, ct->argmin);
74 else if (ct->argmin == ct->argmax)
75 fprintf(stderr,
76 _("bad argument count %d to %s, expected %d arguments\n"),
77 argc-1, cmd, ct->argmin);
78 else
79 fprintf(stderr,
80 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
81 argc-1, cmd, ct->argmin, ct->argmax);
82 return 0;
83 }
84 optind = 0;
85 return ct->cfunc(argc, argv);
86}
87
88const cmdinfo_t *
89find_command(
90 const char *cmd)
91{
92 cmdinfo_t *ct;
93
94 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
95 if (strcmp(ct->name, cmd) == 0 ||
96 (ct->altname && strcmp(ct->altname, cmd) == 0))
97 return (const cmdinfo_t *)ct;
98 }
99 return NULL;
100}
101
102void
103init_commands(void)
104{
105 bmap_init();
106 help_init();
107 open_init();
108 pread_init();
109 prealloc_init();
110 pwrite_init();
111 quit_init();
112 resblks_init();
113 fsync_init();
114 truncate_init();
115}