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
31 #include <sys/ioctl.h>
32 #include <sys/types.h>
35 typedef unsigned char CHAR
;
38 ** Manifest constants.
40 #define SCREEN_WIDTH 80
41 #define SCREEN_ROWS 24
44 #define CTL(x) ((x) & 0x1F)
45 #define ISCTL(x) ((x) && (x) < ' ')
46 #define UNCTL(x) ((x) + 64)
47 #define META(x) ((x) | 0x80)
48 #define ISMETA(x) ((x) & 0x80)
49 #define UNMETA(x) ((x) & 0x7F)
50 #if !defined(HIST_SIZE)
52 #endif /* !defined(HIST_SIZE) */
55 #define SCREEN_INC 256
57 #define DISPOSE(p) free((char *)(p))
59 ((T *)malloc((unsigned int)(sizeof (T) * (c))))
60 #define RENEW(p, T, c) \
61 (p = (T *)realloc((char *)(p), (unsigned int)(sizeof (T) * (c))))
62 #define COPYFROMTO(new, p, len) \
63 (void)memcpy((char *)(new), (char *)(p), (int)(len))
66 ** Command status codes.
68 typedef enum _STATUS
{
69 CSdone
, CSeof
, CSmove
, CSdispatch
, CSstay
73 ** The type of case-changing to perform.
80 ** Key to command mapping.
82 typedef struct _KEYMAP
{
88 ** Command history structure.
90 typedef struct _HISTORY
{
93 CHAR
*Lines
[HIST_SIZE
];
104 static CHAR NIL
[] = "";
105 static const CHAR
*Input
= NIL
;
107 static const char *Prompt
;
110 static char NEWLINE
[]= CRLF
;
120 static KEYMAP Map
[33];
121 static KEYMAP MetaMap
[16];
122 static size_t Length
;
123 static size_t ScreenCount
;
124 static size_t ScreenSize
;
125 static char *backspace
;
129 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
130 int rl_meta_chars
= 1;
135 static CHAR
*editinput();
138 #if defined(USE_TERMCAP)
139 extern char *getenv();
140 extern char *tgetstr();
141 extern int tgetent();
142 #endif /* defined(USE_TERMCAP) */
145 ** TTY input/output functions.
148 #ifdef HAVE_TCGETATTR
155 static struct termios old
;
159 (void)tcgetattr(0, &old
);
160 rl_erase
= old
.c_cc
[VERASE
];
161 rl_kill
= old
.c_cc
[VKILL
];
162 rl_eof
= old
.c_cc
[VEOF
];
163 rl_intr
= old
.c_cc
[VINTR
];
164 rl_quit
= old
.c_cc
[VQUIT
];
167 new.c_cc
[VINTR
] = -1;
168 new.c_cc
[VQUIT
] = -1;
169 new.c_lflag
&= ~(ECHO
| ICANON
);
170 new.c_iflag
&= ~(ISTRIP
| INPCK
);
173 (void)tcsetattr(0, TCSANOW
, &new);
176 (void)tcsetattr(0, TCSANOW
, &old
);
179 #else /* HAVE_TCGETATTR */
185 static struct sgttyb old_sgttyb
;
186 static struct tchars old_tchars
;
187 struct sgttyb new_sgttyb
;
188 struct tchars new_tchars
;
191 (void)ioctl(0, TIOCGETP
, &old_sgttyb
);
192 rl_erase
= old_sgttyb
.sg_erase
;
193 rl_kill
= old_sgttyb
.sg_kill
;
195 (void)ioctl(0, TIOCGETC
, &old_tchars
);
196 rl_eof
= old_tchars
.t_eofc
;
197 rl_intr
= old_tchars
.t_intrc
;
198 rl_quit
= old_tchars
.t_quitc
;
200 new_sgttyb
= old_sgttyb
;
201 new_sgttyb
.sg_flags
&= ~ECHO
;
202 new_sgttyb
.sg_flags
|= RAW
;
204 new_sgttyb
.sg_flags
|= PASS8
;
205 #endif /* defined(PASS8) */
206 (void)ioctl(0, TIOCSETP
, &new_sgttyb
);
208 new_tchars
= old_tchars
;
209 new_tchars
.t_intrc
= -1;
210 new_tchars
.t_quitc
= -1;
211 (void)ioctl(0, TIOCSETC
, &new_tchars
);
214 (void)ioctl(0, TIOCSETP
, &old_sgttyb
);
215 (void)ioctl(0, TIOCSETC
, &old_tchars
);
219 #endif /* HAVE_TCGETATTR */
225 (void)write(1, Screen
, ScreenCount
);
234 Screen
[ScreenCount
] = c
;
235 if (++ScreenCount
>= ScreenSize
- 1) {
236 ScreenSize
+= SCREEN_INC
;
237 RENEW(Screen
, char, ScreenSize
);
261 else if (rl_meta_chars
&& ISMETA(c
)) {
290 return read(0, &c
, (size_t)1) == 1 ? c
: EOF
;
293 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
307 #if defined(USE_TERMCAP)
311 #endif /* defined(USE_TERMCAP) */
312 #if defined(TIOCGWINSZ)
314 #endif /* defined(TIOCGWINSZ) */
317 #if defined(TIOCGWINSZ)
318 /* Perhaps we got resized. */
319 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0
320 && W
.ws_col
> 0 && W
.ws_row
> 0) {
321 TTYwidth
= (int)W
.ws_col
;
322 TTYrows
= (int)W
.ws_row
;
324 #endif /* defined(TIOCGWINSZ) */
329 TTYwidth
= TTYrows
= 0;
330 #if defined(USE_TERMCAP)
332 if ((term
= getenv("TERM")) == NULL
)
334 if (tgetent(buff
, term
) < 0) {
335 TTYwidth
= SCREEN_WIDTH
;
336 TTYrows
= SCREEN_ROWS
;
339 backspace
= tgetstr("le", &bp
);
340 TTYwidth
= tgetnum("co");
341 TTYrows
= tgetnum("li");
342 #endif /* defined(USE_TERMCAP) */
344 #if defined(TIOCGWINSZ)
345 if (ioctl(0, TIOCGWINSZ
, &W
) >= 0) {
346 TTYwidth
= (int)W
.ws_col
;
347 TTYrows
= (int)W
.ws_row
;
349 #endif /* defined(TIOCGWINSZ) */
351 if (TTYwidth
<= 0 || TTYrows
<= 0) {
352 TTYwidth
= SCREEN_WIDTH
;
353 TTYrows
= SCREEN_ROWS
;
366 TTYputs((CHAR
*)Prompt
);
367 for (i
= Point
, p
= Line
; --i
>= 0; p
++)
377 if (ISCTL(Line
[Point
- 1]))
379 else if (rl_meta_chars
&& ISMETA(Line
[Point
- 1])) {
384 if (Change
== CSmove
)
392 TTYshow(Line
[Point
]);
393 if (Change
== CSmove
)
416 if ((Input
= (CHAR
*)getenv((char *)name
)) == NULL
) {
433 for ( ; Point
< End
&& (*p
== ' ' || !isalnum(*p
)); Point
++, p
++)
437 for (; Point
< End
&& isalnum(*p
); Point
++, p
++)
443 } while (++i
< Repeat
);
457 (void)do_forward(CSstay
);
458 if (OldPoint
!= Point
) {
459 if ((count
= Point
- OldPoint
) < 0)
462 if ((end
= Point
+ count
) > End
)
464 for (i
= Point
, p
= &Line
[i
]; i
< end
; i
++, p
++) {
465 if (type
== TOupper
) {
469 else if (isupper(*p
))
480 return do_case(TOlower
);
486 return do_case(TOupper
);
496 for (extras
= 0, i
= Point
, p
= &Line
[i
]; i
<= End
; i
++, p
++) {
502 else if (rl_meta_chars
&& ISMETA(*p
)) {
509 for (i
+= extras
; i
> Point
; i
--)
516 Point
= -strlen(Prompt
);
533 len
= strlen((char *)p
);
534 if (End
+ len
>= Length
) {
535 if ((new = NEW(CHAR
, Length
+ len
+ MEM_INC
)) == NULL
)
538 COPYFROMTO(new, Line
, Length
);
542 Length
+= len
+ MEM_INC
;
545 for (q
= &Line
[Point
], i
= End
- Point
; --i
>= 0; )
547 COPYFROMTO(&Line
[Point
], p
, len
);
550 TTYstring(&Line
[Point
]);
553 return Point
== End
? CSstay
: CSmove
;
560 return H
.Pos
>= H
.Size
- 1 ? NULL
: H
.Lines
[++H
.Pos
];
566 return H
.Pos
== 0 ? NULL
: H
.Lines
[--H
.Pos
];
579 return insert_string(p
);
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(text
, pat
, len
)
632 if ((c
= *pat
) == '\0')
633 return *text
== '\0';
634 for ( ; *text
; text
++)
635 if ((CHAR
)*text
== c
&& strncmp(text
, pat
, len
) == 0)
641 search_hist(search
, move
)
645 static CHAR
*old_search
;
651 /* Save or get remembered search pattern. */
652 if (search
&& *search
) {
655 old_search
= (CHAR
*)strdup((char *)search
);
658 if (old_search
== NULL
|| *old_search
== '\0')
663 /* Set up pattern-finder. */
664 if (*search
== '^') {
666 pat
= (char *)(search
+ 1);
670 pat
= (char *)search
;
674 for (pos
= H
.Pos
; (*move
)() != NULL
; )
675 if ((*match
)((char *)H
.Lines
[H
.Pos
], pat
, len
) == 0)
676 return H
.Lines
[H
.Pos
];
684 static int Searching
;
685 const char *old_prompt
;
696 TTYputs((CHAR
*)Prompt
);
697 move
= Repeat
== NO_ARG
? prev_hist
: next_hist
;
698 p
= search_hist(editinput(), move
);
701 TTYputs((CHAR
*)Prompt
);
704 return do_insert_hist(p
);
717 } while (++i
< Repeat
);
734 if ((Yanked
= NEW(CHAR
, (size_t)i
+ 1)) != NULL
) {
735 COPYFROMTO(Yanked
, &Line
[begin
], i
);
747 if (count
<= 0 || End
== Point
)
750 if (count
== 1 && Point
== End
- 1) {
751 /* Optimize common case of delete at end of line. */
760 else if (rl_meta_chars
&& ISMETA(*p
)) {
769 if (Point
+ count
> End
&& (count
= End
- Point
) <= 0)
773 save_yank(Point
, count
);
775 for (p
= &Line
[Point
], i
= End
- (Point
+ count
) + 1; --i
>= 0; p
++)
779 TTYstring(&Line
[Point
]);
793 } while (++i
< Repeat
);
808 } while (++i
< Repeat
);
810 return delete_string(i
);
816 TTYputs((CHAR
*)NEWLINE
);
817 TTYputs((CHAR
*)Prompt
);
827 if (Repeat
!= NO_ARG
) {
828 if (Repeat
< Point
) {
832 (void)delete_string(i
- Point
);
834 else if (Repeat
> Point
) {
836 (void)delete_string(Repeat
- Point
- 1);
841 save_yank(Point
, End
- Point
);
858 if (Repeat
== NO_ARG
|| Repeat
< 2) {
861 return insert_string(buff
);
864 if ((p
= NEW(CHAR
, Repeat
+ 1)) == NULL
)
866 for (i
= Repeat
, q
= p
; --i
>= 0; )
870 s
= insert_string(p
);
881 if ((c
= TTYget()) == EOF
)
883 /* Also include VT-100 arrows. */
884 if (c
== '[' || c
== 'O')
885 switch (c
= TTYget()) {
886 default: return ring_bell();
887 case EOF
: return CSeof
;
888 case 'A': return h_prev();
889 case 'B': return h_next();
890 case 'C': return fd_char();
891 case 'D': return bk_char();
895 for (Repeat
= c
- '0'; (c
= TTYget()) != EOF
&& isdigit(c
); )
896 Repeat
= Repeat
* 10 + c
- '0';
904 for (OldPoint
= Point
, kp
= MetaMap
; kp
->Function
; kp
++)
906 return (*kp
->Function
)();
920 PushBack
= UNMETA(c
);
923 for (kp
= Map
; kp
->Function
; kp
++)
926 s
= kp
->Function
? (*kp
->Function
)() : insert_char((int)c
);
928 /* No pushback means no repeat count; hacky, but true. */
940 if (c
== rl_erase
|| c
== DEL
)
941 return bk_del_char();
950 if (c
== rl_intr
|| c
== rl_quit
) {
955 if (c
== rl_eof
&& Point
== 0 && End
== 0)
967 OldPoint
= Point
= Mark
= End
= 0;
970 while ((c
= TTYget()) != EOF
)
971 switch (TTYspecial(c
)) {
1005 if ((p
= (CHAR
*)strdup((char *)p
)) == NULL
)
1007 if (H
.Size
< HIST_SIZE
)
1008 H
.Lines
[H
.Size
++] = p
;
1010 DISPOSE(H
.Lines
[0]);
1011 for (i
= 0; i
< HIST_SIZE
- 1; i
++)
1012 H
.Lines
[i
] = H
.Lines
[i
+ 1];
1026 if ((Line
= NEW(CHAR
, Length
)) == NULL
)
1033 ScreenSize
= SCREEN_INC
;
1034 Screen
= NEW(char, ScreenSize
);
1035 Prompt
= prompt
? prompt
: (char *)NIL
;
1036 TTYputs((CHAR
*)Prompt
);
1037 if ((line
= editinput()) != NULL
) {
1038 line
= (CHAR
*)strdup((char *)line
);
1039 TTYputs((CHAR
*)NEWLINE
);
1044 DISPOSE(H
.Lines
[--H
.Size
]);
1045 return (char *)line
;
1052 if (p
== NULL
|| *p
== '\0')
1055 #if defined(UNIQUE_HISTORY)
1056 if (H
.Pos
&& strcmp(p
, H
.Lines
[H
.Pos
- 1]) == 0)
1058 #endif /* defined(UNIQUE_HISTORY) */
1059 hist_add((CHAR
*)p
);
1076 return delete_string(Repeat
== NO_ARG
? 1 : Repeat
);
1104 c
= Line
[Point
- 1];
1106 Line
[Point
- 1] = Line
[Point
];
1107 TTYshow(Line
[Point
- 1]);
1119 return (c
= TTYget()) == EOF
? CSeof
: insert_char((int)c
);
1137 return delete_string(Mark
- Point
);
1152 if ((c
= TTYget()) != CTL('X'))
1153 return c
== EOF
? CSeof
: ring_bell();
1155 if ((c
= Mark
) <= End
) {
1166 if (Yanked
&& *Yanked
)
1167 return insert_string(Yanked
);
1178 save_yank(Mark
, Point
- Mark
);
1180 save_yank(Point
, Mark
- Point
);
1192 if ((c
= TTYget()) == EOF
)
1194 for (i
= Point
+ 1, p
= &Line
[i
]; i
< End
; i
++, p
++)
1205 return do_forward(CSmove
);
1213 (void)do_forward(CSstay
);
1214 if (OldPoint
!= Point
) {
1215 i
= Point
- OldPoint
;
1217 return delete_string(i
);
1230 for (p
= &Line
[Point
]; p
> Line
&& !isalnum(p
[-1]); p
--)
1233 for (; p
> Line
&& p
[-1] != ' ' && isalnum(p
[-1]); p
--)
1238 } while (++i
< Repeat
);
1247 if (OldPoint
!= Point
)
1248 return delete_string(OldPoint
- Point
);
1264 if ((*avp
= p
= NEW(CHAR
*, i
))== NULL
)
1267 for (c
= line
; isspace(*c
); c
++)
1269 if (*c
== '\n' || *c
== '\0')
1272 for (ac
= 0, p
[ac
++] = c
; *c
&& *c
!= '\n'; ) {
1275 if (*c
&& *c
!= '\n') {
1277 new = NEW(CHAR
*, i
+ MEM_INC
);
1282 COPYFROMTO(new, p
, i
* sizeof (char **));
1306 if (H
.Size
== 1 || (p
= H
.Lines
[H
.Size
- 2]) == NULL
)
1309 if ((p
= (CHAR
*)strdup((char *)p
)) == NULL
)
1311 ac
= argify(p
, &av
);
1313 if (Repeat
!= NO_ARG
)
1314 s
= Repeat
< ac
? insert_string(av
[Repeat
]) : ring_bell();
1316 s
= ac
? insert_string(av
[ac
- 1]) : CSstay
;
1324 static KEYMAP Map
[33] = {
1325 { CTL('@'), ring_bell
},
1326 { CTL('A'), beg_line
},
1327 { CTL('B'), bk_char
},
1328 { CTL('D'), del_char
},
1329 { CTL('E'), end_line
},
1330 { CTL('F'), fd_char
},
1331 { CTL('G'), ring_bell
},
1332 { CTL('H'), bk_del_char
},
1333 { CTL('I'), ring_bell
},
1334 { CTL('J'), accept_line
},
1335 { CTL('K'), kill_line
},
1336 { CTL('L'), redisplay
},
1337 { CTL('M'), accept_line
},
1338 { CTL('N'), h_next
},
1339 { CTL('O'), ring_bell
},
1340 { CTL('P'), h_prev
},
1341 { CTL('Q'), ring_bell
},
1342 { CTL('R'), h_search
},
1343 { CTL('S'), ring_bell
},
1344 { CTL('T'), transpose
},
1345 { CTL('U'), ring_bell
},
1346 { CTL('V'), quote
},
1348 { CTL('X'), exchange
},
1350 { CTL('Z'), ring_bell
},
1352 { CTL(']'), move_to_char
},
1353 { CTL('^'), ring_bell
},
1354 { CTL('_'), ring_bell
},
1358 static KEYMAP MetaMap
[16]= {
1359 { CTL('H'), bk_kill_word
},
1360 { DEL
, bk_kill_word
},
1362 { '.', last_argument
},
1367 { 'd', fd_kill_word
},
1369 { 'l', case_down_word
},
1370 { 'u', case_up_word
},
1372 { 'w', copy_region
},