]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blame - libxcmd/command.c
headers: remove definition of ASSERT from xfs.h
[thirdparty/xfsprogs-dev.git] / libxcmd / command.c
CommitLineData
e246ba5f 1/*
da23017d
NS
2 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
dfc130f3 4 *
da23017d
NS
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
e246ba5f 7 * published by the Free Software Foundation.
dfc130f3 8 *
da23017d
NS
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.
dfc130f3 13 *
da23017d
NS
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
e246ba5f 17 */
dfc130f3 18
dcabd4e7 19#include "platform_defs.h"
6b803e5a
CH
20#include "command.h"
21#include "input.h"
e246ba5f
NS
22
23cmdinfo_t *cmdtab;
24int ncmds;
25
3d93ccb7
NS
26static argsfunc_t args_func;
27static checkfunc_t check_func;
28static int ncmdline;
29static char **cmdline;
30
e246ba5f 31static int
3d93ccb7 32compare(const void *a, const void *b)
e246ba5f
NS
33{
34 return strcmp(((const cmdinfo_t *)a)->name,
35 ((const cmdinfo_t *)b)->name);
36}
37
38void
39add_command(
40 const cmdinfo_t *ci)
41{
42 cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
43 cmdtab[ncmds - 1] = *ci;
3d93ccb7
NS
44 qsort(cmdtab, ncmds, sizeof(*cmdtab), compare);
45}
46
47static int
48check_command(
49 const cmdinfo_t *ci)
50{
51 if (check_func)
52 return check_func(ci);
53 return 1;
54}
55
56void
57add_check_command(
58 checkfunc_t cf)
59{
60 check_func = cf;
e246ba5f
NS
61}
62
48c46ee3
NS
63int
64command_usage(
65 const cmdinfo_t *ci)
66{
3d93ccb7 67 printf("%s %s -- %s\n", ci->name, ci->args, ci->oneline);
48c46ee3
NS
68 return 0;
69}
70
e246ba5f
NS
71int
72command(
802d66e3 73 const cmdinfo_t *ct,
e246ba5f
NS
74 int argc,
75 char **argv)
76{
802d66e3 77 char *cmd = argv[0];
e246ba5f 78
3d93ccb7 79 if (!check_command(ct))
48c46ee3 80 return 0;
802d66e3 81
e246ba5f
NS
82 if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
83 if (ct->argmax == -1)
84 fprintf(stderr,
85 _("bad argument count %d to %s, expected at least %d arguments\n"),
86 argc-1, cmd, ct->argmin);
87 else if (ct->argmin == ct->argmax)
88 fprintf(stderr,
89 _("bad argument count %d to %s, expected %d arguments\n"),
90 argc-1, cmd, ct->argmin);
91 else
92 fprintf(stderr,
93 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
94 argc-1, cmd, ct->argmin, ct->argmax);
95 return 0;
96 }
c0211f67 97 platform_getoptreset();
e246ba5f
NS
98 return ct->cfunc(argc, argv);
99}
100
101const cmdinfo_t *
102find_command(
103 const char *cmd)
104{
105 cmdinfo_t *ct;
106
107 for (ct = cmdtab; ct < &cmdtab[ncmds]; ct++) {
108 if (strcmp(ct->name, cmd) == 0 ||
109 (ct->altname && strcmp(ct->altname, cmd) == 0))
110 return (const cmdinfo_t *)ct;
111 }
112 return NULL;
113}
3d93ccb7
NS
114
115void
116add_user_command(char *optarg)
117{
118 ncmdline++;
119 cmdline = realloc(cmdline, sizeof(char*) * (ncmdline));
120 if (!cmdline) {
121 perror("realloc");
122 exit(1);
123 }
124 cmdline[ncmdline-1] = optarg;
125}
126
127static int
128args_command(
129 int index)
130{
131 if (args_func)
132 return args_func(index);
133 return 0;
134}
135
136void
137add_args_command(
138 argsfunc_t af)
139{
140 args_func = af;
141}
142
143void
144command_loop(void)
145{
802d66e3
BN
146 int c, i, j = 0, done = 0;
147 char *input;
148 char **v;
149 const cmdinfo_t *ct;
3d93ccb7
NS
150
151 for (i = 0; !done && i < ncmdline; i++) {
802d66e3
BN
152 input = strdup(cmdline[i]);
153 if (!input) {
154 fprintf(stderr,
155 _("cannot strdup command '%s': %s\n"),
156 cmdline[i], strerror(errno));
157 exit(1);
158 }
159 v = breakline(input, &c);
160 if (c) {
161 ct = find_command(v[0]);
162 if (ct) {
163 if (ct->flags & CMD_FLAG_GLOBAL)
164 done = command(ct, c, v);
165 else {
166 j = 0;
167 while (!done && (j = args_command(j)))
168 done = command(ct, c, v);
169 }
170 } else
171 fprintf(stderr, _("command \"%s\" not found\n"),
172 v[0]);
3d93ccb7 173 }
802d66e3 174 doneline(input, v);
3d93ccb7
NS
175 }
176 if (cmdline) {
177 free(cmdline);
178 return;
179 }
180 while (!done) {
181 if ((input = fetchline()) == NULL)
182 break;
183 v = breakline(input, &c);
802d66e3
BN
184 if (c) {
185 ct = find_command(v[0]);
186 if (ct)
187 done = command(ct, c, v);
188 else
189 fprintf(stderr, _("command \"%s\" not found\n"),
190 v[0]);
191 }
3d93ccb7
NS
192 doneline(input, v);
193 }
194}