1994-05-27 12:33:43 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 1980, 1993
|
|
|
|
* The Regents of the University of California. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
* 4. Neither the name of the University nor the names of its contributors
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef lint
|
2001-03-25 04:57:05 +00:00
|
|
|
#if 0
|
2001-12-18 20:52:09 +00:00
|
|
|
static char sccsid[] = "@(#)tty.c 8.2 (Berkeley) 6/6/93";
|
2001-03-25 04:57:05 +00:00
|
|
|
#endif
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif /* not lint */
|
2002-06-30 05:25:07 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Mail -- a mail program
|
|
|
|
*
|
|
|
|
* Generally useful tty stuff.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "rcv.h"
|
|
|
|
#include "extern.h"
|
|
|
|
|
2001-12-18 20:52:09 +00:00
|
|
|
static cc_t c_erase; /* Current erase char */
|
|
|
|
static cc_t c_kill; /* Current kill char */
|
1994-05-27 12:33:43 +00: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
int
|
|
|
|
grabh(hp, gflags)
|
|
|
|
struct header *hp;
|
|
|
|
int gflags;
|
|
|
|
{
|
2001-12-18 20:52:09 +00:00
|
|
|
struct termios ttybuf;
|
1994-05-27 12:33:43 +00:00
|
|
|
sig_t saveint;
|
|
|
|
sig_t savetstp;
|
|
|
|
sig_t savettou;
|
|
|
|
sig_t savettin;
|
|
|
|
int errs;
|
2001-12-18 20:52:09 +00:00
|
|
|
#ifndef TIOCSTI
|
|
|
|
sig_t savequit;
|
|
|
|
#else
|
|
|
|
# ifdef TIOCEXT
|
|
|
|
int extproc, flag;
|
|
|
|
# endif /* TIOCEXT */
|
|
|
|
#endif /* TIOCSTI */
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
savetstp = signal(SIGTSTP, SIG_DFL);
|
|
|
|
savettou = signal(SIGTTOU, SIG_DFL);
|
|
|
|
savettin = signal(SIGTTIN, SIG_DFL);
|
|
|
|
errs = 0;
|
|
|
|
#ifndef TIOCSTI
|
|
|
|
ttyset = 0;
|
|
|
|
#endif
|
2001-12-18 20:52:09 +00:00
|
|
|
if (tcgetattr(fileno(stdin), &ttybuf) < 0) {
|
2001-03-25 04:57:05 +00:00
|
|
|
warn("tcgetattr(stdin)");
|
2001-05-27 20:26:22 +00:00
|
|
|
return (-1);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
2001-12-18 20:52:09 +00:00
|
|
|
c_erase = ttybuf.c_cc[VERASE];
|
|
|
|
c_kill = ttybuf.c_cc[VKILL];
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef TIOCSTI
|
2001-12-18 20:52:09 +00:00
|
|
|
ttybuf.c_cc[VERASE] = _POSIX_VDISABLE;
|
|
|
|
ttybuf.c_cc[VKILL] = _POSIX_VDISABLE;
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((saveint = signal(SIGINT, SIG_IGN)) == SIG_DFL)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGINT, SIG_DFL);
|
1994-05-27 12:33:43 +00:00
|
|
|
if ((savequit = signal(SIGQUIT, SIG_IGN)) == SIG_DFL)
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGQUIT, SIG_DFL);
|
1994-05-27 12:33:43 +00:00
|
|
|
#else
|
2001-12-18 20:52:09 +00:00
|
|
|
# ifdef TIOCEXT
|
|
|
|
extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
|
|
|
|
if (extproc) {
|
|
|
|
flag = 0;
|
|
|
|
if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
|
|
|
|
warn("TIOCEXT: off");
|
|
|
|
}
|
|
|
|
# endif /* TIOCEXT */
|
1994-05-27 12:33:43 +00:00
|
|
|
if (setjmp(intjmp))
|
|
|
|
goto out;
|
|
|
|
saveint = signal(SIGINT, ttyint);
|
|
|
|
#endif
|
|
|
|
if (gflags & GTO) {
|
|
|
|
#ifndef TIOCSTI
|
2001-05-27 20:26:22 +00:00
|
|
|
if (!ttyset && hp->h_to != NULL)
|
2001-12-18 20:52:09 +00:00
|
|
|
ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif
|
|
|
|
hp->h_to =
|
|
|
|
extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
|
|
|
|
}
|
|
|
|
if (gflags & GSUBJECT) {
|
|
|
|
#ifndef TIOCSTI
|
2001-05-27 20:26:22 +00:00
|
|
|
if (!ttyset && hp->h_subject != NULL)
|
2001-12-18 20:52:09 +00:00
|
|
|
ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif
|
|
|
|
hp->h_subject = readtty("Subject: ", hp->h_subject);
|
|
|
|
}
|
|
|
|
if (gflags & GCC) {
|
|
|
|
#ifndef TIOCSTI
|
2001-05-27 20:26:22 +00:00
|
|
|
if (!ttyset && hp->h_cc != NULL)
|
2001-12-18 20:52:09 +00:00
|
|
|
ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif
|
|
|
|
hp->h_cc =
|
|
|
|
extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
|
|
|
|
}
|
|
|
|
if (gflags & GBCC) {
|
|
|
|
#ifndef TIOCSTI
|
2001-05-27 20:26:22 +00:00
|
|
|
if (!ttyset && hp->h_bcc != NULL)
|
2001-12-18 20:52:09 +00:00
|
|
|
ttyset++, tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif
|
|
|
|
hp->h_bcc =
|
|
|
|
extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
|
|
|
|
}
|
|
|
|
out:
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGTSTP, savetstp);
|
|
|
|
(void)signal(SIGTTOU, savettou);
|
|
|
|
(void)signal(SIGTTIN, savettin);
|
1994-05-27 12:33:43 +00:00
|
|
|
#ifndef TIOCSTI
|
2001-12-18 20:52:09 +00:00
|
|
|
ttybuf.c_cc[VERASE] = c_erase;
|
|
|
|
ttybuf.c_cc[VKILL] = c_kill;
|
1994-05-27 12:33:43 +00:00
|
|
|
if (ttyset)
|
2001-12-18 20:52:09 +00:00
|
|
|
tcsetattr(fileno(stdin), TCSADRAIN, &ttybuf);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGQUIT, savequit);
|
2001-12-18 20:52:09 +00:00
|
|
|
#else
|
|
|
|
# ifdef TIOCEXT
|
|
|
|
if (extproc) {
|
|
|
|
flag = 1;
|
|
|
|
if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0)
|
|
|
|
warn("TIOCEXT: on");
|
|
|
|
}
|
|
|
|
# endif /* TIOCEXT */
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGINT, saveint);
|
|
|
|
return (errs);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read up a header from standard input.
|
|
|
|
* The source string has the preliminary contents to
|
|
|
|
* be read.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
|
|
|
readtty(pr, src)
|
2001-05-27 20:26:22 +00:00
|
|
|
const char *pr;
|
|
|
|
char src[];
|
1994-05-27 12:33:43 +00:00
|
|
|
{
|
|
|
|
char ch, canonb[BUFSIZ];
|
|
|
|
int c;
|
2001-05-27 20:26:22 +00:00
|
|
|
char *cp, *cp2;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
|
|
|
fputs(pr, stdout);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fflush(stdout);
|
|
|
|
if (src != NULL && strlen(src) > BUFSIZ - 2) {
|
1994-05-27 12:33:43 +00:00
|
|
|
printf("too long to edit\n");
|
2001-05-27 20:26:22 +00:00
|
|
|
return (src);
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
#ifndef TIOCSTI
|
2001-05-27 20:26:22 +00:00
|
|
|
if (src != NULL)
|
2001-03-25 04:57:05 +00:00
|
|
|
strlcpy(canonb, src, sizeof(canonb));
|
1994-05-27 12:33:43 +00:00
|
|
|
else
|
2001-03-25 04:57:05 +00:00
|
|
|
*canonb = '\0';
|
1994-05-27 12:33:43 +00:00
|
|
|
fputs(canonb, stdout);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)fflush(stdout);
|
1994-05-27 12:33:43 +00:00
|
|
|
#else
|
2001-05-27 20:26:22 +00:00
|
|
|
cp = src == NULL ? "" : src;
|
|
|
|
while ((c = *cp++) != '\0') {
|
2001-12-18 20:52:09 +00:00
|
|
|
if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
|
|
|
|
(c_kill != _POSIX_VDISABLE && c == c_kill)) {
|
1994-05-27 12:33:43 +00:00
|
|
|
ch = '\\';
|
|
|
|
ioctl(0, TIOCSTI, &ch);
|
|
|
|
}
|
|
|
|
ch = c;
|
|
|
|
ioctl(0, TIOCSTI, &ch);
|
|
|
|
}
|
|
|
|
cp = canonb;
|
2001-03-25 04:57:05 +00:00
|
|
|
*cp = '\0';
|
1994-05-27 12:33:43 +00:00
|
|
|
#endif
|
|
|
|
cp2 = cp;
|
|
|
|
while (cp2 < canonb + BUFSIZ)
|
2001-03-25 04:57:05 +00:00
|
|
|
*cp2++ = '\0';
|
1994-05-27 12:33:43 +00:00
|
|
|
cp2 = cp;
|
|
|
|
if (setjmp(rewrite))
|
|
|
|
goto redo;
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(SIGTSTP, ttystop);
|
|
|
|
(void)signal(SIGTTOU, ttystop);
|
|
|
|
(void)signal(SIGTTIN, ttystop);
|
1994-05-27 12:33:43 +00:00
|
|
|
clearerr(stdin);
|
|
|
|
while (cp2 < canonb + BUFSIZ) {
|
|
|
|
c = getc(stdin);
|
|
|
|
if (c == EOF || c == '\n')
|
|
|
|
break;
|
|
|
|
*cp2++ = c;
|
|
|
|
}
|
2001-05-27 20:26:22 +00:00
|
|
|
*cp2 = '\0';
|
|
|
|
(void)signal(SIGTSTP, SIG_DFL);
|
|
|
|
(void)signal(SIGTTOU, SIG_DFL);
|
|
|
|
(void)signal(SIGTTIN, SIG_DFL);
|
1994-05-27 12:33:43 +00:00
|
|
|
if (c == EOF && ferror(stdin)) {
|
|
|
|
redo:
|
2001-05-27 20:26:22 +00:00
|
|
|
cp = strlen(canonb) > 0 ? canonb : NULL;
|
1994-05-27 12:33:43 +00:00
|
|
|
clearerr(stdin);
|
2001-05-27 20:26:22 +00:00
|
|
|
return (readtty(pr, cp));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
#ifndef TIOCSTI
|
2001-05-27 20:26:22 +00:00
|
|
|
if (cp == NULL || *cp == '\0')
|
|
|
|
return (src);
|
1994-05-27 12:33:43 +00:00
|
|
|
cp2 = cp;
|
|
|
|
if (!ttyset)
|
2001-05-27 20:26:22 +00:00
|
|
|
return (strlen(canonb) > 0 ? savestr(canonb) : NULL);
|
1994-05-27 12:33:43 +00:00
|
|
|
while (*cp != '\0') {
|
|
|
|
c = *cp++;
|
2001-12-18 20:52:09 +00:00
|
|
|
if (c_erase != _POSIX_VDISABLE && c == c_erase) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (cp2 == canonb)
|
|
|
|
continue;
|
|
|
|
if (cp2[-1] == '\\') {
|
|
|
|
cp2[-1] = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cp2--;
|
|
|
|
continue;
|
|
|
|
}
|
2001-12-18 20:52:09 +00:00
|
|
|
if (c_kill != _POSIX_VDISABLE && c == c_kill) {
|
1994-05-27 12:33:43 +00:00
|
|
|
if (cp2 == canonb)
|
|
|
|
continue;
|
|
|
|
if (cp2[-1] == '\\') {
|
|
|
|
cp2[-1] = c;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
cp2 = canonb;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
*cp2++ = c;
|
|
|
|
}
|
|
|
|
*cp2 = '\0';
|
|
|
|
#endif
|
|
|
|
if (equal("", canonb))
|
2001-05-27 20:26:22 +00:00
|
|
|
return (NULL);
|
|
|
|
return (savestr(canonb));
|
1994-05-27 12:33:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Receipt continuation.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
ttystop(s)
|
|
|
|
int s;
|
|
|
|
{
|
|
|
|
sig_t old_action = signal(s, SIG_DFL);
|
2001-12-18 20:52:09 +00:00
|
|
|
sigset_t nset;
|
1994-05-27 12:33:43 +00:00
|
|
|
|
2001-12-18 20:52:09 +00:00
|
|
|
(void)sigemptyset(&nset);
|
|
|
|
(void)sigaddset(&nset, s);
|
|
|
|
(void)sigprocmask(SIG_BLOCK, &nset, NULL);
|
|
|
|
kill(0, s);
|
|
|
|
(void)sigprocmask(SIG_UNBLOCK, &nset, NULL);
|
2001-05-27 20:26:22 +00:00
|
|
|
(void)signal(s, old_action);
|
1994-05-27 12:33:43 +00:00
|
|
|
longjmp(rewrite, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*ARGSUSED*/
|
|
|
|
void
|
|
|
|
ttyint(s)
|
|
|
|
int s;
|
|
|
|
{
|
|
|
|
longjmp(intjmp, 1);
|
|
|
|
}
|