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