initial version made by removing all csp stuff from cspemu
This commit is contained in:
@@ -0,0 +1,18 @@
|
||||
idf_component_register(
|
||||
INCLUDE_DIRS "libconsole/include"
|
||||
)
|
||||
|
||||
set(CONSOLE_FILE_SUPPORT OFF)
|
||||
set(CONSOLE_USE_FILE_IO_STREAMS OFF)
|
||||
set(CONSOLE_USE_TERMIOS OFF)
|
||||
set(CONSOLE_USE_MEMSTREAM OFF)
|
||||
set(CONSOLE_MAX_NUM_ARGS 16)
|
||||
set(CONSOLE_LINE_BUF_LEN 128)
|
||||
set(CONSOLE_PROMPT_MAX_LEN 24)
|
||||
set(CONSOLE_HISTORY_LEN 8)
|
||||
set(CONSOLE_HAVE_CSP OFF)
|
||||
set(CONSOLE_USE_CSP_COMMANDS OFF)
|
||||
|
||||
add_subdirectory(libconsole)
|
||||
|
||||
target_link_libraries(${COMPONENT_LIB} INTERFACE console argtable3)
|
||||
@@ -0,0 +1,54 @@
|
||||
build
|
||||
*.swp
|
||||
.lock*
|
||||
.waf*
|
||||
.waf3*
|
||||
waf-*/
|
||||
*.o
|
||||
*.d
|
||||
*.pyc
|
||||
.project
|
||||
.cproject
|
||||
~*
|
||||
pdebug*
|
||||
*.tar*
|
||||
tags
|
||||
.DS_store
|
||||
|
||||
# ninja files
|
||||
build.ninja
|
||||
rules.ninja
|
||||
.ninja_deps
|
||||
.ninja_log
|
||||
|
||||
# generated by cmake
|
||||
CMakeCache.txt
|
||||
*.cmake
|
||||
CMakeFiles
|
||||
vcom
|
||||
Makefile
|
||||
*.cbp
|
||||
*.a
|
||||
|
||||
.idea/
|
||||
.DS_Store
|
||||
|
||||
# Visual Studio clutter
|
||||
_ReSharper*
|
||||
*.sdf
|
||||
*.suo
|
||||
*.dir
|
||||
*.vcxproj*
|
||||
*.sln
|
||||
.vs
|
||||
CMakeSettings.json
|
||||
Win32
|
||||
x64
|
||||
Debug
|
||||
Release
|
||||
MinSizeRel
|
||||
RelWithDebInfo
|
||||
*.opensdf
|
||||
|
||||
# this is generated when using in-tree make build
|
||||
include/console/config.h
|
||||
@@ -0,0 +1,73 @@
|
||||
cmake_minimum_required(VERSION 3.13)
|
||||
project(lib-console)
|
||||
|
||||
add_subdirectory("lib/argtable3")
|
||||
|
||||
file(GLOB CONSOLE_SOURCES "src/*.c")
|
||||
|
||||
# Console config options
|
||||
|
||||
# Line buffer length
|
||||
set(CONSOLE_LINE_BUF_LEN "255" CACHE STRING "Line buffer length (max command size)")
|
||||
|
||||
# Max number of CLI args
|
||||
set(CONSOLE_MAX_NUM_ARGS "64" CACHE STRING "Max number of arguments for a command")
|
||||
|
||||
# Prompt buffer length
|
||||
set(CONSOLE_PROMPT_MAX_LEN "32" CACHE STRING "Max prompt string length")
|
||||
|
||||
# CLI history length
|
||||
set(CONSOLE_HISTORY_LEN "32" CACHE STRING "Console history length")
|
||||
|
||||
option(CONSOLE_FILE_SUPPORT "Support filesystem operations (history save/load)" ON)
|
||||
|
||||
option(CONSOLE_USE_FILE_IO_STREAMS "Use FILE* based console I/O" ON)
|
||||
option(CONSOLE_USE_TERMIOS "Use unistd/termios to set nonblocking mode & implement bytes available check" ON)
|
||||
option(CONSOLE_USE_MEMSTREAM "Allow using open_memstream() to generate command hints and report argtable errors" ON)
|
||||
|
||||
if(CONSOLE_USE_TERMIOS AND NOT CONSOLE_USE_FILE_IO_STREAMS)
|
||||
message( FATAL_ERROR "Can't use TERMIOS without FILE_IO_STREAMS" )
|
||||
endif()
|
||||
|
||||
option(CONSOLE_USE_FREERTOS "Use FreeRTOS" ON)
|
||||
option(CONSOLE_USE_PTHREADS "Use pthreads" OFF)
|
||||
|
||||
# Default timeout for CSP commands
|
||||
set(CONSOLE_CSP_DEF_TIMEOUT_MS "3000" CACHE STRING "Default timeout for CSP commands (milliseconds)")
|
||||
|
||||
option(CONSOLE_TESTING_ALLOC_FUNCS "Test the internal console_malloc etc. functions on startup (with asserts)" OFF)
|
||||
|
||||
configure_file(
|
||||
"include/console/config.h.in"
|
||||
"include/console/config.h"
|
||||
)
|
||||
|
||||
add_library(console ${CONSOLE_SOURCES})
|
||||
|
||||
# Enable extra warnings
|
||||
#set_target_properties(console PROPERTIES COMPILE_FLAGS "-Wall -Wextra" )
|
||||
|
||||
target_include_directories(console
|
||||
PUBLIC "include" "${CMAKE_CURRENT_BINARY_DIR}/include" # this is where the generated config header is placed
|
||||
PRIVATE "src"
|
||||
)
|
||||
|
||||
# Link libraries
|
||||
set(LIBRARIES argtable3)
|
||||
|
||||
if(ESP_PLATFORM)
|
||||
set(CONSOLE_USE_FREERTOS ON)
|
||||
set(CONSOLE_USE_PTHREADS OFF)
|
||||
# special hack for ESP-IDF is needed to allow implementing extern prototypes in main
|
||||
set(LIBRARIES ${LIBRARIES} idf::main)
|
||||
endif()
|
||||
|
||||
if(NOT CONSOLE_USE_FREERTOS AND NOT CONSOLE_USE_PTHREADS)
|
||||
message( FATAL_ERROR "Required either FreeRTOS or PTHREADS!" )
|
||||
endif()
|
||||
|
||||
if(CONSOLE_USE_PTHREADS)
|
||||
set(LIBRARIES ${LIBRARIES} pthread)
|
||||
endif()
|
||||
|
||||
target_link_libraries(console ${LIBRARIES})
|
||||
@@ -0,0 +1 @@
|
||||
Proprietary code (c) VZLU 2019-2020
|
||||
@@ -0,0 +1,24 @@
|
||||
//
|
||||
// Header with useful defines and common includes
|
||||
// to use when defining console commands.
|
||||
//
|
||||
// This file aims to concentrate the most common includes
|
||||
// and utility macros to make command definitions easier to write.
|
||||
//
|
||||
// Created by MightyPork on 2020/03/11.
|
||||
//
|
||||
|
||||
#ifndef LIBCONSOLE_CMDDEF_H
|
||||
#define LIBCONSOLE_CMDDEF_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <argtable3.h>
|
||||
#include "console/console.h"
|
||||
|
||||
#if CONSOLE_HAVE_CSP
|
||||
#include <csp/csp.h>
|
||||
#endif
|
||||
|
||||
#include "console/utils.h"
|
||||
|
||||
#endif //LIBCONSOLE_CMDDEF_H
|
||||
@@ -0,0 +1,22 @@
|
||||
/**
|
||||
* Console configuration file, filled by CMake
|
||||
*
|
||||
* Created on 2020/03/16.
|
||||
*/
|
||||
|
||||
#ifndef LIBCONSOLE_CONFIG_H
|
||||
#define LIBCONSOLE_CONFIG_H
|
||||
|
||||
#cmakedefine CONSOLE_LINE_BUF_LEN @CONSOLE_LINE_BUF_LEN@
|
||||
#cmakedefine CONSOLE_MAX_NUM_ARGS @CONSOLE_MAX_NUM_ARGS@
|
||||
#cmakedefine CONSOLE_PROMPT_MAX_LEN @CONSOLE_PROMPT_MAX_LEN@
|
||||
#cmakedefine CONSOLE_HISTORY_LEN @CONSOLE_HISTORY_LEN@
|
||||
#cmakedefine01 CONSOLE_FILE_SUPPORT
|
||||
#cmakedefine01 CONSOLE_USE_FILE_IO_STREAMS
|
||||
#cmakedefine01 CONSOLE_USE_TERMIOS
|
||||
#cmakedefine01 CONSOLE_USE_MEMSTREAM
|
||||
#cmakedefine01 CONSOLE_USE_FREERTOS
|
||||
#cmakedefine01 CONSOLE_USE_PTHREADS
|
||||
#cmakedefine01 CONSOLE_TESTING_ALLOC_FUNCS
|
||||
|
||||
#endif //LIBCONSOLE_CONFIG_H
|
||||
@@ -0,0 +1,362 @@
|
||||
/**
|
||||
* Console - VCOM command engine
|
||||
*
|
||||
* Created on 2020/02/28 by Ondrej Hruska
|
||||
*
|
||||
* Parts are based on the console component from esp-idf
|
||||
* licensed under the Apache 2 license.
|
||||
*/
|
||||
|
||||
#ifndef LIBCONSOLE_H
|
||||
#define LIBCONSOLE_H
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <console/config.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef enum {
|
||||
/* Colors */
|
||||
COLOR_RESET = 0xF0,
|
||||
COLOR_BLACK = 0x01,
|
||||
COLOR_RED = 0x02,
|
||||
COLOR_GREEN = 0x03,
|
||||
COLOR_YELLOW = 0x04,
|
||||
COLOR_BLUE = 0x05,
|
||||
COLOR_MAGENTA = 0x06,
|
||||
COLOR_CYAN = 0x07,
|
||||
COLOR_WHITE = 0x08,
|
||||
/* Modifiers */
|
||||
COLOR_NORMAL = 0x0F,
|
||||
COLOR_BOLD = 0x10,
|
||||
COLOR_UNDERLINE = 0x20,
|
||||
COLOR_BLINK = 0x30,
|
||||
COLOR_HIDE = 0x40,
|
||||
} console_color_t;
|
||||
|
||||
#if CONSOLE_USE_FREERTOS
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/task.h"
|
||||
#include "freertos/semphr.h"
|
||||
|
||||
typedef SemaphoreHandle_t console_mutex_t;
|
||||
#endif
|
||||
|
||||
#if CONSOLE_USE_PTHREADS
|
||||
#include <pthread.h>
|
||||
typedef pthread_mutex_t console_mutex_t;
|
||||
#endif
|
||||
|
||||
#if CONSOLE_USE_TERMIOS
|
||||
#include <termios.h>
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Console config struct
|
||||
*/
|
||||
struct console_config {
|
||||
/**
|
||||
* Timeout waiting for execution lock when handling a command.
|
||||
* This should be longer than the slowest command in the system.
|
||||
*/
|
||||
uint32_t execution_lock_timeout_ms;
|
||||
};
|
||||
|
||||
/**
|
||||
* Macro to init the console config struct
|
||||
*/
|
||||
#define CONSOLE_CONFIG_DEFAULTS() { \
|
||||
.execution_lock_timeout_ms = 10000, \
|
||||
}
|
||||
|
||||
typedef struct console_config console_config_t;
|
||||
|
||||
struct console_ctx; // early declaration
|
||||
|
||||
/**
|
||||
* Console context
|
||||
*/
|
||||
typedef struct console_ctx console_ctx_t;
|
||||
|
||||
/**
|
||||
* Console errors - return codes
|
||||
*/
|
||||
enum console_err {
|
||||
CONSOLE_OK = 0,
|
||||
/** unspecified error */
|
||||
CONSOLE_ERROR = 1,
|
||||
/** Allocation failed */
|
||||
CONSOLE_ERR_NO_MEM,
|
||||
/** Function call not allowed (e.g. console not inited) */
|
||||
CONSOLE_ERR_BAD_CALL,
|
||||
/** Argument validation failed */
|
||||
CONSOLE_ERR_INVALID_ARG,
|
||||
/** Command not recognized */
|
||||
CONSOLE_ERR_UNKNOWN_CMD,
|
||||
/** Timeout */
|
||||
CONSOLE_ERR_TIMEOUT,
|
||||
/** IO error (file open fail, etc.) */
|
||||
CONSOLE_ERR_IO,
|
||||
/** Operation denied (not allowed / insufficient rights) */
|
||||
CONSOLE_ERR_NOT_POSSIBLE,
|
||||
/** End marker */
|
||||
_CONSOLE_ERR_MAX,
|
||||
};
|
||||
|
||||
/**
|
||||
* Print string describing console error.
|
||||
* In case of unknown error, the number is shown.
|
||||
*
|
||||
* The argument should be `enum console_err`, but any number is valid.
|
||||
*/
|
||||
void console_err_print_ctx(struct console_ctx *ctx, int e);
|
||||
|
||||
// TODO error-to-string function
|
||||
|
||||
typedef enum console_err console_err_t;
|
||||
|
||||
// early decl's
|
||||
struct cmd_signature;
|
||||
typedef struct cmd_signature cmd_signature_t;
|
||||
|
||||
/**
|
||||
* Command signature, passed as the last argument to the command handler
|
||||
* to perform registration.
|
||||
*
|
||||
* \note Fill only fields that differ from default (zeros/NULLs)
|
||||
*/
|
||||
struct cmd_signature {
|
||||
const char* command; //!< Command name, used in invocations (filled internally, do not set)
|
||||
const char* help; //!< Command help text, shown when called with -h
|
||||
const char* hint; //!< Hint text, generated from argtable if hint==NULL & argtable!=NULL
|
||||
bool no_history; //!< Command skips history
|
||||
bool custom_args; //!< Disable argtable parsing in the handler function, will be parsed manually from argv/argc
|
||||
|
||||
/**
|
||||
* Argtable, struct or array that must end with arg_end().
|
||||
* Used by the register function and for disambiguation.
|
||||
*/
|
||||
void* argtable;
|
||||
};
|
||||
|
||||
/**
|
||||
* Active console context pointer, valid only when handling a console command (otherwise NULL).
|
||||
*
|
||||
* Used by the console printf & other IO methods.
|
||||
*/
|
||||
extern struct console_ctx * console_active_ctx;
|
||||
|
||||
/**
|
||||
* Function handling a callback from the console loop.
|
||||
*/
|
||||
typedef void(*console_callback_t)(console_ctx_t *ctx);
|
||||
|
||||
/**
|
||||
* Console context struct
|
||||
*/
|
||||
struct console_ctx {
|
||||
#if CONSOLE_USE_FILE_IO_STREAMS
|
||||
// Streams
|
||||
FILE* in; //!< stdin fd, can be -1 if not available (running commands in non-interactive mode)
|
||||
FILE* out; //!< stdout fd
|
||||
|
||||
#if CONSOLE_USE_TERMIOS
|
||||
// original termios is stored here before entering raw mode
|
||||
struct termios orig_termios;
|
||||
#endif
|
||||
|
||||
#else
|
||||
void *ioctx;
|
||||
#endif //CONSOLE_USE_FILE_IO_STREAMS
|
||||
|
||||
#if CONSOLE_FILE_SUPPORT
|
||||
char *history_file;
|
||||
#endif //CONSOLE_FILE_SUPPORT
|
||||
|
||||
bool __internal_heap_allocated;
|
||||
bool exit_allowed;
|
||||
|
||||
char prompt[CONSOLE_PROMPT_MAX_LEN]; //!< Prompt, can be modified by a command or `before_readline_fn`
|
||||
char line_buffer[CONSOLE_LINE_BUF_LEN];
|
||||
|
||||
/**
|
||||
* Callback fired in the command evaluation loop, each time before the prompt is shown and new line read.
|
||||
* This command can print to the output streams, change prompt, shutdown console, etc.
|
||||
*/
|
||||
console_callback_t loop_handler;
|
||||
|
||||
/**
|
||||
* Callback fired before the console task shuts down
|
||||
*/
|
||||
console_callback_t shutdown_handler;
|
||||
|
||||
/**
|
||||
* Shutdown requested. Console will exit as soon as possible.
|
||||
*/
|
||||
bool exit_requested;
|
||||
|
||||
/**
|
||||
* Interactive mode. Enables additional outputs for user convenience.
|
||||
*/
|
||||
bool interactive;
|
||||
|
||||
/**
|
||||
* Enable ANSI colors
|
||||
*/
|
||||
bool use_colors;
|
||||
|
||||
/* These fields are valid only during command execution */
|
||||
const char **argv; //!< The current argv
|
||||
size_t argc; //!< The current argc
|
||||
const cmd_signature_t *cmd; //!< Pointer to the currently executed command signature
|
||||
|
||||
/** Used for argument validation */
|
||||
uint32_t __internal_magic;
|
||||
};
|
||||
|
||||
#define CONSOLE_CTX_MAGIC 0x6f587468
|
||||
|
||||
/**
|
||||
* Command handler type.
|
||||
*
|
||||
* @param ctx - console context, including input/output files
|
||||
* @param reg - signature struct; if not NULL, init the static argtable, fill this struct, and return OK (0).
|
||||
* @return status code, 0 = OK (use cons_err_t constants if possible)
|
||||
*/
|
||||
typedef int (*console_command_t)(console_ctx_t *ctx, struct cmd_signature *reg);
|
||||
|
||||
/**
|
||||
* Intialize the console
|
||||
*
|
||||
* @param config - config pointer, NULL to use defaults
|
||||
* @return status code
|
||||
*/
|
||||
console_err_t console_init(const console_config_t *config);
|
||||
|
||||
/**
|
||||
* @brief Register console command
|
||||
*
|
||||
* If the command function is already registered, this creates an alias.
|
||||
* A multi-word command automatically create a command group. The group can
|
||||
* be described using a description string by calling `console_group_add()`
|
||||
* - at convenience before or after the commands are registered.
|
||||
*
|
||||
* @param name - command name (may contain spaces for "multi-part commands")
|
||||
* @param handler pointer to the command handler.
|
||||
* @return status code
|
||||
*/
|
||||
console_err_t console_cmd_register(console_command_t handler, const char *name);
|
||||
|
||||
/**
|
||||
* @brief Register a command group.
|
||||
*
|
||||
* Command groups are created automatically when used.
|
||||
* This method can create a group with description, or attach a custom description
|
||||
* to an existing group.
|
||||
*
|
||||
* @param name - group name (first word of multi-part commands)
|
||||
* @param descr - description to attach, can be NULL
|
||||
* @return staus code
|
||||
*/
|
||||
console_err_t console_group_add(const char *name, const char *descr);
|
||||
|
||||
/**
|
||||
* Add alias to an existing command by name.
|
||||
*
|
||||
* @param original - original command name
|
||||
* @param alias - command's alias
|
||||
* @return status code
|
||||
*/
|
||||
console_err_t console_cmd_add_alias(const char *original, const char *alias);
|
||||
|
||||
/**
|
||||
* Add alias by handler function
|
||||
*
|
||||
* @param handler - command handler
|
||||
* @param alias - new name
|
||||
* @return status code
|
||||
*/
|
||||
console_err_t console_cmd_add_alias_fn(console_command_t handler, const char *alias);
|
||||
|
||||
/**
|
||||
* Internal error print function. Has WEAK linkage, can be overridden.
|
||||
*
|
||||
* This function is used to report detected bugs and should not be called
|
||||
* in well-written "production code".
|
||||
*
|
||||
* @param msg - error message
|
||||
*/
|
||||
void console_internal_error_print(const char *msg);
|
||||
|
||||
/**
|
||||
* This function is guarded by a mutex and will wait for the execution lock as
|
||||
* configured in console_config.
|
||||
*
|
||||
* @brief Run command line
|
||||
* @param[in] outf
|
||||
* @param[in] inf
|
||||
* @param cmdline command line (command name followed by a number of arguments)
|
||||
* @param[out] pRetval return code from the command (set if command was run)
|
||||
* @param[out] pCommandSig - is set to a pointer to the matched command signature, or NULL on error
|
||||
* @return status code
|
||||
*/
|
||||
console_err_t console_handle_cmd(
|
||||
console_ctx_t *ctx,
|
||||
const char *cmdline,
|
||||
int *pRetval,
|
||||
const struct cmd_signature **pCommandSig
|
||||
);
|
||||
|
||||
/**
|
||||
* Count all registered commands
|
||||
*
|
||||
* @return
|
||||
*/
|
||||
size_t console_count_commands(void);
|
||||
|
||||
/**
|
||||
* Create a console IO context and init it to defaults.
|
||||
*
|
||||
* takes stdin and stdout file descriptors, or IO context (based on config flags)
|
||||
*
|
||||
* In the FD variant, pass NULL as STDIN if not available.
|
||||
*
|
||||
* @param ctx - context, if using static alloc, NULL to allocate internally.
|
||||
* @return the context, NULL if alloc or init fails
|
||||
*/
|
||||
console_ctx_t *console_ctx_init(
|
||||
console_ctx_t *ctx,
|
||||
#if CONSOLE_USE_FILE_IO_STREAMS
|
||||
FILE* inf, FILE* outf
|
||||
#else
|
||||
void * ioctx
|
||||
#endif
|
||||
);
|
||||
|
||||
/**
|
||||
* Destroy a console IO context.
|
||||
*
|
||||
* Make sure to release any user fields (ioctx, for example) beforehand.
|
||||
*
|
||||
* @attention ONLY CALL THIS IF THE CONTEXT WAS DYNAMICALLY ALLOCATED!
|
||||
*
|
||||
* @param[in,out] ctx - pointer to context, will be set to NULL.
|
||||
*/
|
||||
void console_ctx_destroy(console_ctx_t *ctx);
|
||||
|
||||
/**
|
||||
* Console task
|
||||
*
|
||||
* @param[in] param - must be a valid console context (see `console_ctx_init()`)
|
||||
*/
|
||||
void console_task(void *param);
|
||||
|
||||
/**
|
||||
* Variant of 'console_task' for pthreads (returns NULL)
|
||||
*/
|
||||
void* console_task_posix(void *param);
|
||||
|
||||
#include "console_io.h"
|
||||
|
||||
#endif //LIBCONSOLE_H
|
||||
@@ -0,0 +1,196 @@
|
||||
/**
|
||||
* Console IO functions.
|
||||
*
|
||||
* This header is included internally by console.h
|
||||
*
|
||||
* Created on 2020/04/09.
|
||||
*/
|
||||
|
||||
#ifndef LIBCONSOLE_IO_H
|
||||
#define LIBCONSOLE_IO_H
|
||||
|
||||
#ifndef LIBCONSOLE_H
|
||||
#error Include console.h!
|
||||
#endif
|
||||
|
||||
#include <stdarg.h>
|
||||
|
||||
// ------ If the FILE based IO streams option is OFF, these are extern -------
|
||||
|
||||
/**
|
||||
* Write to console context.
|
||||
*
|
||||
* In command context, the more convenient "console_write", "console_print", "console_println"
|
||||
* and "console_printf" functions can be used instead.
|
||||
*
|
||||
* This function is a Linenoise write callback.
|
||||
*
|
||||
* Return number of characters written, -1 on error.
|
||||
*/
|
||||
extern int console_write_ctx(console_ctx_t *ctx, const char *text, size_t len);
|
||||
|
||||
/**
|
||||
* Read from console context's input stream.
|
||||
*
|
||||
* In command context, the more convenient "console_read" function and the
|
||||
* "console_can_read" and "console_have_stdin" helper functions can be used instead.
|
||||
*
|
||||
* This is also a Linenoise read callback.
|
||||
*
|
||||
* Return number of characters read, -1 on error
|
||||
*/
|
||||
extern int console_read_ctx(console_ctx_t *ctx, char *dest, size_t count);
|
||||
|
||||
/**
|
||||
* Check if console input stream has bytes ready.
|
||||
*
|
||||
* @return number of queued bytes, 0 if none, -1 on error.
|
||||
*/
|
||||
extern int console_can_read_ctx(console_ctx_t *ctx);
|
||||
|
||||
/**
|
||||
* Test if console context is not NULL and has stdin stream available
|
||||
*
|
||||
* @return have stdin
|
||||
*/
|
||||
extern bool console_have_stdin_ctx(console_ctx_t *ctx);
|
||||
|
||||
|
||||
// ----- end extern interface -----
|
||||
|
||||
/**
|
||||
* Print zero-terminated string to to console output
|
||||
*
|
||||
* @param text - characters to write
|
||||
* @return number of characters written, or -1 on error
|
||||
*/
|
||||
int console_print_ctx(console_ctx_t *ctx, const char *text);
|
||||
|
||||
/**
|
||||
* Print zero-terminated string to console output, followed by a newline
|
||||
*
|
||||
* @param text - characters to write
|
||||
* @return number of characters written, or -1 on error
|
||||
*/
|
||||
ssize_t console_println_ctx(console_ctx_t *ctx, const char *text);
|
||||
|
||||
/**
|
||||
* Console printf
|
||||
*
|
||||
* @param ctx - console context
|
||||
* @param color - color to use, COLOR_RESET = default
|
||||
* @param format
|
||||
* @param ...
|
||||
* @return bytes written, or -1 on error
|
||||
*/
|
||||
ssize_t console_printf_ctx(console_ctx_t *ctx, console_color_t color, const char *format, ...) __attribute__((format(printf,3,4)));
|
||||
|
||||
/**
|
||||
* Console vprintf
|
||||
*
|
||||
* @param ctx - console context
|
||||
* @param color - color to use, COLOR_RESET = default
|
||||
* @param format - format string
|
||||
* @param args - varargs passed as a va_list
|
||||
* @return bytes written, or -1 on error
|
||||
*/
|
||||
ssize_t console_vprintf_ctx(console_ctx_t *ctx, console_color_t color, const char *format, va_list args);
|
||||
|
||||
/**
|
||||
* Write to console output
|
||||
*
|
||||
* @attention Can only be used within a console command context
|
||||
*
|
||||
* @param text - characters to write
|
||||
* @param len - text length
|
||||
* @return number of characters written, or -1 on error
|
||||
*/
|
||||
ssize_t console_write(const char *text, size_t len);
|
||||
|
||||
|
||||
// -------------------- Convenience functions -------------------------
|
||||
|
||||
/**
|
||||
* Test if we are in the console command context.
|
||||
*
|
||||
* @return in command context
|
||||
*/
|
||||
static inline bool console_context_available(void) {
|
||||
return console_active_ctx != NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Test if we are in the console command context AND the console context has an input stream
|
||||
* (input stream may be used in interactive commands)
|
||||
*
|
||||
* @return have stdin
|
||||
*/
|
||||
bool console_have_stdin(void);
|
||||
|
||||
/**
|
||||
* Console printf. Defined as a macro to pass variadic arguments
|
||||
*
|
||||
* @attention Can only be used within a console command context
|
||||
*
|
||||
* @param format
|
||||
* @param ...
|
||||
* @return bytes written, or -1 on error
|
||||
*/
|
||||
#define console_printf(format, ...) console_printf_ctx(console_active_ctx, COLOR_RESET, format, ##__VA_ARGS__)
|
||||
|
||||
/**
|
||||
* Console printf with colors. Defined as a macro to pass variadic arguments
|
||||
*
|
||||
* @attention Can only be used within a console command context
|
||||
*
|
||||
* @param color - from console_colors_t enum
|
||||
* @param format
|
||||
* @param ...
|
||||
* @return bytes written, or -1 on error
|
||||
*/
|
||||
#define console_color_printf(color, format, ...) console_printf_ctx(console_active_ctx, color, format, ##__VA_ARGS__)
|
||||
|
||||
/**
|
||||
* Read from console input
|
||||
*
|
||||
* @attention Can only be used within a console command context
|
||||
*
|
||||
* @param dest - destination buffer
|
||||
* @param count - how many characters to read
|
||||
* @return number of characters read, or -1 on error
|
||||
*/
|
||||
ssize_t console_read(char *dest, size_t count);
|
||||
|
||||
/**
|
||||
* Check if console input stream has bytes ready.
|
||||
*
|
||||
* @attention Can only be used within a console command context
|
||||
*
|
||||
* @return number of queued bytes, 0 if none, -1 on error.
|
||||
*/
|
||||
int console_can_read(void);
|
||||
|
||||
/**
|
||||
* Print zero-terminated string to to console output
|
||||
*
|
||||
* @attention Can only be used within a console command context
|
||||
*
|
||||
* @param text - characters to write
|
||||
* @return number of characters written, or -1 on error
|
||||
*/
|
||||
static inline int console_print(const char *text) {
|
||||
return console_print_ctx(console_active_ctx, text);
|
||||
}
|
||||
|
||||
/**
|
||||
* Print zero-terminated string to console output, followed by a newline
|
||||
*
|
||||
* @attention Can only be used within a console command context
|
||||
*
|
||||
* @param text - characters to write
|
||||
* @return number of characters written, or -1 on error
|
||||
*/
|
||||
ssize_t console_println(const char *text);
|
||||
|
||||
|
||||
#endif //LIBCONSOLE_IO_H
|
||||
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* Prefix Match
|
||||
*
|
||||
* Match input value to a list of options, allowing non-ambiguous abbreviation and partial matching.
|
||||
* This library was designed for command recognition in interactive consoles and command interfaces.
|
||||
*
|
||||
* Created on 2020/06/09 by Ondřej Hruška
|
||||
*/
|
||||
|
||||
#ifndef _PREFIX_MATCH_H
|
||||
#define _PREFIX_MATCH_H
|
||||
|
||||
#include <stdbool.h>
|
||||
#include <stddef.h>
|
||||
|
||||
/** Use case-sensitive matching */
|
||||
#define PREFIXMATCH_CASE_SENSITIVE 1
|
||||
/** Forbid abbreviations */
|
||||
#define PREFIXMATCH_NOABBREV 2
|
||||
/** Allow matching fewer words, if unambiguous */
|
||||
#define PREFIXMATCH_MULTI_PARTIAL 4
|
||||
|
||||
enum pm_test_result {
|
||||
PM_TEST_NO_MATCH = 0,
|
||||
PM_TEST_MATCH = 1,
|
||||
PM_TEST_MATCH_MULTI_PARTIAL = 2,
|
||||
};
|
||||
|
||||
/**
|
||||
* Recognize (optionally abbreviated) input
|
||||
*
|
||||
* @param[in] value - tested value
|
||||
* @param[in] options - options to match against
|
||||
* @param[in] flags - matching options (bitmask) - accepts PREFIXMATCH_CASE_SENSITIVE and PREFIXMATCH_NOABBREV
|
||||
* @return index of the matched option, -1 on mismatch or ambiguous match
|
||||
*/
|
||||
int prefix_match(const char *value, const char **options, int flags);
|
||||
|
||||
/**
|
||||
* Recognize input consisting of one or more (optionally abbreviated) words
|
||||
*
|
||||
* @param[in] value - tested value
|
||||
* @param[in] options - options to match against, multi-word options separated by the listed delimiters
|
||||
* @param[in] delims - string with a list of possible delimiters (like for strtok)
|
||||
* @param[in] flags - matching options (bitmask) - accepts all options
|
||||
* @return index of the matched option, -1 on mismatch or ambiguous match
|
||||
*/
|
||||
int prefix_multipart_match(const char *restrict value, const char **options, const char* restrict delims, int flags);
|
||||
|
||||
// useful internal functions exported for possible re-use
|
||||
|
||||
/**
|
||||
* Test if two word sentences match, with individual words optionally allowed to be abbreviated.
|
||||
*
|
||||
* @internal
|
||||
* @param[in] tested - tested (optionally abbreviated) sentence
|
||||
* @param[in] full - full sentence
|
||||
* @param[in] delims - list of possible delimiters, same may be used for both sentences
|
||||
* @param[in] flags - matching options (bitmask) - accepts all options
|
||||
* @return 1-match; 0-no match; 2-partial (some words) match, if the PREFIXMATCH_MULTI_PARTIAL flag is set
|
||||
*/
|
||||
enum pm_test_result prefix_multipart_test(const char *restrict tested, const char* restrict full, const char *restrict delims, int flags);
|
||||
|
||||
/**
|
||||
* Count words in a "sentence", delimited by any of the given set of delimiters.
|
||||
*
|
||||
* @internal
|
||||
* @param[in] sentence - one or multi-word string
|
||||
* @param[in] delims - delimiters accepted
|
||||
* @return number of words
|
||||
*/
|
||||
size_t pm_count_words(const char * restrict sentence, const char * restrict delims);
|
||||
|
||||
/**
|
||||
* Measure word length
|
||||
*
|
||||
* @internal
|
||||
* @param[in] word - start of a word that ends with either one of the delimiters, or a null byte.
|
||||
* @param[in] delims - delimiters accepted
|
||||
* @return word length
|
||||
*/
|
||||
size_t pm_word_len(const char * restrict word, const char * restrict delims);
|
||||
|
||||
/**
|
||||
* Skip N words in a sentence.
|
||||
*
|
||||
* @param[in] sentence - one or multi-word string
|
||||
* @param[in] delims - delimiters accepted
|
||||
* @param[in] skip - how many words to skip
|
||||
* @return pointer to the first byte after the last skipped word
|
||||
*/
|
||||
const char *pm_skip_words(const char * restrict sentence, const char * restrict delims, size_t skip);
|
||||
|
||||
#endif //_PREFIX_MATCH_H
|
||||
@@ -0,0 +1,213 @@
|
||||
/**
|
||||
* Utilities for console commands
|
||||
*
|
||||
* Created on 2020/03/11.
|
||||
*/
|
||||
|
||||
#ifndef LIBCONSOLE_UTILS_H
|
||||
#define LIBCONSOLE_UTILS_H
|
||||
|
||||
#include <stdio.h>
|
||||
#include <console/console.h>
|
||||
|
||||
#ifndef STR
|
||||
#define STR_HELPER(x) #x
|
||||
#define STR(x) STR_HELPER(x)
|
||||
#endif
|
||||
|
||||
#ifndef MIN
|
||||
#define MIN(a,b) (((a)<(b))?(a):(b))
|
||||
#endif
|
||||
|
||||
#ifndef MAX
|
||||
#define MAX(a,b) (((a)>(b))?(a):(b))
|
||||
#endif
|
||||
|
||||
#ifndef OBC_FIRMWARE
|
||||
#define EXPENDABLE_STRING(x) x
|
||||
#define EXPENDABLE_CODE(x) x
|
||||
#else
|
||||
#define EXPENDABLE_STRING(x) ""
|
||||
#define EXPENDABLE_CODE(x) do {} while(0);
|
||||
#endif
|
||||
|
||||
/**
|
||||
* Read an argument, or return default if it is empty.
|
||||
*
|
||||
* This works for commands arg_int0
|
||||
*
|
||||
* Usage:
|
||||
*
|
||||
* \code
|
||||
* static struct {
|
||||
* struct arg_int *foo;
|
||||
* } args;
|
||||
*
|
||||
* args.foo = arg_int0(...);
|
||||
*
|
||||
* int foo = GET_ARG_INT0(args.foo, 1234);
|
||||
* \endcode
|
||||
*/
|
||||
#define GET_ARG_INT0(_arg, _def) ((_arg)->count ? (_arg)->ival[0] : (_def))
|
||||
|
||||
/**
|
||||
* Get CSP node ID from an argument table, using own address as default.
|
||||
*
|
||||
* Usage:
|
||||
*
|
||||
* \code
|
||||
* static struct {
|
||||
* struct arg_int *node;
|
||||
* } args;
|
||||
*
|
||||
* args.node = arg_int0(...);
|
||||
*
|
||||
* int node = GET_ARG_CSPADDR0(args.node);
|
||||
* \endcode
|
||||
*/
|
||||
#define GET_ARG_CSPADDR0(_arg) GET_ARG_INT0((_arg), csp_get_address())
|
||||
|
||||
/**
|
||||
* Shortcut to get a timeout argument's value, using CSP_DEF_TIMEOUT_MS as default.
|
||||
*/
|
||||
#define GET_ARG_TIMEOUT0(_arg) GET_ARG_INT0((_arg), CONSOLE_CSP_DEF_TIMEOUT_MS)
|
||||
|
||||
/**
|
||||
* Define an optional CSP node argument
|
||||
*/
|
||||
#define arg_cspaddr0() arg_int0(NULL, NULL, "<node>", EXPENDABLE_STRING("node ID"))
|
||||
|
||||
/**
|
||||
* Define a mandatory CSP node argument
|
||||
*/
|
||||
#define arg_cspaddr1() arg_int1(NULL, NULL, "<node>", EXPENDABLE_STRING("node ID"))
|
||||
|
||||
/**
|
||||
* Define an optional timeout argument, with `CSP_DEF_TIMEOUT_MS`
|
||||
* shown as default. Use `GET_ARG_TIMEOUT0()` to retrieve its value.
|
||||
*/
|
||||
#define arg_timeout0() arg_int0("t", "timeout", "<ms>", EXPENDABLE_STRING("timeout in ms (default "STR(CONSOLE_CSP_DEF_TIMEOUT_MS)")"))
|
||||
|
||||
/**
|
||||
* Define a timeout argument with a custom value shown as default.
|
||||
* Use `GET_ARG_INT0()` with the matching default to retrieve its value.
|
||||
*/
|
||||
#define arg_timeout0_def(_def) arg_int0("t", "timeout", "<ms>", EXPENDABLE_STRING("timeout in ms (default "STR(_def)")"))
|
||||
|
||||
|
||||
#define EMPTY_CMD_SETUP(_helptext) \
|
||||
(void)ctx; \
|
||||
static struct { \
|
||||
struct arg_end *end; \
|
||||
} args; \
|
||||
\
|
||||
if (reg) { \
|
||||
args.end = arg_end(1); \
|
||||
\
|
||||
reg->argtable = &args; \
|
||||
reg->help = EXPENDABLE_STRING(_helptext); \
|
||||
return 0; \
|
||||
}
|
||||
|
||||
/**
|
||||
* Hexdump a buffer
|
||||
*
|
||||
* @param outf - output file
|
||||
* @param data - data to dump
|
||||
* @param len - data size
|
||||
*/
|
||||
void console_hexdump(const void *data, size_t len);
|
||||
|
||||
/**
|
||||
* Decode hexa string to binary
|
||||
*
|
||||
* @param hex - hexa string, upper or lower case, must have even length
|
||||
* @param dest - destination buffer
|
||||
* @param capacity - buffer size
|
||||
* @return destination length, or: -1 (bad args), -2 (bad format), -3 (too long)
|
||||
*/
|
||||
int console_base16_decode(const char *hex, void *dest, size_t capacity);
|
||||
|
||||
#if CONSOLE_USE_MEMSTREAM
|
||||
|
||||
/**
|
||||
* Data struct for the filecap utilities
|
||||
*/
|
||||
struct console_filecap {
|
||||
char *buf;
|
||||
size_t buf_size;
|
||||
FILE *file;
|
||||
};
|
||||
|
||||
typedef struct console_filecap console_filecap_t;
|
||||
|
||||
/**
|
||||
* Open a temporary in-memory file that can be used to capture the output
|
||||
* of functions taking a FILE * argument.
|
||||
*
|
||||
* @param cap - pointer to a filecap struct (can be on stack)
|
||||
* @return success
|
||||
*/
|
||||
console_err_t console_filecap_init(console_filecap_t *cap);
|
||||
|
||||
/**
|
||||
* Clean up the capture struct.
|
||||
*
|
||||
* If the buffer is to be used elsewhere, place NULL in the struct to avoid freeing it.
|
||||
*
|
||||
* If the struct itself was allocated on heap, it is the caller's responsibility to free it manually.
|
||||
*
|
||||
* @param cap - pointer to a filecap struct
|
||||
*/
|
||||
void console_filecap_end(console_filecap_t *cap);
|
||||
|
||||
/**
|
||||
* Print the captured output to console output stream and clean up the struct (calls `console_filecap_end()`)
|
||||
*
|
||||
* @param cap - pointer to a filecap struct
|
||||
*/
|
||||
void console_filecap_print_end(console_filecap_t *cap);
|
||||
|
||||
#endif // CONSOLE_USE_MEMSTREAM
|
||||
|
||||
/**
|
||||
* Cross-platform malloc that can be used from console commands.
|
||||
* If CSP is available and the platform is not POSIX, the implementation from there is used (i.e. FreeRTOS alloc)
|
||||
*/
|
||||
void * __attribute__((malloc)) console_malloc(size_t size);
|
||||
|
||||
/**
|
||||
* Cross-platform realloc that can be used from console commands.
|
||||
*
|
||||
* It is not possible to determine the size of an allocated memory region in a portable way,
|
||||
* that's why this function takes the old size as an argument. On POSIX, the argument is simply ignored.
|
||||
*
|
||||
* If CSP is available and the platform is not POSIX, the implementation from there is used (i.e. FreeRTOS alloc).
|
||||
* NOTE: CSP does not provide realloc, therefore this function allocates a new buffer, copies data, and frees the old buffer.
|
||||
*
|
||||
* Returns the original buffer if the new size is <= old size.
|
||||
*/
|
||||
void * console_realloc(void *ptr, size_t oldsize, size_t newsize);
|
||||
|
||||
/**
|
||||
* Cross-platform calloc that can be used from console commands.
|
||||
* If CSP is available and the platform is not POSIX, the implementation from there is used (i.e. FreeRTOS alloc)
|
||||
*/
|
||||
void * __attribute__((malloc,alloc_size(1,2))) console_calloc(size_t nmemb, size_t size);
|
||||
|
||||
/**
|
||||
* `free()` for memory allocated by `console_malloc()` or `console_calloc()`
|
||||
*/
|
||||
void console_free(void *ptr);
|
||||
|
||||
/**
|
||||
* `strdup()` using `console_malloc()`. Free with `console_free()`
|
||||
*/
|
||||
char * console_strdup(const char *ptr);
|
||||
|
||||
/**
|
||||
* `strndup()` using `console_malloc()`. Free with `console_free()`
|
||||
*/
|
||||
char * console_strndup(const char *ptr, size_t maxlen);
|
||||
|
||||
#endif //LIBCONSOLE_UTILS_H
|
||||
@@ -0,0 +1,9 @@
|
||||
cmake_minimum_required(VERSION 3.10)
|
||||
|
||||
project(console-argtable3)
|
||||
|
||||
add_library(argtable3 argtable3.c)
|
||||
target_include_directories(argtable3
|
||||
PUBLIC "."
|
||||
)
|
||||
target_link_libraries(argtable3 PRIVATE console)
|
||||
@@ -0,0 +1,3 @@
|
||||
Copy of upstream argtable3 from github
|
||||
|
||||
It is released under the 3-clause BSD license.
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,306 @@
|
||||
/*******************************************************************************
|
||||
* This file is part of the argtable3 library.
|
||||
*
|
||||
* Copyright (C) 1998-2001,2003-2011,2013 Stewart Heitmann
|
||||
* <sheitmann@users.sourceforge.net>
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are met:
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* * Neither the name of STEWART HEITMANN nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL STEWART HEITMANN BE LIABLE FOR ANY DIRECT,
|
||||
* INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
||||
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
******************************************************************************/
|
||||
|
||||
#ifndef ARGTABLE3
|
||||
#define ARGTABLE3
|
||||
|
||||
#include <stdio.h> /* FILE */
|
||||
#include <time.h> /* struct tm */
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define ARG_REX_ICASE 1
|
||||
|
||||
/* bit masks for arg_hdr.flag */
|
||||
enum
|
||||
{
|
||||
ARG_TERMINATOR=0x1,
|
||||
ARG_HASVALUE=0x2,
|
||||
ARG_HASOPTVALUE=0x4
|
||||
};
|
||||
|
||||
typedef void (arg_resetfn)(void *parent);
|
||||
typedef int (arg_scanfn)(void *parent, const char *argval);
|
||||
typedef int (arg_checkfn)(void *parent);
|
||||
typedef void (arg_errorfn)(void *parent, FILE *fp, int error, const char *argval, const char *progname);
|
||||
|
||||
|
||||
/*
|
||||
* The arg_hdr struct defines properties that are common to all arg_xxx structs.
|
||||
* The argtable library requires each arg_xxx struct to have an arg_hdr
|
||||
* struct as its first data member.
|
||||
* The argtable library functions then use this data to identify the
|
||||
* properties of the command line option, such as its option tags,
|
||||
* datatype string, and glossary strings, and so on.
|
||||
* Moreover, the arg_hdr struct contains pointers to custom functions that
|
||||
* are provided by each arg_xxx struct which perform the tasks of parsing
|
||||
* that particular arg_xxx arguments, performing post-parse checks, and
|
||||
* reporting errors.
|
||||
* These functions are private to the individual arg_xxx source code
|
||||
* and are the pointer to them are initiliased by that arg_xxx struct's
|
||||
* constructor function. The user could alter them after construction
|
||||
* if desired, but the original intention is for them to be set by the
|
||||
* constructor and left unaltered.
|
||||
*/
|
||||
struct arg_hdr
|
||||
{
|
||||
char flag; /* Modifier flags: ARG_TERMINATOR, ARG_HASVALUE. */
|
||||
const char *shortopts; /* String defining the short options */
|
||||
const char *longopts; /* String defiing the long options */
|
||||
const char *datatype; /* Description of the argument data type */
|
||||
const char *glossary; /* Description of the option as shown by arg_print_glossary function */
|
||||
int mincount; /* Minimum number of occurences of this option accepted */
|
||||
int maxcount; /* Maximum number of occurences if this option accepted */
|
||||
void *parent; /* Pointer to parent arg_xxx struct */
|
||||
arg_resetfn *resetfn; /* Pointer to parent arg_xxx reset function */
|
||||
arg_scanfn *scanfn; /* Pointer to parent arg_xxx scan function */
|
||||
arg_checkfn *checkfn; /* Pointer to parent arg_xxx check function */
|
||||
arg_errorfn *errorfn; /* Pointer to parent arg_xxx error function */
|
||||
void *priv; /* Pointer to private header data for use by arg_xxx functions */
|
||||
};
|
||||
|
||||
struct arg_rem
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
};
|
||||
|
||||
struct arg_lit
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
};
|
||||
|
||||
struct arg_int
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
int *ival; /* Array of parsed argument values */
|
||||
};
|
||||
|
||||
struct arg_dbl
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
double *dval; /* Array of parsed argument values */
|
||||
};
|
||||
|
||||
struct arg_str
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
const char **sval; /* Array of parsed argument values */
|
||||
};
|
||||
|
||||
struct arg_rex
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args */
|
||||
const char **sval; /* Array of parsed argument values */
|
||||
};
|
||||
|
||||
struct arg_file
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of matching command line args*/
|
||||
const char **filename; /* Array of parsed filenames (eg: /home/foo.bar) */
|
||||
const char **basename; /* Array of parsed basenames (eg: foo.bar) */
|
||||
const char **extension; /* Array of parsed extensions (eg: .bar) */
|
||||
};
|
||||
|
||||
struct arg_date
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
const char *format; /* strptime format string used to parse the date */
|
||||
int count; /* Number of matching command line args */
|
||||
struct tm *tmval; /* Array of parsed time values */
|
||||
};
|
||||
|
||||
enum {ARG_ELIMIT=1, ARG_EMALLOC, ARG_ENOMATCH, ARG_ELONGOPT, ARG_EMISSARG};
|
||||
struct arg_end
|
||||
{
|
||||
struct arg_hdr hdr; /* The mandatory argtable header struct */
|
||||
int count; /* Number of errors encountered */
|
||||
int *error; /* Array of error codes */
|
||||
void **parent; /* Array of pointers to offending arg_xxx struct */
|
||||
const char **argval; /* Array of pointers to offending argv[] string */
|
||||
};
|
||||
|
||||
|
||||
/**** arg_xxx constructor functions *********************************/
|
||||
|
||||
struct arg_rem* arg_rem(const char* datatype, const char* glossary);
|
||||
|
||||
struct arg_lit* arg_lit0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* glossary);
|
||||
struct arg_lit* arg_lit1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char *glossary);
|
||||
struct arg_lit* arg_litn(const char* shortopts,
|
||||
const char* longopts,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_key* arg_key0(const char* keyword,
|
||||
int flags,
|
||||
const char* glossary);
|
||||
struct arg_key* arg_key1(const char* keyword,
|
||||
int flags,
|
||||
const char* glossary);
|
||||
struct arg_key* arg_keyn(const char* keyword,
|
||||
int flags,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char* glossary);
|
||||
|
||||
struct arg_int* arg_int0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char* glossary);
|
||||
struct arg_int* arg_int1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char *glossary);
|
||||
struct arg_int* arg_intn(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char *datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_dbl* arg_dbl0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char* glossary);
|
||||
struct arg_dbl* arg_dbl1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char *glossary);
|
||||
struct arg_dbl* arg_dbln(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char *datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_str* arg_str0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char* glossary);
|
||||
struct arg_str* arg_str1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char *glossary);
|
||||
struct arg_str* arg_strn(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_rex* arg_rex0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* pattern,
|
||||
const char* datatype,
|
||||
int flags,
|
||||
const char* glossary);
|
||||
struct arg_rex* arg_rex1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* pattern,
|
||||
const char* datatype,
|
||||
int flags,
|
||||
const char *glossary);
|
||||
struct arg_rex* arg_rexn(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* pattern,
|
||||
const char* datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
int flags,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_file* arg_file0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char* glossary);
|
||||
struct arg_file* arg_file1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
const char *glossary);
|
||||
struct arg_file* arg_filen(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_date* arg_date0(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* format,
|
||||
const char* datatype,
|
||||
const char* glossary);
|
||||
struct arg_date* arg_date1(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* format,
|
||||
const char* datatype,
|
||||
const char *glossary);
|
||||
struct arg_date* arg_daten(const char* shortopts,
|
||||
const char* longopts,
|
||||
const char* format,
|
||||
const char* datatype,
|
||||
int mincount,
|
||||
int maxcount,
|
||||
const char *glossary);
|
||||
|
||||
struct arg_end* arg_end(int maxerrors);
|
||||
|
||||
|
||||
/**** other functions *******************************************/
|
||||
int arg_nullcheck(void **argtable);
|
||||
int arg_parse(int argc, char **argv, void **argtable);
|
||||
void arg_print_option(FILE *fp, const char *shortopts, const char *longopts, const char *datatype, const char *suffix);
|
||||
void arg_print_syntax(FILE *fp, void **argtable, const char *suffix);
|
||||
void arg_print_syntaxv(FILE *fp, void **argtable, const char *suffix);
|
||||
void arg_print_glossary(FILE *fp, void **argtable, const char *format);
|
||||
void arg_print_glossary_gnu(FILE *fp, void **argtable);
|
||||
void arg_print_errors(FILE* fp, struct arg_end* end, const char* progname);
|
||||
void arg_freetable(void **argtable, size_t n);
|
||||
void arg_print_formatted(FILE *fp, const unsigned lmargin, const unsigned rmargin, const char *text);
|
||||
|
||||
/**** deprecated functions, for back-compatibility only ********/
|
||||
void arg_free(void **argtable);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,42 @@
|
||||
#include <stdio.h>
|
||||
#include <console/console.h>
|
||||
#include <console/utils.h>
|
||||
#include <malloc.h>
|
||||
|
||||
#if CONSOLE_USE_MEMSTREAM
|
||||
|
||||
console_err_t console_filecap_init(console_filecap_t *cap) {
|
||||
cap->buf = NULL;
|
||||
cap->buf_size = 0;
|
||||
cap->file = open_memstream(&cap->buf, &cap->buf_size);
|
||||
if (!cap->file) {
|
||||
return CONSOLE_ERR_NO_MEM;
|
||||
}
|
||||
return CONSOLE_OK;
|
||||
}
|
||||
|
||||
void console_filecap_end(console_filecap_t *cap) {
|
||||
// clean up
|
||||
if (cap->file) {
|
||||
fclose(cap->file);
|
||||
cap->file = NULL;
|
||||
}
|
||||
if (cap->buf) {
|
||||
free(cap->buf); // allocated by memstream
|
||||
cap->buf = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
void console_filecap_print_end(console_filecap_t *cap) {
|
||||
fflush(cap->file);
|
||||
fclose(cap->file);
|
||||
cap->file = NULL;
|
||||
|
||||
if (cap->buf) {
|
||||
console_write(cap->buf, cap->buf_size);
|
||||
free(cap->buf); // allocated by memstream
|
||||
cap->buf = NULL;
|
||||
}
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,194 @@
|
||||
// enable "vasprintf" from stdio.h
|
||||
#ifndef _GNU_SOURCE
|
||||
#define _GNU_SOURCE
|
||||
#endif
|
||||
|
||||
#include "console/console.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdbool.h>
|
||||
#include <stdlib.h>
|
||||
#include <stdarg.h>
|
||||
#include <string.h>
|
||||
|
||||
// These are either implemented using unix file descriptors, or in a platform specific way through a void* context
|
||||
// - then the user code must provide the implementations.
|
||||
#if CONSOLE_USE_FILE_IO_STREAMS
|
||||
#include <unistd.h>
|
||||
|
||||
bool console_have_stdin_ctx(console_ctx_t *ctx)
|
||||
{
|
||||
return ctx && ctx->in &&
|
||||
!feof(ctx->in);
|
||||
}
|
||||
|
||||
/**
|
||||
* Write to console context.
|
||||
*
|
||||
* This is also a Linenoise write callback.
|
||||
*
|
||||
* Return number of characters written, -1 on error.
|
||||
*/
|
||||
int __attribute__((weak)) console_write_ctx(console_ctx_t *ctx, const char *text, size_t len) {
|
||||
if (!ctx || !ctx->out) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t written = fwrite(text, 1, len, ctx->out);
|
||||
if (written != len) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return (int) written;
|
||||
}
|
||||
|
||||
/**
|
||||
* Read from console context's input stream.
|
||||
*
|
||||
* This is also a Linenoise read callback.
|
||||
*
|
||||
* Return number of characters read, -1 on error
|
||||
*/
|
||||
int __attribute__((weak)) console_read_ctx(console_ctx_t *ctx, char *dest, size_t count) {
|
||||
if (!console_have_stdin_ctx(ctx)) return -1;
|
||||
ssize_t readn = fread(dest, 1, (size_t) count, ctx->in);
|
||||
return (int) readn;
|
||||
}
|
||||
|
||||
#if CONSOLE_USE_TERMIOS
|
||||
#include <termio.h>
|
||||
|
||||
int console_can_read_ctx(console_ctx_t *ctx) {
|
||||
if (!console_have_stdin_ctx(ctx)) return -1;
|
||||
|
||||
int fd = fileno(ctx->in);
|
||||
|
||||
struct termios original;
|
||||
tcgetattr(fd, &original);
|
||||
|
||||
struct termios term;
|
||||
memcpy(&term, &original, sizeof(term));
|
||||
|
||||
term.c_lflag &= ~ICANON;
|
||||
tcsetattr(fd, TCSANOW, &term);
|
||||
|
||||
int characters_buffered = 0;
|
||||
ioctl(fd, FIONREAD, &characters_buffered);
|
||||
|
||||
tcsetattr(fd, TCSANOW, &original);
|
||||
|
||||
return characters_buffered;
|
||||
}
|
||||
#endif // CONSOLE_USE_TERMIOS
|
||||
|
||||
#endif // CONSOLE_USE_FILEDES_IO
|
||||
|
||||
ssize_t console_printf_ctx(console_ctx_t *ctx, console_color_t color, const char *format, ...) {
|
||||
if (!ctx) return -1;
|
||||
va_list list;
|
||||
va_start(list, format);
|
||||
ssize_t len = console_vprintf_ctx(ctx, color, format, list);
|
||||
va_end(list);
|
||||
return len;
|
||||
}
|
||||
|
||||
ssize_t console_vprintf_ctx(console_ctx_t *ctx, console_color_t color, const char *format, va_list args) {
|
||||
if (!ctx) return -1;
|
||||
|
||||
if (ctx->use_colors && color != COLOR_RESET) {
|
||||
switch(color) {
|
||||
case COLOR_BLACK:
|
||||
console_write_ctx(ctx, "\x1b[30;1m", 7);
|
||||
break;
|
||||
case COLOR_RED:
|
||||
console_write_ctx(ctx, "\x1b[31;1m", 7);
|
||||
break;
|
||||
case COLOR_GREEN:
|
||||
console_write_ctx(ctx, "\x1b[32;1m", 7);
|
||||
break;
|
||||
case COLOR_YELLOW:
|
||||
console_write_ctx(ctx, "\x1b[33;1m", 7);
|
||||
break;
|
||||
case COLOR_BLUE:
|
||||
console_write_ctx(ctx, "\x1b[34;1m", 7);
|
||||
break;
|
||||
case COLOR_MAGENTA:
|
||||
console_write_ctx(ctx, "\x1b[35;1m", 7);
|
||||
break;
|
||||
case COLOR_CYAN:
|
||||
console_write_ctx(ctx, "\x1b[36;1m", 7);
|
||||
break;
|
||||
//case COLOR_WHITE:
|
||||
default:
|
||||
console_write_ctx(ctx, "\x1b[37;1m", 7);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
char *buf = NULL;
|
||||
ssize_t len = vasprintf(&buf, format, args);
|
||||
if (buf && len >= 0) {
|
||||
// change to actual len written, can also result in -1 on error
|
||||
len = console_write_ctx(ctx, buf, (size_t) len);
|
||||
free(buf); // allocated by vasprintf
|
||||
}
|
||||
|
||||
if (ctx->use_colors && color != COLOR_RESET) {
|
||||
console_write_ctx(ctx, "\x1b[0m", 4);
|
||||
len += 7+4;
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
int console_print_ctx(console_ctx_t *ctx, const char *text) {
|
||||
if (!ctx) return -1;
|
||||
|
||||
return console_write_ctx(ctx, text, (int) strlen(text));
|
||||
}
|
||||
|
||||
ssize_t console_println_ctx(console_ctx_t *ctx, const char *text) {
|
||||
if (!ctx) return -1;
|
||||
|
||||
ssize_t n = console_write_ctx(ctx, text, (int) strlen(text));
|
||||
if (n < 0) return n;
|
||||
ssize_t m = console_write_ctx(ctx, "\n", 2);
|
||||
if (m < 0) return m;
|
||||
return n + m;
|
||||
}
|
||||
|
||||
// ---------------- convenience functions -------------------
|
||||
|
||||
bool console_have_stdin(void) {
|
||||
if (!console_context_available()) return false;
|
||||
return console_have_stdin_ctx(console_active_ctx);
|
||||
}
|
||||
|
||||
int console_can_read(void) {
|
||||
if (!console_have_stdin()) return -1; // Input not available
|
||||
return console_can_read_ctx(console_active_ctx);
|
||||
}
|
||||
|
||||
/**
|
||||
* Linenoise read callback.
|
||||
*
|
||||
* Return number of characters read, -1 on error
|
||||
*/
|
||||
ssize_t console_read(char *dest, size_t count) {
|
||||
if (!console_have_stdin()) return -1;
|
||||
return console_read_ctx(console_active_ctx, dest, count);
|
||||
}
|
||||
|
||||
/**
|
||||
* Linenoise write callback.
|
||||
*
|
||||
* Return number of characters written, -1 on error.
|
||||
*/
|
||||
ssize_t console_write(const char *text, size_t len) {
|
||||
if (!console_context_available()) return -1;
|
||||
return console_write_ctx(console_active_ctx, text, len);
|
||||
}
|
||||
|
||||
ssize_t console_println(const char *text) {
|
||||
if (!console_context_available()) return -1;
|
||||
return console_println_ctx(console_active_ctx, text);
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,248 @@
|
||||
/* linenoise.h -- VERSION 1.0
|
||||
*
|
||||
* Guerrilla line editing library against the idea that a line editing lib
|
||||
* needs to be 20,000 lines of C code.
|
||||
*
|
||||
* See linenoise.c for more information.
|
||||
*
|
||||
* ------------------------------------------------------------------------
|
||||
*
|
||||
* Copyright (c) 2010-2014, Salvatore Sanfilippo <antirez at gmail dot com>
|
||||
* Copyright (c) 2010-2013, Pieter Noordhuis <pcnoordhuis at gmail dot com>
|
||||
*
|
||||
* THIS IS A MODIFIED VERSION THAT REMOVES GLOBAL STATE AND SUPPORTS
|
||||
* OTHER IO THAN STDOUT/STDIN.
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions are
|
||||
* met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
||||
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
||||
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
||||
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
||||
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
||||
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||||
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||||
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
||||
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*/
|
||||
|
||||
#ifndef LINENOISE_H
|
||||
#define LINENOISE_H
|
||||
|
||||
#ifndef LINENOISE_HISTORY_MAX_LINE
|
||||
#define LINENOISE_HISTORY_MAX_LINE 512
|
||||
#endif
|
||||
|
||||
#include <stddef.h>
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#include "console/config.h"
|
||||
|
||||
typedef struct linenoiseCompletions {
|
||||
size_t len;
|
||||
char **cvec;
|
||||
} linenoiseCompletions;
|
||||
|
||||
/**
|
||||
* Get completions
|
||||
*/
|
||||
typedef void (linenoiseCompletionCallback)(const char *typed, linenoiseCompletions *);
|
||||
|
||||
/**
|
||||
* Get a hint for typed text
|
||||
*/
|
||||
typedef char* (linenoiseHintsCallback)(const char *typed, int *color, int *bold);
|
||||
|
||||
/**
|
||||
* Dispose of a hint returned by `linenoiseHintsCallback()`
|
||||
*/
|
||||
typedef void (linenoiseFreeHintsCallback)(void *);
|
||||
|
||||
/**
|
||||
* Linenoise write callback.
|
||||
*
|
||||
* Return number of characters written, -1 on error.
|
||||
* "ctx" may be a fd cast to void* or other value passed during LN init
|
||||
*/
|
||||
typedef int (linenoiseWrite)(void *ctx, const char *text, int len);
|
||||
|
||||
/**
|
||||
* Linenoise read callback.
|
||||
*
|
||||
* Return number of characters read, -1 on error;
|
||||
* "ctx" may be a fd cast to void* or other value passed during LN init
|
||||
*/
|
||||
typedef int (linenoiseRead)(void *ctx, char *dest, int count);
|
||||
|
||||
/**
|
||||
* Linenoise state; exposed in header to allow static allocation
|
||||
*
|
||||
* Use `linenoiseStateInit()` to set default values.
|
||||
*
|
||||
* To shutdown, use `linenoiseHistoryFree()` and then free the struct as needed.
|
||||
*/
|
||||
struct linenoiseState {
|
||||
bool mlmode; /* Multi line mode. Default is single line. */
|
||||
bool dumbmode; /* Dumb mode where line editing is disabled. Off by default */
|
||||
bool echomode; /* Echo (meaningful only in dumb mode) */
|
||||
bool allowCtrlDExit;
|
||||
int history_max_len;
|
||||
int history_len;
|
||||
char **history;
|
||||
|
||||
char *buf; /* Edited line buffer. */
|
||||
size_t buflen; /* Edited line buffer size. */
|
||||
const char *prompt; /* Prompt to display. */
|
||||
size_t plen; /* Prompt length. */
|
||||
int pos; /* Current cursor position. */
|
||||
int oldpos; /* Previous refresh cursor position. */
|
||||
int len; /* Current edited line length. */
|
||||
int cols; /* Number of columns in terminal. */
|
||||
int maxrows; /* Maximum num of rows used so far (multiline mode) */
|
||||
int history_index; /* The history index we are currently editing. */
|
||||
|
||||
linenoiseCompletionCallback *completionCallback;
|
||||
linenoiseHintsCallback *hintsCallback;
|
||||
linenoiseFreeHintsCallback *freeHintsCallback;
|
||||
|
||||
void *rwctx;
|
||||
linenoiseWrite *write;
|
||||
linenoiseRead *read;
|
||||
};
|
||||
|
||||
/**
|
||||
* Initialize the state struct to defaults.
|
||||
*
|
||||
* Before the library is used, also set prompt, buffer,
|
||||
* the read/write callbacks, r/w context (if used),
|
||||
* completion, hint and free-hint callbacks
|
||||
*/
|
||||
void consLnStateInit(struct linenoiseState *ls);
|
||||
|
||||
/** Set buffer and its capacity */
|
||||
static inline void consLnSetBuf(struct linenoiseState *ls, char *buf, size_t cap) {
|
||||
ls->buf = buf;
|
||||
ls->buflen = cap - 1; /* Make sure there is always space for the nulterm */
|
||||
}
|
||||
|
||||
/** Set prompt pointer. Can be constant or dynamically allocated.
|
||||
* Will NOT be mutated by the library. */
|
||||
static inline void consLnSetPrompt(struct linenoiseState *ls, const char *prompt) {
|
||||
ls->prompt = prompt;
|
||||
}
|
||||
|
||||
/** Set buffer and its capacity */
|
||||
static inline void consLnSetReadWrite(struct linenoiseState *ls, linenoiseRead *read, linenoiseWrite *write, void *ctx) {
|
||||
ls->read = read;
|
||||
ls->write = write;
|
||||
ls->rwctx = ctx;
|
||||
}
|
||||
|
||||
/** Set completion CB */
|
||||
static inline void consLnSetCompletionCallback(struct linenoiseState *ls, linenoiseCompletionCallback *compl) {
|
||||
ls->completionCallback = compl;
|
||||
}
|
||||
|
||||
/** Set hint CB */
|
||||
static inline void consLnSetHintsCallback(struct linenoiseState *ls, linenoiseHintsCallback *hints) {
|
||||
ls->hintsCallback = hints;
|
||||
}
|
||||
|
||||
/** Set free hints CB */
|
||||
static inline void consLnSetFreeHintsCallback(struct linenoiseState *ls, linenoiseFreeHintsCallback *freeh) {
|
||||
ls->freeHintsCallback = freeh;
|
||||
}
|
||||
|
||||
/** This function is used by the callback function registered by the user
|
||||
* in order to add completion options given the input string when the
|
||||
* user typed <tab>. See the example.c source code for a very easy to
|
||||
* understand example.
|
||||
*
|
||||
* The completion will be duplicated, it does not need to live past calling
|
||||
* this function.
|
||||
* */
|
||||
void consLnAddCompletion(linenoiseCompletions *lc, const char *text);
|
||||
|
||||
/** The high level function that is the main API of the linenoise library. */
|
||||
int consLnReadLine(struct linenoiseState *ls); // buffer is in "ls"
|
||||
|
||||
/** This is the API call to add a new entry in the linenoise history.
|
||||
* It uses a fixed array of char pointers that are shifted (memmoved)
|
||||
* when the history max length is reached in order to remove the older
|
||||
* entry and make room for the new one, so it is not exactly suitable for huge
|
||||
* histories, but will work well for a few hundred of entries.
|
||||
*
|
||||
* Using a circular buffer is smarter, but a bit more complex to handle. */
|
||||
int consLnHistoryAdd(struct linenoiseState *ls, const char *line);
|
||||
|
||||
/** Set the maximum length for the history. This function can be called even
|
||||
* if there is already some history, the function will make sure to retain
|
||||
* just the latest 'len' elements if the new history length value is smaller
|
||||
* than the amount of items already inside the history. */
|
||||
int consLnHistorySetMaxLen(struct linenoiseState *ls, int len);
|
||||
|
||||
#if CONSOLE_FILE_SUPPORT
|
||||
/** Save the history in the specified file. On success 0 is returned,
|
||||
* on error -1 is returned. */
|
||||
int consLnHistorySave(struct linenoiseState *ls, const char *filename);
|
||||
|
||||
/** Load the history from the specified file. If the file does not exist
|
||||
* zero is returned and no operation is performed.
|
||||
*
|
||||
* If the file exists and the operation succeeded 0 is returned, otherwise
|
||||
* on error -1 is returned. */
|
||||
int consLnHistoryLoad(struct linenoiseState *ls, const char *filename);
|
||||
#endif
|
||||
|
||||
/** Free history buffer for instance */
|
||||
void consLnHistoryFree(struct linenoiseState *ls);
|
||||
|
||||
/** Clear the screen. Used to handle ctrl+l */
|
||||
void consLnClearScreen(struct linenoiseState *ls);
|
||||
|
||||
/** Set if to use or not the multi line mode. */
|
||||
static inline void consLnSetMultiLine(struct linenoiseState *ls, int ml) {
|
||||
ls->mlmode = ml;
|
||||
}
|
||||
|
||||
/** Get ml mode state */
|
||||
static inline bool consLnGetMultiLine(struct linenoiseState *ls) {
|
||||
return ls->mlmode;
|
||||
}
|
||||
|
||||
/** Set if terminal does not recognize escape sequences */
|
||||
static inline void consLnSetDumbMode(struct linenoiseState *ls, int dumb) {
|
||||
ls->dumbmode = dumb;
|
||||
}
|
||||
|
||||
/** Get dumb mode state */
|
||||
static inline bool consLnGetDumbMode(struct linenoiseState *ls) {
|
||||
return ls->dumbmode;
|
||||
}
|
||||
|
||||
/** Enable/disable echo on keypress (only applies to dumb mode) */
|
||||
static inline void consLnSetEchoMode(struct linenoiseState *ls, int set) {
|
||||
ls->echomode = set;
|
||||
}
|
||||
|
||||
/** Get echo mode state */
|
||||
static inline bool consLnGetEchoMode(struct linenoiseState *ls) {
|
||||
return ls->echomode;
|
||||
}
|
||||
|
||||
#endif /* LINENOISE_H */
|
||||
@@ -0,0 +1,196 @@
|
||||
#include <stddef.h>
|
||||
#include <stdbool.h>
|
||||
#include <string.h>
|
||||
#include "console/prefix_match.h"
|
||||
|
||||
int prefix_match(const char *value, const char **options, int flags) {
|
||||
flags &= ~PREFIXMATCH_MULTI_PARTIAL; // this doesn't make sense here
|
||||
bool case_sensitive = PREFIXMATCH_CASE_SENSITIVE == (flags & PREFIXMATCH_CASE_SENSITIVE);
|
||||
bool can_abbrev = 0 == (flags & PREFIXMATCH_NOABBREV);
|
||||
|
||||
int (*cmpfn) (const char *, const char *) = case_sensitive ? strcmp : strcasecmp;
|
||||
int (*ncmpfn) (const char *, const char *, size_t) = case_sensitive ? strncmp : strncasecmp;
|
||||
|
||||
if (!value || !options) return -1;
|
||||
size_t input_len = strlen(value);
|
||||
const char *option = NULL;
|
||||
int counter = 0;
|
||||
int result = -1;
|
||||
while (NULL != (option = options[counter])) {
|
||||
if (cmpfn(option, value) == 0) {
|
||||
return counter; // full exact match
|
||||
} else {
|
||||
// Test for partial match
|
||||
if (can_abbrev && ncmpfn(value, option, input_len) == 0) {
|
||||
if (result == -1) {
|
||||
result = counter; // first partial match
|
||||
} else {
|
||||
// ambiguous match
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
size_t pm_word_len(const char * restrict word, const char * restrict delims) {
|
||||
char d;
|
||||
const char *dp = delims;
|
||||
size_t word_len = 0;
|
||||
if (!word || !delims) return 0;
|
||||
while ('\0' != (d = *dp++)) {
|
||||
char *end = strchr(word, d);
|
||||
if (NULL == end) continue;
|
||||
size_t len = end - word;
|
||||
if (!word_len || len < word_len) {
|
||||
word_len = len;
|
||||
}
|
||||
}
|
||||
if (!word_len) {
|
||||
word_len = strlen(word);
|
||||
}
|
||||
return word_len;
|
||||
}
|
||||
|
||||
size_t pm_count_words(const char * restrict sentence, const char * restrict delims) {
|
||||
char c;
|
||||
size_t n = 0;
|
||||
bool in_word = false;
|
||||
if (!sentence || !delims) return 0;
|
||||
while (0 != (c = *sentence++)) {
|
||||
bool is_delim = NULL != strchr(delims, c);
|
||||
if (is_delim && in_word) {
|
||||
in_word = false;
|
||||
} else if (!in_word && !is_delim) {
|
||||
n++;
|
||||
in_word = true;
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
|
||||
const char *pm_skip_words(const char * restrict sentence, const char * restrict delims, size_t skip) {
|
||||
char c;
|
||||
size_t n = 0;
|
||||
bool in_word = false;
|
||||
if (!sentence || !delims) return NULL;
|
||||
while (0 != (c = *sentence++)) {
|
||||
bool is_delim = NULL != strchr(delims, c);
|
||||
if (is_delim && in_word) {
|
||||
in_word = false;
|
||||
skip--;
|
||||
if (skip == 0) {
|
||||
return sentence - 1;
|
||||
}
|
||||
} else if (!in_word && !is_delim) {
|
||||
n++;
|
||||
in_word = true;
|
||||
}
|
||||
}
|
||||
return sentence - 1;
|
||||
}
|
||||
|
||||
enum pm_test_result prefix_multipart_test(
|
||||
const char * restrict tested,
|
||||
const char* restrict full,
|
||||
const char * restrict delims,
|
||||
int flags
|
||||
) {
|
||||
bool case_sensitive = PREFIXMATCH_CASE_SENSITIVE == (flags & PREFIXMATCH_CASE_SENSITIVE);
|
||||
bool can_abbrev = 0 == (flags & PREFIXMATCH_NOABBREV);
|
||||
|
||||
int (*ncmpfn) (const char *, const char *, size_t) = case_sensitive ? strncmp : strncasecmp;
|
||||
// lazy shortcut first...
|
||||
if ((case_sensitive && 0 == strcmp(tested, full)) || (!case_sensitive && 0 == strcasecmp(tested, full))) {
|
||||
return PM_TEST_MATCH; // full match
|
||||
}
|
||||
|
||||
const char *word_t = tested;
|
||||
const char *word_f = full;
|
||||
size_t word_t_len = 0;
|
||||
size_t word_f_len = 0;
|
||||
while (1) {
|
||||
word_t += word_t_len;
|
||||
word_f += word_f_len;
|
||||
|
||||
// advance past leading delims, if any
|
||||
while (*word_t != '\0' && NULL != strchr(delims, *word_t)) word_t++;
|
||||
while (*word_f != '\0' && NULL != strchr(delims, *word_f)) word_f++;
|
||||
|
||||
// test for terminator
|
||||
if (*word_t == '\0' && *word_f == '\0') {
|
||||
// both ended at the same number of words
|
||||
return PM_TEST_MATCH; // full match
|
||||
}
|
||||
|
||||
if (*word_t == '\0' || *word_f == '\0') {
|
||||
// sentences ended at different length
|
||||
if (0 != (flags & PREFIXMATCH_MULTI_PARTIAL) && *word_f != '\0') { // word prefix match (a is a prefix of b)
|
||||
return PM_TEST_MATCH_MULTI_PARTIAL;
|
||||
} else {
|
||||
return PM_TEST_NO_MATCH;
|
||||
}
|
||||
}
|
||||
|
||||
// find end of the words
|
||||
word_t_len = pm_word_len(word_t, delims);
|
||||
word_f_len = pm_word_len(word_f, delims);
|
||||
|
||||
if (word_t_len > word_f_len || (!can_abbrev && word_t_len != word_f_len)) {
|
||||
return PM_TEST_NO_MATCH;
|
||||
}
|
||||
|
||||
int cmp = ncmpfn(word_t, word_f, word_t_len);
|
||||
if (0 != cmp) { // words differ
|
||||
return PM_TEST_NO_MATCH;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int prefix_multipart_match(const char * restrict value, const char **options, const char * restrict delims, int flags) {
|
||||
bool multi_partial = 0 != (flags & PREFIXMATCH_MULTI_PARTIAL);
|
||||
flags &= ~PREFIXMATCH_MULTI_PARTIAL; // turn it off for passing the to test fn
|
||||
bool can_abbrev = 0 == (flags & PREFIXMATCH_NOABBREV);
|
||||
|
||||
if (!value || !options) return -1;
|
||||
const char *option = NULL;
|
||||
int counter = 0;
|
||||
int result = -1;
|
||||
int result_partial = -1; // -1=none yet, -2=ambiguous multi-word partial, >=0=index
|
||||
int result_partial_nwords = 0;
|
||||
while (NULL != (option = options[counter])) {
|
||||
if (PM_TEST_MATCH == prefix_multipart_test(value, option, delims, flags | PREFIXMATCH_NOABBREV)) {
|
||||
return counter; // full exact match
|
||||
} else if (can_abbrev) {
|
||||
// Test for partial match
|
||||
if (PM_TEST_MATCH == prefix_multipart_test(value, option, delims, flags)) {
|
||||
if (result == -1) {
|
||||
result = counter; // first partial match in all words
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
} else if (multi_partial && PM_TEST_MATCH_MULTI_PARTIAL == prefix_multipart_test(value, option, delims, flags | PREFIXMATCH_MULTI_PARTIAL)) {
|
||||
int nwords = pm_count_words(option, delims);
|
||||
if (result_partial == -1 || result_partial_nwords < nwords) {
|
||||
result_partial = counter; // first partial match
|
||||
result_partial_nwords = nwords;
|
||||
} else {
|
||||
result_partial = -2;
|
||||
}
|
||||
}
|
||||
}
|
||||
counter++;
|
||||
}
|
||||
|
||||
if (result != -1) {
|
||||
return result;
|
||||
}
|
||||
|
||||
if (result_partial >= 0) {
|
||||
return result_partial;
|
||||
}
|
||||
|
||||
return -1;
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
// Copyright 2016-2017 Espressif Systems (Shanghai) PTE LTD
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
|
||||
#include <stdio.h>
|
||||
#include <ctype.h>
|
||||
#include <string.h>
|
||||
|
||||
#define SS_FLAG_ESCAPE 0x8
|
||||
|
||||
typedef enum {
|
||||
/* parsing the space between arguments */
|
||||
SS_SPACE = 0x0,
|
||||
/* parsing an argument which isn't quoted */
|
||||
SS_ARG = 0x1,
|
||||
/* parsing a quoted argument */
|
||||
SS_QUOTED_ARG = 0x2,
|
||||
/* parsing an escape sequence within unquoted argument */
|
||||
SS_ARG_ESCAPED = SS_ARG | SS_FLAG_ESCAPE,
|
||||
/* parsing an escape sequence within a quoted argument */
|
||||
SS_QUOTED_ARG_ESCAPED = SS_QUOTED_ARG | SS_FLAG_ESCAPE,
|
||||
} split_state_t;
|
||||
|
||||
size_t console_split_argv(char *line, char **argv, size_t argv_size)
|
||||
{
|
||||
const int QUOTE = '"';
|
||||
const int ESCAPE = '\\';
|
||||
const int SPACE = ' ';
|
||||
split_state_t state = SS_SPACE;
|
||||
int argc = 0;
|
||||
char *next_arg_start = line;
|
||||
char *out_ptr = line;
|
||||
for (char *in_ptr = line; argc < (int)(argv_size - 1); ++in_ptr) {
|
||||
int char_in = (unsigned char) *in_ptr;
|
||||
if (char_in == 0) {
|
||||
break;
|
||||
}
|
||||
int char_out = -1;
|
||||
|
||||
/* helper function, called when done with an argument */
|
||||
void end_arg() {
|
||||
char_out = 0;
|
||||
argv[argc++] = next_arg_start;
|
||||
state = SS_SPACE;
|
||||
}
|
||||
|
||||
switch (state) {
|
||||
case SS_SPACE:
|
||||
if (char_in == SPACE) {
|
||||
/* skip space */
|
||||
} else if (char_in == QUOTE) {
|
||||
next_arg_start = out_ptr;
|
||||
state = SS_QUOTED_ARG;
|
||||
} else if (char_in == ESCAPE) {
|
||||
next_arg_start = out_ptr;
|
||||
state = SS_ARG_ESCAPED;
|
||||
} else {
|
||||
next_arg_start = out_ptr;
|
||||
state = SS_ARG;
|
||||
char_out = char_in;
|
||||
}
|
||||
break;
|
||||
|
||||
case SS_QUOTED_ARG:
|
||||
if (char_in == QUOTE) {
|
||||
end_arg();
|
||||
} else if (char_in == ESCAPE) {
|
||||
state = SS_QUOTED_ARG_ESCAPED;
|
||||
} else {
|
||||
char_out = char_in;
|
||||
}
|
||||
break;
|
||||
|
||||
case SS_ARG_ESCAPED:
|
||||
case SS_QUOTED_ARG_ESCAPED:
|
||||
if (char_in == ESCAPE || char_in == QUOTE || char_in == SPACE) {
|
||||
char_out = char_in;
|
||||
} else {
|
||||
/* unrecognized escape character, skip */
|
||||
}
|
||||
state = (split_state_t) (state & (~SS_FLAG_ESCAPE));
|
||||
break;
|
||||
|
||||
case SS_ARG:
|
||||
if (char_in == SPACE) {
|
||||
end_arg();
|
||||
} else if (char_in == ESCAPE) {
|
||||
state = SS_ARG_ESCAPED;
|
||||
} else {
|
||||
char_out = char_in;
|
||||
}
|
||||
break;
|
||||
}
|
||||
/* need to output anything? */
|
||||
if (char_out >= 0) {
|
||||
*out_ptr = char_out;
|
||||
++out_ptr;
|
||||
}
|
||||
}
|
||||
/* make sure the final argument is terminated */
|
||||
*out_ptr = 0;
|
||||
/* finalize the last argument */
|
||||
if (state != SS_SPACE && argc < ((int)argv_size - 1)) {
|
||||
argv[argc++] = next_arg_start;
|
||||
}
|
||||
/* add a NULL at the end of argv */
|
||||
argv[argc] = NULL;
|
||||
|
||||
return argc;
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
/**
|
||||
* Command argument splitting
|
||||
*
|
||||
* Created on 2020/02/28.
|
||||
*/
|
||||
|
||||
#ifndef VCOM_CONSOLE_SPLIT_ARGV_H
|
||||
#define VCOM_CONSOLE_SPLIT_ARGV_H
|
||||
|
||||
/**
|
||||
* @brief Split command line into arguments in place
|
||||
*
|
||||
* - This function finds whitespace-separated arguments in the given input line.
|
||||
*
|
||||
* 'abc def 1 20 .3' -> [ 'abc', 'def', '1', '20', '.3' ]
|
||||
*
|
||||
* - Argument which include spaces may be surrounded with quotes. In this case
|
||||
* spaces are preserved and quotes are stripped.
|
||||
*
|
||||
* 'abc "123 456" def' -> [ 'abc', '123 456', 'def' ]
|
||||
*
|
||||
* - Escape sequences may be used to produce backslash, double quote, and space:
|
||||
*
|
||||
* 'a\ b\\c\"' -> [ 'a b\c"' ]
|
||||
*
|
||||
* Pointers to at most argv_size - 1 arguments are returned in argv array.
|
||||
* The pointer after the last one (i.e. argv[argc]) is set to NULL.
|
||||
*
|
||||
* @param line pointer to buffer to parse; it is modified in place
|
||||
* @param argv array where the pointers to arguments are written
|
||||
* @param argv_size number of elements in argv_array (max. number of arguments)
|
||||
* @return number of arguments found (argc)
|
||||
*/
|
||||
size_t console_split_argv(char *line, char **argv, size_t argv_size);
|
||||
|
||||
#endif //VCOM_CONSOLE_SPLIT_ARGV_H
|
||||
@@ -0,0 +1,204 @@
|
||||
//
|
||||
// Created by MightyPork on 2020/03/11.
|
||||
//
|
||||
|
||||
#include <string.h>
|
||||
#include <stdint.h>
|
||||
#include "console/console.h"
|
||||
#include "console/utils.h"
|
||||
|
||||
#if (CONSOLE_HAVE_CSP && !CSP_POSIX) || CONSOLE_TESTING_ALLOC_FUNCS
|
||||
#include <csp/arch/csp_malloc.h>
|
||||
#else
|
||||
#include <malloc.h>
|
||||
#endif
|
||||
|
||||
// Based on: https://stackoverflow.com/a/7776146/2180189
|
||||
void console_hexdump(const void *data, size_t len)
|
||||
{
|
||||
size_t i;
|
||||
char asciibuf[17];
|
||||
const unsigned char *pc = (const unsigned char *) data;
|
||||
|
||||
// Length checks.
|
||||
|
||||
if (len <= 0) {
|
||||
console_print("BAD LENGTH\r\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Process every byte in the data.
|
||||
for (i = 0; i < len; i++) {
|
||||
size_t ai = i % 16;
|
||||
|
||||
// Multiple of 16 means new line (with line offset).
|
||||
if (ai == 0) {
|
||||
// Don't print ASCII buffer for the "zeroth" line.
|
||||
if (i != 0) {
|
||||
console_printf(" |%s|\r\n", asciibuf);
|
||||
}
|
||||
|
||||
// Output the offset.
|
||||
|
||||
console_printf("%4d :", (int)i);
|
||||
}
|
||||
|
||||
// Now the hex code for the specific character.
|
||||
console_printf(" %02x", pc[i]);
|
||||
|
||||
// And buffer a printable ASCII character for later.
|
||||
if ((pc[i] < 0x20) || (pc[i] > 0x7e)) {
|
||||
asciibuf[ai] = '.';
|
||||
}
|
||||
else {
|
||||
asciibuf[ai] = (char) pc[i];
|
||||
}
|
||||
|
||||
asciibuf[ai + 1] = '\0';
|
||||
}
|
||||
|
||||
// Pad out last line if not exactly 16 characters.
|
||||
while ((i % 16) != 0) {
|
||||
console_print(" ");
|
||||
i++;
|
||||
}
|
||||
|
||||
// And print the final ASCII buffer.
|
||||
console_printf(" |%s|\r\n", asciibuf);
|
||||
}
|
||||
|
||||
static int hex_char_decode(char x)
|
||||
{
|
||||
if (x >= '0' && x <= '9') {
|
||||
return x - '0';
|
||||
}
|
||||
else if (x >= 'a' && x <= 'f') {
|
||||
return 10 + (x - 'a');
|
||||
}
|
||||
else if (x >= 'A' && x <= 'F') {
|
||||
return 10 + (x - 'A');
|
||||
}
|
||||
else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
int console_base16_decode(const char *hex, void *dest, size_t capacity)
|
||||
{
|
||||
if (!hex || !dest) return -1;
|
||||
|
||||
const char *px = (const char *) hex;
|
||||
uint8_t *pd = (unsigned char *) dest;
|
||||
int hlen = (int) strlen(hex);
|
||||
if (hlen % 2) {
|
||||
return -2;
|
||||
}
|
||||
int blen = hlen / 2;
|
||||
if (blen > (int) capacity) {
|
||||
return -3;
|
||||
}
|
||||
|
||||
int v;
|
||||
uint8_t byte;
|
||||
for (int i = 0; i < blen; i++) {
|
||||
v = hex_char_decode(*px++);
|
||||
if (v == -1) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
byte = (v & 0x0F) << 4;
|
||||
|
||||
v = hex_char_decode(*px++);
|
||||
if (v == -1) {
|
||||
return -2;
|
||||
}
|
||||
|
||||
byte |= (v & 0x0F);
|
||||
|
||||
*pd++ = byte;
|
||||
}
|
||||
|
||||
return blen;
|
||||
}
|
||||
|
||||
void * __attribute__((malloc)) console_malloc(size_t size) {
|
||||
#if (CONSOLE_HAVE_CSP && !CSP_POSIX) || CONSOLE_TESTING_ALLOC_FUNCS
|
||||
return csp_malloc(size);
|
||||
#else
|
||||
return malloc(size);
|
||||
#endif
|
||||
}
|
||||
|
||||
void * console_realloc(void *ptr, size_t oldsize, size_t newsize) {
|
||||
#if (CONSOLE_HAVE_CSP && !CSP_POSIX) || CONSOLE_TESTING_ALLOC_FUNCS
|
||||
// csp / freertos do not provide realloc, simply allocate a new one and copy data over.
|
||||
|
||||
if (oldsize >= newsize) {
|
||||
// no realloc needed
|
||||
if (!ptr) { // NULL was given, act as malloc
|
||||
ptr = csp_malloc(newsize);
|
||||
}
|
||||
return ptr;
|
||||
}
|
||||
|
||||
void *tmp = csp_malloc(newsize);
|
||||
if (!tmp) {
|
||||
// "If realloc() fails, the original block is left untouched; it is not freed or moved."
|
||||
return NULL;
|
||||
}
|
||||
if (ptr) {
|
||||
// copy if there is anything to copy from
|
||||
memcpy(tmp, ptr, oldsize); // we know oldsize < newsize, otherwise the check above returns.
|
||||
// discard the old buffer
|
||||
csp_free(ptr);
|
||||
}
|
||||
return tmp;
|
||||
#else
|
||||
(void) oldsize; // unused
|
||||
return realloc(ptr, newsize);
|
||||
#endif
|
||||
}
|
||||
|
||||
void * __attribute__((malloc,alloc_size(1,2))) console_calloc(size_t nmemb, size_t size) {
|
||||
#if (CONSOLE_HAVE_CSP && !CSP_POSIX) || CONSOLE_TESTING_ALLOC_FUNCS
|
||||
return csp_calloc(nmemb, size);
|
||||
#else
|
||||
return calloc(nmemb, size);
|
||||
#endif
|
||||
}
|
||||
|
||||
void console_free(void *ptr) {
|
||||
#if (CONSOLE_HAVE_CSP && !CSP_POSIX) || CONSOLE_TESTING_ALLOC_FUNCS
|
||||
return csp_free(ptr);
|
||||
#else
|
||||
return free(ptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
char * console_strdup(const char *ptr) {
|
||||
#if (CONSOLE_HAVE_CSP && !CSP_POSIX) || CONSOLE_TESTING_ALLOC_FUNCS
|
||||
if (!ptr) return NULL;
|
||||
size_t len = strlen(ptr);
|
||||
char *copy = console_malloc(len+1);
|
||||
if (!copy) return NULL;
|
||||
strncpy(copy, ptr, len);
|
||||
copy[len]=0;
|
||||
return copy;
|
||||
#else
|
||||
return strdup(ptr);
|
||||
#endif
|
||||
}
|
||||
|
||||
char * console_strndup(const char *ptr, size_t maxlen) {
|
||||
#if (CONSOLE_HAVE_CSP && !CSP_POSIX) || CONSOLE_TESTING_ALLOC_FUNCS
|
||||
if (!ptr) return NULL;
|
||||
size_t len = strnlen(ptr, maxlen);
|
||||
char *copy = console_malloc(len+1);
|
||||
if (!copy) return NULL;
|
||||
strncpy(copy, ptr, len);
|
||||
copy[len]=0;
|
||||
return copy;
|
||||
#else
|
||||
return strndup(ptr, maxlen);
|
||||
#endif
|
||||
}
|
||||
@@ -0,0 +1,645 @@
|
||||
/*-
|
||||
* Copyright (c) 1991, 1993
|
||||
* The Regents of the University of California. All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
* are met:
|
||||
* 1. Redistributions of source code must retain the above copyright
|
||||
* notice, this list of conditions and the following disclaimer.
|
||||
* 2. Redistributions in binary form must reproduce the above copyright
|
||||
* notice, this list of conditions and the following disclaimer in the
|
||||
* documentation and/or other materials provided with the distribution.
|
||||
* 4. Neither the name of the University nor the names of its contributors
|
||||
* may be used to endorse or promote products derived from this software
|
||||
* without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
|
||||
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
||||
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
||||
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
||||
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* @(#)queue.h 8.5 (Berkeley) 8/20/94
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
#ifndef _SYS_QUEUE_H_
|
||||
#define _SYS_QUEUE_H_
|
||||
|
||||
#include <sys/cdefs.h>
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/*
|
||||
* This file defines four types of data structures: singly-linked lists,
|
||||
* singly-linked tail queues, lists and tail queues.
|
||||
*
|
||||
* A singly-linked list is headed by a single forward pointer. The elements
|
||||
* are singly linked for minimum space and pointer manipulation overhead at
|
||||
* the expense of O(n) removal for arbitrary elements. New elements can be
|
||||
* added to the list after an existing element or at the head of the list.
|
||||
* Elements being removed from the head of the list should use the explicit
|
||||
* macro for this purpose for optimum efficiency. A singly-linked list may
|
||||
* only be traversed in the forward direction. Singly-linked lists are ideal
|
||||
* for applications with large datasets and few or no removals or for
|
||||
* implementing a LIFO queue.
|
||||
*
|
||||
* A singly-linked tail queue is headed by a pair of pointers, one to the
|
||||
* head of the list and the other to the tail of the list. The elements are
|
||||
* singly linked for minimum space and pointer manipulation overhead at the
|
||||
* expense of O(n) removal for arbitrary elements. New elements can be added
|
||||
* to the list after an existing element, at the head of the list, or at the
|
||||
* end of the list. Elements being removed from the head of the tail queue
|
||||
* should use the explicit macro for this purpose for optimum efficiency.
|
||||
* A singly-linked tail queue may only be traversed in the forward direction.
|
||||
* Singly-linked tail queues are ideal for applications with large datasets
|
||||
* and few or no removals or for implementing a FIFO queue.
|
||||
*
|
||||
* A list is headed by a single forward pointer (or an array of forward
|
||||
* pointers for a hash table header). The elements are doubly linked
|
||||
* so that an arbitrary element can be removed without a need to
|
||||
* traverse the list. New elements can be added to the list before
|
||||
* or after an existing element or at the head of the list. A list
|
||||
* may only be traversed in the forward direction.
|
||||
*
|
||||
* A tail queue is headed by a pair of pointers, one to the head of the
|
||||
* list and the other to the tail of the list. The elements are doubly
|
||||
* linked so that an arbitrary element can be removed without a need to
|
||||
* traverse the list. New elements can be added to the list before or
|
||||
* after an existing element, at the head of the list, or at the end of
|
||||
* the list. A tail queue may be traversed in either direction.
|
||||
*
|
||||
* For details on the use of these macros, see the queue(3) manual page.
|
||||
*
|
||||
*
|
||||
* SLIST LIST STAILQ TAILQ
|
||||
* _HEAD + + + +
|
||||
* _HEAD_INITIALIZER + + + +
|
||||
* _ENTRY + + + +
|
||||
* _INIT + + + +
|
||||
* _EMPTY + + + +
|
||||
* _FIRST + + + +
|
||||
* _NEXT + + + +
|
||||
* _PREV - - - +
|
||||
* _LAST - - + +
|
||||
* _FOREACH + + + +
|
||||
* _FOREACH_SAFE + + + +
|
||||
* _FOREACH_REVERSE - - - +
|
||||
* _FOREACH_REVERSE_SAFE - - - +
|
||||
* _INSERT_HEAD + + + +
|
||||
* _INSERT_BEFORE - + - +
|
||||
* _INSERT_AFTER + + + +
|
||||
* _INSERT_TAIL - - + +
|
||||
* _CONCAT - - + +
|
||||
* _REMOVE_AFTER + - + -
|
||||
* _REMOVE_HEAD + - + -
|
||||
* _REMOVE + + + +
|
||||
*
|
||||
*/
|
||||
#ifdef QUEUE_MACRO_DEBUG
|
||||
/* Store the last 2 places the queue element or head was altered */
|
||||
struct qm_trace {
|
||||
char * lastfile;
|
||||
int lastline;
|
||||
char * prevfile;
|
||||
int prevline;
|
||||
};
|
||||
|
||||
#define TRACEBUF struct qm_trace trace;
|
||||
#define TRASHIT(x) do {(x) = (void *)-1;} while (0)
|
||||
#define QMD_SAVELINK(name, link) void **name = (void *)&(link)
|
||||
|
||||
#define QMD_TRACE_HEAD(head) do { \
|
||||
(head)->trace.prevline = (head)->trace.lastline; \
|
||||
(head)->trace.prevfile = (head)->trace.lastfile; \
|
||||
(head)->trace.lastline = __LINE__; \
|
||||
(head)->trace.lastfile = __FILE__; \
|
||||
} while (0)
|
||||
|
||||
#define QMD_TRACE_ELEM(elem) do { \
|
||||
(elem)->trace.prevline = (elem)->trace.lastline; \
|
||||
(elem)->trace.prevfile = (elem)->trace.lastfile; \
|
||||
(elem)->trace.lastline = __LINE__; \
|
||||
(elem)->trace.lastfile = __FILE__; \
|
||||
} while (0)
|
||||
|
||||
#else
|
||||
#define QMD_TRACE_ELEM(elem)
|
||||
#define QMD_TRACE_HEAD(head)
|
||||
#define QMD_SAVELINK(name, link)
|
||||
#define TRACEBUF
|
||||
#define TRASHIT(x)
|
||||
#endif /* QUEUE_MACRO_DEBUG */
|
||||
|
||||
/*
|
||||
* Singly-linked List declarations.
|
||||
*/
|
||||
#define SLIST_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *slh_first; /* first element */ \
|
||||
}
|
||||
|
||||
#define SLIST_HEAD_INITIALIZER(head) \
|
||||
{ NULL }
|
||||
|
||||
#define SLIST_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *sle_next; /* next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* Singly-linked List functions.
|
||||
*/
|
||||
#define SLIST_EMPTY(head) ((head)->slh_first == NULL)
|
||||
|
||||
#define SLIST_FIRST(head) ((head)->slh_first)
|
||||
|
||||
#define SLIST_FOREACH(var, head, field) \
|
||||
for ((var) = SLIST_FIRST((head)); \
|
||||
(var); \
|
||||
(var) = SLIST_NEXT((var), field))
|
||||
|
||||
#define SLIST_FOREACH_SAFE(var, head, field, tvar) \
|
||||
for ((var) = SLIST_FIRST((head)); \
|
||||
(var) && ((tvar) = SLIST_NEXT((var), field), 1); \
|
||||
(var) = (tvar))
|
||||
|
||||
#define SLIST_FOREACH_PREVPTR(var, varp, head, field) \
|
||||
for ((varp) = &SLIST_FIRST((head)); \
|
||||
((var) = *(varp)) != NULL; \
|
||||
(varp) = &SLIST_NEXT((var), field))
|
||||
|
||||
#define SLIST_INIT(head) do { \
|
||||
SLIST_FIRST((head)) = NULL; \
|
||||
} while (0)
|
||||
|
||||
#define SLIST_INSERT_AFTER(slistelm, elm, field) do { \
|
||||
SLIST_NEXT((elm), field) = SLIST_NEXT((slistelm), field); \
|
||||
SLIST_NEXT((slistelm), field) = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define SLIST_INSERT_HEAD(head, elm, field) do { \
|
||||
SLIST_NEXT((elm), field) = SLIST_FIRST((head)); \
|
||||
SLIST_FIRST((head)) = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define SLIST_NEXT(elm, field) ((elm)->field.sle_next)
|
||||
|
||||
#define SLIST_REMOVE(head, elm, type, field) do { \
|
||||
QMD_SAVELINK(oldnext, (elm)->field.sle_next); \
|
||||
if (SLIST_FIRST((head)) == (elm)) { \
|
||||
SLIST_REMOVE_HEAD((head), field); \
|
||||
} \
|
||||
else { \
|
||||
struct type *curelm = SLIST_FIRST((head)); \
|
||||
while (SLIST_NEXT(curelm, field) != (elm)) \
|
||||
curelm = SLIST_NEXT(curelm, field); \
|
||||
SLIST_REMOVE_AFTER(curelm, field); \
|
||||
} \
|
||||
TRASHIT(*oldnext); \
|
||||
} while (0)
|
||||
|
||||
#define SLIST_REMOVE_AFTER(elm, field) do { \
|
||||
SLIST_NEXT(elm, field) = \
|
||||
SLIST_NEXT(SLIST_NEXT(elm, field), field); \
|
||||
} while (0)
|
||||
|
||||
#define SLIST_REMOVE_HEAD(head, field) do { \
|
||||
SLIST_FIRST((head)) = SLIST_NEXT(SLIST_FIRST((head)), field); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Singly-linked Tail queue declarations.
|
||||
*/
|
||||
#define STAILQ_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *stqh_first;/* first element */ \
|
||||
struct type **stqh_last;/* addr of last next element */ \
|
||||
}
|
||||
|
||||
#define STAILQ_HEAD_INITIALIZER(head) \
|
||||
{ NULL, &(head).stqh_first }
|
||||
|
||||
#define STAILQ_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *stqe_next; /* next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* Singly-linked Tail queue functions.
|
||||
*/
|
||||
#define STAILQ_CONCAT(head1, head2) do { \
|
||||
if (!STAILQ_EMPTY((head2))) { \
|
||||
*(head1)->stqh_last = (head2)->stqh_first; \
|
||||
(head1)->stqh_last = (head2)->stqh_last; \
|
||||
STAILQ_INIT((head2)); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define STAILQ_EMPTY(head) ((head)->stqh_first == NULL)
|
||||
|
||||
#define STAILQ_FIRST(head) ((head)->stqh_first)
|
||||
|
||||
#define STAILQ_FOREACH(var, head, field) \
|
||||
for((var) = STAILQ_FIRST((head)); \
|
||||
(var); \
|
||||
(var) = STAILQ_NEXT((var), field))
|
||||
|
||||
|
||||
#define STAILQ_FOREACH_SAFE(var, head, field, tvar) \
|
||||
for ((var) = STAILQ_FIRST((head)); \
|
||||
(var) && ((tvar) = STAILQ_NEXT((var), field), 1); \
|
||||
(var) = (tvar))
|
||||
|
||||
#define STAILQ_INIT(head) do { \
|
||||
STAILQ_FIRST((head)) = NULL; \
|
||||
(head)->stqh_last = &STAILQ_FIRST((head)); \
|
||||
} while (0)
|
||||
|
||||
#define STAILQ_INSERT_AFTER(head, tqelm, elm, field) do { \
|
||||
if ((STAILQ_NEXT((elm), field) = STAILQ_NEXT((tqelm), field)) == NULL)\
|
||||
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
|
||||
STAILQ_NEXT((tqelm), field) = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define STAILQ_INSERT_HEAD(head, elm, field) do { \
|
||||
if ((STAILQ_NEXT((elm), field) = STAILQ_FIRST((head))) == NULL) \
|
||||
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
|
||||
STAILQ_FIRST((head)) = (elm); \
|
||||
} while (0)
|
||||
|
||||
#define STAILQ_INSERT_TAIL(head, elm, field) do { \
|
||||
STAILQ_NEXT((elm), field) = NULL; \
|
||||
*(head)->stqh_last = (elm); \
|
||||
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
|
||||
} while (0)
|
||||
|
||||
#define STAILQ_LAST(head, type, field) \
|
||||
(STAILQ_EMPTY((head)) ? \
|
||||
NULL : \
|
||||
((struct type *)(void *) \
|
||||
((char *)((head)->stqh_last) - __offsetof(struct type, field))))
|
||||
|
||||
#define STAILQ_NEXT(elm, field) ((elm)->field.stqe_next)
|
||||
|
||||
#define STAILQ_REMOVE(head, elm, type, field) do { \
|
||||
QMD_SAVELINK(oldnext, (elm)->field.stqe_next); \
|
||||
if (STAILQ_FIRST((head)) == (elm)) { \
|
||||
STAILQ_REMOVE_HEAD((head), field); \
|
||||
} \
|
||||
else { \
|
||||
struct type *curelm = STAILQ_FIRST((head)); \
|
||||
while (STAILQ_NEXT(curelm, field) != (elm)) \
|
||||
curelm = STAILQ_NEXT(curelm, field); \
|
||||
STAILQ_REMOVE_AFTER(head, curelm, field); \
|
||||
} \
|
||||
TRASHIT(*oldnext); \
|
||||
} while (0)
|
||||
|
||||
#define STAILQ_REMOVE_HEAD(head, field) do { \
|
||||
if ((STAILQ_FIRST((head)) = \
|
||||
STAILQ_NEXT(STAILQ_FIRST((head)), field)) == NULL) \
|
||||
(head)->stqh_last = &STAILQ_FIRST((head)); \
|
||||
} while (0)
|
||||
|
||||
#define STAILQ_REMOVE_AFTER(head, elm, field) do { \
|
||||
if ((STAILQ_NEXT(elm, field) = \
|
||||
STAILQ_NEXT(STAILQ_NEXT(elm, field), field)) == NULL) \
|
||||
(head)->stqh_last = &STAILQ_NEXT((elm), field); \
|
||||
} while (0)
|
||||
|
||||
#define STAILQ_SWAP(head1, head2, type) do { \
|
||||
struct type *swap_first = STAILQ_FIRST(head1); \
|
||||
struct type **swap_last = (head1)->stqh_last; \
|
||||
STAILQ_FIRST(head1) = STAILQ_FIRST(head2); \
|
||||
(head1)->stqh_last = (head2)->stqh_last; \
|
||||
STAILQ_FIRST(head2) = swap_first; \
|
||||
(head2)->stqh_last = swap_last; \
|
||||
if (STAILQ_EMPTY(head1)) \
|
||||
(head1)->stqh_last = &STAILQ_FIRST(head1); \
|
||||
if (STAILQ_EMPTY(head2)) \
|
||||
(head2)->stqh_last = &STAILQ_FIRST(head2); \
|
||||
} while (0)
|
||||
|
||||
#define STAILQ_INSERT_CHAIN_HEAD(head, elm_chead, elm_ctail, field) do { \
|
||||
if ((STAILQ_NEXT(elm_ctail, field) = STAILQ_FIRST(head)) == NULL ) { \
|
||||
(head)->stqh_last = &STAILQ_NEXT(elm_ctail, field); \
|
||||
} \
|
||||
STAILQ_FIRST(head) = (elm_chead); \
|
||||
} while (0)
|
||||
|
||||
|
||||
/*
|
||||
* List declarations.
|
||||
*/
|
||||
#define LIST_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *lh_first; /* first element */ \
|
||||
}
|
||||
|
||||
#define LIST_HEAD_INITIALIZER(head) \
|
||||
{ NULL }
|
||||
|
||||
#define LIST_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *le_next; /* next element */ \
|
||||
struct type **le_prev; /* address of previous next element */ \
|
||||
}
|
||||
|
||||
/*
|
||||
* List functions.
|
||||
*/
|
||||
|
||||
#if (defined(_KERNEL) && defined(INVARIANTS))
|
||||
#define QMD_LIST_CHECK_HEAD(head, field) do { \
|
||||
if (LIST_FIRST((head)) != NULL && \
|
||||
LIST_FIRST((head))->field.le_prev != \
|
||||
&LIST_FIRST((head))) \
|
||||
panic("Bad list head %p first->prev != head", (head)); \
|
||||
} while (0)
|
||||
|
||||
#define QMD_LIST_CHECK_NEXT(elm, field) do { \
|
||||
if (LIST_NEXT((elm), field) != NULL && \
|
||||
LIST_NEXT((elm), field)->field.le_prev != \
|
||||
&((elm)->field.le_next)) \
|
||||
panic("Bad link elm %p next->prev != elm", (elm)); \
|
||||
} while (0)
|
||||
|
||||
#define QMD_LIST_CHECK_PREV(elm, field) do { \
|
||||
if (*(elm)->field.le_prev != (elm)) \
|
||||
panic("Bad link elm %p prev->next != elm", (elm)); \
|
||||
} while (0)
|
||||
#else
|
||||
#define QMD_LIST_CHECK_HEAD(head, field)
|
||||
#define QMD_LIST_CHECK_NEXT(elm, field)
|
||||
#define QMD_LIST_CHECK_PREV(elm, field)
|
||||
#endif /* (_KERNEL && INVARIANTS) */
|
||||
|
||||
#define LIST_EMPTY(head) ((head)->lh_first == NULL)
|
||||
|
||||
#define LIST_FIRST(head) ((head)->lh_first)
|
||||
|
||||
#define LIST_FOREACH(var, head, field) \
|
||||
for ((var) = LIST_FIRST((head)); \
|
||||
(var); \
|
||||
(var) = LIST_NEXT((var), field))
|
||||
|
||||
#define LIST_FOREACH_SAFE(var, head, field, tvar) \
|
||||
for ((var) = LIST_FIRST((head)); \
|
||||
(var) && ((tvar) = LIST_NEXT((var), field), 1); \
|
||||
(var) = (tvar))
|
||||
|
||||
#define LIST_INIT(head) do { \
|
||||
LIST_FIRST((head)) = NULL; \
|
||||
} while (0)
|
||||
|
||||
#define LIST_INSERT_AFTER(listelm, elm, field) do { \
|
||||
QMD_LIST_CHECK_NEXT(listelm, field); \
|
||||
if ((LIST_NEXT((elm), field) = LIST_NEXT((listelm), field)) != NULL)\
|
||||
LIST_NEXT((listelm), field)->field.le_prev = \
|
||||
&LIST_NEXT((elm), field); \
|
||||
LIST_NEXT((listelm), field) = (elm); \
|
||||
(elm)->field.le_prev = &LIST_NEXT((listelm), field); \
|
||||
} while (0)
|
||||
|
||||
#define LIST_INSERT_BEFORE(listelm, elm, field) do { \
|
||||
QMD_LIST_CHECK_PREV(listelm, field); \
|
||||
(elm)->field.le_prev = (listelm)->field.le_prev; \
|
||||
LIST_NEXT((elm), field) = (listelm); \
|
||||
*(listelm)->field.le_prev = (elm); \
|
||||
(listelm)->field.le_prev = &LIST_NEXT((elm), field); \
|
||||
} while (0)
|
||||
|
||||
#define LIST_INSERT_HEAD(head, elm, field) do { \
|
||||
QMD_LIST_CHECK_HEAD((head), field); \
|
||||
if ((LIST_NEXT((elm), field) = LIST_FIRST((head))) != NULL) \
|
||||
LIST_FIRST((head))->field.le_prev = &LIST_NEXT((elm), field);\
|
||||
LIST_FIRST((head)) = (elm); \
|
||||
(elm)->field.le_prev = &LIST_FIRST((head)); \
|
||||
} while (0)
|
||||
|
||||
#define LIST_NEXT(elm, field) ((elm)->field.le_next)
|
||||
|
||||
#define LIST_REMOVE(elm, field) do { \
|
||||
QMD_SAVELINK(oldnext, (elm)->field.le_next); \
|
||||
QMD_SAVELINK(oldprev, (elm)->field.le_prev); \
|
||||
QMD_LIST_CHECK_NEXT(elm, field); \
|
||||
QMD_LIST_CHECK_PREV(elm, field); \
|
||||
if (LIST_NEXT((elm), field) != NULL) \
|
||||
LIST_NEXT((elm), field)->field.le_prev = \
|
||||
(elm)->field.le_prev; \
|
||||
*(elm)->field.le_prev = LIST_NEXT((elm), field); \
|
||||
TRASHIT(*oldnext); \
|
||||
TRASHIT(*oldprev); \
|
||||
} while (0)
|
||||
|
||||
#define LIST_SWAP(head1, head2, type, field) do { \
|
||||
struct type *swap_tmp = LIST_FIRST((head1)); \
|
||||
LIST_FIRST((head1)) = LIST_FIRST((head2)); \
|
||||
LIST_FIRST((head2)) = swap_tmp; \
|
||||
if ((swap_tmp = LIST_FIRST((head1))) != NULL) \
|
||||
swap_tmp->field.le_prev = &LIST_FIRST((head1)); \
|
||||
if ((swap_tmp = LIST_FIRST((head2))) != NULL) \
|
||||
swap_tmp->field.le_prev = &LIST_FIRST((head2)); \
|
||||
} while (0)
|
||||
|
||||
/*
|
||||
* Tail queue declarations.
|
||||
*/
|
||||
#define TAILQ_HEAD(name, type) \
|
||||
struct name { \
|
||||
struct type *tqh_first; /* first element */ \
|
||||
struct type **tqh_last; /* addr of last next element */ \
|
||||
TRACEBUF \
|
||||
}
|
||||
|
||||
#define TAILQ_HEAD_INITIALIZER(head) \
|
||||
{ NULL, &(head).tqh_first }
|
||||
|
||||
#define TAILQ_ENTRY(type) \
|
||||
struct { \
|
||||
struct type *tqe_next; /* next element */ \
|
||||
struct type **tqe_prev; /* address of previous next element */ \
|
||||
TRACEBUF \
|
||||
}
|
||||
|
||||
/*
|
||||
* Tail queue functions.
|
||||
*/
|
||||
#if (defined(_KERNEL) && defined(INVARIANTS))
|
||||
#define QMD_TAILQ_CHECK_HEAD(head, field) do { \
|
||||
if (!TAILQ_EMPTY(head) && \
|
||||
TAILQ_FIRST((head))->field.tqe_prev != \
|
||||
&TAILQ_FIRST((head))) \
|
||||
panic("Bad tailq head %p first->prev != head", (head)); \
|
||||
} while (0)
|
||||
|
||||
#define QMD_TAILQ_CHECK_TAIL(head, field) do { \
|
||||
if (*(head)->tqh_last != NULL) \
|
||||
panic("Bad tailq NEXT(%p->tqh_last) != NULL", (head)); \
|
||||
} while (0)
|
||||
|
||||
#define QMD_TAILQ_CHECK_NEXT(elm, field) do { \
|
||||
if (TAILQ_NEXT((elm), field) != NULL && \
|
||||
TAILQ_NEXT((elm), field)->field.tqe_prev != \
|
||||
&((elm)->field.tqe_next)) \
|
||||
panic("Bad link elm %p next->prev != elm", (elm)); \
|
||||
} while (0)
|
||||
|
||||
#define QMD_TAILQ_CHECK_PREV(elm, field) do { \
|
||||
if (*(elm)->field.tqe_prev != (elm)) \
|
||||
panic("Bad link elm %p prev->next != elm", (elm)); \
|
||||
} while (0)
|
||||
#else
|
||||
#define QMD_TAILQ_CHECK_HEAD(head, field)
|
||||
#define QMD_TAILQ_CHECK_TAIL(head, headname)
|
||||
#define QMD_TAILQ_CHECK_NEXT(elm, field)
|
||||
#define QMD_TAILQ_CHECK_PREV(elm, field)
|
||||
#endif /* (_KERNEL && INVARIANTS) */
|
||||
|
||||
#define TAILQ_CONCAT(head1, head2, field) do { \
|
||||
if (!TAILQ_EMPTY(head2)) { \
|
||||
*(head1)->tqh_last = (head2)->tqh_first; \
|
||||
(head2)->tqh_first->field.tqe_prev = (head1)->tqh_last; \
|
||||
(head1)->tqh_last = (head2)->tqh_last; \
|
||||
TAILQ_INIT((head2)); \
|
||||
QMD_TRACE_HEAD(head1); \
|
||||
QMD_TRACE_HEAD(head2); \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_EMPTY(head) ((head)->tqh_first == NULL)
|
||||
|
||||
#define TAILQ_FIRST(head) ((head)->tqh_first)
|
||||
|
||||
#define TAILQ_FOREACH(var, head, field) \
|
||||
for ((var) = TAILQ_FIRST((head)); \
|
||||
(var); \
|
||||
(var) = TAILQ_NEXT((var), field))
|
||||
|
||||
#define TAILQ_FOREACH_SAFE(var, head, field, tvar) \
|
||||
for ((var) = TAILQ_FIRST((head)); \
|
||||
(var) && ((tvar) = TAILQ_NEXT((var), field), 1); \
|
||||
(var) = (tvar))
|
||||
|
||||
#define TAILQ_FOREACH_REVERSE(var, head, headname, field) \
|
||||
for ((var) = TAILQ_LAST((head), headname); \
|
||||
(var); \
|
||||
(var) = TAILQ_PREV((var), headname, field))
|
||||
|
||||
#define TAILQ_FOREACH_REVERSE_SAFE(var, head, headname, field, tvar) \
|
||||
for ((var) = TAILQ_LAST((head), headname); \
|
||||
(var) && ((tvar) = TAILQ_PREV((var), headname, field), 1); \
|
||||
(var) = (tvar))
|
||||
|
||||
#define TAILQ_INIT(head) do { \
|
||||
TAILQ_FIRST((head)) = NULL; \
|
||||
(head)->tqh_last = &TAILQ_FIRST((head)); \
|
||||
QMD_TRACE_HEAD(head); \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_INSERT_AFTER(head, listelm, elm, field) do { \
|
||||
QMD_TAILQ_CHECK_NEXT(listelm, field); \
|
||||
if ((TAILQ_NEXT((elm), field) = TAILQ_NEXT((listelm), field)) != NULL)\
|
||||
TAILQ_NEXT((elm), field)->field.tqe_prev = \
|
||||
&TAILQ_NEXT((elm), field); \
|
||||
else { \
|
||||
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
|
||||
QMD_TRACE_HEAD(head); \
|
||||
} \
|
||||
TAILQ_NEXT((listelm), field) = (elm); \
|
||||
(elm)->field.tqe_prev = &TAILQ_NEXT((listelm), field); \
|
||||
QMD_TRACE_ELEM(&(elm)->field); \
|
||||
QMD_TRACE_ELEM(&listelm->field); \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_INSERT_BEFORE(listelm, elm, field) do { \
|
||||
QMD_TAILQ_CHECK_PREV(listelm, field); \
|
||||
(elm)->field.tqe_prev = (listelm)->field.tqe_prev; \
|
||||
TAILQ_NEXT((elm), field) = (listelm); \
|
||||
*(listelm)->field.tqe_prev = (elm); \
|
||||
(listelm)->field.tqe_prev = &TAILQ_NEXT((elm), field); \
|
||||
QMD_TRACE_ELEM(&(elm)->field); \
|
||||
QMD_TRACE_ELEM(&listelm->field); \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_INSERT_HEAD(head, elm, field) do { \
|
||||
QMD_TAILQ_CHECK_HEAD(head, field); \
|
||||
if ((TAILQ_NEXT((elm), field) = TAILQ_FIRST((head))) != NULL) \
|
||||
TAILQ_FIRST((head))->field.tqe_prev = \
|
||||
&TAILQ_NEXT((elm), field); \
|
||||
else \
|
||||
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
|
||||
TAILQ_FIRST((head)) = (elm); \
|
||||
(elm)->field.tqe_prev = &TAILQ_FIRST((head)); \
|
||||
QMD_TRACE_HEAD(head); \
|
||||
QMD_TRACE_ELEM(&(elm)->field); \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_INSERT_TAIL(head, elm, field) do { \
|
||||
QMD_TAILQ_CHECK_TAIL(head, field); \
|
||||
TAILQ_NEXT((elm), field) = NULL; \
|
||||
(elm)->field.tqe_prev = (head)->tqh_last; \
|
||||
*(head)->tqh_last = (elm); \
|
||||
(head)->tqh_last = &TAILQ_NEXT((elm), field); \
|
||||
QMD_TRACE_HEAD(head); \
|
||||
QMD_TRACE_ELEM(&(elm)->field); \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_LAST(head, headname) \
|
||||
(*(((struct headname *)((head)->tqh_last))->tqh_last))
|
||||
|
||||
#define TAILQ_NEXT(elm, field) ((elm)->field.tqe_next)
|
||||
|
||||
#define TAILQ_PREV(elm, headname, field) \
|
||||
(*(((struct headname *)((elm)->field.tqe_prev))->tqh_last))
|
||||
|
||||
#define TAILQ_REMOVE(head, elm, field) do { \
|
||||
QMD_SAVELINK(oldnext, (elm)->field.tqe_next); \
|
||||
QMD_SAVELINK(oldprev, (elm)->field.tqe_prev); \
|
||||
QMD_TAILQ_CHECK_NEXT(elm, field); \
|
||||
QMD_TAILQ_CHECK_PREV(elm, field); \
|
||||
if ((TAILQ_NEXT((elm), field)) != NULL) \
|
||||
TAILQ_NEXT((elm), field)->field.tqe_prev = \
|
||||
(elm)->field.tqe_prev; \
|
||||
else { \
|
||||
(head)->tqh_last = (elm)->field.tqe_prev; \
|
||||
QMD_TRACE_HEAD(head); \
|
||||
} \
|
||||
*(elm)->field.tqe_prev = TAILQ_NEXT((elm), field); \
|
||||
TRASHIT(*oldnext); \
|
||||
TRASHIT(*oldprev); \
|
||||
QMD_TRACE_ELEM(&(elm)->field); \
|
||||
} while (0)
|
||||
|
||||
#define TAILQ_SWAP(head1, head2, type, field) do { \
|
||||
struct type *swap_first = (head1)->tqh_first; \
|
||||
struct type **swap_last = (head1)->tqh_last; \
|
||||
(head1)->tqh_first = (head2)->tqh_first; \
|
||||
(head1)->tqh_last = (head2)->tqh_last; \
|
||||
(head2)->tqh_first = swap_first; \
|
||||
(head2)->tqh_last = swap_last; \
|
||||
if ((swap_first = (head1)->tqh_first) != NULL) \
|
||||
swap_first->field.tqe_prev = &(head1)->tqh_first; \
|
||||
else \
|
||||
(head1)->tqh_last = &(head1)->tqh_first; \
|
||||
if ((swap_first = (head2)->tqh_first) != NULL) \
|
||||
swap_first->field.tqe_prev = &(head2)->tqh_first; \
|
||||
else \
|
||||
(head2)->tqh_last = &(head2)->tqh_first; \
|
||||
} while (0)
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* !_SYS_QUEUE_H_ */
|
||||
Reference in New Issue
Block a user