2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2009 Gabor Kovesdan <gabor@FreeBSD.org>
5 * Copyright (C) 2012 Oleg Moskalenko <mom040267@gmail.com>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
18 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
21 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 #include <sys/cdefs.h>
31 __FBSDID("$FreeBSD$");
48 static wchar_t **wmonths;
49 static char **cmonths;
51 /* initialise months */
54 initialise_months(void)
56 const nl_item item[12] = { ABMON_1, ABMON_2, ABMON_3, ABMON_4,
57 ABMON_5, ABMON_6, ABMON_7, ABMON_8, ABMON_9, ABMON_10,
62 if (mb_cur_max == 1) {
63 if (cmonths == NULL) {
66 cmonths = sort_malloc(sizeof(char*) * 12);
67 for (int i = 0; i < 12; i++) {
69 tmp = nl_langinfo(item[i]);
71 printf("month[%d]=%s\n", i, tmp);
76 for (unsigned int j = 0; j < len; j++)
83 if (wmonths == NULL) {
86 wmonths = sort_malloc(sizeof(wchar_t *) * 12);
87 for (int i = 0; i < 12; i++) {
89 tmp = nl_langinfo(item[i]);
91 printf("month[%d]=%s\n", i, tmp);
95 m = sort_malloc(SIZEOF_WCHAR_STRING(len + 1));
96 if (mbstowcs(m, tmp, len) ==
102 for (unsigned int j = 0; j < len; j++)
103 m[j] = towupper(m[j]);
111 * Compare two wide-character strings
114 wide_str_coll(const wchar_t *s1, const wchar_t *s2)
119 ret = wcscoll(s1, s2);
120 if (errno == EILSEQ) {
122 ret = wcscmp(s1, s2);
124 for (size_t i = 0; ; ++i) {
128 return ((c2 == L'\0') ? 0 : -1);
133 return ((int)(c1 - c2));
140 /* counterparts of wcs functions */
143 bwsprintf(FILE *f, struct bwstring *bws, const char *prefix, const char *suffix)
147 fprintf(f, "%s%s%s", prefix, bws->cdata.str, suffix);
149 fprintf(f, "%s%S%s", prefix, bws->wdata.str, suffix);
152 const void* bwsrawdata(const struct bwstring *bws)
155 return (bws->wdata.str);
158 size_t bwsrawlen(const struct bwstring *bws)
161 return ((mb_cur_max == 1) ? bws->cdata.len :
162 SIZEOF_WCHAR_STRING(bws->wdata.len));
166 bws_memsize(const struct bwstring *bws)
169 return ((mb_cur_max == 1) ?
170 (bws->cdata.len + 2 + sizeof(struct bwstring)) :
171 (SIZEOF_WCHAR_STRING(bws->wdata.len + 1) + sizeof(struct bwstring)));
175 bws_setlen(struct bwstring *bws, size_t newlen)
178 if (mb_cur_max == 1 && bws && newlen != bws->cdata.len &&
179 newlen <= bws->cdata.len) {
180 bws->cdata.len = newlen;
181 bws->cdata.str[newlen] = '\0';
182 } else if (bws && newlen != bws->wdata.len && newlen <= bws->wdata.len) {
183 bws->wdata.len = newlen;
184 bws->wdata.str[newlen] = L'\0';
189 * Allocate a new binary string of specified size
194 struct bwstring *ret;
196 if (mb_cur_max == 1) {
197 ret = sort_malloc(sizeof(struct bwstring) + 1 + sz);
199 ret->cdata.str[sz] = '\0';
202 sizeof(struct bwstring) + SIZEOF_WCHAR_STRING(sz + 1));
204 ret->wdata.str[sz] = L'\0';
211 * Create a copy of binary string.
212 * New string size equals the length of the old string.
215 bwsdup(const struct bwstring *s)
221 struct bwstring *ret = bwsalloc(BWSLEN(s));
224 memcpy(ret->cdata.str, s->cdata.str, (s->cdata.len));
226 memcpy(ret->wdata.str, s->wdata.str,
227 SIZEOF_WCHAR_STRING(s->wdata.len));
234 * Create a new binary string from a wide character buffer.
237 bwssbdup(const wchar_t *str, size_t len)
241 return ((len == 0) ? bwsalloc(0) : NULL);
243 struct bwstring *ret;
248 for (size_t i = 0; i < len; ++i)
249 ret->cdata.str[i] = (char)str[i];
251 memcpy(ret->wdata.str, str, SIZEOF_WCHAR_STRING(len));
258 * Create a new binary string from a raw binary buffer.
261 bwscsbdup(const unsigned char *str, size_t len)
263 struct bwstring *ret;
269 memcpy(ret->cdata.str, str, len);
273 size_t charlen, chars, cptr;
277 s = (const char *) str;
279 memset(&mbs, 0, sizeof(mbs));
282 size_t n = mb_cur_max;
286 charlen = mbrlen(s + cptr, n, &mbs);
293 ret->wdata.str[chars++] =
294 (unsigned char) s[cptr];
298 n = mbrtowc(ret->wdata.str + (chars++),
299 s + cptr, charlen, &mbs);
300 if ((n == (size_t)-1) || (n == (size_t)-2))
302 err(2, "mbrtowc error");
307 ret->wdata.len = chars;
308 ret->wdata.str[ret->wdata.len] = L'\0';
315 * De-allocate object memory
318 bwsfree(const struct bwstring *s)
326 * Copy content of src binary string to dst,
327 * with specified number of symbols to be copied.
328 * An offset value can be specified, from the start of src string.
329 * If the capacity of the dst string is not sufficient,
330 * then the data is truncated.
333 bwsnocpy(struct bwstring *dst, const struct bwstring *src, size_t offset,
337 if (offset >= BWSLEN(src)) {
340 size_t nums = BWSLEN(src) - offset;
342 if (nums > BWSLEN(dst))
346 if (mb_cur_max == 1) {
347 memcpy(dst->cdata.str, src->cdata.str + offset, nums);
348 dst->cdata.len = nums;
349 dst->cdata.str[nums] = '\0';
351 memcpy(dst->wdata.str, src->wdata.str + offset,
352 SIZEOF_WCHAR_STRING(nums));
353 dst->wdata.len = nums;
354 dst->wdata.str[nums] = L'\0';
361 * Write binary string to the file.
362 * The output is ended either with '\n' (nl == true)
363 * or '\0' (nl == false).
366 bwsfwrite(struct bwstring *bws, FILE *f, bool zero_ended)
369 if (mb_cur_max == 1) {
370 size_t len = bws->cdata.len;
373 bws->cdata.str[len] = '\n';
375 if (fwrite(bws->cdata.str, len + 1, 1, f) < 1)
378 bws->cdata.str[len] = '\0';
379 } else if (fwrite(bws->cdata.str, len + 1, 1, f) < 1)
388 eols = zero_ended ? btowc('\0') : btowc('\n');
390 while (printed < BWSLEN(bws)) {
391 const wchar_t *s = bws->wdata.str + printed;
396 nums = fwprintf(f, L"%lc", *s);
404 nums = fwprintf(f, L"%ls", s);
411 fwprintf(f, L"%lc", eols);
412 return (printed + 1);
417 bwsncmp(const struct bwstring *bws1, const struct bwstring *bws2,
418 size_t offset, size_t len)
420 size_t cmp_len, len1, len2;
426 if (len1 <= offset) {
427 return ((len2 <= offset) ? 0 : -1);
443 if (mb_cur_max == 1) {
446 s1 = bws1->cdata.str + offset;
447 s2 = bws2->cdata.str + offset;
449 res = memcmp(s1, s2, cmp_len);
452 const wchar_t *s1, *s2;
454 s1 = bws1->wdata.str + offset;
455 s2 = bws2->wdata.str + offset;
457 res = memcmp(s1, s2, SIZEOF_WCHAR_STRING(cmp_len));
463 if (len1 < cmp_len && len1 < len2)
465 else if (len2 < cmp_len && len2 < len1)
473 bwscmp(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
475 size_t len1, len2, cmp_len;
489 res = bwsncmp(bws1, bws2, offset, cmp_len);
494 else if (len2 < len1)
502 bws_iterator_cmp(bwstring_iterator iter1, bwstring_iterator iter2, size_t len)
507 for (i = 0; i < len; ++i) {
508 c1 = bws_get_iter_value(iter1);
509 c2 = bws_get_iter_value(iter2);
512 iter1 = bws_iterator_inc(iter1, 1);
513 iter2 = bws_iterator_inc(iter2, 1);
520 bwscoll(const struct bwstring *bws1, const struct bwstring *bws2, size_t offset)
528 return ((len2 <= offset) ? 0 : -1);
536 if (mb_cur_max == 1) {
539 s1 = bws1->cdata.str + offset;
540 s2 = bws2->cdata.str + offset;
546 res = memcmp(s1, s2, len2);
549 } else if (len1 < len2) {
550 res = memcmp(s1, s2, len1);
554 res = memcmp(s1, s2, len1);
569 /* goto next non-zero part: */
570 while ((i < maxlen) &&
580 err(2, "bwscoll error 01");
583 } else if (s2[i] == 0)
586 res = strcoll((const char*)(s1 + i), (const char*)(s2 + i));
590 while ((i < maxlen) &&
603 } else if (s2[i] == 0)
607 err(2, "bwscoll error 02");
612 else if (len1 > len2)
618 const wchar_t *s1, *s2;
622 s1 = bws1->wdata.str + offset;
623 s2 = bws2->wdata.str + offset;
633 /* goto next non-zero part: */
634 while ((i < maxlen) &&
644 err(2, "bwscoll error 1");
647 } else if (s2[i] == 0)
650 res = wide_str_coll(s1 + i, s2 + i);
654 while ((i < maxlen) && s1[i] && s2[i])
666 } else if (s2[i] == 0)
670 err(2, "bwscoll error 2");
675 else if (len1 > len2)
685 * Correction of the system API
688 bwstod(struct bwstring *s0, bool *empty)
692 if (mb_cur_max == 1) {
697 end = s + s0->cdata.len;
700 while (isblank(*s) && s < end)
708 ret = strtod((char*)s, &ep);
714 wchar_t *end, *ep, *s;
717 end = s + s0->wdata.len;
720 while (iswblank(*s) && s < end)
728 ret = wcstod(s, &ep);
740 * A helper function for monthcoll. If a line matches
741 * a month name, it returns (number of the month - 1),
742 * while if there is no match, it just return -1.
746 bws_month_score(const struct bwstring *s0)
749 if (mb_cur_max == 1) {
753 end = s + s0->cdata.len;
755 while (isblank(*s) && s < end)
758 for (int i = 11; i >= 0; --i) {
760 (s == strstr(s, cmonths[i])))
765 const wchar_t *end, *s;
768 end = s + s0->wdata.len;
770 while (iswblank(*s) && s < end)
773 for (int i = 11; i >= 0; --i) {
774 if (wmonths[i] && (s == wcsstr(s, wmonths[i])))
783 * Rips out leading blanks (-b).
786 ignore_leading_blanks(struct bwstring *str)
789 if (mb_cur_max == 1) {
790 char *dst, *end, *src;
792 src = str->cdata.str;
794 end = src + str->cdata.len;
796 while (src < end && isblank(*src))
802 newlen = BWSLEN(str) - (src - dst);
809 bws_setlen(str, newlen);
812 wchar_t *dst, *end, *src;
814 src = str->wdata.str;
816 end = src + str->wdata.len;
818 while (src < end && iswblank(*src))
823 size_t newlen = BWSLEN(str) - (src - dst);
830 bws_setlen(str, newlen);
838 * Rips out nonprinting characters (-i).
841 ignore_nonprinting(struct bwstring *str)
843 size_t newlen = BWSLEN(str);
845 if (mb_cur_max == 1) {
846 char *dst, *end, *src;
849 src = str->cdata.str;
851 end = src + str->cdata.len;
865 wchar_t *dst, *end, *src;
868 src = str->wdata.str;
870 end = src + str->wdata.len;
884 bws_setlen(str, newlen);
890 * Rips out any characters that are not alphanumeric characters
894 dictionary_order(struct bwstring *str)
896 size_t newlen = BWSLEN(str);
898 if (mb_cur_max == 1) {
899 char *dst, *end, *src;
902 src = str->cdata.str;
904 end = src + str->cdata.len;
908 if (isalnum(c) || isblank(c)) {
918 wchar_t *dst, *end, *src;
921 src = str->wdata.str;
923 end = src + str->wdata.len;
927 if (iswalnum(c) || iswblank(c)) {
937 bws_setlen(str, newlen);
943 * Converts string to lower case(-f).
946 ignore_case(struct bwstring *str)
949 if (mb_cur_max == 1) {
953 end = s + str->cdata.len;
963 end = s + str->wdata.len;
974 bws_disorder_warnx(struct bwstring *s, const char *fn, size_t pos)
978 warnx("%s:%zu: disorder: %s", fn, pos + 1, s->cdata.str);
980 warnx("%s:%zu: disorder: %ls", fn, pos + 1, s->wdata.str);