]>
Commit | Line | Data |
---|---|---|
5aead01d | 1 | /* |
da23017d NS |
2 | * Copyright (c) 2003-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
5aead01d | 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 | |
5aead01d NS |
7 | * published by the Free Software Foundation. |
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. | |
5aead01d | 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 | |
5aead01d NS |
17 | */ |
18 | ||
6b803e5a CH |
19 | #include "path.h" |
20 | #include "command.h" | |
21 | #include "input.h" | |
5aead01d NS |
22 | #include "init.h" |
23 | ||
24 | char *progname; | |
25 | int exitcode; | |
26 | int expert; | |
29647c8d | 27 | bool foreign_allowed = false; |
5aead01d NS |
28 | |
29 | static char **projopts; /* table of project names (cmdline) */ | |
30 | static int nprojopts; /* number of entries in name table. */ | |
31 | ||
32 | static void | |
33 | add_project_opt( | |
34 | char *optarg) | |
35 | { | |
36 | nprojopts++; | |
37 | projopts = realloc(projopts, sizeof(char*) * nprojopts); | |
38 | if (!projopts) { | |
39 | perror("realloc"); | |
40 | exit(1); | |
41 | } | |
42 | projopts[nprojopts - 1] = optarg; | |
43 | } | |
44 | ||
45 | static void | |
46 | usage(void) | |
47 | { | |
48 | fprintf(stderr, | |
29647c8d | 49 | _("Usage: %s [-V] [-x] [-f] [-p prog] [-c cmd]... [-d project]... [path]\n"), |
5aead01d NS |
50 | progname); |
51 | exit(1); | |
52 | } | |
53 | ||
54 | void | |
55 | init_cvtnum( | |
cb7ba2b0 CH |
56 | unsigned int *blocksize, |
57 | unsigned int *sectsize) | |
5aead01d NS |
58 | { |
59 | *blocksize = 4096; | |
60 | *sectsize = 512; | |
61 | } | |
62 | ||
63 | static void | |
64 | init_commands(void) | |
65 | { | |
66 | edit_init(); | |
67 | free_init(); | |
68 | help_init(); | |
69 | path_init(); | |
70 | project_init(); | |
71 | quot_init(); | |
72 | quota_init(); | |
73 | quit_init(); | |
74 | report_init(); | |
75 | state_init(); | |
76 | } | |
77 | ||
78 | static int | |
79 | init_args_command( | |
80 | int index) | |
81 | { | |
82 | if (index >= fs_count) | |
83 | return 0; | |
84 | ||
85 | do { | |
86 | fs_path = &fs_table[index++]; | |
29647c8d BD |
87 | /* skip project quota entries */ |
88 | if ((fs_path->fs_flags & FS_PROJECT_PATH)) | |
89 | continue; | |
90 | ||
91 | /* only consider foreign filesystems if told so */ | |
92 | if (!foreign_allowed && (fs_path->fs_flags & FS_FOREIGN)) | |
93 | continue; | |
94 | ||
95 | /* We can use this one */ | |
96 | break; | |
97 | } while (index < fs_count); | |
5aead01d | 98 | |
fa13a00f NS |
99 | if (fs_path->fs_flags & FS_PROJECT_PATH) |
100 | return 0; | |
29647c8d BD |
101 | if (!foreign_allowed && (fs_path->fs_flags & FS_FOREIGN)) |
102 | return 0; | |
5aead01d NS |
103 | if (index > fs_count) |
104 | return 0; | |
105 | return index; | |
106 | } | |
107 | ||
29647c8d BD |
108 | static int |
109 | init_check_command( | |
110 | const cmdinfo_t *ct) | |
111 | { | |
b20b6c22 BD |
112 | if (!fs_path) |
113 | return 1; | |
114 | ||
2ba39cd3 | 115 | /* Always run commands that are valid for all fs types. */ |
b20b6c22 BD |
116 | if (ct->flags & CMD_ALL_FSTYPES) |
117 | return 1; | |
118 | ||
2ba39cd3 | 119 | /* If it's an XFS filesystem, always run the command. */ |
b20b6c22 BD |
120 | if (!(fs_path->fs_flags & FS_FOREIGN)) |
121 | return 1; | |
122 | ||
2ba39cd3 | 123 | /* If the user specified foreign filesystems are ok (-f), run cmd. */ |
b20b6c22 BD |
124 | if (foreign_allowed && |
125 | (ct->flags & CMD_FLAG_FOREIGN_OK)) | |
126 | return 1; | |
127 | ||
2ba39cd3 BD |
128 | /* If cmd not allowed on foreign fs, regardless of -f flag, skip it. */ |
129 | if (!(ct->flags & CMD_FLAG_FOREIGN_OK)) { | |
130 | fprintf(stderr, _("%s: command is for XFS filesystems only\n"), | |
131 | ct->name); | |
132 | return 0; | |
133 | } | |
134 | ||
135 | /* foreign fs, but cmd only allowed via -f flag. Skip it. */ | |
136 | fprintf(stderr, | |
137 | _("%s: foreign filesystem. Invoke xfs_quota with -f to enable.\n"), | |
b20b6c22 BD |
138 | ct->name); |
139 | return 0; | |
29647c8d BD |
140 | } |
141 | ||
5aead01d NS |
142 | static void |
143 | init( | |
144 | int argc, | |
145 | char **argv) | |
146 | { | |
147 | int c; | |
148 | ||
149 | progname = basename(argv[0]); | |
150 | setlocale(LC_ALL, ""); | |
151 | bindtextdomain(PACKAGE, LOCALEDIR); | |
152 | textdomain(PACKAGE); | |
153 | ||
29647c8d | 154 | while ((c = getopt(argc, argv, "c:d:D:fP:p:t:xV")) != EOF) { |
5aead01d NS |
155 | switch (c) { |
156 | case 'c': /* commands */ | |
157 | add_user_command(optarg); | |
158 | break; | |
159 | case 'd': | |
160 | add_project_opt(optarg); | |
161 | break; | |
29647c8d BD |
162 | case 'f': |
163 | foreign_allowed = true; | |
88dabc17 | 164 | break; |
5aead01d NS |
165 | case 't': |
166 | mtab_file = optarg; | |
167 | break; | |
168 | case 'D': | |
169 | projects_file = optarg; | |
170 | break; | |
171 | case 'P': | |
172 | projid_file = optarg; | |
173 | break; | |
174 | case 'p': | |
175 | progname = optarg; | |
176 | break; | |
177 | case 'x': | |
178 | expert++; | |
179 | break; | |
180 | case 'V': | |
181 | printf(_("%s version %s\n"), progname, VERSION); | |
182 | exit(0); | |
183 | default: | |
184 | usage(); | |
185 | } | |
186 | } | |
187 | ||
0900efe4 AE |
188 | fs_table_initialise(argc - optind, &argv[optind], nprojopts, projopts); |
189 | free(projopts); | |
5aead01d NS |
190 | |
191 | init_commands(); | |
192 | add_args_command(init_args_command); | |
29647c8d | 193 | add_check_command(init_check_command); |
36298cce DC |
194 | |
195 | /* | |
196 | * Ensure that global commands don't end up with an invalid path pointer | |
197 | * by setting the default device at the first specified on the CLI | |
198 | */ | |
199 | if (argc != optind) | |
200 | fs_path = fs_table_lookup(argv[optind], FS_MOUNT_POINT); | |
201 | else | |
202 | fs_path = &fs_table[0]; | |
5aead01d NS |
203 | } |
204 | ||
205 | int | |
206 | main( | |
207 | int argc, | |
208 | char **argv) | |
209 | { | |
210 | init(argc, argv); | |
211 | command_loop(); | |
212 | return exitcode; | |
213 | } |