Debian Codegraph @

src/fid.c

raw symbols SCIP
1 /* fid.c -- list all tokens in the given file(s)
2 Copyright (C) 1986-2020 Free Software Foundation, Inc.
3 Written by Greg McGary <[email protected]>
4
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 3, or (at your option)
8 any later version.
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19 #include <config.h>
20 #include <stdio.h>
21 #include <getopt.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <stdlib.h>
25 #include <errno.h>
26 #include <alloca.h>
27 #include <error.h>
28 #include <pathmax.h>
29 #include <xalloc.h>
30
31 #include "closeout.h"
32 #include "xnls.h"
33 #include "idfile.h"
34 #include "iduglobal.h"
35 #include "progname.h"
36
37 static int get_file_index (char *file_name);
38 static int is_hit (unsigned char const *hits, int file_number);
39 static int is_hit_1 (unsigned char const **hits, int level, int file_number);
40 static void skip_hits (unsigned char const **hits, int level);
41 void usage (void) __attribute__((__noreturn__));
42
43 struct idhead idh;
44 static int tree8_levels;
45
46 /* If nonzero, display usage information and exit. */
47
48 static int show_help;
49
50 /* If nonzero, print the version on standard output then exit. */
51
52 static int show_version;
53
54 /* The file name of the ID database. */
55
56 static struct file_link *cw_dlink;
57 static struct file_link **members_0;
58 static unsigned int bits_vec_size;
59 static char *hits_buf;
60
61 static struct option const long_options[] =
62 {
63 { "file", required_argument, 0, 'f' },
64 { "help", no_argument, &show_help, 1 },
65 { "version", no_argument, &show_version, 1 },
66 {NULL, 0, NULL, 0}
67 };
68
69 void
70 usage (void)
71 {
72 fprintf (stderr, _("Try `%s --help' for more information.\n"),
73 program_name);
74 exit (EXIT_FAILURE);
75 }
76
77 static void __attribute__((__noreturn__))
78 help_me (void)
79 {
80 printf (_("\
81 Usage: %s [OPTION] FILENAME [FILENAME2]\n\
82 "), program_name);
83 printf (_("\
84 List identifiers that occur in FILENAME, or if FILENAME2 is\n\
85 also given list the identifiers that occur in both files.\n\
86 \n\
87 -f, --file=FILE file name of ID database\n\
88 --help display this help and exit\n\
89 --version output version information and exit\n\
90 "));
91 printf (_("\nReport bugs to " PACKAGE_BUGREPORT "\n\n"));
92 exit (EXIT_SUCCESS);
93 }
94
95 int
96 main (int argc, char **argv)
97 {
98 int index_1 = -1;
99 int index_2 = -1;
100
101 set_program_name (argv[0]);
102 idh.idh_file_name = 0;
103
104
105 #if ENABLE_NLS
106 /* Set locale according to user's wishes. */
107 setlocale (LC_ALL, "");
108
109 /* Tell program which translations to use and where to find. */
110 bindtextdomain (PACKAGE, LOCALEDIR);
111 textdomain (PACKAGE);
112 #endif
113
114 atexit (close_stdout);
115
116 for (;;)
117 {
118 int optc = getopt_long (argc, argv, "f:",
119 long_options, (int *) 0);
120 if (optc < 0)
121 break;
122 switch (optc)
123 {
124 case 0:
125 break;
126
127 case 'f':
128 idh.idh_file_name = optarg;
129 break;
130
131 default:
132 usage ();
133 }
134 }
135
136 if (show_version)
137 {
138 printf ("%s - %s\n", program_name, PACKAGE_VERSION);
139 exit (EXIT_SUCCESS);
140 }
141
142 if (show_help)
143 help_me ();
144
145 argc -= optind;
146 argv += optind;
147 if (argc < 1)
148 {
149 error (0, 0, _("no file name arguments"));
150 usage ();
151 }
152 if (argc > 2)
153 {
154 error (0, 0, _("too many file name arguments"));
155 usage ();
156 }
157
158 /* Look for the ID database up the tree */
159 idh.idh_file_name = locate_id_file_name (idh.idh_file_name);
160 if (idh.idh_file_name == 0)
161 error (EXIT_FAILURE, errno, _("can't locate `ID'"));
162
163 init_idh_obstacks (&idh);
164 init_idh_tables (&idh);
165
166 cw_dlink = get_current_dir_link ();
167
168 /* Determine absolute name of the directory name to which database
169 constituent files are relative. */
170 members_0 = read_id_file (idh.idh_file_name, &idh);
171 bits_vec_size = (idh.idh_files + 7) / 4; /* more than enough */
172 tree8_levels = tree8_count_levels (idh.idh_files);
173
174 index_1 = get_file_index ((argc--, *argv++));
175 if (argc)
176 index_2 = get_file_index ((argc--, *argv++));
177
178 if (index_1 < 0)
179 return 1;
180
181 hits_buf = xmalloc (idh.idh_buf_size);
182 fseek (idh.idh_FILE, idh.idh_tokens_offset, SEEK_SET);
183 {
184 int count = 0;
185 int i;
186 int separator = (isatty (STDOUT_FILENO) ? ' ' : '\n');
187
188 for (i = 0; i < idh.idh_tokens; i++)
189 {
190 unsigned char const *hits;
191
192 gets_past_00 (hits_buf, idh.idh_FILE);
193 hits = token_hits_addr (hits_buf);
194 if (is_hit (hits, index_1) && (index_2 < 0 || is_hit (hits, index_2)))
195 {
196 fputs (token_string (hits_buf), stdout);
197 putchar (separator);
198 count++;
199 }
200 }
201 if (count && separator == ' ')
202 putchar ('\n');
203 }
204
205 return 0;
206 }
207
208 static int
209 get_file_index (char *file_name)
210 {
211 struct file_link **members;
212 struct file_link **end = &members_0[idh.idh_files];
213 struct file_link *fn_flink = 0;
214 char *file_name_buf = alloca (PATH_MAX);
215 int has_slash = (strchr (file_name, '/') != 0);
216 size_t file_name_length = strlen (file_name);
217 int idx = -1;
218
219 if (strstr (file_name, "./"))
220 fn_flink = parse_file_name (file_name, cw_dlink);
221
222 for (members = members_0; members < end; members++)
223 {
224 struct file_link *flink = *members;
225 if (fn_flink)
226 {
227 if (fn_flink != flink)
228 continue;
229 }
230 else if (has_slash)
231 {
232 size_t member_length;
233 maybe_relative_file_name (file_name_buf, flink, cw_dlink);
234 member_length = strlen (file_name_buf);
235 if (file_name_length > member_length
236 || !strequ (&file_name_buf[member_length - file_name_length],
237 file_name))
238 continue;
239 }
240 else if (!strequ (flink->fl_name, file_name))
241 continue;
242 if (idx >= 0)
243 {
244 error (0, 0, _("`%s' is ambiguous"), file_name);
245 return -1;
246 }
247 idx = members - members_0;
248 }
249 if (idx < 0)
250 error (0, 0, _("`%s' not found"), file_name);
251 return idx;
252 }
253
254 static int
255 is_hit (unsigned char const *hits, int file_number)
256 {
257 return is_hit_1 (&hits, tree8_levels, file_number);
258 }
259
260 static int
261 is_hit_1 (unsigned char const **hits, int level, int file_number)
262 {
263 int file_hit = 1 << ((file_number >> (3 * --level)) & 7);
264 int hit = *(*hits)++;
265 int bit;
266
267 if (!(file_hit & hit))
268 return 0;
269 if (level == 0)
270 return 1;
271
272 for (bit = 1; (bit < file_hit) && (bit & 0xff); bit <<= 1)
273 {
274 if (hit & bit)
275 skip_hits (hits, level);
276 }
277 return is_hit_1 (hits, level, file_number);
278 }
279
280 void
281 skip_hits (unsigned char const **hits, int level)
282 {
283 int hit = *(*hits)++;
284 int bit;
285
286 if (--level == 0)
287 return;
288 for (bit = 1; bit & 0xff; bit <<= 1)
289 {
290 if (hit & bit)
291 skip_hits (hits, level);
292 }
293 }