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
32 #include <sys/ioctl.h>
33 #include <sys/types.h>
36 typedef unsigned char CHAR;
39 ** Manifest constants.
41 #define SCREEN_WIDTH 80
42 #define SCREEN_ROWS 24
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)
53 #endif /* !defined(HIST_SIZE) */
56 #define SCREEN_INC 256
58 #define DISPOSE(p) free((char *)(p))
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
74 ** The type of case-changing to perform.
81 ** Key to command mapping.
83 typedef struct _KEYMAP {
89 ** Command history structure.
91 typedef struct _HISTORY {
94 CHAR *Lines[HIST_SIZE];
105 static CHAR NIL[] = "";
106 static const CHAR *Input = NIL;
108 static const char *Prompt;
111 static char NEWLINE[]= CRLF;
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;
130 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
131 int rl_meta_chars = 1;
136 static CHAR *editinput();
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
156 static struct termios old;
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];
168 new.c_cc[VINTR] = -1;
169 new.c_cc[VQUIT] = -1;
170 new.c_lflag &= ~(ECHO | ICANON);
171 new.c_iflag &= ~(ISTRIP | INPCK);
174 (void)tcsetattr(0, TCSANOW, &new);
177 (void)tcsetattr(0, TCSANOW, &old);
180 #else /* HAVE_TCGETATTR */
186 static struct sgttyb old_sgttyb;
187 static struct tchars old_tchars;
188 struct sgttyb new_sgttyb;
189 struct tchars new_tchars;
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;
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);
215 (void)ioctl(0, TIOCSETP, &old_sgttyb);
216 (void)ioctl(0, TIOCSETC, &old_tchars);
220 #endif /* HAVE_TCGETATTR */
226 (void)write(1, Screen, ScreenCount);
235 Screen[ScreenCount] = c;
236 if (++ScreenCount >= ScreenSize - 1) {
237 ScreenSize += SCREEN_INC;
238 RENEW(Screen, char, ScreenSize);
262 else if (rl_meta_chars && ISMETA(c)) {
293 while ( ( retv = read( 0, &c, (size_t)1 ) ) == -1 )
295 if ( errno != EINTR )
299 return retv == 1 ? c : EOF;
302 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
316 #if defined(USE_TERMCAP)
320 #endif /* defined(USE_TERMCAP) */
321 #if defined(TIOCGWINSZ)
323 #endif /* defined(TIOCGWINSZ) */
326 #if defined(TIOCGWINSZ)
327 /* Perhaps we got resized. */
328 if (ioctl(0, TIOCGWINSZ, &W) >= 0
329 && W.ws_col > 0 && W.ws_row > 0) {
330 TTYwidth = (int)W.ws_col;
331 TTYrows = (int)W.ws_row;
333 #endif /* defined(TIOCGWINSZ) */
338 TTYwidth = TTYrows = 0;
339 #if defined(USE_TERMCAP)
341 if ((term = getenv("TERM")) == NULL)
343 if (tgetent(buff, term) < 0) {
344 TTYwidth = SCREEN_WIDTH;
345 TTYrows = SCREEN_ROWS;
348 backspace = tgetstr("le", &bp);
349 TTYwidth = tgetnum("co");
350 TTYrows = tgetnum("li");
351 #endif /* defined(USE_TERMCAP) */
353 #if defined(TIOCGWINSZ)
354 if (ioctl(0, TIOCGWINSZ, &W) >= 0) {
355 TTYwidth = (int)W.ws_col;
356 TTYrows = (int)W.ws_row;
358 #endif /* defined(TIOCGWINSZ) */
360 if (TTYwidth <= 0 || TTYrows <= 0) {
361 TTYwidth = SCREEN_WIDTH;
362 TTYrows = SCREEN_ROWS;
375 TTYputs((CHAR *)Prompt);
376 for (i = Point, p = Line; --i >= 0; p++)
386 if (ISCTL(Line[Point - 1]))
388 else if (rl_meta_chars && ISMETA(Line[Point - 1])) {
393 if (Change == CSmove)
401 TTYshow(Line[Point]);
402 if (Change == CSmove)
425 if ((Input = (CHAR *)getenv((char *)name)) == NULL) {
442 for ( ; Point < End && (*p == ' ' || !isalnum(*p)); Point++, p++)
446 for (; Point < End && isalnum(*p); Point++, p++)
452 } while (++i < Repeat);
466 (void)do_forward(CSstay);
467 if (OldPoint != Point) {
468 if ((count = Point - OldPoint) < 0)
471 if ((end = Point + count) > End)
473 for (i = Point, p = &Line[i]; i < end; i++, p++) {
474 if (type == TOupper) {
478 else if (isupper(*p))
489 return do_case(TOlower);
495 return do_case(TOupper);
505 for (extras = 0, i = Point, p = &Line[i]; i <= End; i++, p++) {
511 else if (rl_meta_chars && ISMETA(*p)) {
518 for (i += extras; i > Point; i--)
525 Point = -strlen(Prompt);
542 len = strlen((char *)p);
543 if (End + len >= Length) {
544 if ((new = NEW(CHAR, Length + len + MEM_INC)) == NULL)
547 COPYFROMTO(new, Line, Length);
551 Length += len + MEM_INC;
554 for (q = &Line[Point], i = End - Point; --i >= 0; )
556 COPYFROMTO(&Line[Point], p, len);
559 TTYstring(&Line[Point]);
562 return Point == End ? CSstay : CSmove;
569 return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos];
575 return H.Pos == 0 ? NULL : H.Lines[--H.Pos];
588 return insert_string(p);
600 if ((p = (*move)()) == NULL)
602 } while (++i < Repeat);
603 return do_insert_hist(p);
609 return do_hist(next_hist);
615 return do_hist(prev_hist);
621 return do_insert_hist(H.Lines[H.Pos = 0]);
627 return do_insert_hist(H.Lines[H.Pos = H.Size - 1]);
631 ** Return zero if pat appears as a substring in text.
634 substrcmp(text, pat, len)
641 if ((c = *pat) == '\0')
642 return *text == '\0';
643 for ( ; *text; text++)
644 if ((CHAR)*text == c && strncmp(text, pat, len) == 0)
650 search_hist(search, move)
654 static CHAR *old_search;
660 /* Save or get remembered search pattern. */
661 if (search && *search) {
664 old_search = (CHAR *)strdup((char *)search);
667 if (old_search == NULL || *old_search == '\0')
672 /* Set up pattern-finder. */
673 if (*search == '^') {
675 pat = (char *)(search + 1);
679 pat = (char *)search;
683 for (pos = H.Pos; (*move)() != NULL; )
684 if ((*match)((char *)H.Lines[H.Pos], pat, len) == 0)
685 return H.Lines[H.Pos];
693 static int Searching;
694 const char *old_prompt;
705 TTYputs((CHAR *)Prompt);
706 move = Repeat == NO_ARG ? prev_hist : next_hist;
707 p = search_hist(editinput(), move);
710 TTYputs((CHAR *)Prompt);
713 return do_insert_hist(p);
726 } while (++i < Repeat);
743 if ((Yanked = NEW(CHAR, (size_t)i + 1)) != NULL) {
744 COPYFROMTO(Yanked, &Line[begin], i);
756 if (count <= 0 || End == Point)
759 if (count == 1 && Point == End - 1) {
760 /* Optimize common case of delete at end of line. */
769 else if (rl_meta_chars && ISMETA(*p)) {
778 if (Point + count > End && (count = End - Point) <= 0)
782 save_yank(Point, count);
784 for (p = &Line[Point], i = End - (Point + count) + 1; --i >= 0; p++)
788 TTYstring(&Line[Point]);
802 } while (++i < Repeat);
817 } while (++i < Repeat);
819 return delete_string(i);
825 TTYputs((CHAR *)NEWLINE);
826 TTYputs((CHAR *)Prompt);
836 if (Repeat != NO_ARG) {
837 if (Repeat < Point) {
841 (void)delete_string(i - Point);
843 else if (Repeat > Point) {
845 (void)delete_string(Repeat - Point - 1);
850 save_yank(Point, End - Point);
867 if (Repeat == NO_ARG || Repeat < 2) {
870 return insert_string(buff);
873 if ((p = NEW(CHAR, Repeat + 1)) == NULL)
875 for (i = Repeat, q = p; --i >= 0; )
879 s = insert_string(p);
890 if ((c = TTYget()) == EOF)
892 /* Also include VT-100 arrows. */
893 if (c == '[' || c == 'O')
894 switch (c = TTYget()) {
895 default: return ring_bell();
896 case EOF: return CSeof;
897 case 'A': return h_prev();
898 case 'B': return h_next();
899 case 'C': return fd_char();
900 case 'D': return bk_char();
904 for (Repeat = c - '0'; (c = TTYget()) != EOF && isdigit(c); )
905 Repeat = Repeat * 10 + c - '0';
913 for (OldPoint = Point, kp = MetaMap; kp->Function; kp++)
915 return (*kp->Function)();
929 PushBack = UNMETA(c);
932 for (kp = Map; kp->Function; kp++)
935 s = kp->Function ? (*kp->Function)() : insert_char((int)c);
937 /* No pushback means no repeat count; hacky, but true. */
949 if (c == rl_erase || c == DEL)
950 return bk_del_char();
959 if (c == rl_intr || c == rl_quit) {
964 if (c == rl_eof && Point == 0 && End == 0)
976 OldPoint = Point = Mark = End = 0;
979 while ((c = TTYget()) != EOF)
980 switch (TTYspecial(c)) {
1014 if ((p = (CHAR *)strdup((char *)p)) == NULL)
1016 if (H.Size < HIST_SIZE)
1017 H.Lines[H.Size++] = p;
1019 DISPOSE(H.Lines[0]);
1020 for (i = 0; i < HIST_SIZE - 1; i++)
1021 H.Lines[i] = H.Lines[i + 1];
1035 if ((Line = NEW(CHAR, Length)) == NULL)
1042 ScreenSize = SCREEN_INC;
1043 Screen = NEW(char, ScreenSize);
1044 Prompt = prompt ? prompt : (char *)NIL;
1045 TTYputs((CHAR *)Prompt);
1046 if ((line = editinput()) != NULL) {
1047 line = (CHAR *)strdup((char *)line);
1048 TTYputs((CHAR *)NEWLINE);
1053 DISPOSE(H.Lines[--H.Size]);
1054 return (char *)line;
1061 if (p == NULL || *p == '\0')
1064 #if defined(UNIQUE_HISTORY)
1065 if (H.Pos && strcmp(p, H.Lines[H.Pos - 1]) == 0)
1067 #endif /* defined(UNIQUE_HISTORY) */
1068 hist_add((CHAR *)p);
1085 return delete_string(Repeat == NO_ARG ? 1 : Repeat);
1113 c = Line[Point - 1];
1115 Line[Point - 1] = Line[Point];
1116 TTYshow(Line[Point - 1]);
1128 return (c = TTYget()) == EOF ? CSeof : insert_char((int)c);
1146 return delete_string(Mark - Point);
1161 if ((c = TTYget()) != CTL('X'))
1162 return c == EOF ? CSeof : ring_bell();
1164 if ((c = Mark) <= End) {
1175 if (Yanked && *Yanked)
1176 return insert_string(Yanked);
1187 save_yank(Mark, Point - Mark);
1189 save_yank(Point, Mark - Point);
1201 if ((c = TTYget()) == EOF)
1203 for (i = Point + 1, p = &Line[i]; i < End; i++, p++)
1214 return do_forward(CSmove);
1222 (void)do_forward(CSstay);
1223 if (OldPoint != Point) {
1224 i = Point - OldPoint;
1226 return delete_string(i);
1239 for (p = &Line[Point]; p > Line && !isalnum(p[-1]); p--)
1242 for (; p > Line && p[-1] != ' ' && isalnum(p[-1]); p--)
1247 } while (++i < Repeat);
1256 if (OldPoint != Point)
1257 return delete_string(OldPoint - Point);
1273 if ((*avp = p = NEW(CHAR*, i))== NULL)
1276 for (c = line; isspace(*c); c++)
1278 if (*c == '\n' || *c == '\0')
1281 for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) {
1284 if (*c && *c != '\n') {
1286 new = NEW(CHAR*, i + MEM_INC);
1291 COPYFROMTO(new, p, i * sizeof (char **));
1315 if (H.Size == 1 || (p = H.Lines[H.Size - 2]) == NULL)
1318 if ((p = (CHAR *)strdup((char *)p)) == NULL)
1320 ac = argify(p, &av);
1322 if (Repeat != NO_ARG)
1323 s = Repeat < ac ? insert_string(av[Repeat]) : ring_bell();
1325 s = ac ? insert_string(av[ac - 1]) : CSstay;
1333 static KEYMAP Map[33] = {
1334 { CTL('@'), ring_bell },
1335 { CTL('A'), beg_line },
1336 { CTL('B'), bk_char },
1337 { CTL('D'), del_char },
1338 { CTL('E'), end_line },
1339 { CTL('F'), fd_char },
1340 { CTL('G'), ring_bell },
1341 { CTL('H'), bk_del_char },
1342 { CTL('I'), ring_bell },
1343 { CTL('J'), accept_line },
1344 { CTL('K'), kill_line },
1345 { CTL('L'), redisplay },
1346 { CTL('M'), accept_line },
1347 { CTL('N'), h_next },
1348 { CTL('O'), ring_bell },
1349 { CTL('P'), h_prev },
1350 { CTL('Q'), ring_bell },
1351 { CTL('R'), h_search },
1352 { CTL('S'), ring_bell },
1353 { CTL('T'), transpose },
1354 { CTL('U'), ring_bell },
1355 { CTL('V'), quote },
1357 { CTL('X'), exchange },
1359 { CTL('Z'), ring_bell },
1361 { CTL(']'), move_to_char },
1362 { CTL('^'), ring_bell },
1363 { CTL('_'), ring_bell },
1367 static KEYMAP MetaMap[16]= {
1368 { CTL('H'), bk_kill_word },
1369 { DEL, bk_kill_word },
1371 { '.', last_argument },
1376 { 'd', fd_kill_word },
1378 { 'l', case_down_word },
1379 { 'u', case_up_word },
1381 { 'w', copy_region },