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
37 * Mail -- a mail program
39 * Startup -- interface with user.
56 char mailname[PATHSIZE];
57 char prevfile[PATHSIZE];
62 char lexstring[STRINGLEN];
64 int regretstack[REGDEP];
65 char *string_stack[REGDEP];
66 int numberstack[REGDEP];
68 struct message *message;
69 struct var *variables[HSHSIZE];
70 struct grouphead *groups[HSHSIZE];
71 struct ignoretab ignore[2];
73 struct ignoretab saveignore[2];
75 struct ignoretab ignoreall[2];
85 static jmp_buf hdrjmp;
87 extern const char *version;
90 main(int argc, char *argv[])
93 struct name *to, *cc, *bcc, *smopts;
94 char *subject, *replyto;
100 * Set up a reasonable environment.
101 * Figure out whether we are being run interactively,
102 * start the SIGCHLD catcher, and so forth.
104 (void)signal(SIGCHLD, sigchild);
106 assign("interactive", "");
109 * Now, determine how we are being used.
110 * We successively pick off - flags.
111 * If there is anything left, it is the base of the list
112 * of users to mail to. Argp will be set to point to the
113 * first of these users.
121 while ((i = getopt(argc, argv, "FEHINT:b:c:edfins:u:v")) != -1) {
125 * Next argument is temp file to write which
126 * articles have been read/deleted for netnews.
129 if ((i = open(Tflag, O_CREAT | O_TRUNC | O_WRONLY,
136 * Next argument is person to pretend to be.
143 * User wants to ignore interrupts.
144 * Set the variable "ignore"
146 assign("ignore", "");
153 * User wants to check mail and exit.
155 assign("checkmode", "");
159 * User wants a header summary only.
161 assign("headersummary", "");
165 * User wants to record messages to files
166 * named after first recipient username.
168 assign("recordrecip", "");
172 * Give a subject field for sending from
179 * User is specifying file to "edit" with Mail,
180 * as opposed to reading system mailbox.
181 * If no argument is given after -f, we read his
184 * getopt() can't handle optional arguments, so here
185 * is an ugly hack to get around it.
187 if ((argv[optind] != NULL) && (argv[optind][0] != '-'))
194 * User doesn't want to source /usr/lib/Mail.rc
200 * Avoid initial header printing.
202 assign("noheader", "");
206 * Send mailer verbose flag
208 assign("verbose", "");
214 assign("interactive", "");
218 * Get Carbon Copy Recipient list
220 cc = cat(cc, nalloc(optarg, GCC));
224 * Get Blind Carbon Copy Recipient list
226 bcc = cat(bcc, nalloc(optarg, GBCC));
230 * Don't send empty files.
232 assign("dontsendempty", "");
236 Usage: %s [-dEiInv] [-s subject] [-c cc-addr] [-b bcc-addr] [-F] to-addr ...\n\
237 %*s [-sendmail-option ...]\n\
238 %s [-dEHiInNv] [-F] -f [name]\n\
239 %s [-dEHiInNv] [-F] [-u user]\n\
240 %s [-d] -e [-f name]\n", __progname, (int)strlen(__progname), "",
241 __progname, __progname, __progname);
245 for (i = optind; (argv[i] != NULL) && (*argv[i] != '-'); i++)
246 to = cat(to, nalloc(argv[i], GTO));
247 for (; argv[i] != NULL; i++)
248 smopts = cat(smopts, nalloc(argv[i], 0));
250 * Check for inconsistent arguments.
252 if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
253 errx(1, "You must specify direct recipients with -s, -c, or -b.");
254 if (ef != NULL && to != NULL)
255 errx(1, "Cannot give -f and people to send to.");
264 if ((path_rc = malloc(sizeof(_PATH_MASTER_RC))) == NULL)
265 err(1, "malloc(path_rc) failed");
267 strcpy(path_rc, _PATH_MASTER_RC);
268 while ((s = strsep(&path_rc, ":")) != NULL)
273 * Expand returns a savestr, but load only uses the file name
274 * for fopen, so it's safe to do this.
276 if ((rc = getenv("MAILRC")) == NULL)
280 replyto = value("REPLYTO");
282 mail(to, cc, bcc, smopts, subject, replyto);
289 if(value("checkmode") != NULL) {
292 if (setfile(ef) <= 0)
293 /* Either an error has occurred, or no mail */
301 * Ok, we are reading mail.
302 * Decide whether we are editing a mailbox or reading
303 * the system mailbox, and open up the right stuff.
308 exit(1); /* error already reported */
309 if (setjmp(hdrjmp) == 0) {
310 if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
311 (void)signal(SIGINT, hdrstop);
312 if (value("quiet") == NULL)
313 printf("Mail version %s. Type ? for help.\n",
316 (void)fflush(stdout);
317 (void)signal(SIGINT, prevint);
320 /* If we were in header summary mode, it's time to exit. */
321 if (value("headersummary") != NULL)
325 (void)signal(SIGHUP, SIG_IGN);
326 (void)signal(SIGINT, SIG_IGN);
327 (void)signal(SIGQUIT, SIG_IGN);
333 * Interrupt printing of the headers.
337 hdrstop(int signo __unused)
340 (void)fflush(stdout);
341 fprintf(stderr, "\nInterrupt\n");
346 * Compute what the screen size for printing headers should be.
347 * We use the following algorithm for the height:
348 * If baud rate < 1200, use 9
349 * If baud rate = 1200, use 14
350 * If baud rate > 1200, use 24 or ws_row
351 * Width is either 80 or ws_col;
360 if (ioctl(1, TIOCGWINSZ, (char *)&ws) < 0)
361 ws.ws_col = ws.ws_row = 0;
362 if (tcgetattr(1, &tbuf) < 0)
365 speed = cfgetospeed(&tbuf);
368 else if (speed == B1200)
370 else if (ws.ws_row != 0)
371 screenheight = ws.ws_row;
374 if ((realscreenheight = ws.ws_row) == 0)
375 realscreenheight = 24;
376 if ((screenwidth = ws.ws_col) == 0)