Repository navigation
Expand file tree
/
Copy pathcgrep.c
More file actions
executable file
·379 lines (312 loc) · 9.08 KB
/
Copy pathcgrep.c
File metadata and controls
executable file
·379 lines (312 loc) · 9.08 KB
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
/* cgrep - grep and display context Author: Mark Mallet */
/*
Nov 19 1984 Mark Mallett (mem@zinn.MV.COM)
mem 860224 Modified to do r/e (regular expression) parsing on unix
mem 860324 Added -f, -n; added code to number lines correctly on output.
mem 870325 Added support for regcmp()/regex() style regular expression
library; redid some conditionals to provide better mix'n'match.
mem 870326 Don't try to print the filename if reading from stdin.
Add -w option. Fix a small problem which occasionally allowed
the separator to come out between adjacent lines of the file.
mem 871119 Fix semantics of call to regcmp(): the NULL terminating the
argument list was missing. It worked, but probably only
due to some bizarre coincidence.
dro 890109 Minor mods to compile under Minix
*/
#define OS_UNIX /* Define this for unix systems */
/* #define REGEX *//* Define this for re_comp/re_exec library */
#define REGCMP /* Define this to use regcmp/regex */
/* #define OS_CPM *//* Define this for CP/M-80 */
/* Don't touch these */
#define NOREGEXP /* Set this for no regular expression */
#ifdef REGEX
#undef NOREGEXP
#endif /* REGEX */
#ifdef REGCMP
#undef NOREGEXP
#endif /* REGCMP */
#ifdef OS_CPM
#include "stdio.h"
#include "ctype.h"
#endif /* OS_CPM */
#ifdef OS_UNIX
#include <sys/types.h>
#include <sys/dir.h> /* Either here or in sys directory - dro */
#include <ctype.h>
#include <limits.h> /* should have this - dro */
#include <regexp.h> /* should have this - dro */
#include <stdlib.h>
#include <stdio.h>
#endif /* OS_UNIX */
/* Local definitions */
#ifndef NULL
#define NULL ((char *)0)
#endif /* NULL */
#ifndef NUL
#define NUL '\000'
#endif
#ifndef TRUE
#define TRUE 1
#define FALSE 0
#endif
/* Internal data declared global */
/* Internal routines */
_PROTOTYPE(int main, (int argc, char **argv));
_PROTOTYPE(void dosrch, (char *ifnm));
_PROTOTYPE(void shwlin, (char *fnm, int linnum, char *line));
_PROTOTYPE(int matlin, (char *line));
_PROTOTYPE(void regerror, (const char *s));
/* External data */
/* Local data */
static int Debug = {FALSE}; /* Debug enabled flag */
static int Lcur = {0}; /* Current line (in Lines array) */
static char **Lines = {NULL}; /* Lines pointer array */
static int Linlen = {100}; /* Line length */
static int Lone = {0}; /* Line one (in Lines array) */
static int Nmr = {0}; /* Number of matched regions */
static char *Pat = {NULL}; /* Pattern */
static char Shwfile = {TRUE}; /* Show file name... */
static char Shwline = {TRUE}; /* Show line number */
static int Waft = {0}; /* Window after */
static int Wbef = {0}; /* Window before */
static int Wsiz = {0}; /* Window size */
regexp *Re; /* Result from reg compilation */
int main(argc, argv)
int argc; /* Argument count */
char **argv; /* Argument values */
{
int i; /* Scratch */
int n; /* Scratch again */
int c; /* A character */
char *aptr; /* Argument pointer */
int nf; /* number of files on command line */
nf = 0; /* No files on line */
for (i = 1; i < argc; i++) { /* Look at args */
if (argv[i][0] != '-') {/* If option */
if (Pat == NULL) { /* If no pattern yet given */
Pat = argv[i]; /* point here */
#ifdef REGEX
if ((Re = re_comp(Pat)) != NULL) {
fprintf(stderr, "cgrep: %s\n", re);
exit(1);
}
#endif /* REGEX */
#ifdef REGCMP
if ((Re = regcomp(Pat)) == NULL) {
fprintf(stderr, "cgrep: error in regular expression.\n");
exit(1);
}
#endif /* REGCMP */
} else { /* This must be a file to search */
nf++; /* Count it */
dosrch(argv[i]); /* Search */
}
} else { /* Option char */
c = argv[i][1]; /* Get option char */
if (isupper(c)) /* Trap idiot definition of tolower */
c = tolower(c); /* Don't care about case */
n = i;
aptr = NULL; /* Find arg, if any */
if (argv[i][2] != NUL) {
aptr = &argv[i][2];
n = i; /* Where to set i if we use this arg */
} else if (i < argc - 1) { /* use next.. */
n = i + 1;
aptr = argv[n];
}
switch (c) { /* Process the option */
case 'a': /* Lines after */
Waft = atoi(aptr);
Lines = NULL;
i = n;
break;
case 'b': /* Lines before */
Wbef = atoi(aptr);
Lines = NULL;
i = n;
break;
/* Disable debug output
case 'd': Debug = TRUE; break;
*/
case 'f': /* Suppress filename on output */
Shwfile = FALSE;
break;
case 'l': /* Line length */
Linlen = atoi(aptr);
Lines = NULL;
i = n;
break;
case 'n': /* Suppress line number on output */
Shwline = FALSE;
break;
case 'w': /* Window: lines before and after */
Waft = Wbef = atoi(aptr);
Lines = NULL;
i = n;
break;
default:
fprintf(stderr, "Invalid option %s\n", argv[i]);
exit(1);
}
}
}
if (Pat == NULL) { /* If no pattern given */
fprintf(stderr,
"Usage: cgrep [-a n] [-b n] [-f] [-l n] [-n] [-w n] pattern [filename... ]\n");
exit(1);
}
if (nf == 0) /* No files processed ? */
dosrch((char *)NULL); /* Do standard input */
return(0);
}
/* Dosrch (ifnm) Perform the search
* Accepts :
*
* ifn Input file name
*
*
* Returns :
*
*
*/
void dosrch(ifnm)
char *ifnm; /* Input filelname */
{
FILE *ifp; /* Input fp */
char *lptr; /* Line pointer */
int i; /* Scratch */
int prtaft; /* Print-after count */
int linnum; /* Line number */
int nlb; /* Number of lines buffered */
if (ifnm != NULL) { /* If file name given */
ifp = fopen(ifnm, "r"); /* Open it for read access */
if (ifp == NULL) {
fprintf(stderr, "Can not open file %s\n", ifnm);
return;
}
} else
ifp = stdin;
if (Lines == NULL) { /* If no line table allocated.. */
Wsiz = Wbef + 2; /* Determine total window size */
Lines = (char **) calloc((size_t)Wsiz, sizeof(char *));
/* Allocate pointer table */
for (i = 0; i < Wsiz; i++) /* Allocate line buffers */
Lines[i] = (char *) calloc((size_t)Linlen, sizeof(char));
}
Lcur = Lone = 0; /* Setup line pointers */
nlb = 0; /* No lines buffered */
linnum = 0; /* Line number is zero */
prtaft = -(Wbef + 1); /* Make sure separator given first time */
for (;;) { /* Loop through the file */
lptr = Lines[Lcur]; /* Get pointer to current line */
if (++Lcur == Wsiz) /* Bump curr pointer and wrap */
Lcur = 0; /* if hit end */
if (Lone == Lcur) /* If wrapped to beginning of window */
if (++Lone == Wsiz) /* Bump beginning */
Lone = 0; /* and wrap if hit end */
if (fgets(lptr, Linlen, ifp) != lptr) break; /* if end of file */
linnum++; /* Count line number */
if (matlin(lptr)) { /* If matching line */
if (prtaft < (-Wbef)) /* Check for separator needed */
if ((Nmr++ > 0) && ((Wbef > 0) || (Waft > 0)))
printf("----------------------------------------------------------------------------\n");
while (Lone != Lcur) { /* Until we close the window */
shwlin(ifnm, linnum - nlb, Lines[Lone]);
/* Show the line */
if (++Lone == Wsiz) Lone = 0;
nlb--;
}
nlb = 0; /* No lines buffered */
prtaft = Waft; /* Print n lines after */
} else { /* Didn't match */
if (prtaft-- > 0) { /* If must print lines after */
shwlin(ifnm, linnum, lptr);
/* Show the line */
Lone = Lcur; /* Match pointers */
} else if (nlb < Wbef) /* Count lines buffered */
nlb++;
}
}
if (ifnm != NULL) fclose(ifp);
}
/* Shwlin (fnm, linnum, line) Show a matching line
*
* Accepts :
*
* fnm File name
*
* linnum Line number
*
* line Line to show
*
*
* Returns :
*
*
*/
void shwlin(fnm, linnum, line)
char *fnm; /* File name */
int linnum; /* Line number */
char *line; /* Line (with newline at end) to print */
{
if (Shwfile && (fnm != NULL)) printf("%s%s", fnm, Shwline ? " " : ":");
if (Shwline) printf("@%05d:", linnum);
printf("%s", line);
}
/* Matlin (line) Perform match against pattern and line
*
* Accepts :
*
* line Address of line to match
*
*
* Returns :
*
* <value> TRUE if match FALSE if not
*
*
*/
int matlin(line)
char *line; /* Line to match */
{
int rtncode; /* Return value from this routine */
#ifdef NOREGEXP
char *pptr, *lptr, *tlptr;
int c1, c2;
#endif /* NOREGEXP */
if (Debug) printf("Matching %s against %s", Pat, line);
#ifdef REGEX
rtncode = re_exec(line); /* Hand off to r/e evaluator */
#endif /* REGEX */
#ifdef REGCMP
rtncode = (regexec(Re, line, TRUE) != 0);
#endif /* REGCMP */
#ifdef NOREGEX /* Have to do menial comparison.. */
lptr = line; /* Init line pointer */
for (rtncode = -1; rtncode < 0;) {
tlptr = lptr++; /* Get temp ptr to line */
pptr = Pat; /* Get ptr to pattern */
while (TRUE) {
if ((c1 = *pptr++) == NUL) {
rtncode = 1; /* GOOD return value */
break;
}
if ((c2 = *tlptr++) == NUL) {
rtncode = 0; /* BAD return value */
break;
}
if (isupper(c1)) c1 = tolower(c1);
if (isupper(c2)) c2 = tolower(c2);
if (c1 != c2) break;
}
}
#endif /* NOREGEX */
if (Debug) printf("matlin returned %s\n", rtncode ? "TRUE" : "FALSE");
return(rtncode);
}
void regerror(s)
const char *s;
{
printf("%s\n", (char *) s);
exit(1);
}