[b861b58] | 1 | /*
|
---|
[df4ed85] | 2 | * Copyright (c) 2006 Josef Cejka
|
---|
[2595dab] | 3 | * Copyright (c) 2006 Jakub Vana
|
---|
[b861b58] | 4 | * All rights reserved.
|
---|
| 5 | *
|
---|
| 6 | * Redistribution and use in source and binary forms, with or without
|
---|
| 7 | * modification, are permitted provided that the following conditions
|
---|
| 8 | * are met:
|
---|
| 9 | *
|
---|
| 10 | * - Redistributions of source code must retain the above copyright
|
---|
| 11 | * notice, this list of conditions and the following disclaimer.
|
---|
| 12 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 13 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 14 | * documentation and/or other materials provided with the distribution.
|
---|
| 15 | * - The name of the author may not be used to endorse or promote products
|
---|
| 16 | * derived from this software without specific prior written permission.
|
---|
| 17 | *
|
---|
| 18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
[4e2cf8b] | 28 | */
|
---|
[b861b58] | 29 |
|
---|
[fadd381] | 30 | /** @addtogroup libc
|
---|
[b2951e2] | 31 | * @{
|
---|
| 32 | */
|
---|
| 33 | /** @file
|
---|
| 34 | */
|
---|
| 35 |
|
---|
[582a0b8] | 36 | #include <stddef.h>
|
---|
[2595dab] | 37 | #include <libc.h>
|
---|
[19f857a] | 38 | #include <str.h>
|
---|
[8d2dd7f2] | 39 | #include <stdint.h>
|
---|
[d8de5d3] | 40 | #include <errno.h>
|
---|
[6fa9a99d] | 41 | #include <abi/kio.h>
|
---|
| 42 | #include <io/kio.h>
|
---|
[d8de5d3] | 43 | #include <io/printf_core.h>
|
---|
[350514c] | 44 |
|
---|
[b7fd2a0] | 45 | errno_t kio_write(const void *buf, size_t size, size_t *nwritten)
|
---|
[2595dab] | 46 | {
|
---|
[b7fd2a0] | 47 | errno_t rc = (errno_t) __SYSCALL3(SYS_KIO, KIO_WRITE, (sysarg_t) buf, size);
|
---|
[a35b458] | 48 |
|
---|
[6afc9d7] | 49 | if (rc == EOK)
|
---|
| 50 | *nwritten = size;
|
---|
| 51 | return rc;
|
---|
[2595dab] | 52 | }
|
---|
[b861b58] | 53 |
|
---|
[6fa9a99d] | 54 | void kio_update(void)
|
---|
[2595dab] | 55 | {
|
---|
[6fa9a99d] | 56 | (void) __SYSCALL3(SYS_KIO, KIO_UPDATE, (uintptr_t) NULL, 0);
|
---|
[2595dab] | 57 | }
|
---|
[b2951e2] | 58 |
|
---|
[6fa9a99d] | 59 | void kio_command(const void *buf, size_t size)
|
---|
[ec85df0] | 60 | {
|
---|
[6fa9a99d] | 61 | (void) __SYSCALL3(SYS_KIO, KIO_COMMAND, (sysarg_t) buf, (sysarg_t) size);
|
---|
[ec85df0] | 62 | }
|
---|
| 63 |
|
---|
[6fa9a99d] | 64 | /** Print formatted text to kio.
|
---|
[d8de5d3] | 65 | *
|
---|
| 66 | * @param fmt Format string
|
---|
| 67 | *
|
---|
| 68 | * \see For more details about format string see printf_core.
|
---|
| 69 | *
|
---|
| 70 | */
|
---|
[6fa9a99d] | 71 | int kio_printf(const char *fmt, ...)
|
---|
[d8de5d3] | 72 | {
|
---|
| 73 | va_list args;
|
---|
| 74 | va_start(args, fmt);
|
---|
[a35b458] | 75 |
|
---|
[6fa9a99d] | 76 | int ret = kio_vprintf(fmt, args);
|
---|
[a35b458] | 77 |
|
---|
[d8de5d3] | 78 | va_end(args);
|
---|
[a35b458] | 79 |
|
---|
[d8de5d3] | 80 | return ret;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[6fa9a99d] | 83 | static int kio_vprintf_str_write(const char *str, size_t size, void *data)
|
---|
[d8de5d3] | 84 | {
|
---|
[6afc9d7] | 85 | size_t wr;
|
---|
[a35b458] | 86 |
|
---|
[6afc9d7] | 87 | wr = 0;
|
---|
| 88 | (void) kio_write(str, size, &wr);
|
---|
[d8de5d3] | 89 | return str_nlength(str, wr);
|
---|
| 90 | }
|
---|
| 91 |
|
---|
[6fa9a99d] | 92 | static int kio_vprintf_wstr_write(const wchar_t *str, size_t size, void *data)
|
---|
[d8de5d3] | 93 | {
|
---|
| 94 | size_t offset = 0;
|
---|
| 95 | size_t chars = 0;
|
---|
[6afc9d7] | 96 | size_t wr;
|
---|
[a35b458] | 97 |
|
---|
[d8de5d3] | 98 | while (offset < size) {
|
---|
| 99 | char buf[STR_BOUNDS(1)];
|
---|
| 100 | size_t sz = 0;
|
---|
[a35b458] | 101 |
|
---|
[d8de5d3] | 102 | if (chr_encode(str[chars], buf, &sz, STR_BOUNDS(1)) == EOK)
|
---|
[6afc9d7] | 103 | kio_write(buf, sz, &wr);
|
---|
[a35b458] | 104 |
|
---|
[d8de5d3] | 105 | chars++;
|
---|
| 106 | offset += sizeof(wchar_t);
|
---|
| 107 | }
|
---|
[a35b458] | 108 |
|
---|
[d8de5d3] | 109 | return chars;
|
---|
| 110 | }
|
---|
| 111 |
|
---|
[6fa9a99d] | 112 | /** Print formatted text to kio.
|
---|
[d8de5d3] | 113 | *
|
---|
| 114 | * @param fmt Format string
|
---|
| 115 | * @param ap Format parameters
|
---|
| 116 | *
|
---|
| 117 | * \see For more details about format string see printf_core.
|
---|
| 118 | *
|
---|
| 119 | */
|
---|
[6fa9a99d] | 120 | int kio_vprintf(const char *fmt, va_list ap)
|
---|
[d8de5d3] | 121 | {
|
---|
| 122 | printf_spec_t ps = {
|
---|
[6fa9a99d] | 123 | kio_vprintf_str_write,
|
---|
| 124 | kio_vprintf_wstr_write,
|
---|
[d8de5d3] | 125 | NULL
|
---|
| 126 | };
|
---|
[a35b458] | 127 |
|
---|
[d8de5d3] | 128 | return printf_core(fmt, &ps, ap);
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[fadd381] | 131 | /** @}
|
---|
[b2951e2] | 132 | */
|
---|