1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 * This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 * Copyright 1992,1993 Simmule Turner and Rich Salz. All rights reserved.
9 * This software is not subject to any license of the American Telephone
10 * and Telegraph Company or of the Regents of the University of California.
12 * Permission is granted to anyone to use this software for any purpose on
13 * any computer system, and to alter it and redistribute it freely, subject
14 * to the following restrictions:
15 * 1. The authors are not responsible for the consequences of use of this
16 * software, no matter how awful, even if they arise from flaws in it.
17 * 2. The origin of this software must not be misrepresented, either by
18 * explicit claim or by omission. Since few users ever read sources,
19 * credits must appear in the documentation.
20 * 3. Altered versions must be plainly marked as such, and must not be
21 * misrepresented as being the original software. Since few users
22 * ever read sources, credits must appear in the documentation.
23 * 4. This notice may not be removed or altered.
28 ** Main editing routines for editline library.
36 ** Manifest constants.
38 #define SCREEN_WIDTH 80
39 #define SCREEN_ROWS 24
42 #define CTL(x) ((x) & 0x1F)
43 #define ISCTL(x) ((x) && (x) < ' ')
44 #define UNCTL(x) ((x) + 64)
45 #define META(x) ((x) | 0x80)
46 #define ISMETA(x) ((x) & 0x80)
47 #define UNMETA(x) ((x) & 0x7F)
48 #if !defined(HIST_SIZE)
50 #endif /* !defined(HIST_SIZE) */
53 ** Command status codes.
55 typedef enum _STATUS
{
56 CSdone
, CSeof
, CSmove
, CSdispatch
, CSstay
, CSsignal
60 ** The type of case-changing to perform.
67 ** Key to command mapping.
69 typedef struct _KEYMAP
{
75 ** Command history structure.
77 typedef struct _HISTORY
{
80 CHAR
*Lines
[HIST_SIZE
];
92 STATIC CHAR NIL
[] = "";
93 STATIC CONST CHAR
*Input
= NIL
;
95 STATIC CONST
char *Prompt
;
98 STATIC CONST
char NEWLINE
[]= CRLF
;
108 FORWARD CONST KEYMAP Map
[32];
109 FORWARD CONST KEYMAP MetaMap
[16];
110 STATIC SIZE_T Length
;
111 STATIC SIZE_T ScreenCount
;
112 STATIC SIZE_T ScreenSize
;
113 STATIC
char *backspace
;
117 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
118 int rl_meta_chars
= 0;
123 STATIC CHAR
*editinput();
124 #if defined(USE_TERMCAP)
128 #endif /* defined(USE_TERMCAP) */
131 ** TTY input/output functions.
138 /* Dummy assignment avoids GCC warning on
139 * "attribute warn_unused_result" */
140 ssize_t dummy
= write(1, Screen
, ScreenCount
);
149 Screen
[ScreenCount
] = c
;
150 if (++ScreenCount
>= ScreenSize
- 1) {
151 ScreenSize
+= SCREEN_INC
;
152 RENEW(Screen
, char, ScreenSize
);
157 TTYputs(CONST CHAR
*p
)
174 else if (rl_meta_chars
&& ISMETA(c
)) {
202 return read(0, &c
, (SIZE_T
)1) == 1 ? c
: EOF
;
205 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
218 #if defined(USE_TERMCAP)
222 #endif /* defined(USE_TERMCAP) */
223 #if defined(TIOCGWINSZ)
225 #endif /* defined(TIOCGWINSZ) */
228 #if defined(TIOCGWINSZ)
229 /* Perhaps we got resized. */
230 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0
231 && W
.ws_col
> 0 && W
.ws_row
> 0) {
232 TTYwidth
= (int)W
.ws_col
;
233 TTYrows
= (int)W
.ws_row
;
235 #endif /* defined(TIOCGWINSZ) */
240 TTYwidth
= TTYrows
= 0;
241 #if defined(USE_TERMCAP)
243 if ((term
= getenv("TERM")) == NULL
)
245 if (tgetent(buff
, term
) < 0) {
246 TTYwidth
= SCREEN_WIDTH
;
247 TTYrows
= SCREEN_ROWS
;
250 p
= tgetstr("le", &bp
);
251 backspace
= p
? strdup(p
) : NULL
;
252 TTYwidth
= tgetnum("co");
253 TTYrows
= tgetnum("li");
254 #endif /* defined(USE_TERMCAP) */
256 #if defined(TIOCGWINSZ)
257 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0) {
258 TTYwidth
= (int)W
.ws_col
;
259 TTYrows
= (int)W
.ws_row
;
261 #endif /* defined(TIOCGWINSZ) */
263 if (TTYwidth
<= 0 || TTYrows
<= 0) {
264 TTYwidth
= SCREEN_WIDTH
;
265 TTYrows
= SCREEN_ROWS
;
277 TTYputs((CONST CHAR
*)Prompt
);
278 for (i
= Point
, p
= Line
; --i
>= 0; p
++)
287 if (ISCTL(Line
[Point
- 1]))
289 else if (rl_meta_chars
&& ISMETA(Line
[Point
- 1])) {
294 if (Change
== CSmove
)
301 TTYshow(Line
[Point
]);
302 if (Change
== CSmove
)
315 do_macro(unsigned int c
)
324 if ((Input
= (CHAR
*)getenv((char *)name
)) == NULL
) {
332 do_forward(STATUS move
)
340 for ( ; Point
< End
&& (*p
== ' ' || !isalnum(*p
)); Point
++, p
++)
344 for (; Point
< End
&& isalnum(*p
); Point
++, p
++)
350 } while (++i
< Repeat
);
363 (void)do_forward(CSstay
);
364 if (OldPoint
!= Point
) {
365 if ((count
= Point
- OldPoint
) < 0)
368 if ((end
= Point
+ count
) > End
)
370 for (i
= Point
, p
= &Line
[i
]; i
< end
; i
++, p
++) {
371 if (type
== TOupper
) {
375 else if (isupper(*p
))
386 return do_case(TOlower
);
392 return do_case(TOupper
);
402 for (extras
= 0, i
= Point
, p
= &Line
[i
]; i
<= End
; i
++, p
++) {
408 else if (rl_meta_chars
&& ISMETA(*p
)) {
415 for (i
+= extras
; i
> Point
; i
--)
422 Point
= -strlen(Prompt
);
431 insert_string(CHAR
*p
)
438 len
= strlen((char *)p
);
439 if (End
+ len
>= Length
) {
440 if ((new = NEW(CHAR
, Length
+ len
+ MEM_INC
)) == NULL
)
443 COPYFROMTO(new, Line
, Length
);
447 Length
+= len
+ MEM_INC
;
450 for (q
= &Line
[Point
], i
= End
- Point
; --i
>= 0; )
452 COPYFROMTO(&Line
[Point
], p
, len
);
455 TTYstring(&Line
[Point
]);
458 return Point
== End
? CSstay
: CSmove
;
464 TTYputs((CONST CHAR
*)NEWLINE
);
465 TTYputs((CONST CHAR
*)Prompt
);
473 rl_meta_chars
= ! rl_meta_chars
;
481 return H
.Pos
>= H
.Size
- 1 ? NULL
: H
.Lines
[++H
.Pos
];
487 return H
.Pos
== 0 ? NULL
: H
.Lines
[--H
.Pos
];
491 do_insert_hist(CHAR
*p
)
499 return insert_string(p
);
503 do_hist(CHAR
*(*move
)())
510 if ((p
= (*move
)()) == NULL
)
512 } while (++i
< Repeat
);
513 return do_insert_hist(p
);
519 return do_hist(next_hist
);
525 return do_hist(prev_hist
);
531 return do_insert_hist(H
.Lines
[H
.Pos
= 0]);
537 return do_insert_hist(H
.Lines
[H
.Pos
= H
.Size
- 1]);
541 ** Return zero if pat appears as a substring in text.
544 substrcmp(char *text
, char *pat
, size_t len
)
548 if ((c
= *pat
) == '\0')
549 return *text
== '\0';
550 for ( ; *text
; text
++)
551 if (*text
== c
&& strncmp(text
, pat
, len
) == 0)
557 search_hist(CHAR
*search
, CHAR
*(*move
)())
559 static CHAR
*old_search
;
562 int (*match
)(char *, char *, size_t);
565 /* Save or get remembered search pattern. */
566 if (search
&& *search
) {
569 old_search
= (CHAR
*)strdup((char *)search
);
572 if (old_search
== NULL
|| *old_search
== '\0')
577 /* Set up pattern-finder. */
578 if (*search
== '^') {
579 match
= (int(*)(char *, char *, size_t))strncmp
;
580 pat
= (char *)(search
+ 1);
584 pat
= (char *)search
;
588 for (pos
= H
.Pos
; (*move
)() != NULL
; )
589 if ((*match
)((char *)H
.Lines
[H
.Pos
], pat
, len
) == 0)
590 return H
.Lines
[H
.Pos
];
598 static int Searching
;
599 CONST
char *old_prompt
;
610 TTYputs((CONST CHAR
*)Prompt
);
611 move
= Repeat
== NO_ARG
? prev_hist
: next_hist
;
615 TTYputs((CONST CHAR
*)Prompt
);
616 if (p
== NULL
&& Signal
> 0) {
621 p
= search_hist(p
, move
);
627 return do_insert_hist(p
);
640 } while (++i
< Repeat
);
645 save_yank(int begin
, int i
)
655 if ((Yanked
= NEW(CHAR
, (SIZE_T
)i
+ 1)) != NULL
) {
656 COPYFROMTO(Yanked
, &Line
[begin
], i
);
662 delete_string(int count
)
667 if (count
<= 0 || End
== Point
)
670 if (count
== 1 && Point
== End
- 1) {
671 /* Optimize common case of delete at end of line. */
680 else if (rl_meta_chars
&& ISMETA(*p
)) {
689 if (Point
+ count
> End
&& (count
= End
- Point
) <= 0)
693 save_yank(Point
, count
);
695 for (p
= &Line
[Point
], i
= End
- (Point
+ count
) + 1; --i
>= 0; p
++)
699 TTYstring(&Line
[Point
]);
713 } while (++i
< Repeat
);
728 } while (++i
< Repeat
);
730 return delete_string(i
);
738 if (Repeat
!= NO_ARG
) {
739 if (Repeat
< Point
) {
743 (void)delete_string(i
- Point
);
745 else if (Repeat
> Point
) {
747 (void)delete_string(Repeat
- Point
- 1);
752 save_yank(Point
, End
- Point
);
768 if (Repeat
== NO_ARG
|| Repeat
< 2) {
771 return insert_string(buff
);
774 if ((p
= NEW(CHAR
, Repeat
+ 1)) == NULL
)
776 for (i
= Repeat
, q
= p
; --i
>= 0; )
780 s
= insert_string(p
);
791 if ((int)(c
= TTYget()) == EOF
)
793 #if defined(ANSI_ARROWS)
794 /* Also include VT-100 arrows. */
795 if (c
== '[' || c
== 'O')
796 switch (c
= TTYget()) {
797 default: return ring_bell();
798 case EOF
: return CSeof
;
799 case 'A': return h_prev();
800 case 'B': return h_next();
801 case 'C': return fd_char();
802 case 'D': return bk_char();
804 #endif /* defined(ANSI_ARROWS) */
807 for (Repeat
= c
- '0'; (int)(c
= TTYget()) != EOF
&& isdigit(c
); )
808 Repeat
= Repeat
* 10 + c
- '0';
816 for (OldPoint
= Point
, kp
= MetaMap
; kp
->Function
; kp
++)
818 return (*kp
->Function
)();
824 emacs(unsigned int c
)
829 if (rl_meta_chars
&& ISMETA(c
)) {
831 PushBack
= UNMETA(c
);
834 for (kp
= Map
; kp
->Function
; kp
++)
837 s
= kp
->Function
? (*kp
->Function
)() : insert_char((int)c
);
839 /* No pushback means no repeat count; hacky, but true. */
845 TTYspecial(unsigned int c
)
850 if (c
== rl_erase
|| (int)c
== DEL
)
851 return bk_del_char();
860 if (c
== rl_eof
&& Point
== 0 && End
== 0)
880 OldPoint
= Point
= Mark
= End
= 0;
884 while ((int)(c
= TTYget()) != EOF
)
885 switch (TTYspecial(c
)) {
914 if (strlen((char *)Line
))
925 if ((p
= (CHAR
*)strdup((char *)p
)) == NULL
)
927 if (H
.Size
< HIST_SIZE
)
928 H
.Lines
[H
.Size
++] = p
;
931 for (i
= 0; i
< HIST_SIZE
- 1; i
++)
932 H
.Lines
[i
] = H
.Lines
[i
+ 1];
939 ** For compatibility with FSF readline.
943 rl_reset_terminal(char *p
)
954 readline(CONST
char *prompt
)
961 if ((Line
= NEW(CHAR
, Length
)) == NULL
)
968 ScreenSize
= SCREEN_INC
;
969 Screen
= NEW(char, ScreenSize
);
970 Prompt
= prompt
? prompt
: (char *)NIL
;
971 TTYputs((CONST CHAR
*)Prompt
);
972 if ((line
= editinput()) != NULL
) {
973 line
= (CHAR
*)strdup((char *)line
);
974 TTYputs((CONST CHAR
*)NEWLINE
);
979 DISPOSE(H
.Lines
[--H
.Size
]);
983 (void)kill(getpid(), s
);
991 if (p
== NULL
|| *p
== '\0')
994 #if defined(UNIQUE_HISTORY)
995 if (H
.Size
&& strcmp(p
, (char *)H
.Lines
[H
.Size
- 1]) == 0)
997 #endif /* defined(UNIQUE_HISTORY) */
1015 return delete_string(Repeat
== NO_ARG
? 1 : Repeat
);
1043 c
= Line
[Point
- 1];
1045 Line
[Point
- 1] = Line
[Point
];
1046 TTYshow(Line
[Point
- 1]);
1058 return (int)(c
= TTYget()) == EOF
? CSeof
: insert_char((int)c
);
1076 return delete_string(Mark
- Point
);
1091 if ((c
= TTYget()) != CTL('X'))
1092 return (int)c
== EOF
? CSeof
: ring_bell();
1094 if ((int)(c
= Mark
) <= End
) {
1105 if (Yanked
&& *Yanked
)
1106 return insert_string(Yanked
);
1117 save_yank(Mark
, Point
- Mark
);
1119 save_yank(Point
, Mark
- Point
);
1131 if ((int)(c
= TTYget()) == EOF
)
1133 for (i
= Point
+ 1, p
= &Line
[i
]; i
< End
; i
++, p
++)
1144 return do_forward(CSmove
);
1152 (void)do_forward(CSstay
);
1153 if (OldPoint
!= Point
) {
1154 i
= Point
- OldPoint
;
1156 return delete_string(i
);
1169 for (p
= &Line
[Point
]; p
> Line
&& !isalnum(p
[-1]); p
--)
1172 for (; p
> Line
&& p
[-1] != ' ' && isalnum(p
[-1]); p
--)
1177 } while (++i
< Repeat
);
1186 if (OldPoint
!= Point
)
1187 return delete_string(OldPoint
- Point
);
1192 argify(CHAR
*line
, CHAR
***avp
)
1201 if ((*avp
= p
= NEW(CHAR
*, i
))== NULL
)
1204 for (c
= line
; isspace(*c
); c
++)
1206 if (*c
== '\n' || *c
== '\0')
1209 for (ac
= 0, p
[ac
++] = c
; *c
&& *c
!= '\n'; ) {
1212 if (*c
&& *c
!= '\n') {
1214 new = NEW(CHAR
*, i
+ MEM_INC
);
1219 COPYFROMTO(new, p
, i
* sizeof (char **));
1243 if (H
.Size
== 1 || (p
= H
.Lines
[H
.Size
- 2]) == NULL
)
1246 if ((p
= (CHAR
*)strdup((char *)p
)) == NULL
)
1248 ac
= argify(p
, &av
);
1250 if (Repeat
!= NO_ARG
)
1251 s
= Repeat
< ac
? insert_string(av
[Repeat
]) : ring_bell();
1253 s
= ac
? insert_string(av
[ac
- 1]) : CSstay
;
1261 STATIC CONST KEYMAP Map
[32] = {
1262 { CTL('@'), ring_bell
},
1263 { CTL('A'), beg_line
},
1264 { CTL('B'), bk_char
},
1265 { CTL('D'), del_char
},
1266 { CTL('E'), end_line
},
1267 { CTL('F'), fd_char
},
1268 { CTL('G'), ring_bell
},
1269 { CTL('H'), bk_del_char
},
1270 { CTL('J'), accept_line
},
1271 { CTL('K'), kill_line
},
1272 { CTL('L'), redisplay
},
1273 { CTL('M'), accept_line
},
1274 { CTL('N'), h_next
},
1275 { CTL('O'), ring_bell
},
1276 { CTL('P'), h_prev
},
1277 { CTL('Q'), ring_bell
},
1278 { CTL('R'), h_search
},
1279 { CTL('S'), ring_bell
},
1280 { CTL('T'), transpose
},
1281 { CTL('U'), ring_bell
},
1282 { CTL('V'), quote
},
1284 { CTL('X'), exchange
},
1286 { CTL('Z'), ring_bell
},
1288 { CTL(']'), move_to_char
},
1289 { CTL('^'), ring_bell
},
1290 { CTL('_'), ring_bell
},
1294 STATIC CONST KEYMAP MetaMap
[16]= {
1295 { CTL('H'), bk_kill_word
},
1296 { DEL
, bk_kill_word
},
1298 { '.', last_argument
},
1302 { 'd', fd_kill_word
},
1304 { 'l', case_down_word
},
1305 { 'm', toggle_meta_mode
},
1306 { 'u', case_up_word
},
1308 { 'w', copy_region
},