summaryrefslogtreecommitdiffstats
path: root/fastjar/jargrep.c
blob: f879aee4e4625876f27f2afd13de09a0c017f699 (plain)
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
/*
  jargrep.c - main functions for jargrep utility
  Copyright (C) 2002, 2003 Free Software Foundation
  Copyright (C) 1999, 2000 Bryan Burns
  Copyright (C) 2000 Cory Hollingsworth 
 
  Parts of this program are base on Bryan Burns work with fastjar 
  Copyright (C) 1999. 

  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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*/

/* Id: jargrep.c,v 1.5 2002/01/03 04:57:56 rodrigc Exp

Log: jargrep.c,v 
Revision 1.5  2002/01/03 04:57:56  rodrigc
2001-01-02  Craig Rodrigues  <rodrigc@gcc.gnu.org>

        PR bootstrap/5117
        * configure.in (AC_CHECK_HEADERS): Check for stdlib.h.
        * Makefile.am: Move grepjar to bin_PROGRAMS.
        * config.h.in: Regenerated.
        * Makefile.in: Regenerated.
        * aclocal.m4: Regenerated.
        * jargrep.c: Eliminate some signed/unsigned and default
        uninitialized warnings. Use HAVE_STDLIB_H instead of
        STDC_HEADERS macro.
        * jartool.c: Likewise.
        * compress.c: Likewise.

Revision 1.4  2000/12/15 18:45:09  tromey
	* jargrep.c: Include getopt.h if it exists.
	(optind): Declare.
	* configure, config.h: Rebuilt.
	* configure.in: Check for getopt.h.

Revision 1.3  2000/12/14 18:45:35  ghazi
Warning fixes:

	* compress.c: Include stdlib.h and compress.h.
	(rcsid): Delete.
	(report_str_error): Make static.
	(ez_inflate_str): Delete unused variable.  Add parens in if-stmt.
	(hrd_inflate_str): Likewise.

	* compress.h (init_compression, end_compression, init_inflation,
	end_inflation): Prototype void arguments.

	* dostime.c (rcsid): Delete.

	* jargrep.c: Include ctype.h, stdlib.h, zlib.h and compress.h.
	Make functions static.  Cast ctype function argument to `unsigned
	char'.  Add parens in if-stmts.  Constify.
	(Usage): Change into a macro.
	(jargrep): Remove unused parameter.

	* jartool.c: Constify.  Add parens in if-stmts.  Align
	signed/unsigned char pointers in functions calls using casts.
	(rcsid): Delete.
	(list_jar): Fix printf format specifier.
	(usage): Chop long string into bits.  Reformat.

	* pushback.c (rcsid): Delete.

Revision 1.2  2000/12/11 02:59:55  apbianco
2000-12-10  Robert Lipe <robertlipe@usa.net>

	* jargrep.c (jargrep): Added null statement after case.

2000-12-10  Alexandre Petit-Bianco  <apbianco@cygnus.com>

	* Makefile: Removed.
	* Makefile.in: Rebuilt with `-i' and `--enable-foreign'.

(http://gcc.gnu.org/ml/gcc/2000-12/msg00294.html)

Revision 1.1  2000/12/09 03:08:23  apbianco
2000-12-08  Alexandre Petit-Bianco  <apbianco@cygnus.com>

        * fastjar: Imported.

Revision 1.8  2000/09/13 14:02:02  cory
Reformatted some of the code to more closly match the layout of the orriginal
fastjar utility.

Revision 1.7  2000/09/12 22:29:36  cory
Jargrep now seems to do what I want it to do.  Performs properly on Linux x86,
will test some other platforms later.


*/

#include "config.h"
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#include "xregex.h"

#include "jargrep.h"
#include "jartool.h"
#include "pushback.h"
#include "zipfile.h"
#include "zlib.h"
#include "compress.h"
#include <getopt.h>

void version(void);
void help(const char *name);

#define Usage "Usage: %s [-bcinsVw] [--version|--help] <-e PATTERN | PATTERN> FILE ...\n"

/*
Function name: opt_valid
arg:	options	Bitfield flag that contains the command line options of grepjar.
purpose:	To guard agains the occurance of certain incompatible flags being used
together.
returns: TRUE if options are valid, FALSE otherwise.
*/

