2 * ed.inputl.c: Input line handling.
5 * Copyright (c) 1980, 1991 The Regents of the University of California.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 #include "ed.defns.h" /* for the function names */
35 #include "tw.h" /* for twenex stuff */
39 /* ed.inputl -- routines to get a single line from the input. */
41 extern int MapsAreInited;
43 /* mismatched first character */
44 static Char mismatch[] = { '\\', '-', '%', '\0' };
45 /* don't Strchr() for '\0', obey current history character settings */
46 #define MISMATCH(c) ((c) == '\0' || (c) == HIST || (c) == HISTSUB || \
47 Strchr(mismatch, (c)))
49 static int Repair (void);
50 static int GetNextCommand (KEYCMD *, Char *);
51 static int SpellLine (int);
52 static int CompleteLine (void);
53 static void RunCommand (Char *);
54 static void doeval1 (Char **);
56 static int rotate = 0;
71 return (int) (LastChar - InputBuf);
80 unsigned char tch; /* the place where read() goes */
82 int num; /* how many chars we have read at NL */
84 struct varent *crct = inheredoc ? NULL : adrof(STRcorrect);
85 struct varent *autol = adrof(STRautolist);
86 struct varent *matchbeep = adrof(STRmatchbeep);
87 struct varent *imode = adrof(STRinputmode);
88 Char *SaveChar, *CorrChar;
89 int matchval; /* from tenematch() */
90 int nr_history_exp; /* number of (attempted) history expansions */
97 if (!MapsAreInited) /* double extra just in case */
100 ClearDisp(); /* reset the display stuff */
101 ResetInLine(0); /* reset the input pointers */
103 MacroLvl = -1; /* editor was interrupted during input */
105 if (imode && imode->vec != NULL) {
106 if (!Strcmp(*(imode->vec), STRinsert))
107 inputmode = MODE_INSERT;
108 else if (!Strcmp(*(imode->vec), STRoverwrite))
109 inputmode = MODE_REPLACE;
112 #if defined(FIONREAD) && !defined(OREO)
113 if (!Tty_raw_mode && MacroLvl < 0) {
118 * *Everyone* else has an int, but SunOS wants long!
119 * This breaks where int != long (alpha)
124 (void) ioctl(SHIN, FIONREAD, (ioctl_t) & chrs);
130 #endif /* FIONREAD && !OREO */
137 copyn(InputBuf, SavedBuf.s, INBUFSIZE);/*FIXBUF*/
138 LastChar = InputBuf + LastSaved;
139 Cursor = InputBuf + CursSaved;
140 Hist_num = HistSaved;
145 Hist_num = HistSaved;
153 Refresh(); /* print the prompt */
155 for (num = OKCMD; num == OKCMD;) { /* while still editing this line */
157 if (Cursor > LastChar)
158 xprintf("Cursor > LastChar\r\n");
159 if (Cursor < InputBuf)
160 xprintf("Cursor < InputBuf\r\n");
161 if (Cursor > InputLim)
162 xprintf("Cursor > InputLim\r\n");
163 if (LastChar > InputLim)
164 xprintf("LastChar > InputLim\r\n");
165 if (InputLim != &InputBuf[INBUFSIZE - 2])/*FIXBUF*/
166 xprintf("InputLim != &InputBuf[INBUFSIZE-2]\r\n");
167 if ((!DoingArg) && (Argument != 1))
168 xprintf("(!DoingArg) && (Argument != 1)\r\n");
169 if (CcKeyMap[0] == 0)
170 xprintf("CcKeyMap[0] == 0 (maybe not inited)\r\n");
173 /* if EOF or error */
174 if ((num = GetNextCommand(&cmdnum, &ch)) != OKCMD) {
178 if (cmdnum >= NumFuns) {/* BUG CHECK command */
180 xprintf(CGETS(6, 1, "ERROR: illegal command from key 0%o\r\n"), ch);
182 continue; /* try again */
185 /* now do the real command */
186 retval = (*CcFuncTbl[cmdnum]) (ch);
188 /* save the last command here */
191 /* make sure fn is initialized */
192 fn = (retval == CC_COMPLETE_ALL) ? LIST_ALL : LIST;
194 /* use any return value */
200 case CC_NORM: /* normal char */
204 case CC_ARGHACK: /* Suggested by Rich Salz */
205 /* <rsalz@pineapple.bbn.com> */
207 curlen = (int) (LastChar - InputBuf);
208 break; /* keep going... */
210 case CC_EOF: /* end of file typed */
212 curlen = (int) (LastChar - InputBuf);
216 case CC_WHICH: /* tell what this command does */
218 *LastChar++ = '\n'; /* for the benifit of CSH */
219 num = (int) (LastChar - InputBuf); /* number characters read */
222 case CC_NEWLINE: /* normal end of line */
226 if (crct && crct->vec != NULL && (!Strcmp(*(crct->vec), STRcmd) ||
227 !Strcmp(*(crct->vec), STRall))) {
231 Origin = Strsave(InputBuf);
232 cleanup_push(Origin, xfree);
234 if (SpellLine(!Strcmp(*(crct->vec), STRcmd)) == 1) {
238 Change = Strsave(InputBuf);
239 cleanup_push(Change, xfree);
240 *Strchr(Change, '\n') = '\0';
241 CorrChar = LastChar; /* Save the corrected end */
242 LastChar = InputBuf; /* Null the current line */
244 printprompt(2, short2str(Change));
245 cleanup_until(Change);
247 if (xread(SHIN, &tch, 1) < 0) {
250 * need to print error message in case file
254 stderror(ERR_SYSTEM, progname, strerror(errno));
256 cleanup_until(Origin);
261 if (ch == 'y' || ch == ' ') {
262 LastChar = CorrChar; /* Restore the corrected end */
263 xprintf("%s", CGETS(6, 2, "yes\n"));
266 Strcpy(InputBuf, Origin);
269 xprintf("%s", CGETS(6, 3, "edit\n"));
272 printprompt(3, NULL);
276 cleanup_until(Origin);
279 else if (ch == 'a') {
280 xprintf("%s", CGETS(6, 4, "abort\n"));
281 LastChar = InputBuf; /* Null the current line */
283 printprompt(0, NULL);
285 cleanup_until(Origin);
288 xprintf("%s", CGETS(6, 5, "no\n"));
292 cleanup_until(Origin);
293 } else if (crct && crct->vec != NULL &&
294 !Strcmp(*(crct->vec), STRcomplete)) {
295 if (LastChar > InputBuf && LastChar[-1] == '\n') {
300 match_unique_match = 1; /* match unique matches */
301 matchval = CompleteLine();
302 match_unique_match = 0;
303 curlen = (int) (LastChar - InputBuf);
308 xprintf("%s", CGETS(6, 6, "No matching command\n"));
309 } else if (matchval == 2) {
310 xprintf("%s", CGETS(6, 7, "Ambiguous command\n"));
325 curlen = (int) (LastChar - InputBuf);
331 tellwhat = 0; /* just in case */
332 Hist_num = 0; /* for the history commands */
333 /* return the number of chars read */
334 num = (int) (LastChar - InputBuf);
336 * For continuation lines, we set the prompt to prompt 2
338 printprompt(1, NULL);
343 if (tenematch(InputBuf, Cursor - InputBuf, SPELL) < 0)
344 SoundBeep(); /* Beep = No match/ambiguous */
349 if (SpellLine(FALSE) < 0)
350 SoundBeep(); /* Beep = No match/ambiguous */
356 case CC_COMPLETE_ALL:
357 case CC_COMPLETE_FWD:
358 case CC_COMPLETE_BACK:
362 curlen = (int) (LastChar - InputBuf);
366 case CC_COMPLETE_ALL:
368 curlen = (int) (LastChar - InputBuf);
372 case CC_COMPLETE_FWD:
373 fn = RECOGNIZE_SCROLL;
377 case CC_COMPLETE_BACK:
378 fn = RECOGNIZE_SCROLL;
385 if (InputBuf[curlen] && rotate) {
386 newlen = (int) (LastChar - InputBuf);
387 for (idx = (int) (Cursor - InputBuf);
388 idx <= newlen; idx++)
389 InputBuf[idx - newlen + curlen] =
391 LastChar = InputBuf + curlen;
392 Cursor = Cursor - newlen + curlen;
394 curlen = (int) (LastChar - InputBuf);
398 autoexpand = varval(STRautoexpand);
399 if (autoexpand != STRNULL)
400 nr_history_exp += ExpandHistory();
402 /* try normal expansion only if no history references were found */
403 if (nr_history_exp == 0 ||
404 Strcmp(autoexpand, STRonlyhistory) != 0) {
406 * Modified by Martin Boyer (gamin@ireq-robot.hydro.qc.ca):
407 * A separate variable now controls beeping after
408 * completion, independently of autolisting.
410 expnum = (int) (Cursor - InputBuf);
411 switch (matchval = tenematch(InputBuf, Cursor-InputBuf, fn)){
413 if (non_unique_match && matchbeep &&
414 matchbeep->vec != NULL &&
415 (Strcmp(*(matchbeep->vec), STRnotunique) == 0))
419 if (matchbeep && matchbeep->vec != NULL) {
420 if (Strcmp(*(matchbeep->vec), STRnomatch) == 0 ||
421 Strcmp(*(matchbeep->vec), STRambiguous) == 0 ||
422 Strcmp(*(matchbeep->vec), STRnotunique) == 0)
429 if (matchval < 0) { /* Error from tenematch */
434 if (matchbeep && matchbeep->vec != NULL) {
435 if ((Strcmp(*(matchbeep->vec), STRambiguous) == 0 ||
436 Strcmp(*(matchbeep->vec), STRnotunique) == 0))
442 * Addition by David C Lawrence <tale@pawl.rpi.edu>: If an
443 * attempted completion is ambiguous, list the choices.
444 * (PWP: this is the best feature addition to tcsh I have
445 * seen in many months.)
447 if (autol && autol->vec != NULL &&
448 (Strcmp(*(autol->vec), STRambiguous) != 0 ||
449 expnum == Cursor - InputBuf)) {
450 if (adrof(STRhighlight) && MarkIsSet) {
451 /* clear highlighting before showing completions */
459 fn = (retval == CC_COMPLETE_ALL) ? LIST_ALL : LIST;
460 (void) tenematch(InputBuf, Cursor-InputBuf, fn);
476 case CC_LIST_CHOICES:
478 if (InputBuf[curlen] && rotate) {
479 newlen = (int) (LastChar - InputBuf);
480 for (idx = (int) (Cursor - InputBuf);
481 idx <= newlen; idx++)
482 InputBuf[idx - newlen + curlen] =
484 LastChar = InputBuf + curlen;
485 Cursor = Cursor - newlen + curlen;
487 curlen = (int) (LastChar - InputBuf);
491 fn = (retval == CC_LIST_ALL) ? LIST_ALL : LIST;
492 /* should catch ^C here... */
493 if (tenematch(InputBuf, Cursor - InputBuf, fn) < 0)
502 if (tenematch(InputBuf, Cursor - InputBuf, GLOB) < 0)
508 if (tenematch(InputBuf, Cursor - InputBuf, GLOB_EXPAND) <= 0)
509 SoundBeep(); /* Beep = No match */
513 case CC_NORMALIZE_PATH:
514 if (tenematch(InputBuf, Cursor - InputBuf, PATH_NORMALIZE) <= 0)
515 SoundBeep(); /* Beep = No match */
520 if (tenematch(InputBuf, Cursor - InputBuf, VARS_EXPAND) <= 0)
521 SoundBeep(); /* Beep = No match */
525 case CC_NORMALIZE_COMMAND:
526 if (tenematch(InputBuf, Cursor - InputBuf, COMMAND_NORMALIZE) <= 0)
527 SoundBeep(); /* Beep = No match */
533 /* should catch ^C here... */
534 (void) tenematch(InputBuf, LastChar - InputBuf, PRINT_HELP);
539 curlen = (int) (LastChar - InputBuf);
542 case CC_FATAL: /* fatal error, reset to known state */
544 xprintf(CGETS(7, 8, "*** editor fatal ERROR ***\r\n\n"));
545 #endif /* DEBUG_EDIT */
546 /* put (real) cursor in a known place */
547 ClearDisp(); /* reset the display stuff */
548 ResetInLine(1); /* reset the input pointers */
549 Refresh(); /* print the prompt again */
553 curlen = (int) (LastChar - InputBuf);
557 default: /* functions we don't know about */
558 if (adrof(STRhighlight)) {
568 curlen = (int) (LastChar - InputBuf);
572 (void) Cookedmode(); /* make sure the tty is set up correctly */
574 flush(); /* flush any buffered output */
581 if (str != NULL && MacroLvl + 1 < MAXMACROLEVELS) {
583 KeyMacro[MacroLvl] = str;
593 Char **evalvec, *evalp;
597 eval1_cleanup(void *xstate)
599 struct eval1_state *state;
602 evalvec = state->evalvec;
603 evalp = state->evalp;
608 * Like eval, only using the current file descriptors
613 struct eval1_state state;
619 gv = v = globall(v, gflag);
621 stderror(ERR_NOMATCH);
630 cleanup_push(gv, blk_cleanup);
632 state.evalvec = evalvec;
636 cleanup_push(&state, eval1_cleanup);
638 cleanup_until(&state);
644 RunCommand(Char *str)
648 xputchar('\n'); /* Start on a clean line */
670 #ifndef WINNT_NATIVE // We use more than 256 for various extended keys
671 wint_t c = ch & CHAR;
675 return c < NT_NUM_KEYS ? CurrentKeyMap[c] : F_INSERT;
679 GetNextCommand(KEYCMD *cmdnum, Char *ch)
684 while (cmd == 0 || cmd == F_XKEY) {
685 if ((num = GetNextChar(ch)) != 1) { /* if EOF or error */
695 !adrof(STRnokanji) && (*ch & META)) {
707 cmd = GetCmdChar(*ch);
713 switch (GetXkey(&cstr, &val)) {
718 PushMacro(val.str.buf);
721 RunCommand(val.str.buf);
729 CurrentKeyMap = CcKeyMap;
735 static Char ungetchar;
736 static int haveungetchar;
739 UngetNextChar(Char cp)
746 GetNextChar(Char *cp)
750 char cbuf[MB_LEN_MAX];
760 if (!Load_input_line())
763 if (*KeyMacro[MacroLvl] == 0) {
767 *cp = *KeyMacro[MacroLvl]++ & CHAR;
768 if (*KeyMacro[MacroLvl] == 0) { /* Needed for QuoteMode On */
774 if (Rawmode() < 0) /* make sure the tty is set up correctly */
775 return 0; /* oops: SHIN was closed */
779 #endif /* WINNT_NATIVE */
782 (void) check_window_size(0); /* for window systems */
783 #endif /* SIG_WINDOW */
786 while ((num_read = xread(SHIN, cbuf + cbp, 1)) == -1) {
787 if (!tried && fixio(SHIN, errno) != -1)
791 /* need to print error message in case the file is migrated */
792 stderror(ERR_SYSTEM, progname, strerror(errno));
796 # endif /* WINNT_NATIVE */
797 *cp = '\0'; /* Loses possible partial character */
801 if (cbp == 0 /* && *cbuf < NT_NUM_KEYS */
802 && CurrentKeyMap[(unsigned char)*cbuf] == F_XKEY) {
803 *cp = (unsigned char)*cbuf;
806 if (normal_mbtowc(cp, cbuf, cbp) == -1) {
808 if (cbp < MB_CUR_MAX)
809 continue; /* Maybe a partial character */
810 /* And drop the following bytes, if any */
811 *cp = (unsigned char)*cbuf | INVALID_BYTE;
817 /* This is the part that doesn't work with WIDE_STRINGS */
818 if (__nt_want_vcode == 2)
821 #endif /* WINNT_NATIVE */
826 * SpellLine - do spelling correction on the entire command line
827 * (which may have trailing newline).
828 * If cmdonly is set, only check spelling of command words.
830 * -1: Something was incorrectible, and nothing was corrected
831 * 0: Everything was correct
832 * 1: Something was corrected
835 SpellLine(int cmdonly)
837 int endflag, matchval;
838 Char *argptr, *OldCursor, *OldLastChar;
840 OldLastChar = LastChar;
846 while (ismetahash(*argptr) || iscmdmeta(*argptr))
848 for (Cursor = argptr;
849 *Cursor != '\0' && ((Cursor != argptr && Cursor[-1] == '\\') ||
850 (!ismetahash(*Cursor) && !iscmdmeta(*Cursor)));
853 if (*Cursor == '\0') {
855 if (LastChar[-1] == '\n')
859 if (!MISMATCH(*argptr) &&
860 (!cmdonly || starting_a_command(argptr, InputBuf))) {
863 * This hack avoids correcting drive letter changes
865 if((Cursor - InputBuf) != 2 || (char)InputBuf[1] != ':')
866 #endif /* WINNT_NATIVE */
868 #ifdef HASH_SPELL_CHECK
870 size_t len = Cursor - InputBuf;
872 save = InputBuf[len];
873 InputBuf[len] = '\0';
874 if (find_cmd(InputBuf, 0) != 0) {
875 InputBuf[len] = save;
879 InputBuf[len] = save;
880 #endif /* HASH_SPELL_CHECK */
881 switch (tenematch(InputBuf, Cursor - InputBuf, SPELL)) {
882 case 1: /* corrected */
885 case -1: /* couldn't be corrected */
889 default: /* was correct */
893 if (LastChar != OldLastChar) {
894 if (argptr < OldCursor)
895 OldCursor += (LastChar - OldLastChar);
896 OldLastChar = LastChar;
906 * CompleteLine - do command completion on the entire command line
907 * (which may have trailing newline).
909 * 0: No command matched or failure
910 * 1: One command matched
911 * 2: Several commands matched
917 Char *argptr, *OldCursor, *OldLastChar;
919 OldLastChar = LastChar;
924 while (ismetahash(*argptr) || iscmdmeta(*argptr))
926 for (Cursor = argptr;
927 *Cursor != '\0' && ((Cursor != argptr && Cursor[-1] == '\\') ||
928 (!ismetahash(*Cursor) && !iscmdmeta(*Cursor)));
931 if (*Cursor == '\0') {
933 if (LastChar[-1] == '\n')
937 if (!MISMATCH(*argptr) && starting_a_command(argptr, InputBuf)) {
938 tmatch = tenematch(InputBuf, Cursor - InputBuf, RECOGNIZE);
941 } else if (tmatch > 1) {
944 if (LastChar != OldLastChar) {
945 if (argptr < OldCursor)
946 OldCursor += (LastChar - OldLastChar);
947 OldLastChar = LastChar;