10Sstevel@tonic-gate /*
20Sstevel@tonic-gate * CDDL HEADER START
30Sstevel@tonic-gate *
40Sstevel@tonic-gate * The contents of this file are subject to the terms of the
50Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
60Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
70Sstevel@tonic-gate * with the License.
80Sstevel@tonic-gate *
90Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
100Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
110Sstevel@tonic-gate * See the License for the specific language governing permissions
120Sstevel@tonic-gate * and limitations under the License.
130Sstevel@tonic-gate *
140Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
150Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
160Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
170Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
180Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
190Sstevel@tonic-gate *
200Sstevel@tonic-gate * CDDL HEADER END
210Sstevel@tonic-gate */
220Sstevel@tonic-gate /*
230Sstevel@tonic-gate * Copyright 2005 Sun Microsystems, Inc. All rights reserved.
240Sstevel@tonic-gate * Use is subject to license terms.
250Sstevel@tonic-gate */
260Sstevel@tonic-gate
270Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
280Sstevel@tonic-gate /* All Rights Reserved */
290Sstevel@tonic-gate
300Sstevel@tonic-gate /* Copyright (c) 1987, 1988 Microsoft Corporation */
310Sstevel@tonic-gate /* All Rights Reserved */
320Sstevel@tonic-gate
330Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
340Sstevel@tonic-gate
350Sstevel@tonic-gate /*
360Sstevel@tonic-gate * fgrep -- print all lines containing any of a set of keywords
370Sstevel@tonic-gate *
380Sstevel@tonic-gate * status returns:
390Sstevel@tonic-gate * 0 - ok, and some matches
400Sstevel@tonic-gate * 1 - ok, but no matches
410Sstevel@tonic-gate * 2 - some error
420Sstevel@tonic-gate */
430Sstevel@tonic-gate
440Sstevel@tonic-gate #include <stdio.h>
450Sstevel@tonic-gate #include <ctype.h>
460Sstevel@tonic-gate #include <sys/types.h>
470Sstevel@tonic-gate #include <stdlib.h>
480Sstevel@tonic-gate #include <string.h>
490Sstevel@tonic-gate #include <locale.h>
500Sstevel@tonic-gate #include <libintl.h>
510Sstevel@tonic-gate #include <euc.h>
52*440Spd155743 #include <sys/stat.h>
53*440Spd155743 #include <fcntl.h>
540Sstevel@tonic-gate
550Sstevel@tonic-gate #include <getwidth.h>
560Sstevel@tonic-gate
570Sstevel@tonic-gate eucwidth_t WW;
580Sstevel@tonic-gate #define WIDTH1 WW._eucw1
590Sstevel@tonic-gate #define WIDTH2 WW._eucw2
600Sstevel@tonic-gate #define WIDTH3 WW._eucw3
610Sstevel@tonic-gate #define MULTI_BYTE WW._multibyte
620Sstevel@tonic-gate #define GETONE(lc, p) \
630Sstevel@tonic-gate cw = ISASCII(lc = (unsigned char)*p++) ? 1 : \
640Sstevel@tonic-gate (ISSET2(lc) ? WIDTH2 : \
650Sstevel@tonic-gate (ISSET3(lc) ? WIDTH3 : WIDTH1)); \
660Sstevel@tonic-gate if (--cw > --ccount) { \
670Sstevel@tonic-gate cw -= ccount; \
680Sstevel@tonic-gate while (ccount--) \
690Sstevel@tonic-gate lc = (lc << 7) | ((*p++) & 0177); \
700Sstevel@tonic-gate if (p >= &buf[fw_lBufsiz + BUFSIZ]) { \
710Sstevel@tonic-gate if (nlp == buf) { \
720Sstevel@tonic-gate /* Increase the buffer size */ \
730Sstevel@tonic-gate fw_lBufsiz += BUFSIZ; \
740Sstevel@tonic-gate if ((buf = realloc(buf, \
750Sstevel@tonic-gate fw_lBufsiz + BUFSIZ)) == NULL) { \
760Sstevel@tonic-gate exit(2); /* out of memory */ \
770Sstevel@tonic-gate } \
780Sstevel@tonic-gate nlp = buf; \
790Sstevel@tonic-gate p = &buf[fw_lBufsiz]; \
800Sstevel@tonic-gate } else { \
810Sstevel@tonic-gate /* shift the buffer contents down */ \
820Sstevel@tonic-gate (void) memmove(buf, nlp, \
830Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - nlp);\
840Sstevel@tonic-gate p -= nlp - buf; \
850Sstevel@tonic-gate nlp = buf; \
860Sstevel@tonic-gate } \
870Sstevel@tonic-gate } \
880Sstevel@tonic-gate if (p > &buf[fw_lBufsiz]) { \
890Sstevel@tonic-gate if ((ccount = fread(p, sizeof (char), \
900Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - p, fptr))\
910Sstevel@tonic-gate <= 0) break; \
920Sstevel@tonic-gate } else if ((ccount = fread(p, \
930Sstevel@tonic-gate sizeof (char), BUFSIZ, fptr)) <= 0) \
940Sstevel@tonic-gate break; \
950Sstevel@tonic-gate blkno += (long long)ccount; \
960Sstevel@tonic-gate } \
970Sstevel@tonic-gate ccount -= cw; \
980Sstevel@tonic-gate while (cw--) \
990Sstevel@tonic-gate lc = (lc << 7) | ((*p++) & 0177)
1000Sstevel@tonic-gate
1010Sstevel@tonic-gate /*
1020Sstevel@tonic-gate * The same() macro and letter() function were inserted to allow for
1030Sstevel@tonic-gate * the -i option work for the multi-byte environment.
1040Sstevel@tonic-gate */
1050Sstevel@tonic-gate wchar_t letter();
1060Sstevel@tonic-gate #define same(a, b) \
1070Sstevel@tonic-gate (a == b || iflag && (!MULTI_BYTE || ISASCII(a)) && (a ^ b) == ' ' && \
1080Sstevel@tonic-gate letter(a) == letter(b))
1090Sstevel@tonic-gate
1100Sstevel@tonic-gate
1110Sstevel@tonic-gate #define QSIZE 400
1120Sstevel@tonic-gate struct words {
1130Sstevel@tonic-gate wchar_t inp;
1140Sstevel@tonic-gate char out;
1150Sstevel@tonic-gate struct words *nst;
1160Sstevel@tonic-gate struct words *link;
1170Sstevel@tonic-gate struct words *fail;
118*440Spd155743 } *w = NULL, *smax, *q;
1190Sstevel@tonic-gate
1200Sstevel@tonic-gate FILE *fptr;
1210Sstevel@tonic-gate long long lnum;
1220Sstevel@tonic-gate int bflag, cflag, lflag, fflag, nflag, vflag, xflag, eflag, sflag;
1230Sstevel@tonic-gate int hflag, iflag;
1240Sstevel@tonic-gate int retcode = 0;
1250Sstevel@tonic-gate int nfile;
1260Sstevel@tonic-gate long long blkno;
1270Sstevel@tonic-gate int nsucc;
1280Sstevel@tonic-gate long long tln;
1290Sstevel@tonic-gate FILE *wordf;
1300Sstevel@tonic-gate char *argptr;
131*440Spd155743 off_t input_size = 0;
1320Sstevel@tonic-gate
1330Sstevel@tonic-gate void execute(char *);
1340Sstevel@tonic-gate void cgotofn(void);
1350Sstevel@tonic-gate void overflo(void);
1360Sstevel@tonic-gate void cfail(void);
1370Sstevel@tonic-gate
1380Sstevel@tonic-gate static long fw_lBufsiz = 0;
1390Sstevel@tonic-gate
1400Sstevel@tonic-gate int
main(int argc,char ** argv)1410Sstevel@tonic-gate main(int argc, char **argv)
1420Sstevel@tonic-gate {
1430Sstevel@tonic-gate int c;
1440Sstevel@tonic-gate int errflg = 0;
145*440Spd155743 struct stat file_stat;
1460Sstevel@tonic-gate
1470Sstevel@tonic-gate (void) setlocale(LC_ALL, "");
1480Sstevel@tonic-gate #if !defined(TEXT_DOMAIN) /* Should be defined by cc -D */
1490Sstevel@tonic-gate #define TEXT_DOMAIN "SYS_TEST" /* Use this only if it weren't */
1500Sstevel@tonic-gate #endif
1510Sstevel@tonic-gate (void) textdomain(TEXT_DOMAIN);
1520Sstevel@tonic-gate
1530Sstevel@tonic-gate while ((c = getopt(argc, argv, "hybcie:f:lnvxs")) != EOF)
1540Sstevel@tonic-gate switch (c) {
1550Sstevel@tonic-gate
1560Sstevel@tonic-gate case 's':
1570Sstevel@tonic-gate sflag++;
1580Sstevel@tonic-gate continue;
1590Sstevel@tonic-gate case 'h':
1600Sstevel@tonic-gate hflag++;
1610Sstevel@tonic-gate continue;
1620Sstevel@tonic-gate case 'b':
1630Sstevel@tonic-gate bflag++;
1640Sstevel@tonic-gate continue;
1650Sstevel@tonic-gate
1660Sstevel@tonic-gate case 'i':
1670Sstevel@tonic-gate case 'y':
1680Sstevel@tonic-gate iflag++;
1690Sstevel@tonic-gate continue;
1700Sstevel@tonic-gate
1710Sstevel@tonic-gate case 'c':
1720Sstevel@tonic-gate cflag++;
1730Sstevel@tonic-gate continue;
1740Sstevel@tonic-gate
1750Sstevel@tonic-gate case 'e':
1760Sstevel@tonic-gate eflag++;
1770Sstevel@tonic-gate argptr = optarg;
178*440Spd155743 input_size = strlen(argptr);
1790Sstevel@tonic-gate continue;
1800Sstevel@tonic-gate
1810Sstevel@tonic-gate case 'f':
1820Sstevel@tonic-gate fflag++;
1830Sstevel@tonic-gate wordf = fopen(optarg, "r");
1840Sstevel@tonic-gate if (wordf == NULL) {
1850Sstevel@tonic-gate (void) fprintf(stderr,
1860Sstevel@tonic-gate gettext("fgrep: can't open %s\n"),
1870Sstevel@tonic-gate optarg);
1880Sstevel@tonic-gate exit(2);
1890Sstevel@tonic-gate }
190*440Spd155743
191*440Spd155743 if (fstat(fileno(wordf), &file_stat) == 0) {
192*440Spd155743 input_size = file_stat.st_size;
193*440Spd155743 } else {
194*440Spd155743 (void) fprintf(stderr,
195*440Spd155743 gettext("fgrep: can't fstat %s\n"),
196*440Spd155743 optarg);
197*440Spd155743 exit(2);
198*440Spd155743 }
199*440Spd155743
2000Sstevel@tonic-gate continue;
2010Sstevel@tonic-gate
2020Sstevel@tonic-gate case 'l':
2030Sstevel@tonic-gate lflag++;
2040Sstevel@tonic-gate continue;
2050Sstevel@tonic-gate
2060Sstevel@tonic-gate case 'n':
2070Sstevel@tonic-gate nflag++;
2080Sstevel@tonic-gate continue;
2090Sstevel@tonic-gate
2100Sstevel@tonic-gate case 'v':
2110Sstevel@tonic-gate vflag++;
2120Sstevel@tonic-gate continue;
2130Sstevel@tonic-gate
2140Sstevel@tonic-gate case 'x':
2150Sstevel@tonic-gate xflag++;
2160Sstevel@tonic-gate continue;
2170Sstevel@tonic-gate
2180Sstevel@tonic-gate case '?':
2190Sstevel@tonic-gate errflg++;
2200Sstevel@tonic-gate }
2210Sstevel@tonic-gate
2220Sstevel@tonic-gate argc -= optind;
2230Sstevel@tonic-gate if (errflg || ((argc <= 0) && !fflag && !eflag)) {
2240Sstevel@tonic-gate (void) printf(gettext("usage: fgrep [ -bchilnsvx ] "
2250Sstevel@tonic-gate "[ -e exp ] [ -f file ] [ strings ] [ file ] ...\n"));
2260Sstevel@tonic-gate exit(2);
2270Sstevel@tonic-gate }
2280Sstevel@tonic-gate if (!eflag && !fflag) {
2290Sstevel@tonic-gate argptr = argv[optind];
230*440Spd155743 input_size = strlen(argptr);
231*440Spd155743 input_size++;
2320Sstevel@tonic-gate optind++;
2330Sstevel@tonic-gate argc--;
2340Sstevel@tonic-gate }
2350Sstevel@tonic-gate
236*440Spd155743 /*
237*440Spd155743 * Normally we need one struct words for each letter in the pattern
238*440Spd155743 * plus one terminating struct words with outp = 1, but when -x option
239*440Spd155743 * is specified we require one more struct words for `\n` character so we
240*440Spd155743 * calculate the input_size as below. We add extra 1 because
241*440Spd155743 * (input_size/2) rounds off odd numbers
242*440Spd155743 */
243*440Spd155743
244*440Spd155743 if (xflag) {
245*440Spd155743 input_size = input_size + (input_size/2) + 1;
246*440Spd155743 }
247*440Spd155743
248*440Spd155743 input_size++;
249*440Spd155743
250*440Spd155743 w = (struct words *)calloc(input_size, sizeof (struct words));
251*440Spd155743 if (w == NULL) {
252*440Spd155743 (void) fprintf(stderr,
253*440Spd155743 gettext("fgrep: could not allocate "
254*440Spd155743 "memory for wordlist\n"));
255*440Spd155743 exit(2);
256*440Spd155743 }
257*440Spd155743
2580Sstevel@tonic-gate getwidth(&WW);
2590Sstevel@tonic-gate if ((WIDTH1 == 0) && (WIDTH2 == 0) &&
2600Sstevel@tonic-gate (WIDTH3 == 0)) {
2610Sstevel@tonic-gate /*
2620Sstevel@tonic-gate * If non EUC-based locale,
2630Sstevel@tonic-gate * assume WIDTH1 is 1.
2640Sstevel@tonic-gate */
2650Sstevel@tonic-gate WIDTH1 = 1;
2660Sstevel@tonic-gate }
2670Sstevel@tonic-gate WIDTH2++;
2680Sstevel@tonic-gate WIDTH3++;
2690Sstevel@tonic-gate
2700Sstevel@tonic-gate cgotofn();
2710Sstevel@tonic-gate cfail();
2720Sstevel@tonic-gate nfile = argc;
2730Sstevel@tonic-gate argv = &argv[optind];
2740Sstevel@tonic-gate if (argc <= 0) {
2750Sstevel@tonic-gate execute((char *)NULL);
2760Sstevel@tonic-gate } else
2770Sstevel@tonic-gate while (--argc >= 0) {
2780Sstevel@tonic-gate execute(*argv);
2790Sstevel@tonic-gate argv++;
2800Sstevel@tonic-gate }
281*440Spd155743
282*440Spd155743 if (w != NULL) {
283*440Spd155743 free(w);
284*440Spd155743 }
285*440Spd155743
2860Sstevel@tonic-gate return (retcode != 0 ? retcode : nsucc == 0);
2870Sstevel@tonic-gate }
2880Sstevel@tonic-gate
2890Sstevel@tonic-gate void
execute(char * file)2900Sstevel@tonic-gate execute(char *file)
2910Sstevel@tonic-gate {
2920Sstevel@tonic-gate char *p;
2930Sstevel@tonic-gate struct words *c;
2940Sstevel@tonic-gate int ccount;
2950Sstevel@tonic-gate static char *buf = NULL;
2960Sstevel@tonic-gate int failed;
2970Sstevel@tonic-gate char *nlp;
2980Sstevel@tonic-gate wchar_t lc;
2990Sstevel@tonic-gate int cw;
3000Sstevel@tonic-gate
3010Sstevel@tonic-gate if (buf == NULL) {
3020Sstevel@tonic-gate fw_lBufsiz = BUFSIZ;
3030Sstevel@tonic-gate if ((buf = malloc(fw_lBufsiz + BUFSIZ)) == NULL) {
3040Sstevel@tonic-gate exit(2); /* out of memory */
3050Sstevel@tonic-gate }
3060Sstevel@tonic-gate }
3070Sstevel@tonic-gate
3080Sstevel@tonic-gate if (file) {
3090Sstevel@tonic-gate if ((fptr = fopen(file, "r")) == NULL) {
3100Sstevel@tonic-gate (void) fprintf(stderr,
3110Sstevel@tonic-gate gettext("fgrep: can't open %s\n"), file);
3120Sstevel@tonic-gate retcode = 2;
3130Sstevel@tonic-gate return;
3140Sstevel@tonic-gate }
3150Sstevel@tonic-gate } else {
3160Sstevel@tonic-gate file = "<stdin>";
3170Sstevel@tonic-gate fptr = stdin;
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate ccount = 0;
3200Sstevel@tonic-gate failed = 0;
3210Sstevel@tonic-gate lnum = 1;
3220Sstevel@tonic-gate tln = 0;
3230Sstevel@tonic-gate blkno = 0;
3240Sstevel@tonic-gate p = buf;
3250Sstevel@tonic-gate nlp = p;
3260Sstevel@tonic-gate c = w;
3270Sstevel@tonic-gate for (;;) {
3280Sstevel@tonic-gate if (c == 0)
3290Sstevel@tonic-gate break;
3300Sstevel@tonic-gate if (ccount <= 0) {
3310Sstevel@tonic-gate if (p >= &buf[fw_lBufsiz + BUFSIZ]) {
3320Sstevel@tonic-gate if (nlp == buf) {
3330Sstevel@tonic-gate /* increase the buffer size */
3340Sstevel@tonic-gate fw_lBufsiz += BUFSIZ;
3350Sstevel@tonic-gate if ((buf = realloc(buf,
3360Sstevel@tonic-gate fw_lBufsiz + BUFSIZ)) == NULL) {
3370Sstevel@tonic-gate exit(2); /* out of memory */
3380Sstevel@tonic-gate }
3390Sstevel@tonic-gate nlp = buf;
3400Sstevel@tonic-gate p = &buf[fw_lBufsiz];
3410Sstevel@tonic-gate } else {
3420Sstevel@tonic-gate /* shift the buffer down */
3430Sstevel@tonic-gate (void) memmove(buf, nlp,
3440Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ]
3450Sstevel@tonic-gate - nlp);
3460Sstevel@tonic-gate p -= nlp - buf;
3470Sstevel@tonic-gate nlp = buf;
3480Sstevel@tonic-gate }
3490Sstevel@tonic-gate
3500Sstevel@tonic-gate }
3510Sstevel@tonic-gate if (p > &buf[fw_lBufsiz]) {
3520Sstevel@tonic-gate if ((ccount = fread(p, sizeof (char),
3530Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - p, fptr))
3540Sstevel@tonic-gate <= 0)
3550Sstevel@tonic-gate break;
3560Sstevel@tonic-gate } else if ((ccount = fread(p, sizeof (char),
3570Sstevel@tonic-gate BUFSIZ, fptr)) <= 0)
3580Sstevel@tonic-gate break;
3590Sstevel@tonic-gate blkno += (long long)ccount;
3600Sstevel@tonic-gate }
3610Sstevel@tonic-gate GETONE(lc, p);
3620Sstevel@tonic-gate nstate:
3630Sstevel@tonic-gate if (same(c->inp, lc)) {
3640Sstevel@tonic-gate c = c->nst;
3650Sstevel@tonic-gate } else if (c->link != 0) {
3660Sstevel@tonic-gate c = c->link;
3670Sstevel@tonic-gate goto nstate;
3680Sstevel@tonic-gate } else {
3690Sstevel@tonic-gate c = c->fail;
3700Sstevel@tonic-gate failed = 1;
3710Sstevel@tonic-gate if (c == 0) {
3720Sstevel@tonic-gate c = w;
3730Sstevel@tonic-gate istate:
3740Sstevel@tonic-gate if (same(c->inp, lc)) {
3750Sstevel@tonic-gate c = c->nst;
3760Sstevel@tonic-gate } else if (c->link != 0) {
3770Sstevel@tonic-gate c = c->link;
3780Sstevel@tonic-gate goto istate;
3790Sstevel@tonic-gate }
3800Sstevel@tonic-gate } else
3810Sstevel@tonic-gate goto nstate;
3820Sstevel@tonic-gate }
3830Sstevel@tonic-gate
3840Sstevel@tonic-gate if (c == 0)
3850Sstevel@tonic-gate break;
3860Sstevel@tonic-gate
3870Sstevel@tonic-gate if (c->out) {
3880Sstevel@tonic-gate while (lc != '\n') {
3890Sstevel@tonic-gate if (ccount <= 0) {
3900Sstevel@tonic-gate if (p == &buf[fw_lBufsiz + BUFSIZ]) {
3910Sstevel@tonic-gate if (nlp == buf) {
3920Sstevel@tonic-gate /* increase buffer size */
3930Sstevel@tonic-gate fw_lBufsiz += BUFSIZ;
3940Sstevel@tonic-gate if ((buf = realloc(buf, fw_lBufsiz + BUFSIZ)) == NULL) {
3950Sstevel@tonic-gate exit(2); /* out of memory */
3960Sstevel@tonic-gate }
3970Sstevel@tonic-gate nlp = buf;
3980Sstevel@tonic-gate p = &buf[fw_lBufsiz];
3990Sstevel@tonic-gate } else {
4000Sstevel@tonic-gate /* shift buffer down */
4010Sstevel@tonic-gate (void) memmove(buf, nlp, &buf[fw_lBufsiz + BUFSIZ] - nlp);
4020Sstevel@tonic-gate p -= nlp - buf;
4030Sstevel@tonic-gate nlp = buf;
4040Sstevel@tonic-gate }
4050Sstevel@tonic-gate }
4060Sstevel@tonic-gate if (p > &buf[fw_lBufsiz]) {
4070Sstevel@tonic-gate if ((ccount = fread(p, sizeof (char),
4080Sstevel@tonic-gate &buf[fw_lBufsiz + BUFSIZ] - p, fptr)) <= 0) break;
4090Sstevel@tonic-gate } else if ((ccount = fread(p, sizeof (char), BUFSIZ,
4100Sstevel@tonic-gate fptr)) <= 0) break;
4110Sstevel@tonic-gate blkno += (long long)ccount;
4120Sstevel@tonic-gate }
4130Sstevel@tonic-gate GETONE(lc, p);
4140Sstevel@tonic-gate }
4150Sstevel@tonic-gate if ((vflag && (failed == 0 || xflag == 0)) ||
4160Sstevel@tonic-gate (vflag == 0 && xflag && failed))
4170Sstevel@tonic-gate goto nomatch;
4180Sstevel@tonic-gate succeed:
4190Sstevel@tonic-gate nsucc = 1;
4200Sstevel@tonic-gate if (cflag)
4210Sstevel@tonic-gate tln++;
4220Sstevel@tonic-gate else if (lflag && !sflag) {
4230Sstevel@tonic-gate (void) printf("%s\n", file);
4240Sstevel@tonic-gate (void) fclose(fptr);
4250Sstevel@tonic-gate return;
4260Sstevel@tonic-gate } else if (!sflag) {
4270Sstevel@tonic-gate if (nfile > 1 && !hflag)
4280Sstevel@tonic-gate (void) printf("%s:", file);
4290Sstevel@tonic-gate if (bflag)
4300Sstevel@tonic-gate (void) printf("%lld:",
4310Sstevel@tonic-gate (blkno - (long long)(ccount-1))
4320Sstevel@tonic-gate / BUFSIZ);
4330Sstevel@tonic-gate if (nflag)
4340Sstevel@tonic-gate (void) printf("%lld:", lnum);
4350Sstevel@tonic-gate if (p <= nlp) {
4360Sstevel@tonic-gate while (nlp < &buf[fw_lBufsiz + BUFSIZ])
4370Sstevel@tonic-gate (void) putchar(*nlp++);
4380Sstevel@tonic-gate nlp = buf;
4390Sstevel@tonic-gate }
4400Sstevel@tonic-gate while (nlp < p)
4410Sstevel@tonic-gate (void) putchar(*nlp++);
4420Sstevel@tonic-gate }
4430Sstevel@tonic-gate nomatch:
4440Sstevel@tonic-gate lnum++;
4450Sstevel@tonic-gate nlp = p;
4460Sstevel@tonic-gate c = w;
4470Sstevel@tonic-gate failed = 0;
4480Sstevel@tonic-gate continue;
4490Sstevel@tonic-gate }
4500Sstevel@tonic-gate if (lc == '\n')
4510Sstevel@tonic-gate if (vflag)
4520Sstevel@tonic-gate goto succeed;
4530Sstevel@tonic-gate else {
4540Sstevel@tonic-gate lnum++;
4550Sstevel@tonic-gate nlp = p;
4560Sstevel@tonic-gate c = w;
4570Sstevel@tonic-gate failed = 0;
4580Sstevel@tonic-gate }
4590Sstevel@tonic-gate }
4600Sstevel@tonic-gate (void) fclose(fptr);
4610Sstevel@tonic-gate if (cflag) {
4620Sstevel@tonic-gate if ((nfile > 1) && !hflag)
4630Sstevel@tonic-gate (void) printf("%s:", file);
4640Sstevel@tonic-gate (void) printf("%lld\n", tln);
4650Sstevel@tonic-gate }
4660Sstevel@tonic-gate }
4670Sstevel@tonic-gate
4680Sstevel@tonic-gate
4690Sstevel@tonic-gate wchar_t
getargc(void)4700Sstevel@tonic-gate getargc(void)
4710Sstevel@tonic-gate {
4720Sstevel@tonic-gate /* appends a newline to shell quoted argument list so */
4730Sstevel@tonic-gate /* the list looks like it came from an ed style file */
4740Sstevel@tonic-gate wchar_t c;
4750Sstevel@tonic-gate int cw;
4760Sstevel@tonic-gate int b;
4770Sstevel@tonic-gate static int endflg;
4780Sstevel@tonic-gate
4790Sstevel@tonic-gate
4800Sstevel@tonic-gate if (wordf) {
4810Sstevel@tonic-gate if ((b = getc(wordf)) == EOF)
4820Sstevel@tonic-gate return (EOF);
4830Sstevel@tonic-gate cw = ISASCII(c = (wchar_t)b) ? 1 :
4840Sstevel@tonic-gate (ISSET2(c) ? WIDTH2 : (ISSET3(c) ? WIDTH3 : WIDTH1));
4850Sstevel@tonic-gate while (--cw) {
4860Sstevel@tonic-gate if ((b = getc(wordf)) == EOF)
4870Sstevel@tonic-gate return (EOF);
4880Sstevel@tonic-gate c = (c << 7) | (b & 0177);
4890Sstevel@tonic-gate }
4900Sstevel@tonic-gate return (iflag ? letter(c) : c);
4910Sstevel@tonic-gate }
4920Sstevel@tonic-gate
4930Sstevel@tonic-gate if (endflg)
4940Sstevel@tonic-gate return (EOF);
4950Sstevel@tonic-gate
4960Sstevel@tonic-gate {
4970Sstevel@tonic-gate cw = ISASCII(c = (unsigned char)*argptr++) ? 1 :
4980Sstevel@tonic-gate (ISSET2(c) ? WIDTH2 : (ISSET3(c) ? WIDTH3 : WIDTH1));
4990Sstevel@tonic-gate
5000Sstevel@tonic-gate while (--cw)
5010Sstevel@tonic-gate c = (c << 7) | ((*argptr++) & 0177);
5020Sstevel@tonic-gate if (c == '\0') {
5030Sstevel@tonic-gate endflg++;
5040Sstevel@tonic-gate return ('\n');
5050Sstevel@tonic-gate }
5060Sstevel@tonic-gate }
5070Sstevel@tonic-gate return (iflag ? letter(c) : c);
5080Sstevel@tonic-gate
5090Sstevel@tonic-gate
5100Sstevel@tonic-gate }
5110Sstevel@tonic-gate
5120Sstevel@tonic-gate void
cgotofn(void)5130Sstevel@tonic-gate cgotofn(void)
5140Sstevel@tonic-gate {
5150Sstevel@tonic-gate int c;
5160Sstevel@tonic-gate struct words *s;
5170Sstevel@tonic-gate
5180Sstevel@tonic-gate s = smax = w;
5190Sstevel@tonic-gate nword:
5200Sstevel@tonic-gate for (;;) {
5210Sstevel@tonic-gate c = getargc();
5220Sstevel@tonic-gate if (c == EOF)
5230Sstevel@tonic-gate return;
5240Sstevel@tonic-gate if (c == 0)
5250Sstevel@tonic-gate goto enter;
5260Sstevel@tonic-gate if (c == '\n') {
5270Sstevel@tonic-gate if (xflag) {
5280Sstevel@tonic-gate for (;;) {
5290Sstevel@tonic-gate if (s->inp == c) {
5300Sstevel@tonic-gate s = s->nst;
5310Sstevel@tonic-gate break;
5320Sstevel@tonic-gate }
5330Sstevel@tonic-gate if (s->inp == 0)
5340Sstevel@tonic-gate goto nenter;
5350Sstevel@tonic-gate if (s->link == 0) {
536*440Spd155743 if (smax >= &w[input_size -1])
5370Sstevel@tonic-gate overflo();
5380Sstevel@tonic-gate s->link = ++smax;
5390Sstevel@tonic-gate s = smax;
5400Sstevel@tonic-gate goto nenter;
5410Sstevel@tonic-gate }
5420Sstevel@tonic-gate s = s->link;
5430Sstevel@tonic-gate }
5440Sstevel@tonic-gate }
5450Sstevel@tonic-gate s->out = 1;
5460Sstevel@tonic-gate s = w;
5470Sstevel@tonic-gate } else {
5480Sstevel@tonic-gate loop:
5490Sstevel@tonic-gate if (s->inp == c) {
5500Sstevel@tonic-gate s = s->nst;
5510Sstevel@tonic-gate continue;
5520Sstevel@tonic-gate }
5530Sstevel@tonic-gate if (s->inp == 0)
5540Sstevel@tonic-gate goto enter;
5550Sstevel@tonic-gate if (s->link == 0) {
556*440Spd155743 if (smax >= &w[input_size -1])
5570Sstevel@tonic-gate overflo();
5580Sstevel@tonic-gate s->link = ++smax;
5590Sstevel@tonic-gate s = smax;
5600Sstevel@tonic-gate goto enter;
5610Sstevel@tonic-gate }
5620Sstevel@tonic-gate s = s->link;
5630Sstevel@tonic-gate goto loop;
5640Sstevel@tonic-gate }
5650Sstevel@tonic-gate }
5660Sstevel@tonic-gate
5670Sstevel@tonic-gate enter:
5680Sstevel@tonic-gate do {
5690Sstevel@tonic-gate s->inp = c;
570*440Spd155743 if (smax >= &w[input_size -1])
5710Sstevel@tonic-gate overflo();
5720Sstevel@tonic-gate s->nst = ++smax;
5730Sstevel@tonic-gate s = smax;
5740Sstevel@tonic-gate } while ((c = getargc()) != '\n' && c != EOF);
5750Sstevel@tonic-gate if (xflag) {
5760Sstevel@tonic-gate nenter:
5770Sstevel@tonic-gate s->inp = '\n';
578*440Spd155743 if (smax >= &w[input_size -1])
5790Sstevel@tonic-gate overflo();
5800Sstevel@tonic-gate s->nst = ++smax;
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate smax->out = 1;
5830Sstevel@tonic-gate s = w;
5840Sstevel@tonic-gate if (c != EOF)
5850Sstevel@tonic-gate goto nword;
5860Sstevel@tonic-gate }
5870Sstevel@tonic-gate
588*440Spd155743 /*
589*440Spd155743 * This function is an unexpected condition, since input_size should have been
590*440Spd155743 * calculated correctly before hand.
591*440Spd155743 */
592*440Spd155743
5930Sstevel@tonic-gate void
overflo(void)5940Sstevel@tonic-gate overflo(void)
5950Sstevel@tonic-gate {
596*440Spd155743 (void) fprintf(stderr, gettext("fgrep: wordlist too large\n"));
5970Sstevel@tonic-gate exit(2);
5980Sstevel@tonic-gate }
5990Sstevel@tonic-gate
6000Sstevel@tonic-gate void
cfail(void)6010Sstevel@tonic-gate cfail(void)
6020Sstevel@tonic-gate {
6030Sstevel@tonic-gate int qsize = QSIZE;
6040Sstevel@tonic-gate struct words **queue = NULL;
6050Sstevel@tonic-gate
6060Sstevel@tonic-gate /*
6070Sstevel@tonic-gate * front and rear are pointers used to traverse the global words
6080Sstevel@tonic-gate * structure "w" which contains the data of input pattern file
6090Sstevel@tonic-gate */
6100Sstevel@tonic-gate struct words **front, **rear;
6110Sstevel@tonic-gate struct words *state;
6120Sstevel@tonic-gate unsigned long frontoffset = 0, rearoffset = 0;
6130Sstevel@tonic-gate char c;
6140Sstevel@tonic-gate struct words *s;
6150Sstevel@tonic-gate s = w;
6160Sstevel@tonic-gate if ((queue = (struct words **)calloc(qsize, sizeof (struct words *)))
6170Sstevel@tonic-gate == NULL) {
6180Sstevel@tonic-gate perror("fgrep");
6190Sstevel@tonic-gate exit(2);
6200Sstevel@tonic-gate }
6210Sstevel@tonic-gate front = rear = queue;
6220Sstevel@tonic-gate init:
6230Sstevel@tonic-gate if ((s->inp) != 0) {
6240Sstevel@tonic-gate *rear++ = s->nst;
6250Sstevel@tonic-gate /*
6260Sstevel@tonic-gate * Reallocates the queue if the number of distinct starting
6270Sstevel@tonic-gate * character of patterns exceeds the qsize value
6280Sstevel@tonic-gate */
6290Sstevel@tonic-gate if (rear >= &queue[qsize - 1]) {
6300Sstevel@tonic-gate frontoffset = front - queue;
6310Sstevel@tonic-gate rearoffset = rear - queue;
6320Sstevel@tonic-gate qsize += QSIZE;
6330Sstevel@tonic-gate if ((queue = (struct words **)realloc(queue,
6340Sstevel@tonic-gate qsize * sizeof (struct words *))) == NULL) {
6350Sstevel@tonic-gate perror("fgrep");
6360Sstevel@tonic-gate exit(2);
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate front = queue + frontoffset;
6390Sstevel@tonic-gate rear = queue + rearoffset;
6400Sstevel@tonic-gate }
6410Sstevel@tonic-gate }
6420Sstevel@tonic-gate if ((s = s->link) != 0) {
6430Sstevel@tonic-gate goto init;
6440Sstevel@tonic-gate }
6450Sstevel@tonic-gate
6460Sstevel@tonic-gate while (rear != front) {
6470Sstevel@tonic-gate s = *front++;
6480Sstevel@tonic-gate cloop:
6490Sstevel@tonic-gate if ((c = s->inp) != 0) {
6500Sstevel@tonic-gate *rear++ = (q = s->nst);
6510Sstevel@tonic-gate /*
6520Sstevel@tonic-gate * Reallocate the queue if the rear pointer reaches the end
6530Sstevel@tonic-gate * queue
6540Sstevel@tonic-gate */
6550Sstevel@tonic-gate if (rear >= &queue[qsize - 1]) {
6560Sstevel@tonic-gate frontoffset = front - queue;
6570Sstevel@tonic-gate rearoffset = rear - queue;
6580Sstevel@tonic-gate qsize += QSIZE;
6590Sstevel@tonic-gate if ((queue = (struct words **)realloc(queue,
6600Sstevel@tonic-gate qsize * sizeof (struct words *))) == NULL) {
6610Sstevel@tonic-gate perror("fgrep");
6620Sstevel@tonic-gate exit(2);
6630Sstevel@tonic-gate }
6640Sstevel@tonic-gate front = queue + frontoffset;
6650Sstevel@tonic-gate rear = queue + rearoffset;
6660Sstevel@tonic-gate }
6670Sstevel@tonic-gate state = s->fail;
6680Sstevel@tonic-gate floop:
6690Sstevel@tonic-gate if (state == 0)
6700Sstevel@tonic-gate state = w;
6710Sstevel@tonic-gate if (state->inp == c) {
6720Sstevel@tonic-gate qloop:
6730Sstevel@tonic-gate q->fail = state->nst;
6740Sstevel@tonic-gate if ((state->nst)->out == 1)
6750Sstevel@tonic-gate q->out = 1;
6760Sstevel@tonic-gate if ((q = q->link) != 0)
6770Sstevel@tonic-gate goto qloop;
6780Sstevel@tonic-gate } else if ((state = state->link) != 0)
6790Sstevel@tonic-gate goto floop;
6800Sstevel@tonic-gate }
6810Sstevel@tonic-gate if ((s = s->link) != 0)
6820Sstevel@tonic-gate goto cloop;
6830Sstevel@tonic-gate }
6840Sstevel@tonic-gate }
6850Sstevel@tonic-gate
6860Sstevel@tonic-gate wchar_t
letter(wchar_t c)6870Sstevel@tonic-gate letter(wchar_t c)
6880Sstevel@tonic-gate {
6890Sstevel@tonic-gate if (c >= 'a' && c <= 'z')
6900Sstevel@tonic-gate return (c);
6910Sstevel@tonic-gate if (c >= 'A' && c <= 'Z')
6920Sstevel@tonic-gate return (c + 'a' - 'A');
6930Sstevel@tonic-gate return (c);
6940Sstevel@tonic-gate }
695