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