Starting implementation of MCI creator tasks
[wine/dcerpc.git] / debugger / editline.c
blob7021bcbb5c20b5bcde2123dc98d9a64629146f97
1 /*
2 * Line-editing routines
4 * Copyright 1992 Simmule Turner and Rich Salz. All rights reserved.
6 *
7 * This software is not subject to any license of the American Telephone
8 * and Telegraph Company or of the Regents of the University of California.
9 *
10 * Permission is granted to anyone to use this software for any purpose on
11 * any computer system, and to alter it and redistribute it freely, subject
12 * to the following restrictions:
13 * 1. The authors are not responsible for the consequences of use of this
14 * software, no matter how awful, even if they arise from flaws in it.
15 * 2. The origin of this software must not be misrepresented, either by
16 * explicit claim or by omission. Since few users ever read sources,
17 * credits must appear in the documentation.
18 * 3. Altered versions must be plainly marked as such, and must not be
19 * misrepresented as being the original software. Since few users
20 * ever read sources, credits must appear in the documentation.
21 * 4. This notice may not be removed or altered.
23 * The code was heavily simplified for inclusion in Wine. -- AJ
26 #include "config.h"
27 #include <ctype.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <errno.h>
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
36 #include "windef.h"
39 ** Manifest constants.
41 #define SCREEN_WIDTH 80
42 #define SCREEN_ROWS 24
43 #define NO_ARG (-1)
44 #define DEL 127
45 #define CTL(x) ((x) & 0x1F)
46 #define ISCTL(x) ((x) && (x) < ' ')
47 #define UNCTL(x) ((x) + 64)
48 #define META(x) ((x) | 0x80)
49 #define ISMETA(x) ((x) & 0x80)
50 #define UNMETA(x) ((x) & 0x7F)
51 #if !defined(HIST_SIZE)
52 #define HIST_SIZE 20
53 #endif /* !defined(HIST_SIZE) */
54 #define CRLF "\r\n"
55 #define MEM_INC 64
56 #define SCREEN_INC 256
58 #define DISPOSE(p) free((char *)(p))
59 #define NEW(T, c) \
60 ((T *)malloc((unsigned int)(sizeof (T) * (c))))
61 #define RENEW(p, T, c) \
62 (p = (T *)realloc((char *)(p), (unsigned int)(sizeof (T) * (c))))
63 #define COPYFROMTO(new, p, len) \
64 (void)memcpy((char *)(new), (char *)(p), (int)(len))
67 ** Command status codes.
69 typedef enum _STATUS {
70 CSdone, CSeof, CSmove, CSdispatch, CSstay
71 } STATUS;
74 ** The type of case-changing to perform.
76 typedef enum _CASE {
77 TOupper, TOlower
78 } CASE;
81 ** Key to command mapping.
83 typedef struct _KEYMAP {
84 CHAR Key;
85 STATUS (*Function)();
86 } KEYMAP;
89 ** Command history structure.
91 typedef struct _HISTORY {
92 int Size;
93 int Pos;
94 CHAR *Lines[HIST_SIZE];
95 } HISTORY;
98 ** Globals.
100 static int rl_eof;
101 static int rl_erase;
102 static int rl_intr;
103 static int rl_kill;
105 static CHAR NIL[] = "";
106 static const CHAR *Input = NIL;
107 static CHAR *Line;
108 static const char *Prompt;
109 static CHAR *Yanked;
110 static char *Screen;
111 static char NEWLINE[]= CRLF;
112 static HISTORY H;
113 static int rl_quit;
114 static int Repeat;
115 static int End;
116 static int Mark;
117 static int OldPoint;
118 static int Point;
119 static int PushBack;
120 static int Pushed;
121 static KEYMAP Map[33];
122 static KEYMAP MetaMap[16];
123 static size_t Length;
124 static size_t ScreenCount;
125 static size_t ScreenSize;
126 static char *backspace;
127 static int TTYwidth;
128 static int TTYrows;
130 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
131 int rl_meta_chars = 1;
134 ** Declarations.
136 static CHAR *editinput();
137 extern int read();
138 extern int write();
139 #if defined(USE_TERMCAP)
140 extern char *getenv();
141 extern char *tgetstr();
142 extern int tgetent();
143 #endif /* defined(USE_TERMCAP) */
146 ** TTY input/output functions.
149 #ifdef HAVE_TCGETATTR
150 #include <termios.h>
152 static void
153 rl_ttyset(Reset)
154 int Reset;
156 static struct termios old;
157 struct termios new;
159 if (Reset == 0) {
160 (void)tcgetattr(0, &old);
161 rl_erase = old.c_cc[VERASE];
162 rl_kill = old.c_cc[VKILL];
163 rl_eof = old.c_cc[VEOF];
164 rl_intr = old.c_cc[VINTR];
165 rl_quit = old.c_cc[VQUIT];
167 new = old;
168 new.c_cc[VINTR] = -1;
169 new.c_cc[VQUIT] = -1;
170 new.c_lflag &= ~(ECHO | ICANON);
171 new.c_iflag &= ~(ISTRIP | INPCK);
172 new.c_cc[VMIN] = 1;
173 new.c_cc[VTIME] = 0;
174 (void)tcsetattr(0, TCSANOW, &new);
176 else
177 (void)tcsetattr(0, TCSANOW, &old);
180 #else /* HAVE_TCGETATTR */
182 static void
183 rl_ttyset(Reset)
184 int Reset;
186 static struct sgttyb old_sgttyb;
187 static struct tchars old_tchars;
188 struct sgttyb new_sgttyb;
189 struct tchars new_tchars;
191 if (Reset == 0) {
192 (void)ioctl(0, TIOCGETP, &old_sgttyb);
193 rl_erase = old_sgttyb.sg_erase;
194 rl_kill = old_sgttyb.sg_kill;
196 (void)ioctl(0, TIOCGETC, &old_tchars);
197 rl_eof = old_tchars.t_eofc;
198 rl_intr = old_tchars.t_intrc;
199 rl_quit = old_tchars.t_quitc;
201 new_sgttyb = old_sgttyb;
202 new_sgttyb.sg_flags &= ~ECHO;
203 new_sgttyb.sg_flags |= RAW;
204 #if defined(PASS8)
205 new_sgttyb.sg_flags |= PASS8;
206 #endif /* defined(PASS8) */
207 (void)ioctl(0, TIOCSETP, &new_sgttyb);
209 new_tchars = old_tchars;
210 new_tchars.t_intrc = -1;
211 new_tchars.t_quitc = -1;
212 (void)ioctl(0, TIOCSETC, &new_tchars);
214 else {
215 (void)ioctl(0, TIOCSETP, &old_sgttyb);
216 (void)ioctl(0, TIOCSETC, &old_tchars);
220 #endif /* HAVE_TCGETATTR */
222 static void
223 TTYflush()
225 if (ScreenCount) {
226 (void)write(1, Screen, ScreenCount);
227 ScreenCount = 0;
231 static void
232 TTYput(c)
233 CHAR c;
235 Screen[ScreenCount] = c;
236 if (++ScreenCount >= ScreenSize - 1) {
237 ScreenSize += SCREEN_INC;
238 RENEW(Screen, char, ScreenSize);
242 static void
243 TTYputs(p)
244 CHAR *p;
246 while (*p)
247 TTYput(*p++);
250 static void
251 TTYshow(c)
252 CHAR c;
254 if (c == DEL) {
255 TTYput('^');
256 TTYput('?');
258 else if (ISCTL(c)) {
259 TTYput('^');
260 TTYput(UNCTL(c));
262 else if (rl_meta_chars && ISMETA(c)) {
263 TTYput('M');
264 TTYput('-');
265 TTYput(UNMETA(c));
267 else
268 TTYput(c);
271 static void
272 TTYstring(p)
273 CHAR *p;
275 while (*p)
276 TTYshow(*p++);
279 static unsigned int
280 TTYget()
282 CHAR c;
283 int retv;
285 TTYflush();
286 if (Pushed) {
287 Pushed = 0;
288 return PushBack;
290 if (*Input)
291 return *Input++;
293 while ( ( retv = read( 0, &c, (size_t)1 ) ) == -1 )
295 if ( errno != EINTR )
297 perror( "read" );
298 return EOF;
302 return retv == 1 ? c : EOF;
305 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
307 static void
308 TTYbackn(n)
309 int n;
311 while (--n >= 0)
312 TTYback();
315 static void
316 TTYinfo()
318 static int init;
319 #if defined(USE_TERMCAP)
320 char *term;
321 char buff[2048];
322 char *bp;
323 #endif /* defined(USE_TERMCAP) */
324 #if defined(TIOCGWINSZ)
325 struct winsize W;
326 #endif /* defined(TIOCGWINSZ) */
328 if (init) {
329 #if defined(TIOCGWINSZ)
330 /* Perhaps we got resized. */
331 if (ioctl(0, TIOCGWINSZ, &W) >= 0
332 && W.ws_col > 0 && W.ws_row > 0) {
333 TTYwidth = (int)W.ws_col;
334 TTYrows = (int)W.ws_row;
336 #endif /* defined(TIOCGWINSZ) */
337 return;
339 init++;
341 TTYwidth = TTYrows = 0;
342 #if defined(USE_TERMCAP)
343 bp = &buff[0];
344 if ((term = getenv("TERM")) == NULL)
345 term = "dumb";
346 if (tgetent(buff, term) < 0) {
347 TTYwidth = SCREEN_WIDTH;
348 TTYrows = SCREEN_ROWS;
349 return;
351 backspace = tgetstr("le", &bp);
352 TTYwidth = tgetnum("co");
353 TTYrows = tgetnum("li");
354 #endif /* defined(USE_TERMCAP) */
356 #if defined(TIOCGWINSZ)
357 if (ioctl(0, TIOCGWINSZ, &W) >= 0) {
358 TTYwidth = (int)W.ws_col;
359 TTYrows = (int)W.ws_row;
361 #endif /* defined(TIOCGWINSZ) */
363 if (TTYwidth <= 0 || TTYrows <= 0) {
364 TTYwidth = SCREEN_WIDTH;
365 TTYrows = SCREEN_ROWS;
371 static void
372 reposition()
374 int i;
375 CHAR *p;
377 TTYput('\r');
378 TTYputs((CHAR *)Prompt);
379 for (i = Point, p = Line; --i >= 0; p++)
380 TTYshow(*p);
383 static void
384 left(Change)
385 STATUS Change;
387 TTYback();
388 if (Point) {
389 if (ISCTL(Line[Point - 1]))
390 TTYback();
391 else if (rl_meta_chars && ISMETA(Line[Point - 1])) {
392 TTYback();
393 TTYback();
396 if (Change == CSmove)
397 Point--;
400 static void
401 right(Change)
402 STATUS Change;
404 TTYshow(Line[Point]);
405 if (Change == CSmove)
406 Point++;
409 static STATUS
410 ring_bell()
412 TTYput('\07');
413 TTYflush();
414 return CSstay;
417 static STATUS
418 do_macro(c)
419 unsigned int c;
421 CHAR name[4];
423 name[0] = '_';
424 name[1] = c;
425 name[2] = '_';
426 name[3] = '\0';
428 if ((Input = (CHAR *)getenv((char *)name)) == NULL) {
429 Input = NIL;
430 return ring_bell();
432 return CSstay;
435 static STATUS
436 do_forward(move)
437 STATUS move;
439 int i;
440 CHAR *p;
442 i = 0;
443 do {
444 p = &Line[Point];
445 for ( ; Point < End && (*p == ' ' || !isalnum(*p)); Point++, p++)
446 if (move == CSmove)
447 right(CSstay);
449 for (; Point < End && isalnum(*p); Point++, p++)
450 if (move == CSmove)
451 right(CSstay);
453 if (Point == End)
454 break;
455 } while (++i < Repeat);
457 return CSstay;
460 static STATUS
461 do_case(type)
462 CASE type;
464 int i;
465 int end;
466 int count;
467 CHAR *p;
469 (void)do_forward(CSstay);
470 if (OldPoint != Point) {
471 if ((count = Point - OldPoint) < 0)
472 count = -count;
473 Point = OldPoint;
474 if ((end = Point + count) > End)
475 end = End;
476 for (i = Point, p = &Line[i]; i < end; i++, p++) {
477 if (type == TOupper) {
478 if (islower(*p))
479 *p = toupper(*p);
481 else if (isupper(*p))
482 *p = tolower(*p);
483 right(CSmove);
486 return CSstay;
489 static STATUS
490 case_down_word()
492 return do_case(TOlower);
495 static STATUS
496 case_up_word()
498 return do_case(TOupper);
501 static void
502 ceol()
504 int extras;
505 int i;
506 CHAR *p;
508 for (extras = 0, i = Point, p = &Line[i]; i <= End; i++, p++) {
509 TTYput(' ');
510 if (ISCTL(*p)) {
511 TTYput(' ');
512 extras++;
514 else if (rl_meta_chars && ISMETA(*p)) {
515 TTYput(' ');
516 TTYput(' ');
517 extras += 2;
521 for (i += extras; i > Point; i--)
522 TTYback();
525 static void
526 clear_line()
528 Point = -strlen(Prompt);
529 TTYput('\r');
530 ceol();
531 Point = 0;
532 End = 0;
533 Line[0] = '\0';
536 static STATUS
537 insert_string(p)
538 CHAR *p;
540 size_t len;
541 int i;
542 CHAR *new;
543 CHAR *q;
545 len = strlen((char *)p);
546 if (End + len >= Length) {
547 if ((new = NEW(CHAR, Length + len + MEM_INC)) == NULL)
548 return CSstay;
549 if (Length) {
550 COPYFROMTO(new, Line, Length);
551 DISPOSE(Line);
553 Line = new;
554 Length += len + MEM_INC;
557 for (q = &Line[Point], i = End - Point; --i >= 0; )
558 q[len + i] = q[i];
559 COPYFROMTO(&Line[Point], p, len);
560 End += len;
561 Line[End] = '\0';
562 TTYstring(&Line[Point]);
563 Point += len;
565 return Point == End ? CSstay : CSmove;
569 static CHAR *
570 next_hist()
572 return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos];
575 static CHAR *
576 prev_hist()
578 return H.Pos == 0 ? NULL : H.Lines[--H.Pos];
581 static STATUS
582 do_insert_hist(p)
583 CHAR *p;
585 if (p == NULL)
586 return ring_bell();
587 Point = 0;
588 reposition();
589 ceol();
590 End = 0;
591 return insert_string(p);
594 static STATUS
595 do_hist(move)
596 CHAR *(*move)();
598 CHAR *p;
599 int i;
601 i = 0;
602 do {
603 if ((p = (*move)()) == NULL)
604 return ring_bell();
605 } while (++i < Repeat);
606 return do_insert_hist(p);
609 static STATUS
610 h_next()
612 return do_hist(next_hist);
615 static STATUS
616 h_prev()
618 return do_hist(prev_hist);
621 static STATUS
622 h_first()
624 return do_insert_hist(H.Lines[H.Pos = 0]);
627 static STATUS
628 h_last()
630 return do_insert_hist(H.Lines[H.Pos = H.Size - 1]);
634 ** Return zero if pat appears as a substring in text.
636 static int
637 substrcmp(text, pat, len)
638 char *text;
639 char *pat;
640 int len;
642 CHAR c;
644 if ((c = *pat) == '\0')
645 return *text == '\0';
646 for ( ; *text; text++)
647 if ((CHAR)*text == c && strncmp(text, pat, len) == 0)
648 return 0;
649 return 1;
652 static CHAR *
653 search_hist(search, move)
654 CHAR *search;
655 CHAR *(*move)();
657 static CHAR *old_search;
658 int len;
659 int pos;
660 int (*match)();
661 char *pat;
663 /* Save or get remembered search pattern. */
664 if (search && *search) {
665 if (old_search)
666 DISPOSE(old_search);
667 old_search = (CHAR *)strdup((char *)search);
669 else {
670 if (old_search == NULL || *old_search == '\0')
671 return NULL;
672 search = old_search;
675 /* Set up pattern-finder. */
676 if (*search == '^') {
677 match = strncmp;
678 pat = (char *)(search + 1);
680 else {
681 match = substrcmp;
682 pat = (char *)search;
684 len = strlen(pat);
686 for (pos = H.Pos; (*move)() != NULL; )
687 if ((*match)((char *)H.Lines[H.Pos], pat, len) == 0)
688 return H.Lines[H.Pos];
689 H.Pos = pos;
690 return NULL;
693 static STATUS
694 h_search()
696 static int Searching;
697 const char *old_prompt;
698 CHAR *(*move)();
699 CHAR *p;
701 if (Searching)
702 return ring_bell();
703 Searching = 1;
705 clear_line();
706 old_prompt = Prompt;
707 Prompt = "Search: ";
708 TTYputs((CHAR *)Prompt);
709 move = Repeat == NO_ARG ? prev_hist : next_hist;
710 p = search_hist(editinput(), move);
711 clear_line();
712 Prompt = old_prompt;
713 TTYputs((CHAR *)Prompt);
715 Searching = 0;
716 return do_insert_hist(p);
719 static STATUS
720 fd_char()
722 int i;
724 i = 0;
725 do {
726 if (Point >= End)
727 break;
728 right(CSmove);
729 } while (++i < Repeat);
730 return CSstay;
733 static void
734 save_yank(begin, i)
735 int begin;
736 int i;
738 if (Yanked) {
739 DISPOSE(Yanked);
740 Yanked = NULL;
743 if (i < 1)
744 return;
746 if ((Yanked = NEW(CHAR, (size_t)i + 1)) != NULL) {
747 COPYFROMTO(Yanked, &Line[begin], i);
748 Yanked[i] = '\0';
752 static STATUS
753 delete_string(count)
754 int count;
756 int i;
757 CHAR *p;
759 if (count <= 0 || End == Point)
760 return ring_bell();
762 if (count == 1 && Point == End - 1) {
763 /* Optimize common case of delete at end of line. */
764 End--;
765 p = &Line[Point];
766 i = 1;
767 TTYput(' ');
768 if (ISCTL(*p)) {
769 i = 2;
770 TTYput(' ');
772 else if (rl_meta_chars && ISMETA(*p)) {
773 i = 3;
774 TTYput(' ');
775 TTYput(' ');
777 TTYbackn(i);
778 *p = '\0';
779 return CSmove;
781 if (Point + count > End && (count = End - Point) <= 0)
782 return CSstay;
784 if (count > 1)
785 save_yank(Point, count);
787 for (p = &Line[Point], i = End - (Point + count) + 1; --i >= 0; p++)
788 p[0] = p[count];
789 ceol();
790 End -= count;
791 TTYstring(&Line[Point]);
792 return CSmove;
795 static STATUS
796 bk_char()
798 int i;
800 i = 0;
801 do {
802 if (Point == 0)
803 break;
804 left(CSmove);
805 } while (++i < Repeat);
807 return CSstay;
810 static STATUS
811 bk_del_char()
813 int i;
815 i = 0;
816 do {
817 if (Point == 0)
818 break;
819 left(CSmove);
820 } while (++i < Repeat);
822 return delete_string(i);
825 static STATUS
826 redisplay()
828 TTYputs((CHAR *)NEWLINE);
829 TTYputs((CHAR *)Prompt);
830 TTYstring(Line);
831 return CSmove;
834 static STATUS
835 kill_line()
837 int i;
839 if (Repeat != NO_ARG) {
840 if (Repeat < Point) {
841 i = Point;
842 Point = Repeat;
843 reposition();
844 (void)delete_string(i - Point);
846 else if (Repeat > Point) {
847 right(CSmove);
848 (void)delete_string(Repeat - Point - 1);
850 return CSmove;
853 save_yank(Point, End - Point);
854 Line[Point] = '\0';
855 ceol();
856 End = Point;
857 return CSstay;
860 static STATUS
861 insert_char(c)
862 int c;
864 STATUS s;
865 CHAR buff[2];
866 CHAR *p;
867 CHAR *q;
868 int i;
870 if (Repeat == NO_ARG || Repeat < 2) {
871 buff[0] = c;
872 buff[1] = '\0';
873 return insert_string(buff);
876 if ((p = NEW(CHAR, Repeat + 1)) == NULL)
877 return CSstay;
878 for (i = Repeat, q = p; --i >= 0; )
879 *q++ = c;
880 *q = '\0';
881 Repeat = 0;
882 s = insert_string(p);
883 DISPOSE(p);
884 return s;
887 static STATUS
888 meta()
890 unsigned int c;
891 KEYMAP *kp;
893 if ((c = TTYget()) == EOF)
894 return CSeof;
895 /* Also include VT-100 arrows. */
896 if (c == '[' || c == 'O')
897 switch (c = TTYget()) {
898 default: return ring_bell();
899 case EOF: return CSeof;
900 case 'A': return h_prev();
901 case 'B': return h_next();
902 case 'C': return fd_char();
903 case 'D': return bk_char();
906 if (isdigit(c)) {
907 for (Repeat = c - '0'; (c = TTYget()) != EOF && isdigit(c); )
908 Repeat = Repeat * 10 + c - '0';
909 Pushed = 1;
910 PushBack = c;
911 return CSstay;
914 if (isupper(c))
915 return do_macro(c);
916 for (OldPoint = Point, kp = MetaMap; kp->Function; kp++)
917 if (kp->Key == c)
918 return (*kp->Function)();
920 return ring_bell();
923 static STATUS
924 emacs(c)
925 unsigned int c;
927 STATUS s;
928 KEYMAP *kp;
930 if (ISMETA(c)) {
931 Pushed = 1;
932 PushBack = UNMETA(c);
933 return meta();
935 for (kp = Map; kp->Function; kp++)
936 if (kp->Key == c)
937 break;
938 s = kp->Function ? (*kp->Function)() : insert_char((int)c);
939 if (!Pushed)
940 /* No pushback means no repeat count; hacky, but true. */
941 Repeat = NO_ARG;
942 return s;
945 static STATUS
946 TTYspecial(c)
947 unsigned int c;
949 if (ISMETA(c))
950 return CSdispatch;
952 if (c == rl_erase || c == DEL)
953 return bk_del_char();
954 if (c == rl_kill) {
955 if (Point != 0) {
956 Point = 0;
957 reposition();
959 Repeat = NO_ARG;
960 return kill_line();
962 if (c == rl_intr || c == rl_quit) {
963 Point = End = 0;
964 Line[0] = '\0';
965 return redisplay();
967 if (c == rl_eof && Point == 0 && End == 0)
968 return CSeof;
970 return CSdispatch;
973 static CHAR *
974 editinput()
976 unsigned int c;
978 Repeat = NO_ARG;
979 OldPoint = Point = Mark = End = 0;
980 Line[0] = '\0';
982 while ((c = TTYget()) != EOF)
983 switch (TTYspecial(c)) {
984 case CSdone:
985 return Line;
986 case CSeof:
987 return NULL;
988 case CSmove:
989 reposition();
990 break;
991 case CSdispatch:
992 switch (emacs(c)) {
993 case CSdone:
994 return Line;
995 case CSeof:
996 return NULL;
997 case CSmove:
998 reposition();
999 break;
1000 case CSdispatch:
1001 case CSstay:
1002 break;
1004 break;
1005 case CSstay:
1006 break;
1008 return NULL;
1011 static void
1012 hist_add(p)
1013 CHAR *p;
1015 int i;
1017 if ((p = (CHAR *)strdup((char *)p)) == NULL)
1018 return;
1019 if (H.Size < HIST_SIZE)
1020 H.Lines[H.Size++] = p;
1021 else {
1022 DISPOSE(H.Lines[0]);
1023 for (i = 0; i < HIST_SIZE - 1; i++)
1024 H.Lines[i] = H.Lines[i + 1];
1025 H.Lines[i] = p;
1027 H.Pos = H.Size - 1;
1030 char *
1031 readline(prompt)
1032 const char *prompt;
1034 CHAR *line;
1036 if (Line == NULL) {
1037 Length = MEM_INC;
1038 if ((Line = NEW(CHAR, Length)) == NULL)
1039 return NULL;
1042 TTYinfo();
1043 rl_ttyset(0);
1044 hist_add(NIL);
1045 ScreenSize = SCREEN_INC;
1046 Screen = NEW(char, ScreenSize);
1047 Prompt = prompt ? prompt : (char *)NIL;
1048 TTYputs((CHAR *)Prompt);
1049 if ((line = editinput()) != NULL) {
1050 line = (CHAR *)strdup((char *)line);
1051 TTYputs((CHAR *)NEWLINE);
1052 TTYflush();
1054 rl_ttyset(1);
1055 DISPOSE(Screen);
1056 DISPOSE(H.Lines[--H.Size]);
1057 return (char *)line;
1060 void
1061 add_history(p)
1062 char *p;
1064 if (p == NULL || *p == '\0')
1065 return;
1067 #if defined(UNIQUE_HISTORY)
1068 if (H.Pos && strcmp(p, H.Lines[H.Pos - 1]) == 0)
1069 return;
1070 #endif /* defined(UNIQUE_HISTORY) */
1071 hist_add((CHAR *)p);
1075 static STATUS
1076 beg_line()
1078 if (Point) {
1079 Point = 0;
1080 return CSmove;
1082 return CSstay;
1085 static STATUS
1086 del_char()
1088 return delete_string(Repeat == NO_ARG ? 1 : Repeat);
1091 static STATUS
1092 end_line()
1094 if (Point != End) {
1095 Point = End;
1096 return CSmove;
1098 return CSstay;
1101 static STATUS
1102 accept_line()
1104 Line[End] = '\0';
1105 return CSdone;
1108 static STATUS
1109 transpose()
1111 CHAR c;
1113 if (Point) {
1114 if (Point == End)
1115 left(CSmove);
1116 c = Line[Point - 1];
1117 left(CSstay);
1118 Line[Point - 1] = Line[Point];
1119 TTYshow(Line[Point - 1]);
1120 Line[Point++] = c;
1121 TTYshow(c);
1123 return CSstay;
1126 static STATUS
1127 quote()
1129 unsigned int c;
1131 return (c = TTYget()) == EOF ? CSeof : insert_char((int)c);
1134 static STATUS
1135 wipe()
1137 int i;
1139 if (Mark > End)
1140 return ring_bell();
1142 if (Point > Mark) {
1143 i = Point;
1144 Point = Mark;
1145 Mark = i;
1146 reposition();
1149 return delete_string(Mark - Point);
1152 static STATUS
1153 mk_set()
1155 Mark = Point;
1156 return CSstay;
1159 static STATUS
1160 exchange()
1162 unsigned int c;
1164 if ((c = TTYget()) != CTL('X'))
1165 return c == EOF ? CSeof : ring_bell();
1167 if ((c = Mark) <= End) {
1168 Mark = Point;
1169 Point = c;
1170 return CSmove;
1172 return CSstay;
1175 static STATUS
1176 yank()
1178 if (Yanked && *Yanked)
1179 return insert_string(Yanked);
1180 return CSstay;
1183 static STATUS
1184 copy_region()
1186 if (Mark > End)
1187 return ring_bell();
1189 if (Point > Mark)
1190 save_yank(Mark, Point - Mark);
1191 else
1192 save_yank(Point, Mark - Point);
1194 return CSstay;
1197 static STATUS
1198 move_to_char()
1200 unsigned int c;
1201 int i;
1202 CHAR *p;
1204 if ((c = TTYget()) == EOF)
1205 return CSeof;
1206 for (i = Point + 1, p = &Line[i]; i < End; i++, p++)
1207 if (*p == c) {
1208 Point = i;
1209 return CSmove;
1211 return CSstay;
1214 static STATUS
1215 fd_word()
1217 return do_forward(CSmove);
1220 static STATUS
1221 fd_kill_word()
1223 int i;
1225 (void)do_forward(CSstay);
1226 if (OldPoint != Point) {
1227 i = Point - OldPoint;
1228 Point = OldPoint;
1229 return delete_string(i);
1231 return CSstay;
1234 static STATUS
1235 bk_word()
1237 int i;
1238 CHAR *p;
1240 i = 0;
1241 do {
1242 for (p = &Line[Point]; p > Line && !isalnum(p[-1]); p--)
1243 left(CSmove);
1245 for (; p > Line && p[-1] != ' ' && isalnum(p[-1]); p--)
1246 left(CSmove);
1248 if (Point == 0)
1249 break;
1250 } while (++i < Repeat);
1252 return CSstay;
1255 static STATUS
1256 bk_kill_word()
1258 (void)bk_word();
1259 if (OldPoint != Point)
1260 return delete_string(OldPoint - Point);
1261 return CSstay;
1264 static int
1265 argify(line, avp)
1266 CHAR *line;
1267 CHAR ***avp;
1269 CHAR *c;
1270 CHAR **p;
1271 CHAR **new;
1272 int ac;
1273 int i;
1275 i = MEM_INC;
1276 if ((*avp = p = NEW(CHAR*, i))== NULL)
1277 return 0;
1279 for (c = line; isspace(*c); c++)
1280 continue;
1281 if (*c == '\n' || *c == '\0')
1282 return 0;
1284 for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) {
1285 if (isspace(*c)) {
1286 *c++ = '\0';
1287 if (*c && *c != '\n') {
1288 if (ac + 1 == i) {
1289 new = NEW(CHAR*, i + MEM_INC);
1290 if (new == NULL) {
1291 p[ac] = NULL;
1292 return ac;
1294 COPYFROMTO(new, p, i * sizeof (char **));
1295 i += MEM_INC;
1296 DISPOSE(p);
1297 *avp = p = new;
1299 p[ac++] = c;
1302 else
1303 c++;
1305 *c = '\0';
1306 p[ac] = NULL;
1307 return ac;
1310 static STATUS
1311 last_argument()
1313 CHAR **av;
1314 CHAR *p;
1315 STATUS s;
1316 int ac;
1318 if (H.Size == 1 || (p = H.Lines[H.Size - 2]) == NULL)
1319 return ring_bell();
1321 if ((p = (CHAR *)strdup((char *)p)) == NULL)
1322 return CSstay;
1323 ac = argify(p, &av);
1325 if (Repeat != NO_ARG)
1326 s = Repeat < ac ? insert_string(av[Repeat]) : ring_bell();
1327 else
1328 s = ac ? insert_string(av[ac - 1]) : CSstay;
1330 if (ac)
1331 DISPOSE(av);
1332 DISPOSE(p);
1333 return s;
1336 static KEYMAP Map[33] = {
1337 { CTL('@'), ring_bell },
1338 { CTL('A'), beg_line },
1339 { CTL('B'), bk_char },
1340 { CTL('D'), del_char },
1341 { CTL('E'), end_line },
1342 { CTL('F'), fd_char },
1343 { CTL('G'), ring_bell },
1344 { CTL('H'), bk_del_char },
1345 { CTL('I'), ring_bell },
1346 { CTL('J'), accept_line },
1347 { CTL('K'), kill_line },
1348 { CTL('L'), redisplay },
1349 { CTL('M'), accept_line },
1350 { CTL('N'), h_next },
1351 { CTL('O'), ring_bell },
1352 { CTL('P'), h_prev },
1353 { CTL('Q'), ring_bell },
1354 { CTL('R'), h_search },
1355 { CTL('S'), ring_bell },
1356 { CTL('T'), transpose },
1357 { CTL('U'), ring_bell },
1358 { CTL('V'), quote },
1359 { CTL('W'), wipe },
1360 { CTL('X'), exchange },
1361 { CTL('Y'), yank },
1362 { CTL('Z'), ring_bell },
1363 { CTL('['), meta },
1364 { CTL(']'), move_to_char },
1365 { CTL('^'), ring_bell },
1366 { CTL('_'), ring_bell },
1367 { 0, NULL }
1370 static KEYMAP MetaMap[16]= {
1371 { CTL('H'), bk_kill_word },
1372 { DEL, bk_kill_word },
1373 { ' ', mk_set },
1374 { '.', last_argument },
1375 { '<', h_first },
1376 { '>', h_last },
1377 { '?', ring_bell },
1378 { 'b', bk_word },
1379 { 'd', fd_kill_word },
1380 { 'f', fd_word },
1381 { 'l', case_down_word },
1382 { 'u', case_up_word },
1383 { 'y', yank },
1384 { 'w', copy_region },
1385 { 0, NULL }