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
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
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 $
39 * Mail -- a mail program
41 * Startup -- interface with user.
46 extern const char *version
;
49 main(int argc
, char **argv
)
52 struct name
*to
, *cc
, *bcc
, *smopts
;
53 char *subject
, *replyto
;
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
);
66 assign("interactive", "");
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.
81 while ((i
= getopt(argc
, argv
, "FEHINT:b:c:edfins:u:v")) != -1) {
85 * Next argument is temp file to write which
86 * articles have been read/deleted for netnews.
89 if ((i
= open(Tflag
, O_CREAT
| O_TRUNC
| O_WRONLY
,
96 * Next argument is person to pretend to be.
103 * User wants to ignore interrupts.
104 * Set the variable "ignore"
106 assign("ignore", "");
113 * User wants to check mail and exit.
115 assign("checkmode", "");
119 * User wants a header summary only.
121 assign("headersummary", "");
125 * User wants to record messages to files
126 * named after first recipient username.
128 assign("recordrecip", "");
132 * Give a subject field for sending from
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
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] != '-'))
154 * User doesn't want to source /usr/lib/Mail.rc
160 * Avoid initial header printing.
162 assign("noheader", "");
166 * Send mailer verbose flag
168 assign("verbose", "");
174 assign("interactive", "");
178 * Get Carbon Copy Recipient list
180 cc
= cat(cc
, nalloc(optarg
, GCC
));
184 * Get Blind Carbon Copy Recipient list
186 bcc
= cat(bcc
, nalloc(optarg
, GBCC
));
190 * Don't send empty files.
192 assign("dontsendempty", "");
195 progname
= getprogname();
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"
202 " %s -H\n", progname
, (int)strlen(progname
), "",
203 progname
, progname
, progname
, progname
);
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.");
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
)
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
)
242 replyto
= value("REPLYTO");
244 mail(to
, cc
, bcc
, smopts
, subject
, replyto
);
251 if (value("checkmode") != NULL
) {
254 if (setfile(ef
) <= 0) {
255 /* Either an error has occured, or no mail. */
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.
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",
279 signal(SIGINT
, prevint
);
282 /* If we were in header summary mode, it's time to exit. */
283 if (value("headersummary") != NULL
)
287 signal(SIGHUP
, SIG_IGN
);
288 signal(SIGINT
, SIG_IGN
);
289 signal(SIGQUIT
, SIG_IGN
);
295 * Interrupt printing of the headers.
302 fprintf(stderr
, "\nInterrupt\n");
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;
321 if (ioctl(1, TIOCGWINSZ
, (char *)&ws
) < 0)
322 ws
.ws_col
= ws
.ws_row
= 0;
323 if (tcgetattr(1, &tbuf
) < 0)
326 speed
= cfgetospeed(&tbuf
);
329 else if (speed
== B1200
)
331 else if (ws
.ws_row
!= 0)
332 screenheight
= ws
.ws_row
;
335 if ((realscreenheight
= ws
.ws_row
) == 0)
336 realscreenheight
= 24;
337 if ((screenwidth
= ws
.ws_col
) == 0)