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>
67 struct mem_chunk
*mc_prev
;
68 struct mem_chunk
*mc_next
;
78 struct mem_chunk
*p_c
;
82 #endif /* HAVE_DEBUG */
84 /* NYD, memory pool debug */
86 static ui32_t _nyd_curr
, _nyd_level
;
87 static struct nyd_info _nyd_infos
[NYD_CALLS_MAX
];
89 static size_t _mem_aall
, _mem_acur
, _mem_amax
,
90 _mem_mall
, _mem_mcur
, _mem_mmax
;
92 static struct mem_chunk
*_mem_list
, *_mem_free
;
95 /* {hold,rele}_all_sigs() */
96 static size_t _alls_depth
;
97 static sigset_t _alls_nset
, _alls_oset
;
99 /* {hold,rele}_sigs() */
100 static size_t _hold_sigdepth
;
101 static sigset_t _hold_nset
, _hold_oset
;
103 /* Create an ISO 6429 (ECMA-48/ANSI) terminal control escape sequence */
105 static char * _colour_iso6429(char const *wish
);
109 static void _nyd_print(struct nyd_info
*nip
);
114 _colour_iso6429(char const *wish
)
116 char const * const wish_orig
= wish
;
117 char *xwish
, *cp
, cfg
[3] = {0, 0, 0};
120 /* Since we use salloc(), reuse the n_strsep() buffer also for the return
121 * value, ensure we have enough room for that */
123 size_t i
= strlen(wish
) +1;
124 xwish
= salloc(MAX(i
, sizeof("\033[1;30;40m")));
125 memcpy(xwish
, wish
, i
);
129 /* Iterate over the colour spec */
130 while ((cp
= n_strsep(&xwish
, ',', TRU1
)) != NULL
) {
131 char *y
, *x
= strchr(cp
, '=');
134 fprintf(stderr
, tr(527,
135 "Invalid colour specification \"%s\": >>> %s <<<\n"),
141 /* TODO convert the ft/fg/bg parser into a table-based one! */
142 if (!asccasecmp(cp
, "ft")) {
143 if (!asccasecmp(x
, "bold"))
145 else if (!asccasecmp(x
, "inverse"))
147 else if (!asccasecmp(x
, "underline"))
151 } else if (!asccasecmp(cp
, "fg")) {
154 } else if (!asccasecmp(cp
, "bg")) {
157 if (!asccasecmp(x
, "black"))
159 else if (!asccasecmp(x
, "blue"))
161 else if (!asccasecmp(x
, "green"))
163 else if (!asccasecmp(x
, "red"))
165 else if (!asccasecmp(x
, "brown"))
167 else if (!asccasecmp(x
, "magenta"))
169 else if (!asccasecmp(x
, "cyan"))
171 else if (!asccasecmp(x
, "white"))
179 /* Restore our salloc() buffer, create return value */
180 xwish
= UNCONST(wish
);
181 if (cfg
[0] || cfg
[1] || cfg
[2]) {
195 if (cfg
[0] || cfg
[1])
205 return UNCONST(wish
);
207 #endif /* HAVE_COLOUR */
211 _nyd_print(struct nyd_info
*nip
) /* XXX like SFSYS;no magics;jumps:lvl wrong */
214 union {int i
; size_t z
;} u
;
216 u
.i
= snprintf(buf
, sizeof buf
, "%c [%2u] %-25.25s %.16s:%-5u\n",
217 "=><"[(nip
->ni_chirp_line
>> 29) & 0x3], nip
->ni_level
, nip
->ni_fun
,
218 nip
->ni_file
, (nip
->ni_chirp_line
& 0x1FFFFFFFu
));
221 if (u
.z
> sizeof buf
)
222 u
.z
= sizeof buf
- 1; /* (Skip \0) */
223 write(STDERR_FILENO
, buf
, u
.z
);
229 panic(char const *format
, ...)
234 fprintf(stderr
, tr(1, "Panic: "));
236 va_start(ap
, format
);
237 vfprintf(stderr
, format
, ap
);
243 abort(); /* Was exit(EXIT_ERR); for a while, but no */
247 alert(char const *format
, ...)
252 fprintf(stderr
, tr(1, "Panic: "));
254 va_start(ap
, format
);
255 vfprintf(stderr
, format
, ap
);
264 safe_signal(int signum
, sighandler_type handler
)
266 struct sigaction nact
, oact
;
270 nact
.sa_handler
= handler
;
271 sigemptyset(&nact
.sa_mask
);
274 nact
.sa_flags
|= SA_RESTART
;
276 rv
= (sigaction(signum
, &nact
, &oact
) != 0) ? SIG_ERR
: oact
.sa_handler
;
285 if (_alls_depth
++ == 0) {
286 sigfillset(&_alls_nset
);
287 sigdelset(&_alls_nset
, SIGABRT
);
289 sigdelset(&_alls_nset
, SIGBUS
);
291 sigdelset(&_alls_nset
, SIGCHLD
);
292 sigdelset(&_alls_nset
, SIGFPE
);
293 sigdelset(&_alls_nset
, SIGILL
);
294 sigdelset(&_alls_nset
, SIGKILL
);
295 sigdelset(&_alls_nset
, SIGSEGV
);
296 sigdelset(&_alls_nset
, SIGSTOP
);
297 sigprocmask(SIG_BLOCK
, &_alls_nset
, &_alls_oset
);
306 if (--_alls_depth
== 0)
307 sigprocmask(SIG_SETMASK
, &_alls_oset
, (sigset_t
*)NULL
);
315 if (_hold_sigdepth
++ == 0) {
316 sigemptyset(&_hold_nset
);
317 sigaddset(&_hold_nset
, SIGHUP
);
318 sigaddset(&_hold_nset
, SIGINT
);
319 sigaddset(&_hold_nset
, SIGQUIT
);
320 sigprocmask(SIG_BLOCK
, &_hold_nset
, &_hold_oset
);
329 if (--_hold_sigdepth
== 0)
330 sigprocmask(SIG_SETMASK
, &_hold_oset
, NULL
);
336 _nyd_chirp(ui8_t act
, char const *file
, ui32_t line
, char const *fun
)
338 struct nyd_info
*nip
= _nyd_infos
;
340 if (_nyd_curr
!= NELEM(_nyd_infos
))
346 nip
->ni_chirp_line
= ((ui32_t
)(act
& 0x3) << 29) | (line
& 0x1FFFFFFFu
);
347 nip
->ni_level
= ((act
== 0) ? _nyd_level
348 : (act
== 1) ? ++_nyd_level
: _nyd_level
--);
352 _nyd_oncrash(int signo
)
354 struct sigaction xact
;
356 struct nyd_info
*nip
;
359 xact
.sa_handler
= SIG_DFL
;
360 sigemptyset(&xact
.sa_mask
);
362 sigaction(signo
, &xact
, NULL
);
364 fprintf(stderr
, "\n\nNYD: program dying due to signal %d:\n", signo
);
365 if (_nyd_infos
[NELEM(_nyd_infos
) - 1].ni_file
!= NULL
)
366 for (i
= _nyd_curr
, nip
= _nyd_infos
+ i
; i
< NELEM(_nyd_infos
); ++i
)
368 for (i
= 0, nip
= _nyd_infos
; i
< _nyd_curr
; ++i
)
372 sigaddset(&xset
, signo
);
373 sigprocmask(SIG_UNBLOCK
, &xset
, NULL
);
381 touch(struct message
*mp
)
384 mp
->m_flag
|= MTOUCH
;
385 if (!(mp
->m_flag
& MREAD
))
386 mp
->m_flag
|= MREAD
| MSTATUS
;
391 is_dir(char const *name
)
397 if (!stat(name
, &sbuf
))
398 rv
= (S_ISDIR(sbuf
.st_mode
) != 0);
404 argcount(char **argv
)
409 for (ap
= argv
; *ap
++ != NULL
;)
412 return (int)PTR2SIZE(ap
- argv
- 1);
422 if ((cp
= ok_vlook(screen
)) == NULL
|| (s
= atoi(cp
)) <= 0)
423 s
= scrnheight
- 4; /* XXX no magics */
434 cp
= ok_vlook(PAGER
);
435 if (cp
== NULL
|| *cp
== '\0')
442 paging_seems_sensible(void)
448 if (IS_TTY_SESSION() && (cp
= ok_vlook(crt
)) != NULL
)
449 rv
= (*cp
!= '\0') ? (size_t)atol(cp
) : (size_t)scrnheight
;
455 page_or_print(FILE *fp
, size_t lines
)
463 if ((rows
= paging_seems_sensible()) != 0 && lines
== 0) {
464 while ((c
= getc(fp
)) != EOF
)
465 if (c
== '\n' && ++lines
> rows
)
470 if (rows
!= 0 && lines
>= rows
)
471 run_command(get_pager(), 0, fileno(fp
), -1, NULL
, NULL
, NULL
);
473 while ((c
= getc(fp
)) != EOF
)
479 which_protocol(char const *name
) /* XXX (->URL (yet auxlily.c)) */
485 enum protocol rv
= PROTO_UNKNOWN
;
488 if (name
[0] == '%' && name
[1] == ':')
490 for (cp
= name
; *cp
&& *cp
!= ':'; cp
++)
494 if (cp
[0] == ':' && cp
[1] == '/' && cp
[2] == '/') {
495 if (!strncmp(name
, "pop3://", 7)) {
499 fprintf(stderr
, tr(216, "No POP3 support compiled in.\n"));
501 } else if (!strncmp(name
, "pop3s://", 8)) {
502 #if defined HAVE_POP3 && defined HAVE_SSL
506 fprintf(stderr
, tr(216, "No POP3 support compiled in.\n"));
509 fprintf(stderr
, tr(225, "No SSL support compiled in.\n"));
512 } else if (!strncmp(name
, "imap://", 7)) {
516 fprintf(stderr
, tr(269, "No IMAP support compiled in.\n"));
518 } else if (!strncmp(name
, "imaps://", 8)) {
519 #if defined HAVE_IMAP && defined HAVE_SSL
523 fprintf(stderr
, tr(269, "No IMAP support compiled in.\n"));
526 fprintf(stderr
, tr(225, "No SSL support compiled in.\n"));
533 /* TODO This is the de facto maildir code and thus belongs into there!
534 * TODO and: we should have maildir:// and mbox:// pseudo-protos, instead of
535 * TODO or (more likely) in addition to *newfolders*) */
538 np
= ac_alloc((sz
= strlen(name
)) + 4 +1);
539 memcpy(np
, name
, sz
+ 1);
540 if (!stat(name
, &st
)) {
541 if (S_ISDIR(st
.st_mode
) &&
542 (memcpy(np
+sz
, "/tmp", 4), !stat(np
, &st
) && S_ISDIR(st
.st_mode
)) &&
543 (memcpy(np
+sz
, "/new", 4), !stat(np
, &st
) && S_ISDIR(st
.st_mode
)) &&
544 (memcpy(np
+sz
, "/cur", 4), !stat(np
, &st
) && S_ISDIR(st
.st_mode
)))
546 } else if ((cp
= ok_vlook(newfolders
)) != NULL
&& !strcmp(cp
, "maildir"))
555 torek_hash(char const *name
)
557 /* Chris Torek's hash.
558 * NOTE: need to change *at least* create-okey-map.pl when changing the
563 while (*name
!= '\0') {
572 pjw(char const *cp
) /* TODO obsolete that -> torek_hash */
579 h
= (h
<< 4 & 0xffffffff) + (*cp
&0377);
580 if ((g
= h
& 0xf0000000) != 0) {
592 static ui32_t
const primes
[] = {
593 509, 1021, 2039, 4093, 8191, 16381, 32749, 65521,
594 131071, 262139, 524287, 1048573, 2097143, 4194301,
595 8388593, 16777213, 33554393, 67108859, 134217689,
596 268435399, 536870909, 1073741789, 2147483647
599 ui32_t mprime
= 7, cutlim
;
603 cutlim
= (n
< 65536 ? n
<< 2 : (n
< 262144 ? n
<< 1 : n
));
605 for (i
= 0; i
< NELEM(primes
); i
++)
606 if ((mprime
= primes
[i
]) >= cutlim
)
608 if (i
== NELEM(primes
) && mprime
< n
)
615 expand_shell_escape(char const **s
, bool_t use_nail_extensions
)
623 if ((c
= *xs
& 0xFF) == '\0')
629 switch ((c
= *xs
& 0xFF)) {
631 case 'a': c
= '\a'; break;
632 case 'b': c
= '\b'; break;
633 case 'c': c
= PROMPT_STOP
; break;
634 case 'f': c
= '\f'; break;
635 case 'n': c
= '\n'; break;
636 case 'r': c
= '\r'; break;
637 case 't': c
= '\t'; break;
638 case 'v': c
= '\v'; break;
640 for (++xs
, c
= 0, n
= 4; --n
> 0 && octalchar(*xs
); ++xs
) {
645 /* S-nail extension for nice (get)prompt(()) support */
650 if (use_nail_extensions
) {
652 case '&': c
= ok_blook(bsdcompat
) ? '&' : '?'; break;
653 case '?': c
= exec_last_comm_error
? '1' : '0'; break;
654 case '$': c
= PROMPT_DOLLAR
; break;
655 case '@': c
= PROMPT_AT
; break;
661 /* A sole <backslash> at EOS is treated as-is! */
675 getprompt(void) /* TODO evaluate only as necessary (needs a bit) */
677 static char buf
[PROMPT_BUFFER_SIZE
];
680 char const *ccp_base
, *ccp
;
681 size_t NATCH_CHAR( cclen_base COMMA cclen COMMA
) maxlen
, dfmaxlen
;
686 if ((ccp_base
= ok_vlook(prompt
)) == NULL
|| *ccp_base
== '\0')
688 NATCH_CHAR( cclen_base
= strlen(ccp_base
); )
690 dfmaxlen
= 0; /* keep CC happy */
694 NATCH_CHAR( cclen
= cclen_base
; )
695 maxlen
= sizeof(buf
) -1;
703 #ifdef HAVE_NATCH_CHAR
704 c
= mblen(ccp
, cclen
); /* TODO use mbrtowc() */
713 } else if ((l
= c
) > 1) {
723 if ((c
= expand_shell_escape(&ccp
, TRU1
)) > 0) {
729 if (c
== 0 || c
== PROMPT_STOP
)
733 char const *a
= (c
== PROMPT_DOLLAR
) ? account_name
: displayname
;
736 if ((l
= field_put_bidi_clip(cp
, dfmaxlen
, a
, strlen(a
))) > 0) {
756 nodename(int mayoverride
)
758 static char *sys_hostname
, *hostname
; /* XXX free-at-exit */
764 struct addrinfo hints
, *res
;
766 struct hostent
*hent
;
771 if (mayoverride
&& (hn
= ok_vlook(hostname
)) != NULL
&& *hn
!= '\0') {
773 } else if ((hn
= sys_hostname
) == NULL
) {
778 memset(&hints
, 0, sizeof hints
);
779 hints
.ai_family
= AF_UNSPEC
;
780 hints
.ai_socktype
= SOCK_DGRAM
; /* (dummy) */
781 hints
.ai_flags
= AI_CANONNAME
;
782 if (getaddrinfo(hn
, "0", &hints
, &res
) == 0) {
783 if (res
->ai_canonname
!= NULL
) {
784 size_t l
= strlen(res
->ai_canonname
) +1;
786 memcpy(hn
, res
->ai_canonname
, l
);
791 hent
= gethostbyname(hn
);
796 sys_hostname
= sstrdup(hn
);
797 #if defined HAVE_SOCKETS && defined HAVE_IPV6
798 if (hn
!= ut
.nodename
)
804 if (hostname
!= NULL
&& hostname
!= sys_hostname
)
806 hostname
= sstrdup(hn
);
812 url_parse(struct url
*urlp
, enum cproto cproto
, char const *data
)
814 #if defined HAVE_SMTP && defined HAVE_POP3 && defined HAVE_IMAP
817 #if defined HAVE_SMTP || defined HAVE_POP3 || defined HAVE_IMAP
825 memset(urlp
, 0, sizeof *urlp
);
826 urlp
->url_input
= data
;
827 urlp
->url_cproto
= cproto
;
829 /* Network protocol */
830 #define _protox(X,Y) \
831 urlp->url_portno = Y;\
832 memcpy(urlp->url_proto, X "://", sizeof(X "://"));\
833 urlp->url_proto[sizeof(X) -1] = '\0';\
834 urlp->url_proto_len = sizeof(X) -1;\
835 urlp->url_proto_xlen = sizeof(X "://") -1
836 #define __if(X,Y,Z) \
837 if (!ascncasecmp(data, X "://", sizeof(X "://") -1)) {\
839 data += sizeof(X "://") -1;\
840 do { Z; } while (0);\
843 #define _if(X,Y) __if(X, Y, (void)0)
845 # define _ifs(X,Y) __if(X, Y, urlp->url_needs_tls = TRU1)
847 # define _ifs(X,Y) goto jeproto;
854 _if ("submission", 587)
865 _protox("pop3", 110);
874 _protox("imap", 143);
886 if (strstr(data
, "://") != NULL
) {
887 #if !defined __ALLPROTO || !defined HAVE_SSL
890 fprintf(stderr
, tr(574, "URL `proto://' prefix invalid: `%s'\n"),
896 /* User and password, I */
898 if ((cp
= UNCONST(last_at_before_slash(data
))) == NULL
)
901 urlp
->url_had_user
= TRU1
;
902 urlp
->url_user
.s
= savestrbuf(data
, urlp
->url_user
.l
= PTR2SIZE(cp
- data
));
905 /* And also have a password? */
906 if ((cp
= strchr(urlp
->url_user
.s
, ':')) != NULL
) {
907 x
= urlp
->url_user
.s
;
908 urlp
->url_user
.s
= savestrbuf(x
, urlp
->url_user
.l
= PTR2SIZE(cp
- x
));
909 urlp
->url_pass
.l
= strlen(urlp
->url_pass
.s
= urlxdec(++cp
));
910 urlp
->url_pass_enc
.l
= strlen(
911 urlp
->url_pass_enc
.s
= urlxenc(urlp
->url_pass
.s
, FAL0
));
914 /* Servername and port -- and possible path suffix */
916 if ((cp
= strchr(data
, ':')) != NULL
) { /* TODO URL parse, IPv6 support */
920 urlp
->url_port
= x
= savestr(x
= cp
+ 1);
921 if ((x
= strchr(x
, '/')) != NULL
)
923 l
= strtol(urlp
->url_port
, &eptr
, 10);
924 if (*eptr
!= '\0' || l
<= 0 || UICMP(32, l
, >=, 0xFFFFu
)) {
925 fprintf(stderr
, tr(573, "URL with invalid port number: `%s'\n"),
929 urlp
->url_portno
= (ui16_t
)l
;
931 if ((x
= strchr(data
, '/')) != NULL
)
932 data
= savestrbuf(data
, PTR2SIZE(x
- data
));
933 cp
= UNCONST(data
+ strlen(data
));
936 /* A (non-empty) path may only occur with IMAP */
937 if (x
!= NULL
&& x
[1] != '\0') {
938 if (cproto
!= CPROTO_IMAP
) {
939 fprintf(stderr
, tr(575, "URL protocol doesn't support paths: `%s'\n"),
943 urlp
->url_path
.l
= strlen(++x
);
944 urlp
->url_path
.s
= savestrbuf(x
, urlp
->url_path
.l
);
947 urlp
->url_host
.s
= savestrbuf(data
, urlp
->url_host
.l
= PTR2SIZE(cp
- data
));
949 for (cp
= urlp
->url_host
.s
, i
= urlp
->url_host
.l
; i
!= 0; ++cp
, --i
)
950 *cp
= lowerconv(*cp
);
955 struct str
*s
= &urlp
->url_hp
;
957 s
->s
= salloc(urlp
->url_host
.l
+ 1 + sizeof("65536")-1 +1);
958 memcpy(s
->s
, urlp
->url_host
.s
, i
= urlp
->url_host
.l
);
959 if (urlp
->url_port
!= NULL
) {
960 size_t j
= strlen(urlp
->url_port
);
962 memcpy(s
->s
+ i
, urlp
->url_port
, j
);
970 * If there was no user in the URL, do we have *user-HOST* or *user*? */
971 if (!urlp
->url_had_user
) {
972 char const usrstr
[] = "user-";
973 size_t const usrlen
= sizeof(usrstr
) -1;
975 cp
= ac_alloc(usrlen
+ urlp
->url_hp
.l
+1);
976 memcpy(cp
, usrstr
, usrlen
);
977 memcpy(cp
+ usrlen
, urlp
->url_hp
.s
, urlp
->url_hp
.l
+1);
978 if ((urlp
->url_user
.s
= vok_vlook(cp
)) == NULL
) {
979 cp
[usrlen
- 1] = '\0';
980 if ((urlp
->url_user
.s
= vok_vlook(cp
)) == NULL
)
981 urlp
->url_user
.s
= UNCONST(myname
);
986 urlp
->url_user
.l
= strlen(urlp
->url_user
.s
= urlxdec(urlp
->url_user
.s
));
987 urlp
->url_user_enc
.l
= strlen(
988 urlp
->url_user_enc
.s
= urlxenc(urlp
->url_user
.s
, FAL0
));
991 if (urlp
->url_user_enc
.l
== 0)
992 urlp
->url_uhp
= urlp
->url_hp
;
994 struct str
*s
= &urlp
->url_uhp
;
995 size_t i
= urlp
->url_user_enc
.l
;
997 s
->s
= salloc(i
+ 1 + urlp
->url_hp
.l
+1);
999 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
1002 memcpy(s
->s
+ i
, urlp
->url_hp
.s
, urlp
->url_hp
.l
+1);
1003 i
+= urlp
->url_hp
.l
;
1008 * For SMTP we apply ridiculously complicated *v15-compat* plus
1009 * *smtp-hostname* / *hostname* dependent rules */
1012 if (cproto
== CPROTO_SMTP
&& ok_blook(v15_compat
) &&
1013 (cp
= ok_vlook(smtp_hostname
)) != NULL
) {
1016 h
.s
= savestrbuf(cp
, h
.l
= strlen(cp
));
1020 if (urlp
->url_user_enc
.l
== 0)
1023 struct str
*s
= &urlp
->url_uh
;
1024 size_t i
= urlp
->url_user_enc
.l
;
1026 s
->s
= salloc(i
+ 1 + h
.l
+1);
1028 memcpy(s
->s
, urlp
->url_user_enc
.s
, i
);
1031 memcpy(s
->s
+ i
, h
.s
, h
.l
+1);
1037 /* Finally, for fun: .url_proto://.url_uhp[/.url_path] */
1039 char *ud
= salloc((i
= urlp
->url_proto_xlen
+ urlp
->url_uhp
.l
) +
1040 1 + urlp
->url_path
.l
+1);
1042 urlp
->url_proto
[urlp
->url_proto_len
] = ':';
1043 memcpy(sstpcpy(ud
, urlp
->url_proto
), urlp
->url_uhp
.s
, urlp
->url_uhp
.l
+1);
1044 urlp
->url_proto
[urlp
->url_proto_len
] = '\0';
1046 if (urlp
->url_path
.l
== 0)
1047 urlp
->url_puhp
= urlp
->url_puhpp
= ud
;
1049 urlp
->url_puhp
= savestrbuf(ud
, i
);
1050 urlp
->url_puhpp
= ud
;
1053 memcpy(ud
, urlp
->url_path
.s
, urlp
->url_path
.l
+1);
1058 #endif /* __ANYPROTO */
1067 ccred_lookup_old(struct ccred
*ccp
, enum cproto cproto
, char const *addr
)
1069 char const *pname
, *pxstr
, *authdef
;
1070 size_t pxlen
, addrlen
, i
;
1073 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1075 bool_t addr_is_nuser
= FAL0
; /* XXX v15.0 legacy! v15_compat */
1078 memset(ccp
, 0, sizeof *ccp
);
1084 pxstr
= "smtp-auth";
1085 pxlen
= sizeof("smtp-auth") -1;
1086 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1087 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1089 addr_is_nuser
= TRU1
;
1093 pxstr
= "pop3-auth";
1094 pxlen
= sizeof("pop3-auth") -1;
1095 authmask
= AUTHTYPE_PLAIN
;
1100 pxstr
= "imap-auth";
1101 pxlen
= sizeof("imap-auth") -1;
1102 authmask
= AUTHTYPE_LOGIN
| AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1107 ccp
->cc_cproto
= cproto
;
1108 addrlen
= strlen(addr
);
1109 vbuf
= ac_alloc(pxlen
+ addrlen
+ sizeof("-password-")-1 +1);
1110 memcpy(vbuf
, pxstr
, pxlen
);
1112 /* Authentication type */
1114 memcpy(vbuf
+ pxlen
+ 1, addr
, addrlen
+1);
1115 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1117 if ((s
= vok_vlook(vbuf
)) == NULL
)
1118 s
= UNCONST(authdef
);
1121 if (!asccasecmp(s
, "none")) {
1122 ccp
->cc_auth
= "NONE";
1123 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1125 } else if (!asccasecmp(s
, "plain")) {
1126 ccp
->cc_auth
= "PLAIN";
1127 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1128 ware
= REQ_PASS
| REQ_USER
;
1129 } else if (!asccasecmp(s
, "login")) {
1130 ccp
->cc_auth
= "LOGIN";
1131 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1132 ware
= REQ_PASS
| REQ_USER
;
1133 } else if (!asccasecmp(s
, "cram-md5")) {
1134 ccp
->cc_auth
= "CRAM-MD5";
1135 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1136 ware
= REQ_PASS
| REQ_USER
;
1137 } else if (!asccasecmp(s
, "gssapi")) {
1138 ccp
->cc_auth
= "GSS-API";
1139 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1141 #if 0 /* TODO SASL (homebrew `auto'matic indeed) */
1142 } else if (!asccasecmp(cp
, "sasl")) {
1143 ware
= REQ_USER
| WANT_PASS
;
1148 if (!(ccp
->cc_authtype
& authmask
)) {
1149 fprintf(stderr
, tr(273, "Unsupported %s authentication method: %s\n"),
1155 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1156 fprintf(stderr
, tr(277, "No CRAM-MD5 support compiled in.\n"));
1162 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1163 fprintf(stderr
, tr(272, "No GSS-API support compiled in.\n"));
1170 if (!(ware
& (WANT_USER
| REQ_USER
)))
1173 if (!addr_is_nuser
) {
1174 if ((s
= UNCONST(last_at_before_slash(addr
))) != NULL
) {
1175 ccp
->cc_user
.s
= urlxdec(savestrbuf(addr
, PTR2SIZE(s
- addr
)));
1176 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
);
1177 } else if (ware
& REQ_USER
)
1182 memcpy(vbuf
+ pxlen
, "-user-", i
= sizeof("-user-") -1);
1184 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1185 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1187 if ((s
= vok_vlook(vbuf
)) == NULL
&& (ware
& REQ_USER
)) {
1188 if ((s
= getuser(NULL
)) != NULL
)
1191 jgetuser
: /* TODO v15.0: today we simply bail, but we should call getuser().
1192 * TODO even better: introduce `PROTO-user' and `PROTO-pass' and
1193 * TODO check that first, then! change control flow, grow `vbuf' */
1194 fprintf(stderr
, tr(274,
1195 "A user is necessary for %s authentication.\n"), pname
);
1201 ccp
->cc_user
.l
= strlen(ccp
->cc_user
.s
= s
);
1205 if (!(ware
& (WANT_PASS
| REQ_PASS
)))
1208 if (!addr_is_nuser
) {
1209 memcpy(vbuf
, "password-", i
= sizeof("password-") -1);
1211 memcpy(vbuf
+ pxlen
, "-password-", i
= sizeof("-password-") -1);
1214 memcpy(vbuf
+ i
, addr
, addrlen
+1);
1215 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1217 if ((!addr_is_nuser
|| (s
= vok_vlook(vbuf
)) == NULL
) &&
1218 (ware
& REQ_PASS
)) {
1219 if ((s
= getpassword(NULL
)) == NULL
) {
1220 fprintf(stderr
, tr(275,
1221 "A password is necessary for %s authentication.\n"), pname
);
1227 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= urlxdec(s
));
1232 return (ccp
!= NULL
);
1236 ccred_lookup(struct ccred
*ccp
, struct url
*urlp
)
1238 char const *pstr
, *authdef
;
1242 enum {NONE
=0, WANT_PASS
=1<<0, REQ_PASS
=1<<1, WANT_USER
=1<<2, REQ_USER
=1<<3}
1246 memset(ccp
, 0, sizeof *ccp
);
1247 ccp
->cc_user
= urlp
->url_user
;
1249 switch ((ccp
->cc_cproto
= urlp
->url_cproto
)) {
1253 plen
= sizeof("smtp") -1;
1254 authmask
= AUTHTYPE_NONE
| AUTHTYPE_PLAIN
| AUTHTYPE_LOGIN
|
1255 AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1260 plen
= sizeof("pop3") -1;
1261 authmask
= AUTHTYPE_PLAIN
;
1266 plen
= sizeof("imap") -1;
1267 authmask
= AUTHTYPE_LOGIN
| AUTHTYPE_CRAM_MD5
| AUTHTYPE_GSSAPI
;
1272 /* Note: "password-" is longer than "-auth-", ditto .url_uhp and .url_hp */
1273 vbuf
= ac_alloc(plen
+ sizeof("password-")-1 + urlp
->url_uhp
.l
+1);
1274 memcpy(vbuf
, pstr
, plen
);
1276 /* Authentication type */
1277 memcpy(vbuf
+ plen
, "-auth-", i
= sizeof("-auth-") -1);
1279 /* -USER@HOST, -HOST, '' */
1280 memcpy(vbuf
+ i
, urlp
->url_uhp
.s
, urlp
->url_uhp
.l
+1);
1281 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1282 memcpy(vbuf
+ i
, urlp
->url_hp
.s
, urlp
->url_hp
.l
+1);
1283 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1284 vbuf
[plen
+ sizeof("-auth") -1] = '\0';
1285 if ((s
= vok_vlook(vbuf
)) == NULL
)
1286 s
= UNCONST(authdef
);
1290 if (!asccasecmp(s
, "none")) {
1291 ccp
->cc_auth
= "NONE";
1292 ccp
->cc_authtype
= AUTHTYPE_NONE
;
1294 } else if (!asccasecmp(s
, "plain")) {
1295 ccp
->cc_auth
= "PLAIN";
1296 ccp
->cc_authtype
= AUTHTYPE_PLAIN
;
1297 ware
= REQ_PASS
| REQ_USER
;
1298 } else if (!asccasecmp(s
, "login")) {
1299 ccp
->cc_auth
= "LOGIN";
1300 ccp
->cc_authtype
= AUTHTYPE_LOGIN
;
1301 ware
= REQ_PASS
| REQ_USER
;
1302 } else if (!asccasecmp(s
, "cram-md5")) {
1303 ccp
->cc_auth
= "CRAM-MD5";
1304 ccp
->cc_authtype
= AUTHTYPE_CRAM_MD5
;
1305 ware
= REQ_PASS
| REQ_USER
;
1306 } else if (!asccasecmp(s
, "gssapi")) {
1307 ccp
->cc_auth
= "GSS-API";
1308 ccp
->cc_authtype
= AUTHTYPE_GSSAPI
;
1310 #if 0 /* TODO SASL (homebrew `auto'matic indeed) */
1311 } else if (!asccasecmp(cp
, "sasl")) {
1312 ware
= REQ_USER
| WANT_PASS
;
1317 if (!(ccp
->cc_authtype
& authmask
)) {
1318 fprintf(stderr
, tr(273, "Unsupported %s authentication method: %s\n"),
1324 if (ccp
->cc_authtype
== AUTHTYPE_CRAM_MD5
) {
1325 fprintf(stderr
, tr(277, "No CRAM-MD5 support compiled in.\n"));
1331 if (ccp
->cc_authtype
== AUTHTYPE_GSSAPI
) {
1332 fprintf(stderr
, tr(272, "No GSS-API support compiled in.\n"));
1339 if ((ccp
->cc_pass
= urlp
->url_pass
).s
!= NULL
)
1342 memcpy(vbuf
, "password-", i
= sizeof("password-") -1);
1343 /* -USER@HOST, -HOST, '' */
1344 memcpy(vbuf
+ i
, urlp
->url_uhp
.s
, urlp
->url_uhp
.l
+1);
1345 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1346 memcpy(vbuf
+ i
, urlp
->url_hp
.s
, urlp
->url_hp
.l
+1);
1347 if ((s
= vok_vlook(vbuf
)) == NULL
) {
1349 if ((s
= vok_vlook(vbuf
)) == NULL
&& (ware
& REQ_PASS
)) {
1350 if ((s
= getpassword(NULL
)) != NULL
)
1353 fprintf(stderr
, tr(275,
1354 "A password is necessary for %s authentication.\n"), pstr
);
1361 ccp
->cc_pass
.l
= strlen(ccp
->cc_pass
.s
= urlxdec(s
));
1366 return (ccp
!= NULL
);
1370 getrandstring(size_t length
)
1372 static unsigned char nodedigest
[16];
1386 data
= ac_alloc(length
);
1388 if ((fd
= open("/dev/urandom", O_RDONLY
)) == -1 ||
1389 length
!= (size_t)read(fd
, data
, length
)) {
1396 md5_update(&ctx
, (unsigned char*)cp
, strlen(cp
));
1397 md5_final(nodedigest
, &ctx
);
1399 /* In that case it's only used for boundaries and Message-Id:s so that
1400 * srand(3) should suffice */
1402 for (i
= 0; i
< sizeof(nodedigest
); ++i
)
1403 nodedigest
[i
] = (unsigned char)(cp
[i
% j
] ^ rand());
1406 for (i
= 0; i
< length
; i
++)
1407 data
[i
] = (char)((int)(255 * (rand() / (RAND_MAX
+ 1.0))) ^
1408 nodedigest
[i
% sizeof nodedigest
]);
1413 b64_encode_buf(&b64
, data
, length
, B64_SALLOC
);
1415 assert(length
< b64
.l
);
1416 b64
.s
[length
] = '\0';
1423 md5tohex(char hex
[MD5TOHEX_SIZE
], void const *vp
)
1425 char const *cp
= vp
;
1429 for (i
= 0; i
< MD5TOHEX_SIZE
/ 2; ++i
) {
1431 # define __hex(n) ((n) > 9 ? (n) - 10 + 'a' : (n) + '0')
1432 hex
[j
] = __hex((cp
[i
] & 0xF0) >> 4);
1433 hex
[++j
] = __hex(cp
[i
] & 0x0F);
1441 cram_md5_string(struct str
const *user
, struct str
const *pass
,
1445 char digest
[16], *cp
;
1449 in
.s
= UNCONST(b64
);
1450 in
.l
= strlen(in
.s
);
1451 b64_decode(&out
, &in
, NULL
);
1452 assert(out
.s
!= NULL
);
1454 hmac_md5((uc_it
*)out
.s
, out
.l
, (uc_it
*)pass
->s
, pass
->l
, digest
);
1456 cp
= md5tohex(salloc(MD5TOHEX_SIZE
+1), digest
);
1458 in
.l
= user
->l
+ MD5TOHEX_SIZE
+1;
1459 in
.s
= ac_alloc(user
->l
+ 1 + MD5TOHEX_SIZE
+1);
1460 memcpy(in
.s
, user
->s
, user
->l
);
1461 in
.s
[user
->l
] = ' ';
1462 memcpy(in
.s
+ user
->l
+ 1, cp
, MD5TOHEX_SIZE
);
1463 b64_encode(&out
, &in
, B64_SALLOC
| B64_CRLF
);
1470 hmac_md5(unsigned char *text
, int text_len
, unsigned char *key
, int key_len
,
1474 * This code is taken from
1476 * Network Working Group H. Krawczyk
1477 * Request for Comments: 2104 IBM
1478 * Category: Informational M. Bellare
1485 * HMAC: Keyed-Hashing for Message Authentication
1488 unsigned char k_ipad
[65]; /* inner padding - key XORd with ipad */
1489 unsigned char k_opad
[65]; /* outer padding - key XORd with opad */
1490 unsigned char tk
[16];
1494 /* if key is longer than 64 bytes reset it to key=MD5(key) */
1499 md5_update(&tctx
, key
, key_len
);
1500 md5_final(tk
, &tctx
);
1506 /* the HMAC_MD5 transform looks like:
1508 * MD5(K XOR opad, MD5(K XOR ipad, text))
1510 * where K is an n byte key
1511 * ipad is the byte 0x36 repeated 64 times
1512 * opad is the byte 0x5c repeated 64 times
1513 * and text is the data being protected */
1515 /* start out by storing key in pads */
1516 memset(k_ipad
, 0, sizeof k_ipad
);
1517 memset(k_opad
, 0, sizeof k_opad
);
1518 memcpy(k_ipad
, key
, key_len
);
1519 memcpy(k_opad
, key
, key_len
);
1521 /* XOR key with ipad and opad values */
1522 for (i
=0; i
<64; i
++) {
1527 /* perform inner MD5 */
1528 md5_init(&context
); /* init context for 1st pass */
1529 md5_update(&context
, k_ipad
, 64); /* start with inner pad */
1530 md5_update(&context
, text
, text_len
); /* then text of datagram */
1531 md5_final(digest
, &context
); /* finish up 1st pass */
1533 /* perform outer MD5 */
1534 md5_init(&context
); /* init context for 2nd pass */
1535 md5_update(&context
, k_opad
, 64); /* start with outer pad */
1536 md5_update(&context
, digest
, 16); /* then results of 1st hash */
1537 md5_final(digest
, &context
); /* finish up 2nd pass */
1540 #endif /* HAVE_MD5 */
1543 makedir(char const *name
)
1546 enum okay rv
= STOP
;
1549 if (!mkdir(name
, 0700))
1553 if ((e
== EEXIST
|| e
== ENOSYS
) && !stat(name
, &st
) &&
1554 S_ISDIR(st
.st_mode
))
1563 cwget(struct cw
*cw
)
1565 enum okay rv
= STOP
;
1568 if ((cw
->cw_fd
= open(".", O_RDONLY
)) == -1)
1570 if (fchdir(cw
->cw_fd
) == -1) {
1581 cwret(struct cw
*cw
)
1583 enum okay rv
= STOP
;
1586 if (!fchdir(cw
->cw_fd
))
1593 cwrelse(struct cw
*cw
)
1600 #else /* !HAVE_FCHDIR */
1602 cwget(struct cw
*cw
)
1604 enum okay rv
= STOP
;
1607 if (getcwd(cw
->cw_wd
, sizeof cw
->cw_wd
) != NULL
&& !chdir(cw
->cw_wd
))
1614 cwret(struct cw
*cw
)
1616 enum okay rv
= STOP
;
1619 if (!chdir(cw
->cw_wd
))
1626 cwrelse(struct cw
*cw
)
1632 #endif /* !HAVE_FCHDIR */
1635 field_detect_clip(size_t maxlen
, char const *buf
, size_t blen
)/*TODO mbrtowc()*/
1640 #ifdef HAVE_NATCH_CHAR
1641 maxlen
= MIN(maxlen
, blen
);
1642 for (rv
= 0; maxlen
> 0;) {
1643 int ml
= mblen(buf
, maxlen
);
1653 rv
= MIN(blen
, maxlen
);
1660 field_put_bidi_clip(char *store
, size_t maxlen
, char const *buf
, size_t blen
)
1662 NATCH_CHAR( struct bidi_info bi
; )
1663 size_t rv
NATCH_CHAR( COMMA i
);
1670 #ifdef HAVE_NATCH_CHAR
1671 bidi_info_create(&bi
);
1672 if (bi
.bi_start
.l
== 0 || !bidi_info_needed(buf
, blen
)) {
1677 if (maxlen
>= (i
= bi
.bi_pad
+ bi
.bi_end
.l
+ bi
.bi_start
.l
))
1682 if ((i
= bi
.bi_start
.l
) > 0) {
1683 memcpy(store
, bi
.bi_start
.s
, i
);
1689 while (maxlen
> 0) {
1690 int ml
= mblen(buf
, blen
);
1695 if (UICMP(z
, maxlen
, <, ml
))
1700 memcpy(store
, buf
, ml
);
1707 if ((i
= bi
.bi_end
.l
) > 0) {
1708 memcpy(store
, bi
.bi_end
.s
, i
);
1716 rv
= MIN(blen
, maxlen
);
1717 memcpy(store
, buf
, rv
);
1726 colalign(char const *cp
, int col
, int fill
, int *cols_decr_used_or_null
)
1728 NATCH_CHAR( struct bidi_info bi
; )
1729 int col_orig
= col
, n
, sz
;
1730 bool_t isbidi
, isuni
, isrepl
;
1734 /* Bidi only on request and when there is 8-bit data */
1735 isbidi
= isuni
= FAL0
;
1736 #ifdef HAVE_NATCH_CHAR
1737 isuni
= ((options
& OPT_UNICODE
) != 0);
1738 bidi_info_create(&bi
);
1739 if (bi
.bi_start
.l
== 0)
1741 if (!(isbidi
= bidi_info_needed(cp
, strlen(cp
))))
1744 if ((size_t)col
>= bi
.bi_pad
)
1751 np
= nb
= salloc(mb_cur_max
* strlen(cp
) +
1753 NATCH_CHAR( + (isbidi
? bi
.bi_start
.l
+ bi
.bi_end
.l
: 0) )
1756 #ifdef HAVE_NATCH_CHAR
1758 memcpy(np
, bi
.bi_start
.s
, bi
.bi_start
.l
);
1759 np
+= bi
.bi_start
.l
;
1763 while (*cp
!= '\0') {
1764 #ifdef HAVE_C90AMEND1
1765 if (mb_cur_max
> 1) {
1770 if ((sz
= mbtowc(&wc
, cp
, mb_cur_max
)) == -1)
1772 else if (iswprint(wc
)) {
1773 # ifndef HAVE_WCWIDTH
1774 n
= 1 + (wc
>= 0x1100u
); /* TODO use S-CText isfullwidth() */
1776 if ((n
= wcwidth(wc
)) == -1)
1785 isrepl
= !isprint(*cp
);
1793 np
[0] = (char)0xEFu
;
1794 np
[1] = (char)0xBFu
;
1795 np
[2] = (char)0xBDu
;
1800 } else if (sz
== 1 && spacechar(*cp
)) {
1808 if (fill
&& col
!= 0) {
1810 memmove(nb
+ col
, nb
, PTR2SIZE(np
- nb
));
1811 memset(nb
, ' ', col
);
1813 memset(np
, ' ', col
);
1818 #ifdef HAVE_NATCH_CHAR
1820 memcpy(np
, bi
.bi_end
.s
, bi
.bi_end
.l
);
1826 if (cols_decr_used_or_null
!= NULL
)
1827 *cols_decr_used_or_null
-= col_orig
- col
;
1833 makeprint(struct str
const *in
, struct str
*out
)
1835 static int print_all_chars
= -1;
1837 char const *inp
, *maxp
;
1842 if (print_all_chars
== -1)
1843 print_all_chars
= ok_blook(print_all_chars
);
1845 out
->s
= outp
= smalloc(DBG( msz
= ) in
->l
*mb_cur_max
+ 2u*mb_cur_max
);
1849 if (print_all_chars
) {
1851 memcpy(outp
, inp
, out
->l
);
1855 #ifdef HAVE_NATCH_CHAR
1856 if (mb_cur_max
> 1) {
1857 char mbb
[MB_LEN_MAX
+ 1];
1860 bool_t isuni
= ((options
& OPT_UNICODE
) != 0);
1863 while (inp
< maxp
) {
1865 n
= mbtowc(&wc
, inp
, PTR2SIZE(maxp
- inp
));
1871 /* FIXME Why mbtowc() resetting here?
1872 * FIXME what about ISO 2022-JP plus -- those
1873 * FIXME will loose shifts, then!
1874 * FIXME THUS - we'd need special "known points"
1875 * FIXME to do so - say, after a newline!!
1876 * FIXME WE NEED TO CHANGE ALL USES +MBLEN! */
1877 mbtowc(&wc
, NULL
, mb_cur_max
);
1878 wc
= isuni
? 0xFFFD : '?';
1883 if (!iswprint(wc
) && wc
!= '\n' && wc
!= '\r' && wc
!= '\b' &&
1885 if ((wc
& ~(wchar_t)037) == 0)
1886 wc
= isuni
? 0x2400 | wc
: '?';
1887 else if (wc
== 0177)
1888 wc
= isuni
? 0x2421 : '?';
1890 wc
= isuni
? 0x2426 : '?';
1892 if ((n
= wctomb(mbb
, wc
)) <= 0)
1895 assert(out
->l
< msz
);
1896 for (i
= 0; i
< n
; ++i
)
1900 #endif /* NATCH_CHAR */
1903 while (inp
< maxp
) {
1905 if (!isprint(c
) && c
!= '\n' && c
!= '\r' && c
!= '\b' && c
!= '\t')
1912 out
->s
[out
->l
] = '\0';
1917 prstr(char const *s
)
1925 makeprint(&in
, &out
);
1926 rp
= savestrbuf(out
.s
, out
.l
);
1933 prout(char const *s
, size_t sz
, FILE *fp
)
1941 makeprint(&in
, &out
);
1942 n
= fwrite(out
.s
, 1, out
.l
, fp
);
1949 putuc(int u
, int c
, FILE *fp
)
1955 #ifdef HAVE_NATCH_CHAR
1956 if ((options
& OPT_UNICODE
) && (u
& ~(wchar_t)0177)) {
1957 char mbb
[MB_LEN_MAX
];
1960 if ((n
= wctomb(mbb
, u
)) > 0) {
1962 for (i
= 0; i
< n
; ++i
)
1963 if (putc(mbb
[i
] & 0377, fp
) == EOF
) {
1968 rv
= (putc('\0', fp
) != EOF
);
1973 rv
= (putc(c
, fp
) != EOF
);
1979 bidi_info_needed(char const *bdat
, size_t blen
)
1984 #ifdef HAVE_NATCH_CHAR
1985 if (options
& OPT_UNICODE
)
1986 for (; blen
> 0; ++bdat
, --blen
) {
1987 if ((ui8_t
)*bdat
> 0x7F) {
1988 /* TODO Checking for BIDI character: use S-CText fromutf8
1989 * TODO plus isrighttoleft (or whatever there will be)! */
1990 ui32_t c
, x
= (ui8_t
)*bdat
;
1992 if ((x
& 0xE0) == 0xC0) {
1997 } else if ((x
& 0xF0) == 0xE0) {
2021 /* (Very very fuzzy, awaiting S-CText for good) */
2022 if ((c
>= 0x05BE && c
<= 0x08E3) ||
2023 (c
>= 0xFB1D && c
<= 0xFEFC) ||
2024 (c
>= 0x10800 && c
<= 0x10C48) ||
2025 (c
>= 0x1EE00 && c
<= 0x1EEF1)) {
2031 #endif /* HAVE_NATCH_CHAR */
2037 bidi_info_create(struct bidi_info
*bip
)
2039 /* Unicode: how to isolate RIGHT-TO-LEFT scripts via *headline-bidi*
2040 * 1.1 (Jun 1993): U+200E (E2 80 8E) LEFT-TO-RIGHT MARK
2041 * 6.3 (Sep 2013): U+2068 (E2 81 A8) FIRST STRONG ISOLATE,
2042 * U+2069 (E2 81 A9) POP DIRECTIONAL ISOLATE
2043 * Worse results seen for: U+202D "\xE2\x80\xAD" U+202C "\xE2\x80\xAC" */
2044 NATCH_CHAR( char const *hb
; )
2047 memset(bip
, 0, sizeof *bip
);
2048 bip
->bi_start
.s
= bip
->bi_end
.s
= UNCONST("");
2050 #ifdef HAVE_NATCH_CHAR
2051 if ((options
& OPT_UNICODE
) && (hb
= ok_vlook(headline_bidi
)) != NULL
) {
2057 bip
->bi_start
.s
= bip
->bi_end
.s
= UNCONST("\xE2\x80\x8E");
2063 bip
->bi_start
.s
= UNCONST("\xE2\x81\xA8");
2064 bip
->bi_end
.s
= UNCONST("\xE2\x81\xA9");
2067 bip
->bi_start
.l
= bip
->bi_end
.l
= 3;
2075 colour_table_create(char const *pager_used
)
2077 union {char *cp
; char const *ccp
; void *vp
; struct colour_table
*ctp
;} u
;
2079 struct colour_table
*ct
;
2082 if (ok_blook(colour_disable
))
2085 /* If pager, check wether it is allowed to use colour */
2086 if (pager_used
!= NULL
) {
2089 if ((u
.cp
= ok_vlook(colour_pagers
)) == NULL
)
2090 u
.ccp
= COLOUR_PAGERS
;
2091 pager
= savestr(u
.cp
);
2093 while ((u
.cp
= n_strsep(&pager
, ',', TRU1
)) != NULL
)
2094 if (strstr(pager_used
, u
.cp
) != NULL
)
2099 /* $TERM is different in that we default to false unless whitelisted */
2101 char *term
, *okterms
;
2103 /* Don't use getenv(), but force copy-in into our own tables.. */
2104 if ((term
= _var_voklook("TERM")) == NULL
)
2106 if ((okterms
= ok_vlook(colour_terms
)) == NULL
)
2107 okterms
= UNCONST(COLOUR_TERMS
);
2108 okterms
= savestr(okterms
);
2111 while ((u
.cp
= n_strsep(&okterms
, ',', TRU1
)) != NULL
)
2112 if (!strncmp(u
.cp
, term
, i
))
2118 colour_table
= ct
= salloc(sizeof *ct
); /* XXX lex.c yet resets (FILTER!) */
2121 enum colourspec cspec
;
2124 {ok_v_colour_msginfo
, COLOURSPEC_MSGINFO
, COLOUR_MSGINFO
},
2125 {ok_v_colour_partinfo
, COLOURSPEC_PARTINFO
, COLOUR_PARTINFO
},
2126 {ok_v_colour_from_
, COLOURSPEC_FROM_
, COLOUR_FROM_
},
2127 {ok_v_colour_header
, COLOURSPEC_HEADER
, COLOUR_HEADER
},
2128 {ok_v_colour_uheader
, COLOURSPEC_UHEADER
, COLOUR_UHEADER
}
2131 for (i
= 0; i
< NELEM(map
); ++i
) {
2132 if ((u
.cp
= _var_oklook(map
[i
].okey
)) == NULL
)
2133 u
.ccp
= map
[i
].defval
;
2134 u
.cp
= _colour_iso6429(u
.ccp
);
2135 ct
->ct_csinfo
[map
[i
].cspec
].l
= strlen(u
.cp
);
2136 ct
->ct_csinfo
[map
[i
].cspec
].s
= u
.cp
;
2139 ct
->ct_csinfo
[COLOURSPEC_RESET
].l
= sizeof("\033[0m") -1;
2140 ct
->ct_csinfo
[COLOURSPEC_RESET
].s
= UNCONST("\033[0m");
2142 if ((u
.cp
= ok_vlook(colour_user_headers
)) == NULL
)
2143 u
.ccp
= COLOUR_USER_HEADERS
;
2144 ct
->ct_csinfo
[COLOURSPEC_RESET
+ 1].l
= i
= strlen(u
.ccp
);
2145 ct
->ct_csinfo
[COLOURSPEC_RESET
+ 1].s
= (i
== 0) ? NULL
: savestr(u
.ccp
);
2151 colour_put(FILE *fp
, enum colourspec cs
)
2154 if (colour_table
!= NULL
) {
2155 struct str
const *cp
= colour_get(cs
);
2157 fwrite(cp
->s
, cp
->l
, 1, fp
);
2163 colour_put_header(FILE *fp
, char const *name
)
2165 enum colourspec cs
= COLOURSPEC_HEADER
;
2166 struct str
const *uheads
;
2167 char *cp
, *cp_base
, *x
;
2171 if (colour_table
== NULL
)
2173 /* Normal header colours if there are no user headers */
2174 uheads
= colour_table
->ct_csinfo
+ COLOURSPEC_RESET
+ 1;
2175 if (uheads
->s
== NULL
)
2178 /* Iterate over all entries in the *colour-user-headers* list */
2179 cp
= ac_alloc(uheads
->l
+1);
2180 memcpy(cp
, uheads
->s
, uheads
->l
+1);
2182 namelen
= strlen(name
);
2183 while ((x
= n_strsep(&cp
, ',', TRU1
)) != NULL
) {
2184 size_t l
= (cp
!= NULL
) ? PTR2SIZE(cp
- x
) - 1 : strlen(x
);
2185 if (l
== namelen
&& !ascncasecmp(x
, name
, namelen
)) {
2186 cs
= COLOURSPEC_UHEADER
;
2198 colour_reset(FILE *fp
)
2201 if (colour_table
!= NULL
)
2202 fwrite("\033[0m", 4, 1, fp
);
2206 FL
struct str
const *
2207 colour_get(enum colourspec cs
)
2209 struct str
const *rv
= NULL
;
2212 if (colour_table
!= NULL
)
2213 if ((rv
= colour_table
->ct_csinfo
+ cs
)->s
== NULL
)
2218 #endif /* HAVE_COLOUR */
2221 time_current_update(struct time_current
*tc
, bool_t full_update
)
2224 tc
->tc_time
= time(NULL
);
2226 memcpy(&tc
->tc_gm
, gmtime(&tc
->tc_time
), sizeof tc
->tc_gm
);
2227 memcpy(&tc
->tc_local
, localtime(&tc
->tc_time
), sizeof tc
->tc_local
);
2228 sstpcpy(tc
->tc_ctime
, ctime(&tc
->tc_time
));
2234 _out_of_memory(void)
2241 smalloc(size_t s SMALLOC_DEBUG_ARGS
)
2248 if ((rv
= malloc(s
)) == NULL
)
2255 srealloc(void *v
, size_t s SMALLOC_DEBUG_ARGS
)
2264 else if ((rv
= realloc(v
, s
)) == NULL
)
2271 scalloc(size_t nmemb
, size_t size SMALLOC_DEBUG_ARGS
)
2278 if ((rv
= calloc(nmemb
, size
)) == NULL
)
2284 #else /* !HAVE_DEBUG */
2285 CTA(sizeof(char) == sizeof(ui8_t
));
2287 # define _HOPE_SIZE (2 * 8 * sizeof(char))
2288 # define _HOPE_SET(C) \
2290 union mem_ptr __xl, __xu;\
2291 struct mem_chunk *__xc;\
2292 __xl.p_p = (C).p_p;\
2293 __xc = __xl.p_c - 1;\
2296 __xl.p_ui8p[0]=0xDE; __xl.p_ui8p[1]=0xAA;\
2297 __xl.p_ui8p[2]=0x55; __xl.p_ui8p[3]=0xAD;\
2298 __xl.p_ui8p[4]=0xBE; __xl.p_ui8p[5]=0x55;\
2299 __xl.p_ui8p[6]=0xAA; __xl.p_ui8p[7]=0xEF;\
2300 __xu.p_ui8p += __xc->mc_size - 8;\
2301 __xu.p_ui8p[0]=0xDE; __xu.p_ui8p[1]=0xAA;\
2302 __xu.p_ui8p[2]=0x55; __xu.p_ui8p[3]=0xAD;\
2303 __xu.p_ui8p[4]=0xBE; __xu.p_ui8p[5]=0x55;\
2304 __xu.p_ui8p[6]=0xAA; __xu.p_ui8p[7]=0xEF;\
2306 # define _HOPE_GET_TRACE(C,BAD) \
2312 # define _HOPE_GET(C,BAD) \
2314 union mem_ptr __xl, __xu;\
2315 struct mem_chunk *__xc;\
2317 __xl.p_p = (C).p_p;\
2319 (C).p_cp = __xl.p_cp;\
2320 __xc = __xl.p_c - 1;\
2323 if (__xl.p_ui8p[0] != 0xDE) __i |= 1<<0;\
2324 if (__xl.p_ui8p[1] != 0xAA) __i |= 1<<1;\
2325 if (__xl.p_ui8p[2] != 0x55) __i |= 1<<2;\
2326 if (__xl.p_ui8p[3] != 0xAD) __i |= 1<<3;\
2327 if (__xl.p_ui8p[4] != 0xBE) __i |= 1<<4;\
2328 if (__xl.p_ui8p[5] != 0x55) __i |= 1<<5;\
2329 if (__xl.p_ui8p[6] != 0xAA) __i |= 1<<6;\
2330 if (__xl.p_ui8p[7] != 0xEF) __i |= 1<<7;\
2333 alert("%p: corrupt lower canary: 0x%02X: %s, line %u",\
2334 __xl.p_p, __i, mdbg_file, mdbg_line);\
2337 __xu.p_ui8p += __xc->mc_size - 8;\
2339 if (__xu.p_ui8p[0] != 0xDE) __i |= 1<<0;\
2340 if (__xu.p_ui8p[1] != 0xAA) __i |= 1<<1;\
2341 if (__xu.p_ui8p[2] != 0x55) __i |= 1<<2;\
2342 if (__xu.p_ui8p[3] != 0xAD) __i |= 1<<3;\
2343 if (__xu.p_ui8p[4] != 0xBE) __i |= 1<<4;\
2344 if (__xu.p_ui8p[5] != 0x55) __i |= 1<<5;\
2345 if (__xu.p_ui8p[6] != 0xAA) __i |= 1<<6;\
2346 if (__xu.p_ui8p[7] != 0xEF) __i |= 1<<7;\
2349 alert("%p: corrupt upper canary: 0x%02X: %s, line %u",\
2350 __xl.p_p, __i, mdbg_file, mdbg_line);\
2353 alert(" ..canary last seen: %s, line %u",\
2354 __xc->mc_file, __xc->mc_line);\
2358 (smalloc
)(size_t s SMALLOC_DEBUG_ARGS
)
2365 s
+= sizeof(struct mem_chunk
) + _HOPE_SIZE
;
2367 if ((p
.p_p
= (malloc
)(s
)) == NULL
)
2369 p
.p_c
->mc_prev
= NULL
;
2370 if ((p
.p_c
->mc_next
= _mem_list
) != NULL
)
2371 _mem_list
->mc_prev
= p
.p_c
;
2372 p
.p_c
->mc_file
= mdbg_file
;
2373 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
2374 p
.p_c
->mc_isfree
= FAL0
;
2375 p
.p_c
->mc_size
= (ui32_t
)s
;
2376 _mem_list
= p
.p_c
++;
2381 _mem_amax
= MAX(_mem_amax
, _mem_acur
);
2384 _mem_mmax
= MAX(_mem_mmax
, _mem_mcur
);
2390 (srealloc
)(void *v
, size_t s SMALLOC_DEBUG_ARGS
)
2396 if ((p
.p_p
= v
) == NULL
) {
2397 p
.p_p
= (smalloc
)(s
, mdbg_file
, mdbg_line
);
2401 _HOPE_GET(p
, isbad
);
2403 if (p
.p_c
->mc_isfree
) {
2404 fprintf(stderr
, "srealloc(): region freed! At %s, line %d\n"
2405 "\tLast seen: %s, line %d\n",
2406 mdbg_file
, mdbg_line
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
2410 if (p
.p_c
== _mem_list
)
2411 _mem_list
= p
.p_c
->mc_next
;
2413 p
.p_c
->mc_prev
->mc_next
= p
.p_c
->mc_next
;
2414 if (p
.p_c
->mc_next
!= NULL
)
2415 p
.p_c
->mc_next
->mc_prev
= p
.p_c
->mc_prev
;
2418 _mem_mcur
-= p
.p_c
->mc_size
;
2422 s
+= sizeof(struct mem_chunk
) + _HOPE_SIZE
;
2424 if ((p
.p_p
= (realloc
)(p
.p_c
, s
)) == NULL
)
2426 p
.p_c
->mc_prev
= NULL
;
2427 if ((p
.p_c
->mc_next
= _mem_list
) != NULL
)
2428 _mem_list
->mc_prev
= p
.p_c
;
2429 p
.p_c
->mc_file
= mdbg_file
;
2430 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
2431 p
.p_c
->mc_isfree
= FAL0
;
2432 p
.p_c
->mc_size
= (ui32_t
)s
;
2433 _mem_list
= p
.p_c
++;
2438 _mem_amax
= MAX(_mem_amax
, _mem_acur
);
2441 _mem_mmax
= MAX(_mem_mmax
, _mem_mcur
);
2448 (scalloc
)(size_t nmemb
, size_t size SMALLOC_DEBUG_ARGS
)
2458 size
+= sizeof(struct mem_chunk
) + _HOPE_SIZE
;
2460 if ((p
.p_p
= (malloc
)(size
)) == NULL
)
2462 memset(p
.p_p
, 0, size
);
2463 p
.p_c
->mc_prev
= NULL
;
2464 if ((p
.p_c
->mc_next
= _mem_list
) != NULL
)
2465 _mem_list
->mc_prev
= p
.p_c
;
2466 p
.p_c
->mc_file
= mdbg_file
;
2467 p
.p_c
->mc_line
= (ui16_t
)mdbg_line
;
2468 p
.p_c
->mc_isfree
= FAL0
;
2469 p
.p_c
->mc_size
= (ui32_t
)size
;
2470 _mem_list
= p
.p_c
++;
2475 _mem_amax
= MAX(_mem_amax
, _mem_acur
);
2478 _mem_mmax
= MAX(_mem_mmax
, _mem_mcur
);
2484 (sfree
)(void *v SMALLOC_DEBUG_ARGS
)
2490 if ((p
.p_p
= v
) == NULL
) {
2491 fprintf(stderr
, "sfree(NULL) from %s, line %d\n", mdbg_file
, mdbg_line
);
2495 _HOPE_GET(p
, isbad
);
2497 if (p
.p_c
->mc_isfree
) {
2498 fprintf(stderr
, "sfree(): double-free avoided at %s, line %d\n"
2499 "\tLast seen: %s, line %d\n",
2500 mdbg_file
, mdbg_line
, p
.p_c
->mc_file
, p
.p_c
->mc_line
);
2504 if (p
.p_c
== _mem_list
)
2505 _mem_list
= p
.p_c
->mc_next
;
2507 p
.p_c
->mc_prev
->mc_next
= p
.p_c
->mc_next
;
2508 if (p
.p_c
->mc_next
!= NULL
)
2509 p
.p_c
->mc_next
->mc_prev
= p
.p_c
->mc_prev
;
2510 p
.p_c
->mc_isfree
= TRU1
;
2513 _mem_mcur
-= p
.p_c
->mc_size
;
2515 if (options
& OPT_DEBUG
) {
2516 p
.p_c
->mc_next
= _mem_free
;
2528 size_t c
= 0, s
= 0;
2531 for (p
.p_c
= _mem_free
; p
.p_c
!= NULL
;) {
2534 s
+= p
.p_c
->mc_size
;
2535 p
.p_c
= p
.p_c
->mc_next
;
2540 if (options
& OPT_DEBUG
)
2541 fprintf(stderr
, "smemreset(): freed %" ZFMT
" chunks/%" ZFMT
" bytes\n",
2547 c_smemtrace(void *v
)
2549 /* For _HOPE_GET() */
2550 char const * const mdbg_file
= "smemtrace()";
2551 int const mdbg_line
= -1;
2553 union mem_ptr p
, xp
;
2559 if ((fp
= Ftmp(NULL
, "memtr", OF_RDWR
| OF_UNLINK
| OF_REGISTER
, 0600)) ==
2565 fprintf(fp
, "Memory statistics:\n"
2566 " Count cur/peek/all: %7" ZFMT
"/%7" ZFMT
"/%10" ZFMT
"\n"
2567 " Bytes cur/peek/all: %7" ZFMT
"/%7" ZFMT
"/%10" ZFMT
"\n\n",
2568 _mem_acur
, _mem_amax
, _mem_aall
, _mem_mcur
, _mem_mmax
, _mem_mall
);
2570 fprintf(fp
, "Currently allocated memory chunks:\n");
2571 for (lines
= 0, p
.p_c
= _mem_list
; p
.p_c
!= NULL
;
2572 ++lines
, p
.p_c
= p
.p_c
->mc_next
) {
2575 _HOPE_GET_TRACE(xp
, isbad
);
2576 fprintf(fp
, "%s%p (%5" ZFMT
" bytes): %s, line %u\n",
2577 (isbad
? "! CANARY ERROR: " : ""), xp
.p_p
,
2578 (size_t)(p
.p_c
->mc_size
- sizeof(struct mem_chunk
)), p
.p_c
->mc_file
,
2582 if (options
& OPT_DEBUG
) {
2583 fprintf(fp
, "sfree()d memory chunks awaiting free():\n");
2584 for (p
.p_c
= _mem_free
; p
.p_c
!= NULL
; ++lines
, p
.p_c
= p
.p_c
->mc_next
) {
2587 _HOPE_GET_TRACE(xp
, isbad
);
2588 fprintf(fp
, "%s%p (%5" ZFMT
" bytes): %s, line %u\n",
2589 (isbad
? "! CANARY ERROR: " : ""), xp
.p_p
,
2590 (size_t)(p
.p_c
->mc_size
- sizeof(struct mem_chunk
)),
2591 p
.p_c
->mc_file
, p
.p_c
->mc_line
);
2595 page_or_print(fp
, lines
);
2603 # ifdef _HAVE_MEMCHECK
2605 _smemcheck(char const *mdbg_file
, int mdbg_line
)
2607 union mem_ptr p
, xp
;
2608 bool_t anybad
= FAL0
, isbad
;
2612 for (lines
= 0, p
.p_c
= _mem_list
; p
.p_c
!= NULL
;
2613 ++lines
, p
.p_c
= p
.p_c
->mc_next
) {
2616 _HOPE_GET_TRACE(xp
, isbad
);
2620 "! CANARY ERROR: %p (%5" ZFMT
" bytes): %s, line %u\n",
2621 xp
.p_p
, (size_t)(p
.p_c
->mc_size
- sizeof(struct mem_chunk
)),
2622 p
.p_c
->mc_file
, p
.p_c
->mc_line
);
2626 if (options
& OPT_DEBUG
) {
2627 for (p
.p_c
= _mem_free
; p
.p_c
!= NULL
; ++lines
, p
.p_c
= p
.p_c
->mc_next
) {
2630 _HOPE_GET_TRACE(xp
, isbad
);
2634 "! CANARY ERROR: %p (%5" ZFMT
" bytes): %s, line %u\n",
2635 xp
.p_p
, (size_t)(p
.p_c
->mc_size
- sizeof(struct mem_chunk
)),
2636 p
.p_c
->mc_file
, p
.p_c
->mc_line
);
2643 # endif /* _HAVE_MEMCHECK */
2647 # undef _HOPE_GET_TRACE
2649 #endif /* HAVE_DEBUG */
2651 /* vim:set fenc=utf-8:s-it-mode */