main.c revision 1.9 1 1.9 wsanchez /* $NetBSD: main.c,v 1.9 1998/10/08 17:36:56 wsanchez 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.8 lukem #include <sys/cdefs.h>
37 1.1 cgd #ifndef lint
38 1.8 lukem __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\
39 1.8 lukem 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.9 wsanchez __RCSID("$NetBSD: main.c,v 1.9 1998/10/08 17:36:56 wsanchez Exp $");
47 1.5 christos #endif
48 1.1 cgd #endif /* not lint */
49 1.1 cgd
50 1.9 wsanchez #define EXTERN
51 1.1 cgd #include "rcv.h"
52 1.9 wsanchez #undef EXTERN
53 1.9 wsanchez
54 1.3 deraadt #include "extern.h"
55 1.1 cgd
56 1.8 lukem int main __P((int, char **));
57 1.8 lukem
58 1.1 cgd /*
59 1.1 cgd * Mail -- a mail program
60 1.1 cgd *
61 1.1 cgd * Startup -- interface with user.
62 1.1 cgd */
63 1.1 cgd
64 1.1 cgd jmp_buf hdrjmp;
65 1.1 cgd
66 1.3 deraadt int
67 1.1 cgd main(argc, argv)
68 1.3 deraadt int argc;
69 1.3 deraadt char *argv[];
70 1.1 cgd {
71 1.8 lukem int i;
72 1.1 cgd struct name *to, *cc, *bcc, *smopts;
73 1.1 cgd char *subject;
74 1.1 cgd char *ef;
75 1.1 cgd char nosrc = 0;
76 1.1 cgd sig_t prevint;
77 1.6 tls char *rc;
78 1.1 cgd
79 1.1 cgd /*
80 1.1 cgd * Set up a reasonable environment.
81 1.1 cgd * Figure out whether we are being run interactively,
82 1.1 cgd * start the SIGCHLD catcher, and so forth.
83 1.1 cgd */
84 1.1 cgd (void) signal(SIGCHLD, sigchild);
85 1.1 cgd if (isatty(0))
86 1.1 cgd assign("interactive", "");
87 1.1 cgd image = -1;
88 1.1 cgd /*
89 1.1 cgd * Now, determine how we are being used.
90 1.1 cgd * We successively pick off - flags.
91 1.1 cgd * If there is anything left, it is the base of the list
92 1.1 cgd * of users to mail to. Argp will be set to point to the
93 1.1 cgd * first of these users.
94 1.1 cgd */
95 1.1 cgd ef = NOSTR;
96 1.1 cgd to = NIL;
97 1.1 cgd cc = NIL;
98 1.1 cgd bcc = NIL;
99 1.1 cgd smopts = NIL;
100 1.1 cgd subject = NOSTR;
101 1.7 mikel while ((i = getopt(argc, argv, "INT:b:c:dfins:u:v")) != -1) {
102 1.1 cgd switch (i) {
103 1.1 cgd case 'T':
104 1.1 cgd /*
105 1.1 cgd * Next argument is temp file to write which
106 1.1 cgd * articles have been read/deleted for netnews.
107 1.1 cgd */
108 1.1 cgd Tflag = optarg;
109 1.1 cgd if ((i = creat(Tflag, 0600)) < 0) {
110 1.1 cgd perror(Tflag);
111 1.1 cgd exit(1);
112 1.1 cgd }
113 1.1 cgd close(i);
114 1.1 cgd break;
115 1.1 cgd case 'u':
116 1.1 cgd /*
117 1.1 cgd * Next argument is person to pretend to be.
118 1.1 cgd */
119 1.1 cgd myname = optarg;
120 1.1 cgd break;
121 1.1 cgd case 'i':
122 1.1 cgd /*
123 1.1 cgd * User wants to ignore interrupts.
124 1.1 cgd * Set the variable "ignore"
125 1.1 cgd */
126 1.1 cgd assign("ignore", "");
127 1.1 cgd break;
128 1.1 cgd case 'd':
129 1.1 cgd debug++;
130 1.1 cgd break;
131 1.1 cgd case 's':
132 1.1 cgd /*
133 1.1 cgd * Give a subject field for sending from
134 1.1 cgd * non terminal
135 1.1 cgd */
136 1.1 cgd subject = optarg;
137 1.1 cgd break;
138 1.1 cgd case 'f':
139 1.1 cgd /*
140 1.1 cgd * User is specifying file to "edit" with Mail,
141 1.1 cgd * as opposed to reading system mailbox.
142 1.1 cgd * If no argument is given after -f, we read his
143 1.1 cgd * mbox file.
144 1.1 cgd *
145 1.1 cgd * getopt() can't handle optional arguments, so here
146 1.1 cgd * is an ugly hack to get around it.
147 1.1 cgd */
148 1.1 cgd if ((argv[optind]) && (argv[optind][0] != '-'))
149 1.1 cgd ef = argv[optind++];
150 1.1 cgd else
151 1.1 cgd ef = "&";
152 1.1 cgd break;
153 1.1 cgd case 'n':
154 1.1 cgd /*
155 1.1 cgd * User doesn't want to source /usr/lib/Mail.rc
156 1.1 cgd */
157 1.1 cgd nosrc++;
158 1.1 cgd break;
159 1.1 cgd case 'N':
160 1.1 cgd /*
161 1.1 cgd * Avoid initial header printing.
162 1.1 cgd */
163 1.1 cgd assign("noheader", "");
164 1.1 cgd break;
165 1.1 cgd case 'v':
166 1.1 cgd /*
167 1.1 cgd * Send mailer verbose flag
168 1.1 cgd */
169 1.1 cgd assign("verbose", "");
170 1.1 cgd break;
171 1.1 cgd case 'I':
172 1.1 cgd /*
173 1.1 cgd * We're interactive
174 1.1 cgd */
175 1.1 cgd assign("interactive", "");
176 1.1 cgd break;
177 1.1 cgd case 'c':
178 1.1 cgd /*
179 1.1 cgd * Get Carbon Copy Recipient list
180 1.1 cgd */
181 1.1 cgd cc = cat(cc, nalloc(optarg, GCC));
182 1.1 cgd break;
183 1.1 cgd case 'b':
184 1.1 cgd /*
185 1.1 cgd * Get Blind Carbon Copy Recipient list
186 1.1 cgd */
187 1.1 cgd bcc = cat(bcc, nalloc(optarg, GBCC));
188 1.1 cgd break;
189 1.1 cgd case '?':
190 1.1 cgd fputs("\
191 1.1 cgd Usage: mail [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
192 1.1 cgd [- sendmail-options ...]\n\
193 1.1 cgd mail [-iInNv] -f [name]\n\
194 1.1 cgd mail [-iInNv] [-u user]\n",
195 1.1 cgd stderr);
196 1.1 cgd exit(1);
197 1.1 cgd }
198 1.1 cgd }
199 1.1 cgd for (i = optind; (argv[i]) && (*argv[i] != '-'); i++)
200 1.1 cgd to = cat(to, nalloc(argv[i], GTO));
201 1.1 cgd for (; argv[i]; i++)
202 1.1 cgd smopts = cat(smopts, nalloc(argv[i], 0));
203 1.1 cgd /*
204 1.1 cgd * Check for inconsistent arguments.
205 1.1 cgd */
206 1.1 cgd if (to == NIL && (subject != NOSTR || cc != NIL || bcc != NIL)) {
207 1.1 cgd fputs("You must specify direct recipients with -s, -c, or -b.\n", stderr);
208 1.1 cgd exit(1);
209 1.1 cgd }
210 1.1 cgd if (ef != NOSTR && to != NIL) {
211 1.1 cgd fprintf(stderr, "Cannot give -f and people to send to.\n");
212 1.1 cgd exit(1);
213 1.1 cgd }
214 1.1 cgd tinit();
215 1.1 cgd setscreensize();
216 1.1 cgd input = stdin;
217 1.1 cgd rcvmode = !to;
218 1.1 cgd spreserve();
219 1.1 cgd if (!nosrc)
220 1.1 cgd load(_PATH_MASTER_RC);
221 1.1 cgd /*
222 1.1 cgd * Expand returns a savestr, but load only uses the file name
223 1.1 cgd * for fopen, so it's safe to do this.
224 1.1 cgd */
225 1.6 tls if ((rc = getenv("MAILRC")) == 0)
226 1.6 tls rc = "~/.mailrc";
227 1.6 tls load(expand(rc));
228 1.1 cgd if (!rcvmode) {
229 1.1 cgd mail(to, cc, bcc, smopts, subject);
230 1.1 cgd /*
231 1.1 cgd * why wait?
232 1.1 cgd */
233 1.1 cgd exit(senderr);
234 1.1 cgd }
235 1.1 cgd /*
236 1.1 cgd * Ok, we are reading mail.
237 1.1 cgd * Decide whether we are editing a mailbox or reading
238 1.1 cgd * the system mailbox, and open up the right stuff.
239 1.1 cgd */
240 1.1 cgd if (ef == NOSTR)
241 1.1 cgd ef = "%";
242 1.1 cgd if (setfile(ef) < 0)
243 1.1 cgd exit(1); /* error already reported */
244 1.1 cgd if (setjmp(hdrjmp) == 0) {
245 1.1 cgd extern char *version;
246 1.1 cgd
247 1.1 cgd if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
248 1.1 cgd signal(SIGINT, hdrstop);
249 1.1 cgd if (value("quiet") == NOSTR)
250 1.1 cgd printf("Mail version %s. Type ? for help.\n",
251 1.1 cgd version);
252 1.1 cgd announce();
253 1.1 cgd fflush(stdout);
254 1.1 cgd signal(SIGINT, prevint);
255 1.1 cgd }
256 1.1 cgd commands();
257 1.1 cgd signal(SIGHUP, SIG_IGN);
258 1.1 cgd signal(SIGINT, SIG_IGN);
259 1.1 cgd signal(SIGQUIT, SIG_IGN);
260 1.1 cgd quit();
261 1.1 cgd exit(0);
262 1.1 cgd }
263 1.1 cgd
264 1.1 cgd /*
265 1.1 cgd * Interrupt printing of the headers.
266 1.1 cgd */
267 1.1 cgd void
268 1.3 deraadt hdrstop(signo)
269 1.3 deraadt int signo;
270 1.1 cgd {
271 1.1 cgd
272 1.1 cgd fflush(stdout);
273 1.1 cgd fprintf(stderr, "\nInterrupt\n");
274 1.1 cgd longjmp(hdrjmp, 1);
275 1.1 cgd }
276 1.1 cgd
277 1.1 cgd /*
278 1.1 cgd * Compute what the screen size for printing headers should be.
279 1.1 cgd * We use the following algorithm for the height:
280 1.1 cgd * If baud rate < 1200, use 9
281 1.1 cgd * If baud rate = 1200, use 14
282 1.1 cgd * If baud rate > 1200, use 24 or ws_row
283 1.1 cgd * Width is either 80 or ws_col;
284 1.1 cgd */
285 1.3 deraadt void
286 1.1 cgd setscreensize()
287 1.1 cgd {
288 1.4 mycroft struct termios tbuf;
289 1.1 cgd struct winsize ws;
290 1.4 mycroft speed_t ospeed;
291 1.1 cgd
292 1.1 cgd if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
293 1.1 cgd ws.ws_col = ws.ws_row = 0;
294 1.4 mycroft if (tcgetattr(1, &tbuf) < 0)
295 1.4 mycroft ospeed = 9600;
296 1.4 mycroft else
297 1.4 mycroft ospeed = cfgetospeed(&tbuf);
298 1.4 mycroft if (ospeed < 1200)
299 1.1 cgd screenheight = 9;
300 1.4 mycroft else if (ospeed == 1200)
301 1.1 cgd screenheight = 14;
302 1.1 cgd else if (ws.ws_row != 0)
303 1.1 cgd screenheight = ws.ws_row;
304 1.1 cgd else
305 1.1 cgd screenheight = 24;
306 1.1 cgd if ((realscreenheight = ws.ws_row) == 0)
307 1.1 cgd realscreenheight = 24;
308 1.1 cgd if ((screenwidth = ws.ws_col) == 0)
309 1.1 cgd screenwidth = 80;
310 1.1 cgd }
311