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>
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
)
302 return retv
== 1 ? c
: EOF
;
305 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
319 #if defined(USE_TERMCAP)
323 #endif /* defined(USE_TERMCAP) */
324 #if defined(TIOCGWINSZ)
326 #endif /* defined(TIOCGWINSZ) */
329 #if defined(TIOCGWINSZ)
330 /* Perhaps we got resized. */
331 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0
332 && W
.ws_col
> 0 && W
.ws_row
> 0) {
333 TTYwidth
= (int)W
.ws_col
;
334 TTYrows
= (int)W
.ws_row
;
336 #endif /* defined(TIOCGWINSZ) */
341 TTYwidth
= TTYrows
= 0;
342 #if defined(USE_TERMCAP)
344 if ((term
= getenv("TERM")) == NULL
)
346 if (tgetent(buff
, term
) < 0) {
347 TTYwidth
= SCREEN_WIDTH
;
348 TTYrows
= SCREEN_ROWS
;
351 backspace
= tgetstr("le", &bp
);
352 TTYwidth
= tgetnum("co");
353 TTYrows
= tgetnum("li");
354 #endif /* defined(USE_TERMCAP) */
356 #if defined(TIOCGWINSZ)
357 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0) {
358 TTYwidth
= (int)W
.ws_col
;
359 TTYrows
= (int)W
.ws_row
;
361 #endif /* defined(TIOCGWINSZ) */
363 if (TTYwidth
<= 0 || TTYrows
<= 0) {
364 TTYwidth
= SCREEN_WIDTH
;
365 TTYrows
= SCREEN_ROWS
;
378 TTYputs((CHAR
*)Prompt
);
379 for (i
= Point
, p
= Line
; --i
>= 0; p
++)
389 if (ISCTL(Line
[Point
- 1]))
391 else if (rl_meta_chars
&& ISMETA(Line
[Point
- 1])) {
396 if (Change
== CSmove
)
404 TTYshow(Line
[Point
]);
405 if (Change
== CSmove
)
428 if ((Input
= (CHAR
*)getenv((char *)name
)) == NULL
) {
445 for ( ; Point
< End
&& (*p
== ' ' || !isalnum(*p
)); Point
++, p
++)
449 for (; Point
< End
&& isalnum(*p
); Point
++, p
++)
455 } while (++i
< Repeat
);
469 (void)do_forward(CSstay
);
470 if (OldPoint
!= Point
) {
471 if ((count
= Point
- OldPoint
) < 0)
474 if ((end
= Point
+ count
) > End
)
476 for (i
= Point
, p
= &Line
[i
]; i
< end
; i
++, p
++) {
477 if (type
== TOupper
) {
481 else if (isupper(*p
))
492 return do_case(TOlower
);
498 return do_case(TOupper
);
508 for (extras
= 0, i
= Point
, p
= &Line
[i
]; i
<= End
; i
++, p
++) {
514 else if (rl_meta_chars
&& ISMETA(*p
)) {
521 for (i
+= extras
; i
> Point
; i
--)
528 Point
= -strlen(Prompt
);
545 len
= strlen((char *)p
);
546 if (End
+ len
>= Length
) {
547 if ((new = NEW(CHAR
, Length
+ len
+ MEM_INC
)) == NULL
)
550 COPYFROMTO(new, Line
, Length
);
554 Length
+= len
+ MEM_INC
;
557 for (q
= &Line
[Point
], i
= End
- Point
; --i
>= 0; )
559 COPYFROMTO(&Line
[Point
], p
, len
);
562 TTYstring(&Line
[Point
]);
565 return Point
== End
? CSstay
: CSmove
;
572 return H
.Pos
>= H
.Size
- 1 ? NULL
: H
.Lines
[++H
.Pos
];
578 return H
.Pos
== 0 ? NULL
: H
.Lines
[--H
.Pos
];
591 return insert_string(p
);
603 if ((p
= (*move
)()) == NULL
)
605 } while (++i
< Repeat
);
606 return do_insert_hist(p
);
612 return do_hist(next_hist
);
618 return do_hist(prev_hist
);
624 return do_insert_hist(H
.Lines
[H
.Pos
= 0]);
630 return do_insert_hist(H
.Lines
[H
.Pos
= H
.Size
- 1]);
634 ** Return zero if pat appears as a substring in text.
637 substrcmp(text
, pat
, len
)
644 if ((c
= *pat
) == '\0')
645 return *text
== '\0';
646 for ( ; *text
; text
++)
647 if ((CHAR
)*text
== c
&& strncmp(text
, pat
, len
) == 0)
653 search_hist(search
, move
)
657 static CHAR
*old_search
;
663 /* Save or get remembered search pattern. */
664 if (search
&& *search
) {
667 old_search
= (CHAR
*)strdup((char *)search
);
670 if (old_search
== NULL
|| *old_search
== '\0')
675 /* Set up pattern-finder. */
676 if (*search
== '^') {
678 pat
= (char *)(search
+ 1);
682 pat
= (char *)search
;
686 for (pos
= H
.Pos
; (*move
)() != NULL
; )
687 if ((*match
)((char *)H
.Lines
[H
.Pos
], pat
, len
) == 0)
688 return H
.Lines
[H
.Pos
];
696 static int Searching
;
697 const char *old_prompt
;
708 TTYputs((CHAR
*)Prompt
);
709 move
= Repeat
== NO_ARG
? prev_hist
: next_hist
;
710 p
= search_hist(editinput(), move
);
713 TTYputs((CHAR
*)Prompt
);
716 return do_insert_hist(p
);
729 } while (++i
< Repeat
);
746 if ((Yanked
= NEW(CHAR
, (size_t)i
+ 1)) != NULL
) {
747 COPYFROMTO(Yanked
, &Line
[begin
], i
);
759 if (count
<= 0 || End
== Point
)
762 if (count
== 1 && Point
== End
- 1) {
763 /* Optimize common case of delete at end of line. */
772 else if (rl_meta_chars
&& ISMETA(*p
)) {
781 if (Point
+ count
> End
&& (count
= End
- Point
) <= 0)
785 save_yank(Point
, count
);
787 for (p
= &Line
[Point
], i
= End
- (Point
+ count
) + 1; --i
>= 0; p
++)
791 TTYstring(&Line
[Point
]);
805 } while (++i
< Repeat
);
820 } while (++i
< Repeat
);
822 return delete_string(i
);
828 TTYputs((CHAR
*)NEWLINE
);
829 TTYputs((CHAR
*)Prompt
);
839 if (Repeat
!= NO_ARG
) {
840 if (Repeat
< Point
) {
844 (void)delete_string(i
- Point
);
846 else if (Repeat
> Point
) {
848 (void)delete_string(Repeat
- Point
- 1);
853 save_yank(Point
, End
- Point
);
870 if (Repeat
== NO_ARG
|| Repeat
< 2) {
873 return insert_string(buff
);
876 if ((p
= NEW(CHAR
, Repeat
+ 1)) == NULL
)
878 for (i
= Repeat
, q
= p
; --i
>= 0; )
882 s
= insert_string(p
);
893 if ((c
= TTYget()) == EOF
)
895 /* Also include VT-100 arrows. */
896 if (c
== '[' || c
== 'O')
897 switch (c
= TTYget()) {
898 default: return ring_bell();
899 case EOF
: return CSeof
;
900 case 'A': return h_prev();
901 case 'B': return h_next();
902 case 'C': return fd_char();
903 case 'D': return bk_char();
907 for (Repeat
= c
- '0'; (c
= TTYget()) != EOF
&& isdigit(c
); )
908 Repeat
= Repeat
* 10 + c
- '0';
916 for (OldPoint
= Point
, kp
= MetaMap
; kp
->Function
; kp
++)
918 return (*kp
->Function
)();
932 PushBack
= UNMETA(c
);
935 for (kp
= Map
; kp
->Function
; kp
++)
938 s
= kp
->Function
? (*kp
->Function
)() : insert_char((int)c
);
940 /* No pushback means no repeat count; hacky, but true. */
952 if (c
== rl_erase
|| c
== DEL
)
953 return bk_del_char();
962 if (c
== rl_intr
|| c
== rl_quit
) {
967 if (c
== rl_eof
&& Point
== 0 && End
== 0)
979 OldPoint
= Point
= Mark
= End
= 0;
982 while ((c
= TTYget()) != EOF
)
983 switch (TTYspecial(c
)) {
1017 if ((p
= (CHAR
*)strdup((char *)p
)) == NULL
)
1019 if (H
.Size
< HIST_SIZE
)
1020 H
.Lines
[H
.Size
++] = p
;
1022 DISPOSE(H
.Lines
[0]);
1023 for (i
= 0; i
< HIST_SIZE
- 1; i
++)
1024 H
.Lines
[i
] = H
.Lines
[i
+ 1];
1038 if ((Line
= NEW(CHAR
, Length
)) == NULL
)
1045 ScreenSize
= SCREEN_INC
;
1046 Screen
= NEW(char, ScreenSize
);
1047 Prompt
= prompt
? prompt
: (char *)NIL
;
1048 TTYputs((CHAR
*)Prompt
);
1049 if ((line
= editinput()) != NULL
) {
1050 line
= (CHAR
*)strdup((char *)line
);
1051 TTYputs((CHAR
*)NEWLINE
);
1056 DISPOSE(H
.Lines
[--H
.Size
]);
1057 return (char *)line
;
1064 if (p
== NULL
|| *p
== '\0')
1067 #if defined(UNIQUE_HISTORY)
1068 if (H
.Pos
&& strcmp(p
, H
.Lines
[H
.Pos
- 1]) == 0)
1070 #endif /* defined(UNIQUE_HISTORY) */
1071 hist_add((CHAR
*)p
);
1088 return delete_string(Repeat
== NO_ARG
? 1 : Repeat
);
1116 c
= Line
[Point
- 1];
1118 Line
[Point
- 1] = Line
[Point
];
1119 TTYshow(Line
[Point
- 1]);
1131 return (c
= TTYget()) == EOF
? CSeof
: insert_char((int)c
);
1149 return delete_string(Mark
- Point
);
1164 if ((c
= TTYget()) != CTL('X'))
1165 return c
== EOF
? CSeof
: ring_bell();
1167 if ((c
= Mark
) <= End
) {
1178 if (Yanked
&& *Yanked
)
1179 return insert_string(Yanked
);
1190 save_yank(Mark
, Point
- Mark
);
1192 save_yank(Point
, Mark
- Point
);
1204 if ((c
= TTYget()) == EOF
)
1206 for (i
= Point
+ 1, p
= &Line
[i
]; i
< End
; i
++, p
++)
1217 return do_forward(CSmove
);
1225 (void)do_forward(CSstay
);
1226 if (OldPoint
!= Point
) {
1227 i
= Point
- OldPoint
;
1229 return delete_string(i
);
1242 for (p
= &Line
[Point
]; p
> Line
&& !isalnum(p
[-1]); p
--)
1245 for (; p
> Line
&& p
[-1] != ' ' && isalnum(p
[-1]); p
--)
1250 } while (++i
< Repeat
);
1259 if (OldPoint
!= Point
)
1260 return delete_string(OldPoint
- Point
);
1276 if ((*avp
= p
= NEW(CHAR
*, i
))== NULL
)
1279 for (c
= line
; isspace(*c
); c
++)
1281 if (*c
== '\n' || *c
== '\0')
1284 for (ac
= 0, p
[ac
++] = c
; *c
&& *c
!= '\n'; ) {
1287 if (*c
&& *c
!= '\n') {
1289 new = NEW(CHAR
*, i
+ MEM_INC
);
1294 COPYFROMTO(new, p
, i
* sizeof (char **));
1318 if (H
.Size
== 1 || (p
= H
.Lines
[H
.Size
- 2]) == NULL
)
1321 if ((p
= (CHAR
*)strdup((char *)p
)) == NULL
)
1323 ac
= argify(p
, &av
);
1325 if (Repeat
!= NO_ARG
)
1326 s
= Repeat
< ac
? insert_string(av
[Repeat
]) : ring_bell();
1328 s
= ac
? insert_string(av
[ac
- 1]) : CSstay
;
1336 static KEYMAP Map
[33] = {
1337 { CTL('@'), ring_bell
},
1338 { CTL('A'), beg_line
},
1339 { CTL('B'), bk_char
},
1340 { CTL('D'), del_char
},
1341 { CTL('E'), end_line
},
1342 { CTL('F'), fd_char
},
1343 { CTL('G'), ring_bell
},
1344 { CTL('H'), bk_del_char
},
1345 { CTL('I'), ring_bell
},
1346 { CTL('J'), accept_line
},
1347 { CTL('K'), kill_line
},
1348 { CTL('L'), redisplay
},
1349 { CTL('M'), accept_line
},
1350 { CTL('N'), h_next
},
1351 { CTL('O'), ring_bell
},
1352 { CTL('P'), h_prev
},
1353 { CTL('Q'), ring_bell
},
1354 { CTL('R'), h_search
},
1355 { CTL('S'), ring_bell
},
1356 { CTL('T'), transpose
},
1357 { CTL('U'), ring_bell
},
1358 { CTL('V'), quote
},
1360 { CTL('X'), exchange
},
1362 { CTL('Z'), ring_bell
},
1364 { CTL(']'), move_to_char
},
1365 { CTL('^'), ring_bell
},
1366 { CTL('_'), ring_bell
},
1370 static KEYMAP MetaMap
[16]= {
1371 { CTL('H'), bk_kill_word
},
1372 { DEL
, bk_kill_word
},
1374 { '.', last_argument
},
1379 { 'd', fd_kill_word
},
1381 { 'l', case_down_word
},
1382 { 'u', case_up_word
},
1384 { 'w', copy_region
},