mk-mk.in: EXT_CFLAGS: remove .eh frames!..
[s-mailx.git] / cmd2.c
blob036b6fa2268fa63597ca76fe1cc700bee5950dfc
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ More user commands.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2013 Steffen "Daode" Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
40 #include "nail.h"
42 #include <sys/wait.h>
44 static int save1(char *str, int domark, char const *cmd,
45 struct ignoretab *ignore, int convert,
46 int sender_record, int domove);
47 static char * snarf(char *linebuf, int *flag, int usembox);
48 static int delm(int *msgvec);
49 #ifdef HAVE_ASSERTS
50 static void clob1(int n);
51 #endif
52 static int ignore1(char **list, struct ignoretab *tab, char const *which);
53 static int igshow(struct ignoretab *tab, char const *which);
54 static int igcomp(const void *l, const void *r);
55 static void unignore_one(const char *name, struct ignoretab *tab);
56 static int unignore1(char **list, struct ignoretab *tab,
57 char const *which);
60 * If any arguments were given, go to the next applicable argument
61 * following dot, otherwise, go to the next applicable message.
62 * If given as first command with no arguments, print first message.
64 int
65 next(void *v)
67 int *msgvec = v;
68 struct message *mp;
69 int *ip, *ip2;
70 int list[2], mdot;
72 if (*msgvec != 0) {
75 * If some messages were supplied, find the
76 * first applicable one following dot using
77 * wrap around.
80 mdot = dot - &message[0] + 1;
83 * Find the first message in the supplied
84 * message list which follows dot.
87 for (ip = msgvec; *ip != 0; ip++) {
88 #ifdef _CRAY
90 * Work around an optimizer bug in Cray Standard C Version 4.0.3 (057126).
91 * Otherwise, SIGFPE is received when mb.mb_threaded != 0.
93 #pragma _CRI suppress ip
94 #endif /* _CRAY */
95 if (mb.mb_threaded ? message[*ip-1].m_threadpos >
96 dot->m_threadpos :
97 *ip > mdot)
98 break;
100 if (*ip == 0)
101 ip = msgvec;
102 ip2 = ip;
103 do {
104 mp = &message[*ip2 - 1];
105 if ((mp->m_flag & (MDELETED|MHIDDEN)) == 0) {
106 setdot(mp);
107 goto hitit;
109 if (*ip2 != 0)
110 ip2++;
111 if (*ip2 == 0)
112 ip2 = msgvec;
113 } while (ip2 != ip);
114 printf(tr(21, "No messages applicable\n"));
115 return(1);
119 * If this is the first command, select message 1.
120 * Note that this must exist for us to get here at all.
123 if (!sawcom) {
124 if (msgCount == 0)
125 goto ateof;
126 goto hitit;
130 * Just find the next good message after dot, no
131 * wraparound.
134 if (mb.mb_threaded == 0) {
135 for (mp = dot + did_print_dot; mp < &message[msgCount]; mp++)
136 if ((mp->m_flag & (MDELETED|MSAVED|MHIDDEN)) == 0)
137 break;
138 } else {
139 mp = dot;
140 if (did_print_dot)
141 mp = next_in_thread(mp);
142 while (mp && mp->m_flag & (MDELETED|MSAVED|MHIDDEN))
143 mp = next_in_thread(mp);
145 if (mp == NULL || mp >= &message[msgCount]) {
146 ateof:
147 printf(tr(22, "At EOF\n"));
148 return(0);
150 setdot(mp);
151 hitit:
153 * Print dot.
156 list[0] = dot - &message[0] + 1;
157 list[1] = 0;
158 return(type(list));
162 * Save a message in a file. Mark the message as saved
163 * so we can discard when the user quits.
165 int
166 save(void *v)
168 char *str = v;
170 return save1(str, 1, "save", saveignore, SEND_MBOX, 0, 0);
173 int
174 Save(void *v)
176 char *str = v;
178 return save1(str, 1, "save", saveignore, SEND_MBOX, 1, 0);
182 * Copy a message to a file without affected its saved-ness
184 int
185 copycmd(void *v)
187 char *str = v;
189 return save1(str, 0, "copy", saveignore, SEND_MBOX, 0, 0);
192 int
193 Copycmd(void *v)
195 char *str = v;
197 return save1(str, 0, "copy", saveignore, SEND_MBOX, 1, 0);
201 * Move a message to a file.
203 int
204 cmove(void *v)
206 char *str = v;
208 return save1(str, 0, "move", saveignore, SEND_MBOX, 0, 1);
211 int
212 cMove(void *v)
214 char *str = v;
216 return save1(str, 0, "move", saveignore, SEND_MBOX, 1, 1);
220 * Decrypt and copy a message to a file.
222 int
223 cdecrypt(void *v)
225 char *str = v;
227 return save1(str, 0, "decrypt", saveignore, SEND_DECRYPT, 0, 0);
230 int
231 cDecrypt(void *v)
233 char *str = v;
235 return save1(str, 0, "decrypt", saveignore, SEND_DECRYPT, 1, 0);
239 * Save/copy the indicated messages at the end of the passed file name.
240 * If mark is true, mark the message "saved."
242 static int
243 save1(char *str, int domark, char const *cmd, struct ignoretab *ignoret,
244 int convert, int sender_record, int domove)
246 off_t mstats[2], tstats[2];
247 struct stat st;
248 int newfile = 0, compressed = 0, success = 1, last = 0, f, *msgvec, *ip;
249 struct message *mp;
250 char *file = NULL, *cp, *cq;
251 char const *disp = "";
252 FILE *obuf;
253 enum protocol prot;
255 /*LINTED*/
256 msgvec = (int *)salloc((msgCount + 2) * sizeof *msgvec);
257 if (sender_record) {
258 for (cp = str; *cp && blankchar(*cp & 0377); cp++);
259 f = (*cp != '\0');
260 } else {
261 if ((file = snarf(str, &f, convert != SEND_TOFILE)) == NULL)
262 return(1);
264 if (!f) {
265 *msgvec = first(0, MMNORM);
266 if (*msgvec == 0) {
267 if (inhook)
268 return 0;
269 printf(tr(23, "No messages to %s.\n"), cmd);
270 return(1);
272 msgvec[1] = 0;
274 if (f && getmsglist(str, msgvec, 0) < 0)
275 return(1);
276 if (*msgvec == 0) {
277 if (inhook)
278 return 0;
279 printf("No applicable messages.\n");
280 return 1;
282 if (sender_record) {
283 if ((cp = nameof(&message[*msgvec - 1], 0)) == NULL) {
284 printf(tr(24,
285 "Cannot determine message sender to %s.\n"),
286 cmd);
287 return 1;
289 for (cq = cp; *cq && *cq != '@'; cq++);
290 *cq = '\0';
291 if (value("outfolder")) {
292 size_t sz = strlen(cp) + 1;
293 file = salloc(sz + 1);
294 file[0] = '+';
295 memcpy(file + 1, cp, sz);
296 } else
297 file = cp;
299 if ((file = expand(file)) == NULL)
300 return (1);
301 prot = which_protocol(file);
302 if (prot != PROTO_IMAP) {
303 if (access(file, 0) >= 0) {
304 newfile = 0;
305 disp = tr(25, "[Appended]");
306 } else {
307 newfile = 1;
308 disp = tr(26, "[New file]");
311 if ((obuf = convert == SEND_TOFILE ? Fopen(file, "a+") :
312 Zopen(file, "a+", &compressed)) == NULL) {
313 if ((obuf = convert == SEND_TOFILE ? Fopen(file, "wx") :
314 Zopen(file, "wx", &compressed)) == NULL) {
315 perror(file);
316 return(1);
318 } else {
319 if (compressed) {
320 newfile = 0;
321 disp = tr(25, "[Appended]");
323 if (!newfile && fstat(fileno(obuf), &st) &&
324 S_ISREG(st.st_mode) &&
325 fseek(obuf, -2L, SEEK_END) == 0) {
326 char buf[2];
327 int prependnl = 0;
329 switch (fread(buf, sizeof *buf, 2, obuf)) {
330 case 2:
331 if (buf[1] != '\n') {
332 prependnl = 1;
333 break;
335 /*FALLTHRU*/
336 case 1:
337 if (buf[0] != '\n')
338 prependnl = 1;
339 break;
340 default:
341 if (ferror(obuf)) {
342 perror(file);
343 return(1);
345 prependnl = 0;
347 fflush(obuf);
348 if (prependnl) {
349 putc('\n', obuf);
350 fflush(obuf);
354 tstats[0] = tstats[1] = 0;
355 imap_created_mailbox = 0;
356 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
357 mp = &message[*ip - 1];
358 if (prot == PROTO_IMAP &&
359 ignoret[0].i_count == 0 &&
360 ignoret[1].i_count == 0
361 #ifdef HAVE_IMAP /* TODO revisit */
362 && imap_thisaccount(file)
363 #endif
365 #ifdef HAVE_IMAP
366 if (imap_copy(mp, *ip, file) == STOP)
367 #endif
368 goto ferr;
369 #ifdef HAVE_IMAP
370 mstats[0] = -1;
371 mstats[1] = mp->m_xsize;
372 #endif
373 } else if (sendmp(mp, obuf, ignoret, NULL,
374 convert, mstats) < 0) {
375 perror(file);
376 goto ferr;
378 touch(mp);
379 if (domark)
380 mp->m_flag |= MSAVED;
381 if (domove) {
382 mp->m_flag |= MDELETED|MSAVED;
383 last = *ip;
385 tstats[0] += mstats[0];
386 tstats[1] += mstats[1];
388 fflush(obuf);
389 if (ferror(obuf)) {
390 perror(file);
391 ferr: success = 0;
393 if (Fclose(obuf) != 0)
394 success = 0;
395 if (success) {
396 if (prot == PROTO_IMAP || prot == PROTO_MAILDIR) {
397 disp = (
398 #ifdef HAVE_IMAP
399 ((prot == PROTO_IMAP) && disconnected(file))
400 ? "[Queued]" :
401 #endif
402 (imap_created_mailbox ? "[New file]"
403 : "[Appended]"));
405 printf("\"%s\" %s ", file, disp);
406 if (tstats[0] >= 0)
407 printf("%lu", (long)tstats[0]);
408 else
409 printf(tr(27, "binary"));
410 printf("/%lu\n", (long)tstats[1]);
411 } else if (domark) {
412 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
413 mp = &message[*ip - 1];
414 mp->m_flag &= ~MSAVED;
416 } else if (domove) {
417 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
418 mp = &message[*ip - 1];
419 mp->m_flag &= ~(MSAVED|MDELETED);
422 if (domove && last && success) {
423 setdot(&message[last-1]);
424 last = first(0, MDELETED);
425 setdot(&message[last ? last-1 : 0]);
427 return(success == 0);
431 * Write the indicated messages at the end of the passed
432 * file name, minus header and trailing blank line.
433 * This is the MIME save function.
435 int
436 cwrite(void *v)
438 char *str = v;
440 if (str == NULL || *str == '\0')
441 str = savestr("/dev/null");
442 return (save1(str, 0, "write", allignore, SEND_TOFILE, 0, 0));
446 * Snarf the file from the end of the command line and
447 * return a pointer to it. If there is no file attached,
448 * return the mbox file. Put a null in front of the file
449 * name so that the message list processing won't see it,
450 * unless the file name is the only thing on the line, in
451 * which case, return 0 in the reference flag variable.
453 static char *
454 snarf(char *linebuf, int *flag, int usembox)
456 char *cp;
458 *flag = 1;
459 if ((cp = laststring(linebuf, flag, 0)) == NULL) {
460 if (usembox) {
461 *flag = 0;
462 cp = expand("&");
463 } else
464 fprintf(stderr, tr(28, "No file specified.\n"));
466 return (cp);
470 * Delete messages.
472 int
473 delete(void *v)
475 int *msgvec = v;
476 delm(msgvec);
477 return 0;
481 * Delete messages, then type the new dot.
483 int
484 deltype(void *v)
486 int *msgvec = v;
487 int list[2];
488 int lastdot;
490 lastdot = dot - &message[0] + 1;
491 if (delm(msgvec) >= 0) {
492 list[0] = dot - &message[0] + 1;
493 if (list[0] > lastdot) {
494 touch(dot);
495 list[1] = 0;
496 return(type(list));
498 printf(tr(29, "At EOF\n"));
499 } else
500 printf(tr(30, "No more messages\n"));
501 return(0);
505 * Delete the indicated messages.
506 * Set dot to some nice place afterwards.
507 * Internal interface.
509 static int
510 delm(int *msgvec)
512 struct message *mp;
513 int *ip;
514 int last;
516 last = 0;
517 for (ip = msgvec; *ip != 0; ip++) {
518 mp = &message[*ip - 1];
519 touch(mp);
520 mp->m_flag |= MDELETED|MTOUCH;
521 mp->m_flag &= ~(MPRESERVE|MSAVED|MBOX);
522 last = *ip;
524 if (last != 0) {
525 setdot(&message[last-1]);
526 last = first(0, MDELETED);
527 if (last != 0) {
528 setdot(&message[last-1]);
529 return(0);
531 else {
532 setdot(&message[0]);
533 return(-1);
538 * Following can't happen -- it keeps lint happy
541 return(-1);
545 * Undelete the indicated messages.
547 int
548 undeletecmd(void *v)
550 int *msgvec = v;
551 struct message *mp;
552 int *ip;
554 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
555 mp = &message[*ip - 1];
556 touch(mp);
557 setdot(mp);
558 if (mp->m_flag & (MDELETED|MSAVED))
559 mp->m_flag &= ~(MDELETED|MSAVED);
560 else
561 mp->m_flag &= ~MDELETED;
562 #ifdef HAVE_IMAP
563 if (mb.mb_type == MB_IMAP || mb.mb_type == MB_CACHE)
564 imap_undelete(mp, *ip);
565 #endif
567 return 0;
570 #ifdef HAVE_ASSERTS
572 * Interactively dump core on "core"
574 /*ARGSUSED*/
575 int
576 core(void *v)
578 int pid;
579 # ifdef WCOREDUMP
580 extern int wait_status;
581 # endif
582 (void)v;
584 switch (pid = fork()) {
585 case -1:
586 perror("fork");
587 return (1);
588 case 0:
589 abort();
590 _exit(1);
592 (void)printf(tr(31, "Okie dokie"));
593 (void)fflush(stdout);
594 (void)wait_child(pid);
595 # ifdef WCOREDUMP
596 if (WCOREDUMP(wait_status))
597 (void)printf(tr(32, " -- Core dumped.\n"));
598 else
599 (void)printf(tr(33, " -- Can't dump core.\n"));
600 # endif
601 return (0);
604 static void
605 clob1(int n)
607 char buf[512], *cp;
609 if (n <= 0)
610 return;
611 for (cp = buf; cp < &buf[512]; *cp++ = (char)0xFF)
613 clob1(n - 1);
617 * Clobber as many bytes of stack as the user requests.
619 int
620 clobber(void *v)
622 char **argv = v;
623 int times;
625 if (argv[0] == 0)
626 times = 1;
627 else
628 times = (atoi(argv[0]) + 511) / 512;
629 clob1(times);
630 return (0);
632 #endif /* HAVE_ASSERTS */
635 * Add the given header fields to the retained list.
636 * If no arguments, print the current list of retained fields.
638 int
639 retfield(void *v)
641 char **list = v;
643 return ignore1(list, ignore + 1, "retained");
647 * Add the given header fields to the ignored list.
648 * If no arguments, print the current list of ignored fields.
650 int
651 igfield(void *v)
653 char **list = v;
655 return ignore1(list, ignore, "ignored");
658 int
659 saveretfield(void *v)
661 char **list = v;
663 return ignore1(list, saveignore + 1, "retained");
666 int
667 saveigfield(void *v)
669 char **list = v;
671 return ignore1(list, saveignore, "ignored");
674 int
675 fwdretfield(void *v)
677 char **list = v;
679 return ignore1(list, fwdignore + 1, "retained");
682 int
683 fwdigfield(void *v)
685 char **list = v;
687 return ignore1(list, fwdignore, "ignored");
690 static int
691 ignore1(char **list, struct ignoretab *tab, char const *which)
693 int h;
694 struct ignore *igp;
695 char **ap;
697 if (*list == NULL)
698 return igshow(tab, which);
699 for (ap = list; *ap != 0; ap++) {
700 char *field;
701 size_t sz;
703 sz = strlen(*ap);
704 field = ac_alloc(sz + 1);
705 i_strcpy(field, *ap, sz + 1);
706 field[sz]='\0';
707 if (member(field, tab)) {
708 ac_free(field);
709 continue;
711 h = hash(field);
712 igp = (struct ignore *)scalloc(1, sizeof (struct ignore));
713 sz = strlen(field) + 1;
714 igp->i_field = smalloc(sz);
715 memcpy(igp->i_field, field, sz);
716 igp->i_link = tab->i_head[h];
717 tab->i_head[h] = igp;
718 tab->i_count++;
719 ac_free(field);
721 return 0;
725 * Print out all currently retained fields.
727 static int
728 igshow(struct ignoretab *tab, char const *which)
730 int h;
731 struct ignore *igp;
732 char **ap, **ring;
734 if (tab->i_count == 0) {
735 printf(tr(34, "No fields currently being %s.\n"), which);
736 return 0;
738 /*LINTED*/
739 ring = (char **)salloc((tab->i_count + 1) * sizeof (char *));
740 ap = ring;
741 for (h = 0; h < HSHSIZE; h++)
742 for (igp = tab->i_head[h]; igp != 0; igp = igp->i_link)
743 *ap++ = igp->i_field;
744 *ap = 0;
745 qsort(ring, tab->i_count, sizeof (char *), igcomp);
746 for (ap = ring; *ap != 0; ap++)
747 printf("%s\n", *ap);
748 return 0;
752 * Compare two names for sorting ignored field list.
754 static int
755 igcomp(const void *l, const void *r)
757 return (strcmp(*(char**)UNCONST(l), *(char**)UNCONST(r)));
760 int
761 unignore(void *v)
763 return unignore1((char **)v, ignore, "ignored");
766 int
767 unretain(void *v)
769 return unignore1((char **)v, ignore + 1, "retained");
772 int
773 unsaveignore(void *v)
775 return unignore1((char **)v, saveignore, "ignored");
778 int
779 unsaveretain(void *v)
781 return unignore1((char **)v, saveignore + 1, "retained");
784 int
785 unfwdignore(void *v)
787 return unignore1((char **)v, fwdignore, "ignored");
790 int
791 unfwdretain(void *v)
793 return unignore1((char **)v, fwdignore + 1, "retained");
796 static void
797 unignore_one(const char *name, struct ignoretab *tab)
799 struct ignore *ip, *iq = NULL;
800 int h = hash(name);
802 for (ip = tab->i_head[h]; ip; ip = ip->i_link) {
803 if (asccasecmp(ip->i_field, name) == 0) {
804 free(ip->i_field);
805 if (iq != NULL)
806 iq->i_link = ip->i_link;
807 else
808 tab->i_head[h] = ip->i_link;
809 free(ip);
810 tab->i_count--;
811 break;
813 iq = ip;
817 static int
818 unignore1(char **list, struct ignoretab *tab, char const *which)
820 if (tab->i_count == 0) {
821 printf(tr(34, "No fields currently being %s.\n"), which);
822 return 0;
824 while (*list)
825 unignore_one(*list++, tab);
826 return 0;