xref: /freebsd/contrib/nvi/cl/README.signal (revision 02e9120893770924227138ba49df1edb3896112a)
1There are six (normally) asynchronous actions about which vi cares:
2SIGHUP, SIGINT, SIGQUIT, SIGTERM, SIGTSTP and SIGWINCH.
3
4The assumptions:
5	1: The DB routines are not reentrant.
6	2: The curses routines may not be reentrant.
7	3: Neither DB nor curses will restart system calls.
8
9XXX
10Note, most C library functions don't restart system calls.  So, we should
11*probably* start blocking around any imported function that we don't know
12doesn't make a system call.  This is going to be a genuine annoyance...
13
14SIGHUP, SIGTERM
15	Used for file recovery.  The DB routines can't be reentered, nor
16	can they handle interrupted system calls, so the vi routines that
17	call DB block signals.  This means that DB routines could be
18	called at interrupt time, if necessary.
19
20SIGQUIT
21	Disabled by the signal initialization routines.  Historically, ^\
22	switched vi into ex mode, and we continue that practice.
23
24SIGWINCH:
25	The interrupt routine sets a global bit which is checked by the
26 	key-read routine, so there are no reentrancy issues.  This means
27	that the screen will not resize until vi runs out of keys, but
28	that doesn't seem like a problem.
29
30SIGINT and SIGTSTP are a much more difficult issue to resolve.  Vi has
31to permit the user to interrupt long-running operations.  Generally, a
32search, substitution or read/write is done on a large file, or, the user
33creates a key mapping with an infinite loop.  This problem will become
34worse as more complex semantics are added to vi, especially things like
35making it a pure text widget.  There are four major solutions on the table,
36each of which have minor permutations.
37
381:	Run in raw mode.
39
40	The up side is that there's no asynchronous behavior to worry about,
41	and obviously no reentrancy problems.  The down side is that it's easy
42	to misinterpret characters (e.g. :w big_file^Mi^V^C is going to look
43	like an interrupt) and it's easy to get into places where we won't see
44	interrupt characters (e.g. ":map a ixx^[hxxaXXX" infinitely loops in
45	historic implementations of vi).  Periodically reading the terminal
46	input buffer might solve the latter problem, but it's not going to be
47	pretty.
48
49	Also, we're going to be checking for ^C's and ^Z's both, all over
50	the place -- I hate to litter the source code with that.  For example,
51	the historic version of vi didn't permit you to suspend the screen if
52	you were on the colon command line.  This isn't right.  ^Z isn't a vi
53	command, it's a terminal event.  (Dammit.)
54
552:	Run in cbreak mode.  There are two problems in this area.  First, the
56	current curses implementations (both System V and Berkeley) don't give
57	you clean cbreak modes. For example, the IEXTEN bit is left on, turning
58	on DISCARD and LNEXT.  To clarify, what vi WANTS is 8-bit clean, with
59	the exception that flow control and signals are turned on, and curses
60	cbreak mode doesn't give you this.
61
62	We can either set raw mode and twiddle the tty, or cbreak mode and
63	twiddle the tty.  I chose to use raw mode, on the grounds that raw
64	mode is better defined and I'm less likely to be surprised by a curses
65	implementation down the road.  The twiddling consists of setting ISIG,
66	IXON/IXOFF, and disabling some of the interrupt characters (see the
67	comments in cl_init.c).  This is all found in historic System V (SVID
68	3) and POSIX 1003.1-1992, so it should be fairly portable.
69
70	The second problem is that vi permits you to enter literal signal
71	characters, e.g. ^V^C.  There are two possible solutions.  First, you
72	can turn off signals when you get a ^V, but that means that a network
73	packet containing ^V and ^C will lose, since the ^C may take effect
74	before vi reads the ^V.  (This is particularly problematic if you're
75	talking over a protocol that recognizes signals locally and sends OOB
76	packets when it sees them.)  Second, you can turn the ^C into a literal
77	character in vi, but that means that there's a race between entering
78	^V<character>^C, i.e. the sequence may end up being ^V^C<character>.
79	Also, the second solution doesn't work for flow control characters, as
80	they aren't delivered to the program as signals.
81
82	Generally, this is what historic vi did.  (It didn't have the curses
83	problems because it didn't use curses.)  It entered signals following
84	^V characters into the input stream, (which is why there's no way to
85	enter a literal flow control character).
86
873:	Run in mostly raw mode; turn signals on when doing an operation the
88	user might want to interrupt, but leave them off most of the time.
89
90	This works well for things like file reads and writes.  This doesn't
91	work well for trying to detect infinite maps.  The problem is that
92	you can write the code so that you don't have to turn on interrupts
93	per keystroke, but the code isn't pretty and it's hard to make sure
94	that an optimization doesn't cover up an infinite loop.  This also
95	requires interaction or state between the vi parser and the key
96	reading routines, as an infinite loop may still be returning keys
97	to the parser.
98
99	Also, if the user inserts an interrupt into the tty queue while the
100	interrupts are turned off, the key won't be treated as an interrupt,
101	and requiring the user to pound the keyboard to catch an interrupt
102	window is nasty.
103
1044:	Run in mostly raw mode, leaving signals on all of the time.  Done
105	by setting raw mode, and twiddling the tty's termios ISIG bit.
106
107	This works well for the interrupt cases, because the code only has
108	to check to see if the interrupt flag has been set, and can otherwise
109	ignore signals.  It's also less likely that we'll miss a case, and we
110	don't have to worry about synchronizing between the vi parser and the
111	key read routines.
112
113	The down side is that we have to turn signals off if the user wants
114	to enter a literal character (e.g. ^V^C).  If the user enters the
115	combination fast enough, or as part of a single network packet,
116	the text input routines will treat it as a signal instead of as a
117	literal character.  To some extent, we have this problem already,
118	since we turn off flow control so that the user can enter literal
119	XON/XOFF characters.
120
121	This is probably the easiest to code, and provides the smoothest
122	programming interface.
123
124There are a couple of other problems to consider.
125
126First, System V's curses doesn't handle SIGTSTP correctly.  If you use the
127newterm() interface, the TSTP signal will leave you in raw mode, and the
128final endwin() will leave you in the correct shell mode.  If you use the
129initscr() interface, the TSTP signal will return you to the correct shell
130mode, but the final endwin() will leave you in raw mode.  There you have
131it: proof that drug testing is not making any significant headway in the
132computer industry.  The 4BSD curses is deficient in that it does not have
133an interface to the terminal keypad.  So, regardless, we have to do our
134own SIGTSTP handling.
135
136The problem with this is that if we do our own SIGTSTP handling, in either
137models #3 or #4, we're going to have to call curses routines at interrupt
138time, which means that we might be reentering curses, which is something we
139don't want to do.
140
141Second, SIGTSTP has its own little problems.  It's broadcast to the entire
142process group, not sent to a single process.  The scenario goes something
143like this: the shell execs the mail program, which execs vi.  The user hits
144^Z, and all three programs get the signal, in some random order.  The mail
145program goes to sleep immediately (since it probably didn't have a SIGTSTP
146handler in place).  The shell gets a SIGCHLD, does a wait, and finds out
147that the only child in its foreground process group (of which it's aware)
148is asleep.  It then optionally resets the terminal (because the modes aren't
149how it left them), and starts prompting the user for input.  The problem is
150that somewhere in the middle of all of this, vi is resetting the terminal,
151and getting ready to send a SIGTSTP to the process group in order to put
152itself to sleep.  There's a solution to all of this: when vi starts, it puts
153itself into its own process group, and then only it (and possible child
154processes) receive the SIGTSTP.  This permits it to clean up the terminal
155and switch back to the original process group, where it sends that process
156group a SIGTSTP, putting everyone to sleep and waking the shell.
157
158Third, handing SIGTSTP asynchronously is further complicated by the child
159processes vi may fork off.  If vi calls ex, ex resets the terminal and
160starts running some filter, and SIGTSTP stops them both, vi has to know
161when it restarts that it can't repaint the screen until ex's child has
162finished running.  This is solveable, but it's annoying.
163
164Well, somebody had to make a decision, and this is the way it's going to be
165(unless I get talked out of it).  SIGINT is handled asynchronously, so
166that we can pretty much guarantee that the user can interrupt any operation
167at any time.  SIGTSTP is handled synchronously, so that we don't have to
168reenter curses and so that we don't have to play the process group games.
169^Z is recognized in the standard text input and command modes.  (^Z should
170also be recognized during operations that may potentially take a long time.
171The simplest solution is probably to twiddle the tty, install a handler for
172SIGTSTP, and then restore normal tty modes when the operation is complete.)
173