1 /* 2 * Copyright (c) 1980, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 #if 0 36 static char sccsid[] = "@(#)cmd2.c 8.1 (Berkeley) 6/6/93"; 37 #endif 38 #endif /* not lint */ 39 #include <sys/cdefs.h> 40 __FBSDID("$FreeBSD$"); 41 42 #include "rcv.h" 43 #include <sys/wait.h> 44 #include "extern.h" 45 46 /* 47 * Mail -- a mail program 48 * 49 * More user commands. 50 */ 51 52 extern int wait_status; 53 54 /* 55 * If any arguments were given, go to the next applicable argument 56 * following dot, otherwise, go to the next applicable message. 57 * If given as first command with no arguments, print first message. 58 */ 59 int 60 next(msgvec) 61 int *msgvec; 62 { 63 struct message *mp; 64 int *ip, *ip2, list[2], mdot; 65 66 if (*msgvec != 0) { 67 68 /* 69 * If some messages were supplied, find the 70 * first applicable one following dot using 71 * wrap around. 72 */ 73 74 mdot = dot - &message[0] + 1; 75 76 /* 77 * Find the first message in the supplied 78 * message list which follows dot. 79 */ 80 81 for (ip = msgvec; *ip != 0; ip++) 82 if (*ip > mdot) 83 break; 84 if (*ip == 0) 85 ip = msgvec; 86 ip2 = ip; 87 do { 88 mp = &message[*ip2 - 1]; 89 if ((mp->m_flag & MDELETED) == 0) { 90 dot = mp; 91 goto hitit; 92 } 93 if (*ip2 != 0) 94 ip2++; 95 if (*ip2 == 0) 96 ip2 = msgvec; 97 } while (ip2 != ip); 98 printf("No messages applicable\n"); 99 return (1); 100 } 101 102 /* 103 * If this is the first command, select message 1. 104 * Note that this must exist for us to get here at all. 105 */ 106 107 if (!sawcom) 108 goto hitit; 109 110 /* 111 * Just find the next good message after dot, no 112 * wraparound. 113 */ 114 115 for (mp = dot+1; mp < &message[msgCount]; mp++) 116 if ((mp->m_flag & (MDELETED|MSAVED)) == 0) 117 break; 118 if (mp >= &message[msgCount]) { 119 printf("At EOF\n"); 120 return (0); 121 } 122 dot = mp; 123 hitit: 124 /* 125 * Print dot. 126 */ 127 128 list[0] = dot - &message[0] + 1; 129 list[1] = 0; 130 return (type(list)); 131 } 132 133 /* 134 * Save a message in a file. Mark the message as saved 135 * so we can discard when the user quits. 136 */ 137 int 138 save(str) 139 char str[]; 140 { 141 142 return (save1(str, 1, "save", saveignore)); 143 } 144 145 /* 146 * Copy a message to a file without affected its saved-ness 147 */ 148 int 149 copycmd(str) 150 char str[]; 151 { 152 153 return (save1(str, 0, "copy", saveignore)); 154 } 155 156 /* 157 * Save/copy the indicated messages at the end of the passed file name. 158 * If mark is true, mark the message "saved." 159 */ 160 int 161 save1(str, mark, cmd, ignore) 162 char str[]; 163 int mark; 164 const char *cmd; 165 struct ignoretab *ignore; 166 { 167 struct message *mp; 168 char *file; 169 const char *disp; 170 int f, *msgvec, *ip; 171 FILE *obuf; 172 173 msgvec = (int *)salloc((msgCount + 2) * sizeof(*msgvec)); 174 if ((file = snarf(str, &f)) == NULL) 175 return (1); 176 if (!f) { 177 *msgvec = first(0, MMNORM); 178 if (*msgvec == 0) { 179 printf("No messages to %s.\n", cmd); 180 return (1); 181 } 182 msgvec[1] = 0; 183 } 184 if (f && getmsglist(str, msgvec, 0) < 0) 185 return (1); 186 if ((file = expand(file)) == NULL) 187 return (1); 188 printf("\"%s\" ", file); 189 (void)fflush(stdout); 190 if (access(file, 0) >= 0) 191 disp = "[Appended]"; 192 else 193 disp = "[New file]"; 194 if ((obuf = Fopen(file, "a")) == NULL) { 195 warn((char *)NULL); 196 return (1); 197 } 198 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) { 199 mp = &message[*ip - 1]; 200 touch(mp); 201 if (sendmessage(mp, obuf, ignore, NULL) < 0) { 202 warnx("%s", file); 203 (void)Fclose(obuf); 204 return (1); 205 } 206 if (mark) 207 mp->m_flag |= MSAVED; 208 } 209 (void)fflush(obuf); 210 if (ferror(obuf)) 211 warn("%s", file); 212 (void)Fclose(obuf); 213 printf("%s\n", disp); 214 return (0); 215 } 216 217 /* 218 * Write the indicated messages at the end of the passed 219 * file name, minus header and trailing blank line. 220 */ 221 int 222 swrite(str) 223 char str[]; 224 { 225 226 return (save1(str, 1, "write", ignoreall)); 227 } 228 229 /* 230 * Snarf the file from the end of the command line and 231 * return a pointer to it. If there is no file attached, 232 * just return NULL. Put a null in front of the file 233 * name so that the message list processing won't see it, 234 * unless the file name is the only thing on the line, in 235 * which case, return 0 in the reference flag variable. 236 */ 237 238 char * 239 snarf(linebuf, flag) 240 char linebuf[]; 241 int *flag; 242 { 243 char *cp; 244 245 *flag = 1; 246 cp = strlen(linebuf) + linebuf - 1; 247 248 /* 249 * Strip away trailing blanks. 250 */ 251 252 while (cp > linebuf && isspace((unsigned char)*cp)) 253 cp--; 254 *++cp = '\0'; 255 256 /* 257 * Now search for the beginning of the file name. 258 */ 259 260 while (cp > linebuf && !isspace((unsigned char)*cp)) 261 cp--; 262 if (*cp == '\0') { 263 printf("No file specified.\n"); 264 return (NULL); 265 } 266 if (isspace((unsigned char)*cp)) 267 *cp++ = '\0'; 268 else 269 *flag = 0; 270 return (cp); 271 } 272 273 /* 274 * Delete messages. 275 */ 276 int 277 delete(msgvec) 278 int msgvec[]; 279 { 280 281 delm(msgvec); 282 return (0); 283 } 284 285 /* 286 * Delete messages, then type the new dot. 287 */ 288 int 289 deltype(msgvec) 290 int msgvec[]; 291 { 292 int list[2]; 293 int lastdot; 294 295 lastdot = dot - &message[0] + 1; 296 if (delm(msgvec) >= 0) { 297 list[0] = dot - &message[0] + 1; 298 if (list[0] > lastdot) { 299 touch(dot); 300 list[1] = 0; 301 return (type(list)); 302 } 303 printf("At EOF\n"); 304 } else 305 printf("No more messages\n"); 306 return (0); 307 } 308 309 /* 310 * Delete the indicated messages. 311 * Set dot to some nice place afterwards. 312 * Internal interface. 313 */ 314 int 315 delm(msgvec) 316 int *msgvec; 317 { 318 struct message *mp; 319 int *ip, last; 320 321 last = 0; 322 for (ip = msgvec; *ip != 0; ip++) { 323 mp = &message[*ip - 1]; 324 touch(mp); 325 mp->m_flag |= MDELETED|MTOUCH; 326 mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX); 327 last = *ip; 328 } 329 if (last != 0) { 330 dot = &message[last-1]; 331 last = first(0, MDELETED); 332 if (last != 0) { 333 dot = &message[last-1]; 334 return (0); 335 } 336 else { 337 dot = &message[0]; 338 return (-1); 339 } 340 } 341 342 /* 343 * Following can't happen -- it keeps lint happy 344 */ 345 346 return (-1); 347 } 348 349 /* 350 * Undelete the indicated messages. 351 */ 352 int 353 undelete_messages(msgvec) 354 int *msgvec; 355 { 356 struct message *mp; 357 int *ip; 358 359 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) { 360 mp = &message[*ip - 1]; 361 touch(mp); 362 dot = mp; 363 mp->m_flag &= ~MDELETED; 364 } 365 return (0); 366 } 367 368 /* 369 * Interactively dump core on "core" 370 */ 371 int 372 core() 373 { 374 int pid; 375 376 switch (pid = fork()) { 377 case -1: 378 warn("fork"); 379 return (1); 380 case 0: 381 abort(); 382 _exit(1); 383 } 384 printf("Okie dokie"); 385 (void)fflush(stdout); 386 wait_child(pid); 387 if (WIFSIGNALED(wait_status) && WCOREDUMP(wait_status)) 388 printf(" -- Core dumped.\n"); 389 else 390 printf(" -- Can't dump core.\n"); 391 return (0); 392 } 393 394 /* 395 * Clobber as many bytes of stack as the user requests. 396 */ 397 int 398 clobber(argv) 399 char **argv; 400 { 401 int times; 402 403 if (argv[0] == 0) 404 times = 1; 405 else 406 times = (atoi(argv[0]) + 511) / 512; 407 clob1(times); 408 return (0); 409 } 410 411 /* 412 * Clobber the stack. 413 */ 414 void 415 clob1(n) 416 int n; 417 { 418 char buf[512]; 419 char *cp; 420 421 if (n <= 0) 422 return; 423 for (cp = buf; cp < &buf[512]; *cp++ = 0xFF) 424 ; 425 clob1(n - 1); 426 } 427 428 /* 429 * Add the given header fields to the retained list. 430 * If no arguments, print the current list of retained fields. 431 */ 432 int 433 retfield(list) 434 char *list[]; 435 { 436 437 return (ignore1(list, ignore + 1, "retained")); 438 } 439 440 /* 441 * Add the given header fields to the ignored list. 442 * If no arguments, print the current list of ignored fields. 443 */ 444 int 445 igfield(list) 446 char *list[]; 447 { 448 449 return (ignore1(list, ignore, "ignored")); 450 } 451 452 int 453 saveretfield(list) 454 char *list[]; 455 { 456 457 return (ignore1(list, saveignore + 1, "retained")); 458 } 459 460 int 461 saveigfield(list) 462 char *list[]; 463 { 464 465 return (ignore1(list, saveignore, "ignored")); 466 } 467 468 int 469 ignore1(list, tab, which) 470 char *list[]; 471 struct ignoretab *tab; 472 const char *which; 473 { 474 char field[LINESIZE]; 475 int h; 476 struct ignore *igp; 477 char **ap; 478 479 if (*list == NULL) 480 return (igshow(tab, which)); 481 for (ap = list; *ap != 0; ap++) { 482 istrncpy(field, *ap, sizeof(field)); 483 if (member(field, tab)) 484 continue; 485 h = hash(field); 486 igp = calloc(1, sizeof(struct ignore)); 487 igp->i_field = calloc((unsigned)strlen(field) + 1, 488 sizeof(char)); 489 strcpy(igp->i_field, field); 490 igp->i_link = tab->i_head[h]; 491 tab->i_head[h] = igp; 492 tab->i_count++; 493 } 494 return (0); 495 } 496 497 /* 498 * Print out all currently retained fields. 499 */ 500 int 501 igshow(tab, which) 502 struct ignoretab *tab; 503 const char *which; 504 { 505 int h; 506 struct ignore *igp; 507 char **ap, **ring; 508 509 if (tab->i_count == 0) { 510 printf("No fields currently being %s.\n", which); 511 return (0); 512 } 513 ring = (char **)salloc((tab->i_count + 1) * sizeof(char *)); 514 ap = ring; 515 for (h = 0; h < HSHSIZE; h++) 516 for (igp = tab->i_head[h]; igp != NULL; igp = igp->i_link) 517 *ap++ = igp->i_field; 518 *ap = 0; 519 qsort(ring, tab->i_count, sizeof(char *), igcomp); 520 for (ap = ring; *ap != 0; ap++) 521 printf("%s\n", *ap); 522 return (0); 523 } 524 525 /* 526 * Compare two names for sorting ignored field list. 527 */ 528 int 529 igcomp(l, r) 530 const void *l, *r; 531 { 532 533 return (strcmp(*(const char **)l, *(const char **)r)); 534 } 535