Expand PMF_FN_* macros.
[netbsd-mini2440.git] / usr.bin / mail / tty.c
blobaaf500b19fdd4beb120bc2ddd5a3733b42e8b487
1 /* $NetBSD: tty.c,v 1.27 2006/11/28 18:45:32 christos Exp $ */
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)tty.c 8.2 (Berkeley) 6/6/93";
36 #else
37 __RCSID("$NetBSD: tty.c,v 1.27 2006/11/28 18:45:32 christos Exp $");
38 #endif
39 #endif /* not lint */
42 * Mail -- a mail program
44 * Generally useful tty stuff.
47 #include "rcv.h"
48 #include "extern.h"
49 #ifdef USE_EDITLINE
50 # include "complete.h"
51 #endif
52 #include "sig.h"
54 static jmp_buf tty_jmpbuf; /* Place to go when interrupted */
56 #ifndef USE_EDITLINE
57 static cc_t c_erase; /* Current erase char */
58 static cc_t c_kill; /* Current kill char */
59 # ifndef TIOCSTI
60 static int ttyset; /* We must now do erase/kill */
61 # endif
62 #endif /* USE_EDITLINE */
65 * Read up a header from standard input.
66 * The source string has the preliminary contents to
67 * be read.
69 * Returns: an salloc'ed copy of the line read if successful, or NULL
70 * if no characters were read or if an error occurred.
73 #ifdef USE_EDITLINE
74 static char *
75 readtty(const char *pr, char *src)
77 char *line;
79 line = my_gets(&elm.string, pr, src);
80 sig_check();
81 if (line == NULL)
82 (void)putc('\n', stdout);
84 return line ? savestr(line) : __UNCONST("");
87 #else /* not USE_EDITLINE */
89 static char *
90 readtty(const char *pr, char *src)
92 char canonb[LINESIZE];
93 char *cp, *cp2;
94 int c;
95 #ifdef TIOCSTI
96 char ch;
97 #endif
99 (void)fputs(pr, stdout);
100 (void)fflush(stdout);
101 if (src != NULL && strlen(src) > sizeof(canonb) - 2) {
102 (void)printf("too long to edit\n");
103 return src;
105 #ifndef TIOCSTI
106 if (src != NULL)
107 cp = copy(src, canonb);
108 else
109 cp = copy(__UNCONST(""), canonb);
110 c = *cp;
111 (void)fputs(canonb, stdout);
112 (void)fflush(stdout);
113 #else
114 cp = src == NULL ? __UNCONST("") : src;
115 while ((c = *cp++) != '\0') {
116 if ((c_erase != _POSIX_VDISABLE && c == c_erase) ||
117 (c_kill != _POSIX_VDISABLE && c == c_kill)) {
118 ch = '\\';
119 (void)ioctl(0, TIOCSTI, &ch);
121 ch = c;
122 (void)ioctl(0, TIOCSTI, &ch);
124 cp = canonb;
125 *cp = '\0';
126 #endif
127 clearerr(stdin);
128 cp2 = cp;
129 while (cp2 < canonb + sizeof(canonb) - 1) {
130 c = getc(stdin);
131 sig_check();
132 if (c == EOF) {
133 if (feof(stdin))
134 (void)putc('\n', stdout);
135 break;
137 if (c == '\n')
138 break;
139 *cp2++ = c;
141 *cp2 = '\0';
143 if (c == EOF && ferror(stdin)) {
144 cp = strlen(canonb) > 0 ? canonb : NULL;
145 clearerr(stdin);
146 return readtty(pr, cp);
148 #ifndef TIOCSTI
149 if (cp == NULL || *cp == '\0')
150 return src;
151 if (ttyset == 0)
152 return strlen(canonb) > 0 ? savestr(canonb) : NULL;
155 * Do erase and kill.
157 cp2 = cp;
158 while (*cp != '\0') {
159 c = *cp++;
160 if (c_erase != _POSIX_VDISABLE && c == c_erase) {
161 if (cp2 == canonb)
162 continue;
163 if (cp2[-1] == '\\') {
164 cp2[-1] = c;
165 continue;
167 cp2--;
168 continue;
170 if (c_kill != _POSIX_VDISABLE && c == c_kill) {
171 if (cp2 == canonb)
172 continue;
173 if (cp2[-1] == '\\') {
174 cp2[-1] = c;
175 continue;
177 cp2 = canonb;
178 continue;
180 *cp2++ = c;
182 *cp2 = '\0';
183 #endif
184 if (canonb[0] == '\0')
185 return __UNCONST("");
186 return savestr(canonb);
188 #endif /* USE_EDITLINE */
190 #ifdef USE_EDITLINE
191 # define save_erase_and_kill(t) 0
192 #else
193 static int
194 save_erase_and_kill(struct termios *t)
197 # ifndef TIOCSTI
198 ttyset = 0;
199 #endif
200 if (tcgetattr(fileno(stdin), t) == -1) {
201 warn("tcgetattr");
202 return -1;
204 c_erase = t->c_cc[VERASE];
205 c_kill = t->c_cc[VKILL];
206 return 0;
208 #endif
210 #if defined(USE_EDITLINE) || defined(TIOCSTI)
211 # define disable_erase_and_kill(t)
212 #else
213 static void
214 disable_erase_and_kill(struct termios *t)
217 if (ttyset == 0) {
218 ttyset = 1;
219 t->c_cc[VERASE] = _POSIX_VDISABLE;
220 t->c_cc[VKILL] = _POSIX_VDISABLE;
221 (void)tcsetattr(fileno(stdin), TCSADRAIN, t);
224 #endif
226 #if defined(USE_EDITLINE) || defined(TIOCSTI)
227 # define restore_erase_and_kill(t)
228 #else
229 static void
230 restore_erase_and_kill(struct termios *t)
233 if (ttyset != 0) {
234 ttyset = 0;
235 t->c_cc[VERASE] = c_erase;
236 t->c_cc[VKILL] = c_kill;
237 (void)tcsetattr(fileno(stdin), TCSADRAIN, t);
240 #endif
243 * Do a shell-like extraction of a line
244 * and make a list of name from it.
245 * Return the list or NULL if none found.
247 static struct name *
248 shextract(char *line, int ntype)
250 struct name *begin, *np, *t;
251 char *argv[MAXARGC];
252 size_t argc, i;
254 begin = NULL;
255 if (line) {
256 np = NULL;
257 argc = getrawlist(line, argv, (int)__arraycount(argv));
258 for (i = 0; i < argc; i++) {
259 t = nalloc(argv[i], ntype);
260 if (begin == NULL)
261 begin = t;
262 else
263 np->n_flink = t;
264 t->n_blink = np;
265 np = t;
268 return begin;
271 /*ARGSUSED*/
272 static void
273 tty_sigint(int signo __unused)
276 longjmp(tty_jmpbuf, 1);
280 * Read all relevant header fields.
281 * Returns 0 on success; 1 if there was an error or signal.
283 PUBLIC int
284 grabh(struct header *hp, int gflags)
286 sig_t volatile old_sigint;
287 int retval;
288 #ifndef USE_EDITLINE
289 struct termios ttybuf;
290 # if defined(TIOCSTI) && defined(TIOCEXT)
291 int extproc;
292 # endif
294 if (save_erase_and_kill(&ttybuf))
295 return -1;
297 # if defined(TIOCSTI) && defined(TIOCEXT)
298 extproc = ((ttybuf.c_lflag & EXTPROC) ? 1 : 0);
299 if (extproc) {
300 int flag;
302 flag = 0;
303 if (ioctl(fileno(stdin), TIOCEXT, &flag) == -1)
304 warn("TIOCEXT: off");
306 # endif
307 #endif /* USE_EDITLINE */
309 sig_check();
310 old_sigint = sig_signal(SIGINT, tty_sigint);
312 /* return here if we detect a SIGINT */
313 if ((retval = setjmp(tty_jmpbuf)) != 0) {
314 (void)putc('\n', stdout);
315 goto out;
319 * Do this irrespective of whether the initial string is empty.
320 * Otherwise, the editing is inconsistent.
322 disable_erase_and_kill(&ttybuf);
324 if (gflags & GTO) {
325 hp->h_to =
326 extract(readtty("To: ", detract(hp->h_to, 0)), GTO);
328 if (gflags & GSUBJECT) {
329 hp->h_subject = readtty("Subject: ", hp->h_subject);
331 if (gflags & GCC) {
332 hp->h_cc =
333 extract(readtty("Cc: ", detract(hp->h_cc, 0)), GCC);
335 if (gflags & GBCC) {
336 hp->h_bcc =
337 extract(readtty("Bcc: ", detract(hp->h_bcc, 0)), GBCC);
339 if (gflags & GSMOPTS) {
340 hp->h_smopts =
341 shextract(readtty("Smopts: ", detract(hp->h_smopts, 0)),
342 GSMOPTS);
344 #ifdef MIME_SUPPORT
345 if (gflags & GSMOPTS) { /* XXX - Use a new flag for this? */
346 if (hp->h_attach) {
347 struct attachment *ap;
348 int i;
350 i = 0;
351 for (ap = hp->h_attach; ap; ap = ap->a_flink)
352 i++;
353 (void)printf("Attachment%s: %d\n", i > 1 ? "s" : "", i);
356 #endif
357 out:
358 restore_erase_and_kill(&ttybuf);
360 #ifndef USE_EDITLINE
361 # if defined(TIOCSTI) && defined(TIOCEXT)
362 if (extproc) {
363 int flag;
364 flag = 1;
365 if (ioctl(fileno(stdin), TIOCEXT, &flag) == -1)
366 warn("TIOCEXT: on");
368 # endif
369 #endif
370 (void)sig_signal(SIGINT, old_sigint);
371 sig_check();
372 return retval;