2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 static char sccsid[] = "@(#)cmd1.c 8.2 (Berkeley) 4/20/95";
37 #include <sys/cdefs.h>
38 __FBSDID("$FreeBSD$");
44 * Mail -- a mail program
50 * Print the current active headings.
51 * Don't change dot if invoker didn't give an argument.
60 int n, mesg, flag, size;
69 mp = &message[screen * size];
70 if (mp >= &message[msgCount])
71 mp = &message[msgCount - size];
75 mesg = mp - &message[0];
76 if (dot != &message[n-1])
78 for (; mp < &message[msgCount]; mp++) {
80 if (mp->m_flag & MDELETED)
87 printf("No more mail.\n");
94 * Scroll to the next/previous screen
110 if (s * size >= msgCount) {
111 printf("On last screenful of messages\n");
119 printf("On first screenful of messages\n");
126 printf("Unrecognized scrolling command \"%s\"\n", arg);
129 return (headers(cur));
133 * Compute screen size.
141 if ((cp = value("screen")) != NULL && (s = atoi(cp)) > 0)
143 return (screenheight - 4);
147 * Print out the headlines for each message
148 * in the passed message list.
156 for (ip = msgvec; *ip != 0; ip++)
159 dot = &message[*ip - 1];
164 * Print out the header of a specific message.
165 * This is a slight improvement to the standard one.
171 char headline[LINESIZE], wcount[LINESIZE], *subjline, dispc, curind;
177 mp = &message[mesg-1];
178 (void)readline(setinput(mp), headline, LINESIZE);
179 if ((subjline = hfield("subject", mp)) == NULL)
180 subjline = hfield("subj", mp);
184 curind = dot == mp ? '>' : ' ';
186 if (mp->m_flag & MSAVED)
188 if (mp->m_flag & MPRESERVE)
190 if ((mp->m_flag & (MREAD|MNEW)) == MNEW)
192 if ((mp->m_flag & (MREAD|MNEW)) == 0)
194 if (mp->m_flag & MBOX)
196 parse(headline, &hl, pbuf);
197 sprintf(wcount, "%3ld/%-5ld", mp->m_lines, mp->m_size);
198 subjlen = screenwidth - 50 - strlen(wcount);
199 name = value("show-rcpt") != NULL ?
200 skin(hfield("to", mp)) : nameof(mp, 0);
201 if (subjline == NULL || subjlen < 0) /* pretty pathetic */
202 printf("%c%c%3d %-20.20s %16.16s %s\n",
203 curind, dispc, mesg, name, hl.l_date, wcount);
205 printf("%c%c%3d %-20.20s %16.16s %s \"%.*s\"\n",
206 curind, dispc, mesg, name, hl.l_date, wcount,
211 * Print out the value of dot.
216 printf("%td\n", dot - &message[0] + 1);
221 * Print out all the possible commands.
226 extern const struct cmd cmdtab[];
227 const struct cmd *cp;
230 printf("Commands are:\n");
231 for (cc = 0, cp = cmdtab; cp->c_name != NULL; cp++) {
232 cc += strlen(cp->c_name) + 2;
235 cc = strlen(cp->c_name) + 2;
237 if ((cp+1)->c_name != NULL)
238 printf("%s, ", cp->c_name);
240 printf("%s\n", cp->c_name);
246 * Paginate messages, honor ignored fields.
253 return (type1(msgvec, 1, 1));
257 * Paginate messages, even printing ignored fields.
264 return (type1(msgvec, 0, 1));
268 * Type out messages, honor ignored fields.
275 return (type1(msgvec, 1, 0));
279 * Type out messages, even printing ignored fields.
286 return (type1(msgvec, 0, 0));
290 * Type out the messages requested.
292 static jmp_buf pipestop;
294 type1(int *msgvec, int doign, int page)
302 if (setjmp(pipestop))
304 if (value("interactive") != NULL &&
305 (page || (cp = value("crt")) != NULL)) {
308 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++)
309 nlines += message[*ip - 1].m_lines;
311 if (page || nlines > (*cp ? atoi(cp) : realscreenheight)) {
313 if (cp == NULL || *cp == '\0')
315 obuf = Popen(cp, "w");
320 (void)signal(SIGPIPE, brokpipe);
325 * Send messages to the output.
328 for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
329 mp = &message[*ip - 1];
332 if (value("quiet") == NULL)
333 fprintf(obuf, "Message %d:\n", *ip);
334 (void)sendmessage(mp, obuf, doign ? ignore : 0, NULL);
338 if (obuf != stdout) {
340 * Ignore SIGPIPE so it can't cause a duplicate close.
342 (void)signal(SIGPIPE, SIG_IGN);
344 (void)signal(SIGPIPE, SIG_DFL);
350 * Respond to a broken pipe signal --
351 * probably caused by quitting more.
355 brokpipe(int signo __unused)
357 longjmp(pipestop, 1);
361 * Print the top so many lines of each desired message.
362 * The number of lines is taken from the variable "toplines"
371 int c, topl, lines, lineb;
372 char *valtop, linebuf[LINESIZE];
376 valtop = value("toplines");
377 if (valtop != NULL) {
379 if (topl < 0 || topl > 10000)
383 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
384 mp = &message[*ip - 1];
387 if (value("quiet") == NULL)
388 printf("Message %d:\n", *ip);
393 for (lines = 0; lines < c && lines <= topl; lines++) {
394 if (readline(ibuf, linebuf, sizeof(linebuf)) < 0)
397 lineb = strspn(linebuf, " \t") == strlen(linebuf);
404 * Touch all the given messages so that they will
413 for (ip = msgvec; *ip != 0; ip++) {
414 dot = &message[*ip-1];
415 dot->m_flag |= MTOUCH;
416 dot->m_flag &= ~MPRESERVE;
422 * Make sure all passed messages get mboxed.
430 for (ip = msgvec; *ip != 0; ip++) {
431 dot = &message[*ip-1];
432 dot->m_flag |= MTOUCH|MBOX;
433 dot->m_flag &= ~MPRESERVE;
439 * List the folders the user currently has.
444 char dirname[PATHSIZE];
447 if (getfold(dirname, sizeof(dirname)) < 0) {
448 printf("No value set for \"folder\"\n");
451 if ((cmd = value("LISTER")) == NULL)
453 (void)run_command(cmd, 0, -1, -1, dirname, NULL);
458 * Update the mail file with any new messages that have
459 * come in since we started reading mail.
462 inc(void *v __unused)
469 printf("No new mail.\n");
471 mdot = newfileinfo(msgCount - nmsg);
472 dot = &message[mdot - 1];
474 printf("\"inc\" command failed...\n");