NetBSD/usr.bin/mail/tty.c

308 lines
7.2 KiB
C
Raw Normal View History

2005-07-20 03:07:10 +04:00
/* $NetBSD: tty.c,v 1.19 2005/07/19 23:07:10 christos Exp $ */
1993-03-21 12:45:37 +03:00
/*
1994-06-29 09:09:04 +04:00
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the University nor the names of its contributors
1993-03-21 12:45:37 +03:00
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
#if 0
1996-12-28 10:10:57 +03:00
static char sccsid[] = "@(#)tty.c 8.2 (Berkeley) 6/6/93";
#else
2005-07-20 03:07:10 +04:00
__RCSID("$NetBSD: tty.c,v 1.19 2005/07/19 23:07:10 christos Exp $");
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
/*
* Mail -- a mail program
*
* Generally useful tty stuff.
*/
#include "rcv.h"
1994-06-29 09:09:04 +04:00
#include "extern.h"
1993-03-21 12:45:37 +03:00
1995-05-02 05:40:14 +04:00
static cc_t c_erase; /* Current erase char */
static cc_t c_kill; /* Current kill char */
1993-03-21 12:45:37 +03:00
static jmp_buf rewrite; /* Place to go when continued */
static jmp_buf intjmp; /* Place to go when interrupted */
#ifndef TIOCSTI
static int ttyset; /* We must now do erase/kill */
#endif
/*
* Read all relevant header fields.
*/
1994-06-29 09:09:04 +04:00
int
2002-03-02 17:59:35 +03:00
grabh(struct header *hp, int gflags)
1993-03-21 12:45:37 +03:00
{
1995-05-02 05:40:14 +04:00
struct termios ttybuf;
1993-03-21 12:45:37 +03:00
sig_t saveint;
sig_t savetstp;
sig_t savettou;
sig_t savettin;
int errs;
1998-12-19 19:35:10 +03:00
#ifndef TIOCSTI
sig_t savequit;
#else
# ifdef TIOCEXT
int extproc, flag;
# endif /* TIOCEXT */
#endif /* TIOCSTI */
#ifdef __GNUC__
/* Avoid longjmp clobbering */
# if defined(TIOCSTI) && defined(TIOCEXT)
2002-03-06 00:18:14 +03:00
(void)&extproc;
1998-12-19 19:35:10 +03:00
# endif
2002-03-06 00:18:14 +03:00
(void)&saveint;
1998-12-19 19:35:10 +03:00
#endif /* __GNUC__ */
1993-03-21 12:45:37 +03:00
savetstp = signal(SIGTSTP, SIG_DFL);
savettou = signal(SIGTTOU, SIG_DFL);
savettin = signal(SIGTTIN, SIG_DFL);
errs = 0;
#ifndef TIOCSTI
ttyset = 0;
#endif
1995-05-02 05:40:14 +04:00
if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
2002-03-06 00:29:30 +03:00
warn("tcgetattr");
1993-03-21 12:45:37 +03:00
return(-1);
}
1995-05-02 05:40:14 +04:00
c_erase = ttybuf.c_cc[VERASE];
c_kill = ttybuf.c_cc[VKILL];
1993-03-21 12:45:37 +03:00
#ifndef TIOCSTI
ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
1993-03-21 12:45:37 +03:00
if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
2005-07-20 03:07:10 +04:00
(void)signal(SIGINT, SIG_DFL);
1993-03-21 12:45:37 +03:00
if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
2005-07-20 03:07:10 +04:00
(void)signal(SIGQUIT, SIG_DFL);
1993-03-21 12:45:37 +03:00
#else
1996-12-28 10:10:57 +03:00
# ifdef TIOCEXT
extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
if (extproc) {
flag = 0;
if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
2002-03-06 00:29:30 +03:00
warn("TIOCEXT: off");
1996-12-28 10:10:57 +03:00
}
# endif /* TIOCEXT */
1993-03-21 12:45:37 +03:00
if (setjmp(intjmp))
goto out;
saveint = signal(SIGINT, ttyint);
#endif
if (gflags & GTO) {
#ifndef TIOCSTI
if (!ttyset && hp->h_to != NULL)
1995-05-02 05:40:14 +04:00
ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
1993-03-21 12:45:37 +03:00
#endif
hp->h_to =
extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
}
if (gflags & GSUBJECT) {
#ifndef TIOCSTI
if (!ttyset && hp->h_subject != NULL)
1995-05-02 05:40:14 +04:00
ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
1993-03-21 12:45:37 +03:00
#endif
hp->h_subject = readtty("Subject: ", hp->h_subject);
}
if (gflags & GCC) {
#ifndef TIOCSTI
if (!ttyset && hp->h_cc != NULL)
1995-05-02 05:40:14 +04:00
ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
1993-03-21 12:45:37 +03:00
#endif
hp->h_cc =
extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
}
if (gflags & GBCC) {
#ifndef TIOCSTI
if (!ttyset && hp->h_bcc != NULL)
1995-05-02 05:40:14 +04:00
ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
1993-03-21 12:45:37 +03:00
#endif
hp->h_bcc =
extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
}
out:
2005-07-20 03:07:10 +04:00
(void)signal(SIGTSTP, savetstp);
(void)signal(SIGTTOU, savettou);
(void)signal(SIGTTIN, savettin);
1993-03-21 12:45:37 +03:00
#ifndef TIOCSTI
1995-05-02 05:40:14 +04:00
ttybuf.c_cc[VERASE] = c_erase;
ttybuf.c_cc[VKILL] = c_kill;
1993-03-21 12:45:37 +03:00
if (ttyset)
1995-05-02 05:40:14 +04:00
tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
2005-07-20 03:07:10 +04:00
(void)signal(SIGQUIT, savequit);
1996-12-28 10:10:57 +03:00
#else
# ifdef TIOCEXT
if (extproc) {
flag = 1;
if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
2002-03-06 00:29:30 +03:00
warn("TIOCEXT: on");
1996-12-28 10:10:57 +03:00
}
# endif /* TIOCEXT */
1993-03-21 12:45:37 +03:00
#endif
2005-07-20 03:07:10 +04:00
(void)signal(SIGINT, saveint);
1993-03-21 12:45:37 +03:00
return(errs);
}
/*
* Read up a header from standard input.
* The source string has the preliminary contents to
* be read.
*
*/
char *
2005-07-19 05:38:38 +04:00
readtty(const char pr[], char src[])
1993-03-21 12:45:37 +03:00
{
char ch, canonb[BUFSIZ];
int c;
char *cp, *cp2;
2005-07-19 05:38:38 +04:00
static char empty[] = "";
#if __GNUC__
/* Avoid longjmp clobbering */
2002-03-06 00:18:14 +03:00
(void)&c;
(void)&cp2;
#endif
1993-03-21 12:45:37 +03:00
2005-07-20 03:07:10 +04:00
(void)fputs(pr, stdout);
(void)fflush(stdout);
if (src != NULL && strlen(src) > BUFSIZ - 2) {
2005-07-20 03:07:10 +04:00
(void)printf("too long to edit\n");
1993-03-21 12:45:37 +03:00
return(src);
}
#ifndef TIOCSTI
if (src != NULL)
1993-03-21 12:45:37 +03:00
cp = copy(src, canonb);
else
cp = copy("", canonb);
2005-07-20 03:07:10 +04:00
(void)fputs(canonb, stdout);
(void)fflush(stdout);
1993-03-21 12:45:37 +03:00
#else
2005-07-19 05:38:38 +04:00
cp = src == NULL ? empty : src;
while ((c = *cp++) != '\0') {
1995-05-02 05:40:14 +04:00
if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
(c_kill != _POSIX_VDISABLE && c == c_kill)) {
1993-03-21 12:45:37 +03:00
ch = '\\';
2005-07-20 03:07:10 +04:00
(void)ioctl(0, TIOCSTI, &ch);
1993-03-21 12:45:37 +03:00
}
ch = c;
2005-07-20 03:07:10 +04:00
(void)ioctl(0, TIOCSTI, &ch);
1993-03-21 12:45:37 +03:00
}
cp = canonb;
*cp = 0;
#endif
cp2 = cp;
while (cp2 < canonb + BUFSIZ)
*cp2++ = 0;
cp2 = cp;
if (setjmp(rewrite))
goto redo;
2005-07-20 03:07:10 +04:00
(void)signal(SIGTSTP, ttystop);
(void)signal(SIGTTOU, ttystop);
(void)signal(SIGTTIN, ttystop);
1993-03-21 12:45:37 +03:00
clearerr(stdin);
while (cp2 < canonb + BUFSIZ) {
c = getc(stdin);
if (c == EOF || c == '\n')
break;
*cp2++ = c;
}
*cp2 = 0;
2005-07-20 03:07:10 +04:00
(void)signal(SIGTSTP, SIG_DFL);
(void)signal(SIGTTOU, SIG_DFL);
(void)signal(SIGTTIN, SIG_DFL);
1993-03-21 12:45:37 +03:00
if (c == EOF && ferror(stdin)) {
redo:
cp = strlen(canonb) > 0 ? canonb : NULL;
1993-03-21 12:45:37 +03:00
clearerr(stdin);
return(readtty(pr, cp));
}
#ifndef TIOCSTI
if (cp == NULL || *cp == '\0')
1993-03-21 12:45:37 +03:00
return(src);
cp2 = cp;
if (!ttyset)
return(strlen(canonb) > 0 ? savestr(canonb) : NULL);
1993-03-21 12:45:37 +03:00
while (*cp != '\0') {
c = *cp++;
1995-05-02 05:40:14 +04:00
if (c_erase != _POSIX_VDISABLE && c == c_erase) {
1993-03-21 12:45:37 +03:00
if (cp2 == canonb)
continue;
if (cp2[-1] == '\\') {
cp2[-1] = c;
continue;
}
cp2--;
continue;
}
1995-05-02 05:40:14 +04:00
if (c_kill != _POSIX_VDISABLE && c == c_kill) {
1993-03-21 12:45:37 +03:00
if (cp2 == canonb)
continue;
if (cp2[-1] == '\\') {
cp2[-1] = c;
continue;
}
cp2 = canonb;
continue;
}
*cp2++ = c;
}
*cp2 = '\0';
#endif
if (equal("", canonb))
return(NULL);
1993-03-21 12:45:37 +03:00
return(savestr(canonb));
}
/*
* Receipt continuation.
*/
void
2002-03-02 17:59:35 +03:00
ttystop(int s)
1993-03-21 12:45:37 +03:00
{
sig_t old_action = signal(s, SIG_DFL);
sigset_t nset;
1993-03-21 12:45:37 +03:00
2005-07-20 03:07:10 +04:00
(void)sigemptyset(&nset);
(void)sigaddset(&nset, s);
(void)sigprocmask(SIG_BLOCK, &nset, NULL);
(void)kill(0, s);
(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
(void)signal(s, old_action);
1993-03-21 12:45:37 +03:00
longjmp(rewrite, 1);
}
/*ARGSUSED*/
void
2002-03-02 17:59:35 +03:00
ttyint(int s)
1993-03-21 12:45:37 +03:00
{
longjmp(intjmp, 1);
}