1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242
|
/*
* Copyright (C) 1996-8 Michael R. Elkins <me@cs.hmc.edu>
* Copyright (C) 1999 Thomas Roessler <roessler@guug.de>
* Copyright (C) 1999-2017 Andrej N. Gritsenko <andrej@rep.kiev.ua>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* This file is part of FoxEye's source: common functions library.
*/
#include "foxeye.h"
#include "init.h"
#include <ctype.h>
#include <sys/utsname.h>
#include <wchar.h>
#include <locale.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
/* simple functions have to be either here or in protos.h
if compiler supports inline directive */
#ifndef HAVE_INLINE
# include "inlines.h"
#endif
static char *mem_msg = N_("Out of memory!");
void *safe_calloc (size_t nmemb, size_t size)
{
void *p;
if (!nmemb || !size)
return NULL;
if (!(p = calloc (nmemb, size)))
bot_shutdown (mem_msg, 2);
DBG ("safe_calloc(%zu*%zu)=0x%08lx", nmemb, size, (long int)p);
return p;
}
void *safe_malloc (size_t siz)
{
void *p;
if (siz == 0)
return NULL;
if ((p = (void *) malloc (siz)) == 0)
bot_shutdown (mem_msg, 2);
DBG ("safe_malloc(%zu)=0x%08lx", siz, (long int)p);
return p;
}
void safe_realloc (void **p, size_t siz)
{
void *r;
if (siz == 0)
{
DBG ("safe_realloc(0x%08lx,0)", (long int)*p);
if (*p)
{
free (*p);
*p = NULL;
}
return;
}
if (*p)
r = (void *) realloc (*p, siz);
else
{
/* realloc(NULL, nbytes) doesn't seem to work under SunOS 4.1.x */
r = (void *) malloc (siz);
}
if (!r)
bot_shutdown (mem_msg, 2);
DBG ("safe_realloc(0x%08lx,%zu)=0x%08lx", (long int)*p, siz, (long int)r);
*p = r;
}
void safe_free (void **p)
{
if (*p)
{
DBG ("safe_free(0x%08lx)", (long int)*p);
free (*p);
*p = NULL;
}
}
/*
* converts null-terminated string src to upper case string
* output buffer dst with size ds must be enough for null-terminated string
* else string will be truncated
* returns length of output null-terminated string (without null char)
* if dst == NULL or ds == 0 then returns 0
*/
size_t unistrlower (char *dst, const char *src, size_t ds)
{
size_t sout = 0, ss;
if (dst == NULL || ds == 0)
return 0;
ds--; /* preserve 1 byte for terminating null char */
if (src && *src)
{
if (MB_CUR_MAX > 1) /* if multibyte encoding */
{
wchar_t wc;
register ssize_t len;
const char *ch;
mbstate_t ms;
char replace_char = *text_replace_char;
char c[MB_LEN_MAX];
memset(&ms, 0, sizeof(ms)); /* reset the state */
for (ch = src, ss = strlen(ch); *ch && ds > 0; )
{
len = mbrtowc(&wc, ch, ss, &ms);
if (len < 1) /* unrecognized char! */
{
if (replace_char)
{
*dst++ = replace_char; /* OK, we replace it then */
sout++;
ds--;
}
if (len == -2) /* premature end of string */
break;
ss--;
ch++; /* and skip bad char */
memset(&ms, 0, sizeof(ms)); /* reset the state */
continue;
}
ss -= len; /* advance pointer in sourse string */
ch += len;
wc = towlower(wc);
len = wcrtomb(c, wc, &ms); /* first get the size of lowercase mbchar */
if (len < 1)
continue; /* tolower() returned unknown char? ignore it */
if (len > (ssize_t)ds)
break; /* oops, out of output size! */
memcpy(dst, c, len); /* really convert it */
ds -= len; /* advance pointers in destination string */
dst += len;
sout += len;
}
}
else
{ /* string in internal single-byte encoding the same as locale */
register char ch;
for (ch = *src++; ch && ds; ch = *src++, sout++, ds--)
*dst++ = tolower((unsigned char)ch);
}
}
*dst = 0;
return (sout);
}
static bool _charset_is_utf = FALSE;
void foxeye_setlocale (void)
{
int changed = 1;
char new_locale[SHORT_STRING];
register char *c;
snprintf (new_locale, sizeof(new_locale), "%s.%s", locale, Charset);
for (c = &new_locale[strlen(locale)];
c < &new_locale[sizeof(new_locale)] && *c != 0; c++)
if ((*c & 0xe0) == 0x60) /* uppercase the charset */
*c &= 0xdf;
DBG ("current locale is %s", setlocale (LC_ALL, NULL));
DBG ("trying set locale to %s", new_locale);
if (setlocale (LC_ALL, new_locale) == NULL)
{
_charset_is_utf = FALSE;
snprintf (new_locale, sizeof(new_locale), "%s.%s", locale, CHARSET_8BIT);
if (setlocale (LC_ALL, new_locale) == NULL)
{
char *deflocale;
deflocale = setlocale (LC_ALL, "");
ERROR ("init: failed to set locale to %s, reverted to default %s!",
new_locale, deflocale);
c = safe_strchr (deflocale, '.');
if (c)
strfcpy (Charset, ++c, sizeof(Charset)); /* reset charset */
changed = 0;
}
else
{
ERROR ("init: failed to set locale to %s.%s, reverted to %s!", locale,
Charset, new_locale);
strfcpy (Charset, CHARSET_8BIT, sizeof(Charset)); /* reset charset */
}
}
else if (!strncasecmp (Charset, "utf", 3))
_charset_is_utf = TRUE;
else
_charset_is_utf = FALSE;
/* enforce LC_CTYPE for mbrtowc() call */
setlocale (LC_CTYPE, new_locale);
if (changed)
setenv("LC_ALL", new_locale, 1); /* reset environment */
#ifdef ENABLE_NLS
setenv("LANGUAGE", locale, 1);
bindtextdomain (PACKAGE, LOCALEDIR);
textdomain (PACKAGE);
#endif
}
/*
* checks null-terminated string in line to contain not more than:
* - len bytes (including termination byte)
* - maxchars characters (without null char)
* returns size of string to be truncated in bytes without null char
*/
size_t unistrcut (const char *line, size_t len, int maxchars)
{
len--; /* preserve 1 byte for '\0' */
if (_charset_is_utf == TRUE) /* let's count chars - works for utf* only */
{
register unsigned char *ch = (unsigned char *)line;
unsigned char *chmax = (unsigned char *)&line[len];
register size_t cursize;
register int chsize = maxchars;
while (chsize > 0 && ch < chmax && *ch) /* go for max chars */
{
cursize = 1;
if ((*ch & 0xc0) == 0xc0) /* first multibyte octet */
while ((ch[cursize] & 0xc0) == 0x80)
cursize++; /* skip rest of octets */
if (ch + cursize > chmax)
break;
chsize--; /* char counted */
ch += cursize;
}
len = (char *)ch - line;
}
else if (MB_CUR_MAX > 1) /* another multibyte charset is in use */
{
register ssize_t cursize;
int chsize = maxchars;
const char *ch = line;
size_t todo = len;
mbstate_t ms;
memset(&ms, 0, sizeof(ms)); /* reset the state! */
while (chsize > 0 && todo > 0 && *ch)
{
cursize = mbrlen(ch, todo, &ms);
if (cursize <= 0) /* break at invalid char */
break;
chsize--;
ch += cursize;
todo -= cursize;
}
len = ch - line;
}
else /* 8bit encoding - just let's cut it */
if ((int)len > maxchars) /* may be it's already ok */
len = maxchars;
return len;
}
static int pattern_size (const char *pattern, const char *pe)
{
register const char *c;
register int s;
c = &pattern[1];
switch (*pattern)
{
case '[':
while (*c) /* skip rest up to ']' */
{
if (*c == '-') c++; /* it's range so skip both chars */
else if (*c == ']') /* and -] is still allowed */
break;
c++;
}
if (!*c++) return (-1); /* "[..." is error */
break;
case '{':
while (*c && *c != '}') /* recursively skip up to '}' */
if ((s = pattern_size (c, pe)) < 0)
return s;
else
c += s;
if (!*c++) return (-1); /* "{..." is error */
break;
case '\\':
if (!*c++) return (-1); /* "\" is error */
default:; /* all other chars are one-by-one */
}
if (c > pe) return (-1); /* out of bound! */
return (c - pattern);
}
static char Wildcards[] = "[?*{";
#define wildcard(c) strchr (Wildcards, c)
static int match_it_mb (const char *p, const char *pe, const char *t, const char *te, char pp)
{
const char *c;
int count = 0, rc;
register int r;
wchar_t wc, cch;
mbstate_t ms;
memset(&ms, 0, sizeof(ms)); /* reset state */
while (p < pe) {
switch (*p) {
case '[':
if (t >= te)
return (-1);
r = pattern_size(p, pe) - 2;
if (r < 0)
return (-1); /* invalid pattern */
c = &p[1]; /* ptr for testing */
p = &c[r]; /* new pattern end */
r = mbrtowc(&cch, t, te - t, &ms);
if (r < 1)
return (-1); /* invalid text */
t += r;
rc = 0;
if (*c == '^') {
rc = -1; /* mark reverse matching */
c++;
}
if (*c == '-') {
if (cch == '-') /* char matched */
break;
c++;
if (*c == ']') { /* -] is literal at start */
if (cch == ']')
break;
c++;
}
}
while (*c != ']') {
r = mbrtowc(&wc, c, p - c, &ms);
if (r < 0)
return (-1); /* invalid pattern */
if (cch == wc)
break;
c += r;
if (*c == '-') {
c++;
if (cch < wc) /* below range */
continue;
r = mbrtowc(&wc, c, p - c, &ms);
if (r < 1)
return (-1); /* invalid pattern */
if (cch <= wc) /* in range */
break;
c += r;
}
}
r = rc;
if (*c == ']') /* no matched chars found */
r = -1 - rc; /* invert matching flag */
count++; /* count it if matched */
break;
case '?':
if (t >= te)
return (-1);
r = mbrtowc(&cch, t, te - t, &ms);
if (r == 0)
r = -1; /* no text here */
t += r; /* should be consumed */
if (pp == '*' && p[1] == '*') /* short '*?*' to '*?' */
p++; /* p points to '*' again */
break;
case '*':
if (pp == '*') { /* skip duplicate '*' */
r = 0;
break;
}
rc = -1;
for (c = t; (char *)c <= te; ) {
r = match_it_mb(&p[1], pe, c, te, '*');
if (r > rc)
rc = r;
if ((char *)c < te) {
r = mbrtowc(&cch, c, te - c, &ms);
if (r < 1)
return (-1); /* invalid text */
c += r;
} else
break;
}
if (rc < 0)
return (rc);
return (rc + count);
case '{':
r = pattern_size(p, pe);
if (r < 0)
break; /* invalid pattern */
c = &p[r];
rc = -1;
while (*p != '}') { /* check each subpattern */
const char *ps, *tc;
int rt;
p++; /* skip '{' or ',' */
ps = p;
while (*p != ',' && *p != '}')
p++; /* now it's on ',' or '}' */
for (tc = t; tc <= te; ) {
r = match_it_mb(ps, p, t, tc, '{');
if (r >= 0) { /* initial part matches */
rt = r;
r = match_it_mb(c, pe, tc, te, '}');
if (r >= 0) { /* rest matches */
r += rt;
if (r > rc) /* best matched */
rc = r;
}
}
if (tc >= te) /* cycle ended */
break;
r = mbrtowc(&wc, tc, te - tc, &ms);
if (r < 0)
return (r); /* invalid text */
tc += r;
}
}
if (rc < 0)
return (rc);
return (rc + count);
case '\\':
p++; /* skip one escaped char */
default:
r = mbrtowc(&cch, t, te - t, &ms);
if (r < 1)
return (-1); /* invalid text */
t += r; /* should be consumed */
r = mbrtowc(&wc, p, pe - p, &ms);
if (cch != wc)
r = -1;
else
p += (r - 1); /* p will be incremented below */
count++;
}
if (r < 0 || t > te)
return (-1); /* invalid pattern above */
pp = *p++;
}
if (t < te) /* not consumed by pattern */
return (-1);
return (count);
}
static int match_it (const char *p, const char *pe, const char *t, const char *te, char pp)
{
const unsigned char *c;
int count = 0, rc;
register int r;
register unsigned char cch;
while (p < pe) {
switch (*p) {
case '[':
if (t >= te)
return (-1);
r = pattern_size(p, pe) - 2;
if (r < 0)
return (-1); /* invalid pattern */
c = &p[1]; /* ptr for testing */
p = &c[r]; /* new pattern end */
cch = *t++;
r = 0;
if (*c == '^') {
r = -1; /* mark reverse matching */
c++;
}
if (*c == '-') {
if (cch == '-') /* char matched */
break;
c++;
if (*c == ']') { /* -] is literal at start */
if (cch == ']')
break;
c++;
}
}
while (*c != ']') {
if (cch == *c)
break;
if (c[1] == '-') {
if (cch > *c && cch <= c[2])
break;
c += 2;
}
c++;
}
if (*c == ']') /* no matched chars found */
r = -1 - r; /* invert matching flag */
count++; /* count it if matched */
break;
case '?':
r = 0;
t++; /* should be consumed */
if (pp == '*' && p[1] == '*') /* short '*?*' to '*?' */
p++; /* p points to '*' again */
break;
case '*':
if (pp == '*') { /* skip duplicate '*' */
r = 0;
break;
}
rc = -1;
for (c = t; (char *)c <= te; c++) {
r = match_it(&p[1], pe, c, te, '*');
if (r > rc)
rc = r;
}
if (rc < 0)
return (rc);
return (rc + count);
case '{':
r = pattern_size(p, pe);
if (r < 0)
break; /* invalid pattern */
c = &p[r];
rc = -1;
while (*p != '}') { /* check each subpattern */
const char *ps, *tc;
int rt;
p++; /* skip '{' or ',' */
ps = p;
while (*p != ',' && *p != '}')
p++; /* now it's on ',' or '}' */
for (tc = t; tc <= te; tc++) {
r = match_it(ps, p, t, tc, '{');
if (r < 0) /* initial part doesn't match */
continue;
rt = r;
r = match_it(c, pe, tc, te, '}');
if (r < 0) /* rest doesn't match */
continue;
r += rt;
if (r > rc) /* best matched */
rc = r;
}
}
if (rc < 0)
return (rc);
return (rc + count);
case '\\':
p++; /* skip one escaped char */
default:
r = 0;
if (*p != *t++)
r = -1;
count++;
}
if (r < 0 || t > te)
return (-1); /* invalid pattern above */
pp = *p++;
}
if (t < te) /* not consumed by pattern */
return (-1);
return (count);
}
#define PATTERN_MAX_LEN HOSTMASKLEN
/* check for matching in shell wildcards style:
* returns -1 if no matched, or number of not-wildcards characters matched */
int match (const char *mask, const char *text)
{
register int cur, ptr = 0;
if ((!text || !*text) && (!mask || !*mask)) /* empty string are equal */
return 0;
if (mask)
{
while (mask[ptr]) /* check whole pattern */
if ((cur = pattern_size (&mask[ptr], &mask[PATTERN_MAX_LEN])) < 0)
return cur; /* OOPS! invalid pattern! */
else
ptr += cur;
}
if ((mask && *mask == '*' && !mask[1]) || /* "*" is equal to anything */
(text && *text == '*' && !text[1]))
return 0;
if (!text || !mask) /* NULL not matched to smth */
return -1;
// return match_it (mask, &text, &mask[ptr]); /* do real comparison */
if (MB_CUR_MAX > 1)
return match_it_mb(mask, &mask[ptr], text, &text[strlen(text)], '0');
return match_it(mask, &mask[ptr], text, &text[strlen(text)], '0');
}
#define swildcard(c) (c == '*' || c == '?' || c == '\\')
static int smatch_it_mb (const char *p, const char *pe, const char *t,
const char *te, char pp)
{
const char *tc;
int count = 0, rc;
register int r;
wchar_t wc, pc;
mbstate_t ms;
memset(&ms, 0, sizeof(ms)); /* reset the state */
while (*p) {
switch (*p) {
case '?':
if (*t == '\0')
return (-1);
r = mbrtowc(&wc, t, te - t, &ms);
if (r < 1)
return (-1);
t += r;
if (pp == '*' && p[1] == '*') /* short '*?*' to '*?' */
p++; /* p points to '*' again */
break;
case '*':
if (pp == '*') /* skip duplicate '*' */
break;
rc = -1;
p++;
for (tc = t; tc <= te; ) {
r = smatch_it_mb(p, pe, tc, te, '*');
if (r > rc)
rc = r;
if (tc == te)
break;
r = mbrtowc(&wc, tc, te - tc, &ms);
if (r < 1)
return (-1);
tc += r;
}
if (rc < 0) /* rest doesn't match */
return (rc);
return (rc + count);
case '\\':
if (swildcard(p[1]))
p++; /* skip escape char if escaped is wildcard */
/* else take it literally */
default:
r = mbrtowc(&wc, t, te - t, &ms);
if (r < 1)
return (-1);
t += r;
if (mbrtowc(&pc, p, pe - p, &ms) < 1 || pc != wc)
return (-1);
p += (r - 1); /* p is incremented below */
count++;
}
pp = *p++;
}
if (*t != '\0')
return (-1);
return (count);
}
static int smatch_it (const char *p, const char *t, char pp)
{
const char *tc;
int count = 0, rc;
register int r;
while (*p) {
switch (*p) {
case '?':
if (*t++ == '\0')
return (-1);
if (pp == '*' && p[1] == '*') /* short '*?*' to '*?' */
p++; /* p points to '*' again */
break;
case '*':
if (pp == '*') /* skip duplicate '*' */
break;
rc = -1;
tc = t;
do {
r = smatch_it(&p[1], tc, '*');
if (r > rc)
rc = r;
} while (*tc++ != '\0');
if (rc < 0) /* rest doesn't match */
return (rc);
return (rc + count);
case '\\':
if (swildcard(p[1]))
p++; /* skip one escaped wildcard */
/* else take it literally */
default:
if (*p != *t++)
return (-1);
count++;
}
pp = *p++;
}
if (*t != '\0')
return (-1);
return (count);
}
int simple_match (const char *mask, const char *text)
{
// register int ptr;
if ((!text || !*text) && (!mask || !*mask)) /* empty string are equal */
return 0;
if ((mask && *mask == '*' && !mask[1]) || /* "*" is equal to anything */
(text && *text == '*' && !text[1]))
return 0;
if (!text || !mask) /* NULL not matched to smth */
return -1;
// ptr = strlen (mask);
// return smatch_it (mask, &text, &mask[ptr]); /* do real comparison */
if (MB_CUR_MAX > 1)
return smatch_it_mb(mask, &mask[strlen(mask)], text, &text[strlen(text)], '\0');
return smatch_it (mask, text, '\0'); /* do real comparison */
}
int Have_Wildcard (const char *str)
{
register int i;
for (i = 0; str[i]; i++)
if (wildcard (str[i]))
return i;
return -1;
}
/*
* count real chars in line and correct its size in case of invalid chars
*/
static int _count_chars (const char *line, size_t *len, int max)
{
if (MB_CUR_MAX > 1) /* multibyte charset is in use */
{
register ssize_t cursize;
int chars = 0;
const char *ch = line;
ssize_t todo = *len;
mbstate_t ms;
memset(&ms, 0, sizeof(ms)); /* reset the state! */
while (todo > 0 && *ch && chars < max)
{
cursize = mbrlen(ch, todo, &ms);
if (cursize <= 0) /* break at invalid char */
break;
if (cursize > todo) /* buf overflow */
break;
chars++;
ch += cursize;
todo -= cursize;
}
if (todo)
*len -= todo;
return chars;
}
else if ((int)*len <= max) /* 8bit encoding - just return */
return *len;
*len = max;
return max;
}
/* bs is max space available if text doesn't fit in linelen s
* bs >= s if no wrapping
* bs == 0 if wrapping enabled
* returns: -1 if there is no text to print, 0 if text should be wrapped,
* or byte size of the text to print */
static ssize_t _try_subst (char *buf, size_t bs, const char *text, size_t s, int max)
{
size_t n = safe_strlen (text);
if (!n || s == 0) /* don't print, don't wrap */
return -1;
if (bs == 0)
{
if (n > s) /* wrap to next line */
return 0;
bs = n;
_count_chars (text, &bs, max);
if (bs && bs < n) /* no so much space available */
return 0;
}
else /* correct size if needed */
_count_chars (text, &bs, n);
if (!bs) /* text appears to be just garbage */
return -1;
memcpy (buf, text, bs);
return bs;
}
typedef struct {
const char *t; /* pointer to template */
size_t i; /* real chars in line */
char *nick;
const char *host;
const char *lname;
char *chan;
uint32_t ip;
unsigned short port;
int idle;
const char *message;
char idlestr[8];
int bold;
int flash;
int color;
int ul;
int inv;
} printl_t;
static void makeidlestr (printl_t *p)
{
if (p->idle <= 0 || p->idlestr[0] != 0)
return;
/* %Mm:%Ss */
if (p->idle < 3600)
snprintf (p->idlestr, sizeof(p->idlestr), "%2dm:%02ds", p->idle/60,
p->idle%60);
/* %kh:%Mm */
else if (p->idle < 86400)
snprintf (p->idlestr, sizeof(p->idlestr), "%2dh:%02dm", p->idle/3600,
(p->idle%3600)/60);
/* %ed:%Hh */
else
snprintf (p->idlestr, sizeof(p->idlestr), "%2dd:%02dh", p->idle/86400,
(p->idle%86400)/3600);
}
#define LEFT_CHARS(_a) (ll ? (ssize_t)(ll - p->i) : _a)
/* returns new buffer pointer */
/* ll - line length, q - need check for '?' */
static char *_try_printl (char *buf, size_t s, printl_t *p, size_t ll, int q)
{
char tbuf[SHORT_STRING];
struct utsname unbuf;
register char *c;
const char *t = p->t;
const char *cc, *end;
unbuf.sysname[0] = 0;
c = buf; /* set to end of buffer */
while (*t && (!q || *t != '?'))
{ /* all colors are mIRC colors */
if (p->i == 0 && &buf[s+7] > c)
{
if (p->bold) /* recreate modes at new line */
*c++ = '\002'; /* i is number of real chars in buff */
if (p->flash) /* c is ptr in buff */
*c++ = '\006';
if (p->ul)
*c++ = '\037';
if (p->inv)
*c++ = '\026';
if (p->color)
{
snprintf (c, s, "\003%d", p->color - 1);
c = &c[strlen(c)];
}
}
while (ll == 0 || p->i < ll)
{
ssize_t n; /* n is number of real chars to add */
size_t nmax;
const char *fix;
ssize_t nn, fw;
static char mircsubst[] = "WkbgRrmyYGcCBMKw";
if (!q || (end = strchr (t, '?')) == NULL)
end = &t[strlen(t)];
cc = strchr (t, '\n');
if (cc && cc < end)
end = cc; /* now end is template for parse */
fw = end - t;
nn = &buf[s-1] - c; /* rest octets in buff */
if (nn < 0)
nn = 0;
if ((!q || nn) && fw > nn) /* how many octets we can put here? */
fw = nn;
_count_chars(t, &fw, LEFT_CHARS(fw)); /* fw now contains size that fits */
cc = memchr (t, '%', fw);
if (!cc) /* next subst is over */
{ /* try by words */
const char *cs;
if (end > &t[fw]) /* if template doesn't fit */
for (cc = cs = t; *cs && cc <= &t[fw]; cc = NextWord ((char *)cs))
cs = cc;
else
cs = end;
/* if (q && *end == '?' && cs > end)
cs = end;*/
/* cs now is first char of unfitting word so skip end spaces */
cc = cs;
if (!q || *cc != '?')
while (cc > t && (*(cc-1) == ' ' || *(cc-1) == '\t')) cc--;
/* cc now is after last fitting word */
fw = cc - t;
if (fw > nn) /* see (!q || nn) above */
fw = nn;
if (fw) memcpy (c, t, fw);
c += fw;
t = cs; /* first word for next line (or NL?) */
break;
}
fw = cc - t; /* octets to subst */
n = _count_chars(t, &fw, LEFT_CHARS(fw));
if (fw) memcpy (c, t, fw);
nn -= fw; /* octets left in buff */
t = cc; /* we are on '%' now */
p->i += n; /* and we have line space for it */
c += fw;
if (ll && p->i != 0) /* nmax is max for line, 0 to wrap */
nmax = 0; /* wrap: drop line to ll */
else
nmax = nn; /* rest of buffer */
fix = &t[1];
n = LEFT_CHARS(nn);
fw = 0;
if (*fix >= '0' && *fix <= '9') /* we have fixed field width here */
{
fw = (int)strtol (fix, (char **)&fix, 10);
if (fw < n)
{
nmax = nn; /* disable wrapping: assume field has width n */
n = fw;
}
}
tbuf[0] = 0;
if ((cc = strchr (mircsubst, *fix)))
{
p->color = ++cc - mircsubst; /* mIRC color incremented by 1 */
snprintf (tbuf, sizeof(tbuf), "\003%d", p->color - 1);
}
else switch (*fix) /* nmax must be 0 (wrap) or nmax == nn */
{
case 'N': /* the user nick */
n = _try_subst (c, nmax, p->nick, nn, n);
break;
case '=': /* my nick */
n = _try_subst (c, nmax, Nick, nn, n);
break;
case '@': /* the host name */
n = _try_subst (c, nmax, p->host, nn, n);
break;
case 'L': /* the user lname */
n = _try_subst (c, nmax, p->lname, nn, n);
break;
case '#': /* channel(s) */
n = _try_subst (c, nmax, p->chan, nn, n);
break;
case '-': /* idle string */
makeidlestr (p);
n = _try_subst (c, nmax, p->idlestr, nn, n);
break;
case 's':
if (unbuf.sysname[0] == 0)
uname (&unbuf);
n = _try_subst (c, nmax, unbuf.sysname, nn, n);
break;
case 'I': /* IPv4 in dot notation */
inet_ntop (AF_INET, &p->ip, tbuf, sizeof(tbuf));
n = -1;
break;
case 'P': /* port number, zero is "" */
snprintf (tbuf, sizeof(tbuf), "%.0hu", p->port);
n = -1; /* in case of zero mark as empty */
break;
case 't': /* current time */
n = _try_subst (c, nmax, TimeString, nn, n);
break;
case 'n': /* color stop */
p->color = 0;
if (nn) *c++ = '\003';
t = &fix[1];
n = 0;
break;
case '^':
p->bold ^= 1;
if (nn) *c++ = '\002';
t = &fix[1];
n = 0;
break;
case '_':
p->ul ^= 1;
if (nn) *c++ = '\037';
t = &fix[1];
n = 0;
break;
case 'v':
p->inv ^= 1;
if (nn) *c++ = '\026';
t = &fix[1];
n = 0;
break;
case 'f':
p->flash ^= 1;
if (nn) *c++ = '\006';
t = &fix[1];
n = 0;
break;
case '?':
p->t = &fix[2]; /* skip "%?x" */
n = 0;
switch (fix[1])
{
case 'N':
if (p->nick && *p->nick)
n = 1;
break;
case '@':
if (p->host && *p->host)
n = 1;
break;
case 'L':
if (p->lname && *p->lname)
n = 1;
break;
case '#':
if (p->chan && *p->chan)
n = 1;
break;
case 'I':
if (p->ip)
n = 1;
break;
case 'P':
if (p->port)
n = 1;
break;
case '-':
if (p->idle)
n = 1;
break;
case '*':
if (p->message && *p->message)
n = 1;
}
end = &buf[s]; /* for the next _try_printl */
if (n)
{
c = _try_printl (c, end - c, p, ll, 1);
_try_printl (c, 0, p, ll, 1);
n = 0;
}
else
{
_try_printl (c, 0, p, ll, 1);
c = _try_printl (c, end - c, p, ll, 1);
}
t = p->t;
break;
case '*':
n = _try_subst (c, nmax, p->message, nn, n);
break;
case 'V':
n = _try_subst (c, nmax, PACKAGE "-" VERSION, nn, n);
break;
case '%': /* just a percent */
if (nn) *c++ = '%';
p->i++;
default:
t = &fix[1]; /* all other are ignored */
n = 0;
}
if (*tbuf)
n = _try_subst (c, nmax, tbuf, nn, n);
if (n)
{
if (n > 0) /* if something was added */
{
register int chars;
chars = _count_chars (c, &n, n);
c += n;
p->i += chars;
n = chars;
}
else /* if no space available or empty */
n = 0;
for (; n < fw; n++) /* check if fixed size */
*c++ = ' '; /* fill rest with spaces */
t = &fix[1]; /* skip command char */
}
}
end = &buf[s];
if ((!q || *t != '?') && c < end) /* if line was wrapped */
{
if (p->bold) /* reset modes at end of line */
*c++ = '\002';
if (p->flash && c < end)
*c++ = '\006';
if (p->ul && c < end)
*c++ = '\037';
if (p->inv && c < end)
*c++ = '\026';
if (p->color && c < end)
*c++ = '\003';
if (*t && c < end)
*c++ = '\n';
}
/* terminate the line */
if (s)
{
if (c == end)
c--;
*c = 0;
}
/* we are at EOL - recalculate buf and s */
if (c > end)
s = 0;
else
s -= (c - buf);
buf = c;
p->i = 0;
if (*t == ' ' || *t == '\t' || *t == '\n')
t++; /* line wrapped so skip wrapping char */
}
if (q && *t == '?') /* skip the '?' */
t++;
p->t = t;
return c;
}
size_t printl (char *buf, size_t s, const char *templ, size_t strlen,
char *nick, const char *uhost, const char *lname, char *chan,
uint32_t ip, unsigned short port, int idle, const char *message)
{
printl_t p;
if (buf == NULL || s == 0) /* nothing to do */
return 0;
if (templ == NULL || *templ == 0) /* just terminate line if empty template */
{
buf[0] = 0;
return 0;
}
p.t = templ;
p.nick = nick;
p.host = uhost;
p.lname = lname;
p.chan = chan;
p.ip = htonl (ip);
p.port = port;
p.idle = idle;
p.message = message;
p.idlestr[0] = 0;
p.i = p.bold = p.flash = p.color = p.ul = p.inv = 0;
return (_try_printl (buf, s, &p, strlen, 0) - buf);
}
/* thanks to glibc and gcc for showing me how to optimize it */
size_t strfcpy (char *d, const char *s, size_t n)
{
register char *s1 = d;
register const char *s2 = s;
register char c;
if (n == 0)
return 0;
if ((--n) >= 4)
{
size_t n4 = n >> 2;
do {
if ((c = *s2) == '\0')
goto to_return;
*s1++ = c, s2++;
if ((c = *s2) == '\0')
goto to_return;
*s1++ = c, s2++;
if ((c = *s2) == '\0')
goto to_return;
*s1++ = c, s2++;
if ((c = *s2) == '\0')
goto to_return;
*s1++ = c, s2++;
} while (--n4 != 0);
}
n &= 3;
if (n > 0)
{
if ((c = *s2) == '\0')
goto to_return;
*s1++ = c, s2++;
if (n > 1)
{
if ((c = *s2) == '\0')
goto to_return;
*s1++ = c, s2++;
if (n > 2)
{
if ((c = *s2) == '\0')
goto to_return;
*s1++ = c, s2++;
}
}
}
to_return:
*s1 = '\0';
return (s1 - d);
}
unsigned short make_hash (const char *s)
{
register unsigned int hash;
if (!s) return 0;
hash = 0;
while (*s)
{
hash += (hash << 5); /* hash*33 */
hash ^= *s++; /* XOR with byte */
hash ^= (hash >> 16); /* XOR high and low parts */
hash &= 0xffff; /* leave only low part */
}
return hash;
}
|