1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00

fixes for inline functions, c99 mode, formatting

This commit is contained in:
Gabor Adam Toth 2011-05-04 01:00:35 +02:00
parent 570077f554
commit aaf480b782
14 changed files with 368 additions and 295 deletions

View file

@ -24,6 +24,9 @@
#define PSYC_EPOCH 1440444041 // 2015-08-24 21:20:41 CET (Monday) #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 typedef enum
{ {
PSYC_FALSE = 0, PSYC_FALSE = 0,
@ -79,28 +82,6 @@ typedef enum
PSYC_LIST_BINARY = 2, PSYC_LIST_BINARY = 2,
} psycListType; } 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 typedef struct
{ {
size_t length; size_t length;
@ -121,92 +102,18 @@ typedef struct
* *
* @return An instance of the psycString struct. * @return An instance of the psycString struct.
*/ */
inline psycString psyc_newString (const char *str, size_t strlen); static 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
{ {
char oper; // not call it 'operator' as C++ may not like that.. psycString s = {strlen, str};
psycString name; return s;
psycString value; }
psycModifierFlag flag;
} psycModifier;
typedef struct static inline
unsigned int psyc_version ()
{ {
size_t lines; return 1;
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);
/// Routing vars in alphabetical order. /// Routing vars in alphabetical order.
extern const psycString PSYC_routingVars[]; extern const psycString PSYC_routingVars[];

138
include/psyc/packet.h Normal file
View file

@ -0,0 +1,138 @@
#ifndef PSYC_PACKET_H
# define PSYC_PACKET_H
#include <psyc.h>
#include <psyc/syntax.h>
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

View file

@ -131,7 +131,7 @@ typedef struct
* @param state Pointer to the state struct that should be initiated. * @param state Pointer to the state struct that should be initiated.
*/ */
static inline static inline
void psyc_initParseState ( psycParseState* state ) void psyc_initParseState (psycParseState* state)
{ {
memset(state, 0, sizeof(psycParseState)); 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. * @param flags Flags to be set for the parser, see psycParseFlag.
*/ */
static inline static inline
void psyc_initParseState2 ( psycParseState* state, uint8_t flags ) void psyc_initParseState2 (psycParseState* state, uint8_t flags)
{ {
memset(state, 0, sizeof(psycParseState)); memset(state, 0, sizeof(psycParseState));
state->flags = flags; state->flags = flags;
@ -160,7 +160,7 @@ void psyc_initParseState2 ( psycParseState* state, uint8_t flags )
* @see psycString * @see psycString
*/ */
static inline static inline
void psyc_setParseBuffer ( psycParseState* state, psycString buffer ) void psyc_setParseBuffer (psycParseState* state, psycString buffer)
{ {
state->buffer = buffer; state->buffer = buffer;
state->cursor = 0; state->cursor = 0;
@ -181,7 +181,7 @@ void psyc_setParseBuffer ( psycParseState* state, psycString buffer )
* @see psycString * @see psycString
*/ */
static inline 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)); 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. * @param state Pointer to the list state struct that should be initiated.
*/ */
static inline static inline
void psyc_initParseListState ( psycParseListState* state ) void psyc_initParseListState (psycParseListState* state)
{ {
memset(state, 0, sizeof(psycParseListState)); 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. * Sets a new buffer in the list parser state struct with data to be parsed.
*/ */
static inline static inline
void psyc_setParseListBuffer ( psycParseListState* state, psycString buffer ) void psyc_setParseListBuffer (psycParseListState* state, psycString buffer)
{ {
state->buffer = buffer; state->buffer = buffer;
state->cursor = 0; state->cursor = 0;
} }
static inline 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)); psyc_setParseListBuffer(state, psyc_newString(buffer, length));
} }
static inline static inline
size_t psyc_getContentLength ( psycParseState* s ) size_t psyc_getContentLength (psycParseState* s)
{ {
return s->contentLength; 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 * @param value A pointer to a psycString. It will point to the
* value/body the variable/method and its length will be set accordingly * 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. * 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 #endif // PSYC_PARSER_H

View file

@ -1,6 +1,8 @@
#ifndef PSYC_RENDER_H #ifndef PSYC_RENDER_H
# define PSYC_RENDER_H # define PSYC_RENDER_H
#include <psyc/packet.h>
/** /**
* @file psyc/render.h * @file psyc/render.h
* @brief Interface for PSYC packet rendering. * @brief Interface for PSYC packet rendering.
@ -29,12 +31,12 @@ typedef enum
/** /**
* Render a PSYC packet into a buffer. * 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. * 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 #endif // PSYC_RENDER_H

View file

@ -93,4 +93,4 @@ void psyc_setTextBuffer2 (psycTextState* state,
* See also http://about.psyc.eu/psyctext * See also http://about.psyc.eu/psyctext
*/ */
psycTextRC psyc_text(psycTextState *state, psycTextCB getValue); psycTextRC psyc_text (psycTextState *state, psycTextCB getValue);

View file

@ -1,10 +1,10 @@
OPT = -O2 OPT = -O2
DEBUG = 2 DEBUG = 2
CFLAGS = -I../include -Wall ${OPT} CFLAGS = -I../include -Wall -std=c99 ${OPT}
DIET = diet DIET = diet
S = packet.c misc.c parser.c match.c render.c memmem.c itoa.c variable.c text.c S = packet.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 O = packet.o parser.o match.o render.o memmem.o itoa.o variable.o text.o
all: CC := ${WRAPPER} ${CC} all: CC := ${WRAPPER} ${CC}
all: lib all: lib

View file

@ -1,2 +0,0 @@
int psyc_version() { return 1; }

View file

@ -1,63 +1,11 @@
#include <psyc/lib.h> #include <psyc/lib.h>
#include <psyc/syntax.h> #include <psyc/syntax.h>
#include <psyc/packet.h>
#include <math.h> #include <math.h>
inline psycString psyc_newString(const char *str, size_t strlen) inline
{ psycListFlag psyc_checkListLength (psycList *list)
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)
{ {
psycListFlag flag = PSYC_LIST_NO_LENGTH; psycListFlag flag = PSYC_LIST_NO_LENGTH;
size_t i, length = 0; size_t i, length = 0;
@ -78,7 +26,8 @@ inline psycListFlag psyc_checkListLength(psycList *list)
return flag; return flag;
} }
inline psycListFlag psyc_getListLength(psycList *list) inline
psycListFlag psyc_getListLength (psycList *list)
{ {
size_t i, length = 0; size_t i, length = 0;
@ -100,7 +49,8 @@ inline psycListFlag psyc_getListLength(psycList *list)
return length; 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}; 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; 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) if (p->data.length == 1 && p->data.ptr[0] == C_GLYPH_PACKET_DELIMITER)
return PSYC_PACKET_NEED_LENGTH; return PSYC_PACKET_NEED_LENGTH;
@ -132,7 +97,8 @@ inline psycPacketFlag psyc_checkPacketLength(psycPacket *p)
return PSYC_PACKET_NO_LENGTH; return PSYC_PACKET_NO_LENGTH;
} }
inline size_t psyc_setPacketLength(psycPacket *p) inline
size_t psyc_setPacketLength(psycPacket *p)
{ {
size_t i; size_t i;
p->routingLength = 0; p->routingLength = 0;
@ -165,10 +131,10 @@ inline size_t psyc_setPacketLength(psycPacket *p)
return p->length; return p->length;
} }
inline psycPacket psyc_newPacket(psycHeader *routing, inline
psycHeader *entity, psycPacket psyc_newPacket (psycHeader *routing, psycHeader *entity,
psycString *method, psycString *data, psycString *method, psycString *data,
psycPacketFlag flag) psycPacketFlag flag)
{ {
psycPacket p = {*routing, *entity, *method, *data, 0, 0, flag}; psycPacket p = {*routing, *entity, *method, *data, 0, 0, flag};
@ -179,11 +145,12 @@ inline psycPacket psyc_newPacket(psycHeader *routing,
return p; return p;
} }
inline psycPacket psyc_newPacket2(psycModifier *routing, size_t routinglen, inline
psycModifier *entity, size_t entitylen, psycPacket psyc_newPacket2 (psycModifier *routing, size_t routinglen,
const char *method, size_t methodlen, psycModifier *entity, size_t entitylen,
const char *data, size_t datalen, const char *method, size_t methodlen,
psycPacketFlag flag) const char *data, size_t datalen,
psycPacketFlag flag)
{ {
psycHeader r = {routinglen, routing}; psycHeader r = {routinglen, routing};
psycHeader e = {entitylen, entity}; psycHeader e = {entitylen, entity};

View file

@ -20,7 +20,8 @@
* Determines if the argument is a glyph. * Determines if the argument is a glyph.
* Glyphs are: : = + - ? ! * Glyphs are: : = + - ? !
*/ */
inline char isGlyph(uint8_t g) static inline
char isGlyph (uint8_t g)
{ {
switch(g) switch(g)
{ {
@ -39,7 +40,8 @@ inline char isGlyph(uint8_t g)
/** /**
* Determines if the argument is numeric. * Determines if the argument is numeric.
*/ */
inline char isNumeric (uint8_t c) static inline
char isNumeric (uint8_t c)
{ {
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
@ -47,7 +49,8 @@ inline char isNumeric (uint8_t c)
/** /**
* Determines if the argument is alphanumeric. * Determines if the argument is alphanumeric.
*/ */
inline char isAlphaNumeric (uint8_t c) static inline
char isAlphaNumeric (uint8_t c)
{ {
return return
(c >= 'a' && c <= 'z') || (c >= 'a' && c <= 'z') ||
@ -59,7 +62,8 @@ inline char isAlphaNumeric (uint8_t c)
* Determines if the argument is a keyword character. * Determines if the argument is a keyword character.
* Keyword characters are: alphanumeric and _ * Keyword characters are: alphanumeric and _
*/ */
inline char isKwChar (uint8_t c) static inline
char isKwChar (uint8_t c)
{ {
return isAlphaNumeric(c) || c == '_'; return isAlphaNumeric(c) || c == '_';
} }
@ -69,7 +73,8 @@ inline char isKwChar (uint8_t c)
* It should contain one or more keyword characters. * It should contain one or more keyword characters.
* @return PSYC_PARSE_ERROR or PSYC_PARSE_SUCCESS * @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->ptr = state->buffer.ptr + state->cursor;
name->length = 0; name->length = 0;
@ -93,7 +98,9 @@ inline psycParseRC psyc_parseName (psycParseState* state, psycString* name)
* *
* @return PSYC_PARSE_COMPLETE or PSYC_PARSE_INCOMPLETE * @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; size_t remaining = *length - *parsed;
value->ptr = state->buffer.ptr + state->cursor; value->ptr = state->buffer.ptr + state->cursor;
@ -118,12 +125,14 @@ inline psycParseRC psyc_parseBinaryValue (psycParseState* state, psycString* val
* Parse simple or binary variable. * Parse simple or binary variable.
* @return PSYC_PARSE_ERROR or PSYC_PARSE_SUCCESS * @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); *oper = *(state->buffer.ptr + state->cursor);
PSYC_ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); 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) if (ret == PSYC_PARSE_ERROR)
return PSYC_PARSE_ERROR_MOD_NAME; return PSYC_PARSE_ERROR_MOD_NAME;
else if (ret != PSYC_PARSE_SUCCESS) else if (ret != PSYC_PARSE_SUCCESS)
@ -187,10 +196,12 @@ inline psycParseRC psyc_parseModifier (psycParseState* state, char* oper, psycSt
* Parse PSYC packets. * Parse PSYC packets.
* Generalized line-based parser. * 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 #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")) PP(("Invalid flag combination"))
#endif #endif
@ -333,7 +344,7 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc
case PSYC_PART_METHOD: case PSYC_PART_METHOD:
pos = state->cursor; pos = state->cursor;
ret = psyc_parseName(state, name); ret = psyc_parseKeyword(state, name);
if (ret == PSYC_PARSE_INSUFFICIENT) if (ret == PSYC_PARSE_INSUFFICIENT)
return ret; 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->contentLengthFound) // We know the length of the packet.
{ {
if (state->contentParsed < state->contentLength && 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; return PSYC_PARSE_BODY_INCOMPLETE;
if (state->cursor >= state->buffer.length) if (state->cursor >= state->buffer.length)
@ -437,7 +449,8 @@ psycParseRC psyc_parse(psycParseState* state, char* oper, psycString* name, psyc
* List value parser. * List value parser.
* @return see psycListRC. * @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) if (state->cursor >= state->buffer.length)
return PSYC_PARSE_LIST_INCOMPLETE; return PSYC_PARSE_LIST_INCOMPLETE;
@ -508,7 +521,8 @@ psycParseListRC psyc_parseList(psycParseListState* state, psycString *name, psyc
// Start or resume parsing the binary data // Start or resume parsing the binary data
if (state->elemParsed < state->elemLength) 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; return PSYC_PARSE_LIST_INCOMPLETE;
state->elemLength = 0; state->elemLength = 0;

View file

@ -2,7 +2,7 @@
#include "psyc/render.h" #include "psyc/render.h"
#include "psyc/syntax.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; size_t i, cur = 0;
psycString *elem; psycString *elem;
@ -37,7 +37,8 @@ psycRenderRC psyc_renderList(psycList *list, char *buffer, size_t buflen)
return PSYC_RENDER_SUCCESS; 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; size_t cur = 0;
@ -58,7 +59,7 @@ inline size_t psyc_renderModifier(psycModifier *mod, char *buffer)
return cur; 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; size_t i, cur = 0;

View file

@ -1,6 +1,6 @@
OPT = -O2 OPT = -O2
DEBUG = 2 DEBUG = 2
CFLAGS = -I../include -Wall ${OPT} CFLAGS = -I../include -Wall -std=c99 ${OPT}
LDFLAGS = -L../lib LDFLAGS = -L../lib
LOADLIBES = -lpsyc -lm LOADLIBES = -lpsyc -lm
TARGETS = testServer testParser testMatch testRender testText isRoutingVar getVarType TARGETS = testServer testParser testMatch testRender testText isRoutingVar getVarType
@ -20,10 +20,13 @@ debug: all
test: ${TARGETS} test: ${TARGETS}
./testRender ./testRender
./testMatch ./testMatch
./testText
./isRoutingVar ./isRoutingVar
./getVarType ./getVarType
for f in packets/full-*; do echo ">> $$f"; ./testParser $$f; done for f in packets/full-*; do echo ">> $$f"; ./testParser $$f; done
testServer: CFLAGS := $(subst -std=c99,,${CFLAGS})
netstart: netstart:
./testServer ${PORT} ./testServer ${PORT}

View file

@ -2,10 +2,10 @@
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <psyc/lib.h> #include <psyc.h>
#include <psyc/parser.h> #include <psyc/parser.h>
int main(int argc, char **argv) int main (int argc, char **argv)
{ {
int idx, ret, routing_only = argc > 2, verbose = argc > 3; int idx, ret, routing_only = argc > 2, verbose = argc > 3;
char buffer[2048], oper; char buffer[2048], oper;
@ -19,9 +19,9 @@ int main(int argc, char **argv)
idx = read(file,(void*)buffer,sizeof(buffer)); idx = read(file,(void*)buffer,sizeof(buffer));
if (verbose) { if (verbose) {
write(1, ">> INPUT\n", 9); printf(">> INPUT\n");
write(1, buffer, idx); printf("%.*s\n", (int)idx, buffer);
write(1, ">> PARSE\n", 9); printf(">> PARSE\n");
} }
if (routing_only) if (routing_only)
@ -32,9 +32,8 @@ int main(int argc, char **argv)
psyc_setParseBuffer(&state, psyc_newString(buffer, idx)); psyc_setParseBuffer(&state, psyc_newString(buffer, idx));
// try parsing that now // try parsing that now
// while ((ret = psyc_parse(&state, &oper, &name, &value))) do
// { {
do {
oper = 0; oper = 0;
name.length = 0; name.length = 0;
value.length = 0; value.length = 0;
@ -42,24 +41,24 @@ int main(int argc, char **argv)
ret = psyc_parse(&state, &oper, &name, &value); ret = psyc_parse(&state, &oper, &name, &value);
if (verbose) if (verbose)
printf(">> ret = %d\n", ret); printf(">> ret = %d\n", ret);
switch (ret) switch (ret)
{ {
case PSYC_PARSE_ROUTING: case PSYC_PARSE_ROUTING:
case PSYC_PARSE_ENTITY: case PSYC_PARSE_ENTITY:
if (verbose) if (verbose)
write(1, &oper, 1); printf("%c", oper);
case PSYC_PARSE_BODY: case PSYC_PARSE_BODY:
// printf("the string is '%.*s'\n", name); // printf("the string is '%.*s'\n", name);
if (verbose) { if (verbose)
write(1, name.ptr, name.length); printf("%.*s = %.*s\n",
write(1, " = ", 3); (int)name.length, name.ptr,
write(1, value.ptr, value.length); (int)value.length, value.ptr);
write(1, "\n", 1);
}
if (memcmp(name.ptr, "_list", 5) == 0) if (memcmp(name.ptr, "_list", 5) == 0)
{ {
if (verbose) if (verbose)
write(1, ">>> LIST START\n", 15); printf(">> LIST START\n");
psyc_initParseListState(&listState); psyc_initParseListState(&listState);
psyc_setParseListBuffer(&listState, value); psyc_setParseListBuffer(&listState, value);
@ -70,11 +69,8 @@ int main(int argc, char **argv)
{ {
case PSYC_PARSE_LIST_END: case PSYC_PARSE_LIST_END:
case PSYC_PARSE_LIST_ELEM: case PSYC_PARSE_LIST_ELEM:
if (verbose) { if (verbose)
write(1, "|", 1); printf("|%.*s\n", (int)elem.length, elem.ptr);
write(1, elem.ptr, elem.length);
write(1, "\n", 1);
}
break; break;
default: default:
printf("Error while parsing list: %i\n", ret); 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 (ret == PSYC_PARSE_LIST_END)
{ {
if (verbose) if (verbose)
write(1, ">>> LIST END\n", 13); printf(">> LIST END\n");
break; break;
} }
} }
@ -101,7 +97,8 @@ int main(int argc, char **argv)
printf("Error while parsing: %i\n", ret); printf("Error while parsing: %i\n", ret);
return 1; return 1;
} }
} while (ret); }
while (ret);
return 0; return 0;
} }

View file

@ -1,13 +1,15 @@
#include <stdlib.h> #include <stdio.h>
#include <unistd.h>
#include "../include/psyc/lib.h" #include <psyc/lib.h>
#include "../include/psyc/render.h" #include <psyc/render.h>
#include "../include/psyc/syntax.h" #include <psyc/syntax.h>
#define myUNI "psyc://10.100.1000/~ludwig" #define myUNI "psyc://10.100.1000/~ludwig"
/* example renderer generating a presence packet */ /* 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[] = { psycModifier routing[] = {
psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_context"), PSYC_C2ARG(myUNI), 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]; char buffer[512];
psyc_render(&packet, buffer, sizeof(buffer)); psyc_render(&packet, buffer, sizeof(buffer));
if (verbose) if (verbose)
write(0, buffer, packet.length); printf("%.*s\n", (int)packet.length, buffer);
return strncmp(rendered, buffer, packet.length); return strncmp(rendered, buffer, packet.length);
} }
int testList(const char *rendered, uint8_t verbose) int testList (const char *rendered, uint8_t verbose)
{ {
psycModifier routing[] = { psycModifier routing[] = {
psyc_newModifier2(C_GLYPH_OPERATOR_SET, PSYC_C2ARG("_source"), PSYC_C2ARG(myUNI), 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]; char buffer[512];
psyc_render(&packet, buffer, sizeof(buffer)); psyc_render(&packet, buffer, sizeof(buffer));
if (verbose) if (verbose)
write(0, buffer, packet.length); printf("%.*s\n", (int)packet.length, buffer);
return strncmp(rendered, buffer, packet.length); return strncmp(rendered, buffer, packet.length);
} }
int main(int argc, char **argv) { int main (int argc, char **argv) {
uint8_t verbose = argc > 1; uint8_t verbose = argc > 1;
if (testPresence(PSYC_C2ARG("_here"), PSYC_C2ARG("I'm omnipresent right now"), "\ if (testPresence(PSYC_C2ARG("_here"), PSYC_C2ARG("I'm omnipresent right now"), "\

View file

@ -31,16 +31,15 @@ const size_t ROUTING_LINES = 16;
const size_t ENTITY_LINES = 32; const size_t ENTITY_LINES = 32;
// get sockaddr, IPv4 or IPv6: // 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_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_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"; char *port = argc > 1 ? argv[1] : "4440";
uint8_t routing_only = argc > 2, verbose = argc > 3; uint8_t routing_only = argc > 2, verbose = argc > 3;
@ -71,12 +70,12 @@ int main(int argc, char **argv)
psycPacket packets[NUM_PARSERS]; psycPacket packets[NUM_PARSERS];
psycModifier routing[NUM_PARSERS][ROUTING_LINES]; psycModifier routing[NUM_PARSERS][ROUTING_LINES];
psycModifier entity[NUM_PARSERS][ENTITY_LINES]; psycModifier entity[NUM_PARSERS][ENTITY_LINES];
psycModifier *mod; psycModifier *mod = NULL;
int ret, retl; int ret, retl;
char oper; char oper;
psycString name, value, elem; psycString name, value, elem;
psycString *pname, *pvalue; psycString *pname = NULL, *pvalue = NULL;
psycParseListState listState; psycParseListState listState;
FD_ZERO(&master); // clear the master and temp sets 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_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE; 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)); fprintf(stderr, "error: %s\n", gai_strerror(rv));
exit(1); 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); listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (listener < 0) { if (listener < 0)
continue; continue;
}
// lose the pesky "address already in use" error message // lose the pesky "address already in use" error message
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int)); 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); close(listener);
continue; continue;
} }
@ -110,7 +111,8 @@ int main(int argc, char **argv)
} }
// if we got here, it means we didn't get bound // if we got here, it means we didn't get bound
if (p == NULL) { if (p == NULL)
{
fprintf(stderr, "failed to bind\n"); fprintf(stderr, "failed to bind\n");
exit(2); exit(2);
} }
@ -118,7 +120,8 @@ int main(int argc, char **argv)
freeaddrinfo(ai); // all done with this freeaddrinfo(ai); // all done with this
// listen // listen
if (listen(listener, 10) == -1) { if (listen(listener, 10) == -1)
{
perror("listen"); perror("listen");
exit(3); exit(3);
} }
@ -130,17 +133,22 @@ int main(int argc, char **argv)
fdmax = listener; // so far, it's this one fdmax = listener; // so far, it's this one
// main loop // main loop
for (;;) { for (;;)
{
read_fds = master; // copy it 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"); perror("select");
exit(4); exit(4);
} }
// run through the existing connections looking for data to read // run through the existing connections looking for data to read
for (i = 0; i <= fdmax; i++) { for (i = 0; i <= fdmax; i++)
if (FD_ISSET(i, &read_fds)) { // we got one!! {
if (i == listener) { if (FD_ISSET(i, &read_fds)) // we got one!!
{
if (i == listener)
{
// handle new connections // handle new connections
if (fdmax == NUM_PARSERS - 1) if (fdmax == NUM_PARSERS - 1)
continue; // ignore if there's too many continue; // ignore if there's too many
@ -148,13 +156,13 @@ int main(int argc, char **argv)
addrlen = sizeof remoteaddr; addrlen = sizeof remoteaddr;
newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen); newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen);
if (newfd == -1) { if (newfd == -1)
perror("accept"); perror("accept");
} else { else
{
FD_SET(newfd, &master); // add to master set 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; fdmax = newfd;
}
// reset parser state & packet // reset parser state & packet
if (routing_only) if (routing_only)
@ -174,19 +182,23 @@ int main(int argc, char **argv)
remoteIP, INET6_ADDRSTRLEN), remoteIP, INET6_ADDRSTRLEN),
newfd); newfd);
} }
} else { }
else
{
// handle data from a client // 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 // got error or connection closed by client
if (nbytes == 0) { if (nbytes == 0) // connection closed
// connection closed
printf("socket %d hung up\n", i); printf("socket %d hung up\n", i);
} else { else
perror("recv"); perror("recv");
}
close(i); // bye! close(i); // bye!
FD_CLR(i, &master); // remove from master set FD_CLR(i, &master); // remove from master set
} else { }
else
{
// we got some data from a client // we got some data from a client
parsebuf = recvbuf - contbytes; parsebuf = recvbuf - contbytes;
psyc_setParseBuffer(&parsers[i], psyc_newString(parsebuf, contbytes + nbytes)); psyc_setParseBuffer(&parsers[i], psyc_newString(parsebuf, contbytes + nbytes));
@ -195,7 +207,8 @@ int main(int argc, char **argv)
name.length = 0; name.length = 0;
value.length = 0; value.length = 0;
do { do
{
ret = psyc_parse(&parsers[i], &oper, &name, &value); ret = psyc_parse(&parsers[i], &oper, &name, &value);
if (verbose) if (verbose)
printf("# ret = %d\n", ret); printf("# ret = %d\n", ret);
@ -209,69 +222,86 @@ int main(int argc, char **argv)
mod->flag = PSYC_MODIFIER_ROUTING; mod->flag = PSYC_MODIFIER_ROUTING;
packets[i].routing.lines++; packets[i].routing.lines++;
break; break;
case PSYC_PARSE_ENTITY_INCOMPLETE: case PSYC_PARSE_ENTITY_INCOMPLETE:
case PSYC_PARSE_ENTITY: case PSYC_PARSE_ENTITY:
assert(packets[i].entity.lines < ENTITY_LINES); assert(packets[i].entity.lines < ENTITY_LINES);
mod = &(packets[i].entity.modifiers[packets[i].entity.lines]); mod = &(packets[i].entity.modifiers[packets[i].entity.lines]);
pname = &mod->name; pname = &mod->name;
pvalue = &mod->value; pvalue = &mod->value;
if (ret == PSYC_PARSE_ENTITY) {
if (ret == PSYC_PARSE_ENTITY)
{
packets[i].entity.lines++; 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; break;
case PSYC_PARSE_BODY_INCOMPLETE: case PSYC_PARSE_BODY_INCOMPLETE:
case PSYC_PARSE_BODY: case PSYC_PARSE_BODY:
pname = &(packets[i].method); pname = &(packets[i].method);
pvalue = &(packets[i].data); pvalue = &(packets[i].data);
break; break;
case PSYC_PARSE_COMPLETE: case PSYC_PARSE_COMPLETE:
printf("# Done parsing.\n"); 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_setPacketLength(&packets[i]);
psyc_render(&packets[i], sendbuf, SEND_BUF_SIZE); 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"); perror("send");
}
ret = -1; ret = -1;
break; break;
case PSYC_PARSE_INSUFFICIENT: case PSYC_PARSE_INSUFFICIENT:
if (verbose) if (verbose)
printf("# Insufficient data.\n"); printf("# Insufficient data.\n");
contbytes = parsers[i].buffer.length - parsers[i].cursor; 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 assert(recvbuf - contbytes >= buf); // make sure it's still in the buffer
memcpy(recvbuf - contbytes, parsebuf + parsers[i].cursor, contbytes); memcpy(recvbuf - contbytes, parsebuf + parsers[i].cursor, contbytes);
} }
ret = 0; ret = 0;
break; break;
default: default:
printf("# Error while parsing: %i\n", ret); printf("# Error while parsing: %i\n", ret);
ret = -1; ret = -1;
} }
switch (ret) { switch (ret)
{
case PSYC_PARSE_ENTITY_INCOMPLETE: case PSYC_PARSE_ENTITY_INCOMPLETE:
case PSYC_PARSE_BODY_INCOMPLETE: case PSYC_PARSE_BODY_INCOMPLETE:
ret = 0; ret = 0;
case PSYC_PARSE_ENTITY: case PSYC_PARSE_ENTITY:
case PSYC_PARSE_ROUTING: case PSYC_PARSE_ROUTING:
case PSYC_PARSE_BODY: case PSYC_PARSE_BODY:
if (oper) { if (oper)
{
mod->oper = oper; mod->oper = oper;
if (verbose) if (verbose)
printf("%c", oper); printf("%c", oper);
} }
if (name.length) { if (name.length)
{
pname->ptr = malloc(name.length); pname->ptr = malloc(name.length);
pname->length = name.length; pname->length = name.length;
assert(pname->ptr != NULL); assert(pname->ptr != NULL);
memcpy((void*)pname->ptr, name.ptr, name.length); memcpy((void*)pname->ptr, name.ptr, name.length);
name.length = 0; name.length = 0;
if (verbose) {
if (verbose)
printf("%.*s = ", (int)pname->length, pname->ptr); printf("%.*s = ", (int)pname->length, pname->ptr);
}
} }
if (value.length) { if (value.length) {
@ -281,51 +311,65 @@ int main(int argc, char **argv)
memcpy((void*)pvalue->ptr + pvalue->length, value.ptr, value.length); memcpy((void*)pvalue->ptr + pvalue->length, value.ptr, value.length);
pvalue->length += value.length; pvalue->length += value.length;
value.length = 0; value.length = 0;
if (verbose) {
if (verbose)
{
printf("%.*s", (int)pvalue->length, pvalue->ptr); printf("%.*s", (int)pvalue->length, pvalue->ptr);
if (parsers[i].valueLength > pvalue->length) if (parsers[i].valueLength > pvalue->length)
printf("..."); printf("...");
printf("\n"); printf("\n");
} }
} else if (verbose) {
printf("\n");
} }
else if (verbose)
printf("\n");
if (verbose) 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_ROUTING:
case PSYC_PARSE_ENTITY: 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) if (verbose)
printf("## LIST START\n"); printf("## LIST START\n");
psyc_initParseListState(&listState); psyc_initParseListState(&listState);
psyc_setParseListBuffer(&listState, *pvalue); psyc_setParseListBuffer(&listState, *pvalue);
do { do
{
retl = psyc_parseList(&listState, pname, pvalue, &elem); retl = psyc_parseList(&listState, pname, pvalue, &elem);
switch (retl) { switch (retl)
{
case PSYC_PARSE_LIST_END: case PSYC_PARSE_LIST_END:
retl = 0; retl = 0;
case PSYC_PARSE_LIST_ELEM: case PSYC_PARSE_LIST_ELEM:
if (verbose) { if (verbose)
{
printf("|%.*s\n", (int)elem.length, elem.ptr); printf("|%.*s\n", (int)elem.length, elem.ptr);
if (ret == PSYC_PARSE_LIST_END) if (ret == PSYC_PARSE_LIST_END)
printf("## LIST END"); printf("## LIST END");
} }
break; break;
default: default:
printf("# Error while parsing list: %i\n", ret); printf("# Error while parsing list: %i\n", ret);
ret = retl = -1; 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); printf("# Closing connection: %i\n", i);
close(i); // bye! close(i); // bye!
FD_CLR(i, &master); // remove from master set FD_CLR(i, &master); // remove from master set