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
33 #include <sys/ioctl.h>
34 #include <sys/types.h>
41 ** Manifest constants.
43 #define SCREEN_WIDTH 80
44 #define SCREEN_ROWS 24
47 #define CTL(x) ((x) & 0x1F)
48 #define ISCTL(x) ((x) && (x) < ' ')
49 #define UNCTL(x) ((x) + 64)
50 #define META(x) ((x) | 0x80)
51 #define ISMETA(x) ((x) & 0x80)
52 #define UNMETA(x) ((x) & 0x7F)
53 #if !defined(HIST_SIZE)
55 #endif /* !defined(HIST_SIZE) */
58 #define SCREEN_INC 256
60 #define DISPOSE(p) DBG_free((char *)(p))
62 ((T *)DBG_alloc((unsigned int)(sizeof (T) * (c))))
63 #define RENEW(p, T, c) \
64 (p = (T *)DBG_realloc((char *)(p), (unsigned int)(sizeof (T) * (c))))
65 #define COPYFROMTO(new, p, len) \
66 (void)memcpy((char *)(new), (char *)(p), (int)(len))
69 ** Command status codes.
71 typedef enum _STATUS {
72 CSdone, CSeof, CSmove, CSdispatch, CSstay
76 ** The type of case-changing to perform.
83 ** Key to command mapping.
85 typedef struct _KEYMAP {
91 ** Command history structure.
93 typedef struct _HISTORY {
96 CHAR *Lines[HIST_SIZE];
107 static CHAR NIL[] = "";
108 static const CHAR *Input = NIL;
110 static const char *Prompt;
113 static char NEWLINE[]= CRLF;
123 static KEYMAP Map[33];
124 static KEYMAP MetaMap[16];
125 static size_t Length;
126 static size_t ScreenCount;
127 static size_t ScreenSize;
128 static char *backspace;
132 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
133 int rl_meta_chars = 1;
138 static CHAR *editinput();
141 #if defined(USE_TERMCAP)
142 extern char *getenv();
143 extern char *tgetstr();
144 extern int tgetent();
145 #endif /* defined(USE_TERMCAP) */
148 ** TTY input/output functions.
151 #ifdef HAVE_TCGETATTR
157 static struct termios old;
161 (void)tcgetattr(0, &old);
162 rl_erase = old.c_cc[VERASE];
163 rl_kill = old.c_cc[VKILL];
164 rl_eof = old.c_cc[VEOF];
165 rl_intr = old.c_cc[VINTR];
166 rl_quit = old.c_cc[VQUIT];
169 new.c_cc[VINTR] = -1;
170 new.c_cc[VQUIT] = -1;
171 new.c_lflag &= ~(ECHO | ICANON);
172 new.c_iflag &= ~(ISTRIP | INPCK);
175 (void)tcsetattr(0, TCSANOW, &new);
178 (void)tcsetattr(0, TCSANOW, &old);
181 #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);
234 Screen[ScreenCount] = c;
235 if (++ScreenCount >= ScreenSize - 1) {
236 ScreenSize += SCREEN_INC;
237 RENEW(Screen, char, ScreenSize);
259 else if (rl_meta_chars && ISMETA(c)) {
289 while ( ( retv = read( 0, &c, (size_t)1 ) ) == -1 )
291 if ( errno != EINTR )
298 return retv == 1 ? c : EOF;
301 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
314 #if defined(USE_TERMCAP)
318 #endif /* defined(USE_TERMCAP) */
319 #if defined(TIOCGWINSZ)
321 #endif /* defined(TIOCGWINSZ) */
324 #if defined(TIOCGWINSZ)
325 /* Perhaps we got resized. */
326 if (ioctl(0, TIOCGWINSZ, &W) >= 0
327 && W.ws_col > 0 && W.ws_row > 0) {
328 TTYwidth = (int)W.ws_col;
329 TTYrows = (int)W.ws_row;
331 #endif /* defined(TIOCGWINSZ) */
336 TTYwidth = TTYrows = 0;
337 #if defined(USE_TERMCAP)
339 if ((term = getenv("TERM")) == NULL)
341 if (tgetent(buff, term) < 0) {
342 TTYwidth = SCREEN_WIDTH;
343 TTYrows = SCREEN_ROWS;
346 backspace = tgetstr("le", &bp);
347 TTYwidth = tgetnum("co");
348 TTYrows = tgetnum("li");
349 #endif /* defined(USE_TERMCAP) */
351 #if defined(TIOCGWINSZ)
352 if (ioctl(0, TIOCGWINSZ, &W) >= 0) {
353 TTYwidth = (int)W.ws_col;
354 TTYrows = (int)W.ws_row;
356 #endif /* defined(TIOCGWINSZ) */
358 if (TTYwidth <= 0 || TTYrows <= 0) {
359 TTYwidth = SCREEN_WIDTH;
360 TTYrows = SCREEN_ROWS;
373 TTYputs((CHAR *)Prompt);
374 for (i = Point, p = Line; --i >= 0; p++)
383 if (ISCTL(Line[Point - 1]))
385 else if (rl_meta_chars && ISMETA(Line[Point - 1])) {
390 if (Change == CSmove)
397 TTYshow(Line[Point]);
398 if (Change == CSmove)
411 do_macro(unsigned int c)
420 if ((Input = (CHAR *)getenv((char *)name)) == NULL) {
428 do_forward(STATUS move)
436 for ( ; Point < End && (*p == ' ' || !isalnum(*p)); Point++, p++)
440 for (; Point < End && isalnum(*p); Point++, p++)
446 } while (++i < Repeat);
459 (void)do_forward(CSstay);
460 if (OldPoint != Point) {
461 if ((count = Point - OldPoint) < 0)
464 if ((end = Point + count) > End)
466 for (i = Point, p = &Line[i]; i < end; i++, p++) {
467 if (type == TOupper) {
471 else if (isupper(*p))
482 return do_case(TOlower);
488 return do_case(TOupper);
498 for (extras = 0, i = Point, p = &Line[i]; i <= End; i++, p++) {
504 else if (rl_meta_chars && ISMETA(*p)) {
511 for (i += extras; i > Point; i--)
518 Point = -strlen(Prompt);
527 insert_string(CHAR *p)
534 len = strlen((char *)p);
535 if (End + len >= Length) {
536 if ((new = NEW(CHAR, Length + len + MEM_INC)) == NULL)
539 COPYFROMTO(new, Line, Length);
543 Length += len + MEM_INC;
546 for (q = &Line[Point], i = End - Point; --i >= 0; )
548 COPYFROMTO(&Line[Point], p, len);
551 TTYstring(&Line[Point]);
554 return Point == End ? CSstay : CSmove;
561 return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos];
567 return H.Pos == 0 ? NULL : H.Lines[--H.Pos];
571 do_insert_hist(CHAR *p)
579 return insert_string(p);
583 do_hist(CHAR *(*move)(void))
590 if ((p = (*move)()) == NULL)
592 } while (++i < Repeat);
593 return do_insert_hist(p);
599 return do_hist(next_hist);
605 return do_hist(prev_hist);
611 return do_insert_hist(H.Lines[H.Pos = 0]);
617 return do_insert_hist(H.Lines[H.Pos = H.Size - 1]);
621 ** Return zero if pat appears as a substring in text.
624 substrcmp(char *text, char *pat, int len)
628 if ((c = *pat) == '\0')
629 return *text == '\0';
630 for ( ; *text; text++)
631 if ((CHAR)*text == c && strncmp(text, pat, len) == 0)
637 search_hist(CHAR *search, CHAR *(*move)(void))
639 static CHAR *old_search;
645 /* Save or get remembered search pattern. */
646 if (search && *search) {
649 old_search = (CHAR *)DBG_strdup((char *)search);
652 if (old_search == NULL || *old_search == '\0')
657 /* Set up pattern-finder. */
658 if (*search == '^') {
660 pat = (char *)(search + 1);
664 pat = (char *)search;
668 for (pos = H.Pos; (*move)() != NULL; )
669 if ((*match)((char *)H.Lines[H.Pos], pat, len) == 0)
670 return H.Lines[H.Pos];
678 static int Searching;
679 const char *old_prompt;
690 TTYputs((CHAR *)Prompt);
691 move = Repeat == NO_ARG ? prev_hist : next_hist;
692 p = search_hist(editinput(), move);
695 TTYputs((CHAR *)Prompt);
698 return do_insert_hist(p);
711 } while (++i < Repeat);
716 save_yank(int begin, int i)
726 if ((Yanked = NEW(CHAR, (size_t)i + 1)) != NULL) {
727 COPYFROMTO(Yanked, &Line[begin], i);
733 delete_string(int count)
738 if (count <= 0 || End == Point)
741 if (count == 1 && Point == End - 1) {
742 /* Optimize common case of delete at end of line. */
751 else if (rl_meta_chars && ISMETA(*p)) {
760 if (Point + count > End && (count = End - Point) <= 0)
764 save_yank(Point, count);
766 for (p = &Line[Point], i = End - (Point + count) + 1; --i >= 0; p++)
770 TTYstring(&Line[Point]);
784 } while (++i < Repeat);
799 } while (++i < Repeat);
801 return delete_string(i);
807 TTYputs((CHAR *)NEWLINE);
808 TTYputs((CHAR *)Prompt);
818 if (Repeat != NO_ARG) {
819 if (Repeat < Point) {
823 (void)delete_string(i - Point);
825 else if (Repeat > Point) {
827 (void)delete_string(Repeat - Point - 1);
832 save_yank(Point, End - Point);
848 if (Repeat == NO_ARG || Repeat < 2) {
851 return insert_string(buff);
854 if ((p = NEW(CHAR, Repeat + 1)) == NULL)
856 for (i = Repeat, q = p; --i >= 0; )
860 s = insert_string(p);
871 if ((c = TTYget()) == EOF)
873 /* Also include VT-100 arrows. */
874 if (c == '[' || c == 'O')
875 switch (c = TTYget()) {
876 default: return ring_bell();
877 case EOF: return CSeof;
878 case 'A': return h_prev();
879 case 'B': return h_next();
880 case 'C': return fd_char();
881 case 'D': return bk_char();
885 for (Repeat = c - '0'; (c = TTYget()) != EOF && isdigit(c); )
886 Repeat = Repeat * 10 + c - '0';
894 for (OldPoint = Point, kp = MetaMap; kp->Function; kp++)
896 return (*kp->Function)();
902 emacs(unsigned int c)
909 PushBack = UNMETA(c);
912 for (kp = Map; kp->Function; kp++)
915 s = kp->Function ? (*kp->Function)() : insert_char((int)c);
917 /* No pushback means no repeat count; hacky, but true. */
923 TTYspecial(unsigned int c)
928 if (c == rl_erase || c == DEL)
929 return bk_del_char();
938 if (c == rl_intr || c == rl_quit) {
943 if (c == rl_eof && Point == 0 && End == 0)
955 OldPoint = Point = Mark = End = 0;
958 while ((c = TTYget()) != EOF)
959 switch (TTYspecial(c)) {
992 if ((p = (CHAR *)DBG_strdup((char *)p)) == NULL)
994 if (H.Size < HIST_SIZE)
995 H.Lines[H.Size++] = p;
998 for (i = 0; i < HIST_SIZE - 1; i++)
999 H.Lines[i] = H.Lines[i + 1];
1006 readline(const char *prompt)
1012 if ((Line = NEW(CHAR, Length)) == NULL)
1019 ScreenSize = SCREEN_INC;
1020 Screen = NEW(char, ScreenSize);
1021 Prompt = prompt ? prompt : (char *)NIL;
1022 TTYputs((CHAR *)Prompt);
1023 if ((line = editinput()) != NULL) {
1024 line = (CHAR *)DBG_strdup((char *)line);
1025 TTYputs((CHAR *)NEWLINE);
1030 DISPOSE(H.Lines[--H.Size]);
1031 return (char *)line;
1035 add_history(char *p)
1037 if (p == NULL || *p == '\0')
1040 #if defined(UNIQUE_HISTORY)
1041 if (H.Pos && strcmp(p, H.Lines[H.Pos - 1]) == 0)
1043 #endif /* defined(UNIQUE_HISTORY) */
1044 hist_add((CHAR *)p);
1061 return delete_string(Repeat == NO_ARG ? 1 : Repeat);
1089 c = Line[Point - 1];
1091 Line[Point - 1] = Line[Point];
1092 TTYshow(Line[Point - 1]);
1104 return (c = TTYget()) == EOF ? CSeof : insert_char((int)c);
1122 return delete_string(Mark - Point);
1137 if ((c = TTYget()) != CTL('X'))
1138 return c == EOF ? CSeof : ring_bell();
1140 if ((c = Mark) <= End) {
1151 if (Yanked && *Yanked)
1152 return insert_string(Yanked);
1163 save_yank(Mark, Point - Mark);
1165 save_yank(Point, Mark - Point);
1177 if ((c = TTYget()) == EOF)
1179 for (i = Point + 1, p = &Line[i]; i < End; i++, p++)
1190 return do_forward(CSmove);
1198 (void)do_forward(CSstay);
1199 if (OldPoint != Point) {
1200 i = Point - OldPoint;
1202 return delete_string(i);
1215 for (p = &Line[Point]; p > Line && !isalnum(p[-1]); p--)
1218 for (; p > Line && p[-1] != ' ' && isalnum(p[-1]); p--)
1223 } while (++i < Repeat);
1232 if (OldPoint != Point)
1233 return delete_string(OldPoint - Point);
1238 argify(CHAR *line, CHAR ***avp)
1247 if ((*avp = p = NEW(CHAR*, i))== NULL)
1250 for (c = line; isspace(*c); c++)
1252 if (*c == '\n' || *c == '\0')
1255 for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) {
1258 if (*c && *c != '\n') {
1260 new = NEW(CHAR*, i + MEM_INC);
1265 COPYFROMTO(new, p, i * sizeof (char **));
1289 if (H.Size == 1 || (p = H.Lines[H.Size - 2]) == NULL)
1292 if ((p = (CHAR *)DBG_strdup((char *)p)) == NULL)
1294 ac = argify(p, &av);
1296 if (Repeat != NO_ARG)
1297 s = Repeat < ac ? insert_string(av[Repeat]) : ring_bell();
1299 s = ac ? insert_string(av[ac - 1]) : CSstay;
1307 static KEYMAP Map[33] = {
1308 { CTL('@'), ring_bell },
1309 { CTL('A'), beg_line },
1310 { CTL('B'), bk_char },
1311 { CTL('D'), del_char },
1312 { CTL('E'), end_line },
1313 { CTL('F'), fd_char },
1314 { CTL('G'), ring_bell },
1315 { CTL('H'), bk_del_char },
1316 { CTL('I'), ring_bell },
1317 { CTL('J'), accept_line },
1318 { CTL('K'), kill_line },
1319 { CTL('L'), redisplay },
1320 { CTL('M'), accept_line },
1321 { CTL('N'), h_next },
1322 { CTL('O'), ring_bell },
1323 { CTL('P'), h_prev },
1324 { CTL('Q'), ring_bell },
1325 { CTL('R'), h_search },
1326 { CTL('S'), ring_bell },
1327 { CTL('T'), transpose },
1328 { CTL('U'), ring_bell },
1329 { CTL('V'), quote },
1331 { CTL('X'), exchange },
1333 { CTL('Z'), ring_bell },
1335 { CTL(']'), move_to_char },
1336 { CTL('^'), ring_bell },
1337 { CTL('_'), ring_bell },
1341 static KEYMAP MetaMap[16]= {
1342 { CTL('H'), bk_kill_word },
1343 { DEL, bk_kill_word },
1345 { '.', last_argument },
1350 { 'd', fd_kill_word },
1352 { 'l', case_down_word },
1353 { 'u', case_up_word },
1355 { 'w', copy_region },