kernel/netgraph7: Fix some build issues.
[dragonfly.git] / usr.bin / mail / main.c
blob8747ca9a49c253882c159b5fdc9e7a17c1845ef3
1 /*
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 * SUCH DAMAGE.
29 * @(#) Copyright (c) 1980, 1993 The Regents of the University of California. All rights reserved.
30 * @(#)main.c 8.2 (Berkeley) 4/20/95
31 * $FreeBSD: src/usr.bin/mail/main.c,v 1.6.2.5 2003/01/06 05:46:03 mikeh Exp $
34 #include "rcv.h"
35 #include <fcntl.h>
36 #include "extern.h"
39 * Mail -- a mail program
41 * Startup -- interface with user.
44 jmp_buf hdrjmp;
46 extern const char *version;
48 int
49 main(int argc, char **argv)
51 int i;
52 struct name *to, *cc, *bcc, *smopts;
53 char *subject, *replyto;
54 char *ef, *rc;
55 char nosrc = 0;
56 const char *progname;
57 sig_t prevint;
60 * Set up a reasonable environment.
61 * Figure out whether we are being run interactively,
62 * start the SIGCHLD catcher, and so forth.
64 signal(SIGCHLD, sigchild);
65 if (isatty(0))
66 assign("interactive", "");
67 image = -1;
69 * Now, determine how we are being used.
70 * We successively pick off - flags.
71 * If there is anything left, it is the base of the list
72 * of users to mail to. Argp will be set to point to the
73 * first of these users.
75 ef = NULL;
76 to = NULL;
77 cc = NULL;
78 bcc = NULL;
79 smopts = NULL;
80 subject = NULL;
81 while ((i = getopt(argc, argv, "FEHINT:b:c:edfins:u:v")) != -1) {
82 switch (i) {
83 case 'T':
85 * Next argument is temp file to write which
86 * articles have been read/deleted for netnews.
88 Tflag = optarg;
89 if ((i = open(Tflag, O_CREAT | O_TRUNC | O_WRONLY,
90 0600)) < 0)
91 err(1, "%s", Tflag);
92 close(i);
93 break;
94 case 'u':
96 * Next argument is person to pretend to be.
98 myname = optarg;
99 unsetenv("MAIL");
100 break;
101 case 'i':
103 * User wants to ignore interrupts.
104 * Set the variable "ignore"
106 assign("ignore", "");
107 break;
108 case 'd':
109 debug++;
110 break;
111 case 'e':
113 * User wants to check mail and exit.
115 assign("checkmode", "");
116 break;
117 case 'H':
119 * User wants a header summary only.
121 assign("headersummary", "");
122 break;
123 case 'F':
125 * User wants to record messages to files
126 * named after first recipient username.
128 assign("recordrecip", "");
129 break;
130 case 's':
132 * Give a subject field for sending from
133 * non terminal
135 subject = optarg;
136 break;
137 case 'f':
139 * User is specifying file to "edit" with Mail,
140 * as opposed to reading system mailbox.
141 * If no argument is given after -f, we read his
142 * mbox file.
144 * getopt() can't handle optional arguments, so here
145 * is an ugly hack to get around it.
147 if ((argv[optind] != NULL) && (argv[optind][0] != '-'))
148 ef = argv[optind++];
149 else
150 ef = "&";
151 break;
152 case 'n':
154 * User doesn't want to source /usr/lib/Mail.rc
156 nosrc++;
157 break;
158 case 'N':
160 * Avoid initial header printing.
162 assign("noheader", "");
163 break;
164 case 'v':
166 * Send mailer verbose flag
168 assign("verbose", "");
169 break;
170 case 'I':
172 * We're interactive
174 assign("interactive", "");
175 break;
176 case 'c':
178 * Get Carbon Copy Recipient list
180 cc = cat(cc, nalloc(optarg, GCC));
181 break;
182 case 'b':
184 * Get Blind Carbon Copy Recipient list
186 bcc = cat(bcc, nalloc(optarg, GBCC));
187 break;
188 case 'E':
190 * Don't send empty files.
192 assign("dontsendempty", "");
193 break;
194 case '?':
195 progname = getprogname();
196 fprintf(stderr,
197 "Usage: %s [-EiInv] [-s subject] [-c cc-addr] [-b bcc-addr] [-F] to-addr ...\n"
198 " %*s [- sendmail-options ...]\n"
199 " %s [-EHiInNv] [-F] -f [name]\n"
200 " %s [-EHiInNv] [-F] [-u user]\n"
201 " %s -e [-f name]\n"
202 " %s -H\n", progname, (int)strlen(progname), "",
203 progname, progname, progname, progname);
204 exit(1);
207 for (i = optind; (argv[i] != NULL) && (*argv[i] != '-'); i++)
208 to = cat(to, nalloc(argv[i], GTO));
209 for (; argv[i] != NULL; i++)
210 smopts = cat(smopts, nalloc(argv[i], 0));
212 * Check for inconsistent arguments.
214 if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
215 errx(1, "You must specify direct recipients with -s, -c, or -b.");
216 if (ef != NULL && to != NULL)
217 errx(1, "Cannot give -f and people to send to.");
218 tinit();
219 setscreensize();
220 input = stdin;
221 rcvmode = !to;
222 spreserve();
223 if (!nosrc) {
224 char *s, *path_rc;
226 if ((path_rc = malloc(sizeof(_PATH_MASTER_RC))) == NULL)
227 err(1, "malloc(path_rc) failed");
229 strcpy(path_rc, _PATH_MASTER_RC);
230 while ((s = strsep(&path_rc, ":")) != NULL)
231 if (*s != '\0')
232 load(s);
235 * Expand returns a savestr, but load only uses the file name
236 * for fopen, so it's safe to do this.
238 if ((rc = getenv("MAILRC")) == NULL)
239 rc = "~/.mailrc";
240 load(expand(rc));
242 replyto = value("REPLYTO");
243 if (!rcvmode) {
244 mail(to, cc, bcc, smopts, subject, replyto);
246 * why wait?
248 exit(senderr);
251 if (value("checkmode") != NULL) {
252 if (ef == NULL)
253 ef = "%s";
254 if (setfile(ef) <= 0) {
255 /* Either an error has occured, or no mail. */
256 exit(1);
257 } else {
258 exit(0);
260 /* NOTREACHED */
263 * Ok, we are reading mail.
264 * Decide whether we are editing a mailbox or reading
265 * the system mailbox, and open up the right stuff.
267 if (ef == NULL)
268 ef = "%";
269 if (setfile(ef) < 0)
270 exit(1); /* error already reported */
271 if (setjmp(hdrjmp) == 0) {
272 if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
273 signal(SIGINT, hdrstop);
274 if (value("quiet") == NULL)
275 printf("Mail version %s. Type ? for help.\n",
276 version);
277 announce();
278 fflush(stdout);
279 signal(SIGINT, prevint);
282 /* If we were in header summary mode, it's time to exit. */
283 if (value("headersummary") != NULL)
284 exit(0);
286 commands();
287 signal(SIGHUP, SIG_IGN);
288 signal(SIGINT, SIG_IGN);
289 signal(SIGQUIT, SIG_IGN);
290 quit();
291 exit(0);
295 * Interrupt printing of the headers.
297 /*ARGSUSED*/
298 void
299 hdrstop(int signo)
301 fflush(stdout);
302 fprintf(stderr, "\nInterrupt\n");
303 longjmp(hdrjmp, 1);
307 * Compute what the screen size for printing headers should be.
308 * We use the following algorithm for the height:
309 * If baud rate < 1200, use 9
310 * If baud rate = 1200, use 14
311 * If baud rate > 1200, use 24 or ws_row
312 * Width is either 80 or ws_col;
314 void
315 setscreensize(void)
317 struct termios tbuf;
318 struct winsize ws;
319 speed_t speed;
321 if (ioctl(1, TIOCGWINSZ, (char *)&ws) < 0)
322 ws.ws_col = ws.ws_row = 0;
323 if (tcgetattr(1, &tbuf) < 0)
324 speed = B9600;
325 else
326 speed = cfgetospeed(&tbuf);
327 if (speed < B1200)
328 screenheight = 9;
329 else if (speed == B1200)
330 screenheight = 14;
331 else if (ws.ws_row != 0)
332 screenheight = ws.ws_row;
333 else
334 screenheight = 24;
335 if ((realscreenheight = ws.ws_row) == 0)
336 realscreenheight = 24;
337 if ((screenwidth = ws.ws_col) == 0)
338 screenwidth = 80;