diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/cbuf.c | 203 | ||||
-rw-r--r-- | lib/cbuf.h | 150 | ||||
-rw-r--r-- | lib/fmt.c | 1468 | ||||
-rw-r--r-- | lib/fmt.h | 69 | ||||
-rw-r--r-- | lib/hash.h | 123 | ||||
-rw-r--r-- | lib/list.h | 538 | ||||
-rw-r--r-- | lib/macros.h | 94 | ||||
-rw-r--r-- | lib/shell.c | 1212 | ||||
-rw-r--r-- | lib/shell.h | 94 |
9 files changed, 3951 insertions, 0 deletions
diff --git a/lib/cbuf.c b/lib/cbuf.c new file mode 100644 index 0000000..e405964 --- /dev/null +++ b/lib/cbuf.c @@ -0,0 +1,203 @@ +/* + * Copyright (c) 2015-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. + * + * Upstream site with license notes : + * http://git.sceen.net/rbraun/librbraun.git/ + */ + +#include <assert.h> +#include <stddef.h> +#include <stdint.h> +#include <string.h> + +#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) + +void +cbuf_init(struct cbuf *cbuf, void *buf, size_t capacity) +{ + assert(ISP2(capacity)); + + cbuf->buf = buf; + cbuf->capacity = capacity; + cbuf->start = CBUF_INIT_INDEX; + cbuf->end = cbuf->start; +} + +static size_t +cbuf_index(const struct cbuf *cbuf, size_t abs_index) +{ + return abs_index & (cbuf->capacity - 1); +} + +static void +cbuf_update_start(struct cbuf *cbuf) +{ + /* Mind integer overflows */ + if (cbuf_size(cbuf) > cbuf->capacity) { + cbuf->start = cbuf->end - cbuf->capacity; + } +} + +int +cbuf_push(struct cbuf *cbuf, const void *buf, size_t size, bool erase) +{ + size_t free_size; + + if (!erase) { + free_size = cbuf_capacity(cbuf) - cbuf_size(cbuf); + + if (size > free_size) { + return ERROR_AGAIN; + } + } + + return cbuf_write(cbuf, cbuf_end(cbuf), buf, size); +} + +int +cbuf_pop(struct cbuf *cbuf, void *buf, size_t *sizep) +{ + __unused int error; + + if (cbuf_size(cbuf) == 0) { + return ERROR_AGAIN; + } + + error = cbuf_read(cbuf, cbuf_start(cbuf), buf, sizep); + assert(!error); + cbuf->start += *sizep; + return 0; +} + +int +cbuf_pushb(struct cbuf *cbuf, uint8_t byte, bool erase) +{ + size_t free_size; + + if (!erase) { + free_size = cbuf_capacity(cbuf) - cbuf_size(cbuf); + + if (free_size == 0) { + return ERROR_AGAIN; + } + } + + cbuf->buf[cbuf_index(cbuf, cbuf->end)] = byte; + cbuf->end++; + cbuf_update_start(cbuf); + return 0; +} + +int +cbuf_popb(struct cbuf *cbuf, void *bytep) +{ + uint8_t *ptr; + + if (cbuf_size(cbuf) == 0) { + return ERROR_AGAIN; + } + + ptr = bytep; + *ptr = cbuf->buf[cbuf_index(cbuf, cbuf->start)]; + cbuf->start++; + return 0; +} + +int +cbuf_write(struct cbuf *cbuf, size_t index, const void *buf, size_t size) +{ + uint8_t *start, *end, *buf_end; + size_t new_end, skip; + + if (!cbuf_range_valid(cbuf, index, cbuf->end)) { + return ERROR_INVAL; + } + + new_end = index + size; + + if (!cbuf_range_valid(cbuf, cbuf->start, new_end)) { + cbuf->end = new_end; + + if (size > cbuf_capacity(cbuf)) { + skip = size - cbuf_capacity(cbuf); + buf += skip; + index += skip; + size = cbuf_capacity(cbuf); + } + } + + start = &cbuf->buf[cbuf_index(cbuf, index)]; + end = start + size; + buf_end = cbuf->buf + cbuf->capacity; + + if ((end <= cbuf->buf) || (end > buf_end)) { + skip = buf_end - start; + memcpy(start, buf, skip); + buf += skip; + start = cbuf->buf; + size -= skip; + } + + memcpy(start, buf, size); + cbuf_update_start(cbuf); + return 0; +} + +int +cbuf_read(const struct cbuf *cbuf, size_t index, void *buf, size_t *sizep) +{ + const uint8_t *start, *end, *buf_end; + size_t size; + + /* At least one byte must be available */ + if (!cbuf_range_valid(cbuf, index, index + 1)) { + return ERROR_INVAL; + } + + size = cbuf->end - index; + + if (*sizep > size) { + *sizep = size; + } + + start = &cbuf->buf[cbuf_index(cbuf, index)]; + end = start + *sizep; + buf_end = cbuf->buf + cbuf->capacity; + + if ((end > cbuf->buf) && (end <= buf_end)) { + size = *sizep; + } else { + size = buf_end - start; + memcpy(buf, start, size); + buf += size; + start = cbuf->buf; + size = *sizep - size; + } + + memcpy(buf, start, size); + return 0; +} diff --git a/lib/cbuf.h b/lib/cbuf.h new file mode 100644 index 0000000..eb2ec83 --- /dev/null +++ b/lib/cbuf.h @@ -0,0 +1,150 @@ +/* + * Copyright (c) 2015-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. + * + * Upstream site with license notes : + * http://git.sceen.net/rbraun/librbraun.git/ + * + * + * Circular byte buffer. + */ + +#ifndef _CBUF_H +#define _CBUF_H + +#include <stdbool.h> +#include <stddef.h> +#include <stdint.h> + +/* + * Circular buffer descriptor. + * + * The buffer capacity must be a power-of-two. Indexes are absolute values + * which can overflow. Their difference cannot exceed the capacity. + */ +struct cbuf { + uint8_t *buf; + size_t capacity; + size_t start; + size_t end; +}; + +static inline size_t +cbuf_capacity(const struct cbuf *cbuf) +{ + return cbuf->capacity; +} + +static inline size_t +cbuf_start(const struct cbuf *cbuf) +{ + return cbuf->start; +} + +static inline size_t +cbuf_end(const struct cbuf *cbuf) +{ + return cbuf->end; +} + +static inline size_t +cbuf_size(const struct cbuf *cbuf) +{ + return cbuf->end - cbuf->start; +} + +static inline void +cbuf_clear(struct cbuf *cbuf) +{ + cbuf->start = cbuf->end; +} + +static inline bool +cbuf_range_valid(const struct cbuf *cbuf, size_t start, size_t end) +{ + return (((end - start) <= cbuf_size(cbuf)) + && ((start - cbuf->start) <= cbuf_size(cbuf)) + && ((cbuf->end - end) <= cbuf_size(cbuf))); +} + +/* + * Initialize a circular buffer. + * + * The descriptor is set to use the given buffer for storage. Capacity + * must be a power-of-two. + */ +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. + */ +int cbuf_push(struct cbuf *cbuf, const void *buf, size_t size, bool erase); + +/* + * Pop data from a circular buffer. + * + * 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 + * output buffer is undefined. + */ +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. + */ +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. + */ +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. + * The given [index, size) range may extend beyond the end of the circular + * buffer. + */ +int cbuf_write(struct cbuf *cbuf, size_t index, const void *buf, size_t size); + +/* + * Read from a circular buffer at a specific location. + * + * 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. + * + * The circular buffer isn't changed by this operation. + */ +int cbuf_read(const struct cbuf *cbuf, size_t index, void *buf, size_t *sizep); + +#endif /* _CBUF_H */ diff --git a/lib/fmt.c b/lib/fmt.c new file mode 100644 index 0000000..0940f24 --- /dev/null +++ b/lib/fmt.c @@ -0,0 +1,1468 @@ +/* + * Copyright (c) 2010-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. + * + * Upstream site with license notes : + * http://git.sceen.net/rbraun/librbraun.git/ + */ + +#include <assert.h> +#include <limits.h> +#include <stdbool.h> +#include <stdarg.h> +#include <stddef.h> +#include <stdint.h> +#include <stdio.h> +#include <string.h> + +#include <lib/fmt.h> +#include <lib/macros.h> + +#include <src/error.h> + +/* + * XXX This type is specified by POSIX. Directly declare it for simplicity. + */ +typedef long ssize_t; + +/* + * Size for the temporary number buffer. The minimum base is 8 so 3 bits + * are consumed per digit. Add one to round up. The conversion algorithm + * doesn't use the null byte. + */ +#define FMT_MAX_NUM_SIZE (((sizeof(unsigned long long) * CHAR_BIT) / 3) + 1) + +/* + * Special size for fmt_vsnprintf(), used when the buffer size is unknown. + */ +#define FMT_NOLIMIT ((size_t)-1) + +/* + * Formatting flags. + * + * FMT_FORMAT_LOWER must be 0x20 as it is OR'd with fmt_digits, eg. + * '0': 0x30 | 0x20 => 0x30 ('0') + * 'A': 0x41 | 0x20 => 0x61 ('a') + */ +#define FMT_FORMAT_ALT_FORM 0x0001 /* "Alternate form" */ +#define FMT_FORMAT_ZERO_PAD 0x0002 /* Zero padding on the left */ +#define FMT_FORMAT_LEFT_JUSTIFY 0x0004 /* Align text on the left */ +#define FMT_FORMAT_BLANK 0x0008 /* Blank space before positive number */ +#define FMT_FORMAT_SIGN 0x0010 /* Always place a sign (either + or -) */ +#define FMT_FORMAT_LOWER 0x0020 /* To lowercase (for %x) */ +#define FMT_FORMAT_CONV_SIGNED 0x0040 /* Format specifies signed conversion */ +#define FMT_FORMAT_DISCARD 0x0080 /* Discard output (scanf) */ +#define FMT_FORMAT_CHECK_WIDTH 0x0100 /* Check field width (scanf) */ + +enum { + FMT_MODIFIER_NONE, + FMT_MODIFIER_CHAR, + FMT_MODIFIER_SHORT, + FMT_MODIFIER_LONG, + FMT_MODIFIER_LONGLONG, + FMT_MODIFIER_PTR, /* Used only for %p */ + FMT_MODIFIER_SIZE, + FMT_MODIFIER_PTRDIFF, +}; + +enum { + FMT_SPECIFIER_INVALID, + FMT_SPECIFIER_INT, + FMT_SPECIFIER_CHAR, + FMT_SPECIFIER_STR, + FMT_SPECIFIER_NRCHARS, + FMT_SPECIFIER_PERCENT, +}; + +/* + * Note that copies of the original va_list object are made, because va_arg() + * may not reliably be used by different callee functions, and despite the + * standard explicitely allowing pointers to va_list objects, it's apparently + * very difficult for implementations to provide and is best avoided. + */ + +struct fmt_sprintf_state { + const char *format; + va_list ap; + unsigned int flags; + int width; + int precision; + unsigned int modifier; + unsigned int specifier; + unsigned int base; + char *str; + char *start; + char *end; +}; + +struct fmt_sscanf_state { + const char *str; + const char *start; + const char *format; + va_list ap; + unsigned int flags; + int width; + int precision; + unsigned int modifier; + unsigned int specifier; + unsigned int base; + int nr_convs; +}; + +static const char fmt_digits[] = "0123456789ABCDEF"; + +static char +fmt_consume(const char **strp) +{ + char c; + + c = **strp; + (*strp)++; + return c; +} + +static void +fmt_restore(const char **strp) +{ + (*strp)--; +} + +static void +fmt_vsnprintf_produce(char **strp, char *end, char c) +{ + if (*strp < end) { + **strp = c; + } + + (*strp)++; +} + +static bool +fmt_isdigit(char c) +{ + return (c >= '0') && (c <= '9'); +} + +static bool +fmt_isxdigit(char c) +{ + return fmt_isdigit(c) + || ((c >= 'a') && (c <= 'f')) + || ((c >= 'A') && (c <= 'F')); +} + +static void +fmt_sprintf_state_init(struct fmt_sprintf_state *state, + char *str, size_t size, + const char *format, va_list ap) +{ + state->format = format; + va_copy(state->ap, ap); + state->str = str; + state->start = str; + + if (size == 0) { + state->end = NULL; + } else if (size == FMT_NOLIMIT) { + state->end = (char *)-1; + } else { + state->end = state->start + size - 1; + } +} + +static int +fmt_sprintf_state_finalize(struct fmt_sprintf_state *state) +{ + va_end(state->ap); + + if (state->str < state->end) { + *state->str = '\0'; + } else if (state->end != NULL) { + *state->end = '\0'; + } + + return state->str - state->start; +} + +static char +fmt_sprintf_state_consume_format(struct fmt_sprintf_state *state) +{ + return fmt_consume(&state->format); +} + +static void +fmt_sprintf_state_restore_format(struct fmt_sprintf_state *state) +{ + fmt_restore(&state->format); +} + +static void +fmt_sprintf_state_consume_flags(struct fmt_sprintf_state *state) +{ + bool found; + char c; + + found = true; + state->flags = 0; + + do { + c = fmt_sprintf_state_consume_format(state); + + switch (c) { + case '#': + state->flags |= FMT_FORMAT_ALT_FORM; + break; + case '0': + state->flags |= FMT_FORMAT_ZERO_PAD; + break; + case '-': + state->flags |= FMT_FORMAT_LEFT_JUSTIFY; + break; + case ' ': + state->flags |= FMT_FORMAT_BLANK; + break; + case '+': + state->flags |= FMT_FORMAT_SIGN; + break; + default: + found = false; + break; + } + } while (found); + + fmt_sprintf_state_restore_format(state); +} + +static void +fmt_sprintf_state_consume_width(struct fmt_sprintf_state *state) +{ + char c; + + c = fmt_sprintf_state_consume_format(state); + + if (fmt_isdigit(c)) { + state->width = 0; + + do { + state->width = state->width * 10 + (c - '0'); + c = fmt_sprintf_state_consume_format(state); + } while (fmt_isdigit(c)); + + fmt_sprintf_state_restore_format(state); + } else if (c == '*') { + state->width = va_arg(state->ap, int); + + if (state->width < 0) { + state->flags |= FMT_FORMAT_LEFT_JUSTIFY; + state->width = -state->width; + } + } else { + state->width = 0; + fmt_sprintf_state_restore_format(state); + } +} + +static void +fmt_sprintf_state_consume_precision(struct fmt_sprintf_state *state) +{ + char c; + + c = fmt_sprintf_state_consume_format(state); + + if (c == '.') { + c = fmt_sprintf_state_consume_format(state); + + if (fmt_isdigit(c)) { + state->precision = 0; + + do { + state->precision = state->precision * 10 + (c - '0'); + c = fmt_sprintf_state_consume_format(state); + } while (fmt_isdigit(c)); + + fmt_sprintf_state_restore_format(state); + } else if (c == '*') { + state->precision = va_arg(state->ap, int); + + if (state->precision < 0) { + state->precision = 0; + } + } else { + state->precision = 0; + fmt_sprintf_state_restore_format(state); + } + } else { + /* precision is >= 0 only if explicit */ + state->precision = -1; + fmt_sprintf_state_restore_format(state); + } +} + +static void +fmt_sprintf_state_consume_modifier(struct fmt_sprintf_state *state) +{ + char c, c2; + + c = fmt_sprintf_state_consume_format(state); + + switch (c) { + case 'h': + case 'l': + c2 = fmt_sprintf_state_consume_format(state); + + if (c == c2) { + state->modifier = (c == 'h') ? FMT_MODIFIER_CHAR + : FMT_MODIFIER_LONGLONG; + } else { + state->modifier = (c == 'h') ? FMT_MODIFIER_SHORT + : FMT_MODIFIER_LONG; + fmt_sprintf_state_restore_format(state); + } + + break; + case 'z': + state->modifier = FMT_MODIFIER_SIZE; + case 't': + state->modifier = FMT_MODIFIER_PTRDIFF; + break; + default: + state->modifier = FMT_MODIFIER_NONE; + fmt_sprintf_state_restore_format(state); + break; + } +} + +static void +fmt_sprintf_state_consume_specifier(struct fmt_sprintf_state *state) +{ + char c; + + c = fmt_sprintf_state_consume_format(state); + + switch (c) { + case 'd': + case 'i': + state->flags |= FMT_FORMAT_CONV_SIGNED; + case 'u': + state->base = 10; + state->specifier = FMT_SPECIFIER_INT; + break; + case 'o': + state->base = 8; + state->specifier = FMT_SPECIFIER_INT; + break; + case 'p': + state->flags |= FMT_FORMAT_ALT_FORM; + state->modifier = FMT_MODIFIER_PTR; + case 'x': + state->flags |= FMT_FORMAT_LOWER; + case 'X': + state->base = 16; + state->specifier = FMT_SPECIFIER_INT; + break; + case 'c': + state->specifier = FMT_SPECIFIER_CHAR; + break; + case 's': + state->specifier = FMT_SPECIFIER_STR; + break; + case 'n': + state->specifier = FMT_SPECIFIER_NRCHARS; + break; + case '%': + state->specifier = FMT_SPECIFIER_PERCENT; + break; + default: + state->specifier = FMT_SPECIFIER_INVALID; + fmt_sprintf_state_restore_format(state); + break; + } +} + +static void +fmt_sprintf_state_produce_raw_char(struct fmt_sprintf_state *state, char c) +{ + fmt_vsnprintf_produce(&state->str, state->end, c); +} + +static int +fmt_sprintf_state_consume(struct fmt_sprintf_state *state) +{ + char c; + + c = fmt_consume(&state->format); + + if (c == '\0') { + return ERROR_IO; + } + + if (c != '%') { + fmt_sprintf_state_produce_raw_char(state, c); + return ERROR_AGAIN; + } + + fmt_sprintf_state_consume_flags(state); + fmt_sprintf_state_consume_width(state); + fmt_sprintf_state_consume_precision(state); + fmt_sprintf_state_consume_modifier(state); + fmt_sprintf_state_consume_specifier(state); + return 0; +} + +static void +fmt_sprintf_state_produce_int(struct fmt_sprintf_state *state) +{ + char c, sign, tmp[FMT_MAX_NUM_SIZE]; + unsigned int r, mask, shift; + unsigned long long n; + int i; + + switch (state->modifier) { + case FMT_MODIFIER_CHAR: + if (state->flags & FMT_FORMAT_CONV_SIGNED) { + n = (signed char)va_arg(state->ap, int); + } else { + n = (unsigned char)va_arg(state->ap, int); + } + + break; + case FMT_MODIFIER_SHORT: + if (state->flags & FMT_FORMAT_CONV_SIGNED) { + n = (short)va_arg(state->ap, int); + } else { + n = (unsigned short)va_arg(state->ap, int); + } + + break; + case FMT_MODIFIER_LONG: + if (state->flags & FMT_FORMAT_CONV_SIGNED) { + n = va_arg(state->ap, long); + } else { + n = va_arg(state->ap, unsigned long); + } + + break; + case FMT_MODIFIER_LONGLONG: + if (state->flags & FMT_FORMAT_CONV_SIGNED) { + n = va_arg(state->ap, long long); + } else { + n = va_arg(state->ap, unsigned long long); + } + + break; + case FMT_MODIFIER_PTR: + n = (uintptr_t)va_arg(state->ap, void *); + break; + case FMT_MODIFIER_SIZE: + if (state->flags & FMT_FORMAT_CONV_SIGNED) { + n = va_arg(state->ap, ssize_t); + } else { + n = va_arg(state->ap, size_t); + } + + break; + case FMT_MODIFIER_PTRDIFF: + n = va_arg(state->ap, ptrdiff_t); + break; + default: + if (state->flags & FMT_FORMAT_CONV_SIGNED) { + n = va_arg(state->ap, int); + } else { + n = va_arg(state->ap, unsigned int); + } + + break; + } + + if ((state->flags & FMT_FORMAT_LEFT_JUSTIFY) || (state->precision >= 0)) { + state->flags &= ~FMT_FORMAT_ZERO_PAD; + } + + sign = '\0'; + + if (state->flags & FMT_FORMAT_ALT_FORM) { + /* '0' for octal */ + state->width--; + + /* '0x' or '0X' for hexadecimal */ + if (state->base == 16) { + state->width--; + } + } else if (state->flags & FMT_FORMAT_CONV_SIGNED) { + if ((long long)n < 0) { + sign = '-'; + state->width--; + n = -(long long)n; + } else if (state->flags & FMT_FORMAT_SIGN) { + /* FMT_FORMAT_SIGN must precede FMT_FORMAT_BLANK. */ + sign = '+'; + state->width--; + } else if (state->flags & FMT_FORMAT_BLANK) { + sign = ' '; + state->width--; + } + } + + /* Conversion, in reverse order */ + + i = 0; + + if (n == 0) { + if (state->precision != 0) { + tmp[i] = '0'; + i++; + } + } else if (state->base == 10) { + /* + * Try to avoid 64 bits operations if the processor doesn't + * support them. Note that even when using modulus and + * division operators close to each other, the compiler may + * forge two functions calls to compute the quotient and the + * remainder, whereas processor instructions are generally + * correctly used once, giving both results at once, through + * plain or reciprocal division. + */ +#ifndef __LP64__ + if (state->modifier == FMT_MODIFIER_LONGLONG) { +#endif /* __LP64__ */ + do { + r = n % 10; + n /= 10; + tmp[i] = fmt_digits[r]; + i++; + } while (n != 0); +#ifndef __LP64__ + } else { + unsigned long m; + + m = (unsigned long)n; + + do { + r = m % 10; + m /= 10; + tmp[i] = fmt_digits[r]; + i++; + } while (m != 0); + } +#endif /* __LP64__ */ + } else { + mask = state->base - 1; + shift = (state->base == 8) ? 3 : 4; + + do { + r = n & mask; + n >>= shift; + tmp[i] = fmt_digits[r] | (state->flags & FMT_FORMAT_LOWER); + i++; + } while (n != 0); + } + + if (i > state->precision) { + state->precision = i; + } + + state->width -= state->precision; + + if (!(state->flags & (FMT_FORMAT_LEFT_JUSTIFY | FMT_FORMAT_ZERO_PAD))) { + while (state->width > 0) { + state->width--; + fmt_sprintf_state_produce_raw_char(state, ' '); + } + + state->width--; + } + + if (state->flags & FMT_FORMAT_ALT_FORM) { + fmt_sprintf_state_produce_raw_char(state, '0'); + + if (state->base == 16) { + c = 'X' | (state->flags & FMT_FORMAT_LOWER); + fmt_sprintf_state_produce_raw_char(state, c); + } + } else if (sign != '\0') { + fmt_sprintf_state_produce_raw_char(state, sign); + } + + if (!(state->flags & FMT_FORMAT_LEFT_JUSTIFY)) { + c = (state->flags & FMT_FORMAT_ZERO_PAD) ? '0' : ' '; + + while (state->width > 0) { + state->width--; + fmt_sprintf_state_produce_raw_char(state, c); + } + + state->width--; + } + + while (i < state->precision) { + state->precision--; + fmt_sprintf_state_produce_raw_char(state, '0'); + } + + state->precision--; + + while (i > 0) { + i--; + fmt_sprintf_state_produce_raw_char(state, tmp[i]); + } + + while (state->width > 0) { + state->width--; + fmt_sprintf_state_produce_raw_char(state, ' '); + } + + state->width--; +} + +static void +fmt_sprintf_state_produce_char(struct fmt_sprintf_state *state) +{ + char c; + + c = va_arg(state->ap, int); + + if (!(state->flags & FMT_FORMAT_LEFT_JUSTIFY)) { + for (;;) { + state->width--; + + if (state->width <= 0) { + break; + } + + fmt_sprintf_state_produce_raw_char(state, ' '); + } + } + + fmt_sprintf_state_produce_raw_char(state, c); + + for (;;) { + state->width--; + + if (state->width <= 0) { + break; + } + + fmt_sprintf_state_produce_raw_char(state, ' '); + } +} + +static void +fmt_sprintf_state_produce_str(struct fmt_sprintf_state *state) +{ + int i, len; + char *s; + + s = va_arg(state->ap, char *); + + if (s == NULL) { + s = "(null)"; + } + + len = 0; + + for (len = 0; s[len] != '\0'; len++) { + if (len == state->precision) { + break; + } + } + + if (!(state->flags & FMT_FORMAT_LEFT_JUSTIFY)) { + while (len < state->width) { + state->width--; + fmt_sprintf_state_produce_raw_char(state, ' '); + } + } + + for (i = 0; i < len; i++) { + fmt_sprintf_state_produce_raw_char(state, *s); + s++; + } + + while (len < state->width) { + state->width--; + fmt_sprintf_state_produce_raw_char(state, ' '); + } +} + +static void +fmt_sprintf_state_produce_nrchars(struct fmt_sprintf_state *state) +{ + if (state->modifier == FMT_MODIFIER_CHAR) { + signed char *ptr = va_arg(state->ap, signed char *); + *ptr = state->str - state->start; + } else if (state->modifier == FMT_MODIFIER_SHORT) { + short *ptr = va_arg(state->ap, short *); + *ptr = state->str - state->start; + } else if (state->modifier == FMT_MODIFIER_LONG) { + long *ptr = va_arg(state->ap, long *); + *ptr = state->str - state->start; + } else if (state->modifier == FMT_MODIFIER_LONGLONG) { + long long *ptr = va_arg(state->ap, long long *); + *ptr = state->str - state->start; + } else if (state->modifier == FMT_MODIFIER_SIZE) { + ssize_t *ptr = va_arg(state->ap, ssize_t *); + *ptr = state->str - state->start; + } else if (state->modifier == FMT_MODIFIER_PTRDIFF) { + ptrdiff_t *ptr = va_arg(state->ap, ptrdiff_t *); + *ptr = state->str - state->start; + } else { + int *ptr = va_arg(state->ap, int *); + *ptr = state->str - state->start; + } +} + +static void +fmt_sprintf_state_produce(struct fmt_sprintf_state *state) +{ + switch (state->specifier) { + case FMT_SPECIFIER_INT: + fmt_sprintf_state_produce_int(state); + break; + case FMT_SPECIFIER_CHAR: + fmt_sprintf_state_produce_char(state); + break; + case FMT_SPECIFIER_STR: + fmt_sprintf_state_produce_str(state); + break; + case FMT_SPECIFIER_NRCHARS: + fmt_sprintf_state_produce_nrchars(state); + break; + case FMT_SPECIFIER_PERCENT: + case FMT_SPECIFIER_INVALID: + fmt_sprintf_state_produce_raw_char(state, '%'); + break; + } +} + +int +fmt_sprintf(char *str, const char *format, ...) +{ + va_list ap; + int length; + + va_start(ap, format); + length = fmt_vsprintf(str, format, ap); + va_end(ap); + + return length; +} + +int +fmt_vsprintf(char *str, const char *format, va_list ap) +{ + return fmt_vsnprintf(str, FMT_NOLIMIT, format, ap); +} + +int +fmt_snprintf(char *str, size_t size, const char *format, ...) +{ + va_list ap; + int length; + + va_start(ap, format); + length = fmt_vsnprintf(str, size, format, ap); + va_end(ap); + + return length; +} + +int +fmt_vsnprintf(char *str, size_t size, const char *format, va_list ap) +{ + struct fmt_sprintf_state state; + int error; + + fmt_sprintf_state_init(&state, str, size, format, ap); + + for (;;) { + error = fmt_sprintf_state_consume(&state); + + if (error == ERROR_AGAIN) { + continue; + } else if (error) { + break; + } + + fmt_sprintf_state_produce(&state); + } + + return fmt_sprintf_state_finalize(&state); +} + +static char +fmt_atoi(char c) +{ + assert(fmt_isxdigit(c)); + + if (fmt_isdigit(c)) { + return c - '0'; + } else if (c >= 'a' && c <= 'f') { + return 10 + (c - 'a'); + } else { + return 10 + (c - 'A'); + } +} + +static bool +fmt_isspace(char c) +{ + if (c == ' ') { + return true; + } + + if ((c >= '\t') && (c <= '\f')) { + return true; + } + + return false; +} + +static void +fmt_skip(const char **strp) +{ + while (fmt_isspace(**strp)) { + (*strp)++; + } +} + +static void +fmt_sscanf_state_init(struct fmt_sscanf_state *state, const char *str, + const char *format, va_list ap) +{ + state->str = str; + state->start = str; + state->format = format; + state->flags = 0; + state->width = 0; + va_copy(state->ap, ap); + state->nr_convs = 0; +} + +static int +fmt_sscanf_state_finalize(struct fmt_sscanf_state *state) +{ + va_end(state->ap); + return state->nr_convs; +} + +static void +fmt_sscanf_state_report_conv(struct fmt_sscanf_state *state) +{ + if (state->nr_convs == EOF) { + state->nr_convs = 1; + return; + } + + state->nr_convs++; +} + +static void +fmt_sscanf_state_report_error(struct fmt_sscanf_state *state) +{ + if (state->nr_convs != 0) { + return; + } + + state->nr_convs = EOF; +} + +static void +fmt_sscanf_state_skip_space(struct fmt_sscanf_state *state) +{ + fmt_skip(&state->str); +} + +static char +fmt_sscanf_state_consume_string(struct fmt_sscanf_state *state) +{ + char c; + + c = fmt_consume(&state->str); + + if (state->flags & FMT_FORMAT_CHECK_WIDTH) { + if (state->width == 0) { + c = EOF; + } else { + state->width--; + } + } + + return c; +} + +static void +fmt_sscanf_state_restore_string(struct fmt_sscanf_state *state) +{ + fmt_restore(&state->str); +} + +static char +fmt_sscanf_state_consume_format(struct fmt_sscanf_state *state) +{ + return fmt_consume(&state->format); +} + +static void +fmt_sscanf_state_restore_format(struct fmt_sscanf_state *state) +{ + fmt_restore(&state->format); +} + +static void +fmt_sscanf_state_consume_flags(struct fmt_sscanf_state *state) +{ + bool found; + char c; + + found = true; + state->flags = 0; + + do { + c = fmt_sscanf_state_consume_format(state); + + switch (c) { + case '*': + state->flags |= FMT_FORMAT_DISCARD; + break; + default: + found = false; + break; + } + } while (found); + + fmt_sscanf_state_restore_format(state); +} + +static void +fmt_sscanf_state_consume_width(struct fmt_sscanf_state *state) +{ + char c; + + state->width = 0; + + for (;;) { + c = fmt_sscanf_state_consume_format(state); + + if (!fmt_isdigit(c)) { + break; + } + + state->width = state->width * 10 + (c - '0'); + } + + if (state->width != 0) { + state->flags |= FMT_FORMAT_CHECK_WIDTH; + } + + fmt_sscanf_state_restore_format(state); +} + +static void +fmt_sscanf_state_consume_modifier(struct fmt_sscanf_state *state) +{ + char c, c2; + + c = fmt_sscanf_state_consume_format(state); + + switch (c) { + case 'h': + case 'l': + c2 = fmt_sscanf_state_consume_format(state); + + if (c == c2) { + state->modifier = (c == 'h') ? FMT_MODIFIER_CHAR + : FMT_MODIFIER_LONGLONG; + } else { + state->modifier = (c == 'h') ? FMT_MODIFIER_SHORT + : FMT_MODIFIER_LONG; + fmt_sscanf_state_restore_format(state); + } + + break; + case 'z': + state->modifier = FMT_MODIFIER_SIZE; + break; + case 't': + state->modifier = FMT_MODIFIER_PTRDIFF; + break; + default: + state->modifier = FMT_MODIFIER_NONE; + fmt_sscanf_state_restore_format(state); + break; + } +} + +static void +fmt_sscanf_state_consume_specifier(struct fmt_sscanf_state *state) +{ + char c; + + c = fmt_sscanf_state_consume_format(state); + + switch (c) { + case 'i': + state->base = 0; + state->flags |= FMT_FORMAT_CONV_SIGNED; + state->specifier = FMT_SPECIFIER_INT; + break; + case 'd': + state->flags |= FMT_FORMAT_CONV_SIGNED; + case 'u': + state->base = 10; + state->specifier = FMT_SPECIFIER_INT; + break; + case 'o': + state->base = 8; + state->specifier = FMT_SPECIFIER_INT; + break; + case 'p': + state->modifier = FMT_MODIFIER_PTR; + case 'x': + case 'X': + state->base = 16; + state->specifier = FMT_SPECIFIER_INT; + break; + case 'c': + state->specifier = FMT_SPECIFIER_CHAR; + break; + case 's': + state->specifier = FMT_SPECIFIER_STR; + break; + case 'n': + state->specifier = FMT_SPECIFIER_NRCHARS; + break; + case '%': + state->specifier = FMT_SPECIFIER_PERCENT; + break; + default: + state->specifier = FMT_SPECIFIER_INVALID; + fmt_sscanf_state_restore_format(state); + break; + } +} + +static int +fmt_sscanf_state_discard_char(struct fmt_sscanf_state *state, char c) +{ + char c2; + + if (fmt_isspace(c)) { + fmt_sscanf_state_skip_space(state); + return 0; + } + + c2 = fmt_sscanf_state_consume_string(state); + + if (c != c2) { + if ((c2 == '\0') && (state->nr_convs == 0)) { + state->nr_convs = EOF; + } + + return ERROR_INVAL; + } + + return 0; +} + +static int +fmt_sscanf_state_consume(struct fmt_sscanf_state *state) +{ + int error; + char c; + + state->flags = 0; + + c = fmt_consume(&state->format); + + if (c == '\0') { + return ERROR_IO; + } + + if (c != '%') { + error = fmt_sscanf_state_discard_char(state, c); + + if (error) { + return error; + } + + return ERROR_AGAIN; + } + + fmt_sscanf_state_consume_flags(state); + fmt_sscanf_state_consume_width(state); + fmt_sscanf_state_consume_modifier(state); + fmt_sscanf_state_consume_specifier(state); + return 0; +} + +static int +fmt_sscanf_state_produce_int(struct fmt_sscanf_state *state) +{ + unsigned long long n, m, tmp; + char c, buf[FMT_MAX_NUM_SIZE]; + bool negative; + size_t i; + + negative = 0; + + fmt_sscanf_state_skip_space(state); + c = fmt_sscanf_state_consume_string(state); + + if (c == '-') { + negative = true; + c = fmt_sscanf_state_consume_string(state); + } + + if (c == '0') { + c = fmt_sscanf_state_consume_string(state); + + if ((c == 'x') || (c == 'X')) { + if (state->base == 0) { + state->base = 16; + } + + if (state->base == 16) { + c = fmt_sscanf_state_consume_string(state); + } else { + fmt_sscanf_state_restore_string(state); + c = '0'; + } + } else { + if (state->base == 0) { + state->base = 8; + } + + if (state->base != 8) { + fmt_sscanf_state_restore_string(state); + c = '0'; + } + } + } + + i = 0; + + while (c != '\0') { + if (state->base == 8) { + if (!((c >= '0') && (c <= '7'))) { + break; + } + } else if (state->base == 16) { + if (!fmt_isxdigit(c)) { + break; + } + } else { + if (!fmt_isdigit(c)) { + break; + } + } + + /* XXX Standard sscanf provides no way to cleanly handle overflows */ + if (i < (ARRAY_SIZE(buf) - 1)) { + buf[i] = c; + } else if (i == (ARRAY_SIZE(buf) - 1)) { + strcpy(buf, "1"); + negative = true; + } + + i++; + c = fmt_sscanf_state_consume_string(state); + } + + fmt_sscanf_state_restore_string(state); + + if (state->flags & FMT_FORMAT_DISCARD) { + return 0; + } + + if (i == 0) { + if (c == '\0') { + fmt_sscanf_state_report_error(state); + return ERROR_INVAL; + } + + buf[0] = '0'; + i = 1; + } + + if (i < ARRAY_SIZE(buf)) { + buf[i] = '\0'; + i--; + } else { + i = strlen(buf) - 1; + } + + n = 0; + +#ifndef __LP64__ + if (state->modifier == FMT_MODIFIER_LONGLONG) { +#endif /* __LP64__ */ + m = 1; + tmp = 0; + + while (&buf[i] >= buf) { + tmp += fmt_atoi(buf[i]) * m; + + if (tmp < n) { + n = 1; + negative = true; + break; + } + + n = tmp; + m *= state->base; + i--; + } +#ifndef __LP64__ + } else { + unsigned long _n, _m, _tmp; + + _n = 0; + _m = 1; + _tmp = 0; + + while (&buf[i] >= buf) { + _tmp += fmt_atoi(buf[i]) * _m; + + if (_tmp < _n) { + _n = 1; + negative = true; + break; + } + + _n = _tmp; + _m *= state->base; + i--; + } + + n = _n; + } +#endif /* __LP64__ */ + + if (negative) { + n = -n; + } + + switch (state->modifier) { + case FMT_MODIFIER_CHAR: + if (state->flags & FMT_FORMAT_CONV_SIGNED) { + *va_arg(state->ap, char *) = n; + } else { + *va_arg(state->ap, unsigned char *) = n; + } + + break; + case FMT_MODIFIER_SHORT: + if (state->flags & FMT_FORMAT_CONV_SIGNED) { + *va_arg(state->ap, short *) = n; + } else { + *va_arg(state->ap, unsigned short *) = n; + } + + break; + case FMT_MODIFIER_LONG: + if (state->flags & FMT_FORMAT_CONV_SIGNED) { + *va_arg(state->ap, long *) = n; + } else { + *va_arg(state->ap, unsigned long *) = n; + } + + break; + case FMT_MODIFIER_LONGLONG: + if (state->flags & FMT_FORMAT_CONV_SIGNED) { + *va_arg(state->ap, long long *) = n; + } else { + *va_arg(state->ap, unsigned long long *) = n; + } + + break; + case FMT_MODIFIER_PTR: + *va_arg(state->ap, uintptr_t *) = n; + break; + case FMT_MODIFIER_SIZE: + *va_arg(state->ap, size_t *) = n; + break; + case FMT_MODIFIER_PTRDIFF: + *va_arg(state->ap, ptrdiff_t *) = n; + break; + default: + if (state->flags & FMT_FORMAT_CONV_SIGNED) { + *va_arg(state->ap, int *) = n; + } else { + *va_arg(state->ap, unsigned int *) = n; + } + } + + fmt_sscanf_state_report_conv(state); + return 0; +} + +static int +fmt_sscanf_state_produce_char(struct fmt_sscanf_state *state) +{ + char c, *dest; + int i, width; + + if (state->flags & FMT_FORMAT_DISCARD) { + dest = NULL; + } else { + dest = va_arg(state->ap, char *); + } + + if (state->flags & FMT_FORMAT_CHECK_WIDTH) { + width = state->width; + } else { + width = 1; + } + + for (i = 0; i < width; i++) { + c = fmt_sscanf_state_consume_string(state); + + if ((c == '\0') || (c == EOF)) { + break; + } + + if (dest != NULL) { + *dest = c; + dest++; + } + } + + if (i < width) { + fmt_sscanf_state_restore_string(state); + } + + if ((dest != NULL) && (i != 0)) { + fmt_sscanf_state_report_conv(state); + } + + return 0; +} + +static int +fmt_sscanf_state_produce_str(struct fmt_sscanf_state *state) +{ + const char *orig; + char c, *dest; + + orig = state->str; + + fmt_sscanf_state_skip_space(state); + + if (state->flags & FMT_FORMAT_DISCARD) { + dest = NULL; + } else { + dest = va_arg(state->ap, char *); + } + + for (;;) { + c = fmt_sscanf_state_consume_string(state); + + if ((c == '\0') || (c == ' ') || (c == EOF)) { + break; + } + + if (dest != NULL) { + *dest = c; + dest++; + } + } + + fmt_sscanf_state_restore_string(state); + + if (state->str == orig) { + fmt_sscanf_state_report_error(state); + return ERROR_INVAL; + } + + if (dest != NULL) { + *dest = '\0'; + fmt_sscanf_state_report_conv(state); + } + + return 0; +} + +static int +fmt_sscanf_state_produce_nrchars(struct fmt_sscanf_state *state) +{ + *va_arg(state->ap, int *) = state->str - state->start; + return 0; +} + +static int +fmt_sscanf_state_produce(struct fmt_sscanf_state *state) +{ + switch (state->specifier) { + case FMT_SPECIFIER_INT: + return fmt_sscanf_state_produce_int(state); + case FMT_SPECIFIER_CHAR: + return fmt_sscanf_state_produce_char(state); + case FMT_SPECIFIER_STR: + return fmt_sscanf_state_produce_str(state); + case FMT_SPECIFIER_NRCHARS: + return fmt_sscanf_state_produce_nrchars(state); + case FMT_SPECIFIER_PERCENT: + fmt_sscanf_state_skip_space(state); + return fmt_sscanf_state_discard_char(state, '%'); + default: + fmt_sscanf_state_report_error(state); + return ERROR_INVAL; + } +} + +int +fmt_sscanf(const char *str, const char *format, ...) +{ + va_list ap; + int ret; + + va_start(ap, format); + ret = fmt_vsscanf(str, format, ap); + va_end(ap); + + return ret; +} + +int +fmt_vsscanf(const char *str, const char *format, va_list ap) +{ + struct fmt_sscanf_state state; + int error; + + fmt_sscanf_state_init(&state, str, format, ap); + + for (;;) { + error = fmt_sscanf_state_consume(&state); + + if (error == ERROR_AGAIN) { + continue; + } else if (error) { + break; + } + + error = fmt_sscanf_state_produce(&state); + + if (error) { + break; + } + } + + return fmt_sscanf_state_finalize(&state); +} diff --git a/lib/fmt.h b/lib/fmt.h new file mode 100644 index 0000000..babaa52 --- /dev/null +++ b/lib/fmt.h @@ -0,0 +1,69 @@ +/* + * Copyright (c) 2010-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. + * + * Upstream site with license notes : + * http://git.sceen.net/rbraun/librbraun.git/ + * + * + * Formatted string functions. + * + * The functions provided by this module implement a subset of the standard + * sprintf- and sscanf-like functions. + * + * sprintf: + * - flags: # 0 - ' ' (space) + + * - field width is supported + * - precision is supported + * + * sscanf: + * - flags: * + * - field width is supported + * + * common: + * - modifiers: hh h l ll z t + * - specifiers: d i o u x X c s p n % + */ + +#ifndef _FMT_H +#define _FMT_H + +#include <stdarg.h> +#include <stddef.h> + +int fmt_sprintf(char *str, const char *format, ...) + __attribute__((format(printf, 2, 3))); + +int fmt_vsprintf(char *str, const char *format, va_list ap) + __attribute__((format(printf, 2, 0))); + +int fmt_snprintf(char *str, size_t size, const char *format, ...) + __attribute__((format(printf, 3, 4))); + +int fmt_vsnprintf(char *str, size_t size, const char *format, va_list ap) + __attribute__((format(printf, 3, 0))); + +int fmt_sscanf(const char *str, const char *format, ...) + __attribute__((format(scanf, 2, 3))); + +int fmt_vsscanf(const char *str, const char *format, va_list ap) + __attribute__((format(scanf, 2, 0))); + +#endif /* _FMT_H */ diff --git a/lib/hash.h b/lib/hash.h new file mode 100644 index 0000000..92f3180 --- /dev/null +++ b/lib/hash.h @@ -0,0 +1,123 @@ +/* + * Copyright (c) 2010-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. + * + * Upstream site with license notes : + * http://git.sceen.net/rbraun/librbraun.git/ + * + * + * Hash functions for integers and strings. + * + * Integer hashing follows Thomas Wang's paper about his 32/64-bits mix + * functions : + * - https://gist.github.com/badboy/6267743 + * + * String hashing uses a variant of the djb2 algorithm with k=31, as in + * the implementation of the hashCode() method of the Java String class : + * - http://www.javamex.com/tutorials/collections/hash_function_technical.shtml + * + * Note that this algorithm isn't suitable to obtain usable 64-bits hashes + * and is expected to only serve as an array index producer. + * + * These functions all have a bits parameter that indicates the number of + * relevant bits the caller is interested in. When returning a hash, its + * value must be truncated so that it can fit in the requested bit size. + * It can be used by the implementation to select high or low bits, depending + * on their relative randomness. To get complete, unmasked hashes, use the + * HASH_ALLBITS macro. + */ + +#ifndef _HASH_H +#define _HASH_H + +#include <assert.h> +#include <stdint.h> + +#ifdef __LP64__ +#define HASH_ALLBITS 64 +#define hash_long(n, bits) hash_int64(n, bits) +#else /* __LP64__ */ +#define HASH_ALLBITS 32 +#define hash_long(n, bits) hash_int32(n, bits) +#endif + +static inline uint32_t +hash_int32(uint32_t n, unsigned int bits) +{ + uint32_t hash; + + hash = n; + hash = ~hash + (hash << 15); + hash ^= (hash >> 12); + hash += (hash << 2); + hash ^= (hash >> 4); + hash += (hash << 3) + (hash << 11); + hash ^= (hash >> 16); + + return hash >> (32 - bits); +} + +static inline uint64_t +hash_int64(uint64_t n, unsigned int bits) +{ + uint64_t hash; + + hash = n; + hash = ~hash + (hash << 21); + hash ^= (hash >> 24); + hash += (hash << 3) + (hash << 8); + hash ^= (hash >> 14); + hash += (hash << 2) + (hash << 4); + hash ^= (hash >> 28); + hash += (hash << 31); + + return hash >> (64 - bits); +} + +static inline uintptr_t +hash_ptr(const void *ptr, unsigned int bits) +{ + if (sizeof(uintptr_t) == 8) { + return hash_int64((uintptr_t)ptr, bits); + } else { + return hash_int32((uintptr_t)ptr, bits); + } +} + +static inline unsigned long +hash_str(const char *str, unsigned int bits) +{ + unsigned long hash; + char c; + + for (hash = 0; /* no condition */; str++) { + c = *str; + + if (c == '\0') { + break; + } + + hash = ((hash << 5) - hash) + c; + } + + return hash & ((1 << bits) - 1); +} + +#endif /* _HASH_H */ diff --git a/lib/list.h b/lib/list.h new file mode 100644 index 0000000..6e9c599 --- /dev/null +++ b/lib/list.h @@ -0,0 +1,538 @@ +/* + * Copyright (c) 2009-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. + * + * Upstream site with license notes : + * http://git.sceen.net/rbraun/librbraun.git/ + * + * + * Doubly-linked list. + */ + +#ifndef _LIST_H +#define _LIST_H + +#include <stdbool.h> +#include <stddef.h> + +#include "macros.h" + +/* + * Structure used as both head and node. + * + * This implementation relies on using the same type for both heads and nodes. + * + * It is recommended to encode the use of struct list variables in their names, + * e.g. struct list free_list or struct list free_objects is a good hint for a + * list of free objects. A declaration like struct list free_node clearly + * indicates it is used as part of a node in the free list. + */ +struct list { + struct list *prev; + struct list *next; +}; + +/* + * Static list initializer. + */ +#define LIST_INITIALIZER(list) { &(list), &(list) } + +/* + * Initialize a list. + */ +static inline void +list_init(struct list *list) +{ + list->prev = list; + list->next = list; +} + +/* + * Initialize a list node. + * + * A node is in no list when its node members point to NULL. + */ +static inline void +list_node_init(struct list *node) +{ + node->prev = NULL; + node->next = NULL; +} + +/* + * Return true if node is in no list. + */ +static inline bool +list_node_unlinked(const struct list *node) +{ + return node->prev == NULL; +} + +/* + * Return the first node of a list. + */ +static inline struct list * +list_first(const struct list *list) +{ + return list->next; +} + +/* + * Return the last node of a list. + */ +static inline struct list * +list_last(const struct list *list) +{ + return list->prev; +} + +/* + * Return the node next to the given node. + */ +static inline struct list * +list_next(const struct list *node) +{ + return node->next; +} + +/* + * Return the node previous to the given node. + */ +static inline struct list * +list_prev(const struct list *node) +{ + return node->prev; +} + +/* + * Return true if node is invalid and denotes one of the ends of the list. + */ +static inline bool +list_end(const struct list *list, const struct list *node) +{ + return list == node; +} + +/* + * Return true if list is empty. + */ +static inline bool +list_empty(const struct list *list) +{ + return list == list->next; +} + +/* + * Return true if list contains exactly one node. + */ +static inline bool +list_singular(const struct list *list) +{ + return !list_empty(list) && (list->next == list->prev); +} + +/* + * Split list2 by moving its nodes up to, but not including, the given + * node into list1, which can be in a stale state. + * + * If list2 is empty, or node is list2 or list2->next, list1 is merely + * initialized. + */ +static inline void +list_split(struct list *list1, struct list *list2, struct list *node) +{ + if (list_empty(list2) || (list2->next == node) || list_end(list2, node)) { + list_init(list1); + return; + } + + list1->next = list2->next; + list1->next->prev = list1; + + list1->prev = node->prev; + node->prev->next = list1; + + list2->next = node; + node->prev = list2; +} + +/* + * Append the nodes of list2 at the end of list1. + * + * After completion, list2 is stale. + */ +static inline void +list_concat(struct list *list1, const struct list *list2) +{ + struct list *last1, *first2, *last2; + + if (list_empty(list2)) { + return; + } + + last1 = list1->prev; + first2 = list2->next; + last2 = list2->prev; + + last1->next = first2; + first2->prev = last1; + + last2->next = list1; + list1->prev = last2; +} + +/* + * Set the new head of a list. + * + * This function is an optimized version of : + * list_init(&new_list); + * list_concat(&new_list, &old_list); + * + * After completion, old_head is stale. + */ +static inline void +list_set_head(struct list *new_head, const struct list *old_head) +{ + if (list_empty(old_head)) { + list_init(new_head); + return; + } + + *new_head = *old_head; + new_head->next->prev = new_head; + new_head->prev->next = new_head; +} + +/* + * Add a node between two nodes. + * + * This function is private. + */ +static inline void +list_add(struct list *prev, struct list *next, struct list *node) +{ + next->prev = node; + node->next = next; + + prev->next = node; + node->prev = prev; +} + +/* + * Insert a node at the head of a list. + */ +static inline void +list_insert_head(struct list *list, struct list *node) +{ + list_add(list, list->next, node); +} + +/* + * Insert a node at the tail of a list. + */ +static inline void +list_insert_tail(struct list *list, struct list *node) +{ + list_add(list->prev, list, node); +} + +/* + * Insert a node before another node. + */ +static inline void +list_insert_before(struct list *next, struct list *node) +{ + list_add(next->prev, next, node); +} + +/* + * Insert a node after another node. + */ +static inline void +list_insert_after(struct list *prev, struct list *node) +{ + list_add(prev, prev->next, node); +} + +/* + * Remove a node from a list. + * + * After completion, the node is stale. + */ +static inline void +list_remove(struct list *node) +{ + node->prev->next = node->next; + node->next->prev = node->prev; +} + +/* + * Macro that evaluates to the address of the structure containing the + * given node based on the given type and member. + */ +#define list_entry(node, type, member) structof(node, type, member) + +/* + * Get the first entry of a list. + */ +#define list_first_entry(list, type, member) \ + list_entry(list_first(list), type, member) + +/* + * Get the last entry of a list. + */ +#define list_last_entry(list, type, member) \ + list_entry(list_last(list), type, member) + +/* + * Get the entry next to the given entry. + */ +#define list_next_entry(entry, member) \ + list_entry(list_next(&(entry)->member), typeof(*(entry)), member) + +/* + * Get the entry previous to the given entry. + */ +#define list_prev_entry(entry, member) \ + list_entry(list_prev(&(entry)->member), typeof(*(entry)), member) + +/* + * Forge a loop to process all nodes of a list. + * + * The node must not be altered during the loop. + */ +#define list_for_each(list, node) \ +for (node = list_first(list); \ + !list_end(list, node); \ + node = list_next(node)) + +/* + * Forge a loop to process all nodes of a list. + */ +#define list_for_each_safe(list, node, tmp) \ +for (node = list_first(list), tmp = list_next(node); \ + !list_end(list, node); \ + node = tmp, tmp = list_next(node)) + +/* + * Version of list_for_each() that processes nodes backward. + */ +#define list_for_each_reverse(list, node) \ +for (node = list_last(list); \ + !list_end(list, node); \ + node = list_prev(node)) + +/* + * Version of list_for_each_safe() that processes nodes backward. + */ +#define list_for_each_reverse_safe(list, node, tmp) \ +for (node = list_last(list), tmp = list_prev(node); \ + !list_end(list, node); \ + node = tmp, tmp = list_prev(node)) + +/* + * Forge a loop to process all entries of a list. + * + * The entry node must not be altered during the loop. + */ +#define list_for_each_entry(list, entry, member) \ +for (entry = list_first_entry(list, typeof(*entry), member); \ + !list_end(list, &entry->member); \ + entry = list_next_entry(entry, member)) + +/* + * Forge a loop to process all entries of a list. + */ +#define list_for_each_entry_safe(list, entry, tmp, member) \ +for (entry = list_first_entry(list, typeof(*entry), member), \ + tmp = list_next_entry(entry, member); \ + !list_end(list, &entry->member); \ + entry = tmp, tmp = list_next_entry(entry, member)) + +/* + * Version of list_for_each_entry() that processes entries backward. + */ +#define list_for_each_entry_reverse(list, entry, member) \ +for (entry = list_last_entry(list, typeof(*entry), member); \ + !list_end(list, &entry->member); \ + entry = list_prev_entry(entry, member)) + +/* + * Version of list_for_each_entry_safe() that processes entries backward. + */ +#define list_for_each_entry_reverse_safe(list, entry, tmp, member) \ +for (entry = list_last_entry(list, typeof(*entry), member), \ + tmp = list_prev_entry(entry, member); \ + !list_end(list, &entry->member); \ + entry = tmp, tmp = list_prev_entry(entry, member)) + +/* + * Lockless variants + * + * This is a subset of the main interface that only supports forward traversal. + * In addition, list_end() is also allowed in read-side critical sections. + */ + +/* + * These macros can be replaced by actual functions in an environment + * that provides lockless synchronization such as RCU. + */ +#define llsync_store_ptr(ptr, value) ((ptr) = (value)) +#define llsync_load_ptr(ptr) (ptr) + +/* + * Return the first node of a list. + */ +static inline struct list * +list_llsync_first(const struct list *list) +{ + return llsync_load_ptr(list->next); +} + +/* + * Return the node next to the given node. + */ +static inline struct list * +list_llsync_next(const struct list *node) +{ + return llsync_load_ptr(node->next); +} + +/* + * Add a node between two nodes. + * + * This function is private. + */ +static inline void +list_llsync_add(struct list *prev, struct list *next, struct list *node) +{ + node->next = next; + node->prev = prev; + llsync_store_ptr(prev->next, node); + next->prev = node; +} + +/* + * Insert a node at the head of a list. + */ +static inline void +list_llsync_insert_head(struct list *list, struct list *node) +{ + list_llsync_add(list, list->next, node); +} + +/* + * Insert a node at the tail of a list. + */ +static inline void +list_llsync_insert_tail(struct list *list, struct list *node) +{ + list_llsync_add(list->prev, list, node); +} + +/* + * Insert a node before another node. + */ +static inline void +list_llsync_insert_before(struct list *next, struct list *node) +{ + list_llsync_add(next->prev, next, node); +} + +/* + * Insert a node after another node. + */ +static inline void +list_llsync_insert_after(struct list *prev, struct list *node) +{ + list_llsync_add(prev, prev->next, node); +} + +/* + * Remove a node from a list. + * + * After completion, the node is stale. + */ +static inline void +list_llsync_remove(struct list *node) +{ + node->next->prev = node->prev; + llsync_store_ptr(node->prev->next, node->next); +} + +/* + * Macro that evaluates to the address of the structure containing the + * given node based on the given type and member. + */ +#define list_llsync_entry(node, type, member) \ + structof(llsync_load_ptr(node), type, member) + +/* + * Get the first entry of a list. + * + * Unlike list_first_entry(), this macro may evaluate to NULL, because + * the node pointer can only be read once, preventing the combination + * of lockless list_empty()/list_first_entry() variants. + */ +#define list_llsync_first_entry(list, type, member) \ +MACRO_BEGIN \ + struct list *___list; \ + struct list *___first; \ + \ + ___list = (list); \ + ___first = list_llsync_first(___list); \ + list_end(___list, ___first) \ + ? NULL \ + : list_entry(___first, type, member); \ +MACRO_END + +/* + * Get the entry next to the given entry. + * + * Unlike list_next_entry(), this macro may evaluate to NULL, because + * the node pointer can only be read once, preventing the combination + * of lockless list_empty()/list_next_entry() variants. + */ +#define list_llsync_next_entry(entry, member) \ + list_llsync_first_entry(&entry->member, typeof(*entry), member) + +/* + * Forge a loop to process all nodes of a list. + * + * The node must not be altered during the loop. + */ +#define list_llsync_for_each(list, node) \ +for (node = list_llsync_first(list); \ + !list_end(list, node); \ + node = list_llsync_next(node)) + +/* + * Forge a loop to process all entries of a list. + * + * The entry node must not be altered during the loop. + */ +#define list_llsync_for_each_entry(list, entry, member) \ +for (entry = list_llsync_entry(list_first(list), \ + typeof(*entry), member); \ + !list_end(list, &entry->member); \ + entry = list_llsync_entry(list_next(&entry->member), \ + typeof(*entry), member)) + +#endif /* _LIST_H */ diff --git a/lib/macros.h b/lib/macros.h new file mode 100644 index 0000000..a61f80e --- /dev/null +++ b/lib/macros.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2009-2015 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. + * + * Upstream site with license notes : + * http://git.sceen.net/rbraun/librbraun.git/ + * + * + * Helper macros. + */ + +#ifndef _MACROS_H +#define _MACROS_H + +#if !defined(__GNUC__) || (__GNUC__ < 4) +#error "GCC 4+ required" +#endif + +#include <stddef.h> + +#define MACRO_BEGIN ({ +#define MACRO_END }) + +#define __QUOTE(x) #x +#define QUOTE(x) __QUOTE(x) + +#define STRLEN(x) (sizeof(x) - 1) +#define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0])) + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) +#define MAX(a, b) ((a) > (b) ? (a) : (b)) + +#define DIV_CEIL(n, d) (((n) + (d) - 1) / (d)) + +#define P2ALIGNED(x, a) (((x) & ((a) - 1)) == 0) +#define ISP2(x) P2ALIGNED(x, x) +#define P2ALIGN(x, a) ((x) & -(a)) /* decreases if not aligned */ +#define P2ROUND(x, a) (-(-(x) & -(a))) /* increases if not aligned */ +#define P2END(x, a) (-(~(x) & -(a))) /* always increases */ + +#define structof(ptr, type, member) \ + ((type *)((char *)(ptr) - offsetof(type, member))) + +#define likely(expr) __builtin_expect(!!(expr), 1) +#define unlikely(expr) __builtin_expect(!!(expr), 0) + +#define barrier() asm volatile("" : : : "memory") + +/* + * The following macros may be provided by the C environment. + */ + +#ifndef __noinline +#define __noinline __attribute__((noinline)) +#endif + +#ifndef __aligned +#define __aligned(x) __attribute__((aligned(x))) +#endif + +#ifndef __always_inline +#define __always_inline inline __attribute__((always_inline)) +#endif + +#ifndef __section +#define __section(x) __attribute__((section(x))) +#endif + +#ifndef __packed +#define __packed __attribute__((packed)) +#endif + +#ifndef __unused +#define __unused __attribute__((unused)) +#endif + +#endif /* _MACROS_H */ diff --git a/lib/shell.c b/lib/shell.c new file mode 100644 index 0000000..8272b24 --- /dev/null +++ b/lib/shell.c @@ -0,0 +1,1212 @@ +/* + * Copyright (c) 2015-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"), + * 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. + * + * Upstream site with license notes : + * http://git.sceen.net/rbraun/librbraun.git/ + */ + +#include <stddef.h> +#include <stdio.h> +#include <stdint.h> +#include <string.h> + +#include <lib/macros.h> +#include <lib/hash.h> +#include <lib/shell.h> + +#include <src/error.h> +#include <src/mutex.h> +#include <src/panic.h> +#include <src/thread.h> +#include <src/uart.h> + +#define SHELL_STACK_SIZE 4096 + +/* + * Binary exponent and size of the hash table used to store commands. + */ +#define SHELL_HTABLE_BITS 6 +#define SHELL_HTABLE_SIZE (1 << SHELL_HTABLE_BITS) + +struct shell_bucket { + struct shell_cmd *cmd; +}; + +/* + * Hash table for quick command lookup. + */ +static struct shell_bucket shell_htable[SHELL_HTABLE_SIZE]; + +#define SHELL_COMPLETION_MATCH_FMT "-16s" +#define SHELL_COMPLETION_NR_MATCHES_PER_LINE 4 + +/* + * Sorted command list. + */ +static struct shell_cmd *shell_list; + +/* + * Lock protecting access to the hash table and list of commands. + * + * Note that this lock only protects access to the commands containers, + * not the commands themselves. In particular, it is not necessary to + * hold this lock when a command is used, i.e. when accessing a command + * name, function pointer, or description. + */ +static struct mutex shell_lock; + +/* + * Escape sequence states. + * + * Here is an incomplete description of escape sequences : + * http://en.wikipedia.org/wiki/ANSI_escape_code + * + * These values must be different from 0. + */ +#define SHELL_ESC_STATE_START 1 +#define SHELL_ESC_STATE_CSI 2 + +/* + * This value changes depending on the standard used and was chosen arbitrarily. + */ +#define SHELL_ESC_SEQ_MAX_SIZE 8 + +typedef void (*shell_esc_seq_fn)(void); + +struct shell_esc_seq { + const char *str; + shell_esc_seq_fn fn; +}; + +#define SHELL_LINE_MAX_SIZE 64 + +/* + * Line containing a shell entry. + * + * The string must be nul-terminated. The size doesn't include this + * additional nul character, the same way strlen() doesn't account for it. + */ +struct shell_line { + char str[SHELL_LINE_MAX_SIZE]; + unsigned long size; +}; + +/* + * Number of entries in the history. + * + * One of these entryes is used as the current line. + */ +#define SHELL_HISTORY_SIZE 21 + +#if SHELL_HISTORY_SIZE == 0 +#error "shell history size must be non-zero" +#endif /* SHELL_HISTORY_SIZE == 0 */ + +/* + * Shell history. + * + * The history is never empty. There is always at least one entry, the + * current line, referenced by the newest (most recent) index. The array + * is used like a circular buffer, i.e. old entries are implicitely + * erased by new ones. The index references the entry used as a template + * for the current line. + */ +static struct shell_line shell_history[SHELL_HISTORY_SIZE]; +static unsigned long shell_history_newest; +static unsigned long shell_history_oldest; +static unsigned long shell_history_index; + +/* + * Cursor within the current line. + */ +static unsigned long shell_cursor; + +#define SHELL_SEPARATOR ' ' + +/* + * Commonly used backspace control characters. + * + * XXX Adjust for your needs. + */ +#define SHELL_ERASE_BS '\b' +#define SHELL_ERASE_DEL '\x7f' + +/* + * Buffer used to store the current line during argument processing. + * + * The pointers in the argv array point inside this buffer. The + * separators immediately following the arguments are replaced with + * nul characters. + */ +static char shell_tmp_line[SHELL_LINE_MAX_SIZE]; + +#define SHELL_MAX_ARGS 16 + +static int shell_argc; +static char *shell_argv[SHELL_MAX_ARGS]; + +static const char * +shell_find_word(const char *str) +{ + for (;;) { + if ((*str == '\0') || (*str != SHELL_SEPARATOR)) { + break; + } + + str++; + } + + return str; +} + +void +shell_cmd_init(struct shell_cmd *cmd, const char *name, + shell_fn_t fn, const char *usage, + const char *short_desc, const char *long_desc) +{ + cmd->ht_next = NULL; + cmd->ls_next = NULL; + cmd->name = name; + cmd->fn = fn; + cmd->usage = usage; + cmd->short_desc = short_desc; + cmd->long_desc = long_desc; +} + +static const char * +shell_cmd_name(const struct shell_cmd *cmd) +{ + return cmd->name; +} + +static inline struct shell_bucket * +shell_bucket_get(const char *name) +{ + return &shell_htable[hash_str(name, SHELL_HTABLE_BITS)]; +} + +static void +shell_cmd_acquire(void) +{ + mutex_lock(&shell_lock); +} + +static void +shell_cmd_release(void) +{ + mutex_unlock(&shell_lock); +} + +static const struct shell_cmd * +shell_cmd_lookup(const char *name) +{ + const struct shell_bucket *bucket; + const struct shell_cmd *cmd; + + shell_cmd_acquire(); + + bucket = shell_bucket_get(name); + + for (cmd = bucket->cmd; cmd != NULL; cmd = cmd->ht_next) { + if (strcmp(cmd->name, name) == 0) { + break; + } + } + + shell_cmd_release(); + + return cmd; +} + +/* + * Look up the first command that matches a given string. + * + * The input string is defined by the given string pointer and size. + * + * The global lock must be acquired before calling this function. + */ +static const struct shell_cmd * +shell_cmd_match(const struct shell_cmd *cmd, const char *str, + unsigned long size) +{ + while (cmd != NULL) { + if (strncmp(cmd->name, str, size) == 0) { + return cmd; + } + + cmd = cmd->ls_next; + } + + return NULL; +} + +/* + * Attempt command auto-completion. + * + * The given string is the beginning of a command, or the empty string. + * The sizep parameter initially points to the size of the given string. + * If the string matches any registered command, the cmdp pointer is + * updated to point to the first matching command in the sorted list of + * commands, and sizep is updated to the number of characters in the + * command name that are common in subsequent commands. The command + * pointer and the returned size can be used to print a list of commands + * 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. + * + * The global lock must be acquired before calling this function. + */ +static int +shell_cmd_complete(const char *str, unsigned long *sizep, + const struct shell_cmd **cmdp) +{ + const struct shell_cmd *cmd, *next; + unsigned long size; + + size = *sizep; + + /* + * Start with looking up a command that matches the given argument. + * If there is no match, return an error. + */ + cmd = shell_cmd_match(shell_list, str, size); + + if (cmd == NULL) { + return ERROR_INVAL; + } + + *cmdp = cmd; + + /* + * If at least one command matches, try to complete it. + * There can be two cases : + * 1/ There is one and only one match, which is directly returned. + * 2/ There are several matches, in which case the common length is + * computed. + */ + next = cmd->ls_next; + + if ((next == NULL) + || (strncmp(cmd->name, next->name, size) != 0)) { + *sizep = strlen(cmd->name); + return 0; + } + + /* + * When computing the common length, all the commands that can match + * must be evaluated. Considering the current command is the first + * that can match, the only other variable missing is the last + * command that can match. + */ + while (next->ls_next != NULL) { + if (strncmp(cmd->name, next->ls_next->name, size) != 0) { + break; + } + + next = next->ls_next; + } + + if (size == 0) { + size = 1; + } + + while ((cmd->name[size - 1] != '\0') + && (cmd->name[size - 1] == next->name[size - 1])) { + size++; + } + + size--; + *sizep = size; + return ERROR_AGAIN; +} + +/* + * Print a list of commands eligible for completion, starting at the + * given command. Other eligible commands share the same prefix, as + * defined by the size argument. + * + * The global lock must be acquired before calling this function. + */ +static void +shell_cmd_print_matches(const struct shell_cmd *cmd, unsigned long size) +{ + const struct shell_cmd *tmp; + unsigned int i; + + printf("\n"); + + for (tmp = cmd, i = 1; tmp != NULL; tmp = tmp->ls_next, i++) { + if (strncmp(cmd->name, tmp->name, size) != 0) { + break; + } + + printf("%" SHELL_COMPLETION_MATCH_FMT, tmp->name); + + if ((i % SHELL_COMPLETION_NR_MATCHES_PER_LINE) == 0) { + printf("\n"); + } + } + + if ((i % SHELL_COMPLETION_NR_MATCHES_PER_LINE) != 1) { + printf("\n"); + } +} + +static int +shell_cmd_check_char(char c) +{ + if (((c >= 'a') && (c <= 'z')) + || ((c >= 'A') && (c <= 'Z')) + || ((c >= '0') && (c <= '9')) + || (c == '-') + || (c == '_')) { + return 0; + } + + return ERROR_INVAL; +} + +static int +shell_cmd_check(const struct shell_cmd *cmd) +{ + unsigned long i; + int error; + + for (i = 0; cmd->name[i] != '\0'; i++) { + error = shell_cmd_check_char(cmd->name[i]); + + if (error) { + return error; + } + } + + if (i == 0) { + return ERROR_INVAL; + } + + return 0; +} + +/* + * The global lock must be acquired before calling this function. + */ +static void +shell_cmd_add_list(struct shell_cmd *cmd) +{ + struct shell_cmd *prev, *next; + + prev = shell_list; + + if ((prev == NULL) + || (strcmp(cmd->name, prev->name) < 0)) { + shell_list = cmd; + cmd->ls_next = prev; + return; + } + + for (;;) { + next = prev->ls_next; + + if ((next == NULL) + || (strcmp(cmd->name, next->name) < 0)) { + break; + } + + prev = next; + } + + prev->ls_next = cmd; + cmd->ls_next = next; +} + +/* + * The global lock must be acquired before calling this function. + */ +static int +shell_cmd_add(struct shell_cmd *cmd) +{ + struct shell_bucket *bucket; + struct shell_cmd *tmp; + + bucket = shell_bucket_get(cmd->name); + tmp = bucket->cmd; + + if (tmp == NULL) { + bucket->cmd = cmd; + goto out; + } + + for (;;) { + if (strcmp(cmd->name, tmp->name) == 0) { + printf("shell: error: %s: shell command name collision", cmd->name); + return ERROR_EXIST; + } + + if (tmp->ht_next == NULL) { + break; + } + + tmp = tmp->ht_next; + } + + tmp->ht_next = cmd; + +out: + shell_cmd_add_list(cmd); + return 0; +} + +int +shell_cmd_register(struct shell_cmd *cmd) +{ + int error; + + error = shell_cmd_check(cmd); + + if (error) { + return error; + } + + shell_cmd_acquire(); + error = shell_cmd_add(cmd); + shell_cmd_release(); + + return error; +} + +static inline const char * +shell_line_str(const struct shell_line *line) +{ + return line->str; +} + +static inline unsigned long +shell_line_size(const struct shell_line *line) +{ + return line->size; +} + +static inline void +shell_line_reset(struct shell_line *line) +{ + line->str[0] = '\0'; + line->size = 0; +} + +static inline void +shell_line_copy(struct shell_line *dest, const struct shell_line *src) +{ + strcpy(dest->str, src->str); + dest->size = src->size; +} + +static inline int +shell_line_cmp(const struct shell_line *a, const struct shell_line *b) +{ + return strcmp(a->str, b->str); +} + +static int +shell_line_insert(struct shell_line *line, unsigned long index, char c) +{ + unsigned long remaining_chars; + + if (index > line->size) { + return ERROR_INVAL; + } + + if ((line->size + 1) == sizeof(line->str)) { + return ERROR_NOMEM; + } + + remaining_chars = line->size - index; + + if (remaining_chars != 0) { + memmove(&line->str[index + 1], &line->str[index], remaining_chars); + } + + line->str[index] = c; + line->size++; + line->str[line->size] = '\0'; + return 0; +} + +static int +shell_line_erase(struct shell_line *line, unsigned long index) +{ + unsigned long remaining_chars; + + if (index >= line->size) { + return ERROR_INVAL; + } + + remaining_chars = line->size - index - 1; + + if (remaining_chars != 0) { + memmove(&line->str[index], &line->str[index + 1], remaining_chars); + } + + line->size--; + line->str[line->size] = '\0'; + return 0; +} + +static struct shell_line * +shell_history_get(unsigned long index) +{ + return &shell_history[index % ARRAY_SIZE(shell_history)]; +} + +static struct shell_line * +shell_history_get_newest(void) +{ + return shell_history_get(shell_history_newest); +} + +static struct shell_line * +shell_history_get_index(void) +{ + return shell_history_get(shell_history_index); +} + +static void +shell_history_reset_index(void) +{ + shell_history_index = shell_history_newest; +} + +static inline int +shell_history_same_newest(void) +{ + return (shell_history_newest != shell_history_oldest) + && shell_line_cmp(shell_history_get_newest(), + shell_history_get(shell_history_newest - 1)) == 0; +} + +static void +shell_history_push(void) +{ + if ((shell_line_size(shell_history_get_newest()) == 0) + || shell_history_same_newest()) { + shell_history_reset_index(); + return; + } + + shell_history_newest++; + shell_history_reset_index(); + + /* Mind integer overflows */ + if ((shell_history_newest - shell_history_oldest) + >= ARRAY_SIZE(shell_history)) { + shell_history_oldest = shell_history_newest + - ARRAY_SIZE(shell_history) + 1; + } +} + +static void +shell_history_back(void) +{ + if (shell_history_index == shell_history_oldest) { + return; + } + + shell_history_index--; + shell_line_copy(shell_history_get_newest(), shell_history_get_index()); +} + +static void +shell_history_forward(void) +{ + if (shell_history_index == shell_history_newest) { + return; + } + + shell_history_index++; + + if (shell_history_index == shell_history_newest) { + shell_line_reset(shell_history_get_newest()); + } else { + shell_line_copy(shell_history_get_newest(), shell_history_get_index()); + } +} + +static void +shell_cmd_help(int argc, char *argv[]) +{ + const struct shell_cmd *cmd; + + if (argc > 2) { + argc = 2; + argv[1] = "help"; + } + + if (argc == 2) { + cmd = shell_cmd_lookup(argv[1]); + + if (cmd == NULL) { + printf("shell: help: %s: command not found\n", argv[1]); + return; + } + + printf("usage: %s\n%s\n", cmd->usage, cmd->short_desc); + + if (cmd->long_desc != NULL) { + printf("\n%s\n", cmd->long_desc); + } + + return; + } + + shell_cmd_acquire(); + + for (cmd = shell_list; cmd != NULL; cmd = cmd->ls_next) { + printf("%13s %s\n", cmd->name, cmd->short_desc); + } + + shell_cmd_release(); +} + +static void +shell_cmd_history(int argc, char *argv[]) +{ + unsigned long i; + + (void)argc; + (void)argv; + + /* Mind integer overflows */ + for (i = shell_history_oldest; i != shell_history_newest; i++) { + printf("%6lu %s\n", i - shell_history_oldest, + shell_line_str(shell_history_get(i))); + } +} + +static struct shell_cmd shell_default_cmds[] = { + SHELL_CMD_INITIALIZER("help", shell_cmd_help, + "help [command]", + "obtain help about shell commands"), + SHELL_CMD_INITIALIZER("history", shell_cmd_history, + "history", + "display history list"), +}; + +static void +shell_prompt(void) +{ + printf("shell> "); +} + +static void +shell_reset(void) +{ + shell_line_reset(shell_history_get_newest()); + shell_cursor = 0; + shell_prompt(); +} + +static void +shell_erase(void) +{ + struct shell_line *current_line; + unsigned long remaining_chars; + + current_line = shell_history_get_newest(); + remaining_chars = shell_line_size(current_line); + + while (shell_cursor != remaining_chars) { + putchar(' '); + shell_cursor++; + } + + while (remaining_chars != 0) { + printf("\b \b"); + remaining_chars--; + } + + shell_cursor = 0; +} + +static void +shell_restore(void) +{ + struct shell_line *current_line; + + current_line = shell_history_get_newest(); + printf("%s", shell_line_str(current_line)); + shell_cursor = shell_line_size(current_line); +} + +static int +shell_is_ctrl_char(char c) +{ + return ((c < ' ') || (c >= 0x7f)); +} + +static void +shell_process_left(void) +{ + if (shell_cursor == 0) { + return; + } + + shell_cursor--; + printf("\e[1D"); +} + +static int +shell_process_right(void) +{ + if (shell_cursor >= shell_line_size(shell_history_get_newest())) { + return ERROR_AGAIN; + } + + shell_cursor++; + printf("\e[1C"); + return 0; +} + +static void +shell_process_up(void) +{ + shell_erase(); + shell_history_back(); + shell_restore(); +} + +static void +shell_process_down(void) +{ + shell_erase(); + shell_history_forward(); + shell_restore(); +} + +static void +shell_process_backspace(void) +{ + struct shell_line *current_line; + unsigned long remaining_chars; + int error; + + current_line = shell_history_get_newest(); + error = shell_line_erase(current_line, shell_cursor - 1); + + if (error) { + return; + } + + shell_cursor--; + printf("\b%s ", shell_line_str(current_line) + shell_cursor); + remaining_chars = shell_line_size(current_line) - shell_cursor + 1; + + while (remaining_chars != 0) { + putchar('\b'); + remaining_chars--; + } +} + +static int +shell_process_raw_char(char c) +{ + struct shell_line *current_line; + unsigned long remaining_chars; + int error; + + current_line = shell_history_get_newest(); + error = shell_line_insert(current_line, shell_cursor, c); + + if (error) { + printf("\nshell: line too long\n"); + return error; + } + + shell_cursor++; + + if (shell_cursor == shell_line_size(current_line)) { + putchar(c); + goto out; + } + + /* + * This assumes that the backspace character only moves the cursor + * without erasing characters. + */ + printf("%s", shell_line_str(current_line) + shell_cursor - 1); + remaining_chars = shell_line_size(current_line) - shell_cursor; + + while (remaining_chars != 0) { + putchar('\b'); + remaining_chars--; + } + +out: + return 0; +} + +static int +shell_process_tabulation(void) +{ + const struct shell_cmd *cmd = NULL; /* GCC */ + const char *name, *str, *word; + unsigned long i, size, cmd_cursor; + int error; + + shell_cmd_acquire(); + + str = shell_line_str(shell_history_get_newest()); + word = shell_find_word(str); + size = shell_cursor - (word - str); + cmd_cursor = shell_cursor - size; + + error = shell_cmd_complete(word, &size, &cmd); + + if (error && (error != ERROR_AGAIN)) { + error = 0; + goto out; + } + + if (error == ERROR_AGAIN) { + unsigned long cursor; + + cursor = shell_cursor; + shell_cmd_print_matches(cmd, size); + shell_prompt(); + shell_restore(); + + /* Keep existing arguments as they are */ + while (shell_cursor != cursor) { + shell_process_left(); + } + } + + name = shell_cmd_name(cmd); + + while (shell_cursor != cmd_cursor) { + shell_process_backspace(); + } + + for (i = 0; i < size; i++) { + error = shell_process_raw_char(name[i]); + + if (error) { + goto out; + } + } + + error = 0; + +out: + shell_cmd_release(); + return error; +} + +static void +shell_esc_seq_up(void) +{ + shell_process_up(); +} + +static void +shell_esc_seq_down(void) +{ + shell_process_down(); +} + +static void +shell_esc_seq_next(void) +{ + shell_process_right(); +} + +static void +shell_esc_seq_prev(void) +{ + shell_process_left(); +} + +static void +shell_esc_seq_home(void) +{ + while (shell_cursor != 0) { + shell_process_left(); + } +} + +static void +shell_esc_seq_del(void) +{ + int error; + + error = shell_process_right(); + + if (error) { + return; + } + + shell_process_backspace(); +} + +static void +shell_esc_seq_end(void) +{ + unsigned long size; + + size = shell_line_size(shell_history_get_newest()); + + while (shell_cursor < size) { + shell_process_right(); + } +} + +static const struct shell_esc_seq shell_esc_seqs[] = { + { "A", shell_esc_seq_up }, + { "B", shell_esc_seq_down }, + { "C", shell_esc_seq_next }, + { "D", shell_esc_seq_prev }, + { "H", shell_esc_seq_home }, + { "1~", shell_esc_seq_home }, + { "3~", shell_esc_seq_del }, + { "F", shell_esc_seq_end }, + { "4~", shell_esc_seq_end }, +}; + +static const struct shell_esc_seq * +shell_esc_seq_lookup(const char *str) +{ + unsigned long i; + + for (i = 0; i < ARRAY_SIZE(shell_esc_seqs); i++) { + if (strcmp(shell_esc_seqs[i].str, str) == 0) { + return &shell_esc_seqs[i]; + } + } + + return NULL; +} + +/* + * Process a single escape sequence character. + * + * Return the next escape state or 0 if the sequence is complete. + */ +static int +shell_process_esc_sequence(char c) +{ + static char str[SHELL_ESC_SEQ_MAX_SIZE], *ptr = str; + + const struct shell_esc_seq *seq; + uintptr_t index; + + index = ptr - str; + + if (index >= (ARRAY_SIZE(str) - 1)) { + printf("shell: escape sequence too long\n"); + goto reset; + } + + *ptr = c; + ptr++; + *ptr = '\0'; + + if ((c >= '@') && (c <= '~')) { + seq = shell_esc_seq_lookup(str); + + if (seq != NULL) { + seq->fn(); + } + + goto reset; + } + + return SHELL_ESC_STATE_CSI; + +reset: + ptr = str; + return 0; +} + +static int +shell_process_args(void) +{ + unsigned long i; + char c, prev; + int j; + + snprintf(shell_tmp_line, sizeof(shell_tmp_line), "%s", + shell_line_str(shell_history_get_newest())); + + for (i = 0, j = 0, prev = SHELL_SEPARATOR; + (c = shell_tmp_line[i]) != '\0'; + i++, prev = c) { + if (c == SHELL_SEPARATOR) { + if (prev != SHELL_SEPARATOR) { + shell_tmp_line[i] = '\0'; + } + } else { + if (prev == SHELL_SEPARATOR) { + shell_argv[j] = &shell_tmp_line[i]; + j++; + + if (j == ARRAY_SIZE(shell_argv)) { + printf("shell: too many arguments\n"); + return ERROR_INVAL; + } + + shell_argv[j] = NULL; + } + } + } + + shell_argc = j; + return 0; +} + +static void +shell_process_line(void) +{ + const struct shell_cmd *cmd; + int error; + + cmd = NULL; + error = shell_process_args(); + + if (error) { + goto out; + } + + if (shell_argc == 0) { + goto out; + } + + cmd = shell_cmd_lookup(shell_argv[0]); + + if (cmd == NULL) { + printf("shell: %s: command not found\n", shell_argv[0]); + goto out; + } + +out: + shell_history_push(); + + if (cmd != NULL) { + cmd->fn(shell_argc, shell_argv); + } +} + +/* + * Process a single control character. + * + * Return an error if the caller should reset the current line state. + */ +static int +shell_process_ctrl_char(char c) +{ + switch (c) { + case SHELL_ERASE_BS: + case SHELL_ERASE_DEL: + shell_process_backspace(); + break; + case '\t': + return shell_process_tabulation(); + case '\n': + case '\r': + putchar('\n'); + shell_process_line(); + return ERROR_AGAIN; + default: + return 0; + } + + return 0; +} + +static void +shell_run(void *arg) +{ + int c, error, escape; + + (void)arg; + + for (;;) { + shell_reset(); + escape = 0; + + for (;;) { + c = getchar(); + + if (escape) { + switch (escape) { + case SHELL_ESC_STATE_START: + /* XXX CSI and SS3 sequence processing is the same */ + if ((c == '[') || (c == 'O')) { + escape = SHELL_ESC_STATE_CSI; + } else { + escape = 0; + } + + break; + case SHELL_ESC_STATE_CSI: + escape = shell_process_esc_sequence(c); + break; + default: + escape = 0; + } + + error = 0; + } else if (shell_is_ctrl_char(c)) { + if (c == '\e') { + escape = SHELL_ESC_STATE_START; + error = 0; + } else { + error = shell_process_ctrl_char(c); + + if (error) { + break; + } + } + } else { + error = shell_process_raw_char(c); + } + + if (error) { + break; + } + } + } +} + +void +shell_setup(void) +{ + int error; + + mutex_init(&shell_lock); + SHELL_REGISTER_CMDS(shell_default_cmds); + + error = thread_create(NULL, shell_run, NULL, "shell", + SHELL_STACK_SIZE, THREAD_MIN_PRIORITY); + + if (error) { + panic("shell: unable to create shell thread"); + } +} diff --git a/lib/shell.h b/lib/shell.h new file mode 100644 index 0000000..f66fbf7 --- /dev/null +++ b/lib/shell.h @@ -0,0 +1,94 @@ +/* + * Copyright (c) 2015-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"), + * 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. + * + * Upstream site with license notes : + * http://git.sceen.net/rbraun/librbraun.git/ + * + * + * Minimalist shell for embedded systems. + */ + +#ifndef _SHELL_H +#define _SHELL_H + +#include <stddef.h> + +#include <lib/macros.h> + +#include <src/error.h> + +#define SHELL_REGISTER_CMDS(cmds) \ +MACRO_BEGIN \ + size_t ___i; \ + int ___error; \ + \ + for (___i = 0; ___i < ARRAY_SIZE(cmds); ___i++) { \ + ___error = shell_cmd_register(&(cmds)[___i]); \ + error_check(___error, __func__); \ + } \ +MACRO_END + +typedef void (*shell_fn_t)(int argc, char *argv[]); + +struct shell_cmd { + struct shell_cmd *ht_next; + struct shell_cmd *ls_next; + const char *name; + shell_fn_t fn; + const char *usage; + const char *short_desc; + const char *long_desc; +}; + +/* + * Static shell command initializers. + */ +#define SHELL_CMD_INITIALIZER(name, fn, usage, short_desc) \ + { NULL, NULL, name, fn, usage, short_desc, NULL } +#define SHELL_CMD_INITIALIZER2(name, fn, usage, short_desc, long_desc) \ + { NULL, NULL, name, fn, usage, short_desc, long_desc } + +/* + * Initialize a shell command structure. + */ +void shell_cmd_init(struct shell_cmd *cmd, const char *name, + shell_fn_t fn, const char *usage, + const char *short_desc, const char *long_desc); + +/* + * Initialize the shell module. + * + * On return, shell commands can be registered. + */ +void shell_setup(void); + +/* + * Register a shell command. + * + * The command name must be unique. It must not include characters outside + * the [a-zA-Z0-9-_] class. + * + * The structure passed when calling this function is directly reused by + * the shell module and must persist in memory. + */ +int shell_cmd_register(struct shell_cmd *cmd); + +#endif /* _SHELL_H */ |