main.c revision 1.6 1 1.6 tls /* $NetBSD: main.c,v 1.6 1996/12/28 07:11:07 tls Exp $ */
2 1.5 christos
3 1.1 cgd /*
4 1.3 deraadt * Copyright (c) 1980, 1993
5 1.3 deraadt * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.3 deraadt static char copyright[] =
38 1.3 deraadt "@(#) Copyright (c) 1980, 1993\n\
39 1.3 deraadt The Regents of the University of California. All rights reserved.\n";
40 1.1 cgd #endif /* not lint */
41 1.1 cgd
42 1.1 cgd #ifndef lint
43 1.5 christos #if 0
44 1.6 tls static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/20/95";
45 1.5 christos #else
46 1.6 tls static char rcsid[] = "$NetBSD: main.c,v 1.6 1996/12/28 07:11:07 tls Exp $";
47 1.5 christos #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.1 cgd #include "rcv.h"
51 1.3 deraadt #include <fcntl.h>
52 1.5 christos #include <sys/ioctl.h>
53 1.3 deraadt #include "extern.h"
54 1.1 cgd
55 1.1 cgd /*
56 1.1 cgd * Mail -- a mail program
57 1.1 cgd *
58 1.1 cgd * Startup -- interface with user.
59 1.1 cgd */
60 1.1 cgd
61 1.1 cgd jmp_buf hdrjmp;
62 1.1 cgd
63 1.3 deraadt int
64 1.1 cgd main(argc, argv)
65 1.3 deraadt int argc;
66 1.3 deraadt char *argv[];
67 1.1 cgd {
68 1.1 cgd register int i;
69 1.1 cgd struct name *to, *cc, *bcc, *smopts;
70 1.1 cgd char *subject;
71 1.1 cgd char *ef;
72 1.1 cgd char nosrc = 0;
73 1.1 cgd sig_t prevint;
74 1.6 tls char *rc;
75 1.1 cgd
76 1.1 cgd /*
77 1.1 cgd * Set up a reasonable environment.
78 1.1 cgd * Figure out whether we are being run interactively,
79 1.1 cgd * start the SIGCHLD catcher, and so forth.
80 1.1 cgd */
81 1.1 cgd (void) signal(SIGCHLD, sigchild);
82 1.1 cgd if (isatty(0))
83 1.1 cgd assign("interactive", "");
84 1.1 cgd image = -1;
85 1.1 cgd /*
86 1.1 cgd * Now, determine how we are being used.
87 1.1 cgd * We successively pick off - flags.
88 1.1 cgd * If there is anything left, it is the base of the list
89 1.1 cgd * of users to mail to. Argp will be set to point to the
90 1.1 cgd * first of these users.
91 1.1 cgd */
92 1.1 cgd ef = NOSTR;
93 1.1 cgd to = NIL;
94 1.1 cgd cc = NIL;
95 1.1 cgd bcc = NIL;
96 1.1 cgd smopts = NIL;
97 1.1 cgd subject = NOSTR;
98 1.1 cgd while ((i = getopt(argc, argv, "INT:b:c:dfins:u:v")) != EOF) {
99 1.1 cgd switch (i) {
100 1.1 cgd case 'T':
101 1.1 cgd /*
102 1.1 cgd * Next argument is temp file to write which
103 1.1 cgd * articles have been read/deleted for netnews.
104 1.1 cgd */
105 1.1 cgd Tflag = optarg;
106 1.1 cgd if ((i = creat(Tflag, 0600)) < 0) {
107 1.1 cgd perror(Tflag);
108 1.1 cgd exit(1);
109 1.1 cgd }
110 1.1 cgd close(i);
111 1.1 cgd break;
112 1.1 cgd case 'u':
113 1.1 cgd /*
114 1.1 cgd * Next argument is person to pretend to be.
115 1.1 cgd */
116 1.1 cgd myname = optarg;
117 1.1 cgd break;
118 1.1 cgd case 'i':
119 1.1 cgd /*
120 1.1 cgd * User wants to ignore interrupts.
121 1.1 cgd * Set the variable "ignore"
122 1.1 cgd */
123 1.1 cgd assign("ignore", "");
124 1.1 cgd break;
125 1.1 cgd case 'd':
126 1.1 cgd debug++;
127 1.1 cgd break;
128 1.1 cgd case 's':
129 1.1 cgd /*
130 1.1 cgd * Give a subject field for sending from
131 1.1 cgd * non terminal
132 1.1 cgd */
133 1.1 cgd subject = optarg;
134 1.1 cgd break;
135 1.1 cgd case 'f':
136 1.1 cgd /*
137 1.1 cgd * User is specifying file to "edit" with Mail,
138 1.1 cgd * as opposed to reading system mailbox.
139 1.1 cgd * If no argument is given after -f, we read his
140 1.1 cgd * mbox file.
141 1.1 cgd *
142 1.1 cgd * getopt() can't handle optional arguments, so here
143 1.1 cgd * is an ugly hack to get around it.
144 1.1 cgd */
145 1.1 cgd if ((argv[optind]) && (argv[optind][0] != '-'))
146 1.1 cgd ef = argv[optind++];
147 1.1 cgd else
148 1.1 cgd ef = "&";
149 1.1 cgd break;
150 1.1 cgd case 'n':
151 1.1 cgd /*
152 1.1 cgd * User doesn't want to source /usr/lib/Mail.rc
153 1.1 cgd */
154 1.1 cgd nosrc++;
155 1.1 cgd break;
156 1.1 cgd case 'N':
157 1.1 cgd /*
158 1.1 cgd * Avoid initial header printing.
159 1.1 cgd */
160 1.1 cgd assign("noheader", "");
161 1.1 cgd break;
162 1.1 cgd case 'v':
163 1.1 cgd /*
164 1.1 cgd * Send mailer verbose flag
165 1.1 cgd */
166 1.1 cgd assign("verbose", "");
167 1.1 cgd break;
168 1.1 cgd case 'I':
169 1.1 cgd /*
170 1.1 cgd * We're interactive
171 1.1 cgd */
172 1.1 cgd assign("interactive", "");
173 1.1 cgd break;
174 1.1 cgd case 'c':
175 1.1 cgd /*
176 1.1 cgd * Get Carbon Copy Recipient list
177 1.1 cgd */
178 1.1 cgd cc = cat(cc, nalloc(optarg, GCC));
179 1.1 cgd break;
180 1.1 cgd case 'b':
181 1.1 cgd /*
182 1.1 cgd * Get Blind Carbon Copy Recipient list
183 1.1 cgd */
184 1.1 cgd bcc = cat(bcc, nalloc(optarg, GBCC));
185 1.1 cgd break;
186 1.1 cgd case '?':
187 1.1 cgd fputs("\
188 1.1 cgd Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
189 1.1 cgd [- sendmail-options ...]\n\
190 1.1 cgd mail [-iInNv] -f [name]\n\
191 1.1 cgd mail [-iInNv] [-u user]\n",
192 1.1 cgd stderr);
193 1.1 cgd exit(1);
194 1.1 cgd }
195 1.1 cgd }
196 1.1 cgd for (i = optind; (argv[i]) && (*argv[i] != '-'); i++)
197 1.1 cgd to = cat(to, nalloc(argv[i], GTO));
198 1.1 cgd for (; argv[i]; i++)
199 1.1 cgd smopts = cat(smopts, nalloc(argv[i], 0));
200 1.1 cgd /*
201 1.1 cgd * Check for inconsistent arguments.
202 1.1 cgd */
203 1.1 cgd if (to == NIL && (subject != NOSTR || cc != NIL || bcc != NIL)) {
204 1.1 cgd fputs("You must specify direct recipients with -s, -c, or -b.\n", stderr);
205 1.1 cgd exit(1);
206 1.1 cgd }
207 1.1 cgd if (ef != NOSTR && to != NIL) {
208 1.1 cgd fprintf(stderr, "Cannot give -f and people to send to.\n");
209 1.1 cgd exit(1);
210 1.1 cgd }
211 1.1 cgd tinit();
212 1.1 cgd setscreensize();
213 1.1 cgd input = stdin;
214 1.1 cgd rcvmode = !to;
215 1.1 cgd spreserve();
216 1.1 cgd if (!nosrc)
217 1.1 cgd load(_PATH_MASTER_RC);
218 1.1 cgd /*
219 1.1 cgd * Expand returns a savestr, but load only uses the file name
220 1.1 cgd * for fopen, so it's safe to do this.
221 1.1 cgd */
222 1.6 tls if ((rc = getenv("MAILRC")) == 0)
223 1.6 tls rc = "~/.mailrc";
224 1.6 tls load(expand(rc));
225 1.1 cgd if (!rcvmode) {
226 1.1 cgd mail(to, cc, bcc, smopts, subject);
227 1.1 cgd /*
228 1.1 cgd * why wait?
229 1.1 cgd */
230 1.1 cgd exit(senderr);
231 1.1 cgd }
232 1.1 cgd /*
233 1.1 cgd * Ok, we are reading mail.
234 1.1 cgd * Decide whether we are editing a mailbox or reading
235 1.1 cgd * the system mailbox, and open up the right stuff.
236 1.1 cgd */
237 1.1 cgd if (ef == NOSTR)
238 1.1 cgd ef = "%";
239 1.1 cgd if (setfile(ef) < 0)
240 1.1 cgd exit(1); /* error already reported */
241 1.1 cgd if (setjmp(hdrjmp) == 0) {
242 1.1 cgd extern char *version;
243 1.1 cgd
244 1.1 cgd if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
245 1.1 cgd signal(SIGINT, hdrstop);
246 1.1 cgd if (value("quiet") == NOSTR)
247 1.1 cgd printf("Mail version %s. Type ? for help.\n",
248 1.1 cgd version);
249 1.1 cgd announce();
250 1.1 cgd fflush(stdout);
251 1.1 cgd signal(SIGINT, prevint);
252 1.1 cgd }
253 1.1 cgd commands();
254 1.1 cgd signal(SIGHUP, SIG_IGN);
255 1.1 cgd signal(SIGINT, SIG_IGN);
256 1.1 cgd signal(SIGQUIT, SIG_IGN);
257 1.1 cgd quit();
258 1.1 cgd exit(0);
259 1.1 cgd }
260 1.1 cgd
261 1.1 cgd /*
262 1.1 cgd * Interrupt printing of the headers.
263 1.1 cgd */
264 1.1 cgd void
265 1.3 deraadt hdrstop(signo)
266 1.3 deraadt int signo;
267 1.1 cgd {
268 1.1 cgd
269 1.1 cgd fflush(stdout);
270 1.1 cgd fprintf(stderr, "\nInterrupt\n");
271 1.1 cgd longjmp(hdrjmp, 1);
272 1.1 cgd }
273 1.1 cgd
274 1.1 cgd /*
275 1.1 cgd * Compute what the screen size for printing headers should be.
276 1.1 cgd * We use the following algorithm for the height:
277 1.1 cgd * If baud rate < 1200, use 9
278 1.1 cgd * If baud rate = 1200, use 14
279 1.1 cgd * If baud rate > 1200, use 24 or ws_row
280 1.1 cgd * Width is either 80 or ws_col;
281 1.1 cgd */
282 1.3 deraadt void
283 1.1 cgd setscreensize()
284 1.1 cgd {
285 1.4 mycroft struct termios tbuf;
286 1.1 cgd struct winsize ws;
287 1.4 mycroft speed_t ospeed;
288 1.1 cgd
289 1.1 cgd if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
290 1.1 cgd ws.ws_col = ws.ws_row = 0;
291 1.4 mycroft if (tcgetattr(1, &tbuf) < 0)
292 1.4 mycroft ospeed = 9600;
293 1.4 mycroft else
294 1.4 mycroft ospeed = cfgetospeed(&tbuf);
295 1.4 mycroft if (ospeed < 1200)
296 1.1 cgd screenheight = 9;
297 1.4 mycroft else if (ospeed == 1200)
298 1.1 cgd screenheight = 14;
299 1.1 cgd else if (ws.ws_row != 0)
300 1.1 cgd screenheight = ws.ws_row;
301 1.1 cgd else
302 1.1 cgd screenheight = 24;
303 1.1 cgd if ((realscreenheight = ws.ws_row) == 0)
304 1.1 cgd realscreenheight = 24;
305 1.1 cgd if ((screenwidth = ws.ws_col) == 0)
306 1.1 cgd screenwidth = 80;
307 1.1 cgd }
308