forked from th-otto/gulam
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpregrep.c
More file actions
274 lines (223 loc) · 4.52 KB
/
pregrep.c
File metadata and controls
274 lines (223 loc) · 4.52 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
/*
pregrep.c of gulam -- print, lpr, and grep functions aug 24, 86
copyright (c) 1986 pm@Case
*/
/* 890111 kbad fixed bug in "prout" which would cause an extra header to
* print(without formfeed) after the last page of a document.
*/
#include "ue.h"
#include "regexp.h"
static regexp *re;
static int plnn; /* line number */
static char *fnm; /* name of current file */
static int lfnm;
#if LPRINT
#define LPPG 55 /* default lines per page */
static int nerr; /* #errors */
static int lppg; /* lines per page */
static char *hdr; /* top of page hdr */
static char *pgp; /* pos of pageno in hdr */
static long page; /* # current page */
static int lprf; /* 1 iff lpr; 0 iff print */
static void header(void);
/* printer initialization strings */
/* the one below is for epson mx80 */
/* "\033\D\010\020\030\040\050\060\070\100\200\" set tabs */
/* "\033\C\102" form length = 66 */
/* ouput to PRN: */
static void prout(char *p, int n)
{
unsigned char c;
if (n < 0)
return; /* jstncs */
while (n--)
{
c = *p++;
do
{
if (useraborted())
{
valu = -1;
nerr++;
return;
}
} while (Cprnos() == 0);
(void) Cprnout(c);
if (c == '\014' && n)
header();
}
}
static void prvar(char *p, char *q)
{
p = varstr(p);
if (p == NULL || *p == '\0')
p = q;
prout(p, (int) strlen(p));
}
static void header(void)
{
plnn = 1;
page++;
if (hdr)
{
strcpy(pgp, itoal(page));
strcat(pgp, "\r\n\n\n");
prout(hdr, ((int) strlen(hdr)));
/* 1st page doesn't have the FF\n, so
* we must generate the \n.
*/
if (page == 1)
prvar("pr_eol", CRLF);
}
}
static void prinit(void) /* init before beginning to print/lpr */
{
char *p;
nerr = 0;
page = 0;
hdr = NULL;
lppg = varnum("pr_lpp");
if (lppg <= 0)
lppg = LPPG;
prvar("pr_bof", ES);
if (!lprf)
{
gsdatetime(ES); /* strg has date-time string */
p = str3cat(strg, " ", fnm);
hdr = str3cat(p, " Page ", "0123456789\r\n\n\n");
gfree(p);
if (hdr)
pgp = strrchr(hdr, '0');
gfree(strg);
strg = NULL;
}
header();
}
static void prline(char *q, int n)
{
if (nerr)
return;
/* FF followed by other chars signals end of page.
* Header is sent by prout(), because we want an embedded FF
* in a document to generate a header.
*/
if ((plnn++ % lppg) == 0)
prvar("pr_eop", "\014\n");
prout(q, n);
prvar("pr_eol", CRLF);
}
/* "print" one file */
static void lprint(uchar *pnm)
{
int fd;
fd = (int)gfopen(pnm, 0);
if (fd < 0)
return;
fnm = pnm;
lfnm = (int)strlen(fnm);
prinit();
eachline(fd, prline);
/* FF alone signals end of file, and will not generate
* a header in prout() after the last page.
*/
prvar("pr_eof", "\014");
if (hdr)
gfree(hdr);
}
void print(uchar *arg)
{
UNUSED(arg);
lprf = 0;
doforeach("print", lprint);
}
void lpr(uchar *arg)
{
UNUSED(arg);
lprf = 1;
doforeach("lpr", lprint);
}
#endif /* LPRINT */
static void fgmatch(char *q, int n)
{
char *s;
UNUSED(n);
plnn++;
if (regexec(re, q) == 1)
{
s = gmalloc(((uint) (lfnm + strlen(q) + 20)));
if (s == NULL)
return;
strcpy(s, fnm);
s[lfnm] = ' ';
strcpy(s + lfnm + 1, itoal((long) plnn));
strcat(s, ": ");
strcat(s, q);
outstr(s);
gfree(s);
}
}
static void legrep(char *pnm)
{
int fd;
fd = (int)gfopen(pnm, 0);
if (fd < 0)
{
emsg = "file not found";
return;
}
fnm = pnm;
lfnm = (int)strlen(fnm);
plnn = 0;
eachline(fd, fgmatch);
}
static void igrep(int flag) /* flag => fgrep; else egrep */
{
char meta[256];
char *p;
char *q;
char *r;
char *s;
unsigned char c;
p = gstrdup(lexgetword()); /* p has pattern */
if (flag && p && (r = q = gmalloc(((uint) (1 + 2 * strlen(p))))) != NULL)
{
charset(meta, "[()|?+*.$", 1);
s = p;
while ((c = *p++) != 0)
{
if (meta[c])
*q++ = '\\';
*q++ = c;
}
*q = '\0';
gfree(s);
p = r;
}
if (p)
{
re = regcomp(p);
gfree(p);
if (re == NULL)
valu = -1;
else
{
doforeach("egrep", legrep);
gfree(re);
}
}
}
void egrep(uchar *arg)
{
UNUSED(arg);
igrep(0);
}
void fgrep(uchar *arg)
{
UNUSED(arg);
igrep(1);
}
/* called from within regexp.c */
void regerror(const char *s)
{
userfeedback(sprintp("ill formed regexp: %s", s), 0);
}