1 /* $NetBSD: main.c,v 1.21 2006/05/01 23:12:24 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. Neither the name of the University nor the names of its contributors 16 * may be used to endorse or promote products derived from this software 17 * without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29 * SUCH DAMAGE. 30 */ 31 32 #include <sys/cdefs.h> 33 #ifndef lint 34 __COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ 35 The Regents of the University of California. All rights reserved.\n"); 36 #endif /* not lint */ 37 38 #ifndef lint 39 #if 0 40 static char sccsid[] = "@(#)main.c 8.2 (Berkeley) 4/20/95"; 41 #else 42 __RCSID("$NetBSD: main.c,v 1.21 2006/05/01 23:12:24 christos Exp $"); 43 #endif 44 #endif /* not lint */ 45 46 #define EXTERN 47 #include "rcv.h" 48 #undef EXTERN 49 50 #include "extern.h" 51 52 int main(int, char **); 53 54 /* 55 * Mail -- a mail program 56 * 57 * Startup -- interface with user. 58 */ 59 60 jmp_buf hdrjmp; 61 62 int 63 main(int argc, char *argv[]) 64 { 65 int i; 66 struct name *to, *cc, *bcc, *smopts; 67 char *subject; 68 const char *ef; 69 char nosrc = 0; 70 sig_t prevint; 71 const char *rc; 72 73 /* 74 * Set up a reasonable environment. 75 * Figure out whether we are being run interactively, 76 * start the SIGCHLD catcher, and so forth. 77 */ 78 (void)signal(SIGCHLD, sigchild); 79 if (isatty(0)) 80 assign("interactive", ""); 81 image = -1; 82 /* 83 * Now, determine how we are being used. 84 * We successively pick off - flags. 85 * If there is anything left, it is the base of the list 86 * of users to mail to. Argp will be set to point to the 87 * first of these users. 88 */ 89 ef = NULL; 90 to = NULL; 91 cc = NULL; 92 bcc = NULL; 93 smopts = NULL; 94 subject = NULL; 95 while ((i = getopt(argc, argv, "~EINT:b:c:dfins:u:v")) != -1) { 96 switch (i) { 97 case 'T': 98 /* 99 * Next argument is temp file to write which 100 * articles have been read/deleted for netnews. 101 */ 102 Tflag = optarg; 103 if ((i = creat(Tflag, 0600)) < 0) { 104 warn("%s", Tflag); 105 exit(1); 106 } 107 (void)close(i); 108 break; 109 case 'u': 110 /* 111 * Next argument is person to pretend to be. 112 */ 113 myname = optarg; 114 (void)unsetenv("MAIL"); 115 break; 116 case 'i': 117 /* 118 * User wants to ignore interrupts. 119 * Set the variable "ignore" 120 */ 121 assign("ignore", ""); 122 break; 123 case 'd': 124 debug++; 125 break; 126 case 's': 127 /* 128 * Give a subject field for sending from 129 * non terminal 130 */ 131 subject = optarg; 132 break; 133 case 'f': 134 /* 135 * User is specifying file to "edit" with Mail, 136 * as opposed to reading system mailbox. 137 * If no argument is given after -f, we read his 138 * mbox file. 139 * 140 * getopt() can't handle optional arguments, so here 141 * is an ugly hack to get around it. 142 */ 143 if ((argv[optind]) && (argv[optind][0] != '-')) 144 ef = argv[optind++]; 145 else 146 ef = "&"; 147 break; 148 case 'n': 149 /* 150 * User doesn't want to source /usr/lib/Mail.rc 151 */ 152 nosrc++; 153 break; 154 case 'N': 155 /* 156 * Avoid initial header printing. 157 */ 158 assign("noheader", ""); 159 break; 160 case 'v': 161 /* 162 * Send mailer verbose flag 163 */ 164 assign("verbose", ""); 165 break; 166 case 'I': 167 case '~': 168 /* 169 * We're interactive 170 */ 171 assign("interactive", ""); 172 break; 173 case 'c': 174 /* 175 * Get Carbon Copy Recipient list 176 */ 177 cc = cat(cc, nalloc(optarg, GCC)); 178 break; 179 case 'b': 180 /* 181 * Get Blind Carbon Copy Recipient list 182 */ 183 bcc = cat(bcc, nalloc(optarg, GBCC)); 184 break; 185 case 'E': 186 /* 187 * Don't send empty files. 188 */ 189 assign("dontsendempty", ""); 190 break; 191 case '?': 192 (void)fputs("\ 193 Usage: mail [-EiInv] [-s subject] [-c cc-addr] [-b bcc-addr] to-addr ...\n\ 194 [- sendmail-options ...]\n\ 195 mail [-EiInNv] -f [name]\n\ 196 mail [-EiInNv] [-u user]\n", 197 stderr); 198 exit(1); 199 } 200 } 201 for (i = optind; (argv[i]) && (*argv[i] != '-'); i++) 202 to = cat(to, nalloc(argv[i], GTO)); 203 for (; argv[i]; i++) 204 smopts = cat(smopts, nalloc(argv[i], 0)); 205 /* 206 * Check for inconsistent arguments. 207 */ 208 if (to == NULL && (subject != NULL || cc != NULL || bcc != NULL)) 209 errx(1, "You must specify direct recipients with -s, -c, or -b."); 210 if (ef != NULL && to != NULL) { 211 errx(1, "Cannot give -f and people to send to."); 212 } 213 tinit(); 214 setscreensize(); 215 input = stdin; 216 rcvmode = !to; 217 spreserve(); 218 if (!nosrc) 219 load(_PATH_MASTER_RC); 220 /* 221 * Expand returns a savestr, but load only uses the file name 222 * for fopen, so it's safe to do this. 223 */ 224 if ((rc = getenv("MAILRC")) == 0) 225 rc = "~/.mailrc"; 226 load(expand(rc)); 227 if (!rcvmode) { 228 (void)mail(to, cc, bcc, smopts, subject); 229 /* 230 * why wait? 231 */ 232 exit(senderr); 233 } 234 /* 235 * Ok, we are reading mail. 236 * Decide whether we are editing a mailbox or reading 237 * the system mailbox, and open up the right stuff. 238 */ 239 if (ef == NULL) 240 ef = "%"; 241 if (setfile(ef) < 0) 242 exit(1); /* error already reported */ 243 if (setjmp(hdrjmp) == 0) { 244 if ((prevint = signal(SIGINT, SIG_IGN)) != SIG_IGN) 245 (void)signal(SIGINT, hdrstop); 246 if (value("quiet") == NULL) 247 (void)printf("Mail version %s. Type ? for help.\n", 248 version); 249 announce(); 250 (void)fflush(stdout); 251 (void)signal(SIGINT, prevint); 252 } 253 commands(); 254 (void)signal(SIGHUP, SIG_IGN); 255 (void)signal(SIGINT, SIG_IGN); 256 (void)signal(SIGQUIT, SIG_IGN); 257 quit(); 258 return 0; 259 } 260 261 /* 262 * Interrupt printing of the headers. 263 */ 264 void 265 /*ARGSUSED*/ 266 hdrstop(int signo) 267 { 268 269 (void)fflush(stdout); 270 (void)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(void) 284 { 285 struct termios tbuf; 286 struct winsize ws; 287 speed_t ospeed; 288 289 if (ioctl(1, TIOCGWINSZ, &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