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