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 $
40 * Mail -- a mail program
42 * Startup -- interface with user.
47 extern const char *version
;
50 main(int argc
, char **argv
)
53 struct name
*to
, *cc
, *bcc
, *smopts
;
54 char *subject
, *replyto
;
61 * Set up a reasonable environment.
62 * Figure out whether we are being run interactively,
63 * start the SIGCHLD catcher, and so forth.
65 signal(SIGCHLD
, sigchild
);
67 assign("interactive", "");
70 * Now, determine how we are being used.
71 * We successively pick off - flags.
72 * If there is anything left, it is the base of the list
73 * of users to mail to. Argp will be set to point to the
74 * first of these users.
82 while ((i
= getopt(argc
, argv
, "FEHINT:b:c:edfins:u:v")) != -1) {
86 * Next argument is temp file to write which
87 * articles have been read/deleted for netnews.
90 if ((i
= open(Tflag
, O_CREAT
| O_TRUNC
| O_WRONLY
,
97 * Next argument is person to pretend to be.
104 * User wants to ignore interrupts.
105 * Set the variable "ignore"
107 assign("ignore", "");
114 * User wants to check mail and exit.
116 assign("checkmode", "");
120 * User wants a header summary only.
122 assign("headersummary", "");
126 * User wants to record messages to files
127 * named after first recipient username.
129 assign("recordrecip", "");
133 * Give a subject field for sending from
140 * User is specifying file to "edit" with Mail,
141 * as opposed to reading system mailbox.
142 * If no argument is given after -f, we read his
145 * getopt() can't handle optional arguments, so here
146 * is an ugly hack to get around it.
148 if ((argv
[optind
] != NULL
) && (argv
[optind
][0] != '-'))
155 * User doesn't want to source /usr/lib/Mail.rc
161 * Avoid initial header printing.
163 assign("noheader", "");
167 * Send mailer verbose flag
169 assign("verbose", "");
175 assign("interactive", "");
179 * Get Carbon Copy Recipient list
181 cc
= cat(cc
, nalloc(optarg
, GCC
));
185 * Get Blind Carbon Copy Recipient list
187 bcc
= cat(bcc
, nalloc(optarg
, GBCC
));
191 * Don't send empty files.
193 assign("dontsendempty", "");
196 progname
= getprogname();
198 "Usage: %s [-EiInv] [-s subject] [-c cc-addr] [-b bcc-addr] [-F] to-addr ...\n"
199 " %*s [- sendmail-options ...]\n"
200 " %s [-EHiInNv] [-F] -f [name]\n"
201 " %s [-EHiInNv] [-F] [-u user]\n"
203 " %s -H\n", progname
, (int)strlen(progname
), "",
204 progname
, progname
, progname
, progname
);
208 for (i
= optind
; (argv
[i
] != NULL
) && (*argv
[i
] != '-'); i
++)
209 to
= cat(to
, nalloc(argv
[i
], GTO
));
210 for (; argv
[i
] != NULL
; i
++)
211 smopts
= cat(smopts
, nalloc(argv
[i
], 0));
213 * Check for inconsistent arguments.
215 if (to
== NULL
&& (subject
!= NULL
|| cc
!= NULL
|| bcc
!= NULL
))
216 errx(1, "You must specify direct recipients with -s, -c, or -b.");
217 if (ef
!= NULL
&& to
!= NULL
)
218 errx(1, "Cannot give -f and people to send to.");
227 if ((path_rc
= malloc(sizeof(_PATH_MASTER_RC
))) == NULL
)
228 err(1, "malloc(path_rc) failed");
230 strcpy(path_rc
, _PATH_MASTER_RC
);
231 while ((s
= strsep(&path_rc
, ":")) != NULL
)
236 * Expand returns a savestr, but load only uses the file name
237 * for fopen, so it's safe to do this.
239 if ((rc
= getenv("MAILRC")) == NULL
)
243 replyto
= value("REPLYTO");
245 mail(to
, cc
, bcc
, smopts
, subject
, replyto
);
252 if (value("checkmode") != NULL
) {
255 if (setfile(ef
) <= 0) {
256 /* Either an error has occured, or no mail. */
264 * Ok, we are reading mail.
265 * Decide whether we are editing a mailbox or reading
266 * the system mailbox, and open up the right stuff.
271 exit(1); /* error already reported */
272 if (setjmp(hdrjmp
) == 0) {
273 if ((prevint
= signal(SIGINT
, SIG_IGN
)) != SIG_IGN
)
274 signal(SIGINT
, hdrstop
);
275 if (value("quiet") == NULL
)
276 printf("Mail version %s. Type ? for help.\n",
280 signal(SIGINT
, prevint
);
283 /* If we were in header summary mode, it's time to exit. */
284 if (value("headersummary") != NULL
)
288 signal(SIGHUP
, SIG_IGN
);
289 signal(SIGINT
, SIG_IGN
);
290 signal(SIGQUIT
, SIG_IGN
);
296 * Interrupt printing of the headers.
303 fprintf(stderr
, "\nInterrupt\n");
308 * Compute what the screen size for printing headers should be.
309 * We use the following algorithm for the height:
310 * If baud rate < 1200, use 9
311 * If baud rate = 1200, use 14
312 * If baud rate > 1200, use 24 or ws_row
313 * Width is either 80 or ws_col;
322 if (ioctl(1, TIOCGWINSZ
, (char *)&ws
) < 0)
323 ws
.ws_col
= ws
.ws_row
= 0;
324 if (tcgetattr(1, &tbuf
) < 0)
327 speed
= cfgetospeed(&tbuf
);
330 else if (speed
== B1200
)
332 else if (ws
.ws_row
!= 0)
333 screenheight
= ws
.ws_row
;
336 if ((realscreenheight
= ws
.ws_row
) == 0)
337 realscreenheight
= 24;
338 if ((screenwidth
= ws
.ws_col
) == 0)