File: extract.c

package info (click to toggle)
swish++ 1.1b3-3
  • links: PTS
  • area: main
  • in suites: slink
  • size: 416 kB
  • ctags: 409
  • sloc: ansic: 2,842; makefile: 247; sh: 48
file content (385 lines) | stat: -rw-r--r-- 9,156 bytes parent folder | download
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
/*
**	SWISH++
**	extract.c
**
**	Copyright (C) 1998  Paul J. Lucas
**
**	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 of the License, 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

// standard
#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <fstream.h>
#include <iomanip.h>
#include <iostream.h>
#include <string>
#include <sys/stat.h>
#include <sys/types.h>

// local
#include "config.h"
#include "directory.h"
#include "ext_proc.h"
#include "fake_ansi.h"
#include "file_vector.h"
#include "postscript.h"
#include "string_set.h"
#include "util.h"
#include "version.h"

extern "C" {
	extern char*	optarg;
	extern int	optind, opterr;
}

#ifndef	PJL_NO_NAMESPACES
using namespace std;
#endif

string_set	extensions;		// file extensions to index
char const*	me;			// executable name
int		num_files;
int		verbosity;		// how much to print

void		do_file( char const *path );
void		usage();

//*****************************************************************************
//
// SYNOPSIS
//
	int main( int argc, char *argv[] )
//
// DESCRIPTION
//
//	Parse the command line, initialize, call other functions ... the
//	usual things that are done in main().
//
// PARAMETERS
//
//	argc	The number of arguments.
//
//	argv	A vector of the arguments; argv[argc] is null.  Aside from
//		the options below, the arguments are the names of the files
//		and directories to extract.
//
// SEE ALSO
//
//	Stroustrup, Bjarne.  "The C++ Programming Language, 3rd ed."
//	Addison-Wesley, Reading, MA.  pp. 116-118.
//
//*****************************************************************************
{
	me = ::strrchr( argv[0], '/' );		// determine base name
	me = me ? me + 1 : argv[0];		// of executable

	/////////// Process command-line options //////////////////////////////

	::opterr = 1;
	for ( int opt;
		(opt = ::getopt( argc, (char**)argv, "e:lv:V" )) != EOF;
	)
		switch ( opt ) {

			case 'e': // Specify filename extension(s) to index.
				extensions.insert( ::optarg );
				break;

			case 'l': // Follow symbolic links during indexing.
				follow_symbolic_links = true;
				break;

			case 'v': // Specify verbosity level.
				verbosity = ::atoi( ::optarg );
				if ( verbosity < 0 )
					verbosity = 0;
				else if ( verbosity > 3 )
					verbosity = 3;
				break;

			case 'V': // Display version and exit.
				cout << "SWISH++ " << version << endl;
				::exit( 0 );

			case '?': // Bad option.
				usage();
		}

	argc -= ::optind, argv += ::optind;
	if ( !argc )
		usage();

	////////// Extract text from specified files //////////////////////////

	time_t time = ::time( 0 );
	while ( *argv ) {
		if ( is_directory( *argv ) )
			do_directory( *argv );
		else
			do_file( *argv );
		++argv;
	}

	if ( verbosity ) { 
		time = ::time( 0 ) - time;
		cout	<< setfill('0')
			<< "\nExtraction done:\n  "
			<< setw(2) << (time / 60) << ':'
			<< setw(2) << (time % 60)
			<< " elapsed time\n  "
			<< num_files << " files\n\n"
			<< setfill(' ');
	}

	return 0;
}

//*****************************************************************************
//
// SYNOPSIS
//
	void do_file( char const *file_name )
//
// DESCRIPTION
//
//	Extract the text.  This algorithm is loosely based on what the Unix
//	strings(1) command does except it goes a bit further to discard
//	things like Encapsulated PostScript and raw hex data.
//
// PARAMETERS
//
//	The file to extract text from.
//
//*****************************************************************************
{
	////////// Determine if we should process the file ////////////////////

	if ( !is_plain_file() ||
		is_symbolic_link( file_name ) && !follow_symbolic_links
	)
		return;

	//
	// Check to see if the file name has a '.' in it and that it is not the
	// last character.
	//
	char const *ext = ::strrchr( file_name, '.' );
	if ( !ext || !*++ext )
		return;

        char const *orig_file_name = file_name;

	//
	// If the candidate extension contains a '/', then it's really not an
	// extension; rather, it's a file name like: "/a.bizarre/file".
	//
	if ( ::strchr( ext, '/' ) )
		return;

	//
	// Determine if the file needs to be preprocessed first.
	//
	static ext_proc_map ext_procs;
	ext_proc_map::value_type const *const proc = ext_procs[ ext ];

	if ( proc ) {
		static char ext_buf[ 10 ];
		//
		// Get the "real" filename extension, e.g., get "txt" out of
		// "foo.txt.gz".
		//
		register char const *p;
		for ( p = ext - 2; p > file_name; --p )
			if ( *p == '.' ) {
				::copy( p + 1, ext - 1, ext_buf );
				ext_buf[ ext - p - 2 ] = '\0';
				break;
			}
		if ( *p != '.' ) {
			//
			// File doesn't have a "real" extension, e.g., it's
			// something like "bar.gz".
			//
			return;
		}
		static string fixed_file_name;
		fixed_file_name = string( file_name, ext - 1 );
		file_name = fixed_file_name.c_str();
		ext = ext_buf;
	}

	//
	// Skip the file if the set of acceptable extensions does not contain
	// the candidate.
	//
	if ( !extensions.find( ext ) )
		return;

	//
	// Check to see if the .txt file already exists; if so, skip it.
	//
	string const file_name_txt = string( file_name ) + ".txt";
	if ( ::stat( file_name_txt.c_str(), &stat_buf ) != -1 )
		return;
	ofstream txt( file_name_txt.c_str() );
	if ( !txt )
		return;

	if ( proc && !process_file( proc->undo, orig_file_name ) )
		return;

	file_vector<char> file( file_name );
	if ( !file )
		return;

	if ( verbosity > 2 ) {			// print base name of file
		char const *const slash = ::strrchr( file_name, '/' );
		cout << "  " << ( slash ? slash + 1 : file_name ) << flush;
	}

	////////// Parse the file /////////////////////////////////////////////

	++num_files;

	char word_buf[ Word_Hard_Max_Size + 1 ];
	register char *word;
	int word_len;
	int num_words = 0;
	bool in_word = false;
	bool in_postscript = false;

	register file_vector<char>::const_iterator c = file.begin();
	while ( c != file.end() ) {
		register char ch = *c++;

		////////// Collect a word /////////////////////////////////////

		if ( is_word_char( ch ) || ch == '%' ) {
			if ( !in_word ) {
				// start a new word
				word = word_buf;
				word[ 0 ] = ch;
				word_len = 1;
				in_word = true;
				continue;
			}
			if ( word_len < Word_Hard_Max_Size ) {
				// continue same word
				word[ word_len++ ] = ch;
				continue;
			}
			in_word = false;		// too big: skip chars
			while ( c != file.end() && is_word_char( *c++ ) ) ;
			continue;
		}

		if ( !in_word )
			continue;

		////////// Got a word /////////////////////////////////////////

		in_word = false;
		if ( word_len < Word_Hard_Min_Size )
			continue;

		word[ word_len ] = '\0';

		//
		// Look for Encapsulated PostScript code and skip it.
		//
		if ( in_postscript ) {
			if ( !::strcmp( word, "%%Trailer" ) )
				in_postscript = false;
			continue;
		}
		static postscript_comment_set postscript_comments;
		if ( postscript_comments.find( word ) ) {
			in_postscript = true;
			continue;
		}

		static postscript_operator_set postscript_operators;
		if ( postscript_operators.find( word ) )
			continue;

		//
		// Strip chars not in Word_End_Chars from end of word.
		//
		for ( register int i = word_len - 1; i >= 0; --i ) {
			if ( !::strchr( Word_End_Chars, tolower( word[ i ] ) ) )
				--word_len;
			else
				break;
		}
		if ( word_len < Word_Hard_Min_Size )
			continue;

		word[ word_len ] = '\0';

		//
		// Strip chars not in Word_Begin_Chars from beginning of word.
		//
		for ( register char const *p = word; *p; ++p ) {
			if ( !::strchr( Word_Begin_Chars, tolower( *p ) ) &&
				*p != '%'
			)
				--word_len, ++word;
			else
				break;
		}
		if ( word_len < Word_Hard_Min_Size )
			continue;

		//
		// Discard what looks like ASCII hex data.
		//
		if ( word_len >= Word_Hex_Min_Size &&
			::strspn( word, "0123456789abcdefABCDEF" ) == word_len
		)
			continue;

		if ( !is_ok_word( word ) )
			continue;

		++num_words;
		txt << word << '\n';
	}

	if ( verbosity > 2 )
		cout << " (" << num_words << " words)" << endl;

	if ( proc )			// restore file to the way it was
		process_file( proc->redo, file_name );
}

//*****************************************************************************
//
//	Miscellaneous function(s)
//
//*****************************************************************************

void usage() {
	cerr <<	"usage: " << me << " [options] dir ... file ...\n"
		" options:\n"
		" --------\n"
		"  -e ext          : Extension to index\n"
		"  -l              : Follow symbolic links\n"
		"  -v verbosity    : Verbosity level [0-3]\n"
		"  -V              : Print version number and exit\n";
	::exit( 1 );
}