1 /* $NetBSD: main.c,v 1.8 1997/10/19 05:03:38 lukem Exp $ */ 2 3 /* 4 * Copyright (c) 1980, 1993 5 * The Regents of the University of California. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 1. Redistributions of source code must retain the above copyright 11 * notice, this list of conditions and the following disclaimer. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. All advertising materials mentioning features or use of this software 16 * must display the following acknowledgement: 17 * This product includes software developed by the University of 18 * California, Berkeley and its contributors. 19 * 4. Neither the name of the University nor the names of its contributors 20 * may be used to endorse or promote products derived from this software 21 * without specific prior written permission. 22 * 23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 33 * SUCH DAMAGE. 34 */ 35 36 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ 39 The Regents of the University of California. All rights reserved.\n"); 40 #endif /* not lint */ 41 42 #ifndef lint 43 #if 0 44 static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/20/95"; 45 #else 46 __RCSID("$NetBSD: main.c,v 1.8 1997/10/19 05:03:38 lukem Exp $"); 47 #endif 48 #endif /* not lint */ 49 50 #include "rcv.h" 51 #include "extern.h" 52 53 int main __P((int, char **)); 54 55 /* 56 * Mail -- a mail program 57 * 58 * Startup -- interface with user. 59 */ 60 61 jmp_buf hdrjmp; 62 63 int 64 main(argc, argv) 65 int argc; 66 char *argv[]; 67 { 68 int i; 69 struct name *to, *cc, *bcc, *smopts; 70 char *subject; 71 char *ef; 72 char nosrc = 0; 73 sig_t prevint; 74 char *rc; 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")) != -1) { 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 if ((rc = getenv("MAILRC")) == 0) 223 rc = "~/.mailrc"; 224 load(expand(rc)); 225 if (!rcvmode) { 226 mail(to, cc, bcc, smopts, subject); 227 /* 228 * why wait? 229 */ 230 exit(senderr); 231 } 232 /* 233 * Ok, we are reading mail. 234 * Decide whether we are editing a mailbox or reading 235 * the system mailbox, and open up the right stuff. 236 */ 237 if (ef == NOSTR) 238 ef = "%"; 239 if (setfile(ef) < 0) 240 exit(1); /* error already reported */ 241 if (setjmp(hdrjmp) == 0) { 242 extern char *version; 243 244 if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN) 245 signal(SIGINT, hdrstop); 246 if (value("quiet") == NOSTR) 247 printf("Mail version %s. Type ? for help.\n", 248 version); 249 announce(); 250 fflush(stdout); 251 signal(SIGINT, prevint); 252 } 253 commands(); 254 signal(SIGHUP, SIG_IGN); 255 signal(SIGINT, SIG_IGN); 256 signal(SIGQUIT, SIG_IGN); 257 quit(); 258 exit(0); 259 } 260 261 /* 262 * Interrupt printing of the headers. 263 */ 264 void 265 hdrstop(signo) 266 int signo; 267 { 268 269 fflush(stdout); 270 fprintf(stderr, "\nInterrupt\n"); 271 longjmp(hdrjmp, 1); 272 } 273 274 /* 275 * Compute what the screen size for printing headers should be. 276 * We use the following algorithm for the height: 277 * If baud rate < 1200, use 9 278 * If baud rate = 1200, use 14 279 * If baud rate > 1200, use 24 or ws_row 280 * Width is either 80 or ws_col; 281 */ 282 void 283 setscreensize() 284 { 285 struct termios tbuf; 286 struct winsize ws; 287 speed_t ospeed; 288 289 if (ioctl(1, TIOCGWINSZ, (char *) &ws) < 0) 290 ws.ws_col = ws.ws_row = 0; 291 if (tcgetattr(1, &tbuf) < 0) 292 ospeed = 9600; 293 else 294 ospeed = cfgetospeed(&tbuf); 295 if (ospeed < 1200) 296 screenheight = 9; 297 else if (ospeed == 1200) 298 screenheight = 14; 299 else if (ws.ws_row != 0) 300 screenheight = ws.ws_row; 301 else 302 screenheight = 24; 303 if ((realscreenheight = ws.ws_row) == 0) 304 realscreenheight = 24; 305 if ((screenwidth = ws.ws_col) == 0) 306 screenwidth = 80; 307 } 308