1
0
Fork 0
mirror of git://git.psyc.eu/libpsyc synced 2024-08-15 03:19:02 +00:00
This commit is contained in:
tg(x) 2011-11-11 22:18:24 +01:00
parent aee9203df6
commit 1cc58abd0e
31 changed files with 2797 additions and 2759 deletions

View file

@ -1,6 +1,9 @@
.PHONY: doc test bench .PHONY: doc test bench
.NOTPARALLEL: clean .NOTPARALLEL: clean
indent_args = -nbad -bap -bbo -nbc -br -brs -ncdb -cdw -ce -ci4 -cli0 -cs -d0 -di1 \
-nfc1 -nfca -hnl -i4 -ip0 -l80 -lp -npcs -nprs -npsl -saf -sai -saw -nsc -nsob -nss
all: all:
${MAKE} -C src ${MAKE} -C src

View file

@ -15,6 +15,7 @@
// * @subsection step1 Step 1: Opening the box // * @subsection step1 Step 1: Opening the box
#ifndef PSYC_H #ifndef PSYC_H
#define PSYC_H
#include <stdint.h> #include <stdint.h>
#include <string.h> #include <string.h>
@ -33,8 +34,7 @@
#define PSYC_NUM_ELEM(a) (sizeof(a) / sizeof(*(a))) #define PSYC_NUM_ELEM(a) (sizeof(a) / sizeof(*(a)))
/// Boolean: true/false, yes/no. /// Boolean: true/false, yes/no.
typedef enum typedef enum {
{
PSYC_FALSE = 0, PSYC_FALSE = 0,
PSYC_TRUE = 1, PSYC_TRUE = 1,
PSYC_NO = 0, PSYC_NO = 0,
@ -42,15 +42,13 @@ typedef enum
} PsycBool; } PsycBool;
/// Return code: OK/error. /// Return code: OK/error.
typedef enum typedef enum {
{
PSYC_OK = 1, PSYC_OK = 1,
PSYC_ERROR = -1, PSYC_ERROR = -1,
} PsycRC; } PsycRC;
/// PSYC packet parts. /// PSYC packet parts.
typedef enum typedef enum {
{
PSYC_PART_RESET = -1, PSYC_PART_RESET = -1,
PSYC_PART_ROUTING = 0, PSYC_PART_ROUTING = 0,
PSYC_PART_LENGTH = 1, PSYC_PART_LENGTH = 1,
@ -68,8 +66,7 @@ typedef enum
* validity. Other variable types are treated * validity. Other variable types are treated
* as opaque data. * as opaque data.
*/ */
typedef enum typedef enum {
{
PSYC_TYPE_UNKNOWN, PSYC_TYPE_UNKNOWN,
PSYC_TYPE_AMOUNT, PSYC_TYPE_AMOUNT,
PSYC_TYPE_COLOR, PSYC_TYPE_COLOR,
@ -89,8 +86,7 @@ typedef enum
* List types. * List types.
* Possible types are text and binary. * Possible types are text and binary.
*/ */
typedef enum typedef enum {
{
PSYC_LIST_TEXT = 1, PSYC_LIST_TEXT = 1,
PSYC_LIST_BINARY = 2, PSYC_LIST_BINARY = 2,
} PsycListType; } PsycListType;
@ -100,22 +96,19 @@ typedef enum
* *
* Contains pointer and length for a buffer. * Contains pointer and length for a buffer.
*/ */
typedef struct typedef struct {
{
/// Length of the data pointed to by ptr /// Length of the data pointed to by ptr
size_t length; size_t length;
/// pointer to the data /// pointer to the data
char *data; char *data;
} PsycString; } PsycString;
typedef struct typedef struct {
{
PsycString key; PsycString key;
void *value; void *value;
} PsycDict; } PsycDict;
typedef struct typedef struct {
{
PsycString key; PsycString key;
intptr_t value; intptr_t value;
} PsycDictInt; } PsycDictInt;
@ -123,14 +116,14 @@ typedef struct
/** /**
* Checks if long keyword string inherits from short keyword string. * Checks if long keyword string inherits from short keyword string.
*/ */
int psyc_inherits (char *sho, size_t slen, int
char *lon, size_t llen); psyc_inherits (char *sho, size_t slen, char *lon, size_t llen);
/** /**
* Checks if short keyword string matches long keyword string. * Checks if short keyword string matches long keyword string.
*/ */
int psyc_matches (char *sho, size_t slen, int
char *lon, size_t llen); psyc_matches (char *sho, size_t slen, char *lon, size_t llen);
/** /**
* Look up value associated with a key in a dictionary. * Look up value associated with a key in a dictionary.
@ -147,7 +140,8 @@ int psyc_matches (char *sho, size_t slen,
* @return The value of the entry if found, or NULL if not found. * @return The value of the entry if found, or NULL if not found.
*/ */
void * psyc_dict_lookup (const PsycDict *dict, size_t size, void *
psyc_dict_lookup (const PsycDict *dict, size_t size,
const char *key, size_t keylen, const char *key, size_t keylen,
PsycBool inherit, int8_t *tmp); PsycBool inherit, int8_t *tmp);
@ -155,17 +149,15 @@ void * psyc_dict_lookup (const PsycDict *dict, size_t size,
* Look up value associated with a key in a dictionary of integers. * Look up value associated with a key in a dictionary of integers.
* @see psyc_dict_lookup * @see psyc_dict_lookup
*/ */
static inline static inline intptr_t
intptr_t psyc_dict_lookup_int (const PsycDictInt *dict, size_t size, psyc_dict_lookup_int (const PsycDictInt * dict, size_t size,
const char *key, size_t keylen, const char *key, size_t keylen,
PsycBool inherit, int8_t *tmp) PsycBool inherit, int8_t *tmp)
{ {
return (intptr_t) psyc_dict_lookup((PsycDict *)dict, size, key, keylen, inherit, tmp); return (intptr_t) psyc_dict_lookup((PsycDict *) dict, size, key, keylen,
inherit, tmp);
} }
#include "psyc/variable.h" #include "psyc/variable.h"
#define PSYC_H
#endif #endif

View file

@ -1,4 +1,5 @@
#ifndef PSYC_PACKET_H #ifndef PSYC_PACKET_H
#define PSYC_PACKET_H
/** /**
* @file psyc/packet.h * @file psyc/packet.h
@ -20,8 +21,7 @@
#include <math.h> #include <math.h>
/** Modifier flags. */ /** Modifier flags. */
typedef enum typedef enum {
{
/// Modifier needs to be checked if it needs length. /// Modifier needs to be checked if it needs length.
PSYC_MODIFIER_CHECK_LENGTH = 0, PSYC_MODIFIER_CHECK_LENGTH = 0,
/// Modifier needs length. /// Modifier needs length.
@ -33,8 +33,7 @@ typedef enum
} PsycModifierFlag; } PsycModifierFlag;
/** List flags. */ /** List flags. */
typedef enum typedef enum {
{
/// List needs to be checked if it needs length. /// List needs to be checked if it needs length.
PSYC_LIST_CHECK_LENGTH = 0, PSYC_LIST_CHECK_LENGTH = 0,
/// List needs length. /// List needs length.
@ -44,8 +43,7 @@ typedef enum
} PsycListFlag; } PsycListFlag;
/** Packet flags. */ /** Packet flags. */
typedef enum typedef enum {
{
/// Packet needs to be checked if it needs content length. /// Packet needs to be checked if it needs content length.
PSYC_PACKET_CHECK_LENGTH = 0, PSYC_PACKET_CHECK_LENGTH = 0,
/// Packet needs content length. /// Packet needs content length.
@ -54,8 +52,7 @@ typedef enum
PSYC_PACKET_NO_LENGTH = 2, PSYC_PACKET_NO_LENGTH = 2,
} PsycPacketFlag; } PsycPacketFlag;
typedef enum typedef enum {
{
PSYC_OPERATOR_SET = ':', PSYC_OPERATOR_SET = ':',
PSYC_OPERATOR_ASSIGN = '=', PSYC_OPERATOR_ASSIGN = '=',
PSYC_OPERATOR_AUGMENT = '+', PSYC_OPERATOR_AUGMENT = '+',
@ -63,16 +60,14 @@ typedef enum
PSYC_OPERATOR_QUERY = '?', PSYC_OPERATOR_QUERY = '?',
} PsycOperator; } PsycOperator;
typedef enum typedef enum {
{
PSYC_STATE_NOOP = 0, PSYC_STATE_NOOP = 0,
PSYC_STATE_RESET = '=', PSYC_STATE_RESET = '=',
PSYC_STATE_RESYNC = '?', PSYC_STATE_RESYNC = '?',
} PsycStateOp; } PsycStateOp;
/** Structure for a modifier. */ /** Structure for a modifier. */
typedef struct typedef struct {
{
char oper; char oper;
PsycString name; PsycString name;
PsycString value; PsycString value;
@ -80,15 +75,13 @@ typedef struct
} PsycModifier; } PsycModifier;
/** Structure for an entity or routing header. */ /** Structure for an entity or routing header. */
typedef struct typedef struct {
{
size_t lines; size_t lines;
PsycModifier *modifiers; PsycModifier *modifiers;
} PsycHeader; } PsycHeader;
/** Structure for a list. */ /** Structure for a list. */
typedef struct typedef struct {
{
size_t num_elems; size_t num_elems;
PsycString *elems; PsycString *elems;
size_t length; size_t length;
@ -96,8 +89,7 @@ typedef struct
} PsycList; } PsycList;
/** Intermediate struct for a PSYC packet */ /** Intermediate struct for a PSYC packet */
typedef struct typedef struct {
{
PsycHeader routing; ///< Routing header. PsycHeader routing; ///< Routing header.
PsycHeader entity; ///< Entity header. PsycHeader entity; ///< Entity header.
char stateop; ///< State operation. @see PsycStateOp char stateop; ///< State operation. @see PsycStateOp
@ -113,8 +105,8 @@ typedef struct
/** /**
* Return the number of digits a number has in its base 10 representation. * Return the number of digits a number has in its base 10 representation.
*/ */
static inline static inline size_t
size_t psyc_num_length (size_t n) psyc_num_length (size_t n)
{ {
return n < 10 ? 1 : log10(n) + 1; return n < 10 ? 1 : log10(n) + 1;
} }
@ -123,14 +115,14 @@ size_t psyc_num_length (size_t n)
* \internal * \internal
* Check if a modifier needs length. * Check if a modifier needs length.
*/ */
static inline static inline PsycModifierFlag
PsycModifierFlag psyc_modifier_length_check (PsycModifier *m) psyc_modifier_length_check (PsycModifier *m)
{ {
PsycModifierFlag flag; PsycModifierFlag flag;
if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD) if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
flag = PSYC_MODIFIER_NEED_LENGTH; flag = PSYC_MODIFIER_NEED_LENGTH;
else if (memchr(m->value.data, (int)'\n', m->value.length)) else if (memchr(m->value.data, (int) '\n', m->value.length))
flag = PSYC_MODIFIER_NEED_LENGTH; flag = PSYC_MODIFIER_NEED_LENGTH;
else else
flag = PSYC_MODIFIER_NO_LENGTH; flag = PSYC_MODIFIER_NO_LENGTH;
@ -139,11 +131,10 @@ PsycModifierFlag psyc_modifier_length_check (PsycModifier *m)
} }
/** Initialize modifier */ /** Initialize modifier */
static inline static inline void
void psyc_modifier_init (PsycModifier *m, char oper, psyc_modifier_init (PsycModifier *m, char oper,
char *name, size_t namelen, char *name, size_t namelen,
char *value, size_t valuelen, char *value, size_t valuelen, PsycModifierFlag flag)
PsycModifierFlag flag)
{ {
*m = (PsycModifier) {oper, {namelen, name}, {valuelen, value}, flag}; *m = (PsycModifier) {oper, {namelen, name}, {valuelen, value}, flag};
@ -155,37 +146,44 @@ void psyc_modifier_init (PsycModifier *m, char oper,
* \internal * \internal
* Get the total length of a modifier when rendered. * Get the total length of a modifier when rendered.
*/ */
size_t psyc_modifier_length (PsycModifier *m); size_t
psyc_modifier_length (PsycModifier *m);
/** /**
* \internal * \internal
* Check if a list needs length. * Check if a list needs length.
*/ */
PsycListFlag psyc_list_length_check (PsycList *list); PsycListFlag
psyc_list_length_check (PsycList *list);
/** /**
* \internal * \internal
* Get the total length of a list when rendered. * Get the total length of a list when rendered.
*/ */
PsycListFlag psyc_list_length (PsycList *list); PsycListFlag
psyc_list_length (PsycList *list);
/** /**
* \internal * \internal
* Check if a packet needs length. * Check if a packet needs length.
*/ */
PsycPacketFlag psyc_packet_length_check (PsycPacket *p); PsycPacketFlag
psyc_packet_length_check (PsycPacket *p);
/** /**
* Calculate and set the rendered length of packet parts and total packet length. * Calculate and set the rendered length of packet parts and total packet length.
*/ */
size_t psyc_packet_length_set (PsycPacket *p); size_t
psyc_packet_length_set (PsycPacket *p);
/** Initialize list. */ /** Initialize list. */
void psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems, void
psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
PsycListFlag flag); PsycListFlag flag);
/** Initialize packet. */ /** Initialize packet. */
void psyc_packet_init (PsycPacket *packet, void
psyc_packet_init (PsycPacket *packet,
PsycModifier *routing, size_t routinglen, PsycModifier *routing, size_t routinglen,
PsycModifier *entity, size_t entitylen, PsycModifier *entity, size_t entitylen,
char *method, size_t methodlen, char *method, size_t methodlen,
@ -193,12 +191,12 @@ void psyc_packet_init (PsycPacket *packet,
char stateop, PsycPacketFlag flag); char stateop, PsycPacketFlag flag);
/** Initialize packet with raw content. */ /** Initialize packet with raw content. */
void psyc_packet_init_raw (PsycPacket *packet, void
psyc_packet_init_raw (PsycPacket *packet,
PsycModifier *routing, size_t routinglen, PsycModifier *routing, size_t routinglen,
char *content, size_t contentlen, char *content, size_t contentlen,
PsycPacketFlag flag); PsycPacketFlag flag);
/** @} */ // end of packet group /** @} */ // end of packet group
#define PSYC_PACKET_H
#endif #endif

View file

@ -1,4 +1,5 @@
#ifndef PSYC_PARSE_H #ifndef PSYC_PARSE_H
#define PSYC_PARSE_H
/** /**
* @file psyc/parse.h * @file psyc/parse.h
@ -224,7 +225,7 @@ typedef struct {
size_t routingLength; ///< Length of routing part parsed so far. size_t routingLength; ///< Length of routing part parsed so far.
size_t contentParsed; ///< Number of bytes parsed from the content so far. size_t contentParsed; ///< Number of bytes parsed from the content so far.
size_t contentLength; ///< Expected length of the content. size_t contentLength; ///< Expected length of the content.
PsycBool contentLengthFound; ///< Is there a length given for this packet? PsycBool contentLengthFound;///< Is there a length given for this packet?
size_t valueParsed; ///< Number of bytes parsed from the value so far. size_t valueParsed; ///< Number of bytes parsed from the value so far.
size_t valueLength; ///< Expected length of the value. size_t valueLength; ///< Expected length of the value.
PsycBool valueLengthFound; ///< Is there a length given for this modifier? PsycBool valueLengthFound; ///< Is there a length given for this modifier?
@ -250,8 +251,8 @@ typedef struct {
* @param flags Flags to be set for the parser, see PsycParseFlag. * @param flags Flags to be set for the parser, see PsycParseFlag.
* @see PsycParseFlag * @see PsycParseFlag
*/ */
static inline static inline void
void psyc_parse_state_init (PsycParseState *state, uint8_t flags) psyc_parse_state_init (PsycParseState *state, uint8_t flags)
{ {
memset(state, 0, sizeof(PsycParseState)); memset(state, 0, sizeof(PsycParseState));
state->flags = flags; state->flags = flags;
@ -271,8 +272,9 @@ void psyc_parse_state_init (PsycParseState *state, uint8_t flags)
* @param length length of the data in bytes * @param length length of the data in bytes
* @see PsycString * @see PsycString
*/ */
static inline static inline void
void psyc_parse_buffer_set (PsycParseState *state, char *buffer, size_t length) psyc_parse_buffer_set (PsycParseState *state, char *buffer,
size_t length)
{ {
state->buffer = (PsycString) {length, buffer}; state->buffer = (PsycString) {length, buffer};
state->cursor = 0; state->cursor = 0;
@ -288,8 +290,8 @@ void psyc_parse_buffer_set (PsycParseState *state, char *buffer, size_t length)
* *
* @param state Pointer to the list state struct that should be initialized. * @param state Pointer to the list state struct that should be initialized.
*/ */
static inline static inline void
void psyc_parse_list_state_init (PsycParseListState *state) psyc_parse_list_state_init (PsycParseListState *state)
{ {
memset(state, 0, sizeof(PsycParseListState)); memset(state, 0, sizeof(PsycParseListState));
} }
@ -297,57 +299,57 @@ void psyc_parse_list_state_init (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
void psyc_parse_list_buffer_set (PsycParseListState *state, char *buffer, size_t length) psyc_parse_list_buffer_set (PsycParseListState *state, char *buffer, size_t length)
{ {
state->buffer = (PsycString) {length, buffer}; state->buffer = (PsycString) {length, buffer};
state->cursor = 0; state->cursor = 0;
} }
static inline static inline size_t
size_t psyc_parse_content_length (PsycParseState *state) psyc_parse_content_length (PsycParseState *state)
{ {
return state->contentLength; return state->contentLength;
} }
static inline static inline PsycBool
PsycBool psyc_parse_content_length_found (PsycParseState *state) psyc_parse_content_length_found (PsycParseState *state)
{ {
return state->contentLengthFound; return state->contentLengthFound;
} }
static inline static inline size_t
size_t psyc_parse_value_length (PsycParseState *state) psyc_parse_value_length (PsycParseState *state)
{ {
return state->valueLength; return state->valueLength;
} }
static inline static inline PsycBool
PsycBool psyc_parse_value_length_found (PsycParseState *state) psyc_parse_value_length_found (PsycParseState *state)
{ {
return state->valueLengthFound; return state->valueLengthFound;
} }
static inline static inline size_t
size_t psyc_parse_cursor (PsycParseState *state) psyc_parse_cursor (PsycParseState *state)
{ {
return state->cursor; return state->cursor;
} }
static inline static inline size_t
size_t psyc_parse_buffer_length (PsycParseState *state) psyc_parse_buffer_length (PsycParseState *state)
{ {
return state->buffer.length; return state->buffer.length;
} }
static inline static inline size_t
size_t psyc_parse_remaining_length (PsycParseState *state) psyc_parse_remaining_length (PsycParseState *state)
{ {
return state->buffer.length - state->cursor; return state->buffer.length - state->cursor;
} }
static inline static inline const char *
const char * psyc_parse_remaining_buffer (PsycParseState *state) psyc_parse_remaining_buffer (PsycParseState *state)
{ {
return state->buffer.data + state->cursor; return state->buffer.data + state->cursor;
} }
@ -371,7 +373,8 @@ const char * psyc_parse_remaining_buffer (PsycParseState *state)
#ifdef __INLINE_PSYC_PARSE #ifdef __INLINE_PSYC_PARSE
static inline static inline
#endif #endif
PsycParseRC psyc_parse (PsycParseState *state, char *oper, PsycParseRC
psyc_parse (PsycParseState *state, char *oper,
PsycString *name, PsycString *value); PsycString *name, PsycString *value);
/** /**
@ -388,10 +391,11 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
#ifdef __INLINE_PSYC_PARSE #ifdef __INLINE_PSYC_PARSE
static inline static inline
#endif #endif
PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem); PsycParseListRC
psyc_parse_list (PsycParseListState *state, PsycString *elem);
static inline static inline PsycBool
PsycBool psyc_parse_number (const char *value, size_t len, int64_t *n) psyc_parse_number (const char *value, size_t len, int64_t *n)
{ {
size_t c = 0; size_t c = 0;
uint8_t neg = 0; uint8_t neg = 0;
@ -415,8 +419,8 @@ PsycBool psyc_parse_number (const char *value, size_t len, int64_t *n)
return PSYC_TRUE; return PSYC_TRUE;
} }
static inline static inline PsycBool
PsycBool psyc_parse_number_unsigned (const char *value, size_t len, uint64_t *n) psyc_parse_number_unsigned (const char *value, size_t len, uint64_t *n)
{ {
size_t c = 0; size_t c = 0;
if (!value) if (!value)
@ -429,14 +433,14 @@ PsycBool psyc_parse_number_unsigned (const char *value, size_t len, uint64_t *n)
return c == len ? PSYC_TRUE : PSYC_FALSE; return c == len ? PSYC_TRUE : PSYC_FALSE;
} }
static inline static inline PsycBool
PsycBool psyc_parse_time (const char *value, size_t len, time_t *t) psyc_parse_time (const char *value, size_t len, time_t *t)
{ {
return psyc_parse_number(value, len, t); return psyc_parse_number(value, len, t);
} }
static inline static inline PsycBool
PsycBool psyc_parse_date (const char *value, size_t len, time_t *t) psyc_parse_date (const char *value, size_t len, time_t *t)
{ {
if (psyc_parse_number(value, len, t)) { if (psyc_parse_number(value, len, t)) {
*t += PSYC_EPOCH; *t += PSYC_EPOCH;
@ -449,10 +453,10 @@ PsycBool psyc_parse_date (const char *value, size_t len, time_t *t)
* Determines if the argument is a glyph. * Determines if the argument is a glyph.
* Glyphs are: : = + - ? ! * Glyphs are: : = + - ? !
*/ */
static inline static inline char
char psyc_is_glyph (uint8_t g) psyc_is_glyph (uint8_t g)
{ {
switch(g) { switch (g) {
case ':': case ':':
case '=': case '=':
case '+': case '+':
@ -468,8 +472,8 @@ char psyc_is_glyph (uint8_t g)
/** /**
* Determines if the argument is numeric. * Determines if the argument is numeric.
*/ */
static inline static inline char
char psyc_is_numeric (uint8_t c) psyc_is_numeric (uint8_t c)
{ {
return c >= '0' && c <= '9'; return c >= '0' && c <= '9';
} }
@ -477,8 +481,8 @@ char psyc_is_numeric (uint8_t c)
/** /**
* Determines if the argument is alphabetic. * Determines if the argument is alphabetic.
*/ */
static inline static inline char
char psyc_is_alpha (uint8_t c) psyc_is_alpha (uint8_t c)
{ {
return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
} }
@ -486,8 +490,8 @@ char psyc_is_alpha (uint8_t c)
/** /**
* Determines if the argument is alphanumeric. * Determines if the argument is alphanumeric.
*/ */
static inline static inline char
char psyc_is_alpha_numeric (uint8_t c) psyc_is_alpha_numeric (uint8_t c)
{ {
return psyc_is_alpha(c) || psyc_is_numeric(c); return psyc_is_alpha(c) || psyc_is_numeric(c);
} }
@ -496,8 +500,8 @@ char psyc_is_alpha_numeric (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 _
*/ */
static inline static inline char
char psyc_is_kw_char (uint8_t c) psyc_is_kw_char (uint8_t c)
{ {
return psyc_is_alpha_numeric(c) || c == '_'; return psyc_is_alpha_numeric(c) || c == '_';
} }
@ -506,24 +510,23 @@ char psyc_is_kw_char (uint8_t c)
* Determines if the argument is a name character. * Determines if the argument is a name character.
* Name characters are: see opaque_part in RFC 2396 * Name characters are: see opaque_part in RFC 2396
*/ */
static inline static inline char
char psyc_is_name_char (uint8_t c) psyc_is_name_char (uint8_t c)
{ {
return psyc_is_alpha(c) || (c >= '$' && c <= ';') || return psyc_is_alpha(c) || (c >= '$' && c <= ';')
c == '_' || c == '!' || c == '?' || c == '=' || c == '@' || c == '~'; || c == '_' || c == '!' || c == '?' || c == '=' || c == '@' || c == '~';
} }
/** /**
* Determines if the argument is a hostname character. * Determines if the argument is a hostname character.
* Hostname characters are: alphanumeric and - * Hostname characters are: alphanumeric and -
*/ */
static inline static inline char
char psyc_is_host_char (uint8_t c) psyc_is_host_char (uint8_t c)
{ {
return psyc_is_alpha_numeric(c) || c == '.' || c == '-'; return psyc_is_alpha_numeric(c) || c == '.' || c == '-';
} }
/** @} */ // end of parse group /** @} */ // end of parse group
#define PSYC_PARSE_H
#endif #endif

View file

@ -1,4 +1,5 @@
#ifndef PSYC_RENDER_H #ifndef PSYC_RENDER_H
#define PSYC_RENDER_H
#include <psyc/packet.h> #include <psyc/packet.h>
@ -19,8 +20,7 @@
/** /**
* Return codes for psyc_render. * Return codes for psyc_render.
*/ */
typedef enum typedef enum {
{
/// Error, method is missing, but data is present. /// Error, method is missing, but data is present.
PSYC_RENDER_ERROR_METHOD_MISSING = -3, PSYC_RENDER_ERROR_METHOD_MISSING = -3,
/// Error, a modifier name is missing. /// Error, a modifier name is missing.
@ -34,8 +34,7 @@ typedef enum
/** /**
* Return codes for psyc_render_list. * Return codes for psyc_render_list.
*/ */
typedef enum typedef enum {
{
/// Error, buffer is too small to render the list. /// Error, buffer is too small to render the list.
PSYC_RENDER_LIST_ERROR = -1, PSYC_RENDER_LIST_ERROR = -1,
/// List is rendered successfully in the buffer. /// List is rendered successfully in the buffer.
@ -59,7 +58,8 @@ typedef enum
#ifdef __INLINE_PSYC_RENDER #ifdef __INLINE_PSYC_RENDER
static inline static inline
#endif #endif
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.
@ -67,9 +67,9 @@ PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen);
#ifdef __INLINE_PSYC_RENDER #ifdef __INLINE_PSYC_RENDER
static inline static inline
#endif #endif
PsycRenderListRC psyc_render_list (PsycList *list, char *buffer, size_t buflen); PsycRenderListRC
psyc_render_list (PsycList *list, char *buffer, size_t buflen);
/** @} */ // end of render group /** @} */ // end of render group
#define PSYC_RENDER_H
#endif #endif

View file

@ -1,4 +1,5 @@
#ifndef PSYC_TEXT_H #ifndef PSYC_TEXT_H
#define PSYC_TEXT_H
/** /**
* @file psyc/text.h * @file psyc/text.h
@ -19,22 +20,20 @@
* Return values for the text template parsing function. * Return values for the text template parsing function.
* @see psyc_text() * @see psyc_text()
*/ */
typedef enum typedef enum {
{
/// No substitution was made, nothing was written to the buffer. /// No substitution was made, nothing was written to the buffer.
PSYC_TEXT_NO_SUBST = -1, PSYC_TEXT_NO_SUBST = -1,
/// Text template parsing & rendering complete. /// Text template parsing & rendering complete.
PSYC_TEXT_COMPLETE = 0, PSYC_TEXT_COMPLETE = 0,
/// Text template parsing & rendering is incomplete, because the buffer was too small. /// Text template parsing & rendering is incomplete, because the buffer was too
/// Another call is required to this function after setting a new buffer. /// small. Another call is required to this function after setting a new buffer.
PSYC_TEXT_INCOMPLETE = 1, PSYC_TEXT_INCOMPLETE = 1,
} PsycTextRC; } PsycTextRC;
/** /**
* Return values for PsycTextCB. * Return values for PsycTextCB.
*/ */
typedef enum typedef enum {
{
/// Value not found, don't substitute anything. /// Value not found, don't substitute anything.
PSYC_TEXT_VALUE_NOT_FOUND = -1, PSYC_TEXT_VALUE_NOT_FOUND = -1,
/// Value found, substitute contents of the value variable. /// Value found, substitute contents of the value variable.
@ -44,8 +43,7 @@ typedef enum
/** /**
* Struct for keeping PSYC text parser state. * Struct for keeping PSYC text parser state.
*/ */
typedef struct typedef struct {
{
size_t cursor; ///< current position in the template size_t cursor; ///< current position in the template
size_t written; ///< number of bytes written to buffer size_t written; ///< number of bytes written to buffer
PsycString tmpl; ///< input buffer with text template to parse PsycString tmpl; ///< input buffer with text template to parse
@ -64,7 +62,8 @@ typedef struct
* PSYC_TEXT_VALUE_NOT_FOUND if no match found in which case psyc_text * PSYC_TEXT_VALUE_NOT_FOUND if no match found in which case psyc_text
* leaves the original template text as is. * leaves the original template text as is.
*/ */
typedef PsycTextValueRC (*PsycTextCB)(const char *name, size_t len, PsycString *value, void *extra); typedef PsycTextValueRC (*PsycTextCB) (const char *name, size_t len,
PsycString *value, void *extra);
/** /**
* Initializes the PSYC text state struct. * Initializes the PSYC text state struct.
@ -75,17 +74,21 @@ typedef PsycTextValueRC (*PsycTextCB)(const char *name, size_t len, PsycString *
* @param buffer Output buffer where the rendered text is going to be written. * @param buffer Output buffer where the rendered text is going to be written.
* @param buflen Length of output buffer. * @param buflen Length of output buffer.
*/ */
static inline static inline void
void psyc_text_state_init (PsycTextState *state, psyc_text_state_init (PsycTextState *state,
char *tmpl, size_t tmplen, char *tmpl, size_t tmplen,
char *buffer, size_t buflen) char *buffer, size_t buflen)
{ {
state->cursor = 0; state->cursor = 0;
state->written = 0; state->written = 0;
state->tmpl = (PsycString) {tmplen, tmpl}; state->tmpl = (PsycString) {
state->buffer = (PsycString) {buflen, buffer}; tmplen, tmpl};
state->open = (PsycString) {1, "["}; state->buffer = (PsycString) {
state->close = (PsycString) {1, "]"}; buflen, buffer};
state->open = (PsycString) {
1, "["};
state->close = (PsycString) {
1, "]"};
} }
/** /**
@ -101,8 +104,8 @@ void psyc_text_state_init (PsycTextState *state,
* @param close Closing brace. * @param close Closing brace.
* @param closelen Length of closing brace. * @param closelen Length of closing brace.
*/ */
static inline static inline void
void psyc_text_state_init_custom (PsycTextState *state, psyc_text_state_init_custom (PsycTextState *state,
char *tmpl, size_t tmplen, char *tmpl, size_t tmplen,
char *buffer, size_t buflen, char *buffer, size_t buflen,
char *open, size_t openlen, char *open, size_t openlen,
@ -110,25 +113,29 @@ void psyc_text_state_init_custom (PsycTextState *state,
{ {
state->cursor = 0; state->cursor = 0;
state->written = 0; state->written = 0;
state->tmpl = (PsycString) {tmplen, tmpl}; state->tmpl = (PsycString) {
state->buffer = (PsycString) {buflen, buffer}; tmplen, tmpl};
state->open = (PsycString) {openlen, open}; state->buffer = (PsycString) {
state->close = (PsycString) {closelen, close}; buflen, buffer};
state->open = (PsycString) {
openlen, open};
state->close = (PsycString) {
closelen, close};
} }
/** /**
* Sets a new output buffer in the PSYC text state struct. * Sets a new output buffer in the PSYC text state struct.
*/ */
static inline static inline void
void psyc_text_buffer_set (PsycTextState *state, psyc_text_buffer_set (PsycTextState *state, char *buffer, size_t length)
char *buffer, size_t length)
{ {
state->buffer = (PsycString){length, buffer}; state->buffer = (PsycString) {
length, buffer};
state->written = 0; state->written = 0;
} }
static inline static inline size_t
size_t psyc_text_bytes_written (PsycTextState *state) psyc_text_bytes_written (PsycTextState *state)
{ {
return state->written; return state->written;
} }
@ -148,9 +155,9 @@ size_t psyc_text_bytes_written (PsycTextState *state)
* *
* @see http://about.psyc.eu/psyctext * @see http://about.psyc.eu/psyctext
**/ **/
PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void *extra); PsycTextRC
psyc_text (PsycTextState *state, PsycTextCB getValue, void *extra);
/** @} */ // end of text group /** @} */ // end of text group
#define PSYC_TEXT_H
#endif #endif

View file

@ -1,4 +1,6 @@
#ifndef PSYC_UNIFORM_H #ifndef PSYC_UNIFORM_H
#define PSYC_UNIFORM_H
/** /**
* @file uniform.h * @file uniform.h
* @brief Uniform parsing. * @brief Uniform parsing.
@ -76,7 +78,7 @@ typedef enum {
PSYC_ENTITY_SERVICE = '$', PSYC_ENTITY_SERVICE = '$',
} PsycEntityType; } PsycEntityType;
int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length); int
psyc_uniform_parse (PsycUniform * uni, char *str, size_t length);
#define PSYC_UNIFORM_H
#endif #endif

View file

@ -1,4 +1,5 @@
#ifndef PSYC_VARIABLE_H #ifndef PSYC_VARIABLE_H
#define PSYC_VARIABLE_H
/** /**
* @file psyc/variable.h * @file psyc/variable.h
@ -16,22 +17,23 @@ extern const size_t psyc_var_types_num;
/** /**
* Is this a routing variable name? * Is this a routing variable name?
*/ */
PsycBool psyc_var_is_routing (const char *name, size_t len); PsycBool
psyc_var_is_routing (const char *name, size_t len);
/** /**
* Get the type of variable name. * Get the type of variable name.
*/ */
PsycType psyc_var_type (const char *name, size_t len); PsycType
psyc_var_type (const char *name, size_t len);
/** /**
* Is this a list variable name? * Is this a list variable name?
*/ */
static inline static inline PsycBool
PsycBool psyc_var_is_list (const char *name, size_t len) psyc_var_is_list (const char *name, size_t len)
{ {
return len < 5 || memcmp(name, "_list", 5) != 0 || return len < 5 || memcmp(name, "_list", 5) != 0 || (len > 5 && name[5] != '_')
(len > 5 && name[5] != '_') ? PSYC_FALSE : PSYC_TRUE; ? PSYC_FALSE : PSYC_TRUE;
} }
#define PSYC_VARIABLE_H
#endif #endif

View file

@ -1,33 +1,39 @@
#define ALPHANUMS "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz" #define ALPHANUMS "zyxwvutsrqponmlkjihgfedcba9876543210123456789abcdefghijklmnopqrstuvwxyz"
/** converts an integer to a string, using a base of 10 by default. /**
* Converts an integer to a string, using a base of 10 by default.
* *
* if you NULL out the output buffer it will return the expected * If you NULL out the output buffer it will return the expected
* output string length anyway. * output string length anyway.
*/ */
int itoa(int number, char* out, int base) { int
itoa (int number, char *out, int base)
{
int t, count; int t, count;
char *p, *q; char *p, *q;
char c; char c;
p = q = out; p = q = out;
if (base < 2 || base > 36) base = 10; if (base < 2 || base > 36)
base = 10;
do { do {
t = number; t = number;
number /= base; number /= base;
if (out) *p = ALPHANUMS[t+35 - number*base]; if (out)
*p = ALPHANUMS[t + 35 - number * base];
p++; p++;
} while (number); } while (number);
if (t < 0) { if (t < 0) {
if (out) *p = '-'; if (out)
*p = '-';
p++; p++;
} }
count = p-out; count = p - out;
if (out) { if (out) {
*p-- = '\0'; *p-- = '\0';
while(q < p) { while (q < p) {
c = *p; c = *p;
*p-- = *q; *p-- = *q;
*q++ = c; *q++ = c;
@ -45,41 +51,44 @@ int itoa(int number, char* out, int base) {
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
int main(int argc, char **argv) { int
main (int argc, char **argv)
{
char out[4404]; char out[4404];
int in[44]; int in[44];
int c, i, j; int c, i, j;
if (argc < 3 || argc > sizeof(in)) { if (argc < 3 || argc > sizeof(in)) {
printf("Usage: %s <times> <numbers>+\n\nExample: %s 999999 123 234 345 -49 -21892\n", argv[0], argv[0]); printf("Usage: %s <times> <numbers>+\n\n"
"Example: %s 999999 123 234 345 -49 -21892\n",
argv[0], argv[0]);
return -1; return -1;
} }
for (j=argc-1; j; j--) { for (j = argc - 1; j; j--) {
// printf("Looking at arg #%d: %s\n", j, argv[j]); //printf("Looking at arg #%d: %s\n", j, argv[j]);
in[j] = atoi(argv[j]); in[j] = atoi(argv[j]);
// printf("Got %d: %d\n", j, in[j]); //printf("Got %d: %d\n", j, in[j]);
} }
for (i=in[1]; i; i--) { for (i = in[1]; i; i--) {
c = 0; c = 0;
for (j=argc-1; j>1; j--) { for (j = argc - 1; j > 1; j--) {
# if 0 #if 0
// use good old sprintf // use good old sprintf
c += sprintf(&out[c], " %d", in[j]); c += sprintf(&out[c], " %d", in[j]);
# else #else
# if 1 #if 1
// use the itoa implementation // use the itoa implementation
out[c++] = ' '; out[c++] = ' ';
c += itoa(in[j], &out[c], 10); c += itoa(in[j], &out[c], 10);
# else #else
// just count the needed space // just count the needed space
c += itoa(in[j], NULL, 10) + 1; c += itoa(in[j], NULL, 10) + 1;
# endif #endif
# endif #endif
} }
} }
printf("%d times, %d count, buffer len: %d, buffer: %s\n", printf("%d times, %d count, buffer len: %d, buffer: %s\n",
in[1], c, strlen(out), "<omitted>"); // out in[1], c, strlen(out), "<omitted>"); // out
return 0; return 0;
} }
#endif #endif

View file

@ -1,8 +1,8 @@
#include "lib.h" #include "lib.h"
int psyc_inherits (char* sho, size_t slen, int
char* lon, size_t llen) { psyc_inherits(char *sho, size_t slen, char *lon, size_t llen)
{
// this allows to pass zero-terminated strings instead of providing // this allows to pass zero-terminated strings instead of providing
// the length.. but we would be faster here if we expected the callee // the length.. but we would be faster here if we expected the callee
// to always use the PSYC_C2ARG() macro instead. additionally, the // to always use the PSYC_C2ARG() macro instead. additionally, the
@ -11,14 +11,13 @@ int psyc_inherits (char* sho, size_t slen,
//if (!slen) slen = strlen(sho); //if (!slen) slen = strlen(sho);
//if (!llen) llen = strlen(lon); //if (!llen) llen = strlen(lon);
if (slen == 0 || *sho != '_' || if (slen == 0 || *sho != '_' || llen == 0 || *lon != '_') {
llen == 0 || *lon != '_') { P1(("Please use long format keywords (compact ones would be faster, I know..)\n"));
P1(("Please use long format keywords (compact ones would be faster, I know..)\n"))
return -2; return -2;
} }
if (slen > llen) { if (slen > llen) {
P1(("The long string is shorter than the short one.\n")) P1(("The long string is shorter than the short one.\n"));
return -3; return -3;
} }
@ -33,79 +32,87 @@ int psyc_inherits (char* sho, size_t slen,
* implementations are not required to recognize * implementations are not required to recognize
* that. * that.
*/ */
P1(("Illegal choice of keyword names!\n")) P1(("Illegal choice of keyword names!\n"));
return -4; return -4;
} }
return 0; return 0;
} }
P4(("%.*s does not inherit from %.*s.\n", (int)llen, lon, (int)slen, sho)) P4(("%.*s does not inherit from %.*s.\n", (int) llen, lon, (int) slen, sho));
return 1; return 1;
} }
int psyc_matches (char* sho, size_t slen, int
char* lon, size_t llen) { psyc_matches(char *sho, size_t slen, char *lon, size_t llen)
{
char *s, *l, *se, *le; char *s, *l, *se, *le;
//if (!slen) slen = strlen(sho); //if (!slen) slen = strlen(sho);
//if (!llen) llen = strlen(lon); //if (!llen) llen = strlen(lon);
if (slen == 0 || *sho != '_' || if (slen == 0 || *sho != '_' || llen == 0 || *lon != '_') {
llen == 0 || *lon != '_') { P1(("Please use long format keywords (compact ones would be faster, I know..)\n"));
P1(("Please use long format keywords (compact ones would be faster, I know..)\n"))
return -2; return -2;
} }
if (slen > llen) { if (slen > llen) {
P1(("The long string is shorter than the short one.\n")) P1(("The long string is shorter than the short one.\n"));
return -3; return -3;
} }
if (slen == llen) { if (slen == llen) {
if (!strncmp(sho, lon, slen)) { if (!strncmp(sho, lon, slen)) {
P1(("Identical arguments.\n")) P1(("Identical arguments.\n"));
return 0; return 0;
} }
P1(("Same length but different.\nNo match, but they could be related or have a common type.\n")) P1(("Same length but different.\nNo match, but they could be related or have a common type.\n"));
return -4; return -4;
} }
P3(("# psyc_matches short '%.*s' in long '%.*s' ?\n", (int)slen, sho, (int)llen, lon)) P3(("# psyc_matches short '%.*s' in long '%.*s' ?\n", (int) slen, sho,
(int) llen, lon));
se = sho+slen; se = sho + slen;
le = lon+llen; le = lon + llen;
sho++; lon++; slen--; llen--; sho++;
while(*sho && sho < se) { lon++;
P3(("# comparing short '%.*s' (%d)\n", (int)slen, sho, (int)slen)) slen--;
unless (s = memchr(sho, '_', slen)) s = se; llen--;
P4(("# sho goes '%c' and lon goes '%c'\n", *sho, (int)*lon)) while (*sho && sho < se) {
while(*lon && lon < le) { P3(("# comparing short '%.*s' (%d)\n", (int) slen, sho, (int) slen));
P3(("# against long '%.*s' (%d)\n", (int)llen, lon, (int)llen)) unless(s = memchr(sho, '_', slen)) s = se;
unless (l = memchr(lon, '_', llen)) l = le; P4(("# sho goes '%c' and lon goes '%c'\n", *sho, (int) *lon));
P3(("# %ld == %ld && !strncmp '%.*s', '%.*s'\n", s-sho, l-lon, (int)(s-sho), sho, (int)(s-sho), lon)) while (*lon && lon < le) {
if (l-lon == s-sho && !strncmp(sho, lon, s-sho)) goto foundone; P3(("# against long '%.*s' (%d)\n", (int) llen, lon, (int) llen));
P4(("# failed\n")) unless(l = memchr(lon, '_', llen)) l = le;
llen -= l-lon + 1; P3(("# %ld == %ld && !strncmp '%.*s', '%.*s'\n", s - sho, l - lon,
(int) (s - sho), sho, (int) (s - sho), lon));
if (l - lon == s - sho && !strncmp(sho, lon, s - sho))
goto foundone;
P4(("# failed\n"));
llen -= l - lon + 1;
lon = ++l; lon = ++l;
} }
goto failed; goto failed;
foundone: foundone:
P3(("# found %ld of short '%.*s' and long '%.*s'\n", s-sho, (int)(s-sho), sho, (int)(s-sho), lon)) P3(("# found %ld of short '%.*s' and long '%.*s'\n", s - sho,
llen -= l-lon; (int) (s - sho), sho, (int) (s - sho), lon));
slen -= s-sho; llen -= l - lon;
slen -= s - sho;
sho = ++s; sho = ++s;
lon = ++l; lon = ++l;
} }
return 0; return 0;
failed: failed:
P4(("No, they don't match.\n")) P4(("No, they don't match.\n"));
return 1; return 1;
} }
/** /**
* Look up value associated with a key in a dictionary. * Look up value associated with a key in a dictionary.
*/ */
void * psyc_dict_lookup (const PsycDict *dict, size_t size, void *
psyc_dict_lookup(const PsycDict * dict, size_t size,
const char *key, size_t keylen, const char *key, size_t keylen,
PsycBool inherit, int8_t *matching) PsycBool inherit, int8_t * matching)
{ {
size_t cursor = 1; size_t cursor = 1;
uint8_t i, m = 0; uint8_t i, m = 0;
@ -114,10 +121,12 @@ void * psyc_dict_lookup (const PsycDict *dict, size_t size,
return 0; return 0;
// first find the keywords with matching length // first find the keywords with matching length
for (i=0; i<size; i++) for (i = 0; i < size; i++) {
if (keylen == dict[i].key.length || if (keylen == dict[i].key.length
(inherit && keylen > dict[i].key.length && key[dict[i].key.length] == '_')) || (inherit && keylen > dict[i].key.length
&& key[dict[i].key.length] == '_'))
matching[m++] = i; matching[m++] = i;
}
matching[m] = -1; // mark the end of matching indexes matching[m] = -1; // mark the end of matching indexes
@ -145,9 +154,13 @@ void * psyc_dict_lookup (const PsycDict *dict, size_t size,
} }
#ifdef CMDTOOL #ifdef CMDTOOL
int main(int argc, char **argv) { int
main(int argc, char **argv)
{
if (argc != 3) { if (argc != 3) {
printf("Usage: %s <short> <long>\n\nExample: %s _failure_delivery _failure_unsuccessful_delivery_death\n", argv[0], argv[0]); printf("Usage: %s <short> <long>\n\n"
"Example: %s _failure_delivery _failure_unsuccessful_delivery_death\n",
argv[0], argv[0]);
return -1; return -1;
} }
if (psyc_matches(argv[1], 0, argv[2], 0) == 0) if (psyc_matches(argv[1], 0, argv[2], 0) == 0)

View file

@ -31,13 +31,12 @@
/* /*
* Find the first occurrence of the byte string s in byte string l. * Find the first occurrence of the byte string s in byte string l.
*/ */
void * void *
memmem(const void *l, size_t l_len, const void *s, size_t s_len) memmem(const void *l, size_t l_len, const void *s, size_t s_len)
{ {
register char *cur, *last; register char *cur, *last;
const char *cl = (const char *)l; const char *cl = (const char *) l;
const char *cs = (const char *)s; const char *cs = (const char *) s;
/* we need something to compare */ /* we need something to compare */
if (l_len == 0 || s_len == 0) if (l_len == 0 || s_len == 0)
@ -49,12 +48,12 @@ memmem(const void *l, size_t l_len, const void *s, size_t s_len)
/* special case where s_len == 1 */ /* special case where s_len == 1 */
if (s_len == 1) if (s_len == 1)
return memchr(l, (int)*cs, l_len); return memchr(l, (int) *cs, l_len);
/* the last position where its possible to find "s" in "l" */ /* the last position where its possible to find "s" in "l" */
last = (char *)cl + l_len - s_len; last = (char *) cl + l_len - s_len;
for (cur = (char *)cl; cur <= last; cur++) for (cur = (char *) cl; cur <= last; cur++)
if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0) if (cur[0] == cs[0] && memcmp(cur, cs, s_len) == 0)
return cur; return cur;

View file

@ -2,20 +2,18 @@
#include <psyc/syntax.h> #include <psyc/syntax.h>
#include <psyc/packet.h> #include <psyc/packet.h>
inline inline PsycListFlag
PsycListFlag psyc_list_length_check (PsycList *list) psyc_list_length_check (PsycList * list)
{ {
PsycListFlag flag = PSYC_LIST_NO_LENGTH; PsycListFlag flag = PSYC_LIST_NO_LENGTH;
size_t i, length = 0; size_t i, length = 0;
for (i = 0; i < list->num_elems; i++) for (i = 0; i < list->num_elems; i++) {
{
PsycString *elem = &list->elems[i]; PsycString *elem = &list->elems[i];
length += 1 + elem->length; // |elem length += 1 + elem->length; // |elem
if (length > PSYC_MODIFIER_SIZE_THRESHOLD || if (length > PSYC_MODIFIER_SIZE_THRESHOLD ||
memchr(elem->data, (int)'|', elem->length) || memchr(elem->data, (int) '|', elem->length) ||
memchr(elem->data, (int)'\n', elem->length)) memchr(elem->data, (int) '\n', elem->length)) {
{
flag = PSYC_LIST_NEED_LENGTH; flag = PSYC_LIST_NEED_LENGTH;
break; break;
} }
@ -24,22 +22,19 @@ PsycListFlag psyc_list_length_check (PsycList *list)
return flag; return flag;
} }
inline inline PsycListFlag
PsycListFlag psyc_list_length (PsycList *list) psyc_list_length (PsycList * list)
{ {
size_t i, length = 0; size_t i, length = 0;
if (list->flag == PSYC_LIST_NEED_LENGTH) if (list->flag == PSYC_LIST_NEED_LENGTH) {
{ for (i = 0; i < list->num_elems; i++) {
for (i = 0; i < list->num_elems; i++)
{
if (i > 0) if (i > 0)
length++; // | length++; // |
length += psyc_num_length(list->elems[i].length) + 1 + list->elems[i].length; // length SP elem length += // length SP elem
psyc_num_length(list->elems[i].length) + 1 + list->elems[i].length;
} }
} } else {
else
{
for (i = 0; i < list->num_elems; i++) for (i = 0; i < list->num_elems; i++)
length += 1 + list->elems[i].length; // |elem length += 1 + list->elems[i].length; // |elem
} }
@ -47,11 +42,12 @@ PsycListFlag psyc_list_length (PsycList *list)
return length; return length;
} }
inline inline void
void psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems, psyc_list_init (PsycList * list, PsycString * elems, size_t num_elems,
PsycListFlag flag) PsycListFlag flag)
{ {
*list = (PsycList) {num_elems, elems, 0, flag}; *list = (PsycList) {
num_elems, elems, 0, flag};
if (flag == PSYC_LIST_CHECK_LENGTH) // check if list elements need length if (flag == PSYC_LIST_CHECK_LENGTH) // check if list elements need length
list->flag = psyc_list_length_check(list); list->flag = psyc_list_length_check(list);
@ -60,8 +56,8 @@ void psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
} }
inline inline size_t
size_t psyc_modifier_length (PsycModifier *m) psyc_modifier_length (PsycModifier * m)
{ {
size_t length = 2; // oper\n size_t length = 2; // oper\n
if (m->name.length > 0) if (m->name.length > 0)
@ -73,8 +69,8 @@ size_t psyc_modifier_length (PsycModifier *m)
return length; return length;
} }
inline inline PsycPacketFlag
PsycPacketFlag psyc_packet_length_check (PsycPacket *p) psyc_packet_length_check (PsycPacket * p)
{ {
if (p->data.length == 1 && p->data.data[0] == PSYC_PACKET_DELIMITER_CHAR) if (p->data.length == 1 && p->data.data[0] == PSYC_PACKET_DELIMITER_CHAR)
return PSYC_PACKET_NEED_LENGTH; return PSYC_PACKET_NEED_LENGTH;
@ -83,8 +79,8 @@ PsycPacketFlag psyc_packet_length_check (PsycPacket *p)
return PSYC_PACKET_NEED_LENGTH; return PSYC_PACKET_NEED_LENGTH;
int i; int i;
// if any entity modifiers need length it is possible they contain // If any entity modifiers need length, it is possible they contain
// a packet terminator, thus the content should have a length as well // a packet terminator, thus the content should have a length as well.
for (i = 0; i < p->entity.lines; i++) for (i = 0; i < p->entity.lines; i++)
if (p->entity.modifiers[i].flag == PSYC_MODIFIER_NEED_LENGTH) if (p->entity.modifiers[i].flag == PSYC_MODIFIER_NEED_LENGTH)
return PSYC_PACKET_NEED_LENGTH; return PSYC_PACKET_NEED_LENGTH;
@ -95,8 +91,8 @@ PsycPacketFlag psyc_packet_length_check (PsycPacket *p)
return PSYC_PACKET_NO_LENGTH; return PSYC_PACKET_NO_LENGTH;
} }
inline inline size_t
size_t psyc_packet_length_set (PsycPacket *p) psyc_packet_length_set (PsycPacket * p)
{ {
size_t i; size_t i;
p->routingLength = 0; p->routingLength = 0;
@ -108,8 +104,7 @@ size_t psyc_packet_length_set (PsycPacket *p)
if (p->content.length) if (p->content.length)
p->contentLength = p->content.length; p->contentLength = p->content.length;
else else {
{
// add state operation // add state operation
if (p->stateop != PSYC_STATE_NOOP) if (p->stateop != PSYC_STATE_NOOP)
p->contentLength += 2; // op\n p->contentLength += 2; // op\n
@ -137,16 +132,16 @@ size_t psyc_packet_length_set (PsycPacket *p)
return p->length; return p->length;
} }
inline inline void
void psyc_packet_init (PsycPacket *p, psyc_packet_init (PsycPacket * p,
PsycModifier *routing, size_t routinglen, PsycModifier * routing, size_t routinglen,
PsycModifier *entity, size_t entitylen, PsycModifier * entity, size_t entitylen,
char *method, size_t methodlen, char *method, size_t methodlen,
char *data, size_t datalen, char *data, size_t datalen,
char stateop, PsycPacketFlag flag) char stateop, PsycPacketFlag flag)
{ {
*p = (PsycPacket) {{routinglen, routing}, {entitylen, entity}, stateop, *p = (PsycPacket) {{routinglen, routing}, {entitylen, entity}, stateop,
{methodlen, method}, {datalen, data}, {0,0}, 0, 0, flag}; {methodlen, method}, {datalen, data}, {0, 0}, 0, 0, flag};
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs length if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs length
p->flag = psyc_packet_length_check(p); p->flag = psyc_packet_length_check(p);
@ -154,13 +149,13 @@ void psyc_packet_init (PsycPacket *p,
psyc_packet_length_set(p); psyc_packet_length_set(p);
} }
inline inline void
void psyc_packet_init_raw (PsycPacket *p, psyc_packet_init_raw (PsycPacket * p,
PsycModifier *routing, size_t routinglen, PsycModifier * routing, size_t routinglen,
char *content, size_t contentlen, char *content, size_t contentlen,
PsycPacketFlag flag) PsycPacketFlag flag)
{ {
*p = (PsycPacket) {{routinglen, routing}, {0,0}, 0, {0,0}, {0,0}, *p = (PsycPacket) {{routinglen, routing}, {0, 0}, 0, {0, 0}, {0, 0},
{contentlen, content}, 0, 0, flag}; {contentlen, content}, 0, 0, flag};
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs length if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs length

View file

@ -10,8 +10,7 @@
#include <psyc/parse.h> #include <psyc/parse.h>
#define ADVANCE_CURSOR_OR_RETURN(ret) \ #define ADVANCE_CURSOR_OR_RETURN(ret) \
if (++(state->cursor) >= state->buffer.length) \ if (++(state->cursor) >= state->buffer.length) { \
{ \
state->cursor = state->startc; \ state->cursor = state->startc; \
return ret; \ return ret; \
} }
@ -29,14 +28,13 @@ typedef enum {
* It should contain one or more keyword characters. * It should contain one or more keyword characters.
* @return PARSE_ERROR or PARSE_SUCCESS * @return PARSE_ERROR or PARSE_SUCCESS
*/ */
static inline static inline ParseRC
ParseRC psyc_parse_keyword (PsycParseState *state, PsycString *name) psyc_parse_keyword (PsycParseState *state, PsycString *name)
{ {
name->data = state->buffer.data + state->cursor; name->data = state->buffer.data + state->cursor;
name->length = 0; name->length = 0;
while (psyc_is_kw_char(state->buffer.data[state->cursor])) while (psyc_is_kw_char(state->buffer.data[state->cursor])) {
{
name->length++; // was a valid char, increase length name->length++; // was a valid char, increase length
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
@ -54,15 +52,15 @@ ParseRC psyc_parse_keyword (PsycParseState *state, PsycString *name)
* *
* @return PARSE_COMPLETE or PARSE_INCOMPLETE * @return PARSE_COMPLETE or PARSE_INCOMPLETE
*/ */
static inline static inline ParseRC
ParseRC psyc_parse_binary_value (PsycParseState *state, PsycString *value, psyc_parse_binary_value (PsycParseState *state, PsycString *value,
size_t *length, size_t *parsed) size_t *length, size_t *parsed)
{ {
size_t remaining = *length - *parsed; size_t remaining = *length - *parsed;
value->data = state->buffer.data + state->cursor; value->data = state->buffer.data + state->cursor;
if (state->cursor + remaining > state->buffer.length) if (state->cursor + remaining > state->buffer.length) {
{ // value doesn't fit in the buffer completely // value doesn't fit in the buffer completely
value->length = state->buffer.length - state->cursor; value->length = state->buffer.length - state->cursor;
state->cursor += value->length; state->cursor += value->length;
*parsed += value->length; *parsed += value->length;
@ -81,8 +79,8 @@ ParseRC psyc_parse_binary_value (PsycParseState *state, PsycString *value,
* Parse simple or binary variable. * Parse simple or binary variable.
* @return PARSE_ERROR or PARSE_SUCCESS * @return PARSE_ERROR or PARSE_SUCCESS
*/ */
static inline static inline ParseRC
ParseRC psyc_parse_modifier (PsycParseState *state, char *oper, psyc_parse_modifier (PsycParseState *state, char *oper,
PsycString *name, PsycString *value) PsycString *name, PsycString *value)
{ {
*oper = *(state->buffer.data + state->cursor); *oper = *(state->buffer.data + state->cursor);
@ -102,22 +100,20 @@ ParseRC psyc_parse_modifier (PsycParseState *state, char *oper,
// Parse the value. // Parse the value.
// If we're in the content part check if it's a binary var. // If we're in the content part check if it's a binary var.
if (state->part == PSYC_PART_CONTENT && state->buffer.data[state->cursor] == ' ') // binary arg if (state->part == PSYC_PART_CONTENT && state->buffer.data[state->cursor] == ' ') {
{ // After SP the length follows. // binary arg
// After SP the length follows.
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
if (psyc_is_numeric(state->buffer.data[state->cursor])) if (psyc_is_numeric(state->buffer.data[state->cursor])) {
{
state->valueLengthFound = 1; state->valueLengthFound = 1;
do do {
{
length = 10 * length + state->buffer.data[state->cursor] - '0'; length = 10 * length + state->buffer.data[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
while (psyc_is_numeric(state->buffer.data[state->cursor])); while (psyc_is_numeric(state->buffer.data[state->cursor]));
state->valueLength = length; state->valueLength = length;
} } else
else
return PSYC_PARSE_ERROR_MOD_LEN; return PSYC_PARSE_ERROR_MOD_LEN;
// After the length a TAB follows. // After the length a TAB follows.
@ -127,26 +123,24 @@ ParseRC psyc_parse_modifier (PsycParseState *state, char *oper,
if (++(state->cursor) >= state->buffer.length) if (++(state->cursor) >= state->buffer.length)
return length ? PARSE_INCOMPLETE : PARSE_SUCCESS; // if length=0 we're done return length ? PARSE_INCOMPLETE : PARSE_SUCCESS; // if length=0 we're done
ret = psyc_parse_binary_value(state, value, &(state->valueLength), &(state->valueParsed)); ret =
psyc_parse_binary_value(state, value, &(state->valueLength),
&(state->valueParsed));
if (ret == PARSE_INCOMPLETE) if (ret == PARSE_INCOMPLETE)
return ret; return ret;
return PARSE_SUCCESS; return PARSE_SUCCESS;
} } else if (state->buffer.data[state->cursor] == '\t') { // simple arg
else if (state->buffer.data[state->cursor] == '\t') // simple arg
{
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
value->data = state->buffer.data + state->cursor; value->data = state->buffer.data + state->cursor;
while (state->buffer.data[state->cursor] != '\n') while (state->buffer.data[state->cursor] != '\n') {
{
value->length++; value->length++;
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
return PARSE_SUCCESS; return PARSE_SUCCESS;
} } else
else
return PSYC_PARSE_ERROR_MOD_TAB; return PSYC_PARSE_ERROR_MOD_TAB;
} }
@ -154,15 +148,15 @@ ParseRC psyc_parse_modifier (PsycParseState *state, char *oper,
#ifdef __INLINE_PSYC_PARSE #ifdef __INLINE_PSYC_PARSE
static inline static inline
#endif #endif
PsycParseRC psyc_parse (PsycParseState *state, char *oper, PsycParseRC
psyc_parse (PsycParseState *state, char *oper,
PsycString *name, PsycString *value) PsycString *name, PsycString *value)
{ {
#ifdef DEBUG #ifdef DEBUG
if (state->flags & PSYC_PARSE_ROUTING_ONLY && if (state->flags & PSYC_PARSE_ROUTING_ONLY &&
state->flags & PSYC_PARSE_START_AT_CONTENT) state->flags & PSYC_PARSE_START_AT_CONTENT)
PP(("Invalid flag combination")) PP(("Invalid flag combination"));
#endif #endif
ParseRC ret; // a return value ParseRC ret; // a return value
size_t pos = state->cursor; // a cursor position size_t pos = state->cursor; // a cursor position
@ -171,11 +165,10 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
state->startc = state->cursor; state->startc = state->cursor;
// First we test if we can access the first char. // First we test if we can access the first char.
if (state->cursor >= state->buffer.length) // cursor is not inside the length if (state->cursor >= state->buffer.length) // Cursor is not inside the length.
return PSYC_PARSE_INSUFFICIENT; return PSYC_PARSE_INSUFFICIENT;
switch (state->part) switch (state->part) {
{
case PSYC_PART_RESET: // New packet starts here, reset state. case PSYC_PART_RESET: // New packet starts here, reset state.
state->valueParsed = 0; state->valueParsed = 0;
state->valueLength = 0; state->valueLength = 0;
@ -188,24 +181,20 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
// fall thru // fall thru
case PSYC_PART_ROUTING: case PSYC_PART_ROUTING:
if (state->routingLength > 0) if (state->routingLength > 0) {
{
if (state->buffer.data[state->cursor] != '\n') if (state->buffer.data[state->cursor] != '\n')
return PSYC_PARSE_ERROR_MOD_NL; return PSYC_PARSE_ERROR_MOD_NL;
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
// Each line of the header starts with a glyph, // Each line of the header starts with a glyph,
// i.e. :_name, -_name +_name etc, // i.e. :_name, -_name +_name etc,
// so just test if the first char is a glyph. // so just test if the first char is a glyph.
if (psyc_is_glyph(state->buffer.data[state->cursor])) // is the first char a glyph? if (psyc_is_glyph(state->buffer.data[state->cursor])) {
{ // it is a glyph, so a variable starts here // it is a glyph, so a variable starts here
ret = psyc_parse_modifier(state, oper, name, value); ret = psyc_parse_modifier(state, oper, name, value);
state->routingLength += state->cursor - pos; state->routingLength += state->cursor - pos;
return ret == PARSE_SUCCESS ? PSYC_PARSE_ROUTING : ret; return ret == PARSE_SUCCESS ? PSYC_PARSE_ROUTING : ret;
} } else { // not a glyph
else // not a glyph
{
state->part = PSYC_PART_LENGTH; state->part = PSYC_PART_LENGTH;
state->startc = state->cursor; state->startc = state->cursor;
// fall thru // fall thru
@ -213,35 +202,29 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
case PSYC_PART_LENGTH: case PSYC_PART_LENGTH:
// End of header, content starts with an optional length then a NL // End of header, content starts with an optional length then a NL
if (psyc_is_numeric(state->buffer.data[state->cursor])) if (psyc_is_numeric(state->buffer.data[state->cursor])) {
{
state->contentLengthFound = 1; state->contentLengthFound = 1;
state->contentLength = 0; state->contentLength = 0;
do do {
{ state->contentLength =
state->contentLength = 10 * state->contentLength + state->buffer.data[state->cursor] - '0'; 10 * state->contentLength +
state->buffer.data[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} } while (psyc_is_numeric(state->buffer.data[state->cursor]));
while (psyc_is_numeric(state->buffer.data[state->cursor]));
} }
if (state->buffer.data[state->cursor] == '\n') // start of content if (state->buffer.data[state->cursor] == '\n') { // start of content
{
// If we need to parse the header only and we know the content length, // If we need to parse the header only and we know the content length,
// then skip content parsing. // then skip content parsing.
if (state->flags & PSYC_PARSE_ROUTING_ONLY) if (state->flags & PSYC_PARSE_ROUTING_ONLY) {
{
state->part = PSYC_PART_DATA; state->part = PSYC_PART_DATA;
if (++(state->cursor) >= state->buffer.length) if (++(state->cursor) >= state->buffer.length)
return PSYC_PARSE_INSUFFICIENT; return PSYC_PARSE_INSUFFICIENT;
goto PSYC_PART_DATA; goto PSYC_PART_DATA;
} } else
else
state->part = PSYC_PART_CONTENT; state->part = PSYC_PART_CONTENT;
} } else { // Not start of content, this must be the end.
else // Not start of content, this must be the end.
{
// If we have a length then it should've been followed by a \n // If we have a length then it should've been followed by a \n
if (state->contentLengthFound) if (state->contentLengthFound)
return PSYC_PARSE_ERROR_LENGTH; return PSYC_PARSE_ERROR_LENGTH;
@ -256,9 +239,9 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
case PSYC_PART_CONTENT: case PSYC_PART_CONTENT:
// In case of an incomplete binary variable resume parsing it. // In case of an incomplete binary variable resume parsing it.
if (state->valueParsed < state->valueLength) if (state->valueParsed < state->valueLength) {
{ ret = psyc_parse_binary_value(state, value, &(state->valueLength),
ret = psyc_parse_binary_value(state, value, &(state->valueLength), &(state->valueParsed)); &(state->valueParsed));
state->contentParsed += value->length; state->contentParsed += value->length;
if (ret == PARSE_INCOMPLETE) if (ret == PARSE_INCOMPLETE)
@ -269,26 +252,22 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
pos = state->cursor; pos = state->cursor;
if (state->contentParsed > 0) if (state->contentParsed > 0) {
{
if (state->buffer.data[state->cursor] != '\n') if (state->buffer.data[state->cursor] != '\n')
return PSYC_PARSE_ERROR_MOD_NL; return PSYC_PARSE_ERROR_MOD_NL;
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
// Each line of the header starts with a glyph, // Each line of the header starts with a glyph,
// i.e. :_name, -_name +_name etc. // i.e. :_name, -_name +_name etc.
// So just test if the first char is a glyph. // So just test if the first char is a glyph.
// In the body, the same applies, only that the // In the body, the same applies, only that the
// method does not start with a glyph. // method does not start with a glyph.
if (psyc_is_glyph(state->buffer.data[state->cursor])) if (psyc_is_glyph(state->buffer.data[state->cursor])) {
{
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
if (state->contentParsed == 0 && state->buffer.data[state->cursor] == '\n') if (state->contentParsed == 0
{ && state->buffer.data[state->cursor] == '\n') {
*oper = *(state->buffer.data + state->cursor - 1); *oper = *(state->buffer.data + state->cursor - 1);
switch (*oper) switch (*oper) {
{
case PSYC_STATE_RESYNC: case PSYC_STATE_RESYNC:
state->contentParsed += 2; state->contentParsed += 2;
return PSYC_PARSE_STATE_RESYNC; return PSYC_PARSE_STATE_RESYNC;
@ -310,9 +289,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
return PSYC_PARSE_ENTITY; return PSYC_PARSE_ENTITY;
return ret; return ret;
} } else {
else
{
state->contentParsed += state->cursor - pos; state->contentParsed += state->cursor - pos;
state->startc = state->cursor; state->startc = state->cursor;
state->part = PSYC_PART_METHOD; state->part = PSYC_PART_METHOD;
@ -325,8 +302,8 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
if (ret == PARSE_INSUFFICIENT) if (ret == PARSE_INSUFFICIENT)
return ret; return ret;
else if (ret == PARSE_SUCCESS) else if (ret == PARSE_SUCCESS) {
{ // the method ends with a \n then the data follows // The method ends with a \n then the data follows.
if (state->buffer.data[state->cursor] != '\n') if (state->buffer.data[state->cursor] != '\n')
return PSYC_PARSE_ERROR_METHOD; return PSYC_PARSE_ERROR_METHOD;
@ -334,20 +311,16 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
state->valueParsed = 0; state->valueParsed = 0;
state->valueLength = 0; state->valueLength = 0;
if (state->contentLengthFound) if (state->contentLengthFound) {
{ // if length was found set start position to the beginning of data // Length found, set start position to the beginning of data.
state->cursor++; state->cursor++;
state->startc = state->cursor; state->startc = state->cursor;
state->contentParsed += state->cursor - pos; state->contentParsed += state->cursor - pos;
state->part = PSYC_PART_DATA; state->part = PSYC_PART_DATA;
} } else { // Otherwise keep it at the beginning of method.
else // otherwise keep it at the beginning of method
{
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
} }
} } else { // No method, which means the packet should end now.
else // No method, which means the packet should end now.
{
state->part = PSYC_PART_END; state->part = PSYC_PART_END;
state->startc = state->cursor; state->startc = state->cursor;
goto PSYC_PART_END; goto PSYC_PART_END;
@ -359,48 +332,44 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
value->data = state->buffer.data + state->cursor; value->data = state->buffer.data + state->cursor;
value->length = 0; value->length = 0;
if (state->contentLengthFound) // We know the length of the packet. if (state->contentLengthFound) { // We know the length of the packet.
{ if (!state->valueLengthFound) { // start of data
if (!state->valueLengthFound) // start of data
{
state->valueLengthFound = 1; state->valueLengthFound = 1;
state->valueLength = state->contentLength - state->contentParsed; // length of data state->valueLength = state->contentLength - state->contentParsed;
if (state->valueLength && !(state->flags & PSYC_PARSE_ROUTING_ONLY)) if (state->valueLength && !(state->flags & PSYC_PARSE_ROUTING_ONLY))
state->valueLength--; // \n at the end is not part of data state->valueLength--; // \n at the end is not part of data
} }
if (state->valueParsed < state->valueLength) if (state->valueParsed < state->valueLength) {
{ ret = psyc_parse_binary_value(state, value, &(state->valueLength),
ret = psyc_parse_binary_value(state, value, &(state->valueLength), &(state->valueParsed)); &(state->valueParsed));
state->contentParsed += value->length; state->contentParsed += value->length;
if (ret == PARSE_INCOMPLETE) if (ret == PARSE_INCOMPLETE)
return state->valueParsed == value->length ? PSYC_PARSE_BODY_START : PSYC_PARSE_BODY_CONT; return state->valueParsed == value->length
? PSYC_PARSE_BODY_START : PSYC_PARSE_BODY_CONT;
} }
state->part = PSYC_PART_END; state->part = PSYC_PART_END;
return state->valueLength == value->length ? PSYC_PARSE_BODY : PSYC_PARSE_BODY_END; return state->valueLength == value->length ?
} PSYC_PARSE_BODY : PSYC_PARSE_BODY_END;
else // Search for the terminator. } else { // Search for the terminator.
{
size_t datac = state->cursor; // start of data size_t datac = state->cursor; // start of data
if (state->flags & PSYC_PARSE_ROUTING_ONLY) if (state->flags & PSYC_PARSE_ROUTING_ONLY) // in routing-only mode restart
state->startc = datac; // in routing-only mode restart from the start of data state->startc = datac; // from the start of data
while (1) while (1) {
{
uint8_t nl = state->buffer.data[state->cursor] == '\n'; uint8_t nl = state->buffer.data[state->cursor] == '\n';
// check for |\n if we're at the start of data or we have found a \n // check for |\n if we're at the start of data or we have found a \n
if (state->cursor == datac || nl) if (state->cursor == datac || nl) {
{ // incremented cursor inside length?
if (state->cursor+1+nl >= state->buffer.length) // incremented cursor inside length? if (state->cursor + 1 + nl >= state->buffer.length) {
{
state->cursor = state->startc; state->cursor = state->startc;
return PSYC_PARSE_INSUFFICIENT; return PSYC_PARSE_INSUFFICIENT;
} }
if (state->buffer.data[state->cursor+nl] == '|' && if (state->buffer.data[state->cursor + nl] == '|'
state->buffer.data[state->cursor+1+nl] == '\n') // packet ends here && state->buffer.data[state->cursor + 1 + nl] == '\n') {
{ // packet ends here
if (state->flags & PSYC_PARSE_ROUTING_ONLY) if (state->flags & PSYC_PARSE_ROUTING_ONLY)
value->length++; value->length++;
@ -417,9 +386,9 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
case PSYC_PART_END: case PSYC_PART_END:
PSYC_PART_END: PSYC_PART_END:
if (state->contentLengthFound && state->valueLengthFound && state->valueLength && // if data was not empty next is the \n at the end of data
!(state->flags & PSYC_PARSE_ROUTING_ONLY)) if (state->contentLengthFound && state->valueLengthFound
{ // if data was not empty next is the \n at the end of data && state->valueLength && !(state->flags & PSYC_PARSE_ROUTING_ONLY)) {
state->valueLength = 0; state->valueLength = 0;
state->valueLengthFound = 0; state->valueLengthFound = 0;
@ -429,21 +398,18 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
state->contentParsed++; state->contentParsed++;
state->cursor++; state->cursor++;
} }
// End of packet, at this point we have already passed a \n // End of packet, at this point we have already passed a \n
// and the cursor should point to | // and the cursor should point to |
if (state->cursor+1 >= state->buffer.length) // incremented cursor inside length? if (state->cursor + 1 >= state->buffer.length)
return PSYC_PARSE_INSUFFICIENT; return PSYC_PARSE_INSUFFICIENT;
if (state->buffer.data[state->cursor] == '|' && if (state->buffer.data[state->cursor] == '|'
state->buffer.data[state->cursor+1] == '\n') // packet ends here && state->buffer.data[state->cursor + 1] == '\n') {
{ // Packet ends here.
state->cursor += 2; state->cursor += 2;
state->part = PSYC_PART_RESET; state->part = PSYC_PART_RESET;
return PSYC_PARSE_COMPLETE; return PSYC_PARSE_COMPLETE;
} } else { // Packet should've ended here, return error.
else // packet should've ended here, return error
{
state->part = PSYC_PART_RESET; state->part = PSYC_PART_RESET;
return PSYC_PARSE_ERROR_END; return PSYC_PARSE_ERROR_END;
} }
@ -455,59 +421,50 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
#ifdef __INLINE_PSYC_PARSE #ifdef __INLINE_PSYC_PARSE
static inline static inline
#endif #endif
PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem) PsycParseListRC
psyc_parse_list (PsycParseListState *state, PsycString *elem)
{ {
if (state->cursor >= state->buffer.length) if (state->cursor >= state->buffer.length)
return PSYC_PARSE_LIST_INCOMPLETE; return PSYC_PARSE_LIST_INCOMPLETE;
state->startc = state->cursor; state->startc = state->cursor;
if (!state->type) // If type is not set we're at the start if (!state->type) { // If type is not set we're at the start.
{
// First character is either | for text lists, or a number for binary lists // First character is either | for text lists, or a number for binary lists
if (state->buffer.data[state->cursor] == '|') if (state->buffer.data[state->cursor] == '|') {
{
state->type = PSYC_LIST_TEXT; state->type = PSYC_LIST_TEXT;
state->cursor++; state->cursor++;
} } else if (psyc_is_numeric(state->buffer.data[state->cursor]))
else if (psyc_is_numeric(state->buffer.data[state->cursor]))
state->type = PSYC_LIST_BINARY; state->type = PSYC_LIST_BINARY;
else else
return PSYC_PARSE_LIST_ERROR_TYPE; return PSYC_PARSE_LIST_ERROR_TYPE;
} }
if (state->type == PSYC_LIST_TEXT) if (state->type == PSYC_LIST_TEXT) {
{
elem->data = state->buffer.data + state->cursor; elem->data = state->buffer.data + state->cursor;
elem->length = 0; elem->length = 0;
if (state->cursor >= state->buffer.length) if (state->cursor >= state->buffer.length)
return PSYC_PARSE_LIST_END; return PSYC_PARSE_LIST_END;
while (state->buffer.data[state->cursor] != '|') while (state->buffer.data[state->cursor] != '|') {
{
elem->length++; elem->length++;
if (++(state->cursor) >= state->buffer.length) if (++(state->cursor) >= state->buffer.length)
return PSYC_PARSE_LIST_END; return PSYC_PARSE_LIST_END;
} }
state->cursor++; state->cursor++;
return PSYC_PARSE_LIST_ELEM; return PSYC_PARSE_LIST_ELEM;
} } else { // binary list
else // binary list if (!(state->elemParsed < state->elemLength)) {
{
if (!(state->elemParsed < state->elemLength))
{
// Element starts with a number. // Element starts with a number.
if (psyc_is_numeric(state->buffer.data[state->cursor])) if (psyc_is_numeric(state->buffer.data[state->cursor])) {
{ do {
do state->elemLength =
{ 10 * state->elemLength +
state->elemLength = 10 * state->elemLength + state->buffer.data[state->cursor] - '0'; state->buffer.data[state->cursor] - '0';
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_LIST_INCOMPLETE); ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_LIST_INCOMPLETE);
} } while (psyc_is_numeric(state->buffer.data[state->cursor]));
while (psyc_is_numeric(state->buffer.data[state->cursor])); } else
}
else
return PSYC_PARSE_LIST_ERROR_LEN; return PSYC_PARSE_LIST_ERROR_LEN;
if (state->buffer.data[state->cursor] != ' ') if (state->buffer.data[state->cursor] != ' ')
@ -518,12 +475,11 @@ PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem)
elem->length = 0; elem->length = 0;
state->elemParsed = 0; state->elemParsed = 0;
} }
// 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 (PARSE_INCOMPLETE == psyc_parse_binary_value((PsycParseState*)state,
if (psyc_parse_binary_value((PsycParseState*)state, elem, elem, &state->elemLength,
&(state->elemLength), &(state->elemParsed)) == PARSE_INCOMPLETE) &state->elemParsed))
return PSYC_PARSE_LIST_INCOMPLETE; return PSYC_PARSE_LIST_INCOMPLETE;
state->elemLength = 0; state->elemLength = 0;

View file

@ -6,7 +6,8 @@
#ifdef __INLINE_PSYC_RENDER #ifdef __INLINE_PSYC_RENDER
static inline static inline
#endif #endif
PsycRenderListRC psyc_render_list (PsycList *list, char *buffer, size_t buflen) PsycRenderListRC
psyc_render_list (PsycList * list, char *buffer, size_t buflen)
{ {
size_t i, cur = 0; size_t i, cur = 0;
PsycString *elem; PsycString *elem;
@ -14,10 +15,8 @@ PsycRenderListRC psyc_render_list (PsycList *list, char *buffer, size_t buflen)
if (list->length > buflen) // return error if list doesn't fit in buffer if (list->length > buflen) // return error if list doesn't fit in buffer
return PSYC_RENDER_LIST_ERROR; return PSYC_RENDER_LIST_ERROR;
if (list->flag == PSYC_LIST_NEED_LENGTH) if (list->flag == PSYC_LIST_NEED_LENGTH) {
{ for (i = 0; i < list->num_elems; i++) {
for (i = 0; i < list->num_elems; i++)
{
elem = &list->elems[i]; elem = &list->elems[i];
if (i > 0) if (i > 0)
buffer[cur++] = '|'; buffer[cur++] = '|';
@ -26,11 +25,8 @@ PsycRenderListRC psyc_render_list (PsycList *list, char *buffer, size_t buflen)
memcpy(buffer + cur, elem->data, elem->length); memcpy(buffer + cur, elem->data, elem->length);
cur += elem->length; cur += elem->length;
} }
} } else {
else for (i = 0; i < list->num_elems; i++) {
{
for (i = 0; i < list->num_elems; i++)
{
elem = &list->elems[i]; elem = &list->elems[i];
buffer[cur++] = '|'; buffer[cur++] = '|';
memcpy(buffer + cur, elem->data, elem->length); memcpy(buffer + cur, elem->data, elem->length);
@ -38,13 +34,13 @@ PsycRenderListRC psyc_render_list (PsycList *list, char *buffer, size_t buflen)
} }
} }
// actual length should be equal to pre-calculated length at this point // Actual length should be equal to pre-calculated length at this point.
assert(cur == list->length); assert(cur == list->length);
return PSYC_RENDER_LIST_SUCCESS; return PSYC_RENDER_LIST_SUCCESS;
} }
static inline static inline size_t
size_t psyc_render_modifier (PsycModifier *mod, char *buffer) psyc_render_modifier (PsycModifier * mod, char *buffer)
{ {
size_t cur = 0; size_t cur = 0;
@ -54,8 +50,7 @@ size_t psyc_render_modifier (PsycModifier *mod, char *buffer)
if (cur == 1) if (cur == 1)
return cur; // error, name can't be empty return cur; // error, name can't be empty
if (mod->flag == PSYC_MODIFIER_NEED_LENGTH) if (mod->flag == PSYC_MODIFIER_NEED_LENGTH) {
{
buffer[cur++] = ' '; buffer[cur++] = ' ';
cur += itoa(mod->value.length, buffer + cur, 10); cur += itoa(mod->value.length, buffer + cur, 10);
} }
@ -71,7 +66,8 @@ size_t psyc_render_modifier (PsycModifier *mod, char *buffer)
#ifdef __INLINE_PSYC_RENDER #ifdef __INLINE_PSYC_RENDER
static inline static inline
#endif #endif
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, len; size_t i, cur = 0, len;
@ -79,8 +75,7 @@ PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen)
return PSYC_RENDER_ERROR; return PSYC_RENDER_ERROR;
// render routing modifiers // render routing modifiers
for (i = 0; i < packet->routing.lines; i++) for (i = 0; i < packet->routing.lines; i++) {
{
len = psyc_render_modifier(&packet->routing.modifiers[i], buffer + cur); len = psyc_render_modifier(&packet->routing.modifiers[i], buffer + cur);
cur += len; cur += len;
if (len <= 1) if (len <= 1)
@ -91,41 +86,35 @@ PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen)
if (packet->flag == PSYC_PACKET_NEED_LENGTH) if (packet->flag == PSYC_PACKET_NEED_LENGTH)
cur += itoa(packet->contentLength, buffer + cur, 10); cur += itoa(packet->contentLength, buffer + cur, 10);
if (packet->flag == PSYC_PACKET_NEED_LENGTH || packet->content.length || if (packet->flag == PSYC_PACKET_NEED_LENGTH || packet->content.length
packet->stateop || packet->entity.lines || || packet->stateop || packet->entity.lines
packet->method.length || packet->data.length) || packet->method.length || packet->data.length)
buffer[cur++] = '\n'; // start of content part if there's content or length buffer[cur++] = '\n'; // start of content part if there's content or length
if (packet->content.length) // render raw content if present if (packet->content.length) { // render raw content if present
{
memcpy(buffer + cur, packet->content.data, packet->content.length); memcpy(buffer + cur, packet->content.data, packet->content.length);
cur += packet->content.length; cur += packet->content.length;
} } else {
else
{
if (packet->stateop) { if (packet->stateop) {
buffer[cur++] = packet->stateop; buffer[cur++] = packet->stateop;
buffer[cur++] = '\n'; buffer[cur++] = '\n';
} }
// render entity modifiers // render entity modifiers
for (i = 0; i < packet->entity.lines; i++) for (i = 0; i < packet->entity.lines; i++)
cur += psyc_render_modifier(&packet->entity.modifiers[i], buffer + cur); cur += psyc_render_modifier(&packet->entity.modifiers[i],
buffer + cur);
if (packet->method.length) // add method\n if (packet->method.length) { // add method\n
{
memcpy(buffer + cur, packet->method.data, packet->method.length); memcpy(buffer + cur, packet->method.data, packet->method.length);
cur += packet->method.length; cur += packet->method.length;
buffer[cur++] = '\n'; buffer[cur++] = '\n';
if (packet->data.length) // add data\n if (packet->data.length) { // add data\n
{
memcpy(buffer + cur, packet->data.data, packet->data.length); memcpy(buffer + cur, packet->data.data, packet->data.length);
cur += packet->data.length; cur += packet->data.length;
buffer[cur++] = '\n'; buffer[cur++] = '\n';
} }
} } else if (packet->data.length) // error, we have data but no modifier
else if (packet->data.length) // error, we have data but no modifier
return PSYC_RENDER_ERROR_METHOD_MISSING; return PSYC_RENDER_ERROR_METHOD_MISSING;
} }

View file

@ -1,7 +1,8 @@
#include "lib.h" #include "lib.h"
#include <psyc/text.h> #include <psyc/text.h>
PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra) PsycTextRC
psyc_text (PsycTextState *state, PsycTextCB getValue, void *extra)
{ {
const char *start = state->tmpl.data, *end; // start & end of variable name const char *start = state->tmpl.data, *end; // start & end of variable name
const char *prev = state->tmpl.data + state->cursor; const char *prev = state->tmpl.data + state->cursor;
@ -10,8 +11,7 @@ PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra)
size_t len; size_t len;
uint8_t no_subst = (state->cursor == 0); // whether we can return NO_SUBST uint8_t no_subst = (state->cursor == 0); // whether we can return NO_SUBST
while (state->cursor < state->tmpl.length) while (state->cursor < state->tmpl.length) {
{
start = memmem(state->tmpl.data + state->cursor, start = memmem(state->tmpl.data + state->cursor,
state->tmpl.length - state->cursor, state->tmpl.length - state->cursor,
state->open.data, state->open.length); state->open.data, state->open.length);
@ -29,40 +29,39 @@ PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra)
if (!end) if (!end)
break; // ] not found break; // ] not found
if (start + state->open.length == end) if (start + state->open.length == end) {
{
state->cursor += state->close.length; state->cursor += state->close.length;
continue; // [] is invalid, name can't be empty continue; // [] is invalid, name can't be empty
} }
ret = getValue(start + state->open.length, end - start - state->open.length, &value, extra); ret = getValue(start + state->open.length,
end - start - state->open.length, &value, extra);
if (ret < 0) if (ret < 0)
continue; // value not found, no substitution continue; // value not found, no substitution
// first copy the part in the input from the previous subst. to the current one // First copy the part in the input from the previous subst.
// if there's enough buffer space for that // to the current one, if there's enough buffer space for that.
len = start - prev; len = start - prev;
if (state->written + len > state->buffer.length) if (state->written + len > state->buffer.length) {
{
state->cursor = prev - state->tmpl.data; state->cursor = prev - state->tmpl.data;
return PSYC_TEXT_INCOMPLETE; return PSYC_TEXT_INCOMPLETE;
} }
memcpy((void *)(state->buffer.data + state->written), prev, len); memcpy((void *) (state->buffer.data + state->written), prev, len);
state->written += len; state->written += len;
// now substitute the value if there's enough buffer space // Now substitute the value if there's enough buffer space.
if (state->written + value.length > state->buffer.length) if (state->written + value.length > state->buffer.length) {
{
state->cursor = start - state->tmpl.data; state->cursor = start - state->tmpl.data;
return PSYC_TEXT_INCOMPLETE; return PSYC_TEXT_INCOMPLETE;
} }
memcpy((void *)(state->buffer.data + state->written), value.data, value.length); memcpy((void *) (state->buffer.data + state->written), value.data,
value.length);
state->written += value.length; state->written += value.length;
// mark the start of the next chunk of text in the template // Mark the start of the next chunk of text in the template.
prev = state->tmpl.data + state->cursor; prev = state->tmpl.data + state->cursor;
no_subst = 0; no_subst = 0;
} }
@ -70,12 +69,12 @@ PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra)
if (no_subst) if (no_subst)
return PSYC_TEXT_NO_SUBST; return PSYC_TEXT_NO_SUBST;
// copy the rest of the template after the last var // Copy the rest of the template after the last var.
len = state->tmpl.length - (prev - state->tmpl.data); len = state->tmpl.length - (prev - state->tmpl.data);
if (state->written + len > state->buffer.length) if (state->written + len > state->buffer.length)
return PSYC_TEXT_INCOMPLETE; return PSYC_TEXT_INCOMPLETE;
memcpy((void *)(state->buffer.data + state->written), prev, len); memcpy((void *) (state->buffer.data + state->written), prev, len);
state->written += len; state->written += len;
return PSYC_TEXT_COMPLETE; return PSYC_TEXT_COMPLETE;

View file

@ -3,7 +3,8 @@
#include "psyc/uniform.h" #include "psyc/uniform.h"
#include "psyc/parse.h" #include "psyc/parse.h"
int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length) int
psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
{ {
char c; char c;
PsycString *p; PsycString *p;
@ -25,12 +26,10 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
} }
p = &uni->scheme; p = &uni->scheme;
if (p->length == 4 && if (p->length == 4 && (tolower(p->data[0]) == 'p' &&
tolower(p->data[0]) == 'p' &&
tolower(p->data[1]) == 's' && tolower(p->data[1]) == 's' &&
tolower(p->data[2]) == 'y' && tolower(p->data[2]) == 'y' &&
tolower(p->data[3]) == 'c') { tolower(p->data[3]) == 'c')) {
uni->type = PSYC_SCHEME_PSYC; uni->type = PSYC_SCHEME_PSYC;
part = PSYC_UNIFORM_SLASHES; part = PSYC_UNIFORM_SLASHES;
uni->slashes.data = str + pos; uni->slashes.data = str + pos;
@ -42,7 +41,8 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
case PSYC_UNIFORM_SLASHES: case PSYC_UNIFORM_SLASHES:
if (c == '/') if (c == '/')
uni->slashes.length++; uni->slashes.length++;
else return PSYC_PARSE_UNIFORM_INVALID_SLASHES; else
return PSYC_PARSE_UNIFORM_INVALID_SLASHES;
if (uni->slashes.length == 2) { if (uni->slashes.length == 2) {
part = PSYC_UNIFORM_HOST; part = PSYC_UNIFORM_HOST;
@ -69,8 +69,8 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
part = PSYC_UNIFORM_RESOURCE; part = PSYC_UNIFORM_RESOURCE;
p = &uni->resource; p = &uni->resource;
} } else
else return PSYC_PARSE_UNIFORM_INVALID_HOST; return PSYC_PARSE_UNIFORM_INVALID_HOST;
p->data = str + pos + 1; p->data = str + pos + 1;
p->length = 0; p->length = 0;
@ -93,8 +93,7 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
uni->resource.data = str + pos + 1; uni->resource.data = str + pos + 1;
uni->resource.length = 0; uni->resource.length = 0;
break; break;
} } else {
else {
part = PSYC_UNIFORM_TRANSPORT; part = PSYC_UNIFORM_TRANSPORT;
uni->transport.data = str + pos; uni->transport.data = str + pos;
uni->transport.length = 0; uni->transport.length = 0;
@ -135,13 +134,15 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
uni->channel.data = str + pos + 1; uni->channel.data = str + pos + 1;
uni->channel.length = 0; uni->channel.length = 0;
break; break;
} else return PSYC_PARSE_UNIFORM_INVALID_RESOURCE; } else
return PSYC_PARSE_UNIFORM_INVALID_RESOURCE;
case PSYC_UNIFORM_CHANNEL: case PSYC_UNIFORM_CHANNEL:
if (psyc_is_name_char(c)) { if (psyc_is_name_char(c)) {
uni->channel.length++; uni->channel.length++;
break; break;
} else return PSYC_PARSE_UNIFORM_INVALID_CHANNEL; } else
return PSYC_PARSE_UNIFORM_INVALID_CHANNEL;
} }
pos++; pos++;
} }
@ -150,16 +151,19 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
return PSYC_PARSE_UNIFORM_INVALID_HOST; return PSYC_PARSE_UNIFORM_INVALID_HOST;
uni->host_port.data = uni->host.data; uni->host_port.data = uni->host.data;
uni->host_port.length = uni->host.length + uni->port.length + uni->transport.length; uni->host_port.length = uni->host.length + uni->port.length
+ uni->transport.length;
if (uni->port.length > 0 || uni->transport.length > 0) if (uni->port.length > 0 || uni->transport.length > 0)
uni->host_port.length++; uni->host_port.length++;
uni->root.data = str; uni->root.data = str;
uni->root.length = uni->scheme.length + 1 + uni->slashes.length + uni->root.length = uni->scheme.length + 1
uni->host_port.length; + uni->slashes.length + uni->host_port.length;
uni->entity.data = str; uni->entity.data = str;
uni->entity.length = uni->root.length + uni->slash.length + uni->resource.length; uni->entity.length = uni->root.length + uni->slash.length
+ uni->resource.length;
uni->body.data = uni->host.data; uni->body.data = uni->host.data;
uni->body.length = length - uni->scheme.length - 1 - uni->slashes.length; uni->body.length = length - uni->scheme.length - 1 - uni->slashes.length;
@ -169,7 +173,8 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
uni->nick.length = uni->resource.length; uni->nick.length = uni->resource.length;
} }
} else return PSYC_PARSE_UNIFORM_INVALID_SCHEME; } else
return PSYC_PARSE_UNIFORM_INVALID_SCHEME;
if (uni->host.length == 0) if (uni->host.length == 0)
return PSYC_PARSE_UNIFORM_INVALID_HOST; return PSYC_PARSE_UNIFORM_INVALID_HOST;

View file

@ -3,8 +3,7 @@
/// Routing variables in alphabetical order. /// Routing variables in alphabetical order.
const PsycString psyc_routing_vars[] = const PsycString psyc_routing_vars[] = {
{
PSYC_C2STRI("_amount_fragments"), PSYC_C2STRI("_amount_fragments"),
PSYC_C2STRI("_context"), PSYC_C2STRI("_context"),
//PSYC_C2STRI("_count"), // older PSYC //PSYC_C2STRI("_count"), // older PSYC
@ -15,7 +14,8 @@ const PsycString psyc_routing_vars[] =
//PSYC_C2STRI("_source_identification"), // older PSYC //PSYC_C2STRI("_source_identification"), // older PSYC
PSYC_C2STRI("_source_identity"), PSYC_C2STRI("_source_identity"),
PSYC_C2STRI("_source_relay"), PSYC_C2STRI("_source_relay"),
PSYC_C2STRI("_source_relay_relay"), // until you have a better idea.. is this really in use? // until you have a better idea.. is this really in use?
PSYC_C2STRI("_source_relay_relay"),
PSYC_C2STRI("_tag"), PSYC_C2STRI("_tag"),
PSYC_C2STRI("_tag_relay"), PSYC_C2STRI("_tag_relay"),
//PSYC_C2STRI("_tag_reply"), // older PSYC //PSYC_C2STRI("_tag_reply"), // older PSYC
@ -27,8 +27,7 @@ const PsycString psyc_routing_vars[] =
}; };
// Variable types in alphabetical order. // Variable types in alphabetical order.
const PsycDictInt psyc_var_types[] = const PsycDictInt psyc_var_types[] = {
{
{PSYC_C2STRI("_amount"), PSYC_TYPE_AMOUNT}, {PSYC_C2STRI("_amount"), PSYC_TYPE_AMOUNT},
{PSYC_C2STRI("_color"), PSYC_TYPE_COLOR}, {PSYC_C2STRI("_color"), PSYC_TYPE_COLOR},
{PSYC_C2STRI("_date"), PSYC_TYPE_DATE}, {PSYC_C2STRI("_date"), PSYC_TYPE_DATE},
@ -49,8 +48,8 @@ const size_t psyc_var_types_num = PSYC_NUM_ELEM(psyc_var_types);
/** /**
* Get the type of variable name. * Get the type of variable name.
*/ */
inline inline PsycBool
PsycBool psyc_var_is_routing (const char *name, size_t len) psyc_var_is_routing (const char *name, size_t len)
{ {
size_t cursor = 1; size_t cursor = 1;
uint8_t i, m = 0; uint8_t i, m = 0;
@ -60,16 +59,14 @@ PsycBool psyc_var_is_routing (const char *name, size_t len)
return PSYC_FALSE; return PSYC_FALSE;
// first find the vars with matching length // first find the vars with matching length
for (i=0; i<psyc_routing_vars_num; i++) for (i = 0; i < psyc_routing_vars_num; i++)
if (len == psyc_routing_vars[i].length) if (len == psyc_routing_vars[i].length)
matching[m++] = i; matching[m++] = i;
matching[m] = -1; // mark the end of matching indexes matching[m] = -1; // mark the end of matching indexes
while (cursor < len && matching[0] >= 0) while (cursor < len && matching[0] >= 0) {
{ for (i = m = 0; i < psyc_routing_vars_num; i++) {
for (i = m = 0; i < psyc_routing_vars_num; i++)
{
if (matching[i] < 0) if (matching[i] < 0)
break; // reached the end of possible matches break; // reached the end of possible matches
if (psyc_routing_vars[matching[i]].data[cursor] == name[cursor]) if (psyc_routing_vars[matching[i]].data[cursor] == name[cursor])
@ -90,10 +87,11 @@ PsycBool psyc_var_is_routing (const char *name, size_t len)
/** /**
* Get the type of variable name. * Get the type of variable name.
*/ */
inline inline PsycType
PsycType psyc_var_type (const char *name, size_t len) psyc_var_type (const char *name, size_t len)
{ {
int8_t m[psyc_var_types_num]; int8_t m[psyc_var_types_num];
return (PsycType) psyc_dict_lookup((PsycDict*)psyc_var_types, psyc_var_types_num, return (PsycType) psyc_dict_lookup((PsycDict *) psyc_var_types,
name, len, PSYC_YES, (int8_t*)&m); psyc_var_types_num, name, len, PSYC_YES,
(int8_t *) &m);
} }

View file

@ -30,7 +30,9 @@
// cmd line args // cmd line args
extern uint8_t verbose, stats; extern uint8_t verbose, stats;
void check_range(char c, const char *s, int min, int max) { void
check_range (char c, const char *s, int min, int max)
{
int n = atoi(s); int n = atoi(s);
if (n < min) { if (n < min) {
printf("-%c: error, should be >= %d\n", c, min); printf("-%c: error, should be >= %d\n", c, min);
@ -43,14 +45,18 @@ void check_range(char c, const char *s, int min, int max) {
} }
// 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);
} }
void test_file(const char* filename, size_t count, size_t recv_buf_size) { void
test_file (const char* filename, size_t count, size_t recv_buf_size)
{
char *buf, *recvbuf; // cont buf + recv buf: [ ccrrrr] char *buf, *recvbuf; // cont buf + recv buf: [ ccrrrr]
size_t i, nbytes, size; size_t i, nbytes, size;
struct timeval start, end; struct timeval start, end;
@ -112,7 +118,9 @@ void test_file(const char* filename, size_t count, size_t recv_buf_size) {
} }
} }
void test_server(const char* port, size_t count, size_t recv_buf_size) { void
test_server (const char* port, size_t count, size_t recv_buf_size)
{
char buf[CONT_BUF_SIZE + RECV_BUF_SIZE]; // cont buf + recv buf: [ ccrrrr] char buf[CONT_BUF_SIZE + RECV_BUF_SIZE]; // cont buf + recv buf: [ ccrrrr]
char *recvbuf = buf + CONT_BUF_SIZE; // recv buf: ^^^^ char *recvbuf = buf + CONT_BUF_SIZE; // recv buf: ^^^^

View file

@ -42,11 +42,19 @@
#define HELP_P " -P\t\t\tShow progress\n" #define HELP_P " -P\t\t\tShow progress\n"
#define HELP_h " -h\t\t\tShow this help\n" #define HELP_h " -h\t\t\tShow this help\n"
void test_init(int i); void
int test_input(int i, char *recvbuf, size_t nbytes); test_init (int i);
void test_file(const char* filename, size_t count, size_t recv_buf_size); int
void test_server(const char* port, size_t count, size_t recv_buf_size); test_input (int i, char *recvbuf, size_t nbytes);
void check_range(char c, const char *s, int min, int max);
void
test_file (const char* filename, size_t count, size_t recv_buf_size);
void
test_server (const char* port, size_t count, size_t recv_buf_size);
void
check_range (char c, const char *s, int min, int max);
#endif #endif

View file

@ -26,11 +26,15 @@ json_object *obj;
json_tokener *tok; json_tokener *tok;
enum json_tokener_error error; enum json_tokener_error error;
void test_init (int i) { void
test_init (int i)
{
tok = json_tokener_new(); tok = json_tokener_new();
} }
int test_input (int i, char *recvbuf, size_t nbytes) { int
test_input (int i, char *recvbuf, size_t nbytes)
{
size_t cursor = 0; size_t cursor = 0;
int r, ret = 0; int r, ret = 0;
@ -82,7 +86,9 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
return ret; return ret;
} }
int main (int argc, char **argv) { int
main (int argc, char **argv)
{
int c; int c;
while ((c = getopt (argc, argv, "f:p:b:c:mnqsvPSh")) != -1) { while ((c = getopt (argc, argv, "f:p:b:c:mnqsvPSh")) != -1) {
@ -91,8 +97,7 @@ int main (int argc, char **argv) {
CASE_m CASE_n CASE_q CASE_s CASE_m CASE_n CASE_q CASE_s
CASE_v CASE_S CASE_P CASE_v CASE_S CASE_P
case 'h': case 'h':
printf( printf(HELP_FILE("test_json", "mnqSsvP")
HELP_FILE("test_json", "mnqSsvP")
HELP_PORT("test_json", "nqsvP") HELP_PORT("test_json", "nqsvP")
HELP_f HELP_p HELP_b HELP_c HELP_f HELP_p HELP_b HELP_c
HELP_m HELP_n HELP_q HELP_S HELP_m HELP_n HELP_q HELP_S

View file

@ -24,13 +24,17 @@ int exit_code;
JsonParser *parser; JsonParser *parser;
JsonGenerator *generator; JsonGenerator *generator;
void test_init (int i) { void
test_init (int i)
{
g_type_init(); g_type_init();
parser = json_parser_new(); parser = json_parser_new();
generator = json_generator_new(); generator = json_generator_new();
} }
int test_input (int i, char *recvbuf, size_t nbytes) { int
test_input (int i, char *recvbuf, size_t nbytes)
{
JsonNode *root; JsonNode *root;
GError *error = NULL; GError *error = NULL;
char *str; char *str;
@ -77,8 +81,7 @@ int main (int argc, char **argv) {
CASE_c CASE_n CASE_q CASE_c CASE_n CASE_q
CASE_s CASE_v CASE_P CASE_s CASE_v CASE_P
case 'h': case 'h':
printf( printf(HELP_FILE("test_json_glib", "mnqSsvP")
HELP_FILE("test_json_glib", "mnqSsvP")
HELP_PORT("test_json_glib", "nqsvP") HELP_PORT("test_json_glib", "nqsvP")
HELP_f HELP_p HELP_b HELP_c HELP_f HELP_p HELP_b HELP_c
HELP_m HELP_n HELP_q HELP_S HELP_m HELP_n HELP_q HELP_S

View file

@ -2,17 +2,33 @@
#include <lib.h> #include <lib.h>
int main() { int main() {
if (psyc_matches(PSYC_C2ARG("_failure_delivery"), PSYC_C2ARG("_failure_unsuccessful_delivery_death"))) return 1; if (psyc_matches(PSYC_C2ARG("_failure_delivery"),
if (psyc_matches(PSYC_C2ARG("_failure"), PSYC_C2ARG("_failure_unsuccessful_delivery_death"))) return 2; PSYC_C2ARG("_failure_unsuccessful_delivery_death")))
if (psyc_matches(PSYC_C2ARG("_unsuccessful"), PSYC_C2ARG("_failure_unsuccessful_delivery_death"))) return 3; return 1;
unless (psyc_matches(PSYC_C2ARG("_fail"), PSYC_C2ARG("_failure_unsuccessful_delivery_death"))) return 4; if (psyc_matches(PSYC_C2ARG("_failure"),
unless (psyc_matches(PSYC_C2ARG("_truthahn"), PSYC_C2ARG("_failure_unsuccessful_delivery_death"))) return 5; PSYC_C2ARG("_failure_unsuccessful_delivery_death")))
return 2;
if (psyc_matches(PSYC_C2ARG("_unsuccessful"),
PSYC_C2ARG("_failure_unsuccessful_delivery_death")))
return 3;
unless (psyc_matches(PSYC_C2ARG("_fail"),
PSYC_C2ARG("_failure_unsuccessful_delivery_death")))
return 4;
unless (psyc_matches(PSYC_C2ARG("_truthahn"),
PSYC_C2ARG("_failure_unsuccessful_delivery_death")))
return 5;
puts("psyc_matches passed all tests."); puts("psyc_matches passed all tests.");
unless (psyc_inherits(PSYC_C2ARG("_failure_delivery"), PSYC_C2ARG("_failure_unsuccessful_delivery_death"))) return 11; unless (psyc_inherits(PSYC_C2ARG("_failure_delivery"),
if (psyc_inherits(PSYC_C2ARG("_failure_unsuccessful"), PSYC_C2ARG("_failure_unsuccessful_delivery_death"))) return 12; PSYC_C2ARG("_failure_unsuccessful_delivery_death")))
unless (psyc_inherits(PSYC_C2ARG("_fail"), PSYC_C2ARG("_failure_unsuccessful_delivery_death"))) return 13; return 11;
if (psyc_inherits(PSYC_C2ARG("_failure_unsuccessful"),
PSYC_C2ARG("_failure_unsuccessful_delivery_death")))
return 12;
unless (psyc_inherits(PSYC_C2ARG("_fail"),
PSYC_C2ARG("_failure_unsuccessful_delivery_death")))
return 13;
puts("psyc_inherits passed all tests."); puts("psyc_inherits passed all tests.");

View file

@ -5,7 +5,8 @@
#include <psyc.h> #include <psyc.h>
#include <psyc/parse.h> #include <psyc/parse.h>
int main (int argc, char **argv) int
main (int argc, char **argv)
{ {
uint8_t routing_only = argc > 2 && memchr(argv[2], (int)'r', strlen(argv[2])); uint8_t routing_only = argc > 2 && memchr(argv[2], (int)'r', strlen(argv[2]));
uint8_t verbose = argc > 2 && memchr(argv[2], (int)'v', strlen(argv[2])); uint8_t verbose = argc > 2 && memchr(argv[2], (int)'v', strlen(argv[2]));
@ -33,8 +34,7 @@ int main (int argc, char **argv)
psyc_parse_buffer_set(&state, buffer, idx); psyc_parse_buffer_set(&state, buffer, idx);
// try parsing that now // try parsing that now
do do {
{
oper = 0; oper = 0;
name.length = 0; name.length = 0;
value.length = 0; value.length = 0;
@ -43,8 +43,7 @@ int main (int argc, char **argv)
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)
@ -56,18 +55,15 @@ int main (int argc, char **argv)
(int)name.length, name.data, (int)name.length, name.data,
(int)value.length, value.data); (int)value.length, value.data);
if (psyc_var_is_list(PSYC_S2ARG(name))) if (psyc_var_is_list(PSYC_S2ARG(name))) {
{
if (verbose) if (verbose)
printf(">> LIST START\n"); printf(">> LIST START\n");
psyc_parse_list_state_init(&listState); psyc_parse_list_state_init(&listState);
psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(value)); psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(value));
while ((ret = psyc_parse_list(&listState, &elem))) while ((ret = psyc_parse_list(&listState, &elem))) {
{ switch (ret) {
switch (ret)
{
case PSYC_PARSE_LIST_END: case PSYC_PARSE_LIST_END:
case PSYC_PARSE_LIST_ELEM: case PSYC_PARSE_LIST_ELEM:
if (verbose) if (verbose)
@ -78,8 +74,7 @@ int main (int argc, char **argv)
return 1; return 1;
} }
if (ret == PSYC_PARSE_LIST_END) if (ret == PSYC_PARSE_LIST_END) {
{
if (verbose) if (verbose)
printf(">> LIST END\n"); printf(">> LIST END\n");
break; break;

View file

@ -31,14 +31,16 @@ PsycModifier entity[NUM_PARSERS][ENTITY_LINES];
int contbytes, exit_code; int contbytes, exit_code;
static inline static inline void
void resetString (PsycString *s, uint8_t freeptr); resetString (PsycString *s, uint8_t freeptr);
// initialize parser & packet variables // initialize parser & packet variables
void test_init (int i) { void
test_init (int i)
{
// reset parser state & packet // reset parser state & packet
psyc_parse_state_init(&parsers[i], routing_only ? psyc_parse_state_init(&parsers[i],
PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL); routing_only ? PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL);
memset(&packets[i], 0, sizeof(PsycPacket)); memset(&packets[i], 0, sizeof(PsycPacket));
memset(&routing[i], 0, sizeof(PsycModifier) * ROUTING_LINES); memset(&routing[i], 0, sizeof(PsycModifier) * ROUTING_LINES);
@ -48,7 +50,9 @@ void test_init (int i) {
} }
// parse & render input // parse & render input
int test_input (int i, char *recvbuf, size_t nbytes) { int
test_input (int i, char *recvbuf, size_t nbytes)
{
int j, ret, retl, r; int j, ret, retl, r;
char sendbuf[SEND_BUF_SIZE]; char sendbuf[SEND_BUF_SIZE];
char *parsebuf = recvbuf - contbytes; char *parsebuf = recvbuf - contbytes;
@ -82,7 +86,8 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
do { do {
if (verbose >= 3) if (verbose >= 3)
printf("\n# buffer = [%.*s]\n# part = %d\n", (int)parser->buffer.length, parser->buffer.data, parser->part); printf("\n# buffer = [%.*s]\n# part = %d\n",
(int)parser->buffer.length, parser->buffer.data, parser->part);
// Parse the next part of the packet (a routing/entity modifier or the body) // Parse the next part of the packet (a routing/entity modifier or the body)
ret = exit_code = psyc_parse(parser, &oper, &name, &value); ret = exit_code = psyc_parse(parser, &oper, &name, &value);
if (verbose >= 2) if (verbose >= 2)
@ -146,12 +151,14 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
psyc_packet_length_set(packet); psyc_packet_length_set(packet);
if (psyc_render(packet, sendbuf, SEND_BUF_SIZE) == PSYC_RENDER_SUCCESS) { if (PSYC_RENDER_SUCCESS == psyc_render(packet, sendbuf,
SEND_BUF_SIZE)) {
if (!quiet) { if (!quiet) {
if (filename && write(1, sendbuf, packet->length) == -1) { if (filename && write(1, sendbuf, packet->length) == -1) {
perror("write"); perror("write");
ret = -1; ret = -1;
} else if (!filename && send(i, sendbuf, packet->length, 0) == -1) { } else if (!filename && -1 == send(i, sendbuf,
packet->length, 0)) {
perror("send"); perror("send");
ret = -1; ret = -1;
} }
@ -197,9 +204,11 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
if (contbytes > 0) { // copy end of parsebuf before start of recvbuf if (contbytes > 0) { // copy end of parsebuf before start of recvbuf
if (verbose >= 3) if (verbose >= 3)
printf("# remaining = [%.*s]\n", (int)contbytes, psyc_parse_remaining_buffer(parser)); printf("# remaining = [%.*s]\n",
assert(contbytes <= CONT_BUF_SIZE); // make sure it's still in the buffer (int)contbytes, psyc_parse_remaining_buffer(parser));
memmove(recvbuf - contbytes, psyc_parse_remaining_buffer(parser), contbytes); assert(contbytes <= CONT_BUF_SIZE); // make sure it fits in the buffer
memmove(recvbuf - contbytes,
psyc_parse_remaining_buffer(parser), contbytes);
} }
ret = 0; ret = 0;
break; break;
@ -312,8 +321,8 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
return ret; return ret;
} }
static inline static inline void
void resetString (PsycString *s, uint8_t freeptr) resetString (PsycString *s, uint8_t freeptr)
{ {
if (freeptr && s->length) if (freeptr && s->length)
free((void*)s->data); free((void*)s->data);
@ -322,7 +331,9 @@ void resetString (PsycString *s, uint8_t freeptr)
s->length = 0; s->length = 0;
} }
int main (int argc, char **argv) { int
main (int argc, char **argv)
{
int c; int c;
while ((c = getopt (argc, argv, "f:p:b:c:mnqrsvPSh")) != -1) { while ((c = getopt (argc, argv, "f:p:b:c:mnqrsvPSh")) != -1) {
switch (c) { switch (c) {
@ -330,8 +341,7 @@ int main (int argc, char **argv) {
CASE_m CASE_n CASE_q CASE_r CASE_m CASE_n CASE_q CASE_r
CASE_s CASE_v CASE_S CASE_P CASE_s CASE_v CASE_S CASE_P
case 'h': case 'h':
printf( printf(HELP_FILE("test_psyc", "mnqrSsvP")
HELP_FILE("test_psyc", "mnqrSsvP")
HELP_PORT("test_psyc", "nqrsvP") HELP_PORT("test_psyc", "nqrsvP")
HELP_f HELP_p HELP_b HELP_c HELP_f HELP_p HELP_b HELP_c
HELP_m HELP_n HELP_r HELP_m HELP_n HELP_r

View file

@ -25,12 +25,16 @@ size_t count = 1, recv_buf_size;
PsycParseState parser; PsycParseState parser;
void test_init (int i) { void
psyc_parse_state_init(&parser, routing_only ? test_init (int i)
PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL); {
psyc_parse_state_init(&parser, routing_only
? PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL);
} }
int test_input (int i, char *recvbuf, size_t nbytes) { int
test_input (int i, char *recvbuf, size_t nbytes)
{
char oper; char oper;
PsycString name, value; PsycString name, value;
int ret; int ret;
@ -44,15 +48,15 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
} }
} }
int main (int argc, char **argv) { int
main (int argc, char **argv)
{
int c; int c;
while ((c = getopt (argc, argv, "f:p:b:c:rsh")) != -1) { while ((c = getopt (argc, argv, "f:p:b:c:rsh")) != -1) {
switch (c) { switch (c) {
CASE_f CASE_p CASE_b CASE_c CASE_f CASE_p CASE_b CASE_c CASE_r CASE_s
CASE_r CASE_s
case 'h': case 'h':
printf( printf(HELP_FILE("test_psyc_speed", "rs")
HELP_FILE("test_psyc_speed", "rs")
HELP_PORT("test_psyc_speed", "rs") HELP_PORT("test_psyc_speed", "rs")
HELP_f HELP_p HELP_b HELP_c HELP_f HELP_p HELP_b HELP_c
HELP_r HELP_s HELP_h, HELP_r HELP_s HELP_h,

View file

@ -7,7 +7,8 @@
#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, int
testPresence (const char *avail, int availlen,
const char *desc, int desclen, const char *desc, int desclen,
const char *rendered, uint8_t verbose) const char *rendered, uint8_t verbose)
{ {
@ -40,7 +41,8 @@ int testPresence (const char *avail, int availlen,
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[2]; PsycModifier routing[2];
psyc_modifier_init(&routing[0], PSYC_OPERATOR_SET, psyc_modifier_init(&routing[0], PSYC_OPERATOR_SET,
@ -63,8 +65,10 @@ int testList (const char *rendered, uint8_t verbose)
}; };
PsycList list_text, list_bin; PsycList list_text, list_bin;
psyc_list_init(&list_text, elems_text, PSYC_NUM_ELEM(elems_text), PSYC_LIST_CHECK_LENGTH); psyc_list_init(&list_text, elems_text,
psyc_list_init(&list_bin, elems_bin, PSYC_NUM_ELEM(elems_bin), PSYC_LIST_CHECK_LENGTH); PSYC_NUM_ELEM(elems_text), PSYC_LIST_CHECK_LENGTH);
psyc_list_init(&list_bin, elems_bin,
PSYC_NUM_ELEM(elems_bin), PSYC_LIST_CHECK_LENGTH);
char buf_text[32], buf_bin[32]; char buf_text[32], buf_bin[32];
psyc_render_list(&list_text, buf_text, sizeof(buf_text)); psyc_render_list(&list_text, buf_text, sizeof(buf_text));
@ -93,7 +97,9 @@ int testList (const char *rendered, uint8_t verbose)
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

@ -21,10 +21,15 @@ size_t count = 1, recv_buf_size;
int exit_code; int exit_code;
void test_init (int i) { void
test_init (int i)
{
} }
int test_input (int i, char *recvbuf, size_t nbytes) { int
test_input (int i, char *recvbuf, size_t nbytes)
{
size_t len = strnlen(recvbuf, nbytes); size_t len = strnlen(recvbuf, nbytes);
if (!len) { if (!len) {
@ -35,16 +40,16 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
return 0; return 0;
} }
int main (int argc, char **argv) { int
main (int argc, char **argv)
{
int c; int c;
while ((c = getopt (argc, argv, "f:p:b:c:sh")) != -1) { while ((c = getopt (argc, argv, "f:p:b:c:sh")) != -1) {
switch (c) { switch (c) {
CASE_f CASE_p CASE_b CASE_c CASE_f CASE_p CASE_b CASE_c CASE_s
CASE_s
case 'h': case 'h':
printf( printf(HELP_FILE("test_strlen", "s")
HELP_FILE("test_strlen", "s")
HELP_PORT("test_strlen", "s") HELP_PORT("test_strlen", "s")
HELP_f HELP_p HELP_b HELP_c HELP_f HELP_p HELP_b HELP_c
HELP_s HELP_h, HELP_s HELP_h,

View file

@ -7,7 +7,8 @@
uint8_t verbose; uint8_t verbose;
PsycTextValueRC getValueFooBar (const char *name, size_t len, PsycString *value, void *extra) PsycTextValueRC
getValueFooBar (const char *name, size_t len, PsycString *value, void *extra)
{ {
if (verbose) if (verbose)
printf("> getValue: %.*s\n", (int)len, name); printf("> getValue: %.*s\n", (int)len, name);
@ -16,7 +17,8 @@ PsycTextValueRC getValueFooBar (const char *name, size_t len, PsycString *value,
return PSYC_TEXT_VALUE_FOUND; return PSYC_TEXT_VALUE_FOUND;
} }
PsycTextValueRC getValueEmpty (const char *name, size_t len, PsycString *value, void *extra) PsycTextValueRC
getValueEmpty (const char *name, size_t len, PsycString *value, void *extra)
{ {
if (verbose) if (verbose)
printf("> getValue: %.*s\n", (int)len, name); printf("> getValue: %.*s\n", (int)len, name);
@ -25,26 +27,27 @@ PsycTextValueRC getValueEmpty (const char *name, size_t len, PsycString *value,
return PSYC_TEXT_VALUE_FOUND; return PSYC_TEXT_VALUE_FOUND;
} }
PsycTextValueRC getValueNotFound (const char *name, size_t len, PsycString *value, void *extra) PsycTextValueRC
getValueNotFound (const char *name, size_t len, PsycString *value, void *extra)
{ {
if (verbose) if (verbose)
printf("> getValue: %.*s\n", (int)len, name); printf("> getValue: %.*s\n", (int)len, name);
return PSYC_TEXT_VALUE_NOT_FOUND; return PSYC_TEXT_VALUE_NOT_FOUND;
} }
int testText (char *template, size_t tmplen, char *buffer, size_t buflen, PsycString *result, PsycTextCB getValue) int
testText (char *template, size_t tmplen, char *buffer, size_t buflen,
PsycString *result, PsycTextCB getValue)
{ {
PsycTextState state; PsycTextState state;
size_t length = 0; size_t length = 0;
PsycTextRC ret; PsycTextRC ret;
psyc_text_state_init(&state, template, tmplen, buffer, buflen); psyc_text_state_init(&state, template, tmplen, buffer, buflen);
do do {
{
ret = psyc_text(&state, getValue, NULL); ret = psyc_text(&state, getValue, NULL);
length += psyc_text_bytes_written(&state); length += psyc_text_bytes_written(&state);
switch (ret) switch (ret) {
{
case PSYC_TEXT_INCOMPLETE: case PSYC_TEXT_INCOMPLETE:
if (verbose) if (verbose)
printf("# %.*s...\n", (int)length, buffer); printf("# %.*s...\n", (int)length, buffer);
@ -67,7 +70,8 @@ int testText (char *template, size_t tmplen, char *buffer, size_t buflen, PsycSt
return -2; // shouldn't be reached return -2; // shouldn't be reached
} }
int main(int argc, char **argv) int
main (int argc, char **argv)
{ {
verbose = argc > 1; verbose = argc > 1;
char buffer[BUFSIZE]; char buffer[BUFSIZE];
@ -85,11 +89,11 @@ int main(int argc, char **argv)
if (memcmp(result.data, PSYC_C2ARG("Hello & !"))) if (memcmp(result.data, PSYC_C2ARG("Hello & !")))
return 2; return 2;
if (testText(str, len, buffer, BUFSIZE, &result, &getValueNotFound) != PSYC_TEXT_NO_SUBST) if (PSYC_TEXT_NO_SUBST != testText(str, len, buffer, BUFSIZE,
&result, &getValueNotFound))
return 3; return 3;
for (i = 1; i < 22; i++) for (i = 1; i < 22; i++) {
{
testText(str, len, buffer, i, &result, &getValueFooBar); testText(str, len, buffer, i, &result, &getValueFooBar);
if (memcmp(result.data, PSYC_C2ARG("Hello Foo Bar & Foo Bar!"))) if (memcmp(result.data, PSYC_C2ARG("Hello Foo Bar & Foo Bar!")))
return 10 + i; return 10 + i;

View file

@ -4,13 +4,15 @@
#include <lib.h> #include <lib.h>
void void
testUniform (char *str, int ret) { testUniform (char *str, int ret)
{
PsycUniform *uni = malloc(sizeof(PsycUniform)); PsycUniform *uni = malloc(sizeof(PsycUniform));
memset(uni, 0, sizeof(PsycUniform)); memset(uni, 0, sizeof(PsycUniform));
printf("%s\n", str); printf("%s\n", str);
int r = psyc_uniform_parse(uni, str, strlen(str)); int r = psyc_uniform_parse(uni, str, strlen(str));
PP(("[%.*s] : [%.*s] [%.*s] : [%.*s] [%.*s] / [%.*s] # [%.*s]\n[%.*s]\n[%.*s] [%.*s]\n[%.*s]\n\n", PP(("[%.*s] : [%.*s] [%.*s] : [%.*s] [%.*s] / "
"[%.*s] # [%.*s]\n[%.*s]\n[%.*s] [%.*s]\n[%.*s]\n\n",
(int)PSYC_S2ARG2(uni->scheme), (int)PSYC_S2ARG2(uni->scheme),
(int)PSYC_S2ARG2(uni->slashes), (int)PSYC_S2ARG2(uni->slashes),
(int)PSYC_S2ARG2(uni->host), (int)PSYC_S2ARG2(uni->host),
@ -25,12 +27,15 @@ testUniform (char *str, int ret) {
free(uni); free(uni);
if (r != ret) { if (r != ret) {
fprintf(stderr, "ERROR: psyc_uniform_parse returned %d instead of %d\n", r, ret); fprintf(stderr, "ERROR: psyc_uniform_parse returned %d instead of %d\n",
r, ret);
exit(1); exit(1);
} }
} }
int main () { int
main ()
{
testUniform("psyc://foo.tld:4404d/@bar#baz", PSYC_SCHEME_PSYC); testUniform("psyc://foo.tld:4404d/@bar#baz", PSYC_SCHEME_PSYC);
testUniform("psyc://foo:4405/~bar", PSYC_SCHEME_PSYC); testUniform("psyc://foo:4405/~bar", PSYC_SCHEME_PSYC);
testUniform("psyc://foo:1234", PSYC_SCHEME_PSYC); testUniform("psyc://foo:1234", PSYC_SCHEME_PSYC);
@ -47,7 +52,8 @@ int main () {
testUniform("psyc://:123/", PSYC_PARSE_UNIFORM_INVALID_HOST); testUniform("psyc://:123/", PSYC_PARSE_UNIFORM_INVALID_HOST);
testUniform("psyc://host:/~foo", PSYC_PARSE_UNIFORM_INVALID_PORT); testUniform("psyc://host:/~foo", PSYC_PARSE_UNIFORM_INVALID_PORT);
testUniform("psyc://host:d/~foo", PSYC_PARSE_UNIFORM_INVALID_PORT); testUniform("psyc://host:d/~foo", PSYC_PARSE_UNIFORM_INVALID_PORT);
testUniform("psyc://1234567890abcdef:1g/~foo", PSYC_PARSE_UNIFORM_INVALID_TRANSPORT); testUniform("psyc://1234567890abcdef:1g/~foo",
PSYC_PARSE_UNIFORM_INVALID_TRANSPORT);
printf("SUCCESS: psyc_uniform_parse passed all tests.\n"); printf("SUCCESS: psyc_uniform_parse passed all tests.\n");
return 0; return 0;

View file

@ -6,8 +6,7 @@
int main() { int main() {
#if 0 #if 0
const char* vars[] = const char* vars[] = {
{
"_source", "_source",
"_source_relay", "_source_relay",
"_source_foo", "_source_foo",
@ -18,8 +17,7 @@ int main() {
}; };
int i; int i;
for (i = 0; i < sizeof(vars) / sizeof(*vars); i++) for (i = 0; i < sizeof(vars) / sizeof(*vars); i++) {
{
printf(">> %s: %d %d\n", vars[i], sizeof(vars[i]), sizeof(*vars[i])); printf(">> %s: %d %d\n", vars[i], sizeof(vars[i]), sizeof(*vars[i]));
printf("%s: %d\n", vars[i], psyc_var_is_routing(vars[i], strlen(vars[i]))); printf("%s: %d\n", vars[i], psyc_var_is_routing(vars[i], strlen(vars[i])));
} }