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