17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate * CDDL HEADER START
37c478bd9Sstevel@tonic-gate *
47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate * with the License.
87c478bd9Sstevel@tonic-gate *
97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate * and limitations under the License.
137c478bd9Sstevel@tonic-gate *
147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate *
207c478bd9Sstevel@tonic-gate * CDDL HEADER END
217c478bd9Sstevel@tonic-gate */
227c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
237c478bd9Sstevel@tonic-gate /* All Rights Reserved */
247c478bd9Sstevel@tonic-gate
257c478bd9Sstevel@tonic-gate
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate * University Copyright- Copyright (c) 1982, 1986, 1988
287c478bd9Sstevel@tonic-gate * The Regents of the University of California
297c478bd9Sstevel@tonic-gate * All Rights Reserved
307c478bd9Sstevel@tonic-gate *
317c478bd9Sstevel@tonic-gate * University Acknowledgment- Portions of this document are derived from
327c478bd9Sstevel@tonic-gate * software developed by the University of California, Berkeley, and its
337c478bd9Sstevel@tonic-gate * contributors.
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate
367c478bd9Sstevel@tonic-gate /*
377c478bd9Sstevel@tonic-gate * mailx -- a modified version of a University of California at Berkeley
387c478bd9Sstevel@tonic-gate * mail program
397c478bd9Sstevel@tonic-gate *
407c478bd9Sstevel@tonic-gate * Generally useful tty stuff.
417c478bd9Sstevel@tonic-gate */
427c478bd9Sstevel@tonic-gate
437c478bd9Sstevel@tonic-gate #include "rcv.h"
447c478bd9Sstevel@tonic-gate #include <locale.h>
457c478bd9Sstevel@tonic-gate
467c478bd9Sstevel@tonic-gate #ifdef USG_TTY
477c478bd9Sstevel@tonic-gate
487c478bd9Sstevel@tonic-gate static char *readtty(char pr[], char src[]);
497c478bd9Sstevel@tonic-gate static int savetty(void);
507c478bd9Sstevel@tonic-gate static void ttycont(int);
517c478bd9Sstevel@tonic-gate
527c478bd9Sstevel@tonic-gate static int c_erase; /* Current erase char */
537c478bd9Sstevel@tonic-gate static int c_kill; /* Current kill char */
547c478bd9Sstevel@tonic-gate static int c_intr; /* interrupt char */
557c478bd9Sstevel@tonic-gate static int c_quit; /* quit character */
567c478bd9Sstevel@tonic-gate static struct termio savtty;
577c478bd9Sstevel@tonic-gate static char canonb[LINESIZE]; /* canonical buffer for input */
587c478bd9Sstevel@tonic-gate /* processing */
597c478bd9Sstevel@tonic-gate
607c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
617c478bd9Sstevel@tonic-gate static void Echo(int cc);
627c478bd9Sstevel@tonic-gate static int countcol(void);
637c478bd9Sstevel@tonic-gate static void outstr(register char *s);
647c478bd9Sstevel@tonic-gate static void resetty(void);
657c478bd9Sstevel@tonic-gate static void rubout(register char *cp);
667c478bd9Sstevel@tonic-gate static int setty(void);
677c478bd9Sstevel@tonic-gate
687c478bd9Sstevel@tonic-gate static int c_word; /* Current word erase char */
697c478bd9Sstevel@tonic-gate static int Col; /* current output column */
707c478bd9Sstevel@tonic-gate static int Pcol; /* end column of prompt string */
717c478bd9Sstevel@tonic-gate static int Out; /* file descriptor of stdout */
727c478bd9Sstevel@tonic-gate static int erasing; /* we are erasing characters */
737c478bd9Sstevel@tonic-gate static struct termio ttybuf;
747c478bd9Sstevel@tonic-gate #else
757c478bd9Sstevel@tonic-gate static jmp_buf rewrite; /* Place to go when continued */
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate #ifdef SIGCONT
797c478bd9Sstevel@tonic-gate # ifdef preSVr4
807c478bd9Sstevel@tonic-gate typedef int sig_atomic_t;
817c478bd9Sstevel@tonic-gate # endif
827c478bd9Sstevel@tonic-gate static sig_atomic_t hadcont; /* Saw continue signal */
837c478bd9Sstevel@tonic-gate
847c478bd9Sstevel@tonic-gate /*ARGSUSED*/
85*2a8bcb4eSToomas Soome static void
867c478bd9Sstevel@tonic-gate #ifdef __cplusplus
ttycont(int)877c478bd9Sstevel@tonic-gate ttycont(int)
887c478bd9Sstevel@tonic-gate #else
897c478bd9Sstevel@tonic-gate /* ARGSUSED */
907c478bd9Sstevel@tonic-gate ttycont(int s)
917c478bd9Sstevel@tonic-gate #endif
927c478bd9Sstevel@tonic-gate {
937c478bd9Sstevel@tonic-gate hadcont++;
947c478bd9Sstevel@tonic-gate longjmp(rewrite, 1);
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
987c478bd9Sstevel@tonic-gate /*ARGSUSED*/
99*2a8bcb4eSToomas Soome static void
ttystop(int s)1007c478bd9Sstevel@tonic-gate ttystop(int s)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate resetty();
1037c478bd9Sstevel@tonic-gate kill(mypid, SIGSTOP);
1047c478bd9Sstevel@tonic-gate }
1057c478bd9Sstevel@tonic-gate #endif
1067c478bd9Sstevel@tonic-gate #endif
1077c478bd9Sstevel@tonic-gate
1087c478bd9Sstevel@tonic-gate /*
1097c478bd9Sstevel@tonic-gate * Read all relevant header fields.
1107c478bd9Sstevel@tonic-gate */
1117c478bd9Sstevel@tonic-gate
112*2a8bcb4eSToomas Soome int
grabh(register struct header * hp,int gflags,int subjtop)1137c478bd9Sstevel@tonic-gate grabh(register struct header *hp, int gflags, int subjtop)
1147c478bd9Sstevel@tonic-gate {
1157c478bd9Sstevel@tonic-gate #ifdef SIGCONT
1167c478bd9Sstevel@tonic-gate void (*savecont)(int);
1177c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
1187c478bd9Sstevel@tonic-gate void (*savestop)(int);
1197c478bd9Sstevel@tonic-gate #endif
1207c478bd9Sstevel@tonic-gate #endif
1217c478bd9Sstevel@tonic-gate if (savetty())
1227c478bd9Sstevel@tonic-gate return -1;
1237c478bd9Sstevel@tonic-gate #ifdef SIGCONT
1247c478bd9Sstevel@tonic-gate savecont = sigset(SIGCONT, ttycont);
1257c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
1267c478bd9Sstevel@tonic-gate savestop = sigset(SIGTSTP, ttystop);
1277c478bd9Sstevel@tonic-gate #endif
1287c478bd9Sstevel@tonic-gate #endif
1297c478bd9Sstevel@tonic-gate if (gflags & GTO) {
1307c478bd9Sstevel@tonic-gate hp->h_to = addto(NOSTR, readtty("To: ", hp->h_to));
1317c478bd9Sstevel@tonic-gate if (hp->h_to != NOSTR)
1327c478bd9Sstevel@tonic-gate hp->h_seq++;
1337c478bd9Sstevel@tonic-gate }
1347c478bd9Sstevel@tonic-gate if (gflags & GSUBJECT && subjtop) {
1357c478bd9Sstevel@tonic-gate hp->h_subject = readtty("Subject: ", hp->h_subject);
1367c478bd9Sstevel@tonic-gate if (hp->h_subject != NOSTR)
1377c478bd9Sstevel@tonic-gate hp->h_seq++;
1387c478bd9Sstevel@tonic-gate }
1397c478bd9Sstevel@tonic-gate if (gflags & GCC) {
1407c478bd9Sstevel@tonic-gate hp->h_cc = addto(NOSTR, readtty("Cc: ", hp->h_cc));
1417c478bd9Sstevel@tonic-gate if (hp->h_cc != NOSTR)
1427c478bd9Sstevel@tonic-gate hp->h_seq++;
1437c478bd9Sstevel@tonic-gate }
1447c478bd9Sstevel@tonic-gate if (gflags & GBCC) {
1457c478bd9Sstevel@tonic-gate hp->h_bcc = addto(NOSTR, readtty("Bcc: ", hp->h_bcc));
1467c478bd9Sstevel@tonic-gate if (hp->h_bcc != NOSTR)
1477c478bd9Sstevel@tonic-gate hp->h_seq++;
1487c478bd9Sstevel@tonic-gate }
1497c478bd9Sstevel@tonic-gate if (gflags & GSUBJECT && !subjtop) {
1507c478bd9Sstevel@tonic-gate hp->h_subject = readtty("Subject: ", hp->h_subject);
1517c478bd9Sstevel@tonic-gate if (hp->h_subject != NOSTR)
1527c478bd9Sstevel@tonic-gate hp->h_seq++;
1537c478bd9Sstevel@tonic-gate }
1547c478bd9Sstevel@tonic-gate #ifdef SIGCONT
1557c478bd9Sstevel@tonic-gate (void) sigset(SIGCONT, savecont);
1567c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
1577c478bd9Sstevel@tonic-gate (void) sigset(SIGTSTP, savestop);
1587c478bd9Sstevel@tonic-gate #endif
1597c478bd9Sstevel@tonic-gate #endif
1607c478bd9Sstevel@tonic-gate return(0);
1617c478bd9Sstevel@tonic-gate }
1627c478bd9Sstevel@tonic-gate
1637c478bd9Sstevel@tonic-gate /*
1647c478bd9Sstevel@tonic-gate * Read up a header from standard input.
1657c478bd9Sstevel@tonic-gate * The source string has the preliminary contents to
1667c478bd9Sstevel@tonic-gate * be read.
1677c478bd9Sstevel@tonic-gate *
1687c478bd9Sstevel@tonic-gate */
1697c478bd9Sstevel@tonic-gate
1707c478bd9Sstevel@tonic-gate static char *
readtty(char pr[],char src[])1717c478bd9Sstevel@tonic-gate readtty(char pr[], char src[])
1727c478bd9Sstevel@tonic-gate {
1737c478bd9Sstevel@tonic-gate int c;
1747c478bd9Sstevel@tonic-gate register char *cp;
1757c478bd9Sstevel@tonic-gate
1767c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
1777c478bd9Sstevel@tonic-gate register char *cp2;
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate erasing = 0;
1807c478bd9Sstevel@tonic-gate Col = 0;
1817c478bd9Sstevel@tonic-gate outstr(pr);
1827c478bd9Sstevel@tonic-gate Pcol = Col;
1837c478bd9Sstevel@tonic-gate #else
1847c478bd9Sstevel@tonic-gate fputs(pr, stdout);
1857c478bd9Sstevel@tonic-gate #endif
1867c478bd9Sstevel@tonic-gate fflush(stdout);
1877c478bd9Sstevel@tonic-gate if (src != NOSTR && (int)strlen(src) > LINESIZE - 2) {
1887c478bd9Sstevel@tonic-gate printf(gettext("too long to edit\n"));
1897c478bd9Sstevel@tonic-gate return(src);
1907c478bd9Sstevel@tonic-gate }
1917c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
1927c478bd9Sstevel@tonic-gate if (setty())
1937c478bd9Sstevel@tonic-gate return(src);
1947c478bd9Sstevel@tonic-gate cp2 = src==NOSTR ? "" : src;
1957c478bd9Sstevel@tonic-gate for (cp=canonb; *cp2; cp++, cp2++)
1967c478bd9Sstevel@tonic-gate *cp = *cp2;
1977c478bd9Sstevel@tonic-gate *cp = '\0';
1987c478bd9Sstevel@tonic-gate outstr(canonb);
1997c478bd9Sstevel@tonic-gate #else
2007c478bd9Sstevel@tonic-gate cp = src == NOSTR ? "" : src;
2017c478bd9Sstevel@tonic-gate while (c = *cp++) {
2027c478bd9Sstevel@tonic-gate char ch;
2037c478bd9Sstevel@tonic-gate
2047c478bd9Sstevel@tonic-gate if (c == c_erase || c == c_kill) {
2057c478bd9Sstevel@tonic-gate ch = '\\';
2067c478bd9Sstevel@tonic-gate ioctl(0, TIOCSTI, &ch);
2077c478bd9Sstevel@tonic-gate }
2087c478bd9Sstevel@tonic-gate ch = c;
2097c478bd9Sstevel@tonic-gate ioctl(0, TIOCSTI, &ch);
2107c478bd9Sstevel@tonic-gate }
2117c478bd9Sstevel@tonic-gate cp = canonb;
2127c478bd9Sstevel@tonic-gate *cp = 0;
2137c478bd9Sstevel@tonic-gate if (setjmp(rewrite))
2147c478bd9Sstevel@tonic-gate goto redo;
2157c478bd9Sstevel@tonic-gate #endif
2167c478bd9Sstevel@tonic-gate
2177c478bd9Sstevel@tonic-gate for (;;) {
2187c478bd9Sstevel@tonic-gate fflush(stdout);
2197c478bd9Sstevel@tonic-gate #ifdef SIGCONT
2207c478bd9Sstevel@tonic-gate hadcont = 0;
2217c478bd9Sstevel@tonic-gate #endif
2227c478bd9Sstevel@tonic-gate c = getc(stdin);
2237c478bd9Sstevel@tonic-gate
2247c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
2257c478bd9Sstevel@tonic-gate if (c==c_erase) {
2267c478bd9Sstevel@tonic-gate if (cp > canonb)
2277c478bd9Sstevel@tonic-gate if (cp[-1]=='\\' && !erasing) {
2287c478bd9Sstevel@tonic-gate *cp++ = (char)c;
2297c478bd9Sstevel@tonic-gate Echo(c);
2307c478bd9Sstevel@tonic-gate } else {
2317c478bd9Sstevel@tonic-gate rubout(--cp);
2327c478bd9Sstevel@tonic-gate }
2337c478bd9Sstevel@tonic-gate } else if (c==c_kill) {
2347c478bd9Sstevel@tonic-gate if (cp > canonb && cp[-1]=='\\') {
2357c478bd9Sstevel@tonic-gate *cp++ = (char)c;
2367c478bd9Sstevel@tonic-gate Echo(c);
2377c478bd9Sstevel@tonic-gate } else while (cp > canonb) {
2387c478bd9Sstevel@tonic-gate rubout(--cp);
2397c478bd9Sstevel@tonic-gate }
2407c478bd9Sstevel@tonic-gate } else if (c==c_word) {
2417c478bd9Sstevel@tonic-gate if (cp > canonb)
2427c478bd9Sstevel@tonic-gate if (cp[-1]=='\\' && !erasing) {
2437c478bd9Sstevel@tonic-gate *cp++ = (char)c;
2447c478bd9Sstevel@tonic-gate Echo(c);
2457c478bd9Sstevel@tonic-gate } else {
2467c478bd9Sstevel@tonic-gate while (--cp >= canonb)
2477c478bd9Sstevel@tonic-gate if (!isspace(*cp))
2487c478bd9Sstevel@tonic-gate break;
2497c478bd9Sstevel@tonic-gate else
2507c478bd9Sstevel@tonic-gate rubout(cp);
2517c478bd9Sstevel@tonic-gate while (cp >= canonb)
2527c478bd9Sstevel@tonic-gate if (!isspace(*cp))
2537c478bd9Sstevel@tonic-gate rubout(cp--);
2547c478bd9Sstevel@tonic-gate else
2557c478bd9Sstevel@tonic-gate break;
2567c478bd9Sstevel@tonic-gate if (cp < canonb)
2577c478bd9Sstevel@tonic-gate cp = canonb;
2587c478bd9Sstevel@tonic-gate else if (*cp)
2597c478bd9Sstevel@tonic-gate cp++;
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate } else
2627c478bd9Sstevel@tonic-gate #endif
2637c478bd9Sstevel@tonic-gate if (c==EOF || ferror(stdin) || c==c_intr || c==c_quit) {
2647c478bd9Sstevel@tonic-gate #ifdef SIGCONT
2657c478bd9Sstevel@tonic-gate if (hadcont) {
2667c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
2677c478bd9Sstevel@tonic-gate (void) setty();
2687c478bd9Sstevel@tonic-gate outstr("(continue)\n");
2697c478bd9Sstevel@tonic-gate Col = 0;
2707c478bd9Sstevel@tonic-gate outstr(pr);
2717c478bd9Sstevel@tonic-gate *cp = '\0';
2727c478bd9Sstevel@tonic-gate outstr(canonb);
2737c478bd9Sstevel@tonic-gate clearerr(stdin);
2747c478bd9Sstevel@tonic-gate continue;
2757c478bd9Sstevel@tonic-gate #else
2767c478bd9Sstevel@tonic-gate redo:
2777c478bd9Sstevel@tonic-gate hadcont = 0;
2787c478bd9Sstevel@tonic-gate cp = canonb[0] != 0 ? canonb : src;
2797c478bd9Sstevel@tonic-gate clearerr(stdin);
2807c478bd9Sstevel@tonic-gate return(readtty(pr, cp));
2817c478bd9Sstevel@tonic-gate #endif
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate #endif
2847c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
2857c478bd9Sstevel@tonic-gate resetty();
2867c478bd9Sstevel@tonic-gate #endif
2877c478bd9Sstevel@tonic-gate savedead(c==c_quit? SIGQUIT: SIGINT);
2887c478bd9Sstevel@tonic-gate } else switch (c) {
2897c478bd9Sstevel@tonic-gate case '\n':
2907c478bd9Sstevel@tonic-gate case '\r':
2917c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
2927c478bd9Sstevel@tonic-gate resetty();
2937c478bd9Sstevel@tonic-gate putchar('\n');
2947c478bd9Sstevel@tonic-gate fflush(stdout);
2957c478bd9Sstevel@tonic-gate #endif
2967c478bd9Sstevel@tonic-gate if (canonb[0]=='\0')
2977c478bd9Sstevel@tonic-gate return(NOSTR);
2987c478bd9Sstevel@tonic-gate return(savestr(canonb));
2997c478bd9Sstevel@tonic-gate default:
3007c478bd9Sstevel@tonic-gate *cp++ = (char)c;
3017c478bd9Sstevel@tonic-gate *cp = '\0';
3027c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
3037c478bd9Sstevel@tonic-gate erasing = 0;
3047c478bd9Sstevel@tonic-gate Echo(c);
3057c478bd9Sstevel@tonic-gate #endif
3067c478bd9Sstevel@tonic-gate }
3077c478bd9Sstevel@tonic-gate }
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate
310*2a8bcb4eSToomas Soome static int
savetty(void)3117c478bd9Sstevel@tonic-gate savetty(void)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate if (ioctl(fileno(stdout), TCGETA, &savtty) < 0)
3147c478bd9Sstevel@tonic-gate { perror("ioctl");
3157c478bd9Sstevel@tonic-gate return(-1);
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate c_erase = savtty.c_cc[VERASE];
3187c478bd9Sstevel@tonic-gate c_kill = savtty.c_cc[VKILL];
3197c478bd9Sstevel@tonic-gate c_intr = savtty.c_cc[VINTR];
3207c478bd9Sstevel@tonic-gate c_quit = savtty.c_cc[VQUIT];
3217c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
3227c478bd9Sstevel@tonic-gate c_word = 'W' & 037; /* erase word character */
3237c478bd9Sstevel@tonic-gate Out = fileno(stdout);
3247c478bd9Sstevel@tonic-gate ttybuf = savtty;
3257c478bd9Sstevel@tonic-gate #ifdef u370
3267c478bd9Sstevel@tonic-gate ttybuf.c_cflag &= ~PARENB; /* disable parity */
3277c478bd9Sstevel@tonic-gate ttybuf.c_cflag |= CS8; /* character size = 8 */
3287c478bd9Sstevel@tonic-gate #endif /* u370 */
3297c478bd9Sstevel@tonic-gate ttybuf.c_cc[VTIME] = 0;
3307c478bd9Sstevel@tonic-gate ttybuf.c_cc[VMIN] = 1;
3317c478bd9Sstevel@tonic-gate ttybuf.c_iflag &= ~(BRKINT);
3327c478bd9Sstevel@tonic-gate ttybuf.c_lflag &= ~(ICANON|ISIG|ECHO);
3337c478bd9Sstevel@tonic-gate #endif
3347c478bd9Sstevel@tonic-gate return 0;
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate
3377c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
338*2a8bcb4eSToomas Soome static int
setty(void)3397c478bd9Sstevel@tonic-gate setty(void)
3407c478bd9Sstevel@tonic-gate {
3417c478bd9Sstevel@tonic-gate if (ioctl(Out, TCSETAW, &ttybuf) < 0) {
3427c478bd9Sstevel@tonic-gate perror("ioctl");
3437c478bd9Sstevel@tonic-gate return(-1);
3447c478bd9Sstevel@tonic-gate }
3457c478bd9Sstevel@tonic-gate return(0);
3467c478bd9Sstevel@tonic-gate }
3477c478bd9Sstevel@tonic-gate
348*2a8bcb4eSToomas Soome static void
resetty(void)3497c478bd9Sstevel@tonic-gate resetty(void)
3507c478bd9Sstevel@tonic-gate {
3517c478bd9Sstevel@tonic-gate if (ioctl(Out, TCSETAW, &savtty) < 0)
3527c478bd9Sstevel@tonic-gate perror("ioctl");
3537c478bd9Sstevel@tonic-gate }
3547c478bd9Sstevel@tonic-gate
355*2a8bcb4eSToomas Soome static void
outstr(register char * s)3567c478bd9Sstevel@tonic-gate outstr(register char *s)
3577c478bd9Sstevel@tonic-gate {
3587c478bd9Sstevel@tonic-gate while (*s)
3597c478bd9Sstevel@tonic-gate Echo(*s++);
3607c478bd9Sstevel@tonic-gate }
3617c478bd9Sstevel@tonic-gate
362*2a8bcb4eSToomas Soome static void
rubout(register char * cp)3637c478bd9Sstevel@tonic-gate rubout(register char *cp)
3647c478bd9Sstevel@tonic-gate {
3657c478bd9Sstevel@tonic-gate register int oldcol;
3667c478bd9Sstevel@tonic-gate register int c = *cp;
3677c478bd9Sstevel@tonic-gate
3687c478bd9Sstevel@tonic-gate erasing = 1;
3697c478bd9Sstevel@tonic-gate *cp = '\0';
3707c478bd9Sstevel@tonic-gate switch (c) {
3717c478bd9Sstevel@tonic-gate case '\t':
3727c478bd9Sstevel@tonic-gate oldcol = countcol();
3737c478bd9Sstevel@tonic-gate do
3747c478bd9Sstevel@tonic-gate putchar('\b');
3757c478bd9Sstevel@tonic-gate while (--Col > oldcol);
3767c478bd9Sstevel@tonic-gate break;
3777c478bd9Sstevel@tonic-gate case '\b':
3787c478bd9Sstevel@tonic-gate if (isprint(cp[-1]))
3797c478bd9Sstevel@tonic-gate putchar(*(cp-1));
3807c478bd9Sstevel@tonic-gate else
3817c478bd9Sstevel@tonic-gate putchar(' ');
3827c478bd9Sstevel@tonic-gate Col++;
3837c478bd9Sstevel@tonic-gate break;
3847c478bd9Sstevel@tonic-gate default:
3857c478bd9Sstevel@tonic-gate if (isprint(c)) {
3867c478bd9Sstevel@tonic-gate fputs("\b \b", stdout);
3877c478bd9Sstevel@tonic-gate Col--;
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate }
3917c478bd9Sstevel@tonic-gate
392*2a8bcb4eSToomas Soome static int
countcol(void)3937c478bd9Sstevel@tonic-gate countcol(void)
3947c478bd9Sstevel@tonic-gate {
3957c478bd9Sstevel@tonic-gate register int col;
3967c478bd9Sstevel@tonic-gate register char *s;
3977c478bd9Sstevel@tonic-gate
3987c478bd9Sstevel@tonic-gate for (col=Pcol, s=canonb; *s; s++)
3997c478bd9Sstevel@tonic-gate switch (*s) {
4007c478bd9Sstevel@tonic-gate case '\t':
4017c478bd9Sstevel@tonic-gate while (++col % 8)
4027c478bd9Sstevel@tonic-gate ;
4037c478bd9Sstevel@tonic-gate break;
4047c478bd9Sstevel@tonic-gate case '\b':
4057c478bd9Sstevel@tonic-gate col--;
4067c478bd9Sstevel@tonic-gate break;
4077c478bd9Sstevel@tonic-gate default:
4087c478bd9Sstevel@tonic-gate if (isprint(*s))
4097c478bd9Sstevel@tonic-gate col++;
4107c478bd9Sstevel@tonic-gate }
4117c478bd9Sstevel@tonic-gate return(col);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate
414*2a8bcb4eSToomas Soome static void
Echo(int cc)4157c478bd9Sstevel@tonic-gate Echo(int cc)
4167c478bd9Sstevel@tonic-gate {
4177c478bd9Sstevel@tonic-gate char c = (char)cc;
4187c478bd9Sstevel@tonic-gate
4197c478bd9Sstevel@tonic-gate switch (c) {
4207c478bd9Sstevel@tonic-gate case '\t':
4217c478bd9Sstevel@tonic-gate do
4227c478bd9Sstevel@tonic-gate putchar(' ');
4237c478bd9Sstevel@tonic-gate while (++Col % 8);
4247c478bd9Sstevel@tonic-gate break;
4257c478bd9Sstevel@tonic-gate case '\b':
4267c478bd9Sstevel@tonic-gate if (Col > 0) {
4277c478bd9Sstevel@tonic-gate putchar('\b');
4287c478bd9Sstevel@tonic-gate Col--;
4297c478bd9Sstevel@tonic-gate }
4307c478bd9Sstevel@tonic-gate break;
4317c478bd9Sstevel@tonic-gate case '\r':
4327c478bd9Sstevel@tonic-gate case '\n':
4337c478bd9Sstevel@tonic-gate Col = 0;
4347c478bd9Sstevel@tonic-gate fputs("\r\n", stdout);
4357c478bd9Sstevel@tonic-gate break;
4367c478bd9Sstevel@tonic-gate default:
4377c478bd9Sstevel@tonic-gate if (isprint(c)) {
4387c478bd9Sstevel@tonic-gate Col++;
4397c478bd9Sstevel@tonic-gate putchar(c);
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate }
4427c478bd9Sstevel@tonic-gate }
4437c478bd9Sstevel@tonic-gate #endif
4447c478bd9Sstevel@tonic-gate
4457c478bd9Sstevel@tonic-gate #else
4467c478bd9Sstevel@tonic-gate
4477c478bd9Sstevel@tonic-gate #ifdef SIGCONT
4487c478bd9Sstevel@tonic-gate static void signull(int);
4497c478bd9Sstevel@tonic-gate #endif
4507c478bd9Sstevel@tonic-gate
4517c478bd9Sstevel@tonic-gate static int c_erase; /* Current erase char */
4527c478bd9Sstevel@tonic-gate static int c_kill; /* Current kill char */
4537c478bd9Sstevel@tonic-gate static int hadcont; /* Saw continue signal */
4547c478bd9Sstevel@tonic-gate static jmp_buf rewrite; /* Place to go when continued */
4557c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
4567c478bd9Sstevel@tonic-gate static int ttyset; /* We must now do erase/kill */
4577c478bd9Sstevel@tonic-gate #endif
4587c478bd9Sstevel@tonic-gate
4597c478bd9Sstevel@tonic-gate /*
4607c478bd9Sstevel@tonic-gate * Read all relevant header fields.
4617c478bd9Sstevel@tonic-gate */
4627c478bd9Sstevel@tonic-gate
463*2a8bcb4eSToomas Soome int
grabh(struct header * hp,int gflags,int subjtop)4647c478bd9Sstevel@tonic-gate grabh(struct header *hp, int gflags, int subjtop)
4657c478bd9Sstevel@tonic-gate {
4667c478bd9Sstevel@tonic-gate struct sgttyb ttybuf;
4677c478bd9Sstevel@tonic-gate void (*savecont)(int);
4687c478bd9Sstevel@tonic-gate register int s;
4697c478bd9Sstevel@tonic-gate int errs;
4707c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
4717c478bd9Sstevel@tonic-gate void (*savesigs[2])(int);
4727c478bd9Sstevel@tonic-gate #endif
4737c478bd9Sstevel@tonic-gate
4747c478bd9Sstevel@tonic-gate #ifdef SIGCONT
4757c478bd9Sstevel@tonic-gate savecont = sigset(SIGCONT, signull);
4767c478bd9Sstevel@tonic-gate #endif
4777c478bd9Sstevel@tonic-gate errs = 0;
4787c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
4797c478bd9Sstevel@tonic-gate ttyset = 0;
4807c478bd9Sstevel@tonic-gate #endif
4817c478bd9Sstevel@tonic-gate if (gtty(fileno(stdin), &ttybuf) < 0) {
4827c478bd9Sstevel@tonic-gate perror("gtty");
4837c478bd9Sstevel@tonic-gate return(-1);
4847c478bd9Sstevel@tonic-gate }
4857c478bd9Sstevel@tonic-gate c_erase = ttybuf.sg_erase;
4867c478bd9Sstevel@tonic-gate c_kill = ttybuf.sg_kill;
4877c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
4887c478bd9Sstevel@tonic-gate ttybuf.sg_erase = 0;
4897c478bd9Sstevel@tonic-gate ttybuf.sg_kill = 0;
4907c478bd9Sstevel@tonic-gate for (s = SIGINT; s <= SIGQUIT; s++)
4917c478bd9Sstevel@tonic-gate if ((savesigs[s-SIGINT] = sigset(s, SIG_IGN)) == SIG_DFL)
4927c478bd9Sstevel@tonic-gate sigset(s, SIG_DFL);
4937c478bd9Sstevel@tonic-gate #endif
4947c478bd9Sstevel@tonic-gate if (gflags & GTO) {
4957c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
4967c478bd9Sstevel@tonic-gate if (!ttyset && hp->h_to != NOSTR)
4977c478bd9Sstevel@tonic-gate ttyset++, stty(fileno(stdin), &ttybuf);
4987c478bd9Sstevel@tonic-gate #endif
4997c478bd9Sstevel@tonic-gate hp->h_to = addto(NOSTR, readtty("To: ", hp->h_to));
5007c478bd9Sstevel@tonic-gate if (hp->h_to != NOSTR)
5017c478bd9Sstevel@tonic-gate hp->h_seq++;
5027c478bd9Sstevel@tonic-gate }
5037c478bd9Sstevel@tonic-gate if (gflags & GSUBJECT && subjtop) {
5047c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
5057c478bd9Sstevel@tonic-gate if (!ttyset && hp->h_subject != NOSTR)
5067c478bd9Sstevel@tonic-gate ttyset++, stty(fileno(stdin), &ttybuf);
5077c478bd9Sstevel@tonic-gate #endif
5087c478bd9Sstevel@tonic-gate hp->h_subject = readtty("Subject: ", hp->h_subject);
5097c478bd9Sstevel@tonic-gate if (hp->h_subject != NOSTR)
5107c478bd9Sstevel@tonic-gate hp->h_seq++;
5117c478bd9Sstevel@tonic-gate }
5127c478bd9Sstevel@tonic-gate if (gflags & GCC) {
5137c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
5147c478bd9Sstevel@tonic-gate if (!ttyset && hp->h_cc != NOSTR)
5157c478bd9Sstevel@tonic-gate ttyset++, stty(fileno(stdin), &ttybuf);
5167c478bd9Sstevel@tonic-gate #endif
5177c478bd9Sstevel@tonic-gate hp->h_cc = addto(NOSTR, readtty("Cc: ", hp->h_cc));
5187c478bd9Sstevel@tonic-gate if (hp->h_cc != NOSTR)
5197c478bd9Sstevel@tonic-gate hp->h_seq++;
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate if (gflags & GBCC) {
5227c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
5237c478bd9Sstevel@tonic-gate if (!ttyset && hp->h_bcc != NOSTR)
5247c478bd9Sstevel@tonic-gate ttyset++, stty(fileno(stdin), &ttybuf);
5257c478bd9Sstevel@tonic-gate #endif
5267c478bd9Sstevel@tonic-gate hp->h_bcc = addto(NOSTR, readtty("Bcc: ", hp->h_bcc));
5277c478bd9Sstevel@tonic-gate if (hp->h_bcc != NOSTR)
5287c478bd9Sstevel@tonic-gate hp->h_seq++;
5297c478bd9Sstevel@tonic-gate }
5307c478bd9Sstevel@tonic-gate if (gflags & GSUBJECT && !subjtop) {
5317c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
5327c478bd9Sstevel@tonic-gate if (!ttyset && hp->h_subject != NOSTR)
5337c478bd9Sstevel@tonic-gate ttyset++, stty(fileno(stdin), &ttybuf);
5347c478bd9Sstevel@tonic-gate #endif
5357c478bd9Sstevel@tonic-gate hp->h_subject = readtty("Subject: ", hp->h_subject);
5367c478bd9Sstevel@tonic-gate if (hp->h_subject != NOSTR)
5377c478bd9Sstevel@tonic-gate hp->h_seq++;
5387c478bd9Sstevel@tonic-gate }
5397c478bd9Sstevel@tonic-gate #ifdef SIGCONT
5407c478bd9Sstevel@tonic-gate sigset(SIGCONT, savecont);
5417c478bd9Sstevel@tonic-gate #endif
5427c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
5437c478bd9Sstevel@tonic-gate ttybuf.sg_erase = c_erase;
5447c478bd9Sstevel@tonic-gate ttybuf.sg_kill = c_kill;
5457c478bd9Sstevel@tonic-gate if (ttyset)
5467c478bd9Sstevel@tonic-gate stty(fileno(stdin), &ttybuf);
5477c478bd9Sstevel@tonic-gate for (s = SIGINT; s <= SIGQUIT; s++)
5487c478bd9Sstevel@tonic-gate sigset(s, savesigs[s-SIGINT]);
5497c478bd9Sstevel@tonic-gate #endif
5507c478bd9Sstevel@tonic-gate return(errs);
5517c478bd9Sstevel@tonic-gate }
5527c478bd9Sstevel@tonic-gate
5537c478bd9Sstevel@tonic-gate /*
5547c478bd9Sstevel@tonic-gate * Read up a header from standard input.
5557c478bd9Sstevel@tonic-gate * The source string has the preliminary contents to
5567c478bd9Sstevel@tonic-gate * be read.
5577c478bd9Sstevel@tonic-gate *
5587c478bd9Sstevel@tonic-gate */
5597c478bd9Sstevel@tonic-gate
5607c478bd9Sstevel@tonic-gate char *
readtty(char pr[],char src[])5617c478bd9Sstevel@tonic-gate readtty(char pr[], char src[])
5627c478bd9Sstevel@tonic-gate {
5637c478bd9Sstevel@tonic-gate char ch, canonb[LINESIZE];
5647c478bd9Sstevel@tonic-gate int c;
5657c478bd9Sstevel@tonic-gate register char *cp, *cp2;
5667c478bd9Sstevel@tonic-gate
5677c478bd9Sstevel@tonic-gate fputs(pr, stdout);
5687c478bd9Sstevel@tonic-gate fflush(stdout);
5697c478bd9Sstevel@tonic-gate if (src != NOSTR && strlen(src) > LINESIZE - 2) {
5707c478bd9Sstevel@tonic-gate printf(gettext("too long to edit\n"));
5717c478bd9Sstevel@tonic-gate return(src);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
5747c478bd9Sstevel@tonic-gate if (src != NOSTR)
5757c478bd9Sstevel@tonic-gate cp = copy(src, canonb);
5767c478bd9Sstevel@tonic-gate else
5777c478bd9Sstevel@tonic-gate cp = copy("", canonb);
5787c478bd9Sstevel@tonic-gate fputs(canonb, stdout);
5797c478bd9Sstevel@tonic-gate fflush(stdout);
5807c478bd9Sstevel@tonic-gate #else
5817c478bd9Sstevel@tonic-gate cp = src == NOSTR ? "" : src;
5827c478bd9Sstevel@tonic-gate while (c = *cp++) {
5837c478bd9Sstevel@tonic-gate if (c == c_erase || c == c_kill) {
5847c478bd9Sstevel@tonic-gate ch = '\\';
5857c478bd9Sstevel@tonic-gate ioctl(0, TIOCSTI, &ch);
5867c478bd9Sstevel@tonic-gate }
5877c478bd9Sstevel@tonic-gate ch = c;
5887c478bd9Sstevel@tonic-gate ioctl(0, TIOCSTI, &ch);
5897c478bd9Sstevel@tonic-gate }
5907c478bd9Sstevel@tonic-gate cp = canonb;
5917c478bd9Sstevel@tonic-gate *cp = 0;
5927c478bd9Sstevel@tonic-gate #endif
5937c478bd9Sstevel@tonic-gate cp2 = cp;
5947c478bd9Sstevel@tonic-gate while (cp2 < canonb + LINESIZE)
5957c478bd9Sstevel@tonic-gate *cp2++ = 0;
5967c478bd9Sstevel@tonic-gate cp2 = cp;
5977c478bd9Sstevel@tonic-gate if (setjmp(rewrite))
5987c478bd9Sstevel@tonic-gate goto redo;
5997c478bd9Sstevel@tonic-gate #ifdef SIGCONT
6007c478bd9Sstevel@tonic-gate sigset(SIGCONT, ttycont);
6017c478bd9Sstevel@tonic-gate #endif
6027c478bd9Sstevel@tonic-gate clearerr(stdin);
6037c478bd9Sstevel@tonic-gate while (cp2 < canonb + LINESIZE) {
6047c478bd9Sstevel@tonic-gate c = getc(stdin);
6057c478bd9Sstevel@tonic-gate if (c == EOF || c == '\n')
6067c478bd9Sstevel@tonic-gate break;
6077c478bd9Sstevel@tonic-gate *cp2++ = c;
6087c478bd9Sstevel@tonic-gate }
6097c478bd9Sstevel@tonic-gate *cp2 = 0;
6107c478bd9Sstevel@tonic-gate #ifdef SIGCONT
6117c478bd9Sstevel@tonic-gate sigset(SIGCONT, signull);
6127c478bd9Sstevel@tonic-gate #endif
6137c478bd9Sstevel@tonic-gate if (c == EOF && ferror(stdin) && hadcont) {
6147c478bd9Sstevel@tonic-gate redo:
6157c478bd9Sstevel@tonic-gate hadcont = 0;
6167c478bd9Sstevel@tonic-gate cp = strlen(canonb) > 0 ? canonb : NOSTR;
6177c478bd9Sstevel@tonic-gate clearerr(stdin);
6187c478bd9Sstevel@tonic-gate return(readtty(pr, cp));
6197c478bd9Sstevel@tonic-gate }
6207c478bd9Sstevel@tonic-gate clearerr(stdin);
6217c478bd9Sstevel@tonic-gate #ifndef TIOCSTI
6227c478bd9Sstevel@tonic-gate if (cp == NOSTR || *cp == '\0')
6237c478bd9Sstevel@tonic-gate return(src);
6247c478bd9Sstevel@tonic-gate cp2 = cp;
6257c478bd9Sstevel@tonic-gate if (!ttyset)
6267c478bd9Sstevel@tonic-gate return(strlen(canonb) > 0 ? savestr(canonb) : NOSTR);
6277c478bd9Sstevel@tonic-gate while (*cp != '\0') {
6287c478bd9Sstevel@tonic-gate c = *cp++;
6297c478bd9Sstevel@tonic-gate if (c == c_erase) {
6307c478bd9Sstevel@tonic-gate if (cp2 == canonb)
6317c478bd9Sstevel@tonic-gate continue;
6327c478bd9Sstevel@tonic-gate if (cp2[-1] == '\\') {
6337c478bd9Sstevel@tonic-gate cp2[-1] = c;
6347c478bd9Sstevel@tonic-gate continue;
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate cp2--;
6377c478bd9Sstevel@tonic-gate continue;
6387c478bd9Sstevel@tonic-gate }
6397c478bd9Sstevel@tonic-gate if (c == c_kill) {
6407c478bd9Sstevel@tonic-gate if (cp2 == canonb)
6417c478bd9Sstevel@tonic-gate continue;
6427c478bd9Sstevel@tonic-gate if (cp2[-1] == '\\') {
6437c478bd9Sstevel@tonic-gate cp2[-1] = c;
6447c478bd9Sstevel@tonic-gate continue;
6457c478bd9Sstevel@tonic-gate }
6467c478bd9Sstevel@tonic-gate cp2 = canonb;
6477c478bd9Sstevel@tonic-gate continue;
6487c478bd9Sstevel@tonic-gate }
6497c478bd9Sstevel@tonic-gate *cp2++ = c;
6507c478bd9Sstevel@tonic-gate }
6517c478bd9Sstevel@tonic-gate *cp2 = '\0';
6527c478bd9Sstevel@tonic-gate #endif
6537c478bd9Sstevel@tonic-gate if (equal("", canonb))
6547c478bd9Sstevel@tonic-gate return(NOSTR);
6557c478bd9Sstevel@tonic-gate return(savestr(canonb));
6567c478bd9Sstevel@tonic-gate }
6577c478bd9Sstevel@tonic-gate
6587c478bd9Sstevel@tonic-gate #ifdef SIGCONT
6597c478bd9Sstevel@tonic-gate /*
6607c478bd9Sstevel@tonic-gate * Receipt continuation.
6617c478bd9Sstevel@tonic-gate */
6627c478bd9Sstevel@tonic-gate /*ARGSUSED*/
663*2a8bcb4eSToomas Soome void
ttycont(int)6647c478bd9Sstevel@tonic-gate ttycont(int)
6657c478bd9Sstevel@tonic-gate {
6667c478bd9Sstevel@tonic-gate
6677c478bd9Sstevel@tonic-gate hadcont++;
6687c478bd9Sstevel@tonic-gate longjmp(rewrite, 1);
6697c478bd9Sstevel@tonic-gate }
6707c478bd9Sstevel@tonic-gate
6717c478bd9Sstevel@tonic-gate /*
6727c478bd9Sstevel@tonic-gate * Null routine to allow us to hold SIGCONT
6737c478bd9Sstevel@tonic-gate */
6747c478bd9Sstevel@tonic-gate /*ARGSUSED*/
675*2a8bcb4eSToomas Soome static void
signull(int)6767c478bd9Sstevel@tonic-gate signull(int)
6777c478bd9Sstevel@tonic-gate {}
6787c478bd9Sstevel@tonic-gate #endif
6797c478bd9Sstevel@tonic-gate #endif /* USG_TTY */
680