1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
16 * The Original Code is Mozilla Communicator client code, released
19 * The Initial Developer of the Original Code is
20 * Simmule Turner and Rich Salz.
21 * Portions created by the Initial Developer are Copyright (C) 1998
22 * the Initial Developer. All Rights Reserved.
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
41 * Copyright 1992,1993 Simmule Turner and Rich Salz. All rights reserved.
43 * This software is not subject to any license of the American Telephone
44 * and Telegraph Company or of the Regents of the University of California.
46 * Permission is granted to anyone to use this software for any purpose on
47 * any computer system, and to alter it and redistribute it freely, subject
48 * to the following restrictions:
49 * 1. The authors are not responsible for the consequences of use of this
50 * software, no matter how awful, even if they arise from flaws in it.
51 * 2. The origin of this software must not be misrepresented, either by
52 * explicit claim or by omission. Since few users ever read sources,
53 * credits must appear in the documentation.
54 * 3. Altered versions must be plainly marked as such, and must not be
55 * misrepresented as being the original software. Since few users
56 * ever read sources, credits must appear in the documentation.
57 * 4. This notice may not be removed or altered.
62 ** Main editing routines for editline library.
70 ** Manifest constants.
72 #define SCREEN_WIDTH 80
73 #define SCREEN_ROWS 24
76 #define CTL(x) ((x) & 0x1F)
77 #define ISCTL(x) ((x) && (x) < ' ')
78 #define UNCTL(x) ((x) + 64)
79 #define META(x) ((x) | 0x80)
80 #define ISMETA(x) ((x) & 0x80)
81 #define UNMETA(x) ((x) & 0x7F)
82 #if !defined(HIST_SIZE)
84 #endif /* !defined(HIST_SIZE) */
87 ** Command status codes.
89 typedef enum _STATUS {
90 CSdone, CSeof, CSmove, CSdispatch, CSstay, CSsignal
94 ** The type of case-changing to perform.
101 ** Key to command mapping.
103 typedef struct _KEYMAP {
105 STATUS (*Function)();
109 ** Command history structure.
111 typedef struct _HISTORY {
114 CHAR *Lines[HIST_SIZE];
126 STATIC CHAR NIL[] = "";
127 STATIC CONST CHAR *Input = NIL;
129 STATIC CONST char *Prompt;
132 STATIC char NEWLINE[]= CRLF;
142 FORWARD KEYMAP Map[32];
143 FORWARD KEYMAP MetaMap[16];
144 STATIC SIZE_T Length;
145 STATIC SIZE_T ScreenCount;
146 STATIC SIZE_T ScreenSize;
147 STATIC char *backspace;
151 /* Display print 8-bit chars as `M-x' or as the actual 8-bit char? */
152 int rl_meta_chars = 0;
157 STATIC CHAR *editinput();
158 #if defined(USE_TERMCAP)
162 #endif /* defined(USE_TERMCAP) */
165 ** TTY input/output functions.
172 /* Dummy assignment avoids GCC warning on
173 * "attribute warn_unused_result" */
174 ssize_t dummy = write(1, Screen, ScreenCount);
184 Screen[ScreenCount] = c;
185 if (++ScreenCount >= ScreenSize - 1) {
186 ScreenSize += SCREEN_INC;
187 RENEW(Screen, char, ScreenSize);
211 else if (rl_meta_chars && ISMETA(c)) {
240 return read(0, &c, (SIZE_T)1) == 1 ? c : EOF;
243 #define TTYback() (backspace ? TTYputs((CHAR *)backspace) : TTYput('\b'))
257 #if defined(USE_TERMCAP)
261 #endif /* defined(USE_TERMCAP) */
262 #if defined(TIOCGWINSZ)
264 #endif /* defined(TIOCGWINSZ) */
267 #if defined(TIOCGWINSZ)
268 /* Perhaps we got resized. */
269 if (ioctl(0, TIOCGWINSZ, &W) >= 0
270 && W.ws_col > 0 && W.ws_row > 0) {
271 TTYwidth = (int)W.ws_col;
272 TTYrows = (int)W.ws_row;
274 #endif /* defined(TIOCGWINSZ) */
279 TTYwidth = TTYrows = 0;
280 #if defined(USE_TERMCAP)
282 if ((term = getenv("TERM")) == NULL)
284 if (tgetent(buff, term) < 0) {
285 TTYwidth = SCREEN_WIDTH;
286 TTYrows = SCREEN_ROWS;
289 p = tgetstr("le", &bp);
290 backspace = p ? strdup(p) : NULL;
291 TTYwidth = tgetnum("co");
292 TTYrows = tgetnum("li");
293 #endif /* defined(USE_TERMCAP) */
295 #if defined(TIOCGWINSZ)
296 if (ioctl(0, TIOCGWINSZ, &W) >= 0) {
297 TTYwidth = (int)W.ws_col;
298 TTYrows = (int)W.ws_row;
300 #endif /* defined(TIOCGWINSZ) */
302 if (TTYwidth <= 0 || TTYrows <= 0) {
303 TTYwidth = SCREEN_WIDTH;
304 TTYrows = SCREEN_ROWS;
316 TTYputs((CONST CHAR *)Prompt);
317 for (i = Point, p = Line; --i >= 0; p++)
327 if (ISCTL(Line[Point - 1]))
329 else if (rl_meta_chars && ISMETA(Line[Point - 1])) {
334 if (Change == CSmove)
342 TTYshow(Line[Point]);
343 if (Change == CSmove)
366 if ((Input = (CHAR *)getenv((char *)name)) == NULL) {
383 for ( ; Point < End && (*p == ' ' || !isalnum(*p)); Point++, p++)
387 for (; Point < End && isalnum(*p); Point++, p++)
393 } while (++i < Repeat);
407 (void)do_forward(CSstay);
408 if (OldPoint != Point) {
409 if ((count = Point - OldPoint) < 0)
412 if ((end = Point + count) > End)
414 for (i = Point, p = &Line[i]; i < end; i++, p++) {
415 if (type == TOupper) {
419 else if (isupper(*p))
430 return do_case(TOlower);
436 return do_case(TOupper);
446 for (extras = 0, i = Point, p = &Line[i]; i <= End; i++, p++) {
452 else if (rl_meta_chars && ISMETA(*p)) {
459 for (i += extras; i > Point; i--)
466 Point = -strlen(Prompt);
483 len = strlen((char *)p);
484 if (End + len >= Length) {
485 if ((new = NEW(CHAR, Length + len + MEM_INC)) == NULL)
488 COPYFROMTO(new, Line, Length);
492 Length += len + MEM_INC;
495 for (q = &Line[Point], i = End - Point; --i >= 0; )
497 COPYFROMTO(&Line[Point], p, len);
500 TTYstring(&Line[Point]);
503 return Point == End ? CSstay : CSmove;
509 TTYputs((CONST CHAR *)NEWLINE);
510 TTYputs((CONST CHAR *)Prompt);
518 rl_meta_chars = ! rl_meta_chars;
526 return H.Pos >= H.Size - 1 ? NULL : H.Lines[++H.Pos];
532 return H.Pos == 0 ? NULL : H.Lines[--H.Pos];
545 return insert_string(p);
557 if ((p = (*move)()) == NULL)
559 } while (++i < Repeat);
560 return do_insert_hist(p);
566 return do_hist(next_hist);
572 return do_hist(prev_hist);
578 return do_insert_hist(H.Lines[H.Pos = 0]);
584 return do_insert_hist(H.Lines[H.Pos = H.Size - 1]);
588 ** Return zero if pat appears as a substring in text.
591 substrcmp(text, pat, len)
598 if ((c = *pat) == '\0')
599 return *text == '\0';
600 for ( ; *text; text++)
601 if (*text == c && strncmp(text, pat, len) == 0)
607 search_hist(search, move)
611 static CHAR *old_search;
617 /* Save or get remembered search pattern. */
618 if (search && *search) {
621 old_search = (CHAR *)strdup((char *)search);
624 if (old_search == NULL || *old_search == '\0')
629 /* Set up pattern-finder. */
630 if (*search == '^') {
632 pat = (char *)(search + 1);
636 pat = (char *)search;
640 for (pos = H.Pos; (*move)() != NULL; )
641 if ((*match)((char *)H.Lines[H.Pos], pat, len) == 0)
642 return H.Lines[H.Pos];
650 static int Searching;
651 CONST char *old_prompt;
662 TTYputs((CONST CHAR *)Prompt);
663 move = Repeat == NO_ARG ? prev_hist : next_hist;
667 TTYputs((CONST CHAR *)Prompt);
668 if (p == NULL && Signal > 0) {
673 p = search_hist(p, move);
679 return do_insert_hist(p);
692 } while (++i < Repeat);
709 if ((Yanked = NEW(CHAR, (SIZE_T)i + 1)) != NULL) {
710 COPYFROMTO(Yanked, &Line[begin], i);
722 if (count <= 0 || End == Point)
725 if (count == 1 && Point == End - 1) {
726 /* Optimize common case of delete at end of line. */
735 else if (rl_meta_chars && ISMETA(*p)) {
744 if (Point + count > End && (count = End - Point) <= 0)
748 save_yank(Point, count);
750 for (p = &Line[Point], i = End - (Point + count) + 1; --i >= 0; p++)
754 TTYstring(&Line[Point]);
768 } while (++i < Repeat);
783 } while (++i < Repeat);
785 return delete_string(i);
793 if (Repeat != NO_ARG) {
794 if (Repeat < Point) {
798 (void)delete_string(i - Point);
800 else if (Repeat > Point) {
802 (void)delete_string(Repeat - Point - 1);
807 save_yank(Point, End - Point);
824 if (Repeat == NO_ARG || Repeat < 2) {
827 return insert_string(buff);
830 if ((p = NEW(CHAR, Repeat + 1)) == NULL)
832 for (i = Repeat, q = p; --i >= 0; )
836 s = insert_string(p);
847 if ((int)(c = TTYget()) == EOF)
849 #if defined(ANSI_ARROWS)
850 /* Also include VT-100 arrows. */
851 if (c == '[' || c == 'O')
852 switch (c = TTYget()) {
853 default: return ring_bell();
854 case EOF: return CSeof;
855 case 'A': return h_prev();
856 case 'B': return h_next();
857 case 'C': return fd_char();
858 case 'D': return bk_char();
860 #endif /* defined(ANSI_ARROWS) */
863 for (Repeat = c - '0'; (int)(c = TTYget()) != EOF && isdigit(c); )
864 Repeat = Repeat * 10 + c - '0';
872 for (OldPoint = Point, kp = MetaMap; kp->Function; kp++)
874 return (*kp->Function)();
886 if (rl_meta_chars && ISMETA(c)) {
888 PushBack = UNMETA(c);
891 for (kp = Map; kp->Function; kp++)
894 s = kp->Function ? (*kp->Function)() : insert_char((int)c);
896 /* No pushback means no repeat count; hacky, but true. */
908 if (c == rl_erase || (int)c == DEL)
909 return bk_del_char();
918 if (c == rl_eof && Point == 0 && End == 0)
938 OldPoint = Point = Mark = End = 0;
942 while ((int)(c = TTYget()) != EOF)
943 switch (TTYspecial(c)) {
972 if (strlen((char *)Line))
984 if ((p = (CHAR *)strdup((char *)p)) == NULL)
986 if (H.Size < HIST_SIZE)
987 H.Lines[H.Size++] = p;
990 for (i = 0; i < HIST_SIZE - 1; i++)
991 H.Lines[i] = H.Lines[i + 1];
998 ** For compatibility with FSF readline.
1002 rl_reset_terminal(p)
1021 if ((Line = NEW(CHAR, Length)) == NULL)
1028 ScreenSize = SCREEN_INC;
1029 Screen = NEW(char, ScreenSize);
1030 Prompt = prompt ? prompt : (char *)NIL;
1031 TTYputs((CONST CHAR *)Prompt);
1032 if ((line = editinput()) != NULL) {
1033 line = (CHAR *)strdup((char *)line);
1034 TTYputs((CHAR *)NEWLINE);
1039 DISPOSE(H.Lines[--H.Size]);
1043 (void)kill(getpid(), s);
1045 return (char *)line;
1052 if (p == NULL || *p == '\0')
1055 #if defined(UNIQUE_HISTORY)
1056 if (H.Size && strcmp(p, (char *)H.Lines[H.Size - 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 (int)(c = TTYget()) == EOF ? CSeof : insert_char((int)c);
1137 return delete_string(Mark - Point);
1152 if ((c = TTYget()) != CTL('X'))
1153 return (int)c == EOF ? CSeof : ring_bell();
1155 if ((int)(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 ((int)(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[32] = {
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('J'), accept_line },
1334 { CTL('K'), kill_line },
1335 { CTL('L'), redisplay },
1336 { CTL('M'), accept_line },
1337 { CTL('N'), h_next },
1338 { CTL('O'), ring_bell },
1339 { CTL('P'), h_prev },
1340 { CTL('Q'), ring_bell },
1341 { CTL('R'), h_search },
1342 { CTL('S'), ring_bell },
1343 { CTL('T'), transpose },
1344 { CTL('U'), ring_bell },
1345 { CTL('V'), quote },
1347 { CTL('X'), exchange },
1349 { CTL('Z'), ring_bell },
1351 { CTL(']'), move_to_char },
1352 { CTL('^'), ring_bell },
1353 { CTL('_'), ring_bell },
1357 STATIC KEYMAP MetaMap[16]= {
1358 { CTL('H'), bk_kill_word },
1359 { DEL, bk_kill_word },
1361 { '.', last_argument },
1365 { 'd', fd_kill_word },
1367 { 'l', case_down_word },
1368 { 'm', toggle_meta_mode },
1369 { 'u', case_up_word },
1371 { 'w', copy_region },