forked from udo-munk/s
-
Notifications
You must be signed in to change notification settings - Fork 0
/
commands.c
594 lines (565 loc) · 14.4 KB
/
commands.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
/*
* commands.c - commands without an address
* ... also handles the :w command, which can contain an address
*
*
* Entry point:
*
* int simp_cmd(n, c)
* int n;
* char c;
* If the count n, character c and, in some cases, characters that
* follow c constitute one of the commands listed below, then the
* command is performed and 1 is returned. Otherwise, 0 is returned.
*
* The commands recognized by simp_cmd() are:
*
* a - append characters after the cursor location
* A - append characters at the end of the current line
* C - change the remainder of the line (same as c$)
* D - delete the remainder of the line (same as d$)
* i - insert characters at the cursor location
* J - join two lines together
* m - mark the current position
* o - insert lines below the current line
* O - insert lines above the current line
* p - put the contents of the yank buffer below the current line
* P - put the contents of the yank buffer above the current line
* q - quit; don't save the modifications
* r<char> - replace the current character by <char>
* s - substitute for the current character (same as c<space>)
* u - undo the most recent buffer-change command
* <n>x - delete n characters (same as <n>d<space>)
* ZZ - save the modifications and quit
* ? - tell the current position in the file
* . - redo the most recent buffer-change command
* * - iterate the last search-change pair
* :e - edit another file
* :r - read a file; place its contents below the current line
* :R - read a file; place its contents above the current line
* :w - write a range of lines to a file
*
*
* External procedures calls:
*
* address(n, c, op) .. file address.c
* int n;
* char c, op;
* Set the buffer's cursor according to the count n,
* the cursor movement command c and the operation op.
*
* b_delete(first, last) .. file Bman.c
* int first, last;
* Delete a range of buffer lines.
*
* b_free() .. file Bman.c
* Free all temporary buffer storage.
*
* b_getcur(line_ptr, pos_ptr) .. file Bman.c
* int *line_ptr, *pos_ptr;
* Return the line and position of the cursor.
*
* b_getmark(line_ptr, pos_ptr) .. file Bman.c
* int *line_ptr, *pos_ptr;
* Return the line and position of the mark.
*
* b_gets(k, s) .. file Bman.c
* int k;
* char s[];
* Copy the k-th buffer line to s.
*
* int b_insert(k, s) .. file Bman.c
* int k;
* char s[];
* Insert s into the buffer as line k; tell if successful.
*
* int b_modified() .. file Bman.c
* Tell if buffer contents differ from the external file.
*
* b_setcur(line, pos) .. file Bman.c
* int line, pos;
* Set the cursor to the indicated location.
*
* b_setline(line) .. file Bman.c
* int line;
* Set the cursor to line's first nonwhite character.
*
* b_setmark(line, pos) .. file Bman.c
* int line, pos;
* Mark the indicated location.
*
* int b_size() .. file Bman.c
* Return the number of lines currently in the buffer.
*
* b_unmod() .. file Bman.c
* Record that buffer contents match the external file.
*
* do_insert() .. file operator.c
* Read characters from keyboard; insert them in buffer.
*
* do_put(way) .. file operator.c
* int way;
* Put the yank buffer on the indicated side of the current line.
*
* k_donext(command) .. file keyboard.c
* char *command;
* Arrange for the given editor command to be executed next.
*
* k_finish() .. file keyboard.c
* Close down the keyboard manager.
*
* int k_getch() .. file keyboard.c
* Return the next character of the command.
*
* k_redo() .. file keyboard.c
* Redo the most recent buffer-change command.
*
* s_finish() .. file Sman.c
* Shut down the display module.
*
* char *s_getmsg(msg) .. file Sman.c
* char *msg;
* Print msg; return the user's reply,
*
* int s_ismsg() .. file Sman.c
* Tell if an error message is pending.
*
* s_putmsg(msg) .. file Sman.c
* char *msg;
* Print the message on the last row.
*
* s_refresh() .. file Sman.c
* Bring the display up to date with the buffer.
*
* s_savemsg(msg, val) .. file Sman.c
* char *msg;
* int val;
* Format msg and save it for the next screen refreshing.
*
* undo() .. file Bman.c
* Undo the most recent buffer-change command.
*/
#include <stdlib.h>
#include <string.h>
#include "s.h"
extern void b_getcur(), b_gets(), b_setcur(), s_refresh(), do_insert();
extern void k_donext(), s_savemsg(), b_delete(), b_setmark();
extern void do_put(), s_putmsg(), b_free(), k_finish(), s_finish();
extern void undo(), k_redo(), adjust(), b_getmark(), address();
extern void b_newcmd(), b_unmod(), b_setline(), s_errmsg();
extern int b_size(), b_modified(), k_getch(), s_ismsg(), strsame(), b_insert();
static void do_star(), do_io(), do_write(), write_lines();
static int do_read();
static char cur_file[MAXTEXT]; /* remembers name of the current file */
int simp_cmd(n, c)
int n;
char c;
{
int cur_line, cur_pos, i;
char *t, text1[MAXTEXT], text2[MAXTEXT];
b_getcur(&cur_line, &cur_pos);
switch (c) {
case 'a':
b_gets(cur_line, text1);
if (text1[0] != '\0') { /* unless the line is empty */
b_setcur(cur_line, cur_pos+1);
s_refresh();
}
do_insert();
break;
case 'A':
b_gets(cur_line, text1);
b_setcur(cur_line, strlen(text1));
s_refresh();
do_insert();
break;
case 'C':
k_donext("c$");
break;
case 'D':
k_donext("d$");
break;
case 'i':
do_insert();
break;
case 'J':
if (cur_line >= b_size()) {
UNKNOWN;
break;
}
b_gets(cur_line, text1);
i = strlen(text1);
/* put a space between the two lines */
text1[i] = ' ';
b_gets(cur_line + 1, text2);
/* strip leading white characters from second line */
for (t = text2; isspace(*t); ++t)
;
if (i + 1 + strlen(t) >= MAXTEXT-1) {
s_savemsg("Line length exceeds %d.", MAXTEXT);
break;
}
strcpy(text1 + i + 1, t);
b_delete(cur_line, cur_line + 1);
b_insert(cur_line, text1);
b_setcur(cur_line, i);
break;
case 'm':
b_setmark();
break;
case 'o':
++cur_line;
/* no break statement; fall into ... */
case 'O':
b_insert(cur_line, "");
b_setcur(cur_line, 0);
s_refresh();
do_insert();
break;
case 'p':
do_put(1);
break;
case 'P':
do_put(-1);
break;
case 'q':
if (b_modified()) {
s_putmsg("Discard? ");
if (k_getch() != 'y') {
UNKNOWN;
break;
}
}
if (s_ismsg())
s_refresh();
b_free();
k_finish();
s_finish();
exit(0);
case 'r':
sprintf(text1, "c %c%c", k_getch(), ESCAPE);
k_donext(text1); /* c<space><char><esc> */
break;
case 's':
k_donext("c "); /* c<space> */
break;
case 'u':
undo();
break;
case 'x':
if (n == 0) /* set default */
n = 1;
sprintf(text1, "%dd ", n);
k_donext(text1); /* <n>d<space> */
break;
case 'Z':
if (k_getch() != 'Z') {
UNKNOWN;
break;
}
if (b_modified()) {
sprintf(text1, ":w%cq", CR);
k_donext(text1); /* :w<return>q */
} else
k_donext("q");
break;
case '?':
sprintf(text1, "%s: %sline %d of %d", cur_file,
(b_modified()) ? "[Modified] " : "",
cur_line, b_size());
s_savemsg(text1, 0);
break;
case '.':
k_redo();
break;
case '*':
do_star();
break;
case ':':
do_io();
break;
case '#':
adjust(n);
break;
default:
return(0);
}
return(1);
}
/*
-------------------- the star command --------------------
* Entry point:
*
* do_star()
* From the minimum of the current and marked lines, alternate
* between n and redo commands until the maximum of the two lines
* is reached. The marked line defaults to the last buffer line.
*/
#define STAR_MAX 50 /* limits the number of n and redo commands */
static void do_star()
{
static int all_lines, doing_star = 0, iterations, start_line = 0,
start_pos;
int cur_line, cur_pos, done, mark_line, mark_pos, old_line, old_pos;
if (!b_modified()) {
UNKNOWN;
return;
}
if (s_ismsg()) { /* an error message is waiting; don't continue */
doing_star = 0;
if (start_line == 0)
UNKNOWN;
else
b_setcur(start_line, start_pos);
return;
}
if (!doing_star) { /* initialize for this star command */
doing_star = 1;
iterations = 0;
b_getcur(&start_line, &start_pos);
b_getmark(&mark_line, &mark_pos);
if (mark_line > 0 && mark_line < start_line) {
/* guarantee that cursor precedes mark */
b_setmark();
b_setcur(mark_line, mark_pos);
b_getcur(&start_line, &start_pos);
b_getmark(&mark_line, &mark_pos);
}
if (mark_line == 0 || mark_line == b_size() || start_line == b_size())
all_lines = 1;
else {
all_lines = 0;
/* move mark to the following line */
b_setcur(mark_line+1, 0);
b_setmark();
b_setcur(start_line, start_pos);
}
}
/* execute an n command */
b_getcur(&old_line, &old_pos);
address(0, 'n', ' ');
b_getcur(&cur_line, &cur_pos);
b_getmark(&mark_line, &mark_pos);
/* done if past mark or if the search fails */
done = (((!all_lines && cur_line >= mark_line) ||
(cur_line < old_line) ||
(cur_line == old_line)) && (cur_pos <= old_pos));
if (!done && ++iterations <= STAR_MAX)
/* execute a redo command then return to this procedure */
k_donext(".*");
else {
if (done)
if (iterations == 1)
s_savemsg("1 change", 0);
else
s_savemsg("%d changes", iterations);
else
s_savemsg("%d changes; type '*' to continue.", STAR_MAX);
doing_star = 0;
b_setcur(start_line, start_pos);
}
}
/*
-------------------- I/O commands --------------------
*
* Entry point:
*
* do_io();
* Read the part of an edit command after ':', then execute it.
*
*/
static void do_io()
{
int cur_line, cur_pos, size;
char *file, *s_getmsg(), msg[80], *reply;
/* get the remainder of the command */
reply = s_getmsg(":");
b_getcur(&cur_line, &cur_pos);
/* write commands contain an address; treat them as a special case */
if (*reply == 'w') {
do_write(reply+1);
b_setcur(cur_line, cur_pos);
return;
}
/* find the start of the file name */
for (file = reply+1; *file == ' '; ++file)
;
if (*file == '\0')
file = cur_file; /* default: use the current file name */
switch (*reply) {
case 'e':
if (b_modified()) {
s_putmsg("Discard? ");
if (k_getch() != 'y')
break;
}
if (file != cur_file)
strcpy(cur_file, file); /* remember name */
b_delete(1, b_size());
b_newcmd(); /* so the :e command cannot be undone */
if ((size = do_read(file, 1)) > 0) {
sprintf(msg, "%s: %d lines", file, size);
s_savemsg(msg, 0);
} else {
if (size == -1) {
sprintf(msg, "%s is a new file", file);
s_savemsg(msg, 0);
}
b_insert(1, "");
b_setcur(1, 0);
}
/* record that buffer contents match external file */
b_unmod();
break;
case 'r':
++cur_line;
/* no break statement; fall into ... */
case 'R':
if ((size = do_read(file, cur_line)) > 5)
s_savemsg("%d lines read", size);
else if (size == -1) {
sprintf(msg, "Cannot read %s.", file);
s_savemsg(msg, 0);
}
break;
default:
UNKNOWN;
break;
}
}
/*
* do_read - read a file to buffer at the indicated line;
* a returned value >= 0 tells number of lines that were read from the file
* a returned value -1 means that the file could not be opened for reading
* a returned value -2 means that the file contains nonprintable characters
*/
static int do_read(file, line)
char *file;
int line;
{
FILE *fp, *fopen();
int i, c;
char text[MAXTEXT];
if ((fp = fopen(file, "r")) == NULL)
return(-1);
/*
* Read the first ten characters and check that they are printable.
* Some C I/O packages read '\r' (<return>) characters. Also,
* nonprintable characters in files might be used for, e.g.,
* printer control characters.
*/
for (i = 0; (c = getc(fp)) != EOF && i < 10; ++i)
if (!isprint(c) && c != '\n' && c != '\t' ) {
sprintf(text, "%s is not a text file", file);
s_savemsg(text, 0);
fclose(fp);
return(-2);
}
if (i == 0) {
sprintf(text, "%s is empty", file);
s_savemsg(text, 0);
fclose(fp);
return(0);
}
rewind(fp);
/* copy the file to the buffer */
for (i = line; fgets(text, MAXTEXT, fp) != NULL; ++i) {
text[strlen(text)-1] = '\0'; /* trim off the newline */
if (b_insert(i, text) == 0) {
s_errmsg("Out of memory on line %d", i);
break;
}
}
fclose(fp);
/* move to the first nonwhite character in the first line read */
b_setline(line);
return(i - line);
}
/* do_write - write a file according to given specifications */
static void do_write(specs)
char *specs;
{
int cur_line, cur_pos, n, new_line, new_pos;
char *addr, *file;
/* special case: :w<return> writes buffer to current file */
if (*specs == '\0') {
write_lines(1, b_size(), cur_file);
return;
}
/* special case: :w<space>file<return> writes buffer to named file */
if (*specs == ' ') {
for (file = specs + 1; *file == ' '; ++file)
;
write_lines(1, b_size(), file);
return;
}
/* get the count that follows ":w" */
n = 0;
for (addr = specs; isdigit(*addr); ++addr)
n = 10*n + *addr - '0';
if (*addr == '\0') {
UNKNOWN;
return;
}
/* find and mark the end of the address */
for (file = addr; *file != '\0' && *file != ' '; ++file)
;
if (*file == ' ')
*file++ = '\0';
b_getcur(&cur_line, &cur_pos);
/* push the address, minus the first character, back on the input */
k_donext(addr + 1);
address(n, *addr, ':');
b_getcur(&new_line, &new_pos);
if (new_line == cur_line && new_pos == cur_pos) {
/* the address did not make sense */
UNKNOWN;
return;
}
/* special case: :w<addr><return> writes to the current file */
if (*file == '\0')
file = cur_file;
if (cur_line <= new_line)
write_lines(cur_line, new_line, file);
else
write_lines(new_line, cur_line, file);
}
/* write_lines - write a range of lines to a file */
static void write_lines(from, to, file)
int from, to;
char *file;
{
FILE *fopen(), *fp;
int count = to - from + 1;
char text[MAXTEXT-1];
/* be cautious about overwriting files */
if (!strsame(file, cur_file)) {
if ((fp = fopen(file, "r")) != NULL) {
s_putmsg("Overwrite? ");
if (k_getch() == 'y')
fclose(fp);
else
return;
}
} else if (from > 1 || to < b_size()) {
s_putmsg("Write partial buffer to current file? ");
if (k_getch() != 'y')
return;
}
if ((fp = fopen(file, "w")) == NULL) {
sprintf(text, "Cannot write %s.", file);
s_savemsg(text, 0);
return;
}
/* if entire buffer is saved, record that the user is free to quit */
if (strsame(file, cur_file) && from == 1 && to == b_size())
b_unmod();
/* write the lines */
while (from <= to) {
b_gets(from++, text);
fprintf(fp, "%s\n", text);
}
fclose(fp);
sprintf(text, "%s: %d line%s", file, count, (count == 1) ? "" : "s");
s_savemsg(text, 0);
}