1 /* cmd-file-find.c
2 *
3 ****************************************************************
4 * Copyright (C) 2003, 2004 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 "hackerlab/fs/file-names.h"
14 #include "tla/libfsutils/dir-as-cwd.h"
15 #include "tla/libarch/my.h"
16 #include "tla/libarch/libraries.h"
17 #include "tla/libarch/project-tree.h"
18 #include "tla/libarch/patch-logs.h"
19 #include "tla/libarch/inv-ids.h"
20 #include "tla/libarch/file-diffs.h"
21 #include "tla/libarch/namespace.h"
22 #include "tla/libfsutils/dir-as-cwd.h"
23 #include "tla/libarch/cmd.h"
24 #include "tla/libarch/cmd-file-diff.h"
25
26
27
28 static t_uchar * usage = "[options] file [revision]";
29 static t_uchar * version_string = (cfg__std__package " from regexps.com\n"
30 "\n"
31 "Copyright 2003 Tom Lord\n"
32 "\n"
33 "This is free software; see the source for copying conditions.\n"
34 "There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A\n"
35 "PARTICULAR PURPOSE.\n"
36 "\n"
37 "Report bugs to " cfg__tla_bug_mail ".\n"
38 "\n"
39 cfg__std__release_id_string
40 "\n");
41
42 #define OPTS(OP) \
43 OP (opt_help_msg, "h", "help", 0, \
44 "Display a help message and exit.") \
45 OP (opt_long_help, "H", 0, 0, \
46 "Display a verbose help message and exit.") \
47 OP (opt_version, "V", "version", 0, \
48 "Display a release identifier string\n" \
49 "and exit.") \
50 OP (opt_archive, "A", "archive", 1, \
51 "Override `my-default-archive'") \
52 OP (opt_new_file, "N", "new-file", 0, \
53 "Print missing file as `/dev/null'") \
54 OP (opt_unescaped, 0, "unescaped", 0, \
55 "show filenames in unescaped form")
56
57 t_uchar arch_cmd_file_find_help[] = ("find given version of file\n"
58 "Print location of file corresponding to FILE in a cached copy of REVISION\n"
59 "\n"
60 "The default patch level for a given version is the latest level for\n"
61 "which the project tree has a patch. The default archive and version\n"
62 "is as printed by \"tla tree-version\".\n");
63
64
65 enum options
66 {
67 OPTS (OPT_ENUM)
68 };
69
70 static struct opt_desc opts[] =
71 {
72 OPTS (OPT_DESC)
73 {-1, 0, 0, 0, 0}
74 };
75
76
77
78 int
arch_cmd_file_find(t_uchar * program_name,int argc,char * argv[])79 arch_cmd_file_find (t_uchar * program_name, int argc, char * argv[])
80 {
81 int o;
82 struct opt_parsed * option;
83 t_uchar * default_archive;
84 int escape_classes = arch_escape_classes;
85 int new_is_null = 0;
86 int status = 2;
87
88 default_archive = 0;
89
90 safe_buffer_fd (1, 0, O_WRONLY, 0);
91
92 option = 0;
93
94 while (1)
95 {
96 o = opt_standard (lim_use_must_malloc, &option, opts, &argc, argv, program_name, usage, version_string, arch_cmd_file_find_help, opt_help_msg, opt_long_help, opt_version);
97 if (o == opt_none)
98 break;
99 switch (o)
100 {
101 default:
102 safe_printfmt (2, "unhandled option `%s'\n", option->opt_string);
103 panic ("internal error parsing arguments");
104
105 usage_error:
106 opt_usage (2, argv[0], program_name, usage, 1);
107 exit (1);
108
109 /* bogus_arg: */
110 safe_printfmt (2, "ill-formed argument for `%s' (`%s')\n", option->opt_string, option->arg_string);
111 goto usage_error;
112
113 case opt_archive:
114 {
115 default_archive = str_save (0, option->arg_string);
116 break;
117 }
118 case opt_new_file:
119 {
120 new_is_null = 1;
121 break;
122 }
123 case opt_unescaped:
124 {
125 escape_classes = 0;
126 break;
127 }
128 }
129 }
130
131 if ((argc < 2) || (argc > 3))
132 goto usage_error;
133
134 if (default_archive && !arch_valid_archive_name (default_archive))
135 {
136 safe_printfmt (2, "%s: invalid archive name (%s)\n",
137 argv[0], default_archive);
138 exit (1);
139 }
140
141 {
142 t_uchar * filespec;
143 t_uchar * filedir = 0;
144 t_uchar * filedir_path = 0;
145 t_uchar * filedir_loc = 0;
146 t_uchar * file_tail = 0;
147 t_uchar * mod_loc = 0;
148 t_uchar * tree_root = 0;
149 t_uchar * revspec = 0;
150 t_uchar * archive = 0;
151 t_uchar * revision = 0;
152
153 filespec = argv[1];
154
155 filedir = file_name_directory_file (0, filespec);
156 if (!filedir)
157 filedir = str_save (0, ".");
158
159 filedir_path = directory_as_cwd (filedir);
160
161 tree_root = arch_tree_root (0, filedir, 0);
162 if (!tree_root)
163 {
164 safe_printfmt (2, "%s: file is not in a project tree (%s)\n",
165 argv[0], filespec);
166 exit (2);
167 }
168
169 invariant (!str_cmp_prefix (tree_root, filedir_path)
170 && (!filedir_path[str_length (tree_root)] || ('/' == filedir_path[str_length (tree_root)])));
171
172 /*filedir_loc = str_alloc_cat (0, "./", filedir_path + str_length (tree_root) + 1);*/
173 /* fix for bug 7502 */
174 if (str_length(filedir_path) == str_length(tree_root))
175 filedir_loc = str_save(0, "./");
176 else
177 filedir_loc = str_alloc_cat (0, "./", filedir_path + str_length (tree_root) + 1);
178 /* fix for bug 7502 */
179 file_tail = file_name_tail (0, filespec);
180 mod_loc = file_name_in_vicinity (0, filedir_loc, file_tail);
181
182
183 if (argc == 3)
184 {
185 revspec = str_save (0, argv[2]);
186 }
187 else
188 {
189 revspec = arch_tree_version (tree_root);
190 if (!revspec)
191 {
192 safe_printfmt (2, "%s: tree has no default version (%s)\n",
193 argv[0], tree_root);
194 exit (2);
195 }
196 }
197
198 if (!arch_valid_package_name (revspec, arch_maybe_archive, arch_req_version, 1))
199 {
200 safe_printfmt (2, "%s: invalid revision specification (%s)\n",
201 argv[0], revspec);
202 exit (2);
203 }
204
205
206 archive = arch_parse_package_name (arch_ret_archive, default_archive, revspec);
207 if (!archive)
208 {
209 safe_printfmt (2, "%s: no archive specified\n", argv[0]);
210 exit (2);
211 }
212
213
214 if (arch_valid_package_name (revspec, arch_maybe_archive, arch_req_version, 0))
215 {
216 t_uchar * version = 0;
217 t_uchar * level = 0;
218
219 version = arch_parse_package_name (arch_ret_package_version, 0, revspec);
220 level = arch_highest_patch_level (tree_root, archive, version);
221 revision = str_alloc_cat_many (0, version, "--", level, str_end);
222
223 lim_free (0, level);
224 lim_free (0, version);
225 }
226 else
227 {
228 revision = arch_parse_package_name (arch_ret_non_archive, 0, revspec);
229 }
230
231
232 status = arch_file_get_or_diff (1, tree_root, mod_loc, archive, revision, 0, new_is_null, escape_classes);
233
234
235 lim_free (0, filedir);
236 lim_free (0, filedir_path);
237 lim_free (0, filedir_loc);
238 lim_free (0, file_tail);
239 lim_free (0, mod_loc);
240 lim_free (0, tree_root);
241 lim_free (0, revspec);
242 lim_free (0, archive);
243 lim_free (0, revision);
244 }
245
246 lim_free (0, default_archive);
247
248 exit (status);
249 return status;
250 }
251
252
253
254
255 /* tag: Jan Hudec Fr, 11 Jul 2003 12:34:30 +0200 (cmd-file-find.c)
256 */
257