Changeset 28a5ebd in mainline for kernel/generic/src


Ignore:
Timestamp:
2020-06-18T15:39:50Z (5 years ago)
Author:
Martin Decky <martin@…>
Branches:
lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
Children:
ce52c333
Parents:
4f663f3e
Message:

Use char32_t instead of wchat_t to represent UTF-32 strings

The intention of the native HelenOS string API has been always to
support Unicode in the UTF-8 and UTF-32 encodings as the sole character
representations and ignore the obsolete mess of older single-byte and
multibyte character encodings. Before C11, the wchar_t type has been
slightly misused for the purpose of the UTF-32 strings. The newer
char32_t type is obviously a much more suitable option. The standard
defines char32_t as uint_least32_t, thus we can take the liberty to fix
it to uint32_t.

To maintain compatilibity with the C Standard, the putwchar(wchar_t)
functions has been replaced by our custom putuchar(char32_t) functions
where appropriate.

Location:
kernel/generic/src
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • kernel/generic/src/console/chardev.c

    r4f663f3e r28a5ebd  
    6565 *
    6666 */
    67 void indev_push_character(indev_t *indev, wchar_t ch)
     67void indev_push_character(indev_t *indev, char32_t ch)
    6868{
    6969        assert(indev);
     
    9292 *
    9393 */
    94 wchar_t indev_pop_character(indev_t *indev)
     94char32_t indev_pop_character(indev_t *indev)
    9595{
    9696        if (atomic_load(&haltstate)) {
     
    117117        waitq_sleep(&indev->wq);
    118118        irq_spinlock_lock(&indev->lock, true);
    119         wchar_t ch = indev->buffer[(indev->index - indev->counter) %
     119        char32_t ch = indev->buffer[(indev->index - indev->counter) %
    120120            INDEV_BUFLEN];
    121121        indev->counter--;
  • kernel/generic/src/console/console.c

    r4f663f3e r28a5ebd  
    5959
    6060#define KIO_PAGES    8
    61 #define KIO_LENGTH   (KIO_PAGES * PAGE_SIZE / sizeof(wchar_t))
     61#define KIO_LENGTH   (KIO_PAGES * PAGE_SIZE / sizeof(char32_t))
    6262
    6363/** Kernel log cyclic buffer */
    64 wchar_t kio[KIO_LENGTH] __attribute__((aligned(PAGE_SIZE)));
     64char32_t kio[KIO_LENGTH] __attribute__((aligned(PAGE_SIZE)));
    6565
    6666/** Kernel log initialized */
     
    9595};
    9696
    97 static void stdout_write(outdev_t *, wchar_t);
     97static void stdout_write(outdev_t *, char32_t);
    9898static void stdout_redraw(outdev_t *);
    9999static void stdout_scroll_up(outdev_t *);
     
    148148}
    149149
    150 static void stdout_write(outdev_t *dev, wchar_t ch)
     150static void stdout_write(outdev_t *dev, char32_t ch)
    151151{
    152152        list_foreach(dev->list, link, outdev_t, sink) {
     
    261261        buf[offset] = 0;
    262262
    263         wchar_t ch;
     263        char32_t ch;
    264264        while ((ch = indev_pop_character(indev)) != '\n') {
    265265                if (ch == '\b') {
    266266                        if (count > 0) {
    267267                                /* Space, backspace, space */
    268                                 putwchar('\b');
    269                                 putwchar(' ');
    270                                 putwchar('\b');
     268                                putuchar('\b');
     269                                putuchar(' ');
     270                                putuchar('\b');
    271271
    272272                                count--;
     
    277277
    278278                if (chr_encode(ch, buf, &offset, buflen - 1) == EOK) {
    279                         putwchar(ch);
     279                        putuchar(ch);
    280280                        count++;
    281281                        buf[offset] = 0;
     
    287287
    288288/** Get character from input device & echo it to screen */
    289 wchar_t getc(indev_t *indev)
    290 {
    291         wchar_t ch = indev_pop_character(indev);
    292         putwchar(ch);
     289char32_t getc(indev_t *indev)
     290{
     291        char32_t ch = indev_pop_character(indev);
     292        putuchar(ch);
    293293        return ch;
    294294}
     
    324324        /* Print characters that weren't printed earlier */
    325325        while (kio_stored > 0) {
    326                 wchar_t tmp = kio[(kio_start + kio_len - kio_stored) % KIO_LENGTH];
     326                char32_t tmp = kio[(kio_start + kio_len - kio_stored) % KIO_LENGTH];
    327327                kio_stored--;
    328328
     
    344344 * The caller is required to hold kio_lock
    345345 */
    346 void kio_push_char(const wchar_t ch)
     346void kio_push_char(const char32_t ch)
    347347{
    348348        kio[(kio_start + kio_len) % KIO_LENGTH] = ch;
     
    360360}
    361361
    362 void putwchar(const wchar_t ch)
     362void putuchar(const char32_t ch)
    363363{
    364364        bool ordy = ((stdout) && (stdout->op->write));
     
    377377                 * for possible future output.
    378378                 *
    379                  * The early_putwchar() function is used to output
     379                 * The early_putuchar() function is used to output
    380380                 * the character for low-level debugging purposes.
    381381                 * Note that the early_putc() function might be
    382382                 * a no-op on certain hardware configurations.
    383383                 */
    384                 early_putwchar(ch);
     384                early_putuchar(ch);
    385385        }
    386386
  • kernel/generic/src/console/kconsole.c

    r4f663f3e r28a5ebd  
    8686LIST_INITIALIZE(cmd_list);      /**< Command list. */
    8787
    88 static wchar_t history[KCONSOLE_HISTORY][MAX_CMDLINE] = { };
     88static char32_t history[KCONSOLE_HISTORY][MAX_CMDLINE] = { };
    8989static size_t history_pos = 0;
    9090
     
    156156
    157157/** Print count times a character */
    158 _NO_TRACE static void print_cc(wchar_t ch, size_t count)
     158_NO_TRACE static void print_cc(char32_t ch, size_t count)
    159159{
    160160        size_t i;
    161161        for (i = 0; i < count; i++)
    162                 putwchar(ch);
     162                putuchar(ch);
    163163}
    164164
     
    290290}
    291291
    292 _NO_TRACE static cmd_info_t *parse_cmd(const wchar_t *cmdline)
     292_NO_TRACE static cmd_info_t *parse_cmd(const char32_t *cmdline)
    293293{
    294294        size_t start = 0;
     
    331331}
    332332
    333 _NO_TRACE static wchar_t *clever_readline(const char *prompt, indev_t *indev,
     333_NO_TRACE static char32_t *clever_readline(const char *prompt, indev_t *indev,
    334334    char *tmp)
    335335{
     
    337337
    338338        size_t position = 0;
    339         wchar_t *current = history[history_pos];
     339        char32_t *current = history[history_pos];
    340340        current[0] = 0;
    341341
    342342        while (true) {
    343                 wchar_t ch = indev_pop_character(indev);
     343                char32_t ch = indev_pop_character(indev);
    344344
    345345                if (ch == '\n') {
    346346                        /* Enter */
    347                         putwchar(ch);
     347                        putuchar(ch);
    348348                        break;
    349349                }
     
    356356                        if (wstr_remove(current, position - 1)) {
    357357                                position--;
    358                                 putwchar('\b');
     358                                putuchar('\b');
    359359                                printf("%ls ", current + position);
    360360                                print_cc('\b', wstr_length(current) - position + 1);
     
    369369                        for (; (current[position] != 0) && (!isspace(current[position]));
    370370                            position++)
    371                                 putwchar(current[position]);
     371                                putuchar(current[position]);
    372372
    373373                        /*
     
    464464                        /* Left */
    465465                        if (position > 0) {
    466                                 putwchar('\b');
     466                                putuchar('\b');
    467467                                position--;
    468468                        }
     
    473473                        /* Right */
    474474                        if (position < wstr_length(current)) {
    475                                 putwchar(current[position]);
     475                                putuchar(current[position]);
    476476                                position++;
    477477                        }
     
    646646        size_t offset = *start;
    647647        size_t prev = *start;
    648         wchar_t ch;
     648        char32_t ch;
    649649
    650650        while ((ch = str_decode(cmdline, &offset, size)) != 0) {
     
    825825
    826826        while (true) {
    827                 wchar_t *tmp = clever_readline((char *) prompt, stdin, buffer);
     827                char32_t *tmp = clever_readline((char *) prompt, stdin, buffer);
    828828                size_t len = wstr_length(tmp);
    829829                if (!len)
  • kernel/generic/src/console/prompt.c

    r4f663f3e r28a5ebd  
    5656
    5757        while (true) {
    58                 wchar_t answer = indev_pop_character(indev);
     58                char32_t answer = indev_pop_character(indev);
    5959
    6060                if ((answer == 'y') || (answer == 'Y')) {
     
    8787        printf("--More--");
    8888        while (true) {
    89                 wchar_t continue_showing_hints = indev_pop_character(indev);
     89                char32_t continue_showing_hints = indev_pop_character(indev);
    9090                /* Display a full page again? */
    9191                if ((continue_showing_hints == 'y') ||
  • kernel/generic/src/lib/str.c

    r4f663f3e r28a5ebd  
    4242 * strings, called just strings are encoded in UTF-8. Wide strings (encoded
    4343 * in UTF-32) are supported to a limited degree. A single character is
    44  * represented as wchar_t.@n
     44 * represented as char32_t.@n
    4545 *
    4646 * Overview of the terminology:@n
     
    5050 *  byte                  8 bits stored in uint8_t (unsigned 8 bit integer)
    5151 *
    52  *  character             UTF-32 encoded Unicode character, stored in wchar_t
    53  *                        (signed 32 bit integer), code points 0 .. 1114111
     52 *  character             UTF-32 encoded Unicode character, stored in char32_t
     53 *                        (unsigned 32 bit integer), code points 0 .. 1114111
    5454 *                        are valid
    5555 *
     
    6161 *
    6262 *  wide string           UTF-32 encoded NULL-terminated Unicode string,
    63  *                        wchar_t *
     63 *                        char32_t *
    6464 *
    6565 *  [wide] string size    number of BYTES in a [wide] string (excluding
     
    100100 * A specific character inside a [wide] string can be referred to by:@n
    101101 *
    102  *  pointer (char *, wchar_t *)
     102 *  pointer (char *, char32_t *)
    103103 *  byte offset (size_t)
    104104 *  character index (size_t)
     
    118118#include <macros.h>
    119119
    120 /** Check the condition if wchar_t is signed */
    121 #ifdef __WCHAR_UNSIGNED__
    122 #define WCHAR_SIGNED_CHECK(cond)  (true)
    123 #else
    124 #define WCHAR_SIGNED_CHECK(cond)  (cond)
    125 #endif
    126 
    127120/** Byte mask consisting of lowest @n bits (out of 8) */
    128121#define LO_MASK_8(n)  ((uint8_t) ((1 << (n)) - 1))
     
    152145 *
    153146 */
    154 wchar_t str_decode(const char *str, size_t *offset, size_t size)
     147char32_t str_decode(const char *str, size_t *offset, size_t size)
    155148{
    156149        if (*offset + 1 > size)
     
    189182                return U_SPECIAL;
    190183
    191         wchar_t ch = b0 & LO_MASK_8(b0_bits);
     184        char32_t ch = b0 & LO_MASK_8(b0_bits);
    192185
    193186        /* Decode continuation bytes */
     
    200193
    201194                /* Shift data bits to ch */
    202                 ch = (ch << CONT_BITS) | (wchar_t) (b & LO_MASK_8(CONT_BITS));
     195                ch = (ch << CONT_BITS) | (char32_t) (b & LO_MASK_8(CONT_BITS));
    203196                cbytes--;
    204197        }
     
    222215 *         code was invalid.
    223216 */
    224 errno_t chr_encode(const wchar_t ch, char *str, size_t *offset, size_t size)
     217errno_t chr_encode(const char32_t ch, char *str, size_t *offset, size_t size)
    225218{
    226219        if (*offset >= size)
     
    308301 *
    309302 */
    310 size_t wstr_size(const wchar_t *str)
    311 {
    312         return (wstr_length(str) * sizeof(wchar_t));
     303size_t wstr_size(const char32_t *str)
     304{
     305        return (wstr_length(str) * sizeof(char32_t));
    313306}
    314307
     
    354347 *
    355348 */
    356 size_t wstr_lsize(const wchar_t *str, size_t max_len)
    357 {
    358         return (wstr_nlength(str, max_len * sizeof(wchar_t)) * sizeof(wchar_t));
     349size_t wstr_lsize(const char32_t *str, size_t max_len)
     350{
     351        return (wstr_nlength(str, max_len * sizeof(char32_t)) * sizeof(char32_t));
    359352}
    360353
     
    384377 *
    385378 */
    386 size_t wstr_length(const wchar_t *wstr)
     379size_t wstr_length(const char32_t *wstr)
    387380{
    388381        size_t len = 0;
     
    421414 *
    422415 */
    423 size_t wstr_nlength(const wchar_t *str, size_t size)
     416size_t wstr_nlength(const char32_t *str, size_t size)
    424417{
    425418        size_t len = 0;
    426         size_t limit = ALIGN_DOWN(size, sizeof(wchar_t));
     419        size_t limit = ALIGN_DOWN(size, sizeof(char32_t));
    427420        size_t offset = 0;
    428421
    429422        while ((offset < limit) && (*str++ != 0)) {
    430423                len++;
    431                 offset += sizeof(wchar_t);
     424                offset += sizeof(char32_t);
    432425        }
    433426
     
    440433 *
    441434 */
    442 bool ascii_check(wchar_t ch)
    443 {
    444         if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 127))
     435bool ascii_check(char32_t ch)
     436{
     437        if (ch <= 127)
    445438                return true;
    446439
     
    453446 *
    454447 */
    455 bool chr_check(wchar_t ch)
    456 {
    457         if (WCHAR_SIGNED_CHECK(ch >= 0) && (ch <= 1114111))
     448bool chr_check(char32_t ch)
     449{
     450        if (ch <= 1114111)
    458451                return true;
    459452
     
    481474int str_cmp(const char *s1, const char *s2)
    482475{
    483         wchar_t c1 = 0;
    484         wchar_t c2 = 0;
     476        char32_t c1 = 0;
     477        char32_t c2 = 0;
    485478
    486479        size_t off1 = 0;
     
    528521int str_lcmp(const char *s1, const char *s2, size_t max_len)
    529522{
    530         wchar_t c1 = 0;
    531         wchar_t c2 = 0;
     523        char32_t c1 = 0;
     524        char32_t c2 = 0;
    532525
    533526        size_t off1 = 0;
     
    580573        size_t dest_off = 0;
    581574
    582         wchar_t ch;
     575        char32_t ch;
    583576        while ((ch = str_decode(src, &src_off, STR_NO_LIMIT)) != 0) {
    584577                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
     
    613606        size_t dest_off = 0;
    614607
    615         wchar_t ch;
     608        char32_t ch;
    616609        while ((ch = str_decode(src, &src_off, n)) != 0) {
    617610                if (chr_encode(ch, dest, &dest_off, size - 1) != EOK)
     
    628621 * written will always be well-formed.
    629622 *
    630  * @param dest  Destination buffer.
    631  * @param size  Size of the destination buffer.
    632  * @param src   Source wide string.
    633  */
    634 void wstr_to_str(char *dest, size_t size, const wchar_t *src)
    635 {
    636         wchar_t ch;
     623 * @param dest Destination buffer.
     624 * @param size Size of the destination buffer.
     625 * @param src  Source wide string.
     626 */
     627void wstr_to_str(char *dest, size_t size, const char32_t *src)
     628{
     629        char32_t ch;
    637630        size_t src_idx;
    638631        size_t dest_off;
     
    659652 * @return Pointer to character in @a str or NULL if not found.
    660653 */
    661 char *str_chr(const char *str, wchar_t ch)
    662 {
    663         wchar_t acc;
     654char *str_chr(const char *str, char32_t ch)
     655{
     656        char32_t acc;
    664657        size_t off = 0;
    665658        size_t last = 0;
     
    688681 *
    689682 */
    690 bool wstr_linsert(wchar_t *str, wchar_t ch, size_t pos, size_t max_pos)
     683bool wstr_linsert(char32_t *str, char32_t ch, size_t pos, size_t max_pos)
    691684{
    692685        size_t len = wstr_length(str);
     
    716709 *
    717710 */
    718 bool wstr_remove(wchar_t *str, size_t pos)
     711bool wstr_remove(char32_t *str, size_t pos)
    719712{
    720713        size_t len = wstr_length(str);
  • kernel/generic/src/log/log.c

    r4f663f3e r28a5ebd  
    214214}
    215215
    216 static int log_printf_wstr_write(const wchar_t *wstr, size_t size, void *data)
     216static int log_printf_wstr_write(const char32_t *wstr, size_t size, void *data)
    217217{
    218218        char buffer[16];
     
    220220        size_t chars = 0;
    221221
    222         for (offset = 0; offset < size; offset += sizeof(wchar_t), chars++) {
     222        for (offset = 0; offset < size; offset += sizeof(char32_t), chars++) {
    223223                kio_push_char(wstr[chars]);
    224224
  • kernel/generic/src/printf/printf_core.c

    r4f663f3e r28a5ebd  
    139139 *
    140140 */
    141 static int printf_wputnchars(const wchar_t *buf, size_t size,
     141static int printf_wputnchars(const char32_t *buf, size_t size,
    142142    printf_spec_t *ps)
    143143{
     
    185185 *
    186186 */
    187 static int printf_putwchar(const wchar_t ch, printf_spec_t *ps)
     187static int printf_putuchar(const char32_t ch, printf_spec_t *ps)
    188188{
    189189        if (!chr_check(ch))
    190190                return ps->str_write((void *) &invalch, 1, ps->data);
    191191
    192         return ps->wstr_write(&ch, sizeof(wchar_t), ps->data);
     192        return ps->wstr_write(&ch, sizeof(char32_t), ps->data);
    193193}
    194194
     
    240240 *
    241241 */
    242 static int print_wchar(const wchar_t ch, int width, uint32_t flags, printf_spec_t *ps)
     242static int print_wchar(const char32_t ch, int width, uint32_t flags, printf_spec_t *ps)
    243243{
    244244        size_t counter = 0;
     
    254254        }
    255255
    256         if (printf_putwchar(ch, ps) > 0)
     256        if (printf_putuchar(ch, ps) > 0)
    257257                counter++;
    258258
     
    326326 * @return Number of wide characters printed, negative value on failure.
    327327 */
    328 static int print_wstr(wchar_t *str, int width, unsigned int precision,
     328static int print_wstr(char32_t *str, int width, unsigned int precision,
    329329    uint32_t flags, printf_spec_t *ps)
    330330{
     
    576576 *  - "l"  Signed or unsigned long int.@n
    577577 *         If conversion is "c", the character is wint_t (wide character).@n
    578  *         If conversion is "s", the string is wchar_t * (wide string).@n
     578 *         If conversion is "s", the string is char32_t * (UTF-32 string).@n
    579579 *  - "ll" Signed or unsigned long long int.@n
    580580 *  - "z"  Signed or unsigned ssize_t or site_t.@n
     
    630630        while (true) {
    631631                i = nxt;
    632                 wchar_t uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
     632                char32_t uc = str_decode(fmt, &nxt, STR_NO_LIMIT);
    633633
    634634                if (uc == 0)
     
    789789                        case 's':
    790790                                if (qualifier == PrintfQualifierLong)
    791                                         retval = print_wstr(va_arg(ap, wchar_t *), width, precision, flags, ps);
     791                                        retval = print_wstr(va_arg(ap, char32_t *), width, precision, flags, ps);
    792792                                else
    793793                                        retval = print_str(va_arg(ap, char *), width, precision, flags, ps);
  • kernel/generic/src/printf/vprintf.c

    r4f663f3e r28a5ebd  
    4747
    4848        while (offset < size) {
    49                 putwchar(str_decode(str, &offset, size));
     49                putuchar(str_decode(str, &offset, size));
    5050                chars++;
    5151        }
     
    5454}
    5555
    56 static int vprintf_wstr_write(const wchar_t *str, size_t size, void *data)
     56static int vprintf_wstr_write(const char32_t *str, size_t size, void *data)
    5757{
    5858        size_t offset = 0;
     
    6060
    6161        while (offset < size) {
    62                 putwchar(str[chars]);
     62                putuchar(str[chars]);
    6363                chars++;
    64                 offset += sizeof(wchar_t);
     64                offset += sizeof(char32_t);
    6565        }
    6666
     
    7272        size_t offset = 0;
    7373        size_t chars = 0;
    74         wchar_t uc;
     74        char32_t uc;
    7575
    7676        while ((uc = str_decode(str, &offset, STR_NO_LIMIT)) != 0) {
    77                 putwchar(uc);
     77                putuchar(uc);
    7878                chars++;
    7979        }
    8080
    81         putwchar('\n');
     81        putuchar('\n');
    8282        return chars;
    8383}
  • kernel/generic/src/printf/vsnprintf.c

    r4f663f3e r28a5ebd  
    8888
    8989                while (index < size) {
    90                         wchar_t uc = str_decode(str, &index, size);
     90                        char32_t uc = str_decode(str, &index, size);
    9191
    9292                        if (chr_encode(uc, data->dst, &data->len, data->size - 1) != EOK)
     
    133133 *
    134134 */
    135 static int vsnprintf_wstr_write(const wchar_t *str, size_t size, vsnprintf_data_t *data)
     135static int vsnprintf_wstr_write(const char32_t *str, size_t size, vsnprintf_data_t *data)
    136136{
    137137        size_t index = 0;
    138138
    139         while (index < (size / sizeof(wchar_t))) {
     139        while (index < (size / sizeof(char32_t))) {
    140140                size_t left = data->size - data->len;
    141141
     
    177177        printf_spec_t ps = {
    178178                (int (*) (const char *, size_t, void *)) vsnprintf_str_write,
    179                 (int (*) (const wchar_t *, size_t, void *)) vsnprintf_wstr_write,
     179                (int (*) (const char32_t *, size_t, void *)) vsnprintf_wstr_write,
    180180                &data
    181181        };
Note: See TracChangeset for help on using the changeset viewer.