FIX "," message specification (since [1c4b8c9], v14.8.4)..
[s-mailx.git] / cmd1.c
blob03f0c7843c543000e3bef7566b1c7b8b1b6efa58
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ User commands.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2015 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
6 */
7 /*
8 * Copyright (c) 1980, 1993
9 * The Regents of the University of California. All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 3. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
35 #undef n_FILE
36 #define n_FILE cmd1
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 static int _screen;
43 static sigjmp_buf _cmd1_pipestop;
44 static sigjmp_buf _cmd1_pipejmp;
46 static void _cmd1_onpipe(int signo);
47 static void _cmd1_brokpipe(int signo);
49 /* Prepare and print "[Message: xy]:" intro */
50 static void _show_msg_overview(FILE *obuf, struct message *mp, int msg_no);
52 /* ... And place the extracted date in `date' */
53 static void _parse_from_(struct message *mp, char date[FROM_DATEBUF]);
55 /* Print out the header of a specific message
56 * __hprf: handle *headline*
57 * __subject: return -1 if Subject: yet seen, otherwise smalloc()d Subject:
58 * __putindent: print out the indenting in threaded display */
59 static void _print_head(size_t yetprinted, size_t msgno, FILE *f,
60 bool_t threaded);
61 static void __hprf(size_t yetprinted, char const *fmt, size_t msgno,
62 FILE *f, bool_t threaded, char const *attrlist);
63 static char * __subject(struct message *mp, bool_t threaded,
64 size_t yetprinted);
65 static int __putindent(FILE *fp, struct message *mp, int maxwidth);
67 static int _dispc(struct message *mp, char const *a);
69 /* Shared `z' implementation */
70 static int a_cmd_scroll(char const *arg, bool_t onlynew);
72 /* Shared `headers' implementation */
73 static int _headers(int msgspec);
75 /* Show the requested messages */
76 static int _type1(int *msgvec, bool_t doign, bool_t dopage, bool_t dopipe,
77 bool_t dodecode, char *cmd, ui64_t *tstats);
79 /* Pipe the requested messages */
80 static int _pipe1(char *str, int doign);
82 static void
83 _cmd1_onpipe(int signo)
85 NYD_X; /* Signal handler */
86 UNUSED(signo);
87 siglongjmp(_cmd1_pipejmp, 1);
90 static void
91 _cmd1_brokpipe(int signo)
93 NYD_X; /* Signal handler */
94 UNUSED(signo);
95 siglongjmp(_cmd1_pipestop, 1);
98 static void
99 _show_msg_overview(FILE *obuf, struct message *mp, int msg_no)
101 char const *cpre = "", *csuf = "";
102 NYD_ENTER;
104 #ifdef HAVE_COLOUR
105 if (colour_table != NULL) {
106 struct str const *sp;
108 if ((sp = colour_get(COLOURSPEC_MSGINFO)) != NULL)
109 cpre = sp->s;
110 csuf = colour_get(COLOURSPEC_RESET)->s;
112 #endif
113 fprintf(obuf, _("%s[-- Message %2d -- %lu lines, %lu bytes --]:%s\n"),
114 cpre, msg_no, (ul_i)mp->m_lines, (ul_i)mp->m_size, csuf);
115 NYD_LEAVE;
118 static void
119 _parse_from_(struct message *mp, char date[FROM_DATEBUF]) /* TODO line pool */
121 FILE *ibuf;
122 int hlen;
123 char *hline = NULL;
124 size_t hsize = 0;
125 NYD_ENTER;
127 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) != NULL &&
128 (hlen = readline_restart(ibuf, &hline, &hsize, 0)) > 0)
129 extract_date_from_from_(hline, hlen, date);
130 if (hline != NULL)
131 free(hline);
132 NYD_LEAVE;
135 static void
136 _print_head(size_t yetprinted, size_t msgno, FILE *f, bool_t threaded)
138 enum {attrlen = 14};
139 char attrlist[attrlen +1], *cp;
140 char const *fmt;
141 NYD_ENTER;
143 if ((cp = ok_vlook(attrlist)) != NULL) {
144 if (strlen(cp) == attrlen) {
145 memcpy(attrlist, cp, attrlen +1);
146 goto jattrok;
148 n_err(_("*attrlist* is not of the correct length, using builtin\n"));
151 if (ok_blook(bsdcompat) || ok_blook(bsdflags) || env_blook("SYSV3", FAL0)) {
152 char const bsdattr[attrlen +1] = "NU *HMFAT+-$~";
153 memcpy(attrlist, bsdattr, sizeof bsdattr);
154 } else {
155 char const pattr[attrlen +1] = "NUROSPMFAT+-$~";
156 memcpy(attrlist, pattr, sizeof pattr);
159 jattrok:
160 if ((fmt = ok_vlook(headline)) == NULL) {
161 fmt = ((ok_blook(bsdcompat) || ok_blook(bsdheadline))
162 ? "%>%a%m %-20f %16d %3l/%-5o %i%-S"
163 : "%>%a%m %-18f %16d %4l/%-5o %i%-s");
166 __hprf(yetprinted, fmt, msgno, f, threaded, attrlist);
167 NYD_LEAVE;
170 static void
171 __hprf(size_t yetprinted, char const *fmt, size_t msgno, FILE *f,
172 bool_t threaded, char const *attrlist)
174 char buf[16], datebuf[FROM_DATEBUF], *cp, *subjline;
175 char const *datefmt, *date, *name, *fp;
176 int i, n, s, wleft, subjlen;
177 struct message *mp;
178 time_t datet;
179 enum {
180 _NONE = 0,
181 _ISADDR = 1<<0,
182 _ISTO = 1<<1,
183 _IFMT = 1<<2,
184 _LOOP_MASK = (1<<3) - 1,
185 _SFMT = 1<<3
186 } flags = _NONE;
187 NYD_ENTER;
188 UNUSED(buf);
190 mp = message + msgno - 1;
191 datet = mp->m_time;
192 date = NULL;
194 datefmt = ok_vlook(datefield);
195 jredo:
196 if (datefmt != NULL) {
197 fp = hfield1("date", mp);/* TODO use m_date field! */
198 if (fp == NULL) {
199 datefmt = NULL;
200 goto jredo;
202 datet = rfctime(fp);
203 date = fakedate(datet);
204 fp = ok_vlook(datefield_markout_older);
205 i = (*datefmt != '\0');
206 if (fp != NULL)
207 i |= (*fp != '\0') ? 2 | 4 : 2; /* XXX no magics */
209 /* May we strftime(3)? */
210 if (i & (1 | 4))
211 memcpy(&time_current.tc_local, localtime(&datet),
212 sizeof time_current.tc_local);
214 if ((i & 2) && (datet > time_current.tc_time + DATE_SECSDAY ||
215 #define _6M ((DATE_DAYSYEAR / 2) * DATE_SECSDAY)
216 (datet + _6M < time_current.tc_time))) {
217 #undef _6M
218 if ((datefmt = (i & 4) ? fp : NULL) == NULL) {
219 memset(datebuf, ' ', FROM_DATEBUF); /* xxx ur */
220 memcpy(datebuf + 4, date + 4, 7);
221 datebuf[4 + 7] = ' ';
222 memcpy(datebuf + 4 + 7 + 1, date + 20, 4);
223 datebuf[4 + 7 + 1 + 4] = '\0';
224 date = datebuf;
226 } else if ((i & 1) == 0)
227 datefmt = NULL;
228 } else if (datet == (time_t)0 && !(mp->m_flag & MNOFROM)) {
229 /* TODO eliminate this path, query the FROM_ date in setptr(),
230 * TODO all other codepaths do so by themselves ALREADY ?????
231 * TODO assert(mp->m_time != 0);, then
232 * TODO ALSO changes behaviour of datefield_markout_older */
233 _parse_from_(mp, datebuf);
234 date = datebuf;
235 } else
236 date = fakedate(datet);
238 flags |= _ISADDR;
239 name = name1(mp, 0);
240 if (name != NULL && ok_blook(showto) && is_myname(skin(name))) {
241 if ((cp = hfield1("to", mp)) != NULL) {
242 name = cp;
243 flags |= _ISTO;
246 if (name == NULL) {
247 name = "";
248 flags &= ~_ISADDR;
250 if (flags & _ISADDR)
251 name = ok_blook(showname) ? realname(name) : prstr(skin(name));
253 subjline = NULL;
255 /* Detect the width of the non-format characters in *headline*;
256 * like that we can simply use putc() in the next loop, since we have
257 * already calculated their column widths (TODO it's sick) */
258 wleft = subjlen = scrnwidth;
260 for (fp = fmt; *fp != '\0'; ++fp) {
261 if (*fp == '%') {
262 if (*++fp == '-')
263 ++fp;
264 else if (*fp == '+')
265 ++fp;
266 if (digitchar(*fp)) {
267 n = 0;
269 n = 10*n + *fp - '0';
270 while (++fp, digitchar(*fp));
271 subjlen -= n;
273 if (*fp == 'i')
274 flags |= _IFMT;
276 if (*fp == '\0')
277 break;
278 } else {
279 #ifdef HAVE_WCWIDTH
280 if (mb_cur_max > 1) {
281 wchar_t wc;
282 if ((s = mbtowc(&wc, fp, mb_cur_max)) == -1)
283 n = s = 1;
284 else if ((n = wcwidth(wc)) == -1)
285 n = 1;
286 } else
287 #endif
288 n = s = 1;
289 subjlen -= n;
290 wleft -= n;
291 while (--s > 0)
292 ++fp;
296 /* Walk *headline*, producing output TODO not (really) MB safe */
297 for (fp = fmt; *fp != '\0'; ++fp) {
298 char c;
299 if ((c = *fp & 0xFF) != '%')
300 putc(c, f);
301 else {
302 flags &= _LOOP_MASK;
303 n = 0;
304 s = 1;
305 if (*++fp == '-') {
306 s = -1;
307 ++fp;
308 } else if (*fp == '+')
309 ++fp;
310 if (digitchar(*fp)) {
312 n = 10*n + *fp - '0';
313 while (++fp, digitchar(*fp));
315 if (*fp == '\0')
316 break;
318 n *= s;
319 switch ((c = *fp & 0xFF)) {
320 case '%':
321 goto jputc;
322 case '>':
323 case '<':
324 if (dot != mp)
325 c = ' ';
326 goto jputc;
327 case '$':
328 #ifdef HAVE_SPAM
329 if (n == 0)
330 n = 5;
331 if (UICMP(32, ABS(n), >, wleft))
332 n = (n < 0) ? -wleft : wleft;
333 snprintf(buf, sizeof buf, "%u.%02u",
334 (mp->m_spamscore >> 8), (mp->m_spamscore & 0xFF));
335 n = fprintf(f, "%*s", n, buf);
336 wleft = (n >= 0) ? wleft - n : 0;
337 break;
338 #else
339 c = '?';
340 goto jputc;
341 #endif
342 case 'a':
343 c = _dispc(mp, attrlist);
344 jputc:
345 if (UICMP(32, ABS(n), >, wleft))
346 n = (n < 0) ? -wleft : wleft;
347 n = fprintf(f, "%*c", n, c);
348 wleft = (n >= 0) ? wleft - n : 0;
349 break;
350 case 'd':
351 if (datefmt != NULL) {
352 i = strftime(datebuf, sizeof datebuf, datefmt,
353 &time_current.tc_local);
354 if (i != 0)
355 date = datebuf;
356 else
357 n_err(_("Ignored date format, it excesses the target "
358 "buffer (%lu bytes)\n"), (ul_i)sizeof(datebuf));
359 datefmt = NULL;
361 if (n == 0)
362 n = 16;
363 if (UICMP(32, ABS(n), >, wleft))
364 n = (n < 0) ? -wleft : wleft;
365 n = fprintf(f, "%*.*s", n, n, date);
366 wleft = (n >= 0) ? wleft - n : 0;
367 break;
368 case 'e':
369 if (n == 0)
370 n = 2;
371 if (UICMP(32, ABS(n), >, wleft))
372 n = (n < 0) ? -wleft : wleft;
373 n = fprintf(f, "%*u", n, (threaded == 1 ? mp->m_level : 0));
374 wleft = (n >= 0) ? wleft - n : 0;
375 break;
376 case 'f':
377 if (n == 0) {
378 n = 18;
379 if (s < 0)
380 n = -n;
382 i = ABS(n);
383 if (i > wleft) {
384 i = wleft;
385 n = (n < 0) ? -wleft : wleft;
387 if (flags & _ISTO) /* XXX tr()! */
388 i -= 3;
389 n = fprintf(f, "%s%s", ((flags & _ISTO) ? "To " : ""),
390 colalign(name, i, n, &wleft));
391 if (n < 0)
392 wleft = 0;
393 else if (flags & _ISTO)
394 wleft -= 3;
395 break;
396 case 'i':
397 if (threaded) {
398 n = __putindent(f, mp, MIN(wleft, scrnwidth - 60));
399 wleft = (n >= 0) ? wleft - n : 0;
401 break;
402 case 'l':
403 if (n == 0)
404 n = 4;
405 if (UICMP(32, ABS(n), >, wleft))
406 n = (n < 0) ? -wleft : wleft;
407 if (mp->m_xlines) {
408 n = fprintf(f, "%*ld", n, mp->m_xlines);
409 wleft = (n >= 0) ? wleft - n : 0;
410 } else {
411 n = ABS(n);
412 wleft -= n;
413 while (n-- != 0)
414 putc(' ', f);
416 break;
417 case 'm':
418 if (n == 0) {
419 n = 3;
420 if (threaded)
421 for (i = msgCount; i > 999; i /= 10)
422 ++n;
424 if (UICMP(32, ABS(n), >, wleft))
425 n = (n < 0) ? -wleft : wleft;
426 n = fprintf(f, "%*lu", n, (ul_i)msgno);
427 wleft = (n >= 0) ? wleft - n : 0;
428 break;
429 case 'o':
430 if (n == 0)
431 n = -5;
432 if (UICMP(32, ABS(n), >, wleft))
433 n = (n < 0) ? -wleft : wleft;
434 n = fprintf(f, "%*lu", n, (long)mp->m_xsize);
435 wleft = (n >= 0) ? wleft - n : 0;
436 break;
437 case 'S':
438 flags |= _SFMT;
439 /*FALLTHRU*/
440 case 's':
441 if (n == 0)
442 n = subjlen - 2;
443 if (n > 0 && s < 0)
444 n = -n;
445 if (subjlen > wleft)
446 subjlen = wleft;
447 if (UICMP(32, ABS(n), >, subjlen))
448 n = (n < 0) ? -subjlen : subjlen;
449 if (flags & _SFMT)
450 n -= (n < 0) ? -2 : 2;
451 if (n == 0)
452 break;
453 if (subjline == NULL)
454 subjline = __subject(mp, (threaded && (flags & _IFMT)),
455 yetprinted);
456 if (subjline == (char*)-1) {
457 n = fprintf(f, "%*s", n, "");
458 wleft = (n >= 0) ? wleft - n : 0;
459 } else {
460 n = fprintf(f, ((flags & _SFMT) ? "\"%s\"" : "%s"),
461 colalign(subjline, ABS(n), n, &wleft));
462 if (n < 0)
463 wleft = 0;
465 break;
466 case 'T': { /* Message recipient flags */
467 /* We never can reuse "name" since it's the full name */
468 struct name const *np = lextract(hfield1("to", mp), GTO | GSKIN);
469 c = ' ';
470 i = 0;
471 j_A_redo:
472 for (; np != NULL; np = np->n_flink) {
473 switch (is_mlist(np->n_name, FAL0)) {
474 case MLIST_SUBSCRIBED: c = 'S'; goto jputc;
475 case MLIST_KNOWN: c = 'L'; goto jputc;
476 case MLIST_OTHER:
477 default: break;
480 if (i != 0)
481 goto jputc;
482 ++i;
483 np = lextract(hfield1("cc", mp), GCC | GSKIN);
484 goto j_A_redo;
486 case 't':
487 if (n == 0) {
488 n = 3;
489 if (threaded)
490 for (i = msgCount; i > 999; i /= 10)
491 ++n;
493 if (UICMP(32, ABS(n), >, wleft))
494 n = (n < 0) ? -wleft : wleft;
495 n = fprintf(f, "%*lu", n,
496 (threaded ? (ul_i)mp->m_threadpos : (ul_i)msgno));
497 wleft = (n >= 0) ? wleft - n : 0;
498 break;
499 case 'U':
500 #ifdef HAVE_IMAP
501 if (n == 0)
502 n = 9;
503 if (UICMP(32, ABS(n), >, wleft))
504 n = (n < 0) ? -wleft : wleft;
505 n = fprintf(f, "%*lu", n, mp->m_uid);
506 wleft = (n >= 0) ? wleft - n : 0;
507 break;
508 #else
509 c = '?';
510 goto jputc;
511 #endif
512 default:
513 if (options & OPT_D_V)
514 n_err(_("Unkown *headline* format: \"%%%c\"\n"), c);
515 c = '?';
516 goto jputc;
519 if (wleft <= 0)
520 break;
523 putc('\n', f);
525 if (subjline != NULL && subjline != (char*)-1)
526 free(subjline);
527 NYD_LEAVE;
530 static char *
531 __subject(struct message *mp, bool_t threaded, size_t yetprinted)
533 struct str in, out;
534 char *rv = (char*)-1, *ms;
535 NYD_ENTER;
537 if ((ms = hfield1("subject", mp)) == NULL)
538 goto jleave;
540 in.l = strlen(in.s = ms);
541 mime_fromhdr(&in, &out, TD_ICONV | TD_ISPR);
542 rv = ms = out.s;
544 if (!threaded || mp->m_level == 0)
545 goto jleave;
547 /* In a display thread - check wether this message uses the same
548 * Subject: as it's parent or elder neighbour, suppress printing it if
549 * this is the case. To extend this a bit, ignore any leading Re: or
550 * Fwd: plus follow-up WS. Ignore invisible messages along the way */
551 ms = subject_re_trim(ms);
553 for (; (mp = prev_in_thread(mp)) != NULL && yetprinted-- > 0;) {
554 char *os;
556 if (visible(mp) && (os = hfield1("subject", mp)) != NULL) {
557 struct str oout;
558 int x;
560 in.l = strlen(in.s = os);
561 mime_fromhdr(&in, &oout, TD_ICONV | TD_ISPR);
562 x = asccasecmp(ms, subject_re_trim(oout.s));
563 free(oout.s);
565 if (!x) {
566 free(out.s);
567 rv = (char*)-1;
569 break;
572 jleave:
573 NYD_LEAVE;
574 return rv;
577 static int
578 __putindent(FILE *fp, struct message *mp, int maxwidth)/* XXX no magic consts */
580 struct message *mq;
581 int *us, indlvl, indw, i, important = MNEW | MFLAGGED;
582 char *cs;
583 NYD_ENTER;
585 if (mp->m_level == 0 || maxwidth == 0) {
586 indw = 0;
587 goto jleave;
590 cs = ac_alloc(mp->m_level);
591 us = ac_alloc(mp->m_level * sizeof *us);
593 i = mp->m_level - 1;
594 if (mp->m_younger && UICMP(32, i + 1, ==, mp->m_younger->m_level)) {
595 if (mp->m_parent && mp->m_parent->m_flag & important)
596 us[i] = mp->m_flag & important ? 0x2523 : 0x2520;
597 else
598 us[i] = mp->m_flag & important ? 0x251D : 0x251C;
599 cs[i] = '+';
600 } else {
601 if (mp->m_parent && mp->m_parent->m_flag & important)
602 us[i] = mp->m_flag & important ? 0x2517 : 0x2516;
603 else
604 us[i] = mp->m_flag & important ? 0x2515 : 0x2514;
605 cs[i] = '\\';
608 mq = mp->m_parent;
609 for (i = mp->m_level - 2; i >= 0; --i) {
610 if (mq) {
611 if (UICMP(32, i, >, mq->m_level - 1)) {
612 us[i] = cs[i] = ' ';
613 continue;
615 if (mq->m_younger) {
616 if (mq->m_parent && (mq->m_parent->m_flag & important))
617 us[i] = 0x2503;
618 else
619 us[i] = 0x2502;
620 cs[i] = '|';
621 } else
622 us[i] = cs[i] = ' ';
623 mq = mq->m_parent;
624 } else
625 us[i] = cs[i] = ' ';
628 --maxwidth;
629 for (indlvl = indw = 0; (ui8_t)indlvl < mp->m_level && indw < maxwidth;
630 ++indlvl) {
631 if (indw < maxwidth - 1)
632 indw += (int)putuc(us[indlvl], cs[indlvl] & 0xFF, fp);
633 else
634 indw += (int)putuc(0x21B8, '^', fp);
636 indw += putuc(0x25B8, '>', fp);
638 ac_free(us);
639 ac_free(cs);
640 jleave:
641 NYD_LEAVE;
642 return indw;
645 static int
646 _dispc(struct message *mp, char const *a)
648 int i = ' ';
649 NYD_ENTER;
651 if ((mp->m_flag & (MREAD | MNEW)) == MREAD)
652 i = a[3];
653 if ((mp->m_flag & (MREAD | MNEW)) == (MREAD | MNEW))
654 i = a[2];
655 if (mp->m_flag & MANSWERED)
656 i = a[8];
657 if (mp->m_flag & MDRAFTED)
658 i = a[9];
659 if ((mp->m_flag & (MREAD | MNEW)) == MNEW)
660 i = a[0];
661 if (!(mp->m_flag & (MREAD | MNEW)))
662 i = a[1];
663 if (mp->m_flag & MSPAM)
664 i = a[12];
665 if (mp->m_flag & MSPAMUNSURE)
666 i = a[13];
667 if (mp->m_flag & MSAVED)
668 i = a[4];
669 if (mp->m_flag & MPRESERVE)
670 i = a[5];
671 if (mp->m_flag & (MBOX | MBOXED))
672 i = a[6];
673 if (mp->m_flag & MFLAGGED)
674 i = a[7];
675 if (mb.mb_threaded == 1 && mp->m_collapsed > 0)
676 i = a[11];
677 if (mb.mb_threaded == 1 && mp->m_collapsed < 0)
678 i = a[10];
679 NYD_LEAVE;
680 return i;
683 static int
684 a_cmd_scroll(char const *arg, bool_t onlynew){
685 long l;
686 char *eptr;
687 bool_t isabs;
688 int msgspec, size, maxs;
689 NYD2_ENTER;
691 msgspec = onlynew ? -1 : 0;
692 size = screensize();
693 maxs = msgCount / size;
695 switch(*arg){
696 case '\0':
697 ++_screen;
698 goto jfwd;
699 case '$':
700 if(arg[1] != '\0')
701 goto jerr;
702 if(_screen == maxs)
703 goto jerrfwd;
704 _screen = maxs;
705 break;
706 case '+':
707 if(arg[1] == '\0')
708 ++_screen;
709 else{
710 isabs = FAL0;
712 ++arg;
713 if(0){
714 case '1': case '2': case '3': case '4': case '5':
715 case '6': case '7': case '8': case '9': case '0':
716 isabs = TRU1;
718 l = strtol(arg, &eptr, 10);
719 if(*eptr != '\0')
720 goto jerr;
721 if(l > maxs - (isabs ? 0 : _screen))
722 goto jerrfwd;
723 _screen = isabs ? (int)l : _screen + l;
725 jfwd:
726 if(_screen > maxs){
727 jerrfwd:
728 _screen = maxs;
729 printf(_("On last screenful of messages\n"));
731 break;
733 case '-':
734 if(arg[1] == '\0')
735 --_screen;
736 else{
737 ++arg;
738 l = strtol(arg, &eptr, 10);
739 if(*eptr != '\0')
740 goto jerr;
741 if(l > _screen)
742 goto jerrbwd;
743 _screen -= l;
745 if(_screen < 0){
746 jerrbwd:
747 _screen = 0;
748 printf(_("On first screenful of messages\n"));
750 if(msgspec == -1)
751 msgspec = -2;
752 break;
753 default:
754 jerr:
755 n_err(_("Unrecognized scrolling command \"%s\"\n"), arg);
756 size = 1;
757 goto jleave;
760 size = _headers(msgspec);
761 jleave:
762 NYD2_LEAVE;
763 return size;
766 static int
767 _headers(int msgspec) /* FIXME rework v14.8; also: Neitzel mail, 2014-08-21 */
769 ui32_t flag;
770 int g, k, mesg, size, lastg = 1;
771 struct message *mp, *mq, *lastmq = NULL;
772 enum mflag fl = MNEW | MFLAGGED;
773 NYD_ENTER;
775 time_current_update(&time_current, FAL0);
777 flag = 0;
778 size = screensize();
779 if (_screen < 0)
780 _screen = 0;
781 #if 0 /* FIXME original code path */
782 k = _screen * size;
783 #else
784 if (msgspec <= 0)
785 k = _screen * size;
786 else
787 k = msgspec;
788 #endif
789 if (k >= msgCount)
790 k = msgCount - size;
791 if (k < 0)
792 k = 0;
794 if (mb.mb_threaded == 0) {
795 g = 0;
796 mq = message;
797 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
798 if (visible(mp)) {
799 if (g % size == 0)
800 mq = mp;
801 if (mp->m_flag & fl) {
802 lastg = g;
803 lastmq = mq;
805 if ((msgspec > 0 && PTRCMP(mp, ==, message + msgspec - 1)) ||
806 (msgspec == 0 && g == k) ||
807 (msgspec == -2 && g == k + size && lastmq) ||
808 (msgspec < 0 && g >= k && (mp->m_flag & fl) != 0))
809 break;
810 g++;
812 if (lastmq && (msgspec == -2 ||
813 (msgspec == -1 && PTRCMP(mp, ==, message + msgCount)))) {
814 g = lastg;
815 mq = lastmq;
817 _screen = g / size;
818 mp = mq;
819 mesg = (int)PTR2SIZE(mp - message);
820 if (PTRCMP(dot, !=, message + msgspec - 1)) { /* TODO really?? */
821 for (mq = mp; PTRCMP(mq, <, message + msgCount); ++mq)
822 if (visible(mq)) {
823 setdot(mq);
824 break;
827 #ifdef HAVE_IMAP
828 if (mb.mb_type == MB_IMAP)
829 imap_getheaders(mesg + 1, mesg + size);
830 #endif
831 srelax_hold();
832 for (; PTRCMP(mp, <, message + msgCount); ++mp) {
833 ++mesg;
834 if (!visible(mp))
835 continue;
836 if (UICMP(32, flag++, >=, size))
837 break;
838 _print_head(0, mesg, stdout, 0);
839 srelax();
841 srelax_rele();
842 } else { /* threaded */
843 g = 0;
844 mq = threadroot;
845 for (mp = threadroot; mp; mp = next_in_thread(mp))
846 if (visible(mp) &&
847 (mp->m_collapsed <= 0 ||
848 PTRCMP(mp, ==, message + msgspec - 1))) {
849 if (g % size == 0)
850 mq = mp;
851 if (mp->m_flag & fl) {
852 lastg = g;
853 lastmq = mq;
855 if ((msgspec > 0 && PTRCMP(mp, ==, message + msgspec - 1)) ||
856 (msgspec == 0 && g == k) ||
857 (msgspec == -2 && g == k + size && lastmq) ||
858 (msgspec < 0 && g >= k && (mp->m_flag & fl) != 0))
859 break;
860 g++;
862 if (lastmq && (msgspec == -2 ||
863 (msgspec == -1 && PTRCMP(mp, ==, message + msgCount)))) {
864 g = lastg;
865 mq = lastmq;
867 _screen = g / size;
868 mp = mq;
869 if (PTRCMP(dot, !=, message + msgspec - 1)) { /* TODO really?? */
870 for (mq = mp; mq; mq = next_in_thread(mq))
871 if (visible(mq) && mq->m_collapsed <= 0) {
872 setdot(mq);
873 break;
876 srelax_hold();
877 while (mp) {
878 if (visible(mp) &&
879 (mp->m_collapsed <= 0 ||
880 PTRCMP(mp, ==, message + msgspec - 1))) {
881 if (UICMP(32, flag++, >=, size))
882 break;
883 _print_head(flag - 1, PTR2SIZE(mp - message + 1), stdout,
884 mb.mb_threaded);
885 srelax();
887 mp = next_in_thread(mp);
889 srelax_rele();
892 if (!flag)
893 printf(_("No more mail.\n"));
894 NYD_LEAVE;
895 return !flag;
898 static int
899 _type1(int *msgvec, bool_t doign, bool_t dopage, bool_t dopipe,
900 bool_t dodecode, char *cmd, ui64_t *tstats)
902 ui64_t mstats[1];
903 int rv, *ip;
904 struct message *mp;
905 char const *cp;
906 FILE * volatile obuf;
907 bool_t volatile hadsig = FAL0, isrelax = FAL0;
908 NYD_ENTER;
909 {/* C89.. */
910 enum sendaction const action = ((dopipe && ok_blook(piperaw))
911 ? SEND_MBOX : dodecode
912 ? SEND_SHOW : doign
913 ? SEND_TODISP : SEND_TODISP_ALL);
914 bool_t const volatile formfeed = (dopipe && ok_blook(page));
915 obuf = stdout;
917 if (sigsetjmp(_cmd1_pipestop, 1)) {
918 hadsig = TRU1;
919 goto jclose_pipe;
922 if (dopipe) {
923 if ((cp = ok_vlook(SHELL)) == NULL)
924 cp = XSHELL;
925 if ((obuf = Popen(cmd, "w", cp, NULL, 1)) == NULL) {
926 n_perr(cmd, 0);
927 obuf = stdout;
928 } else
929 safe_signal(SIGPIPE, &_cmd1_brokpipe);
930 } else if ((options & OPT_TTYOUT) && (dopage ||
931 ((options & OPT_INTERACTIVE) && (cp = ok_vlook(crt)) != NULL))) {
932 char const *pager = NULL;
933 size_t nlines = 0;
935 if (!dopage) {
936 for (ip = msgvec; *ip && PTRCMP(ip - msgvec, <, msgCount); ++ip) {
937 mp = message + *ip - 1;
938 if (!(mp->m_have & HAVE_BODY))
939 if (get_body(mp) != OKAY) {
940 rv = 1;
941 goto jleave;
943 nlines += mp->m_lines + 1; /* Message info XXX and PARTS... */
947 /* >= not <: we return to the prompt */
948 if (dopage || UICMP(z, nlines, >=,
949 (*cp != '\0' ? atoi(cp) : realscreenheight))) {
950 char const *env_add[2];
951 pager = get_pager(env_add + 0);
952 env_add[1] = NULL;
953 obuf = Popen(pager, "w", NULL, env_add, 1);
954 if (obuf == NULL) {
955 n_perr(pager, 0);
956 obuf = stdout;
957 pager = NULL;
958 } else
959 safe_signal(SIGPIPE, &_cmd1_brokpipe);
961 #ifdef HAVE_COLOUR
962 if (IS_TTY_SESSION() && action != SEND_MBOX)
963 colour_table_create(pager != NULL); /* (salloc()s!) */
964 #endif
966 #ifdef HAVE_COLOUR
967 else if (IS_TTY_SESSION() && action != SEND_MBOX)
968 colour_table_create(FAL0); /* (salloc()s!) */
969 #endif
971 /*TODO unless we have our signal manager special care must be taken */
972 srelax_hold();
973 isrelax = TRU1;
974 for (ip = msgvec; *ip && PTRCMP(ip - msgvec, <, msgCount); ++ip) {
975 mp = message + *ip - 1;
976 touch(mp);
977 setdot(mp);
978 uncollapse1(mp, 1);
979 if (!dopipe && ip != msgvec)
980 fprintf(obuf, "\n");
981 if (action != SEND_MBOX)
982 _show_msg_overview(obuf, mp, *ip);
983 sendmp(mp, obuf, (doign ? ignore : NULL), NULL, action, mstats);
984 srelax();
985 if (formfeed) /* TODO a nicer way to separate piped messages! */
986 putc('\f', obuf);
987 if (tstats != NULL)
988 tstats[0] += mstats[0];
990 srelax_rele();
991 isrelax = FAL0;
993 jclose_pipe:
994 if (obuf != stdout) {
995 /* Ignore SIGPIPE so it can't cause a duplicate close */
996 safe_signal(SIGPIPE, SIG_IGN);
997 if (hadsig && isrelax)
998 srelax_rele();
999 colour_reset(obuf); /* XXX hacky; only here because we still jump */
1000 Pclose(obuf, TRU1);
1001 safe_signal(SIGPIPE, dflpipe);
1003 rv = 0;
1005 jleave:
1006 NYD_LEAVE;
1007 return rv;
1010 static int
1011 _pipe1(char *str, int doign)
1013 ui64_t stats[1];
1014 char *cmd;
1015 int *msgvec, rv = 1;
1016 bool_t needs_list;
1017 NYD_ENTER;
1019 if ((cmd = laststring(str, &needs_list, TRU1)) == NULL) {
1020 cmd = ok_vlook(cmd);
1021 if (cmd == NULL || *cmd == '\0') {
1022 n_err(_("Variable *cmd* not set\n"));
1023 goto jleave;
1027 msgvec = salloc((msgCount + 2) * sizeof *msgvec);
1029 if (!needs_list) {
1030 *msgvec = first(0, MMNORM);
1031 if (*msgvec == 0) {
1032 if (pstate & PS_HOOK_MASK) {
1033 rv = 0;
1034 goto jleave;
1036 puts(_("No messages to pipe."));
1037 goto jleave;
1039 msgvec[1] = 0;
1040 } else if (getmsglist(str, msgvec, 0) < 0)
1041 goto jleave;
1042 if (*msgvec == 0) {
1043 if (pstate & PS_HOOK_MASK) {
1044 rv = 0;
1045 goto jleave;
1047 printf("No applicable messages.\n");
1048 goto jleave;
1051 printf(_("Pipe to: \"%s\"\n"), cmd);
1052 stats[0] = 0;
1053 if ((rv = _type1(msgvec, doign, FAL0, TRU1, FAL0, cmd, stats)) == 0)
1054 printf("\"%s\" %" PRIu64 " bytes\n", cmd, stats[0]);
1055 jleave:
1056 NYD_LEAVE;
1057 return rv;
1060 FL int
1061 c_cmdnotsupp(void *v) /* TODO -> lex.c */
1063 NYD_ENTER;
1064 UNUSED(v);
1065 n_err(_("The requested feature is not compiled in\n"));
1066 NYD_LEAVE;
1067 return 1;
1070 FL int
1071 c_headers(void *v)
1073 int rv;
1074 NYD_ENTER;
1076 rv = print_header_group((int*)v);
1077 NYD_LEAVE;
1078 return rv;
1081 FL int
1082 print_header_group(int *vector)
1084 int rv;
1085 NYD_ENTER;
1087 assert(vector != NULL && vector != (void*)-1);
1088 rv = _headers(vector[0]);
1089 NYD_LEAVE;
1090 return rv;
1093 FL int
1094 c_scroll(void *v)
1096 int rv;
1097 NYD_ENTER;
1099 rv = a_cmd_scroll(v, FAL0);
1100 NYD_LEAVE;
1101 return rv;
1104 FL int
1105 c_Scroll(void *v)
1107 int rv;
1108 NYD_ENTER;
1110 rv = a_cmd_scroll(v, TRU1);
1111 NYD_LEAVE;
1112 return rv;
1115 FL int
1116 c_from(void *v)
1118 int *msgvec = v, *ip, n;
1119 char *cp;
1120 FILE * volatile obuf;
1121 NYD_ENTER;
1123 time_current_update(&time_current, FAL0);
1124 obuf = stdout;
1126 /* TODO unfixable memory leaks still */
1127 if (IS_TTY_SESSION() && (cp = ok_vlook(crt)) != NULL) {
1128 for (n = 0, ip = msgvec; *ip != 0; ++ip)
1129 n++;
1130 if (n > (*cp == '\0' ? screensize() : atoi(cp)) + 3) {
1131 char const *p;
1132 if (sigsetjmp(_cmd1_pipejmp, 1))
1133 goto jendpipe;
1134 p = get_pager(NULL);
1135 if ((obuf = Popen(p, "w", NULL, NULL, 1)) == NULL) {
1136 n_perr(p, 0);
1137 obuf = stdout;
1138 cp = NULL;
1139 } else
1140 safe_signal(SIGPIPE, &_cmd1_onpipe);
1144 srelax_hold();
1145 for (n = 0, ip = msgvec; *ip != 0; ++ip) { /* TODO join into _print_head() */
1146 _print_head((size_t)n++, (size_t)*ip, obuf, mb.mb_threaded);
1147 srelax();
1149 srelax_rele();
1151 if (--ip >= msgvec)
1152 setdot(message + *ip - 1);
1154 jendpipe:
1155 if (obuf != stdout) {
1156 safe_signal(SIGPIPE, SIG_IGN);
1157 Pclose(obuf, TRU1);
1158 safe_signal(SIGPIPE, dflpipe);
1160 NYD_LEAVE;
1161 return 0;
1164 FL void
1165 print_headers(size_t bottom, size_t topx, bool_t only_marked)
1167 size_t printed;
1168 NYD_ENTER;
1170 #ifdef HAVE_IMAP
1171 if (mb.mb_type == MB_IMAP)
1172 imap_getheaders(bottom, topx);
1173 #endif
1174 time_current_update(&time_current, FAL0);
1176 srelax_hold();
1177 for (printed = 0; bottom <= topx; ++bottom) {
1178 struct message *mp = message + bottom - 1;
1179 if (only_marked) {
1180 if (!(mp->m_flag & MMARK))
1181 continue;
1182 } else if (!visible(mp))
1183 continue;
1184 _print_head(printed++, bottom, stdout, FAL0);
1185 srelax();
1187 srelax_rele();
1188 NYD_LEAVE;
1191 FL int
1192 c_pdot(void *v)
1194 NYD_ENTER;
1195 UNUSED(v);
1196 printf("%d\n", (int)PTR2SIZE(dot - message + 1));
1197 NYD_LEAVE;
1198 return 0;
1201 FL int
1202 c_more(void *v)
1204 int *msgvec = v, rv;
1205 NYD_ENTER;
1207 rv = _type1(msgvec, TRU1, TRU1, FAL0, FAL0, NULL, NULL);
1208 NYD_LEAVE;
1209 return rv;
1212 FL int
1213 c_More(void *v)
1215 int *msgvec = v, rv;
1216 NYD_ENTER;
1218 rv = _type1(msgvec, FAL0, TRU1, FAL0, FAL0, NULL, NULL);
1219 NYD_LEAVE;
1220 return rv;
1223 FL int
1224 c_type(void *v)
1226 int *msgvec = v, rv;
1227 NYD_ENTER;
1229 rv = _type1(msgvec, TRU1, FAL0, FAL0, FAL0, NULL, NULL);
1230 NYD_LEAVE;
1231 return rv;
1234 FL int
1235 c_Type(void *v)
1237 int *msgvec = v, rv;
1238 NYD_ENTER;
1240 rv = _type1(msgvec, FAL0, FAL0, FAL0, FAL0, NULL, NULL);
1241 NYD_LEAVE;
1242 return rv;
1245 FL int
1246 c_show(void *v)
1248 int *msgvec = v, rv;
1249 NYD_ENTER;
1251 rv = _type1(msgvec, FAL0, FAL0, FAL0, TRU1, NULL, NULL);
1252 NYD_LEAVE;
1253 return rv;
1256 FL int
1257 c_pipe(void *v)
1259 char *str = v;
1260 int rv;
1261 NYD_ENTER;
1263 rv = _pipe1(str, 1);
1264 NYD_LEAVE;
1265 return rv;
1268 FL int
1269 c_Pipe(void *v)
1271 char *str = v;
1272 int rv;
1273 NYD_ENTER;
1275 rv = _pipe1(str, 0);
1276 NYD_LEAVE;
1277 return rv;
1280 FL int
1281 c_top(void *v)
1283 int *msgvec = v, *ip, c, topl, lines, empty_last;
1284 struct message *mp;
1285 char *cp, *linebuf = NULL;
1286 size_t linesize = 0;
1287 FILE *ibuf;
1288 NYD_ENTER;
1290 topl = 5;
1291 cp = ok_vlook(toplines);
1292 if (cp != NULL) {
1293 topl = atoi(cp);
1294 if (topl < 0 || topl > 10000)
1295 topl = 5;
1298 #ifdef HAVE_COLOUR
1299 if (IS_TTY_SESSION())
1300 colour_table_create(FAL0); /* (salloc()s) */
1301 #endif
1302 empty_last = 1;
1303 for (ip = msgvec; *ip != 0 && UICMP(z, PTR2SIZE(ip - msgvec), <, msgCount);
1304 ++ip) {
1305 mp = message + *ip - 1;
1306 touch(mp);
1307 setdot(mp);
1308 pstate |= PS_DID_PRINT_DOT;
1309 if (!empty_last)
1310 printf("\n");
1311 _show_msg_overview(stdout, mp, *ip);
1312 if (mp->m_flag & MNOFROM)
1313 /* XXX c_top(): coloured output? */
1314 printf("From %s %s\n", fakefrom(mp), fakedate(mp->m_time));
1315 if ((ibuf = setinput(&mb, mp, NEED_BODY)) == NULL) { /* XXX could use TOP */
1316 v = NULL;
1317 break;
1319 c = mp->m_lines;
1320 for (lines = 0; lines < c && UICMP(32, lines, <=, topl); ++lines) {
1321 if (readline_restart(ibuf, &linebuf, &linesize, 0) < 0)
1322 break;
1323 puts(linebuf);
1325 for (cp = linebuf; *cp != '\0' && blankchar(*cp); ++cp)
1327 empty_last = (*cp == '\0');
1331 if (linebuf != NULL)
1332 free(linebuf);
1333 NYD_LEAVE;
1334 return (v != NULL);
1337 FL int
1338 c_stouch(void *v)
1340 int *msgvec = v, *ip;
1341 NYD_ENTER;
1343 for (ip = msgvec; *ip != 0; ++ip) {
1344 setdot(message + *ip - 1);
1345 dot->m_flag |= MTOUCH;
1346 dot->m_flag &= ~MPRESERVE;
1347 pstate |= PS_DID_PRINT_DOT;
1349 NYD_LEAVE;
1350 return 0;
1353 FL int
1354 c_mboxit(void *v)
1356 int *msgvec = v, *ip;
1357 NYD_ENTER;
1359 if (pstate & PS_EDIT) {
1360 n_err(_("`mbox' can only be used in a system mailbox\n")); /* TODO */
1361 goto jleave;
1364 for (ip = msgvec; *ip != 0; ++ip) {
1365 setdot(message + *ip - 1);
1366 dot->m_flag |= MTOUCH | MBOX;
1367 dot->m_flag &= ~MPRESERVE;
1368 pstate |= PS_DID_PRINT_DOT;
1370 jleave:
1371 NYD_LEAVE;
1372 return 0;
1375 FL int
1376 c_folders(void *v)
1378 char dirname[PATH_MAX], *name, **argv = v;
1379 char const *cmd;
1380 int rv = 1;
1381 NYD_ENTER;
1383 if (*argv) {
1384 name = expand(*argv);
1385 if (name == NULL)
1386 goto jleave;
1387 } else if (!getfold(dirname, sizeof dirname)) {
1388 n_err(_("No value set for \"folder\"\n"));
1389 goto jleave;
1390 } else
1391 name = dirname;
1393 if (which_protocol(name) == PROTO_IMAP) {
1394 #ifdef HAVE_IMAP
1395 imap_folders(name, *argv == NULL);
1396 #else
1397 rv = c_cmdnotsupp(NULL);
1398 #endif
1399 } else {
1400 if ((cmd = ok_vlook(LISTER)) == NULL)
1401 cmd = XLISTER;
1402 run_command(cmd, 0, -1, -1, name, NULL, NULL);
1404 jleave:
1405 NYD_LEAVE;
1406 return rv;
1409 /* s-it-mode */