diff options
author | Jakob Kaivo <jkk@ung.org> | 2019-02-28 14:22:04 -0500 |
---|---|---|
committer | Jakob Kaivo <jkk@ung.org> | 2019-02-28 14:22:04 -0500 |
commit | f777999710878dc9c5cdea3b297c0c1ccd934adc (patch) | |
tree | 2d37c4cdd8487d563487caa58fa60ef946b8d5fb | |
parent | 5608b687ac1a10b1cc525d8e61d0abfce2f5737d (diff) |
clean up internal locale stuff
-rw-r--r-- | src/locale/setlocale.c | 70 | ||||
-rw-r--r-- | src/nonstd/__load_locale.c | 4 | ||||
-rw-r--r-- | src/nonstd/_locale.h (renamed from src/nonstd/__load_locale.h) | 12 | ||||
-rw-r--r-- | src/nonstd/locale-internal.ref | 2 | ||||
-rw-r--r-- | src/nonstd/struct_locale_t.c | 27 |
5 files changed, 56 insertions, 59 deletions
diff --git a/src/locale/setlocale.c b/src/locale/setlocale.c index 0f64b35e..ae1026da 100644 --- a/src/locale/setlocale.c +++ b/src/locale/setlocale.c @@ -1,62 +1,42 @@ #include <locale.h> #include "string.h" #include "stdlib.h" +#include "nonstd/locale.h" char * setlocale(int category, const char *locale) { - static struct { - char *lc_collate; - char *lc_ctype; - char *lc_messages; - char *lc_monetary; - char *lc_numeric; - char *lc_time; - } current = { 0 }; - - char *desired = NULL; - - switch (category) { - case LC_COLLATE: desired = current.lc_collate; break; - case LC_CTYPE: desired = current.lc_ctype; break; - case LC_MONETARY: desired = current.lc_monetary; break; - case LC_NUMERIC: desired = current.lc_numeric; break; - case LC_TIME: desired = current.lc_time; break; - #ifdef LC_MESSAGES - case LC_MESSAGES: desired = current.lc_messages; break; - #endif - default: break; - } + struct __locale_t *l = __libc(GLOBAL_LOCALE); + int mask = 0; if (locale == NULL) { - if (category == LC_ALL) { - /* build a string if locale is not heterogenous */ - } - return desired; - } - - if (category == LC_ALL) { - /* TODO: make sure all these can be honored */ + switch (category) { + case LC_ALL: return l->all; + case LC_COLLATE: return l->collate; + case LC_CTYPE: return l->ctype; + case LC_MONETARY: return l->monetary; + case LC_NUMERIC: return l->numeric; + case LC_TIME: return l->time; #ifdef LC_MESSAGES - setlocale(LC_MESSAGES, locale); + case LC_MESSAGES: return l->messages; #endif - setlocale(LC_COLLATE, locale); - setlocale(LC_CTYPE, locale); - setlocale(LC_MONETARY, locale); - setlocale(LC_NUMERIC, locale); - return setlocale(LC_TIME, locale); + default: return NULL; + } } - if (desired) { - free(desired); + switch (category) { + case LC_ALL: mask = LC_ALL_MASK; break; + case LC_COLLATE: mask = LC_COLLATE_MASK; break; + case LC_CTYPE: mask = LC_CTYPE_MASK; break; + case LC_MONETARY: mask = LC_MONETARY_MASK; break; + case LC_NUMERIC: mask = LC_NUMERIC_MASK; break; + case LC_TIME: mask = LC_TIME_MASK; break; + #ifdef LC_MESSAGES + case LC_MESSAGES: mask = LC_MESSAGES_MASK; break; + #endif + default: return NULL; } - if (!strcmp(locale, "")) { - desired = getenv(""); - } else { - desired = (char*)locale; - } - - return desired; + return __load_locale(l, mask, locale); } /** get or set program locale **/ diff --git a/src/nonstd/__load_locale.c b/src/nonstd/__load_locale.c new file mode 100644 index 00000000..193aa333 --- /dev/null +++ b/src/nonstd/__load_locale.c @@ -0,0 +1,4 @@ +#include <nonstd/locale.h> + +#define __load_locale(_loc, _mask, _name) \ + ((char * (*)(struct __locale *, int, const char *))__libc(LOAD_LOCALE))(_loc, _mask, _name) diff --git a/src/nonstd/__load_locale.h b/src/nonstd/_locale.h index 16709870..40e66548 100644 --- a/src/nonstd/__load_locale.h +++ b/src/nonstd/_locale.h @@ -6,12 +6,14 @@ #include "nonstd/locale.h" #include "nonstd/ctype.h" +/* #define LC_COLLATE_MASK (1<<0) #define LC_CTYPE_MASK (1<<1) #define LC_MONETARY_MASK (1<<2) #define LC_NUMERIC_MASK (1<<3) #define LC_TIME_MASK (1<<4) #define LC_MESSAGES_MASK (1<<5) +*/ #define stringreplace(_old, _new) do { \ _old = realloc(_old, strlen(_new) + 1); \ @@ -21,7 +23,7 @@ strcpy(_old, _new); \ } while (0) -char * __load_locale(struct __locale_t *loc, int mask, const char *name) +static char * (__load_locale)(struct __locale_t *loc, int mask, const char *name) { char localepath[FILENAME_MAX] = "/lib/locale/"; strcat(localepath, name); @@ -46,16 +48,16 @@ char * __load_locale(struct __locale_t *loc, int mask, const char *name) loc->ctattr = realloc(loc->ctattr, CHAR_MAX); for (i = 0; i < 32; i++) { - loc->ctattr[i] = CNTRL; + loc->ctattr[i] = CT_CNTRL; } for (i = 'a'; i < 'z'; i++) { - loc->ctattr[i] = LOWER; + loc->ctattr[i] = CT_LOWER; } for (i = 'A'; i < 'Z'; i++) { - loc->ctattr[i] = UPPER; + loc->ctattr[i] = CT_UPPER; } for (i = '0'; i < '9'; i++) { - loc->ctattr[i] = DIGIT | XDIGIT; + loc->ctattr[i] = CT_DIGIT | CT_XDIGIT; } /* others */ diff --git a/src/nonstd/locale-internal.ref b/src/nonstd/locale-internal.ref new file mode 100644 index 00000000..d795ee8f --- /dev/null +++ b/src/nonstd/locale-internal.ref @@ -0,0 +1,2 @@ +#include <nonstd/locale.h> +REFERENCE(<nonstd/internal.h>) diff --git a/src/nonstd/struct_locale_t.c b/src/nonstd/struct_locale_t.c index 1981bd8d..5487ae6a 100644 --- a/src/nonstd/struct_locale_t.c +++ b/src/nonstd/struct_locale_t.c @@ -1,22 +1,31 @@ #include <nonstd/locale.h> struct __locale_t { - int mask; - char *all; - char *collate; - unsigned char *collation; - char *ctype; - unsigned char *ctattr; - unsigned char *ctoupper; - unsigned char *ctolower; - char *message; + char all[UCHAR_MAX]; + + char ctype[UCHAR_MAX]; + struct { + unsigned char ctattr[UCHAR_MAX + 1]; + unsigned char ctoupper[UCHAR_MAX + 1]; + unsigned char ctolower[UCHAR_MAX + 1]; + } lc_ctype; + + char collate[UCHAR_MAX]; + struct collation { + char * sequence; + int weight; + } *lc_collate; + + char messages[UCHAR_MAX]; struct { char *yesexpr; char *noexpr; } lc_messages; + char *monetary; char *numeric; struct lconv mn; + char *time; struct { char *abday[7]; |