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 * 4. 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
29 * @(#)cmd1.c 8.2 (Berkeley) 4/20/95
30 * $FreeBSD: src/usr.bin/mail/cmd1.c,v 1.3.6.3 2003/01/06 05:46:03 mikeh Exp $
31 * $DragonFly: src/usr.bin/mail/cmd1.c,v 1.4 2004/09/08 03:01:11 joerg Exp $
38 * Mail -- a mail program
43 extern const struct cmd cmdtab[];
46 * Print the current active headings.
47 * Don't change dot if invoker didn't give an argument.
55 int n, mesg, flag, size;
64 mp = &message[screen * size];
65 if (mp >= &message[msgCount])
66 mp = &message[msgCount - size];
70 mesg = mp - &message[0];
71 if (dot != &message[n-1])
73 for (; mp < &message[msgCount]; mp++) {
75 if (mp->m_flag & MDELETED)
82 printf("No more mail.\n");
89 * Scroll to the next/previous screen
104 if (s * size >= msgCount) {
105 printf("On last screenful of messages\n");
113 printf("On first screenful of messages\n");
120 printf("Unrecognized scrolling command \"%s\"\n", arg);
123 return (headers(cur));
127 * Compute screen size.
135 if ((cp = value("screen")) != NULL && (s = atoi(cp)) > 0)
137 return (screenheight - 4);
141 * Print out the headlines for each message
142 * in the passed message list.
149 for (ip = msgvec; *ip != 0; ip++)
152 dot = &message[*ip - 1];
157 * Print out the header of a specific message.
158 * This is a slight improvement to the standard one.
164 char headline[LINESIZE], wcount[LINESIZE], *subjline, dispc, curind;
170 mp = &message[mesg-1];
171 readline(setinput(mp), headline, LINESIZE);
172 if ((subjline = hfield("subject", mp)) == NULL)
173 subjline = hfield("subj", mp);
177 curind = dot == mp ? '>' : ' ';
179 if (mp->m_flag & MSAVED)
181 if (mp->m_flag & MPRESERVE)
183 if ((mp->m_flag & (MREAD|MNEW)) == MNEW)
185 if ((mp->m_flag & (MREAD|MNEW)) == 0)
187 if (mp->m_flag & MBOX)
189 parse(headline, &hl, pbuf);
190 sprintf(wcount, "%3ld/%-5ld", mp->m_lines, mp->m_size);
191 subjlen = screenwidth - 50 - strlen(wcount);
192 name = value("show-rcpt") != NULL ?
193 skin(hfield("to", mp)) : nameof(mp, 0);
194 if (subjline == NULL || subjlen < 0) /* pretty pathetic */
195 printf("%c%c%3d %-20.20s %16.16s %s\n",
196 curind, dispc, mesg, name, hl.l_date, wcount);
198 printf("%c%c%3d %-20.20s %16.16s %s \"%.*s\"\n",
199 curind, dispc, mesg, name, hl.l_date, wcount,
204 * Print out the value of dot.
209 printf("%d\n", dot - &message[0] + 1);
214 * Print out all the possible commands.
219 const struct cmd *cp;
222 printf("Commands are:\n");
223 for (cc = 0, cp = cmdtab; cp->c_name != NULL; cp++) {
224 cc += strlen(cp->c_name) + 2;
227 cc = strlen(cp->c_name) + 2;
229 if ((cp+1)->c_name != NULL)
230 printf("%s, ", cp->c_name);
232 printf("%s\n", cp->c_name);
238 * Paginate messages, honor ignored fields.
243 return (type1(msgvec, 1, 1));
247 * Paginate messages, even printing ignored fields.
252 return (type1(msgvec, 0, 1));
256 * Type out messages, honor ignored fields.
261 return (type1(msgvec, 1, 0));
265 * Type out messages, even printing ignored fields.
270 return (type1(msgvec, 0, 0));
274 * Type out the messages requested.
278 type1(int *msgvec, int doign, int page)
286 if (setjmp(pipestop))
288 if (value("interactive") != NULL &&
289 (page || (cp = value("crt")) != NULL)) {
292 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++)
293 nlines += message[*ip - 1].m_lines;
295 if (page || nlines > (*cp ? atoi(cp) : realscreenheight)) {
297 if (cp == NULL || *cp == '\0')
299 obuf = Popen(cp, "w");
304 signal(SIGPIPE, brokpipe);
309 * Send messages to the output.
312 for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
313 mp = &message[*ip - 1];
316 if (value("quiet") == NULL)
317 fprintf(obuf, "Message %d:\n", *ip);
318 sendmessage(mp, obuf, doign ? ignore : 0, NULL);
322 if (obuf != stdout) {
324 * Ignore SIGPIPE so it can't cause a duplicate close.
326 signal(SIGPIPE, SIG_IGN);
328 signal(SIGPIPE, SIG_DFL);
334 * Respond to a broken pipe signal --
335 * probably caused by quitting more.
341 longjmp(pipestop, 1);
345 * Print the top so many lines of each desired message.
346 * The number of lines is taken from the variable "toplines"
354 int c, topl, lines, lineb;
355 char *valtop, linebuf[LINESIZE];
359 valtop = value("toplines");
360 if (valtop != NULL) {
362 if (topl < 0 || topl > 10000)
366 for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
367 mp = &message[*ip - 1];
370 if (value("quiet") == NULL)
371 printf("Message %d:\n", *ip);
376 for (lines = 0; lines < c && lines <= topl; lines++) {
377 if (readline(ibuf, linebuf, sizeof(linebuf)) < 0)
380 lineb = strspn(linebuf, " \t") == strlen(linebuf);
387 * Touch all the given messages so that they will
395 for (ip = msgvec; *ip != 0; ip++) {
396 dot = &message[*ip-1];
397 dot->m_flag |= MTOUCH;
398 dot->m_flag &= ~MPRESERVE;
404 * Make sure all passed messages get mboxed.
411 for (ip = msgvec; *ip != 0; ip++) {
412 dot = &message[*ip-1];
413 dot->m_flag |= MTOUCH|MBOX;
414 dot->m_flag &= ~MPRESERVE;
420 * List the folders the user currently has.
425 char dirname[PATHSIZE];
428 if (getfold(dirname, sizeof(dirname)) < 0) {
429 printf("No value set for \"folder\"\n");
432 if ((cmd = value("LISTER")) == NULL)
434 run_command(cmd, 0, -1, -1, dirname, NULL, NULL);
439 * Update the mail file with any new messages that have
440 * come in since we started reading mail.
450 printf("No new mail.\n");
452 mdot = newfileinfo(msgCount - nmsg);
453 dot = &message[mdot - 1];
455 printf("\"inc\" command failed...\n");