From 475eeb81211e532da054e22c15f8f421c6a62f52 Mon Sep 17 00:00:00 2001 From: "psyc://psyced.org/~lynX" <@> Date: Sat, 16 Apr 2011 17:30:03 +0200 Subject: [PATCH] debug macros --- include/psyc.h | 10 +++++ include/psyc/debug.h | 95 +++++++++++++++++++++++++++++++++++++++++++ include/psyc/lib.h | 9 ++++ include/psyc/syntax.h | 39 ++++++++++++++++++ src/Makefile | 17 ++++---- src/match.c | 14 ++----- src/tests/testMatch.c | 12 ++++++ 7 files changed, 178 insertions(+), 18 deletions(-) create mode 100644 include/psyc.h create mode 100644 include/psyc/debug.h create mode 100644 include/psyc/lib.h create mode 100644 include/psyc/syntax.h create mode 100644 src/tests/testMatch.c diff --git a/include/psyc.h b/include/psyc.h new file mode 100644 index 0000000..040509d --- /dev/null +++ b/include/psyc.h @@ -0,0 +1,10 @@ +#include +#include + +#define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday) + +/** @brief Checks if short keyword string matches long keyword string + */ +int PSYC_matches(uint8_t* sho, unsigned int slen, + uint8_t* lon, unsigned int llen); + diff --git a/include/psyc/debug.h b/include/psyc/debug.h new file mode 100644 index 0000000..0fcdb05 --- /dev/null +++ b/include/psyc/debug.h @@ -0,0 +1,95 @@ +#ifdef DEBUG +# include +# define PP(args) printf args; +#else +# define PP(args) +#endif + +#ifdef TEST +# define PT(MSG) PP(MSG); +# define DT(CODE) CODE +#else +# define PT(MSG) +# define DT(CODE) +#endif + +/* simplified form of conditional compilation */ + +#ifndef DEBUG_FLAGS +# ifdef DEBUG +# if DEBUG == 1 +# define DEBUG_FLAGS 0x03 +# else +# if DEBUG == 2 +# define DEBUG_FLAGS 0x07 +# else +# if DEBUG == 3 +# define DEBUG_FLAGS 0x0f +# else +# if DEBUG == 4 +# define DEBUG_FLAGS 0x1f +# else +# define DEBUG_FLAGS 0x01 +# endif +# endif +# endif +# endif +# else +# define DEBUG_FLAGS 0x00 /* no debugging */ +# endif +#endif + +#if DEBUG_FLAGS & 0x01 +# define D0(CODE) CODE +# define P0(MSG) PP(MSG); +#else +# define D0(CODE) +# define P0(MSG) +#endif + +#if DEBUG_FLAGS & 0x02 +# define D1(CODE) CODE +# define P1(MSG) PP(MSG); +#else +# define D1(CODE) +# define P1(MSG) +#endif + +#if DEBUG_FLAGS & 0x04 +# define D2(CODE) CODE +# define P2(MSG) PP(MSG); +#else +# define D2(CODE) +# define P2(MSG) +#endif + +#if DEBUG_FLAGS & 0x08 +# define D3(CODE) CODE +# define P3(MSG) PP(MSG); +#else +# define D3(CODE) +# define P3(MSG) +#endif + +#if DEBUG_FLAGS & 0x10 +# define D4(CODE) CODE +# define P4(MSG) PP(MSG); +#else +# define D4(CODE) +# define P4(MSG) +#endif + +// ASSERT() unused as yet +#if DEBUG > 0 +# ifdef STRICT +# define ASSERT(NAME,COND,VALUE) { unless (COND) { \ + PP(("Assertion %s failed in %s: %s\n", NAME, ME, VALUE)); \ + raise_error("Assertion failed (strict mode).\n"); } } +# else +# define ASSERT(NAME,COND,VALUE) { unless (COND) \ + PP(("Assertion %s failed in %s: %s\n", NAME, ME, VALUE)); } +# endif +#else +# define ASSERT(NAME,CONDITION,VALUE) +#endif + diff --git a/include/psyc/lib.h b/include/psyc/lib.h new file mode 100644 index 0000000..699f601 --- /dev/null +++ b/include/psyc/lib.h @@ -0,0 +1,9 @@ +/* this is needed to compile the library, not to use it */ + +#include "../psyc.h" +#include "./debug.h" + +/* perlisms for readability */ +#define unless(COND) if (!(COND)) +#define until(COND) while (!(COND)) + diff --git a/include/psyc/syntax.h b/include/psyc/syntax.h new file mode 100644 index 0000000..c15889e --- /dev/null +++ b/include/psyc/syntax.h @@ -0,0 +1,39 @@ +#ifndef PSYC_SYNTAX_H +#define PSYC_SYNTAX_H + +#ifndef PSYC_LIST_SIZE_LIMIT +# define PSYC_LIST_SIZE_LIMIT 404 +#endif + +/* beyond this a content length must be provided */ +#ifndef PSYC_CONTENT_SIZE_THRESHOLD +# define PSYC_CONTENT_SIZE_THRESHOLD 444 +#endif + +#define C_GLYPH_PACKET_DELIMITER '.' +#define S_GLYPH_PACKET_DELIMITER "." + +#define C_GLYPH_SEPARATOR_KEYWORD '_' +#define S_GLYPH_SEPARATOR_KEYWORD "_" + +#define C_GLYPH_MODIFIER_SET ':' +#define S_GLYPH_MODIFIER_SET ":" + +#define C_GLYPH_MODIFIER_ASSIGN '=' +#define S_GLYPH_MODIFIER_ASSIGN "=" + +#define C_GLYPH_MODIFIER_AUGMENT '+' +#define S_GLYPH_MODIFIER_AUGMENT "+" + +#define C_GLYPH_MODIFIER_DIMINISH '-' +#define S_GLYPH_MODIFIER_DIMINISH "-" + +#define C_GLYPH_MODIFIER_QUERY '?' +#define S_GLYPH_MODIFIER_QUERY "?" + +/* might move into routing.h or something */ +#define PSYC_ROUTING 1 +#define PSYC_ROUTING_MERGE 2 +#define PSYC_ROUTING_RENDER 4 + +#endif /* PSYC_SYNTAX_H */ diff --git a/src/Makefile b/src/Makefile index 0403868..10a5e93 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,18 +1,21 @@ -CC=cc +CC=cc -I../include # CC=clang +S=parser.c match.c +O=parser.o match.o + default: @/bin/echo -e "Usage:\n\tmake diet - compile with diet libc\n\tmake lib - compile with normal gnu libc" diet: - /opt/diet/bin/diet ${CC} -static -c -Os parser.c -o libpsyc.o -DDEBUG - /opt/diet/bin/diet ar rcs libpsyc.a libpsyc.o + /opt/diet/bin/diet ${CC} -static -c -Os $S -DDEBUG + /opt/diet/bin/diet ar rcs libpsyc.a $O -lib: - ${CC} -static -c -Os parser.c -lc -o libpsyc.o -DDEBUG -I../include - ar rcs libpsyc.a libpsyc.o +lib: $S + ${CC} -static -c -Os $S -lc -DDEBUG + ar rcs libpsyc.a $O match: match.c - ${CC} -o $@ -DTEST $< + ${CC} -o $@ -DDEBUG=2 -DCMDTOOL -DTEST $< it: match diff --git a/src/match.c b/src/match.c index 6354012..f80653e 100644 --- a/src/match.c +++ b/src/match.c @@ -1,15 +1,7 @@ -#include -#include - -#ifdef TEST -# include -# define PT(args) printf args; -#else -# define PT(args) -#endif +#include "psyc/lib.h" int PSYC_matches(uint8_t* sho, unsigned int slen, - uint8_t* lon, unsigned int llen) { + uint8_t* lon, unsigned int llen) { uint8_t *s, *l, *se, *le; if (!slen) slen = strlen(sho); @@ -66,7 +58,7 @@ failed: return 1; } -#ifdef TEST +#ifdef CMDTOOL int main(int argc, char **argv) { if (argc != 3) { printf("Usage: %s \n\nExample: %s _failure_delivery _failure_unsuccessful_delivery_death\n", argv[0], argv[0]); diff --git a/src/tests/testMatch.c b/src/tests/testMatch.c new file mode 100644 index 0000000..933d217 --- /dev/null +++ b/src/tests/testMatch.c @@ -0,0 +1,12 @@ +#include +#include "../include/psyc/lib.h" + +int main() { + if (PSYC_matches("_failure_delivery", 0, "_failure_unsuccessful_delivery_death", 0)) return -1; + if (PSYC_matches("_failure_trash", 8, "_failure_unsuccessful_delivery_death", 0)) return -2; + if (PSYC_matches("_unsuccessful", 0, "_failure_unsuccessful_delivery_death", 0)) return -3; + if (PSYC_matches("_fail", 0, "_failure_unsuccessful_delivery_death", 0)) return -4; + unless (PSYC_matches("_truthahn", 0, "_failure_unsuccessful_delivery_death", 0)) return -5; + + return 0; // passed all tests +}