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. Neither the name of the University nor the names of its contributors
14 * may be used to endorse or promote products derived from this software
15 * without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 static char sccsid[] = "@(#)cmd1.c 8.2 (Berkeley) 4/20/95";
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
42 * Mail -- a mail program
48 * Print the current active headings.
49 * Don't change dot if invoker didn't give an argument.
58 int n, mesg, flag, size;
67 mp = &message[screen * size];
68 if (mp >= &message[msgCount])
69 mp = &message[msgCount - size];
73 mesg = mp - &message[0];
74 if (dot != &message[n-1])
76 for (; mp < &message[msgCount]; mp++) {
78 if (mp->m_flag & MDELETED)
85 printf("No more mail.\n");
92 * Scroll to the next/previous screen
108 if (s * size >= msgCount) {
109 printf("On last screenful of messages\n");
117 printf("On first screenful of messages\n");
124 printf("Unrecognized scrolling command \"%s\"\n", arg);
127 return (headers(cur));
131 * Compute screen size.
139 if ((cp = value("screen")) != NULL && (s = atoi(cp)) > 0)
141 return (screenheight - 4);
145 * Print out the headlines for each message
146 * in the passed message list.
154 for (ip = msgvec; *ip != 0; ip++)
157 dot = &message[*ip - 1];
162 * Print out the header of a specific message.
163 * This is a slight improvement to the standard one.
169 char headline[LINESIZE], wcount[LINESIZE], *subjline, dispc, curind;
175 mp = &message[mesg-1];
176 (void)readline(setinput(mp), headline, LINESIZE);
177 if ((subjline = hfield("subject", mp)) == NULL)
178 subjline = hfield("subj", mp);
182 curind = dot == mp ? '>' : ' ';
184 if (mp->m_flag & MSAVED)
186 if (mp->m_flag & MPRESERVE)
188 if ((mp->m_flag & (MREAD|MNEW)) == MNEW)
190 if ((mp->m_flag & (MREAD|MNEW)) == 0)
192 if (mp->m_flag & MBOX)
194 parse(headline, &hl, pbuf);
195 sprintf(wcount, "%3ld/%-5ld", mp->m_lines, mp->m_size);
196 subjlen = screenwidth - 50 - strlen(wcount);
197 name = value("show-rcpt") != NULL ?
198 skin(hfield("to", mp)) : nameof(mp, 0);
199 if (subjline == NULL || subjlen < 0) /* pretty pathetic */
200 printf("%c%c%3d %-20.20s %16.16s %s\n",
201 curind, dispc, mesg, name, hl.l_date, wcount);
203 printf("%c%c%3d %-20.20s %16.16s %s \"%.*s\"\n",
204 curind, dispc, mesg, name, hl.l_date, wcount,
209 * Print out the value of dot.
214 printf("%td\n", dot - &message[0] + 1);
219 * Print out all the possible commands.
224 extern const struct cmd cmdtab[];
225 const struct cmd *cp;
228 printf("Commands are:\n");
229 for (cc = 0, cp = cmdtab; cp->c_name != NULL; cp++) {
230 cc += strlen(cp->c_name) + 2;
233 cc = strlen(cp->c_name) + 2;
235 if ((cp+1)->c_name != NULL)
236 printf("%s, ", cp->c_name);
238 printf("%s\n", cp->c_name);
244 * Paginate messages, honor ignored fields.
251 return (type1(msgvec, 1, 1));
255 * Paginate messages, even printing ignored fields.
262 return (type1(msgvec, 0, 1));
266 * Type out messages, honor ignored fields.
273 return (type1(msgvec, 1, 0));
277 * Type out messages, even printing ignored fields.
284 return (type1(msgvec, 0, 0));
288 * Type out the messages requested.
290 static jmp_buf pipestop;
292 type1(int *msgvec, int doign, int page)
300 if (setjmp(pipestop))
302 if (value("interactive") != NULL &&
303 (page || (cp = value("crt")) != NULL)) {
306 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++)
307 nlines += message[*ip - 1].m_lines;
309 if (page || nlines > (*cp ? atoi(cp) : realscreenheight)) {
311 if (cp == NULL || *cp == '\0')
313 obuf = Popen(cp, "w");
318 (void)signal(SIGPIPE, brokpipe);
323 * Send messages to the output.
326 for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
327 mp = &message[*ip - 1];
330 if (value("quiet") == NULL)
331 fprintf(obuf, "Message %d:\n", *ip);
332 (void)sendmessage(mp, obuf, doign ? ignore : 0, NULL);
336 if (obuf != stdout) {
338 * Ignore SIGPIPE so it can't cause a duplicate close.
340 (void)signal(SIGPIPE, SIG_IGN);
342 (void)signal(SIGPIPE, SIG_DFL);
348 * Respond to a broken pipe signal --
349 * probably caused by quitting more.
353 brokpipe(int signo __unused)
355 longjmp(pipestop, 1);
359 * Print the top so many lines of each desired message.
360 * The number of lines is taken from the variable "toplines"
369 int c, topl, lines, lineb;
370 char *valtop, linebuf[LINESIZE];
374 valtop = value("toplines");
375 if (valtop != NULL) {
377 if (topl < 0 || topl > 10000)
381 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
382 mp = &message[*ip - 1];
385 if (value("quiet") == NULL)
386 printf("Message %d:\n", *ip);
391 for (lines = 0; lines < c && lines <= topl; lines++) {
392 if (readline(ibuf, linebuf, sizeof(linebuf)) < 0)
395 lineb = strspn(linebuf, " \t") == strlen(linebuf);
402 * Touch all the given messages so that they will
411 for (ip = msgvec; *ip != 0; ip++) {
412 dot = &message[*ip-1];
413 dot->m_flag |= MTOUCH;
414 dot->m_flag &= ~MPRESERVE;
420 * Make sure all passed messages get mboxed.
428 for (ip = msgvec; *ip != 0; ip++) {
429 dot = &message[*ip-1];
430 dot->m_flag |= MTOUCH|MBOX;
431 dot->m_flag &= ~MPRESERVE;
437 * List the folders the user currently has.
442 char dirname[PATHSIZE];
445 if (getfold(dirname, sizeof(dirname)) < 0) {
446 printf("No value set for \"folder\"\n");
449 if ((cmd = value("LISTER")) == NULL)
451 (void)run_command(cmd, 0, -1, -1, dirname, NULL);
456 * Update the mail file with any new messages that have
457 * come in since we started reading mail.
460 inc(void *v __unused)
467 printf("No new mail.\n");
469 mdot = newfileinfo(msgCount - nmsg);
470 dot = &message[mdot - 1];
472 printf("\"inc\" command failed...\n");