Home | History | Annotate | Line # | Download | only in mail
cmd1.c revision 1.26
      1  1.26  christos /*	$NetBSD: cmd1.c,v 1.26 2006/10/21 21:37:20 christos Exp $	*/
      2   1.5  christos 
      3   1.1       cgd /*-
      4   1.3   deraadt  * Copyright (c) 1980, 1993
      5   1.3   deraadt  *	The Regents of the University of California.  All rights reserved.
      6   1.1       cgd  *
      7   1.1       cgd  * Redistribution and use in source and binary forms, with or without
      8   1.1       cgd  * modification, are permitted provided that the following conditions
      9   1.1       cgd  * are met:
     10   1.1       cgd  * 1. Redistributions of source code must retain the above copyright
     11   1.1       cgd  *    notice, this list of conditions and the following disclaimer.
     12   1.1       cgd  * 2. Redistributions in binary form must reproduce the above copyright
     13   1.1       cgd  *    notice, this list of conditions and the following disclaimer in the
     14   1.1       cgd  *    documentation and/or other materials provided with the distribution.
     15  1.21       agc  * 3. Neither the name of the University nor the names of its contributors
     16   1.1       cgd  *    may be used to endorse or promote products derived from this software
     17   1.1       cgd  *    without specific prior written permission.
     18   1.1       cgd  *
     19   1.1       cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     20   1.1       cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     21   1.1       cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     22   1.1       cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     23   1.1       cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     24   1.1       cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     25   1.1       cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     26   1.1       cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     27   1.1       cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     28   1.1       cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     29   1.1       cgd  * SUCH DAMAGE.
     30   1.1       cgd  */
     31   1.1       cgd 
     32  1.10     lukem #include <sys/cdefs.h>
     33   1.1       cgd #ifndef lint
     34   1.5  christos #if 0
     35   1.6       tls static char sccsid[] = "@(#)cmd1.c	8.2 (Berkeley) 4/20/95";
     36   1.5  christos #else
     37  1.26  christos __RCSID("$NetBSD: cmd1.c,v 1.26 2006/10/21 21:37:20 christos Exp $");
     38   1.5  christos #endif
     39   1.1       cgd #endif /* not lint */
     40   1.1       cgd 
     41   1.1       cgd #include "rcv.h"
     42   1.3   deraadt #include "extern.h"
     43  1.26  christos #ifdef MIME_SUPPORT
     44  1.26  christos #include "mime.h"
     45  1.26  christos #endif
     46   1.1       cgd 
     47   1.1       cgd /*
     48   1.1       cgd  * Mail -- a mail program
     49   1.1       cgd  *
     50   1.1       cgd  * User commands.
     51   1.1       cgd  */
     52   1.1       cgd 
     53   1.1       cgd /*
     54   1.1       cgd  * Print the current active headings.
     55   1.1       cgd  * Don't change dot if invoker didn't give an argument.
     56   1.1       cgd  */
     57   1.1       cgd 
     58   1.1       cgd static int screen;
     59   1.1       cgd 
     60   1.3   deraadt int
     61  1.15       wiz headers(void *v)
     62   1.1       cgd {
     63   1.5  christos 	int *msgvec = v;
     64  1.10     lukem 	int n, mesg, flag;
     65  1.10     lukem 	struct message *mp;
     66   1.1       cgd 	int size;
     67   1.1       cgd 
     68   1.1       cgd 	size = screensize();
     69   1.1       cgd 	n = msgvec[0];
     70   1.1       cgd 	if (n != 0)
     71   1.1       cgd 		screen = (n-1)/size;
     72   1.1       cgd 	if (screen < 0)
     73   1.1       cgd 		screen = 0;
     74   1.1       cgd 	mp = &message[screen * size];
     75   1.1       cgd 	if (mp >= &message[msgCount])
     76   1.1       cgd 		mp = &message[msgCount - size];
     77   1.1       cgd 	if (mp < &message[0])
     78   1.1       cgd 		mp = &message[0];
     79   1.1       cgd 	flag = 0;
     80   1.1       cgd 	mesg = mp - &message[0];
     81  1.26  christos 	if (dot != &message[n - 1])
     82   1.1       cgd 		dot = mp;
     83   1.1       cgd 	for (; mp < &message[msgCount]; mp++) {
     84   1.1       cgd 		mesg++;
     85   1.1       cgd 		if (mp->m_flag & MDELETED)
     86   1.1       cgd 			continue;
     87   1.1       cgd 		if (flag++ >= size)
     88   1.1       cgd 			break;
     89   1.1       cgd 		printhead(mesg);
     90   1.1       cgd 	}
     91   1.1       cgd 	if (flag == 0) {
     92  1.23  christos 		(void)printf("No more mail.\n");
     93   1.1       cgd 		return(1);
     94   1.1       cgd 	}
     95   1.1       cgd 	return(0);
     96   1.1       cgd }
     97   1.1       cgd 
     98   1.1       cgd /*
     99   1.1       cgd  * Scroll to the next/previous screen
    100   1.1       cgd  */
    101   1.3   deraadt int
    102  1.15       wiz scroll(void *v)
    103   1.1       cgd {
    104   1.5  christos 	char *arg = v;
    105  1.10     lukem 	int s, size;
    106   1.1       cgd 	int cur[1];
    107   1.1       cgd 
    108   1.1       cgd 	cur[0] = 0;
    109   1.1       cgd 	size = screensize();
    110   1.1       cgd 	s = screen;
    111   1.1       cgd 	switch (*arg) {
    112   1.1       cgd 	case 0:
    113   1.1       cgd 	case '+':
    114   1.1       cgd 		s++;
    115  1.12      dean 		if (s * size >= msgCount) {
    116  1.23  christos 			(void)printf("On last screenful of messages\n");
    117   1.1       cgd 			return(0);
    118   1.1       cgd 		}
    119   1.1       cgd 		screen = s;
    120   1.1       cgd 		break;
    121   1.1       cgd 
    122   1.1       cgd 	case '-':
    123   1.1       cgd 		if (--s < 0) {
    124  1.23  christos 			(void)printf("On first screenful of messages\n");
    125   1.1       cgd 			return(0);
    126   1.1       cgd 		}
    127   1.1       cgd 		screen = s;
    128   1.1       cgd 		break;
    129   1.1       cgd 
    130   1.1       cgd 	default:
    131  1.23  christos 		(void)printf("Unrecognized scrolling command \"%s\"\n", arg);
    132   1.1       cgd 		return(1);
    133   1.1       cgd 	}
    134   1.1       cgd 	return(headers(cur));
    135   1.1       cgd }
    136   1.1       cgd 
    137   1.1       cgd /*
    138   1.1       cgd  * Compute screen size.
    139   1.1       cgd  */
    140   1.3   deraadt int
    141  1.15       wiz screensize(void)
    142   1.1       cgd {
    143   1.1       cgd 	int s;
    144   1.1       cgd 	char *cp;
    145   1.1       cgd 
    146  1.16       wiz 	if ((cp = value("screen")) != NULL && (s = atoi(cp)) > 0)
    147   1.1       cgd 		return s;
    148   1.1       cgd 	return screenheight - 4;
    149   1.1       cgd }
    150   1.1       cgd 
    151   1.1       cgd /*
    152   1.1       cgd  * Print out the headlines for each message
    153   1.1       cgd  * in the passed message list.
    154   1.1       cgd  */
    155   1.3   deraadt int
    156  1.15       wiz from(void *v)
    157   1.1       cgd {
    158   1.5  christos 	int *msgvec = v;
    159  1.10     lukem 	int *ip;
    160   1.1       cgd 
    161   1.8        pk 	for (ip = msgvec; *ip != 0; ip++)
    162   1.1       cgd 		printhead(*ip);
    163   1.1       cgd 	if (--ip >= msgvec)
    164   1.1       cgd 		dot = &message[*ip - 1];
    165   1.1       cgd 	return(0);
    166   1.1       cgd }
    167   1.1       cgd 
    168   1.1       cgd /*
    169   1.1       cgd  * Print out the header of a specific message.
    170   1.1       cgd  * This is a slight improvement to the standard one.
    171   1.1       cgd  */
    172   1.3   deraadt void
    173  1.15       wiz printhead(int mesg)
    174   1.1       cgd {
    175   1.1       cgd 	struct message *mp;
    176   1.1       cgd 	char headline[LINESIZE], wcount[LINESIZE], *subjline, dispc, curind;
    177   1.1       cgd 	char pbuf[BUFSIZ];
    178   1.1       cgd 	struct headline hl;
    179   1.1       cgd 	int subjlen;
    180   1.1       cgd 	char *name;
    181   1.1       cgd 
    182  1.26  christos 	mp = &message[mesg - 1];
    183  1.26  christos 	(void)mail_readline(setinput(mp), headline, LINESIZE);
    184  1.16       wiz 	if ((subjline = hfield("subject", mp)) == NULL)
    185   1.1       cgd 		subjline = hfield("subj", mp);
    186  1.26  christos 
    187   1.1       cgd 	/*
    188   1.1       cgd 	 * Bletch!
    189   1.1       cgd 	 */
    190   1.1       cgd 	curind = dot == mp ? '>' : ' ';
    191   1.1       cgd 	dispc = ' ';
    192   1.1       cgd 	if (mp->m_flag & MSAVED)
    193   1.1       cgd 		dispc = '*';
    194   1.1       cgd 	if (mp->m_flag & MPRESERVE)
    195   1.1       cgd 		dispc = 'P';
    196   1.1       cgd 	if ((mp->m_flag & (MREAD|MNEW)) == MNEW)
    197   1.1       cgd 		dispc = 'N';
    198   1.1       cgd 	if ((mp->m_flag & (MREAD|MNEW)) == 0)
    199   1.1       cgd 		dispc = 'U';
    200   1.1       cgd 	if (mp->m_flag & MBOX)
    201   1.1       cgd 		dispc = 'M';
    202   1.1       cgd 	parse(headline, &hl, pbuf);
    203  1.23  christos 	(void)snprintf(wcount, LINESIZE, "%3ld/%-5llu", mp->m_blines,
    204  1.23  christos 	    (unsigned long long)mp->m_size);
    205   1.1       cgd 	subjlen = screenwidth - 50 - strlen(wcount);
    206  1.16       wiz 	name = value("show-rcpt") != NULL ?
    207   1.1       cgd 		skin(hfield("to", mp)) : nameof(mp, 0);
    208  1.16       wiz 	if (subjline == NULL || subjlen < 0)		/* pretty pathetic */
    209  1.23  christos 		(void)printf("%c%c%3d %-20.20s  %16.16s %s\n",
    210   1.1       cgd 			curind, dispc, mesg, name, hl.l_date, wcount);
    211   1.1       cgd 	else
    212  1.23  christos 		(void)printf("%c%c%3d %-20.20s  %16.16s %s \"%.*s\"\n",
    213   1.1       cgd 			curind, dispc, mesg, name, hl.l_date, wcount,
    214   1.1       cgd 			subjlen, subjline);
    215   1.1       cgd }
    216   1.1       cgd 
    217   1.1       cgd /*
    218   1.1       cgd  * Print out the value of dot.
    219   1.1       cgd  */
    220   1.3   deraadt int
    221  1.23  christos /*ARGSUSED*/
    222  1.26  christos pdot(void *v __unused)
    223   1.1       cgd {
    224  1.23  christos 	(void)printf("%d\n", (int)(dot - &message[0] + 1));
    225   1.1       cgd 	return(0);
    226   1.1       cgd }
    227   1.1       cgd 
    228   1.1       cgd /*
    229   1.1       cgd  * Print out all the possible commands.
    230   1.1       cgd  */
    231   1.3   deraadt int
    232  1.23  christos /*ARGSUSED*/
    233  1.26  christos pcmdlist(void *v __unused)
    234   1.1       cgd {
    235  1.10     lukem 	const struct cmd *cp;
    236  1.10     lukem 	int cc;
    237   1.1       cgd 
    238  1.23  christos 	(void)printf("Commands are:\n");
    239   1.1       cgd 	for (cc = 0, cp = cmdtab; cp->c_name != NULL; cp++) {
    240   1.1       cgd 		cc += strlen(cp->c_name) + 2;
    241   1.1       cgd 		if (cc > 72) {
    242  1.23  christos 			(void)printf("\n");
    243   1.1       cgd 			cc = strlen(cp->c_name) + 2;
    244   1.1       cgd 		}
    245  1.26  christos 		if ((cp + 1)->c_name != NULL)
    246  1.23  christos 			(void)printf("%s, ", cp->c_name);
    247   1.1       cgd 		else
    248  1.23  christos 			(void)printf("%s\n", cp->c_name);
    249   1.1       cgd 	}
    250   1.1       cgd 	return(0);
    251   1.1       cgd }
    252   1.1       cgd 
    253  1.26  christos #ifdef MIME_SUPPORT
    254  1.26  christos static int
    255  1.26  christos de_mime(const char *name)
    256  1.26  christos {
    257  1.26  christos 	const char *p;
    258  1.26  christos 	const char *list;
    259  1.26  christos 
    260  1.26  christos #define DELIM	" \t,"	/* list of string delimiters */
    261  1.26  christos 
    262  1.26  christos 	list = value(ENAME_MIME_DECODE_MSG);
    263  1.26  christos 	if (list == NULL)
    264  1.26  christos 		return 0;
    265  1.26  christos 
    266  1.26  christos 	if (list[0] == '\0')
    267  1.26  christos 		return 1;
    268  1.26  christos 
    269  1.26  christos 	p = strcasestr(list, name);
    270  1.26  christos 	if (p == NULL)
    271  1.26  christos 		return 0;
    272  1.26  christos 
    273  1.26  christos 	return strchr(DELIM, p[strlen(name)]) && (
    274  1.26  christos 		p == list || strchr(DELIM, p[-1]));
    275  1.26  christos 
    276  1.26  christos #undef DELIM
    277  1.26  christos }
    278  1.26  christos #endif /* MIME_SUPPORT */
    279  1.26  christos 
    280   1.1       cgd /*
    281   1.1       cgd  * Paginate messages, honor ignored fields.
    282   1.1       cgd  */
    283   1.3   deraadt int
    284  1.15       wiz more(void *v)
    285   1.1       cgd {
    286   1.5  christos 	int *msgvec = v;
    287  1.26  christos #ifdef MIME_SUPPORT
    288  1.26  christos 	return type1(msgvec, 1, 1, de_mime("more"));
    289  1.26  christos #else
    290  1.26  christos 	return(type1(msgvec, 1, 1));
    291  1.26  christos #endif
    292  1.26  christos 
    293   1.1       cgd }
    294   1.1       cgd 
    295   1.1       cgd /*
    296   1.1       cgd  * Paginate messages, even printing ignored fields.
    297   1.1       cgd  */
    298   1.3   deraadt int
    299  1.15       wiz More(void *v)
    300   1.1       cgd {
    301   1.5  christos 	int *msgvec = v;
    302   1.1       cgd 
    303  1.26  christos #ifdef MIME_SUPPORT
    304  1.26  christos 	return type1(msgvec, 0, 1, de_mime("more"));
    305  1.26  christos #else
    306  1.26  christos 	return(type1(msgvec, 0, 1));
    307  1.26  christos #endif
    308   1.1       cgd }
    309   1.1       cgd 
    310   1.1       cgd /*
    311   1.1       cgd  * Type out messages, honor ignored fields.
    312   1.1       cgd  */
    313   1.3   deraadt int
    314  1.15       wiz type(void *v)
    315   1.1       cgd {
    316   1.5  christos 	int *msgvec = v;
    317   1.1       cgd 
    318  1.26  christos #ifdef MIME_SUPPORT
    319  1.26  christos 	return type1(msgvec, 1, 0, de_mime("type"));
    320  1.26  christos #else
    321   1.1       cgd 	return(type1(msgvec, 1, 0));
    322  1.26  christos #endif
    323   1.1       cgd }
    324   1.1       cgd 
    325   1.1       cgd /*
    326   1.1       cgd  * Type out messages, even printing ignored fields.
    327   1.1       cgd  */
    328   1.3   deraadt int
    329  1.15       wiz Type(void *v)
    330   1.1       cgd {
    331   1.5  christos 	int *msgvec = v;
    332   1.1       cgd 
    333  1.26  christos #ifdef MIME_SUPPORT
    334  1.26  christos 	return type1(msgvec, 0, 0, de_mime("type"));
    335  1.26  christos #else
    336  1.26  christos 	return(type1(msgvec, 0, 0));
    337  1.26  christos #endif
    338  1.26  christos }
    339  1.26  christos 
    340  1.26  christos 
    341  1.26  christos #ifdef MIME_SUPPORT
    342  1.26  christos /*
    343  1.26  christos  * Paginate messages, honor ignored fields.
    344  1.26  christos  */
    345  1.26  christos int
    346  1.26  christos page(void *v)
    347  1.26  christos {
    348  1.26  christos 	int *msgvec = v;
    349  1.26  christos #ifdef MIME_SUPPORT
    350  1.26  christos 	return type1(msgvec, 1, 1, de_mime("page"));
    351  1.26  christos #else
    352  1.26  christos 	return(type1(msgvec, 1, 1));
    353  1.26  christos #endif
    354  1.26  christos 
    355  1.26  christos }
    356  1.26  christos 
    357  1.26  christos /*
    358  1.26  christos  * Paginate messages, even printing ignored fields.
    359  1.26  christos  */
    360  1.26  christos int
    361  1.26  christos Page(void *v)
    362  1.26  christos {
    363  1.26  christos 	int *msgvec = v;
    364  1.26  christos 
    365  1.26  christos #ifdef MIME_SUPPORT
    366  1.26  christos 	return type1(msgvec, 0, 1, de_mime("page"));
    367  1.26  christos #else
    368  1.26  christos 	return(type1(msgvec, 0, 1));
    369  1.26  christos #endif
    370  1.26  christos }
    371  1.26  christos 
    372  1.26  christos /*
    373  1.26  christos  * Type out messages, honor ignored fields.
    374  1.26  christos  */
    375  1.26  christos int
    376  1.26  christos print(void *v)
    377  1.26  christos {
    378  1.26  christos 	int *msgvec = v;
    379  1.26  christos 
    380  1.26  christos #ifdef MIME_SUPPORT
    381  1.26  christos 	return type1(msgvec, 1, 0, de_mime("print"));
    382  1.26  christos #else
    383  1.26  christos 	return(type1(msgvec, 1, 0));
    384  1.26  christos #endif
    385  1.26  christos }
    386  1.26  christos 
    387  1.26  christos /*
    388  1.26  christos  * Type out messages, even printing ignored fields.
    389  1.26  christos  */
    390  1.26  christos int
    391  1.26  christos Print(void *v)
    392  1.26  christos {
    393  1.26  christos 	int *msgvec = v;
    394  1.26  christos 
    395  1.26  christos #ifdef MIME_SUPPORT
    396  1.26  christos 	return type1(msgvec, 0, 0, de_mime("print"));
    397  1.26  christos #else
    398   1.1       cgd 	return(type1(msgvec, 0, 0));
    399  1.26  christos #endif
    400  1.26  christos }
    401  1.26  christos 
    402  1.26  christos /*
    403  1.26  christos  * Identical to type(), but with opposite mime behavior.
    404  1.26  christos  */
    405  1.26  christos int
    406  1.26  christos view(void *v)
    407  1.26  christos {
    408  1.26  christos 	int *msgvec = v;
    409  1.26  christos 	return type1(msgvec, 1, 0, !de_mime("print"));
    410  1.26  christos }
    411  1.26  christos 
    412  1.26  christos /*
    413  1.26  christos  * Identical to Type(), but with opposite mime behavior.
    414  1.26  christos  */
    415  1.26  christos int
    416  1.26  christos View(void *v)
    417  1.26  christos {
    418  1.26  christos 	int *msgvec = v;
    419  1.26  christos 
    420  1.26  christos 	return type1(msgvec, 0, 0, !de_mime("print"));
    421   1.1       cgd }
    422  1.26  christos #endif /* MIME_SUPPORT */
    423   1.1       cgd 
    424   1.1       cgd /*
    425   1.1       cgd  * Type out the messages requested.
    426   1.1       cgd  */
    427   1.1       cgd jmp_buf	pipestop;
    428   1.3   deraadt int
    429  1.26  christos #ifdef MIME_SUPPORT
    430  1.26  christos type1(int *msgvec, int doign, int dopage, int mime_decode)
    431  1.26  christos #else
    432  1.26  christos type1(int *msgvec, int doign, int dopage)
    433  1.26  christos #endif
    434   1.1       cgd {
    435  1.10     lukem 	int *ip;
    436   1.5  christos 	struct message *mp;
    437  1.22  christos 	const char *cp;
    438   1.1       cgd 	int nlines;
    439   1.1       cgd 
    440  1.26  christos 	/* Some volatile variables so longjmp will get the current not
    441  1.26  christos 	 * starting values.  Note it is the variable that is volatile,
    442  1.26  christos 	 * not what it is pointing at! */
    443  1.26  christos #ifdef MIME_SUPPORT
    444  1.26  christos 	struct mime_info *volatile mip; /* avoid longjmp clobbering */
    445  1.26  christos #endif
    446  1.26  christos 	FILE *volatile obuf;		/* avoid longjmp clobbering */
    447  1.26  christos 
    448  1.26  christos #ifdef MIME_SUPPORT
    449  1.26  christos 	mip = NULL;
    450  1.26  christos #endif
    451   1.1       cgd 	obuf = stdout;
    452   1.1       cgd 	if (setjmp(pipestop))
    453   1.1       cgd 		goto close_pipe;
    454  1.16       wiz 	if (value("interactive") != NULL &&
    455  1.26  christos 	    (dopage || (cp = value("crt")) != NULL)) {
    456   1.1       cgd 		nlines = 0;
    457  1.26  christos 		if (!dopage) {
    458   1.1       cgd 			for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++)
    459  1.19      ross 				nlines += message[*ip - 1].m_blines;
    460   1.1       cgd 		}
    461  1.26  christos 		if (dopage || nlines > (*cp ? atoi(cp) : realscreenheight)) {
    462   1.1       cgd 			cp = value("PAGER");
    463   1.1       cgd 			if (cp == NULL || *cp == '\0')
    464   1.1       cgd 				cp = _PATH_MORE;
    465   1.1       cgd 			obuf = Popen(cp, "w");
    466   1.1       cgd 			if (obuf == NULL) {
    467  1.18       wiz 				warn("%s", cp);
    468   1.1       cgd 				obuf = stdout;
    469   1.1       cgd 			} else
    470  1.23  christos 				(void)signal(SIGPIPE, brokpipe);
    471   1.1       cgd 		}
    472   1.1       cgd 	}
    473   1.1       cgd 	for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
    474   1.1       cgd 		mp = &message[*ip - 1];
    475   1.1       cgd 		touch(mp);
    476   1.1       cgd 		dot = mp;
    477  1.16       wiz 		if (value("quiet") == NULL)
    478  1.26  christos 			(void)fprintf(obuf, "Message %d:\n", *ip);
    479  1.26  christos #ifdef MIME_SUPPORT
    480  1.26  christos 		if (mime_decode)
    481  1.26  christos 			mip = mime_decode_open(mp);
    482  1.26  christos 		(void)mime_sendmessage(mp, obuf, doign ? ignore : 0, NULL, mip);
    483  1.26  christos 		mime_decode_close(mip);
    484  1.26  christos #else
    485  1.26  christos 		(void)sendmessage(mp, obuf, doign ? ignore : 0, NULL);
    486  1.26  christos #endif
    487   1.1       cgd 	}
    488   1.1       cgd close_pipe:
    489  1.26  christos #ifdef MIME_SUPPORT
    490  1.26  christos 	if (mip != NULL || obuf != stdout) {
    491  1.26  christos 		/*
    492  1.26  christos 		 * Ignore SIGPIPE so it can't cause a duplicate close.
    493  1.26  christos 		 */
    494  1.26  christos 		(void)signal(SIGPIPE, SIG_IGN);
    495  1.26  christos 		mime_decode_close(mip);
    496  1.26  christos 		if (obuf != stdout)
    497  1.26  christos 			(void)Pclose(obuf);
    498  1.26  christos 		(void)signal(SIGPIPE, SIG_DFL);
    499  1.26  christos 
    500  1.26  christos 	}
    501  1.26  christos #else
    502   1.1       cgd 	if (obuf != stdout) {
    503   1.1       cgd 		/*
    504   1.1       cgd 		 * Ignore SIGPIPE so it can't cause a duplicate close.
    505   1.1       cgd 		 */
    506  1.23  christos 		(void)signal(SIGPIPE, SIG_IGN);
    507  1.26  christos 		(void)Pclose(obuf);
    508  1.23  christos 		(void)signal(SIGPIPE, SIG_DFL);
    509   1.1       cgd 	}
    510  1.26  christos #endif
    511   1.1       cgd 	return(0);
    512   1.1       cgd }
    513   1.1       cgd 
    514   1.1       cgd /*
    515   1.1       cgd  * Respond to a broken pipe signal --
    516   1.1       cgd  * probably caused by quitting more.
    517   1.1       cgd  */
    518   1.1       cgd void
    519  1.23  christos /*ARGSUSED*/
    520  1.26  christos brokpipe(int signo __unused)
    521   1.1       cgd {
    522   1.1       cgd 	longjmp(pipestop, 1);
    523   1.1       cgd }
    524   1.1       cgd 
    525   1.1       cgd /*
    526  1.24  christos  * Pipe the current message buffer to a command.
    527  1.24  christos  */
    528  1.24  christos int
    529  1.24  christos pipecmd(void *v)
    530  1.24  christos {
    531  1.24  christos 	char *cmd = v;
    532  1.26  christos 	FILE *volatile obuf;	/* void longjmp clobbering - we want
    533  1.26  christos 				   the current value not start value */
    534  1.24  christos 	if (dot == NULL) {
    535  1.24  christos 		warn("pipcmd: no current message");
    536  1.24  christos 		return 1;
    537  1.24  christos 	}
    538  1.24  christos 
    539  1.24  christos 	obuf = stdout;
    540  1.24  christos 	if (setjmp(pipestop))
    541  1.24  christos 		goto close_pipe;
    542  1.24  christos 
    543  1.24  christos 	obuf = Popen(cmd, "w");
    544  1.24  christos 	if (obuf == NULL) {
    545  1.24  christos 		warn("pipecmd: Popen failed: %s", cmd);
    546  1.24  christos 		return 1;
    547  1.24  christos 	} else
    548  1.24  christos 		(void)signal(SIGPIPE, brokpipe);
    549  1.24  christos 
    550  1.26  christos #ifdef MIME_SUPPORT
    551  1.26  christos 	(void)sendmessage(dot, obuf, ignoreall, NULL, NULL);
    552  1.26  christos #else
    553  1.26  christos 	(void)sendmessage(dot, obuf, ignoreall, NULL);
    554  1.26  christos #endif
    555  1.24  christos 
    556  1.24  christos  close_pipe:
    557  1.24  christos 	if (obuf != stdout) {
    558  1.24  christos 		/*
    559  1.24  christos 		 * Ignore SIGPIPE so it can't cause a duplicate close.
    560  1.24  christos 		 */
    561  1.24  christos 		(void)signal(SIGPIPE, SIG_IGN);
    562  1.26  christos 		(void)Pclose(obuf);
    563  1.24  christos 		(void)signal(SIGPIPE, SIG_DFL);
    564  1.24  christos 	}
    565  1.24  christos 	return 0;
    566  1.24  christos }
    567  1.24  christos 
    568  1.24  christos /*
    569   1.1       cgd  * Print the top so many lines of each desired message.
    570   1.1       cgd  * The number of lines is taken from the variable "toplines"
    571   1.1       cgd  * and defaults to 5.
    572   1.1       cgd  */
    573   1.3   deraadt int
    574  1.15       wiz top(void *v)
    575   1.1       cgd {
    576   1.5  christos 	int *msgvec = v;
    577  1.10     lukem 	int *ip;
    578  1.10     lukem 	struct message *mp;
    579   1.1       cgd 	int c, topl, lines, lineb;
    580   1.1       cgd 	char *valtop, linebuf[LINESIZE];
    581   1.1       cgd 	FILE *ibuf;
    582   1.1       cgd 
    583   1.1       cgd 	topl = 5;
    584   1.1       cgd 	valtop = value("toplines");
    585  1.16       wiz 	if (valtop != NULL) {
    586   1.1       cgd 		topl = atoi(valtop);
    587   1.1       cgd 		if (topl < 0 || topl > 10000)
    588   1.1       cgd 			topl = 5;
    589   1.1       cgd 	}
    590   1.1       cgd 	lineb = 1;
    591   1.1       cgd 	for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
    592   1.1       cgd 		mp = &message[*ip - 1];
    593   1.1       cgd 		touch(mp);
    594   1.1       cgd 		dot = mp;
    595  1.16       wiz 		if (value("quiet") == NULL)
    596  1.23  christos 			(void)printf("Message %d:\n", *ip);
    597   1.1       cgd 		ibuf = setinput(mp);
    598   1.1       cgd 		c = mp->m_lines;
    599   1.1       cgd 		if (!lineb)
    600  1.23  christos 			(void)printf("\n");
    601   1.1       cgd 		for (lines = 0; lines < c && lines <= topl; lines++) {
    602  1.26  christos 			if (mail_readline(ibuf, linebuf, LINESIZE) < 0)
    603   1.1       cgd 				break;
    604  1.23  christos 			(void)puts(linebuf);
    605   1.1       cgd 			lineb = blankline(linebuf);
    606   1.1       cgd 		}
    607   1.1       cgd 	}
    608   1.1       cgd 	return(0);
    609   1.1       cgd }
    610   1.1       cgd 
    611   1.1       cgd /*
    612   1.1       cgd  * Touch all the given messages so that they will
    613   1.1       cgd  * get mboxed.
    614   1.1       cgd  */
    615   1.3   deraadt int
    616  1.15       wiz stouch(void *v)
    617   1.1       cgd {
    618   1.5  christos 	int *msgvec = v;
    619  1.10     lukem 	int *ip;
    620   1.1       cgd 
    621   1.1       cgd 	for (ip = msgvec; *ip != 0; ip++) {
    622  1.26  christos 		dot = &message[*ip - 1];
    623   1.1       cgd 		dot->m_flag |= MTOUCH;
    624   1.1       cgd 		dot->m_flag &= ~MPRESERVE;
    625   1.1       cgd 	}
    626   1.1       cgd 	return(0);
    627   1.1       cgd }
    628   1.1       cgd 
    629   1.1       cgd /*
    630   1.1       cgd  * Make sure all passed messages get mboxed.
    631   1.1       cgd  */
    632   1.3   deraadt int
    633  1.15       wiz mboxit(void *v)
    634   1.1       cgd {
    635   1.5  christos 	int *msgvec = v;
    636  1.10     lukem 	int *ip;
    637   1.1       cgd 
    638   1.1       cgd 	for (ip = msgvec; *ip != 0; ip++) {
    639  1.26  christos 		dot = &message[*ip - 1];
    640   1.1       cgd 		dot->m_flag |= MTOUCH|MBOX;
    641   1.1       cgd 		dot->m_flag &= ~MPRESERVE;
    642   1.1       cgd 	}
    643   1.1       cgd 	return(0);
    644   1.1       cgd }
    645   1.1       cgd 
    646   1.1       cgd /*
    647   1.1       cgd  * List the folders the user currently has.
    648   1.1       cgd  */
    649   1.3   deraadt int
    650  1.23  christos /*ARGSUSED*/
    651  1.26  christos folders(void *v __unused)
    652   1.1       cgd {
    653   1.7     mikel 	char dirname[PATHSIZE];
    654  1.22  christos 	const char *cmd;
    655   1.1       cgd 
    656   1.1       cgd 	if (getfold(dirname) < 0) {
    657  1.23  christos 		(void)printf("No value set for \"folder\"\n");
    658   1.1       cgd 		return 1;
    659   1.1       cgd 	}
    660  1.16       wiz 	if ((cmd = value("LISTER")) == NULL)
    661   1.1       cgd 		cmd = "ls";
    662  1.20  christos 	(void)run_command(cmd, 0, -1, -1, dirname, NULL);
    663   1.6       tls 	return 0;
    664   1.6       tls }
    665   1.6       tls 
    666   1.6       tls /*
    667   1.6       tls  * Update the mail file with any new messages that have
    668   1.6       tls  * come in since we started reading mail.
    669   1.6       tls  */
    670   1.6       tls int
    671  1.23  christos /*ARGSUSED*/
    672  1.26  christos inc(void *v __unused)
    673   1.6       tls {
    674   1.6       tls 	int nmsg, mdot;
    675   1.6       tls 
    676   1.6       tls 	nmsg = incfile();
    677   1.6       tls 
    678   1.6       tls 	if (nmsg == 0) {
    679  1.23  christos 	(void)printf("No new mail.\n");
    680   1.6       tls 	} else if (nmsg > 0) {
    681   1.6       tls 		mdot = newfileinfo(msgCount - nmsg);
    682   1.6       tls 		dot = &message[mdot - 1];
    683   1.6       tls 	} else {
    684  1.23  christos 	(void)printf("\"inc\" command failed...\n");
    685   1.6       tls 	}
    686   1.6       tls 
    687   1.1       cgd 	return 0;
    688   1.1       cgd }
    689