File: fid.c

package info (click to toggle)
id-utils 3.2d-2.1
  • links: PTS
  • area: main
  • in suites: potato
  • size: 1,808 kB
  • ctags: 1,677
  • sloc: ansic: 14,285; sh: 403; makefile: 262; perl: 255; sed: 93; lisp: 26
file content (284 lines) | stat: -rw-r--r-- 6,673 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* fid.c -- list all tokens in the given file(s)
   Copyright (C) 1986, 1995, 1996 Free Software Foundation, Inc.
   Written by Greg McGary <gkm@gnu.ai.mit.edu>

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */

#include <config.h>
#include <stdio.h>
#include <getopt.h>
#include "xstring.h"
#include "xunistd.h"
#include "xnls.h"
#include "idfile.h"
#include "error.h"
#include "pathmax.h"
#include "xmalloc.h"
#include "xalloca.h"

int get_file_index __P((char *file_name));
int is_hit __P((unsigned char const *hits, int file_number));
int is_hit_1 __P((unsigned char const **hits, int level, int file_number));
void skip_hits __P((unsigned char const **hits, int level));
void usage __P((void));

struct idhead idh;
int tree8_levels;

/* The name this program was run with. */

char const *program_name;

/* If nonzero, display usage information and exit.  */

static int show_help;

/* If nonzero, print the version on standard output then exit.  */

static int show_version;

/* The file name of the ID database.  */

struct file_link *cw_dlink;
struct file_link **members_0;
unsigned int bits_vec_size;
char *hits_buf;

static struct option const long_options[] =
{
  { "file", required_argument, 0, 'f' },
  { "help", no_argument, &show_help, 1 },
  { "version", no_argument, &show_version, 1 },
  { 0 }
};

void
usage (void)
{
  fprintf (stderr, _("Try `%s --help' for more information.\n"),
	   program_name);
  exit (1);
}

static void
help_me (void)
{
  printf (_("\
Usage: %s [OPTION] FILENAME [FILENAME2]\n\
"), program_name);
  printf (_("\
List identifiers that occur in FILENAME, or if FILENAME2 is\n\
also given list the identifiers that occur in both files.\n\
\n\
  -f, --file=FILE  file name of ID database\n\
      --help       display this help and exit\n\
      --version    output version information and exit\n\
"));
  exit (0);
}

int
main (int argc, char **argv)
{
  int index_1 = -1;
  int index_2 = -1;

  program_name = argv[0];
  idh.idh_file_name = 0;

  /* Set locale according to user's wishes.  */
  setlocale (LC_ALL, "");

  /* Tell program which translations to use and where to find.  */
  bindtextdomain (PACKAGE, LOCALEDIR);
  textdomain (PACKAGE);

  for (;;)
    {
      int optc = getopt_long (argc, argv, "f:",
			      long_options, (int *) 0);
      if (optc < 0)
	break;
      switch (optc)
	{
	case 0:
	  break;

	case 'f':
	  idh.idh_file_name = optarg;
	  break;

	default:
	  usage ();
	}
    }

  if (show_version)
    {
      printf ("%s - %s\n", program_name, PACKAGE_VERSION);
      exit (0);
    }

  if (show_help)
    help_me ();

  argc -= optind;
  argv += optind;
  if (argc < 1)
    {
      error (0, 0, _("no file name arguments"));
      usage ();
    }
  if (argc > 2)
    {
      error (0, 0, _("too many file name arguments"));
      usage ();
    }

  /* Look for the ID database up the tree */
  idh.idh_file_name = locate_id_file_name (idh.idh_file_name);
  if (idh.idh_file_name == 0)
    error (1, errno, _("can't locate `ID'"));

  init_idh_obstacks (&idh);
  init_idh_tables (&idh);

  cw_dlink = get_current_dir_link ();

  /* Determine absolute name of the directory name to which database
     constituent files are relative. */
  members_0 = read_id_file (idh.idh_file_name, &idh);
  bits_vec_size = (idh.idh_files + 7) / 4; /* more than enough */
  tree8_levels = tree8_count_levels (idh.idh_files);

  index_1 = get_file_index ((argc--, *argv++));
  if (argc)
    index_2 = get_file_index ((argc--, *argv++));

  if (index_1 < 0)
    return 1;

  hits_buf = xmalloc (idh.idh_buf_size);
  fseek (idh.idh_FILE, idh.idh_tokens_offset, SEEK_SET);
  {
    int count = 0;
    int i;
    int separator = (isatty (STDOUT_FILENO) ? ' ' : '\n');
    
    for (i = 0; i < idh.idh_tokens; i++)
      {
	unsigned char const *hits;

	gets_past_00 (hits_buf, idh.idh_FILE);
	hits = token_hits_addr (hits_buf);
	if (is_hit (hits, index_1) && (index_2 < 0 || is_hit (hits, index_2)))
	  {
	    fputs (token_string (hits_buf), stdout);
	    putchar (separator);
	    count++;
	  }
      }
    if (count && separator == ' ')
      putchar ('\n');
  }

  return 0;
}

int
get_file_index (char *file_name)
{
  struct file_link **members;
  struct file_link **end = &members_0[idh.idh_files];
  struct file_link *fn_flink = 0;
  char *file_name_buf = ALLOCA (char, PATH_MAX);
  int has_slash = (strchr (file_name, '/') != 0);
  int file_name_length = strlen (file_name);
  int idx = -1;

  if (strstr (file_name, "./"))
    fn_flink = parse_file_name (file_name, cw_dlink);

  for (members = members_0; members < end; members++)
    {
      struct file_link *flink = *members;
      if (fn_flink)
	{
	  if (fn_flink != flink)
	    continue;
	}
      else if (has_slash)
	{
	  int member_length;
	  maybe_relative_file_name (file_name_buf, flink, cw_dlink);
	  member_length = strlen (file_name_buf);
	  if (file_name_length > member_length
	      || !strequ (&file_name_buf[member_length - file_name_length], file_name))
	    continue;
	}
      else if (!strequ (flink->fl_name, file_name))
	continue;
      if (idx >= 0)
	{
	  error (0, 0, _("`%s' is ambiguous"), file_name);
	  return -1;
	}
      idx = members - members_0;
    }
  if (idx < 0)
    error (0, 0, _("`%s' not found"), file_name);
  return idx;
}

int
is_hit (unsigned char const *hits, int file_number)
{
  return is_hit_1 (&hits, tree8_levels, file_number);
}

int
is_hit_1 (unsigned char const **hits, int level, int file_number)
{
  int file_hit = 1 << ((file_number >> (3 * --level)) & 7);
  int hit = *(*hits)++;
  int bit;

  if (!(file_hit & hit))
    return 0;
  if (level == 0)
    return 1;

  for (bit = 1; (bit < file_hit) && (bit & 0xff); bit <<= 1)
    {
      if (hit & bit)
	skip_hits (hits, level);
    }
  return is_hit_1 (hits, level, file_number);
}

void
skip_hits (unsigned char const **hits, int level)
{
  int hit = *(*hits)++;
  int bit;

  if (--level == 0)
    return;
  for (bit = 1; bit & 0xff; bit <<= 1)
    {
      if (hit & bit)
	skip_hits (hits, level);
    }
}