]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/command.c
dcfbdd61830b1c73405fa418b78e2f5ac0a50b41
[thirdparty/xfsprogs-dev.git] / db / command.c
1 /*
2 * Copyright (c) 2000-2005 Silicon Graphics, Inc. All Rights Reserved.
3 *
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.
7 *
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.
11 *
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.
18 *
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.
22 *
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
25 *
26 * http://www.sgi.com
27 *
28 * For further information regarding this notice, see:
29 *
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
31 */
32
33 #include <xfs/libxfs.h>
34 #include "addr.h"
35 #include "attrset.h"
36 #include "block.h"
37 #include "bmap.h"
38 #include "check.h"
39 #include "command.h"
40 #include "convert.h"
41 #include "debug.h"
42 #include "type.h"
43 #include "echo.h"
44 #include "faddr.h"
45 #include "fprint.h"
46 #include "field.h"
47 #include "agf.h"
48 #include "agfl.h"
49 #include "agi.h"
50 #include "frag.h"
51 #include "freesp.h"
52 #include "help.h"
53 #include "hash.h"
54 #include "inode.h"
55 #include "input.h"
56 #include "io.h"
57 #include "output.h"
58 #include "print.h"
59 #include "quit.h"
60 #include "sb.h"
61 #include "write.h"
62 #include "malloc.h"
63 #include "dquot.h"
64
65 cmdinfo_t *cmdtab;
66 int ncmds;
67
68 static int
69 cmd_compare(const void *a, const void *b)
70 {
71 return strcmp(((const cmdinfo_t *)a)->name,
72 ((const cmdinfo_t *)b)->name);
73 }
74
75 void
76 add_command(
77 const cmdinfo_t *ci)
78 {
79 cmdtab = xrealloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
80 cmdtab[ncmds - 1] = *ci;
81 qsort(cmdtab, ncmds, sizeof(*cmdtab), cmd_compare);
82 }
83
84 int
85 command(
86 int argc,
87 char **argv)
88 {
89 char *cmd;
90 const cmdinfo_t *ct;
91
92 cmd = argv[0];
93 ct = find_command(cmd);
94 if (ct == NULL) {
95 dbprintf("command %s not found\n", cmd);
96 return 0;
97 }
98 if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
99 dbprintf("bad argument count %d to %s, expected ", argc-1, cmd);
100 if (ct->argmax == -1)
101 dbprintf("at least %d", ct->argmin);
102 else if (ct->argmin == ct->argmax)
103 dbprintf("%d", ct->argmin);
104 else
105 dbprintf("between %d and %d", ct->argmin, ct->argmax);
106 dbprintf(" arguments\n");
107 return 0;
108 }
109 platform_getoptreset();
110 return ct->cfunc(argc, argv);
111 }
112
113 const cmdinfo_t *
114 find_command(
115 const char *cmd)
116 {
117 cmdinfo_t *ct;
118
119 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
120 if (strcmp(ct->name, cmd) == 0 ||
121 (ct->altname && strcmp(ct->altname, cmd) == 0))
122 return (const cmdinfo_t *)ct;
123 }
124 return NULL;
125 }
126
127 void
128 init_commands(void)
129 {
130 addr_init();
131 agf_init();
132 agfl_init();
133 agi_init();
134 attrset_init();
135 block_init();
136 bmap_init();
137 check_init();
138 convert_init();
139 debug_init();
140 echo_init();
141 frag_init();
142 freesp_init();
143 help_init();
144 hash_init();
145 inode_init();
146 input_init();
147 io_init();
148 output_init();
149 print_init();
150 quit_init();
151 sb_init();
152 type_init();
153 write_init();
154 dquot_init();
155 }