static int opt_valid(int options) {
	int retflag;

	if((options & JG_PRINT_COUNT) && 
		(options & (JG_PRINT_BYTEOFFSET | JG_PRINT_LINE_NUMBER)))
	{
		retflag = FALSE;
	}
	else retflag = TRUE;

	return retflag;
}

/*
Function name: create_regexp
args:	regstr	String containing the uncompiled regular expression.  This may be the 
				expression as is passed in through argv.
		options	This is the flag containing the commandline options that have been
				parsed by getopt.
purpose: Handle the exception handling involved with setting upt a new regular 
expression.
returns: Newly allocated compile regular expression ready to be used in an regexec call.
*/

static regex_t *create_regexp(const char *regstr, int options) {
	regex_t *exp;
	int errcode;
	int msgsize;
	char *errmsg;

	if((exp = (regex_t *) malloc(sizeof(regex_t))))
	{
		if((errcode = regcomp(exp, regstr, (options & JG_IGNORE_CASE) ? REG_ICASE : 0))) {
			fprintf(stderr, "regcomp of regex failed,\n");
			if((errmsg = (char *) malloc(msgsize = regerror(errcode, exp, NULL, 0) + 1))) {
				regerror(errcode, exp, errmsg, msgsize);
				fprintf(stderr, "Error: %s\n", errmsg);
				free(exp);
				free(errmsg);
				exit(1);
			}
			else {
				fprintf(stderr, "Malloc of errmsg failed.\n");
				fprintf(stderr, "Error: %s\n", strerror(errno));
				free(exp);
				exit(1);
			}
		}
	}
	else {
		fprintf(stderr, "Malloc of regex failed,\n");
		fprintf(stderr, "Error: %s\n", strerror(errno));
		exit(1);
	}

	return exp; 
}

/*
Function name: check_sig
args:	scratch	Pointer to array of bytes containing signature.
		pbf		Pointer to push back handle for jar file.
purpose: 	Verify that checksum is correct.
returns: 0, 1, or 2.  0 means we are ready to read embedded file information.  1 means
we have read beyound the embedded file list and can exit knowing we have read all the
relevent information.  2 means we still haven't reached embdedded file list and need to
do some more reading.
*/
static int check_sig(ub1 *scratch, pb_file *pbfp) {
	ub4 signature;
	int retflag = 0;

	signature = UNPACK_UB4(scratch, 0);

#ifdef DEBUG    
    printf("signature is %x\n", signature);
#endif
    if(signature == 0x08074b50){
#ifdef DEBUG    
      printf("skipping data descriptor\n");
#endif
      pb_read(pbfp, scratch, 12);
      retflag = 2;
    } else if(signature == 0x02014b50){
#ifdef DEBUG    
      printf("Central header reached.. we're all done!\n");
#endif
      retflag = 1;
    }else if(signature != 0x04034b50){
      printf("Ick! %#x\n", signature);
      retflag = 1;
    }
    
	return retflag;
}

/*
Function name: decd_siz
args	csize		Pointer to embedded file's compressed size.
		usize		Pointer to embedded file's uncmpressed size.
		fnlen		Pointer to embedded file's file name length.
		elfen		Pointer to length of extra fields in jar file.
		flags		Pointer to bitmapped flags.
		method		Pointer to indicator of storage method of embedded file.
		file_header	Pointer to string containing the above values to be unbacked.
Purpose: Unpack the series of values from file_header.
*/

static void decd_siz(ub4 *csize, ub4 *usize, ub2 *fnlen, ub2 *eflen, ub2 *flags, ub2 *method, ub1 *file_header) {
    *csize = UNPACK_UB4(file_header, LOC_CSIZE);
#ifdef DEBUG    
    printf("Compressed size is %u\n", *csize);
#endif

	*usize = UNPACK_UB4(file_header, LOC_USIZE);
#ifdef DEBUG
	printf("Uncompressed size is %u\n", *usize);
#endif

    *fnlen = UNPACK_UB2(file_header, LOC_FNLEN);
#ifdef DEBUG    
    printf("Filename length is %hu\n", *fnlen);
#endif

    *eflen = UNPACK_UB2(file_header, LOC_EFLEN);
#ifdef DEBUG    
    printf("Extra field length is %hu\n", *eflen);
#endif

    *flags = UNPACK_UB2(file_header, LOC_EXTRA);
#ifdef DEBUG    
    printf("Flags are %#hx\n", *flags);
#endif

    *method = UNPACK_UB2(file_header, LOC_COMP);
#ifdef DEBUG
    printf("Compression method is %#hx\n", *method);
#endif

}

