summaryrefslogtreecommitdiff
path: root/src/locale
diff options
context:
space:
mode:
authorJakob Kaivo <jkk@ung.org>2020-08-12 11:02:56 -0400
committerJakob Kaivo <jkk@ung.org>2020-08-12 11:02:56 -0400
commitecd4d00f1bdf184163cc44ed8261d9f11ca9aae0 (patch)
tree7148339b74cfd5f453be629843e4647b8ed3e7a2 /src/locale
parentc37709cb45a4dac2c9c8ffb0fdae2361e2ead7de (diff)
remove all generated nonstd/ headers
Diffstat (limited to 'src/locale')
-rw-r--r--src/locale/__load_locale.c239
-rw-r--r--src/locale/_locale.h65
-rw-r--r--src/locale/localeconv.c3
-rw-r--r--src/locale/setlocale.c3
4 files changed, 308 insertions, 2 deletions
diff --git a/src/locale/__load_locale.c b/src/locale/__load_locale.c
new file mode 100644
index 00000000..d79bb4e4
--- /dev/null
+++ b/src/locale/__load_locale.c
@@ -0,0 +1,239 @@
+#include "locale.h"
+#include "limits.h"
+#include "stdio.h"
+#include "string.h"
+
+#include "_locale.h"
+#include "../ctype/_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 LC_ALL_MASK (0xff)
+
+#define setall(_map, _input, _mask) do { \
+ size_t _i; \
+ for (_i = 0; _i < sizeof(_input) - 1; _i++) { \
+ _map[(int)_input[_i]] |= _mask; \
+ } \
+} while (0)
+
+char * __load_locale(struct __locale_t *loc, int mask, const char *name)
+{
+ if (name == NULL) {
+ name = "";
+ }
+
+ char localepath[FILENAME_MAX] = "/lib/locale/";
+ strcat(localepath, name);
+
+ FILE *localefile = fopen(localepath, "rb");
+ if (localefile == NULL && strcmp(name, "C") && strcmp(name, "POSIX")) {
+ return NULL;
+ }
+
+ if (mask & LC_COLLATE_MASK) {
+ strcpy(loc->collate, name);
+
+ if (localefile == NULL) {
+ /* TODO: POSIX/C locale collation */
+ } else {
+ /* read from file */
+ loc->lc_collate = NULL;
+ }
+ }
+
+ if (mask & LC_CTYPE_MASK) {
+ strcpy(loc->ctype, name);
+
+ if (localefile == NULL) {
+ char upper[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
+ char lower[] = "abcdefghijklmnopqrstuvwxyz";
+ char digit[] = "0123456789";
+ char xdigit[] = "0123456789ABCDEFabcdef";
+ char space[] = " \f\n\r\t\v";
+ char punct[] = "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~";
+ char blank[] = " \t";
+ size_t i;
+
+ memset(loc->lc_ctype.ctattr, 0, sizeof(loc->lc_ctype.ctattr));
+
+ for (i = 0; i < 32; i++) {
+ loc->lc_ctype.ctattr[i] = CT_CNTRL;
+ }
+ loc->lc_ctype.ctattr[127] = CT_CNTRL;
+
+ setall(loc->lc_ctype.ctattr, upper, CT_UPPER);
+ setall(loc->lc_ctype.ctattr, upper, CT_ALPHA);
+ setall(loc->lc_ctype.ctattr, lower, CT_LOWER);
+ setall(loc->lc_ctype.ctattr, lower, CT_ALPHA);
+ setall(loc->lc_ctype.ctattr, digit, CT_DIGIT);
+ setall(loc->lc_ctype.ctattr, xdigit, CT_XDIGIT);
+ setall(loc->lc_ctype.ctattr, space, CT_SPACE);
+ setall(loc->lc_ctype.ctattr, punct, CT_PUNCT);
+ setall(loc->lc_ctype.ctattr, blank, CT_BLANK);
+
+ for (i = 0; i < CHAR_MAX; i++) {
+ if (loc->lc_ctype.ctattr[i] &
+ (CT_UPPER | CT_ALPHA | CT_LOWER |
+ CT_DIGIT | CT_XDIGIT | CT_PUNCT)) {
+ loc->lc_ctype.ctattr[i] |= CT_GRAPH;
+ loc->lc_ctype.ctattr[i] |= CT_PRINT;
+ }
+ }
+
+ loc->lc_ctype.ctattr[' '] |= CT_PRINT;
+
+ for (i = 0; i < UCHAR_MAX; i++) {
+ loc->lc_ctype.ctoupper[i] = i;
+ loc->lc_ctype.ctolower[i] = i;
+ }
+
+ for (i = 0; i < sizeof(upper); i++) {
+ loc->lc_ctype.ctolower[(int)upper[i]] = lower[i];
+ loc->lc_ctype.ctoupper[(int)lower[i]] = upper[i];
+ }
+ } else {
+ /* read from file */
+ /*
+ loc->lc_ctype.ctattr
+ loc->lc_ctype.ctoupper
+ loc->lc_ctype.ctolower
+ */
+ }
+ }
+
+ if (mask & LC_MONETARY_MASK) {
+ strcpy(loc->monetary, name);
+
+ if (localefile == NULL) {
+ loc->mn.mon_decimal_point = "";
+ loc->mn.mon_thousands_sep = "";
+ loc->mn.mon_grouping = "";
+ loc->mn.positive_sign = "";
+ loc->mn.negative_sign = "";
+ loc->mn.currency_symbol = "";
+ loc->mn.frac_digits = CHAR_MAX;
+ loc->mn.p_cs_precedes = CHAR_MAX;
+ loc->mn.n_cs_precedes = CHAR_MAX;
+ loc->mn.p_sep_by_space = CHAR_MAX;
+ loc->mn.n_sep_by_space = CHAR_MAX;
+ loc->mn.p_sign_posn = CHAR_MAX;
+ loc->mn.n_sign_posn = CHAR_MAX;
+ loc->mn.int_curr_symbol = "";
+ loc->mn.int_frac_digits = CHAR_MAX;
+
+ #if defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__
+ loc->mn.int_p_cs_precedes = CHAR_MAX;
+ loc->mn.int_n_cs_precedes = CHAR_MAX;
+ loc->mn.int_p_sep_by_space = CHAR_MAX;
+ loc->mn.int_n_sep_by_space = CHAR_MAX;
+ loc->mn.int_p_sign_posn = CHAR_MAX;
+ loc->mn.int_n_sign_posn = CHAR_MAX;
+ #else
+ loc->mn.__int_p_cs_precedes = CHAR_MAX;
+ loc->mn.__int_n_cs_precedes = CHAR_MAX;
+ loc->mn.__int_p_sep_by_space = CHAR_MAX;
+ loc->mn.__int_n_sep_by_space = CHAR_MAX;
+ loc->mn.__int_p_sign_posn = CHAR_MAX;
+ loc->mn.__int_n_sign_posn = CHAR_MAX;
+ #endif
+ } else {
+ /*
+ loc->mn.monetary fields;
+ */
+ }
+ }
+
+ if (mask & LC_NUMERIC_MASK) {
+ strcpy(loc->numeric, name);
+
+ if (localefile == NULL) {
+ loc->mn.decimal_point = ".";
+ loc->mn.thousands_sep = "";
+ loc->mn.grouping = "";
+ } else {
+ /*
+ loc->mn.numeric fields
+ */
+ }
+ }
+
+ if (mask & LC_TIME_MASK) {
+ strcpy(loc->time, name);
+
+ if (localefile == NULL) {
+ memset(&loc->lc_time, 0, sizeof(loc->lc_time));
+
+ loc->lc_time.d_t_fmt = "%a %b %e %H:%M:%S %Y";
+ loc->lc_time.d_fmt = "%m/%d/%y";
+ loc->lc_time.t_fmt = "%H:%M:%S";
+ loc->lc_time.am_pm[0] = "AM";
+ loc->lc_time.am_pm[1] = "PM";
+ loc->lc_time.t_fmt_ampm = "%I:%M:%S %p";
+ loc->lc_time.day[0] = "Sunday";
+ loc->lc_time.day[1] = "Monday";
+ loc->lc_time.day[2] = "Tuesday";
+ loc->lc_time.day[3] = "Wednesday";
+ loc->lc_time.day[4] = "Thursday";
+ loc->lc_time.day[5] = "Friday";
+ loc->lc_time.day[6] = "Saturday";
+ loc->lc_time.abday[0] = "Sun";
+ loc->lc_time.abday[1] = "Mon";
+ loc->lc_time.abday[2] = "Tue";
+ loc->lc_time.abday[3] = "Wed";
+ loc->lc_time.abday[4] = "Thu";
+ loc->lc_time.abday[5] = "Fri";
+ loc->lc_time.abday[6] = "Sat";
+ loc->lc_time.mon[0] = "January";
+ loc->lc_time.mon[1] = "February";
+ loc->lc_time.mon[2] = "March";
+ loc->lc_time.mon[3] = "April";
+ loc->lc_time.mon[4] = "May";
+ loc->lc_time.mon[5] = "June";
+ loc->lc_time.mon[6] = "July";
+ loc->lc_time.mon[7] = "August";
+ loc->lc_time.mon[8] = "September";
+ loc->lc_time.mon[9] = "October";
+ loc->lc_time.mon[10] = "November";
+ loc->lc_time.mon[11] = "December";
+ loc->lc_time.abmon[0] = "Jan";
+ loc->lc_time.abmon[1] = "Feb";
+ loc->lc_time.abmon[2] = "Mar";
+ loc->lc_time.abmon[3] = "Apr";
+ loc->lc_time.abmon[4] = "May";
+ loc->lc_time.abmon[5] = "Jun";
+ loc->lc_time.abmon[6] = "Jul";
+ loc->lc_time.abmon[7] = "Aug";
+ loc->lc_time.abmon[8] = "Sep";
+ loc->lc_time.abmon[9] = "Oct";
+ loc->lc_time.abmon[10] = "Nov";
+ loc->lc_time.abmon[11] = "Dec";
+ } else {
+ /* read from file */
+ /* loc->lc_time */
+ }
+ }
+
+ if (mask & LC_MESSAGES_MASK) {
+ strcpy(loc->messages, name);
+
+ if (localefile == NULL) {
+ loc->lc_messages.yesexpr = "^[yY]";
+ loc->lc_messages.noexpr = "^[nN]";
+ } else {
+ /* read */
+ loc->lc_messages.yesexpr = NULL;
+ loc->lc_messages.noexpr = NULL;
+ }
+ }
+
+ return (char*)name;
+}
+
+/*
+STDC(0)
+*/
diff --git a/src/locale/_locale.h b/src/locale/_locale.h
new file mode 100644
index 00000000..bed7e0f5
--- /dev/null
+++ b/src/locale/_locale.h
@@ -0,0 +1,65 @@
+#ifndef ___LOCALE_H__
+#define ___LOCALE_H__
+
+#include <locale.h>
+#include <limits.h>
+
+struct __locale_t {
+ char all[UCHAR_MAX];
+
+ char ctype[UCHAR_MAX];
+ struct {
+ unsigned int 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[UCHAR_MAX];
+ char numeric[UCHAR_MAX];
+ struct lconv mn;
+
+ char time[UCHAR_MAX];
+ struct {
+ char *abday[7];
+ char *day[7];
+ char *abmon[12];
+ char *mon[12];
+ char *d_t_fmt;
+ char *d_fmt;
+ char *t_fmt;
+ char *am_pm[2];
+ char *t_fmt_ampm;
+ struct {
+ char direction;
+ int offset;
+ int start_year;
+ int start_month;
+ int start_day;
+ int end_year;
+ int end_month;
+ int end_day;
+ char *era_name;
+ char *era_format;
+ } era;
+ char *era_d_fmt;
+ char *era_t_fmt;
+ char *era_d_t_fmt;
+ char *alt_digits;
+ } lc_time;
+};
+
+char * __load_locale(struct __locale_t *, int, const char *);
+
+#endif
diff --git a/src/locale/localeconv.c b/src/locale/localeconv.c
index f08eb872..e63402e2 100644
--- a/src/locale/localeconv.c
+++ b/src/locale/localeconv.c
@@ -1,5 +1,6 @@
#include <locale.h>
-#include "nonstd/locale.h"
+#include "_locale.h"
+#include "../_nonstd.h"
/** return locale-specific information **/
struct lconv * localeconv(void)
diff --git a/src/locale/setlocale.c b/src/locale/setlocale.c
index 56043e61..068bf858 100644
--- a/src/locale/setlocale.c
+++ b/src/locale/setlocale.c
@@ -1,7 +1,8 @@
#include <locale.h>
#include "string.h"
#include "stdlib.h"
-#include "nonstd/locale.h"
+#include "_locale.h"
+#include "../_nonstd.h"
#include "LC_ALL_MASK.c"
#include "LC_COLLATE_MASK.c"