1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 * 33 * @(#)tty.c 8.2 (Berkeley) 6/6/93 34 * $FreeBSD: src/usr.bin/mail/tty.c,v 1.2.8.3 2003/01/06 05:46:04 mikeh Exp $ 35 * $DragonFly: src/usr.bin/mail/tty.c,v 1.5 2004/09/08 03:01:11 joerg Exp $ 36 */ 37 38 /* 39 * Mail -- a mail program 40 * 41 * Generally useful tty stuff. 42 */ 43 44 #include "rcv.h" 45 #include "extern.h" 46 47 static cc_t c_erase; /* Current erase char */ 48 static cc_t c_kill; /* Current kill char */ 49 static jmp_buf rewrite; /* Place to go when continued */ 50 static jmp_buf intjmp; /* Place to go when interrupted */ 51 52 /* 53 * Read all relevant header fields. 54 */ 55 56 int 57 grabh(struct header *hp, int gflags) 58 { 59 struct termios ttybuf; 60 sig_t saveint; 61 sig_t savetstp; 62 sig_t savettou; 63 sig_t savettin; 64 int errs; 65 int extproc, flag; 66 67 savetstp = signal(SIGTSTP, SIG_DFL); 68 savettou = signal(SIGTTOU, SIG_DFL); 69 savettin = signal(SIGTTIN, SIG_DFL); 70 errs = 0; 71 if (tcgetattr(fileno(stdin), &ttybuf) < 0) { 72 warn("tcgetattr(stdin)"); 73 return (-1); 74 } 75 c_erase = ttybuf.c_cc[VERASE]; 76 c_kill = ttybuf.c_cc[VKILL]; 77 extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0); 78 if (extproc) { 79 flag = 0; 80 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0) 81 warn("TIOCEXT: off"); 82 } 83 if (setjmp(intjmp)) 84 goto out; 85 saveint = signal(SIGINT, ttyint); 86 if (gflags & GTO) { 87 hp->h_to = 88 extract(readtty("To: ", detract(hp->h_to, 0)), GTO); 89 } 90 if (gflags & GSUBJECT) { 91 hp->h_subject = readtty("Subject: ", hp->h_subject); 92 } 93 if (gflags & GCC) { 94 hp->h_cc = 95 extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC); 96 } 97 if (gflags & GBCC) { 98 hp->h_bcc = 99 extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC); 100 } 101 out: 102 signal(SIGTSTP, savetstp); 103 signal(SIGTTOU, savettou); 104 signal(SIGTTIN, savettin); 105 if (extproc) { 106 flag = 1; 107 if (ioctl(fileno(stdin), TIOCEXT, &flag) < 0) 108 warn("TIOCEXT: on"); 109 } 110 signal(SIGINT, saveint); 111 return (errs); 112 } 113 114 /* 115 * Read up a header from standard input. 116 * The source string has the preliminary contents to 117 * be read. 118 * 119 */ 120 121 char * 122 readtty(const char *pr, char *src) 123 { 124 char ch, canonb[BUFSIZ]; 125 int c; 126 char *cp, *cp2; 127 128 fputs(pr, stdout); 129 fflush(stdout); 130 if (src != NULL && strlen(src) > BUFSIZ - 2) { 131 printf("too long to edit\n"); 132 return (src); 133 } 134 cp = src == NULL ? "" : src; 135 while ((c = *cp++) != '\0') { 136 if ((c_erase != _POSIX_VDISABLE && c == c_erase) || 137 (c_kill != _POSIX_VDISABLE && c == c_kill)) { 138 ch = '\\'; 139 ioctl(0, TIOCSTI, &ch); 140 } 141 ch = c; 142 ioctl(0, TIOCSTI, &ch); 143 } 144 cp = canonb; 145 *cp = '\0'; 146 cp2 = cp; 147 while (cp2 < canonb + BUFSIZ) 148 *cp2++ = '\0'; 149 cp2 = cp; 150 if (setjmp(rewrite)) 151 goto redo; 152 signal(SIGTSTP, ttystop); 153 signal(SIGTTOU, ttystop); 154 signal(SIGTTIN, ttystop); 155 clearerr(stdin); 156 while (cp2 < canonb + BUFSIZ) { 157 c = getc(stdin); 158 if (c == EOF || c == '\n') 159 break; 160 *cp2++ = c; 161 } 162 *cp2 = '\0'; 163 signal(SIGTSTP, SIG_DFL); 164 signal(SIGTTOU, SIG_DFL); 165 signal(SIGTTIN, SIG_DFL); 166 if (c == EOF && ferror(stdin)) { 167 redo: 168 cp = strlen(canonb) > 0 ? canonb : NULL; 169 clearerr(stdin); 170 return (readtty(pr, cp)); 171 } 172 if (equal("", canonb)) 173 return (NULL); 174 return (savestr(canonb)); 175 } 176 177 /* 178 * Receipt continuation. 179 */ 180 void 181 ttystop(int s) 182 { 183 sig_t old_action = signal(s, SIG_DFL); 184 sigset_t nset; 185 186 sigemptyset(&nset); 187 sigaddset(&nset, s); 188 sigprocmask(SIG_BLOCK, &nset, NULL); 189 kill(0, s); 190 sigprocmask(SIG_UNBLOCK, &nset, NULL); 191 signal(s, old_action); 192 longjmp(rewrite, 1); 193 } 194 195 /*ARGSUSED*/ 196 void 197 ttyint(int s) 198 { 199 longjmp(intjmp, 1); 200 } 201