Home | History | Annotate | Line # | Download | only in libhack
      1  1.9    martin /*      $NetBSD: multibyte.c,v 1.9 2019/08/01 12:28:53 martin Exp $      */
      2  1.1  christos 
      3  1.1  christos /*
      4  1.1  christos  * Ignore all multibyte sequences, removes all the citrus code.
      5  1.1  christos  * Probably only used by vfprintf() when parsing the format string.
      6  1.3       dsl  * And possibly from libcurses if compiled with HAVE_WCHAR.
      7  1.1  christos  */
      8  1.1  christos 
      9  1.5     joerg #include <stdlib.h>
     10  1.8    martin #include <string.h>
     11  1.1  christos #include <wchar.h>
     12  1.8    martin #include <wctype.h>
     13  1.8    martin #include <ctype.h>
     14  1.1  christos 
     15  1.1  christos size_t
     16  1.1  christos mbrtowc(wchar_t *wc, const char *str, size_t max_sz, mbstate_t *ps)
     17  1.1  christos {
     18  1.6  christos 	if (str == NULL)
     19  1.6  christos 		return 0;
     20  1.6  christos 
     21  1.6  christos 	if (wc != NULL)
     22  1.6  christos 		*wc = (unsigned char)*str;
     23  1.6  christos 
     24  1.6  christos 	return *str == '\0' ? 0 : 1;
     25  1.1  christos }
     26  1.1  christos 
     27  1.1  christos size_t
     28  1.5     joerg mbrtowc_l(wchar_t *wc, const char *str, size_t max_sz, mbstate_t *ps, locale_t loc)
     29  1.5     joerg {
     30  1.5     joerg 	return mbrtowc(wc, str, max_sz, ps);
     31  1.5     joerg }
     32  1.5     joerg 
     33  1.5     joerg size_t
     34  1.1  christos wcrtomb(char *str, wchar_t wc, mbstate_t *ps)
     35  1.1  christos {
     36  1.4   tnozaki     *str = wc & 0xFF;
     37  1.1  christos     return 1;
     38  1.1  christos }
     39  1.1  christos 
     40  1.5     joerg 
     41  1.5     joerg size_t
     42  1.5     joerg wcrtomb_l(char *str, wchar_t wc, mbstate_t *ps, locale_t loc)
     43  1.5     joerg {
     44  1.5     joerg 	return wcrtomb(str, wc, ps);
     45  1.5     joerg }
     46  1.5     joerg 
     47  1.1  christos int
     48  1.1  christos wctob(wint_t x)
     49  1.1  christos {
     50  1.1  christos 	return x;
     51  1.1  christos }
     52  1.1  christos 
     53  1.5     joerg int
     54  1.5     joerg wctob_l(wint_t x, locale_t loc)
     55  1.5     joerg {
     56  1.5     joerg 	return x;
     57  1.5     joerg }
     58  1.5     joerg 
     59  1.5     joerg wint_t
     60  1.5     joerg btowc(int x)
     61  1.5     joerg {
     62  1.5     joerg 	return x;
     63  1.5     joerg }
     64  1.5     joerg 
     65  1.1  christos wint_t
     66  1.5     joerg btowc_l(int x, locale_t loc)
     67  1.5     joerg {
     68  1.1  christos 	return x;
     69  1.1  christos }
     70  1.1  christos 
     71  1.1  christos size_t
     72  1.1  christos mbrlen(const char * __restrict p, size_t l, mbstate_t * __restrict v)
     73  1.1  christos {
     74  1.1  christos 	size_t i;
     75  1.1  christos 	for (i = 0; i < l; i++)
     76  1.1  christos 		if (p[i] == '\0')
     77  1.1  christos 			return i;
     78  1.1  christos 	return l;
     79  1.1  christos }
     80  1.1  christos 
     81  1.5     joerg 
     82  1.5     joerg size_t
     83  1.5     joerg mbrlen_l(const char * __restrict p, size_t l, mbstate_t * __restrict v,
     84  1.5     joerg     locale_t loc)
     85  1.5     joerg {
     86  1.5     joerg 	return mbrlen(p, l, v);
     87  1.5     joerg }
     88  1.5     joerg 
     89  1.1  christos int
     90  1.1  christos mbsinit(const mbstate_t *s)
     91  1.1  christos {
     92  1.1  christos 	return 0;
     93  1.1  christos }
     94  1.1  christos 
     95  1.1  christos size_t
     96  1.1  christos mbsrtowcs(wchar_t * __restrict pwcs, const char ** __restrict s, size_t n,
     97  1.1  christos     mbstate_t * __restrict ps)
     98  1.1  christos {
     99  1.2        he 	const char *p;
    100  1.2        he 	wchar_t *d;
    101  1.2        he 	size_t count;
    102  1.2        he 
    103  1.2        he 	for (p = *s, d = pwcs, count = 0;
    104  1.2        he 		count <= n;
    105  1.2        he 		count++, d++, p++)
    106  1.2        he 	{
    107  1.2        he 		if (mbrtowc(d, p, 1, ps) == 0)
    108  1.2        he 			break;
    109  1.2        he 	}
    110  1.2        he 	return count;
    111  1.1  christos }
    112  1.1  christos 
    113  1.5     joerg 
    114  1.5     joerg size_t
    115  1.5     joerg mbsrtowcs_l(wchar_t * __restrict pwcs, const char ** __restrict s, size_t n,
    116  1.5     joerg     mbstate_t * __restrict ps, locale_t loc)
    117  1.5     joerg {
    118  1.5     joerg 	return mbsrtowcs(pwcs, s, n, ps);
    119  1.5     joerg }
    120  1.5     joerg 
    121  1.1  christos size_t
    122  1.1  christos wcsrtombs(char * __restrict s, const wchar_t ** __restrict pwcs, size_t n,
    123  1.1  christos     mbstate_t * __restrict ps)
    124  1.1  christos {
    125  1.2        he 	char *d;
    126  1.2        he 	const wchar_t *p;
    127  1.2        he 	size_t count;
    128  1.2        he 
    129  1.2        he 	for (p = *pwcs, d = s, count = 0;
    130  1.2        he 		count <= n && *p != 0;
    131  1.2        he 		count++, d++, p++)
    132  1.2        he 	{
    133  1.2        he 		wcrtomb(d, *p, ps);
    134  1.2        he 	}
    135  1.2        he 	*d = 0;
    136  1.2        he 	return count;
    137  1.1  christos }
    138  1.5     joerg 
    139  1.5     joerg size_t
    140  1.5     joerg wcsrtombs_l(char * __restrict s, const wchar_t ** __restrict pwcs, size_t n,
    141  1.5     joerg     mbstate_t * __restrict ps, locale_t loc)
    142  1.5     joerg {
    143  1.5     joerg 	return wcsrtombs(s, pwcs, n, ps);
    144  1.5     joerg }
    145  1.5     joerg 
    146  1.5     joerg size_t
    147  1.5     joerg _mb_cur_max_l(locale_t loc)
    148  1.5     joerg {
    149  1.5     joerg 	return MB_CUR_MAX;
    150  1.5     joerg }
    151  1.8    martin 
    152  1.8    martin wint_t
    153  1.8    martin fgetwc(FILE *stream)
    154  1.8    martin {
    155  1.8    martin 	return fgetc(stream);
    156  1.8    martin }
    157  1.8    martin 
    158  1.8    martin wint_t
    159  1.8    martin fputwc(wchar_t wc, FILE *stream)
    160  1.8    martin {
    161  1.8    martin 	return fputc(wc & 0xFF, stream);
    162  1.8    martin }
    163  1.8    martin 
    164  1.8    martin wint_t __fputwc_unlock(wchar_t wc, FILE *stream);
    165  1.8    martin wint_t
    166  1.8    martin __fputwc_unlock(wchar_t wc, FILE *stream)
    167  1.8    martin {
    168  1.8    martin 	return __sputc(wc & 0xFF, stream);
    169  1.8    martin }
    170  1.8    martin 
    171  1.8    martin #define	MAPSINGLE(CT)	\
    172  1.8    martin 	int	\
    173  1.8    martin 	isw##CT(wint_t wc)	\
    174  1.8    martin 	{	\
    175  1.8    martin 		return is##CT(wc & 0xFF);	\
    176  1.8    martin 	}
    177  1.8    martin 
    178  1.8    martin MAPSINGLE(alnum)
    179  1.8    martin MAPSINGLE(alpha)
    180  1.8    martin MAPSINGLE(blank)
    181  1.8    martin MAPSINGLE(cntrl)
    182  1.8    martin MAPSINGLE(digit)
    183  1.8    martin MAPSINGLE(graph)
    184  1.8    martin MAPSINGLE(lower)
    185  1.8    martin MAPSINGLE(print)
    186  1.8    martin MAPSINGLE(punct)
    187  1.8    martin MAPSINGLE(space)
    188  1.8    martin MAPSINGLE(upper)
    189  1.8    martin MAPSINGLE(xdigit)
    190  1.8    martin 
    191  1.8    martin int
    192  1.8    martin iswspace_l(wint_t wc, locale_t loc)
    193  1.8    martin {
    194  1.8    martin 	return iswspace(wc);
    195  1.8    martin }
    196  1.8    martin 
    197  1.8    martin struct wct_entry_hack {
    198  1.8    martin 	const char *name;
    199  1.8    martin 	int (*predicate)(wint_t);
    200  1.8    martin };
    201  1.8    martin 
    202  1.8    martin #define	WCTENTRY(T)	{ .name= #T , .predicate= isw##T },
    203  1.8    martin static const struct wct_entry_hack my_wcts[] = {
    204  1.8    martin 	{ .name = NULL },
    205  1.8    martin 	WCTENTRY(alnum)
    206  1.8    martin 	WCTENTRY(alpha)
    207  1.8    martin 	WCTENTRY(blank)
    208  1.8    martin 	WCTENTRY(cntrl)
    209  1.8    martin 	WCTENTRY(digit)
    210  1.8    martin 	WCTENTRY(graph)
    211  1.8    martin 	WCTENTRY(lower)
    212  1.8    martin 	WCTENTRY(print)
    213  1.8    martin 	WCTENTRY(punct)
    214  1.8    martin 	WCTENTRY(space)
    215  1.8    martin 	WCTENTRY(upper)
    216  1.8    martin 	WCTENTRY(xdigit)
    217  1.8    martin };
    218  1.8    martin 
    219  1.8    martin wctype_t
    220  1.8    martin wctype(const char *charclass)
    221  1.8    martin {
    222  1.8    martin 
    223  1.8    martin 	for (size_t i = 1; i < __arraycount(my_wcts); i++)
    224  1.8    martin 		if (strcmp(charclass, my_wcts[i].name) == 0)
    225  1.8    martin 			return (wctype_t)i;
    226  1.8    martin 
    227  1.8    martin 	return (wctype_t)0;
    228  1.8    martin }
    229  1.8    martin 
    230  1.8    martin int
    231  1.8    martin iswctype(wint_t wc, wctype_t charclass)
    232  1.8    martin {
    233  1.8    martin 	size_t ndx = (size_t)charclass;
    234  1.8    martin 
    235  1.8    martin 	if (ndx < 1 || ndx >= __arraycount(my_wcts))
    236  1.8    martin 		return 0;
    237  1.8    martin 
    238  1.8    martin 	return my_wcts[ndx].predicate(wc);
    239  1.8    martin }
    240  1.8    martin 
    241  1.8    martin size_t
    242  1.8    martin wcslen(const wchar_t *s)
    243  1.8    martin {
    244  1.8    martin 	size_t l;
    245  1.8    martin 
    246  1.8    martin 	if (s == NULL)
    247  1.8    martin 		return 0;
    248  1.8    martin 
    249  1.9    martin 	for (l = 0; *s; l++)
    250  1.8    martin 		s++;
    251  1.8    martin 
    252  1.8    martin 	return l;
    253  1.8    martin }
    254  1.8    martin 
    255  1.8    martin int
    256  1.8    martin wcswidth(const wchar_t *pwcs, size_t n)
    257  1.8    martin {
    258  1.8    martin 	int cols;
    259  1.8    martin 
    260  1.8    martin 	if (pwcs == NULL)
    261  1.8    martin 		return 0;
    262  1.8    martin 
    263  1.8    martin 	if (*pwcs == 0)
    264  1.8    martin 		return 0;
    265  1.8    martin 
    266  1.8    martin 	for (cols = 0; *pwcs && n > 0; cols++)
    267  1.8    martin 		if (!isprint(*pwcs & 0xFF))
    268  1.8    martin 			return -1;
    269  1.8    martin 	return cols;
    270  1.8    martin }
    271  1.8    martin 
    272  1.8    martin int
    273  1.8    martin wcwidth(wchar_t wc)
    274  1.8    martin {
    275  1.8    martin 	if (wc == 0)
    276  1.8    martin 		return 0;
    277  1.8    martin 	if (!isprint(wc & 0xFF))
    278  1.8    martin 		return -1;
    279  1.8    martin 	return 1;
    280  1.8    martin }
    281  1.8    martin 
    282  1.8    martin wchar_t *
    283  1.8    martin wmemchr(const wchar_t *s, wchar_t c, size_t n)
    284  1.8    martin {
    285  1.8    martin 
    286  1.8    martin 	if (s == NULL)
    287  1.8    martin 		return NULL;
    288  1.8    martin 	while (*s != 0 && *s != c)
    289  1.8    martin 		s++;
    290  1.8    martin 	if (*s != 0)
    291  1.8    martin 		return __UNCONST(s);
    292  1.8    martin 	return NULL;
    293  1.8    martin }
    294  1.8    martin 
    295  1.8    martin wchar_t *
    296  1.8    martin wmemcpy(wchar_t * restrict s1, const wchar_t * restrict s2, size_t n)
    297  1.8    martin {
    298  1.8    martin 	wchar_t *p;
    299  1.8    martin 
    300  1.8    martin 	for (p = s1; n > 0; n--)
    301  1.8    martin 		*p++ = *s2++;
    302  1.8    martin 
    303  1.8    martin 	return s1;
    304  1.8    martin }
    305  1.8    martin 
    306  1.8    martin wint_t
    307  1.8    martin towlower(wint_t wc)
    308  1.8    martin {
    309  1.8    martin 	return tolower(wc & 0xFF);
    310  1.8    martin }
    311  1.8    martin 
    312  1.8    martin wint_t
    313  1.8    martin towupper(wint_t wc)
    314  1.8    martin {
    315  1.8    martin 	return toupper(wc & 0xFF);
    316  1.8    martin }
    317  1.8    martin 
    318  1.8    martin 
    319