Annotation of src/usr.bin/mail/cmd1.c, Revision 1.25
1.25 ! millert 1: /* $OpenBSD: cmd1.c,v 1.24 2003/06/03 02:56:11 millert Exp $ */
1.6 millert 2: /* $NetBSD: cmd1.c,v 1.9 1997/07/09 05:29:48 mikel Exp $ */
1.2 niklas 3:
1.1 deraadt 4: /*-
5: * Copyright (c) 1980, 1993
6: * The Regents of the University of California. All rights reserved.
7: *
8: * Redistribution and use in source and binary forms, with or without
9: * modification, are permitted provided that the following conditions
10: * are met:
11: * 1. Redistributions of source code must retain the above copyright
12: * notice, this list of conditions and the following disclaimer.
13: * 2. Redistributions in binary form must reproduce the above copyright
14: * notice, this list of conditions and the following disclaimer in the
15: * documentation and/or other materials provided with the distribution.
1.24 millert 16: * 3. Neither the name of the University nor the names of its contributors
1.1 deraadt 17: * may be used to endorse or promote products derived from this software
18: * without specific prior written permission.
19: *
20: * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21: * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22: * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23: * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24: * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25: * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26: * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27: * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28: * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30: * SUCH DAMAGE.
31: */
32:
33: #ifndef lint
1.3 deraadt 34: #if 0
1.21 millert 35: static const char sccsid[] = "@(#)cmd1.c 8.2 (Berkeley) 4/20/95";
1.3 deraadt 36: #else
1.25 ! millert 37: static const char rcsid[] = "$OpenBSD: cmd1.c,v 1.24 2003/06/03 02:56:11 millert Exp $";
1.3 deraadt 38: #endif
1.1 deraadt 39: #endif /* not lint */
40:
41: #include "rcv.h"
42: #include "extern.h"
43:
44: /*
45: * Mail -- a mail program
46: *
47: * User commands.
48: */
49:
50: /*
51: * Print the current active headings.
52: * Don't change dot if invoker didn't give an argument.
53: */
54:
55: static int screen;
1.20 millert 56: static volatile sig_atomic_t gothdrint;
1.1 deraadt 57:
58: int
1.21 millert 59: headers(void *v)
1.1 deraadt 60: {
1.3 deraadt 61: int *msgvec = v;
1.12 millert 62: int n, mesg, flag, size;
63: struct message *mp;
1.20 millert 64: struct sigaction act, oact;
65: sigset_t oset;
1.1 deraadt 66:
67: size = screensize();
68: n = msgvec[0];
69: if (n != 0)
70: screen = (n-1)/size;
71: if (screen < 0)
72: screen = 0;
73: mp = &message[screen * size];
74: if (mp >= &message[msgCount])
75: mp = &message[msgCount - size];
76: if (mp < &message[0])
77: mp = &message[0];
78: flag = 0;
79: mesg = mp - &message[0];
80: if (dot != &message[n-1])
81: dot = mp;
1.20 millert 82: sigemptyset(&act.sa_mask);
83: act.sa_flags = SA_RESTART;
84: act.sa_handler = hdrint;
85: if (sigaction(SIGINT, NULL, &oact) == 0 &&
86: oact.sa_handler != SIG_IGN) {
87: (void)sigaction(SIGINT, &act, &oact);
88: (void)sigprocmask(SIG_UNBLOCK, &intset, &oset);
89: }
90: for (gothdrint = 0; !gothdrint && mp < &message[msgCount]; mp++) {
1.1 deraadt 91: mesg++;
92: if (mp->m_flag & MDELETED)
93: continue;
94: if (flag++ >= size)
95: break;
96: printhead(mesg);
97: }
1.20 millert 98: if (gothdrint) {
99: fflush(stdout);
100: fputs("\nInterrupt\n", stderr);
101: }
102: if (oact.sa_handler != SIG_IGN) {
103: (void)sigprocmask(SIG_SETMASK, &oset, NULL);
104: (void)sigaction(SIGINT, &oact, NULL);
105: }
1.1 deraadt 106: if (flag == 0) {
1.6 millert 107: puts("No more mail.");
1.1 deraadt 108: return(1);
109: }
110: return(0);
111: }
112:
113: /*
114: * Scroll to the next/previous screen
115: */
116: int
1.21 millert 117: scroll(void *v)
1.1 deraadt 118: {
1.3 deraadt 119: char *arg = v;
1.13 millert 120: int size, maxscreen;
1.1 deraadt 121: int cur[1];
122:
123: cur[0] = 0;
124: size = screensize();
1.13 millert 125: maxscreen = (msgCount - 1) / size;
1.1 deraadt 126: switch (*arg) {
127: case 0:
128: case '+':
1.13 millert 129: if (screen >= maxscreen) {
1.6 millert 130: puts("On last screenful of messages");
1.1 deraadt 131: return(0);
132: }
1.13 millert 133: screen++;
1.1 deraadt 134: break;
135:
136: case '-':
1.13 millert 137: if (screen <= 0) {
1.6 millert 138: puts("On first screenful of messages");
1.1 deraadt 139: return(0);
140: }
1.13 millert 141: screen--;
1.1 deraadt 142: break;
143:
144: default:
145: printf("Unrecognized scrolling command \"%s\"\n", arg);
146: return(1);
147: }
148: return(headers(cur));
149: }
150:
151: /*
152: * Compute screen size.
153: */
154: int
1.21 millert 155: screensize(void)
1.1 deraadt 156: {
157: int s;
158: char *cp;
159:
1.8 millert 160: if ((cp = value("screen")) != NULL && (s = atoi(cp)) > 0)
1.6 millert 161: return(s);
162: return(screenheight - 4);
1.1 deraadt 163: }
164:
165: /*
166: * Print out the headlines for each message
167: * in the passed message list.
168: */
169: int
1.21 millert 170: from(void *v)
1.1 deraadt 171: {
1.3 deraadt 172: int *msgvec = v;
1.12 millert 173: int *ip;
1.1 deraadt 174:
175: for (ip = msgvec; *ip != NULL; ip++)
176: printhead(*ip);
177: if (--ip >= msgvec)
178: dot = &message[*ip - 1];
179: return(0);
180: }
181:
182: /*
183: * Print out the header of a specific message.
184: * This is a slight improvement to the standard one.
185: */
186: void
1.21 millert 187: printhead(int mesg)
1.1 deraadt 188: {
189: struct message *mp;
1.25 ! millert 190: char headline[LINESIZE], *subjline, dispc, curind;
! 191: char visname[LINESIZE], vissub[LINESIZE];
! 192: char pbuf[LINESIZE];
! 193: char fmtline[LINESIZE];
! 194: const char *fmt;
1.1 deraadt 195: struct headline hl;
196: char *name;
1.17 millert 197: char *to, *from;
198: struct name *np;
199: char **ap;
1.1 deraadt 200:
201: mp = &message[mesg-1];
1.20 millert 202: (void)readline(setinput(mp), headline, LINESIZE, NULL);
1.25 ! millert 203: if ((subjline = hfield("subject", mp)) == NULL &&
! 204: (subjline = hfield("subj", mp)) == NULL)
! 205: subjline = "";
1.1 deraadt 206: /*
207: * Bletch!
208: */
209: curind = dot == mp ? '>' : ' ';
210: dispc = ' ';
211: if (mp->m_flag & MSAVED)
212: dispc = '*';
213: if (mp->m_flag & MPRESERVE)
214: dispc = 'P';
215: if ((mp->m_flag & (MREAD|MNEW)) == MNEW)
216: dispc = 'N';
217: if ((mp->m_flag & (MREAD|MNEW)) == 0)
218: dispc = 'U';
219: if (mp->m_flag & MBOX)
220: dispc = 'M';
221: parse(headline, &hl, pbuf);
1.17 millert 222: from = nameof(mp, 0);
223: to = skin(hfield("to", mp));
224: np = extract(from, GTO);
225: np = delname(np, myname);
226: if (altnames)
227: for (ap = altnames; *ap; ap++)
228: np = delname(np, *ap);
229: if (np)
230: /* not from me */
231: name = value("show-rcpt") != NULL && to ? to : from;
1.1 deraadt 232: else
1.17 millert 233: /* from me - show TO */
234: name = value("showto") != NULL && to ? to : from;
1.25 ! millert 235: strnvis(visname, name, sizeof(visname), VIS_SAFE|VIS_NOSLASH);
1.17 millert 236: if (name == to)
1.25 ! millert 237: fmt = "%c%c%3d TO %-14.14s %16.16s %4d/%-5d %s";
1.17 millert 238: else
1.25 ! millert 239: fmt = "%c%c%3d %-17.17s %16.16s %4d/%-5d %s";
! 240: strnvis(vissub, subjline, sizeof(vissub), VIS_SAFE|VIS_NOSLASH);
! 241: /* hl.l_date was sanity-checked when read in. */
! 242: snprintf(fmtline, sizeof(fmtline), fmt, curind, dispc, mesg, visname,
! 243: hl.l_date, mp->m_lines, mp->m_size, vissub);
! 244: printf("%.*s\n", screenwidth, fmtline);
1.1 deraadt 245: }
246:
247: /*
248: * Print out the value of dot.
249: */
250: int
1.21 millert 251: pdot(void *v)
1.1 deraadt 252: {
1.12 millert 253: printf("%d\n", (int)(dot - &message[0] + 1));
1.1 deraadt 254: return(0);
255: }
256:
257: /*
258: * Print out all the possible commands.
259: */
260: int
1.21 millert 261: pcmdlist(void *v)
1.1 deraadt 262: {
1.2 niklas 263: extern const struct cmd cmdtab[];
1.12 millert 264: const struct cmd *cp;
265: int cc;
1.1 deraadt 266:
1.6 millert 267: puts("Commands are:");
1.1 deraadt 268: for (cc = 0, cp = cmdtab; cp->c_name != NULL; cp++) {
269: cc += strlen(cp->c_name) + 2;
270: if (cc > 72) {
1.6 millert 271: putchar('\n');
1.1 deraadt 272: cc = strlen(cp->c_name) + 2;
273: }
1.8 millert 274: if ((cp+1)->c_name != NULL)
1.1 deraadt 275: printf("%s, ", cp->c_name);
276: else
1.6 millert 277: puts(cp->c_name);
1.1 deraadt 278: }
279: return(0);
280: }
281:
282: /*
1.17 millert 283: * Pipe message to command
284: */
285: int
1.21 millert 286: pipeit(void *ml, void *sl)
1.17 millert 287: {
288: int *msgvec = ml;
289: char *cmd = sl;
290:
291: return(type1(msgvec, cmd, 0, 0));
292: }
293:
294: /*
1.1 deraadt 295: * Paginate messages, honor ignored fields.
296: */
297: int
1.21 millert 298: more(void *v)
1.1 deraadt 299: {
1.3 deraadt 300: int *msgvec = v;
1.17 millert 301: return(type1(msgvec, NULL, 1, 1));
1.1 deraadt 302: }
303:
304: /*
305: * Paginate messages, even printing ignored fields.
306: */
307: int
1.21 millert 308: More(void *v)
1.1 deraadt 309: {
1.3 deraadt 310: int *msgvec = v;
1.1 deraadt 311:
1.17 millert 312: return(type1(msgvec, NULL, 0, 1));
1.1 deraadt 313: }
314:
315: /*
316: * Type out messages, honor ignored fields.
317: */
318: int
1.21 millert 319: type(void *v)
1.1 deraadt 320: {
1.3 deraadt 321: int *msgvec = v;
1.1 deraadt 322:
1.17 millert 323: return(type1(msgvec, NULL, 1, 0));
1.1 deraadt 324: }
325:
326: /*
327: * Type out messages, even printing ignored fields.
328: */
329: int
1.21 millert 330: Type(void *v)
1.1 deraadt 331: {
1.3 deraadt 332: int *msgvec = v;
1.1 deraadt 333:
1.17 millert 334: return(type1(msgvec, NULL, 0, 0));
1.1 deraadt 335: }
336:
337: /*
338: * Type out the messages requested.
339: */
340: int
1.21 millert 341: type1(int *msgvec, char *cmd, int doign, int page)
1.1 deraadt 342: {
1.19 millert 343: int nlines, *ip, restoreterm;
1.3 deraadt 344: struct message *mp;
1.19 millert 345: struct termios tbuf;
1.20 millert 346: char *cp;
347: FILE *obuf;
1.1 deraadt 348:
349: obuf = stdout;
1.19 millert 350: restoreterm = 0;
1.17 millert 351:
352: /*
353: * start a pipe if needed.
354: */
355: if (cmd) {
1.19 millert 356: restoreterm = (tcgetattr(fileno(stdin), &tbuf) == 0);
1.17 millert 357: obuf = Popen(cmd, "w");
358: if (obuf == NULL) {
1.20 millert 359: warn("%s", cmd);
1.17 millert 360: obuf = stdout;
361: }
362: } else if (value("interactive") != NULL &&
363: (page || (cp = value("crt")) != NULL)) {
1.1 deraadt 364: nlines = 0;
365: if (!page) {
366: for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++)
367: nlines += message[*ip - 1].m_lines;
368: }
369: if (page || nlines > (*cp ? atoi(cp) : realscreenheight)) {
1.19 millert 370: restoreterm = (tcgetattr(fileno(stdin), &tbuf) == 0);
1.17 millert 371: obuf = Popen(value("PAGER"), "w");
1.1 deraadt 372: if (obuf == NULL) {
1.16 millert 373: warn("%s", cp);
1.1 deraadt 374: obuf = stdout;
1.20 millert 375: }
1.1 deraadt 376: }
377: }
1.17 millert 378:
379: /*
1.21 millert 380: * Send messages to the output.
1.17 millert 381: */
1.1 deraadt 382: for (ip = msgvec; *ip && ip - msgvec < msgCount; ip++) {
383: mp = &message[*ip - 1];
384: touch(mp);
385: dot = mp;
1.17 millert 386: if (cmd == NULL && value("quiet") == NULL)
1.1 deraadt 387: fprintf(obuf, "Message %d:\n", *ip);
1.19 millert 388: if (sendmessage(mp, obuf, doign ? ignore : 0, NULL) == -1)
389: break;
1.1 deraadt 390: }
1.17 millert 391:
1.1 deraadt 392: if (obuf != stdout) {
1.6 millert 393: (void)Pclose(obuf);
1.19 millert 394: if (restoreterm)
395: (void)tcsetattr(fileno(stdin), TCSADRAIN, &tbuf);
1.1 deraadt 396: }
397: return(0);
398: }
399:
400: /*
401: * Print the top so many lines of each desired message.
402: * The number of lines is taken from the variable "toplines"
403: * and defaults to 5.
404: */
405: int
1.21 millert 406: top(void * v)
1.1 deraadt 407: {
1.3 deraadt 408: int *msgvec = v;
1.12 millert 409: int *ip;
410: struct message *mp;
1.1 deraadt 411: int c, topl, lines, lineb;
412: char *valtop, linebuf[LINESIZE];
413: FILE *ibuf;
414:
415: topl = 5;
416: valtop = value("toplines");
1.8 millert 417: if (valtop != NULL) {
1.1 deraadt 418: topl = atoi(valtop);
419: if (topl < 0 || topl > 10000)
420: topl = 5;
421: }
422: lineb = 1;
423: for (ip = msgvec; *ip && ip-msgvec < msgCount; ip++) {
424: mp = &message[*ip - 1];
425: touch(mp);
426: dot = mp;
1.8 millert 427: if (value("quiet") == NULL)
1.1 deraadt 428: printf("Message %d:\n", *ip);
429: ibuf = setinput(mp);
430: c = mp->m_lines;
431: if (!lineb)
1.6 millert 432: putchar('\n');
1.1 deraadt 433: for (lines = 0; lines < c && lines <= topl; lines++) {
1.20 millert 434: if (readline(ibuf, linebuf, sizeof(linebuf), NULL) < 0)
1.1 deraadt 435: break;
436: puts(linebuf);
437: lineb = blankline(linebuf);
438: }
439: }
440: return(0);
441: }
442:
443: /*
444: * Touch all the given messages so that they will
445: * get mboxed.
446: */
447: int
1.21 millert 448: stouch(void *v)
1.1 deraadt 449: {
1.3 deraadt 450: int *msgvec = v;
1.12 millert 451: int *ip;
1.1 deraadt 452:
453: for (ip = msgvec; *ip != 0; ip++) {
454: dot = &message[*ip-1];
455: dot->m_flag |= MTOUCH;
456: dot->m_flag &= ~MPRESERVE;
457: }
458: return(0);
459: }
460:
461: /*
462: * Make sure all passed messages get mboxed.
463: */
464: int
1.21 millert 465: mboxit(void *v)
1.1 deraadt 466: {
1.3 deraadt 467: int *msgvec = v;
1.12 millert 468: int *ip;
1.1 deraadt 469:
470: for (ip = msgvec; *ip != 0; ip++) {
471: dot = &message[*ip-1];
472: dot->m_flag |= MTOUCH|MBOX;
473: dot->m_flag &= ~MPRESERVE;
474: }
475: return(0);
476: }
477:
478: /*
479: * List the folders the user currently has.
480: */
481: int
1.21 millert 482: folders(void *v)
1.1 deraadt 483: {
1.17 millert 484: char *files = (char *)v;
1.5 deraadt 485: char dirname[PATHSIZE];
1.17 millert 486: char cmd[BUFSIZ];
1.1 deraadt 487:
1.22 millert 488: if (getfold(dirname, sizeof(dirname)) < 0)
489: strlcpy(dirname, "$HOME", sizeof(dirname));
1.17 millert 490:
491: snprintf(cmd, sizeof(cmd), "cd %s; %s %s", dirname, value("LISTER"),
492: files && *files ? files : "");
493:
494: (void)run_command(value("SHELL"), 0, -1, -1, "-c", cmd, NULL);
1.6 millert 495: return(0);
496: }
497:
498: /*
499: * Update the mail file with any new messages that have
500: * come in since we started reading mail.
501: */
502: int
1.21 millert 503: inc(void *v)
1.6 millert 504: {
505: int nmsg, mdot;
506:
507: nmsg = incfile();
508:
509: if (nmsg == 0) {
510: puts("No new mail.");
511: } else if (nmsg > 0) {
512: mdot = newfileinfo(msgCount - nmsg);
513: dot = &message[mdot - 1];
514: } else {
515: puts("\"inc\" command failed...");
516: }
517:
518: return(0);
1.20 millert 519: }
520:
521: /*
522: * User hit ^C while printing the headers.
523: */
524: void
1.21 millert 525: hdrint(int s)
1.20 millert 526: {
527:
528: gothdrint = 1;
1.1 deraadt 529: }