Skip to content

Commit

Permalink
libs/posix: Implemented new locale functions
Browse files Browse the repository at this point in the history
Implemented the missing POSIX functions in <locale.h>:
newlocale, duplocale, uselocale, and freelocale, and also
provided missing type definitions for <locale.h>.

Implemented missing POSIX locale-based function variants.

Modified LocaleBackend so that it could support thread-local
locales.

Some glibc-like locale-related variables supporting
ctype and printf family of functions have also been updated
to reflect the thread-local variables present in the latest
glibc sources.

As there have been some modifications to global symbols
in libroot, libroot_stubs.c has been regenerated.

Bug: #17168
Change-Id: Ibf296c58c47d42d1d1dfb2ce64042442f2679431
Reviewed-on: https://review.haiku-os.org/c/haiku/+/5351
Tested-by: Commit checker robot <no-reply+buildbot@haiku-os.org>
Reviewed-by: Adrien Destugues <pulkomandy@pulkomandy.tk>
  • Loading branch information
trungnt2910 authored and waddlesplash committed Jul 11, 2022
1 parent 86fa1c2 commit d338200
Show file tree
Hide file tree
Showing 64 changed files with 2,112 additions and 961 deletions.
30 changes: 27 additions & 3 deletions headers/posix/ctype.h
Expand Up @@ -6,6 +6,9 @@
#define _CTYPE_H


#include <locale_t.h>


