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