/*
Function name: new_filename
args:	pbf		Pointer to push back file handle.  Used for reading input file.
		len		Length of file name to be read.
purpose:	Read in the embedded file name from jar file.
returns: Pointer to newly allocated string containing file name.
*/

static char *new_filename(pb_file *pbf, ub4 len) {
	char *filename;

	if(!(filename = (char *) malloc(len + 1))) {
		fprintf(stderr, "Malloc failed of filename\n");
		fprintf(stderr, "Error: %s\n", strerror(errno));
	}
    pb_read(pbf, filename, len);
    filename[len] = '\0';

#ifdef DEBUG    
    printf("filename is %s\n", filename);
#endif

	return filename;
}

/*
Funtion name: read_string
args:	pbf		Pointer to push back file handle.  Used for reading input file.
		size	Size of embedded file in bytes.
purpose:	Create a string containing the contents of the embedded noncompressed file.
returns: Pointer to newly allocated string containing embedded file contents.
*/

static char *read_string(pb_file *pbf, int size) {
	char *page;
	
	if((page = (char *) malloc(size + 1))) {
		pb_read(pbf, page, size);
		page[size] = '\0';
	}
	else {
		fprintf(stderr, "Malloc of page buffer failed.\n");
		fprintf(stderr, "Error: %s\n", strerror(errno));
		exit(1);
	}

	return page;
}

/*
Function name: extract_line
args:	stream	String containing the full contents of a file which is to be substringed
				in order to provide line representing our grep output.
		begin	Index into stream which regular expression first matches.
		end		Index into stream which end of match to the regular expression.
		b		Pointer to the index of what will be the beginning of the line when
				string is returned.  Used for -b option.
purpose:	Create a string that can be printed by jargrep from the long string stream.
The matching line that is printed out by jargrep is generated by this function.
returns: Pointer to newly allocated string containing matched expression.
*/

static char *extract_line(const char *stream, regoff_t begin, regoff_t end, int *b) {
	int e;
	int length;
	char *retstr;

	for(*b = begin; *b >= 0 && !iscntrl((unsigned char)stream[*b]); (*b)--);
	(*b)++;
	for(e = end; stream[e] == '\t' || !iscntrl((unsigned char)stream[e]); e++);
	length = e - *b;
	if((retstr = (char *) malloc(length + 1))) {
		sprintf(retstr, "%d:", *b);
		strncpy(retstr, &(stream[*b]), length);
		retstr[length] = '\0';
	}
	else {
		fprintf(stderr, "Malloc failed of output string.\n");
		fprintf(stderr, "Error: %s\n", strerror(errno));
		exit(1);
	}

	return retstr;
}

/*
Function name: chk_wrd
args:	exp		Pointer to compiled POSIX style regular expression of search target.
		str		String known to contain at least one match of exp.
purpose: Verify that the occurance of the regular expression in str occurs as a whole
word and not a substring of another word.
returns: TRUE if it is a word, FALSE of it is a substring.
*/

static int chk_wrd(regex_t *exp, const char *str) {
	int wrd_fnd = FALSE;
	int frnt_ok;
	int bck_ok;
	const char *str2;
	regmatch_t match;

	str2 = str;
	frnt_ok = bck_ok = FALSE;
	while(!wrd_fnd && !regexec(exp, str2, 1, &match, 0)) {
		if(!match.rm_so && (str2 == str)) frnt_ok = TRUE;
		else if(!isalnum((unsigned char)str2[match.rm_so - 1])
			&& str2[match.rm_so - 1] != '_')
			frnt_ok = TRUE;
		else frnt_ok = FALSE;
		if(frnt_ok) {
			if(str2[match.rm_eo] == '\0') bck_ok = TRUE;
			else if(!isalnum((unsigned char)str2[match.rm_eo])
				&& str2[match.rm_eo] != '_')
				bck_ok = TRUE;
			else bck_ok = FALSE;
		}
		wrd_fnd = frnt_ok && bck_ok;
		str2 = &(str2[match.rm_eo]);
	}

	return wrd_fnd;
}

