-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathe.c
651 lines (592 loc) · 11.2 KB
/
e.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
#include <ncurses.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#include <errno.h>
#include <ctype.h>
#define TABSIZE 8
#define CHUNKSIZE 1024
#define STRMAX 64
#define BUFSIZE 256
#ifdef CTRL
#undef CTRL
#endif
#define CTRL(a) ((a) & 31)
char *text = 0;
int text_size = 0;
char file_name[STRMAX] = "", block_name[STRMAX] = "";
char find_str[STRMAX] = "", replace_str[STRMAX] = "";
int bos_pos = 0, eos_pos = 0, cur_pos = 0, eof_pos = 0, bow_line = 0;
int bow_line_prev = 0, win_shift = 0, cur_line = 0, cur_y = 0, cur_x = 0;
int is_changed = 0, ins_mode = 1, find_mode = 0;
#define ALIGN(x,mod) (((x) / (mod) + 1) * (mod))
#define nexttab(x) ALIGN (x, TABSIZE)
#define align_chunk(x) ALIGN (x, CHUNKSIZE)
void adduch (unsigned char ch)
{
if ((ch >= 32 && ch < 128) || ch >= 160) /* ^C or ~C */
addch (ch);
else {
attron (A_BLINK);
addch (ch > 128 && ch < 159 ? ch - 32 : ch + 64);
attroff (A_BLINK);
}
}
int confirm (char *s)
{
int ch;
move (LINES - 1, 0);
attron (A_BOLD);
addstr (s);
attroff (A_BOLD);
clrtoeol ();
refresh ();
ch = getch ();
return ch == 'y' || ch == 'Y';
}
int enter_string (char *s, char *buf)
{
int b_len, ch, flag = 1;
for (;;) {
move (LINES - 1, 0);
attron (A_BOLD);
addstr (s);
attroff (A_BOLD);
for (b_len = 0; buf[b_len]; b_len++)
adduch (buf[b_len]);
clrtoeol ();
refresh ();
ch = getch ();
switch (ch) {
case CTRL ('Y'):
*buf = 0;
break;
case CTRL ('Q'):
ch = getch ();
goto ins_char;
case KEY_BACKSPACE:
if (b_len)
buf[b_len - 1] = 0;
break;
case '\r':
return 1;
case CTRL ('X'):
return 0;
default:
if (!iscntrl (ch)) {
ins_char: if (flag)
*buf = b_len = 0;
if (b_len < STRMAX - 1) {
buf[b_len] = ch;
buf[b_len + 1] = 0;
}
} else
beep ();
break;
}
flag = 0;
}
/* NOTREACHED */
}
int error (char *s, ...)
{
va_list args;
char buf[BUFSIZE];
int i = 0;
va_start (args, s);
i += snprintf (buf + i, BUFSIZE - i, "Error ");
if (*s != '$')
i += vsnprintf (buf + i, BUFSIZE - i, s, args);
else {
i += vsnprintf (buf + i, BUFSIZE - i, s + 1, args);
i += snprintf (buf + i, BUFSIZE - i, ", %s", strerror (errno));
}
va_end (args);
beep ();
confirm (buf);
return 0; /* convinient */
}
int bol (int pos)
{
while (pos && text[pos - 1] != '\n')
pos--;
return pos;
}
int prevline (int pos)
{
pos = bol (pos);
return pos ? bol (pos - 1) : 0;
}
int eol (int pos)
{
while (pos < eof_pos && text[pos] != '\n')
pos++;
return pos;
}
int nextline (int pos)
{
pos = eol (pos);
return pos < eof_pos ? pos + 1 : pos;
}
int win_x (int line, int xx)
{
int i, x = 0;
for (i = line; i < eof_pos && i < line + xx; i++)
if (text[i] == '\n')
break;
else if (text[i] == '\t')
x = nexttab (x);
else
x++;
return x;
}
int pos_x (int line, int xx)
{
int i, x = 0;
for (i = line; i < eof_pos && x < xx; i++)
if (text[i] == '\n')
break;
else if (text[i] == '\t')
x = nexttab (x);
else
x++;
return i;
}
void show (void)
{
int i, m, t, j;
/*
* speed up scrolling
*/
if (bow_line > bow_line_prev) {
m = bow_line_prev;
for (i = 0; m != bow_line && i < LINES; i++)
m = nextline (m);
if (i < LINES)
scrl (i);
} else if (bow_line < bow_line_prev) {
m = bow_line_prev;
for (i = 0; m != bow_line && i < LINES; i++)
m = prevline (m);
if (i < LINES)
scrl (-i);
}
bow_line_prev = bow_line;
erase ();
if (!text)
return;
for (m = bow_line, i = 0; m < eof_pos && i < LINES; i++) {
m = pos_x (m, win_shift);
move (i, 0);
#define EOS_COLS (i < LINES - 1 ? COLS : COLS - 1)
for (j = 0; m < eof_pos && j < EOS_COLS; m++) {
if (m >= bos_pos && m < eos_pos)
attron (A_REVERSE);
else
attroff (A_REVERSE);
if (text[m] == '\n')
break;
else if (text[m] == '\t')
for (t = nexttab (j); j < t; j++)
addch (' ');
else {
adduch (text[m]);
j++;
}
}
if (m >= bos_pos && m < eos_pos)
while (j++ < EOS_COLS)
addch (' ');
#undef EOS_COLS
m = nextline (m);
}
attroff (A_REVERSE);
}
void k_up (void)
{
cur_line = prevline (cur_line);
cur_pos = pos_x (cur_line, cur_x + win_shift);
}
void k_down (void)
{
if (eol (cur_pos) < eof_pos) {
cur_line = nextline (cur_line);
cur_pos = pos_x (cur_line, cur_x + win_shift);
}
}
int ins_mem (int size)
{
char *p;
int i;
if (!text || eof_pos + size > text_size) {
i = align_chunk (eof_pos + size);
p = realloc (text, i);
if (!p)
return error ("- no memory");
text = p;
text_size = i;
}
/*
* read last sentence in BUGS section of memcpy(3) in FreeBSD,
* also bcopy(3) is not in ``ISO C''
*/
for (i = eof_pos - 1; i >= cur_pos; i--)
text[i + size] = text[i];
eof_pos += size;
if (bos_pos >= cur_pos)
bos_pos += size;
if (eos_pos > cur_pos)
eos_pos += size;
is_changed = 1;
return 1;
}
void del_mem (int pos, int size)
{
int i;
char *p;
for (i = pos + size; i < eof_pos; i++) /* read comment to ins_mem() */
text[i - size] = text[i];
eof_pos -= size;
is_changed = 1;
#define del_pos(p) (p > pos + size ? p -= size : p > pos ? p = pos : p)
del_pos (bos_pos);
del_pos (eos_pos);
del_pos (cur_pos);
del_pos (bow_line);
del_pos (bow_line_prev);
#undef del_pos
i = align_chunk (eof_pos);
if (i < text_size) {
p = realloc (text, i);
if (!p) {
error ("- realloc to decrease failed?");
return;
}
text = p;
text_size = i;
}
}
void ins_ch (char ch)
{
if (!ins_mode && cur_pos < eof_pos) {
if (ch == '\n') {
cur_pos = nextline (cur_pos);
return;
} else if (text[cur_pos] != '\n') {
is_changed = 1;
goto a;
}
}
if (ins_mem (1))
a: text[cur_pos++] = ch;
}
void k_copyblock (void)
{
if (eos_pos <= bos_pos || (cur_pos > bos_pos && cur_pos < eos_pos))
beep ();
else if (ins_mem (eos_pos - bos_pos))
strncpy (text + cur_pos, text + bos_pos, eos_pos - bos_pos);
}
void k_moveblock (void)
{
int i;
if (eos_pos <= bos_pos || (cur_pos > bos_pos && cur_pos < eos_pos)) {
beep ();
return;
}
k_copyblock ();
i = eos_pos - bos_pos;
del_mem (bos_pos, i);
bos_pos = cur_pos;
eos_pos = cur_pos + i;
}
void k_deleteblock (void)
{
if (eos_pos <= bos_pos)
beep ();
else
del_mem (bos_pos, eos_pos - bos_pos);
}
int find_again (int flag)
{
int f_len, i;
f_len = strlen (find_str);
if (!f_len)
return 0;
for (i = cur_pos + flag; i <= eof_pos - f_len; i++)
if (!strncmp (text + i, find_str, f_len))
break;
if (i > eof_pos - f_len)
beep ();
else
cur_pos = i;
return i <= eof_pos - f_len;
}
int k_find (void)
{
if (!enter_string ("Search for: ", find_str) || !*find_str)
return 0;
find_mode = 1;
return find_again (0);
}
void replace_again (void)
{
int i;
if (!find_again (0))
return;
del_mem (cur_pos, strlen (find_str));
if (!*replace_str)
return;
i = strlen (replace_str);
if (ins_mem (i)) {
strncpy (text + cur_pos, replace_str, i);
cur_pos += i;
}
}
void k_replace (void)
{
if (!k_find ())
return;
if (!enter_string ("Replace to: ", replace_str))
return;
find_mode = 0;
replace_again ();
}
void k_again (void)
{
if (find_mode)
find_again (1);
else
replace_again ();
}
int load (char *name)
{
FILE *f;
int i;
f = fopen (name, "r");
if (!f)
return error ("$load file \"%s\"", name);
if (fseek (f, 0, SEEK_END))
return error ("$seek");
i = ftell (f);
if (ins_mem (i)) {
if (fseek (f, 0, SEEK_SET))
return error ("$seek");
if ((int)fread (text + cur_pos, 1, i, f) < i)
return error ("$read");
} else
i = 0;
fclose (f);
return i;
}
int save (char *name, int pos, int size)
{
FILE *f;
f = fopen (name, "w");
if (!f)
return error ("$save file \"%s\"", name);
if ((int)fwrite (text + pos, 1, size, f) < size)
return error ("$write");
if (fclose (f))
return error ("$close");
return 1;
}
void k_save (void)
{
if (!enter_string ("Enter file name to save: ", file_name))
return;
if (save (file_name, 0, eof_pos))
is_changed = 0;
}
void k_getblock (void)
{
if (!enter_string ("Enter file name to load block: ", block_name))
return;
eos_pos = load (block_name) + cur_pos;
bos_pos = cur_pos;
}
void k_putblock (void)
{
if (bos_pos >= eos_pos)
return;
if (!enter_string ("Enter file name to save block: ", block_name))
return;
save (block_name, bos_pos, eos_pos - bos_pos);
}
void goto_line (int l)
{
for (cur_pos = 0; --l > 0 && cur_pos < eof_pos;)
cur_pos = nextline (cur_pos);
}
void k_goto (void)
{
char buf[STRMAX];
*buf = 0;
if (!enter_string ("Goto line: ", buf))
return;
if (*buf)
goto_line (atoi (buf));
else
cur_pos = bos_pos;
}
void done (int sig)
{
(void)sig;
endwin ();
exit (0);
}
void init (void)
{
signal (SIGINT, done);
initscr ();
keypad (stdscr, TRUE);
scrollok (stdscr, TRUE);
idlok (stdscr, TRUE);
nonl ();
raw ();
noecho ();
}
void norm_cur (void)
{
int i;
cur_line = bol (cur_pos);
while (cur_line < bow_line)
bow_line = prevline (bow_line);
cur_y = 0;
for (i = bow_line; i < cur_line; i = nextline (i))
cur_y++;
for (; cur_y >= LINES; cur_y--)
bow_line = nextline (bow_line);
cur_x = win_x (cur_line, cur_pos - cur_line) - win_shift;
while (cur_x < 0) {
cur_x += TABSIZE;
win_shift -= TABSIZE;
}
while (cur_x >= COLS) {
cur_x -= TABSIZE;
win_shift += TABSIZE;
}
}
int main (int argv, char **argc)
{
int i, ch;
init ();
if (argv >= 2) {
strncpy (file_name, argc[1], STRMAX - 1);
file_name[STRMAX - 1] = 0;
load (file_name);
is_changed = 0;
}
for (;;) {
show ();
move (cur_y, cur_x);
refresh ();
ch = getch ();
switch (ch) {
case KEY_UP:
k_up ();
break;
case KEY_DOWN:
k_down ();
break;
case KEY_LEFT:
if (cur_pos)
cur_pos--;
break;
case KEY_RIGHT:
if (cur_pos < eof_pos)
cur_pos++;
break;
case KEY_PPAGE: case CTRL ('J'):
for (i = 1; i < LINES; i++)
k_up ();
break;
case KEY_NPAGE: case CTRL ('K'):
for (i = 1; i < LINES; i++)
k_down ();
break;
case KEY_DC: /* del */
if (cur_pos < eof_pos)
del_mem (cur_pos, 1);
break;
case KEY_BACKSPACE:
if (cur_pos)
del_mem (--cur_pos, 1);
break;
case KEY_HOME:
cur_pos = cur_line;
break;
case KEY_END:
cur_pos = eol (cur_pos);
break;
case CTRL ('X'):
if (!is_changed || confirm ("Discard changes and exit? (y/N):"))
done (0);
break;
case CTRL ('T'): /* go Top */
bow_line = cur_pos = 0;
break;
case CTRL ('O'): /* go bOttom */
cur_pos = eof_pos;
break;
case CTRL ('Y'): /* del line */
del_mem (cur_line, nextline (cur_line) - cur_line);
break;
case CTRL ('B'): /* mark Begin of block */
bos_pos = cur_pos;
break;
case CTRL ('E'): /* mark End of block */
eos_pos = cur_pos;
break;
case CTRL ('Q'): /* Quote char */
ins_ch (getch ());
break;
case CTRL ('C'):
k_copyblock ();
break;
case CTRL ('D'):
k_deleteblock ();
break;
case CTRL ('V'):
k_moveblock ();
break;
case CTRL ('S'):
k_save ();
break;
case CTRL ('F'):
k_find ();
break;
case CTRL ('R'):
k_replace ();
break;
case CTRL ('N'):
k_again ();
break;
case CTRL ('P'):
k_putblock ();
break;
case CTRL ('G'):
k_getblock ();
break;
case KEY_IC:
ins_mode ^= 1;
break;
case CTRL ('A'):
k_goto ();
break;
case '\r':
ch = '\n';
/* FALLTHRU */
default:
if (!iscntrl (ch) || ch == '\t' || ch == '\n')
ins_ch (ch);
else
beep ();
break;
}
norm_cur ();
}
/* NOTREACHED */
return 0;
}