1 /* $OpenBSD: main.c,v 1.11 1998/09/27 21:16:42 millert Exp $ */ 2 /* $NetBSD: main.c,v 1.7 1997/05/13 06:15:57 mikel Exp $ */ 3 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 44 #if 0 45 static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/20/95"; 46 #else 47 static char rcsid[] = "$OpenBSD: main.c,v 1.11 1998/09/27 21:16:42 millert Exp $"; 48 #endif 49 #endif /* not lint */ 50 51 #include "rcv.h" 52 #include <fcntl.h> 53 #include <sys/ioctl.h> 54 #include "extern.h" 55 56 int main __P((int, char **)); 57 58 /* 59 * Mail -- a mail program 60 * 61 * Startup -- interface with user. 62 */ 63 64 sigjmp_buf hdrjmp; 65 66 int 67 main(argc, argv) 68 int argc; 69 char *argv[]; 70 { 71 int i; 72 struct name *to, *cc, *bcc, *smopts; 73 char *subject; 74 char *ef; 75 char nosrc = 0; 76 sig_t prevint; 77 char *rc; 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 */ 84 (void)signal(SIGCHLD, sigchild); 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 */ 95 ef = NULL; 96 to = NIL; 97 cc = NIL; 98 bcc = NIL; 99 smopts = NIL; 100 subject = NULL; 101 while ((i = getopt(argc, argv, "INT:b:c:dfins:u:v")) != -1) { 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; 109 if ((i = creat(Tflag, 0600)) < 0) 110 err(1, Tflag); 111 (void)close(i); 112 break; 113 case 'u': 114 /* 115 * Next argument is person to pretend to be. 116 */ 117 unsetenv("MAIL"); 118 myname = optarg; 119 uflag = 1; 120 break; 121 case 'i': 122 /* 123 * User wants to ignore interrupts. 124 * Set the variable "ignore" 125 */ 126 assign("ignore", ""); 127 break; 128 case 'd': 129 debug++; 130 break; 131 case 's': 132 /* 133 * Give a subject field for sending from 134 * non terminal 135 */ 136 subject = optarg; 137 break; 138 case 'f': 139 /* 140 * User is specifying file to "edit" with Mail, 141 * as opposed to reading system mailbox. 142 * If no argument is given after -f, we read his 143 * mbox file. 144 * 145 * getopt() can't handle optional arguments, so here 146 * is an ugly hack to get around it. 147 */ 148 if ((argv[optind]) && (argv[optind][0] != '-')) 149 ef = argv[optind++]; 150 else 151 ef = "&"; 152 break; 153 case 'n': 154 /* 155 * User doesn't want to source /usr/lib/Mail.rc 156 */ 157 nosrc++; 158 break; 159 case 'N': 160 /* 161 * Avoid initial header printing. 162 */ 163 assign("noheader", ""); 164 break; 165 case 'v': 166 /* 167 * Send mailer verbose flag 168 */ 169 assign("verbose", ""); 170 break; 171 case 'I': 172 /* 173 * We're interactive 174 */ 175 assign("interactive", ""); 176 break; 177 case 'c': 178 /* 179 * Get Carbon Copy Recipient list 180 */ 181 cc = cat(cc, nalloc(optarg, GCC)); 182 break; 183 case 'b': 184 /* 185 * Get Blind Carbon Copy Recipient list 186 */ 187 bcc = cat(bcc, nalloc(optarg, GBCC)); 188 break; 189 case '?': 190 fprintf(stderr, "\ 191 Usage: %s [-iInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\ 192 [- sendmail-options ...]\n\ 193 %s [-iInNv] -f [name]\n\ 194 %s [-iInNv] [-u user]\n", __progname, __progname, __progname); 195 exit(1); 196 } 197 } 198 for (i = optind; (argv[i]) && (*argv[i] != '-'); i++) 199 to = cat(to, nalloc(argv[i], GTO)); 200 for (; argv[i]; i++) 201 smopts = cat(smopts, nalloc(argv[i], 0)); 202 /* 203 * Check for inconsistent arguments. 204 */ 205 if (to == NIL && (subject != NULL || cc != NIL || bcc != NIL)) 206 errx(1, "You must specify direct recipients with -s, -c, or -b"); 207 if (ef != NULL && to != NIL) 208 errx(1, "Cannot give -f and people to send to"); 209 tinit(); 210 setscreensize(); 211 input = stdin; 212 rcvmode = !to; 213 spreserve(); 214 if (!nosrc) 215 load(_PATH_MASTER_RC); 216 /* 217 * Expand returns a savestr, but load only uses the file name 218 * for fopen, so it's safe to do this. 219 */ 220 if ((rc = getenv("MAILRC")) == 0) 221 rc = "~/.mailrc"; 222 load(expand(rc)); 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 == NULL) 236 ef = "%"; 237 if (setfile(ef) < 0) 238 exit(1); /* error already reported */ 239 if (sigsetjmp(hdrjmp, 1) == 0) { 240 extern char *version; 241 242 if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN) 243 (void)signal(SIGINT, hdrstop); 244 if (value("quiet") == NULL) 245 (void)printf("Mail version %s. Type ? for help.\n", 246 version); 247 announce(); 248 (void)fflush(stdout); 249 (void)signal(SIGINT, prevint); 250 } 251 commands(); 252 (void)signal(SIGHUP, SIG_IGN); 253 (void)signal(SIGINT, SIG_IGN); 254 (void)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 fputs("\nInterrupt\n", stderr); 269 siglongjmp(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 < B1200) 294 screenheight = 9; 295 else if (ospeed == B1200) 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 } 306