xref: /onnv-gate/usr/src/cmd/mailx/tty.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
23*0Sstevel@tonic-gate /*	  All Rights Reserved  	*/
24*0Sstevel@tonic-gate 
25*0Sstevel@tonic-gate 
26*0Sstevel@tonic-gate /*
27*0Sstevel@tonic-gate  * University Copyright- Copyright (c) 1982, 1986, 1988
28*0Sstevel@tonic-gate  * The Regents of the University of California
29*0Sstevel@tonic-gate  * All Rights Reserved
30*0Sstevel@tonic-gate  *
31*0Sstevel@tonic-gate  * University Acknowledgment- Portions of this document are derived from
32*0Sstevel@tonic-gate  * software developed by the University of California, Berkeley, and its
33*0Sstevel@tonic-gate  * contributors.
34*0Sstevel@tonic-gate  */
35*0Sstevel@tonic-gate 
36*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
37*0Sstevel@tonic-gate 
38*0Sstevel@tonic-gate /*
39*0Sstevel@tonic-gate  * mailx -- a modified version of a University of California at Berkeley
40*0Sstevel@tonic-gate  *	mail program
41*0Sstevel@tonic-gate  *
42*0Sstevel@tonic-gate  * Generally useful tty stuff.
43*0Sstevel@tonic-gate  */
44*0Sstevel@tonic-gate 
45*0Sstevel@tonic-gate #include "rcv.h"
46*0Sstevel@tonic-gate #include <locale.h>
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #ifdef	USG_TTY
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate static char	*readtty(char pr[], char src[]);
51*0Sstevel@tonic-gate static int	savetty(void);
52*0Sstevel@tonic-gate static void	ttycont(int);
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate static	int	c_erase;		/* Current erase char */
55*0Sstevel@tonic-gate static	int	c_kill;			/* Current kill char */
56*0Sstevel@tonic-gate static	int	c_intr;			/* interrupt char */
57*0Sstevel@tonic-gate static	int	c_quit;			/* quit character */
58*0Sstevel@tonic-gate static	struct termio savtty;
59*0Sstevel@tonic-gate static	char canonb[LINESIZE];		/* canonical buffer for input */
60*0Sstevel@tonic-gate 					/* processing */
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #ifndef TIOCSTI
63*0Sstevel@tonic-gate static void	Echo(int cc);
64*0Sstevel@tonic-gate static int	countcol(void);
65*0Sstevel@tonic-gate static void	outstr(register char *s);
66*0Sstevel@tonic-gate static void	resetty(void);
67*0Sstevel@tonic-gate static void	rubout(register char *cp);
68*0Sstevel@tonic-gate static int	setty(void);
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate static	int	c_word;			/* Current word erase char */
71*0Sstevel@tonic-gate static	int	Col;			/* current output column */
72*0Sstevel@tonic-gate static	int	Pcol;			/* end column of prompt string */
73*0Sstevel@tonic-gate static	int	Out;			/* file descriptor of stdout */
74*0Sstevel@tonic-gate static	int	erasing;		/* we are erasing characters */
75*0Sstevel@tonic-gate static	struct termio ttybuf;
76*0Sstevel@tonic-gate #else
77*0Sstevel@tonic-gate static	jmp_buf	rewrite;		/* Place to go when continued */
78*0Sstevel@tonic-gate #endif
79*0Sstevel@tonic-gate 
80*0Sstevel@tonic-gate #ifdef SIGCONT
81*0Sstevel@tonic-gate # ifdef preSVr4
82*0Sstevel@tonic-gate typedef int	sig_atomic_t;
83*0Sstevel@tonic-gate # endif
84*0Sstevel@tonic-gate static	sig_atomic_t	hadcont;		/* Saw continue signal */
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*ARGSUSED*/
87*0Sstevel@tonic-gate static void
88*0Sstevel@tonic-gate #ifdef	__cplusplus
ttycont(int)89*0Sstevel@tonic-gate ttycont(int)
90*0Sstevel@tonic-gate #else
91*0Sstevel@tonic-gate /* ARGSUSED */
92*0Sstevel@tonic-gate ttycont(int s)
93*0Sstevel@tonic-gate #endif
94*0Sstevel@tonic-gate {
95*0Sstevel@tonic-gate 	hadcont++;
96*0Sstevel@tonic-gate 	longjmp(rewrite, 1);
97*0Sstevel@tonic-gate }
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate #ifndef TIOCSTI
100*0Sstevel@tonic-gate /*ARGSUSED*/
101*0Sstevel@tonic-gate static void
ttystop(int s)102*0Sstevel@tonic-gate ttystop(int s)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate 	resetty();
105*0Sstevel@tonic-gate 	kill(mypid, SIGSTOP);
106*0Sstevel@tonic-gate }
107*0Sstevel@tonic-gate #endif
108*0Sstevel@tonic-gate #endif
109*0Sstevel@tonic-gate 
110*0Sstevel@tonic-gate /*
111*0Sstevel@tonic-gate  * Read all relevant header fields.
112*0Sstevel@tonic-gate  */
113*0Sstevel@tonic-gate 
114*0Sstevel@tonic-gate int
grabh(register struct header * hp,int gflags,int subjtop)115*0Sstevel@tonic-gate grabh(register struct header *hp, int gflags, int subjtop)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate #ifdef SIGCONT
118*0Sstevel@tonic-gate 	void (*savecont)(int);
119*0Sstevel@tonic-gate #ifndef TIOCSTI
120*0Sstevel@tonic-gate 	void (*savestop)(int);
121*0Sstevel@tonic-gate #endif
122*0Sstevel@tonic-gate #endif
123*0Sstevel@tonic-gate 	if (savetty())
124*0Sstevel@tonic-gate 		return -1;
125*0Sstevel@tonic-gate #ifdef SIGCONT
126*0Sstevel@tonic-gate 	savecont = sigset(SIGCONT, ttycont);
127*0Sstevel@tonic-gate #ifndef TIOCSTI
128*0Sstevel@tonic-gate 	savestop = sigset(SIGTSTP, ttystop);
129*0Sstevel@tonic-gate #endif
130*0Sstevel@tonic-gate #endif
131*0Sstevel@tonic-gate 	if (gflags & GTO) {
132*0Sstevel@tonic-gate 		hp->h_to = addto(NOSTR, readtty("To: ", hp->h_to));
133*0Sstevel@tonic-gate 		if (hp->h_to != NOSTR)
134*0Sstevel@tonic-gate 			hp->h_seq++;
135*0Sstevel@tonic-gate 	}
136*0Sstevel@tonic-gate 	if (gflags & GSUBJECT && subjtop) {
137*0Sstevel@tonic-gate 		hp->h_subject = readtty("Subject: ", hp->h_subject);
138*0Sstevel@tonic-gate 		if (hp->h_subject != NOSTR)
139*0Sstevel@tonic-gate 			hp->h_seq++;
140*0Sstevel@tonic-gate 	}
141*0Sstevel@tonic-gate 	if (gflags & GCC) {
142*0Sstevel@tonic-gate 		hp->h_cc = addto(NOSTR, readtty("Cc: ", hp->h_cc));
143*0Sstevel@tonic-gate 		if (hp->h_cc != NOSTR)
144*0Sstevel@tonic-gate 			hp->h_seq++;
145*0Sstevel@tonic-gate 	}
146*0Sstevel@tonic-gate 	if (gflags & GBCC) {
147*0Sstevel@tonic-gate 		hp->h_bcc = addto(NOSTR, readtty("Bcc: ", hp->h_bcc));
148*0Sstevel@tonic-gate 		if (hp->h_bcc != NOSTR)
149*0Sstevel@tonic-gate 			hp->h_seq++;
150*0Sstevel@tonic-gate 	}
151*0Sstevel@tonic-gate 	if (gflags & GSUBJECT && !subjtop) {
152*0Sstevel@tonic-gate 		hp->h_subject = readtty("Subject: ", hp->h_subject);
153*0Sstevel@tonic-gate 		if (hp->h_subject != NOSTR)
154*0Sstevel@tonic-gate 			hp->h_seq++;
155*0Sstevel@tonic-gate 	}
156*0Sstevel@tonic-gate #ifdef SIGCONT
157*0Sstevel@tonic-gate 	(void) sigset(SIGCONT, savecont);
158*0Sstevel@tonic-gate #ifndef TIOCSTI
159*0Sstevel@tonic-gate 	(void) sigset(SIGTSTP, savestop);
160*0Sstevel@tonic-gate #endif
161*0Sstevel@tonic-gate #endif
162*0Sstevel@tonic-gate 	return(0);
163*0Sstevel@tonic-gate }
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate /*
166*0Sstevel@tonic-gate  * Read up a header from standard input.
167*0Sstevel@tonic-gate  * The source string has the preliminary contents to
168*0Sstevel@tonic-gate  * be read.
169*0Sstevel@tonic-gate  *
170*0Sstevel@tonic-gate  */
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate static char *
readtty(char pr[],char src[])173*0Sstevel@tonic-gate readtty(char pr[], char src[])
174*0Sstevel@tonic-gate {
175*0Sstevel@tonic-gate 	int c;
176*0Sstevel@tonic-gate 	register char *cp;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate #ifndef TIOCSTI
179*0Sstevel@tonic-gate 	register char *cp2;
180*0Sstevel@tonic-gate 
181*0Sstevel@tonic-gate 	erasing = 0;
182*0Sstevel@tonic-gate 	Col = 0;
183*0Sstevel@tonic-gate 	outstr(pr);
184*0Sstevel@tonic-gate 	Pcol = Col;
185*0Sstevel@tonic-gate #else
186*0Sstevel@tonic-gate 	fputs(pr, stdout);
187*0Sstevel@tonic-gate #endif
188*0Sstevel@tonic-gate 	fflush(stdout);
189*0Sstevel@tonic-gate 	if (src != NOSTR && (int)strlen(src) > LINESIZE - 2) {
190*0Sstevel@tonic-gate 		printf(gettext("too long to edit\n"));
191*0Sstevel@tonic-gate 		return(src);
192*0Sstevel@tonic-gate 	}
193*0Sstevel@tonic-gate #ifndef TIOCSTI
194*0Sstevel@tonic-gate 	if (setty())
195*0Sstevel@tonic-gate 		return(src);
196*0Sstevel@tonic-gate 	cp2 = src==NOSTR ? "" : src;
197*0Sstevel@tonic-gate 	for (cp=canonb; *cp2; cp++, cp2++)
198*0Sstevel@tonic-gate 		*cp = *cp2;
199*0Sstevel@tonic-gate 	*cp = '\0';
200*0Sstevel@tonic-gate 	outstr(canonb);
201*0Sstevel@tonic-gate #else
202*0Sstevel@tonic-gate 	cp = src == NOSTR ? "" : src;
203*0Sstevel@tonic-gate 	while (c = *cp++) {
204*0Sstevel@tonic-gate 		char ch;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 		if (c == c_erase || c == c_kill) {
207*0Sstevel@tonic-gate 			ch = '\\';
208*0Sstevel@tonic-gate 			ioctl(0, TIOCSTI, &ch);
209*0Sstevel@tonic-gate 		}
210*0Sstevel@tonic-gate 		ch = c;
211*0Sstevel@tonic-gate 		ioctl(0, TIOCSTI, &ch);
212*0Sstevel@tonic-gate 	}
213*0Sstevel@tonic-gate 	cp = canonb;
214*0Sstevel@tonic-gate 	*cp = 0;
215*0Sstevel@tonic-gate 	if (setjmp(rewrite))
216*0Sstevel@tonic-gate 		goto redo;
217*0Sstevel@tonic-gate #endif
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	for (;;) {
220*0Sstevel@tonic-gate 		fflush(stdout);
221*0Sstevel@tonic-gate #ifdef SIGCONT
222*0Sstevel@tonic-gate 		hadcont = 0;
223*0Sstevel@tonic-gate #endif
224*0Sstevel@tonic-gate 		c = getc(stdin);
225*0Sstevel@tonic-gate 
226*0Sstevel@tonic-gate #ifndef TIOCSTI
227*0Sstevel@tonic-gate 		if (c==c_erase) {
228*0Sstevel@tonic-gate 			if (cp > canonb)
229*0Sstevel@tonic-gate 				if (cp[-1]=='\\' && !erasing) {
230*0Sstevel@tonic-gate 					*cp++ = (char)c;
231*0Sstevel@tonic-gate 					Echo(c);
232*0Sstevel@tonic-gate 				} else {
233*0Sstevel@tonic-gate 					rubout(--cp);
234*0Sstevel@tonic-gate 				}
235*0Sstevel@tonic-gate 		} else if (c==c_kill) {
236*0Sstevel@tonic-gate 			if (cp > canonb && cp[-1]=='\\') {
237*0Sstevel@tonic-gate 				*cp++ = (char)c;
238*0Sstevel@tonic-gate 				Echo(c);
239*0Sstevel@tonic-gate 			} else while (cp > canonb) {
240*0Sstevel@tonic-gate 				rubout(--cp);
241*0Sstevel@tonic-gate 			}
242*0Sstevel@tonic-gate 		} else if (c==c_word) {
243*0Sstevel@tonic-gate 			if (cp > canonb)
244*0Sstevel@tonic-gate 				if (cp[-1]=='\\' && !erasing) {
245*0Sstevel@tonic-gate 					*cp++ = (char)c;
246*0Sstevel@tonic-gate 					Echo(c);
247*0Sstevel@tonic-gate 				} else {
248*0Sstevel@tonic-gate 					while (--cp >= canonb)
249*0Sstevel@tonic-gate 						if (!isspace(*cp))
250*0Sstevel@tonic-gate 							break;
251*0Sstevel@tonic-gate 						else
252*0Sstevel@tonic-gate 							rubout(cp);
253*0Sstevel@tonic-gate 					while (cp >= canonb)
254*0Sstevel@tonic-gate 						if (!isspace(*cp))
255*0Sstevel@tonic-gate 							rubout(cp--);
256*0Sstevel@tonic-gate 						else
257*0Sstevel@tonic-gate 							break;
258*0Sstevel@tonic-gate 					if (cp < canonb)
259*0Sstevel@tonic-gate 						cp = canonb;
260*0Sstevel@tonic-gate 					else if (*cp)
261*0Sstevel@tonic-gate 						cp++;
262*0Sstevel@tonic-gate 				}
263*0Sstevel@tonic-gate 		} else
264*0Sstevel@tonic-gate #endif
265*0Sstevel@tonic-gate 		if (c==EOF || ferror(stdin) || c==c_intr || c==c_quit) {
266*0Sstevel@tonic-gate #ifdef SIGCONT
267*0Sstevel@tonic-gate 			if (hadcont) {
268*0Sstevel@tonic-gate #ifndef TIOCSTI
269*0Sstevel@tonic-gate 				(void) setty();
270*0Sstevel@tonic-gate 				outstr("(continue)\n");
271*0Sstevel@tonic-gate 				Col = 0;
272*0Sstevel@tonic-gate 				outstr(pr);
273*0Sstevel@tonic-gate 				*cp = '\0';
274*0Sstevel@tonic-gate 				outstr(canonb);
275*0Sstevel@tonic-gate 				clearerr(stdin);
276*0Sstevel@tonic-gate 				continue;
277*0Sstevel@tonic-gate #else
278*0Sstevel@tonic-gate 			redo:
279*0Sstevel@tonic-gate 				hadcont = 0;
280*0Sstevel@tonic-gate 				cp = canonb[0] != 0 ? canonb : src;
281*0Sstevel@tonic-gate 				clearerr(stdin);
282*0Sstevel@tonic-gate 				return(readtty(pr, cp));
283*0Sstevel@tonic-gate #endif
284*0Sstevel@tonic-gate 			}
285*0Sstevel@tonic-gate #endif
286*0Sstevel@tonic-gate #ifndef TIOCSTI
287*0Sstevel@tonic-gate 			resetty();
288*0Sstevel@tonic-gate #endif
289*0Sstevel@tonic-gate 			savedead(c==c_quit? SIGQUIT: SIGINT);
290*0Sstevel@tonic-gate 		} else switch (c) {
291*0Sstevel@tonic-gate 			case '\n':
292*0Sstevel@tonic-gate 			case '\r':
293*0Sstevel@tonic-gate #ifndef TIOCSTI
294*0Sstevel@tonic-gate 				resetty();
295*0Sstevel@tonic-gate 				putchar('\n');
296*0Sstevel@tonic-gate 				fflush(stdout);
297*0Sstevel@tonic-gate #endif
298*0Sstevel@tonic-gate 				if (canonb[0]=='\0')
299*0Sstevel@tonic-gate 					return(NOSTR);
300*0Sstevel@tonic-gate 				return(savestr(canonb));
301*0Sstevel@tonic-gate 			default:
302*0Sstevel@tonic-gate 				*cp++ = (char)c;
303*0Sstevel@tonic-gate 				*cp = '\0';
304*0Sstevel@tonic-gate #ifndef TIOCSTI
305*0Sstevel@tonic-gate 				erasing = 0;
306*0Sstevel@tonic-gate 				Echo(c);
307*0Sstevel@tonic-gate #endif
308*0Sstevel@tonic-gate 		}
309*0Sstevel@tonic-gate 	}
310*0Sstevel@tonic-gate }
311*0Sstevel@tonic-gate 
312*0Sstevel@tonic-gate static int
savetty(void)313*0Sstevel@tonic-gate savetty(void)
314*0Sstevel@tonic-gate {
315*0Sstevel@tonic-gate 	if (ioctl(fileno(stdout), TCGETA, &savtty) < 0)
316*0Sstevel@tonic-gate 	{	perror("ioctl");
317*0Sstevel@tonic-gate 		return(-1);
318*0Sstevel@tonic-gate 	}
319*0Sstevel@tonic-gate 	c_erase = savtty.c_cc[VERASE];
320*0Sstevel@tonic-gate 	c_kill = savtty.c_cc[VKILL];
321*0Sstevel@tonic-gate 	c_intr = savtty.c_cc[VINTR];
322*0Sstevel@tonic-gate 	c_quit = savtty.c_cc[VQUIT];
323*0Sstevel@tonic-gate #ifndef TIOCSTI
324*0Sstevel@tonic-gate 	c_word = 'W' & 037;	/* erase word character */
325*0Sstevel@tonic-gate 	Out = fileno(stdout);
326*0Sstevel@tonic-gate 	ttybuf = savtty;
327*0Sstevel@tonic-gate #ifdef	u370
328*0Sstevel@tonic-gate 	ttybuf.c_cflag &= ~PARENB;	/* disable parity */
329*0Sstevel@tonic-gate 	ttybuf.c_cflag |= CS8;		/* character size = 8 */
330*0Sstevel@tonic-gate #endif	/* u370 */
331*0Sstevel@tonic-gate 	ttybuf.c_cc[VTIME] = 0;
332*0Sstevel@tonic-gate 	ttybuf.c_cc[VMIN] = 1;
333*0Sstevel@tonic-gate 	ttybuf.c_iflag &= ~(BRKINT);
334*0Sstevel@tonic-gate 	ttybuf.c_lflag &= ~(ICANON|ISIG|ECHO);
335*0Sstevel@tonic-gate #endif
336*0Sstevel@tonic-gate 	return 0;
337*0Sstevel@tonic-gate }
338*0Sstevel@tonic-gate 
339*0Sstevel@tonic-gate #ifndef TIOCSTI
340*0Sstevel@tonic-gate static int
setty(void)341*0Sstevel@tonic-gate setty(void)
342*0Sstevel@tonic-gate {
343*0Sstevel@tonic-gate 	if (ioctl(Out, TCSETAW, &ttybuf) < 0) {
344*0Sstevel@tonic-gate 		perror("ioctl");
345*0Sstevel@tonic-gate 		return(-1);
346*0Sstevel@tonic-gate 	}
347*0Sstevel@tonic-gate 	return(0);
348*0Sstevel@tonic-gate }
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate static void
resetty(void)351*0Sstevel@tonic-gate resetty(void)
352*0Sstevel@tonic-gate {
353*0Sstevel@tonic-gate 	if (ioctl(Out, TCSETAW, &savtty) < 0)
354*0Sstevel@tonic-gate 		perror("ioctl");
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate 
357*0Sstevel@tonic-gate static void
outstr(register char * s)358*0Sstevel@tonic-gate outstr(register char *s)
359*0Sstevel@tonic-gate {
360*0Sstevel@tonic-gate 	while (*s)
361*0Sstevel@tonic-gate 		Echo(*s++);
362*0Sstevel@tonic-gate }
363*0Sstevel@tonic-gate 
364*0Sstevel@tonic-gate static void
rubout(register char * cp)365*0Sstevel@tonic-gate rubout(register char *cp)
366*0Sstevel@tonic-gate {
367*0Sstevel@tonic-gate 	register int oldcol;
368*0Sstevel@tonic-gate 	register int c = *cp;
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate 	erasing = 1;
371*0Sstevel@tonic-gate 	*cp = '\0';
372*0Sstevel@tonic-gate 	switch (c) {
373*0Sstevel@tonic-gate 	case '\t':
374*0Sstevel@tonic-gate 		oldcol = countcol();
375*0Sstevel@tonic-gate 		do
376*0Sstevel@tonic-gate 			putchar('\b');
377*0Sstevel@tonic-gate 		while (--Col > oldcol);
378*0Sstevel@tonic-gate 		break;
379*0Sstevel@tonic-gate 	case '\b':
380*0Sstevel@tonic-gate 		if (isprint(cp[-1]))
381*0Sstevel@tonic-gate 			putchar(*(cp-1));
382*0Sstevel@tonic-gate 		else
383*0Sstevel@tonic-gate 			putchar(' ');
384*0Sstevel@tonic-gate 		Col++;
385*0Sstevel@tonic-gate 		break;
386*0Sstevel@tonic-gate 	default:
387*0Sstevel@tonic-gate 		if (isprint(c)) {
388*0Sstevel@tonic-gate 			fputs("\b \b", stdout);
389*0Sstevel@tonic-gate 			Col--;
390*0Sstevel@tonic-gate 		}
391*0Sstevel@tonic-gate 	}
392*0Sstevel@tonic-gate }
393*0Sstevel@tonic-gate 
394*0Sstevel@tonic-gate static int
countcol(void)395*0Sstevel@tonic-gate countcol(void)
396*0Sstevel@tonic-gate {
397*0Sstevel@tonic-gate 	register int col;
398*0Sstevel@tonic-gate 	register char *s;
399*0Sstevel@tonic-gate 
400*0Sstevel@tonic-gate 	for (col=Pcol, s=canonb; *s; s++)
401*0Sstevel@tonic-gate 		switch (*s) {
402*0Sstevel@tonic-gate 		case '\t':
403*0Sstevel@tonic-gate 			while (++col % 8)
404*0Sstevel@tonic-gate 				;
405*0Sstevel@tonic-gate 			break;
406*0Sstevel@tonic-gate 		case '\b':
407*0Sstevel@tonic-gate 			col--;
408*0Sstevel@tonic-gate 			break;
409*0Sstevel@tonic-gate 		default:
410*0Sstevel@tonic-gate 			if (isprint(*s))
411*0Sstevel@tonic-gate 				col++;
412*0Sstevel@tonic-gate 		}
413*0Sstevel@tonic-gate 	return(col);
414*0Sstevel@tonic-gate }
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate static void
Echo(int cc)417*0Sstevel@tonic-gate Echo(int cc)
418*0Sstevel@tonic-gate {
419*0Sstevel@tonic-gate 	char c = (char)cc;
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	switch (c) {
422*0Sstevel@tonic-gate 	case '\t':
423*0Sstevel@tonic-gate 		do
424*0Sstevel@tonic-gate 			putchar(' ');
425*0Sstevel@tonic-gate 		while (++Col % 8);
426*0Sstevel@tonic-gate 		break;
427*0Sstevel@tonic-gate 	case '\b':
428*0Sstevel@tonic-gate 		if (Col > 0) {
429*0Sstevel@tonic-gate 			putchar('\b');
430*0Sstevel@tonic-gate 			Col--;
431*0Sstevel@tonic-gate 		}
432*0Sstevel@tonic-gate 		break;
433*0Sstevel@tonic-gate 	case '\r':
434*0Sstevel@tonic-gate 	case '\n':
435*0Sstevel@tonic-gate 		Col = 0;
436*0Sstevel@tonic-gate 		fputs("\r\n", stdout);
437*0Sstevel@tonic-gate 		break;
438*0Sstevel@tonic-gate 	default:
439*0Sstevel@tonic-gate 		if (isprint(c)) {
440*0Sstevel@tonic-gate 			Col++;
441*0Sstevel@tonic-gate 			putchar(c);
442*0Sstevel@tonic-gate 		}
443*0Sstevel@tonic-gate 	}
444*0Sstevel@tonic-gate }
445*0Sstevel@tonic-gate #endif
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate #else
448*0Sstevel@tonic-gate 
449*0Sstevel@tonic-gate #ifdef SIGCONT
450*0Sstevel@tonic-gate static void	signull(int);
451*0Sstevel@tonic-gate #endif
452*0Sstevel@tonic-gate 
453*0Sstevel@tonic-gate static	int	c_erase;		/* Current erase char */
454*0Sstevel@tonic-gate static	int	c_kill;			/* Current kill char */
455*0Sstevel@tonic-gate static	int	hadcont;		/* Saw continue signal */
456*0Sstevel@tonic-gate static	jmp_buf	rewrite;		/* Place to go when continued */
457*0Sstevel@tonic-gate #ifndef TIOCSTI
458*0Sstevel@tonic-gate static	int	ttyset;			/* We must now do erase/kill */
459*0Sstevel@tonic-gate #endif
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate /*
462*0Sstevel@tonic-gate  * Read all relevant header fields.
463*0Sstevel@tonic-gate  */
464*0Sstevel@tonic-gate 
465*0Sstevel@tonic-gate int
grabh(struct header * hp,int gflags,int subjtop)466*0Sstevel@tonic-gate grabh(struct header *hp, int gflags, int subjtop)
467*0Sstevel@tonic-gate {
468*0Sstevel@tonic-gate 	struct sgttyb ttybuf;
469*0Sstevel@tonic-gate 	void (*savecont)(int);
470*0Sstevel@tonic-gate 	register int s;
471*0Sstevel@tonic-gate 	int errs;
472*0Sstevel@tonic-gate #ifndef TIOCSTI
473*0Sstevel@tonic-gate 	void (*savesigs[2])(int);
474*0Sstevel@tonic-gate #endif
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate #ifdef SIGCONT
477*0Sstevel@tonic-gate 	savecont = sigset(SIGCONT, signull);
478*0Sstevel@tonic-gate #endif
479*0Sstevel@tonic-gate 	errs = 0;
480*0Sstevel@tonic-gate #ifndef TIOCSTI
481*0Sstevel@tonic-gate 	ttyset = 0;
482*0Sstevel@tonic-gate #endif
483*0Sstevel@tonic-gate 	if (gtty(fileno(stdin), &ttybuf) < 0) {
484*0Sstevel@tonic-gate 		perror("gtty");
485*0Sstevel@tonic-gate 		return(-1);
486*0Sstevel@tonic-gate 	}
487*0Sstevel@tonic-gate 	c_erase = ttybuf.sg_erase;
488*0Sstevel@tonic-gate 	c_kill = ttybuf.sg_kill;
489*0Sstevel@tonic-gate #ifndef TIOCSTI
490*0Sstevel@tonic-gate 	ttybuf.sg_erase = 0;
491*0Sstevel@tonic-gate 	ttybuf.sg_kill = 0;
492*0Sstevel@tonic-gate 	for (s = SIGINT; s <= SIGQUIT; s++)
493*0Sstevel@tonic-gate 		if ((savesigs[s-SIGINT] = sigset(s, SIG_IGN)) == SIG_DFL)
494*0Sstevel@tonic-gate 			sigset(s, SIG_DFL);
495*0Sstevel@tonic-gate #endif
496*0Sstevel@tonic-gate 	if (gflags & GTO) {
497*0Sstevel@tonic-gate #ifndef TIOCSTI
498*0Sstevel@tonic-gate 		if (!ttyset && hp->h_to != NOSTR)
499*0Sstevel@tonic-gate 			ttyset++, stty(fileno(stdin), &ttybuf);
500*0Sstevel@tonic-gate #endif
501*0Sstevel@tonic-gate 		hp->h_to = addto(NOSTR, readtty("To: ", hp->h_to));
502*0Sstevel@tonic-gate 		if (hp->h_to != NOSTR)
503*0Sstevel@tonic-gate 			hp->h_seq++;
504*0Sstevel@tonic-gate 	}
505*0Sstevel@tonic-gate 	if (gflags & GSUBJECT && subjtop) {
506*0Sstevel@tonic-gate #ifndef TIOCSTI
507*0Sstevel@tonic-gate 		if (!ttyset && hp->h_subject != NOSTR)
508*0Sstevel@tonic-gate 			ttyset++, stty(fileno(stdin), &ttybuf);
509*0Sstevel@tonic-gate #endif
510*0Sstevel@tonic-gate 		hp->h_subject = readtty("Subject: ", hp->h_subject);
511*0Sstevel@tonic-gate 		if (hp->h_subject != NOSTR)
512*0Sstevel@tonic-gate 			hp->h_seq++;
513*0Sstevel@tonic-gate 	}
514*0Sstevel@tonic-gate 	if (gflags & GCC) {
515*0Sstevel@tonic-gate #ifndef TIOCSTI
516*0Sstevel@tonic-gate 		if (!ttyset && hp->h_cc != NOSTR)
517*0Sstevel@tonic-gate 			ttyset++, stty(fileno(stdin), &ttybuf);
518*0Sstevel@tonic-gate #endif
519*0Sstevel@tonic-gate 		hp->h_cc = addto(NOSTR, readtty("Cc: ", hp->h_cc));
520*0Sstevel@tonic-gate 		if (hp->h_cc != NOSTR)
521*0Sstevel@tonic-gate 			hp->h_seq++;
522*0Sstevel@tonic-gate 	}
523*0Sstevel@tonic-gate 	if (gflags & GBCC) {
524*0Sstevel@tonic-gate #ifndef TIOCSTI
525*0Sstevel@tonic-gate 		if (!ttyset && hp->h_bcc != NOSTR)
526*0Sstevel@tonic-gate 			ttyset++, stty(fileno(stdin), &ttybuf);
527*0Sstevel@tonic-gate #endif
528*0Sstevel@tonic-gate 		hp->h_bcc = addto(NOSTR, readtty("Bcc: ", hp->h_bcc));
529*0Sstevel@tonic-gate 		if (hp->h_bcc != NOSTR)
530*0Sstevel@tonic-gate 			hp->h_seq++;
531*0Sstevel@tonic-gate 	}
532*0Sstevel@tonic-gate 	if (gflags & GSUBJECT && !subjtop) {
533*0Sstevel@tonic-gate #ifndef TIOCSTI
534*0Sstevel@tonic-gate 		if (!ttyset && hp->h_subject != NOSTR)
535*0Sstevel@tonic-gate 			ttyset++, stty(fileno(stdin), &ttybuf);
536*0Sstevel@tonic-gate #endif
537*0Sstevel@tonic-gate 		hp->h_subject = readtty("Subject: ", hp->h_subject);
538*0Sstevel@tonic-gate 		if (hp->h_subject != NOSTR)
539*0Sstevel@tonic-gate 			hp->h_seq++;
540*0Sstevel@tonic-gate 	}
541*0Sstevel@tonic-gate #ifdef SIGCONT
542*0Sstevel@tonic-gate 	sigset(SIGCONT, savecont);
543*0Sstevel@tonic-gate #endif
544*0Sstevel@tonic-gate #ifndef TIOCSTI
545*0Sstevel@tonic-gate 	ttybuf.sg_erase = c_erase;
546*0Sstevel@tonic-gate 	ttybuf.sg_kill = c_kill;
547*0Sstevel@tonic-gate 	if (ttyset)
548*0Sstevel@tonic-gate 		stty(fileno(stdin), &ttybuf);
549*0Sstevel@tonic-gate 	for (s = SIGINT; s <= SIGQUIT; s++)
550*0Sstevel@tonic-gate 		sigset(s, savesigs[s-SIGINT]);
551*0Sstevel@tonic-gate #endif
552*0Sstevel@tonic-gate 	return(errs);
553*0Sstevel@tonic-gate }
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate /*
556*0Sstevel@tonic-gate  * Read up a header from standard input.
557*0Sstevel@tonic-gate  * The source string has the preliminary contents to
558*0Sstevel@tonic-gate  * be read.
559*0Sstevel@tonic-gate  *
560*0Sstevel@tonic-gate  */
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate char *
readtty(char pr[],char src[])563*0Sstevel@tonic-gate readtty(char pr[], char src[])
564*0Sstevel@tonic-gate {
565*0Sstevel@tonic-gate 	char ch, canonb[LINESIZE];
566*0Sstevel@tonic-gate 	int c;
567*0Sstevel@tonic-gate 	register char *cp, *cp2;
568*0Sstevel@tonic-gate 
569*0Sstevel@tonic-gate 	fputs(pr, stdout);
570*0Sstevel@tonic-gate 	fflush(stdout);
571*0Sstevel@tonic-gate 	if (src != NOSTR && strlen(src) > LINESIZE - 2) {
572*0Sstevel@tonic-gate 		printf(gettext("too long to edit\n"));
573*0Sstevel@tonic-gate 		return(src);
574*0Sstevel@tonic-gate 	}
575*0Sstevel@tonic-gate #ifndef TIOCSTI
576*0Sstevel@tonic-gate 	if (src != NOSTR)
577*0Sstevel@tonic-gate 		cp = copy(src, canonb);
578*0Sstevel@tonic-gate 	else
579*0Sstevel@tonic-gate 		cp = copy("", canonb);
580*0Sstevel@tonic-gate 	fputs(canonb, stdout);
581*0Sstevel@tonic-gate 	fflush(stdout);
582*0Sstevel@tonic-gate #else
583*0Sstevel@tonic-gate 	cp = src == NOSTR ? "" : src;
584*0Sstevel@tonic-gate 	while (c = *cp++) {
585*0Sstevel@tonic-gate 		if (c == c_erase || c == c_kill) {
586*0Sstevel@tonic-gate 			ch = '\\';
587*0Sstevel@tonic-gate 			ioctl(0, TIOCSTI, &ch);
588*0Sstevel@tonic-gate 		}
589*0Sstevel@tonic-gate 		ch = c;
590*0Sstevel@tonic-gate 		ioctl(0, TIOCSTI, &ch);
591*0Sstevel@tonic-gate 	}
592*0Sstevel@tonic-gate 	cp = canonb;
593*0Sstevel@tonic-gate 	*cp = 0;
594*0Sstevel@tonic-gate #endif
595*0Sstevel@tonic-gate 	cp2 = cp;
596*0Sstevel@tonic-gate 	while (cp2 < canonb + LINESIZE)
597*0Sstevel@tonic-gate 		*cp2++ = 0;
598*0Sstevel@tonic-gate 	cp2 = cp;
599*0Sstevel@tonic-gate 	if (setjmp(rewrite))
600*0Sstevel@tonic-gate 		goto redo;
601*0Sstevel@tonic-gate #ifdef SIGCONT
602*0Sstevel@tonic-gate 	sigset(SIGCONT, ttycont);
603*0Sstevel@tonic-gate #endif
604*0Sstevel@tonic-gate 	clearerr(stdin);
605*0Sstevel@tonic-gate 	while (cp2 < canonb + LINESIZE) {
606*0Sstevel@tonic-gate 		c = getc(stdin);
607*0Sstevel@tonic-gate 		if (c == EOF || c == '\n')
608*0Sstevel@tonic-gate 			break;
609*0Sstevel@tonic-gate 		*cp2++ = c;
610*0Sstevel@tonic-gate 	}
611*0Sstevel@tonic-gate 	*cp2 = 0;
612*0Sstevel@tonic-gate #ifdef SIGCONT
613*0Sstevel@tonic-gate 	sigset(SIGCONT, signull);
614*0Sstevel@tonic-gate #endif
615*0Sstevel@tonic-gate 	if (c == EOF && ferror(stdin) && hadcont) {
616*0Sstevel@tonic-gate redo:
617*0Sstevel@tonic-gate 		hadcont = 0;
618*0Sstevel@tonic-gate 		cp = strlen(canonb) > 0 ? canonb : NOSTR;
619*0Sstevel@tonic-gate 		clearerr(stdin);
620*0Sstevel@tonic-gate 		return(readtty(pr, cp));
621*0Sstevel@tonic-gate 	}
622*0Sstevel@tonic-gate 	clearerr(stdin);
623*0Sstevel@tonic-gate #ifndef TIOCSTI
624*0Sstevel@tonic-gate 	if (cp == NOSTR || *cp == '\0')
625*0Sstevel@tonic-gate 		return(src);
626*0Sstevel@tonic-gate 	cp2 = cp;
627*0Sstevel@tonic-gate 	if (!ttyset)
628*0Sstevel@tonic-gate 		return(strlen(canonb) > 0 ? savestr(canonb) : NOSTR);
629*0Sstevel@tonic-gate 	while (*cp != '\0') {
630*0Sstevel@tonic-gate 		c = *cp++;
631*0Sstevel@tonic-gate 		if (c == c_erase) {
632*0Sstevel@tonic-gate 			if (cp2 == canonb)
633*0Sstevel@tonic-gate 				continue;
634*0Sstevel@tonic-gate 			if (cp2[-1] == '\\') {
635*0Sstevel@tonic-gate 				cp2[-1] = c;
636*0Sstevel@tonic-gate 				continue;
637*0Sstevel@tonic-gate 			}
638*0Sstevel@tonic-gate 			cp2--;
639*0Sstevel@tonic-gate 			continue;
640*0Sstevel@tonic-gate 		}
641*0Sstevel@tonic-gate 		if (c == c_kill) {
642*0Sstevel@tonic-gate 			if (cp2 == canonb)
643*0Sstevel@tonic-gate 				continue;
644*0Sstevel@tonic-gate 			if (cp2[-1] == '\\') {
645*0Sstevel@tonic-gate 				cp2[-1] = c;
646*0Sstevel@tonic-gate 				continue;
647*0Sstevel@tonic-gate 			}
648*0Sstevel@tonic-gate 			cp2 = canonb;
649*0Sstevel@tonic-gate 			continue;
650*0Sstevel@tonic-gate 		}
651*0Sstevel@tonic-gate 		*cp2++ = c;
652*0Sstevel@tonic-gate 	}
653*0Sstevel@tonic-gate 	*cp2 = '\0';
654*0Sstevel@tonic-gate #endif
655*0Sstevel@tonic-gate 	if (equal("", canonb))
656*0Sstevel@tonic-gate 		return(NOSTR);
657*0Sstevel@tonic-gate 	return(savestr(canonb));
658*0Sstevel@tonic-gate }
659*0Sstevel@tonic-gate 
660*0Sstevel@tonic-gate #ifdef SIGCONT
661*0Sstevel@tonic-gate /*
662*0Sstevel@tonic-gate  * Receipt continuation.
663*0Sstevel@tonic-gate  */
664*0Sstevel@tonic-gate /*ARGSUSED*/
665*0Sstevel@tonic-gate void
ttycont(int)666*0Sstevel@tonic-gate ttycont(int)
667*0Sstevel@tonic-gate {
668*0Sstevel@tonic-gate 
669*0Sstevel@tonic-gate 	hadcont++;
670*0Sstevel@tonic-gate 	longjmp(rewrite, 1);
671*0Sstevel@tonic-gate }
672*0Sstevel@tonic-gate 
673*0Sstevel@tonic-gate /*
674*0Sstevel@tonic-gate  * Null routine to allow us to hold SIGCONT
675*0Sstevel@tonic-gate  */
676*0Sstevel@tonic-gate /*ARGSUSED*/
677*0Sstevel@tonic-gate static void
signull(int)678*0Sstevel@tonic-gate signull(int)
679*0Sstevel@tonic-gate {}
680*0Sstevel@tonic-gate #endif
681*0Sstevel@tonic-gate #endif	/* USG_TTY */
682