]>
git.cameronkatri.com Git - apple_cmds.git/blob - mail_cmds/mail/cmd2.c
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
36 static char sccsid
[] = "@(#)cmd2.c 8.1 (Berkeley) 6/6/93";
38 static const char rcsid
[] =
39 "$FreeBSD: src/usr.bin/mail/cmd2.c,v 1.9 2002/06/30 05:25:06 obrien Exp $";
42 #include <sys/cdefs.h>
49 * Mail -- a mail program
54 extern int wait_status
;
57 * If any arguments were given, go to the next applicable argument
58 * following dot, otherwise, go to the next applicable message.
59 * If given as first command with no arguments, print first message.
66 int *ip
, *ip2
, list
[2], mdot
;
71 * If some messages were supplied, find the
72 * first applicable one following dot using
76 mdot
= dot
- &message
[0] + 1;
79 * Find the first message in the supplied
80 * message list which follows dot.
83 for (ip
= msgvec
; *ip
!= 0; ip
++) {
84 if (!sawcom
) /* don't skip current message if first command */
93 mp
= &message
[*ip2
- 1];
94 if ((mp
->m_flag
& MDELETED
) == 0) {
103 printf("No messages applicable\n");
108 * If this is the first command, select message 1.
109 * Note that this must exist for us to get here at all.
116 * Just find the next good message after dot, no
120 for (mp
= dot
+1; mp
< &message
[msgCount
]; mp
++)
121 if ((mp
->m_flag
& (MDELETED
|MSAVED
)) == 0)
123 if (mp
>= &message
[msgCount
]) {
133 list
[0] = dot
- &message
[0] + 1;
139 * Save a message in a file. Mark the message as saved
140 * so we can discard when the user quits.
147 return (save1(str
, 1, "save", saveignore
));
151 * Save a message list in a file named after author. Mark the message as saved
152 * so we can discard when the user quits.
159 return (save1(str
, 1, "Save", saveignore
));
163 * Copy a message to a file without affected its saved-ness
170 return (save1(str
, 0, "copy", saveignore
));
174 * Copy a message to a file named after author without affected its saved-ness
182 printf("Copy command is not yet implemented\n");
185 return (save1(str
, 0, "Copy", saveignore
));
190 * Save/copy the indicated messages at the end of the passed file name.
191 * If mark is true, mark the message "saved."
194 save1(str
, mark
, cmd
, ignore
)
198 struct ignoretab
*ignore
;
205 int doing_Save
= !strcmp(cmd
,"Save");
207 msgvec
= (int *)salloc((msgCount
+ 2) * sizeof(*msgvec
));
208 if ((file
= snarf(str
, &f
, doing_Save
)) == NULL
)
211 *msgvec
= first(0, MMNORM
);
213 printf("No messages to %s.\n", cmd
);
218 if (f
&& getmsglist(str
, msgvec
, 0) < 0)
220 /* Save derives file name from author of first message: */
223 mp
= &message
[msgvec
[0] - 1];
225 if ((rcv
= skin(hfield("from", mp
))) == NULL
)
226 rcv
= skin(nameof(mp
, 1));
227 rcv
= getauthor(rcv
);
229 printf("Failed to find sender: Save not performed\n");
232 file
= rcv
; /* use it as file name */
234 if ((file
= expand(file
)) == NULL
)
236 printf("\"%s\" ", file
);
237 (void)fflush(stdout
);
238 if (access(file
, 0) >= 0)
242 if ((obuf
= Fopen(file
, "a")) == NULL
) {
246 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
247 mp
= &message
[*ip
- 1];
249 if (sendmessage(mp
, obuf
, ignore
, NULL
) < 0) {
255 mp
->m_flag
|= MSAVED
;
261 printf("%s\n", disp
);
266 * Write the indicated messages at the end of the passed
267 * file name, minus header and trailing blank line.
274 return (save1(str
, 1, "write", ignoreall
));
278 * Snarf the file from the end of the command line and
279 * return a pointer to it. If there is no file attached,
280 * just return NULL. Put a null in front of the file
281 * name so that the message list processing won't see it,
282 * unless the file name is the only thing on the line, in
283 * which case, return 0 in the reference flag variable.
287 snarf(linebuf
, flag
, doing_Save
)
295 cp
= strlen(linebuf
) + linebuf
- 1;
298 * Strip away trailing blanks.
301 while (cp
> linebuf
&& isspace((unsigned char)*cp
))
306 * Now search for the beginning of the file name.
309 while (cp
> linebuf
&& !isspace((unsigned char)*cp
))
313 printf("No file specified: using MBOX.\n");
314 *flag
= 0; /* no message list found either */
318 if (isspace((unsigned char)*cp
))
321 if (!doing_Save
) *flag
= 0;
339 * Delete messages, then type the new dot.
348 lastdot
= dot
- &message
[0] + 1;
349 if (delm(msgvec
) >= 0) {
350 list
[0] = dot
- &message
[0] + 1;
351 if (list
[0] > lastdot
) {
358 printf("No more messages\n");
363 * Delete the indicated messages.
364 * Set dot to some nice place afterwards.
365 * Internal interface.
375 for (ip
= msgvec
; *ip
!= 0; ip
++) {
376 mp
= &message
[*ip
- 1];
378 mp
->m_flag
|= MDELETED
|MTOUCH
;
379 mp
->m_flag
&= ~(MPRESERVE
|MSAVED
|MBOX
);
383 dot
= &message
[last
-1];
384 last
= first(0, MDELETED
);
386 dot
= &message
[last
-1];
396 * Following can't happen -- it keeps lint happy
403 * Undelete the indicated messages.
406 undelete_messages(msgvec
)
412 for (ip
= msgvec
; *ip
&& ip
-msgvec
< msgCount
; ip
++) {
413 mp
= &message
[*ip
- 1];
416 mp
->m_flag
&= ~MDELETED
;
422 * Interactively dump core on "core"
429 switch (pid
= fork()) {
437 printf("Okie dokie");
438 (void)fflush(stdout
);
440 if (WIFSIGNALED(wait_status
) && WCOREDUMP(wait_status
))
441 printf(" -- Core dumped.\n");
443 printf(" -- Can't dump core.\n");
448 * Clobber as many bytes of stack as the user requests.
459 times
= (atoi(argv
[0]) + 511) / 512;
476 for (cp
= buf
; cp
< &buf
[512]; *cp
++ = 0xFF)
482 * Add the given header fields to the retained list.
483 * If no arguments, print the current list of retained fields.
490 return (ignore1(list
, ignore
+ 1, "retained"));
494 * Add the given header fields to the ignored list.
495 * If no arguments, print the current list of ignored fields.
502 return (ignore1(list
, ignore
, "ignored"));
510 return (ignore1(list
, saveignore
+ 1, "retained"));
518 return (ignore1(list
, saveignore
, "ignored"));
522 ignore1(list
, tab
, which
)
524 struct ignoretab
*tab
;
527 char field
[LINESIZE
];
533 return (igshow(tab
, which
));
534 for (ap
= list
; *ap
!= 0; ap
++) {
535 istrncpy(field
, *ap
, sizeof(field
));
536 if (member(field
, tab
))
539 igp
= calloc(1, sizeof(struct ignore
));
540 igp
->i_field
= calloc((unsigned)strlen(field
) + 1,
542 strcpy(igp
->i_field
, field
);
543 igp
->i_link
= tab
->i_head
[h
];
544 tab
->i_head
[h
] = igp
;
551 * Print out all currently retained fields.
555 struct ignoretab
*tab
;
562 if (tab
->i_count
== 0) {
563 printf("No fields currently being %s.\n", which
);
566 ring
= (char **)salloc((tab
->i_count
+ 1) * sizeof(char *));
568 for (h
= 0; h
< HSHSIZE
; h
++)
569 for (igp
= tab
->i_head
[h
]; igp
!= NULL
; igp
= igp
->i_link
)
570 *ap
++ = igp
->i_field
;
572 qsort(ring
, tab
->i_count
, sizeof(char *), igcomp
);
573 for (ap
= ring
; *ap
!= 0; ap
++)
579 * Compare two names for sorting ignored field list.
586 return (strcmp(*(const char **)l
, *(const char **)r
));