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