Debian Codegraph @

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