Annotation of src/usr.bin/mail/main.c, Revision 1.10
1.10 ! millert 1: /* $OpenBSD: main.c,v 1.9 1997/07/30 06:32:40 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.10 ! millert 47: static char rcsid[] = "$OpenBSD: main.c,v 1.9 1997/07/30 06:32:40 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 **));
! 57:
1.1 deraadt 58: /*
59: * Mail -- a mail program
60: *
61: * Startup -- interface with user.
62: */
63:
1.7 millert 64: sigjmp_buf hdrjmp;
1.1 deraadt 65:
66: int
67: main(argc, argv)
68: int argc;
69: char *argv[];
70: {
1.10 ! millert 71: int i;
1.1 deraadt 72: struct name *to, *cc, *bcc, *smopts;
73: char *subject;
74: char *ef;
75: char nosrc = 0;
76: sig_t prevint;
1.5 millert 77: char *rc;
1.1 deraadt 78:
79: /*
80: * Set up a reasonable environment.
81: * Figure out whether we are being run interactively,
82: * start the SIGCHLD catcher, and so forth.
83: */
1.6 millert 84: (void)signal(SIGCHLD, sigchild);
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)
110: err(1, Tflag);
111: (void)close(i);
1.1 deraadt 112: break;
113: case 'u':
114: /*
115: * Next argument is person to pretend to be.
116: */
1.3 deraadt 117: unsetenv("MAIL");
1.1 deraadt 118: myname = optarg;
119: break;
120: case 'i':
121: /*
122: * User wants to ignore interrupts.
123: * Set the variable "ignore"
124: */
125: assign("ignore", "");
126: break;
127: case 'd':
128: debug++;
129: break;
130: case 's':
131: /*
132: * Give a subject field for sending from
133: * non terminal
134: */
135: subject = optarg;
136: break;
137: case 'f':
138: /*
139: * User is specifying file to "edit" with Mail,
140: * as opposed to reading system mailbox.
141: * If no argument is given after -f, we read his
142: * mbox file.
143: *
144: * getopt() can't handle optional arguments, so here
145: * is an ugly hack to get around it.
146: */
147: if ((argv[optind]) && (argv[optind][0] != '-'))
148: ef = argv[optind++];
149: else
150: ef = "&";
151: break;
152: case 'n':
153: /*
154: * User doesn't want to source /usr/lib/Mail.rc
155: */
156: nosrc++;
157: break;
158: case 'N':
159: /*
160: * Avoid initial header printing.
161: */
162: assign("noheader", "");
163: break;
164: case 'v':
165: /*
166: * Send mailer verbose flag
167: */
168: assign("verbose", "");
169: break;
170: case 'I':
171: /*
172: * We're interactive
173: */
174: assign("interactive", "");
175: break;
176: case 'c':
177: /*
178: * Get Carbon Copy Recipient list
179: */
180: cc = cat(cc, nalloc(optarg, GCC));
181: break;
182: case 'b':
183: /*
184: * Get Blind Carbon Copy Recipient list
185: */
186: bcc = cat(bcc, nalloc(optarg, GBCC));
187: break;
188: case '?':
1.5 millert 189: fprintf(stderr, "\
190: Usage: %s [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\
1.1 deraadt 191: [- sendmail-options ...]\n\
1.5 millert 192: %s [-iInNv] -f [name]\n\
193: %s [-iInNv] [-u user]\n", __progname, __progname, __progname);
1.1 deraadt 194: exit(1);
195: }
196: }
197: for (i = optind; (argv[i]) && (*argv[i] != '-'); i++)
198: to = cat(to, nalloc(argv[i], GTO));
199: for (; argv[i]; i++)
200: smopts = cat(smopts, nalloc(argv[i], 0));
201: /*
202: * Check for inconsistent arguments.
203: */
1.7 millert 204: if (to == NIL && (subject != NULL || cc != NIL || bcc != NIL))
1.5 millert 205: errx(1, "You must specify direct recipients with -s, -c, or -b");
1.7 millert 206: if (ef != NULL && to != NIL)
1.5 millert 207: errx(1, "Cannot give -f and people to send to");
1.1 deraadt 208: tinit();
209: setscreensize();
210: input = stdin;
211: rcvmode = !to;
212: spreserve();
213: if (!nosrc)
214: load(_PATH_MASTER_RC);
215: /*
216: * Expand returns a savestr, but load only uses the file name
217: * for fopen, so it's safe to do this.
218: */
1.5 millert 219: if ((rc = getenv("MAILRC")) == 0)
220: rc = "~/.mailrc";
221: load(expand(rc));
1.1 deraadt 222: if (!rcvmode) {
223: mail(to, cc, bcc, smopts, subject);
224: /*
225: * why wait?
226: */
227: exit(senderr);
228: }
229: /*
230: * Ok, we are reading mail.
231: * Decide whether we are editing a mailbox or reading
232: * the system mailbox, and open up the right stuff.
233: */
1.7 millert 234: if (ef == NULL)
1.1 deraadt 235: ef = "%";
236: if (setfile(ef) < 0)
237: exit(1); /* error already reported */
1.7 millert 238: if (sigsetjmp(hdrjmp, 1) == 0) {
1.1 deraadt 239: extern char *version;
240:
241: if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN)
1.9 millert 242: (void)signal(SIGINT, hdrstop);
1.7 millert 243: if (value("quiet") == NULL)
1.9 millert 244: (void)printf("Mail version %s. Type ? for help.\n",
1.1 deraadt 245: version);
246: announce();
1.9 millert 247: (void)fflush(stdout);
248: (void)signal(SIGINT, prevint);
1.1 deraadt 249: }
250: commands();
1.9 millert 251: (void)signal(SIGHUP, SIG_IGN);
252: (void)signal(SIGINT, SIG_IGN);
253: (void)signal(SIGQUIT, SIG_IGN);
1.1 deraadt 254: quit();
255: exit(0);
256: }
257:
258: /*
259: * Interrupt printing of the headers.
260: */
261: void
262: hdrstop(signo)
263: int signo;
264: {
265:
266: fflush(stdout);
1.5 millert 267: fputs("\nInterrupt\n", stderr);
1.7 millert 268: siglongjmp(hdrjmp, 1);
1.1 deraadt 269: }
270:
271: /*
272: * Compute what the screen size for printing headers should be.
273: * We use the following algorithm for the height:
274: * If baud rate < 1200, use 9
275: * If baud rate = 1200, use 14
276: * If baud rate > 1200, use 24 or ws_row
277: * Width is either 80 or ws_col;
278: */
279: void
280: setscreensize()
281: {
282: struct termios tbuf;
283: struct winsize ws;
284: speed_t ospeed;
285:
286: if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0)
287: ws.ws_col = ws.ws_row = 0;
288: if (tcgetattr(1, &tbuf) < 0)
289: ospeed = 9600;
290: else
291: ospeed = cfgetospeed(&tbuf);
1.8 millert 292: if (ospeed < B1200)
1.1 deraadt 293: screenheight = 9;
1.8 millert 294: else if (ospeed == B1200)
1.1 deraadt 295: screenheight = 14;
296: else if (ws.ws_row != 0)
297: screenheight = ws.ws_row;
298: else
299: screenheight = 24;
300: if ((realscreenheight = ws.ws_row) == 0)
301: realscreenheight = 24;
302: if ((screenwidth = ws.ws_col) == 0)
303: screenwidth = 80;
304: }