-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmisc.c
303 lines (295 loc) · 5.59 KB
/
misc.c
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
#include "qed.h"
void newcore(void);
void morecore(void);
void newstrarea(void);
void morestrarea(void);
void bufinit(ldesc *n);
void chngbuf(int bb);
void newbuf(int bb);
void fixbufs(int n);
void relocatebuf(ldesc *from, ldesc *to);
void shiftbuf(int up);
void syncbuf(void);
void error(int code);
void init(void);
void comment(void);
int abs(int n);
void settruth(int t);
void setcount(int c);
int truth(void);
void modified(void);
void freemem(void);
void
newcore(void)
{
free(begcore);
begcore = (ldesc *)qlloc(LDCHUNK*sizeof(ldesc));
fendcore = begcore + LDCHUNK;
endcore = fendcore - 2;
}
void
morecore(void)
{
ldesc *o1, *o2;
size_t n;
o1=begcore;
o2=fendcore;
n=(o2-o1)+LDCHUNK;
begcore=reqlloc(begcore,n * sizeof(ldesc));
fendcore=begcore+n;
endcore=fendcore-2;
memset(begcore+(o2-o1),0,LDCHUNK * sizeof(ldesc));
if(o1!=begcore)
relocatebuf(o1,begcore);
}
void
newstrarea(void)
{
int i;
free(strarea);
nstrarea = 2+NSTRCHARS; /* initial 2 is for nullstr */
strarea = (char *)qlloc(nstrarea*sizeof(char));
memset(strarea,0,nstrarea);
for(i=0;i!=NSTRING;i++){
string[i].str = nullstr;
string[i].len = 0;
}
/* initialize strfree */
string[NSTRING].str = strchars;
}
void
morestrarea(void)
{
size_t n;
n = nstrarea + NSTRCHARS;
shiftstring(DOWN);
strarea = (char *)reqlloc(strarea,n*sizeof(char));
memset(strarea+nstrarea,0,NSTRCHARS);
shiftstring(UP);
nstrarea=n;
}
void
bufinit(ldesc *n)
{
struct buffer *bufp;
for(bufp=buffer;bufp!=buffer+NBUFS;bufp++){
bufp->zero=n;
bufp->dot=n;
bufp->dol=n;
bufp->cflag = FALSE;
}
}
void
chngbuf(int bb)
{
syncbuf();
newbuf(bb);
savedfile = FILE(curbuf-buffer);
}
void
newbuf(int bb)
{
curbuf = buffer+bb;
zero=curbuf->zero;
dot=curbuf->dot;
dol=curbuf->dol;
cflag = curbuf->cflag;
}
void
fixbufs(int n)
{
struct buffer *bufp;
lock++;
for(bufp=curbuf+1;bufp!=buffer+NBUFS;bufp++){
bufp->zero+=n;
bufp->dot+=n;
bufp->dol+=n;
}
unlock();
}
void
relocatebuf(ldesc *from, ldesc *to)
{
struct buffer *bufp;
ptrdiff_t d;
d=to-from;
lock++;
for(bufp=buffer;bufp!=buffer+NBUFS;bufp++){
bufp->zero+=d;
bufp->dot+=d;
bufp->dol+=d;
}
/* fix the ldesc* globals */
zero+=d;
dot+=d;
dol+=d;
lastdol+=d;
addr1+=d;
addr2+=d;
unlock();
}
void
shiftbuf(int up)
{
struct buffer *bufp;
if(up)
for(bufp=buffer;bufp!=buffer+NBUFS;bufp++){
bufp->zero=begcore+(ptrdiff_t)bufp->zero;
bufp->dot=begcore+(ptrdiff_t)bufp->dot;
bufp->dol=begcore+(ptrdiff_t)bufp->dol;
}
else
for(bufp=buffer;bufp!=buffer+NBUFS;bufp++){
bufp->zero=(ldesc*)(bufp->zero-begcore);
bufp->dot=(ldesc*)(bufp->dot-begcore);
bufp->dol=(ldesc*)(bufp->dol-begcore);
}
}
void
syncbuf(void)
{
curbuf->zero = zero; /* we never assign to it, so needn't save */
curbuf->dot=dot;
curbuf->dol=dol;
curbuf->cflag = cflag!=FALSE; /* Normalize to fit in a char */
}
void
error(int code)
{
extern sig_t savint; /* error during a ! > < | ?? */
unlock();
if(code){
for(;stackp != stack ;--stackp)
if(stackp->type == BUF || stackp->type == STRING){
putchar('?');
if(stackp->type == BUF){
putchar('b');
putchar(bname[stackp->bufptr - buffer]);
putlong((long)stackp->lineno);
putchar('.');
}else{
putchar('z');
putchar(bname[stackp->lineno]);
}
putlong((long)stackp->charno);
putchar(' ');
}
putchar('?');
putchar(lasterr=(code&~FILERR));
if(code&FILERR){
putchar(' ');
putl(string[FILEBUF].str);
} else
putchar('\n');
}
if(eflag && code){ /* temp file is *not* unlinked. Intentional? */
freemem();
exit(code);
}
nestlevel = 0;
listf = FALSE;
gflag = FALSE;
biggflag = FALSE;
stackp = stack;
peekc = 0;
if(code && code!='?'){ /* if '?', system cleared tty input buf */
while(lastttyc!='\n' && lastttyc!=EOF){
getchar();
}
}
lseek(0, 0L, SEEK_END);
if (io > 0) {
close(io);
io = -1;
cflag = TRUE; /* well, maybe not, but better be safe */
}
appflag=0;
if(savint!=SIG_ERR){
signal(SIGINT, savint);
savint= SIG_ERR;
}
reset();
}
char tfname[]="/tmp/qed.XXXXXXXXXX";
int tfnX=10; /* Number of X-s in the template */
void
init(void)
{
char *p;
int n=tfnX;
lock++;
if(tfile>0){
close(tfile);
unlink(tfname);
}
p=tfname+strlen(tfname);
while(n-->0)
*--p='X';
tfile = mkstemp(tfname);
fcntl(tfile, F_SETFD, FD_CLOEXEC);
bufinit(begcore);
newbuf(0);
lastdol=dol;
stackp=stack;
stackp->type=TTY;
unlock();
}
void
comment(void)
{
int c, mesg;
c = getchar();
mesg = 0;
if(c == '\"') {
mesg++;
c = getchar();
}
while(c != '\n' && c != '\"') {
if(mesg)
putchar(c);
c = getchar();
}
if(mesg) {
if(c == '\n')
putchar(c);
flush();
}
}
int
abs(int n)
{
return(n<0?-n:n);
}
/*
* Slow, but avoids garbage collection
*/
void
settruth(int t)
{
if(stoi(string[TRUTH].str) != t)
numset(TRUTH, t);
}
void
setcount(int c)
{
if(stoi(string[COUNT].str) != c)
numset(COUNT, c);
}
int
truth(void)
{
return(stoi(string[TRUTH].str) != FALSE);
}
void
modified(void)
{
cflag=TRUE;
eok=FALSE;
qok=FALSE;
}
void
freemem(void)
{
free(begcore);
free(strarea);
}