--- src/tests/test-winmsgtok.c | 110 --------------------------- src/winmsgtok.c | 119 ----------------------------- src/winmsgtok.h | 182 --------------------------------------------- 3 files changed, 411 deletions(-) delete mode 100644 src/tests/test-winmsgtok.c delete mode 100644 src/winmsgtok.c delete mode 100644 src/winmsgtok.h
diff --git a/src/tests/test-winmsgtok.c b/src/tests/test-winmsgtok.c deleted file mode 100644 index 13fd790..0000000 --- a/src/tests/test-winmsgtok.c +++ /dev/null @@ -1,110 +0,0 @@ -/* Copyright (c) 2013 - * Mike Gerwitz (m...@gnu.org) - * - * This file is part of GNU screen. - * - * GNU screen is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program (see the file COPYING); if not, see - * <http://www.gnu.org/licenses>. - * - **************************************************************** - */ - -#include "../winmsgtok.h" -#include "signature.h" -#include "macros.h" - -SIGNATURE_CHECK(wmtok_init, WinMsgTokState *, (WinMsgTokState *)); -SIGNATURE_CHECK(wmtok_tokenize, size_t, - (WinMsgTok **, char *, size_t, WinMsgTokState *)); -SIGNATURE_CHECK(wmtok_free, void, (WinMsgTokState *)); - -SIGNATURE_CHECK(wmtok_type, WinMsgTokType, (WinMsgTok *)); -SIGNATURE_CHECK(wmtok_lexeme, char *, (char *, WinMsgTok *, size_t)); -SIGNATURE_CHECK(wmtok_data, union WinMsgTokData, (WinMsgTok *)); - -char lexbuf[256]; - -#define ASSERT_TOKSTR(s, ...) { \ - WinMsgTokType etok[] = {__VA_ARGS__, WMTOK_END}; \ - _assert_tokstr(s, etok); \ - } - -#define ASSERT_TOK_NODATA(tok, type, lexeme) \ - ASSERT(wmtok_type(tok) == type); \ - ASSERT(STREQ(wmtok_lexeme(lexbuf, tok, sizeof(lexbuf)), lexeme)); - -#define ASSERT_TOK_P(tok, type, lexeme, data) \ - ASSERT_TOK_NODATA(tok, type, lexeme); \ - ASSERT(wmtok_data(tok).ptr == data); - -#define ASSERT_TOK(tok, type, lexeme, data) \ - ASSERT_TOK_NODATA(tok, type, lexeme); \ - ASSERT(wmtok_data(tok).ptr == data); - -/* expected values when asserting against tokens */ -struct TokAssert { - WinMsgTokType type; - char *lexeme; -}; - - -void _assert_tokstr(char *src, WinMsgTokType *expected) -{ - WinMsgTokState *st = wmtok_init(NULL); - WinMsgTok dest[1024]; - WinMsgTok *tok = dest; - - size_t n = wmtok_tokenize(&tok, src, sizeof(dest), st); - size_t i = 0; - - do { - ASSERT(wmtok_type(tok++) == *expected); - } while ((i++ < n) && (*expected++ != WMTOK_END)); - - ASSERT(n == i); - - wmtok_free(st); -} - -int main(void) -{ - /* allocate state if null pointer given */ - { - WinMsgTokState *st; - - ASSERT_MALLOC(>0, st = wmtok_init(NULL)); - ASSERT(st != NULL); - - wmtok_free(st); - } - - /* previously allocated state given */ - { - WinMsgTokState *st = malloc(sizeof(WinMsgTokState)); - ASSERT(wmtok_init(st) == st); - wmtok_free(st); - } - - /* tokenizing the empty string should yield a single END token */ - { - WinMsgTokState *st = wmtok_init(NULL); - WinMsgTok dest[16]; /* extra in case something goes terribly wrong */ - WinMsgTok *tok = dest; - - ASSERT(wmtok_tokenize(&tok, "", sizeof(dest), st) == 1); - ASSERT_TOK(tok, WMTOK_END, "", NULL); - - wmtok_free(st); - } -} diff --git a/src/winmsgtok.c b/src/winmsgtok.c deleted file mode 100644 index e9b7695..0000000 --- a/src/winmsgtok.c +++ /dev/null @@ -1,119 +0,0 @@ -/* Copyright (c) 2013 - * Mike Gerwitz (m...@gnu.org) - * - * This file is part of GNU screen. - * - * GNU screen is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program (see the file COPYING); if not, see - * <http://www.gnu.org/licenses>. - * - **************************************************************** - */ - -#include <stdlib.h> -#include <string.h> -#include "winmsgtok.h" - - -static WinMsgTok *_wmtok_tok() -{ - /* TODO: this is temporary code for POC and is obviously bad ;) */ - static WinMsgTok toks[512] = {{0}}; - static int i = 0; - WinMsgTok *p = &toks[i++]; - - return p; -} - - -/* Initialize tokenizer state. If ST is the null pointer, then a state will - * be allocated. In either case, ST must be freed using wmtok_free to ensure - * that any resources allocated by this function (both now and in the - * future) are properly freed. If ST was allocated outside of this function, - * it must also be freed by the caller following a call to wmtok_free. */ -WinMsgTokState *wmtok_init(WinMsgTokState *st) -{ - if (st == NULL) { - st = malloc(sizeof(WinMsgTokState)); - st->_dofree = true; - } - - return st; -} - -/* Tokenize a source window message string, producing a token string into - * DEST. The number of tokens will not exceed LEN. A return value of - * (size_t)-1 indicates that additional tokens may be available, in which - * case this function may simply be called again with the same ST to resume - * lexing. */ -size_t wmtok_tokenize(WinMsgTok **dest, char *src, - size_t len, WinMsgTokState *st) -{ - WinMsgTok **p = dest; - - /* do not exceed LEN tokens; otherwise, we risk overflowing the - * destination buffer */ - for (size_t n = 0; n < len; n++) { - /* upon reaching the end of the src string, lexing is complete */ - if (*src++ == '\0') { - /* TODO: temporary code for POC */ - *p = _wmtok_tok(); - (*p)->lexeme.ptr = src - 1; - (*p)->lexeme.len = 1; - p++; - break; - } - } - - /* actual length may differ from N */ - return p - dest; -} - -/* Free tozenkizer state and any resources allocated to it; should always be - * called after wmtok_init, even if ST was allocated independently. */ -void wmtok_free(WinMsgTokState *st) -{ - if (st->_dofree) { - free(st); - } -} - -/* Retrieves the type of the given token as a WMTOK_* constant. */ -WinMsgTokType wmtok_type(WinMsgTok *tok) -{ - return tok->type; -} - -/* Retrieve a copy of the source string (lexeme) from which the token was - * generated. The last byte written will be the null byte and will overwrite - * the last character in the lexeme if LEN is too small. */ -char *wmtok_lexeme(char *dest, WinMsgTok *tok, size_t len) -{ - size_t n = (tok->lexeme.len > len) ? len - 1 : tok->lexeme.len; - - /* the lexeme points to the original string from which we derived the - * token; make a copy to avoid hard-to-find bugs */ - strncpy(dest, tok->lexeme.ptr, n); - dest[n] = '\0'; - - return dest; -} - -/* Retrieve additional data needed to propely parse the token, if - * applicable. The data may be of any type, so a transparent union is - * returned to permit multiple types; the caller is expected to know which - * type the token uses. */ -union WinMsgTokData wmtok_data(WinMsgTok *tok) -{ - return tok->data; -} diff --git a/src/winmsgtok.h b/src/winmsgtok.h deleted file mode 100644 index 438e139..0000000 --- a/src/winmsgtok.h +++ /dev/null @@ -1,182 +0,0 @@ -/* Copyright (c) 2013 - * Mike Gerwitz (m...@gnu.org) - * - * This file is part of GNU screen. - * - * GNU screen is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3, or (at your option) - * any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program (see the file COPYING); if not, see - * <http://www.gnu.org/licenses>. - * - **************************************************************** - */ - -/* EBNF Grammar - * ------------ - * The language understood by the window message parser is represented here - * in Extended Backus-Naur Form; the lexer is structured accordingly. - * - * winmsg = expr ; - * expr = { term } , end ; - * - * term = literal - * | escseq ; - * - * (* a sequence of characters to be echoed *) - * literal = { ctrlesc | character - escchar } ; - * - * (* a control escape sequence, e.g. ^[ *) - * ctrlesc = '^' , ascii printable character ; - * - * - * (* escape sequence; e.g. %n *) - * escseq = escchar , ( delimited escseq | standalone escseq ) ; - * standalone escseq = [ escflag ] , [ number ] , [ esclong ] , - * ascii printable character , [ escfmt ] ; - * escflag = '+' | '-' | '.' | '0' ; - * esclong = 'L' ; - * - * (* used for certain escapes to override default format *) - * escfmt = '{' , escfmt expr ; - * escfmt expr = { escfmt term } ; - * escfmt term = '}' - * | term ; - * - * (* the escape character can be dynamically set; default is '%' *) - * escchar = ? current escape character ? ; - * - * (* delimited escape sequences are of arbitrary length *) - * delimited escseq = rendition - * | conditional - * | group ; - * - * - * (* renditions set character output settings such as color or weight *) - * rendition = '{' , rendexpr , '}' ; - * rendexpr = rendpop - * | [ rend attrmod ] , rend colordesc ; - * rendpop = '-' ; - * - * (* attribute modifier *) - * rend attrmod = rend attrchtype , ' ' , rend attrvalue ; - * rend attrchtype = '+' | '-' | '!' | '=' ; - * rend attrvalue = 'd' | 'u' | 'b' | 'r' | 's' | 'B' ; - * - * (* color descriptor; fg and optional bg *) - * rend colordesc = rend colorval , [ ';' ] , [ rend colorval ] ; - * rend colorval = rend colorcode - * | number - * | 'x' , hexnumber ; - * rend colorcode = 'k' | 'r' | 'g' | 'y' | 'b' | 'm' | 'c' | 'w' | 'd' - * | 'K' | 'R' | 'G' | 'Y' | 'B' | 'M' | 'C' | 'W' | 'D' - * | 'i' | 'I' | '.' ; - * - * - * (* conditional output *) - * conditional = condchar , condterm ; - * condexpr = { condterm } ; - * condelseexpr = { condelseterm } ; - * condterm = condelse - * | condelseterm ; - * condelseterm = condend - * | term ; - * condelse = escchar , ':' , condelseexpr ; - * condend = escchar , condchar ; - * condchar = '?' ; - * - * - * (* acts as a sub-expression with own size restrictions, alignment, etc *) - * group = '[' , groupexpr , groupend ; - * groupexpr = { groupterm } ; - * groupterm = groupend - * | ? TODO: planned feature ? - * | term ; - * groupend = escchar , ']' ; - * - * - * (* TODO: UTF-8 support is planned *) - * character = { ascii character } ; - * ascii character = ascii control character - * | ascii printable character ; - * - * ascii control character = ? 0x00 <= byte value <= 0x1f ? - * ascii printable character = ? byte value >= 0x20 ? - * - * digit = '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9' ; - * number = { digit } ; - * - * hexdigit = 'a' | 'b' | 'c' | 'd' | 'e' | 'f' - * | 'A' | 'B' | 'C' | 'D' | 'E' | 'F' - * | digit ; - * hexnumber = { hexdigit } ; - * - * end = '\0' ; - */ - -#include <stdbool.h> -#include <stdint.h> -#include <unistd.h> - - -/* Token types */ -typedef enum { - WMTOK_END, /* terminating token; end of string */ - WMTOK_ECHO, /* lexeme should be echoed */ -} WinMsgTokType; - -/* Transparent type exposing token values */ -union WinMsgTokData { - /* integers of various sorts */ - uint_fast8_t uint8; - uint_fast16_t uint16; - uint_fast32_t uint32; - uintmax_t uintmax; - intmax_t intmax; - size_t size; - - void *ptr; /* pointer to separately allocated data */ -}; - -/* Window message token */ -typedef struct { - WinMsgTokType type; /* token type */ - - /* token src string; to reduce overhead, it will point into the original - * src string, and therefore includes a length (since it may not be - * null-terminated) */ - struct { - char *ptr; /* pointer to lexeme in src string */ - size_t len; /* length of lexeme */ - } lexeme; - - /* general-purpose, pre-allocated area for a basic value; use the void - * ptr if additional allocation is necessary */ - union WinMsgTokData data; -} WinMsgTok; - -/* Represents state and configuration options for tokenizer */ -typedef struct { - char esc; /* escape character */ - - bool _dofree; /* whether we allocated ourself */ -} WinMsgTokState; - - -WinMsgTokState *wmtok_init(WinMsgTokState *); -size_t wmtok_tokenize(WinMsgTok **, char *, size_t, WinMsgTokState *); -void wmtok_free(WinMsgTokState *); - -/** These functions expose data from the opaque WinMsgTok type in a way that - * allows altering the structure in the future with no ill effects. **/ -WinMsgTokType wmtok_type(WinMsgTok *); -char *wmtok_lexeme(char *, WinMsgTok *, size_t); -union WinMsgTokData wmtok_data(WinMsgTok *); -- 2.7.0 -- + privacy is necessary + using gnupg http://gnupg.org + public key id: 0x92FEFDB7E44C32F9
signature.asc
Description: PGP signature