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