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