Changeset 163e34c in mainline


Ignore:
Timestamp:
2025-04-13T19:33:48Z (4 days ago)
Author:
Jiří Zárevúcky <zarevucky.jiri@…>
Branches:
master
Children:
f5e1692
Parents:
97f6b71
git-author:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-13 18:56:51)
git-committer:
Jiří Zárevúcky <zarevucky.jiri@…> (2025-04-13 19:33:48)
Message:

Actually convert the printf outputs everywhere

Files:
3 deleted
11 edited
2 moved

Legend:

Unmodified
Added
Removed
  • common/include/printf_core.h

    r97f6b71 r163e34c  
    3636#define _LIBC_PRINTF_CORE_H_
    3737
     38#include <errno.h>
     39#include <stdarg.h>
    3840#include <stddef.h>
    39 #include <stdarg.h>
    4041#include <uchar.h>
    4142
    4243/** Structure for specifying output methods for different printf clones. */
    4344typedef struct {
    44         /* String output function, returns number of printed characters or EOF */
    45         int (*str_write)(const char *, size_t, void *);
    46 
    47         /* Wide string output function, returns number of printed characters or EOF */
    48         int (*wstr_write)(const char32_t *, size_t, void *);
     45        /*
     46         * String output function, returns EOK on success.
     47         * Only returns an error when an irrecoverable failure occurs and
     48         * the string cannot be fully output.
     49         */
     50        errno_t (*write)(const char *, size_t, void *);
    4951
    5052        /* User data - output stream specification, state, locks, etc. */
  • common/printf/printf_core.c

    r97f6b71 r163e34c  
    201201    size_t *written_bytes)
    202202{
    203         int written = ps->str_write(buf, n, ps->data);
    204         if (written < 0)
    205                 return EIO;
     203        errno_t rc = ps->write(buf, n, ps->data);
     204        if (rc != EOK)
     205                return rc;
     206
    206207        _saturating_add(written_bytes, n);
    207208        return EOK;
    208 
    209         #if 0
    210         errno_t rc = ps->write(buf, &n, ps->data);
    211         _saturating_add(written_bytes, n);
    212         return rc;
    213         #endif
    214209}
    215210
     
    14661461
    14671462                rc = _format_number(number, width, precision, base, flags, ps, &counter);
    1468                 if (rc != EOK)
    1469                         continue;
    14701463        }
    14711464
  • common/stdc/vsnprintf.c

    r97f6b71 r163e34c  
    11/*
    22 * Copyright (c) 2006 Josef Cejka
     3 * Copyright (c) 2025 Jiří Zárevúcky
    34 * All rights reserved.
    45 *
     
    2728 */
    2829
    29 /** @addtogroup kernel_generic
     30/** @addtogroup libc
    3031 * @{
    3132 */
     
    3334 */
    3435
    35 #ifndef KERN_PRINTF_CORE_H_
    36 #define KERN_PRINTF_CORE_H_
     36#include <errno.h>
     37#include <macros.h>
     38#include <printf_core.h>
     39#include <stdarg.h>
     40#include <stdio.h>
     41#include <str.h>
    3742
    38 #include <stdarg.h>
    39 #include <stddef.h>
    40 #include <uchar.h>
     43typedef struct {
     44        char *dst;      /* Destination */
     45        size_t left;
     46} vsnprintf_data_t;
    4147
    42 /** Structure for specifying output methods for different printf clones. */
    43 typedef struct {
    44         /* String output function, returns number of printed characters or EOF */
    45         int (*str_write)(const char *, size_t, void *);
     48static int vsnprintf_str_write(const char *str, size_t size, void *data)
     49{
     50        vsnprintf_data_t *d = data;
     51        size_t left = min(size, d->left);
     52        if (left > 0) {
     53                memcpy(d->dst, str, left);
     54                d->dst += left;
     55                d->left -= left;
     56        }
     57        return EOK;
     58}
    4659
    47         /* Wide string output function, returns number of printed characters or EOF */
    48         int (*wstr_write)(const char32_t *, size_t, void *);
     60int vsnprintf(char *str, size_t size, const char *fmt, va_list ap)
     61{
     62        vsnprintf_data_t data = {
     63                .dst = str,
     64                .left = size ? size - 1 : 0,
     65        };
    4966
    50         /* User data - output stream specification, state, locks, etc. */
    51         void *data;
    52 } printf_spec_t;
     67        printf_spec_t ps = {
     68                vsnprintf_str_write,
     69                &data
     70        };
    5371
    54 extern int printf_core(const char *fmt, printf_spec_t *ps, va_list ap);
     72        int written = printf_core(fmt, &ps, ap);
     73        if (written < 0)
     74                return written;
    5575
    56 #endif
     76        /* Write the terminating NUL character. */
     77        if (size > 0)
     78                data.dst[0] = 0;
     79
     80        return written;
     81}
    5782
    5883/** @}
  • kernel/generic/meson.build

    r97f6b71 r163e34c  
    4040        'common/adt/list.c',
    4141        'common/adt/odict.c',
     42        'common/gsort.c',
    4243        'common/printf/printf_core.c',
    4344        'common/stdc/calloc.c',
    4445        'common/stdc/ctype.c',
    4546        'common/stdc/mem.c',
    46         'common/gsort.c',
     47        'common/stdc/snprintf.c',
     48        'common/stdc/vsnprintf.c',
    4749        'common/str.c',
    4850        'common/str_error.c',
     
    9698        'src/mm/reserve.c',
    9799        'src/printf/printf.c',
    98         'src/printf/snprintf.c',
    99100        'src/printf/vprintf.c',
    100         'src/printf/vsnprintf.c',
    101101        'src/proc/program.c',
    102102        'src/proc/scheduler.c',
  • kernel/generic/src/log/log.c

    r97f6b71 r163e34c  
    3333 */
    3434
    35 #include <sysinfo/sysinfo.h>
    36 #include <synch/spinlock.h>
    37 #include <typedefs.h>
     35#include <abi/log.h>
     36#include <arch.h>
     37#include <atomic.h>
     38#include <console/console.h>
     39#include <ddi/ddi.h>
    3840#include <ddi/irq.h>
    39 #include <ddi/ddi.h>
     41#include <errno.h>
    4042#include <ipc/event.h>
    4143#include <ipc/irq.h>
    42 #include <arch.h>
     44#include <log.h>
    4345#include <panic.h>
     46#include <print.h>
     47#include <printf_core.h>
    4448#include <putchar.h>
    45 #include <atomic.h>
     49#include <stdarg.h>
     50#include <stdlib.h>
     51#include <str.h>
     52#include <synch/spinlock.h>
    4653#include <syscall/copy.h>
    47 #include <errno.h>
    48 #include <str.h>
    49 #include <print.h>
    50 #include <printf/printf_core.h>
    51 #include <stdarg.h>
    52 #include <log.h>
    53 #include <console/console.h>
    54 #include <abi/log.h>
    55 #include <stdlib.h>
     54#include <sysinfo/sysinfo.h>
     55#include <typedefs.h>
    5656
    5757#define LOG_PAGES    8
     
    204204{
    205205        size_t offset = 0;
    206         size_t chars = 0;
    207 
    208         while (offset < size) {
     206
     207        while (offset < size)
    209208                kio_push_char(str_decode(str, &offset, size));
    210                 chars++;
    211         }
    212209
    213210        log_append((const uint8_t *)str, size);
    214211
    215         return chars;
    216 }
    217 
    218 static int log_printf_wstr_write(const char32_t *wstr, size_t size, void *data)
    219 {
    220         char buffer[16];
    221         size_t offset = 0;
    222         size_t chars = 0;
    223 
    224         for (offset = 0; offset < size; offset += sizeof(char32_t), chars++) {
    225                 kio_push_char(wstr[chars]);
    226 
    227                 size_t buffer_offset = 0;
    228                 errno_t rc = chr_encode(wstr[chars], buffer, &buffer_offset, 16);
    229                 if (rc != EOK) {
    230                         return EOF;
    231                 }
    232 
    233                 log_append((const uint8_t *)buffer, buffer_offset);
    234         }
    235 
    236         return chars;
     212        return EOK;
    237213}
    238214
     
    243219int log_vprintf(const char *fmt, va_list args)
    244220{
    245         int ret;
    246 
    247221        printf_spec_t ps = {
    248222                log_printf_str_write,
    249                 log_printf_wstr_write,
    250223                NULL
    251224        };
    252225
    253         ret = printf_core(fmt, &ps, args);
    254 
    255         return ret;
     226        return printf_core(fmt, &ps, args);
    256227}
    257228
  • kernel/generic/src/printf/vprintf.c

    r97f6b71 r163e34c  
    3636#include <console/console.h>
    3737#include <print.h>
    38 #include <printf/printf_core.h>
     38#include <printf_core.h>
    3939#include <putchar.h>
    4040#include <str.h>
     
    4242#include <typedefs.h>
    4343
    44 static int vprintf_str_write(const char *str, size_t size, void *data)
     44static errno_t vprintf_str_write(const char *str, size_t size, void *data)
    4545{
    4646        size_t offset = 0;
    47         size_t chars = 0;
    4847
    49         while (offset < size) {
     48        while (offset < size)
    5049                putuchar(str_decode(str, &offset, size));
    51                 chars++;
    52         }
    5350
    54         return chars;
    55 }
    56 
    57 static int vprintf_wstr_write(const char32_t *str, size_t size, void *data)
    58 {
    59         size_t offset = 0;
    60         size_t chars = 0;
    61 
    62         while (offset < size) {
    63                 putuchar(str[chars]);
    64                 chars++;
    65                 offset += sizeof(char32_t);
    66         }
    67 
    68         return chars;
     51        return EOK;
    6952}
    7053
     
    9275        printf_spec_t ps = {
    9376                vprintf_str_write,
    94                 vprintf_wstr_write,
    9577                NULL
    9678        };
  • uspace/lib/c/generic/io/asprintf.c

    r97f6b71 r163e34c  
    4040#include <str.h>
    4141#include <printf_core.h>
    42 
    43 static int asprintf_str_write(const char *str, size_t count, void *unused)
    44 {
    45         return str_nlength(str, count);
    46 }
    47 
    48 static int asprintf_wstr_write(const char32_t *str, size_t count, void *unused)
    49 {
    50         return wstr_nlength(str, count);
    51 }
    52 
    53 int vprintf_length(const char *fmt, va_list args)
    54 {
    55         printf_spec_t ps = {
    56                 asprintf_str_write,
    57                 asprintf_wstr_write,
    58                 NULL
    59         };
    60 
    61         return printf_core(fmt, &ps, args);
    62 }
    63 
    64 int printf_length(const char *fmt, ...)
    65 {
    66         va_list args;
    67         va_start(args, fmt);
    68         int ret = vprintf_length(fmt, args);
    69         va_end(args);
    70 
    71         return ret;
    72 }
    7342
    7443/** Allocate and print to string.
     
    11584        int ret = vasprintf(strp, fmt, args);
    11685        va_end(args);
    117 
    11886        return ret;
    11987}
  • uspace/lib/c/generic/io/kio.c

    r97f6b71 r163e34c  
    131131}
    132132
    133 static int kio_vprintf_str_write(const char *str, size_t size, void *data)
     133static errno_t kio_vprintf_str_write(const char *str, size_t size, void *data)
    134134{
    135         size_t wr;
    136 
    137         wr = 0;
    138         (void) kio_write(str, size, &wr);
    139         return str_nlength(str, wr);
    140 }
    141 
    142 static int kio_vprintf_wstr_write(const char32_t *str, size_t size, void *data)
    143 {
    144         size_t offset = 0;
    145         size_t chars = 0;
    146         size_t wr;
    147 
    148         while (offset < size) {
    149                 char buf[STR_BOUNDS(1)];
    150                 size_t sz = 0;
    151 
    152                 if (chr_encode(str[chars], buf, &sz, STR_BOUNDS(1)) == EOK)
    153                         kio_write(buf, sz, &wr);
    154 
    155                 chars++;
    156                 offset += sizeof(char32_t);
    157         }
    158 
    159         return chars;
     135        size_t wr = 0;
     136        return kio_write(str, size, &wr);
    160137}
    161138
     
    172149        printf_spec_t ps = {
    173150                kio_vprintf_str_write,
    174                 kio_vprintf_wstr_write,
    175151                NULL
    176152        };
  • uspace/lib/c/generic/io/vprintf.c

    r97f6b71 r163e34c  
    4242static FIBRIL_MUTEX_INITIALIZE(printf_mutex);
    4343
    44 static int vprintf_str_write(const char *str, size_t size, void *stream)
     44static errno_t vprintf_str_write(const char *str, size_t size, void *stream)
    4545{
    46         size_t wr = fwrite(str, 1, size, (FILE *) stream);
    47         return str_nlength(str, wr);
    48 }
     46        errno_t old_errno = errno;
    4947
    50 static int vprintf_wstr_write(const char32_t *str, size_t size, void *stream)
    51 {
    52         size_t offset = 0;
    53         size_t chars = 0;
     48        errno = EOK;
     49        size_t written = fwrite(str, 1, size, (FILE *) stream);
    5450
    55         while (offset < size) {
    56                 if (fputuc(str[chars], (FILE *) stream) <= 0)
    57                         break;
     51        if (errno == EOK && written != size)
     52                errno = EIO;
    5853
    59                 chars++;
    60                 offset += sizeof(char32_t);
    61         }
     54        if (errno != EOK)
     55                return errno;
    6256
    63         return chars;
     57        errno = old_errno;
     58        return EOK;
    6459}
    6560
     
    7772        printf_spec_t ps = {
    7873                vprintf_str_write,
    79                 vprintf_wstr_write,
    8074                stream
    8175        };
  • uspace/lib/c/include/stdio.h

    r97f6b71 r163e34c  
    209209};
    210210
    211 extern int vprintf_length(const char *, va_list);
    212 extern int printf_length(const char *, ...)
    213     _HELENOS_PRINTF_ATTRIBUTE(1, 2);
    214211extern FILE *fdopen(int, const char *);
    215212extern int fileno(FILE *);
  • uspace/lib/c/meson.build

    r97f6b71 r163e34c  
    7272        'common/stdc/mem.c',
    7373        'common/stdc/qsort.c',
     74        'common/stdc/snprintf.c',
    7475        'common/stdc/uchar.c',
     76        'common/stdc/vsnprintf.c',
    7577        'common/stdc/wchar.c',
    7678        'common/str.c',
     
    113115        'generic/io/logctl.c',
    114116        'generic/io/printf.c',
    115         'generic/io/snprintf.c',
    116117        'generic/io/table.c',
    117118        'generic/io/vprintf.c',
    118         'generic/io/vsnprintf.c',
    119119        'generic/ipc.c',
    120120        'generic/irq.c',
  • uspace/lib/posix/src/stdio.c

    r97f6b71 r163e34c  
    232232 * @return The number of written characters.
    233233 */
    234 static int _dprintf_str_write(const char *str, size_t size, void *fd)
     234static errno_t _dprintf_str_write(const char *str, size_t size, void *fd)
    235235{
    236236        const int fildes = *(int *) fd;
    237237        size_t wr;
    238         if (failed(vfs_write(fildes, &posix_pos[fildes], str, size, &wr)))
    239                 return -1;
    240         return str_nlength(str, wr);
    241 }
    242 
    243 /**
    244  * Write wide string to the opened file.
    245  *
    246  * @param str String to be written.
    247  * @param size Size of the string (in bytes).
    248  * @param fd File descriptor of the opened file.
    249  * @return The number of written characters.
    250  */
    251 static int _dprintf_wstr_write(const char32_t *str, size_t size, void *fd)
    252 {
    253         size_t offset = 0;
    254         size_t chars = 0;
    255         size_t sz;
    256         char buf[4];
    257 
    258         while (offset < size) {
    259                 sz = 0;
    260                 if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) {
    261                         break;
    262                 }
    263 
    264                 const int fildes = *(int *) fd;
    265                 size_t nwr;
    266                 if (vfs_write(fildes, &posix_pos[fildes], buf, sz, &nwr) != EOK)
    267                         break;
    268 
    269                 chars++;
    270                 offset += sizeof(char32_t);
    271         }
    272 
    273         return chars;
     238        return vfs_write(fildes, &posix_pos[fildes], str, size, &wr);
    274239}
    275240
     
    285250{
    286251        printf_spec_t spec = {
    287                 .str_write = _dprintf_str_write,
    288                 .wstr_write = _dprintf_wstr_write,
     252                .write = _dprintf_str_write,
    289253                .data = &fildes
    290254        };
Note: See TracChangeset for help on using the changeset viewer.