forked from electro/esp-irblaster
initial version made by removing all csp stuff from cspemu
This commit is contained in:
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