1 /* $Header: /p/tcsh/cvsroot/tcsh/ed.inputl.c,v 3.66 2006/11/29 22:32:24 christos Exp $ */
3 * ed.inputl.c: Input line handling.
6 * Copyright (c) 1980, 1991 The Regents of the University of California.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
35 RCSID("$tcsh: ed.inputl.c,v 3.66 2006/11/29 22:32:24 christos Exp $")
38 #include "ed.defns.h" /* for the function names */
39 #include "tw.h" /* for twenex stuff */
43 /* ed.inputl -- routines to get a single line from the input. */
45 extern int MapsAreInited
;
47 /* mismatched first character */
48 static Char mismatch
[] =
49 {'!', '^' , '\\', '-', '%', '\0', '"', '\'', '`', '\0' };
51 static int Repair (void);
52 static int GetNextCommand (KEYCMD
*, Char
*);
53 static int SpellLine (int);
54 static int CompleteLine (void);
55 static void RunCommand (Char
*);
56 static void doeval1 (Char
**);
58 static int rotate
= 0;
73 return (int) (LastChar
- InputBuf
);
82 unsigned char tch
; /* the place where read() goes */
84 int num
; /* how many chars we have read at NL */
86 struct varent
*crct
= inheredoc
? NULL
: adrof(STRcorrect
);
87 struct varent
*autol
= adrof(STRautolist
);
88 struct varent
*matchbeep
= adrof(STRmatchbeep
);
89 struct varent
*imode
= adrof(STRinputmode
);
90 Char
*SaveChar
, *CorrChar
;
91 int matchval
; /* from tenematch() */
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(CGETS(6, 2, "yes\n"));
266 Strcpy(InputBuf
, Origin
);
269 xprintf(CGETS(6, 3, "edit\n"));
272 printprompt(3, NULL
);
276 cleanup_until(Origin
);
279 else if (ch
== 'a') {
280 xprintf(CGETS(6, 4, "abort\n"));
281 LastChar
= InputBuf
; /* Null the current line */
283 printprompt(0, NULL
);
285 cleanup_until(Origin
);
288 xprintf(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(CGETS(6, 6, "No matching command\n"));
309 } else if (matchval
== 2) {
310 xprintf(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
);
397 if (adrof(STRautoexpand
))
398 (void) e_expand_history(0);
400 * Modified by Martin Boyer (gamin@ireq-robot.hydro.qc.ca):
401 * A separate variable now controls beeping after
402 * completion, independently of autolisting.
404 expnum
= (int) (Cursor
- InputBuf
);
405 switch (matchval
= tenematch(InputBuf
, Cursor
-InputBuf
, fn
)){
407 if (non_unique_match
&& matchbeep
&& matchbeep
->vec
!= NULL
&&
408 (Strcmp(*(matchbeep
->vec
), STRnotunique
) == 0))
412 if (matchbeep
&& matchbeep
->vec
!= NULL
) {
413 if (Strcmp(*(matchbeep
->vec
), STRnomatch
) == 0 ||
414 Strcmp(*(matchbeep
->vec
), STRambiguous
) == 0 ||
415 Strcmp(*(matchbeep
->vec
), STRnotunique
) == 0)
422 if (matchval
< 0) { /* Error from tenematch */
427 if (matchbeep
&& matchbeep
->vec
!= NULL
) {
428 if ((Strcmp(*(matchbeep
->vec
), STRambiguous
) == 0 ||
429 Strcmp(*(matchbeep
->vec
), STRnotunique
) == 0))
435 * Addition by David C Lawrence <tale@pawl.rpi.edu>: If an
436 * attempted completion is ambiguous, list the choices.
437 * (PWP: this is the best feature addition to tcsh I have
438 * seen in many months.)
440 if (autol
&& autol
->vec
!= NULL
&&
441 (Strcmp(*(autol
->vec
), STRambiguous
) != 0 ||
442 expnum
== Cursor
- InputBuf
)) {
443 if (adrof(STRhighlight
) && MarkIsSet
) {
444 /* clear highlighting before showing completions */
452 fn
= (retval
== CC_COMPLETE_ALL
) ? LIST_ALL
: LIST
;
453 (void) tenematch(InputBuf
, Cursor
-InputBuf
, fn
);
468 case CC_LIST_CHOICES
:
470 if (InputBuf
[curlen
] && rotate
) {
471 newlen
= (int) (LastChar
- InputBuf
);
472 for (idx
= (int) (Cursor
- InputBuf
);
473 idx
<= newlen
; idx
++)
474 InputBuf
[idx
- newlen
+ curlen
] =
476 LastChar
= InputBuf
+ curlen
;
477 Cursor
= Cursor
- newlen
+ curlen
;
479 curlen
= (int) (LastChar
- InputBuf
);
483 fn
= (retval
== CC_LIST_ALL
) ? LIST_ALL
: LIST
;
484 /* should catch ^C here... */
485 if (tenematch(InputBuf
, Cursor
- InputBuf
, fn
) < 0)
494 if (tenematch(InputBuf
, Cursor
- InputBuf
, GLOB
) < 0)
500 if (tenematch(InputBuf
, Cursor
- InputBuf
, GLOB_EXPAND
) <= 0)
501 SoundBeep(); /* Beep = No match */
505 case CC_NORMALIZE_PATH
:
506 if (tenematch(InputBuf
, Cursor
- InputBuf
, PATH_NORMALIZE
) <= 0)
507 SoundBeep(); /* Beep = No match */
512 if (tenematch(InputBuf
, Cursor
- InputBuf
, VARS_EXPAND
) <= 0)
513 SoundBeep(); /* Beep = No match */
517 case CC_NORMALIZE_COMMAND
:
518 if (tenematch(InputBuf
, Cursor
- InputBuf
, COMMAND_NORMALIZE
) <= 0)
519 SoundBeep(); /* Beep = No match */
525 /* should catch ^C here... */
526 (void) tenematch(InputBuf
, LastChar
- InputBuf
, PRINT_HELP
);
531 curlen
= (int) (LastChar
- InputBuf
);
534 case CC_FATAL
: /* fatal error, reset to known state */
536 xprintf(CGETS(7, 8, "*** editor fatal ERROR ***\r\n\n"));
537 #endif /* DEBUG_EDIT */
538 /* put (real) cursor in a known place */
539 ClearDisp(); /* reset the display stuff */
540 ResetInLine(1); /* reset the input pointers */
541 Refresh(); /* print the prompt again */
545 curlen
= (int) (LastChar
- InputBuf
);
549 default: /* functions we don't know about */
550 if (adrof(STRhighlight
)) {
560 curlen
= (int) (LastChar
- InputBuf
);
564 (void) Cookedmode(); /* make sure the tty is set up correctly */
566 flush(); /* flush any buffered output */
573 if (str
!= NULL
&& MacroLvl
+ 1 < MAXMACROLEVELS
) {
575 KeyMacro
[MacroLvl
] = str
;
585 Char
**evalvec
, *evalp
;
589 eval1_cleanup(void *xstate
)
591 struct eval1_state
*state
;
594 evalvec
= state
->evalvec
;
595 evalp
= state
->evalp
;
600 * Like eval, only using the current file descriptors
605 struct eval1_state state
;
611 gv
= v
= globall(v
, gflag
);
613 stderror(ERR_NOMATCH
);
622 cleanup_push(gv
, blk_cleanup
);
624 state
.evalvec
= evalvec
;
628 cleanup_push(&state
, eval1_cleanup
);
630 cleanup_until(&state
);
636 RunCommand(Char
*str
)
640 xputchar('\n'); /* Start on a clean line */
660 GetNextCommand(KEYCMD
*cmdnum
, Char
*ch
)
665 while (cmd
== 0 || cmd
== F_XKEY
) {
666 if ((num
= GetNextChar(ch
)) != 1) { /* if EOF or error */
676 !adrof(STRnokanji
) && (*ch
& META
)) {
687 /* XXX: This needs to be fixed so that we don't just truncate
688 * the character, we unquote it.
690 if (*ch
< NT_NUM_KEYS
)
691 cmd
= CurrentKeyMap
[*ch
];
694 cmd
= CurrentKeyMap
[(unsigned char) *ch
];
703 switch (GetXkey(&cstr
, &val
)) {
708 PushMacro(val
.str
.buf
);
711 RunCommand(val
.str
.buf
);
719 CurrentKeyMap
= CcKeyMap
;
725 static Char ungetchar
;
726 static int haveungetchar
;
729 UngetNextChar(Char cp
)
736 GetNextChar(Char
*cp
)
740 char cbuf
[MB_LEN_MAX
];
750 if (!Load_input_line())
753 if (*KeyMacro
[MacroLvl
] == 0) {
757 *cp
= *KeyMacro
[MacroLvl
]++ & CHAR
;
758 if (*KeyMacro
[MacroLvl
] == 0) { /* Needed for QuoteMode On */
764 if (Rawmode() < 0) /* make sure the tty is set up correctly */
765 return 0; /* oops: SHIN was closed */
769 #endif /* WINNT_NATIVE */
772 (void) check_window_size(0); /* for window systems */
773 #endif /* SIG_WINDOW */
776 while ((num_read
= xread(SHIN
, cbuf
+ cbp
, 1)) == -1) {
777 if (!tried
&& fixio(SHIN
, errno
) != -1)
781 /* need to print error message in case the file is migrated */
782 stderror(ERR_SYSTEM
, progname
, strerror(errno
));
786 # endif /* WINNT_NATIVE */
787 *cp
= '\0'; /* Loses possible partial character */
792 *cp
= (unsigned char)*cbuf
;
795 if (normal_mbtowc(cp
, cbuf
, cbp
) == -1) {
797 if (cbp
< MB_CUR_MAX
)
798 continue; /* Maybe a partial character */
799 /* And drop the following bytes, if any */
800 *cp
= (unsigned char)*cbuf
| INVALID_BYTE
;
806 /* This is the part that doesn't work with WIDE_STRINGS */
807 if (__nt_want_vcode
== 2)
810 #endif /* WINNT_NATIVE */
815 * SpellLine - do spelling correction on the entire command line
816 * (which may have trailing newline).
817 * If cmdonly is set, only check spelling of command words.
819 * -1: Something was incorrectible, and nothing was corrected
820 * 0: Everything was correct
821 * 1: Something was corrected
824 SpellLine(int cmdonly
)
826 int endflag
, matchval
;
827 Char
*argptr
, *OldCursor
, *OldLastChar
;
829 OldLastChar
= LastChar
;
835 while (ismetahash(*argptr
) || iscmdmeta(*argptr
))
837 for (Cursor
= argptr
;
838 *Cursor
!= '\0' && ((Cursor
!= argptr
&& Cursor
[-1] == '\\') ||
839 (!ismetahash(*Cursor
) && !iscmdmeta(*Cursor
)));
842 if (*Cursor
== '\0') {
844 if (LastChar
[-1] == '\n')
848 /* Obey current history character settings */
850 mismatch
[1] = HISTSUB
;
851 if (!Strchr(mismatch
, *argptr
) &&
852 (!cmdonly
|| starting_a_command(argptr
, InputBuf
))) {
855 * This hack avoids correcting drive letter changes
857 if((Cursor
- InputBuf
) != 2 || (char)InputBuf
[1] != ':')
858 #endif /* WINNT_NATIVE */
860 #ifdef HASH_SPELL_CHECK
862 size_t len
= Cursor
- InputBuf
;
864 save
= InputBuf
[len
];
865 InputBuf
[len
] = '\0';
866 if (find_cmd(InputBuf
, 0) != 0) {
867 InputBuf
[len
] = save
;
871 InputBuf
[len
] = save
;
872 #endif /* HASH_SPELL_CHECK */
873 switch (tenematch(InputBuf
, Cursor
- InputBuf
, SPELL
)) {
874 case 1: /* corrected */
877 case -1: /* couldn't be corrected */
881 default: /* was correct */
885 if (LastChar
!= OldLastChar
) {
886 if (argptr
< OldCursor
)
887 OldCursor
+= (LastChar
- OldLastChar
);
888 OldLastChar
= LastChar
;
898 * CompleteLine - do command completion on the entire command line
899 * (which may have trailing newline).
901 * 0: No command matched or failure
902 * 1: One command matched
903 * 2: Several commands matched
909 Char
*argptr
, *OldCursor
, *OldLastChar
;
911 OldLastChar
= LastChar
;
916 while (ismetahash(*argptr
) || iscmdmeta(*argptr
))
918 for (Cursor
= argptr
;
919 *Cursor
!= '\0' && ((Cursor
!= argptr
&& Cursor
[-1] == '\\') ||
920 (!ismetahash(*Cursor
) && !iscmdmeta(*Cursor
)));
923 if (*Cursor
== '\0') {
925 if (LastChar
[-1] == '\n')
929 if (!Strchr(mismatch
, *argptr
) && starting_a_command(argptr
, InputBuf
)) {
930 tmatch
= tenematch(InputBuf
, Cursor
- InputBuf
, RECOGNIZE
);
933 } else if (tmatch
> 1) {
936 if (LastChar
!= OldLastChar
) {
937 if (argptr
< OldCursor
)
938 OldCursor
+= (LastChar
- OldLastChar
);
939 OldLastChar
= LastChar
;