Tweak previous, it added a bad memory access
[s-mailx.git] / cmd-headers.c
blobbdf140049a0c3d513f65a1d668e525c7363b1e17
1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Header display, search, etc., related user commands.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2017 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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 cmd_headers
38 #ifndef HAVE_AMALGAMATION
39 # include "nail.h"
40 #endif
42 static int _screen;
44 /* ... And place the extracted date in `date' */
45 static void _parse_from_(struct message *mp, char date[n_FROM_DATEBUF]);
47 /* Print out the header of a specific message
48 * __hprf: handle *headline*
49 * __subject: return -1 if Subject: yet seen, otherwise smalloc()d Subject:
50 * __putindent: print out the indenting in threaded display */
51 static void _print_head(size_t yetprinted, size_t msgno, FILE *f,
52 bool_t threaded);
53 static void __hprf(size_t yetprinted, char const *fmt, size_t msgno,
54 FILE *f, bool_t threaded, char const *attrlist);
55 static char * __subject(struct message *mp, bool_t threaded,
56 size_t yetprinted);
57 static int __putindent(FILE *fp, struct message *mp, int maxwidth);
59 static int _dispc(struct message *mp, char const *a);
61 /* Shared `z' implementation */
62 static int a_cmd_scroll(char const *arg, bool_t onlynew);
64 /* Shared `headers' implementation */
65 static int _headers(int msgspec);
67 static void
68 _parse_from_(struct message *mp, char date[n_FROM_DATEBUF]) /* TODO line pool */
70 FILE *ibuf;
71 int hlen;
72 char *hline = NULL;
73 size_t hsize = 0;
74 NYD_ENTER;
76 if ((ibuf = setinput(&mb, mp, NEED_HEADER)) != NULL &&
77 (hlen = readline_restart(ibuf, &hline, &hsize, 0)) > 0)
78 extract_date_from_from_(hline, hlen, date);
79 if (hline != NULL)
80 free(hline);
81 NYD_LEAVE;
84 static void
85 _print_head(size_t yetprinted, size_t msgno, FILE *f, bool_t threaded)
87 enum {attrlen = 14};
88 char attrlist[attrlen +1], *cp;
89 char const *fmt;
90 NYD_ENTER;
92 if ((cp = ok_vlook(attrlist)) != NULL) {
93 if (strlen(cp) == attrlen) {
94 memcpy(attrlist, cp, attrlen +1);
95 goto jattrok;
97 n_err(_("*attrlist* is not of the correct length, using builtin\n"));
100 if (ok_blook(bsdcompat) || ok_blook(bsdflags)) {
101 char const bsdattr[attrlen +1] = "NU *HMFAT+-$~";
102 memcpy(attrlist, bsdattr, sizeof bsdattr);
103 } else if (ok_blook(SYSV3)) {
104 char const bsdattr[attrlen +1] = "NU *HMFAT+-$~";
105 memcpy(attrlist, bsdattr, sizeof bsdattr);
106 n_OBSOLETE(_("*SYSV3*: please use *bsdcompat* or *bsdflags*, "
107 "or set *attrlist*"));
108 } else {
109 char const pattr[attrlen +1] = "NUROSPMFAT+-$~";
110 memcpy(attrlist, pattr, sizeof pattr);
113 jattrok:
114 if ((fmt = ok_vlook(headline)) == NULL) {
115 fmt = ((ok_blook(bsdcompat) || ok_blook(bsdheadline))
116 ? "%>%a%m %-20f %16d %4l/%-5o %i%-S"
117 : "%>%a%m %-18f %-16d %4l/%-5o %i%-s");
120 __hprf(yetprinted, fmt, msgno, f, threaded, attrlist);
121 NYD_LEAVE;
124 static void
125 __hprf(size_t yetprinted, char const *fmt, size_t msgno, FILE *f,
126 bool_t threaded, char const *attrlist)
128 char buf[16], datebuf[n_FROM_DATEBUF], cbuf[8], *cp, *subjline;
129 char const *datefmt, *date, *name, *fp n_COLOUR( COMMA *colo_tag );
130 int i, n, s, wleft, subjlen;
131 struct message *mp;
132 time_t datet;
133 n_COLOUR( struct n_colour_pen *cpen_new COMMA *cpen_cur COMMA *cpen_bas; )
134 enum {
135 _NONE = 0,
136 _ISDOT = 1<<0,
137 _ISADDR = 1<<1,
138 _ISTO = 1<<2,
139 _IFMT = 1<<3,
140 _LOOP_MASK = (1<<4) - 1,
141 _SFMT = 1<<4, /* It is 'S' */
142 /* For the simple byte-based counts in wleft and n we sometimes need
143 * adjustments to compensate for additional bytes of UTF-8 sequences */
144 _PUTCB_UTF8_SHIFT = 5,
145 _PUTCB_UTF8_MASK = 3<<5
146 } flags = _NONE;
147 NYD_ENTER;
148 n_UNUSED(buf);
150 if ((mp = message + msgno - 1) == dot)
151 flags = _ISDOT;
152 datet = mp->m_time;
153 date = NULL;
154 n_COLOUR( colo_tag = NULL; )
156 datefmt = ok_vlook(datefield);
157 jredo:
158 if (datefmt != NULL) {
159 fp = hfield1("date", mp);/* TODO use m_date field! */
160 if (fp == NULL) {
161 datefmt = NULL;
162 goto jredo;
164 datet = rfctime(fp);
165 date = fakedate(datet);
166 fp = ok_vlook(datefield_markout_older);
167 i = (*datefmt != '\0');
168 if (fp != NULL)
169 i |= (*fp != '\0') ? 2 | 4 : 2; /* XXX no magics */
171 /* May we strftime(3)? */
172 if (i & (1 | 4))
173 memcpy(&time_current.tc_local, localtime(&datet),
174 sizeof time_current.tc_local);
176 if ((i & 2) &&
177 (UICMP(64,datet, >, time_current.tc_time + n_DATE_SECSDAY) ||
178 #define _6M ((n_DATE_DAYSYEAR / 2) * n_DATE_SECSDAY)
179 UICMP(64,datet + _6M, <, time_current.tc_time))) {
180 #undef _6M
181 if ((datefmt = (i & 4) ? fp : NULL) == NULL) {
182 memset(datebuf, ' ', n_FROM_DATEBUF); /* xxx ur */
183 memcpy(datebuf + 4, date + 4, 7);
184 datebuf[4 + 7] = ' ';
185 memcpy(datebuf + 4 + 7 + 1, date + 20, 4);
186 datebuf[4 + 7 + 1 + 4] = '\0';
187 date = datebuf;
189 n_COLOUR( colo_tag = n_COLOUR_TAG_SUM_OLDER; )
190 } else if ((i & 1) == 0)
191 datefmt = NULL;
192 } else if (datet == (time_t)0 && !(mp->m_flag & MNOFROM)) {
193 /* TODO eliminate this path, query the FROM_ date in setptr(),
194 * TODO all other codepaths do so by themselves ALREADY ?????
195 * TODO assert(mp->m_time != 0);, then
196 * TODO ALSO changes behaviour of datefield_markout_older */
197 _parse_from_(mp, datebuf);
198 date = datebuf;
199 } else
200 date = fakedate(datet);
202 flags |= _ISADDR;
203 name = name1(mp, 0);
204 if (name != NULL && ok_blook(showto) && is_myname(skin(name))) {
205 if ((cp = hfield1("to", mp)) != NULL) {
206 name = cp;
207 flags |= _ISTO;
210 if (name == NULL) {
211 name = n_empty;
212 flags &= ~_ISADDR;
214 if (flags & _ISADDR)
215 name = ok_blook(showname) ? realname(name) : prstr(skin(name));
217 subjline = NULL;
219 /* Detect the width of the non-format characters in *headline*;
220 * like that we can simply use putc() in the next loop, since we have
221 * already calculated their column widths (TODO it's sick) */
222 wleft = subjlen = n_scrnwidth;
224 for (fp = fmt; *fp != '\0'; ++fp) {
225 if (*fp == '%') {
226 if (*++fp == '-')
227 ++fp;
228 else if (*fp == '+')
229 ++fp;
230 if (digitchar(*fp)) {
231 n = 0;
233 n = 10*n + *fp - '0';
234 while (++fp, digitchar(*fp));
235 subjlen -= n;
237 if (*fp == 'i')
238 flags |= _IFMT;
240 if (*fp == '\0')
241 break;
242 } else {
243 #ifdef HAVE_WCWIDTH
244 if (n_mb_cur_max > 1) {
245 wchar_t wc;
246 if ((s = mbtowc(&wc, fp, n_mb_cur_max)) == -1)
247 n = s = 1;
248 else if ((n = wcwidth(wc)) == -1)
249 n = 1;
250 } else
251 #endif
252 n = s = 1;
253 subjlen -= n;
254 wleft -= n;
255 while (--s > 0)
256 ++fp;
260 /* Walk *headline*, producing output TODO not (really) MB safe */
261 #ifdef HAVE_COLOUR
262 if(n_COLOUR_IS_ACTIVE()){
263 if(flags & _ISDOT)
264 colo_tag = n_COLOUR_TAG_SUM_DOT;
265 cpen_bas = n_colour_pen_create(n_COLOUR_ID_SUM_HEADER, colo_tag);
266 n_colour_pen_put(cpen_new = cpen_cur = cpen_bas);
267 }else
268 cpen_new = cpen_bas = cpen_cur = NULL;
269 #endif
271 for (fp = fmt; *fp != '\0'; ++fp) {
272 char c;
274 if ((c = *fp & 0xFF) != '%') {
275 n_COLOUR(
276 if(n_COLOUR_IS_ACTIVE() && (cpen_new = cpen_bas) != cpen_cur)
277 n_colour_pen_put(cpen_cur = cpen_new);
279 putc(c, f);
280 continue;
283 flags &= _LOOP_MASK;
284 n = 0;
285 s = 1;
286 if ((c = *++fp) == '-') {
287 s = -1;
288 ++fp;
289 } else if (c == '+')
290 ++fp;
291 if (digitchar(*fp)) {
293 n = 10*n + *fp - '0';
294 while (++fp, digitchar(*fp));
297 if ((c = *fp & 0xFF) == '\0')
298 break;
299 n *= s;
301 cbuf[1] = '\0';
302 switch (c) {
303 case '%':
304 goto jputcb;
305 case '>':
306 case '<':
307 if (flags & _ISDOT) {
308 n_COLOUR(
309 if(n_COLOUR_IS_ACTIVE())
310 cpen_new = n_colour_pen_create(n_COLOUR_ID_SUM_DOTMARK,
311 colo_tag);
313 if (n_psonce & n_PSO_UNICODE) {
314 if (c == '>')
315 /* 25B8;BLACK RIGHT-POINTING SMALL TRIANGLE: â–¸ */
316 cbuf[1] = (char)0x96, cbuf[2] = (char)0xB8;
317 else
318 /* 25C2;BLACK LEFT-POINTING SMALL TRIANGLE: â—‚ */
319 cbuf[1] = (char)0x97, cbuf[2] = (char)0x82;
320 c = (char)0xE2;
321 cbuf[3] = '\0';
322 flags |= 2 << _PUTCB_UTF8_SHIFT;
324 } else
325 c = ' ';
326 goto jputcb;
327 case '$':
328 #ifdef HAVE_SPAM
329 if (n == 0)
330 n = 5;
331 if (UICMP(32, n_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 jputcb;
341 #endif
342 case 'a':
343 c = _dispc(mp, attrlist);
344 jputcb:
345 #ifdef HAVE_COLOUR
346 if(n_COLOUR_IS_ACTIVE()){
347 if(cpen_new == cpen_cur)
348 cpen_new = cpen_bas;
349 if(cpen_new != cpen_cur)
350 n_colour_pen_put(cpen_cur = cpen_new);
352 #endif
353 if (UICMP(32, n_ABS(n), >, wleft))
354 n = (n < 0) ? -wleft : wleft;
355 cbuf[0] = c;
356 n = fprintf(f, "%*s", n, cbuf);
357 if (n >= 0) {
358 wleft -= n;
359 if ((n = (flags & _PUTCB_UTF8_MASK)) != 0) {
360 n >>= _PUTCB_UTF8_SHIFT;
361 wleft += n;
363 } else {
364 wleft = 0; /* TODO I/O error.. ? break? */
366 #ifdef HAVE_COLOUR
367 if(n_COLOUR_IS_ACTIVE() && (cpen_new = cpen_bas) != cpen_cur)
368 n_colour_pen_put(cpen_cur = cpen_new);
369 #endif
370 break;
371 case 'd':
372 if (datefmt != NULL) {
373 i = strftime(datebuf, sizeof datebuf, datefmt,
374 &time_current.tc_local);
375 if (i != 0)
376 date = datebuf;
377 else
378 n_err(_("Ignored date format, it excesses the target "
379 "buffer (%lu bytes)\n"), (ul_i)sizeof(datebuf));
380 datefmt = NULL;
382 if (n == 0)
383 n = 16;
384 if (UICMP(32, n_ABS(n), >, wleft))
385 n = (n < 0) ? -wleft : wleft;
386 n = fprintf(f, "%*.*s", n, n_ABS(n), date);
387 wleft = (n >= 0) ? wleft - n : 0;
388 break;
389 case 'e':
390 if (n == 0)
391 n = 2;
392 if (UICMP(32, n_ABS(n), >, wleft))
393 n = (n < 0) ? -wleft : wleft;
394 n = fprintf(f, "%*u", n, (threaded == 1 ? mp->m_level : 0));
395 wleft = (n >= 0) ? wleft - n : 0;
396 break;
397 case 'f':
398 if (n == 0) {
399 n = 18;
400 if (s < 0)
401 n = -n;
403 i = n_ABS(n);
404 if (i > wleft) {
405 i = wleft;
406 n = (n < 0) ? -wleft : wleft;
408 if (flags & _ISTO) /* XXX tr()! */
409 i -= 3;
410 n = fprintf(f, "%s%s", ((flags & _ISTO) ? "To " : n_empty),
411 colalign(name, i, n, &wleft));
412 if (n < 0)
413 wleft = 0;
414 else if (flags & _ISTO)
415 wleft -= 3;
416 break;
417 case 'i':
418 if (threaded) {
419 #ifdef HAVE_COLOUR
420 if(n_COLOUR_IS_ACTIVE()){
421 cpen_new = n_colour_pen_create(n_COLOUR_ID_SUM_THREAD, colo_tag);
422 if(cpen_new != cpen_cur)
423 n_colour_pen_put(cpen_cur = cpen_new);
425 #endif
426 n = __putindent(f, mp, n_MIN(wleft, (int)n_scrnwidth - 60));
427 wleft = (n >= 0) ? wleft - n : 0;
428 #ifdef HAVE_COLOUR
429 if(n_COLOUR_IS_ACTIVE() && (cpen_new = cpen_bas) != cpen_cur)
430 n_colour_pen_put(cpen_cur = cpen_new);
431 #endif
433 break;
434 case 'l':
435 if (n == 0)
436 n = 4;
437 if (UICMP(32, n_ABS(n), >, wleft))
438 n = (n < 0) ? -wleft : wleft;
439 if (mp->m_xlines) {
440 n = fprintf(f, "%*ld", n, mp->m_xlines);
441 wleft = (n >= 0) ? wleft - n : 0;
442 } else {
443 n = n_ABS(n);
444 wleft -= n;
445 while (n-- != 0)
446 putc(' ', f);
448 break;
449 case 'm':
450 if (n == 0) {
451 n = 3;
452 if (threaded)
453 for (i = msgCount; i > 999; i /= 10)
454 ++n;
456 if (UICMP(32, n_ABS(n), >, wleft))
457 n = (n < 0) ? -wleft : wleft;
458 n = fprintf(f, "%*lu", n, (ul_i)msgno);
459 wleft = (n >= 0) ? wleft - n : 0;
460 break;
461 case 'o':
462 if (n == 0)
463 n = -5;
464 if (UICMP(32, n_ABS(n), >, wleft))
465 n = (n < 0) ? -wleft : wleft;
466 n = fprintf(f, "%*lu", n, (ul_i)mp->m_xsize);
467 wleft = (n >= 0) ? wleft - n : 0;
468 break;
469 case 'S':
470 flags |= _SFMT;
471 /*FALLTHRU*/
472 case 's':
473 if (n == 0)
474 n = subjlen - 2;
475 if (n > 0 && s < 0)
476 n = -n;
477 if (subjlen > wleft)
478 subjlen = wleft;
479 if (UICMP(32, n_ABS(n), >, subjlen))
480 n = (n < 0) ? -subjlen : subjlen;
481 if (flags & _SFMT)
482 n -= (n < 0) ? -2 : 2;
483 if (n == 0)
484 break;
485 if (subjline == NULL)
486 subjline = __subject(mp, (threaded && (flags & _IFMT)), yetprinted);
487 if (subjline == (char*)-1) {
488 n = fprintf(f, "%*s", n, n_empty);
489 wleft = (n >= 0) ? wleft - n : 0;
490 } else {
491 n = fprintf(f, ((flags & _SFMT) ? "\"%s\"" : "%s"),
492 colalign(subjline, n_ABS(n), n, &wleft));
493 if (n < 0)
494 wleft = 0;
496 break;
497 case 'T': { /* Message recipient flags */
498 /* We never can reuse "name" since it's the full name */
499 struct name const *np = lextract(hfield1("to", mp), GTO | GSKIN);
500 c = ' ';
501 i = 0;
502 j_A_redo:
503 for (; np != NULL; np = np->n_flink) {
504 switch (is_mlist(np->n_name, FAL0)) {
505 case MLIST_SUBSCRIBED: c = 'S'; goto jputcb;
506 case MLIST_KNOWN: c = 'L'; goto jputcb;
507 case MLIST_OTHER:
508 default: break;
511 if (i != 0)
512 goto jputcb;
513 ++i;
514 np = lextract(hfield1("cc", mp), GCC | GSKIN);
515 goto j_A_redo;
517 case 't':
518 if (n == 0) {
519 n = 3;
520 if (threaded)
521 for (i = msgCount; i > 999; i /= 10)
522 ++n;
524 if (UICMP(32, n_ABS(n), >, wleft))
525 n = (n < 0) ? -wleft : wleft;
526 n = fprintf(f, "%*lu",
527 n, (threaded ? (ul_i)mp->m_threadpos : (ul_i)msgno));
528 wleft = (n >= 0) ? wleft - n : 0;
529 break;
530 default:
531 if (n_poption & n_PO_D_V)
532 n_err(_("Unkown *headline* format: %%%c\n"), c);
533 c = '?';
534 goto jputcb;
537 if (wleft <= 0)
538 break;
541 n_COLOUR( n_colour_reset(); )
542 putc('\n', f);
544 if (subjline != NULL && subjline != (char*)-1)
545 free(subjline);
546 NYD_LEAVE;
549 static char *
550 __subject(struct message *mp, bool_t threaded, size_t yetprinted)
552 struct str in, out;
553 char *rv, *ms;
554 NYD_ENTER;
556 rv = (char*)-1;
558 if ((ms = hfield1("subject", mp)) == NULL)
559 goto jleave;
561 in.l = strlen(in.s = ms);
562 mime_fromhdr(&in, &out, TD_ICONV | TD_ISPR);
563 rv = ms = out.s;
565 if (!threaded || mp->m_level == 0)
566 goto jleave;
568 /* In a display thread - check whether this message uses the same
569 * Subject: as it's parent or elder neighbour, suppress printing it if
570 * this is the case. To extend this a bit, ignore any leading Re: or
571 * Fwd: plus follow-up WS. Ignore invisible messages along the way */
572 ms = n_UNCONST(subject_re_trim(n_UNCONST(ms)));
574 for (; (mp = prev_in_thread(mp)) != NULL && yetprinted-- > 0;) {
575 char *os;
577 if (visible(mp) && (os = hfield1("subject", mp)) != NULL) {
578 struct str oout;
579 int x;
581 in.l = strlen(in.s = os);
582 mime_fromhdr(&in, &oout, TD_ICONV | TD_ISPR);
583 x = asccasecmp(ms, subject_re_trim(oout.s));
584 free(oout.s);
586 if (!x) {
587 free(out.s);
588 rv = (char*)-1;
590 break;
593 jleave:
594 NYD_LEAVE;
595 return rv;
598 static int
599 __putindent(FILE *fp, struct message *mp, int maxwidth)/* XXX no magic consts */
601 struct message *mq;
602 int *us, indlvl, indw, i, important = MNEW | MFLAGGED;
603 char *cs;
604 NYD_ENTER;
606 if (mp->m_level == 0 || maxwidth == 0) {
607 indw = 0;
608 goto jleave;
611 cs = ac_alloc(mp->m_level);
612 us = ac_alloc(mp->m_level * sizeof *us);
614 i = mp->m_level - 1;
615 if (mp->m_younger && UICMP(32, i + 1, ==, mp->m_younger->m_level)) {
616 if (mp->m_parent && mp->m_parent->m_flag & important)
617 us[i] = mp->m_flag & important ? 0x2523 : 0x2520;
618 else
619 us[i] = mp->m_flag & important ? 0x251D : 0x251C;
620 cs[i] = '+';
621 } else {
622 if (mp->m_parent && mp->m_parent->m_flag & important)
623 us[i] = mp->m_flag & important ? 0x2517 : 0x2516;
624 else
625 us[i] = mp->m_flag & important ? 0x2515 : 0x2514;
626 cs[i] = '\\';
629 mq = mp->m_parent;
630 for (i = mp->m_level - 2; i >= 0; --i) {
631 if (mq) {
632 if (UICMP(32, i, >, mq->m_level - 1)) {
633 us[i] = cs[i] = ' ';
634 continue;
636 if (mq->m_younger) {
637 if (mq->m_parent && (mq->m_parent->m_flag & important))
638 us[i] = 0x2503;
639 else
640 us[i] = 0x2502;
641 cs[i] = '|';
642 } else
643 us[i] = cs[i] = ' ';
644 mq = mq->m_parent;
645 } else
646 us[i] = cs[i] = ' ';
649 --maxwidth;
650 for (indlvl = indw = 0; (ui8_t)indlvl < mp->m_level && indw < maxwidth;
651 ++indlvl) {
652 if (indw < maxwidth - 1)
653 indw += (int)putuc(us[indlvl], cs[indlvl] & 0xFF, fp);
654 else
655 indw += (int)putuc(0x21B8, '^', fp);
657 indw += putuc(0x25B8, '>', fp);
659 ac_free(us);
660 ac_free(cs);
661 jleave:
662 NYD_LEAVE;
663 return indw;
666 static int
667 _dispc(struct message *mp, char const *a)
669 int i = ' ';
670 NYD_ENTER;
672 if ((mp->m_flag & (MREAD | MNEW)) == MREAD)
673 i = a[3];
674 if ((mp->m_flag & (MREAD | MNEW)) == (MREAD | MNEW))
675 i = a[2];
676 if (mp->m_flag & MANSWERED)
677 i = a[8];
678 if (mp->m_flag & MDRAFTED)
679 i = a[9];
680 if ((mp->m_flag & (MREAD | MNEW)) == MNEW)
681 i = a[0];
682 if (!(mp->m_flag & (MREAD | MNEW)))
683 i = a[1];
684 if (mp->m_flag & MSPAM)
685 i = a[12];
686 if (mp->m_flag & MSPAMUNSURE)
687 i = a[13];
688 if (mp->m_flag & MSAVED)
689 i = a[4];
690 if (mp->m_flag & MPRESERVE)
691 i = a[5];
692 if (mp->m_flag & (MBOX | MBOXED))
693 i = a[6];
694 if (mp->m_flag & MFLAGGED)
695 i = a[7];
696 if (mb.mb_threaded == 1 && mp->m_collapsed > 0)
697 i = a[11];
698 if (mb.mb_threaded == 1 && mp->m_collapsed < 0)
699 i = a[10];
700 NYD_LEAVE;
701 return i;
704 static int
705 a_cmd_scroll(char const *arg, bool_t onlynew){
706 siz_t l;
707 bool_t isabs;
708 int msgspec, size, maxs;
709 NYD2_ENTER;
711 /* TODO scroll problem: we do not know whether + and $ have already reached
712 * TODO the last screen in threaded mode */
713 msgspec = onlynew ? -1 : 0;
714 size = (int)/*TODO*/n_screensize();
715 if((maxs = msgCount / size) > 0 && msgCount % size == 0)
716 --maxs;
718 if(arg == NULL)
719 arg = n_empty;
720 switch(*arg){
721 case '\0':
722 ++_screen;
723 goto jfwd;
724 case '^':
725 if(arg[1] != '\0')
726 goto jerr;
727 if(_screen == 0)
728 goto jerrbwd;
729 _screen = 0;
730 break;
731 case '$':
732 if(arg[1] != '\0')
733 goto jerr;
734 if(_screen == maxs)
735 goto jerrfwd;
736 _screen = maxs;
737 break;
738 case '+':
739 if(arg[1] == '\0')
740 ++_screen;
741 else{
742 isabs = FAL0;
744 ++arg;
745 if(0){
746 case '1': case '2': case '3': case '4': case '5':
747 case '6': case '7': case '8': case '9': case '0':
748 isabs = TRU1;
750 if((n_idec_siz_cp(&l, arg, 0, NULL
751 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
752 ) != n_IDEC_STATE_CONSUMED)
753 goto jerr;
754 if(l > maxs - (isabs ? 0 : _screen))
755 goto jerrfwd;
756 _screen = isabs ? (int)l : _screen + l;
758 jfwd:
759 if(_screen > maxs){
760 jerrfwd:
761 _screen = maxs;
762 fprintf(n_stdout, _("On last screenful of messages\n"));
764 break;
766 case '-':
767 if(arg[1] == '\0')
768 --_screen;
769 else{
770 if((n_idec_siz_cp(&l, ++arg, 0, NULL
771 ) & (n_IDEC_STATE_EMASK | n_IDEC_STATE_CONSUMED)
772 ) != n_IDEC_STATE_CONSUMED)
773 goto jerr;
774 if(l > _screen)
775 goto jerrbwd;
776 _screen -= l;
778 if(_screen < 0){
779 jerrbwd:
780 _screen = 0;
781 fprintf(n_stdout, _("On first screenful of messages\n"));
783 if(msgspec == -1)
784 msgspec = -2;
785 break;
786 default:
787 jerr:
788 n_err(_("Unrecognized scrolling command: %s\n"), arg);
789 size = 1;
790 goto jleave;
793 size = _headers(msgspec);
794 jleave:
795 NYD2_LEAVE;
796 return size;
799 static int
800 _headers(int msgspec) /* TODO rework v15 */
802 ui32_t volatile flag;
803 int g, k, mesg, size;
804 int volatile lastg = 1;
805 struct message *mp, *mq, *lastmq = NULL;
806 enum mflag fl = MNEW | MFLAGGED;
807 NYD_ENTER;
809 time_current_update(&time_current, FAL0);
810 flag = 0;
812 size = (int)/*TODO*/n_screensize();
813 if (_screen < 0)
814 _screen = 0;
815 #if 0 /* FIXME original code path */
816 k = _screen * size;
817 #else
818 if (msgspec <= 0)
819 k = _screen * size;
820 else
821 k = msgspec;
822 #endif
823 if (k >= msgCount)
824 k = msgCount - size;
825 if (k < 0)
826 k = 0;
828 if (mb.mb_threaded == 0) {
829 g = 0;
830 mq = message;
831 for (mp = message; PTRCMP(mp, <, message + msgCount); ++mp)
832 if (visible(mp)) {
833 if (g % size == 0)
834 mq = mp;
835 if (mp->m_flag & fl) {
836 lastg = g;
837 lastmq = mq;
839 if ((msgspec > 0 && PTRCMP(mp, ==, message + msgspec - 1)) ||
840 (msgspec == 0 && g == k) ||
841 (msgspec == -2 && g == k + size && lastmq) ||
842 (msgspec < 0 && g >= k && (mp->m_flag & fl) != 0))
843 break;
844 g++;
846 if (lastmq && (msgspec == -2 ||
847 (msgspec == -1 && PTRCMP(mp, ==, message + msgCount)))) {
848 g = lastg;
849 mq = lastmq;
851 _screen = g / size;
853 mp = mq;
854 mesg = (int)PTR2SIZE(mp - message);
855 if (PTRCMP(dot, !=, message + msgspec - 1)) { /* TODO really?? */
856 for (mq = mp; PTRCMP(mq, <, message + msgCount); ++mq)
857 if (visible(mq)) {
858 setdot(mq);
859 break;
863 n_COLOUR( n_colour_env_create(n_COLOUR_CTX_SUM, n_stdout, FAL0); )
864 srelax_hold();
865 for (; PTRCMP(mp, <, message + msgCount); ++mp) {
866 ++mesg;
867 if (!visible(mp))
868 continue;
869 if (UICMP(32, flag++, >=, size))
870 break;
871 _print_head(0, mesg, n_stdout, 0);
872 srelax();
874 srelax_rele();
875 n_COLOUR( n_colour_env_gut(); )
876 } else { /* threaded */
877 g = 0;
878 mq = threadroot;
879 for (mp = threadroot; mp; mp = next_in_thread(mp))
880 if (visible(mp) &&
881 (mp->m_collapsed <= 0 ||
882 PTRCMP(mp, ==, message + msgspec - 1))) {
883 if (g % size == 0)
884 mq = mp;
885 if (mp->m_flag & fl) {
886 lastg = g;
887 lastmq = mq;
889 if ((msgspec > 0 && PTRCMP(mp, ==, message + msgspec - 1)) ||
890 (msgspec == 0 && g == k) ||
891 (msgspec == -2 && g == k + size && lastmq) ||
892 (msgspec < 0 && g >= k && (mp->m_flag & fl) != 0))
893 break;
894 g++;
896 if (lastmq && (msgspec == -2 ||
897 (msgspec == -1 && PTRCMP(mp, ==, message + msgCount)))) {
898 g = lastg;
899 mq = lastmq;
901 _screen = g / size;
902 mp = mq;
903 if (PTRCMP(dot, !=, message + msgspec - 1)) { /* TODO really?? */
904 for (mq = mp; mq; mq = next_in_thread(mq))
905 if (visible(mq) && mq->m_collapsed <= 0) {
906 setdot(mq);
907 break;
911 n_COLOUR( n_colour_env_create(n_COLOUR_CTX_SUM, n_stdout, FAL0); )
912 srelax_hold();
913 while (mp) {
914 if (visible(mp) &&
915 (mp->m_collapsed <= 0 ||
916 PTRCMP(mp, ==, message + msgspec - 1))) {
917 if (UICMP(32, flag++, >=, size))
918 break;
919 _print_head(flag - 1, PTR2SIZE(mp - message + 1), n_stdout,
920 mb.mb_threaded);
921 srelax();
923 mp = next_in_thread(mp);
925 srelax_rele();
926 n_COLOUR( n_colour_env_gut(); )
929 if (!flag) {
930 fprintf(n_stdout, _("No more mail.\n"));
931 if (n_pstate & (n_PS_ROBOT | n_PS_HOOK_MASK))
932 flag = !flag;
934 NYD_LEAVE;
935 return !flag;
938 FL int
939 c_headers(void *v)
941 int rv;
942 NYD_ENTER;
944 rv = print_header_group((int*)v);
945 NYD_LEAVE;
946 return rv;
949 FL int
950 print_header_group(int *vector)
952 int rv;
953 NYD_ENTER;
955 assert(vector != NULL && vector != (void*)-1);
956 rv = _headers(vector[0]);
957 NYD_LEAVE;
958 return rv;
961 FL int
962 c_scroll(void *v)
964 int rv;
965 NYD_ENTER;
967 rv = a_cmd_scroll(*(char const**)v, FAL0);
968 NYD_LEAVE;
969 return rv;
972 FL int
973 c_Scroll(void *v)
975 int rv;
976 NYD_ENTER;
978 rv = a_cmd_scroll(*(char const**)v, TRU1);
979 NYD_LEAVE;
980 return rv;
983 FL int
984 c_dotmove(void *v)
986 char const *args;
987 int msgvec[2], rv;
988 NYD_ENTER;
990 if (*(args = v) == '\0' || args[1] != '\0') {
991 jerr:
992 n_err(_("Synopsis: dotmove: up <-> or down <+> by one message\n"));
993 rv = 1;
994 } else switch (args[0]) {
995 case '-':
996 case '+':
997 if (msgCount == 0) {
998 fprintf(n_stdout, _("At EOF\n"));
999 rv = 0;
1000 } else if (getmsglist(n_UNCONST(/*TODO*/ args), msgvec, 0) > 0) {
1001 setdot(message + msgvec[0] - 1);
1002 msgvec[1] = 0;
1003 rv = c_headers(msgvec);
1004 } else
1005 rv = 1;
1006 break;
1007 default:
1008 goto jerr;
1010 NYD_LEAVE;
1011 return rv;
1014 FL int
1015 c_from(void *v)
1017 int *msgvec = v, *ip, n;
1018 char *cp;
1019 FILE * volatile obuf;
1020 NYD_ENTER;
1022 time_current_update(&time_current, FAL0);
1024 obuf = n_stdout;
1026 if (n_psonce & n_PSO_INTERACTIVE) {
1027 if ((cp = ok_vlook(crt)) != NULL) {
1028 uiz_t ib;
1030 for (n = 0, ip = msgvec; *ip != 0; ++ip)
1031 ++n;
1033 if(*cp == '\0')
1034 ib = n_screensize();
1035 else
1036 n_idec_uiz_cp(&ib, cp, 0, NULL);
1037 if (UICMP(z, n, >, ib) && (obuf = n_pager_open()) == NULL)
1038 obuf = n_stdout;
1042 /* Update dot before display so that the dotmark etc. are correct */
1043 for (ip = msgvec; *ip != 0; ++ip)
1045 if (--ip >= msgvec)
1046 setdot(message + *ip - 1);
1048 n_COLOUR( n_colour_env_create(n_COLOUR_CTX_SUM, obuf, obuf != n_stdout); )
1049 srelax_hold();
1050 for (n = 0, ip = msgvec; *ip != 0; ++ip) { /* TODO join into _print_head() */
1051 _print_head((size_t)n++, (size_t)*ip, obuf, mb.mb_threaded);
1052 srelax();
1054 srelax_rele();
1055 n_COLOUR( n_colour_env_gut(); )
1057 if (obuf != n_stdout)
1058 n_pager_close(obuf);
1059 NYD_LEAVE;
1060 return 0;
1063 FL void
1064 print_headers(size_t bottom, size_t topx, bool_t only_marked)
1066 size_t printed;
1067 NYD_ENTER;
1069 time_current_update(&time_current, FAL0);
1071 n_COLOUR( n_colour_env_create(n_COLOUR_CTX_SUM, n_stdout, FAL0); )
1072 srelax_hold();
1073 for (printed = 0; bottom <= topx; ++bottom) {
1074 struct message *mp = message + bottom - 1;
1075 if (only_marked) {
1076 if (!(mp->m_flag & MMARK))
1077 continue;
1078 } else if (!visible(mp))
1079 continue;
1080 _print_head(printed++, bottom, n_stdout, FAL0);
1081 srelax();
1083 srelax_rele();
1084 n_COLOUR( n_colour_env_gut(); )
1085 NYD_LEAVE;
1088 /* s-it-mode */