1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2016 Steffen (Daode) Nurpmeso <steffen@sdaoden.eu>.
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
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
38 #ifndef HAVE_AMALGAMATION
44 /* Prepare and print "[Message: xy]:" intro */
45 static void _show_msg_overview(FILE *obuf
, struct message
*mp
, int msg_no
);
47 /* ... And place the extracted date in `date' */
48 static void _parse_from_(struct message
*mp
, char date
[FROM_DATEBUF
]);
50 /* Print out the header of a specific message
51 * __hprf: handle *headline*
52 * __subject: return -1 if Subject: yet seen, otherwise smalloc()d Subject:
53 * __putindent: print out the indenting in threaded display */
54 static void _print_head(size_t yetprinted
, size_t msgno
, FILE *f
,
56 static void __hprf(size_t yetprinted
, char const *fmt
, size_t msgno
,
57 FILE *f
, bool_t threaded
, char const *attrlist
);
58 static char * __subject(struct message
*mp
, bool_t threaded
,
60 static int __putindent(FILE *fp
, struct message
*mp
, int maxwidth
);
62 static int _dispc(struct message
*mp
, char const *a
);
64 /* Shared `z' implementation */
65 static int a_cmd_scroll(char const *arg
, bool_t onlynew
);
67 /* Shared `headers' implementation */
68 static int _headers(int msgspec
);
70 /* Show the requested messages */
71 static int _type1(int *msgvec
, bool_t doign
, bool_t dopage
, bool_t dopipe
,
72 bool_t dodecode
, char *cmd
, ui64_t
*tstats
);
74 /* Pipe the requested messages */
75 static int _pipe1(char *str
, int doign
);
78 static int a_cmd_top(void *vp
, struct ignoretab
*itp
);
81 _show_msg_overview(FILE *obuf
, struct message
*mp
, int msg_no
)
83 char const *cpre
= "", *csuf
= "";
87 if (pstate
& PS_COLOUR_ACTIVE
) {
88 struct n_colour_pen
*cpen
;
90 if ((cpen
= n_colour_pen_create(n_COLOUR_ID_VIEW_MSGINFO
, NULL
)) != NULL
){
93 if ((sp
= n_colour_pen_to_str(cpen
)) != NULL
)
95 if ((sp
= n_colour_reset_to_str()) != NULL
)
100 fprintf(obuf
, _("%s[-- Message %2d -- %lu lines, %lu bytes --]:%s\n"),
101 cpre
, msg_no
, (ul_i
)mp
->m_lines
, (ul_i
)mp
->m_size
, csuf
);
106 _parse_from_(struct message
*mp
, char date
[FROM_DATEBUF
]) /* TODO line pool */
114 if ((ibuf
= setinput(&mb
, mp
, NEED_HEADER
)) != NULL
&&
115 (hlen
= readline_restart(ibuf
, &hline
, &hsize
, 0)) > 0)
116 extract_date_from_from_(hline
, hlen
, date
);
123 _print_head(size_t yetprinted
, size_t msgno
, FILE *f
, bool_t threaded
)
126 char attrlist
[attrlen
+1], *cp
;
130 if ((cp
= ok_vlook(attrlist
)) != NULL
) {
131 if (strlen(cp
) == attrlen
) {
132 memcpy(attrlist
, cp
, attrlen
+1);
135 n_err(_("*attrlist* is not of the correct length, using builtin\n"));
138 if (ok_blook(bsdcompat
) || ok_blook(bsdflags
)) {
139 char const bsdattr
[attrlen
+1] = "NU *HMFAT+-$~";
140 memcpy(attrlist
, bsdattr
, sizeof bsdattr
);
141 } else if (ok_blook(SYSV3
)) {
142 char const bsdattr
[attrlen
+1] = "NU *HMFAT+-$~";
143 memcpy(attrlist
, bsdattr
, sizeof bsdattr
);
144 OBSOLETE(_("*SYSV3*: please use *bsdcompat* or *bsdflags*, "
145 "or set *attrlist*"));
147 char const pattr
[attrlen
+1] = "NUROSPMFAT+-$~";
148 memcpy(attrlist
, pattr
, sizeof pattr
);
152 if ((fmt
= ok_vlook(headline
)) == NULL
) {
153 fmt
= ((ok_blook(bsdcompat
) || ok_blook(bsdheadline
))
154 ? "%>%a%m %-20f %16d %3l/%-5o %i%-S"
155 : "%>%a%m %-18f %16d %4l/%-5o %i%-s");
158 __hprf(yetprinted
, fmt
, msgno
, f
, threaded
, attrlist
);
163 __hprf(size_t yetprinted
, char const *fmt
, size_t msgno
, FILE *f
,
164 bool_t threaded
, char const *attrlist
)
166 char buf
[16], datebuf
[FROM_DATEBUF
], cbuf
[8], *cp
, *subjline
;
167 char const *datefmt
, *date
, *name
, *fp
n_COLOUR( COMMA
*colo_tag
);
168 int i
, n
, s
, wleft
, subjlen
;
171 n_COLOUR( struct n_colour_pen
*cpen_new COMMA
*cpen_cur COMMA
*cpen_bas
; )
178 _LOOP_MASK
= (1<<4) - 1,
179 _SFMT
= 1<<4, /* It is 'S' */
180 /* For the simple byte-based counts in wleft and n we sometimes need
181 * adjustments to compensate for additional bytes of UTF-8 sequences */
182 _PUTCB_UTF8_SHIFT
= 5,
183 _PUTCB_UTF8_MASK
= 3<<5
188 if ((mp
= message
+ msgno
- 1) == dot
)
192 n_COLOUR( colo_tag
= NULL
; )
194 datefmt
= ok_vlook(datefield
);
196 if (datefmt
!= NULL
) {
197 fp
= hfield1("date", mp
);/* TODO use m_date field! */
203 date
= fakedate(datet
);
204 fp
= ok_vlook(datefield_markout_older
);
205 i
= (*datefmt
!= '\0');
207 i
|= (*fp
!= '\0') ? 2 | 4 : 2; /* XXX no magics */
209 /* May we strftime(3)? */
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
))) {
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';
226 n_COLOUR( colo_tag
= n_COLOUR_TAG_SUM_OLDER
; )
227 } else if ((i
& 1) == 0)
229 } else if (datet
== (time_t)0 && !(mp
->m_flag
& MNOFROM
)) {
230 /* TODO eliminate this path, query the FROM_ date in setptr(),
231 * TODO all other codepaths do so by themselves ALREADY ?????
232 * TODO assert(mp->m_time != 0);, then
233 * TODO ALSO changes behaviour of datefield_markout_older */
234 _parse_from_(mp
, datebuf
);
237 date
= fakedate(datet
);
241 if (name
!= NULL
&& ok_blook(showto
) && is_myname(skin(name
))) {
242 if ((cp
= hfield1("to", mp
)) != NULL
) {
252 name
= ok_blook(showname
) ? realname(name
) : prstr(skin(name
));
256 /* Detect the width of the non-format characters in *headline*;
257 * like that we can simply use putc() in the next loop, since we have
258 * already calculated their column widths (TODO it's sick) */
259 wleft
= subjlen
= scrnwidth
;
261 for (fp
= fmt
; *fp
!= '\0'; ++fp
) {
267 if (digitchar(*fp
)) {
270 n
= 10*n
+ *fp
- '0';
271 while (++fp
, digitchar(*fp
));
281 if (mb_cur_max
> 1) {
283 if ((s
= mbtowc(&wc
, fp
, mb_cur_max
)) == -1)
285 else if ((n
= wcwidth(wc
)) == -1)
297 /* Walk *headline*, producing output TODO not (really) MB safe */
300 colo_tag
= n_COLOUR_TAG_SUM_DOT
;
301 cpen_bas
= n_colour_pen_create(n_COLOUR_ID_SUM_HEADER
, colo_tag
);
302 n_colour_pen_put(cpen_new
= cpen_cur
= cpen_bas
, f
);
305 for (fp
= fmt
; *fp
!= '\0'; ++fp
) {
308 if ((c
= *fp
& 0xFF) != '%') {
310 if ((cpen_new
= cpen_bas
) != cpen_cur
)
311 n_colour_pen_put(cpen_cur
= cpen_new
, f
);
320 if ((c
= *++fp
) == '-') {
325 if (digitchar(*fp
)) {
327 n
= 10*n
+ *fp
- '0';
328 while (++fp
, digitchar(*fp
));
331 if ((c
= *fp
& 0xFF) == '\0')
341 if (flags
& _ISDOT
) {
342 n_COLOUR( cpen_new
= n_colour_pen_create(n_COLOUR_ID_SUM_DOTMARK
,
344 if (options
& OPT_UNICODE
) {
346 /* 25B8;BLACK RIGHT-POINTING SMALL TRIANGLE: â–¸ */
347 cbuf
[1] = (char)0x96, cbuf
[2] = (char)0xB8;
349 /* 25C2;BLACK LEFT-POINTING SMALL TRIANGLE: â—‚ */
350 cbuf
[1] = (char)0x97, cbuf
[2] = (char)0x82;
353 flags
|= 2 << _PUTCB_UTF8_SHIFT
;
362 if (UICMP(32, n_ABS(n
), >, wleft
))
363 n
= (n
< 0) ? -wleft
: wleft
;
364 snprintf(buf
, sizeof buf
, "%u.%02u",
365 (mp
->m_spamscore
>> 8), (mp
->m_spamscore
& 0xFF));
366 n
= fprintf(f
, "%*s", n
, buf
);
367 wleft
= (n
>= 0) ? wleft
- n
: 0;
374 c
= _dispc(mp
, attrlist
);
377 if (cpen_new
== cpen_cur
)
379 if (cpen_new
!= cpen_cur
)
380 n_colour_pen_put(cpen_cur
= cpen_new
, f
);
382 if (UICMP(32, n_ABS(n
), >, wleft
))
383 n
= (n
< 0) ? -wleft
: wleft
;
385 n
= fprintf(f
, "%*s", n
, cbuf
);
388 if ((n
= (flags
& _PUTCB_UTF8_MASK
)) != 0) {
389 n
>>= _PUTCB_UTF8_SHIFT
;
393 wleft
= 0; /* TODO I/O error.. ? break? */
396 if ((cpen_new
= cpen_bas
) != cpen_cur
)
397 n_colour_pen_put(cpen_cur
= cpen_new
, f
);
401 if (datefmt
!= NULL
) {
402 i
= strftime(datebuf
, sizeof datebuf
, datefmt
,
403 &time_current
.tc_local
);
407 n_err(_("Ignored date format, it excesses the target "
408 "buffer (%lu bytes)\n"), (ul_i
)sizeof(datebuf
));
413 if (UICMP(32, n_ABS(n
), >, wleft
))
414 n
= (n
< 0) ? -wleft
: wleft
;
415 n
= fprintf(f
, "%*.*s", n
, n
, date
);
416 wleft
= (n
>= 0) ? wleft
- n
: 0;
421 if (UICMP(32, n_ABS(n
), >, wleft
))
422 n
= (n
< 0) ? -wleft
: wleft
;
423 n
= fprintf(f
, "%*u", n
, (threaded
== 1 ? mp
->m_level
: 0));
424 wleft
= (n
>= 0) ? wleft
- n
: 0;
435 n
= (n
< 0) ? -wleft
: wleft
;
437 if (flags
& _ISTO
) /* XXX tr()! */
439 n
= fprintf(f
, "%s%s", ((flags
& _ISTO
) ? "To " : ""),
440 colalign(name
, i
, n
, &wleft
));
443 else if (flags
& _ISTO
)
449 cpen_new
= n_colour_pen_create(n_COLOUR_ID_SUM_THREAD
, colo_tag
);
450 if (cpen_new
!= cpen_cur
)
451 n_colour_pen_put(cpen_cur
= cpen_new
, f
);
453 n
= __putindent(f
, mp
, n_MIN(wleft
, scrnwidth
- 60));
454 wleft
= (n
>= 0) ? wleft
- n
: 0;
456 if ((cpen_new
= cpen_bas
) != cpen_cur
)
457 n_colour_pen_put(cpen_cur
= cpen_new
, f
);
464 if (UICMP(32, n_ABS(n
), >, wleft
))
465 n
= (n
< 0) ? -wleft
: wleft
;
467 n
= fprintf(f
, "%*ld", n
, mp
->m_xlines
);
468 wleft
= (n
>= 0) ? wleft
- n
: 0;
480 for (i
= msgCount
; i
> 999; i
/= 10)
483 if (UICMP(32, n_ABS(n
), >, wleft
))
484 n
= (n
< 0) ? -wleft
: wleft
;
485 n
= fprintf(f
, "%*lu", n
, (ul_i
)msgno
);
486 wleft
= (n
>= 0) ? wleft
- n
: 0;
491 if (UICMP(32, n_ABS(n
), >, wleft
))
492 n
= (n
< 0) ? -wleft
: wleft
;
493 n
= fprintf(f
, "%*lu", n
, (ul_i
)mp
->m_xsize
);
494 wleft
= (n
>= 0) ? wleft
- n
: 0;
506 if (UICMP(32, n_ABS(n
), >, subjlen
))
507 n
= (n
< 0) ? -subjlen
: subjlen
;
509 n
-= (n
< 0) ? -2 : 2;
512 if (subjline
== NULL
)
513 subjline
= __subject(mp
, (threaded
&& (flags
& _IFMT
)), yetprinted
);
514 if (subjline
== (char*)-1) {
515 n
= fprintf(f
, "%*s", n
, "");
516 wleft
= (n
>= 0) ? wleft
- n
: 0;
518 n
= fprintf(f
, ((flags
& _SFMT
) ? "\"%s\"" : "%s"),
519 colalign(subjline
, n_ABS(n
), n
, &wleft
));
524 case 'T': { /* Message recipient flags */
525 /* We never can reuse "name" since it's the full name */
526 struct name
const *np
= lextract(hfield1("to", mp
), GTO
| GSKIN
);
530 for (; np
!= NULL
; np
= np
->n_flink
) {
531 switch (is_mlist(np
->n_name
, FAL0
)) {
532 case MLIST_SUBSCRIBED
: c
= 'S'; goto jputcb
;
533 case MLIST_KNOWN
: c
= 'L'; goto jputcb
;
541 np
= lextract(hfield1("cc", mp
), GCC
| GSKIN
);
548 for (i
= msgCount
; i
> 999; i
/= 10)
551 if (UICMP(32, n_ABS(n
), >, wleft
))
552 n
= (n
< 0) ? -wleft
: wleft
;
553 n
= fprintf(f
, "%*lu",
554 n
, (threaded
? (ul_i
)mp
->m_threadpos
: (ul_i
)msgno
));
555 wleft
= (n
>= 0) ? wleft
- n
: 0;
558 if (options
& OPT_D_V
)
559 n_err(_("Unkown *headline* format: %%%c\n"), c
);
573 if (subjline
!= NULL
&& subjline
!= (char*)-1)
579 __subject(struct message
*mp
, bool_t threaded
, size_t yetprinted
)
582 char *rv
= (char*)-1, *ms
;
585 if ((ms
= hfield1("subject", mp
)) == NULL
)
588 in
.l
= strlen(in
.s
= ms
);
589 mime_fromhdr(&in
, &out
, TD_ICONV
| TD_ISPR
);
592 if (!threaded
|| mp
->m_level
== 0)
595 /* In a display thread - check whether this message uses the same
596 * Subject: as it's parent or elder neighbour, suppress printing it if
597 * this is the case. To extend this a bit, ignore any leading Re: or
598 * Fwd: plus follow-up WS. Ignore invisible messages along the way */
599 ms
= subject_re_trim(ms
);
601 for (; (mp
= prev_in_thread(mp
)) != NULL
&& yetprinted
-- > 0;) {
604 if (visible(mp
) && (os
= hfield1("subject", mp
)) != NULL
) {
608 in
.l
= strlen(in
.s
= os
);
609 mime_fromhdr(&in
, &oout
, TD_ICONV
| TD_ISPR
);
610 x
= asccasecmp(ms
, subject_re_trim(oout
.s
));
626 __putindent(FILE *fp
, struct message
*mp
, int maxwidth
)/* XXX no magic consts */
629 int *us
, indlvl
, indw
, i
, important
= MNEW
| MFLAGGED
;
633 if (mp
->m_level
== 0 || maxwidth
== 0) {
638 cs
= ac_alloc(mp
->m_level
);
639 us
= ac_alloc(mp
->m_level
* sizeof *us
);
642 if (mp
->m_younger
&& UICMP(32, i
+ 1, ==, mp
->m_younger
->m_level
)) {
643 if (mp
->m_parent
&& mp
->m_parent
->m_flag
& important
)
644 us
[i
] = mp
->m_flag
& important
? 0x2523 : 0x2520;
646 us
[i
] = mp
->m_flag
& important
? 0x251D : 0x251C;
649 if (mp
->m_parent
&& mp
->m_parent
->m_flag
& important
)
650 us
[i
] = mp
->m_flag
& important
? 0x2517 : 0x2516;
652 us
[i
] = mp
->m_flag
& important
? 0x2515 : 0x2514;
657 for (i
= mp
->m_level
- 2; i
>= 0; --i
) {
659 if (UICMP(32, i
, >, mq
->m_level
- 1)) {
664 if (mq
->m_parent
&& (mq
->m_parent
->m_flag
& important
))
677 for (indlvl
= indw
= 0; (ui8_t
)indlvl
< mp
->m_level
&& indw
< maxwidth
;
679 if (indw
< maxwidth
- 1)
680 indw
+= (int)putuc(us
[indlvl
], cs
[indlvl
] & 0xFF, fp
);
682 indw
+= (int)putuc(0x21B8, '^', fp
);
684 indw
+= putuc(0x25B8, '>', fp
);
694 _dispc(struct message
*mp
, char const *a
)
699 if ((mp
->m_flag
& (MREAD
| MNEW
)) == MREAD
)
701 if ((mp
->m_flag
& (MREAD
| MNEW
)) == (MREAD
| MNEW
))
703 if (mp
->m_flag
& MANSWERED
)
705 if (mp
->m_flag
& MDRAFTED
)
707 if ((mp
->m_flag
& (MREAD
| MNEW
)) == MNEW
)
709 if (!(mp
->m_flag
& (MREAD
| MNEW
)))
711 if (mp
->m_flag
& MSPAM
)
713 if (mp
->m_flag
& MSPAMUNSURE
)
715 if (mp
->m_flag
& MSAVED
)
717 if (mp
->m_flag
& MPRESERVE
)
719 if (mp
->m_flag
& (MBOX
| MBOXED
))
721 if (mp
->m_flag
& MFLAGGED
)
723 if (mb
.mb_threaded
== 1 && mp
->m_collapsed
> 0)
725 if (mb
.mb_threaded
== 1 && mp
->m_collapsed
< 0)
732 a_cmd_scroll(char const *arg
, bool_t onlynew
){
736 int msgspec
, size
, maxs
;
739 /* TODO scroll problem: we do not know whether + and $ have already reached
740 * TODO the last screen in threaded mode */
741 msgspec
= onlynew
? -1 : 0;
743 if((maxs
= msgCount
/ size
) > 0 && msgCount
% size
== 0)
772 case '1': case '2': case '3': case '4': case '5':
773 case '6': case '7': case '8': case '9': case '0':
776 l
= strtol(arg
, &eptr
, 10);
779 if(l
> maxs
- (isabs
? 0 : _screen
))
781 _screen
= isabs
? (int)l
: _screen
+ l
;
787 printf(_("On last screenful of messages\n"));
796 l
= strtol(arg
, &eptr
, 10);
806 printf(_("On first screenful of messages\n"));
813 n_err(_("Unrecognized scrolling command: %s\n"), arg
);
818 size
= _headers(msgspec
);
825 _headers(int msgspec
) /* TODO rework v15 */
828 bool_t
volatile isrelax
;
829 ui32_t
volatile flag
;
830 int g
, k
, mesg
, size
;
831 int volatile lastg
= 1;
832 struct message
*mp
, *mq
, *lastmq
= NULL
;
833 enum mflag fl
= MNEW
| MFLAGGED
;
836 time_current_update(&time_current
, FAL0
);
840 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
848 if (options
& OPT_INTERACTIVE
)
849 n_colour_env_create(n_COLOUR_CTX_SUM
, FAL0
);
855 #if 0 /* FIXME original code path */
868 if (mb
.mb_threaded
== 0) {
871 for (mp
= message
; PTRCMP(mp
, <, message
+ msgCount
); ++mp
)
875 if (mp
->m_flag
& fl
) {
879 if ((msgspec
> 0 && PTRCMP(mp
, ==, message
+ msgspec
- 1)) ||
880 (msgspec
== 0 && g
== k
) ||
881 (msgspec
== -2 && g
== k
+ size
&& lastmq
) ||
882 (msgspec
< 0 && g
>= k
&& (mp
->m_flag
& fl
) != 0))
886 if (lastmq
&& (msgspec
== -2 ||
887 (msgspec
== -1 && PTRCMP(mp
, ==, message
+ msgCount
)))) {
894 mesg
= (int)PTR2SIZE(mp
- message
);
895 if (PTRCMP(dot
, !=, message
+ msgspec
- 1)) { /* TODO really?? */
896 for (mq
= mp
; PTRCMP(mq
, <, message
+ msgCount
); ++mq
)
905 for (; PTRCMP(mp
, <, message
+ msgCount
); ++mp
) {
909 if (UICMP(32, flag
++, >=, size
))
911 _print_head(0, mesg
, stdout
, 0);
916 } else { /* threaded */
919 for (mp
= threadroot
; mp
; mp
= next_in_thread(mp
))
921 (mp
->m_collapsed
<= 0 ||
922 PTRCMP(mp
, ==, message
+ msgspec
- 1))) {
925 if (mp
->m_flag
& fl
) {
929 if ((msgspec
> 0 && PTRCMP(mp
, ==, message
+ msgspec
- 1)) ||
930 (msgspec
== 0 && g
== k
) ||
931 (msgspec
== -2 && g
== k
+ size
&& lastmq
) ||
932 (msgspec
< 0 && g
>= k
&& (mp
->m_flag
& fl
) != 0))
936 if (lastmq
&& (msgspec
== -2 ||
937 (msgspec
== -1 && PTRCMP(mp
, ==, message
+ msgCount
)))) {
943 if (PTRCMP(dot
, !=, message
+ msgspec
- 1)) { /* TODO really?? */
944 for (mq
= mp
; mq
; mq
= next_in_thread(mq
))
945 if (visible(mq
) && mq
->m_collapsed
<= 0) {
955 (mp
->m_collapsed
<= 0 ||
956 PTRCMP(mp
, ==, message
+ msgspec
- 1))) {
957 if (UICMP(32, flag
++, >=, size
))
959 _print_head(flag
- 1, PTR2SIZE(mp
- message
+ 1), stdout
,
963 mp
= next_in_thread(mp
);
970 printf(_("No more mail.\n"));
971 if (pstate
& (PS_HOOK_MASK
| PS_ROBOT
))
975 n_sigman_cleanup_ping(&sm
);
979 n_COLOUR( n_colour_env_gut((sm
.sm_signo
!= SIGPIPE
) ? stdout
: NULL
); )
981 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
986 _type1(int *msgvec
, bool_t doign
, bool_t dopage
, bool_t dopipe
,
987 bool_t dodecode
, char *cmd
, ui64_t
*tstats
)
995 FILE * volatile obuf
;
996 bool_t
volatile isrelax
= FAL0
;
999 enum sendaction
const action
= ((dopipe
&& ok_blook(piperaw
))
1000 ? SEND_MBOX
: dodecode
1002 ? SEND_TODISP
: SEND_TODISP_ALL
);
1003 bool_t
const volatile formfeed
= (dopipe
&& ok_blook(page
));
1006 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
1014 if ((obuf
= Popen(cmd
, "w", ok_vlook(SHELL
), NULL
, 1)) == NULL
) {
1018 } else if ((options
& OPT_TTYOUT
) && (dopage
||
1019 ((options
& OPT_INTERACTIVE
) && (cp
= ok_vlook(crt
)) != NULL
))) {
1023 for (ip
= msgvec
; *ip
&& PTRCMP(ip
- msgvec
, <, msgCount
); ++ip
) {
1024 mp
= message
+ *ip
- 1;
1025 if (!(mp
->m_have
& HAVE_BODY
))
1026 if (get_body(mp
) != OKAY
)
1027 goto jcleanup_leave
;
1028 nlines
+= mp
->m_lines
+ 1; /* Message info XXX and PARTS... */
1032 /* >= not <: we return to the prompt */
1033 if (dopage
|| UICMP(z
, nlines
, >=,
1034 (*cp
!= '\0' ? strtoul(cp
, NULL
, 0) : (size_t)realscreenheight
))) {
1035 if ((obuf
= n_pager_open()) == NULL
)
1039 if ((options
& OPT_INTERACTIVE
) &&
1040 (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
||
1041 action
== SEND_SHOW
))
1042 n_colour_env_create(n_COLOUR_CTX_VIEW
, obuf
!= stdout
);
1046 else if ((options
& OPT_INTERACTIVE
) &&
1047 (action
== SEND_TODISP
|| action
== SEND_TODISP_ALL
))
1048 n_colour_env_create(n_COLOUR_CTX_VIEW
, FAL0
);
1051 /*TODO unless we have our signal manager special care must be taken */
1054 for (ip
= msgvec
; *ip
&& PTRCMP(ip
- msgvec
, <, msgCount
); ++ip
) {
1055 mp
= message
+ *ip
- 1;
1058 pstate
|= PS_DID_PRINT_DOT
;
1060 if (!dopipe
&& ip
!= msgvec
)
1061 fprintf(obuf
, "\n");
1062 if (action
!= SEND_MBOX
)
1063 _show_msg_overview(obuf
, mp
, *ip
);
1064 sendmp(mp
, obuf
, (doign
? ignore
: NULL
), NULL
, action
, mstats
);
1066 if (formfeed
) /* TODO a nicer way to separate piped messages! */
1069 tstats
[0] += mstats
[0];
1076 n_sigman_cleanup_ping(&sm
);
1080 n_COLOUR( n_colour_env_gut((sm
.sm_signo
!= SIGPIPE
) ? obuf
: NULL
); )
1082 n_pager_close(obuf
);
1085 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
1090 _pipe1(char *str
, int doign
)
1093 char const *cmd
, *cmdq
;
1094 int *msgvec
, rv
= 1;
1098 if ((cmd
= laststring(str
, &needs_list
, TRU1
)) == NULL
) {
1099 cmd
= ok_vlook(cmd
);
1100 if (cmd
== NULL
|| *cmd
== '\0') {
1101 n_err(_("Variable *cmd* not set\n"));
1106 msgvec
= salloc((msgCount
+ 2) * sizeof *msgvec
);
1109 *msgvec
= first(0, MMNORM
);
1111 if (pstate
& (PS_HOOK_MASK
| PS_ROBOT
)) {
1115 puts(_("No messages to pipe."));
1119 } else if (getmsglist(str
, msgvec
, 0) < 0)
1122 if (pstate
& (PS_HOOK_MASK
| PS_ROBOT
)) {
1126 printf("No applicable messages.\n");
1130 cmdq
= n_shexp_quote_cp(cmd
, FAL0
);
1131 printf(_("Pipe to: %s\n"), cmdq
);
1133 if ((rv
= _type1(msgvec
, doign
, FAL0
, TRU1
, FAL0
, n_UNCONST(cmd
), stats
)
1135 printf("%s %" PRIu64
" bytes\n", cmdq
, stats
[0]);
1142 a_cmd_top(void *vp
, struct ignoretab
*itp
){
1145 enum{a_NONE
, a_SQUEEZE
= 1u<<0,
1146 a_EMPTY
= 1u<<8, a_STOP
= 1u<<9, a_WORKMASK
= 0xFF00u
} f
;
1147 size_t tmax
, plines
;
1151 if((iobuf
= Ftmp(NULL
, "topio", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1152 n_perr(_("`top': I/O temporary file"), 0);
1156 if((pbuf
= Ftmp(NULL
, "toppag", OF_RDWR
| OF_UNLINK
| OF_REGISTER
)) == NULL
){
1157 n_perr(_("`top': temporary pager file"), 0);
1162 /* TODO In v15 we should query the m_message object, and directly send only
1163 * TODO those parts, optionally over empty-line-squeeze and quote-strip
1164 * TODO filters, in which we are interested in: only text content!
1165 * TODO And: with *topsqueeze*, header/content separating empty line.. */
1166 pstate
&= ~PS_MSGLIST_DIRECT
; /* TODO NO ATTACHMENTS */
1170 if (options
& OPT_INTERACTIVE
)
1171 n_colour_env_create(n_COLOUR_CTX_VIEW
, TRU1
);
1173 n_string_creat_auto(&s
);
1177 if((l
= strtol(ok_vlook(toplines
), NULL
, 0)) <= 0){
1178 tmax
= (size_t)screensize();
1186 f
= ok_blook(topsqueeze
) ? a_SQUEEZE
: a_NONE
;
1188 for(ip
= msgvec
= vp
;
1189 *ip
!= 0 && UICMP(z
, PTR2SIZE(ip
- msgvec
), <, msgCount
); ++ip
){
1192 mp
= &message
[*ip
- 1];
1195 pstate
|= PS_DID_PRINT_DOT
;
1199 if(ftruncate(fileno(iobuf
), 0)){
1200 n_perr(_("`top': ftruncate(2)"), 0);
1204 if(sendmp(mp
, iobuf
, itp
, NULL
, SEND_TODISP_ALL
, NULL
) < 0){
1205 n_err(_("`top': failed to prepare message %d\n"), *ip
);
1209 fflush_rewind(iobuf
);
1211 _show_msg_overview(pbuf
, mp
, *ip
);
1216 n_string_trunc(&s
, 0);
1217 for(l
= 0, f
&= ~a_WORKMASK
; !(f
& a_STOP
);){
1220 if((c
= getc(iobuf
)) == EOF
){
1226 n_string_push_c(&s
, c
);
1227 else if((f
& a_SQUEEZE
) && s
.s_len
== 0){
1228 if(!(f
& a_STOP
) && ((f
& a_EMPTY
) || tmax
- 1 <= l
))
1230 if(putc('\n', pbuf
) == EOF
){
1237 char const *cp
, *xcp
;
1239 cp
= n_string_cp_const(&s
);
1240 /* TODO Brute simple skip part overviews; see above.. */
1241 if(!(f
& a_SQUEEZE
))
1243 else if(s
.s_len
> 8 &&
1244 (xcp
= strstr(cp
, "[-- ")) != NULL
&&
1245 strstr(&xcp
[1], " --]") != NULL
)
1247 else for(; (c
= *cp
) != '\0'; ++cp
){
1250 if(!blankspacechar(c
)){
1259 if(fputs(n_string_cp_const(&s
), pbuf
) == EOF
||
1260 putc('\n', pbuf
) == EOF
){
1269 n_string_trunc(&s
, 0);
1277 if(!(f
& a_EMPTY
) && putc('\n', pbuf
) == EOF
){
1287 n_COLOUR( n_colour_env_gut(pbuf
); )
1290 page_or_print(pbuf
, plines
);
1297 return (vp
!= NULL
);
1301 c_cmdnotsupp(void *v
) /* TODO -> lex.c */
1305 n_err(_("The requested feature is not compiled in\n"));
1316 rv
= print_header_group((int*)v
);
1322 print_header_group(int *vector
)
1327 assert(vector
!= NULL
&& vector
!= (void*)-1);
1328 rv
= _headers(vector
[0]);
1339 rv
= a_cmd_scroll(v
, FAL0
);
1350 rv
= a_cmd_scroll(v
, TRU1
);
1359 int *msgvec
= v
, *ip
, n
;
1361 FILE * volatile obuf
;
1362 bool_t
volatile isrelax
;
1365 time_current_update(&time_current
, FAL0
);
1369 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
1376 if (options
& OPT_INTERACTIVE
) {
1377 if ((cp
= ok_vlook(crt
)) != NULL
) {
1378 for (n
= 0, ip
= msgvec
; *ip
!= 0; ++ip
)
1380 if (UICMP(z
, n
, >, (*cp
== '\0'
1381 ? (size_t)screensize() : strtoul(cp
, NULL
, 0)) + 3) &&
1382 (obuf
= n_pager_open()) == NULL
)
1385 n_COLOUR( n_colour_env_create(n_COLOUR_CTX_SUM
, obuf
!= stdout
); )
1388 /* Update dot before display so that the dotmark etc. are correct */
1389 for (ip
= msgvec
; *ip
!= 0; ++ip
)
1392 setdot(message
+ *ip
- 1);
1396 for (n
= 0, ip
= msgvec
; *ip
!= 0; ++ip
) { /* TODO join into _print_head() */
1397 _print_head((size_t)n
++, (size_t)*ip
, obuf
, mb
.mb_threaded
);
1403 n_sigman_cleanup_ping(&sm
);
1407 n_COLOUR( n_colour_env_gut((sm
.sm_signo
!= SIGPIPE
) ? obuf
: NULL
); )
1409 n_pager_close(obuf
);
1411 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
1416 print_headers(size_t bottom
, size_t topx
, bool_t only_marked
)
1419 bool_t
volatile isrelax
;
1423 time_current_update(&time_current
, FAL0
);
1426 n_SIGMAN_ENTER_SWITCH(&sm
, n_SIGMAN_ALL
) {
1434 if (options
& OPT_INTERACTIVE
)
1435 n_colour_env_create(n_COLOUR_CTX_SUM
, FAL0
);
1440 for (printed
= 0; bottom
<= topx
; ++bottom
) {
1441 struct message
*mp
= message
+ bottom
- 1;
1443 if (!(mp
->m_flag
& MMARK
))
1445 } else if (!visible(mp
))
1447 _print_head(printed
++, bottom
, stdout
, FAL0
);
1453 n_sigman_cleanup_ping(&sm
);
1457 n_COLOUR( n_colour_env_gut((sm
.sm_signo
!= SIGPIPE
) ? stdout
: NULL
); )
1459 n_sigman_leave(&sm
, n_SIGMAN_VIPSIGS_NTTYOUT
);
1467 printf("%d\n", (int)PTR2SIZE(dot
- message
+ 1));
1475 int *msgvec
= v
, rv
;
1478 rv
= _type1(msgvec
, TRU1
, TRU1
, FAL0
, FAL0
, NULL
, NULL
);
1486 int *msgvec
= v
, rv
;
1489 rv
= _type1(msgvec
, FAL0
, TRU1
, FAL0
, FAL0
, NULL
, NULL
);
1497 int *msgvec
= v
, rv
;
1500 rv
= _type1(msgvec
, TRU1
, FAL0
, FAL0
, FAL0
, NULL
, NULL
);
1508 int *msgvec
= v
, rv
;
1511 rv
= _type1(msgvec
, FAL0
, FAL0
, FAL0
, FAL0
, NULL
, NULL
);
1519 int *msgvec
= v
, rv
;
1522 rv
= _type1(msgvec
, FAL0
, FAL0
, FAL0
, TRU1
, NULL
, NULL
);
1534 rv
= _pipe1(str
, 1);
1546 rv
= _pipe1(str
, 0);
1553 struct ignoretab it
[2];
1557 n_ignoretab_creat(&it
[0], TRU1
);
1558 n_ignoretab_creat(&it
[1], TRU1
);
1559 n_ignoretab_insert(&it
[1], "from", sizeof("from") -1);
1560 n_ignoretab_insert(&it
[1], "to", sizeof("to") -1);
1561 n_ignoretab_insert(&it
[1], "cc", sizeof("cc") -1);
1562 n_ignoretab_insert(&it
[1], "subject", sizeof("subject") -1);
1564 rv
= !a_cmd_top(v
, it
);
1574 rv
= !a_cmd_top(v
, ignore
);
1589 if(*(argv
= v
) != NULL
){
1590 if((cp
= fexpand(*argv
, FEXP_NSHELL
| FEXP_LOCAL
)) == NULL
)
1593 cp
= folder_query();
1595 rv
= run_command(ok_vlook(LISTER
), 0, COMMAND_FD_PASS
, COMMAND_FD_PASS
, cp
,