libwebsockets
Lightweight C library for HTML5 websockets
Loading...
Searching...
No Matches
lws-hl.h File Reference

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)

Data Structure Documentation

◆ lws_hl_ctx

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.

Definition at line 121 of file lws-hl.h.

Collaboration diagram for lws_hl_ctx:
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]

◆ lws_hl_html

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.

Definition at line 244 of file lws-hl.h.

Collaboration diagram for lws_hl_html:
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]

Macro Definition Documentation

◆ LHL_SCRATCH_SIZE

#define LHL_SCRATCH_SIZE   40

Definition at line 42 of file lws-hl.h.

◆ LHL_PIECE_MAX

#define LHL_PIECE_MAX   96

Definition at line 45 of file lws-hl.h.

◆ LHL_HTML_BUF

#define LHL_HTML_BUF   832

Definition at line 228 of file lws-hl.h.

Typedef Documentation

◆ lws_hl_ctx_t

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.

Definition at line 71 of file lws-hl.h.

◆ lws_hl_token_cb

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

Parameters
useropaque pointer set at lws_hl_construct()
clsthe classification of the token piece
tokthe token piece bytes
lenthe 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.

Definition at line 93 of file lws-hl.h.

◆ lws_hl_ops_t

typedef struct lws_hl_ops lws_hl_ops_t

◆ lws_hl_write_cb

typedef lws_stateful_ret_t(* lws_hl_write_cb) (void *user, const uint8_t *buf, size_t len)

Definition at line 231 of file lws-hl.h.

◆ lws_hl_html_t

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.

Enumeration Type Documentation

◆ lws_hl_class_t

how the token piece is to be presented

Enumerator
LHL_CLS_PLAIN 

ws, operators, punctuation... unstyled

LHL_CLS_IDENT 

non-keyword identifier

LHL_CLS_KEYWORD 

language keyword

LHL_CLS_TYPE 

builtin / well-known type name

LHL_CLS_NUMBER 

numeric constant

LHL_CLS_STRING 

string literal

LHL_CLS_CHARLIT 

character literal

LHL_CLS_COMMENT 

comment

LHL_CLS_PREPROC 

preprocessor directive / header name

LHL_CLS_DIFF_ADD 

diff: added line

LHL_CLS_DIFF_REM 

diff: removed line

LHL_CLS_DIFF_HUNK 

diff: hunk header line

LHL_CLS_DIFF_META 

diff: file header / metadata line

LHL_CLS_COUNT 

count of valid classes

Definition at line 49 of file lws-hl.h.

49 {
59
60 /* diff presentation (lines, including their newline) */
61
66
lws_hl_class_t
Definition lws-hl.h:49
@ LHL_CLS_PREPROC
Definition lws-hl.h:58
@ LHL_CLS_IDENT
Definition lws-hl.h:51
@ LHL_CLS_STRING
Definition lws-hl.h:55
@ LHL_CLS_PLAIN
Definition lws-hl.h:50
@ LHL_CLS_COMMENT
Definition lws-hl.h:57
@ LHL_CLS_DIFF_REM
Definition lws-hl.h:63
@ LHL_CLS_CHARLIT
Definition lws-hl.h:56
@ LHL_CLS_COUNT
Definition lws-hl.h:67
@ LHL_CLS_KEYWORD
Definition lws-hl.h:52
@ LHL_CLS_NUMBER
Definition lws-hl.h:54
@ LHL_CLS_TYPE
Definition lws-hl.h:53
@ LHL_CLS_DIFF_ADD
Definition lws-hl.h:62
@ LHL_CLS_DIFF_META
Definition lws-hl.h:65
@ LHL_CLS_DIFF_HUNK
Definition lws-hl.h:64

Function Documentation

◆ lws_hl_construct()

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

Parameters
ctxthe highlighter context to prepare
langthe language ops to use, eg, &lws_hl_lang_c
cbthe token sink callback
useropaque 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_hl_parse()

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

Parameters
ctxthe highlighter context
bufpointer to pointer to the start of the chunk of source
lenpointer 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_hl_finish()

lws_hl_finish() - signal end of input and close out pending state

Parameters
ctxthe 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_hl_html_construct()

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

Parameters
hthe html emit context to prepare
wcthe output write callback
useropaque pointer passed to wc
clsNULL 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_hl_html_token()

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

Parameters
userpointer to the lws_hl_html_t
clsthe classification of the token piece
tokthe token piece bytes
lenthe 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_hl_html_close()

lws_hl_html_close() - close any dangling element after end of input

Parameters
hthe 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_html_escape()

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

Parameters
wcthe output write callback
useropaque pointer passed to wc
srcthe bytes to escape
lenthe 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.