-
Notifications
You must be signed in to change notification settings - Fork 1
/
ed.c
788 lines (670 loc) · 17.1 KB
/
ed.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
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "aux.h"
#include "parse.h"
#include "ll.h"
#include "ed.h"
#include "io.h"
#include "err.h"
#include "undo.h"
#define ED_PROMPT_SIZE 64
static char gbl_prompt[ED_PROMPT_SIZE];
#define ED_DEFAULT_FILENAME_SIZE 4096
static char gbl_default_filename[ED_DEFAULT_FILENAME_SIZE];
static _Bool gbl_saved = 1;
void set_prompt(char *s) {
size_t sz = strlen(s);
if (sz >= ED_PROMPT_SIZE) {
err_normal(&to_repl,
"ERR: Prompt string should not be more than %d characters\n",
ED_PROMPT_SIZE-1);
}
strncpy(gbl_prompt, s, sz);
if (gbl_prompt[sz - 1] == '\n') {
gbl_prompt[sz - 1] = '\0';
}
}
char *get_prompt() {
return gbl_prompt;
}
void set_default_filename(char *s) {
size_t size = strlen(s);
if (size >= ED_DEFAULT_FILENAME_SIZE) {
err_normal(&to_repl,
"ERR: Filename should not be more than %d characters\n",
ED_DEFAULT_FILENAME_SIZE-1);
}
strncpy(gbl_default_filename, s, size+1);
if (gbl_default_filename[size - 1] == '\n') {
gbl_default_filename[size - 1] = '\0';
}
}
char *get_default_filename() {
if (gbl_default_filename[0] == '\0') {
return NULL;
}
return gbl_default_filename;
}
static ds_t *gbl_command_buf;
static yb_t *gbl_yank_buf;
static re_t *gbl_re;
static yb_t *gbl_global_cmd_buf;
void set_command_buf(char *cmd) {
if (cmd == ds_get_s(gbl_command_buf)) {
return;
}
ds_set(gbl_command_buf, cmd);
}
void append_command_buf(char *s) {
while (*s) {
ds_append(gbl_command_buf, *s++);
}
}
char *get_command_buf() {
return ds_get_s(gbl_command_buf);
}
void gbl_buffers_init() {
atexit(gbl_buffers_free);
gbl_command_buf = ds_make();
gbl_yank_buf = yb_make();
gbl_re = re_make();
gbl_global_cmd_buf = yb_make();
}
void gbl_buffers_free() {
ds_free(gbl_command_buf);
free(gbl_command_buf);
yb_free(gbl_yank_buf);
free(gbl_yank_buf);
re_free(gbl_re);
free(gbl_re);
yb_free(gbl_global_cmd_buf);
free(gbl_global_cmd_buf);
}
/* Yank Buffer Functions */
void yank_buf_push(char *s) {
yb_append(gbl_yank_buf, s);
}
char *yank_buf_get(int at) {
return yb_at(gbl_yank_buf, at);
}
void yank_buf_clear() {
yb_clear(gbl_yank_buf);
}
/* Mark functions */
#define FIRST_MARK '!'
#define LAST_MARK '~'
#define MARK_LIM (LAST_MARK - FIRST_MARK)
node_t *gbl_marks[MARK_LIM];
int set_mark(node_t *node, int at) {
if (at < FIRST_MARK || at > LAST_MARK) {
err_normal(&to_repl, "Cannot set mark to '%c'."
" Marks must be one of the characters between %c and %c"
" in the ASCII table (see `man ascii`)\n", at, FIRST_MARK,
LAST_MARK);
}
int i = at - FIRST_MARK;
gbl_marks[i] = node;
return i;
}
node_t *get_mark(int at) {
if (at < FIRST_MARK || at > LAST_MARK) {
err_normal(&to_repl, "Invalid Mark: %c", at);
}
return gbl_marks[at - FIRST_MARK];
}
void clear_mark(int at) {
gbl_marks[at - FIRST_MARK] = NULL;
}
/* ed_ functions */
static size_t append_aux(node_t *from) {
char *line = NULL;
size_t bytes = 0;
size_t lines = 0;
size_t linecap;
push_to_append_buf(&brake);
while ((bytes = io_read_line(&line, &linecap, stdin, NULL)) > 0) {
if (line[0] == '.')
break;
from = ll_add_next(from, line);
push_to_append_buf(from);
lines++;
bytes += bytes;
}
free(line);
return lines;
}
void ed_append(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('a');
from = (from == global_tail() ? ll_last_node() : from);
size_t lines = append_aux(from);
io_write_line(stdout, "%ld line%s appended\n", lines, (lines==1)?"":"s");
gbl_saved = 0;
}
void ed_insert(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('i');
from = (from == global_tail() ? ll_last_node() : from);
from = (from == global_head() ? from : ll_prev(from, 1));
size_t lines = append_aux(from);
io_write_line(stdout, "%ld line%s inserted\n", lines, (lines==1)?"":"s");
gbl_saved = 0;
}
void ed_print(node_t *from, node_t *to, char *rest) {
if (ll_len() == 0) {
return;
}
if (parse_defaults) {
io_write_line(stdout, "%s", ll_s(global_current()));
return;
}
to = (to == global_tail() ? to : ll_next(to, 1));
from = (from == global_tail() ? ll_prev(from, 1) : from);
while (from != to) {
io_write_line(stdout, "%s", ll_s(from));
from = ll_next(from, 1);
}
}
void ed_print_n(node_t *from, node_t *to, char *rest) {
if (ll_len() == 0) {
return;
}
to = (to == global_tail() ? to : ll_next(to, 1));
from = (from == global_tail() ? ll_prev(from, 1) : from);
size_t n = ll_node_index(from);
if (parse_defaults) {
io_write_line(stdout, "%ld\t%s", n, ll_s(global_current()));
return;
}
while (from != to) {
io_write_line(stdout, "%ld\t%s", n, ll_s(from));
from = ll_next(from, 1);
n++;
}
}
static size_t delete_aux(node_t *from, node_t *to) {
to = (to == global_tail() ? to : ll_next(to, 1));
from = (from == global_tail() ? ll_prev(from, 1) : from);
size_t lines = 0;
push_to_delete_buf(&brake);
while (from != to) {
push_to_delete_buf(from);
from = ll_remove_shallow(from);
lines++;
}
return lines;
}
void ed_delete(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('d');
delete_aux(from, to);
gbl_saved = 0;
}
void ed_change(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('c');
size_t lines = delete_aux(from, to);
append_aux(ll_prev(from, 1));
io_write_line(stdout, "%ld line%s changed\n", lines, (lines==1)?"":"s");
gbl_saved = 0;
}
void ed_move(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('m');
from = (from == global_head() ? ll_first_node() : from);
if (*rest == '\n') {
err_normal(&to_repl, "%s\n", "ERROR: No arguments");
}
parse_t *pt = pt_make();
parse_address(pt, rest);
node_t *move_to = pt_from(pt);
free(pt);
move_to = (move_to == global_tail() ? ll_last_node(): move_to);
node_t *move_to_subsequent = ll_next(move_to, 1);
if (ll_node_index(move_to) <= ll_node_index(to)) {
err_normal(&to_repl, "%s\n", "Invalid Destination.");
}
push_to_append_buf(&brake);
push_to_append_buf(ll_prev(from, 1));
push_to_append_buf(from);
push_to_append_buf(to);
push_to_append_buf(ll_next(to, 1));
push_to_append_buf(move_to);
push_to_append_buf(move_to_subsequent);
/*
* from->prev <-> to->next
* move_to <-> from
* to <-> move_to_subsequent
*/
ll_attach_nodes(ll_prev(from, 1), ll_next(to, 1));
ll_attach_nodes(move_to, from);
ll_attach_nodes(to, move_to_subsequent);
gbl_saved = 0;
}
void ed_newline(node_t *from, node_t *to, char *rest) {
if (parse_defaults) {
ll_set_current_node(from = ll_next(from, 1));
}
from = (from == global_tail() ? ll_last_node() : from);
ed_print(from, from, rest);
}
void ed_prompt(node_t *from, node_t *to, char *rest) {
set_prompt(rest);
}
void ed_shell(node_t *from, node_t *to, char *rest) {
if (*rest == '!') {
rest = get_command_buf();
}
FILE *fp = shopen(rest, "r");
set_command_buf(rest);
io_write_line(stdout, "%s\n", get_command_buf());
char *line = NULL;
size_t linecap;
while (io_read_line(&line, &linecap, fp, NULL) > 0) {
io_write_line(stdout, "%s", line);
}
io_write_line(stdout, "!\n");
free(line);
pclose(fp);
}
int edit_aux(char *rest) {
_Bool dontfree = 0;
if (get_default_filename() == NULL) {
dontfree = 1;
}
else {
rest = parse_filename(rest);
}
FILE *fp;
_Bool frompipe = 0;
char *cmd = rest;
if (*cmd == '!') {
cmd++;
cmd = skipspaces(cmd);
if (*cmd == '!') {
cmd = get_command_buf();
}
fp = shopen(cmd, "r");
if (fp == NULL) {
goto end;
}
set_command_buf(cmd);
frompipe = 1;
}
else {
fp = fileopen(rest, "r");
if (fp == NULL) {
goto end;
}
set_default_filename(rest);
}
/* Remove existing nodes */
node_t *node = ll_first_node();
while (node != global_tail()) {
node = ll_remove_node(node);
}
/* Load new nodes */
io_load_file(fp);
frompipe == 1 ? pclose(fp) : fclose(fp);
end:
if (!dontfree) {
free(rest);
}
return errno;
}
void ed_edit(node_t *from, node_t *to, char *rest) {
if (!gbl_saved) {
err_normal(&to_repl, "%s",
"No write since last change. Use 'E' to override or save changes.\n");
}
reset_undo();
if (*rest == '\n' || *rest == '\0') {
err_normal(&to_repl, "%s\n", "Error: No arguments");
}
if (edit_aux(rest) != 0) {
err_normal(&to_repl, "%s\n", strerror(errno));
}
gbl_saved = 1;
}
void ed_edit_force(node_t *from, node_t *to, char *rest) {
gbl_saved = 1;
ed_edit(from, to, rest);
}
void ed_file(node_t *from, node_t *to, char *rest) {
if (!isalnum(*rest)) {
if (get_default_filename() == NULL) {
return;
}
io_write_line(stdout, "%s\n", get_default_filename());
return;
}
set_default_filename(rest);
}
void ed_join(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('j');
push_to_delete_buf(&brake);
node_t *from_next = ll_next(from, 1);
to = (to == global_tail() ? to : ll_next(to, 1));
while (from_next != to) {
push_to_delete_buf(from_next);
ll_join_nodes(from, from_next);
from_next = ll_next(from, 1);
}
gbl_saved = 0;
}
void ed_quit(node_t *from, node_t *to, char *rest) {
if (!gbl_saved) {
err_normal(&to_repl, "%s",
"No write since last change."
" Use 'Q' to quit without saving or save changes.\n");
}
exit(EXIT_SUCCESS);
}
void ed_quit_force(node_t *from, node_t *to, char *rest) {
gbl_saved = 1;
ed_quit(from, to, rest);
}
void ed_read(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('r');
FILE *fp;
_Bool frompipe = 0;
if (*rest == '!') {
rest++;
rest = skipspaces(rest);
fp = shopen(rest, "r");
frompipe = 1;
}
else {
fp = fileopen(rest, "r");
}
char *line = NULL;
size_t linecap;
from = (from == global_tail() ? ll_last_node() : from);
push_to_append_buf(&brake);
while (io_read_line(&line, &linecap, fp, NULL) > 0) {
from = ll_add_next(from, line);
push_to_append_buf(from);
}
free(line);
frompipe == 1 ? pclose(fp) : fclose(fp);
}
void ed_mark(node_t *from, node_t *to, char *rest) {
set_mark(from, *rest);
}
void ed_write(node_t *from, node_t *to, char *rest) {
FILE *fp;
_Bool quit = 0;
_Bool frompipe = 0;
if (*rest == '!') {
fp = shopen(skipspaces(++rest), "w");
frompipe = 1;
}
else {
if (*rest == 'q') {
quit = 1;
rest = skipspaces(++rest);
}
if (!isalnum(*rest)) {
if (get_default_filename() == NULL) {
err_normal(&to_repl, "No default filename set. "
"Set a default filename or provide a file in the command\n");
}
rest = get_default_filename();
}
if (get_default_filename() == NULL) {
set_default_filename(rest);
}
fp = fileopen(rest, "w");
}
if (parse_defaults) {
from = ll_first_node();
to = ll_last_node();
}
to = (to == global_tail() ? to : ll_next(to, 1));
while (from != to) {
fprintf(fp, "%s", ll_s(from));
from = ll_next(from, 1);
}
gbl_saved = 1;
frompipe == 1 ? pclose(fp) : fclose(fp);
if (quit) {
ed_quit(NULL, NULL, NULL);
}
}
void ed_write_append(node_t *from, node_t *to, char *rest) {
FILE *fp;
_Bool quit = 0;
_Bool frompipe = 0;
if (*rest == 'q') {
quit = 1;
rest = skipspaces(++rest);
}
if (!isalnum(*rest)) {
if (get_default_filename() == NULL) {
err_normal(&to_repl, "No default filename set. "
"Set a default filename or provide a file in the command\n");
}
rest = get_default_filename();
}
if (get_default_filename() == NULL) {
set_default_filename(rest);
}
fp = fileopen(rest, "a");
if (parse_defaults) {
from = ll_first_node();
to = ll_last_node();
}
to = (to == global_tail() ? to : ll_next(to, 1));
while (from != to) {
fprintf(fp, "%s", ll_s(from));
from = ll_next(from, 1);
}
gbl_saved = 1;
frompipe == 1 ? pclose(fp) : fclose(fp);
if (quit) {
ed_quit(NULL, NULL, NULL);
}
}
void ed_equals(node_t *from, node_t *to, char *rest) {
if (parse_defaults) {
from = ll_last_node();
}
from = (from == global_tail() ? ll_last_node() : from);
io_write_line(stdout, "%d\n", ll_node_index(from));
}
void ed_comment(node_t *from, node_t *to, char *rest) {
}
void ed_semicolon(node_t *from, node_t *to, char *rest) {
ll_set_current_node(from);
}
void ed_transfer(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('t');
from = (from == global_head() ? ll_first_node() : from);
to = (to == global_tail() ? ll_last_node() : ll_next(to, 1));
parse_t *pt = pt_make();
parse_address(pt, rest);
node_t *move_to = pt_from(pt);
free(pt);
move_to = (move_to == global_tail() ? ll_last_node(): move_to);
push_to_delete_buf(&brake);
while (from != to) {
push_to_delete_buf(from);
from = ll_next(from, 1);
}
push_to_append_buf(&brake);
while ((from = pop_delete_buf()) != &brake) {
push_to_append_buf(ll_add_next(move_to, ll_s(from)));
}
gbl_saved = 0;
}
void ed_yank(node_t *from, node_t *to, char *rest) {
from = (from == global_head() ? ll_first_node() : from);
to = (to == global_tail() ? ll_last_node() : ll_next(to, 1));
yank_buf_clear();
while (from != to) {
yank_buf_push(ll_s(from));
from = ll_next(from, 1);
}
}
void ed_paste(node_t *from, node_t *to, char *rest) {
for (int i = 0; i < (int)yb_nmembs(gbl_yank_buf); ++i) {
from = ll_add_next(from, yank_buf_get(i));
}
}
/*
* 'rest' should be of the form:
* [delimiter][RE][delimiter][SUBS][delimiter][TAIL]
* OR
* A count suffix N or any permutation of characters r,g,p.
*
* Replace from->s to a calloc'ated string with all characters
* that match [RE], replaced with [SUBS].
*
* See manual for more.
*
* re_replace() or any other function in the chain shall not deallocate anything,
* deallocation is relegated to the user of this function.
*/
int gbl_total_substitutions = 0;
void ed_subs(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('s');
#if 0
if (parse_defaults) {
from = ll_first_node();
to = ll_last_node();
}
#endif
from = (from == global_head() ? ll_first_node() : from);
to = (to == global_tail() ? to : ll_next(to, 1));
node_t *new;
char *subst;
char *tail;
if (*rest == '\n' || isdigit(*rest) || *rest == 'r' || *rest == 'g' || *rest == 'p') {
if (re_has_subst(gbl_re) == 0) {
err_normal(&to_repl, "%s\n", "No previous substitutions");
}
subst = ds_get_s(re_get_subst(gbl_re));
tail = rest;
parse_tail_alt(gbl_re, tail);
goto end;
}
char delimiter = *rest++;
char *regex = rest;
subst = next_unescaped_delimiter(regex, delimiter);
tail = next_unescaped_delimiter(subst, delimiter);
parse_regex(gbl_re, regex);
parse_tail(gbl_re, tail);
end:
//copy = ll_prev(from, 1);
push_to_delete_buf(&brake);
push_to_append_buf(&brake);
while (from != to) {
push_to_delete_buf(from);
new = ll_make_shallow(re_replace(gbl_re, ll_s(from), subst));
ll_attach_nodes(ll_prev(from, 1), new);
ll_attach_nodes(new, ll_next(from, 1));
push_to_append_buf(from);
from = ll_next(new, 1);
}
gbl_saved = 0;
}
void ed_global(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('g');
regex_t reg;
rest = parse_global_command(®, rest);
if (parse_defaults) {
from = ll_first_node();
to = ll_last_node();
}
from = (from == global_head() ? ll_first_node() : from);
to = (to == global_tail() ? to : ll_next(to, 1));
node_t *node;
read_command_list(gbl_global_cmd_buf, rest);
while (from != to) {
node = ll_reg_next(from, ®);
if (node == NULL) {
break;
}
execute_command_list(gbl_global_cmd_buf, node);
from = ll_next(node, 1);
}
push_to_undo_buf('g');
gbl_saved = 0;
}
void ed_global_interact(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('g');
regex_t reg;
rest = parse_global_command(®, rest);
if (parse_defaults) {
from = ll_first_node();
to = ll_last_node();
}
from = (from == global_head() ? ll_first_node() : from);
to = (to == global_tail() ? to : ll_next(to, 1));
node_t *node;
rest[strlen(rest) - 2] = '\\';
while (from != to) {
node = ll_reg_next(from, ®);
if (node == NULL) {
break;
}
io_write_line(stdout, "%s", ll_s(node));
read_command_list(gbl_global_cmd_buf, rest);
execute_command_list(gbl_global_cmd_buf, node);
from = ll_next(node, 1);
}
push_to_undo_buf('g');
gbl_saved = 0;
}
void ed_global_invert(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('g');
regex_t reg;
rest = parse_global_command(®, rest);
if (parse_defaults) {
from = ll_first_node();
to = ll_last_node();
}
from = (from == global_head() ? ll_first_node() : from);
to = (to == global_tail() ? to : ll_next(to, 1));
node_t *node;
read_command_list(gbl_global_cmd_buf, rest);
while (from != to) {
node = ll_reg_next_invert(from, ®);
if (node == NULL) {
break;
}
execute_command_list(gbl_global_cmd_buf, node);
from = ll_next(node, 1);
}
gbl_saved = 0;
push_to_undo_buf('g');
}
void ed_global_interact_invert(node_t *from, node_t *to, char *rest) {
push_to_undo_buf('g');
regex_t reg;
rest = parse_global_command(®, rest);
if (parse_defaults) {
from = ll_first_node();
to = ll_last_node();
}
from = (from == global_head() ? ll_first_node() : from);
to = (to == global_tail() ? to : ll_next(to, 1));
node_t *node;
rest[strlen(rest) - 2] = '\\';
while (from != to) {
node = ll_reg_next_invert(from, ®);
if (node == NULL) {
break;
}
io_write_line(stdout, "%s", ll_s(node));
read_command_list(gbl_global_cmd_buf, rest);
execute_command_list(gbl_global_cmd_buf, node);
from = ll_next(node, 1);
}
gbl_saved = 0;
push_to_undo_buf('g');
}
void ed_undo(node_t *from, node_t *to, char *rest) {
undo();
}
void ed_redo(node_t *from, node_t *to, char *rest) {
redo();
}