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