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. 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.
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
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 $
42 * Mail -- a mail program
47 extern int wait_status;
50 * If any arguments were given, go to the next applicable argument
51 * following dot, otherwise, go to the next applicable message.
52 * If given as first command with no arguments, print first message.
58 int *ip, *ip2, list[2], mdot;
63 * If some messages were supplied, find the
64 * first applicable one following dot using
68 mdot = dot - &message[0] + 1;
71 * Find the first message in the supplied
72 * message list which follows dot.
75 for (ip = msgvec; *ip != 0; ip++)
82 mp = &message[*ip2 - 1];
83 if ((mp->m_flag & MDELETED) == 0) {
92 printf("No messages applicable\n");
97 * If this is the first command, select message 1.
98 * Note that this must exist for us to get here at all.
105 * Just find the next good message after dot, no
109 for (mp = dot+1; mp < &message[msgCount]; mp++)
110 if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
112 if (mp >= &message[msgCount]) {
122 list[0] = dot - &message[0] + 1;
128 * Save a message in a file. Mark the message as saved
129 * so we can discard when the user quits.
134 return (save1(str, 1, "save", saveignore));
138 * Copy a message to a file without affected its saved-ness
143 return (save1(str, 0, "copy", saveignore));
147 * Save/copy the indicated messages at the end of the passed file name.
148 * If mark is true, mark the message "saved."
151 save1(char *str, int mark, const char *cmd, struct ignoretab *ignore)
159 msgvec = (int *)salloc((msgCount + 2) * sizeof(*msgvec));
160 if ((file = snarf(str, &f)) == NULL)
163 *msgvec = first(0, MMNORM);
165 printf("No messages to %s.\n", cmd);
170 if (f && getmsglist(str, msgvec, 0) < 0)
172 if ((file = expand(file)) == NULL)
174 printf("\"%s\" ", file);
176 if (access(file, 0) >= 0)
180 if ((obuf = Fopen(file, "a")) == NULL) {
184 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
185 mp = &message[*ip - 1];
187 if (sendmessage(mp, obuf, ignore, NULL) < 0) {
193 mp->m_flag |= MSAVED;
199 printf("%s\n", disp);
204 * Write the indicated messages at the end of the passed
205 * file name, minus header and trailing blank line.
210 return (save1(str, 1, "write", ignoreall));
214 * Snarf the file from the end of the command line and
215 * return a pointer to it. If there is no file attached,
216 * just return NULL. Put a null in front of the file
217 * name so that the message list processing won't see it,
218 * unless the file name is the only thing on the line, in
219 * which case, return 0 in the reference flag variable.
223 snarf(char *linebuf, int *flag)
228 cp = strlen(linebuf) + linebuf - 1;
231 * Strip away trailing blanks.
234 while (cp > linebuf && isspace((unsigned char)*cp))
239 * Now search for the beginning of the file name.
242 while (cp > linebuf && !isspace((unsigned char)*cp))
245 printf("No file specified.\n");
248 if (isspace((unsigned char)*cp))
266 * Delete messages, then type the new dot.
274 lastdot = dot - &message[0] + 1;
275 if (delm(msgvec) >= 0) {
276 list[0] = dot - &message[0] + 1;
277 if (list[0] > lastdot) {
284 printf("No more messages\n");
289 * Delete the indicated messages.
290 * Set dot to some nice place afterwards.
291 * Internal interface.
300 for (ip = msgvec; *ip != 0; ip++) {
301 mp = &message[*ip - 1];
303 mp->m_flag |= MDELETED|MTOUCH;
304 mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
308 dot = &message[last-1];
309 last = first(0, MDELETED);
311 dot = &message[last-1];
321 * Following can't happen -- it keeps lint happy
328 * Undelete the indicated messages.
331 undelete_messages(int *msgvec)
336 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
337 mp = &message[*ip - 1];
340 mp->m_flag &= ~MDELETED;
346 * Interactively dump core on "core"
353 switch (pid = fork()) {
361 printf("Okie dokie");
364 if (WIFSIGNALED(wait_status) && WCOREDUMP(wait_status))
365 printf(" -- Core dumped.\n");
367 printf(" -- Can't dump core.\n");
372 * Clobber as many bytes of stack as the user requests.
382 times = (atoi(argv[0]) + 511) / 512;
398 for (cp = buf; cp < &buf[512]; *cp++ = 0xFF)
404 * Add the given header fields to the retained list.
405 * If no arguments, print the current list of retained fields.
408 retfield(char **list)
410 return (ignore1(list, ignore + 1, "retained"));
414 * Add the given header fields to the ignored list.
415 * If no arguments, print the current list of ignored fields.
420 return (ignore1(list, ignore, "ignored"));
424 saveretfield(char **list)
426 return (ignore1(list, saveignore + 1, "retained"));
430 saveigfield(char **list)
432 return (ignore1(list, saveignore, "ignored"));
436 ignore1(char **list, struct ignoretab *tab, const char *which)
438 char field[LINESIZE];
444 return (igshow(tab, which));
445 for (ap = list; *ap != NULL; ap++) {
446 istrncpy(field, *ap, sizeof(field));
447 if (member(field, tab))
450 igp = calloc(1, sizeof(struct ignore));
451 igp->i_field = calloc((unsigned)strlen(field) + 1,
453 strcpy(igp->i_field, field);
454 igp->i_link = tab->i_head[h];
455 tab->i_head[h] = igp;
462 * Print out all currently retained fields.
465 igshow(struct ignoretab *tab, const char *which)
471 if (tab->i_count == 0) {
472 printf("No fields currently being %s.\n", which);
475 ring = (char **)salloc((tab->i_count + 1) * sizeof(char *));
477 for (h = 0; h < HSHSIZE; h++)
478 for (igp = tab->i_head[h]; igp != NULL; igp = igp->i_link)
479 *ap++ = igp->i_field;
481 qsort(ring, tab->i_count, sizeof(char *), igcomp);
482 for (ap = ring; *ap != NULL; ap++)
488 * Compare two names for sorting ignored field list.
491 igcomp(const void *l, const void *r)
493 return (strcmp(*(const char **)l, *(const char **)r));