1 /* vi: set sw=4 ts=4: */
3 * tiny vi.c: A small 'vi' clone
4 * Copyright (C) 2000, 2001 Sterling Huxley <sterling@europa.com>
6 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
12 * $HOME/.exrc and ./.exrc
13 * add magic to search /foo.*bar
16 * if mark[] values were line numbers rather than pointers
17 * it would be easier to change the mark when add/delete lines
18 * More intelligence in refresh()
19 * ":r !cmd" and "!cmd" to filter text through an external command
20 * A true "undo" facility
21 * An "ex" line oriented mode- maybe using "cmdedit"
26 #define ENABLE_FEATURE_VI_CRASHME 0
28 #if ENABLE_LOCALE_SUPPORT
29 #define Isprint(c) isprint((c))
31 /* 0x9b is Meta-ESC */
32 #define Isprint(c) ((unsigned char)(c) >= ' ' && (c) != 0x7f && (unsigned char)(c) != 0x9b)
35 #define MAX_SCR_COLS BUFSIZ
37 // Misc. non-Ascii keys that report an escape sequence
38 #define VI_K_UP (char)128 // cursor key Up
39 #define VI_K_DOWN (char)129 // cursor key Down
40 #define VI_K_RIGHT (char)130 // Cursor Key Right
41 #define VI_K_LEFT (char)131 // cursor key Left
42 #define VI_K_HOME (char)132 // Cursor Key Home
43 #define VI_K_END (char)133 // Cursor Key End
44 #define VI_K_INSERT (char)134 // Cursor Key Insert
45 #define VI_K_PAGEUP (char)135 // Cursor Key Page Up
46 #define VI_K_PAGEDOWN (char)136 // Cursor Key Page Down
47 #define VI_K_FUN1 (char)137 // Function Key F1
48 #define VI_K_FUN2 (char)138 // Function Key F2
49 #define VI_K_FUN3 (char)139 // Function Key F3
50 #define VI_K_FUN4 (char)140 // Function Key F4
51 #define VI_K_FUN5 (char)141 // Function Key F5
52 #define VI_K_FUN6 (char)142 // Function Key F6
53 #define VI_K_FUN7 (char)143 // Function Key F7
54 #define VI_K_FUN8 (char)144 // Function Key F8
55 #define VI_K_FUN9 (char)145 // Function Key F9
56 #define VI_K_FUN10 (char)146 // Function Key F10
57 #define VI_K_FUN11 (char)147 // Function Key F11
58 #define VI_K_FUN12 (char)148 // Function Key F12
60 /* vt102 typical ESC sequence */
61 /* terminal standout start/normal ESC sequence */
62 static const char SOs[] = "\033[7m";
63 static const char SOn[] = "\033[0m";
64 /* terminal bell sequence */
65 static const char bell[] = "\007";
66 /* Clear-end-of-line and Clear-end-of-screen ESC sequence */
67 static const char Ceol[] = "\033[0K";
68 static const char Ceos [] = "\033[0J";
69 /* Cursor motion arbitrary destination ESC sequence */
70 static const char CMrc[] = "\033[%d;%dH";
71 /* Cursor motion up and down ESC sequence */
72 static const char CMup[] = "\033[A";
73 static const char CMdown[] = "\n";
79 FORWARD = 1, // code depends on "1" for array index
80 BACK = -1, // code depends on "-1" for array index
81 LIMITED = 0, // how much of text[] in char_search
82 FULL = 1, // how much of text[] in char_search
84 S_BEFORE_WS = 1, // used in skip_thing() for moving "dot"
85 S_TO_WS = 2, // used in skip_thing() for moving "dot"
86 S_OVER_WS = 3, // used in skip_thing() for moving "dot"
87 S_END_PUNCT = 4, // used in skip_thing() for moving "dot"
88 S_END_ALNUM = 5, // used in skip_thing() for moving "dot"
91 /* vi.c expects chars to be unsigned. */
92 /* busybox build system provides that, but it's better */
93 /* to audit and fix the source */
96 #define VI_AUTOINDENT 1
97 #define VI_SHOWMATCH 2
98 #define VI_IGNORECASE 4
99 #define VI_ERR_METHOD 8
100 #define autoindent (vi_setops & VI_AUTOINDENT)
101 #define showmatch (vi_setops & VI_SHOWMATCH )
102 #define ignorecase (vi_setops & VI_IGNORECASE)
103 /* indicate error with beep or flash */
104 #define err_method (vi_setops & VI_ERR_METHOD)
107 static int editing; // >0 while we are editing a file
108 static int cmd_mode; // 0=command 1=insert 2=replace
109 static int file_modified; // buffer contents changed
110 static int last_file_modified = -1;
111 static int fn_start; // index of first cmd line file name
112 static int save_argc; // how many file names on cmd line
113 static int cmdcnt; // repetition count
114 static fd_set rfds; // use select() for small sleeps
115 static struct timeval tv; // use select() for small sleeps
116 static int rows, columns; // the terminal screen is this size
117 static int crow, ccol, offset; // cursor is on Crow x Ccol with Horz Ofset
118 static char *status_buffer; // mesages to the user
119 #define STATUS_BUFFER_LEN 200
120 static int have_status_msg; // is default edit status needed?
121 static int last_status_cksum; // hash of current status line
122 static char *cfn; // previous, current, and next file name
123 static char *text, *end; // pointers to the user data in memory
124 static char *screen; // pointer to the virtual screen buffer
125 static int screensize; // and its size
126 static char *screenbegin; // index into text[], of top line on the screen
127 static char *dot; // where all the action takes place
129 static struct termios term_orig, term_vi; // remember what the cooked mode was
130 static char erase_char; // the users erase character
131 static char last_input_char; // last char read from user
132 static char last_forward_char; // last char searched for with 'f'
134 #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
135 static int last_row; // where the cursor was last moved to
137 #if ENABLE_FEATURE_VI_USE_SIGNALS
138 static jmp_buf restart; // catch_sig()
140 #if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
143 #if ENABLE_FEATURE_VI_DOT_CMD
144 static int adding2q; // are we currently adding user input to q
145 static char *last_modifying_cmd; // last modifying cmd for "."
146 static char *ioq, *ioq_start; // pointer to string for get_one_char to "read"
148 #if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
149 static char *modifying_cmds; // cmds that modify text[]
151 #if ENABLE_FEATURE_VI_READONLY
152 static int vi_readonly, readonly;
154 #if ENABLE_FEATURE_VI_YANKMARK
155 static char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
156 static int YDreg, Ureg; // default delete register and orig line for "U"
157 static char *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
158 static char *context_start, *context_end;
160 #if ENABLE_FEATURE_VI_SEARCH
161 static char *last_search_pattern; // last pattern from a '/' or '?' search
165 static void edit_file(char *); // edit one file
166 static void do_cmd(char); // execute a command
167 static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
168 static char *begin_line(char *); // return pointer to cur line B-o-l
169 static char *end_line(char *); // return pointer to cur line E-o-l
170 static char *prev_line(char *); // return pointer to prev line B-o-l
171 static char *next_line(char *); // return pointer to next line B-o-l
172 static char *end_screen(void); // get pointer to last char on screen
173 static int count_lines(char *, char *); // count line from start to stop
174 static char *find_line(int); // find begining of line #li
175 static char *move_to_col(char *, int); // move "p" to column l
176 static int isblnk(char); // is the char a blank or tab
177 static void dot_left(void); // move dot left- dont leave line
178 static void dot_right(void); // move dot right- dont leave line
179 static void dot_begin(void); // move dot to B-o-l
180 static void dot_end(void); // move dot to E-o-l
181 static void dot_next(void); // move dot to next line B-o-l
182 static void dot_prev(void); // move dot to prev line B-o-l
183 static void dot_scroll(int, int); // move the screen up or down
184 static void dot_skip_over_ws(void); // move dot pat WS
185 static void dot_delete(void); // delete the char at 'dot'
186 static char *bound_dot(char *); // make sure text[0] <= P < "end"
187 static char *new_screen(int, int); // malloc virtual screen memory
188 static char *new_text(int); // malloc memory for text[] buffer
189 static char *char_insert(char *, char); // insert the char c at 'p'
190 static char *stupid_insert(char *, char); // stupidly insert the char c at 'p'
191 static char find_range(char **, char **, char); // return pointers for an object
192 static int st_test(char *, int, int, char *); // helper for skip_thing()
193 static char *skip_thing(char *, int, int, int); // skip some object
194 static char *find_pair(char *, char); // find matching pair () [] {}
195 static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
196 static char *text_hole_make(char *, int); // at "p", make a 'size' byte hole
197 static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
198 static void show_help(void); // display some help info
199 static void rawmode(void); // set "raw" mode on tty
200 static void cookmode(void); // return to "cooked" mode on tty
201 static int mysleep(int); // sleep for 'h' 1/100 seconds
202 static char readit(void); // read (maybe cursor) key from stdin
203 static char get_one_char(void); // read 1 char from stdin
204 static int file_size(const char *); // what is the byte size of "fn"
205 static int file_insert(char *, char *, int);
206 static int file_write(char *, char *, char *);
207 static void place_cursor(int, int, int);
208 static void screen_erase(void);
209 static void clear_to_eol(void);
210 static void clear_to_eos(void);
211 static void standout_start(void); // send "start reverse video" sequence
212 static void standout_end(void); // send "end reverse video" sequence
213 static void flash(int); // flash the terminal screen
214 static void show_status_line(void); // put a message on the bottom line
215 static void psb(const char *, ...); // Print Status Buf
216 static void psbs(const char *, ...); // Print Status Buf in standout mode
217 static void ni(const char *); // display messages
218 static int format_edit_status(void); // format file status on status line
219 static void redraw(int); // force a full screen refresh
220 static void format_line(char*, char*, int);
221 static void refresh(int); // update the terminal from screen[]
223 static void Indicate_Error(void); // use flash or beep to indicate error
224 #define indicate_error(c) Indicate_Error()
225 static void Hit_Return(void);
227 #if ENABLE_FEATURE_VI_SEARCH
228 static char *char_search(char *, const char *, int, int); // search for pattern starting at p
229 static int mycmp(const char *, const char *, int); // string cmp based in "ignorecase"
231 #if ENABLE_FEATURE_VI_COLON
232 static char *get_one_address(char *, int *); // get colon addr, if present
233 static char *get_address(char *, int *, int *); // get two colon addrs, if present
234 static void colon(char *); // execute the "colon" mode cmds
236 #if ENABLE_FEATURE_VI_USE_SIGNALS
237 static void winch_sig(int); // catch window size changes
238 static void suspend_sig(int); // catch ctrl-Z
239 static void catch_sig(int); // catch ctrl-C and alarm time-outs
241 #if ENABLE_FEATURE_VI_DOT_CMD
242 static void start_new_cmd_q(char); // new queue for command
243 static void end_cmd_q(void); // stop saving input chars
245 #define end_cmd_q() ((void)0)
247 #if ENABLE_FEATURE_VI_SETOPTS
248 static void showmatching(char *); // show the matching pair () [] {}
250 #if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
251 static char *string_insert(char *, char *); // insert the string at 'p'
253 #if ENABLE_FEATURE_VI_YANKMARK
254 static char *text_yank(char *, char *, int); // save copy of "p" into a register
255 static char what_reg(void); // what is letter of current YDreg
256 static void check_context(char); // remember context for '' command
258 #if ENABLE_FEATURE_VI_CRASHME
259 static void crash_dummy();
260 static void crash_test();
261 static int crashme = 0;
263 #if ENABLE_FEATURE_VI_COLON
264 static char *initial_cmds[] = { NULL, NULL , NULL }; // currently 2 entries, NULL terminated
268 static void write1(const char *out)
273 int vi_main(int argc, char **argv);
274 int vi_main(int argc, char **argv)
277 RESERVE_CONFIG_BUFFER(STATUS_BUFFER, STATUS_BUFFER_LEN);
279 #if ENABLE_FEATURE_VI_YANKMARK
282 #if defined(CONFIG_FEATURE_VI_USE_SIGNALS) || defined(CONFIG_FEATURE_VI_CRASHME)
285 #if ENABLE_FEATURE_VI_CRASHME
286 srand((long) my_pid);
289 status_buffer = STATUS_BUFFER;
290 last_status_cksum = 0;
292 #if ENABLE_FEATURE_VI_READONLY
293 vi_readonly = readonly = FALSE;
294 if (strncmp(argv[0], "view", 4) == 0) {
299 vi_setops = VI_AUTOINDENT | VI_SHOWMATCH | VI_IGNORECASE | VI_ERR_METHOD;
300 #if ENABLE_FEATURE_VI_YANKMARK
301 for (i = 0; i < 28; i++) {
303 } // init the yank regs
305 #if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
306 modifying_cmds = (char *) "aAcCdDiIJoOpPrRsxX<>~"; // cmds modifying text[]
309 // 1- process $HOME/.exrc file (not inplemented yet)
310 // 2- process EXINIT variable from environment
311 // 3- process command line args
312 #if ENABLE_FEATURE_VI_COLON
314 char *p = getenv("EXINIT");
316 initial_cmds[0] = xstrdup(p);
319 while ((c = getopt(argc, argv, "hCR" USE_FEATURE_VI_COLON("c:"))) != -1) {
321 #if ENABLE_FEATURE_VI_CRASHME
326 #if ENABLE_FEATURE_VI_READONLY
327 case 'R': // Read-only flag
332 //case 'r': // recover flag- ignore- we don't use tmp file
333 //case 'x': // encryption flag- ignore
334 //case 'c': // execute command first
335 #if ENABLE_FEATURE_VI_COLON
336 case 'c': // cmd line vi command
338 initial_cmds[initial_cmds[0] != 0] = xstrdup(optarg);
340 //case 'h': // help -- just use default
348 // The argv array can be used by the ":next" and ":rewind" commands
350 fn_start = optind; // remember first file name for :next and :rew
353 //----- This is the main file handling loop --------------
354 if (optind >= argc) {
355 editing = 1; // 0= exit, 1= one file, 2= multiple files
358 for (; optind < argc; optind++) {
359 editing = 1; // 0=exit, 1=one file, 2+ =many files
361 cfn = xstrdup(argv[optind]);
365 //-----------------------------------------------------------
370 static void edit_file(char * fn)
375 #if ENABLE_FEATURE_VI_USE_SIGNALS
378 #if ENABLE_FEATURE_VI_YANKMARK
379 static char *cur_line;
386 if (ENABLE_FEATURE_VI_WIN_RESIZE)
387 get_terminal_width_height(0, &columns, &rows);
388 new_screen(rows, columns); // get memory for virtual screen
390 cnt = file_size(fn); // file size
391 size = 2 * cnt; // 200% of file size
392 new_text(size); // get a text[] buffer
393 screenbegin = dot = end = text;
395 ch = file_insert(fn, text, cnt);
398 char_insert(text, '\n'); // start empty buf with dummy line
401 last_file_modified = -1;
402 #if ENABLE_FEATURE_VI_YANKMARK
403 YDreg = 26; // default Yank/Delete reg
404 Ureg = 27; // hold orig line for "U" cmd
405 for (cnt = 0; cnt < 28; cnt++) {
408 mark[26] = mark[27] = text; // init "previous context"
411 last_forward_char = last_input_char = '\0';
415 #if ENABLE_FEATURE_VI_USE_SIGNALS
417 signal(SIGWINCH, winch_sig);
418 signal(SIGTSTP, suspend_sig);
419 sig = setjmp(restart);
421 screenbegin = dot = text;
426 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
429 offset = 0; // no horizontal offset
431 #if ENABLE_FEATURE_VI_DOT_CMD
432 free(last_modifying_cmd);
434 ioq = ioq_start = last_modifying_cmd = NULL;
437 redraw(FALSE); // dont force every col re-draw
440 #if ENABLE_FEATURE_VI_COLON
445 while ((p = initial_cmds[n])) {
455 free(initial_cmds[n]);
456 initial_cmds[n] = NULL;
461 //------This is the main Vi cmd handling loop -----------------------
462 while (editing > 0) {
463 #if ENABLE_FEATURE_VI_CRASHME
465 if ((end - text) > 1) {
466 crash_dummy(); // generate a random command
469 dot = string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
474 last_input_char = c = get_one_char(); // get a cmd from user
475 #if ENABLE_FEATURE_VI_YANKMARK
476 // save a copy of the current line- for the 'U" command
477 if (begin_line(dot) != cur_line) {
478 cur_line = begin_line(dot);
479 text_yank(begin_line(dot), end_line(dot), Ureg);
482 #if ENABLE_FEATURE_VI_DOT_CMD
483 // These are commands that change text[].
484 // Remember the input for the "." command
485 if (!adding2q && ioq_start == 0
486 && strchr(modifying_cmds, c)
491 do_cmd(c); // execute the user command
493 // poll to see if there is input already waiting. if we are
494 // not able to display output fast enough to keep up, skip
495 // the display update until we catch up with input.
496 if (mysleep(0) == 0) {
497 // no input pending- so update output
501 #if ENABLE_FEATURE_VI_CRASHME
503 crash_test(); // test editor variables
506 //-------------------------------------------------------------------
508 place_cursor(rows, 0, FALSE); // go to bottom of screen
509 clear_to_eol(); // Erase to end of line
513 //----- The Colon commands -------------------------------------
514 #if ENABLE_FEATURE_VI_COLON
515 static char *get_one_address(char * p, int *addr) // get colon addr, if present
520 #if ENABLE_FEATURE_VI_YANKMARK
523 #if ENABLE_FEATURE_VI_SEARCH
524 char *pat, buf[BUFSIZ];
527 *addr = -1; // assume no addr
528 if (*p == '.') { // the current line
531 *addr = count_lines(text, q);
532 #if ENABLE_FEATURE_VI_YANKMARK
533 } else if (*p == '\'') { // is this a mark addr
537 if (c >= 'a' && c <= 'z') {
540 q = mark[(unsigned char) c];
541 if (q != NULL) { // is mark valid
542 *addr = count_lines(text, q); // count lines
546 #if ENABLE_FEATURE_VI_SEARCH
547 } else if (*p == '/') { // a search pattern
555 pat = xstrdup(buf); // save copy of pattern
558 q = char_search(dot, pat, FORWARD, FULL);
560 *addr = count_lines(text, q);
564 } else if (*p == '$') { // the last line in file
566 q = begin_line(end - 1);
567 *addr = count_lines(text, q);
568 } else if (isdigit(*p)) { // specific line number
569 sscanf(p, "%d%n", addr, &st);
571 } else { // I don't reconise this
572 // unrecognised address- assume -1
578 static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
580 //----- get the address' i.e., 1,3 'a,'b -----
581 // get FIRST addr, if present
583 p++; // skip over leading spaces
584 if (*p == '%') { // alias for 1,$
587 *e = count_lines(text, end-1);
590 p = get_one_address(p, b);
593 if (*p == ',') { // is there a address separator
597 // get SECOND addr, if present
598 p = get_one_address(p, e);
602 p++; // skip over trailing spaces
606 #if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
607 static void setops(const char *args, const char *opname, int flg_no,
608 const char *short_opname, int opt)
610 const char *a = args + flg_no;
611 int l = strlen(opname) - 1; /* opname have + ' ' */
613 if (strncasecmp(a, opname, l) == 0
614 || strncasecmp(a, short_opname, 2) == 0
624 static void colon(char * buf)
626 char c, *orig_buf, *buf1, *q, *r;
627 char *fn, cmd[BUFSIZ], args[BUFSIZ];
628 int i, l, li, ch, b, e;
629 int useforce = FALSE, forced = FALSE;
632 // :3154 // if (-e line 3154) goto it else stay put
633 // :4,33w! foo // write a portion of buffer to file "foo"
634 // :w // write all of buffer to current file
636 // :q! // quit- dont care about modified file
637 // :'a,'z!sort -u // filter block through sort
638 // :'f // goto mark "f"
639 // :'fl // list literal the mark "f" line
640 // :.r bar // read file "bar" into buffer before dot
641 // :/123/,/abc/d // delete lines from "123" line to "abc" line
642 // :/xyz/ // goto the "xyz" line
643 // :s/find/replace/ // substitute pattern "find" with "replace"
644 // :!<cmd> // run <cmd> then return
650 buf++; // move past the ':'
654 q = text; // assume 1,$ for the range
656 li = count_lines(text, end - 1);
657 fn = cfn; // default to current file
658 memset(cmd, '\0', BUFSIZ); // clear cmd[]
659 memset(args, '\0', BUFSIZ); // clear args[]
661 // look for optional address(es) :. :1 :1,9 :'q,'a :%
662 buf = get_address(buf, &b, &e);
664 // remember orig command line
667 // get the COMMAND into cmd[]
669 while (*buf != '\0') {
678 buf1 = last_char_is(cmd, '!');
681 *buf1 = '\0'; // get rid of !
684 // if there is only one addr, then the addr
685 // is the line number of the single line the
686 // user wants. So, reset the end
687 // pointer to point at end of the "b" line
688 q = find_line(b); // what line is #b
693 // we were given two addrs. change the
694 // end pointer to the addr given by user.
695 r = find_line(e); // what line is #e
699 // ------------ now look for the command ------------
701 if (i == 0) { // :123CR goto line #123
703 dot = find_line(b); // what line is #b
707 #if ENABLE_FEATURE_ALLOW_EXEC
708 else if (strncmp(cmd, "!", 1) == 0) { // run a cmd
709 // :!ls run the <cmd>
710 alarm(0); // wait for input- no alarms
711 place_cursor(rows - 1, 0, FALSE); // go to Status line
712 clear_to_eol(); // clear the line
714 system(orig_buf + 1); // run the cmd
716 Hit_Return(); // let user see results
717 alarm(3); // done waiting for input
720 else if (strncmp(cmd, "=", i) == 0) { // where is the address
721 if (b < 0) { // no addr given- use defaults
722 b = e = count_lines(text, dot);
725 } else if (strncasecmp(cmd, "delete", i) == 0) { // delete lines
726 if (b < 0) { // no addr given- use defaults
727 q = begin_line(dot); // assume .,. for the range
730 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
732 } else if (strncasecmp(cmd, "edit", i) == 0) { // Edit a file
735 // don't edit, if the current file has been modified
736 if (file_modified && ! useforce) {
737 psbs("No write since last change (:edit! overrides)");
741 // the user supplied a file name
743 } else if (cfn && cfn[0]) {
744 // no user supplied name- use the current filename
748 // no user file name, no current name- punt
749 psbs("No current filename");
753 // see if file exists- if not, its just a new file request
754 sr = stat(fn, &st_buf);
756 // This is just a request for a new file creation.
757 // The file_insert below will fail but we get
758 // an empty buffer with a file name. Then the "write"
759 // command can do the create.
761 if ((st_buf.st_mode & S_IFREG) == 0) {
762 // This is not a regular file
763 psbs("\"%s\" is not a regular file", fn);
766 if ((st_buf.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)) == 0) {
767 // dont have any read permissions
768 psbs("\"%s\" is not readable", fn);
773 // There is a read-able regular file
774 // make this the current file
775 q = xstrdup(fn); // save the cfn
776 free(cfn); // free the old name
777 cfn = q; // remember new cfn
780 // delete all the contents of text[]
781 new_text(2 * file_size(fn));
782 screenbegin = dot = end = text;
785 ch = file_insert(fn, text, file_size(fn));
788 // start empty buf with dummy line
789 char_insert(text, '\n');
793 last_file_modified = -1;
794 #if ENABLE_FEATURE_VI_YANKMARK
795 if (Ureg >= 0 && Ureg < 28 && reg[Ureg] != 0) {
796 free(reg[Ureg]); // free orig line reg- for 'U'
799 if (YDreg >= 0 && YDreg < 28 && reg[YDreg] != 0) {
800 free(reg[YDreg]); // free default yank/delete register
803 for (li = 0; li < 28; li++) {
807 // how many lines in text[]?
808 li = count_lines(text, end - 1);
810 #if ENABLE_FEATURE_VI_READONLY
814 (sr < 0 ? " [New file]" : ""),
815 #if ENABLE_FEATURE_VI_READONLY
816 ((vi_readonly || readonly) ? " [Read only]" : ""),
819 } else if (strncasecmp(cmd, "file", i) == 0) { // what File is this
820 if (b != -1 || e != -1) {
821 ni("No address allowed on this command");
825 // user wants a new filename
829 // user wants file status info
830 last_status_cksum = 0; // force status update
832 } else if (strncasecmp(cmd, "features", i) == 0) { // what features are available
833 // print out values of all features
834 place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
835 clear_to_eol(); // clear the line
840 } else if (strncasecmp(cmd, "list", i) == 0) { // literal print line
841 if (b < 0) { // no addr given- use defaults
842 q = begin_line(dot); // assume .,. for the range
845 place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
846 clear_to_eol(); // clear the line
848 for (; q <= r; q++) {
852 c_is_no_print = (c & 0x80) && !Isprint(c);
859 } else if (c < ' ' || c == 127) {
870 #if ENABLE_FEATURE_VI_SET
874 } else if (strncasecmp(cmd, "quit", i) == 0 // Quit
875 || strncasecmp(cmd, "next", i) == 0 // edit next file
878 // force end of argv list
885 // don't exit if the file been modified
887 psbs("No write since last change (:%s! overrides)",
888 (*cmd == 'q' ? "quit" : "next"));
891 // are there other file to edit
892 if (*cmd == 'q' && optind < save_argc - 1) {
893 psbs("%d more file to edit", (save_argc - optind - 1));
896 if (*cmd == 'n' && optind >= save_argc - 1) {
897 psbs("No more files to edit");
901 } else if (strncasecmp(cmd, "read", i) == 0) { // read file into text[]
904 psbs("No filename given");
907 if (b < 0) { // no addr given- use defaults
908 q = begin_line(dot); // assume "dot"
910 // read after current line- unless user said ":0r foo"
913 #if ENABLE_FEATURE_VI_READONLY
914 l = readonly; // remember current files' status
916 ch = file_insert(fn, q, file_size(fn));
917 #if ENABLE_FEATURE_VI_READONLY
921 goto vc1; // nothing was inserted
922 // how many lines in text[]?
923 li = count_lines(q, q + ch - 1);
925 #if ENABLE_FEATURE_VI_READONLY
929 #if ENABLE_FEATURE_VI_READONLY
930 ((vi_readonly || readonly) ? " [Read only]" : ""),
934 // if the insert is before "dot" then we need to update
939 } else if (strncasecmp(cmd, "rewind", i) == 0) { // rewind cmd line args
940 if (file_modified && ! useforce) {
941 psbs("No write since last change (:rewind! overrides)");
943 // reset the filenames to edit
944 optind = fn_start - 1;
947 #if ENABLE_FEATURE_VI_SET
948 } else if (strncasecmp(cmd, "set", i) == 0) { // set or clear features
949 #if ENABLE_FEATURE_VI_SETOPTS
952 i = 0; // offset into args
953 // only blank is regarded as args delmiter. What about tab '\t' ?
954 if (!args[0] || strcasecmp(args, "all") == 0) {
955 // print out values of all options
956 place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
957 clear_to_eol(); // clear the line
958 printf("----------------------------------------\r\n");
959 #if ENABLE_FEATURE_VI_SETOPTS
962 printf("autoindent ");
968 printf("ignorecase ");
971 printf("showmatch ");
972 printf("tabstop=%d ", tabstop);
977 #if ENABLE_FEATURE_VI_SETOPTS
980 if (strncasecmp(argp, "no", 2) == 0)
981 i = 2; // ":set noautoindent"
982 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
983 setops(argp, "flash ", i, "fl", VI_ERR_METHOD);
984 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
985 setops(argp, "showmatch ", i, "ic", VI_SHOWMATCH);
987 if (strncasecmp(argp + i, "tabstop=%d ", 7) == 0) {
988 sscanf(strchr(argp + i, '='), "tabstop=%d" + 7, &ch);
989 if (ch > 0 && ch < columns - 1)
992 while (*argp && *argp != ' ')
993 argp++; // skip to arg delimiter (i.e. blank)
994 while (*argp && *argp == ' ')
995 argp++; // skip all delimiting blanks
997 #endif /* FEATURE_VI_SETOPTS */
998 #endif /* FEATURE_VI_SET */
999 #if ENABLE_FEATURE_VI_SEARCH
1000 } else if (strncasecmp(cmd, "s", 1) == 0) { // substitute a pattern with a replacement pattern
1004 // F points to the "find" pattern
1005 // R points to the "replace" pattern
1006 // replace the cmd line delimiters "/" with NULLs
1007 gflag = 0; // global replace flag
1008 c = orig_buf[1]; // what is the delimiter
1009 F = orig_buf + 2; // start of "find"
1010 R = strchr(F, c); // middle delimiter
1011 if (!R) goto colon_s_fail;
1012 *R++ = '\0'; // terminate "find"
1013 buf1 = strchr(R, c);
1014 if (!buf1) goto colon_s_fail;
1015 *buf1++ = '\0'; // terminate "replace"
1016 if (*buf1 == 'g') { // :s/foo/bar/g
1018 gflag++; // turn on gflag
1021 if (b < 0) { // maybe :s/foo/bar/
1022 q = begin_line(dot); // start with cur line
1023 b = count_lines(text, q); // cur line number
1026 e = b; // maybe :.s/foo/bar/
1027 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
1028 ls = q; // orig line start
1030 buf1 = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
1032 // we found the "find" pattern - delete it
1033 text_hole_delete(buf1, buf1 + strlen(F) - 1);
1034 // inset the "replace" patern
1035 string_insert(buf1, R); // insert the string
1036 // check for "global" :s/foo/bar/g
1038 if ((buf1 + strlen(R)) < end_line(ls)) {
1039 q = buf1 + strlen(R);
1040 goto vc4; // don't let q move past cur line
1046 #endif /* FEATURE_VI_SEARCH */
1047 } else if (strncasecmp(cmd, "version", i) == 0) { // show software version
1048 psb("%s", BB_VER " " BB_BT);
1049 } else if (strncasecmp(cmd, "write", i) == 0 // write text to file
1050 || strncasecmp(cmd, "wq", i) == 0
1051 || strncasecmp(cmd, "wn", i) == 0
1052 || strncasecmp(cmd, "x", i) == 0
1054 // is there a file name to write to?
1058 #if ENABLE_FEATURE_VI_READONLY
1059 if ((vi_readonly || readonly) && ! useforce) {
1060 psbs("\"%s\" File is read only", fn);
1064 // how many lines in text[]?
1065 li = count_lines(q, r);
1067 // see if file exists- if not, its just a new file request
1069 // if "fn" is not write-able, chmod u+w
1070 // sprintf(syscmd, "chmod u+w %s", fn);
1074 l = file_write(fn, q, r);
1075 if (useforce && forced) {
1077 // sprintf(syscmd, "chmod u-w %s", fn);
1083 psbs("Write error: %s", strerror(errno));
1085 psb("\"%s\" %dL, %dC", fn, li, l);
1086 if (q == text && r == end - 1 && l == ch) {
1088 last_file_modified = -1;
1090 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n' ||
1091 cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N')
1096 #if ENABLE_FEATURE_VI_READONLY
1099 #if ENABLE_FEATURE_VI_YANKMARK
1100 } else if (strncasecmp(cmd, "yank", i) == 0) { // yank lines
1101 if (b < 0) { // no addr given- use defaults
1102 q = begin_line(dot); // assume .,. for the range
1105 text_yank(q, r, YDreg);
1106 li = count_lines(q, r);
1107 psb("Yank %d lines (%d chars) into [%c]",
1108 li, strlen(reg[YDreg]), what_reg());
1115 dot = bound_dot(dot); // make sure "dot" is valid
1117 #if ENABLE_FEATURE_VI_SEARCH
1119 psb(":s expression missing delimiters");
1123 #endif /* FEATURE_VI_COLON */
1125 static void Hit_Return(void)
1129 standout_start(); // start reverse video
1130 write1("[Hit return to continue]");
1131 standout_end(); // end reverse video
1132 while ((c = get_one_char()) != '\n' && c != '\r') /*do nothing */
1134 redraw(TRUE); // force redraw all
1137 //----- Synchronize the cursor to Dot --------------------------
1138 static void sync_cursor(char * d, int *row, int *col)
1140 char *beg_cur; // begin and end of "d" line
1141 char *end_scr; // begin and end of screen
1145 beg_cur = begin_line(d); // first char of cur line
1147 end_scr = end_screen(); // last char of screen
1149 if (beg_cur < screenbegin) {
1150 // "d" is before top line on screen
1151 // how many lines do we have to move
1152 cnt = count_lines(beg_cur, screenbegin);
1154 screenbegin = beg_cur;
1155 if (cnt > (rows - 1) / 2) {
1156 // we moved too many lines. put "dot" in middle of screen
1157 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1158 screenbegin = prev_line(screenbegin);
1161 } else if (beg_cur > end_scr) {
1162 // "d" is after bottom line on screen
1163 // how many lines do we have to move
1164 cnt = count_lines(end_scr, beg_cur);
1165 if (cnt > (rows - 1) / 2)
1166 goto sc1; // too many lines
1167 for (ro = 0; ro < cnt - 1; ro++) {
1168 // move screen begin the same amount
1169 screenbegin = next_line(screenbegin);
1170 // now, move the end of screen
1171 end_scr = next_line(end_scr);
1172 end_scr = end_line(end_scr);
1175 // "d" is on screen- find out which row
1177 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1183 // find out what col "d" is on
1185 do { // drive "co" to correct column
1186 if (*tp == '\n' || *tp == '\0')
1190 co += ((tabstop - 1) - (co % tabstop));
1191 } else if (*tp < ' ' || *tp == 127) {
1192 co++; // display as ^X, use 2 columns
1194 } while (tp++ < d && ++co);
1196 // "co" is the column where "dot" is.
1197 // The screen has "columns" columns.
1198 // The currently displayed columns are 0+offset -- columns+ofset
1199 // |-------------------------------------------------------------|
1201 // offset | |------- columns ----------------|
1203 // If "co" is already in this range then we do not have to adjust offset
1204 // but, we do have to subtract the "offset" bias from "co".
1205 // If "co" is outside this range then we have to change "offset".
1206 // If the first char of a line is a tab the cursor will try to stay
1207 // in column 7, but we have to set offset to 0.
1209 if (co < 0 + offset) {
1212 if (co >= columns + offset) {
1213 offset = co - columns + 1;
1215 // if the first char of the line is a tab, and "dot" is sitting on it
1216 // force offset to 0.
1217 if (d == beg_cur && *d == '\t') {
1226 //----- Text Movement Routines ---------------------------------
1227 static char *begin_line(char * p) // return pointer to first char cur line
1229 while (p > text && p[-1] != '\n')
1230 p--; // go to cur line B-o-l
1234 static char *end_line(char * p) // return pointer to NL of cur line line
1236 while (p < end - 1 && *p != '\n')
1237 p++; // go to cur line E-o-l
1241 static inline char *dollar_line(char * p) // return pointer to just before NL line
1243 while (p < end - 1 && *p != '\n')
1244 p++; // go to cur line E-o-l
1245 // Try to stay off of the Newline
1246 if (*p == '\n' && (p - begin_line(p)) > 0)
1251 static char *prev_line(char * p) // return pointer first char prev line
1253 p = begin_line(p); // goto begining of cur line
1254 if (p[-1] == '\n' && p > text)
1255 p--; // step to prev line
1256 p = begin_line(p); // goto begining of prev line
1260 static char *next_line(char * p) // return pointer first char next line
1263 if (*p == '\n' && p < end - 1)
1264 p++; // step to next line
1268 //----- Text Information Routines ------------------------------
1269 static char *end_screen(void)
1274 // find new bottom line
1276 for (cnt = 0; cnt < rows - 2; cnt++)
1282 static int count_lines(char * start, char * stop) // count line from start to stop
1287 if (stop < start) { // start and stop are backwards- reverse them
1293 stop = end_line(stop); // get to end of this line
1294 for (q = start; q <= stop && q <= end - 1; q++) {
1301 static char *find_line(int li) // find begining of line #li
1305 for (q = text; li > 1; li--) {
1311 //----- Dot Movement Routines ----------------------------------
1312 static void dot_left(void)
1314 if (dot > text && dot[-1] != '\n')
1318 static void dot_right(void)
1320 if (dot < end - 1 && *dot != '\n')
1324 static void dot_begin(void)
1326 dot = begin_line(dot); // return pointer to first char cur line
1329 static void dot_end(void)
1331 dot = end_line(dot); // return pointer to last char cur line
1334 static char *move_to_col(char * p, int l)
1341 if (*p == '\n' || *p == '\0')
1345 co += ((tabstop - 1) - (co % tabstop));
1346 } else if (*p < ' ' || *p == 127) {
1347 co++; // display as ^X, use 2 columns
1349 } while (++co <= l && p++ < end);
1353 static void dot_next(void)
1355 dot = next_line(dot);
1358 static void dot_prev(void)
1360 dot = prev_line(dot);
1363 static void dot_scroll(int cnt, int dir)
1367 for (; cnt > 0; cnt--) {
1370 // ctrl-Y scroll up one line
1371 screenbegin = prev_line(screenbegin);
1374 // ctrl-E scroll down one line
1375 screenbegin = next_line(screenbegin);
1378 // make sure "dot" stays on the screen so we dont scroll off
1379 if (dot < screenbegin)
1381 q = end_screen(); // find new bottom line
1383 dot = begin_line(q); // is dot is below bottom line?
1387 static void dot_skip_over_ws(void)
1390 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1394 static void dot_delete(void) // delete the char at 'dot'
1396 text_hole_delete(dot, dot);
1399 static char *bound_dot(char * p) // make sure text[0] <= P < "end"
1401 if (p >= end && end > text) {
1403 indicate_error('1');
1407 indicate_error('2');
1412 //----- Helper Utility Routines --------------------------------
1414 //----------------------------------------------------------------
1415 //----- Char Routines --------------------------------------------
1416 /* Chars that are part of a word-
1417 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1418 * Chars that are Not part of a word (stoppers)
1419 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1420 * Chars that are WhiteSpace
1421 * TAB NEWLINE VT FF RETURN SPACE
1422 * DO NOT COUNT NEWLINE AS WHITESPACE
1425 static char *new_screen(int ro, int co)
1430 screensize = ro * co + 8;
1431 screen = xmalloc(screensize);
1432 // initialize the new screen. assume this will be a empty file.
1434 // non-existent text[] lines start with a tilde (~).
1435 for (li = 1; li < ro - 1; li++) {
1436 screen[(li * co) + 0] = '~';
1441 static char *new_text(int size)
1444 size = 10240; // have a minimum size for new files
1446 text = xmalloc(size + 8);
1447 memset(text, '\0', size); // clear new text[]
1448 //text += 4; // leave some room for "oops"
1452 #if ENABLE_FEATURE_VI_SEARCH
1453 static int mycmp(const char * s1, const char * s2, int len)
1457 i = strncmp(s1, s2, len);
1458 #if ENABLE_FEATURE_VI_SETOPTS
1460 i = strncasecmp(s1, s2, len);
1466 // search for pattern starting at p
1467 static char *char_search(char * p, const char * pat, int dir, int range)
1469 #ifndef REGEX_SEARCH
1474 if (dir == FORWARD) {
1475 stop = end - 1; // assume range is p - end-1
1476 if (range == LIMITED)
1477 stop = next_line(p); // range is to next line
1478 for (start = p; start < stop; start++) {
1479 if (mycmp(start, pat, len) == 0) {
1483 } else if (dir == BACK) {
1484 stop = text; // assume range is text - p
1485 if (range == LIMITED)
1486 stop = prev_line(p); // range is to prev line
1487 for (start = p - len; start >= stop; start--) {
1488 if (mycmp(start, pat, len) == 0) {
1493 // pattern not found
1495 #else /* REGEX_SEARCH */
1497 struct re_pattern_buffer preg;
1501 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1507 // assume a LIMITED forward search
1515 // count the number of chars to search over, forward or backward
1519 // RANGE could be negative if we are searching backwards
1522 q = re_compile_pattern(pat, strlen(pat), &preg);
1524 // The pattern was not compiled
1525 psbs("bad search pattern: \"%s\": %s", pat, q);
1526 i = 0; // return p if pattern not compiled
1536 // search for the compiled pattern, preg, in p[]
1537 // range < 0- search backward
1538 // range > 0- search forward
1540 // re_search() < 0 not found or error
1541 // re_search() > 0 index of found pattern
1542 // struct pattern char int int int struct reg
1543 // re_search (*pattern_buffer, *string, size, start, range, *regs)
1544 i = re_search(&preg, q, size, 0, range, 0);
1547 i = 0; // return NULL if pattern not found
1550 if (dir == FORWARD) {
1556 #endif /* REGEX_SEARCH */
1558 #endif /* FEATURE_VI_SEARCH */
1560 static char *char_insert(char * p, char c) // insert the char c at 'p'
1562 if (c == 22) { // Is this an ctrl-V?
1563 p = stupid_insert(p, '^'); // use ^ to indicate literal next
1564 p--; // backup onto ^
1565 refresh(FALSE); // show the ^
1569 file_modified++; // has the file been modified
1570 } else if (c == 27) { // Is this an ESC?
1573 end_cmd_q(); // stop adding to q
1574 last_status_cksum = 0; // force status update
1575 if ((p[-1] != '\n') && (dot > text)) {
1578 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
1580 if ((p[-1] != '\n') && (dot>text)) {
1582 p = text_hole_delete(p, p); // shrink buffer 1 char
1585 // insert a char into text[]
1586 char *sp; // "save p"
1589 c = '\n'; // translate \r to \n
1590 sp = p; // remember addr of insert
1591 p = stupid_insert(p, c); // insert the char
1592 #if ENABLE_FEATURE_VI_SETOPTS
1593 if (showmatch && strchr(")]}", *sp) != NULL) {
1596 if (autoindent && c == '\n') { // auto indent the new line
1599 q = prev_line(p); // use prev line as templet
1600 for (; isblnk(*q); q++) {
1601 p = stupid_insert(p, *q); // insert the char
1609 static char *stupid_insert(char * p, char c) // stupidly insert the char c at 'p'
1611 p = text_hole_make(p, 1);
1614 file_modified++; // has the file been modified
1620 static char find_range(char ** start, char ** stop, char c)
1622 char *save_dot, *p, *q;
1628 if (strchr("cdy><", c)) {
1629 // these cmds operate on whole lines
1630 p = q = begin_line(p);
1631 for (cnt = 1; cnt < cmdcnt; cnt++) {
1635 } else if (strchr("^%$0bBeEft", c)) {
1636 // These cmds operate on char positions
1637 do_cmd(c); // execute movement cmd
1639 } else if (strchr("wW", c)) {
1640 do_cmd(c); // execute movement cmd
1641 // if we are at the next word's first char
1642 // step back one char
1643 // but check the possibilities when it is true
1644 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
1645 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1646 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1647 dot--; // move back off of next word
1648 if (dot > text && *dot == '\n')
1649 dot--; // stay off NL
1651 } else if (strchr("H-k{", c)) {
1652 // these operate on multi-lines backwards
1653 q = end_line(dot); // find NL
1654 do_cmd(c); // execute movement cmd
1657 } else if (strchr("L+j}\r\n", c)) {
1658 // these operate on multi-lines forwards
1659 p = begin_line(dot);
1660 do_cmd(c); // execute movement cmd
1661 dot_end(); // find NL
1664 c = 27; // error- return an ESC char
1677 static int st_test(char * p, int type, int dir, char * tested)
1687 if (type == S_BEFORE_WS) {
1689 test = ((!isspace(c)) || c == '\n');
1691 if (type == S_TO_WS) {
1693 test = ((!isspace(c)) || c == '\n');
1695 if (type == S_OVER_WS) {
1697 test = ((isspace(c)));
1699 if (type == S_END_PUNCT) {
1701 test = ((ispunct(c)));
1703 if (type == S_END_ALNUM) {
1705 test = ((isalnum(c)) || c == '_');
1711 static char *skip_thing(char * p, int linecnt, int dir, int type)
1715 while (st_test(p, type, dir, &c)) {
1716 // make sure we limit search to correct number of lines
1717 if (c == '\n' && --linecnt < 1)
1719 if (dir >= 0 && p >= end - 1)
1721 if (dir < 0 && p <= text)
1723 p += dir; // move to next char
1728 // find matching char of pair () [] {}
1729 static char *find_pair(char * p, char c)
1736 dir = 1; // assume forward
1760 for (q = p + dir; text <= q && q < end; q += dir) {
1761 // look for match, count levels of pairs (( ))
1763 level++; // increase pair levels
1765 level--; // reduce pair level
1767 break; // found matching pair
1770 q = NULL; // indicate no match
1774 #if ENABLE_FEATURE_VI_SETOPTS
1775 // show the matching char of a pair, () [] {}
1776 static void showmatching(char * p)
1780 // we found half of a pair
1781 q = find_pair(p, *p); // get loc of matching char
1783 indicate_error('3'); // no matching char
1785 // "q" now points to matching pair
1786 save_dot = dot; // remember where we are
1787 dot = q; // go to new loc
1788 refresh(FALSE); // let the user see it
1789 mysleep(40); // give user some time
1790 dot = save_dot; // go back to old loc
1794 #endif /* FEATURE_VI_SETOPTS */
1796 // open a hole in text[]
1797 static char *text_hole_make(char * p, int size) // at "p", make a 'size' byte hole
1806 cnt = end - src; // the rest of buffer
1807 if (memmove(dest, src, cnt) != dest) {
1808 psbs("can't create room for new characters");
1810 memset(p, ' ', size); // clear new hole
1811 end = end + size; // adjust the new END
1812 file_modified++; // has the file been modified
1817 // close a hole in text[]
1818 static char *text_hole_delete(char * p, char * q) // delete "p" thru "q", inclusive
1823 // move forwards, from beginning
1827 if (q < p) { // they are backward- swap them
1831 hole_size = q - p + 1;
1833 if (src < text || src > end)
1835 if (dest < text || dest >= end)
1838 goto thd_atend; // just delete the end of the buffer
1839 if (memmove(dest, src, cnt) != dest) {
1840 psbs("can't delete the character");
1843 end = end - hole_size; // adjust the new END
1845 dest = end - 1; // make sure dest in below end-1
1847 dest = end = text; // keep pointers valid
1848 file_modified++; // has the file been modified
1853 // copy text into register, then delete text.
1854 // if dist <= 0, do not include, or go past, a NewLine
1856 static char *yank_delete(char * start, char * stop, int dist, int yf)
1860 // make sure start <= stop
1862 // they are backwards, reverse them
1868 // we cannot cross NL boundaries
1872 // dont go past a NewLine
1873 for (; p + 1 <= stop; p++) {
1875 stop = p; // "stop" just before NewLine
1881 #if ENABLE_FEATURE_VI_YANKMARK
1882 text_yank(start, stop, YDreg);
1884 if (yf == YANKDEL) {
1885 p = text_hole_delete(start, stop);
1890 static void show_help(void)
1892 puts("These features are available:"
1893 #if ENABLE_FEATURE_VI_SEARCH
1894 "\n\tPattern searches with / and ?"
1896 #if ENABLE_FEATURE_VI_DOT_CMD
1897 "\n\tLast command repeat with \'.\'"
1899 #if ENABLE_FEATURE_VI_YANKMARK
1900 "\n\tLine marking with 'x"
1901 "\n\tNamed buffers with \"x"
1903 #if ENABLE_FEATURE_VI_READONLY
1904 "\n\tReadonly if vi is called as \"view\""
1905 "\n\tReadonly with -R command line arg"
1907 #if ENABLE_FEATURE_VI_SET
1908 "\n\tSome colon mode commands with \':\'"
1910 #if ENABLE_FEATURE_VI_SETOPTS
1911 "\n\tSettable options with \":set\""
1913 #if ENABLE_FEATURE_VI_USE_SIGNALS
1914 "\n\tSignal catching- ^C"
1915 "\n\tJob suspend and resume with ^Z"
1917 #if ENABLE_FEATURE_VI_WIN_RESIZE
1918 "\n\tAdapt to window re-sizes"
1923 static inline void print_literal(char * buf, const char * s) // copy s to buf, convert unprintable
1936 c_is_no_print = (c & 0x80) && !Isprint(c);
1937 if (c_is_no_print) {
1941 if (c < ' ' || c == 127) {
1957 #if ENABLE_FEATURE_VI_DOT_CMD
1958 static void start_new_cmd_q(char c)
1961 free(last_modifying_cmd);
1962 // get buffer for new cmd
1963 last_modifying_cmd = xmalloc(BUFSIZ);
1964 memset(last_modifying_cmd, '\0', BUFSIZ); // clear new cmd queue
1965 // if there is a current cmd count put it in the buffer first
1967 sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
1968 else // just save char c onto queue
1969 last_modifying_cmd[0] = c;
1973 static void end_cmd_q(void)
1975 #if ENABLE_FEATURE_VI_YANKMARK
1976 YDreg = 26; // go back to default Yank/Delete reg
1980 #endif /* FEATURE_VI_DOT_CMD */
1982 #if ENABLE_FEATURE_VI_YANKMARK \
1983 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
1984 || ENABLE_FEATURE_VI_CRASHME
1985 static char *string_insert(char * p, char * s) // insert the string at 'p'
1990 p = text_hole_make(p, i);
1992 for (cnt = 0; *s != '\0'; s++) {
1996 #if ENABLE_FEATURE_VI_YANKMARK
1997 psb("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2003 #if ENABLE_FEATURE_VI_YANKMARK
2004 static char *text_yank(char * p, char * q, int dest) // copy text into a register
2009 if (q < p) { // they are backwards- reverse them
2016 free(t); // if already a yank register, free it
2017 t = xmalloc(cnt + 1); // get a new register
2018 memset(t, '\0', cnt + 1); // clear new text[]
2019 strncpy(t, p, cnt); // copy text[] into bufer
2024 static char what_reg(void)
2028 c = 'D'; // default to D-reg
2029 if (0 <= YDreg && YDreg <= 25)
2030 c = 'a' + (char) YDreg;
2038 static void check_context(char cmd)
2040 // A context is defined to be "modifying text"
2041 // Any modifying command establishes a new context.
2043 if (dot < context_start || dot > context_end) {
2044 if (strchr(modifying_cmds, cmd) != NULL) {
2045 // we are trying to modify text[]- make this the current context
2046 mark[27] = mark[26]; // move cur to prev
2047 mark[26] = dot; // move local to cur
2048 context_start = prev_line(prev_line(dot));
2049 context_end = next_line(next_line(dot));
2050 //loiter= start_loiter= now;
2055 static inline char *swap_context(char * p) // goto new context for '' command make this the current context
2059 // the current context is in mark[26]
2060 // the previous context is in mark[27]
2061 // only swap context if other context is valid
2062 if (text <= mark[27] && mark[27] <= end - 1) {
2064 mark[27] = mark[26];
2066 p = mark[26]; // where we are going- previous context
2067 context_start = prev_line(prev_line(prev_line(p)));
2068 context_end = next_line(next_line(next_line(p)));
2072 #endif /* FEATURE_VI_YANKMARK */
2074 static int isblnk(char c) // is the char a blank or tab
2076 return (c == ' ' || c == '\t');
2079 //----- Set terminal attributes --------------------------------
2080 static void rawmode(void)
2082 tcgetattr(0, &term_orig);
2083 term_vi = term_orig;
2084 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG ON- allow intr's
2085 term_vi.c_iflag &= (~IXON & ~ICRNL);
2086 term_vi.c_oflag &= (~ONLCR);
2087 term_vi.c_cc[VMIN] = 1;
2088 term_vi.c_cc[VTIME] = 0;
2089 erase_char = term_vi.c_cc[VERASE];
2090 tcsetattr(0, TCSANOW, &term_vi);
2093 static void cookmode(void)
2096 tcsetattr(0, TCSANOW, &term_orig);
2099 //----- Come here when we get a window resize signal ---------
2100 #if ENABLE_FEATURE_VI_USE_SIGNALS
2101 static void winch_sig(int sig ATTRIBUTE_UNUSED)
2103 signal(SIGWINCH, winch_sig);
2104 if (ENABLE_FEATURE_VI_WIN_RESIZE)
2105 get_terminal_width_height(0, &columns, &rows);
2106 new_screen(rows, columns); // get memory for virtual screen
2107 redraw(TRUE); // re-draw the screen
2110 //----- Come here when we get a continue signal -------------------
2111 static void cont_sig(int sig ATTRIBUTE_UNUSED)
2113 rawmode(); // terminal to "raw"
2114 last_status_cksum = 0; // force status update
2115 redraw(TRUE); // re-draw the screen
2117 signal(SIGTSTP, suspend_sig);
2118 signal(SIGCONT, SIG_DFL);
2119 kill(my_pid, SIGCONT);
2122 //----- Come here when we get a Suspend signal -------------------
2123 static void suspend_sig(int sig ATTRIBUTE_UNUSED)
2125 place_cursor(rows - 1, 0, FALSE); // go to bottom of screen
2126 clear_to_eol(); // Erase to end of line
2127 cookmode(); // terminal to "cooked"
2129 signal(SIGCONT, cont_sig);
2130 signal(SIGTSTP, SIG_DFL);
2131 kill(my_pid, SIGTSTP);
2134 //----- Come here when we get a signal ---------------------------
2135 static void catch_sig(int sig)
2137 signal(SIGINT, catch_sig);
2139 longjmp(restart, sig);
2141 #endif /* FEATURE_VI_USE_SIGNALS */
2143 static int mysleep(int hund) // sleep for 'h' 1/100 seconds
2145 // Don't hang- Wait 5/100 seconds- 1 Sec= 1000000
2150 tv.tv_usec = hund * 10000;
2151 select(1, &rfds, NULL, NULL, &tv);
2152 return FD_ISSET(0, &rfds);
2155 #define readbuffer bb_common_bufsiz1
2157 static int readed_for_parse;
2159 //----- IO Routines --------------------------------------------
2160 static char readit(void) // read (maybe cursor) key from stdin
2169 static const struct esc_cmds esccmds[] = {
2170 {"OA", VI_K_UP}, // cursor key Up
2171 {"OB", VI_K_DOWN}, // cursor key Down
2172 {"OC", VI_K_RIGHT}, // Cursor Key Right
2173 {"OD", VI_K_LEFT}, // cursor key Left
2174 {"OH", VI_K_HOME}, // Cursor Key Home
2175 {"OF", VI_K_END}, // Cursor Key End
2176 {"[A", VI_K_UP}, // cursor key Up
2177 {"[B", VI_K_DOWN}, // cursor key Down
2178 {"[C", VI_K_RIGHT}, // Cursor Key Right
2179 {"[D", VI_K_LEFT}, // cursor key Left
2180 {"[H", VI_K_HOME}, // Cursor Key Home
2181 {"[F", VI_K_END}, // Cursor Key End
2182 {"[1~", VI_K_HOME}, // Cursor Key Home
2183 {"[2~", VI_K_INSERT}, // Cursor Key Insert
2184 {"[4~", VI_K_END}, // Cursor Key End
2185 {"[5~", VI_K_PAGEUP}, // Cursor Key Page Up
2186 {"[6~", VI_K_PAGEDOWN},// Cursor Key Page Down
2187 {"OP", VI_K_FUN1}, // Function Key F1
2188 {"OQ", VI_K_FUN2}, // Function Key F2
2189 {"OR", VI_K_FUN3}, // Function Key F3
2190 {"OS", VI_K_FUN4}, // Function Key F4
2191 {"[15~", VI_K_FUN5}, // Function Key F5
2192 {"[17~", VI_K_FUN6}, // Function Key F6
2193 {"[18~", VI_K_FUN7}, // Function Key F7
2194 {"[19~", VI_K_FUN8}, // Function Key F8
2195 {"[20~", VI_K_FUN9}, // Function Key F9
2196 {"[21~", VI_K_FUN10}, // Function Key F10
2197 {"[23~", VI_K_FUN11}, // Function Key F11
2198 {"[24~", VI_K_FUN12}, // Function Key F12
2199 {"[11~", VI_K_FUN1}, // Function Key F1
2200 {"[12~", VI_K_FUN2}, // Function Key F2
2201 {"[13~", VI_K_FUN3}, // Function Key F3
2202 {"[14~", VI_K_FUN4}, // Function Key F4
2205 #define ESCCMDS_COUNT (sizeof(esccmds)/sizeof(struct esc_cmds))
2207 alarm(0); // turn alarm OFF while we wait for input
2209 n = readed_for_parse;
2210 // get input from User- are there already input chars in Q?
2213 // the Q is empty, wait for a typed char
2214 n = read(0, readbuffer, BUFSIZ - 1);
2217 goto ri0; // interrupted sys call
2220 if (errno == EFAULT)
2222 if (errno == EINVAL)
2230 if (readbuffer[0] == 27) {
2231 // This is an ESC char. Is this Esc sequence?
2232 // Could be bare Esc key. See if there are any
2233 // more chars to read after the ESC. This would
2234 // be a Function or Cursor Key sequence.
2238 tv.tv_usec = 50000; // Wait 5/100 seconds- 1 Sec=1000000
2240 // keep reading while there are input chars and room in buffer
2241 while (select(1, &rfds, NULL, NULL, &tv) > 0 && n <= (BUFSIZ - 5)) {
2242 // read the rest of the ESC string
2243 int r = read(0, (void *) (readbuffer + n), BUFSIZ - n);
2249 readed_for_parse = n;
2252 if (c == 27 && n > 1) {
2253 // Maybe cursor or function key?
2254 const struct esc_cmds *eindex;
2256 for (eindex = esccmds; eindex < &esccmds[ESCCMDS_COUNT]; eindex++) {
2257 int cnt = strlen(eindex->seq);
2261 if (strncmp(eindex->seq, readbuffer + 1, cnt))
2263 // is a Cursor key- put derived value back into Q
2265 // for squeeze out the ESC sequence
2269 if (eindex == &esccmds[ESCCMDS_COUNT]) {
2270 /* defined ESC sequence not found, set only one ESC */
2276 // remove key sequence from Q
2277 readed_for_parse -= n;
2278 memmove(readbuffer, readbuffer + n, BUFSIZ - n);
2279 alarm(3); // we are done waiting for input, turn alarm ON
2283 //----- IO Routines --------------------------------------------
2284 static char get_one_char(void)
2288 #if ENABLE_FEATURE_VI_DOT_CMD
2289 // ! adding2q && ioq == 0 read()
2290 // ! adding2q && ioq != 0 *ioq
2291 // adding2q *last_modifying_cmd= read()
2293 // we are not adding to the q.
2294 // but, we may be reading from a q
2296 // there is no current q, read from STDIN
2297 c = readit(); // get the users input
2299 // there is a queue to get chars from first
2302 // the end of the q, read from STDIN
2304 ioq_start = ioq = 0;
2305 c = readit(); // get the users input
2309 // adding STDIN chars to q
2310 c = readit(); // get the users input
2311 if (last_modifying_cmd != 0) {
2312 int len = strlen(last_modifying_cmd);
2313 if (len + 1 >= BUFSIZ) {
2314 psbs("last_modifying_cmd overrun");
2316 // add new char to q
2317 last_modifying_cmd[len] = c;
2322 c = readit(); // get the users input
2323 #endif /* FEATURE_VI_DOT_CMD */
2324 return c; // return the char, where ever it came from
2327 static char *get_input_line(const char * prompt) // get input line- use "status line"
2335 strcpy(buf, prompt);
2336 last_status_cksum = 0; // force status update
2337 place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
2338 clear_to_eol(); // clear the line
2339 write1(prompt); // write out the :, /, or ? prompt
2342 while (i < BUFSIZ) {
2343 c = get_one_char(); // read user input
2344 if (c == '\n' || c == '\r' || c == 27)
2345 break; // is this end of input
2346 if (c == erase_char || c == 8 || c == 127) {
2347 // user wants to erase prev char
2348 i--; // backup to prev char
2349 buf[i] = '\0'; // erase the char
2350 buf[i + 1] = '\0'; // null terminate buffer
2351 write1("\b \b"); // erase char on screen
2352 if (i <= 0) { // user backs up before b-o-l, exit
2356 buf[i] = c; // save char in buffer
2357 buf[i + 1] = '\0'; // make sure buffer is null terminated
2358 putchar(c); // echo the char back to user
2364 obufp = xstrdup(buf);
2368 static int file_size(const char * fn) // what is the byte size of "fn"
2376 sr = stat(fn, &st_buf); // see if file exists
2378 cnt = (int) st_buf.st_size;
2383 static int file_insert(char * fn, char * p, int size)
2388 #if ENABLE_FEATURE_VI_READONLY
2391 if (!fn || !fn[0]) {
2392 psbs("No filename given");
2396 // OK- this is just a no-op
2401 psbs("Trying to insert a negative number (%d) of characters", size);
2404 if (p < text || p > end) {
2405 psbs("Trying to insert file outside of memory");
2409 // see if we can open the file
2410 #if ENABLE_FEATURE_VI_READONLY
2411 if (vi_readonly) goto fi1; // do not try write-mode
2413 fd = open(fn, O_RDWR); // assume read & write
2415 // could not open for writing- maybe file is read only
2416 #if ENABLE_FEATURE_VI_READONLY
2419 fd = open(fn, O_RDONLY); // try read-only
2421 psbs("\"%s\" %s", fn, "cannot open file");
2424 #if ENABLE_FEATURE_VI_READONLY
2425 // got the file- read-only
2429 p = text_hole_make(p, size);
2430 cnt = read(fd, p, size);
2434 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
2435 psbs("cannot read file \"%s\"", fn);
2436 } else if (cnt < size) {
2437 // There was a partial read, shrink unused space text[]
2438 p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
2439 psbs("cannot read all of file \"%s\"", fn);
2447 static int file_write(char * fn, char * first, char * last)
2449 int fd, cnt, charcnt;
2452 psbs("No current filename");
2456 // FIXIT- use the correct umask()
2457 fd = open(fn, (O_WRONLY | O_CREAT | O_TRUNC), 0664);
2460 cnt = last - first + 1;
2461 charcnt = write(fd, first, cnt);
2462 if (charcnt == cnt) {
2464 //file_modified = FALSE; // the file has not been modified
2472 //----- Terminal Drawing ---------------------------------------
2473 // The terminal is made up of 'rows' line of 'columns' columns.
2474 // classically this would be 24 x 80.
2475 // screen coordinates
2481 // 23,0 ... 23,79 status line
2484 //----- Move the cursor to row x col (count from 0, not 1) -------
2485 static void place_cursor(int row, int col, int opti)
2489 #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2492 // char cm3[BUFSIZ];
2493 int Rrow = last_row;
2496 memset(cm1, '\0', BUFSIZ - 1); // clear the buffer
2498 if (row < 0) row = 0;
2499 if (row >= rows) row = rows - 1;
2500 if (col < 0) col = 0;
2501 if (col >= columns) col = columns - 1;
2503 //----- 1. Try the standard terminal ESC sequence
2504 sprintf(cm1, CMrc, row + 1, col + 1);
2509 #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2510 //----- find the minimum # of chars to move cursor -------------
2511 //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
2512 memset(cm2, '\0', BUFSIZ - 1); // clear the buffer
2514 // move to the correct row
2515 while (row < Rrow) {
2516 // the cursor has to move up
2520 while (row > Rrow) {
2521 // the cursor has to move down
2522 strcat(cm2, CMdown);
2526 // now move to the correct column
2527 strcat(cm2, "\r"); // start at col 0
2528 // just send out orignal source char to get to correct place
2529 screenp = &screen[row * columns]; // start of screen line
2530 strncat(cm2, screenp, col);
2532 //----- 3. Try some other way of moving cursor
2533 //---------------------------------------------
2535 // pick the shortest cursor motion to send out
2537 if (strlen(cm2) < strlen(cm)) {
2539 } /* else if (strlen(cm3) < strlen(cm)) {
2542 #endif /* FEATURE_VI_OPTIMIZE_CURSOR */
2544 write1(cm); // move the cursor
2547 //----- Erase from cursor to end of line -----------------------
2548 static void clear_to_eol(void)
2550 write1(Ceol); // Erase from cursor to end of line
2553 //----- Erase from cursor to end of screen -----------------------
2554 static void clear_to_eos(void)
2556 write1(Ceos); // Erase from cursor to end of screen
2559 //----- Start standout mode ------------------------------------
2560 static void standout_start(void) // send "start reverse video" sequence
2562 write1(SOs); // Start reverse video mode
2565 //----- End standout mode --------------------------------------
2566 static void standout_end(void) // send "end reverse video" sequence
2568 write1(SOn); // End reverse video mode
2571 //----- Flash the screen --------------------------------------
2572 static void flash(int h)
2574 standout_start(); // send "start reverse video" sequence
2577 standout_end(); // send "end reverse video" sequence
2581 static void Indicate_Error(void)
2583 #if ENABLE_FEATURE_VI_CRASHME
2585 return; // generate a random command
2588 write1(bell); // send out a bell character
2594 //----- Screen[] Routines --------------------------------------
2595 //----- Erase the Screen[] memory ------------------------------
2596 static void screen_erase(void)
2598 memset(screen, ' ', screensize); // clear new screen
2601 static int bufsum(char *buf, int count)
2604 char *e = buf + count;
2607 sum += (unsigned char) *buf++;
2611 //----- Draw the status line at bottom of the screen -------------
2612 static void show_status_line(void)
2614 int cnt = 0, cksum = 0;
2616 // either we already have an error or status message, or we
2618 if (!have_status_msg) {
2619 cnt = format_edit_status();
2620 cksum = bufsum(status_buffer, cnt);
2622 if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
2623 last_status_cksum= cksum; // remember if we have seen this line
2624 place_cursor(rows - 1, 0, FALSE); // put cursor on status line
2625 write1(status_buffer);
2627 if (have_status_msg) {
2628 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
2630 have_status_msg = 0;
2633 have_status_msg = 0;
2635 place_cursor(crow, ccol, FALSE); // put cursor back in correct place
2640 //----- format the status buffer, the bottom line of screen ------
2641 // format status buffer, with STANDOUT mode
2642 static void psbs(const char *format, ...)
2646 va_start(args, format);
2647 strcpy(status_buffer, SOs); // Terminal standout mode on
2648 vsprintf(status_buffer + strlen(status_buffer), format, args);
2649 strcat(status_buffer, SOn); // Terminal standout mode off
2652 have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
2655 // format status buffer
2656 static void psb(const char *format, ...)
2660 va_start(args, format);
2661 vsprintf(status_buffer, format, args);
2664 have_status_msg = 1;
2667 static void ni(const char * s) // display messages
2671 print_literal(buf, s);
2672 psbs("\'%s\' is not implemented", buf);
2675 static int format_edit_status(void) // show file status on status line
2679 int cur, percent, ret, trunc_at;
2681 // file_modified is now a counter rather than a flag. this
2682 // helps reduce the amount of line counting we need to do.
2683 // (this will cause a mis-reporting of modified status
2684 // once every MAXINT editing operations.)
2686 // it would be nice to do a similar optimization here -- if
2687 // we haven't done a motion that could have changed which line
2688 // we're on, then we shouldn't have to do this count_lines()
2689 cur = count_lines(text, dot);
2691 // reduce counting -- the total lines can't have
2692 // changed if we haven't done any edits.
2693 if (file_modified != last_file_modified) {
2694 tot = cur + count_lines(dot, end - 1) - 1;
2695 last_file_modified = file_modified;
2698 // current line percent
2699 // ------------- ~~ ----------
2702 percent = (100 * cur) / tot;
2708 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2709 columns : STATUS_BUFFER_LEN-1;
2711 ret = snprintf(status_buffer, trunc_at+1,
2712 #if ENABLE_FEATURE_VI_READONLY
2713 "%c %s%s%s %d/%d %d%%",
2715 "%c %s%s %d/%d %d%%",
2717 (cmd_mode ? (cmd_mode == 2 ? 'R':'I'):'-'),
2718 (cfn != 0 ? cfn : "No file"),
2719 #if ENABLE_FEATURE_VI_READONLY
2720 ((vi_readonly || readonly) ? " [Read-only]" : ""),
2722 (file_modified ? " [modified]" : ""),
2725 if (ret >= 0 && ret < trunc_at)
2726 return ret; /* it all fit */
2728 return trunc_at; /* had to truncate */
2731 //----- Force refresh of all Lines -----------------------------
2732 static void redraw(int full_screen)
2734 place_cursor(0, 0, FALSE); // put cursor in correct place
2735 clear_to_eos(); // tel terminal to erase display
2736 screen_erase(); // erase the internal screen buffer
2737 last_status_cksum = 0; // force status update
2738 refresh(full_screen); // this will redraw the entire display
2742 //----- Format a text[] line into a buffer ---------------------
2743 static void format_line(char *dest, char *src, int li)
2748 for (co = 0; co < MAX_SCR_COLS; co++) {
2749 c = ' '; // assume blank
2750 if (li > 0 && co == 0) {
2751 c = '~'; // not first line, assume Tilde
2753 // are there chars in text[] and have we gone past the end
2754 if (text < end && src < end) {
2759 if ((c & 0x80) && !Isprint(c)) {
2762 if ((unsigned char)(c) < ' ' || c == 0x7f) {
2766 for (; (co % tabstop) != (tabstop - 1); co++) {
2774 c += '@'; // make it visible
2777 // the co++ is done here so that the column will
2778 // not be overwritten when we blank-out the rest of line
2785 //----- Refresh the changed screen lines -----------------------
2786 // Copy the source line from text[] into the buffer and note
2787 // if the current screenline is different from the new buffer.
2788 // If they differ then that line needs redrawing on the terminal.
2790 static void refresh(int full_screen)
2792 static int old_offset;
2795 char buf[MAX_SCR_COLS];
2796 char *tp, *sp; // pointer into text[] and screen[]
2797 #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2798 int last_li = -2; // last line that changed- for optimizing cursor movement
2801 if (ENABLE_FEATURE_VI_WIN_RESIZE)
2802 get_terminal_width_height(0, &columns, &rows);
2803 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2804 tp = screenbegin; // index into text[] of top line
2806 // compare text[] to screen[] and mark screen[] lines that need updating
2807 for (li = 0; li < rows - 1; li++) {
2808 int cs, ce; // column start & end
2809 memset(buf, ' ', MAX_SCR_COLS); // blank-out the buffer
2810 buf[MAX_SCR_COLS-1] = 0; // NULL terminate the buffer
2811 // format current text line into buf
2812 format_line(buf, tp, li);
2814 // skip to the end of the current text[] line
2815 while (tp < end && *tp++ != '\n') /*no-op*/ ;
2817 // see if there are any changes between vitual screen and buf
2818 changed = FALSE; // assume no change
2821 sp = &screen[li * columns]; // start of screen line
2823 // force re-draw of every single column from 0 - columns-1
2826 // compare newly formatted buffer with virtual screen
2827 // look forward for first difference between buf and screen
2828 for (; cs <= ce; cs++) {
2829 if (buf[cs + offset] != sp[cs]) {
2830 changed = TRUE; // mark for redraw
2835 // look backward for last difference between buf and screen
2836 for ( ; ce >= cs; ce--) {
2837 if (buf[ce + offset] != sp[ce]) {
2838 changed = TRUE; // mark for redraw
2842 // now, cs is index of first diff, and ce is index of last diff
2844 // if horz offset has changed, force a redraw
2845 if (offset != old_offset) {
2850 // make a sanity check of columns indexes
2852 if (ce > columns-1) ce= columns-1;
2853 if (cs > ce) { cs= 0; ce= columns-1; }
2854 // is there a change between vitual screen and buf
2856 // copy changed part of buffer to virtual screen
2857 memmove(sp+cs, buf+(cs+offset), ce-cs+1);
2859 // move cursor to column of first change
2860 if (offset != old_offset) {
2861 // opti_cur_move is still too stupid
2862 // to handle offsets correctly
2863 place_cursor(li, cs, FALSE);
2865 #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2866 // if this just the next line
2867 // try to optimize cursor movement
2868 // otherwise, use standard ESC sequence
2869 place_cursor(li, cs, li == (last_li+1) ? TRUE : FALSE);
2872 place_cursor(li, cs, FALSE); // use standard ESC sequence
2873 #endif /* FEATURE_VI_OPTIMIZE_CURSOR */
2876 // write line out to terminal
2878 int nic = ce - cs + 1;
2879 char *out = sp + cs;
2886 #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2892 #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2893 place_cursor(crow, ccol, (crow == last_row) ? TRUE : FALSE);
2896 place_cursor(crow, ccol, FALSE);
2899 if (offset != old_offset)
2900 old_offset = offset;
2903 //---------------------------------------------------------------------
2904 //----- the Ascii Chart -----------------------------------------------
2906 // 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
2907 // 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
2908 // 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
2909 // 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
2910 // 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
2911 // 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
2912 // 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
2913 // 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
2914 // 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
2915 // 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
2916 // 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
2917 // 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
2918 // 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
2919 // 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
2920 // 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
2921 // 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
2922 //---------------------------------------------------------------------
2924 //----- Execute a Vi Command -----------------------------------
2925 static void do_cmd(char c)
2928 char c1, *p, *q, buf[9], *save_dot;
2929 int cnt, i, j, dir, yf;
2931 c1 = c; // quiet the compiler
2932 cnt = yf = dir = 0; // quiet the compiler
2933 msg = p = q = save_dot = buf; // quiet the compiler
2934 memset(buf, '\0', 9); // clear buf
2938 /* if this is a cursor key, skip these checks */
2951 if (cmd_mode == 2) {
2952 // flip-flop Insert/Replace mode
2953 if (c == VI_K_INSERT)
2955 // we are 'R'eplacing the current *dot with new char
2957 // don't Replace past E-o-l
2958 cmd_mode = 1; // convert to insert
2960 if (1 <= c || Isprint(c)) {
2962 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
2963 dot = char_insert(dot, c); // insert new char
2968 if (cmd_mode == 1) {
2969 // hitting "Insert" twice means "R" replace mode
2970 if (c == VI_K_INSERT) goto dc5;
2971 // insert the char c at "dot"
2972 if (1 <= c || Isprint(c)) {
2973 dot = char_insert(dot, c);
2988 #if ENABLE_FEATURE_VI_CRASHME
2989 case 0x14: // dc4 ctrl-T
2990 crashme = (crashme == 0) ? 1 : 0;
3021 //case 'u': // u- FIXME- there is no undo
3023 default: // unrecognised command
3032 end_cmd_q(); // stop adding to q
3033 case 0x00: // nul- ignore
3035 case 2: // ctrl-B scroll up full screen
3036 case VI_K_PAGEUP: // Cursor Key Page Up
3037 dot_scroll(rows - 2, -1);
3039 #if ENABLE_FEATURE_VI_USE_SIGNALS
3040 case 0x03: // ctrl-C interrupt
3041 longjmp(restart, 1);
3043 case 26: // ctrl-Z suspend
3044 suspend_sig(SIGTSTP);
3047 case 4: // ctrl-D scroll down half screen
3048 dot_scroll((rows - 2) / 2, 1);
3050 case 5: // ctrl-E scroll down one line
3053 case 6: // ctrl-F scroll down full screen
3054 case VI_K_PAGEDOWN: // Cursor Key Page Down
3055 dot_scroll(rows - 2, 1);
3057 case 7: // ctrl-G show current status
3058 last_status_cksum = 0; // force status update
3060 case 'h': // h- move left
3061 case VI_K_LEFT: // cursor key Left
3062 case 8: // ctrl-H- move left (This may be ERASE char)
3063 case 0x7f: // DEL- move left (This may be ERASE char)
3069 case 10: // Newline ^J
3070 case 'j': // j- goto next line, same col
3071 case VI_K_DOWN: // cursor key Down
3075 dot_next(); // go to next B-o-l
3076 dot = move_to_col(dot, ccol + offset); // try stay in same col
3078 case 12: // ctrl-L force redraw whole screen
3079 case 18: // ctrl-R force redraw
3080 place_cursor(0, 0, FALSE); // put cursor in correct place
3081 clear_to_eos(); // tel terminal to erase display
3083 screen_erase(); // erase the internal screen buffer
3084 last_status_cksum = 0; // force status update
3085 refresh(TRUE); // this will redraw the entire display
3087 case 13: // Carriage Return ^M
3088 case '+': // +- goto next line
3095 case 21: // ctrl-U scroll up half screen
3096 dot_scroll((rows - 2) / 2, -1);
3098 case 25: // ctrl-Y scroll up one line
3104 cmd_mode = 0; // stop insrting
3106 last_status_cksum = 0; // force status update
3108 case ' ': // move right
3109 case 'l': // move right
3110 case VI_K_RIGHT: // Cursor Key Right
3116 #if ENABLE_FEATURE_VI_YANKMARK
3117 case '"': // "- name a register to use for Delete/Yank
3118 c1 = get_one_char();
3126 case '\'': // '- goto a specific mark
3127 c1 = get_one_char();
3132 q = mark[(unsigned char) c1];
3133 if (text <= q && q < end) {
3135 dot_begin(); // go to B-o-l
3138 } else if (c1 == '\'') { // goto previous context
3139 dot = swap_context(dot); // swap current and previous context
3140 dot_begin(); // go to B-o-l
3146 case 'm': // m- Mark a line
3147 // this is really stupid. If there are any inserts or deletes
3148 // between text[0] and dot then this mark will not point to the
3149 // correct location! It could be off by many lines!
3150 // Well..., at least its quick and dirty.
3151 c1 = get_one_char();
3155 // remember the line
3156 mark[(int) c1] = dot;
3161 case 'P': // P- Put register before
3162 case 'p': // p- put register after
3165 psbs("Nothing in register %c", what_reg());
3168 // are we putting whole lines or strings
3169 if (strchr(p, '\n') != NULL) {
3171 dot_begin(); // putting lines- Put above
3174 // are we putting after very last line?
3175 if (end_line(dot) == (end - 1)) {
3176 dot = end; // force dot to end of text[]
3178 dot_next(); // next line, then put before
3183 dot_right(); // move to right, can move to NL
3185 dot = string_insert(dot, p); // insert the string
3186 end_cmd_q(); // stop adding to q
3188 case 'U': // U- Undo; replace current line with original version
3189 if (reg[Ureg] != 0) {
3190 p = begin_line(dot);
3192 p = text_hole_delete(p, q); // delete cur line
3193 p = string_insert(p, reg[Ureg]); // insert orig line
3198 #endif /* FEATURE_VI_YANKMARK */
3199 case '$': // $- goto end of line
3200 case VI_K_END: // Cursor Key End
3204 dot = end_line(dot);
3206 case '%': // %- find matching char of pair () [] {}
3207 for (q = dot; q < end && *q != '\n'; q++) {
3208 if (strchr("()[]{}", *q) != NULL) {
3209 // we found half of a pair
3210 p = find_pair(q, *q);
3222 case 'f': // f- forward to a user specified char
3223 last_forward_char = get_one_char(); // get the search char
3225 // dont separate these two commands. 'f' depends on ';'
3227 //**** fall thru to ... ';'
3228 case ';': // ;- look at rest of line for last forward char
3232 if (last_forward_char == 0)
3235 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3238 if (*q == last_forward_char)
3241 case '-': // -- goto prev line
3248 #if ENABLE_FEATURE_VI_DOT_CMD
3249 case '.': // .- repeat the last modifying command
3250 // Stuff the last_modifying_cmd back into stdin
3251 // and let it be re-executed.
3252 if (last_modifying_cmd != 0) {
3253 ioq = ioq_start = xstrdup(last_modifying_cmd);
3257 #if ENABLE_FEATURE_VI_SEARCH
3258 case '?': // /- search for a pattern
3259 case '/': // /- search for a pattern
3262 q = get_input_line(buf); // get input line- use "status line"
3264 goto dc3; // if no pat re-use old pat
3265 if (q[0]) { // strlen(q) > 1: new pat- save it and find
3266 // there is a new pat
3267 free(last_search_pattern);
3268 last_search_pattern = xstrdup(q);
3269 goto dc3; // now find the pattern
3271 // user changed mind and erased the "/"- do nothing
3273 case 'N': // N- backward search for last pattern
3277 dir = BACK; // assume BACKWARD search
3279 if (last_search_pattern[0] == '?') {
3283 goto dc4; // now search for pattern
3285 case 'n': // n- repeat search for last pattern
3286 // search rest of text[] starting at next char
3287 // if search fails return orignal "p" not the "p+1" address
3292 if (last_search_pattern == 0) {
3293 msg = "No previous regular expression";
3296 if (last_search_pattern[0] == '/') {
3297 dir = FORWARD; // assume FORWARD search
3300 if (last_search_pattern[0] == '?') {
3305 q = char_search(p, last_search_pattern + 1, dir, FULL);
3307 dot = q; // good search, update "dot"
3311 // no pattern found between "dot" and "end"- continue at top
3316 q = char_search(p, last_search_pattern + 1, dir, FULL);
3317 if (q != NULL) { // found something
3318 dot = q; // found new pattern- goto it
3319 msg = "search hit BOTTOM, continuing at TOP";
3321 msg = "search hit TOP, continuing at BOTTOM";
3324 msg = "Pattern not found";
3330 case '{': // {- move backward paragraph
3331 q = char_search(dot, "\n\n", BACK, FULL);
3332 if (q != NULL) { // found blank line
3333 dot = next_line(q); // move to next blank line
3336 case '}': // }- move forward paragraph
3337 q = char_search(dot, "\n\n", FORWARD, FULL);
3338 if (q != NULL) { // found blank line
3339 dot = next_line(q); // move to next blank line
3342 #endif /* FEATURE_VI_SEARCH */
3343 case '0': // 0- goto begining of line
3353 if (c == '0' && cmdcnt < 1) {
3354 dot_begin(); // this was a standalone zero
3356 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3359 case ':': // :- the colon mode commands
3360 p = get_input_line(":"); // get input line- use "status line"
3361 #if ENABLE_FEATURE_VI_COLON
3362 colon(p); // execute the command
3365 p++; // move past the ':'
3369 if (strncasecmp(p, "quit", cnt) == 0
3370 || strncasecmp(p, "q!", cnt) == 0 // delete lines
3372 if (file_modified && p[1] != '!') {
3373 psbs("No write since last change (:quit! overrides)");
3377 } else if (strncasecmp(p, "write", cnt) == 0
3378 || strncasecmp(p, "wq", cnt) == 0
3379 || strncasecmp(p, "wn", cnt) == 0
3380 || strncasecmp(p, "x", cnt) == 0
3382 cnt = file_write(cfn, text, end - 1);
3385 psbs("Write error: %s", strerror(errno));
3388 last_file_modified = -1;
3389 psb("\"%s\" %dL, %dC", cfn, count_lines(text, end - 1), cnt);
3390 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3391 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3396 } else if (strncasecmp(p, "file", cnt) == 0) {
3397 last_status_cksum = 0; // force status update
3398 } else if (sscanf(p, "%d", &j) > 0) {
3399 dot = find_line(j); // go to line # j
3401 } else { // unrecognised cmd
3404 #endif /* !FEATURE_VI_COLON */
3406 case '<': // <- Left shift something
3407 case '>': // >- Right shift something
3408 cnt = count_lines(text, dot); // remember what line we are on
3409 c1 = get_one_char(); // get the type of thing to delete
3410 find_range(&p, &q, c1);
3411 yank_delete(p, q, 1, YANKONLY); // save copy before change
3414 i = count_lines(p, q); // # of lines we are shifting
3415 for ( ; i > 0; i--, p = next_line(p)) {
3417 // shift left- remove tab or 8 spaces
3419 // shrink buffer 1 char
3420 text_hole_delete(p, p);
3421 } else if (*p == ' ') {
3422 // we should be calculating columns, not just SPACE
3423 for (j = 0; *p == ' ' && j < tabstop; j++) {
3424 text_hole_delete(p, p);
3427 } else if (c == '>') {
3428 // shift right -- add tab or 8 spaces
3429 char_insert(p, '\t');
3432 dot = find_line(cnt); // what line were we on
3434 end_cmd_q(); // stop adding to q
3436 case 'A': // A- append at e-o-l
3437 dot_end(); // go to e-o-l
3438 //**** fall thru to ... 'a'
3439 case 'a': // a- append after current char
3444 case 'B': // B- back a blank-delimited Word
3445 case 'E': // E- end of a blank-delimited word
3446 case 'W': // W- forward a blank-delimited word
3453 if (c == 'W' || isspace(dot[dir])) {
3454 dot = skip_thing(dot, 1, dir, S_TO_WS);
3455 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3458 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3460 case 'C': // C- Change to e-o-l
3461 case 'D': // D- delete to e-o-l
3463 dot = dollar_line(dot); // move to before NL
3464 // copy text into a register and delete
3465 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3467 goto dc_i; // start inserting
3468 #if ENABLE_FEATURE_VI_DOT_CMD
3470 end_cmd_q(); // stop adding to q
3473 case 'G': // G- goto to a line number (default= E-O-F)
3474 dot = end - 1; // assume E-O-F
3476 dot = find_line(cmdcnt); // what line is #cmdcnt
3480 case 'H': // H- goto top line on screen
3482 if (cmdcnt > (rows - 1)) {
3483 cmdcnt = (rows - 1);
3490 case 'I': // I- insert before first non-blank
3493 //**** fall thru to ... 'i'
3494 case 'i': // i- insert before current char
3495 case VI_K_INSERT: // Cursor Key Insert
3497 cmd_mode = 1; // start insrting
3499 case 'J': // J- join current and next lines together
3503 dot_end(); // move to NL
3504 if (dot < end - 1) { // make sure not last char in text[]
3505 *dot++ = ' '; // replace NL with space
3507 while (isblnk(*dot)) { // delete leading WS
3511 end_cmd_q(); // stop adding to q
3513 case 'L': // L- goto bottom line on screen
3515 if (cmdcnt > (rows - 1)) {
3516 cmdcnt = (rows - 1);
3524 case 'M': // M- goto middle line on screen
3526 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3527 dot = next_line(dot);
3529 case 'O': // O- open a empty line above
3531 p = begin_line(dot);
3532 if (p[-1] == '\n') {
3534 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3536 dot = char_insert(dot, '\n');
3539 dot = char_insert(dot, '\n'); // i\n ESC
3544 case 'R': // R- continuous Replace char
3548 case 'X': // X- delete char before dot
3549 case 'x': // x- delete the current char
3550 case 's': // s- substitute the current char
3557 if (dot[dir] != '\n') {
3559 dot--; // delete prev char
3560 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3563 goto dc_i; // start insrting
3564 end_cmd_q(); // stop adding to q
3566 case 'Z': // Z- if modified, {write}; exit
3567 // ZZ means to save file (if necessary), then exit
3568 c1 = get_one_char();
3573 if (file_modified) {
3574 #if ENABLE_FEATURE_VI_READONLY
3575 if (vi_readonly || readonly) {
3576 psbs("\"%s\" File is read only", cfn);
3580 cnt = file_write(cfn, text, end - 1);
3583 psbs("Write error: %s", strerror(errno));
3584 } else if (cnt == (end - 1 - text + 1)) {
3591 case '^': // ^- move to first non-blank on line
3595 case 'b': // b- back a word
3596 case 'e': // e- end of word
3603 if ((dot + dir) < text || (dot + dir) > end - 1)
3606 if (isspace(*dot)) {
3607 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3609 if (isalnum(*dot) || *dot == '_') {
3610 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3611 } else if (ispunct(*dot)) {
3612 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3615 case 'c': // c- change something
3616 case 'd': // d- delete something
3617 #if ENABLE_FEATURE_VI_YANKMARK
3618 case 'y': // y- yank something
3619 case 'Y': // Y- Yank a line
3621 yf = YANKDEL; // assume either "c" or "d"
3622 #if ENABLE_FEATURE_VI_YANKMARK
3623 if (c == 'y' || c == 'Y')
3628 c1 = get_one_char(); // get the type of thing to delete
3629 find_range(&p, &q, c1);
3630 if (c1 == 27) { // ESC- user changed mind and wants out
3631 c = c1 = 27; // Escape- do nothing
3632 } else if (strchr("wW", c1)) {
3634 // don't include trailing WS as part of word
3635 while (isblnk(*q)) {
3636 if (q <= text || q[-1] == '\n')
3641 dot = yank_delete(p, q, 0, yf); // delete word
3642 } else if (strchr("^0bBeEft$", c1)) {
3643 // single line copy text into a register and delete
3644 dot = yank_delete(p, q, 0, yf); // delete word
3645 } else if (strchr("cdykjHL%+-{}\r\n", c1)) {
3646 // multiple line copy text into a register and delete
3647 dot = yank_delete(p, q, 1, yf); // delete lines
3649 dot = char_insert(dot, '\n');
3650 // on the last line of file don't move to prev line
3651 if (dot != (end-1)) {
3654 } else if (c == 'd') {
3659 // could not recognize object
3660 c = c1 = 27; // error-
3664 // if CHANGING, not deleting, start inserting after the delete
3666 strcpy(buf, "Change");
3667 goto dc_i; // start inserting
3670 strcpy(buf, "Delete");
3672 #if ENABLE_FEATURE_VI_YANKMARK
3673 if (c == 'y' || c == 'Y') {
3674 strcpy(buf, "Yank");
3678 for (cnt = 0; p <= q; p++) {
3682 psb("%s %d lines (%d chars) using [%c]",
3683 buf, cnt, strlen(reg[YDreg]), what_reg());
3685 end_cmd_q(); // stop adding to q
3688 case 'k': // k- goto prev line, same col
3689 case VI_K_UP: // cursor key Up
3694 dot = move_to_col(dot, ccol + offset); // try stay in same col
3696 case 'r': // r- replace the current char with user input
3697 c1 = get_one_char(); // get the replacement char
3700 file_modified++; // has the file been modified
3702 end_cmd_q(); // stop adding to q
3704 case 't': // t- move to char prior to next x
3705 last_forward_char = get_one_char();
3707 if (*dot == last_forward_char)
3709 last_forward_char= 0;
3711 case 'w': // w- forward a word
3715 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3716 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3717 } else if (ispunct(*dot)) { // we are on PUNCT
3718 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3721 dot++; // move over word
3722 if (isspace(*dot)) {
3723 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3727 c1 = get_one_char(); // get the replacement char
3730 cnt = (rows - 2) / 2; // put dot at center
3732 cnt = rows - 2; // put dot at bottom
3733 screenbegin = begin_line(dot); // start dot at top
3734 dot_scroll(cnt, -1);
3736 case '|': // |- move to column "cmdcnt"
3737 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3739 case '~': // ~- flip the case of letters a-z -> A-Z
3743 if (islower(*dot)) {
3744 *dot = toupper(*dot);
3745 file_modified++; // has the file been modified
3746 } else if (isupper(*dot)) {
3747 *dot = tolower(*dot);
3748 file_modified++; // has the file been modified
3751 end_cmd_q(); // stop adding to q
3753 //----- The Cursor and Function Keys -----------------------------
3754 case VI_K_HOME: // Cursor Key Home
3757 // The Fn keys could point to do_macro which could translate them
3758 case VI_K_FUN1: // Function Key F1
3759 case VI_K_FUN2: // Function Key F2
3760 case VI_K_FUN3: // Function Key F3
3761 case VI_K_FUN4: // Function Key F4
3762 case VI_K_FUN5: // Function Key F5
3763 case VI_K_FUN6: // Function Key F6
3764 case VI_K_FUN7: // Function Key F7
3765 case VI_K_FUN8: // Function Key F8
3766 case VI_K_FUN9: // Function Key F9
3767 case VI_K_FUN10: // Function Key F10
3768 case VI_K_FUN11: // Function Key F11
3769 case VI_K_FUN12: // Function Key F12
3774 // if text[] just became empty, add back an empty line
3776 char_insert(text, '\n'); // start empty buf with dummy line
3779 // it is OK for dot to exactly equal to end, otherwise check dot validity
3781 dot = bound_dot(dot); // make sure "dot" is valid
3783 #if ENABLE_FEATURE_VI_YANKMARK
3784 check_context(c); // update the current context
3788 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3789 cnt = dot - begin_line(dot);
3790 // Try to stay off of the Newline
3791 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3795 #if ENABLE_FEATURE_VI_CRASHME
3796 static int totalcmds = 0;
3797 static int Mp = 85; // Movement command Probability
3798 static int Np = 90; // Non-movement command Probability
3799 static int Dp = 96; // Delete command Probability
3800 static int Ip = 97; // Insert command Probability
3801 static int Yp = 98; // Yank command Probability
3802 static int Pp = 99; // Put command Probability
3803 static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
3804 const char chars[20] = "\t012345 abcdABCD-=.$";
3805 const char *const words[20] = {
3806 "this", "is", "a", "test",
3807 "broadcast", "the", "emergency", "of",
3808 "system", "quick", "brown", "fox",
3809 "jumped", "over", "lazy", "dogs",
3810 "back", "January", "Febuary", "March"
3812 const char *const lines[20] = {
3813 "You should have received a copy of the GNU General Public License\n",
3814 "char c, cm, *cmd, *cmd1;\n",
3815 "generate a command by percentages\n",
3816 "Numbers may be typed as a prefix to some commands.\n",
3817 "Quit, discarding changes!\n",
3818 "Forced write, if permission originally not valid.\n",
3819 "In general, any ex or ed command (such as substitute or delete).\n",
3820 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3821 "Please get w/ me and I will go over it with you.\n",
3822 "The following is a list of scheduled, committed changes.\n",
3823 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3824 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3825 "Any question about transactions please contact Sterling Huxley.\n",
3826 "I will try to get back to you by Friday, December 31.\n",
3827 "This Change will be implemented on Friday.\n",
3828 "Let me know if you have problems accessing this;\n",
3829 "Sterling Huxley recently added you to the access list.\n",
3830 "Would you like to go to lunch?\n",
3831 "The last command will be automatically run.\n",
3832 "This is too much english for a computer geek.\n",
3834 char *multilines[20] = {
3835 "You should have received a copy of the GNU General Public License\n",
3836 "char c, cm, *cmd, *cmd1;\n",
3837 "generate a command by percentages\n",
3838 "Numbers may be typed as a prefix to some commands.\n",
3839 "Quit, discarding changes!\n",
3840 "Forced write, if permission originally not valid.\n",
3841 "In general, any ex or ed command (such as substitute or delete).\n",
3842 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3843 "Please get w/ me and I will go over it with you.\n",
3844 "The following is a list of scheduled, committed changes.\n",
3845 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3846 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3847 "Any question about transactions please contact Sterling Huxley.\n",
3848 "I will try to get back to you by Friday, December 31.\n",
3849 "This Change will be implemented on Friday.\n",
3850 "Let me know if you have problems accessing this;\n",
3851 "Sterling Huxley recently added you to the access list.\n",
3852 "Would you like to go to lunch?\n",
3853 "The last command will be automatically run.\n",
3854 "This is too much english for a computer geek.\n",
3857 // create a random command to execute
3858 static void crash_dummy()
3860 static int sleeptime; // how long to pause between commands
3861 char c, cm, *cmd, *cmd1;
3862 int i, cnt, thing, rbi, startrbi, percent;
3864 // "dot" movement commands
3865 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
3867 // is there already a command running?
3868 if (readed_for_parse > 0)
3872 sleeptime = 0; // how long to pause between commands
3873 memset(readbuffer, '\0', BUFSIZ); // clear the read buffer
3874 // generate a command by percentages
3875 percent = (int) lrand48() % 100; // get a number from 0-99
3876 if (percent < Mp) { // Movement commands
3877 // available commands
3880 } else if (percent < Np) { // non-movement commands
3881 cmd = "mz<>\'\""; // available commands
3883 } else if (percent < Dp) { // Delete commands
3884 cmd = "dx"; // available commands
3886 } else if (percent < Ip) { // Inset commands
3887 cmd = "iIaAsrJ"; // available commands
3889 } else if (percent < Yp) { // Yank commands
3890 cmd = "yY"; // available commands
3892 } else if (percent < Pp) { // Put commands
3893 cmd = "pP"; // available commands
3896 // We do not know how to handle this command, try again
3900 // randomly pick one of the available cmds from "cmd[]"
3901 i = (int) lrand48() % strlen(cmd);
3903 if (strchr(":\024", cm))
3904 goto cd0; // dont allow colon or ctrl-T commands
3905 readbuffer[rbi++] = cm; // put cmd into input buffer
3907 // now we have the command-
3908 // there are 1, 2, and multi char commands
3909 // find out which and generate the rest of command as necessary
3910 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
3911 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
3912 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
3913 cmd1 = "abcdefghijklmnopqrstuvwxyz";
3915 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
3917 readbuffer[rbi++] = c; // add movement to input buffer
3919 if (strchr("iIaAsc", cm)) { // multi-char commands
3921 // change some thing
3922 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
3924 readbuffer[rbi++] = c; // add movement to input buffer
3926 thing = (int) lrand48() % 4; // what thing to insert
3927 cnt = (int) lrand48() % 10; // how many to insert
3928 for (i = 0; i < cnt; i++) {
3929 if (thing == 0) { // insert chars
3930 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
3931 } else if (thing == 1) { // insert words
3932 strcat(readbuffer, words[(int) lrand48() % 20]);
3933 strcat(readbuffer, " ");
3934 sleeptime = 0; // how fast to type
3935 } else if (thing == 2) { // insert lines
3936 strcat(readbuffer, lines[(int) lrand48() % 20]);
3937 sleeptime = 0; // how fast to type
3938 } else { // insert multi-lines
3939 strcat(readbuffer, multilines[(int) lrand48() % 20]);
3940 sleeptime = 0; // how fast to type
3943 strcat(readbuffer, "\033");
3945 readed_for_parse = strlen(readbuffer);
3949 mysleep(sleeptime); // sleep 1/100 sec
3952 // test to see if there are any errors
3953 static void crash_test()
3955 static time_t oldtim;
3958 char d[2], msg[BUFSIZ];
3962 strcat(msg, "end<text ");
3964 if (end > textend) {
3965 strcat(msg, "end>textend ");
3968 strcat(msg, "dot<text ");
3971 strcat(msg, "dot>end ");
3973 if (screenbegin < text) {
3974 strcat(msg, "screenbegin<text ");
3976 if (screenbegin > end - 1) {
3977 strcat(msg, "screenbegin>end-1 ");
3982 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
3983 totalcmds, last_input_char, msg, SOs, SOn);
3985 while (read(0, d, 1) > 0) {
3986 if (d[0] == '\n' || d[0] == '\r')
3991 tim = (time_t) time((time_t *) 0);
3992 if (tim >= (oldtim + 3)) {
3993 sprintf(status_buffer,
3994 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
3995 totalcmds, M, N, I, D, Y, P, U, end - text + 1);