2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 static const char copyright[] =
34 "@(#) Copyright (c) 1980, 1993\n\
35 The Regents of the University of California. All rights reserved.\n";
40 static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/20/95";
43 #include <sys/cdefs.h>
44 __FBSDID("$FreeBSD$");
51 * Mail -- a mail program
53 * Startup -- interface with user.
70 char mailname[PATHSIZE];
71 char prevfile[PATHSIZE];
76 char lexstring[STRINGLEN];
78 int regretstack[REGDEP];
79 char *string_stack[REGDEP];
80 int numberstack[REGDEP];
82 struct message *message;
83 struct var *variables[HSHSIZE];
84 struct grouphead *groups[HSHSIZE];
85 struct ignoretab ignore[2];
87 struct ignoretab saveignore[2];
89 struct ignoretab ignoreall[2];
99 static jmp_buf hdrjmp;
101 extern const char *version;
104 main(int argc, char *argv[])
107 struct name *to, *cc, *bcc, *smopts;
108 char *subject, *replyto;
114 * Set up a reasonable environment.
115 * Figure out whether we are being run interactively,
116 * start the SIGCHLD catcher, and so forth.
118 (void)signal(SIGCHLD, sigchild);
120 assign("interactive", "");
123 * Now, determine how we are being used.
124 * We successively pick off - flags.
125 * If there is anything left, it is the base of the list
126 * of users to mail to. Argp will be set to point to the
127 * first of these users.
135 while ((i = getopt(argc, argv, "FEHINT:b:c:edfins:u:v")) != -1) {
139 * Next argument is temp file to write which
140 * articles have been read/deleted for netnews.
143 if ((i = open(Tflag, O_CREAT | O_TRUNC | O_WRONLY,
150 * Next argument is person to pretend to be.
157 * User wants to ignore interrupts.
158 * Set the variable "ignore"
160 assign("ignore", "");
167 * User wants to check mail and exit.
169 assign("checkmode", "");
173 * User wants a header summary only.
175 assign("headersummary", "");
179 * User wants to record messages to files
180 * named after first recipient username.
182 assign("recordrecip", "");
186 * Give a subject field for sending from
193 * User is specifying file to "edit" with Mail,
194 * as opposed to reading system mailbox.
195 * If no argument is given after -f, we read his
198 * getopt() can't handle optional arguments, so here
199 * is an ugly hack to get around it.
201 if ((argv[optind] != NULL) && (argv[optind][0] != '-'))
208 * User doesn't want to source /usr/lib/Mail.rc
214 * Avoid initial header printing.
216 assign("noheader", "");
220 * Send mailer verbose flag
222 assign("verbose", "");
228 assign("interactive", "");
232 * Get Carbon Copy Recipient list
234 cc = cat(cc, nalloc(optarg, GCC));
238 * Get Blind Carbon Copy Recipient list
240 bcc = cat(bcc, nalloc(optarg, GBCC));
244 * Don't send empty files.
246 assign("dontsendempty", "");
250 Usage: %s [-dEiInv] [-s subject] [-c cc-addr] [-b bcc-addr] [-F] to-addr ...\n\
251 %*s [-sendmail-option ...]\n\
252 %s [-dEHiInNv] [-F] -f [name]\n\
253 %s [-dEHiInNv] [-F] [-u user]\n\
254 %s [-d] -e [-f name]\n", __progname, (int)strlen(__progname), "",
255 __progname, __progname, __progname);
259 for (i = optind; (argv[i] != NULL) && (*argv[i] != '-'); i++)
260 to = cat(to, nalloc(argv[i], GTO));
261 for (; argv[i] != NULL; i++)
262 smopts = cat(smopts, nalloc(argv[i], 0));
264 * Check for inconsistent arguments.
266 if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
267 errx(1, "You must specify direct recipients with -s, -c, or -b.");
268 if (ef != NULL && to != NULL)
269 errx(1, "Cannot give -f and people to send to.");
278 if ((path_rc = malloc(sizeof(_PATH_MASTER_RC))) == NULL)
279 err(1, "malloc(path_rc) failed");
281 strcpy(path_rc, _PATH_MASTER_RC);
282 while ((s = strsep(&path_rc, ":")) != NULL)
287 * Expand returns a savestr, but load only uses the file name
288 * for fopen, so it's safe to do this.
290 if ((rc = getenv("MAILRC")) == NULL)
294 replyto = value("REPLYTO");
296 mail(to, cc, bcc, smopts, subject, replyto);
303 if(value("checkmode") != NULL) {
306 if (setfile(ef) <= 0)
307 /* Either an error has occurred, or no mail */
315 * Ok, we are reading mail.
316 * Decide whether we are editing a mailbox or reading
317 * the system mailbox, and open up the right stuff.
322 exit(1); /* error already reported */
323 if (setjmp(hdrjmp) == 0) {
324 if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
325 (void)signal(SIGINT, hdrstop);
326 if (value("quiet") == NULL)
327 printf("Mail version %s. Type ? for help.\n",
330 (void)fflush(stdout);
331 (void)signal(SIGINT, prevint);
334 /* If we were in header summary mode, it's time to exit. */
335 if (value("headersummary") != NULL)
339 (void)signal(SIGHUP, SIG_IGN);
340 (void)signal(SIGINT, SIG_IGN);
341 (void)signal(SIGQUIT, SIG_IGN);
347 * Interrupt printing of the headers.
351 hdrstop(int signo __unused)
354 (void)fflush(stdout);
355 fprintf(stderr, "\nInterrupt\n");
360 * Compute what the screen size for printing headers should be.
361 * We use the following algorithm for the height:
362 * If baud rate < 1200, use 9
363 * If baud rate = 1200, use 14
364 * If baud rate > 1200, use 24 or ws_row
365 * Width is either 80 or ws_col;
374 if (ioctl(1, TIOCGWINSZ, (char *)&ws) < 0)
375 ws.ws_col = ws.ws_row = 0;
376 if (tcgetattr(1, &tbuf) < 0)
379 speed = cfgetospeed(&tbuf);
382 else if (speed == B1200)
384 else if (ws.ws_row != 0)
385 screenheight = ws.ws_row;
388 if ((realscreenheight = ws.ws_row) == 0)
389 realscreenheight = 24;
390 if ((screenwidth = ws.ws_col) == 0)