/*
Function name: prnt_mtchs
args:	exp			Pointer to compiled POSIX style regular expression of search target.
		filename	String containing the name of the embedded file which matches have
					been found in.
		stream		String containing the processed contents of the embedded jar file
					represended with filename.
		pmatch		Array of regmatch_t matches into stream.
		nl_offset	Array of offsets of '\n' characters in stream.  May be NULL if -n is
					not set on command line.
		num			Number of matches in pmatch array.
		lines		Number of lines in file.  Not set if -n is not set on command line.
		options		Bitwise flag containing flags set to represent the command line 
					options.
purpose:	Control output of jargrep.  Output is controlled by which options have been
set at the command line.
*/

static void prnt_mtchs(regex_t *exp, const char *filename, const char *stream, regmatch_t *pmatch, regmatch_t *nl_offset, int num, int lines, int options) {
	int i;
	int j = 0;
	int ln_cnt;
	int begin;
	int o_begin;
	char *str;

	o_begin = -1;
	ln_cnt = 0;
	for(i = 0; i < num; i++) {
		str = extract_line(stream, pmatch[i].rm_so, pmatch[i].rm_eo, &begin);
		if(begin > o_begin) {
			if(!(options & JG_WORD_EXPRESSIONS) || chk_wrd(exp, str)) {
				ln_cnt++;
				if(!(options & JG_PRINT_COUNT)) {
					printf("%s:", filename);
					if(options & JG_PRINT_LINE_NUMBER) {
						for(; j < lines && nl_offset[j].rm_so < begin; j++);
						printf("%d:", j + 1);
					}
					if(options & JG_PRINT_BYTEOFFSET) printf("%d:", begin);
					printf("%s\n", str);
				}
			}
		}
		o_begin = begin;
		free(str);
	}
	if(options & JG_PRINT_COUNT) printf("%s:%d\n", filename, ln_cnt);
}

/*
Function name: check_crc
args:	pbf		Pointer to pushback file pointer for jar file.
		stream	String containing the non modified contents fo the extraced file entry.
		usize	Size of file in bytes.
purpose:	Verify the CRC matches that as what is stored in the jar file.
*/

static void check_crc(pb_file *pbf, const char *stream, ub4 usize) {
	ub4 crc=0;
	ub4 lcrc;
	ub1 scratch[16];

	crc = crc32(crc, NULL, 0);
	crc = crc32(crc, (const unsigned char *)stream, usize);
	if(pb_read(pbf, scratch, 16) != 16) {
		perror("read");
        exit(1);
	}
	if(UNPACK_UB4(scratch, 0) != 0x08074b50) {
		fprintf(stderr, "Error! Missing data descriptor!\n");
		exit(1);
	}
	lcrc = UNPACK_UB4(scratch, 4);
	if(crc != lcrc){
    	fprintf(stderr, "Error! CRCs do not match! Got %x, expected %x\n",
              crc, lcrc);
      	exit(1);
    }
}

/*
Function name mk_ascii
args:	stream	String that contains the contents of the extraced file entry.
		usize	String size.
purpose:	Make certain that the contents of the file are ASCII, not binary.  This
permits grepping of binary files as well by converting non ASCII and control characters
into '\n'.
*/

static void mk_ascii(char *stream, size_t usize) {
	size_t i;

	for(i = 0; i < usize; i++) 
		if(stream[i] != '\t'
		   && (iscntrl((unsigned char)stream[i])
		       || (unsigned char) stream[i] >= 128))
			stream[i] = '\n';
}

/*
Funtion name: fnd_match
args:	exp			Pointer to compiled POSIX style regular expression of search target.
		str_stream	String that contains the contents of the extracted file entry.
		i			Pointer to counter and index of matches.
purpose:	Search str_stream for occurances of the regular expression exp and create
an array of matches.
returns:  Pointer to newly allocated array of regmatch_t which gives indexes to start
and end of matches.  NULL is returned upon no matches found.
*/

