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>
42 ** Manifest constants.
44 #define SCREEN_WIDTH 80
45 #define SCREEN_ROWS 24
48 #define CTL(x) ((x) & 0x1F)
49 #define ISCTL(x) ((x) && (x) < ' ')
50 #define UNCTL(x) ((x) + 64)
51 #define META(x) ((x) | 0x80)
52 #define ISMETA(x) ((x) & 0x80)
53 #define UNMETA(x) ((x) & 0x7F)
54 #if !defined(HIST_SIZE)
56 #endif /* !defined(HIST_SIZE) */
59 #define SCREEN_INC 256
61 #define DISPOSE(p) DBG_free((char *)(p))
63 ((T *)DBG_alloc((unsigned int)(sizeof (T) * (c))))
64 #define RENEW(p, T, c) \
65 (p = (T *)DBG_realloc((char *)(p), (unsigned int)(sizeof (T) * (c))))
66 #define COPYFROMTO(new, p, len) \
67 (void)memcpy((char *)(new), (char *)(p), (int)(len))
70 ** Command status codes.
72 typedef enum _STATUS
{
73 CSdone
, CSeof
, CSmove
, CSdispatch
, CSstay
77 ** The type of case-changing to perform.
84 ** Key to command mapping.
86 typedef struct _KEYMAP
{
92 ** Command history structure.
94 typedef struct _HISTORY
{
97 CHAR
*Lines
[HIST_SIZE
];
108 static CHAR NIL
[] = "";
109 static const CHAR
*Input
= NIL
;
111 static const char *Prompt
;
114 static char NEWLINE
[]= CRLF
;
124 static KEYMAP Map
[33];
125 static KEYMAP MetaMap
[16];
126 static size_t Length
;
127 static size_t ScreenCount
;
128 static size_t ScreenSize
;
129 static char *backspace
;
133 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
134 int rl_meta_chars
= 1;
139 static CHAR
*editinput();
142 #if defined(USE_TERMCAP)
143 extern char *getenv();
144 extern char *tgetstr();
145 extern int tgetent();
146 #endif /* defined(USE_TERMCAP) */
149 ** TTY input/output functions.
152 #ifdef HAVE_TCGETATTR
158 static struct termios old
;
162 (void)tcgetattr(0, &old
);
163 rl_erase
= old
.c_cc
[VERASE
];
164 rl_kill
= old
.c_cc
[VKILL
];
165 rl_eof
= old
.c_cc
[VEOF
];
166 rl_intr
= old
.c_cc
[VINTR
];
167 rl_quit
= old
.c_cc
[VQUIT
];
170 new.c_cc
[VINTR
] = -1;
171 new.c_cc
[VQUIT
] = -1;
172 new.c_lflag
&= ~(ECHO
| ICANON
);
173 new.c_iflag
&= ~(ISTRIP
| INPCK
);
176 (void)tcsetattr(0, TCSANOW
, &new);
179 (void)tcsetattr(0, TCSANOW
, &old
);
182 #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
);
235 Screen
[ScreenCount
] = c
;
236 if (++ScreenCount
>= ScreenSize
- 1) {
237 ScreenSize
+= SCREEN_INC
;
238 RENEW(Screen
, char, ScreenSize
);
260 else if (rl_meta_chars
&& ISMETA(c
)) {
290 while ( ( retv
= read( 0, &c
, (size_t)1 ) ) == -1 )
292 if ( errno
!= EINTR
)
299 return retv
== 1 ? c
: EOF
;
302 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
315 #if defined(USE_TERMCAP)
319 #endif /* defined(USE_TERMCAP) */
320 #if defined(TIOCGWINSZ)
322 #endif /* defined(TIOCGWINSZ) */
325 #if defined(TIOCGWINSZ)
326 /* Perhaps we got resized. */
327 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0
328 && W
.ws_col
> 0 && W
.ws_row
> 0) {
329 TTYwidth
= (int)W
.ws_col
;
330 TTYrows
= (int)W
.ws_row
;
332 #endif /* defined(TIOCGWINSZ) */
337 TTYwidth
= TTYrows
= 0;
338 #if defined(USE_TERMCAP)
340 if ((term
= getenv("TERM")) == NULL
)
342 if (tgetent(buff
, term
) < 0) {
343 TTYwidth
= SCREEN_WIDTH
;
344 TTYrows
= SCREEN_ROWS
;
347 backspace
= tgetstr("le", &bp
);
348 TTYwidth
= tgetnum("co");
349 TTYrows
= tgetnum("li");
350 #endif /* defined(USE_TERMCAP) */
352 #if defined(TIOCGWINSZ)
353 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0) {
354 TTYwidth
= (int)W
.ws_col
;
355 TTYrows
= (int)W
.ws_row
;
357 #endif /* defined(TIOCGWINSZ) */
359 if (TTYwidth
<= 0 || TTYrows
<= 0) {
360 TTYwidth
= SCREEN_WIDTH
;
361 TTYrows
= SCREEN_ROWS
;
374 TTYputs((CHAR
*)Prompt
);
375 for (i
= Point
, p
= Line
; --i
>= 0; p
++)
384 if (ISCTL(Line
[Point
- 1]))
386 else if (rl_meta_chars
&& ISMETA(Line
[Point
- 1])) {
391 if (Change
== CSmove
)
398 TTYshow(Line
[Point
]);
399 if (Change
== CSmove
)
412 do_macro(unsigned int c
)
421 if ((Input
= (CHAR
*)getenv((char *)name
)) == NULL
) {
429 do_forward(STATUS move
)
437 for ( ; Point
< End
&& (*p
== ' ' || !isalnum(*p
)); Point
++, p
++)
441 for (; Point
< End
&& isalnum(*p
); Point
++, p
++)
447 } while (++i
< Repeat
);
460 (void)do_forward(CSstay
);
461 if (OldPoint
!= Point
) {
462 if ((count
= Point
- OldPoint
) < 0)
465 if ((end
= Point
+ count
) > End
)
467 for (i
= Point
, p
= &Line
[i
]; i
< end
; i
++, p
++) {
468 if (type
== TOupper
) {
472 else if (isupper(*p
))
483 return do_case(TOlower
);
489 return do_case(TOupper
);
499 for (extras
= 0, i
= Point
, p
= &Line
[i
]; i
<= End
; i
++, p
++) {
505 else if (rl_meta_chars
&& ISMETA(*p
)) {
512 for (i
+= extras
; i
> Point
; i
--)
519 Point
= -strlen(Prompt
);
528 insert_string(CHAR
*p
)
535 len
= strlen((char *)p
);
536 if (End
+ len
>= Length
) {
537 if ((new = NEW(CHAR
, Length
+ len
+ MEM_INC
)) == NULL
)
540 COPYFROMTO(new, Line
, Length
);
544 Length
+= len
+ MEM_INC
;
547 for (q
= &Line
[Point
], i
= End
- Point
; --i
>= 0; )
549 COPYFROMTO(&Line
[Point
], p
, len
);
552 TTYstring(&Line
[Point
]);
555 return Point
== End
? CSstay
: CSmove
;
562 return H
.Pos
>= H
.Size
- 1 ? NULL
: H
.Lines
[++H
.Pos
];
568 return H
.Pos
== 0 ? NULL
: H
.Lines
[--H
.Pos
];
572 do_insert_hist(CHAR
*p
)
580 return insert_string(p
);
584 do_hist(CHAR
*(*move
)(void))
591 if ((p
= (*move
)()) == NULL
)
593 } while (++i
< Repeat
);
594 return do_insert_hist(p
);
600 return do_hist(next_hist
);
606 return do_hist(prev_hist
);
612 return do_insert_hist(H
.Lines
[H
.Pos
= 0]);
618 return do_insert_hist(H
.Lines
[H
.Pos
= H
.Size
- 1]);
622 ** Return zero if pat appears as a substring in text.
625 substrcmp(char *text
, char *pat
, int len
)
629 if ((c
= *pat
) == '\0')
630 return *text
== '\0';
631 for ( ; *text
; text
++)
632 if ((CHAR
)*text
== c
&& strncmp(text
, pat
, len
) == 0)
638 search_hist(CHAR
*search
, CHAR
*(*move
)(void))
640 static CHAR
*old_search
;
646 /* Save or get remembered search pattern. */
647 if (search
&& *search
) {
650 old_search
= (CHAR
*)DBG_strdup((char *)search
);
653 if (old_search
== NULL
|| *old_search
== '\0')
658 /* Set up pattern-finder. */
659 if (*search
== '^') {
661 pat
= (char *)(search
+ 1);
665 pat
= (char *)search
;
669 for (pos
= H
.Pos
; (*move
)() != NULL
; )
670 if ((*match
)((char *)H
.Lines
[H
.Pos
], pat
, len
) == 0)
671 return H
.Lines
[H
.Pos
];
679 static int Searching
;
680 const char *old_prompt
;
691 TTYputs((CHAR
*)Prompt
);
692 move
= Repeat
== NO_ARG
? prev_hist
: next_hist
;
693 p
= search_hist(editinput(), move
);
696 TTYputs((CHAR
*)Prompt
);
699 return do_insert_hist(p
);
712 } while (++i
< Repeat
);
717 save_yank(int begin
, int i
)
727 if ((Yanked
= NEW(CHAR
, (size_t)i
+ 1)) != NULL
) {
728 COPYFROMTO(Yanked
, &Line
[begin
], i
);
734 delete_string(int count
)
739 if (count
<= 0 || End
== Point
)
742 if (count
== 1 && Point
== End
- 1) {
743 /* Optimize common case of delete at end of line. */
752 else if (rl_meta_chars
&& ISMETA(*p
)) {
761 if (Point
+ count
> End
&& (count
= End
- Point
) <= 0)
765 save_yank(Point
, count
);
767 for (p
= &Line
[Point
], i
= End
- (Point
+ count
) + 1; --i
>= 0; p
++)
771 TTYstring(&Line
[Point
]);
785 } while (++i
< Repeat
);
800 } while (++i
< Repeat
);
802 return delete_string(i
);
808 TTYputs((CHAR
*)NEWLINE
);
809 TTYputs((CHAR
*)Prompt
);
819 if (Repeat
!= NO_ARG
) {
820 if (Repeat
< Point
) {
824 (void)delete_string(i
- Point
);
826 else if (Repeat
> Point
) {
828 (void)delete_string(Repeat
- Point
- 1);
833 save_yank(Point
, End
- Point
);
849 if (Repeat
== NO_ARG
|| Repeat
< 2) {
852 return insert_string(buff
);
855 if ((p
= NEW(CHAR
, Repeat
+ 1)) == NULL
)
857 for (i
= Repeat
, q
= p
; --i
>= 0; )
861 s
= insert_string(p
);
872 if ((c
= TTYget()) == EOF
)
874 /* Also include VT-100 arrows. */
875 if (c
== '[' || c
== 'O')
876 switch (c
= TTYget()) {
877 default: return ring_bell();
878 case EOF
: return CSeof
;
879 case 'A': return h_prev();
880 case 'B': return h_next();
881 case 'C': return fd_char();
882 case 'D': return bk_char();
886 for (Repeat
= c
- '0'; (c
= TTYget()) != EOF
&& isdigit(c
); )
887 Repeat
= Repeat
* 10 + c
- '0';
895 for (OldPoint
= Point
, kp
= MetaMap
; kp
->Function
; kp
++)
897 return (*kp
->Function
)();
903 emacs(unsigned int c
)
910 PushBack
= UNMETA(c
);
913 for (kp
= Map
; kp
->Function
; kp
++)
916 s
= kp
->Function
? (*kp
->Function
)() : insert_char((int)c
);
918 /* No pushback means no repeat count; hacky, but true. */
924 TTYspecial(unsigned int c
)
929 if (c
== rl_erase
|| c
== DEL
)
930 return bk_del_char();
939 if (c
== rl_intr
|| c
== rl_quit
) {
944 if (c
== rl_eof
&& Point
== 0 && End
== 0)
956 OldPoint
= Point
= Mark
= End
= 0;
959 while ((c
= TTYget()) != EOF
)
960 switch (TTYspecial(c
)) {
993 if ((p
= (CHAR
*)DBG_strdup((char *)p
)) == NULL
)
995 if (H
.Size
< HIST_SIZE
)
996 H
.Lines
[H
.Size
++] = p
;
999 for (i
= 0; i
< HIST_SIZE
- 1; i
++)
1000 H
.Lines
[i
] = H
.Lines
[i
+ 1];
1007 readline(const char *prompt
)
1013 if ((Line
= NEW(CHAR
, Length
)) == NULL
)
1020 ScreenSize
= SCREEN_INC
;
1021 Screen
= NEW(char, ScreenSize
);
1022 Prompt
= prompt
? prompt
: (char *)NIL
;
1023 TTYputs((CHAR
*)Prompt
);
1024 if ((line
= editinput()) != NULL
) {
1025 line
= (CHAR
*)DBG_strdup((char *)line
);
1026 TTYputs((CHAR
*)NEWLINE
);
1031 DISPOSE(H
.Lines
[--H
.Size
]);
1032 return (char *)line
;
1036 add_history(char *p
)
1038 if (p
== NULL
|| *p
== '\0')
1041 #if defined(UNIQUE_HISTORY)
1042 if (H
.Pos
&& strcmp(p
, H
.Lines
[H
.Pos
- 1]) == 0)
1044 #endif /* defined(UNIQUE_HISTORY) */
1045 hist_add((CHAR
*)p
);
1062 return delete_string(Repeat
== NO_ARG
? 1 : Repeat
);
1090 c
= Line
[Point
- 1];
1092 Line
[Point
- 1] = Line
[Point
];
1093 TTYshow(Line
[Point
- 1]);
1105 return (c
= TTYget()) == EOF
? CSeof
: insert_char((int)c
);
1123 return delete_string(Mark
- Point
);
1138 if ((c
= TTYget()) != CTL('X'))
1139 return c
== EOF
? CSeof
: ring_bell();
1141 if ((c
= Mark
) <= End
) {
1152 if (Yanked
&& *Yanked
)
1153 return insert_string(Yanked
);
1164 save_yank(Mark
, Point
- Mark
);
1166 save_yank(Point
, Mark
- Point
);
1178 if ((c
= TTYget()) == EOF
)
1180 for (i
= Point
+ 1, p
= &Line
[i
]; i
< End
; i
++, p
++)
1191 return do_forward(CSmove
);
1199 (void)do_forward(CSstay
);
1200 if (OldPoint
!= Point
) {
1201 i
= Point
- OldPoint
;
1203 return delete_string(i
);
1216 for (p
= &Line
[Point
]; p
> Line
&& !isalnum(p
[-1]); p
--)
1219 for (; p
> Line
&& p
[-1] != ' ' && isalnum(p
[-1]); p
--)
1224 } while (++i
< Repeat
);
1233 if (OldPoint
!= Point
)
1234 return delete_string(OldPoint
- Point
);
1239 argify(CHAR
*line
, CHAR
***avp
)
1248 if ((*avp
= p
= NEW(CHAR
*, i
))== NULL
)
1251 for (c
= line
; isspace(*c
); c
++)
1253 if (*c
== '\n' || *c
== '\0')
1256 for (ac
= 0, p
[ac
++] = c
; *c
&& *c
!= '\n'; ) {
1259 if (*c
&& *c
!= '\n') {
1261 new = NEW(CHAR
*, i
+ MEM_INC
);
1266 COPYFROMTO(new, p
, i
* sizeof (char **));
1290 if (H
.Size
== 1 || (p
= H
.Lines
[H
.Size
- 2]) == NULL
)
1293 if ((p
= (CHAR
*)DBG_strdup((char *)p
)) == NULL
)
1295 ac
= argify(p
, &av
);
1297 if (Repeat
!= NO_ARG
)
1298 s
= Repeat
< ac
? insert_string(av
[Repeat
]) : ring_bell();
1300 s
= ac
? insert_string(av
[ac
- 1]) : CSstay
;
1308 static KEYMAP Map
[33] = {
1309 { CTL('@'), ring_bell
},
1310 { CTL('A'), beg_line
},
1311 { CTL('B'), bk_char
},
1312 { CTL('D'), del_char
},
1313 { CTL('E'), end_line
},
1314 { CTL('F'), fd_char
},
1315 { CTL('G'), ring_bell
},
1316 { CTL('H'), bk_del_char
},
1317 { CTL('I'), ring_bell
},
1318 { CTL('J'), accept_line
},
1319 { CTL('K'), kill_line
},
1320 { CTL('L'), redisplay
},
1321 { CTL('M'), accept_line
},
1322 { CTL('N'), h_next
},
1323 { CTL('O'), ring_bell
},
1324 { CTL('P'), h_prev
},
1325 { CTL('Q'), ring_bell
},
1326 { CTL('R'), h_search
},
1327 { CTL('S'), ring_bell
},
1328 { CTL('T'), transpose
},
1329 { CTL('U'), ring_bell
},
1330 { CTL('V'), quote
},
1332 { CTL('X'), exchange
},
1334 { CTL('Z'), ring_bell
},
1336 { CTL(']'), move_to_char
},
1337 { CTL('^'), ring_bell
},
1338 { CTL('_'), ring_bell
},
1342 static KEYMAP MetaMap
[16]= {
1343 { CTL('H'), bk_kill_word
},
1344 { DEL
, bk_kill_word
},
1346 { '.', last_argument
},
1351 { 'd', fd_kill_word
},
1353 { 'l', case_down_word
},
1354 { 'u', case_up_word
},
1356 { 'w', copy_region
},