2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
34 static char sccsid[] = "@(#)cmd2.c 8.1 (Berkeley) 6/6/93";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
45 * Mail -- a mail program
50 extern int wait_status;
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.
62 int *ip, *ip2, list[2], mdot;
67 * If some messages were supplied, find the
68 * first applicable one following dot using
72 mdot = dot - &message[0] + 1;
75 * Find the first message in the supplied
76 * message list which follows dot.
79 for (ip = msgvec; *ip != 0; ip++)
86 mp = &message[*ip2 - 1];
87 if ((mp->m_flag & MDELETED) == 0) {
96 printf("No messages applicable\n");
101 * If this is the first command, select message 1.
102 * Note that this must exist for us to get here at all.
109 * Just find the next good message after dot, no
113 for (mp = dot+1; mp < &message[msgCount]; mp++)
114 if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
116 if (mp >= &message[msgCount]) {
126 list[0] = dot - &message[0] + 1;
132 * Save a message in a file. Mark the message as saved
133 * so we can discard when the user quits.
140 return (save1(str, 1, "save", saveignore));
144 * Copy a message to a file without affected its saved-ness
151 return (save1(str, 0, "copy", saveignore));
155 * Save/copy the indicated messages at the end of the passed file name.
156 * If mark is true, mark the message "saved."
159 save1(char str[], int mark, const char *cmd, struct ignoretab *ignore)
167 msgvec = (int *)salloc((msgCount + 2) * sizeof(*msgvec));
168 if ((file = snarf(str, &f)) == NULL)
171 *msgvec = first(0, MMNORM);
173 printf("No messages to %s.\n", cmd);
178 if (f && getmsglist(str, msgvec, 0) < 0)
180 if ((file = expand(file)) == NULL)
182 printf("\"%s\" ", file);
183 (void)fflush(stdout);
184 if (access(file, 0) >= 0)
188 if ((obuf = Fopen(file, "a")) == NULL) {
192 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
193 mp = &message[*ip - 1];
195 if (sendmessage(mp, obuf, ignore, NULL) < 0) {
201 mp->m_flag |= MSAVED;
207 printf("%s\n", disp);
212 * Write the indicated messages at the end of the passed
213 * file name, minus header and trailing blank line.
220 return (save1(str, 1, "write", ignoreall));
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.
233 snarf(char *linebuf, int *flag)
238 cp = strlen(linebuf) + linebuf - 1;
241 * Strip away trailing blanks.
244 while (cp > linebuf && isspace((unsigned char)*cp))
249 * Now search for the beginning of the file name.
252 while (cp > linebuf && !isspace((unsigned char)*cp))
255 printf("No file specified.\n");
258 if (isspace((unsigned char)*cp))
278 * Delete messages, then type the new dot.
287 lastdot = dot - &message[0] + 1;
288 if (delm(msgvec) >= 0) {
289 list[0] = dot - &message[0] + 1;
290 if (list[0] > lastdot) {
297 printf("No more messages\n");
302 * Delete the indicated messages.
303 * Set dot to some nice place afterwards.
304 * Internal interface.
313 for (ip = msgvec; *ip != 0; ip++) {
314 mp = &message[*ip - 1];
316 mp->m_flag |= MDELETED|MTOUCH;
317 mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
321 dot = &message[last-1];
322 last = first(0, MDELETED);
324 dot = &message[last-1];
334 * Following can't happen -- it keeps lint happy
341 * Undelete the indicated messages.
350 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
351 mp = &message[*ip - 1];
354 mp->m_flag &= ~MDELETED;
360 * Interactively dump core on "core"
367 switch (pid = fork()) {
375 printf("Okie dokie");
376 (void)fflush(stdout);
378 if (WIFSIGNALED(wait_status) && WCOREDUMP(wait_status))
379 printf(" -- Core dumped.\n");
381 printf(" -- Can't dump core.\n");
386 * Clobber as many bytes of stack as the user requests.
396 times = (atoi(argv[0]) + 511) / 512;
412 for (cp = buf; cp < &buf[512]; *cp++ = 0xFF)
418 * Add the given header fields to the retained list.
419 * If no arguments, print the current list of retained fields.
426 return (ignore1(list, ignore + 1, "retained"));
430 * Add the given header fields to the ignored list.
431 * If no arguments, print the current list of ignored fields.
438 return (ignore1(list, ignore, "ignored"));
442 saveretfield(void *v)
446 return (ignore1(list, saveignore + 1, "retained"));
454 return (ignore1(list, saveignore, "ignored"));
458 ignore1(char **list, struct ignoretab *tab, const char *which)
460 char field[LINESIZE];
466 return (igshow(tab, which));
467 for (ap = list; *ap != 0; ap++) {
468 istrncpy(field, *ap, sizeof(field));
469 if (member(field, tab))
472 igp = calloc(1, sizeof(struct ignore));
473 igp->i_field = calloc((unsigned)strlen(field) + 1,
475 strcpy(igp->i_field, field);
476 igp->i_link = tab->i_head[h];
477 tab->i_head[h] = igp;
484 * Print out all currently retained fields.
487 igshow(struct ignoretab *tab, const char *which)
493 if (tab->i_count == 0) {
494 printf("No fields currently being %s.\n", which);
497 ring = (char **)salloc((tab->i_count + 1) * sizeof(char *));
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;
503 qsort(ring, tab->i_count, sizeof(char *), igcomp);
504 for (ap = ring; *ap != 0; ap++)
510 * Compare two names for sorting ignored field list.
513 igcomp(const void *l, const void *r)
516 return (strcmp(*(const char **)l, *(const char **)r));