]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - libxcmd/command.c
2 * Copyright (c) 2003-2005 Silicon Graphics, Inc.
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.
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.
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
20 #include <xfs/command.h>
21 #include <xfs/input.h>
26 static argsfunc_t args_func
;
27 static checkfunc_t check_func
;
29 static char **cmdline
;
32 compare(const void *a
, const void *b
)
34 return strcmp(((const cmdinfo_t
*)a
)->name
,
35 ((const cmdinfo_t
*)b
)->name
);
42 cmdtab
= realloc((void *)cmdtab
, ++ncmds
* sizeof(*cmdtab
));
43 cmdtab
[ncmds
- 1] = *ci
;
44 qsort(cmdtab
, ncmds
, sizeof(*cmdtab
), compare
);
52 return check_func(ci
);
67 printf("%s %s -- %s\n", ci
->name
, ci
->args
, ci
->oneline
);
79 if (!check_command(ct
))
82 if (argc
-1 < ct
->argmin
|| (ct
->argmax
!= -1 && argc
-1 > ct
->argmax
)) {
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
)
89 _("bad argument count %d to %s, expected %d arguments\n"),
90 argc
-1, cmd
, ct
->argmin
);
93 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
94 argc
-1, cmd
, ct
->argmin
, ct
->argmax
);
97 platform_getoptreset();
98 return ct
->cfunc(argc
, argv
);
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
;
116 add_user_command(char *optarg
)
119 cmdline
= realloc(cmdline
, sizeof(char*) * (ncmdline
));
124 cmdline
[ncmdline
-1] = optarg
;
132 return args_func(index
);
146 int c
, i
, j
= 0, done
= 0;
151 for (i
= 0; !done
&& i
< ncmdline
; i
++) {
152 input
= strdup(cmdline
[i
]);
155 _("cannot strdup command '%s': %s\n"),
156 cmdline
[i
], strerror(errno
));
159 v
= breakline(input
, &c
);
161 ct
= find_command(v
[0]);
163 if (ct
->flags
& CMD_FLAG_GLOBAL
)
164 done
= command(ct
, c
, v
);
167 while (!done
&& (j
= args_command(j
)))
168 done
= command(ct
, c
, v
);
171 fprintf(stderr
, _("command \"%s\" not found\n"),
181 if ((input
= fetchline()) == NULL
)
183 v
= breakline(input
, &c
);
185 ct
= find_command(v
[0]);
187 done
= command(ct
, c
, v
);
189 fprintf(stderr
, _("command \"%s\" not found\n"),