1 /* Copyright 1992 Simmule Turner and Rich Salz. All rights reserved.
3 * This software is not subject to any license of the American Telephone
4 * and Telegraph Company or of the Regents of the University of California.
6 * Permission is granted to anyone to use this software for any purpose on
7 * any computer system, and to alter it and redistribute it freely, subject
8 * to the following restrictions:
9 * 1. The authors are not responsible for the consequences of use of this
10 * software, no matter how awful, even if they arise from flaws in it.
11 * 2. The origin of this software must not be misrepresented, either by
12 * explicit claim or by omission. Since few users ever read sources,
13 * credits must appear in the documentation.
14 * 3. Altered versions must be plainly marked as such, and must not be
15 * misrepresented as being the original software. Since few users
16 * ever read sources, credits must appear in the documentation.
17 * 4. This notice may not be removed or altered.
21 ** Main editing routines for editline library.
24 #include "edit_locl.h"
31 ** Manifest constants.
33 #define SCREEN_WIDTH 80
34 #define SCREEN_ROWS 24
37 #define CTL(x) ((x) & 0x1F)
38 #define ISCTL(x) ((x) && (x) < ' ')
39 #define UNCTL(x) ((x) + 64)
40 #define META(x) ((x) | 0x80)
41 #define ISMETA(x) ((x) & 0x80)
42 #define UNMETA(x) ((x) & 0x7F)
43 #if !defined(HIST_SIZE)
45 #endif /* !defined(HIST_SIZE) */
48 ** Command status codes.
50 typedef enum _el_STATUS
{
51 CSdone
, CSeof
, CSmove
, CSdispatch
, CSstay
55 ** The type of case-changing to perform.
62 ** Key to command mapping.
64 typedef struct _KEYMAP
{
66 el_STATUS (*Function
)(void);
70 ** Command history structure.
72 typedef struct _HISTORY
{
75 unsigned char *Lines
[HIST_SIZE
];
86 static unsigned char NIL
[] = "";
87 static const unsigned char *Input
= NIL
;
88 static unsigned char *Line
;
89 static const char *Prompt
;
90 static unsigned char *Yanked
;
92 static char NEWLINE
[]= CRLF
;
102 static KEYMAP Map
[33];
103 static KEYMAP MetaMap
[16];
104 static size_t Length
;
105 static size_t ScreenCount
;
106 static size_t ScreenSize
;
107 static char *backspace
;
111 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
112 int rl_meta_chars
= 1;
117 static unsigned char *editinput(void);
118 char *tgetstr(const char*, char**);
119 int tgetent(char*, const char*);
120 int tgetnum(const char*);
123 ** TTY input/output functions.
130 write(1, Screen
, ScreenCount
);
136 TTYput(unsigned char c
)
138 Screen
[ScreenCount
] = c
;
139 if (++ScreenCount
>= ScreenSize
- 1) {
140 ScreenSize
+= SCREEN_INC
;
141 Screen
= realloc(Screen
, ScreenSize
);
146 TTYputs(const char *p
)
153 TTYshow(unsigned char c
)
163 else if (rl_meta_chars
&& ISMETA(c
)) {
173 TTYstring(unsigned char *p
)
194 } while(e
< 0 && errno
== EINTR
);
220 #if defined(TIOCGWINSZ)
222 #endif /* defined(TIOCGWINSZ) */
225 #if defined(TIOCGWINSZ)
226 /* Perhaps we got resized. */
227 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0
228 && W
.ws_col
> 0 && W
.ws_row
> 0) {
229 TTYwidth
= (int)W
.ws_col
;
230 TTYrows
= (int)W
.ws_row
;
232 #endif /* defined(TIOCGWINSZ) */
244 TTYwidth
= TTYrows
= 0;
246 if ((term
= getenv("TERM")) == NULL
)
248 if (tgetent(buff
, term
) >= 0) {
250 tmp
= tgetstr("le", &bp
);
252 backspace
= strdup(tmp
);
253 TTYwidth
= tgetnum("co");
254 TTYrows
= tgetnum("li");
260 #if defined(TIOCGWINSZ)
261 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0) {
262 TTYwidth
= (int)W
.ws_col
;
263 TTYrows
= (int)W
.ws_row
;
265 #endif /* defined(TIOCGWINSZ) */
267 if (TTYwidth
<= 0 || TTYrows
<= 0) {
268 TTYwidth
= SCREEN_WIDTH
;
269 TTYrows
= SCREEN_ROWS
;
275 ** Print an array of words in columns.
278 columns(int ac
, unsigned char **av
)
289 /* Find longest name, determine column count from that. */
290 for (longest
= 0, i
= 0; i
< ac
; i
++)
291 if ((j
= strlen((char *)av
[i
])) > longest
)
293 cols
= TTYwidth
/ (longest
+ 3);
296 for (skip
= ac
/ cols
+ 1, i
= 0; i
< skip
; i
++) {
297 for (j
= i
; j
< ac
; j
+= skip
) {
298 for (p
= av
[j
], len
= strlen((char *)p
), k
= len
; --k
>= 0; p
++)
301 while (++len
< longest
+ 3)
316 for (i
= Point
, p
= Line
; --i
>= 0; p
++)
321 left(el_STATUS Change
)
325 if (ISCTL(Line
[Point
- 1]))
327 else if (rl_meta_chars
&& ISMETA(Line
[Point
- 1])) {
332 if (Change
== CSmove
)
337 right(el_STATUS Change
)
339 TTYshow(Line
[Point
]);
340 if (Change
== CSmove
)
353 do_macro(unsigned char c
)
355 unsigned char name
[4];
362 if ((Input
= (unsigned char *)getenv((char *)name
)) == NULL
) {
370 do_forward(el_STATUS move
)
378 for ( ; Point
< End
&& (*p
== ' ' || !isalnum(*p
)); Point
++, p
++)
382 for (; Point
< End
&& isalnum(*p
); Point
++, p
++)
388 } while (++i
< Repeat
);
402 if (OldPoint
!= Point
) {
403 if ((count
= Point
- OldPoint
) < 0)
406 if ((end
= Point
+ count
) > End
)
408 for (i
= Point
, p
= &Line
[i
]; i
< end
; i
++, p
++) {
409 if (type
== TOupper
) {
413 else if (isupper(*p
))
424 return do_case(TOlower
);
430 return do_case(TOupper
);
440 for (extras
= 0, i
= Point
, p
= &Line
[i
]; i
<= End
; i
++, p
++) {
446 else if (rl_meta_chars
&& ISMETA(*p
)) {
453 for (i
+= extras
; i
> Point
; i
--)
460 Point
= -strlen(Prompt
);
469 insert_string(unsigned char *p
)
476 len
= strlen((char *)p
);
477 if (End
+ len
>= Length
) {
478 if ((new = malloc(sizeof(unsigned char) * (Length
+ len
+ MEM_INC
))) == NULL
)
481 memcpy(new, Line
, Length
);
485 Length
+= len
+ MEM_INC
;
488 for (q
= &Line
[Point
], i
= End
- Point
; --i
>= 0; )
490 memcpy(&Line
[Point
], p
, len
);
493 TTYstring(&Line
[Point
]);
496 return Point
== End
? CSstay
: CSmove
;
500 static unsigned char *
503 return H
.Pos
>= H
.Size
- 1 ? NULL
: H
.Lines
[++H
.Pos
];
506 static unsigned char *
509 return H
.Pos
== 0 ? NULL
: H
.Lines
[--H
.Pos
];
513 do_insert_hist(unsigned char *p
)
521 return insert_string(p
);
525 do_hist(unsigned char *(*move
)(void))
532 if ((p
= (*move
)()) == NULL
)
534 } while (++i
< Repeat
);
535 return do_insert_hist(p
);
541 return do_hist(next_hist
);
547 return do_hist(prev_hist
);
553 return do_insert_hist(H
.Lines
[H
.Pos
= 0]);
559 return do_insert_hist(H
.Lines
[H
.Pos
= H
.Size
- 1]);
563 ** Return zero if pat appears as a substring in text.
566 substrcmp(const char *text
, const char *pat
, size_t len
)
570 if ((c
= *pat
) == '\0')
571 return *text
== '\0';
572 for ( ; *text
; text
++)
573 if (*text
== c
&& strncmp(text
, pat
, len
) == 0)
578 static unsigned char *
579 search_hist(unsigned char *search
, unsigned char *(*move
)(void))
581 static unsigned char *old_search
;
584 int (*match
)(const char *, const char *, size_t);
587 /* Save or get remembered search pattern. */
588 if (search
&& *search
) {
591 old_search
= (unsigned char *)strdup((char *)search
);
594 if (old_search
== NULL
|| *old_search
== '\0')
599 /* Set up pattern-finder. */
600 if (*search
== '^') {
602 pat
= (char *)(search
+ 1);
606 pat
= (char *)search
;
610 for (pos
= H
.Pos
; (*move
)() != NULL
; )
611 if ((*match
)((char *)H
.Lines
[H
.Pos
], pat
, len
) == 0)
612 return H
.Lines
[H
.Pos
];
620 static int Searching
;
621 const char *old_prompt
;
622 unsigned char *(*move
)(void);
633 move
= Repeat
== NO_ARG
? prev_hist
: next_hist
;
634 p
= search_hist(editinput(), move
);
640 return do_insert_hist(p
);
653 } while (++i
< Repeat
);
658 save_yank(int begin
, int i
)
668 if ((Yanked
= malloc(sizeof(unsigned char) * (i
+ 1))) != NULL
) {
669 memcpy(Yanked
, &Line
[begin
], i
);
675 delete_string(int count
)
680 if (count
<= 0 || End
== Point
)
683 if (count
== 1 && Point
== End
- 1) {
684 /* Optimize common case of delete at end of line. */
693 else if (rl_meta_chars
&& ISMETA(*p
)) {
702 if (Point
+ count
> End
&& (count
= End
- Point
) <= 0)
706 save_yank(Point
, count
);
708 for (p
= &Line
[Point
], i
= End
- (Point
+ count
) + 1; --i
>= 0; p
++)
712 TTYstring(&Line
[Point
]);
726 } while (++i
< Repeat
);
741 } while (++i
< Repeat
);
743 return delete_string(i
);
760 if (Repeat
!= NO_ARG
) {
761 if (Repeat
< Point
) {
765 delete_string(i
- Point
);
767 else if (Repeat
> Point
) {
769 delete_string(Repeat
- Point
- 1);
774 save_yank(Point
, End
- Point
);
785 unsigned char buff
[2];
790 if (Repeat
== NO_ARG
|| Repeat
< 2) {
793 return insert_string(buff
);
796 if ((p
= malloc(Repeat
+ 1)) == NULL
)
798 for (i
= Repeat
, q
= p
; --i
>= 0; )
802 s
= insert_string(p
);
813 if ((c
= TTYget()) == EOF
)
815 /* Also include VT-100 arrows. */
816 if (c
== '[' || c
== 'O')
817 switch (c
= TTYget()) {
818 default: return ring_bell();
819 case EOF
: return CSeof
;
820 case 'A': return h_prev();
821 case 'B': return h_next();
822 case 'C': return fd_char();
823 case 'D': return bk_char();
827 for (Repeat
= c
- '0'; (c
= TTYget()) != EOF
&& isdigit(c
); )
828 Repeat
= Repeat
* 10 + c
- '0';
836 for (OldPoint
= Point
, kp
= MetaMap
; kp
->Function
; kp
++)
838 return (*kp
->Function
)();
844 emacs(unsigned int c
)
851 PushBack
= UNMETA(c
);
854 for (kp
= Map
; kp
->Function
; kp
++)
857 s
= kp
->Function
? (*kp
->Function
)() : insert_char((int)c
);
859 /* No pushback means no repeat count; hacky, but true. */
865 TTYspecial(unsigned int c
)
870 if (c
== rl_erase
|| c
== DEL
)
871 return bk_del_char();
880 if (c
== rl_intr
|| c
== rl_quit
) {
885 if (c
== rl_eof
&& Point
== 0 && End
== 0)
891 static unsigned char *
897 OldPoint
= Point
= Mark
= End
= 0;
900 while ((c
= TTYget()) != EOF
)
901 switch (TTYspecial(c
)) {
930 hist_add(unsigned char *p
)
934 if ((p
= (unsigned char *)strdup((char *)p
)) == NULL
)
936 if (H
.Size
< HIST_SIZE
)
937 H
.Lines
[H
.Size
++] = p
;
940 for (i
= 0; i
< HIST_SIZE
- 1; i
++)
941 H
.Lines
[i
] = H
.Lines
[i
+ 1];
948 ** For compatibility with FSF readline.
952 rl_reset_terminal(char *p
)
962 readline(const char* prompt
)
968 if ((Line
= malloc(Length
)) == NULL
)
975 ScreenSize
= SCREEN_INC
;
976 Screen
= malloc(ScreenSize
);
977 Prompt
= prompt
? prompt
: (char *)NIL
;
979 if ((line
= editinput()) != NULL
) {
980 line
= (unsigned char *)strdup((char *)line
);
986 free(H
.Lines
[--H
.Size
]);
993 if (p
== NULL
|| *p
== '\0')
996 #if defined(UNIQUE_HISTORY)
997 if (H
.Pos
&& strcmp(p
, H
.Lines
[H
.Pos
- 1]) == 0)
999 #endif /* defined(UNIQUE_HISTORY) */
1000 hist_add((unsigned char *)p
);
1017 return delete_string(Repeat
== NO_ARG
? 1 : Repeat
);
1031 ** Move back to the beginning of the current word and return an
1032 ** allocated copy of it.
1034 static unsigned char *
1037 static char SEPS
[] = "#;&|^$=`'{}()<>\n\t ";
1042 for (p
= &Line
[Point
]; p
> Line
&& strchr(SEPS
, (char)p
[-1]) == NULL
; p
--)
1044 len
= Point
- (p
- Line
) + 1;
1045 if ((new = malloc(len
)) == NULL
)
1047 memcpy(new, p
, len
);
1048 new[len
- 1] = '\0';
1056 unsigned char *word
;
1061 p
= (unsigned char *)rl_complete((char *)word
, &unique
);
1065 s
= insert_string(p
);
1078 unsigned char *word
;
1082 ac
= rl_list_possib((char *)word
, (void *)&av
);
1110 c
= Line
[Point
- 1];
1112 Line
[Point
- 1] = Line
[Point
];
1113 TTYshow(Line
[Point
- 1]);
1125 return (c
= TTYget()) == EOF
? CSeof
: insert_char((int)c
);
1143 return delete_string(Mark
- Point
);
1158 if ((c
= TTYget()) != CTL('X'))
1159 return c
== EOF
? CSeof
: ring_bell();
1161 if ((c
= Mark
) <= End
) {
1172 if (Yanked
&& *Yanked
)
1173 return insert_string(Yanked
);
1184 save_yank(Mark
, Point
- Mark
);
1186 save_yank(Point
, Mark
- Point
);
1198 if ((c
= TTYget()) == EOF
)
1200 for (i
= Point
+ 1, p
= &Line
[i
]; i
< End
; i
++, p
++)
1211 return do_forward(CSmove
);
1220 if (OldPoint
!= Point
) {
1221 i
= Point
- OldPoint
;
1223 return delete_string(i
);
1236 for (p
= &Line
[Point
]; p
> Line
&& !isalnum(p
[-1]); p
--)
1239 for (; p
> Line
&& p
[-1] != ' ' && isalnum(p
[-1]); p
--)
1244 } while (++i
< Repeat
);
1253 if (OldPoint
!= Point
)
1254 return delete_string(OldPoint
- Point
);
1259 argify(unsigned char *line
, unsigned char ***avp
)
1263 unsigned char **new;
1268 if ((*avp
= p
= malloc(sizeof(unsigned char*) * i
))== NULL
)
1271 for (c
= line
; isspace(*c
); c
++)
1273 if (*c
== '\n' || *c
== '\0')
1276 for (ac
= 0, p
[ac
++] = c
; *c
&& *c
!= '\n'; ) {
1279 if (*c
&& *c
!= '\n') {
1281 new = malloc(sizeof(unsigned char*) * (i
+ MEM_INC
));
1286 memcpy(new, p
, i
* sizeof (char **));
1310 if (H
.Size
== 1 || (p
= H
.Lines
[H
.Size
- 2]) == NULL
)
1313 if ((p
= (unsigned char *)strdup((char *)p
)) == NULL
)
1315 ac
= argify(p
, &av
);
1317 if (Repeat
!= NO_ARG
)
1318 s
= Repeat
< ac
? insert_string(av
[Repeat
]) : ring_bell();
1320 s
= ac
? insert_string(av
[ac
- 1]) : CSstay
;
1328 static KEYMAP Map
[33] = {
1329 { CTL('@'), ring_bell
},
1330 { CTL('A'), beg_line
},
1331 { CTL('B'), bk_char
},
1332 { CTL('D'), del_char
},
1333 { CTL('E'), end_line
},
1334 { CTL('F'), fd_char
},
1335 { CTL('G'), ring_bell
},
1336 { CTL('H'), bk_del_char
},
1337 { CTL('I'), c_complete
},
1338 { CTL('J'), accept_line
},
1339 { CTL('K'), kill_line
},
1340 { CTL('L'), redisplay
},
1341 { CTL('M'), accept_line
},
1342 { CTL('N'), h_next
},
1343 { CTL('O'), ring_bell
},
1344 { CTL('P'), h_prev
},
1345 { CTL('Q'), ring_bell
},
1346 { CTL('R'), h_search
},
1347 { CTL('S'), ring_bell
},
1348 { CTL('T'), transpose
},
1349 { CTL('U'), ring_bell
},
1350 { CTL('V'), quote
},
1352 { CTL('X'), exchange
},
1354 { CTL('Z'), ring_bell
},
1356 { CTL(']'), move_to_char
},
1357 { CTL('^'), ring_bell
},
1358 { CTL('_'), ring_bell
},
1362 static KEYMAP MetaMap
[16]= {
1363 { CTL('H'), bk_kill_word
},
1364 { DEL
, bk_kill_word
},
1366 { '.', last_argument
},
1369 { '?', c_possible
},
1371 { 'd', fd_kill_word
},
1373 { 'l', case_down_word
},
1374 { 'u', case_up_word
},
1376 { 'w', copy_region
},