diff options
author | Richard Braun <rbraun@sceen.net> | 2018-01-05 01:58:08 +0100 |
---|---|---|
committer | Richard Braun <rbraun@sceen.net> | 2018-01-05 01:58:26 +0100 |
commit | 62d27d872769ebcd79a931fa07b1128576e65e65 (patch) | |
tree | 0a99e87809c9055dcdcfbd9ed4bec1a72534ced9 | |
parent | a6c056ce150bd0339d5325edc18717b2d02a5b8d (diff) |
error: remove and use standard errno codes instead
This change also adds strerror to string.h.
-rw-r--r-- | Makefile | 1 | ||||
-rw-r--r-- | include/errno.h (renamed from src/error.h) | 37 | ||||
-rw-r--r-- | include/string.h | 1 | ||||
-rw-r--r-- | lib/cbuf.c | 15 | ||||
-rw-r--r-- | lib/cbuf.h | 12 | ||||
-rw-r--r-- | lib/fmt.c | 23 | ||||
-rw-r--r-- | lib/shell.c | 32 | ||||
-rw-r--r-- | lib/shell.h | 9 | ||||
-rw-r--r-- | src/cpu.c | 1 | ||||
-rw-r--r-- | src/error.c | 67 | ||||
-rw-r--r-- | src/i8259.c | 4 | ||||
-rw-r--r-- | src/mutex.c | 4 | ||||
-rw-r--r-- | src/mutex.h | 2 | ||||
-rw-r--r-- | src/string.c | 24 | ||||
-rw-r--r-- | src/thread.c | 6 | ||||
-rw-r--r-- | src/uart.c | 4 | ||||
-rw-r--r-- | src/uart.h | 2 |
17 files changed, 93 insertions, 151 deletions
@@ -183,7 +183,6 @@ SOURCES = \ src/condvar.c \ src/cpu.c \ src/cpu_asm.S \ - src/error.c \ src/i8254.c \ src/i8259.c \ src/io_asm.S \ diff --git a/src/error.h b/include/errno.h index fca3980..d935f1c 100644 --- a/src/error.h +++ b/include/errno.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2017 Richard Braun. + * Copyright (c) 2017-2018 Richard Braun. * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), @@ -20,31 +20,14 @@ * DEALINGS IN THE SOFTWARE. */ -#ifndef _ERROR_H -#define _ERROR_H +#ifndef _ERRNO_H +#define _ERRNO_H -enum { - ERROR_INVAL = 1, - ERROR_AGAIN, - ERROR_NOMEM, - ERROR_IO, - ERROR_BUSY, - ERROR_EXIST, -}; +#define EINVAL 1 +#define EAGAIN 2 +#define ENOMEM 3 +#define EIO 4 +#define EBUSY 5 +#define EEXIST 6 -/* - * Return the message matching the given error. - * - * The returned address points to a statically allocated, read only, - * null-terminated string literal. The caller must not attempt to use it - * for anything else than error reporting. - */ -const char * error_str(unsigned int error); - -/* - * If error denotes an actual error (i.e. is not 0), panic, using the given - * string as a prefix for the error message. A NULL prefix is allowed. - */ -void error_check(int error, const char *prefix); - -#endif /* _ERROR_H */ +#endif /* _ERRNO_H */ diff --git a/include/string.h b/include/string.h index 3f7c87e..fc17482 100644 --- a/include/string.h +++ b/include/string.h @@ -31,5 +31,6 @@ char * strcpy(char *dest, const char *src); size_t strlen(const char *s); int strcmp(const char *s1, const char *s2); int strncmp(const char *s1, const char *s2, size_t n); +char * strerror(int errnum); #endif /* _STRING_H */ @@ -24,6 +24,7 @@ */ #include <assert.h> +#include <errno.h> #include <stddef.h> #include <stdint.h> #include <string.h> @@ -31,8 +32,6 @@ #include <lib/cbuf.h> #include <lib/macros.h> -#include <src/error.h> - /* Negative close to 0 so that an overflow occurs early */ #define CBUF_INIT_INDEX ((size_t)-500) @@ -71,7 +70,7 @@ cbuf_push(struct cbuf *cbuf, const void *buf, size_t size, bool erase) free_size = cbuf_capacity(cbuf) - cbuf_size(cbuf); if (size > free_size) { - return ERROR_AGAIN; + return EAGAIN; } } @@ -84,7 +83,7 @@ cbuf_pop(struct cbuf *cbuf, void *buf, size_t *sizep) __unused int error; if (cbuf_size(cbuf) == 0) { - return ERROR_AGAIN; + return EAGAIN; } error = cbuf_read(cbuf, cbuf_start(cbuf), buf, sizep); @@ -102,7 +101,7 @@ cbuf_pushb(struct cbuf *cbuf, uint8_t byte, bool erase) free_size = cbuf_capacity(cbuf) - cbuf_size(cbuf); if (free_size == 0) { - return ERROR_AGAIN; + return EAGAIN; } } @@ -118,7 +117,7 @@ cbuf_popb(struct cbuf *cbuf, void *bytep) uint8_t *ptr; if (cbuf_size(cbuf) == 0) { - return ERROR_AGAIN; + return EAGAIN; } ptr = bytep; @@ -134,7 +133,7 @@ cbuf_write(struct cbuf *cbuf, size_t index, const void *buf, size_t size) size_t new_end, skip; if (!cbuf_range_valid(cbuf, index, cbuf->end)) { - return ERROR_INVAL; + return EINVAL; } new_end = index + size; @@ -175,7 +174,7 @@ cbuf_read(const struct cbuf *cbuf, size_t index, void *buf, size_t *sizep) /* At least one byte must be available */ if (!cbuf_range_valid(cbuf, index, index + 1)) { - return ERROR_INVAL; + return EINVAL; } size = cbuf->end - index; @@ -96,7 +96,7 @@ void cbuf_init(struct cbuf *cbuf, void *buf, size_t capacity); * Push data to a circular buffer. * * If the function isn't allowed to erase old data and the circular buffer - * doesn't have enough unused bytes for the new data, ERROR_AGAIN is returned. + * doesn't have enough unused bytes for the new data, EAGAIN is returned. */ int cbuf_push(struct cbuf *cbuf, const void *buf, size_t size, bool erase); @@ -106,7 +106,7 @@ int cbuf_push(struct cbuf *cbuf, const void *buf, size_t size, bool erase); * On entry, the sizep argument points to the size of the output buffer. * On exit, it is updated to the number of bytes actually transferred. * - * If the buffer is empty, ERROR_AGAIN is returned, and the size of the + * If the buffer is empty, EAGAIN is returned, and the size of the * output buffer is undefined. */ int cbuf_pop(struct cbuf *cbuf, void *buf, size_t *sizep); @@ -115,21 +115,21 @@ int cbuf_pop(struct cbuf *cbuf, void *buf, size_t *sizep); * Push a byte to a circular buffer. * * If the function isn't allowed to erase old data and the circular buffer - * is full, ERROR_AGAIN is returned. + * is full, EAGAIN is returned. */ int cbuf_pushb(struct cbuf *cbuf, uint8_t byte, bool erase); /* * Pop a byte from a circular buffer. * - * If the buffer is empty, ERROR_AGAIN is returned. + * If the buffer is empty, EAGAIN is returned. */ int cbuf_popb(struct cbuf *cbuf, void *bytep); /* * Write into a circular buffer at a specific location. * - * If the given index is outside buffer boundaries, ERROR_INVAL is returned. + * If the given index is outside buffer boundaries, EINVAL is returned. * The given [index, size) range may extend beyond the end of the circular * buffer. */ @@ -141,7 +141,7 @@ int cbuf_write(struct cbuf *cbuf, size_t index, const void *buf, size_t size); * On entry, the sizep argument points to the size of the output buffer. * On exit, it is updated to the number of bytes actually transferred. * - * If the given index is outside buffer boundaries, ERROR_INVAL is returned. + * If the given index is outside buffer boundaries, EINVAL is returned. * * The circular buffer isn't changed by this operation. */ @@ -24,6 +24,7 @@ */ #include <assert.h> +#include <errno.h> #include <limits.h> #include <stdbool.h> #include <stdarg.h> @@ -35,8 +36,6 @@ #include <lib/fmt.h> #include <lib/macros.h> -#include <src/error.h> - /* * XXX This type is specified by POSIX. Directly declare it for simplicity. */ @@ -410,12 +409,12 @@ fmt_sprintf_state_consume(struct fmt_sprintf_state *state) c = fmt_consume(&state->format); if (c == '\0') { - return ERROR_IO; + return EIO; } if (c != '%') { fmt_sprintf_state_produce_raw_char(state, c); - return ERROR_AGAIN; + return EAGAIN; } fmt_sprintf_state_consume_flags(state); @@ -794,7 +793,7 @@ fmt_vsnprintf(char *str, size_t size, const char *format, va_list ap) for (;;) { error = fmt_sprintf_state_consume(&state); - if (error == ERROR_AGAIN) { + if (error == EAGAIN) { continue; } else if (error) { break; @@ -1075,7 +1074,7 @@ fmt_sscanf_state_discard_char(struct fmt_sscanf_state *state, char c) state->nr_convs = EOF; } - return ERROR_INVAL; + return EINVAL; } return 0; @@ -1092,7 +1091,7 @@ fmt_sscanf_state_consume(struct fmt_sscanf_state *state) c = fmt_consume(&state->format); if (c == '\0') { - return ERROR_IO; + return EIO; } if (c != '%') { @@ -1102,7 +1101,7 @@ fmt_sscanf_state_consume(struct fmt_sscanf_state *state) return error; } - return ERROR_AGAIN; + return EAGAIN; } fmt_sscanf_state_consume_flags(state); @@ -1194,7 +1193,7 @@ fmt_sscanf_state_produce_int(struct fmt_sscanf_state *state) if (i == 0) { if (c == '\0') { fmt_sscanf_state_report_error(state); - return ERROR_INVAL; + return EINVAL; } buf[0] = '0'; @@ -1388,7 +1387,7 @@ fmt_sscanf_state_produce_str(struct fmt_sscanf_state *state) if (state->str == orig) { fmt_sscanf_state_report_error(state); - return ERROR_INVAL; + return EINVAL; } if (dest != NULL) { @@ -1423,7 +1422,7 @@ fmt_sscanf_state_produce(struct fmt_sscanf_state *state) return fmt_sscanf_state_discard_char(state, '%'); default: fmt_sscanf_state_report_error(state); - return ERROR_INVAL; + return EINVAL; } } @@ -1451,7 +1450,7 @@ fmt_vsscanf(const char *str, const char *format, va_list ap) for (;;) { error = fmt_sscanf_state_consume(&state); - if (error == ERROR_AGAIN) { + if (error == EAGAIN) { continue; } else if (error) { break; diff --git a/lib/shell.c b/lib/shell.c index 8272b24..3fada36 100644 --- a/lib/shell.c +++ b/lib/shell.c @@ -23,6 +23,7 @@ * http://git.sceen.net/rbraun/librbraun.git/ */ +#include <errno.h> #include <stddef.h> #include <stdio.h> #include <stdint.h> @@ -32,7 +33,6 @@ #include <lib/hash.h> #include <lib/shell.h> -#include <src/error.h> #include <src/mutex.h> #include <src/panic.h> #include <src/thread.h> @@ -271,8 +271,8 @@ shell_cmd_match(const struct shell_cmd *cmd, const char *str, * eligible for completion. * * If there is a single match for the given string, return 0. If there - * are more than one match, return ERROR_AGAIN. If there is no match, - * return ERROR_INVAL. + * are more than one match, return EAGAIN. If there is no match, + * return EINVAL. * * The global lock must be acquired before calling this function. */ @@ -292,7 +292,7 @@ shell_cmd_complete(const char *str, unsigned long *sizep, cmd = shell_cmd_match(shell_list, str, size); if (cmd == NULL) { - return ERROR_INVAL; + return EINVAL; } *cmdp = cmd; @@ -337,7 +337,7 @@ shell_cmd_complete(const char *str, unsigned long *sizep, size--; *sizep = size; - return ERROR_AGAIN; + return EAGAIN; } /* @@ -383,7 +383,7 @@ shell_cmd_check_char(char c) return 0; } - return ERROR_INVAL; + return EINVAL; } static int @@ -401,7 +401,7 @@ shell_cmd_check(const struct shell_cmd *cmd) } if (i == 0) { - return ERROR_INVAL; + return EINVAL; } return 0; @@ -459,7 +459,7 @@ shell_cmd_add(struct shell_cmd *cmd) for (;;) { if (strcmp(cmd->name, tmp->name) == 0) { printf("shell: error: %s: shell command name collision", cmd->name); - return ERROR_EXIST; + return EEXIST; } if (tmp->ht_next == NULL) { @@ -532,11 +532,11 @@ shell_line_insert(struct shell_line *line, unsigned long index, char c) unsigned long remaining_chars; if (index > line->size) { - return ERROR_INVAL; + return EINVAL; } if ((line->size + 1) == sizeof(line->str)) { - return ERROR_NOMEM; + return ENOMEM; } remaining_chars = line->size - index; @@ -557,7 +557,7 @@ shell_line_erase(struct shell_line *line, unsigned long index) unsigned long remaining_chars; if (index >= line->size) { - return ERROR_INVAL; + return EINVAL; } remaining_chars = line->size - index - 1; @@ -777,7 +777,7 @@ static int shell_process_right(void) { if (shell_cursor >= shell_line_size(shell_history_get_newest())) { - return ERROR_AGAIN; + return EAGAIN; } shell_cursor++; @@ -880,12 +880,12 @@ shell_process_tabulation(void) error = shell_cmd_complete(word, &size, &cmd); - if (error && (error != ERROR_AGAIN)) { + if (error && (error != EAGAIN)) { error = 0; goto out; } - if (error == ERROR_AGAIN) { + if (error == EAGAIN) { unsigned long cursor; cursor = shell_cursor; @@ -1069,7 +1069,7 @@ shell_process_args(void) if (j == ARRAY_SIZE(shell_argv)) { printf("shell: too many arguments\n"); - return ERROR_INVAL; + return EINVAL; } shell_argv[j] = NULL; @@ -1132,7 +1132,7 @@ shell_process_ctrl_char(char c) case '\r': putchar('\n'); shell_process_line(); - return ERROR_AGAIN; + return EAGAIN; default: return 0; } diff --git a/lib/shell.h b/lib/shell.h index f66fbf7..b5ce7d5 100644 --- a/lib/shell.h +++ b/lib/shell.h @@ -29,11 +29,13 @@ #ifndef _SHELL_H #define _SHELL_H +#include <errno.h> #include <stddef.h> +#include <string.h> #include <lib/macros.h> -#include <src/error.h> +#include <src/panic.h> #define SHELL_REGISTER_CMDS(cmds) \ MACRO_BEGIN \ @@ -42,7 +44,10 @@ MACRO_BEGIN \ \ for (___i = 0; ___i < ARRAY_SIZE(cmds); ___i++) { \ ___error = shell_cmd_register(&(cmds)[___i]); \ - error_check(___error, __func__); \ + \ + if (___error) { \ + panic("%s: %s", __func__, strerror(___error)); \ + } \ } \ MACRO_END @@ -33,7 +33,6 @@ #include <lib/macros.h> #include "cpu.h" -#include "error.h" #include "i8259.h" #include "thread.h" diff --git a/src/error.c b/src/error.c deleted file mode 100644 index 8bd7875..0000000 --- a/src/error.c +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright (c) 2017 Richard Braun. - * - * Permission is hereby granted, free of charge, to any person obtaining a - * copy of this software and associated documentation files (the "Software"), - * to deal in the Software without restriction, including without limitation - * the rights to use, copy, modify, merge, publish, distribute, sublicense, - * and/or sell copies of the Software, and to permit persons to whom the - * Software is furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING - * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER - * DEALINGS IN THE SOFTWARE. - */ - -#include <stdio.h> -#include <stdlib.h> - -#include <lib/macros.h> - -#include "error.h" -#include "panic.h" - -/* - * Error message table. - * - * This table must be consistent with the enum defined in error.h. - */ -static const char *error_msg_table[] = { - "success", - "invalid argument", - "resource temporarily unavailable", - "not enough space", - "input/output error", - "resource busy", - "entry exist", -}; - -const char * -error_str(unsigned int error) -{ - if (error >= ARRAY_SIZE(error_msg_table)) { - return "invalid error code"; - } - - return error_msg_table[error]; -} - -void -error_check(int error, const char *prefix) -{ - if (!error) { - return; - } - - panic("%s%s%s", - (prefix == NULL) ? "" : prefix, - (prefix == NULL) ? "" : ": ", - error_str(error)); -} diff --git a/src/i8259.c b/src/i8259.c index 589e13c..aed7068 100644 --- a/src/i8259.c +++ b/src/i8259.c @@ -38,6 +38,7 @@ */ #include <assert.h> +#include <errno.h> #include <stddef.h> #include <stdbool.h> #include <stdint.h> @@ -45,7 +46,6 @@ #include <lib/macros.h> #include "cpu.h" -#include "error.h" #include "i8259.h" #include "io.h" @@ -131,7 +131,7 @@ i8259_convert_global_irq(unsigned int irq, struct i8259_pic **pic, error = 0; } else { *local_irq = 0; - error = ERROR_INVAL; + error = EINVAL; } return error; diff --git a/src/mutex.c b/src/mutex.c index d70bd82..336f192 100644 --- a/src/mutex.c +++ b/src/mutex.c @@ -21,13 +21,13 @@ */ #include <assert.h> +#include <errno.h> #include <stdbool.h> #include <stddef.h> #include <lib/list.h> #include <lib/macros.h> -#include "error.h" #include "mutex.h" #include "thread.h" @@ -122,7 +122,7 @@ mutex_trylock(struct mutex *mutex) thread_preempt_disable(); if (mutex->locked) { - error = ERROR_BUSY; + error = EBUSY; } else { error = 0; mutex_set_owner(mutex, thread_self()); diff --git a/src/mutex.h b/src/mutex.h index c158d5e..fedbb8f 100644 --- a/src/mutex.h +++ b/src/mutex.h @@ -151,7 +151,7 @@ void mutex_lock(struct mutex *mutex); * * This is the non-blocking variant of mutex_lock(). * - * Return 0 on success, ERROR_BUSY if locking the mutex failed. + * Return 0 on success, EBUSY if locking the mutex failed. */ int mutex_trylock(struct mutex *mutex); diff --git a/src/string.c b/src/string.c index 210b2bc..dbdb234 100644 --- a/src/string.c +++ b/src/string.c @@ -20,6 +20,7 @@ * DEALINGS IN THE SOFTWARE. */ +#include <errno.h> #include <stddef.h> #include <string.h> @@ -141,3 +142,26 @@ strncmp(const char *s1, const char *s2, size_t n) return (int)c1 - (int)c2; } + +char * +strerror(int errnum) +{ + switch (errnum) { + case 0: + return "success"; + case EINVAL: + return "invalid argument"; + case EAGAIN: + return "resource temporarily unavailable"; + case ENOMEM: + return "not enough space"; + case EIO: + return "input/output error"; + case EBUSY: + return "resource busy"; + case EEXIST: + return "entry exist"; + default: + return "unknown error"; + } +} diff --git a/src/thread.c b/src/thread.c index 0d63be8..70f036e 100644 --- a/src/thread.c +++ b/src/thread.c @@ -22,6 +22,7 @@ */ #include <assert.h> +#include <errno.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> @@ -32,7 +33,6 @@ #include <lib/list.h> #include "cpu.h" -#include "error.h" #include "panic.h" #include "thread.h" #include "timer.h" @@ -675,7 +675,7 @@ thread_create(struct thread **threadp, thread_fn_t fn, void *arg, thread = malloc(sizeof(*thread)); if (!thread) { - return ERROR_NOMEM; + return ENOMEM; } if (stack_size < THREAD_STACK_MIN_SIZE) { @@ -686,7 +686,7 @@ thread_create(struct thread **threadp, thread_fn_t fn, void *arg, if (!stack) { free(thread); - return ERROR_NOMEM; + return ENOMEM; } thread_init(thread, fn, arg, name, stack, stack_size, priority); @@ -22,6 +22,7 @@ */ #include <assert.h> +#include <errno.h> #include <stdbool.h> #include <stddef.h> #include <stdint.h> @@ -31,7 +32,6 @@ #include <lib/macros.h> #include "cpu.h" -#include "error.h" #include "io.h" #include "uart.h" #include "thread.h" @@ -164,7 +164,7 @@ uart_read(uint8_t *byte) eflags = cpu_intr_save(); if (uart_waiter) { - error = ERROR_BUSY; + error = EBUSY; goto out; } @@ -52,7 +52,7 @@ void uart_write(uint8_t byte); * until there is data to consume. * * If successful, return 0. If another thread is already waiting for data, - * ERROR_BUSY is returned. + * EBUSY is returned. * * Preemption must be enabled when calling this function. */ |