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