Annotation of src/usr.bin/mail/main.c, Revision 1.28
1.28 ! millert 1: /* $OpenBSD: main.c,v 1.27 2015/01/16 06:40:09 deraadt Exp $ */
1.5 millert 2: /* $NetBSD: main.c,v 1.7 1997/05/13 06:15:57 mikel Exp $ */
1.2 deraadt 3:
1.1 deraadt 4: /*
5: * Copyright (c) 1980, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.18 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #include "rcv.h"
34: #include <fcntl.h>
1.2 deraadt 35: #include <sys/ioctl.h>
1.1 deraadt 36: #include "extern.h"
37:
1.16 millert 38: __dead void usage(void);
39: int main(int, char **);
1.10 millert 40:
1.1 deraadt 41: /*
42: * Mail -- a mail program
43: *
44: * Startup -- interface with user.
45: */
46:
47: int
1.16 millert 48: main(int argc, char **argv)
1.1 deraadt 49: {
1.10 millert 50: int i;
1.1 deraadt 51: struct name *to, *cc, *bcc, *smopts;
1.28 ! millert 52: char *fromaddr;
1.1 deraadt 53: char *subject;
54: char *ef;
55: char nosrc = 0;
1.5 millert 56: char *rc;
1.17 millert 57: extern const char version[];
1.1 deraadt 58:
59: /*
60: * Set up a reasonable environment.
61: * Figure out whether we are being run interactively,
62: * start the SIGCHLD catcher, and so forth.
63: */
1.6 millert 64: (void)signal(SIGCHLD, sigchild);
1.15 millert 65: (void)signal(SIGPIPE, SIG_IGN);
1.1 deraadt 66: if (isatty(0))
67: assign("interactive", "");
68: image = -1;
69: /*
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.
75: */
1.7 millert 76: ef = NULL;
1.16 millert 77: to = NULL;
78: cc = NULL;
79: bcc = NULL;
80: smopts = NULL;
1.28 ! millert 81: fromaddr = NULL;
1.7 millert 82: subject = NULL;
1.28 ! millert 83: while ((i = getopt(argc, argv, "EIN:b:c:dfinr:s:u:v")) != -1) {
1.1 deraadt 84: switch (i) {
85: case 'u':
86: /*
87: * Next argument is person to pretend to be.
88: */
1.27 deraadt 89: if (strlen(optarg) >= LOGIN_NAME_MAX)
1.14 millert 90: errx(1, "username `%s' too long", optarg);
1.3 deraadt 91: unsetenv("MAIL");
1.1 deraadt 92: myname = optarg;
1.11 millert 93: uflag = 1;
1.1 deraadt 94: break;
95: case 'i':
96: /*
97: * User wants to ignore interrupts.
98: * Set the variable "ignore"
99: */
100: assign("ignore", "");
101: break;
102: case 'd':
103: debug++;
104: break;
1.28 ! millert 105: case 'r':
! 106: /*
! 107: * Set From: address
! 108: */
! 109: fromaddr = optarg;
! 110: break;
1.1 deraadt 111: case 's':
112: /*
113: * Give a subject field for sending from
114: * non terminal
115: */
116: subject = optarg;
117: break;
118: case 'f':
119: /*
120: * User is specifying file to "edit" with Mail,
121: * as opposed to reading system mailbox.
1.25 millert 122: * We read his mbox file unless another file
123: * is specified after the arguments.
124: */
125: ef = "&";
1.1 deraadt 126: break;
127: case 'n':
128: /*
129: * User doesn't want to source /usr/lib/Mail.rc
130: */
131: nosrc++;
132: break;
133: case 'N':
134: /*
135: * Avoid initial header printing.
136: */
137: assign("noheader", "");
138: break;
139: case 'v':
140: /*
141: * Send mailer verbose flag
142: */
143: assign("verbose", "");
144: break;
145: case 'I':
146: /*
147: * We're interactive
148: */
149: assign("interactive", "");
150: break;
151: case 'c':
152: /*
153: * Get Carbon Copy Recipient list
154: */
155: cc = cat(cc, nalloc(optarg, GCC));
156: break;
157: case 'b':
158: /*
159: * Get Blind Carbon Copy Recipient list
160: */
161: bcc = cat(bcc, nalloc(optarg, GBCC));
162: break;
1.21 martynas 163: case 'E':
164: /*
165: * Don't send messages with an empty body.
166: */
167: assign("skipempty", "");
168: break;
1.15 millert 169: default:
170: usage();
171: /*NOTREACHED*/
1.1 deraadt 172: }
173: }
1.25 millert 174: if (ef != NULL) {
175: /* Check for optional mailbox file name. */
176: if (optind < argc) {
177: ef = argv[optind++];
178: if (optind < argc)
179: errx(1, "Cannot give -f and people to send to");
180: }
181: } else {
1.26 millert 182: for (i = optind; argv[i]; i++)
1.25 millert 183: to = cat(to, nalloc(argv[i], GTO));
184: }
1.1 deraadt 185: /*
186: * Check for inconsistent arguments.
187: */
1.16 millert 188: if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL))
1.5 millert 189: errx(1, "You must specify direct recipients with -s, -c, or -b");
1.15 millert 190: /*
191: * Block SIGINT except where we install an explicit handler for it.
192: */
193: sigemptyset(&intset);
194: sigaddset(&intset, SIGINT);
195: (void)sigprocmask(SIG_BLOCK, &intset, NULL);
196: /*
197: * Initialization.
198: */
1.1 deraadt 199: tinit();
200: setscreensize();
201: input = stdin;
202: rcvmode = !to;
203: spreserve();
204: if (!nosrc)
205: load(_PATH_MASTER_RC);
206: /*
207: * Expand returns a savestr, but load only uses the file name
208: * for fopen, so it's safe to do this.
209: */
1.5 millert 210: if ((rc = getenv("MAILRC")) == 0)
211: rc = "~/.mailrc";
212: load(expand(rc));
1.1 deraadt 213: if (!rcvmode) {
1.28 ! millert 214: mail(to, cc, bcc, smopts, fromaddr, subject);
1.1 deraadt 215: /*
216: * why wait?
217: */
218: exit(senderr);
219: }
220: /*
221: * Ok, we are reading mail.
222: * Decide whether we are editing a mailbox or reading
223: * the system mailbox, and open up the right stuff.
224: */
1.7 millert 225: if (ef == NULL)
1.1 deraadt 226: ef = "%";
227: if (setfile(ef) < 0)
228: exit(1); /* error already reported */
229:
1.15 millert 230: if (value("quiet") == NULL)
231: (void)printf("Mail version %s. Type ? for help.\n",
232: version);
233: announce();
234: (void)fflush(stdout);
1.1 deraadt 235: commands();
1.15 millert 236: (void)ignoresig(SIGHUP, NULL, NULL);
237: (void)ignoresig(SIGINT, NULL, NULL);
238: (void)ignoresig(SIGQUIT, NULL, NULL);
1.1 deraadt 239: quit();
240: exit(0);
241: }
242:
243: /*
244: * Compute what the screen size for printing headers should be.
245: * We use the following algorithm for the height:
246: * If baud rate < 1200, use 9
247: * If baud rate = 1200, use 14
248: * If baud rate > 1200, use 24 or ws_row
249: * Width is either 80 or ws_col;
250: */
251: void
1.16 millert 252: setscreensize(void)
1.1 deraadt 253: {
254: struct termios tbuf;
255: struct winsize ws;
256: speed_t ospeed;
257:
258: if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
259: ws.ws_col = ws.ws_row = 0;
260: if (tcgetattr(1, &tbuf) < 0)
261: ospeed = 9600;
262: else
263: ospeed = cfgetospeed(&tbuf);
1.8 millert 264: if (ospeed < B1200)
1.1 deraadt 265: screenheight = 9;
1.8 millert 266: else if (ospeed == B1200)
1.1 deraadt 267: screenheight = 14;
268: else if (ws.ws_row != 0)
269: screenheight = ws.ws_row;
270: else
271: screenheight = 24;
272: if ((realscreenheight = ws.ws_row) == 0)
273: realscreenheight = 24;
274: if ((screenwidth = ws.ws_col) == 0)
275: screenwidth = 80;
1.15 millert 276: }
277:
278: __dead void
1.16 millert 279: usage(void)
1.15 millert 280: {
281:
1.22 jmc 282: fprintf(stderr, "usage: %s [-dEIinv] [-b list] [-c list] "
1.28 ! millert 283: "[-r from-addr] [-s subject] to-addr ...\n", __progname);
1.22 jmc 284: fprintf(stderr, " %s [-dEIiNnv] -f [file]\n", __progname);
285: fprintf(stderr, " %s [-dEIiNnv] [-u user]\n", __progname);
1.15 millert 286: exit(1);
1.1 deraadt 287: }