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>
40 ** Manifest constants.
42 #define SCREEN_WIDTH 80
43 #define SCREEN_ROWS 24
46 #define CTL(x) ((x) & 0x1F)
47 #define ISCTL(x) ((x) && (x) < ' ')
48 #define UNCTL(x) ((x) + 64)
49 #define META(x) ((x) | 0x80)
50 #define ISMETA(x) ((x) & 0x80)
51 #define UNMETA(x) ((x) & 0x7F)
52 #if !defined(HIST_SIZE)
54 #endif /* !defined(HIST_SIZE) */
57 #define SCREEN_INC 256
59 #define DISPOSE(p) DBG_free((char *)(p))
61 ((T *)DBG_alloc((unsigned int)(sizeof (T) * (c))))
62 #define RENEW(p, T, c) \
63 (p = (T *)DBG_realloc((char *)(p), (unsigned int)(sizeof (T) * (c))))
64 #define COPYFROMTO(new, p, len) \
65 (void)memcpy((char *)(new), (char *)(p), (int)(len))
68 ** Command status codes.
70 typedef enum _STATUS {
71 CSdone, CSeof, CSmove, CSdispatch, CSstay
75 ** The type of case-changing to perform.
82 ** Key to command mapping.
84 typedef struct _KEYMAP {
90 ** Command history structure.
92 typedef struct _HISTORY {
95 CHAR *Lines[HIST_SIZE];
106 static CHAR NIL[] = "";
107 static const CHAR *Input = NIL;
109 static const char *Prompt;
112 static char NEWLINE[]= CRLF;
122 static KEYMAP Map[33];
123 static KEYMAP MetaMap[16];
124 static size_t Length;
125 static size_t ScreenCount;
126 static size_t ScreenSize;
127 static char *backspace;
131 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
132 int rl_meta_chars = 1;
137 static CHAR *editinput();
140 #if defined(USE_TERMCAP)
141 extern char *getenv();
142 extern char *tgetstr();
143 extern int tgetent();
144 #endif /* defined(USE_TERMCAP) */
147 ** TTY input/output functions.
150 #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 */
187 static struct sgttyb old_sgttyb;
188 static struct tchars old_tchars;
189 struct sgttyb new_sgttyb;
190 struct tchars new_tchars;
193 (void)ioctl(0, TIOCGETP, &old_sgttyb);
194 rl_erase = old_sgttyb.sg_erase;
195 rl_kill = old_sgttyb.sg_kill;
197 (void)ioctl(0, TIOCGETC, &old_tchars);
198 rl_eof = old_tchars.t_eofc;
199 rl_intr = old_tchars.t_intrc;
200 rl_quit = old_tchars.t_quitc;
202 new_sgttyb = old_sgttyb;
203 new_sgttyb.sg_flags &= ~ECHO;
204 new_sgttyb.sg_flags |= RAW;
206 new_sgttyb.sg_flags |= PASS8;
207 #endif /* defined(PASS8) */
208 (void)ioctl(0, TIOCSETP, &new_sgttyb);
210 new_tchars = old_tchars;
211 new_tchars.t_intrc = -1;
212 new_tchars.t_quitc = -1;
213 (void)ioctl(0, TIOCSETC, &new_tchars);
216 (void)ioctl(0, TIOCSETP, &old_sgttyb);
217 (void)ioctl(0, TIOCSETC, &old_tchars);
221 #endif /* HAVE_TCGETATTR */
227 (void)write(1, Screen, ScreenCount);
236 Screen[ScreenCount] = c;
237 if (++ScreenCount >= ScreenSize - 1) {
238 ScreenSize += SCREEN_INC;
239 RENEW(Screen, char, ScreenSize);
263 else if (rl_meta_chars && ISMETA(c)) {
294 while ( ( retv = read( 0, &c, (size_t)1 ) ) == -1 )
296 if ( errno != EINTR )
303 return retv == 1 ? c : EOF;
306 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
320 #if defined(USE_TERMCAP)
324 #endif /* defined(USE_TERMCAP) */
325 #if defined(TIOCGWINSZ)
327 #endif /* defined(TIOCGWINSZ) */
330 #if defined(TIOCGWINSZ)
331 /* Perhaps we got resized. */
332 if (ioctl(0, TIOCGWINSZ, &W) >= 0
333 && W.ws_col > 0 && W.ws_row > 0) {
334 TTYwidth = (int)W.ws_col;
335 TTYrows = (int)W.ws_row;
337 #endif /* defined(TIOCGWINSZ) */
342 TTYwidth = TTYrows = 0;
343 #if defined(USE_TERMCAP)
345 if ((term = getenv("TERM")) == NULL)
347 if (tgetent(buff, term) < 0) {
348 TTYwidth = SCREEN_WIDTH;
349 TTYrows = SCREEN_ROWS;
352 backspace = tgetstr("le", &bp);
353 TTYwidth = tgetnum("co");
354 TTYrows = tgetnum("li");
355 #endif /* defined(USE_TERMCAP) */
357 #if defined(TIOCGWINSZ)
358 if (ioctl(0, TIOCGWINSZ, &W) >= 0) {
359 TTYwidth = (int)W.ws_col;
360 TTYrows = (int)W.ws_row;
362 #endif /* defined(TIOCGWINSZ) */
364 if (TTYwidth <= 0 || TTYrows <= 0) {
365 TTYwidth = SCREEN_WIDTH;
366 TTYrows = SCREEN_ROWS;
379 TTYputs((CHAR *)Prompt);
380 for (i = Point, p = Line; --i >= 0; p++)
390 if (ISCTL(Line[Point - 1]))
392 else if (rl_meta_chars && ISMETA(Line[Point - 1])) {
397 if (Change == CSmove)
405 TTYshow(Line[Point]);
406 if (Change == CSmove)
429 if ((Input = (CHAR *)getenv((char *)name)) == NULL) {
446 for ( ; Point < End && (*p == ' ' || !isalnum(*p)); Point++, p++)
450 for (; Point < End && isalnum(*p); Point++, p++)
456 } while (++i < Repeat);
470 (void)do_forward(CSstay);
471 if (OldPoint != Point) {
472 if ((count = Point - OldPoint) < 0)
475 if ((end = Point + count) > End)
477 for (i = Point, p = &Line[i]; i < end; i++, p++) {
478 if (type == TOupper) {
482 else if (isupper(*p))
493 return do_case(TOlower);
499 return do_case(TOupper);
509 for (extras = 0, i = Point, p = &Line[i]; i <= End; i++, p++) {
515 else if (rl_meta_chars && ISMETA(*p)) {
522 for (i += extras; i > Point; i--)
529 Point = -strlen(Prompt);
546 len = strlen((char *)p);
547 if (End + len >= Length) {
548 if ((new = NEW(CHAR, Length + len + MEM_INC)) == NULL)
551 COPYFROMTO(new, Line, Length);
555 Length += len + MEM_INC;
558 for (q = &Line[Point], i = End - Point; --i >= 0; )
560 COPYFROMTO(&Line[Point], p, len);
563 TTYstring(&Line[Point]);
566 return Point == End ? CSstay : CSmove;
573 return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos];
579 return H.Pos == 0 ? NULL : H.Lines[--H.Pos];
592 return insert_string(p);
604 if ((p = (*move)()) == NULL)
606 } while (++i < Repeat);
607 return do_insert_hist(p);
613 return do_hist(next_hist);
619 return do_hist(prev_hist);
625 return do_insert_hist(H.Lines[H.Pos = 0]);
631 return do_insert_hist(H.Lines[H.Pos = H.Size - 1]);
635 ** Return zero if pat appears as a substring in text.
638 substrcmp(text, pat, len)
645 if ((c = *pat) == '\0')
646 return *text == '\0';
647 for ( ; *text; text++)
648 if ((CHAR)*text == c && strncmp(text, pat, len) == 0)
654 search_hist(search, move)
658 static CHAR *old_search;
664 /* Save or get remembered search pattern. */
665 if (search && *search) {
668 old_search = (CHAR *)DBG_strdup((char *)search);
671 if (old_search == NULL || *old_search == '\0')
676 /* Set up pattern-finder. */
677 if (*search == '^') {
679 pat = (char *)(search + 1);
683 pat = (char *)search;
687 for (pos = H.Pos; (*move)() != NULL; )
688 if ((*match)((char *)H.Lines[H.Pos], pat, len) == 0)
689 return H.Lines[H.Pos];
697 static int Searching;
698 const char *old_prompt;
709 TTYputs((CHAR *)Prompt);
710 move = Repeat == NO_ARG ? prev_hist : next_hist;
711 p = search_hist(editinput(), move);
714 TTYputs((CHAR *)Prompt);
717 return do_insert_hist(p);
730 } while (++i < Repeat);
747 if ((Yanked = NEW(CHAR, (size_t)i + 1)) != NULL) {
748 COPYFROMTO(Yanked, &Line[begin], i);
760 if (count <= 0 || End == Point)
763 if (count == 1 && Point == End - 1) {
764 /* Optimize common case of delete at end of line. */
773 else if (rl_meta_chars && ISMETA(*p)) {
782 if (Point + count > End && (count = End - Point) <= 0)
786 save_yank(Point, count);
788 for (p = &Line[Point], i = End - (Point + count) + 1; --i >= 0; p++)
792 TTYstring(&Line[Point]);
806 } while (++i < Repeat);
821 } while (++i < Repeat);
823 return delete_string(i);
829 TTYputs((CHAR *)NEWLINE);
830 TTYputs((CHAR *)Prompt);
840 if (Repeat != NO_ARG) {
841 if (Repeat < Point) {
845 (void)delete_string(i - Point);
847 else if (Repeat > Point) {
849 (void)delete_string(Repeat - Point - 1);
854 save_yank(Point, End - Point);
871 if (Repeat == NO_ARG || Repeat < 2) {
874 return insert_string(buff);
877 if ((p = NEW(CHAR, Repeat + 1)) == NULL)
879 for (i = Repeat, q = p; --i >= 0; )
883 s = insert_string(p);
894 if ((c = TTYget()) == EOF)
896 /* Also include VT-100 arrows. */
897 if (c == '[' || c == 'O')
898 switch (c = TTYget()) {
899 default: return ring_bell();
900 case EOF: return CSeof;
901 case 'A': return h_prev();
902 case 'B': return h_next();
903 case 'C': return fd_char();
904 case 'D': return bk_char();
908 for (Repeat = c - '0'; (c = TTYget()) != EOF && isdigit(c); )
909 Repeat = Repeat * 10 + c - '0';
917 for (OldPoint = Point, kp = MetaMap; kp->Function; kp++)
919 return (*kp->Function)();
933 PushBack = UNMETA(c);
936 for (kp = Map; kp->Function; kp++)
939 s = kp->Function ? (*kp->Function)() : insert_char((int)c);
941 /* No pushback means no repeat count; hacky, but true. */
953 if (c == rl_erase || c == DEL)
954 return bk_del_char();
963 if (c == rl_intr || c == rl_quit) {
968 if (c == rl_eof && Point == 0 && End == 0)
980 OldPoint = Point = Mark = End = 0;
983 while ((c = TTYget()) != EOF)
984 switch (TTYspecial(c)) {
1018 if ((p = (CHAR *)DBG_strdup((char *)p)) == NULL)
1020 if (H.Size < HIST_SIZE)
1021 H.Lines[H.Size++] = p;
1023 DISPOSE(H.Lines[0]);
1024 for (i = 0; i < HIST_SIZE - 1; i++)
1025 H.Lines[i] = H.Lines[i + 1];
1039 if ((Line = NEW(CHAR, Length)) == NULL)
1046 ScreenSize = SCREEN_INC;
1047 Screen = NEW(char, ScreenSize);
1048 Prompt = prompt ? prompt : (char *)NIL;
1049 TTYputs((CHAR *)Prompt);
1050 if ((line = editinput()) != NULL) {
1051 line = (CHAR *)DBG_strdup((char *)line);
1052 TTYputs((CHAR *)NEWLINE);
1057 DISPOSE(H.Lines[--H.Size]);
1058 return (char *)line;
1065 if (p == NULL || *p == '\0')
1068 #if defined(UNIQUE_HISTORY)
1069 if (H.Pos && strcmp(p, H.Lines[H.Pos - 1]) == 0)
1071 #endif /* defined(UNIQUE_HISTORY) */
1072 hist_add((CHAR *)p);
1089 return delete_string(Repeat == NO_ARG ? 1 : Repeat);
1117 c = Line[Point - 1];
1119 Line[Point - 1] = Line[Point];
1120 TTYshow(Line[Point - 1]);
1132 return (c = TTYget()) == EOF ? CSeof : insert_char((int)c);
1150 return delete_string(Mark - Point);
1165 if ((c = TTYget()) != CTL('X'))
1166 return c == EOF ? CSeof : ring_bell();
1168 if ((c = Mark) <= End) {
1179 if (Yanked && *Yanked)
1180 return insert_string(Yanked);
1191 save_yank(Mark, Point - Mark);
1193 save_yank(Point, Mark - Point);
1205 if ((c = TTYget()) == EOF)
1207 for (i = Point + 1, p = &Line[i]; i < End; i++, p++)
1218 return do_forward(CSmove);
1226 (void)do_forward(CSstay);
1227 if (OldPoint != Point) {
1228 i = Point - OldPoint;
1230 return delete_string(i);
1243 for (p = &Line[Point]; p > Line && !isalnum(p[-1]); p--)
1246 for (; p > Line && p[-1] != ' ' && isalnum(p[-1]); p--)
1251 } while (++i < Repeat);
1260 if (OldPoint != Point)
1261 return delete_string(OldPoint - Point);
1277 if ((*avp = p = NEW(CHAR*, i))== NULL)
1280 for (c = line; isspace(*c); c++)
1282 if (*c == '\n' || *c == '\0')
1285 for (ac = 0, p[ac++] = c; *c && *c != '\n'; ) {
1288 if (*c && *c != '\n') {
1290 new = NEW(CHAR*, i + MEM_INC);
1295 COPYFROMTO(new, p, i * sizeof (char **));
1319 if (H.Size == 1 || (p = H.Lines[H.Size - 2]) == NULL)
1322 if ((p = (CHAR *)DBG_strdup((char *)p)) == NULL)
1324 ac = argify(p, &av);
1326 if (Repeat != NO_ARG)
1327 s = Repeat < ac ? insert_string(av[Repeat]) : ring_bell();
1329 s = ac ? insert_string(av[ac - 1]) : CSstay;
1337 static KEYMAP Map[33] = {
1338 { CTL('@'), ring_bell },
1339 { CTL('A'), beg_line },
1340 { CTL('B'), bk_char },
1341 { CTL('D'), del_char },
1342 { CTL('E'), end_line },
1343 { CTL('F'), fd_char },
1344 { CTL('G'), ring_bell },
1345 { CTL('H'), bk_del_char },
1346 { CTL('I'), ring_bell },
1347 { CTL('J'), accept_line },
1348 { CTL('K'), kill_line },
1349 { CTL('L'), redisplay },
1350 { CTL('M'), accept_line },
1351 { CTL('N'), h_next },
1352 { CTL('O'), ring_bell },
1353 { CTL('P'), h_prev },
1354 { CTL('Q'), ring_bell },
1355 { CTL('R'), h_search },
1356 { CTL('S'), ring_bell },
1357 { CTL('T'), transpose },
1358 { CTL('U'), ring_bell },
1359 { CTL('V'), quote },
1361 { CTL('X'), exchange },
1363 { CTL('Z'), ring_bell },
1365 { CTL(']'), move_to_char },
1366 { CTL('^'), ring_bell },
1367 { CTL('_'), ring_bell },
1371 static KEYMAP MetaMap[16]= {
1372 { CTL('H'), bk_kill_word },
1373 { DEL, bk_kill_word },
1375 { '.', last_argument },
1380 { 'd', fd_kill_word },
1382 { 'l', case_down_word },
1383 { 'u', case_up_word },
1385 { 'w', copy_region },