static regmatch_t *fnd_match(regex_t *exp, const char *str_stream, int *i) {
	int regflag;
	regmatch_t match;
	regmatch_t *match_array;
	regmatch_t *tmp;

	match_array = NULL;
	for(*i = 0, regflag = regexec(exp, str_stream, 1, &match, 0); !regflag; 
		regflag = regexec(exp, &(str_stream[match.rm_eo]), 1, &match, 0), (*i)++)
	{
		if((tmp = (regmatch_t *) 
		    realloc(match_array, sizeof(regmatch_t) * ((*i) + 1))))
		{
			match_array = tmp;
			if(*i) {
				match.rm_so += match_array[(*i) - 1].rm_eo;
				match.rm_eo += match_array[(*i) - 1].rm_eo;
			}
			match_array[*i] = match;
		}
		else {
			fprintf(stderr, "Realloc of match_array failed.\n");
			fprintf(stderr, "Error: %s\n", strerror(errno));
			exit(1);
		}
	} 

	return match_array;
}

/*
Function name: cont_grep
args:	exp		Pointer to compiled POSIX style regular expression of search target.
		nl_exp	Pointer to compiled POSIX style regular expression of newlines.  This
				argument is NULL unless the -n option is used on the command line.
		fd		File descriptor of the jar file being grepped.
		pbf		Pointer to pushback file style file stream.  This is for use with
				the pushback.c file io funtions.
		options	Bitwise flag containing flags set to represent the command line options.
purpose:	This function handles single entries in an open jar file.  The header is
read and then the embeded file is extracted and grepped.
returns: FALSE upon failure, TRUE otherwise.
*/

static int cont_grep(regex_t *exp, regex_t *nl_exp, int fd, pb_file *pbf, int options) {
	int retflag = TRUE;
	int i;
	int j;
	ub4 csize;
	ub4 usize;
	ub2 fnlen;
	ub2 eflen;
	ub2 flags;
	ub2 method;
	ub1 file_header[30];
	char *filename;
	char *str_stream;
	regmatch_t *match_array;
	regmatch_t *nl_offsets=0;

	if(pb_read(pbf, (file_header + 4), 26) != 26) {
		perror("read");
		retflag = FALSE;
   	}
	else {
		decd_siz(&csize, &usize, &fnlen, &eflen, &flags, &method, file_header);
		filename = new_filename(pbf, fnlen);
		lseek(fd, eflen, SEEK_CUR);
		if(filename[fnlen - 1] != '/') {
			str_stream = (method == 8 || (flags & 0x0008)) ? 
				(char *) inflate_string(pbf, &csize, &usize) : 
					read_string(pbf, csize);
			if(flags & 0x008) check_crc(pbf, str_stream, usize);
			mk_ascii(str_stream, usize);
			match_array = fnd_match(exp, str_stream, &i);
			if((options & JG_PRINT_LINE_NUMBER) && i) 
				nl_offsets = fnd_match(nl_exp, str_stream, &j);
			prnt_mtchs(exp, filename, str_stream, match_array, nl_offsets, i, j, options);
			if(match_array) free(match_array);
			free(str_stream);
		}
		free(filename);
		retflag = TRUE;
	}

	return retflag;
}

/*
Funtion name: jargrep
args:	exp		Pointer to compiled POSIX style regular expression of search target.
		nl_exp	Pointer to compiled regular expression for newlines or NULL.  Only set 
				if -n option is present at command line.
		jarfile	Filename of jar file to be searched.
		options	Bitwise flag containing flags set to represent the command line options.
purpose:	Open jar file.  Check signatures.  When right signature is found go to deeper
grep routine.
*/

static void jargrep(regex_t *exp, regex_t *nl_exp, const char *jarfile, int options){
	int fd;
	int floop = TRUE;
	pb_file pbf;
	ub1 scratch[16];

	if((fd = open(jarfile, O_RDONLY)) == -1) {
		if(!(options & JG_SUPRESS_ERROR))
			fprintf(stderr, "Error reading file '%s': %s\n", jarfile, strerror(errno));
	}
	else {
		pb_init(&pbf, fd);	
		
		do {
			if(pb_read(&pbf, scratch, 4) != 4) {
				perror("read");
				floop = FALSE;
			}
			else {
				switch (check_sig(scratch, &pbf)) {
				case 0:
					floop = cont_grep(exp, nl_exp, fd, &pbf, options);
					break;
				case 1:
					floop = FALSE;
					break;
				case 2:
					/* fall through continue */
					;
				}
			}
		} while(floop);
	}
}

