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"
28 __RCSID("$Heimdal: editline.c 15581 2005-07-07 20:55:18Z lha $"
32 ** Manifest constants.
34 #define SCREEN_WIDTH 80
35 #define SCREEN_ROWS 24
38 #define CTL(x) ((x) & 0x1F)
39 #define ISCTL(x) ((x) && (x) < ' ')
40 #define UNCTL(x) ((x) + 64)
41 #define META(x) ((x) | 0x80)
42 #define ISMETA(x) ((x) & 0x80)
43 #define UNMETA(x) ((x) & 0x7F)
44 #if !defined(HIST_SIZE)
46 #endif /* !defined(HIST_SIZE) */
49 ** Command status codes.
51 typedef enum _el_STATUS
{
52 CSdone
, CSeof
, CSmove
, CSdispatch
, CSstay
56 ** The type of case-changing to perform.
63 ** Key to command mapping.
65 typedef struct _KEYMAP
{
67 el_STATUS (*Function
)(void);
71 ** Command history structure.
73 typedef struct _HISTORY
{
76 unsigned char *Lines
[HIST_SIZE
];
87 static unsigned char NIL
[] = "";
88 static const unsigned char *Input
= NIL
;
89 static unsigned char *Line
;
90 static const char *Prompt
;
91 static unsigned char *Yanked
;
93 static char NEWLINE
[]= CRLF
;
103 static KEYMAP Map
[33];
104 static KEYMAP MetaMap
[16];
105 static size_t Length
;
106 static size_t ScreenCount
;
107 static size_t ScreenSize
;
108 static char *backspace
;
112 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
113 int rl_meta_chars
= 1;
118 static unsigned char *editinput(void);
119 char *tgetstr(const char*, char**);
120 int tgetent(char*, const char*);
121 int tgetnum(const char*);
124 ** TTY input/output functions.
131 write(1, Screen
, ScreenCount
);
137 TTYput(unsigned char c
)
139 Screen
[ScreenCount
] = c
;
140 if (++ScreenCount
>= ScreenSize
- 1) {
141 ScreenSize
+= SCREEN_INC
;
142 Screen
= realloc(Screen
, ScreenSize
);
147 TTYputs(const char *p
)
154 TTYshow(unsigned char c
)
164 else if (rl_meta_chars
&& ISMETA(c
)) {
174 TTYstring(unsigned char *p
)
195 } while(e
< 0 && errno
== EINTR
);
221 #if defined(TIOCGWINSZ)
223 #endif /* defined(TIOCGWINSZ) */
226 #if defined(TIOCGWINSZ)
227 /* Perhaps we got resized. */
228 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0
229 && W
.ws_col
> 0 && W
.ws_row
> 0) {
230 TTYwidth
= (int)W
.ws_col
;
231 TTYrows
= (int)W
.ws_row
;
233 #endif /* defined(TIOCGWINSZ) */
245 TTYwidth
= TTYrows
= 0;
247 if ((term
= getenv("TERM")) == NULL
)
249 if (tgetent(buff
, term
) >= 0) {
251 tmp
= tgetstr("le", &bp
);
253 backspace
= strdup(tmp
);
254 TTYwidth
= tgetnum("co");
255 TTYrows
= tgetnum("li");
261 #if defined(TIOCGWINSZ)
262 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0) {
263 TTYwidth
= (int)W
.ws_col
;
264 TTYrows
= (int)W
.ws_row
;
266 #endif /* defined(TIOCGWINSZ) */
268 if (TTYwidth
<= 0 || TTYrows
<= 0) {
269 TTYwidth
= SCREEN_WIDTH
;
270 TTYrows
= SCREEN_ROWS
;
276 ** Print an array of words in columns.
279 columns(int ac
, unsigned char **av
)
290 /* Find longest name, determine column count from that. */
291 for (longest
= 0, i
= 0; i
< ac
; i
++)
292 if ((j
= strlen((char *)av
[i
])) > longest
)
294 cols
= TTYwidth
/ (longest
+ 3);
297 for (skip
= ac
/ cols
+ 1, i
= 0; i
< skip
; i
++) {
298 for (j
= i
; j
< ac
; j
+= skip
) {
299 for (p
= av
[j
], len
= strlen((char *)p
), k
= len
; --k
>= 0; p
++)
302 while (++len
< longest
+ 3)
317 for (i
= Point
, p
= Line
; --i
>= 0; p
++)
322 left(el_STATUS Change
)
326 if (ISCTL(Line
[Point
- 1]))
328 else if (rl_meta_chars
&& ISMETA(Line
[Point
- 1])) {
333 if (Change
== CSmove
)
338 right(el_STATUS Change
)
340 TTYshow(Line
[Point
]);
341 if (Change
== CSmove
)
354 do_macro(unsigned char c
)
356 unsigned char name
[4];
363 if ((Input
= (unsigned char *)getenv((char *)name
)) == NULL
) {
371 do_forward(el_STATUS move
)
379 for ( ; Point
< End
&& (*p
== ' ' || !isalnum(*p
)); Point
++, p
++)
383 for (; Point
< End
&& isalnum(*p
); Point
++, p
++)
389 } while (++i
< Repeat
);
403 if (OldPoint
!= Point
) {
404 if ((count
= Point
- OldPoint
) < 0)
407 if ((end
= Point
+ count
) > End
)
409 for (i
= Point
, p
= &Line
[i
]; i
< end
; i
++, p
++) {
410 if (type
== TOupper
) {
414 else if (isupper(*p
))
425 return do_case(TOlower
);
431 return do_case(TOupper
);
441 for (extras
= 0, i
= Point
, p
= &Line
[i
]; i
<= End
; i
++, p
++) {
447 else if (rl_meta_chars
&& ISMETA(*p
)) {
454 for (i
+= extras
; i
> Point
; i
--)
461 Point
= -strlen(Prompt
);
470 insert_string(unsigned char *p
)
477 len
= strlen((char *)p
);
478 if (End
+ len
>= Length
) {
479 if ((new = malloc(sizeof(unsigned char) * (Length
+ len
+ MEM_INC
))) == NULL
)
482 memcpy(new, Line
, Length
);
486 Length
+= len
+ MEM_INC
;
489 for (q
= &Line
[Point
], i
= End
- Point
; --i
>= 0; )
491 memcpy(&Line
[Point
], p
, len
);
494 TTYstring(&Line
[Point
]);
497 return Point
== End
? CSstay
: CSmove
;
501 static unsigned char *
504 return H
.Pos
>= H
.Size
- 1 ? NULL
: H
.Lines
[++H
.Pos
];
507 static unsigned char *
510 return H
.Pos
== 0 ? NULL
: H
.Lines
[--H
.Pos
];
514 do_insert_hist(unsigned char *p
)
522 return insert_string(p
);
526 do_hist(unsigned char *(*move
)(void))
533 if ((p
= (*move
)()) == NULL
)
535 } while (++i
< Repeat
);
536 return do_insert_hist(p
);
542 return do_hist(next_hist
);
548 return do_hist(prev_hist
);
554 return do_insert_hist(H
.Lines
[H
.Pos
= 0]);
560 return do_insert_hist(H
.Lines
[H
.Pos
= H
.Size
- 1]);
564 ** Return zero if pat appears as a substring in text.
567 substrcmp(const char *text
, const char *pat
, size_t len
)
571 if ((c
= *pat
) == '\0')
572 return *text
== '\0';
573 for ( ; *text
; text
++)
574 if (*text
== c
&& strncmp(text
, pat
, len
) == 0)
579 static unsigned char *
580 search_hist(unsigned char *search
, unsigned char *(*move
)(void))
582 static unsigned char *old_search
;
585 int (*match
)(const char *, const char *, size_t);
588 /* Save or get remembered search pattern. */
589 if (search
&& *search
) {
592 old_search
= (unsigned char *)strdup((char *)search
);
595 if (old_search
== NULL
|| *old_search
== '\0')
600 /* Set up pattern-finder. */
601 if (*search
== '^') {
603 pat
= (char *)(search
+ 1);
607 pat
= (char *)search
;
611 for (pos
= H
.Pos
; (*move
)() != NULL
; )
612 if ((*match
)((char *)H
.Lines
[H
.Pos
], pat
, len
) == 0)
613 return H
.Lines
[H
.Pos
];
621 static int Searching
;
622 const char *old_prompt
;
623 unsigned char *(*move
)(void);
634 move
= Repeat
== NO_ARG
? prev_hist
: next_hist
;
635 p
= search_hist(editinput(), move
);
641 return do_insert_hist(p
);
654 } while (++i
< Repeat
);
659 save_yank(int begin
, int i
)
669 if ((Yanked
= malloc(sizeof(unsigned char) * (i
+ 1))) != NULL
) {
670 memcpy(Yanked
, &Line
[begin
], i
);
676 delete_string(int count
)
681 if (count
<= 0 || End
== Point
)
684 if (count
== 1 && Point
== End
- 1) {
685 /* Optimize common case of delete at end of line. */
694 else if (rl_meta_chars
&& ISMETA(*p
)) {
703 if (Point
+ count
> End
&& (count
= End
- Point
) <= 0)
707 save_yank(Point
, count
);
709 for (p
= &Line
[Point
], i
= End
- (Point
+ count
) + 1; --i
>= 0; p
++)
713 TTYstring(&Line
[Point
]);
727 } while (++i
< Repeat
);
742 } while (++i
< Repeat
);
744 return delete_string(i
);
761 if (Repeat
!= NO_ARG
) {
762 if (Repeat
< Point
) {
766 delete_string(i
- Point
);
768 else if (Repeat
> Point
) {
770 delete_string(Repeat
- Point
- 1);
775 save_yank(Point
, End
- Point
);
786 unsigned char buff
[2];
791 if (Repeat
== NO_ARG
|| Repeat
< 2) {
794 return insert_string(buff
);
797 if ((p
= malloc(Repeat
+ 1)) == NULL
)
799 for (i
= Repeat
, q
= p
; --i
>= 0; )
803 s
= insert_string(p
);
814 if ((c
= TTYget()) == EOF
)
816 /* Also include VT-100 arrows. */
817 if (c
== '[' || c
== 'O')
818 switch (c
= TTYget()) {
819 default: return ring_bell();
820 case EOF
: return CSeof
;
821 case 'A': return h_prev();
822 case 'B': return h_next();
823 case 'C': return fd_char();
824 case 'D': return bk_char();
828 for (Repeat
= c
- '0'; (c
= TTYget()) != EOF
&& isdigit(c
); )
829 Repeat
= Repeat
* 10 + c
- '0';
837 for (OldPoint
= Point
, kp
= MetaMap
; kp
->Function
; kp
++)
839 return (*kp
->Function
)();
845 emacs(unsigned int c
)
852 PushBack
= UNMETA(c
);
855 for (kp
= Map
; kp
->Function
; kp
++)
858 s
= kp
->Function
? (*kp
->Function
)() : insert_char((int)c
);
860 /* No pushback means no repeat count; hacky, but true. */
866 TTYspecial(unsigned int c
)
871 if (c
== rl_erase
|| c
== DEL
)
872 return bk_del_char();
881 if (c
== rl_intr
|| c
== rl_quit
) {
886 if (c
== rl_eof
&& Point
== 0 && End
== 0)
892 static unsigned char *
898 OldPoint
= Point
= Mark
= End
= 0;
901 while ((c
= TTYget()) != EOF
)
902 switch (TTYspecial(c
)) {
931 hist_add(unsigned char *p
)
935 if ((p
= (unsigned char *)strdup((char *)p
)) == NULL
)
937 if (H
.Size
< HIST_SIZE
)
938 H
.Lines
[H
.Size
++] = p
;
941 for (i
= 0; i
< HIST_SIZE
- 1; i
++)
942 H
.Lines
[i
] = H
.Lines
[i
+ 1];
949 ** For compatibility with FSF readline.
953 rl_reset_terminal(char *p
)
963 readline(const char* prompt
)
969 if ((Line
= malloc(Length
)) == NULL
)
976 ScreenSize
= SCREEN_INC
;
977 Screen
= malloc(ScreenSize
);
978 Prompt
= prompt
? prompt
: (char *)NIL
;
980 if ((line
= editinput()) != NULL
) {
981 line
= (unsigned char *)strdup((char *)line
);
987 free(H
.Lines
[--H
.Size
]);
994 if (p
== NULL
|| *p
== '\0')
997 #if defined(UNIQUE_HISTORY)
998 if (H
.Pos
&& strcmp(p
, H
.Lines
[H
.Pos
- 1]) == 0)
1000 #endif /* defined(UNIQUE_HISTORY) */
1001 hist_add((unsigned char *)p
);
1018 return delete_string(Repeat
== NO_ARG
? 1 : Repeat
);
1032 ** Move back to the beginning of the current word and return an
1033 ** allocated copy of it.
1035 static unsigned char *
1038 static char SEPS
[] = "#;&|^$=`'{}()<>\n\t ";
1043 for (p
= &Line
[Point
]; p
> Line
&& strchr(SEPS
, (char)p
[-1]) == NULL
; p
--)
1045 len
= Point
- (p
- Line
) + 1;
1046 if ((new = malloc(len
)) == NULL
)
1048 memcpy(new, p
, len
);
1049 new[len
- 1] = '\0';
1057 unsigned char *word
;
1062 p
= (unsigned char *)rl_complete((char *)word
, &unique
);
1066 s
= insert_string(p
);
1079 unsigned char *word
;
1083 ac
= rl_list_possib((char *)word
, (void *)&av
);
1111 c
= Line
[Point
- 1];
1113 Line
[Point
- 1] = Line
[Point
];
1114 TTYshow(Line
[Point
- 1]);
1126 return (c
= TTYget()) == EOF
? CSeof
: insert_char((int)c
);
1144 return delete_string(Mark
- Point
);
1159 if ((c
= TTYget()) != CTL('X'))
1160 return c
== EOF
? CSeof
: ring_bell();
1162 if ((c
= Mark
) <= End
) {
1173 if (Yanked
&& *Yanked
)
1174 return insert_string(Yanked
);
1185 save_yank(Mark
, Point
- Mark
);
1187 save_yank(Point
, Mark
- Point
);
1199 if ((c
= TTYget()) == EOF
)
1201 for (i
= Point
+ 1, p
= &Line
[i
]; i
< End
; i
++, p
++)
1212 return do_forward(CSmove
);
1221 if (OldPoint
!= Point
) {
1222 i
= Point
- OldPoint
;
1224 return delete_string(i
);
1237 for (p
= &Line
[Point
]; p
> Line
&& !isalnum(p
[-1]); p
--)
1240 for (; p
> Line
&& p
[-1] != ' ' && isalnum(p
[-1]); p
--)
1245 } while (++i
< Repeat
);
1254 if (OldPoint
!= Point
)
1255 return delete_string(OldPoint
- Point
);
1260 argify(unsigned char *line
, unsigned char ***avp
)
1264 unsigned char **new;
1269 if ((*avp
= p
= malloc(sizeof(unsigned char*) * i
))== NULL
)
1272 for (c
= line
; isspace(*c
); c
++)
1274 if (*c
== '\n' || *c
== '\0')
1277 for (ac
= 0, p
[ac
++] = c
; *c
&& *c
!= '\n'; ) {
1280 if (*c
&& *c
!= '\n') {
1282 new = malloc(sizeof(unsigned char*) * (i
+ MEM_INC
));
1287 memcpy(new, p
, i
* sizeof (char **));
1311 if (H
.Size
== 1 || (p
= H
.Lines
[H
.Size
- 2]) == NULL
)
1314 if ((p
= (unsigned char *)strdup((char *)p
)) == NULL
)
1316 ac
= argify(p
, &av
);
1318 if (Repeat
!= NO_ARG
)
1319 s
= Repeat
< ac
? insert_string(av
[Repeat
]) : ring_bell();
1321 s
= ac
? insert_string(av
[ac
- 1]) : CSstay
;
1329 static KEYMAP Map
[33] = {
1330 { CTL('@'), ring_bell
},
1331 { CTL('A'), beg_line
},
1332 { CTL('B'), bk_char
},
1333 { CTL('D'), del_char
},
1334 { CTL('E'), end_line
},
1335 { CTL('F'), fd_char
},
1336 { CTL('G'), ring_bell
},
1337 { CTL('H'), bk_del_char
},
1338 { CTL('I'), c_complete
},
1339 { CTL('J'), accept_line
},
1340 { CTL('K'), kill_line
},
1341 { CTL('L'), redisplay
},
1342 { CTL('M'), accept_line
},
1343 { CTL('N'), h_next
},
1344 { CTL('O'), ring_bell
},
1345 { CTL('P'), h_prev
},
1346 { CTL('Q'), ring_bell
},
1347 { CTL('R'), h_search
},
1348 { CTL('S'), ring_bell
},
1349 { CTL('T'), transpose
},
1350 { CTL('U'), ring_bell
},
1351 { CTL('V'), quote
},
1353 { CTL('X'), exchange
},
1355 { CTL('Z'), ring_bell
},
1357 { CTL(']'), move_to_char
},
1358 { CTL('^'), ring_bell
},
1359 { CTL('_'), ring_bell
},
1363 static KEYMAP MetaMap
[16]= {
1364 { CTL('H'), bk_kill_word
},
1365 { DEL
, bk_kill_word
},
1367 { '.', last_argument
},
1370 { '?', c_possible
},
1372 { 'd', fd_kill_word
},
1374 { 'l', case_down_word
},
1375 { 'u', case_up_word
},
1377 { 'w', copy_region
},