1 /*@ S-nail - a mail user agent derived from Berkeley Mail.
2 *@ Auxiliary functions.
4 * Copyright (c) 2000-2004 Gunnar Ritter, Freiburg i. Br., Germany.
5 * Copyright (c) 2012 - 2014 Steffen (Daode) Nurpmeso <sdaoden@users.sf.net>.
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. All advertising materials mentioning features or use of this software
20 * must display the following acknowledgement:
21 * This product includes software developed by the University of
22 * California, Berkeley and its contributors.
23 * 4. Neither the name of the University nor the names of its contributors
24 * may be used to endorse or promote products derived from this software
25 * without specific prior written permission.
27 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 #ifndef HAVE_AMALGAMATION
44 #include <sys/utsname.h>
52 # include <sys/socket.h>
69 static ui32_t _nyd_curr
;
70 static ui32_t _nyd_level
;
71 static struct nyd_info _nyd_infos
[NYD_CALLS_MAX
];
74 /* {hold,rele}_all_sigs() */
75 static size_t _alls_depth
;
76 static sigset_t _alls_nset
, _alls_oset
;
78 /* {hold,rele}_sigs() */
79 static size_t _hold_sigdepth
;
80 static sigset_t _hold_nset
, _hold_oset
;
82 /* Create an ISO 6429 (ECMA-48/ANSI) terminal control escape sequence */
84 static char * _colour_iso6429(char const *wish
);
88 static void _nyd_print(struct nyd_info
*nip
);
93 _colour_iso6429(char const *wish
)
95 char const * const wish_orig
= wish
;
96 char *xwish
, *cp
, cfg
[3] = {0, 0, 0};
99 /* Since we use salloc(), reuse the n_strsep() buffer also for the return
100 * value, ensure we have enough room for that */
102 size_t i
= strlen(wish
) +1;
103 xwish
= salloc(MAX(i
, sizeof("\033[1;30;40m")));
104 memcpy(xwish
, wish
, i
);
108 /* Iterate over the colour spec */
109 while ((cp
= n_strsep(&xwish
, ',', TRU1
)) != NULL
) {
110 char *y
, *x
= strchr(cp
, '=');
113 fprintf(stderr
, tr(527,
114 "Invalid colour specification \"%s\": >>> %s <<<\n"),
120 /* TODO convert the ft/fg/bg parser into a table-based one! */
121 if (!asccasecmp(cp
, "ft")) {
122 if (!asccasecmp(x
, "bold"))
124 else if (!asccasecmp(x
, "inverse"))
126 else if (!asccasecmp(x
, "underline"))
130 } else if (!asccasecmp(cp
, "fg")) {
133 } else if (!asccasecmp(cp
, "bg")) {
136 if (!asccasecmp(x
, "black"))
138 else if (!asccasecmp(x
, "blue"))
140 else if (!asccasecmp(x
, "green"))
142 else if (!asccasecmp(x
, "red"))
144 else if (!asccasecmp(x
, "brown"))
146 else if (!asccasecmp(x
, "magenta"))
148 else if (!asccasecmp(x
, "cyan"))
150 else if (!asccasecmp(x
, "white"))
158 /* Restore our salloc() buffer, create return value */
159 xwish
= UNCONST(wish
);
160 if (cfg
[0] || cfg
[1] || cfg
[2]) {
174 if (cfg
[0] || cfg
[1])
184 return UNCONST(wish
);
186 #endif /* HAVE_COLOUR */
190 _nyd_print(struct nyd_info
*nip
) /* XXX like SFSYS;no magics;jumps:lvl wrong */
193 union {int i
; size_t z
;} u
;
195 u
.i
= snprintf(buf
, sizeof buf
, "%c [%2u] %-25.25s %.16s:%-5u\n",
196 "=><"[(nip
->ni_chirp_line
>> 29) & 0x3], nip
->ni_level
, nip
->ni_fun
,
197 nip
->ni_file
, (nip
->ni_chirp_line
& 0x1FFFFFFFu
));
200 if (u
.z
> sizeof buf
)
201 u
.z
= sizeof buf
- 1; /* (Skip \0) */
202 write(STDERR_FILENO
, buf
, u
.z
);
208 panic(char const *format
, ...)
213 fprintf(stderr
, tr(1, "Panic: "));
215 va_start(ap
, format
);
216 vfprintf(stderr
, format
, ap
);
222 abort(); /* Was exit(EXIT_ERR); for a while, but no */
226 alert(char const *format
, ...)
231 fprintf(stderr
, tr(1, "Panic: "));
233 va_start(ap
, format
);
234 vfprintf(stderr
, format
, ap
);
243 safe_signal(int signum
, sighandler_type handler
)
245 struct sigaction nact
, oact
;
249 nact
.sa_handler
= handler
;
250 sigemptyset(&nact
.sa_mask
);
253 nact
.sa_flags
|= SA_RESTART
;
255 rv
= (sigaction(signum
, &nact
, &oact
) != 0) ? SIG_ERR
: oact
.sa_handler
;
264 if (_alls_depth
++ == 0) {
265 sigfillset(&_alls_nset
);
266 sigdelset(&_alls_nset
, SIGABRT
);
268 sigdelset(&_alls_nset
, SIGBUS
);
270 sigdelset(&_alls_nset
, SIGCHLD
);
271 sigdelset(&_alls_nset
, SIGFPE
);
272 sigdelset(&_alls_nset
, SIGILL
);
273 sigdelset(&_alls_nset
, SIGKILL
);
274 sigdelset(&_alls_nset
, SIGSEGV
);
275 sigdelset(&_alls_nset
, SIGSTOP
);
276 sigprocmask(SIG_BLOCK
, &_alls_nset
, &_alls_oset
);
285 if (--_alls_depth
== 0)
286 sigprocmask(SIG_SETMASK
, &_alls_oset
, (sigset_t
*)NULL
);
294 if (_hold_sigdepth
++ == 0) {
295 sigemptyset(&_hold_nset
);
296 sigaddset(&_hold_nset
, SIGHUP
);
297 sigaddset(&_hold_nset
, SIGINT
);
298 sigaddset(&_hold_nset
, SIGQUIT
);
299 sigprocmask(SIG_BLOCK
, &_hold_nset
, &_hold_oset
);
308 if (--_hold_sigdepth
== 0)
309 sigprocmask(SIG_SETMASK
, &_hold_oset
, NULL
);
315 _nyd_chirp(ui8_t act
, char const *file
, ui32_t line
, char const *fun
)
317 struct nyd_info
*nip
= _nyd_infos
;
319 if (_nyd_curr
!= NELEM(_nyd_infos
))
325 nip
->ni_chirp_line
= ((ui32_t
)(act
& 0x3) << 29) | (line
& 0x1FFFFFFFu
);
326 nip
->ni_level
= ((act
== 0) ? _nyd_level
327 : (act
== 1) ? ++_nyd_level
: _nyd_level
--);
331 _nyd_oncrash(int signo
)
333 struct sigaction xact
;
335 struct nyd_info
*nip
;
338 xact
.sa_handler
= SIG_DFL
;
339 sigemptyset(&xact
.sa_mask
);
341 sigaction(signo
, &xact
, NULL
);
343 fprintf(stderr
, "\n\nNYD: program dying due to signal %d:\n", signo
);
344 if (_nyd_infos
[NELEM(_nyd_infos
) - 1].ni_file
!= NULL
)
345 for (i
= _nyd_curr
, nip
= _nyd_infos
+ i
; i
< NELEM(_nyd_infos
); ++i
)
347 for (i
= 0, nip
= _nyd_infos
; i
< _nyd_curr
; ++i
)
351 sigaddset(&xset
, signo
);
352 sigprocmask(SIG_UNBLOCK
, &xset
, NULL
);
360 touch(struct message
*mp
)
363 mp
->m_flag
|= MTOUCH
;
364 if (!(mp
->m_flag
& MREAD
))
365 mp
->m_flag
|= MREAD
| MSTATUS
;
370 is_dir(char const *name
)
376 if (!stat(name
, &sbuf
))
377 rv
= (S_ISDIR(sbuf
.st_mode
) != 0);
383 argcount(char **argv
)
388 for (ap
= argv
; *ap
++ != NULL
;)
391 return (int)PTR2SIZE(ap
- argv
- 1);
401 if ((cp
= ok_vlook(screen
)) == NULL
|| (s
= atoi(cp
)) <= 0)
402 s
= scrnheight
- 4; /* XXX no magics */
413 cp
= ok_vlook(PAGER
);
414 if (cp
== NULL
|| *cp
== '\0')
421 paging_seems_sensible(void)
427 if (IS_TTY_SESSION() && (cp
= ok_vlook(crt
)) != NULL
)
428 rv
= (*cp
!= '\0') ? (size_t)atol(cp
) : (size_t)scrnheight
;
434 page_or_print(FILE *fp
, size_t lines
)
442 if ((rows
= paging_seems_sensible()) != 0 && lines
== 0) {
443 while ((c
= getc(fp
)) != EOF
)
444 if (c
== '\n' && ++lines
> rows
)
449 if (rows
!= 0 && lines
>= rows
)
450 run_command(get_pager(), 0, fileno(fp
), -1, NULL
, NULL
, NULL
);
452 while ((c
= getc(fp
)) != EOF
)
458 which_protocol(char const *name
)
464 enum protocol rv
= PROTO_UNKNOWN
;
467 if (name
[0] == '%' && name
[1] == ':')
469 for (cp
= name
; *cp
&& *cp
!= ':'; cp
++)
473 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
474 if (!strncmp(name
, "pop3://", 7)) {
478 fprintf(stderr
, tr(216, "No POP3 support compiled in.\n"));
480 } else if (!strncmp(name
, "pop3s://", 8)) {
481 #if defined HAVE_POP3 && defined HAVE_SSL
485 fprintf(stderr
, tr(216, "No POP3 support compiled in.\n"));
488 fprintf(stderr
, tr(225, "No SSL support compiled in.\n"));
491 } else if (!strncmp(name
, "imap://", 7)) {
495 fprintf(stderr
, tr(269, "No IMAP support compiled in.\n"));
497 } else if (!strncmp(name
, "imaps://", 8)) {
498 #if defined HAVE_IMAP && defined HAVE_SSL
502 fprintf(stderr
, tr(269, "No IMAP support compiled in.\n"));
505 fprintf(stderr
, tr(225, "No SSL support compiled in.\n"));
512 /* TODO This is the de facto maildir code and thus belongs into there!
513 * TODO and: we should have maildir:// and mbox:// pseudo-protos, instead of
514 * TODO or (more likely) in addition to *newfolders*) */
517 np
= ac_alloc((sz
= strlen(name
)) + 4 +1);
518 memcpy(np
, name
, sz
+ 1);
519 if (!stat(name
, &st
)) {
520 if (S_ISDIR(st
.st_mode
) &&
521 (strcpy(&np
[sz
], "/tmp"), !stat(np
, &st
) && S_ISDIR(st
.st_mode
)) &&
522 (strcpy(&np
[sz
], "/new"), !stat(np
, &st
) && S_ISDIR(st
.st_mode
)) &&
523 (strcpy(&np
[sz
], "/cur"), !stat(np
, &st
) && S_ISDIR(st
.st_mode
)))
525 } else if ((cp
= ok_vlook(newfolders
)) != NULL
&& !strcmp(cp
, "maildir"))
534 torek_hash(char const *name
)
536 /* Chris Torek's hash.
537 * NOTE: need to change *at least* create-okey-map.pl when changing the
542 while (*name
!= '\0') {
551 pjw(char const *cp
) /* TODO obsolete that -> torek_hash */
558 h
= (h
<< 4 & 0xffffffff) + (*cp
&0377);
559 if ((g
= h
& 0xf0000000) != 0) {
571 static ui32_t
const primes
[] = {
572 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521,
573 131071, 262139, 524287, 1048573, 2097143, 4194301,
574 8388593, 16777213, 33554393, 67108859, 134217689,
575 268435399, 536870909, 1073741789, 2147483647
578 ui32_t mprime
= 7, cutlim
;
582 cutlim
= (n
< 65536 ? n
<< 2 : (n
< 262144 ? n
<< 1 : n
));
584 for (i
= 0; i
< NELEM(primes
); i
++)
585 if ((mprime
= primes
[i
]) >= cutlim
)
587 if (i
== NELEM(primes
) && mprime
< n
)
594 expand_shell_escape(char const **s
, bool_t use_nail_extensions
)
602 if ((c
= *xs
& 0xFF) == '\0')
608 switch ((c
= *xs
& 0xFF)) {
610 case 'a': c
= '\a'; break;
611 case 'b': c
= '\b'; break;
612 case 'c': c
= PROMPT_STOP
; break;
613 case 'f': c
= '\f'; break;
614 case 'n': c
= '\n'; break;
615 case 'r': c
= '\r'; break;
616 case 't': c
= '\t'; break;
617 case 'v': c
= '\v'; break;
619 for (++xs
, c
= 0, n
= 4; --n
> 0 && octalchar(*xs
); ++xs
) {
624 /* S-nail extension for nice (get)prompt(()) support */
629 if (use_nail_extensions
) {
631 case '&': c
= ok_blook(bsdcompat
) ? '&' : '?'; break;
632 case '?': c
= exec_last_comm_error
? '1' : '0'; break;
633 case '$': c
= PROMPT_DOLLAR
; break;
634 case '@': c
= PROMPT_AT
; break;
640 /* A sole <backslash> at EOS is treated as-is! */
656 static char buf
[PROMPT_BUFFER_SIZE
];
662 if ((ccp
= ok_vlook(prompt
)) == NULL
|| *ccp
== '\0')
665 for (; PTRCMP(cp
, <, buf
+ sizeof(buf
) - 1); ++cp
) {
668 int c
= expand_shell_escape(&ccp
, TRU1
);
674 if (c
== 0 || c
== PROMPT_STOP
)
677 a
= (c
== PROMPT_DOLLAR
) ? account_name
: displayname
;
681 if (PTRCMP(cp
+ l
, >=, buf
+ sizeof(buf
) - 1))
695 nodename(int mayoverride
)
697 static char *hostname
;
703 struct addrinfo hints
, *res
;
705 struct hostent
*hent
;
710 if (mayoverride
&& (hn
= ok_vlook(hostname
)) != NULL
&& *hn
!= '\0') {
711 if (hostname
!= NULL
)
713 hostname
= sstrdup(hn
);
714 } else if (hostname
== NULL
) {
719 memset(&hints
, 0, sizeof hints
);
720 hints
.ai_family
= AF_UNSPEC
;
721 hints
.ai_socktype
= SOCK_DGRAM
; /* (dummy) */
722 hints
.ai_flags
= AI_CANONNAME
;
723 if (getaddrinfo(hn
, "0", &hints
, &res
) == 0) {
724 if (res
->ai_canonname
!= NULL
) {
725 size_t l
= strlen(res
->ai_canonname
) +1;
727 memcpy(hn
, res
->ai_canonname
, l
);
732 hent
= gethostbyname(hn
);
737 hostname
= sstrdup(hn
);
738 #if defined HAVE_SOCKETS && defined HAVE_IPV6
739 if (hn
!= ut
.nodename
)
748 lookup_password_for_token(char const *token
)
755 var
= ac_alloc(tl
+ 9 +1);
757 memcpy(var
, "password-", 9);
758 memcpy(var
+ 9, token
, tl
);
761 if ((cp
= vok_vlook(var
)) != NULL
)
769 getrandstring(size_t length
)
771 static unsigned char nodedigest
[16];
785 data
= ac_alloc(length
);
787 if ((fd
= open("/dev/urandom", O_RDONLY
)) == -1 ||
788 length
!= (size_t)read(fd
, data
, length
)) {
795 md5_update(&ctx
, (unsigned char*)cp
, strlen(cp
));
796 md5_final(nodedigest
, &ctx
);
798 /* In that case it's only used for boundaries and Message-Id:s so that
799 * srand(3) should suffice */
801 for (i
= 0; i
< sizeof(nodedigest
); ++i
)
802 nodedigest
[i
] = (unsigned char)(cp
[i
% j
] ^ rand());
805 for (i
= 0; i
< length
; i
++)
806 data
[i
] = (char)((int)(255 * (rand() / (RAND_MAX
+ 1.0))) ^
807 nodedigest
[i
% sizeof nodedigest
]);
812 b64_encode_buf(&b64
, data
, length
, B64_SALLOC
);
814 assert(length
< b64
.l
);
815 b64
.s
[length
] = '\0';
822 md5tohex(char hex
[MD5TOHEX_SIZE
], void const *vp
)
828 for (i
= 0; i
< MD5TOHEX_SIZE
/ 2; i
++) {
830 hex
[j
] = hexchar((cp
[i
] & 0xf0) >> 4);
831 hex
[++j
] = hexchar(cp
[i
] & 0x0f);
838 cram_md5_string(char const *user
, char const *pass
, char const *b64
)
841 char digest
[16], *cp
;
848 b64_decode(&out
, &in
, NULL
);
849 assert(out
.s
!= NULL
);
851 hmac_md5((unsigned char*)out
.s
, out
.l
, UNCONST(pass
), strlen(pass
), digest
);
853 cp
= md5tohex(salloc(MD5TOHEX_SIZE
+1), digest
);
856 in
.l
= lu
+ MD5TOHEX_SIZE
+1;
857 in
.s
= ac_alloc(lu
+ 1 + MD5TOHEX_SIZE
+1);
858 memcpy(in
.s
, user
, lu
);
860 memcpy(in
.s
+ lu
, cp
, MD5TOHEX_SIZE
);
861 b64_encode(&out
, &in
, B64_SALLOC
| B64_CRLF
);
868 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
872 * This code is taken from
874 * Network Working Group H. Krawczyk
875 * Request for Comments: 2104 IBM
876 * Category: Informational M. Bellare
883 * HMAC: Keyed-Hashing for Message Authentication
886 unsigned char k_ipad
[65]; /* inner padding - key XORd with ipad */
887 unsigned char k_opad
[65]; /* outer padding - key XORd with opad */
888 unsigned char tk
[16];
892 /* if key is longer than 64 bytes reset it to key=MD5(key) */
897 md5_update(&tctx
, key
, key_len
);
898 md5_final(tk
, &tctx
);
904 /* the HMAC_MD5 transform looks like:
906 * MD5(K XOR opad, MD5(K XOR ipad, text))
908 * where K is an n byte key
909 * ipad is the byte 0x36 repeated 64 times
910 * opad is the byte 0x5c repeated 64 times
911 * and text is the data being protected */
913 /* start out by storing key in pads */
914 memset(k_ipad
, 0, sizeof k_ipad
);
915 memset(k_opad
, 0, sizeof k_opad
);
916 memcpy(k_ipad
, key
, key_len
);
917 memcpy(k_opad
, key
, key_len
);
919 /* XOR key with ipad and opad values */
920 for (i
=0; i
<64; i
++) {
925 /* perform inner MD5 */
926 md5_init(&context
); /* init context for 1st pass */
927 md5_update(&context
, k_ipad
, 64); /* start with inner pad */
928 md5_update(&context
, text
, text_len
); /* then text of datagram */
929 md5_final(digest
, &context
); /* finish up 1st pass */
931 /* perform outer MD5 */
932 md5_init(&context
); /* init context for 2nd pass */
933 md5_update(&context
, k_opad
, 64); /* start with outer pad */
934 md5_update(&context
, digest
, 16); /* then results of 1st hash */
935 md5_final(digest
, &context
); /* finish up 2nd pass */
938 #endif /* HAVE_MD5 */
941 makedir(char const *name
)
947 if (!mkdir(name
, 0700))
951 if ((e
== EEXIST
|| e
== ENOSYS
) && !stat(name
, &st
) &&
966 if ((cw
->cw_fd
= open(".", O_RDONLY
)) == -1)
968 if (fchdir(cw
->cw_fd
) == -1) {
984 if (!fchdir(cw
->cw_fd
))
991 cwrelse(struct cw
*cw
)
998 #else /* !HAVE_FCHDIR */
1000 cwget(struct cw
*cw
)
1002 enum okay rv
= STOP
;
1005 if (getcwd(cw
->cw_wd
, sizeof cw
->cw_wd
) != NULL
&& !chdir(cw
->cw_wd
))
1012 cwret(struct cw
*cw
)
1014 enum okay rv
= STOP
;
1017 if (!chdir(cw
->cw_wd
))
1024 cwrelse(struct cw
*cw
)
1030 #endif /* !HAVE_FCHDIR */
1033 colalign(char const *cp
, int col
, int fill
, int *cols_decr_used_or_null
)
1035 int col_orig
= col
, n
, sz
;
1039 np
= nb
= salloc(mb_cur_max
* strlen(cp
) + col
+1);
1042 if (mb_cur_max
> 1) {
1045 if ((sz
= mbtowc(&wc
, cp
, mb_cur_max
)) == -1)
1047 else if ((n
= wcwidth(wc
)) == -1)
1055 if (sz
== 1 && spacechar(*cp
)) {
1063 if (fill
&& col
!= 0) {
1065 memmove(nb
+ col
, nb
, PTR2SIZE(np
- nb
));
1066 memset(nb
, ' ', col
);
1068 memset(np
, ' ', col
);
1074 if (cols_decr_used_or_null
!= NULL
)
1075 *cols_decr_used_or_null
-= col_orig
- col
;
1081 makeprint(struct str
const *in
, struct str
*out
)
1083 static int print_all_chars
= -1;
1085 char const *inp
, *maxp
;
1090 if (print_all_chars
== -1)
1091 print_all_chars
= ok_blook(print_all_chars
);
1094 out
->s
= outp
= smalloc(msz
);
1098 if (print_all_chars
) {
1100 memcpy(outp
, inp
, out
->l
);
1104 #ifdef HAVE_C90AMEND1
1105 if (mb_cur_max
> 1) {
1106 char mbb
[MB_LEN_MAX
+ 1];
1112 while (inp
< maxp
) {
1114 n
= mbtowc(&wc
, inp
, PTR2SIZE(maxp
- inp
));
1120 /* FIXME Why mbtowc() resetting here?
1121 * FIXME what about ISO 2022-JP plus -- those
1122 * FIXME will loose shifts, then!
1123 * FIXME THUS - we'd need special "known points"
1124 * FIXME to do so - say, after a newline!!
1125 * FIXME WE NEED TO CHANGE ALL USES +MBLEN! */
1126 mbtowc(&wc
, NULL
, mb_cur_max
);
1127 wc
= utf8
? 0xFFFD : '?';
1132 if (!iswprint(wc
) && wc
!= '\n' && wc
!= '\r' && wc
!= '\b' &&
1134 if ((wc
& ~(wchar_t)037) == 0)
1135 wc
= utf8
? 0x2400 | wc
: '?';
1136 else if (wc
== 0177)
1137 wc
= utf8
? 0x2421 : '?';
1139 wc
= utf8
? 0x2426 : '?';
1141 if ((n
= wctomb(mbb
, wc
)) <= 0)
1144 if (out
->l
>= msz
- 1) {
1145 dist
= outp
- out
->s
;
1146 out
->s
= srealloc(out
->s
, msz
+= 32);
1147 outp
= &out
->s
[dist
];
1149 for (i
= 0; i
< n
; ++i
)
1153 #endif /* C90AMEND1 */
1156 while (inp
< maxp
) {
1158 if (!isprint(c
) && c
!= '\n' && c
!= '\r' && c
!= '\b' && c
!= '\t')
1165 out
->s
[out
->l
] = '\0';
1170 prstr(char const *s
)
1178 makeprint(&in
, &out
);
1179 rp
= savestrbuf(out
.s
, out
.l
);
1186 prout(char const *s
, size_t sz
, FILE *fp
)
1194 makeprint(&in
, &out
);
1195 n
= fwrite(out
.s
, 1, out
.l
, fp
);
1202 putuc(int u
, int c
, FILE *fp
)
1208 #ifdef HAVE_C90AMEND1
1209 if (utf8
&& (u
& ~(wchar_t)0177)) {
1210 char mbb
[MB_LEN_MAX
];
1213 if ((n
= wctomb(mbb
, u
)) > 0) {
1215 for (i
= 0; i
< n
; ++i
)
1216 if (putc(mbb
[i
] & 0377, fp
) == EOF
) {
1221 rv
= (putc('\0', fp
) != EOF
);
1226 rv
= (putc(c
, fp
) != EOF
);
1233 colour_table_create(char const *pager_used
)
1235 union {char *cp
; char const *ccp
; void *vp
; struct colour_table
*ctp
;} u
;
1237 struct colour_table
*ct
;
1240 if (ok_blook(colour_disable
))
1243 /* If pager, check wether it is allowed to use colour */
1244 if (pager_used
!= NULL
) {
1247 if ((u
.cp
= ok_vlook(colour_pagers
)) == NULL
)
1248 u
.ccp
= COLOUR_PAGERS
;
1249 pager
= savestr(u
.cp
);
1251 while ((u
.cp
= n_strsep(&pager
, ',', TRU1
)) != NULL
)
1252 if (strstr(pager_used
, u
.cp
) != NULL
)
1257 /* $TERM is different in that we default to false unless whitelisted */
1259 char *term
, *okterms
;
1261 /* Don't use getenv(), but force copy-in into our own tables.. */
1262 if ((term
= _var_voklook("TERM")) == NULL
)
1264 if ((okterms
= ok_vlook(colour_terms
)) == NULL
)
1265 okterms
= UNCONST(COLOUR_TERMS
);
1266 okterms
= savestr(okterms
);
1269 while ((u
.cp
= n_strsep(&okterms
, ',', TRU1
)) != NULL
)
1270 if (!strncmp(u
.cp
, term
, i
))
1276 colour_table
= ct
= salloc(sizeof *ct
); /* XXX lex.c yet resets (FILTER!) */
1279 enum colourspec cspec
;
1282 {ok_v_colour_msginfo
, COLOURSPEC_MSGINFO
, COLOUR_MSGINFO
},
1283 {ok_v_colour_partinfo
, COLOURSPEC_PARTINFO
, COLOUR_PARTINFO
},
1284 {ok_v_colour_from_
, COLOURSPEC_FROM_
, COLOUR_FROM_
},
1285 {ok_v_colour_header
, COLOURSPEC_HEADER
, COLOUR_HEADER
},
1286 {ok_v_colour_uheader
, COLOURSPEC_UHEADER
, COLOUR_UHEADER
}
1289 for (i
= 0; i
< NELEM(map
); ++i
) {
1290 if ((u
.cp
= _var_oklook(map
[i
].okey
)) == NULL
)
1291 u
.ccp
= map
[i
].defval
;
1292 u
.cp
= _colour_iso6429(u
.ccp
);
1293 ct
->ct_csinfo
[map
[i
].cspec
].l
= strlen(u
.cp
);
1294 ct
->ct_csinfo
[map
[i
].cspec
].s
= u
.cp
;
1297 ct
->ct_csinfo
[COLOURSPEC_RESET
].l
= sizeof("\033[0m") -1;
1298 ct
->ct_csinfo
[COLOURSPEC_RESET
].s
= UNCONST("\033[0m");
1300 if ((u
.cp
= ok_vlook(colour_user_headers
)) == NULL
)
1301 u
.ccp
= COLOUR_USER_HEADERS
;
1302 ct
->ct_csinfo
[COLOURSPEC_RESET
+ 1].l
= i
= strlen(u
.ccp
);
1303 ct
->ct_csinfo
[COLOURSPEC_RESET
+ 1].s
= (i
== 0) ? NULL
: savestr(u
.ccp
);
1309 colour_put(FILE *fp
, enum colourspec cs
)
1312 if (colour_table
!= NULL
) {
1313 struct str
const *cp
= colour_get(cs
);
1315 fwrite(cp
->s
, cp
->l
, 1, fp
);
1321 colour_put_header(FILE *fp
, char const *name
)
1323 enum colourspec cs
= COLOURSPEC_HEADER
;
1324 struct str
const *uheads
;
1325 char *cp
, *cp_base
, *x
;
1329 if (colour_table
== NULL
)
1331 /* Normal header colours if there are no user headers */
1332 uheads
= colour_table
->ct_csinfo
+ COLOURSPEC_RESET
+ 1;
1333 if (uheads
->s
== NULL
)
1336 /* Iterate over all entries in the *colour-user-headers* list */
1337 cp
= ac_alloc(uheads
->l
+1);
1338 memcpy(cp
, uheads
->s
, uheads
->l
+1);
1340 namelen
= strlen(name
);
1341 while ((x
= n_strsep(&cp
, ',', TRU1
)) != NULL
) {
1342 size_t l
= (cp
!= NULL
) ? PTR2SIZE(cp
- x
) - 1 : strlen(x
);
1343 if (l
== namelen
&& !ascncasecmp(x
, name
, namelen
)) {
1344 cs
= COLOURSPEC_UHEADER
;
1356 colour_reset(FILE *fp
)
1359 if (colour_table
!= NULL
)
1360 fwrite("\033[0m", 4, 1, fp
);
1364 FL
struct str
const *
1365 colour_get(enum colourspec cs
)
1367 struct str
const *rv
= NULL
;
1370 if (colour_table
!= NULL
)
1371 if ((rv
= colour_table
->ct_csinfo
+ cs
)->s
== NULL
)
1376 #endif /* HAVE_COLOUR */
1379 time_current_update(struct time_current
*tc
, bool_t full_update
)
1382 tc
->tc_time
= time(NULL
);
1384 memcpy(&tc
->tc_gm
, gmtime(&tc
->tc_time
), sizeof tc
->tc_gm
);
1385 memcpy(&tc
->tc_local
, localtime(&tc
->tc_time
), sizeof tc
->tc_local
);
1386 sstpcpy(tc
->tc_ctime
, ctime(&tc
->tc_time
));
1392 _out_of_memory(void)
1399 smalloc(size_t s SMALLOC_DEBUG_ARGS
)
1406 if ((rv
= malloc(s
)) == NULL
)
1413 srealloc(void *v
, size_t s SMALLOC_DEBUG_ARGS
)
1422 else if ((rv
= realloc(v
, s
)) == NULL
)
1429 scalloc(size_t nmemb
, size_t size SMALLOC_DEBUG_ARGS
)
1436 if ((rv
= calloc(nmemb
, size
)) == NULL
)
1442 #else /* !HAVE_DEBUG */
1443 CTA(sizeof(char) == sizeof(ui8_t
));
1445 # define _HOPE_SIZE (2 * 8 * sizeof(char))
1446 # define _HOPE_SET(C) \
1448 union ptr __xl, __xu;\
1449 struct chunk *__xc;\
1454 __xl.ui8p[0]=0xDE; __xl.ui8p[1]=0xAA; __xl.ui8p[2]=0x55; __xl.ui8p[3]=0xAD;\
1455 __xl.ui8p[4]=0xBE; __xl.ui8p[5]=0x55; __xl.ui8p[6]=0xAA; __xl.ui8p[7]=0xEF;\
1456 __xu.ui8p += __xc->size - 8;\
1457 __xu.ui8p[0]=0xDE; __xu.ui8p[1]=0xAA; __xu.ui8p[2]=0x55; __xu.ui8p[3]=0xAD;\
1458 __xu.ui8p[4]=0xBE; __xu.ui8p[5]=0x55; __xu.ui8p[6]=0xAA; __xu.ui8p[7]=0xEF;\
1460 # define _HOPE_GET_TRACE(C,BAD) \
1466 # define _HOPE_GET(C,BAD) \
1468 union ptr __xl, __xu;\
1469 struct chunk *__xc;\
1477 if (__xl.ui8p[0] != 0xDE) __i |= 1<<0;\
1478 if (__xl.ui8p[1] != 0xAA) __i |= 1<<1;\
1479 if (__xl.ui8p[2] != 0x55) __i |= 1<<2;\
1480 if (__xl.ui8p[3] != 0xAD) __i |= 1<<3;\
1481 if (__xl.ui8p[4] != 0xBE) __i |= 1<<4;\
1482 if (__xl.ui8p[5] != 0x55) __i |= 1<<5;\
1483 if (__xl.ui8p[6] != 0xAA) __i |= 1<<6;\
1484 if (__xl.ui8p[7] != 0xEF) __i |= 1<<7;\
1487 alert("%p: corrupt lower canary: 0x%02X: %s, line %u",\
1488 __xl.p, __i, mdbg_file, mdbg_line);\
1491 __xu.ui8p += __xc->size - 8;\
1493 if (__xu.ui8p[0] != 0xDE) __i |= 1<<0;\
1494 if (__xu.ui8p[1] != 0xAA) __i |= 1<<1;\
1495 if (__xu.ui8p[2] != 0x55) __i |= 1<<2;\
1496 if (__xu.ui8p[3] != 0xAD) __i |= 1<<3;\
1497 if (__xu.ui8p[4] != 0xBE) __i |= 1<<4;\
1498 if (__xu.ui8p[5] != 0x55) __i |= 1<<5;\
1499 if (__xu.ui8p[6] != 0xAA) __i |= 1<<6;\
1500 if (__xu.ui8p[7] != 0xEF) __i |= 1<<7;\
1503 alert("%p: corrupt upper canary: 0x%02X: %s, line %u",\
1504 __xl.p, __i, mdbg_file, mdbg_line);\
1507 alert(" ..canary last seen: %s, line %u", __xc->file, __xc->line);\
1527 struct chunk
*_mlist
, *_mfree
;
1530 (smalloc
)(size_t s SMALLOC_DEBUG_ARGS
)
1537 s
+= sizeof(struct chunk
) + _HOPE_SIZE
;
1539 if ((p
.p
= (malloc
)(s
)) == NULL
)
1542 if ((p
.c
->next
= _mlist
) != NULL
)
1544 p
.c
->file
= mdbg_file
;
1545 p
.c
->line
= (ui16_t
)mdbg_line
;
1547 p
.c
->size
= (ui32_t
)s
;
1555 (srealloc
)(void *v
, size_t s SMALLOC_DEBUG_ARGS
)
1561 if ((p
.p
= v
) == NULL
) {
1562 p
.p
= (smalloc
)(s
, mdbg_file
, mdbg_line
);
1566 _HOPE_GET(p
, isbad
);
1569 fprintf(stderr
, "srealloc(): region freed! At %s, line %d\n"
1570 "\tLast seen: %s, line %d\n",
1571 mdbg_file
, mdbg_line
, p
.c
->file
, p
.c
->line
);
1578 p
.c
->prev
->next
= p
.c
->next
;
1579 if (p
.c
->next
!= NULL
)
1580 p
.c
->next
->prev
= p
.c
->prev
;
1585 s
+= sizeof(struct chunk
) + _HOPE_SIZE
;
1587 if ((p
.p
= (realloc
)(p
.c
, s
)) == NULL
)
1590 if ((p
.c
->next
= _mlist
) != NULL
)
1592 p
.c
->file
= mdbg_file
;
1593 p
.c
->line
= (ui16_t
)mdbg_line
;
1595 p
.c
->size
= (ui32_t
)s
;
1604 (scalloc
)(size_t nmemb
, size_t size SMALLOC_DEBUG_ARGS
)
1614 size
+= sizeof(struct chunk
) + _HOPE_SIZE
;
1616 if ((p
.p
= (malloc
)(size
)) == NULL
)
1618 memset(p
.p
, 0, size
);
1620 if ((p
.c
->next
= _mlist
) != NULL
)
1622 p
.c
->file
= mdbg_file
;
1623 p
.c
->line
= (ui16_t
)mdbg_line
;
1625 p
.c
->size
= (ui32_t
)size
;
1633 (sfree
)(void *v SMALLOC_DEBUG_ARGS
)
1639 if ((p
.p
= v
) == NULL
) {
1640 fprintf(stderr
, "sfree(NULL) from %s, line %d\n", mdbg_file
, mdbg_line
);
1644 _HOPE_GET(p
, isbad
);
1647 fprintf(stderr
, "sfree(): double-free avoided at %s, line %d\n"
1648 "\tLast seen: %s, line %d\n",
1649 mdbg_file
, mdbg_line
, p
.c
->file
, p
.c
->line
);
1656 p
.c
->prev
->next
= p
.c
->next
;
1657 if (p
.c
->next
!= NULL
)
1658 p
.c
->next
->prev
= p
.c
->prev
;
1661 if (options
& OPT_DEBUG
) {
1674 size_t c
= 0, s
= 0;
1677 for (p
.c
= _mfree
; p
.c
!= NULL
;) {
1686 if (options
& OPT_DEBUG
)
1687 fprintf(stderr
, "smemreset(): freed %" ZFMT
" chunks/%" ZFMT
" bytes\n",
1693 c_smemtrace(void *v
)
1695 /* For _HOPE_GET() */
1696 char const * const mdbg_file
= "smemtrace()";
1697 int const mdbg_line
= -1;
1705 if ((fp
= Ftmp(NULL
, "memtr", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
1711 fprintf(fp
, "Currently allocated memory chunks:\n");
1712 for (lines
= 0, p
.c
= _mlist
; p
.c
!= NULL
; ++lines
, p
.c
= p
.c
->next
) {
1715 _HOPE_GET_TRACE(xp
, isbad
);
1716 fprintf(fp
, "%s%p (%5" ZFMT
" bytes): %s, line %u\n",
1717 (isbad
? "! CANARY ERROR: " : ""), xp
.p
,
1718 (size_t)(p
.c
->size
- sizeof(struct chunk
)), p
.c
->file
, p
.c
->line
);
1721 if (options
& OPT_DEBUG
) {
1722 fprintf(fp
, "sfree()d memory chunks awaiting free():\n");
1723 for (p
.c
= _mfree
; p
.c
!= NULL
; ++lines
, p
.c
= p
.c
->next
) {
1726 _HOPE_GET_TRACE(xp
, isbad
);
1727 fprintf(fp
, "%s%p (%5" ZFMT
" bytes): %s, line %u\n",
1728 (isbad
? "! CANARY ERROR: " : ""), xp
.p
,
1729 (size_t)(p
.c
->size
- sizeof(struct chunk
)), p
.c
->file
, p
.c
->line
);
1733 page_or_print(fp
, lines
);
1743 _smemcheck(char const *mdbg_file
, int mdbg_line
)
1746 bool_t anybad
= FAL0
, isbad
;
1750 for (lines
= 0, p
.c
= _mlist
; p
.c
!= NULL
; ++lines
, p
.c
= p
.c
->next
) {
1753 _HOPE_GET_TRACE(xp
, isbad
);
1757 "! CANARY ERROR: %p (%5" ZFMT
" bytes): %s, line %u\n",
1758 xp
.p
, (size_t)(p
.c
->size
- sizeof(struct chunk
)),
1759 p
.c
->file
, p
.c
->line
);
1763 if (options
& OPT_DEBUG
) {
1764 for (p
.c
= _mfree
; p
.c
!= NULL
; ++lines
, p
.c
= p
.c
->next
) {
1767 _HOPE_GET_TRACE(xp
, isbad
);
1771 "! CANARY ERROR: %p (%5" ZFMT
" bytes): %s, line %u\n",
1772 xp
.p
, (size_t)(p
.c
->size
- sizeof(struct chunk
)),
1773 p
.c
->file
, p
.c
->line
);
1780 # endif /* MEMCHECK */
1781 #endif /* HAVE_DEBUG */
1783 /* vim:set fenc=utf-8:s-it-mode */