2 * Heirloom mailx - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
7 * Copyright (c) 1980, 1993
8 * The Regents of the University of California. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
41 static char sccsid
[] = "@(#)tty.c 2.29 (gritter) 3/9/07";
46 * Mail -- a mail program
48 * Generally useful tty stuff.
56 #include <sys/ioctl.h>
58 static cc_t c_erase
; /* Current erase char */
59 static cc_t c_kill
; /* Current kill char */
60 static sigjmp_buf rewrite
; /* Place to go when continued */
61 static sigjmp_buf intjmp
; /* Place to go when interrupted */
63 static int ttyset
; /* We must now do erase/kill */
65 static struct termios ttybuf
;
66 static long vdis
; /* _POSIX_VDISABLE char */
68 static void ttystop(int s
);
69 static void ttyint(int s
);
70 static int safe_getc(FILE *ibuf
);
71 static char *rtty_internal(const char *pr
, char *src
);
74 * Receipt continuation.
79 sighandler_type old_action
= safe_signal(s
, SIG_DFL
);
84 sigprocmask(SIG_BLOCK
, &nset
, NULL
);
86 sigprocmask(SIG_UNBLOCK
, &nset
, NULL
);
87 safe_signal(s
, old_action
);
88 siglongjmp(rewrite
, 1);
95 siglongjmp(intjmp
, 1);
99 * Interrupts will cause trouble if we are inside a stdio call. As
100 * this is only relevant if input comes from a terminal, we can simply
101 * bypass it by read() then.
104 safe_getc(FILE *ibuf
)
106 if (fileno(ibuf
) == 0 && is_a_tty
[0]) {
111 if ((sz
= read(0, &c
, 1)) != 1) {
112 if (sz
< 0 && errno
== EINTR
)
122 * Read up a header from standard input.
123 * The source string has the preliminary contents to
127 rtty_internal(const char *pr
, char *src
)
129 char ch
, canonb
[LINESIZE
];
137 if (src
!= NULL
&& strlen(src
) > sizeof canonb
- 2) {
138 printf(catgets(catd
, CATSET
, 200, "too long to edit\n"));
143 cp
= sstpcpy(canonb
, src
);
145 cp
= sstpcpy(canonb
, "");
146 fputs(canonb
, stdout
);
149 cp
= src
== NULL
? "" : src
;
150 while ((c
= *cp
++) != '\0') {
151 if ((c_erase
!= vdis
&& c
== c_erase
) ||
152 (c_kill
!= vdis
&& c
== c_kill
)) {
154 ioctl(0, TIOCSTI
, &ch
);
157 ioctl(0, TIOCSTI
, &ch
);
163 while (cp2
< canonb
+ sizeof canonb
)
166 if (sigsetjmp(rewrite
, 1))
168 safe_signal(SIGTSTP
, ttystop
);
169 safe_signal(SIGTTOU
, ttystop
);
170 safe_signal(SIGTTIN
, ttystop
);
172 while (cp2
< canonb
+ sizeof canonb
- 1) {
173 c
= safe_getc(stdin
);
174 if (c
== EOF
|| c
== '\n')
179 safe_signal(SIGTSTP
, SIG_DFL
);
180 safe_signal(SIGTTOU
, SIG_DFL
);
181 safe_signal(SIGTTIN
, SIG_DFL
);
182 if (c
== EOF
&& ferror(stdin
)) {
184 cp
= strlen(canonb
) > 0 ? canonb
: NULL
;
186 return(rtty_internal(pr
, cp
));
189 if (cp
== NULL
|| *cp
== '\0')
193 return(strlen(canonb
) > 0 ? savestr(canonb
) : NULL
);
194 while (*cp
!= '\0') {
196 if (c_erase
!= vdis
&& c
== c_erase
) {
199 if (cp2
[-1] == '\\') {
206 if (c_kill
!= vdis
&& c
== c_kill
) {
209 if (cp2
[-1] == '\\') {
220 if (equal("", canonb
))
222 return(savestr(canonb
));
226 * Read all relevant header fields.
230 #define TTYSET_CHECK(h) if (!ttyset && (h) != NULL) \
231 ttyset++, tcsetattr(0, TCSADRAIN, \
234 #define TTYSET_CHECK(h)
237 #define GRAB_SUBJECT if (gflags & GSUBJECT) { \
238 TTYSET_CHECK(hp->h_subject) \
239 hp->h_subject = rtty_internal("Subject: ", \
244 grabaddrs(const char *field
, struct name
*np
, int comma
, enum gfield gflags
)
250 np
= sextract(rtty_internal(field
, detract(np
, comma
)), gflags
);
251 for (nq
= np
; nq
!= NULL
; nq
= nq
->n_flink
)
252 if (mime_name_invalid(nq
->n_name
, 1))
258 grabh(struct header
*hp
, enum gfield gflags
, int subjfirst
)
260 sighandler_type saveint
;
262 sighandler_type savequit
;
264 sighandler_type savetstp
;
265 sighandler_type savettou
;
266 sighandler_type savettin
;
272 savetstp
= safe_signal(SIGTSTP
, SIG_DFL
);
273 savettou
= safe_signal(SIGTTOU
, SIG_DFL
);
274 savettin
= safe_signal(SIGTTIN
, SIG_DFL
);
276 comma
= value("bsdcompat") || value("bsdmsgs") ? 0 : GCOMMA
;
280 if (tcgetattr(fileno(stdin
), &ttybuf
) < 0) {
284 c_erase
= ttybuf
.c_cc
[VERASE
];
285 c_kill
= ttybuf
.c_cc
[VKILL
];
286 #if defined (_PC_VDISABLE) && defined (HAVE_FPATHCONF)
287 if ((vdis
= fpathconf(0, _PC_VDISABLE
)) < 0)
289 #elif defined (_POSIX_VDISABLE)
290 vdis
= _POSIX_VDISABLE
;
295 ttybuf
.c_cc
[VERASE
] = 0;
296 ttybuf
.c_cc
[VKILL
] = 0;
297 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) == SIG_DFL
)
298 safe_signal(SIGINT
, SIG_DFL
);
299 if ((savequit
= safe_signal(SIGQUIT
, SIG_IGN
)) == SIG_DFL
)
300 safe_signal(SIGQUIT
, SIG_DFL
);
302 saveint
= safe_signal(SIGINT
, SIG_IGN
);
303 if (sigsetjmp(intjmp
, 1)) {
304 /* avoid garbled output with C-c */
309 if (saveint
!= SIG_IGN
)
310 safe_signal(SIGINT
, ttyint
);
313 hp
->h_to
= grabaddrs("To: ", hp
->h_to
, comma
, GTO
|GFULL
);
317 hp
->h_cc
= grabaddrs("Cc: ", hp
->h_cc
, comma
, GCC
|GFULL
);
319 hp
->h_bcc
= grabaddrs("Bcc: ", hp
->h_bcc
, comma
, GBCC
|GFULL
);
320 if (gflags
& GEXTRA
) {
321 if (hp
->h_from
== NULL
)
322 hp
->h_from
= sextract(myaddrs(hp
), GEXTRA
|GFULL
);
323 hp
->h_from
= grabaddrs("From: ", hp
->h_from
, comma
,
325 if (hp
->h_replyto
== NULL
)
326 hp
->h_replyto
= sextract(value("replyto"),
328 hp
->h_replyto
= grabaddrs("Reply-To: ", hp
->h_replyto
, comma
,
330 if (hp
->h_sender
== NULL
)
331 hp
->h_sender
= sextract(value("sender"),
333 hp
->h_sender
= grabaddrs("Sender: ", hp
->h_sender
, comma
,
335 if (hp
->h_organization
== NULL
)
336 hp
->h_organization
= value("ORGANIZATION");
337 TTYSET_CHECK(hp
->h_organization
);
338 hp
->h_organization
= rtty_internal("Organization: ",
344 safe_signal(SIGTSTP
, savetstp
);
345 safe_signal(SIGTTOU
, savettou
);
346 safe_signal(SIGTTIN
, savettin
);
348 ttybuf
.c_cc
[VERASE
] = c_erase
;
349 ttybuf
.c_cc
[VKILL
] = c_kill
;
351 tcsetattr(fileno(stdin
), TCSADRAIN
, &ttybuf
);
352 safe_signal(SIGQUIT
, savequit
);
354 safe_signal(SIGINT
, saveint
);
359 * Read a line from tty; to be called from elsewhere
363 readtty(char *prefix
, char *string
)
366 struct termios ttybuf
;
367 sighandler_type saveint
= SIG_DFL
;
369 sighandler_type savequit
;
371 sighandler_type savetstp
;
372 sighandler_type savettou
;
373 sighandler_type savettin
;
377 savetstp
= safe_signal(SIGTSTP
, SIG_DFL
);
378 savettou
= safe_signal(SIGTTOU
, SIG_DFL
);
379 savettin
= safe_signal(SIGTTIN
, SIG_DFL
);
383 if (tcgetattr(fileno(stdin
), &ttybuf
) < 0) {
387 c_erase
= ttybuf
.c_cc
[VERASE
];
388 c_kill
= ttybuf
.c_cc
[VKILL
];
390 ttybuf
.c_cc
[VERASE
] = 0;
391 ttybuf
.c_cc
[VKILL
] = 0;
392 if ((saveint
= safe_signal(SIGINT
, SIG_IGN
)) == SIG_DFL
)
393 safe_signal(SIGINT
, SIG_DFL
);
394 if ((savequit
= safe_signal(SIGQUIT
, SIG_IGN
)) == SIG_DFL
)
395 safe_signal(SIGQUIT
, SIG_DFL
);
397 if (sigsetjmp(intjmp
, 1)) {
398 /* avoid garbled output with C-c */
403 saveint
= safe_signal(SIGINT
, ttyint
);
406 ret
= rtty_internal(prefix
, string
);
407 if (ret
!= NULL
&& *ret
== '\0')
410 safe_signal(SIGTSTP
, savetstp
);
411 safe_signal(SIGTTOU
, savettou
);
412 safe_signal(SIGTTIN
, savettin
);
414 ttybuf
.c_cc
[VERASE
] = c_erase
;
415 ttybuf
.c_cc
[VKILL
] = c_kill
;
417 tcsetattr(fileno(stdin
), TCSADRAIN
, &ttybuf
);
418 safe_signal(SIGQUIT
, savequit
);
420 safe_signal(SIGINT
, saveint
);
429 if (value("interactive") == NULL
)
432 cp
= readtty(msg
, NULL
);
434 *cp
!= 'y' && *cp
!= 'Y' && *cp
!= 'n' && *cp
!= 'N');
435 return *cp
== 'y' || *cp
== 'Y';