BSD 3 development
[unix-history] / .ref-BSD-2 / src / Mail / tty.c
CommitLineData
0f5e4452
KS
1/* Copyright (c) 1979 Regents of the University of California */
2#
3
4/*
5 * Mail -- a mail program
6 *
7 * Generally useful tty stuff.
8 */
9
10#include "rcv.h"
11#include <sgtty.h>
12
13static int c_erase; /* Current erase char */
14static int c_kill; /* Current kill char */
15
16/*
17 * Read all relevant header fields.
18 */
19
20grabh(hp, gflags)
21 struct header *hp;
22{
23 struct sgttyb ttybuf;
24 register int s;
25 int (*savesigs[2])(), errs, set;
26
27 errs = 0;
28 set = 0;
29 if (gtty(fileno(stdin), &ttybuf) < 0) {
30 perror("gtty");
31 return(-1);
32 }
33 c_erase = ttybuf.sg_erase;
34 c_kill = ttybuf.sg_kill;
35 ttybuf.sg_erase = 0;
36 ttybuf.sg_kill = 0;
37 for (s = SIGINT; s <= SIGQUIT; s++)
38 if ((savesigs[s-SIGINT] = signal(s, SIG_IGN)) == SIG_DFL)
39 signal(s, SIG_DFL);
40 if (gflags & GTO) {
41 if (!set && hp->h_to != NOSTR)
42 set++, stty(fileno(stdin), &ttybuf);
43 hp->h_to = readtty("To: ", hp->h_to);
44 if (hp->h_to != NOSTR)
45 hp->h_seq++;
46 }
47 if (gflags & GSUBJ) {
48 if (!set && hp->h_subj != NOSTR)
49 set++, stty(fileno(stdin), &ttybuf);
50 hp->h_subj = readtty("Subj: ", hp->h_subj);
51 if (hp->h_subj != NOSTR)
52 hp->h_seq++;
53 }
54 if (gflags & GCC) {
55 if (!set && hp->h_cc != NOSTR)
56 set++, stty(fileno(stdin), &ttybuf);
57 hp->h_cc = readtty("Cc: ", hp->h_cc);
58 if (hp->h_cc != NOSTR)
59 hp->h_seq++;
60 }
61 ttybuf.sg_erase = c_erase;
62 ttybuf.sg_kill = c_kill;
63 if (set)
64 stty(fileno(stdin), &ttybuf);
65
66out:
67 for (s = SIGINT; s <= SIGQUIT; s++)
68 signal(s, savesigs[s-SIGINT]);
69 return(errs);
70}
71
72/*
73 * Read up a header from standard input.
74 * The source string has the preliminary contents to
75 * be read.
76 *
77 */
78
79char *
80readtty(pr, src)
81 char pr[], src[];
82{
83 char canonb[BUFSIZ];
84 register int c;
85 register char *cp, *cp2;
86
87 fputs(pr, stdout);
88 if (src != NOSTR)
89 cp = copy(src, canonb);
90 else
91 cp = copy("", canonb);
92 fputs(canonb, stdout);
93 fflush(stdout);
94 if ((cp2 = gets(cp)) == NOSTR || *cp2 == '\0')
95 return(src);
96 cp = canonb;
97 cp2 = cp;
98 while (*cp != '\0') {
99 c = *cp++;
100 if (c == c_erase) {
101 if (cp2 == canonb)
102 continue;
103 if (cp2[-1] == '\\') {
104 cp2[-1] = c;
105 continue;
106 }
107 cp2--;
108 continue;
109 }
110 if (c == c_kill) {
111 if (cp2 == canonb)
112 continue;
113 if (cp2[-1] == '\\') {
114 cp2[-1] = c;
115 continue;
116 }
117 cp2 = canonb;
118 continue;
119 }
120 *cp2++ = c;
121 }
122 *cp2 = '\0';
123 if (equal("", canonb))
124 return(NOSTR);
125 return(savestr(canonb));
126}