2 * Line-editing routines
4 * Copyright 1992 Simmule Turner and Rich Salz. All rights reserved.
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.
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
38 ** Manifest constants.
40 #define SCREEN_WIDTH 80
41 #define SCREEN_ROWS 24
44 #define CTL(x) ((x) & 0x1F)
45 #define ISCTL(x) ((x) && (x) < ' ')
46 #define UNCTL(x) ((x) + 64)
47 #define META(x) ((x) | 0x80)
48 #define ISMETA(x) ((x) & 0x80)
49 #define UNMETA(x) ((x) & 0x7F)
50 #if !defined(HIST_SIZE)
52 #endif /* !defined(HIST_SIZE) */
55 #define SCREEN_INC 256
57 #define DISPOSE(p) DBG_free((char *)(p))
59 ((T *)DBG_alloc((unsigned int)(sizeof (T) * (c))))
60 #define RENEW(p, T, c) \
61 (p = (T *)DBG_realloc((char *)(p), (unsigned int)(sizeof (T) * (c))))
62 #define COPYFROMTO(new, p, len) \
63 (void)memcpy((char *)(new), (char *)(p), (int)(len))
66 ** Command status codes.
68 typedef enum _STATUS {
69 CSdone, CSeof, CSmove, CSdispatch, CSstay
73 ** The type of case-changing to perform.
80 ** Key to command mapping.
82 typedef struct _KEYMAP {
88 ** Command history structure.
90 typedef struct _HISTORY {
93 CHAR *Lines[HIST_SIZE];
104 static CHAR NIL[] = "";
105 static const CHAR *Input = NIL;
107 static const char *Prompt;
110 static char NEWLINE[]= CRLF;
120 static KEYMAP Map[33];
121 static KEYMAP MetaMap[16];
122 static size_t Length;
123 static size_t ScreenCount;
124 static size_t ScreenSize;
125 static char *backspace;
129 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
130 int rl_meta_chars = 1;
135 static CHAR *editinput();
138 ** TTY input/output functions.
144 static DWORD old_mode;
147 GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &old_mode);
148 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), 0 /*ENABLE_PROCESSED_INPUT|ENABLE_WINDOW_INPUT|ENABLE_MOUSE_INPUT*/);
150 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), old_mode);
158 DEBUG_Output(DBG_CHN_MESG, Screen, ScreenCount);
166 Screen[ScreenCount] = c;
167 if (++ScreenCount >= ScreenSize - 1) {
168 ScreenSize += SCREEN_INC;
169 RENEW(Screen, char, ScreenSize);
191 else if (rl_meta_chars && ISMETA(c)) {
222 /* data available ? */
223 if (ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &c, 1, &retv, NULL) &&
226 switch (WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), INFINITE)) {
230 DEBUG_Printf(DBG_CHN_FIXME, "shouldn't happen\n");
239 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
251 COORD c = GetLargestConsoleWindowSize(GetStdHandle(STD_INPUT_HANDLE));
265 TTYputs((CHAR *)Prompt);
266 for (i = Point, p = Line; --i >= 0; p++)
275 if (ISCTL(Line[Point - 1]))
277 else if (rl_meta_chars && ISMETA(Line[Point - 1])) {
282 if (Change == CSmove)
289 TTYshow(Line[Point]);
290 if (Change == CSmove)
303 do_macro(unsigned int c)
312 if ((Input = (CHAR *)getenv((char *)name)) == NULL) {
320 do_forward(STATUS move)
328 for ( ; Point < End && (*p == ' ' || !isalnum(*p)); Point++, p++)
332 for (; Point < End && isalnum(*p); Point++, p++)
338 } while (++i < Repeat);
351 (void)do_forward(CSstay);
352 if (OldPoint != Point) {
353 if ((count = Point - OldPoint) < 0)
356 if ((end = Point + count) > End)
358 for (i = Point, p = &Line[i]; i < end; i++, p++) {
359 if (type == TOupper) {
363 else if (isupper(*p))
374 return do_case(TOlower);
380 return do_case(TOupper);
390 for (extras = 0, i = Point, p = &Line[i]; i <= End; i++, p++) {
396 else if (rl_meta_chars && ISMETA(*p)) {
403 for (i += extras; i > Point; i--)
410 Point = -strlen(Prompt);
419 insert_string(CHAR *p)
426 len = strlen((char *)p);
427 if (End + len >= Length) {
428 if ((new = NEW(CHAR, Length + len + MEM_INC)) == NULL)
431 COPYFROMTO(new, Line, Length);
435 Length += len + MEM_INC;
438 for (q = &Line[Point], i = End - Point; --i >= 0; )
440 COPYFROMTO(&Line[Point], p, len);
443 TTYstring(&Line[Point]);
446 return Point == End ? CSstay : CSmove;
453 return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos];
459 return H.Pos == 0 ? NULL : H.Lines[--H.Pos];
463 do_insert_hist(CHAR *p)
471 return insert_string(p);
475 do_hist(CHAR *(*move)(void))
482 if ((p = (*move)()) == NULL)
484 } while (++i < Repeat);
485 return do_insert_hist(p);
491 return do_hist(next_hist);
497 return do_hist(prev_hist);
503 return do_insert_hist(H.Lines[H.Pos = 0]);
509 return do_insert_hist(H.Lines[H.Pos = H.Size - 1]);
513 ** Return zero if pat appears as a substring in text.
516 substrcmp(char *text, char *pat, int len)
520 if ((c = *pat) == '\0')
521 return *text == '\0';
522 for ( ; *text; text++)
523 if ((CHAR)*text == c && strncmp(text, pat, len) == 0)
529 search_hist(CHAR *search, CHAR *(*move)(void))
531 static CHAR *old_search;
537 /* Save or get remembered search pattern. */
538 if (search && *search) {
541 old_search = (CHAR *)DBG_strdup((char *)search);
544 if (old_search == NULL || *old_search == '\0')
549 /* Set up pattern-finder. */
550 if (*search == '^') {
552 pat = (char *)(search + 1);
556 pat = (char *)search;
560 for (pos = H.Pos; (*move)() != NULL; )
561 if ((*match)((char *)H.Lines[H.Pos], pat, len) == 0)
562 return H.Lines[H.Pos];
570 static int Searching;
571 const char *old_prompt;
582 TTYputs((CHAR *)Prompt);
583 move = Repeat == NO_ARG ? prev_hist : next_hist;
584 p = search_hist(editinput(), move);
587 TTYputs((CHAR *)Prompt);
590 return do_insert_hist(p);
603 } while (++i < Repeat);
608 save_yank(int begin, int i)
618 if ((Yanked = NEW(CHAR, (size_t)i + 1)) != NULL) {
619 COPYFROMTO(Yanked, &Line[begin], i);
625 delete_string(int count)
630 if (count <= 0 || End == Point)
633 if (count == 1 && Point == End - 1) {
634 /* Optimize common case of delete at end of line. */
643 else if (rl_meta_chars && ISMETA(*p)) {
652 if (Point + count > End && (count = End - Point) <= 0)
656 save_yank(Point, count);
658 for (p = &Line[Point], i = End - (Point + count) + 1; --i >= 0; p++)
662 TTYstring(&Line[Point]);
676 } while (++i < Repeat);
691 } while (++i < Repeat);
693 return delete_string(i);
699 TTYputs((CHAR *)NEWLINE);
700 TTYputs((CHAR *)Prompt);
710 if (Repeat != NO_ARG) {
711 if (Repeat < Point) {
715 (void)delete_string(i - Point);
717 else if (Repeat > Point) {
719 (void)delete_string(Repeat - Point - 1);
724 save_yank(Point, End - Point);
740 if (Repeat == NO_ARG || Repeat < 2) {
743 return insert_string(buff);
746 if ((p = NEW(CHAR, Repeat + 1)) == NULL)
748 for (i = Repeat, q = p; --i >= 0; )
752 s = insert_string(p);
763 if ((c = TTYget()) == EOF)
765 /* Also include VT-100 arrows. */
766 if (c == '[' || c == 'O')
767 switch (c = TTYget()) {
768 default: return ring_bell();
769 case EOF: return CSeof;
770 case 'A': return h_prev();
771 case 'B': return h_next();
772 case 'C': return fd_char();
773 case 'D': return bk_char();
777 for (Repeat = c - '0'; (c = TTYget()) != EOF && isdigit(c); )
778 Repeat = Repeat * 10 + c - '0';
786 for (OldPoint = Point, kp = MetaMap; kp->Function; kp++)
788 return (*kp->Function)();
794 emacs(unsigned int c)
801 PushBack = UNMETA(c);
804 for (kp = Map; kp->Function; kp++)
807 s = kp->Function ? (*kp->Function)() : insert_char((int)c);
809 /* No pushback means no repeat count; hacky, but true. */
815 TTYspecial(unsigned int c)
820 if (c == rl_erase || c == DEL)
821 return bk_del_char();
830 if (c == rl_intr || c == rl_quit) {
835 if (c == rl_eof && Point == 0 && End == 0)
847 OldPoint = Point = Mark = End = 0;
850 while ((c = TTYget()) != EOF)
851 switch (TTYspecial(c)) {
884 if ((p = (CHAR *)DBG_strdup((char *)p)) == NULL)
886 if (H.Size < HIST_SIZE)
887 H.Lines[H.Size++] = p;
890 for (i = 0; i < HIST_SIZE - 1; i++)
891 H.Lines[i] = H.Lines[i + 1];
898 readline(const char *prompt)
904 if ((Line = NEW(CHAR, Length)) == NULL)
911 ScreenSize = SCREEN_INC;
912 Screen = NEW(char, ScreenSize);
913 Prompt = prompt ? prompt : (char *)NIL;
914 TTYputs((CHAR *)Prompt);
915 if ((line = editinput()) != NULL) {
916 line = (CHAR *)DBG_strdup((char *)line);
917 TTYputs((CHAR *)NEWLINE);
922 DISPOSE(H.Lines[--H.Size]);
929 if (p == NULL || *p == '\0')
932 #if defined(UNIQUE_HISTORY)
933 if (H.Pos && strcmp(p, H.Lines[H.Pos - 1]) == 0)
935 #endif /* defined(UNIQUE_HISTORY) */
953 return delete_string(Repeat == NO_ARG ? 1 : Repeat);
983 Line[Point - 1] = Line[Point];
984 TTYshow(Line[Point - 1]);
996 return (c = TTYget()) == EOF ? CSeof : insert_char((int)c);
1014 return delete_string(Mark - Point);
1029 if ((c = TTYget()) != CTL('X'))
1030 return c == EOF ? CSeof : ring_bell();
1032 if ((c = Mark) <= End) {
1043 if (Yanked && *Yanked)
1044 return insert_string(Yanked);
1055 save_yank(Mark, Point - Mark);
1057 save_yank(Point, Mark - Point);
1069 if ((c = TTYget()) == EOF)
1071 for (i = Point + 1, p = &Line[i]; i < End; i++, p++)
1082 return do_forward(CSmove);
1090 (void)do_forward(CSstay);
1091 if (OldPoint != Point) {
1092 i = Point - OldPoint;
1094 return delete_string(i);
1107 for (p = &Line[Point]; p > Line && !isalnum(p[-1]); p--)
1110 for (; p > Line && p[-1] != ' ' && isalnum(p[-1]); p--)
1115 } while (++i < Repeat);
1124 if (OldPoint != Point)
1125 return delete_string(OldPoint - Point);
1130 argify(CHAR *line, CHAR ***avp)
1139 if ((*avp = p = NEW(CHAR*, i))== NULL)
1142 for (c = line; isspace(*c); c++)
1144 if (*c == '\n' || *c == '\0')
1147 for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) {
1150 if (*c && *c != '\n') {
1152 new = NEW(CHAR*, i + MEM_INC);
1157 COPYFROMTO(new, p, i * sizeof (char **));
1181 if (H.Size == 1 || (p = H.Lines[H.Size - 2]) == NULL)
1184 if ((p = (CHAR *)DBG_strdup((char *)p)) == NULL)
1186 ac = argify(p, &av);
1188 if (Repeat != NO_ARG)
1189 s = Repeat < ac ? insert_string(av[Repeat]) : ring_bell();
1191 s = ac ? insert_string(av[ac - 1]) : CSstay;
1199 static KEYMAP Map[33] = {
1200 { CTL('@'), ring_bell },
1201 { CTL('A'), beg_line },
1202 { CTL('B'), bk_char },
1203 { CTL('D'), del_char },
1204 { CTL('E'), end_line },
1205 { CTL('F'), fd_char },
1206 { CTL('G'), ring_bell },
1207 { CTL('H'), bk_del_char },
1208 { CTL('I'), ring_bell },
1209 { CTL('J'), accept_line },
1210 { CTL('K'), kill_line },
1211 { CTL('L'), redisplay },
1212 { CTL('M'), accept_line },
1213 { CTL('N'), h_next },
1214 { CTL('O'), ring_bell },
1215 { CTL('P'), h_prev },
1216 { CTL('Q'), ring_bell },
1217 { CTL('R'), h_search },
1218 { CTL('S'), ring_bell },
1219 { CTL('T'), transpose },
1220 { CTL('U'), ring_bell },
1221 { CTL('V'), quote },
1223 { CTL('X'), exchange },
1225 { CTL('Z'), ring_bell },
1227 { CTL(']'), move_to_char },
1228 { CTL('^'), ring_bell },
1229 { CTL('_'), ring_bell },
1233 static KEYMAP MetaMap[16]= {
1234 { CTL('H'), bk_kill_word },
1235 { DEL, bk_kill_word },
1237 { '.', last_argument },
1242 { 'd', fd_kill_word },
1244 { 'l', case_down_word },
1245 { 'u', case_up_word },
1247 { 'w', copy_region },