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