1 /* cmd-default-id.c
2 *
3 ****************************************************************
4 * Copyright (C) 2001, 2002, 2003 Tom Lord
5 *
6 * See the file "COPYING" for further information about
7 * the copyright and warranty status of this work.
8 */
9
10
11 #include "config-options.h"
12 #include "hackerlab/cmd/main.h"
13 #include "tla/libarch/inv-ids.h"
14 #include "tla/libarch/cmd.h"
15 #include "tla/libarch/cmd-default-id.h"
16
17
18
19 static t_uchar * usage = "[options] [TAG-PREFIX]";
20 static t_uchar * version_string = (cfg__std__package " from regexps.com\n"
21 "\n"
22 "Copyright 2001, 2002, 2003 Tom Lord\n"
23 "\n"
24 "This is free software; see the source for copying conditions.\n"
25 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n"
26 "PARTICULAR PURPOSE.\n"
27 "\n"
28 "Report bugs to " cfg__tla_bug_mail ".\n"
29 "\n"
30 cfg__std__release_id_string
31 "\n");
32
33 #define OPTS(OP) \
34 OP (opt_help_msg, "h", "help", 0, \
35 "Display a help message and exit.") \
36 OP (opt_long_help, "H", 0, 0, \
37 "Display a verbose help message and exit.") \
38 OP (opt_version, "V", "version", 0, \
39 "Display a release identifier string\n" \
40 "and exit.") \
41 OP (opt_dir, "D", "dir DIR", 1, \
42 "cd to DIR first") \
43 OP (opt_delete, "d", "delete", 0, \
44 "remove the default") \
45 OP (opt_strong, "s", "strong", 0, \
46 "use the strong default (default)") \
47 OP (opt_weak, "w", "weak", 0, \
48 "use the weak default") \
49 OP (opt_dont_care, 0, "dont-care", 0, \
50 "use the dont-care default")
51
52
53 t_uchar arch_cmd_default_id_help[] = ("print or modify default ids\n"
54 "For files in this directory, use:\n"
55 "\n"
56 " ID-PREFIX__BASENAME\n"
57 "\n"
58 "as the default explicit id for all files in this directory that\n"
59 "do not have an explicit explicit id.\n"
60 "\n"
61 "With no arguments, print the previously set ID-PREFIX.\n"
62 "\n"
63 "By default, this command sets, prints or deletes a \"strong\n"
64 "default\" -- a default explicit id which overrides implicit ids.\n"
65 "\n"
66 "With --weak, set (or print) a weak default id which is\n"
67 "overridden by explicit ids.\n"
68 "\n"
69 "The --dont-care option sets (or with -d, clears) a flag for that\n"
70 "directory that causes unidged files not to be reported as such\n"
71 "in \"tla tree-lint\" reports.\n");
72
73 enum options
74 {
75 OPTS (OPT_ENUM)
76 };
77
78 static struct opt_desc opts[] =
79 {
80 OPTS (OPT_DESC)
81 {-1, 0, 0, 0, 0}
82 };
83
84
85
86 enum which_default
87 {
88 strong,
89 weak,
90 dont_care
91 };
92
93 enum op
94 {
95 set,
96 delete,
97 print
98 };
99
100 int
arch_cmd_default_id(t_uchar * program_name,int argc,char * argv[])101 arch_cmd_default_id (t_uchar * program_name, int argc, char * argv[])
102 {
103 int o;
104 t_uchar * dir;
105 struct opt_parsed * option;
106 enum op op;
107 enum which_default which;
108 t_uchar * set_value;
109
110
111 safe_buffer_fd (1, 0, O_WRONLY, 0);
112
113 option = 0;
114 dir = ".";
115 op = print;
116 which = strong;
117 set_value = 0;
118
119
120 while (1)
121 {
122 o = opt_standard (lim_use_must_malloc, &option, opts, &argc, argv, program_name, usage, version_string, arch_cmd_default_id_help, opt_help_msg, opt_long_help, opt_version);
123 if (o == opt_none)
124 break;
125 switch (o)
126 {
127 default:
128 safe_printfmt (2, "unhandled option `%s'\n", option->opt_string);
129 panic ("internal error parsing arguments");
130
131 usage_error:
132 opt_usage (2, argv[0], program_name, usage, 1);
133 exit (1);
134
135 /* bogus_arg: */
136 safe_printfmt (2, "ill-formed argument for `%s' (`%s')\n", option->opt_string, option->arg_string);
137 goto usage_error;
138
139 case opt_dir:
140 {
141 dir = str_save (0, option->arg_string);
142 break;
143 }
144
145 case opt_weak:
146 {
147 which = weak;
148 break;
149 }
150
151 case opt_strong:
152 {
153 which = strong;
154 break;
155 }
156
157 case opt_dont_care:
158 {
159 which = dont_care;
160 break;
161 }
162
163 case opt_delete:
164 {
165 op = delete;
166 break;
167 }
168 }
169 }
170
171 if (op == delete ? (argc > 1) : (argc > 2))
172 goto usage_error;
173
174 if (argc == 2)
175 op = set;
176
177 switch (op)
178 {
179 default:
180 panic ("default-id hosed");
181 exit (2); /* notreached */
182
183 case set:
184 {
185 t_uchar * id_prefix;
186
187 id_prefix = argv[1];
188
189 switch (which)
190 {
191 default:
192 panic ("default-id hosed");
193 exit (2); /* notreached */
194
195 case strong:
196 {
197 arch_set_strong_explicit_default (dir, id_prefix);
198 break;
199 }
200 case weak:
201 {
202 arch_set_weak_explicit_default (dir, id_prefix);
203 break;
204 }
205 case dont_care:
206 {
207 arch_set_dont_care_explicit_default (dir);
208 break;
209 }
210 }
211 break;
212 }
213
214 case delete:
215 {
216 switch (which)
217 {
218 default:
219 panic ("default-id hosed");
220 exit (2); /* notreached */
221
222 case strong:
223 {
224 arch_delete_strong_explicit_default (dir);
225 break;
226 }
227 case weak:
228 {
229 arch_delete_weak_explicit_default (dir);
230 break;
231 }
232 case dont_care:
233 {
234 arch_delete_dont_care_explicit_default (dir);
235 break;
236 }
237 }
238 break;
239 }
240
241 case print:
242 {
243 t_uchar * type;
244 t_uchar * file;
245
246 switch (which)
247 {
248 default:
249 panic ("default-id hosed");
250 exit (2); /* notreached */
251
252 case strong:
253 {
254 file = arch_strong_explicit_dflt_file (dir);
255 type = "strong";
256
257 print_id_file_contents:
258
259 if (safe_access (file, F_OK))
260 {
261 safe_printfmt (2, "%s explicit default not set\n", type);
262 exit (1);
263 }
264 else
265 {
266 int in_fd;
267 t_uchar * buf;
268 size_t len;
269
270 in_fd = safe_open (file, O_RDONLY, 0);
271 buf = 0;
272 len = 0;
273 safe_file_to_string (&buf, &len, in_fd);
274 safe_close (in_fd);
275 safe_write_retry (1, buf, len);
276 exit (0);
277 }
278 break;
279 }
280 case weak:
281 {
282 file = arch_weak_explicit_dflt_file (dir);
283 type = "weak";
284 goto print_id_file_contents;
285 }
286 case dont_care:
287 {
288 file = arch_dont_care_explicit_dflt_file (dir);
289
290 if (safe_access (file, F_OK))
291 safe_printfmt (1, "dont-care flag NOT set\n");
292 else
293 safe_printfmt (1, "dont-care flag SET\n");
294 break;
295 }
296 }
297 break;
298 }
299 }
300 exit (3); /* notreached */
301 return 0;
302 }
303
304
305
306 /* tag: Tom Lord Wed May 14 13:56:06 2003 (default-tag.c)
307 */
308