Debian Codegraph @

gc/cord/tests/de.c

raw
1 /*
2 * Copyright (c) 1993-1994 by Xerox Corporation. All rights reserved.
3 *
4 * THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
5 * OR IMPLIED. ANY USE IS AT YOUR OWN RISK.
6 *
7 * Permission is hereby granted to use or copy this program
8 * for any purpose, provided the above notices are retained on all copies.
9 * Permission to modify the code and to distribute modified code is granted,
10 * provided the above notices are retained, and a notice that the code was
11 * modified is included with the above copyright notice.
12 */
13
14 /*
15 * A really simple-minded text editor based on cords.
16 * Things it does right:
17 * No size bounds.
18 * Unbounded undo.
19 * Shouldn't crash no matter what file you invoke it on (e.g. /vmunix)
20 * (Make sure /vmunix is not writable before you try this.)
21 * Scrolls horizontally.
22 * Things it does wrong:
23 * It doesn't handle tabs reasonably (use "expand" first).
24 * The command set is MUCH too small.
25 * The redisplay algorithm doesn't let curses do the scrolling.
26 * The rule for moving the window over the file is suboptimal.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h> /* for exit() */
31
32 #include "gc.h"
33 #include "cord.h"
34
35 #ifdef THINK_C
36 #define MACINTOSH
37 #endif
38 #include <ctype.h>
39
40 #if (defined(__BORLANDC__) || defined(__CYGWIN__) || defined(__MINGW32__) \
41 || defined(__NT__) || defined(_WIN32)) && !defined(WIN32)
42 /* If this is DOS or win16, we'll fail anyway. */
43 # define WIN32
44 #endif
45
46 #if defined(WIN32)
47 # ifndef WIN32_LEAN_AND_MEAN
48 # define WIN32_LEAN_AND_MEAN 1
49 # endif
50 # define NOSERVICE
51 # include <windows.h>
52 # include "de_win.h"
53 #elif defined(MACINTOSH)
54 # include <console.h>
55 /* curses emulation. */
56 # define initscr()
57 # define endwin()
58 # define nonl()
59 # define noecho() csetmode(C_NOECHO, stdout)
60 # define cbreak() csetmode(C_CBREAK, stdout)
61 # define refresh()
62 # define addch(c) putchar(c)
63 # define standout() cinverse(1, stdout)
64 # define standend() cinverse(0, stdout)
65 # define move(line,col) cgotoxy(col + 1, line + 1, stdout)
66 # define clrtoeol() ccleol(stdout)
67 # define de_error(s) { fprintf(stderr, s); getchar(); }
68 # define LINES 25
69 # define COLS 80
70 #else
71 # include <curses.h>
72 # include <unistd.h> /* for sleep() */
73 # define de_error(s) { fprintf(stderr, s); sleep(2); }
74 #endif
75 #include "de_cmds.h"
76
77 #if defined(CPPCHECK)
78 # define MACRO_BLKSTMT_BEGIN {
79 # define MACRO_BLKSTMT_END }
80 #else
81 # define MACRO_BLKSTMT_BEGIN do {
82 # define MACRO_BLKSTMT_END } while (0)
83 #endif
84
85 #define OUT_OF_MEMORY MACRO_BLKSTMT_BEGIN \
86 fprintf(stderr, "Out of memory\n"); \
87 exit(3); \
88 MACRO_BLKSTMT_END
89
90 /* List of line number to position mappings, in descending order. */
91 /* There may be holes. */
92 typedef struct LineMapRep {
93 int line;
94 size_t pos;
95 struct LineMapRep * previous;
96 } * line_map;
97
98 /* List of file versions, one per edit operation */
99 typedef struct HistoryRep {
100 CORD file_contents;
101 struct HistoryRep * previous;
102 line_map map; /* Invalid for first record "now" */
103 } * history;
104
105 history now = 0;
106 CORD current; /* == now -> file_contents. */
107 size_t current_len; /* Current file length. */
108 line_map current_map = 0; /* Current line no. to pos. map */
109 size_t current_map_size = 0; /* Number of current_map entries. */
110 /* Not always accurate, but reset */
111 /* by prune_map. */
112 # define MAX_MAP_SIZE 3000
113
114 /* Current display position */
115 int dis_line = 0;
116 int dis_col = 0;
117
118 # define ALL -1
119 # define NONE - 2
120 int need_redisplay = 0; /* Line that needs to be redisplayed. */
121
122
123 /* Current cursor position. Always within file. */
124 int line = 0;
125 int col = 0;
126 size_t file_pos = 0; /* Character position corresponding to cursor. */
127
128 /* Invalidate line map for lines > i */
129 void invalidate_map(int i)
130 {
131 for (;;) {
132 if (NULL == current_map) exit(4); /* for CSA, should not happen */
133 if (current_map -> line <= i) break;
134 current_map = current_map -> previous;
135 current_map_size--;
136 }
137 }
138
139 /* Reduce the number of map entries to save space for huge files. */
140 /* This also affects maps in histories. */
141 void prune_map(void)
142 {
143 line_map map = current_map;
144 int start_line = map -> line;
145
146 current_map_size = 0;
147 do {
148 current_map_size++;
149 if (map -> line < start_line - LINES && map -> previous != 0) {
150 line_map pred = map -> previous -> previous;
151
152 GC_PTR_STORE_AND_DIRTY(&map->previous, pred);
153 }
154 map = map -> previous;
155 } while (map != 0);
156 }
157
158 /* Add mapping entry */
159 void add_map(int line_arg, size_t pos)
160 {
161 line_map new_map = GC_NEW(struct LineMapRep);
162 line_map cur_map;
163
164 if (NULL == new_map) OUT_OF_MEMORY;
165 if (current_map_size >= MAX_MAP_SIZE) prune_map();
166 new_map -> line = line_arg;
167 new_map -> pos = pos;
168 cur_map = current_map;
169 GC_PTR_STORE_AND_DIRTY(&new_map->previous, cur_map);
170 current_map = new_map;
171 current_map_size++;
172 }
173
174 /* Return position of column *c of ith line in */
175 /* current file. Adjust *c to be within the line.*/
176 /* A 0 pointer is taken as 0 column. */
177 /* Returns CORD_NOT_FOUND if i is too big. */
178 /* Assumes i > dis_line. */
179 size_t line_pos(int i, int *c)
180 {
181 int j;
182 size_t cur;
183 line_map map = current_map;
184
185 while (map -> line > i) map = map -> previous;
186 if (map -> line < i - 2) /* rebuild */ invalidate_map(i);
187 for (j = map -> line, cur = map -> pos; j < i;) {
188 cur = CORD_chr(current, cur, '\n');
189 if (cur == current_len-1) return(CORD_NOT_FOUND);
190 cur++;
191 if (++j > current_map -> line) add_map(j, cur);
192 }
193 if (c != 0) {
194 size_t next = CORD_chr(current, cur, '\n');
195
196 if (next == CORD_NOT_FOUND) next = current_len - 1;
197 if (next < cur + *c) {
198 *c = (int)(next - cur);
199 }
200 cur += *c;
201 }
202 return(cur);
203 }
204
205 void add_hist(CORD s)
206 {
207 history new_file = GC_NEW(struct HistoryRep);
208
209 if (NULL == new_file) OUT_OF_MEMORY;
210 new_file -> file_contents = current = s;
211 current_len = CORD_len(s);
212 new_file -> previous = now;
213 GC_END_STUBBORN_CHANGE(new_file);
214 if (now != NULL) {
215 now -> map = current_map;
216 GC_END_STUBBORN_CHANGE(now);
217 }
218 now = new_file;
219 }
220
221 void del_hist(void)
222 {
223 now = now -> previous;
224 current = now -> file_contents;
225 current_map = now -> map;
226 current_len = CORD_len(current);
227 }
228
229 /* Current screen_contents; a dynamically allocated array of CORDs */
230 CORD * screen = 0;
231 int screen_size = 0;
232
233 # ifndef WIN32
234 /* Replace a line in the curses stdscr. All control characters are */
235 /* displayed as upper case characters in standout mode. This isn't */
236 /* terribly appropriate for tabs. */
237 void replace_line(int i, CORD s)
238 {
239 CORD_pos p;
240 # if !defined(MACINTOSH)
241 size_t len = CORD_len(s);
242 # endif
243
244 if (screen == 0 || LINES > screen_size) {
245 screen_size = LINES;
246 screen = (CORD *)GC_MALLOC(screen_size * sizeof(CORD));
247 if (NULL == screen) OUT_OF_MEMORY;
248 }
249 # if !defined(MACINTOSH)
250 /* A gross workaround for an apparent curses bug: */
251 if (i == LINES-1 && len == (unsigned)COLS) {
252 s = CORD_substr(s, 0, len - 1);
253 }
254 # endif
255 if (CORD_cmp(screen[i], s) != 0) {
256 move(i, 0); clrtoeol(); move(i,0);
257
258 CORD_FOR (p, s) {
259 int c = CORD_pos_fetch(p) & 0x7f;
260
261 if (iscntrl(c)) {
262 standout(); addch(c + 0x40); standend();
263 } else {
264 addch(c);
265 }
266 }
267 GC_PTR_STORE_AND_DIRTY(screen + i, s);
268 }
269 }
270 #else
271 # define replace_line(i,s) invalidate_line(i)
272 #endif
273
274 /* Return up to COLS characters of the line of s starting at pos, */
275 /* returning only characters after the given column. */
276 CORD retrieve_line(CORD s, size_t pos, unsigned column)
277 {
278 CORD candidate = CORD_substr(s, pos, column + COLS);
279 /* avoids scanning very long lines */
280 size_t eol = CORD_chr(candidate, 0, '\n');
281 int len;
282
283 if (eol == CORD_NOT_FOUND) eol = CORD_len(candidate);
284 len = (int)eol - (int)column;
285 if (len < 0) len = 0;
286 return(CORD_substr(s, pos + column, len));
287 }
288
289 # ifdef WIN32
290 # define refresh();
291
292 CORD retrieve_screen_line(int i)
293 {
294 size_t pos;
295
296 invalidate_map(dis_line + LINES); /* Prune search */
297 pos = line_pos(dis_line + i, 0);
298 if (pos == CORD_NOT_FOUND) return(CORD_EMPTY);
299 return(retrieve_line(current, pos, dis_col));
300 }
301 # endif
302
303 /* Display the visible section of the current file */
304 void redisplay(void)
305 {
306 int i;
307
308 invalidate_map(dis_line + LINES); /* Prune search */
309 for (i = 0; i < LINES; i++) {
310 if (need_redisplay == ALL || need_redisplay == i) {
311 size_t pos = line_pos(dis_line + i, 0);
312
313 if (pos == CORD_NOT_FOUND) break;
314 replace_line(i, retrieve_line(current, pos, dis_col));
315 if (need_redisplay == i) goto done;
316 }
317 }
318 for (; i < LINES; i++) replace_line(i, CORD_EMPTY);
319 done:
320 refresh();
321 need_redisplay = NONE;
322 }
323
324 int dis_granularity;
325
326 /* Update dis_line, dis_col, and dis_pos to make cursor visible. */
327 /* Assumes line, col, dis_line, dis_pos are in bounds. */
328 void normalize_display(void)
329 {
330 int old_line = dis_line;
331 int old_col = dis_col;
332
333 dis_granularity = 1;
334 if (LINES > 15 && COLS > 15) dis_granularity = 2;
335 while (dis_line > line) dis_line -= dis_granularity;
336 while (dis_col > col) dis_col -= dis_granularity;
337 while (line >= dis_line + LINES) dis_line += dis_granularity;
338 while (col >= dis_col + COLS) dis_col += dis_granularity;
339 if (old_line != dis_line || old_col != dis_col) {
340 need_redisplay = ALL;
341 }
342 }
343
344 # if defined(WIN32)
345 # elif defined(MACINTOSH)
346 # define move_cursor(x,y) cgotoxy(x + 1, y + 1, stdout)
347 # else
348 # define move_cursor(x,y) move(y,x)
349 # endif
350
351 /* Adjust display so that cursor is visible; move cursor into position */
352 /* Update screen if necessary. */
353 void fix_cursor(void)
354 {
355 normalize_display();
356 if (need_redisplay != NONE) redisplay();
357 move_cursor(col - dis_col, line - dis_line);
358 refresh();
359 # ifndef WIN32
360 fflush(stdout);
361 # endif
362 }
363
364 /* Make sure line, col, and dis_pos are somewhere inside file. */
365 /* Recompute file_pos. Assumes dis_pos is accurate or past eof */
366 void fix_pos(void)
367 {
368 int my_col = col;
369
370 if ((size_t)line > current_len)
371 line = (int)current_len;
372 file_pos = line_pos(line, &my_col);
373 if (file_pos == CORD_NOT_FOUND) {
374 for (line = current_map -> line, file_pos = current_map -> pos;
375 file_pos < current_len;
376 line++, file_pos = CORD_chr(current, file_pos, '\n') + 1);
377 line--;
378 file_pos = line_pos(line, &col);
379 } else {
380 col = my_col;
381 }
382 }
383
384 #if defined(WIN32)
385 # define beep() Beep(1000 /* Hz */, 300 /* ms */)
386 #elif defined(MACINTOSH)
387 # define beep() SysBeep(1)
388 #else
389 /*
390 * beep() is part of some curses packages and not others.
391 * We try to match the type of the builtin one, if any.
392 */
393 int beep(void)
394 {
395 putc('\007', stderr);
396 return(0);
397 }
398 #endif /* !WIN32 && !MACINTOSH */
399
400 # define NO_PREFIX -1
401 # define BARE_PREFIX -2
402 int repeat_count = NO_PREFIX; /* Current command prefix. */
403
404 int locate_mode = 0; /* Currently between 2 ^Ls */
405 CORD locate_string = CORD_EMPTY; /* Current search string. */
406
407 char * arg_file_name;
408
409 #ifdef WIN32
410 /* Change the current position to whatever is currently displayed at */
411 /* the given SCREEN coordinates. */
412 void set_position(int c, int l)
413 {
414 line = l + dis_line;
415 col = c + dis_col;
416 fix_pos();
417 move_cursor(col - dis_col, line - dis_line);
418 }
419 #endif /* WIN32 */
420
421 /* Perform the command associated with character c. C may be an */
422 /* integer > 256 denoting a windows command, one of the above control */
423 /* characters, or another ASCII character to be used as either a */
424 /* character to be inserted, a repeat count, or a search string, */
425 /* depending on the current state. */
426 void do_command(int c)
427 {
428 int i;
429 int need_fix_pos;
430 FILE * out;
431
432 if ( c == '\r') c = '\n';
433 if (locate_mode) {
434 size_t new_pos;
435
436 if (c == LOCATE) {
437 locate_mode = 0;
438 locate_string = CORD_EMPTY;
439 return;
440 }
441 locate_string = CORD_cat_char(locate_string, (char)c);
442 new_pos = CORD_str(current, file_pos - CORD_len(locate_string) + 1,
443 locate_string);
444 if (new_pos != CORD_NOT_FOUND) {
445 need_redisplay = ALL;
446 new_pos += CORD_len(locate_string);
447 for (;;) {
448 file_pos = line_pos(line + 1, 0);
449 if (file_pos > new_pos) break;
450 line++;
451 }
452 col = (int)(new_pos - line_pos(line, 0));
453 file_pos = new_pos;
454 fix_cursor();
455 } else {
456 locate_string = CORD_substr(locate_string, 0,
457 CORD_len(locate_string) - 1);
458 beep();
459 }
460 return;
461 }
462 if (c == REPEAT) {
463 repeat_count = BARE_PREFIX; return;
464 } else if (c < 0x100 && isdigit(c)){
465 if (repeat_count == BARE_PREFIX) {
466 repeat_count = c - '0'; return;
467 } else if (repeat_count != NO_PREFIX) {
468 repeat_count = 10 * repeat_count + c - '0'; return;
469 }
470 }
471 if (repeat_count == NO_PREFIX) repeat_count = 1;
472 if (repeat_count == BARE_PREFIX && (c == UP || c == DOWN)) {
473 repeat_count = LINES - dis_granularity;
474 }
475 if (repeat_count == BARE_PREFIX) repeat_count = 8;
476 need_fix_pos = 0;
477 for (i = 0; i < repeat_count; i++) {
478 switch(c) {
479 case LOCATE:
480 locate_mode = 1;
481 break;
482 case TOP:
483 line = col = 0;
484 file_pos = 0;
485 break;
486 case UP:
487 if (line != 0) {
488 line--;
489 need_fix_pos = 1;
490 }
491 break;
492 case DOWN:
493 line++;
494 need_fix_pos = 1;
495 break;
496 case LEFT:
497 if (col != 0) {
498 col--; file_pos--;
499 }
500 break;
501 case RIGHT:
502 if (CORD_fetch(current, file_pos) == '\n') break;
503 col++; file_pos++;
504 break;
505 case UNDO:
506 del_hist();
507 need_redisplay = ALL; need_fix_pos = 1;
508 break;
509 case BS:
510 if (col == 0) {
511 beep();
512 break;
513 }
514 col--; file_pos--;
515 /* FALLTHRU */
516 case DEL:
517 if (file_pos == current_len-1) break;
518 /* Can't delete trailing newline */
519 if (CORD_fetch(current, file_pos) == '\n') {
520 need_redisplay = ALL; need_fix_pos = 1;
521 } else {
522 need_redisplay = line - dis_line;
523 }
524 add_hist(CORD_cat(
525 CORD_substr(current, 0, file_pos),
526 CORD_substr(current, file_pos+1, current_len)));
527 invalidate_map(line);
528 break;
529 case WRITE:
530 {
531 CORD name = CORD_cat(CORD_from_char_star(arg_file_name),
532 ".new");
533
534 if ((out = fopen(CORD_to_const_char_star(name), "wb")) == NULL
535 || CORD_put(current, out) == EOF) {
536 de_error("Write failed\n");
537 need_redisplay = ALL;
538 } else {
539 fclose(out);
540 }
541 }
542 break;
543 default:
544 {
545 CORD left_part = CORD_substr(current, 0, file_pos);
546 CORD right_part = CORD_substr(current, file_pos, current_len);
547
548 add_hist(CORD_cat(CORD_cat_char(left_part, (char)c),
549 right_part));
550 invalidate_map(line);
551 if (c == '\n') {
552 col = 0; line++; file_pos++;
553 need_redisplay = ALL;
554 } else {
555 col++; file_pos++;
556 need_redisplay = line - dis_line;
557 }
558 break;
559 }
560 }
561 }
562 if (need_fix_pos) fix_pos();
563 fix_cursor();
564 repeat_count = NO_PREFIX;
565 }
566
567 /* OS independent initialization */
568
569 void generic_init(void)
570 {
571 FILE * f;
572 CORD initial;
573
574 if ((f = fopen(arg_file_name, "rb")) == NULL) {
575 initial = "\n";
576 } else {
577 size_t len;
578
579 initial = CORD_from_file(f);
580 len = CORD_len(initial);
581 if (0 == len || CORD_fetch(initial, len - 1) != '\n') {
582 initial = CORD_cat(initial, "\n");
583 }
584 }
585 add_map(0,0);
586 add_hist(initial);
587 now -> map = current_map;
588 now -> previous = now; /* Can't back up further: beginning of the world */
589 GC_END_STUBBORN_CHANGE(now);
590 need_redisplay = ALL;
591 fix_cursor();
592 }
593
594 #ifndef WIN32
595
596 int main(int argc, char **argv)
597 {
598 int c;
599 void *buf;
600
601 # if defined(MACINTOSH)
602 console_options.title = "\pDumb Editor";
603 cshow(stdout);
604 argc = ccommand(&argv);
605 # endif
606 GC_set_find_leak(0); /* app is not for testing leak detection mode */
607 GC_INIT();
608 # ifndef NO_INCREMENTAL
609 GC_enable_incremental();
610 # endif
611
612 if (argc != 2) {
613 fprintf(stderr, "Usage: %s file\n", argv[0]);
614 fprintf(stderr, "Cursor keys: ^B(left) ^F(right) ^P(up) ^N(down)\n");
615 fprintf(stderr, "Undo: ^U Write to <file>.new: ^W");
616 fprintf(stderr, "Quit:^D Repeat count: ^R[n]\n");
617 fprintf(stderr, "Top: ^T Locate (search, find): ^L text ^L\n");
618 exit(1);
619 }
620 arg_file_name = argv[1];
621 buf = GC_MALLOC_ATOMIC(8192);
622 if (NULL == buf) OUT_OF_MEMORY;
623 setvbuf(stdout, (char *)buf, _IOFBF, 8192);
624 initscr();
625 noecho(); nonl(); cbreak();
626 generic_init();
627 while ((c = getchar()) != QUIT) {
628 if (c == EOF) break;
629 do_command(c);
630 }
631 move(LINES-1, 0);
632 clrtoeol();
633 refresh();
634 nl();
635 echo();
636 endwin();
637 return 0;
638 }
639
640 #endif /* !WIN32 */