/* This is used to mark options with no short value.  */
#define LONG_OPT(Num)  ((Num) + 128)

#define OPT_HELP     LONG_OPT (0)

static const struct option option_vec[] =
{
  { "help", no_argument, NULL, OPT_HELP },
  { "version", no_argument, NULL, 'V' },
  { NULL, no_argument, NULL, 0 }
};

/*
Funtion Name: main
args:	argc	number of in coming args.
		argv	array of strings.
purpose: Entry point of the program.  Parse command line arguments and set options.
Set up regular expressions.  Call grep routines for each file as input.
returns: 1 on error 0 on success.
*/

int main(int argc, char **argv) {
	int c;
	int retval = 0;
	int fileindex;
	int options = 0;
	regex_t *regexp;
	regex_t *nl_exp = NULL;
	char *regexpstr = NULL;

	while((c = getopt_long(argc, argv, "bce:insVw",
			       option_vec, NULL)) != -1) {
		switch(c) {
			case 'b':
				options |= JG_PRINT_BYTEOFFSET;
				break;
			case 'c':
				options |= JG_PRINT_COUNT;
				break;
			case 'e':
				if(!(regexpstr = (char *) malloc(strlen(optarg) + 1))) {
					fprintf(stderr, "Malloc failure.\n");
					fprintf(stderr, "Error: %s\n", strerror(errno));
					exit(1);
				}
				strcpy(regexpstr, optarg);
				break;
			case 'i':
				options |= JG_IGNORE_CASE;
				break;
			case 'n':
				options |= JG_PRINT_LINE_NUMBER;
				break;
			case 's':
				options |= JG_SUPRESS_ERROR;
				break;
			case 'v':
				options |= JG_INVERT;
				break;
			case 'V':
				version ();
				break;
			case 'w':
				options |= JG_WORD_EXPRESSIONS;
				break;
			case OPT_HELP:
				help(argv[0]);
				break;
			default:
				fprintf(stderr, Usage, argv[0]);
				exit(1);
		}
	}
	if(!regexpstr){
		if(((argc - optind) >= 2)) {
			regexpstr = argv[optind];
			fileindex = optind + 1;
		}
		else {
			fprintf(stderr, "Invalid arguments.\n");
			fprintf(stderr, Usage, argv[0]);
			exit(1);
		}
	}
	else if((argc - optind) == 1) {
		fileindex = optind;
	}
	else {
		fprintf(stderr, "Invalid arguments.\n");
		fprintf(stderr, Usage, argv[0]);
		exit(1);
	}

	if(opt_valid(options)) {
		regexp = create_regexp(regexpstr, options);
		if(options & JG_PRINT_LINE_NUMBER) nl_exp = create_regexp("\n", 0);
		init_inflation();
		for(; fileindex < argc; fileindex++)
			jargrep(regexp, nl_exp, argv[fileindex], options);
		regfree(regexp);
		if(options & JG_PRINT_LINE_NUMBER) regfree(nl_exp);
	}
	else {
		retval = 1;
		fprintf(stderr, "Error: Invalid combination of options.\n");
	}

	return retval;
}

void help(const char *filename)
{
  printf (Usage, filename);
  printf ("\
\n\
Search files in a jar file for a pattern.\n\
\n\
   -b                print byte offset of match\n\
   -c                print number of matches\n\
   -i                compare case-insensitively\n\
   -n                print line number of each match\n\
   -s                suppress error messages\n\
   -w                force PATTERN to match only whole words\n\
   -e PATTERN        use PATTERN as regular expression\n\
   -V|--version      print version number and exit\n\
   --help            print help\n\
");

  exit (0);
}

void version ()
{
  printf("grepjar (%s) %s\n\n", PACKAGE, VERSION);
  printf("Copyright 1999, 2000, 2001  Bryan Burns\n");
  printf("Copyright 2000 Cory Hollingsworth\n");
  printf("Copyright 2002 Free Software Foundation\n");
  printf("\
This is free software; see the source for copying conditions.  There is NO\n\
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.\n");
  exit (0);
}
OpenPOWER on IntegriCloud