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
37 ** Manifest constants.
39 #define SCREEN_WIDTH 80
40 #define SCREEN_ROWS 24
43 #define CTL(x) ((x) & 0x1F)
44 #define ISCTL(x) ((x) && (x) < ' ')
45 #define UNCTL(x) ((x) + 64)
46 #define META(x) ((x) | 0x80)
47 #define ISMETA(x) ((x) & 0x80)
48 #define UNMETA(x) ((x) & 0x7F)
49 #if !defined(HIST_SIZE)
51 #endif /* !defined(HIST_SIZE) */
54 #define SCREEN_INC 256
56 #define DISPOSE(p) DBG_free((char *)(p))
58 ((T *)DBG_alloc((unsigned int)(sizeof (T) * (c))))
59 #define RENEW(p, T, c) \
60 (p = (T *)DBG_realloc((char *)(p), (unsigned int)(sizeof (T) * (c))))
61 #define COPYFROMTO(new, p, len) \
62 (void)memcpy((char *)(new), (char *)(p), (int)(len))
65 ** Command status codes.
67 typedef enum _STATUS {
68 CSdone, CSeof, CSmove, CSdispatch, CSstay
72 ** The type of case-changing to perform.
79 ** Key to command mapping.
81 typedef struct _KEYMAP {
87 ** Command history structure.
89 typedef struct _HISTORY {
92 CHAR *Lines[HIST_SIZE];
103 static CHAR NIL[] = "";
104 static const CHAR *Input = NIL;
106 static const char *Prompt;
109 static char NEWLINE[]= CRLF;
119 static KEYMAP Map[33];
120 static KEYMAP MetaMap[16];
121 static size_t Length;
122 static size_t ScreenCount;
123 static size_t ScreenSize;
124 static char *backspace;
128 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
129 int rl_meta_chars = 1;
134 static CHAR *editinput();
137 ** TTY input/output functions.
143 static DWORD old_mode;
146 GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &old_mode);
147 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), 0 /*ENABLE_PROCESSED_INPUT|ENABLE_WINDOW_INPUT|ENABLE_MOUSE_INPUT*/);
149 SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), old_mode);
157 DEBUG_Output(DBG_CHN_MESG, Screen, ScreenCount);
165 Screen[ScreenCount] = c;
166 if (++ScreenCount >= ScreenSize - 1) {
167 ScreenSize += SCREEN_INC;
168 RENEW(Screen, char, ScreenSize);
190 else if (rl_meta_chars && ISMETA(c)) {
221 /* data available ? */
222 if (ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &c, 1, &retv, NULL) &&
225 switch (WaitForSingleObject(GetStdHandle(STD_INPUT_HANDLE), INFINITE)) {
229 DEBUG_Printf(DBG_CHN_FIXME, "shouldn't happen\n");
238 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
250 COORD c = GetLargestConsoleWindowSize(GetStdHandle(STD_INPUT_HANDLE));
264 TTYputs((CHAR *)Prompt);
265 for (i = Point, p = Line; --i >= 0; p++)
274 if (ISCTL(Line[Point - 1]))
276 else if (rl_meta_chars && ISMETA(Line[Point - 1])) {
281 if (Change == CSmove)
288 TTYshow(Line[Point]);
289 if (Change == CSmove)
302 do_macro(unsigned int c)
311 if ((Input = (CHAR *)getenv((char *)name)) == NULL) {
319 do_forward(STATUS move)
327 for ( ; Point < End && (*p == ' ' || !isalnum(*p)); Point++, p++)
331 for (; Point < End && isalnum(*p); Point++, p++)
337 } while (++i < Repeat);
350 (void)do_forward(CSstay);
351 if (OldPoint != Point) {
352 if ((count = Point - OldPoint) < 0)
355 if ((end = Point + count) > End)
357 for (i = Point, p = &Line[i]; i < end; i++, p++) {
358 if (type == TOupper) {
362 else if (isupper(*p))
373 return do_case(TOlower);
379 return do_case(TOupper);
389 for (extras = 0, i = Point, p = &Line[i]; i <= End; i++, p++) {
395 else if (rl_meta_chars && ISMETA(*p)) {
402 for (i += extras; i > Point; i--)
409 Point = -strlen(Prompt);
418 insert_string(CHAR *p)
425 len = strlen((char *)p);
426 if (End + len >= Length) {
427 if ((new = NEW(CHAR, Length + len + MEM_INC)) == NULL)
430 COPYFROMTO(new, Line, Length);
434 Length += len + MEM_INC;
437 for (q = &Line[Point], i = End - Point; --i >= 0; )
439 COPYFROMTO(&Line[Point], p, len);
442 TTYstring(&Line[Point]);
445 return Point == End ? CSstay : CSmove;
452 return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos];
458 return H.Pos == 0 ? NULL : H.Lines[--H.Pos];
462 do_insert_hist(CHAR *p)
470 return insert_string(p);
474 do_hist(CHAR *(*move)(void))
481 if ((p = (*move)()) == NULL)
483 } while (++i < Repeat);
484 return do_insert_hist(p);
490 return do_hist(next_hist);
496 return do_hist(prev_hist);
502 return do_insert_hist(H.Lines[H.Pos = 0]);
508 return do_insert_hist(H.Lines[H.Pos = H.Size - 1]);
512 ** Return zero if pat appears as a substring in text.
515 substrcmp(char *text, char *pat, int len)
519 if ((c = *pat) == '\0')
520 return *text == '\0';
521 for ( ; *text; text++)
522 if ((CHAR)*text == c && strncmp(text, pat, len) == 0)
528 search_hist(CHAR *search, CHAR *(*move)(void))
530 static CHAR *old_search;
536 /* Save or get remembered search pattern. */
537 if (search && *search) {
540 old_search = (CHAR *)DBG_strdup((char *)search);
543 if (old_search == NULL || *old_search == '\0')
548 /* Set up pattern-finder. */
549 if (*search == '^') {
551 pat = (char *)(search + 1);
555 pat = (char *)search;
559 for (pos = H.Pos; (*move)() != NULL; )
560 if ((*match)((char *)H.Lines[H.Pos], pat, len) == 0)
561 return H.Lines[H.Pos];
569 static int Searching;
570 const char *old_prompt;
581 TTYputs((CHAR *)Prompt);
582 move = Repeat == NO_ARG ? prev_hist : next_hist;
583 p = search_hist(editinput(), move);
586 TTYputs((CHAR *)Prompt);
589 return do_insert_hist(p);
602 } while (++i < Repeat);
607 save_yank(int begin, int i)
617 if ((Yanked = NEW(CHAR, (size_t)i + 1)) != NULL) {
618 COPYFROMTO(Yanked, &Line[begin], i);
624 delete_string(int count)
629 if (count <= 0 || End == Point)
632 if (count == 1 && Point == End - 1) {
633 /* Optimize common case of delete at end of line. */
642 else if (rl_meta_chars && ISMETA(*p)) {
651 if (Point + count > End && (count = End - Point) <= 0)
655 save_yank(Point, count);
657 for (p = &Line[Point], i = End - (Point + count) + 1; --i >= 0; p++)
661 TTYstring(&Line[Point]);
675 } while (++i < Repeat);
690 } while (++i < Repeat);
692 return delete_string(i);
698 TTYputs((CHAR *)NEWLINE);
699 TTYputs((CHAR *)Prompt);
709 if (Repeat != NO_ARG) {
710 if (Repeat < Point) {
714 (void)delete_string(i - Point);
716 else if (Repeat > Point) {
718 (void)delete_string(Repeat - Point - 1);
723 save_yank(Point, End - Point);
739 if (Repeat == NO_ARG || Repeat < 2) {
742 return insert_string(buff);
745 if ((p = NEW(CHAR, Repeat + 1)) == NULL)
747 for (i = Repeat, q = p; --i >= 0; )
751 s = insert_string(p);
762 if ((c = TTYget()) == EOF)
764 /* Also include VT-100 arrows. */
765 if (c == '[' || c == 'O')
766 switch (c = TTYget()) {
767 default: return ring_bell();
768 case EOF: return CSeof;
769 case 'A': return h_prev();
770 case 'B': return h_next();
771 case 'C': return fd_char();
772 case 'D': return bk_char();
776 for (Repeat = c - '0'; (c = TTYget()) != EOF && isdigit(c); )
777 Repeat = Repeat * 10 + c - '0';
785 for (OldPoint = Point, kp = MetaMap; kp->Function; kp++)
787 return (*kp->Function)();
793 emacs(unsigned int c)
800 PushBack = UNMETA(c);
803 for (kp = Map; kp->Function; kp++)
806 s = kp->Function ? (*kp->Function)() : insert_char((int)c);
808 /* No pushback means no repeat count; hacky, but true. */
814 TTYspecial(unsigned int c)
819 if (c == rl_erase || c == DEL)
820 return bk_del_char();
829 if (c == rl_intr || c == rl_quit) {
834 if (c == rl_eof && Point == 0 && End == 0)
846 OldPoint = Point = Mark = End = 0;
849 while ((c = TTYget()) != EOF)
850 switch (TTYspecial(c)) {
883 if ((p = (CHAR *)DBG_strdup((char *)p)) == NULL)
885 if (H.Size < HIST_SIZE)
886 H.Lines[H.Size++] = p;
889 for (i = 0; i < HIST_SIZE - 1; i++)
890 H.Lines[i] = H.Lines[i + 1];
897 readline(const char *prompt)
903 if ((Line = NEW(CHAR, Length)) == NULL)
910 ScreenSize = SCREEN_INC;
911 Screen = NEW(char, ScreenSize);
912 Prompt = prompt ? prompt : (char *)NIL;
913 TTYputs((CHAR *)Prompt);
914 if ((line = editinput()) != NULL) {
915 line = (CHAR *)DBG_strdup((char *)line);
916 TTYputs((CHAR *)NEWLINE);
921 DISPOSE(H.Lines[--H.Size]);
928 if (p == NULL || *p == '\0')
931 #if defined(UNIQUE_HISTORY)
932 if (H.Pos && strcmp(p, H.Lines[H.Pos - 1]) == 0)
934 #endif /* defined(UNIQUE_HISTORY) */
952 return delete_string(Repeat == NO_ARG ? 1 : Repeat);
982 Line[Point - 1] = Line[Point];
983 TTYshow(Line[Point - 1]);
995 return (c = TTYget()) == EOF ? CSeof : insert_char((int)c);
1013 return delete_string(Mark - Point);
1028 if ((c = TTYget()) != CTL('X'))
1029 return c == EOF ? CSeof : ring_bell();
1031 if ((c = Mark) <= End) {
1042 if (Yanked && *Yanked)
1043 return insert_string(Yanked);
1054 save_yank(Mark, Point - Mark);
1056 save_yank(Point, Mark - Point);
1068 if ((c = TTYget()) == EOF)
1070 for (i = Point + 1, p = &Line[i]; i < End; i++, p++)
1081 return do_forward(CSmove);
1089 (void)do_forward(CSstay);
1090 if (OldPoint != Point) {
1091 i = Point - OldPoint;
1093 return delete_string(i);
1106 for (p = &Line[Point]; p > Line && !isalnum(p[-1]); p--)
1109 for (; p > Line && p[-1] != ' ' && isalnum(p[-1]); p--)
1114 } while (++i < Repeat);
1123 if (OldPoint != Point)
1124 return delete_string(OldPoint - Point);
1129 argify(CHAR *line, CHAR ***avp)
1138 if ((*avp = p = NEW(CHAR*, i))== NULL)
1141 for (c = line; isspace(*c); c++)
1143 if (*c == '\n' || *c == '\0')
1146 for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) {
1149 if (*c && *c != '\n') {
1151 new = NEW(CHAR*, i + MEM_INC);
1156 COPYFROMTO(new, p, i * sizeof (char **));
1180 if (H.Size == 1 || (p = H.Lines[H.Size - 2]) == NULL)
1183 if ((p = (CHAR *)DBG_strdup((char *)p)) == NULL)
1185 ac = argify(p, &av);
1187 if (Repeat != NO_ARG)
1188 s = Repeat < ac ? insert_string(av[Repeat]) : ring_bell();
1190 s = ac ? insert_string(av[ac - 1]) : CSstay;
1198 static KEYMAP Map[33] = {
1199 { CTL('@'), ring_bell },
1200 { CTL('A'), beg_line },
1201 { CTL('B'), bk_char },
1202 { CTL('D'), del_char },
1203 { CTL('E'), end_line },
1204 { CTL('F'), fd_char },
1205 { CTL('G'), ring_bell },
1206 { CTL('H'), bk_del_char },
1207 { CTL('I'), ring_bell },
1208 { CTL('J'), accept_line },
1209 { CTL('K'), kill_line },
1210 { CTL('L'), redisplay },
1211 { CTL('M'), accept_line },
1212 { CTL('N'), h_next },
1213 { CTL('O'), ring_bell },
1214 { CTL('P'), h_prev },
1215 { CTL('Q'), ring_bell },
1216 { CTL('R'), h_search },
1217 { CTL('S'), ring_bell },
1218 { CTL('T'), transpose },
1219 { CTL('U'), ring_bell },
1220 { CTL('V'), quote },
1222 { CTL('X'), exchange },
1224 { CTL('Z'), ring_bell },
1226 { CTL(']'), move_to_char },
1227 { CTL('^'), ring_bell },
1228 { CTL('_'), ring_bell },
1232 static KEYMAP MetaMap[16]= {
1233 { CTL('H'), bk_kill_word },
1234 { DEL, bk_kill_word },
1236 { '.', last_argument },
1241 { 'd', fd_kill_word },
1243 { 'l', case_down_word },
1244 { 'u', case_up_word },
1246 { 'w', copy_region },