2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)cmd2.c 8.1 (Berkeley) 6/6/93";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
43 * Mail -- a mail program
48 extern int wait_status;
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.
60 int *ip, *ip2, list[2], mdot;
65 * If some messages were supplied, find the
66 * first applicable one following dot using
70 mdot = dot - &message[0] + 1;
73 * Find the first message in the supplied
74 * message list which follows dot.
77 for (ip = msgvec; *ip != 0; ip++)
84 mp = &message[*ip2 - 1];
85 if ((mp->m_flag & MDELETED) == 0) {
94 printf("No messages applicable\n");
99 * If this is the first command, select message 1.
100 * Note that this must exist for us to get here at all.
107 * Just find the next good message after dot, no
111 for (mp = dot+1; mp < &message[msgCount]; mp++)
112 if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
114 if (mp >= &message[msgCount]) {
124 list[0] = dot - &message[0] + 1;
130 * Save a message in a file. Mark the message as saved
131 * so we can discard when the user quits.
138 return (save1(str, 1, "save", saveignore));
142 * Copy a message to a file without affected its saved-ness
149 return (save1(str, 0, "copy", saveignore));
153 * Save/copy the indicated messages at the end of the passed file name.
154 * If mark is true, mark the message "saved."
157 save1(char str[], int mark, const char *cmd, struct ignoretab *ignore)
165 msgvec = (int *)salloc((msgCount + 2) * sizeof(*msgvec));
166 if ((file = snarf(str, &f)) == NULL)
169 *msgvec = first(0, MMNORM);
171 printf("No messages to %s.\n", cmd);
176 if (f && getmsglist(str, msgvec, 0) < 0)
178 if ((file = expand(file)) == NULL)
180 printf("\"%s\" ", file);
181 (void)fflush(stdout);
182 if (access(file, 0) >= 0)
186 if ((obuf = Fopen(file, "a")) == NULL) {
190 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
191 mp = &message[*ip - 1];
193 if (sendmessage(mp, obuf, ignore, NULL) < 0) {
199 mp->m_flag |= MSAVED;
205 printf("%s\n", disp);
210 * Write the indicated messages at the end of the passed
211 * file name, minus header and trailing blank line.
218 return (save1(str, 1, "write", ignoreall));
222 * Snarf the file from the end of the command line and
223 * return a pointer to it. If there is no file attached,
224 * just return NULL. Put a null in front of the file
225 * name so that the message list processing won't see it,
226 * unless the file name is the only thing on the line, in
227 * which case, return 0 in the reference flag variable.
231 snarf(char *linebuf, int *flag)
236 cp = strlen(linebuf) + linebuf - 1;
239 * Strip away trailing blanks.
242 while (cp > linebuf && isspace((unsigned char)*cp))
247 * Now search for the beginning of the file name.
250 while (cp > linebuf && !isspace((unsigned char)*cp))
253 printf("No file specified.\n");
256 if (isspace((unsigned char)*cp))
276 * Delete messages, then type the new dot.
285 lastdot = dot - &message[0] + 1;
286 if (delm(msgvec) >= 0) {
287 list[0] = dot - &message[0] + 1;
288 if (list[0] > lastdot) {
295 printf("No more messages\n");
300 * Delete the indicated messages.
301 * Set dot to some nice place afterwards.
302 * Internal interface.
311 for (ip = msgvec; *ip != 0; ip++) {
312 mp = &message[*ip - 1];
314 mp->m_flag |= MDELETED|MTOUCH;
315 mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
319 dot = &message[last-1];
320 last = first(0, MDELETED);
322 dot = &message[last-1];
332 * Following can't happen -- it keeps lint happy
339 * Undelete the indicated messages.
348 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
349 mp = &message[*ip - 1];
352 mp->m_flag &= ~MDELETED;
358 * Interactively dump core on "core"
365 switch (pid = fork()) {
373 printf("Okie dokie");
374 (void)fflush(stdout);
376 if (WIFSIGNALED(wait_status) && WCOREDUMP(wait_status))
377 printf(" -- Core dumped.\n");
379 printf(" -- Can't dump core.\n");
384 * Clobber as many bytes of stack as the user requests.
394 times = (atoi(argv[0]) + 511) / 512;
410 for (cp = buf; cp < &buf[512]; *cp++ = 0xFF)
416 * Add the given header fields to the retained list.
417 * If no arguments, print the current list of retained fields.
424 return (ignore1(list, ignore + 1, "retained"));
428 * Add the given header fields to the ignored list.
429 * If no arguments, print the current list of ignored fields.
436 return (ignore1(list, ignore, "ignored"));
440 saveretfield(void *v)
444 return (ignore1(list, saveignore + 1, "retained"));
452 return (ignore1(list, saveignore, "ignored"));
456 ignore1(char **list, struct ignoretab *tab, const char *which)
458 char field[LINESIZE];
464 return (igshow(tab, which));
465 for (ap = list; *ap != 0; ap++) {
466 istrncpy(field, *ap, sizeof(field));
467 if (member(field, tab))
470 igp = calloc(1, sizeof(struct ignore));
471 igp->i_field = calloc((unsigned)strlen(field) + 1,
473 strcpy(igp->i_field, field);
474 igp->i_link = tab->i_head[h];
475 tab->i_head[h] = igp;
482 * Print out all currently retained fields.
485 igshow(struct ignoretab *tab, const char *which)
491 if (tab->i_count == 0) {
492 printf("No fields currently being %s.\n", which);
495 ring = (char **)salloc((tab->i_count + 1) * sizeof(char *));
497 for (h = 0; h < HSHSIZE; h++)
498 for (igp = tab->i_head[h]; igp != NULL; igp = igp->i_link)
499 *ap++ = igp->i_field;
501 qsort(ring, tab->i_count, sizeof(char *), igcomp);
502 for (ap = ring; *ap != 0; ap++)
508 * Compare two names for sorting ignored field list.
511 igcomp(const void *l, const void *r)
514 return (strcmp(*(const char **)l, *(const char **)r));