|
libwebsockets
Lightweight C library for HTML5 websockets
|
Go to the source code of this file.
Data Structures | |
| struct | lws_hl_ops |
| struct | lws_hl_ctx |
| struct | lws_hl_html |
Macros | |
| #define | LHL_SCRATCH_SIZE 40 |
| #define | LHL_PIECE_MAX 96 |
| #define | LHL_HTML_BUF 832 |
Typedefs | |
| typedef struct lws_hl_ctx | lws_hl_ctx_t |
| typedef lws_stateful_ret_t(* | lws_hl_token_cb) (void *user, lws_hl_class_t cls, const uint8_t *tok, size_t len) |
| typedef struct lws_hl_ops | lws_hl_ops_t |
| typedef lws_stateful_ret_t(* | lws_hl_write_cb) (void *user, const uint8_t *buf, size_t len) |
| typedef struct lws_hl_html | lws_hl_html_t |
Enumerations | |
| enum | lws_hl_class_t { LHL_CLS_PLAIN , LHL_CLS_IDENT , LHL_CLS_KEYWORD , LHL_CLS_TYPE , LHL_CLS_NUMBER , LHL_CLS_STRING , LHL_CLS_CHARLIT , LHL_CLS_COMMENT , LHL_CLS_PREPROC , LHL_CLS_DIFF_ADD , LHL_CLS_DIFF_REM , LHL_CLS_DIFF_HUNK , LHL_CLS_DIFF_META , LHL_CLS_COUNT } |
Functions | |
| LWS_VISIBLE LWS_EXTERN int | lws_hl_construct (lws_hl_ctx_t *ctx, const lws_hl_ops_t *lang, lws_hl_token_cb cb, void *user) |
| LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t | lws_hl_parse (lws_hl_ctx_t *ctx, const uint8_t **buf, size_t *len) |
| LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t | lws_hl_finish (lws_hl_ctx_t *ctx) |
| LWS_VISIBLE LWS_EXTERN int | lws_hl_html_construct (lws_hl_html_t *h, lws_hl_write_cb wc, void *user, const char *const *cls) |
| LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t | lws_hl_html_token (void *user, lws_hl_class_t cls, const uint8_t *tok, size_t len) |
| LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t | lws_hl_html_close (lws_hl_html_t *h) |
| LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t | lws_html_escape (lws_hl_write_cb wc, void *user, const uint8_t *src, size_t len) |
| struct lws_hl_ctx |
lws_hl_ctx_t: highlighting tokenizer context
The context is allocated by the caller (its size is known from this header) and requires no heap. Fields below user are private to the driver in use and must not be touched.
| Data Fields | ||
|---|---|---|
| const lws_hl_ops_t * | ops | |
| lws_hl_token_cb | cb | |
| void * | user | |
| const uint8_t * | chunk | |
| size_t | pos | |
| size_t | tok | |
| size_t | epos | |
| uint8_t | state | |
| uint8_t | scratch_pos | |
| uint8_t | flags | |
| uint8_t | tokcls | |
| uint8_t | scratch[LHL_SCRATCH_SIZE] | |
| struct lws_hl_html |
lws_hl_html_t: stock token sink producing CSP-safe inline markup
The context is allocated by the caller (its size is known from this header). Pass lws_hl_html_token() as the lws_hl token callback with this struct as the callback user pointer; token text is HTML-escaped and, for classes with a non-NULL css class name, wrapped in ... elements spanning consecutive same-class pieces.
| Data Fields | ||
|---|---|---|
| lws_hl_write_cb | wc | |
| void * | user | |
| const char *const * | cls | |
| lws_hl_class_t | last | |
| size_t | buflen | |
| uint8_t | open | |
| char | buf[LHL_HTML_BUF] | |
| typedef struct lws_hl_ctx lws_hl_ctx_t |
lws_hl_ctx_t: highlighting tokenizer context
The context is allocated by the caller (its size is known from this header) and requires no heap. Fields below user are private to the driver in use and must not be touched.
| typedef lws_stateful_ret_t(* lws_hl_token_cb) (void *user, lws_hl_class_t cls, const uint8_t *tok, size_t len) |
lws_hl_token_cb() - token sink callback
| user | opaque pointer set at lws_hl_construct() |
| cls | the classification of the token piece |
| tok | the token piece bytes |
| len | the length of the token piece in bytes |
Token pieces are at most LHL_PIECE_MAX bytes; longer constructs are split into consecutive pieces of the same class (which the stock HTML sink merges into a single element).
tok points either into the input fragment currently being parsed, or into tokenizer private storage; it is only valid for the duration of the callback.
Return LWS_SRET_OK to continue, or a nonzero lws_stateful_ret_t (for example LWS_SRET_WANT_OUTPUT) to stop parsing at this piece. Parsing resumes at the same piece on the next lws_hl_parse() call.
| typedef struct lws_hl_ops lws_hl_ops_t |
| typedef lws_stateful_ret_t(* lws_hl_write_cb) (void *user, const uint8_t *buf, size_t len) |
| typedef struct lws_hl_html lws_hl_html_t |
lws_hl_html_t: stock token sink producing CSP-safe inline markup
The context is allocated by the caller (its size is known from this header). Pass lws_hl_html_token() as the lws_hl token callback with this struct as the callback user pointer; token text is HTML-escaped and, for classes with a non-NULL css class name, wrapped in ... elements spanning consecutive same-class pieces.
| enum lws_hl_class_t |
how the token piece is to be presented
Definition at line 49 of file lws-hl.h.
| LWS_VISIBLE LWS_EXTERN int lws_hl_construct | ( | lws_hl_ctx_t * | ctx, |
| const lws_hl_ops_t * | lang, | ||
| lws_hl_token_cb | cb, | ||
| void * | user ) |
lws_hl_construct() - prepare an lws_hl_ctx for use
| ctx | the highlighter context to prepare |
| lang | the language ops to use, eg, &lws_hl_lang_c |
| cb | the token sink callback |
| user | opaque pointer passed to the callbacks |
Prepares a caller-allocated context. Returns 0 for OK, or nonzero if lang or its required ops members are NULL, or the language construct hook failed.
References LWS_EXTERN, and LWS_VISIBLE.
| LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_hl_parse | ( | lws_hl_ctx_t * | ctx, |
| const uint8_t ** | buf, | ||
| size_t * | len ) |
lws_hl_parse() - parse a chunk of source into classified token pieces
| ctx | the highlighter context |
| buf | pointer to pointer to the start of the chunk of source |
| len | pointer to the number of bytes of source available at *buf |
Parses as much of *buf as it can, emitting token pieces of at most LHL_PIECE_MAX bytes to the token sink. On return, *buf and *len are adjusted to describe the input that has not yet been consumed-and-emitted; when the token sink defers a piece (nonzero return from the sink), that is where parsing will resume on the next call.
Fragments may be of any size including zero or one byte; fragments need not align to token boundaries. If a decision about the final byte of a fragment (for example a '/' that may start a comment) needs the next byte, it is held and reconsumed on the next call... a call may consume nothing but return LWS_SRET_OK for this reason; lws_hl_finish() resolves it.
Returns LWS_SRET_OK if all input was consumed (keep calling with more input, or call lws_hl_finish()); the nonzero return from the token sink if it deferred a piece (call again later to resume); or LWS_SRET_FATAL on bad arguments.
References LWS_EXTERN, and LWS_VISIBLE.
| LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_hl_finish | ( | lws_hl_ctx_t * | ctx | ) |
lws_hl_finish() - signal end of input and close out pending state
| ctx | the highlighter context |
Must be called once after the last input fragment. Constructs still open at end of input (an unterminated string, comment, identifier...) are emitted with their best-guess classification. If the token sink defers, the nonzero return is passed back and lws_hl_finish() must be called again.
After a successful return the context is reset and may be reused for new input.
Returns LWS_SRET_OK, the nonzero return from the token sink, or LWS_SRET_FATAL on bad arguments.
References LWS_EXTERN, and LWS_VISIBLE.
| LWS_VISIBLE LWS_EXTERN int lws_hl_html_construct | ( | lws_hl_html_t * | h, |
| lws_hl_write_cb | wc, | ||
| void * | user, | ||
| const char *const * | cls ) |
lws_hl_html_construct() - prepare an html emit context
| h | the html emit context to prepare |
| wc | the output write callback |
| user | opaque pointer passed to wc |
| cls | NULL to use stock class names, or an array of LHL_CLS_COUNT class name strings; NULL entries in the array are emitted unwrapped |
The class name table is not copied and must remain allocated until the context is no longer used.
Returns 0 for OK or nonzero on bad arguments.
References LWS_EXTERN, and LWS_VISIBLE.
| LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_hl_html_token | ( | void * | user, |
| lws_hl_class_t | cls, | ||
| const uint8_t * | tok, | ||
| size_t | len ) |
lws_hl_html_token() - token sink for lws_hl_parse() producing html markup
| user | pointer to the lws_hl_html_t |
| cls | the classification of the token piece |
| tok | the token piece bytes |
| len | the length of the token piece in bytes |
For each token piece, produces a single write callback call of the escaped markup. Because each piece is emitted atomically, a nonzero return from the write callback safely defers the piece; the markup for the piece is then retried, not duplicated.
Returns LWS_SRET_OK, or the nonzero return from the write callback.
References LWS_EXTERN, and LWS_VISIBLE.
| LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_hl_html_close | ( | lws_hl_html_t * | h | ) |
lws_hl_html_close() - close any dangling element after end of input
| h | the html emit context |
Call after lws_hl_finish() returned LWS_SRET_OK; closes a dangling if one is open. Returns LWS_SRET_OK, or the nonzero return from the write callback (in which case, call it again).
References LWS_EXTERN, and LWS_VISIBLE.
| LWS_VISIBLE LWS_EXTERN lws_stateful_ret_t lws_html_escape | ( | lws_hl_write_cb | wc, |
| void * | user, | ||
| const uint8_t * | src, | ||
| size_t | len ) |
lws_html_escape() - stream html-escaped text through a write callback
| wc | the output write callback |
| user | opaque pointer passed to wc |
| src | the bytes to escape |
| len | the number of bytes at src |
Escapes '<', '>' and '&' as html entities, and replaces NUL and other C0 control bytes other than TAB, LF and CR with U+FFFD. Bytes >= 0x80 are passed through unmodified, so the caller must serve the result with a utf-8 charset declaration.
The write callback should accept everything; a nonzero return aborts escaping and is passed back (earlier writes may already have happened).
Returns LWS_SRET_OK, or the nonzero return from the write callback.