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
35 static char sccsid[] = "@(#)cmd2.c 8.1 (Berkeley) 6/6/93";
43 * Mail -- a mail program
49 * If any arguments were given, go to the next applicable argument
50 * following dot, otherwise, go to the next applicable message.
51 * If given as first command with no arguments, print first message.
57 register struct message *mp;
58 register int *ip, *ip2;
64 * If some messages were supplied, find the
65 * first applicable one following dot using
69 mdot = dot - &message[0] + 1;
72 * Find the first message in the supplied
73 * message list which follows dot.
76 for (ip = msgvec; *ip != 0; ip++)
83 mp = &message[*ip2 - 1];
84 if ((mp->m_flag & MDELETED) == 0) {
93 printf("No messages applicable\n");
98 * If this is the first command, select message 1.
99 * Note that this must exist for us to get here at all.
106 * Just find the next good message after dot, no
110 for (mp = dot+1; mp < &message[msgCount]; mp++)
111 if ((mp->m_flag & (MDELETED|MSAVED)) == 0)
113 if (mp >= &message[msgCount]) {
123 list[0] = dot - &message[0] + 1;
129 * Save a message in a file. Mark the message as saved
130 * so we can discard when the user quits.
137 return save1(str, 1, "save", saveignore);
141 * Copy a message to a file without affected its saved-ness
148 return save1(str, 0, "copy", saveignore);
152 * Save/copy the indicated messages at the end of the passed file name.
153 * If mark is true, mark the message "saved."
156 save1(str, mark, cmd, ignore)
160 struct ignoretab *ignore;
163 register struct message *mp;
168 msgvec = (int *) salloc((msgCount + 2) * sizeof *msgvec);
169 if ((file = snarf(str, &f)) == NOSTR)
172 *msgvec = first(0, MMNORM);
174 printf("No messages to %s.\n", cmd);
179 if (f && getmsglist(str, msgvec, 0) < 0)
181 if ((file = expand(file)) == NOSTR)
183 printf("\"%s\" ", file);
185 if (access(file, 0) >= 0)
189 if ((obuf = Fopen(file, "a")) == NULL) {
193 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
194 mp = &message[*ip - 1];
196 if (send(mp, obuf, ignore, NOSTR) < 0) {
202 mp->m_flag |= MSAVED;
208 printf("%s\n", disp);
213 * Write the indicated messages at the end of the passed
214 * file name, minus header and trailing blank line.
221 return save1(str, 1, "write", ignoreall);
225 * Snarf the file from the end of the command line and
226 * return a pointer to it. If there is no file attached,
227 * just return NOSTR. Put a null in front of the file
228 * name so that the message list processing won't see it,
229 * unless the file name is the only thing on the line, in
230 * which case, return 0 in the reference flag variable.
241 cp = strlen(linebuf) + linebuf - 1;
244 * Strip away trailing blanks.
247 while (cp > linebuf && isspace(*cp))
252 * Now search for the beginning of the file name.
255 while (cp > linebuf && !isspace(*cp))
258 printf("No file specified.\n");
280 * Delete messages, then type the new dot.
289 lastdot = dot - &message[0] + 1;
290 if (delm(msgvec) >= 0) {
291 list[0] = dot - &message[0] + 1;
292 if (list[0] > lastdot) {
299 printf("No more messages\n");
304 * Delete the indicated messages.
305 * Set dot to some nice place afterwards.
306 * Internal interface.
312 register struct message *mp;
317 for (ip = msgvec; *ip != 0; ip++) {
318 mp = &message[*ip - 1];
320 mp->m_flag |= MDELETED|MTOUCH;
321 mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
325 dot = &message[last-1];
326 last = first(0, MDELETED);
328 dot = &message[last-1];
338 * Following can't happen -- it keeps lint happy
345 * Undelete the indicated messages.
348 undelete_messages(msgvec)
351 register struct message *mp;
354 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
355 mp = &message[*ip - 1];
358 mp->m_flag &= ~MDELETED;
364 * Interactively dump core on "core"
370 extern union wait wait_status;
372 switch (pid = fork()) {
380 printf("Okie dokie");
383 if (wait_status.w_coredump)
384 printf(" -- Core dumped.\n");
386 printf(" -- Can't dump core.\n");
391 * Clobber as many bytes of stack as the user requests.
402 times = (atoi(argv[0]) + 511) / 512;
419 for (cp = buf; cp < &buf[512]; *cp++ = 0xFF)
425 * Add the given header fields to the retained list.
426 * If no arguments, print the current list of retained fields.
433 return ignore1(list, ignore + 1, "retained");
437 * Add the given header fields to the ignored list.
438 * If no arguments, print the current list of ignored fields.
445 return ignore1(list, ignore, "ignored");
453 return ignore1(list, saveignore + 1, "retained");
461 return ignore1(list, saveignore, "ignored");
465 ignore1(list, tab, which)
467 struct ignoretab *tab;
472 register struct ignore *igp;
476 return igshow(tab, which);
477 for (ap = list; *ap != 0; ap++) {
479 if (member(field, tab))
482 igp = (struct ignore *) calloc(1, sizeof (struct ignore));
483 igp->i_field = calloc((unsigned) strlen(field) + 1,
485 strcpy(igp->i_field, field);
486 igp->i_link = tab->i_head[h];
487 tab->i_head[h] = igp;
494 * Print out all currently retained fields.
498 struct ignoretab *tab;
506 if (tab->i_count == 0) {
507 printf("No fields currently being %s.\n", which);
510 ring = (char **) salloc((tab->i_count + 1) * sizeof (char *));
512 for (h = 0; h < HSHSIZE; h++)
513 for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
514 *ap++ = igp->i_field;
516 qsort(ring, tab->i_count, sizeof (char *), igcomp);
517 for (ap = ring; *ap != 0; ap++)
523 * Compare two names for sorting ignored field list.
529 return (strcmp(*(char **)l, *(char **)r));