#ifdef __cplusplus
extern "C" {
#endif
Expand All @@ -27,6 +30,22 @@ int toascii(int);
int tolower(int);
int toupper(int);


int isalnum_l(int, locale_t);
int isalpha_l(int, locale_t);
int isblank_l(int, locale_t);
int iscntrl_l(int, locale_t);
int isdigit_l(int, locale_t);
int isgraph_l(int, locale_t);
int islower_l(int, locale_t);
int isprint_l(int, locale_t);
int ispunct_l(int, locale_t);
int isspace_l(int, locale_t);
int isupper_l(int, locale_t);
int isxdigit_l(int, locale_t);
int tolower_l(int, locale_t);
int toupper_l(int, locale_t);

enum {
_ISblank = 0x0001, /* blank */
_IScntrl = 0x0002, /* control */
Expand All @@ -48,14 +67,19 @@ extern const unsigned short int *__ctype_b;
extern const int *__ctype_tolower;
extern const int *__ctype_toupper;

extern const unsigned short int **__ctype_b_loc();
extern const int **__ctype_tolower_loc();
extern const int **__ctype_toupper_loc();

#define __isctype(c, type) \
(__ctype_b[(int)(c)] & (unsigned short int)type)
((*__ctype_b_loc())[(int)(c)] & (unsigned short int)type)

#define tolower(c) ((int)(*__ctype_tolower_loc())[(int)(c)])
#define toupper(c) ((int)(*__ctype_toupper_loc())[(int)(c)])

#define isascii(c) (((c) & ~0x7f) == 0) /* ASCII characters have bit 8 cleared */
#define toascii(c) ((c) & 0x7f) /* Clear higher bits */

#define tolower(c) ((int)__ctype_tolower[(int)(c)])
#define toupper(c) ((int)__ctype_toupper[(int)(c)])
#define _tolower(c) tolower(c)
#define _toupper(c) toupper(c)

Expand Down
1 change: 1 addition & 0 deletions headers/posix/langinfo.h
Expand Up @@ -145,6 +145,7 @@ enum {
__BEGIN_DECLS

extern char* nl_langinfo(nl_item item);
extern char* nl_langinfo_l(nl_item item, locale_t locale);

__END_DECLS

Expand Down
19 changes: 19 additions & 0 deletions headers/posix/locale.h
Expand Up @@ -6,6 +6,7 @@
#define _LOCALE_H_


#include <locale_t.h>
#include <null.h>

struct lconv {
Expand Down Expand Up @@ -48,13 +49,31 @@ struct lconv {
*/
#define LC_LAST LC_MESSAGES

#define LC_COLLATE_MASK (1 << (LC_COLLATE - 1))
#define LC_CTYPE_MASK (1 << (LC_CTYPE - 1))
#define LC_MONETARY_MASK (1 << (LC_MONETARY - 1))
#define LC_NUMERIC_MASK (1 << (LC_NUMERIC - 1))
#define LC_TIME_MASK (1 << (LC_TIME - 1))
#define LC_MESSAGES_MASK (1 << (LC_MESSAGES - 1))

#define LC_ALL_MASK (LC_COLLATE_MASK | LC_CTYPE_MASK | \
LC_MONETARY_MASK | LC_NUMERIC_MASK | \
LC_TIME_MASK | LC_MESSAGES_MASK)

#define LC_GLOBAL_LOCALE ((locale_t)-1)

#ifdef __cplusplus
extern "C" {
#endif

extern struct lconv *localeconv(void);
extern char *setlocale(int category, const char *locale);

extern locale_t duplocale(locale_t);
extern void freelocale(locale_t);
extern locale_t newlocale(int, const char *, locale_t);
extern locale_t uselocale(locale_t);

#ifdef __cplusplus
}
#endif
Expand Down
12 changes: 12 additions & 0 deletions headers/posix/locale_t.h
@@ -0,0 +1,12 @@
/*
* Copyright 2022 Haiku, Inc. All rights reserved.
* Distributed under the terms of the MIT License.
*/
#ifndef _LOCALE_T_H_
#define _LOCALE_T_H_


typedef void* locale_t;


#endif
3 changes: 2 additions & 1 deletion headers/posix/monetary.h
Expand Up @@ -6,7 +6,7 @@
#define _MONETARY_H_


#include <locale.h>
#include <locale_t.h>
#include <stddef.h>
#include <sys/cdefs.h>
#include <sys/types.h>
Expand All @@ -15,6 +15,7 @@
__BEGIN_DECLS

extern ssize_t strfmon(char* s, size_t maxsize, const char* format, ...);
extern ssize_t strfmon_l(char* s, size_t maxsize, locale_t locale, const char* format, ...);

__END_DECLS

Expand Down
6 changes: 6 additions & 0 deletions headers/posix/string.h
Expand Up @@ -6,6 +6,7 @@
#define _STRING_H_


#include <locale_t.h>
#include <sys/types.h>


Expand Down Expand Up @@ -74,6 +75,11 @@ extern char *strupr(char *string);

extern const char *strsignal(int signum);

/* locale versions of string functions */
extern int strcoll_l(const char *string1, const char *string2, locale_t locale);
extern char *strerror_l(int errorCode, locale_t locale);
extern size_t strxfrm_l(char *string1, const char *string2, size_t length, locale_t locale);

/* for compatibility, pull in functions declared in strings.h */
#include <strings.h>

Expand Down
5 changes: 5 additions & 0 deletions headers/posix/strings.h
Expand Up @@ -6,6 +6,7 @@
#define _STRINGS_H_


#include <locale_t.h>
#include <sys/types.h>


Expand All @@ -19,6 +20,10 @@ extern int strcasecmp(const char *string1, const char *string2);
extern int strncasecmp(const char *string1, const char *string2,
size_t length);

extern int strcasecmp_l(const char *string1, const char *string2, locale_t locale);
extern int strncasecmp_l(const char *string1, const char *string2,
size_t length, locale_t locale);

/* legacy compatibility -- might be removed one day */
#define bcmp(a, b, length) memcmp((a), (b), (length))
#define bcopy(source, dest, length) memmove((dest), (source), (length))
Expand Down
3 changes: 3 additions & 0 deletions headers/posix/time.h
Expand Up @@ -6,6 +6,7 @@
#define _TIME_H_


#include <locale_t.h>
#include <sys/types.h>


Expand Down Expand Up @@ -92,6 +93,8 @@ extern struct tm *localtime_r(const time_t *timer, struct tm *tm);
extern int nanosleep(const struct timespec *, struct timespec *);
extern size_t strftime(char *buffer, size_t maxSize, const char *format,
const struct tm *tm);
extern size_t strftime_l(char *buffer, size_t maxSize, const char *format,
const struct tm *tm, locale_t locale);
extern char *strptime(const char *buf, const char *format, struct tm *tm);

/* clock functions */
Expand Down
7 changes: 7 additions & 0 deletions headers/posix/wchar.h
Expand Up @@ -7,6 +7,7 @@


#include <limits.h>
#include <locale.h>
#include <stddef.h>
#include <stdio.h>
#include <time.h>
Expand Down Expand Up @@ -93,13 +94,15 @@ extern wchar_t *wcpcpy(wchar_t *dest, const wchar_t *src);
extern wchar_t *wcpncpy(wchar_t *dest, const wchar_t *src, size_t srcLength);
extern size_t wcrtomb(char *dest, wchar_t wc, mbstate_t *mbState);
extern int wcscasecmp(const wchar_t *wcs1, const wchar_t *wcs2);
extern int wcscasecmp_l(const wchar_t *wcs1, const wchar_t *wcs2, locale_t locale);
extern wchar_t *wcscat(wchar_t *dest, const wchar_t *src);
extern wchar_t *wcschr(const wchar_t *wcs, wchar_t wc);
#ifdef _GNU_SOURCE
extern wchar_t *wcschrnul(const wchar_t *wcs, wchar_t wc);
#endif
extern int wcscmp(const wchar_t *wcs1, const wchar_t *wcs2);
extern int wcscoll(const wchar_t *wcs1, const wchar_t *wcs2);
extern int wcscoll_l(const wchar_t *wcs1, const wchar_t *wcs2, locale_t locale);
extern wchar_t *wcscpy(wchar_t *dest, const wchar_t *src);
extern size_t wcscspn(const wchar_t *wcs, const wchar_t *reject);
extern wchar_t *wcsdup(const wchar_t *wcs);
Expand All @@ -110,6 +113,8 @@ extern size_t wcslcpy(wchar_t *dest, const wchar_t *src, size_t maxLength);
extern size_t wcslen(const wchar_t *wcs);
extern int wcsncasecmp(const wchar_t *wcs1, const wchar_t *wcs2,
size_t maxLength);
extern int wcsncasecmp_l(const wchar_t *wcs1, const wchar_t *wcs2,
size_t maxLength, locale_t locale);
extern wchar_t *wcsncat(wchar_t *dest, const wchar_t *src, size_t srcLength);
extern int wcsncmp(const wchar_t *wcs1, const wchar_t *wcs2,
size_t length);
Expand All @@ -136,6 +141,8 @@ extern unsigned long long wcstoull(const wchar_t *wcs, wchar_t **endPtr,
extern wchar_t *wcswcs(const wchar_t *haystack, const wchar_t *needle);
extern int wcswidth(const wchar_t *wcs, size_t length);
extern size_t wcsxfrm(wchar_t *dest, const wchar_t *src, size_t destLength);
extern size_t wcsxfrm_l(wchar_t *dest, const wchar_t *src, size_t destLength,
locale_t locale);
extern int wctob(wint_t wc);
extern int wcwidth(wchar_t wc);
extern wchar_t *wmemchr(const wchar_t *wcs, wchar_t wc, size_t n);
Expand Down
24 changes: 24 additions & 0 deletions headers/posix/wctype.h
Expand Up @@ -6,6 +6,7 @@
#define _WCTYPE_H_


#include <locale.h>
#include <wchar.h>

typedef int wctrans_t;
Expand Down Expand Up @@ -36,6 +37,29 @@ extern wint_t towupper(wint_t wc);
extern wctrans_t wctrans(const char *charClass);
extern wctype_t wctype(const char *property);


extern int iswalnum_l(wint_t wc, locale_t locale);
extern int iswalpha_l(wint_t wc, locale_t locale);
extern int iswcntrl_l(wint_t wc, locale_t locale);
extern int iswctype_l(wint_t wc, wctype_t desc, locale_t locale);
extern int iswdigit_l(wint_t wc, locale_t locale);
extern int iswgraph_l(wint_t wc, locale_t locale);
extern int iswlower_l(wint_t wc, locale_t locale);
extern int iswprint_l(wint_t wc, locale_t locale);
extern int iswpunct_l(wint_t wc, locale_t locale);
extern int iswspace_l(wint_t wc, locale_t locale);
extern int iswupper_l(wint_t wc, locale_t locale);
extern int iswxdigit_l(wint_t wc, locale_t locale);

extern int iswblank_l(wint_t wc, locale_t locale);

extern wint_t towctrans_l(wint_t wc, wctrans_t transition, locale_t locale);
extern wint_t towlower_l(wint_t wc, locale_t locale);
extern wint_t towupper_l(wint_t wc, locale_t locale);

extern wctrans_t wctrans_l(const char *charClass, locale_t locale);
extern wctype_t wctype_l(const char *property, locale_t locale);

#ifdef __cplusplus
}
#endif
Expand Down

0 comments on commit d338200

Please sign in to comment.