-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathmisc.c
509 lines (438 loc) · 8.32 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
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
/*
* LEVEE, or Captain Video; A vi clone
*
* Copyright (c) 1982-2007 David L Parsons
* All rights reserved.
*
* Redistribution and use in source and binary forms, without or
* without modification, are permitted provided that the above
* copyright notice and this paragraph are duplicated in all such
* forms and that any documentation, advertising materials, and
* other materials related to such distribution and use acknowledge
* that the software was developed by David L Parsons ([email protected]).
* My name may not be used to endorse or promote products derived
* from this software without specific prior written permission.
* THIS SOFTWARE IS PROVIDED AS IS'' AND WITHOUT ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
* WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
* PURPOSE.
*/
#include "levee.h"
#include "extern.h"
#include <stdlib.h>
#include <ctype.h>
#include <errno.h>
bool
lvgetline(char *str,int size)
{
int len;
char flag;
flag = line(str, 0, Min(size, COLS-curpos.x), &len);
str[len] = 0;
#if 0
if (lineonly)
dnewline();
else
dclear_to_eol();
#endif
return (flag == EOL);
} /* lvgetline */
char
readchar(void)
{
ch = peekc(); /* get the peeked character */
needchar = TRUE; /* force a read on next readchar/peekc */
if (xerox) { /* save this character for redo */
if (rcp >= &rcb[256-1]) /* oops, buffer overflow */
error();
else /* concat it at the end of rcb^ */
*rcp++ = ch;
}
return ch;
} /* readchar */
/* look at next input character without actually using it */
char
peekc(void)
{
if (needchar) { /* if buffer is empty, */
if (macro >= 0) { /* if a macro */
lastchar = *mcr[macro].ip;
mcr[macro].ip++;
if (*mcr[macro].ip == 0) {
if (--mcr[macro].m_iter > 0)
mcr[macro].ip = mcr[macro].mtext;
else
--macro;
}
}
else /* else get one from the keyboard */
lastchar = getKey();
needchar = FALSE;
}
return lastchar;
} /* peekc */
/* find the amount of leading whitespace between start && limit.
endd is the last bit of whitespace found.
*/
int
findDLE(int start,int *endd,int limit,int dle)
{
while ((core[start] == '\t' || core[start] == ' ') && start < limit) {
if (core[start] == '\t')
dle = tabsize * (1+(dle/tabsize));
else
dle++;
start++;
}
*endd = start;
return dle;
} /* findDLE */
int
skipws(int loc)
{
while ((core[loc] == '\t' || core[loc] == ' ') && loc <= bufmax)
loc++;
return(loc);
} /* skipws */
int
setX(int cp)
{
int top, xp;
top = bseekeol(cp);
xp = 0;
while (top < cp) {
switch (os_cclass(core[top])) {
case CC_TAB:
xp = tabsize*(1+(xp/tabsize));
break;
case CC_CTRL:
xp += 2;
break;
case CC_PRINT:
xp++;
break;
default:
xp += 3;
break;
}
top++;
}
return(xp);
} /* setX */
int
setY(int cp)
{
int yp, ix;
ix = ptop;
yp = -1;
cp = Min(cp,bufmax-1);
do {
yp++;
ix = 1+fseekeol(ix);
} while (ix <= cp);
return(yp);
} /* setY */
int
to_line(int cp)
{
int tdx,line;
tdx = 0;
line = 0;
while (tdx <= cp) {
tdx = 1+fseekeol(tdx);
line++;
}
return(line);
} /* to_line */
int
to_index(int line)
{
int cp = 0;
while (cp < bufmax && line > 1) {
cp = 1+fseekeol(cp);
line--;
}
return(cp);
} /* to_index */
void
swap(int *a,int *b)
{
int c;
c = *a;
*a = *b;
*b = c;
} /* swap */
void
error(void)
{
indirect = 0;
macro = -1;
if (xerox)
rcb[0] = 0;
xerox = FALSE;
Ping();
} /* error */
/* the dirty work to start up a macro */
void
insertmacro(char *cmdstr, int count)
{
if (macro >= NMACROS)
error();
else if (*cmdstr != 0) {
macro++;
mcr[macro].mtext = cmdstr; /* point at the text */
mcr[macro].ip = cmdstr; /* starting index */
mcr[macro].m_iter = count; /* # times to do the macro */
}
} /* insertmacro */
int
lookup(int c)
{
int ix = MAXMACROS;
while (--ix >= 0 && mbuffer[ix].token != c)
;
return ix;
} /* lookup */
void
fixmarkers(int base,int offset)
{
unsigned char c;
for (c = 0;c<'z'-'`';c++)
if (contexts[c] > base) {
if (contexts[c]+offset < base)
contexts[c] = -1;
else
contexts[c] += offset;
}
} /* fixmarkers */
void
wr_stat(void)
{
clrprompt();
if ( filenm != F_UNSET ) {
printch('"');
prints(args.gl_pathv[filenm]);
prints("\" ");
if (newfile)
prints("<New file> ");
printch(' ');
if (readonly)
prints("<readonly> ");
else if (modified)
prints("<Modified> ");
if (bufmax > 0) {
prints(" line ");
printi(to_line(curr));
prints(" -");
printi((int)((long)(curr*100L)/(long)bufmax));
prints("%-");
}
else
prints("-empty-");
}
else
prints("No file");
} /* wr_stat */
static int tabptr,
tabstack[20],
ixp;
void
back_up(char c)
{
int original_xp = ixp, count;
switch (os_cclass(c)) {
case CC_TAB:
ixp = tabstack[--tabptr];
break;
case CC_CTRL:
ixp -= 2;
break;
case CC_PRINT:
ixp--;
break;
default:
ixp -= 3;
break;
}
count = original_xp - ixp;
dgotoxy(ixp, -1);
if ( count > 0 ) {
do {
dwrite(" ", count%8);
count -= 8;
} while ( count > 0 );
dgotoxy(ixp, -1);
}
} /* back_up */
/*
* put input into buf[] || instring[].
* return states are:
* 0 : backed over beginning
* ESC : ended with an ESC
* EOL : ended with an '\r'
*/
char
line(char *s, int start,int endd,int *size)
{
int col0,
ip;
unsigned char c;
col0 = ixp = curpos.x;
ip = start;
tabptr = 0;
while (1) {
c = readchar();
if (movemap[c] == INSMACRO) /* map!ped macro */
insertmacro(mbuffer[lookup(c)].m_text, 1);
else if (c == DW) {
while (!wc(s[ip-1]) && ip > start)
back_up(s[--ip]);
while (wc(s[ip-1]) && ip > start)
back_up(s[--ip]);
}
else if (c == Eraseline) {
ip = start;
tabptr = 0;
dgotoxy(ixp=col0, -1);
}
else if (c == Erasechar) {
if (ip>start)
back_up(s[--ip]);
else {
*size = 0;
return(0);
}
}
else if (c=='\r' || c==ESC) {
*size = (ip-start);
return (c==ESC) ? ESC : EOL;
}
else if ((!beautify) || c == TAB || c == ''
|| (c >= ' ' && c <= '~')
|| (c & 0x80) ) {
if (ip < endd) {
if (c == '')
c = readchar();
switch (os_cclass(c)) {
case CC_TAB:
tabstack[tabptr++] = ixp;
ixp = tabsize*(1+(ixp/tabsize));
break;
case CC_CTRL:
ixp += 2;
break;
case CC_PRINT:
ixp++;
break;
default:
ixp += 3;
break;
}
s[ip++] = c;
printch(c);
}
else
error();
}
else
error();
}
} /* line */
/* move to core[loc] */
void
setpos(int loc)
{
lstart = bseekeol(loc);
lend = fseekeol(loc);
xp = setX(loc);
curr = loc;
} /* setpos */
void
resetX(void)
{
if (deranged) {
xp = setX(curr);
dgotoxy(xp, -1);
deranged = FALSE;
}
} /* resetX */
/* set end of window */
int
setend(void)
{
int bottom, count;
int lines = 0;
bottom = ptop;
count = LINES-1;
while (bottom < bufmax && count > 0) {
bottom = 1+fseekeol(bottom);
count--;
lines++;
}
pend = bottom-1; /* last char before eol || eof */
return lines;
} /* setend */
/* set top of window
* return the number of lines actually between curr && ptop.
*/
int
settop(int lines)
{
int top, yp;
top = curr;
yp = -1;
do {
yp++;
top = bseekeol(top) - 1;
lines--;
} while (top >= 0 && lines > 0);
ptop = top+1; /* tah-dah */
setend();
return(yp);
} /* settop */
int
Max(int a,int b)
{
return (a>b) ? a : b;
}
int
Min(int a,int b)
{
return (a<b) ? a : b;
}
/*
* return a tempfile name in malloc()ed memory
*/
char *
lvtempfile(char *template)
{
int length=40;
char *file = malloc(length);
char *p;
while (file) {
if ( os_mktemp(file, length, template) )
return file;
unless (errno == E2BIG) {
free(file);
return 0;
}
length *= 2;
if (p = realloc(file, length))
file = p;
else {
free(file);
return 0;
}
}
return 0;
}
#if !HAVE_STRDUP
char *
strdup(char *s)
{
char *p;
if (p=malloc(strlen(s)+1))
strcpy(p, s);
return p;
}
#endif