diff --git a/include/psyc.h b/include/psyc.h index 8ca562b..6d8f1bc 100644 --- a/include/psyc.h +++ b/include/psyc.h @@ -24,6 +24,9 @@ #define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday) +#define PSYC_C2STR(string) {sizeof(string)-1, string} +#define PSYC_C2ARG(string) string, sizeof(string)-1 + typedef enum { PSYC_FALSE = 0, @@ -79,28 +82,6 @@ typedef enum PSYC_LIST_BINARY = 2, } psycListType; -typedef enum -{ - PSYC_MODIFIER_CHECK_LENGTH = 0, - PSYC_MODIFIER_NEED_LENGTH = 1, - PSYC_MODIFIER_NO_LENGTH = 2, - PSYC_MODIFIER_ROUTING = 3, -} psycModifierFlag; - -typedef enum -{ - PSYC_LIST_CHECK_LENGTH = 0, - PSYC_LIST_NEED_LENGTH = 1, - PSYC_LIST_NO_LENGTH = 2, -} psycListFlag; - -typedef enum -{ - PSYC_PACKET_CHECK_LENGTH = 0, - PSYC_PACKET_NEED_LENGTH = 1, - PSYC_PACKET_NO_LENGTH = 2, -} psycPacketFlag; - typedef struct { size_t length; @@ -121,92 +102,18 @@ typedef struct * * @return An instance of the psycString struct. */ -inline psycString psyc_newString (const char *str, size_t strlen); - -#define PSYC_C2STR(string) {sizeof(string)-1, string} -#define PSYC_C2ARG(string) string, sizeof(string)-1 - -/* intermediate struct for a PSYC variable modification */ -typedef struct +static inline +psycString psyc_newString (const char *str, size_t strlen) { - char oper; // not call it 'operator' as C++ may not like that.. - psycString name; - psycString value; - psycModifierFlag flag; -} psycModifier; + psycString s = {strlen, str}; + return s; +} -typedef struct +static inline +unsigned int psyc_version () { - size_t lines; - psycModifier *modifiers; -} psycHeader; - -typedef struct -{ - size_t num_elems; - psycString *elems; - size_t length; - psycListFlag flag; -} psycList; - -/** intermediate struct for a PSYC packet */ -typedef struct -{ - psycHeader routing; ///< Routing header. - psycHeader entity; ///< Entity header. - psycString method; - psycString data; - size_t routingLength; ///< Length of routing part. - size_t contentLength; ///< Length of content part. - size_t length; ///< Total length of packet. - psycPacketFlag flag; -} psycPacket; - -inline int psyc_version(); - -/** Check if a modifier needs length */ -inline psycModifierFlag psyc_checkModifierLength(psycModifier *m); - -/** Get the total length of a modifier. */ -inline size_t psyc_getModifierLength(psycModifier *m); - -/** Create new modifier */ -inline psycModifier psyc_newModifier(char oper, psycString *name, psycString *value, - psycModifierFlag flag); - -/** Create new modifier */ -inline psycModifier psyc_newModifier2(char oper, - const char *name, size_t namelen, - const char *value, size_t valuelen, - psycModifierFlag flag); - -/** Check if a list needs length */ -inline psycListFlag psyc_checkListLength(psycList *list); - -/** Get the total length of a list. */ -inline psycListFlag psyc_getListLength(psycList *list); - -/** Check if a packet needs length */ -inline psycPacketFlag psyc_checkPacketLength(psycPacket *p); - -/** Calculate and set the length of packet parts and total packet length */ -inline size_t psyc_setPacketLength(psycPacket *p); - -/** Create new list */ -inline psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag flag); - -/** Create new packet */ -inline psycPacket psyc_newPacket(psycHeader *routing, - psycHeader *entity, - psycString *method, psycString *data, - psycPacketFlag flag); - -/** Create new packet */ -inline psycPacket psyc_newPacket2(psycModifier *routing, size_t routinglen, - psycModifier *entity, size_t entitylen, - const char *method, size_t methodlen, - const char *data, size_t datalen, - psycPacketFlag flag); + return 1; +} /// Routing vars in alphabetical order. extern const psycString PSYC_routingVars[]; diff --git a/include/psyc/packet.h b/include/psyc/packet.h new file mode 100644 index 0000000..173b3d9 --- /dev/null +++ b/include/psyc/packet.h @@ -0,0 +1,138 @@ +#ifndef PSYC_PACKET_H +# define PSYC_PACKET_H + +#include +#include + +typedef enum +{ + PSYC_MODIFIER_CHECK_LENGTH = 0, + PSYC_MODIFIER_NEED_LENGTH = 1, + PSYC_MODIFIER_NO_LENGTH = 2, + PSYC_MODIFIER_ROUTING = 3, +} psycModifierFlag; + +typedef enum +{ + PSYC_LIST_CHECK_LENGTH = 0, + PSYC_LIST_NEED_LENGTH = 1, + PSYC_LIST_NO_LENGTH = 2, +} psycListFlag; + +typedef enum +{ + PSYC_PACKET_CHECK_LENGTH = 0, + PSYC_PACKET_NEED_LENGTH = 1, + PSYC_PACKET_NO_LENGTH = 2, +} psycPacketFlag; + +/** intermediate struct for a PSYC variable modification */ +typedef struct +{ + char oper; + psycString name; + psycString value; + psycModifierFlag flag; +} psycModifier; + +typedef struct +{ + size_t lines; + psycModifier *modifiers; +} psycHeader; + +typedef struct +{ + size_t num_elems; + psycString *elems; + size_t length; + psycListFlag flag; +} psycList; + +/** intermediate struct for a PSYC packet */ +typedef struct +{ + psycHeader routing; ///< Routing header. + psycHeader entity; ///< Entity header. + psycString method; + psycString data; + size_t routingLength; ///< Length of routing part. + size_t contentLength; ///< Length of content part. + size_t length; ///< Total length of packet. + psycPacketFlag flag; +} psycPacket; + +/** Check if a modifier needs length */ +static inline +psycModifierFlag psyc_checkModifierLength (psycModifier *m) +{ + psycModifierFlag flag; + + if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD) + flag = PSYC_MODIFIER_NEED_LENGTH; + else if (memchr(m->value.ptr, (int)'\n', m->value.length)) + flag = PSYC_MODIFIER_NEED_LENGTH; + else + flag = PSYC_MODIFIER_NO_LENGTH; + + return flag; +} + +/** Create new modifier */ +static inline +psycModifier psyc_newModifier (char oper, psycString *name, psycString *value, + psycModifierFlag flag) +{ + psycModifier m = {oper, *name, *value, flag}; + + if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length + m.flag = psyc_checkModifierLength(&m); + + return m; +} + +/** Create new modifier */ +static inline +psycModifier psyc_newModifier2 (char oper, + const char *name, size_t namelen, + const char *value, size_t valuelen, + psycModifierFlag flag) +{ + psycString n = {namelen, name}; + psycString v = {valuelen, value}; + + return psyc_newModifier(oper, &n, &v, flag); +} + +/** Get the total length of a modifier. */ +size_t psyc_getModifierLength (psycModifier *m); + +/** Check if a list needs length */ +psycListFlag psyc_checkListLength (psycList *list); + +/** Get the total length of a list. */ +psycListFlag psyc_getListLength (psycList *list); + +/** Check if a packet needs length */ +psycPacketFlag psyc_checkPacketLength (psycPacket *p); + +/** Calculate and set the length of packet parts and total packet length */ +size_t psyc_setPacketLength (psycPacket *p); + +/** Create new list */ +psycList psyc_newList (psycString *elems, size_t num_elems, psycListFlag flag); + +/** Create new packet */ +psycPacket psyc_newPacket (psycHeader *routing, + psycHeader *entity, + psycString *method, psycString *data, + psycPacketFlag flag); + +/** Create new packet */ +psycPacket psyc_newPacket2 (psycModifier *routing, size_t routinglen, + psycModifier *entity, size_t entitylen, + const char *method, size_t methodlen, + const char *data, size_t datalen, + psycPacketFlag flag); + +#endif // PSYC_PACKET_H diff --git a/include/psyc/parser.h b/include/psyc/parser.h index 77e10cd..87de724 100644 --- a/include/psyc/parser.h +++ b/include/psyc/parser.h @@ -131,7 +131,7 @@ typedef struct * @param state Pointer to the state struct that should be initiated. */ static inline -void psyc_initParseState ( psycParseState* state ) +void psyc_initParseState (psycParseState* state) { memset(state, 0, sizeof(psycParseState)); } @@ -143,7 +143,7 @@ void psyc_initParseState ( psycParseState* state ) * @param flags Flags to be set for the parser, see psycParseFlag. */ static inline -void psyc_initParseState2 ( psycParseState* state, uint8_t flags ) +void psyc_initParseState2 (psycParseState* state, uint8_t flags) { memset(state, 0, sizeof(psycParseState)); state->flags = flags; @@ -160,7 +160,7 @@ void psyc_initParseState2 ( psycParseState* state, uint8_t flags ) * @see psycString */ static inline -void psyc_setParseBuffer ( psycParseState* state, psycString buffer ) +void psyc_setParseBuffer (psycParseState* state, psycString buffer) { state->buffer = buffer; state->cursor = 0; @@ -181,7 +181,7 @@ void psyc_setParseBuffer ( psycParseState* state, psycString buffer ) * @see psycString */ static inline -void psyc_setParseBuffer2 ( psycParseState* state, char *buffer, size_t length ) +void psyc_setParseBuffer2 (psycParseState* state, char *buffer, size_t length) { psyc_setParseBuffer(state, psyc_newString(buffer, length)); } @@ -192,7 +192,7 @@ void psyc_setParseBuffer2 ( psycParseState* state, char *buffer, size_t length ) * @param state Pointer to the list state struct that should be initiated. */ static inline -void psyc_initParseListState ( psycParseListState* state ) +void psyc_initParseListState (psycParseListState* state) { memset(state, 0, sizeof(psycParseListState)); } @@ -201,20 +201,20 @@ void psyc_initParseListState ( psycParseListState* state ) * Sets a new buffer in the list parser state struct with data to be parsed. */ static inline -void psyc_setParseListBuffer ( psycParseListState* state, psycString buffer ) +void psyc_setParseListBuffer (psycParseListState* state, psycString buffer) { state->buffer = buffer; state->cursor = 0; } static inline -void psyc_setParseListBuffer2 ( psycParseListState* state, char *buffer, size_t length ) +void psyc_setParseListBuffer2 (psycParseListState* state, char *buffer, size_t length) { psyc_setParseListBuffer(state, psyc_newString(buffer, length)); } static inline -size_t psyc_getContentLength ( psycParseState* s ) +size_t psyc_getContentLength (psycParseState* s) { return s->contentLength; } @@ -232,12 +232,12 @@ size_t psyc_getContentLength ( psycParseState* s ) * @param value A pointer to a psycString. It will point to the * value/body the variable/method and its length will be set accordingly */ -psycParseRC psyc_parse ( psycParseState* state, char* oper, psycString* name, psycString* value); +psycParseRC psyc_parse (psycParseState* state, char* oper, psycString* name, psycString* value); /** * List value parser. */ -psycParseListRC psyc_parseList ( psycParseListState* state, psycString *name, psycString* value, psycString* elem); +psycParseListRC psyc_parseList (psycParseListState* state, psycString *name, psycString* value, psycString* elem); #endif // PSYC_PARSER_H diff --git a/include/psyc/render.h b/include/psyc/render.h index 86eb037..bf7ebcb 100644 --- a/include/psyc/render.h +++ b/include/psyc/render.h @@ -1,6 +1,8 @@ #ifndef PSYC_RENDER_H # define PSYC_RENDER_H +#include + /** * @file psyc/render.h * @brief Interface for PSYC packet rendering. @@ -29,12 +31,12 @@ typedef enum /** * Render a PSYC packet into a buffer. */ -psycRenderRC psyc_render(psycPacket *packet, char *buffer, size_t buflen); +psycRenderRC psyc_render (psycPacket *packet, char *buffer, size_t buflen); /** * Render a PSYC list into a buffer. */ -psycRenderRC psyc_renderList(psycList *list, char *buffer, size_t buflen); +psycRenderRC psyc_renderList (psycList *list, char *buffer, size_t buflen); #endif // PSYC_RENDER_H diff --git a/include/psyc/text.h b/include/psyc/text.h index 2bb5394..a9f25b3 100644 --- a/include/psyc/text.h +++ b/include/psyc/text.h @@ -93,4 +93,4 @@ void psyc_setTextBuffer2 (psycTextState* state, * See also http://about.psyc.eu/psyctext */ -psycTextRC psyc_text(psycTextState *state, psycTextCB getValue); +psycTextRC psyc_text (psycTextState *state, psycTextCB getValue); diff --git a/src/Makefile b/src/Makefile index 4ffaed1..e0bb01b 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1,10 +1,10 @@ OPT = -O2 DEBUG = 2 -CFLAGS = -I../include -Wall ${OPT} +CFLAGS = -I../include -Wall -std=c99 ${OPT} DIET = diet -S = packet.c misc.c parser.c match.c render.c memmem.c itoa.c variable.c text.c -O = packet.o misc.o parser.o match.o render.o memmem.o itoa.o variable.o text.o +S = packet.c parser.c match.c render.c memmem.c itoa.c variable.c text.c +O = packet.o parser.o match.o render.o memmem.o itoa.o variable.o text.o all: CC := ${WRAPPER} ${CC} all: lib diff --git a/src/misc.c b/src/misc.c deleted file mode 100644 index a92359d..0000000 --- a/src/misc.c +++ /dev/null @@ -1,2 +0,0 @@ -int psyc_version() { return 1; } - diff --git a/src/packet.c b/src/packet.c index a29a21f..c30d9fd 100644 --- a/src/packet.c +++ b/src/packet.c @@ -1,63 +1,11 @@ #include #include +#include #include -inline psycString psyc_newString(const char *str, size_t strlen) -{ - psycString s = {strlen, str}; - return s; -} - -inline psycModifierFlag psyc_checkModifierLength(psycModifier *m) -{ - psycModifierFlag flag; - - if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD) - flag = PSYC_MODIFIER_NEED_LENGTH; - else if (memchr(m->value.ptr, (int)'\n', m->value.length)) - flag = PSYC_MODIFIER_NEED_LENGTH; - else - flag = PSYC_MODIFIER_NO_LENGTH; - - return flag; -} - -inline size_t psyc_getModifierLength(psycModifier *m) -{ - size_t length = 1 + // oper - m->name.length + 1 + // name\t - m->value.length + 1; // value\n - - if (m->flag == PSYC_MODIFIER_NEED_LENGTH) // add length of length if needed - length += log10((double)m->value.length) + 2; // SP length - - return length; -} - -inline psycModifier psyc_newModifier(char oper, psycString *name, psycString *value, - psycModifierFlag flag) -{ - psycModifier m = {oper, *name, *value, flag}; - - if (flag == PSYC_MODIFIER_CHECK_LENGTH) // find out if it needs a length - m.flag = psyc_checkModifierLength(&m); - - return m; -} - -inline psycModifier psyc_newModifier2(char oper, - const char *name, size_t namelen, - const char *value, size_t valuelen, - psycModifierFlag flag) -{ - psycString n = {namelen, name}; - psycString v = {valuelen, value}; - - return psyc_newModifier(oper, &n, &v, flag); -} - -inline psycListFlag psyc_checkListLength(psycList *list) +inline +psycListFlag psyc_checkListLength (psycList *list) { psycListFlag flag = PSYC_LIST_NO_LENGTH; size_t i, length = 0; @@ -78,7 +26,8 @@ inline psycListFlag psyc_checkListLength(psycList *list) return flag; } -inline psycListFlag psyc_getListLength(psycList *list) +inline +psycListFlag psyc_getListLength (psycList *list) { size_t i, length = 0; @@ -100,7 +49,8 @@ inline psycListFlag psyc_getListLength(psycList *list) return length; } -inline psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag flag) +inline +psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag flag) { psycList list = {num_elems, elems, 0, flag}; @@ -111,7 +61,22 @@ inline psycList psyc_newList(psycString *elems, size_t num_elems, psycListFlag f return list; } -inline psycPacketFlag psyc_checkPacketLength(psycPacket *p) + +inline +size_t psyc_getModifierLength (psycModifier *m) +{ + size_t length = 1 + // oper + m->name.length + 1 + // name\t + m->value.length + 1; // value\n + + if (m->flag == PSYC_MODIFIER_NEED_LENGTH) // add length of length if needed + length += log10((double)m->value.length) + 2; // SP length + + return length; +} + +inline +psycPacketFlag psyc_checkPacketLength(psycPacket *p) { if (p->data.length == 1 && p->data.ptr[0] == C_GLYPH_PACKET_DELIMITER) return PSYC_PACKET_NEED_LENGTH; @@ -132,7 +97,8 @@ inline psycPacketFlag psyc_checkPacketLength(psycPacket *p) return PSYC_PACKET_NO_LENGTH; } -inline size_t psyc_setPacketLength(psycPacket *p) +inline +size_t psyc_setPacketLength(psycPacket *p) { size_t i; p->routingLength = 0; @@ -165,10 +131,10 @@ inline size_t psyc_setPacketLength(psycPacket *p) return p->length; } -inline psycPacket psyc_newPacket(psycHeader *routing, - psycHeader *entity, - psycString *method, psycString *data, - psycPacketFlag flag) +inline +psycPacket psyc_newPacket (psycHeader *routing, psycHeader *entity, + psycString *method, psycString *data, + psycPacketFlag flag) { psycPacket p = {*routing, *entity, *method, *data, 0, 0, flag}; @@ -179,11 +145,12 @@ inline psycPacket psyc_newPacket(psycHeader *routing, return p; } -inline psycPacket psyc_newPacket2(psycModifier *routing, size_t routinglen, - psycModifier *entity, size_t entitylen, - const char *method, size_t methodlen, - const char *data, size_t datalen, - psycPacketFlag flag) +inline +psycPacket psyc_newPacket2 (psycModifier *routing, size_t routinglen, + psycModifier *entity, size_t entitylen, + const char *method, size_t methodlen, + const char *data, size_t datalen, + psycPacketFlag flag) { psycHeader r = {routinglen, routing}; psycHeader e = {entitylen, entity}; diff --git a/src/parser.c b/src/parser.c index a4b866c..6778090 100644 --- a/src/parser.c +++ b/src/parser.c @@ -20,7 +20,8 @@ * Determines if the argument is a glyph. * Glyphs are: : = + - ? ! */ -inline char isGlyph(uint8_t g) +static inline +char isGlyph (uint8_t g) { switch(g) { @@ -39,7 +40,8 @@ inline char isGlyph(uint8_t g) /** * Determines if the argument is numeric. */ -inline char isNumeric (uint8_t c) +static inline +char isNumeric (uint8_t c) { return c >= '0' && c <= '9'; } @@ -47,7 +49,8 @@ inline char isNumeric (uint8_t c) /** * Determines if the argument is alphanumeric. */ -inline char isAlphaNumeric (uint8_t c) +static inline +char isAlphaNumeric (uint8_t c) { return (c >= 'a' && c <= 'z') || @@ -59,7 +62,8 @@ inline char isAlphaNumeric (uint8_t c) * Determines if the argument is a keyword character. * Keyword characters are: alphanumeric and _ */ -inline char isKwChar (uint8_t c) +static inline +char isKwChar (uint8_t c) { return isAlphaNumeric(c) || c == '_'; } @@ -69,7 +73,8 @@ inline char isKwChar (uint8_t c) * It should contain one or more keyword characters. * @return PSYC_PARSE_ERROR or PSYC_PARSE_SUCCESS */ -inline psycParseRC psyc_parseName (psycParseState* state, psycString* name) +static inline +psycParseRC psyc_parseKeyword (psycParseState* state, psycString* name) { name->ptr = state->buffer.ptr + state->cursor; name->length = 0; @@ -93,7 +98,9 @@ inline psycParseRC psyc_parseName (psycParseState* state, psycString* name) * * @return PSYC_PARSE_COMPLETE or PSYC_PARSE_INCOMPLETE */ -inline psycParseRC psyc_parseBinaryValue (psycParseState* state, psycString* value, size_t* length, size_t* parsed) +static inline +psycParseRC psyc_parseBinaryValue (psycParseState *state, psycString *value, + size_t *length, size_t *parsed) { size_t remaining = *length - *parsed; value->ptr = state->buffer.ptr + state->cursor; @@ -118,12 +125,14 @@ inline psycParseRC psyc_parseBinaryValue (psycParseState* state, psycString* val * Parse simple or binary variable. * @return PSYC_PARSE_ERROR or PSYC_PARSE_SUCCESS */ -inline psycParseRC psyc_parseModifier (psycParseState* state, char* oper, psycString* name, psycString* value) +static inline +psycParseRC psyc_parseModifier (psycParseState *state, char *oper, + psycString *name, psycString *value) { *oper = *(state->buffer.ptr + state->cursor); PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); - psycParseRC ret = psyc_parseName(state, name); + psycParseRC ret = psyc_parseKeyword(state, name); if (ret == PSYC_PARSE_ERROR) return PSYC_PARSE_ERROR_MOD_NAME; else if (ret != PSYC_PARSE_SUCCESS) @@ -187,10 +196,12 @@ inline psycParseRC psyc_parseModifier (psycParseState* state, char* oper, psycSt * Parse PSYC packets. * Generalized line-based parser. */ -psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psycString* value) +psycParseRC psyc_parse (psycParseState* state, char* oper, + psycString* name, psycString* value) { #ifdef DEBUG - if (state->flags & PSYC_PARSE_ROUTING_ONLY && state->flags & PSYC_PARSE_START_AT_CONTENT) + if (state->flags & PSYC_PARSE_ROUTING_ONLY && + state->flags & PSYC_PARSE_START_AT_CONTENT) PP(("Invalid flag combination")) #endif @@ -333,7 +344,7 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc case PSYC_PART_METHOD: pos = state->cursor; - ret = psyc_parseName(state, name); + ret = psyc_parseKeyword(state, name); if (ret == PSYC_PARSE_INSUFFICIENT) return ret; @@ -369,7 +380,8 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc if (state->contentLengthFound) // We know the length of the packet. { if (state->contentParsed < state->contentLength && - psyc_parseBinaryValue(state, value, &(state->contentLength), &(state->contentParsed)) == PSYC_PARSE_INCOMPLETE) + psyc_parseBinaryValue(state, value, &(state->contentLength), + &(state->contentParsed)) == PSYC_PARSE_INCOMPLETE) return PSYC_PARSE_BODY_INCOMPLETE; if (state->cursor >= state->buffer.length) @@ -437,7 +449,8 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc * List value parser. * @return see psycListRC. */ -psycParseListRC psyc_parseList(psycParseListState* state, psycString *name, psycString* value, psycString* elem) +psycParseListRC psyc_parseList (psycParseListState* state, psycString *name, + psycString* value, psycString* elem) { if (state->cursor >= state->buffer.length) return PSYC_PARSE_LIST_INCOMPLETE; @@ -508,7 +521,8 @@ psycParseListRC psyc_parseList(psycParseListState* state, psycString *name, psyc // Start or resume parsing the binary data if (state->elemParsed < state->elemLength) { - if (psyc_parseBinaryValue((psycParseState*)state, elem, &(state->elemLength), &(state->elemParsed)) == PSYC_PARSE_INCOMPLETE) + if (psyc_parseBinaryValue((psycParseState*)state, elem, + &(state->elemLength), &(state->elemParsed)) == PSYC_PARSE_INCOMPLETE) return PSYC_PARSE_LIST_INCOMPLETE; state->elemLength = 0; diff --git a/src/render.c b/src/render.c index b6a9569..6935f8f 100644 --- a/src/render.c +++ b/src/render.c @@ -2,7 +2,7 @@ #include "psyc/render.h" #include "psyc/syntax.h" -psycRenderRC psyc_renderList(psycList *list, char *buffer, size_t buflen) +psycRenderRC psyc_renderList (psycList *list, char *buffer, size_t buflen) { size_t i, cur = 0; psycString *elem; @@ -37,7 +37,8 @@ psycRenderRC psyc_renderList(psycList *list, char *buffer, size_t buflen) return PSYC_RENDER_SUCCESS; } -inline size_t psyc_renderModifier(psycModifier *mod, char *buffer) +static inline +size_t psyc_renderModifier (psycModifier *mod, char *buffer) { size_t cur = 0; @@ -58,7 +59,7 @@ inline size_t psyc_renderModifier(psycModifier *mod, char *buffer) return cur; } -psycRenderRC psyc_render(psycPacket *packet, char *buffer, size_t buflen) +psycRenderRC psyc_render (psycPacket *packet, char *buffer, size_t buflen) { size_t i, cur = 0; diff --git a/test/Makefile b/test/Makefile index 7b7dbb5..4b73eb7 100644 --- a/test/Makefile +++ b/test/Makefile @@ -1,6 +1,6 @@ OPT = -O2 DEBUG = 2 -CFLAGS = -I../include -Wall ${OPT} +CFLAGS = -I../include -Wall -std=c99 ${OPT} LDFLAGS = -L../lib LOADLIBES = -lpsyc -lm TARGETS = testServer testParser testMatch testRender testText isRoutingVar getVarType @@ -20,10 +20,13 @@ debug: all test: ${TARGETS} ./testRender ./testMatch + ./testText ./isRoutingVar ./getVarType for f in packets/full-*; do echo ">> $$f"; ./testParser $$f; done +testServer: CFLAGS := $(subst -std=c99,,${CFLAGS}) + netstart: ./testServer ${PORT} diff --git a/test/testParser.c b/test/testParser.c index deb470e..2f203f7 100644 --- a/test/testParser.c +++ b/test/testParser.c @@ -2,10 +2,10 @@ #include #include -#include +#include #include -int main(int argc, char **argv) +int main (int argc, char **argv) { int idx, ret, routing_only = argc > 2, verbose = argc > 3; char buffer[2048], oper; @@ -19,9 +19,9 @@ int main(int argc, char **argv) idx = read(file,(void*)buffer,sizeof(buffer)); if (verbose) { - write(1, ">> INPUT\n", 9); - write(1, buffer, idx); - write(1, ">> PARSE\n", 9); + printf(">> INPUT\n"); + printf("%.*s\n", (int)idx, buffer); + printf(">> PARSE\n"); } if (routing_only) @@ -32,9 +32,8 @@ int main(int argc, char **argv) psyc_setParseBuffer(&state, psyc_newString(buffer, idx)); // try parsing that now -// while ((ret = psyc_parse(&state, &oper, &name, &value))) -// { - do { + do + { oper = 0; name.length = 0; value.length = 0; @@ -42,24 +41,24 @@ int main(int argc, char **argv) ret = psyc_parse(&state, &oper, &name, &value); if (verbose) printf(">> ret = %d\n", ret); + switch (ret) { case PSYC_PARSE_ROUTING: case PSYC_PARSE_ENTITY: if (verbose) - write(1, &oper, 1); + printf("%c", oper); case PSYC_PARSE_BODY: // printf("the string is '%.*s'\n", name); - if (verbose) { - write(1, name.ptr, name.length); - write(1, " = ", 3); - write(1, value.ptr, value.length); - write(1, "\n", 1); - } + if (verbose) + printf("%.*s = %.*s\n", + (int)name.length, name.ptr, + (int)value.length, value.ptr); + if (memcmp(name.ptr, "_list", 5) == 0) { if (verbose) - write(1, ">>> LIST START\n", 15); + printf(">> LIST START\n"); psyc_initParseListState(&listState); psyc_setParseListBuffer(&listState, value); @@ -70,11 +69,8 @@ int main(int argc, char **argv) { case PSYC_PARSE_LIST_END: case PSYC_PARSE_LIST_ELEM: - if (verbose) { - write(1, "|", 1); - write(1, elem.ptr, elem.length); - write(1, "\n", 1); - } + if (verbose) + printf("|%.*s\n", (int)elem.length, elem.ptr); break; default: printf("Error while parsing list: %i\n", ret); @@ -84,7 +80,7 @@ int main(int argc, char **argv) if (ret == PSYC_PARSE_LIST_END) { if (verbose) - write(1, ">>> LIST END\n", 13); + printf(">> LIST END\n"); break; } } @@ -101,7 +97,8 @@ int main(int argc, char **argv) printf("Error while parsing: %i\n", ret); return 1; } - } while (ret); + } + while (ret); return 0; } diff --git a/test/testRender.c b/test/testRender.c index 4a274f8..edfb798 100644 --- a/test/testRender.c +++ b/test/testRender.c @@ -1,13 +1,15 @@ -#include -#include -#include "../include/psyc/lib.h" -#include "../include/psyc/render.h" -#include "../include/psyc/syntax.h" +#include + +#include +#include +#include #define myUNI "psyc://10.100.1000/~ludwig" /* example renderer generating a presence packet */ -int testPresence(const char *avail, int availlen, const char *desc, int desclen, const char *rendered, uint8_t verbose) +int testPresence (const char *avail, int availlen, + const char *desc, int desclen, + const char *rendered, uint8_t verbose) { psycModifier routing[] = { psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_context"), PSYC_C2ARG(myUNI), @@ -31,11 +33,11 @@ int testPresence(const char *avail, int availlen, const char *desc, int desclen, char buffer[512]; psyc_render(&packet, buffer, sizeof(buffer)); if (verbose) - write(0, buffer, packet.length); + printf("%.*s\n", (int)packet.length, buffer); return strncmp(rendered, buffer, packet.length); } -int testList(const char *rendered, uint8_t verbose) +int testList (const char *rendered, uint8_t verbose) { psycModifier routing[] = { psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_source"), PSYC_C2ARG(myUNI), @@ -80,11 +82,11 @@ int testList(const char *rendered, uint8_t verbose) char buffer[512]; psyc_render(&packet, buffer, sizeof(buffer)); if (verbose) - write(0, buffer, packet.length); + printf("%.*s\n", (int)packet.length, buffer); return strncmp(rendered, buffer, packet.length); } -int main(int argc, char **argv) { +int main (int argc, char **argv) { uint8_t verbose = argc > 1; if (testPresence(PSYC_C2ARG("_here"), PSYC_C2ARG("I'm omnipresent right now"), "\ diff --git a/test/testServer.c b/test/testServer.c index b2256da..9003cf2 100644 --- a/test/testServer.c +++ b/test/testServer.c @@ -31,16 +31,15 @@ const size_t ROUTING_LINES = 16; const size_t ENTITY_LINES = 32; // get sockaddr, IPv4 or IPv6: -void *get_in_addr(struct sockaddr *sa) +void *get_in_addr (struct sockaddr *sa) { - if (sa->sa_family == AF_INET) { + if (sa->sa_family == AF_INET) return &(((struct sockaddr_in*)sa)->sin_addr); - } return &(((struct sockaddr_in6*)sa)->sin6_addr); } -int main(int argc, char **argv) +int main (int argc, char **argv) { char *port = argc > 1 ? argv[1] : "4440"; uint8_t routing_only = argc > 2, verbose = argc > 3; @@ -71,12 +70,12 @@ int main(int argc, char **argv) psycPacket packets[NUM_PARSERS]; psycModifier routing[NUM_PARSERS][ROUTING_LINES]; psycModifier entity[NUM_PARSERS][ENTITY_LINES]; - psycModifier *mod; + psycModifier *mod = NULL; int ret, retl; char oper; psycString name, value, elem; - psycString *pname, *pvalue; + psycString *pname = NULL, *pvalue = NULL; psycParseListState listState; FD_ZERO(&master); // clear the master and temp sets @@ -87,21 +86,23 @@ int main(int argc, char **argv) hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; - if ((rv = getaddrinfo(NULL, port, &hints, &ai)) != 0) { + if ((rv = getaddrinfo(NULL, port, &hints, &ai)) != 0) +{ fprintf(stderr, "error: %s\n", gai_strerror(rv)); exit(1); } - for (p = ai; p != NULL; p = p->ai_next) { + for (p = ai; p != NULL; p = p->ai_next) + { listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol); - if (listener < 0) { + if (listener < 0) continue; - } // lose the pesky "address already in use" error message setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); - if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) { + if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) + { close(listener); continue; } @@ -110,7 +111,8 @@ int main(int argc, char **argv) } // if we got here, it means we didn't get bound - if (p == NULL) { + if (p == NULL) + { fprintf(stderr, "failed to bind\n"); exit(2); } @@ -118,7 +120,8 @@ int main(int argc, char **argv) freeaddrinfo(ai); // all done with this // listen - if (listen(listener, 10) == -1) { + if (listen(listener, 10) == -1) + { perror("listen"); exit(3); } @@ -130,17 +133,22 @@ int main(int argc, char **argv) fdmax = listener; // so far, it's this one // main loop - for (;;) { + for (;;) + { read_fds = master; // copy it - if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) { + if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) + { perror("select"); exit(4); } // run through the existing connections looking for data to read - for (i = 0; i <= fdmax; i++) { - if (FD_ISSET(i, &read_fds)) { // we got one!! - if (i == listener) { + for (i = 0; i <= fdmax; i++) + { + if (FD_ISSET(i, &read_fds)) // we got one!! + { + if (i == listener) + { // handle new connections if (fdmax == NUM_PARSERS - 1) continue; // ignore if there's too many @@ -148,13 +156,13 @@ int main(int argc, char **argv) addrlen = sizeof remoteaddr; newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen); - if (newfd == -1) { + if (newfd == -1) perror("accept"); - } else { + else + { FD_SET(newfd, &master); // add to master set - if (newfd > fdmax) { // keep track of the max + if (newfd > fdmax) // keep track of the max fdmax = newfd; - } // reset parser state & packet if (routing_only) @@ -174,19 +182,23 @@ int main(int argc, char **argv) remoteIP, INET6_ADDRSTRLEN), newfd); } - } else { + } + else + { // handle data from a client - if ((nbytes = recv(i, recvbuf, RECV_BUF_SIZE, 0)) <= 0) { + if ((nbytes = recv(i, recvbuf, RECV_BUF_SIZE, 0)) <= 0) + { // got error or connection closed by client - if (nbytes == 0) { - // connection closed + if (nbytes == 0) // connection closed printf("socket %d hung up\n", i); - } else { + else perror("recv"); - } + close(i); // bye! FD_CLR(i, &master); // remove from master set - } else { + } + else + { // we got some data from a client parsebuf = recvbuf - contbytes; psyc_setParseBuffer(&parsers[i], psyc_newString(parsebuf, contbytes + nbytes)); @@ -195,7 +207,8 @@ int main(int argc, char **argv) name.length = 0; value.length = 0; - do { + do + { ret = psyc_parse(&parsers[i], &oper, &name, &value); if (verbose) printf("# ret = %d\n", ret); @@ -209,69 +222,86 @@ int main(int argc, char **argv) mod->flag = PSYC_MODIFIER_ROUTING; packets[i].routing.lines++; break; + case PSYC_PARSE_ENTITY_INCOMPLETE: case PSYC_PARSE_ENTITY: assert(packets[i].entity.lines < ENTITY_LINES); mod = &(packets[i].entity.modifiers[packets[i].entity.lines]); pname = &mod->name; pvalue = &mod->value; - if (ret == PSYC_PARSE_ENTITY) { + + if (ret == PSYC_PARSE_ENTITY) + { packets[i].entity.lines++; - mod->flag = parsers[i].valueLength ? PSYC_MODIFIER_NEED_LENGTH : PSYC_MODIFIER_NO_LENGTH; + mod->flag = parsers[i].valueLength ? + PSYC_MODIFIER_NEED_LENGTH : PSYC_MODIFIER_NO_LENGTH; } break; + case PSYC_PARSE_BODY_INCOMPLETE: case PSYC_PARSE_BODY: pname = &(packets[i].method); pvalue = &(packets[i].data); break; + case PSYC_PARSE_COMPLETE: printf("# Done parsing.\n"); - packets[i].flag = parsers[i].contentLengthFound ? PSYC_PACKET_NEED_LENGTH : PSYC_PACKET_NO_LENGTH; + packets[i].flag = parsers[i].contentLengthFound ? + PSYC_PACKET_NEED_LENGTH : PSYC_PACKET_NO_LENGTH; + psyc_setPacketLength(&packets[i]); psyc_render(&packets[i], sendbuf, SEND_BUF_SIZE); - if (send(i, sendbuf, packets[i].length, 0) == -1) { + + if (send(i, sendbuf, packets[i].length, 0) == -1) perror("send"); - } + ret = -1; break; case PSYC_PARSE_INSUFFICIENT: if (verbose) printf("# Insufficient data.\n"); + contbytes = parsers[i].buffer.length - parsers[i].cursor; - if (contbytes > 0) { // copy end of parsebuf before start of recvbuf + + if (contbytes > 0) // copy end of parsebuf before start of recvbuf + { assert(recvbuf - contbytes >= buf); // make sure it's still in the buffer memcpy(recvbuf - contbytes, parsebuf + parsers[i].cursor, contbytes); } ret = 0; break; + default: printf("# Error while parsing: %i\n", ret); ret = -1; } - switch (ret) { + switch (ret) + { case PSYC_PARSE_ENTITY_INCOMPLETE: case PSYC_PARSE_BODY_INCOMPLETE: ret = 0; case PSYC_PARSE_ENTITY: case PSYC_PARSE_ROUTING: case PSYC_PARSE_BODY: - if (oper) { + if (oper) + { mod->oper = oper; if (verbose) printf("%c", oper); } - if (name.length) { + if (name.length) + { pname->ptr = malloc(name.length); pname->length = name.length; + assert(pname->ptr != NULL); memcpy((void*)pname->ptr, name.ptr, name.length); name.length = 0; - if (verbose) { + + if (verbose) printf("%.*s = ", (int)pname->length, pname->ptr); - } } if (value.length) { @@ -281,51 +311,65 @@ int main(int argc, char **argv) memcpy((void*)pvalue->ptr + pvalue->length, value.ptr, value.length); pvalue->length += value.length; value.length = 0; - if (verbose) { + + if (verbose) + { printf("%.*s", (int)pvalue->length, pvalue->ptr); if (parsers[i].valueLength > pvalue->length) printf("..."); printf("\n"); } - } else if (verbose) { - printf("\n"); } + else if (verbose) + printf("\n"); if (verbose) - printf("\t\t\t\t\t\t\t\t# n:%ld v:%ld c:%ld r:%ld\n", pname->length, pvalue->length, parsers[i].contentParsed, parsers[i].routingLength); + printf("\t\t\t\t\t\t\t\t# n:%ld v:%ld c:%ld r:%ld\n", + pname->length, pvalue->length, + parsers[i].contentParsed, parsers[i].routingLength); } - switch (ret) { + switch (ret) + { case PSYC_PARSE_ROUTING: case PSYC_PARSE_ENTITY: - if (pname->length >= 5 && memcmp(pname->ptr, "_list", 5) == 0) { + if (pname->length >= 5 && memcmp(pname->ptr, "_list", 5) == 0) + { if (verbose) printf("## LIST START\n"); psyc_initParseListState(&listState); psyc_setParseListBuffer(&listState, *pvalue); - do { + do + { retl = psyc_parseList(&listState, pname, pvalue, &elem); - switch (retl) { + switch (retl) + { case PSYC_PARSE_LIST_END: retl = 0; case PSYC_PARSE_LIST_ELEM: - if (verbose) { + if (verbose) + { printf("|%.*s\n", (int)elem.length, elem.ptr); if (ret == PSYC_PARSE_LIST_END) printf("## LIST END"); } break; + default: printf("# Error while parsing list: %i\n", ret); ret = retl = -1; } - } while (retl > 0); + } + while (retl > 0); } } - } while (ret > 0); - if (ret < 0) { + } + while (ret > 0); + + if (ret < 0) + { printf("# Closing connection: %i\n", i); close(i); // bye! FD_CLR(i, &master); // remove from master set