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