]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - io/command.c
Merge over several portability changes from xfstest update.
[thirdparty/xfsprogs-dev.git] / io / command.c
1 /*
2 * Copyright (c) 2003-2004 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 "command.h"
35 #include "init.h"
36 #include "io.h"
37
38 cmdinfo_t *cmdtab;
39 int ncmds;
40
41 static int
42 cmd_compare(const void *a, const void *b)
43 {
44 return strcmp(((const cmdinfo_t *)a)->name,
45 ((const cmdinfo_t *)b)->name);
46 }
47
48 void
49 add_command(
50 const cmdinfo_t *ci)
51 {
52 cmdtab = realloc((void *)cmdtab, ++ncmds * sizeof(*cmdtab));
53 cmdtab[ncmds - 1] = *ci;
54 qsort(cmdtab, ncmds, sizeof(*cmdtab), cmd_compare);
55 }
56
57 int
58 command_usage(
59 const cmdinfo_t *ci)
60 {
61 printf("%s %s\n", ci->name, ci->oneline);
62 return 0;
63 }
64
65 int
66 command(
67 int argc,
68 char **argv)
69 {
70 char *cmd;
71 const cmdinfo_t *ct;
72
73 cmd = argv[0];
74 ct = find_command(cmd);
75 if (!ct) {
76 fprintf(stderr, _("command \"%s\" not found\n"), cmd);
77 return 0;
78 }
79 if (!file && !(ct->flags & CMD_NOFILE_OK)) {
80 fprintf(stderr, _("no files are open, try 'help open'\n"));
81 return 0;
82 }
83 if (!mapping && !(ct->flags & CMD_NOMAP_OK)) {
84 fprintf(stderr, _("no mapped regions, try 'help mmap'\n"));
85 return 0;
86 }
87 if (file && !(ct->flags & CMD_FOREIGN_OK) &&
88 (file->flags & IO_FOREIGN)) {
89 fprintf(stderr,
90 _("foreign file active, %s command is for XFS filesystems only\n"),
91 cmd);
92 return 0;
93 }
94 if (argc-1 < ct->argmin || (ct->argmax != -1 && argc-1 > ct->argmax)) {
95 if (ct->argmax == -1)
96 fprintf(stderr,
97 _("bad argument count %d to %s, expected at least %d arguments\n"),
98 argc-1, cmd, ct->argmin);
99 else if (ct->argmin == ct->argmax)
100 fprintf(stderr,
101 _("bad argument count %d to %s, expected %d arguments\n"),
102 argc-1, cmd, ct->argmin);
103 else
104 fprintf(stderr,
105 _("bad argument count %d to %s, expected between %d and %d arguments\n"),
106 argc-1, cmd, ct->argmin, ct->argmax);
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 bmap_init();
131 fadvise_init();
132 file_init();
133 freeze_init();
134 fsync_init();
135 help_init();
136 inject_init();
137 mmap_init();
138 open_init();
139 pread_init();
140 prealloc_init();
141 pwrite_init();
142 quit_init();
143 resblks_init();
144 sendfile_init();
145 shutdown_init();
146 truncate_init();
147 }