mirror of
git://git.psyc.eu/libpsyc
synced 2024-08-15 03:19:02 +00:00
indent
This commit is contained in:
parent
aee9203df6
commit
1cc58abd0e
31 changed files with 2797 additions and 2759 deletions
3
Makefile
3
Makefile
|
@ -1,6 +1,9 @@
|
|||
.PHONY: doc test bench
|
||||
.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:
|
||||
${MAKE} -C src
|
||||
|
||||
|
|
|
@ -15,6 +15,7 @@
|
|||
// * @subsection step1 Step 1: Opening the box
|
||||
|
||||
#ifndef PSYC_H
|
||||
#define PSYC_H
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
|
@ -33,8 +34,7 @@
|
|||
#define PSYC_NUM_ELEM(a) (sizeof(a) / sizeof(*(a)))
|
||||
|
||||
/// Boolean: true/false, yes/no.
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
PSYC_FALSE = 0,
|
||||
PSYC_TRUE = 1,
|
||||
PSYC_NO = 0,
|
||||
|
@ -42,15 +42,13 @@ typedef enum
|
|||
} PsycBool;
|
||||
|
||||
/// Return code: OK/error.
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
PSYC_OK = 1,
|
||||
PSYC_ERROR = -1,
|
||||
} PsycRC;
|
||||
|
||||
/// PSYC packet parts.
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
PSYC_PART_RESET = -1,
|
||||
PSYC_PART_ROUTING = 0,
|
||||
PSYC_PART_LENGTH = 1,
|
||||
|
@ -68,8 +66,7 @@ typedef enum
|
|||
* validity. Other variable types are treated
|
||||
* as opaque data.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
PSYC_TYPE_UNKNOWN,
|
||||
PSYC_TYPE_AMOUNT,
|
||||
PSYC_TYPE_COLOR,
|
||||
|
@ -89,8 +86,7 @@ typedef enum
|
|||
* List types.
|
||||
* Possible types are text and binary.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
PSYC_LIST_TEXT = 1,
|
||||
PSYC_LIST_BINARY = 2,
|
||||
} PsycListType;
|
||||
|
@ -100,22 +96,19 @@ typedef enum
|
|||
*
|
||||
* Contains pointer and length for a buffer.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
/// Length of the data pointed to by ptr
|
||||
size_t length;
|
||||
/// pointer to the data
|
||||
char *data;
|
||||
} PsycString;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
PsycString key;
|
||||
void *value;
|
||||
} PsycDict;
|
||||
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
PsycString key;
|
||||
intptr_t value;
|
||||
} PsycDictInt;
|
||||
|
@ -123,14 +116,14 @@ typedef struct
|
|||
/**
|
||||
* Checks if long keyword string inherits from short keyword string.
|
||||
*/
|
||||
int psyc_inherits (char *sho, size_t slen,
|
||||
char *lon, size_t llen);
|
||||
int
|
||||
psyc_inherits (char *sho, size_t slen, char *lon, size_t llen);
|
||||
|
||||
/**
|
||||
* Checks if short keyword string matches long keyword string.
|
||||
*/
|
||||
int psyc_matches (char *sho, size_t slen,
|
||||
char *lon, size_t llen);
|
||||
int
|
||||
psyc_matches (char *sho, size_t slen, char *lon, size_t llen);
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
|
||||
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,
|
||||
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.
|
||||
* @see psyc_dict_lookup
|
||||
*/
|
||||
static inline
|
||||
intptr_t psyc_dict_lookup_int (const PsycDictInt *dict, size_t size,
|
||||
static inline intptr_t
|
||||
psyc_dict_lookup_int (const PsycDictInt * dict, size_t size,
|
||||
const char *key, size_t keylen,
|
||||
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"
|
||||
|
||||
#define PSYC_H
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#ifndef PSYC_PACKET_H
|
||||
#define PSYC_PACKET_H
|
||||
|
||||
/**
|
||||
* @file psyc/packet.h
|
||||
|
@ -20,8 +21,7 @@
|
|||
#include <math.h>
|
||||
|
||||
/** Modifier flags. */
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
/// Modifier needs to be checked if it needs length.
|
||||
PSYC_MODIFIER_CHECK_LENGTH = 0,
|
||||
/// Modifier needs length.
|
||||
|
@ -33,8 +33,7 @@ typedef enum
|
|||
} PsycModifierFlag;
|
||||
|
||||
/** List flags. */
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
/// List needs to be checked if it needs length.
|
||||
PSYC_LIST_CHECK_LENGTH = 0,
|
||||
/// List needs length.
|
||||
|
@ -44,8 +43,7 @@ typedef enum
|
|||
} PsycListFlag;
|
||||
|
||||
/** Packet flags. */
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
/// Packet needs to be checked if it needs content length.
|
||||
PSYC_PACKET_CHECK_LENGTH = 0,
|
||||
/// Packet needs content length.
|
||||
|
@ -54,8 +52,7 @@ typedef enum
|
|||
PSYC_PACKET_NO_LENGTH = 2,
|
||||
} PsycPacketFlag;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
PSYC_OPERATOR_SET = ':',
|
||||
PSYC_OPERATOR_ASSIGN = '=',
|
||||
PSYC_OPERATOR_AUGMENT = '+',
|
||||
|
@ -63,16 +60,14 @@ typedef enum
|
|||
PSYC_OPERATOR_QUERY = '?',
|
||||
} PsycOperator;
|
||||
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
PSYC_STATE_NOOP = 0,
|
||||
PSYC_STATE_RESET = '=',
|
||||
PSYC_STATE_RESYNC = '?',
|
||||
} PsycStateOp;
|
||||
|
||||
/** Structure for a modifier. */
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
char oper;
|
||||
PsycString name;
|
||||
PsycString value;
|
||||
|
@ -80,15 +75,13 @@ typedef struct
|
|||
} PsycModifier;
|
||||
|
||||
/** Structure for an entity or routing header. */
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
size_t lines;
|
||||
PsycModifier *modifiers;
|
||||
} PsycHeader;
|
||||
|
||||
/** Structure for a list. */
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
size_t num_elems;
|
||||
PsycString *elems;
|
||||
size_t length;
|
||||
|
@ -96,8 +89,7 @@ typedef struct
|
|||
} PsycList;
|
||||
|
||||
/** Intermediate struct for a PSYC packet */
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
PsycHeader routing; ///< Routing header.
|
||||
PsycHeader entity; ///< Entity header.
|
||||
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.
|
||||
*/
|
||||
static inline
|
||||
size_t psyc_num_length (size_t n)
|
||||
static inline size_t
|
||||
psyc_num_length (size_t n)
|
||||
{
|
||||
return n < 10 ? 1 : log10(n) + 1;
|
||||
}
|
||||
|
@ -123,14 +115,14 @@ size_t psyc_num_length (size_t n)
|
|||
* \internal
|
||||
* Check if a modifier needs length.
|
||||
*/
|
||||
static inline
|
||||
PsycModifierFlag psyc_modifier_length_check (PsycModifier *m)
|
||||
static inline PsycModifierFlag
|
||||
psyc_modifier_length_check (PsycModifier *m)
|
||||
{
|
||||
PsycModifierFlag flag;
|
||||
|
||||
if (m->value.length > PSYC_MODIFIER_SIZE_THRESHOLD)
|
||||
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;
|
||||
else
|
||||
flag = PSYC_MODIFIER_NO_LENGTH;
|
||||
|
@ -139,11 +131,10 @@ PsycModifierFlag psyc_modifier_length_check (PsycModifier *m)
|
|||
}
|
||||
|
||||
/** Initialize modifier */
|
||||
static inline
|
||||
void psyc_modifier_init (PsycModifier *m, char oper,
|
||||
static inline void
|
||||
psyc_modifier_init (PsycModifier *m, char oper,
|
||||
char *name, size_t namelen,
|
||||
char *value, size_t valuelen,
|
||||
PsycModifierFlag flag)
|
||||
char *value, size_t valuelen, PsycModifierFlag flag)
|
||||
{
|
||||
*m = (PsycModifier) {oper, {namelen, name}, {valuelen, value}, flag};
|
||||
|
||||
|
@ -155,37 +146,44 @@ void psyc_modifier_init (PsycModifier *m, char oper,
|
|||
* \internal
|
||||
* Get the total length of a modifier when rendered.
|
||||
*/
|
||||
size_t psyc_modifier_length (PsycModifier *m);
|
||||
size_t
|
||||
psyc_modifier_length (PsycModifier *m);
|
||||
|
||||
/**
|
||||
* \internal
|
||||
* Check if a list needs length.
|
||||
*/
|
||||
PsycListFlag psyc_list_length_check (PsycList *list);
|
||||
PsycListFlag
|
||||
psyc_list_length_check (PsycList *list);
|
||||
|
||||
/**
|
||||
* \internal
|
||||
* Get the total length of a list when rendered.
|
||||
*/
|
||||
PsycListFlag psyc_list_length (PsycList *list);
|
||||
PsycListFlag
|
||||
psyc_list_length (PsycList *list);
|
||||
|
||||
/**
|
||||
* \internal
|
||||
* 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.
|
||||
*/
|
||||
size_t psyc_packet_length_set (PsycPacket *p);
|
||||
size_t
|
||||
psyc_packet_length_set (PsycPacket *p);
|
||||
|
||||
/** 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);
|
||||
|
||||
/** Initialize packet. */
|
||||
void psyc_packet_init (PsycPacket *packet,
|
||||
void
|
||||
psyc_packet_init (PsycPacket *packet,
|
||||
PsycModifier *routing, size_t routinglen,
|
||||
PsycModifier *entity, size_t entitylen,
|
||||
char *method, size_t methodlen,
|
||||
|
@ -193,12 +191,12 @@ void psyc_packet_init (PsycPacket *packet,
|
|||
char stateop, PsycPacketFlag flag);
|
||||
|
||||
/** Initialize packet with raw content. */
|
||||
void psyc_packet_init_raw (PsycPacket *packet,
|
||||
void
|
||||
psyc_packet_init_raw (PsycPacket *packet,
|
||||
PsycModifier *routing, size_t routinglen,
|
||||
char *content, size_t contentlen,
|
||||
PsycPacketFlag flag);
|
||||
|
||||
/** @} */ // end of packet group
|
||||
|
||||
#define PSYC_PACKET_H
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#ifndef PSYC_PARSE_H
|
||||
#define 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 contentParsed; ///< Number of bytes parsed from the content so far.
|
||||
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 valueLength; ///< Expected length of the value.
|
||||
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.
|
||||
* @see PsycParseFlag
|
||||
*/
|
||||
static inline
|
||||
void psyc_parse_state_init (PsycParseState *state, uint8_t flags)
|
||||
static inline void
|
||||
psyc_parse_state_init (PsycParseState *state, uint8_t flags)
|
||||
{
|
||||
memset(state, 0, sizeof(PsycParseState));
|
||||
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
|
||||
* @see PsycString
|
||||
*/
|
||||
static inline
|
||||
void psyc_parse_buffer_set (PsycParseState *state, char *buffer, size_t length)
|
||||
static inline void
|
||||
psyc_parse_buffer_set (PsycParseState *state, char *buffer,
|
||||
size_t length)
|
||||
{
|
||||
state->buffer = (PsycString) {length, buffer};
|
||||
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.
|
||||
*/
|
||||
static inline
|
||||
void psyc_parse_list_state_init (PsycParseListState *state)
|
||||
static inline void
|
||||
psyc_parse_list_state_init (PsycParseListState *state)
|
||||
{
|
||||
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.
|
||||
*/
|
||||
static inline
|
||||
void psyc_parse_list_buffer_set (PsycParseListState *state, char *buffer, size_t length)
|
||||
static inline void
|
||||
psyc_parse_list_buffer_set (PsycParseListState *state, char *buffer, size_t length)
|
||||
{
|
||||
state->buffer = (PsycString) {length, buffer};
|
||||
state->cursor = 0;
|
||||
}
|
||||
|
||||
static inline
|
||||
size_t psyc_parse_content_length (PsycParseState *state)
|
||||
static inline size_t
|
||||
psyc_parse_content_length (PsycParseState *state)
|
||||
{
|
||||
return state->contentLength;
|
||||
}
|
||||
|
||||
static inline
|
||||
PsycBool psyc_parse_content_length_found (PsycParseState *state)
|
||||
static inline PsycBool
|
||||
psyc_parse_content_length_found (PsycParseState *state)
|
||||
{
|
||||
return state->contentLengthFound;
|
||||
}
|
||||
|
||||
static inline
|
||||
size_t psyc_parse_value_length (PsycParseState *state)
|
||||
static inline size_t
|
||||
psyc_parse_value_length (PsycParseState *state)
|
||||
{
|
||||
return state->valueLength;
|
||||
}
|
||||
|
||||
static inline
|
||||
PsycBool psyc_parse_value_length_found (PsycParseState *state)
|
||||
static inline PsycBool
|
||||
psyc_parse_value_length_found (PsycParseState *state)
|
||||
{
|
||||
return state->valueLengthFound;
|
||||
}
|
||||
|
||||
static inline
|
||||
size_t psyc_parse_cursor (PsycParseState *state)
|
||||
static inline size_t
|
||||
psyc_parse_cursor (PsycParseState *state)
|
||||
{
|
||||
return state->cursor;
|
||||
}
|
||||
|
||||
static inline
|
||||
size_t psyc_parse_buffer_length (PsycParseState *state)
|
||||
static inline size_t
|
||||
psyc_parse_buffer_length (PsycParseState *state)
|
||||
{
|
||||
return state->buffer.length;
|
||||
}
|
||||
|
||||
static inline
|
||||
size_t psyc_parse_remaining_length (PsycParseState *state)
|
||||
static inline size_t
|
||||
psyc_parse_remaining_length (PsycParseState *state)
|
||||
{
|
||||
return state->buffer.length - state->cursor;
|
||||
}
|
||||
|
||||
static inline
|
||||
const char * psyc_parse_remaining_buffer (PsycParseState *state)
|
||||
static inline const char *
|
||||
psyc_parse_remaining_buffer (PsycParseState *state)
|
||||
{
|
||||
return state->buffer.data + state->cursor;
|
||||
}
|
||||
|
@ -371,7 +373,8 @@ const char * psyc_parse_remaining_buffer (PsycParseState *state)
|
|||
#ifdef __INLINE_PSYC_PARSE
|
||||
static inline
|
||||
#endif
|
||||
PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
||||
PsycParseRC
|
||||
psyc_parse (PsycParseState *state, char *oper,
|
||||
PsycString *name, PsycString *value);
|
||||
|
||||
/**
|
||||
|
@ -388,10 +391,11 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
#ifdef __INLINE_PSYC_PARSE
|
||||
static inline
|
||||
#endif
|
||||
PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem);
|
||||
PsycParseListRC
|
||||
psyc_parse_list (PsycParseListState *state, PsycString *elem);
|
||||
|
||||
static inline
|
||||
PsycBool psyc_parse_number (const char *value, size_t len, int64_t *n)
|
||||
static inline PsycBool
|
||||
psyc_parse_number (const char *value, size_t len, int64_t *n)
|
||||
{
|
||||
size_t c = 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;
|
||||
}
|
||||
|
||||
static inline
|
||||
PsycBool psyc_parse_number_unsigned (const char *value, size_t len, uint64_t *n)
|
||||
static inline PsycBool
|
||||
psyc_parse_number_unsigned (const char *value, size_t len, uint64_t *n)
|
||||
{
|
||||
size_t c = 0;
|
||||
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;
|
||||
}
|
||||
|
||||
static inline
|
||||
PsycBool psyc_parse_time (const char *value, size_t len, time_t *t)
|
||||
static inline PsycBool
|
||||
psyc_parse_time (const char *value, size_t len, time_t *t)
|
||||
{
|
||||
return psyc_parse_number(value, len, t);
|
||||
}
|
||||
|
||||
static inline
|
||||
PsycBool psyc_parse_date (const char *value, size_t len, time_t *t)
|
||||
static inline PsycBool
|
||||
psyc_parse_date (const char *value, size_t len, time_t *t)
|
||||
{
|
||||
if (psyc_parse_number(value, len, t)) {
|
||||
*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.
|
||||
* Glyphs are: : = + - ? !
|
||||
*/
|
||||
static inline
|
||||
char psyc_is_glyph (uint8_t g)
|
||||
static inline char
|
||||
psyc_is_glyph (uint8_t g)
|
||||
{
|
||||
switch(g) {
|
||||
switch (g) {
|
||||
case ':':
|
||||
case '=':
|
||||
case '+':
|
||||
|
@ -468,8 +472,8 @@ char psyc_is_glyph (uint8_t g)
|
|||
/**
|
||||
* Determines if the argument is numeric.
|
||||
*/
|
||||
static inline
|
||||
char psyc_is_numeric (uint8_t c)
|
||||
static inline char
|
||||
psyc_is_numeric (uint8_t c)
|
||||
{
|
||||
return c >= '0' && c <= '9';
|
||||
}
|
||||
|
@ -477,8 +481,8 @@ char psyc_is_numeric (uint8_t c)
|
|||
/**
|
||||
* Determines if the argument is alphabetic.
|
||||
*/
|
||||
static inline
|
||||
char psyc_is_alpha (uint8_t c)
|
||||
static inline char
|
||||
psyc_is_alpha (uint8_t c)
|
||||
{
|
||||
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.
|
||||
*/
|
||||
static inline
|
||||
char psyc_is_alpha_numeric (uint8_t c)
|
||||
static inline char
|
||||
psyc_is_alpha_numeric (uint8_t 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.
|
||||
* Keyword characters are: alphanumeric and _
|
||||
*/
|
||||
static inline
|
||||
char psyc_is_kw_char (uint8_t c)
|
||||
static inline char
|
||||
psyc_is_kw_char (uint8_t 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.
|
||||
* Name characters are: see opaque_part in RFC 2396
|
||||
*/
|
||||
static inline
|
||||
char psyc_is_name_char (uint8_t c)
|
||||
static inline char
|
||||
psyc_is_name_char (uint8_t c)
|
||||
{
|
||||
return psyc_is_alpha(c) || (c >= '$' && c <= ';') ||
|
||||
c == '_' || c == '!' || c == '?' || c == '=' || c == '@' || c == '~';
|
||||
return psyc_is_alpha(c) || (c >= '$' && c <= ';')
|
||||
|| c == '_' || c == '!' || c == '?' || c == '=' || c == '@' || c == '~';
|
||||
}
|
||||
|
||||
/**
|
||||
* Determines if the argument is a hostname character.
|
||||
* Hostname characters are: alphanumeric and -
|
||||
*/
|
||||
static inline
|
||||
char psyc_is_host_char (uint8_t c)
|
||||
static inline char
|
||||
psyc_is_host_char (uint8_t c)
|
||||
{
|
||||
return psyc_is_alpha_numeric(c) || c == '.' || c == '-';
|
||||
}
|
||||
|
||||
/** @} */ // end of parse group
|
||||
|
||||
#define PSYC_PARSE_H
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#ifndef PSYC_RENDER_H
|
||||
#define PSYC_RENDER_H
|
||||
|
||||
#include <psyc/packet.h>
|
||||
|
||||
|
@ -19,8 +20,7 @@
|
|||
/**
|
||||
* Return codes for psyc_render.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
/// Error, method is missing, but data is present.
|
||||
PSYC_RENDER_ERROR_METHOD_MISSING = -3,
|
||||
/// Error, a modifier name is missing.
|
||||
|
@ -34,8 +34,7 @@ typedef enum
|
|||
/**
|
||||
* Return codes for psyc_render_list.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
/// Error, buffer is too small to render the list.
|
||||
PSYC_RENDER_LIST_ERROR = -1,
|
||||
/// List is rendered successfully in the buffer.
|
||||
|
@ -59,7 +58,8 @@ typedef enum
|
|||
#ifdef __INLINE_PSYC_RENDER
|
||||
static inline
|
||||
#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.
|
||||
|
@ -67,9 +67,9 @@ PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen);
|
|||
#ifdef __INLINE_PSYC_RENDER
|
||||
static inline
|
||||
#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
|
||||
|
||||
#define PSYC_RENDER_H
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#ifndef PSYC_TEXT_H
|
||||
#define PSYC_TEXT_H
|
||||
|
||||
/**
|
||||
* @file psyc/text.h
|
||||
|
@ -19,22 +20,20 @@
|
|||
* Return values for the text template parsing function.
|
||||
* @see psyc_text()
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
/// No substitution was made, nothing was written to the buffer.
|
||||
PSYC_TEXT_NO_SUBST = -1,
|
||||
/// Text template parsing & rendering complete.
|
||||
PSYC_TEXT_COMPLETE = 0,
|
||||
/// Text template parsing & rendering is incomplete, because the buffer was too small.
|
||||
/// Another call is required to this function after setting a new buffer.
|
||||
/// Text template parsing & rendering is incomplete, because the buffer was too
|
||||
/// small. Another call is required to this function after setting a new buffer.
|
||||
PSYC_TEXT_INCOMPLETE = 1,
|
||||
} PsycTextRC;
|
||||
|
||||
/**
|
||||
* Return values for PsycTextCB.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
typedef enum {
|
||||
/// Value not found, don't substitute anything.
|
||||
PSYC_TEXT_VALUE_NOT_FOUND = -1,
|
||||
/// Value found, substitute contents of the value variable.
|
||||
|
@ -44,8 +43,7 @@ typedef enum
|
|||
/**
|
||||
* Struct for keeping PSYC text parser state.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
typedef struct {
|
||||
size_t cursor; ///< current position in the template
|
||||
size_t written; ///< number of bytes written to buffer
|
||||
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
|
||||
* 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.
|
||||
|
@ -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 buflen Length of output buffer.
|
||||
*/
|
||||
static inline
|
||||
void psyc_text_state_init (PsycTextState *state,
|
||||
static inline void
|
||||
psyc_text_state_init (PsycTextState *state,
|
||||
char *tmpl, size_t tmplen,
|
||||
char *buffer, size_t buflen)
|
||||
{
|
||||
state->cursor = 0;
|
||||
state->written = 0;
|
||||
state->tmpl = (PsycString) {tmplen, tmpl};
|
||||
state->buffer = (PsycString) {buflen, buffer};
|
||||
state->open = (PsycString) {1, "["};
|
||||
state->close = (PsycString) {1, "]"};
|
||||
state->tmpl = (PsycString) {
|
||||
tmplen, tmpl};
|
||||
state->buffer = (PsycString) {
|
||||
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 closelen Length of closing brace.
|
||||
*/
|
||||
static inline
|
||||
void psyc_text_state_init_custom (PsycTextState *state,
|
||||
static inline void
|
||||
psyc_text_state_init_custom (PsycTextState *state,
|
||||
char *tmpl, size_t tmplen,
|
||||
char *buffer, size_t buflen,
|
||||
char *open, size_t openlen,
|
||||
|
@ -110,25 +113,29 @@ void psyc_text_state_init_custom (PsycTextState *state,
|
|||
{
|
||||
state->cursor = 0;
|
||||
state->written = 0;
|
||||
state->tmpl = (PsycString) {tmplen, tmpl};
|
||||
state->buffer = (PsycString) {buflen, buffer};
|
||||
state->open = (PsycString) {openlen, open};
|
||||
state->close = (PsycString) {closelen, close};
|
||||
state->tmpl = (PsycString) {
|
||||
tmplen, tmpl};
|
||||
state->buffer = (PsycString) {
|
||||
buflen, buffer};
|
||||
state->open = (PsycString) {
|
||||
openlen, open};
|
||||
state->close = (PsycString) {
|
||||
closelen, close};
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets a new output buffer in the PSYC text state struct.
|
||||
*/
|
||||
static inline
|
||||
void psyc_text_buffer_set (PsycTextState *state,
|
||||
char *buffer, size_t length)
|
||||
static inline void
|
||||
psyc_text_buffer_set (PsycTextState *state, char *buffer, size_t length)
|
||||
{
|
||||
state->buffer = (PsycString){length, buffer};
|
||||
state->buffer = (PsycString) {
|
||||
length, buffer};
|
||||
state->written = 0;
|
||||
}
|
||||
|
||||
static inline
|
||||
size_t psyc_text_bytes_written (PsycTextState *state)
|
||||
static inline size_t
|
||||
psyc_text_bytes_written (PsycTextState *state)
|
||||
{
|
||||
return state->written;
|
||||
}
|
||||
|
@ -148,9 +155,9 @@ size_t psyc_text_bytes_written (PsycTextState *state)
|
|||
*
|
||||
* @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
|
||||
|
||||
#define PSYC_TEXT_H
|
||||
#endif
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
#ifndef PSYC_UNIFORM_H
|
||||
#define PSYC_UNIFORM_H
|
||||
|
||||
/**
|
||||
* @file uniform.h
|
||||
* @brief Uniform parsing.
|
||||
|
@ -76,7 +78,7 @@ typedef enum {
|
|||
PSYC_ENTITY_SERVICE = '$',
|
||||
} 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
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
#ifndef PSYC_VARIABLE_H
|
||||
#define 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?
|
||||
*/
|
||||
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.
|
||||
*/
|
||||
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?
|
||||
*/
|
||||
static inline
|
||||
PsycBool psyc_var_is_list (const char *name, size_t len)
|
||||
static inline PsycBool
|
||||
psyc_var_is_list (const char *name, size_t len)
|
||||
{
|
||||
return len < 5 || memcmp(name, "_list", 5) != 0 ||
|
||||
(len > 5 && name[5] != '_') ? PSYC_FALSE : PSYC_TRUE;
|
||||
return len < 5 || memcmp(name, "_list", 5) != 0 || (len > 5 && name[5] != '_')
|
||||
? PSYC_FALSE : PSYC_TRUE;
|
||||
}
|
||||
|
||||
#define PSYC_VARIABLE_H
|
||||
#endif
|
||||
|
|
53
src/itoa.c
53
src/itoa.c
|
@ -1,33 +1,39 @@
|
|||
#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.
|
||||
*/
|
||||
int itoa(int number, char* out, int base) {
|
||||
int
|
||||
itoa (int number, char *out, int base)
|
||||
{
|
||||
int t, count;
|
||||
char *p, *q;
|
||||
char c;
|
||||
|
||||
p = q = out;
|
||||
if (base < 2 || base > 36) base = 10;
|
||||
if (base < 2 || base > 36)
|
||||
base = 10;
|
||||
|
||||
do {
|
||||
t = number;
|
||||
number /= base;
|
||||
if (out) *p = ALPHANUMS[t+35 - number*base];
|
||||
if (out)
|
||||
*p = ALPHANUMS[t + 35 - number * base];
|
||||
p++;
|
||||
} while (number);
|
||||
|
||||
if (t < 0) {
|
||||
if (out) *p = '-';
|
||||
if (out)
|
||||
*p = '-';
|
||||
p++;
|
||||
}
|
||||
count = p-out;
|
||||
count = p - out;
|
||||
if (out) {
|
||||
*p-- = '\0';
|
||||
while(q < p) {
|
||||
while (q < p) {
|
||||
c = *p;
|
||||
*p-- = *q;
|
||||
*q++ = c;
|
||||
|
@ -45,41 +51,44 @@ int itoa(int number, char* out, int base) {
|
|||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
char out[4404];
|
||||
int in[44];
|
||||
int c, i, j;
|
||||
|
||||
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;
|
||||
}
|
||||
for (j=argc-1; j; j--) {
|
||||
// printf("Looking at arg #%d: %s\n", j, argv[j]);
|
||||
for (j = argc - 1; j; j--) {
|
||||
//printf("Looking at arg #%d: %s\n", j, 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;
|
||||
for (j=argc-1; j>1; j--) {
|
||||
# if 0
|
||||
for (j = argc - 1; j > 1; j--) {
|
||||
#if 0
|
||||
// use good old sprintf
|
||||
c += sprintf(&out[c], " %d", in[j]);
|
||||
# else
|
||||
# if 1
|
||||
#else
|
||||
#if 1
|
||||
// use the itoa implementation
|
||||
out[c++] = ' ';
|
||||
c += itoa(in[j], &out[c], 10);
|
||||
# else
|
||||
#else
|
||||
// just count the needed space
|
||||
c += itoa(in[j], NULL, 10) + 1;
|
||||
# endif
|
||||
# endif
|
||||
#endif
|
||||
#endif
|
||||
}
|
||||
}
|
||||
printf("%d times, %d count, buffer len: %d, buffer: %s\n",
|
||||
in[1], c, strlen(out), "<omitted>"); // out
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
|
|
103
src/match.c
103
src/match.c
|
@ -1,8 +1,8 @@
|
|||
#include "lib.h"
|
||||
|
||||
int psyc_inherits (char* sho, size_t slen,
|
||||
char* lon, size_t llen) {
|
||||
|
||||
int
|
||||
psyc_inherits(char *sho, size_t slen, char *lon, size_t llen)
|
||||
{
|
||||
// this allows to pass zero-terminated strings instead of providing
|
||||
// the length.. but we would be faster here if we expected the callee
|
||||
// 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 (!llen) llen = strlen(lon);
|
||||
|
||||
if (slen == 0 || *sho != '_' ||
|
||||
llen == 0 || *lon != '_') {
|
||||
P1(("Please use long format keywords (compact ones would be faster, I know..)\n"))
|
||||
if (slen == 0 || *sho != '_' || llen == 0 || *lon != '_') {
|
||||
P1(("Please use long format keywords (compact ones would be faster, I know..)\n"));
|
||||
return -2;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -33,79 +32,87 @@ int psyc_inherits (char* sho, size_t slen,
|
|||
* implementations are not required to recognize
|
||||
* that.
|
||||
*/
|
||||
P1(("Illegal choice of keyword names!\n"))
|
||||
P1(("Illegal choice of keyword names!\n"));
|
||||
return -4;
|
||||
}
|
||||
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;
|
||||
}
|
||||
|
||||
int psyc_matches (char* sho, size_t slen,
|
||||
char* lon, size_t llen) {
|
||||
int
|
||||
psyc_matches(char *sho, size_t slen, char *lon, size_t llen)
|
||||
{
|
||||
char *s, *l, *se, *le;
|
||||
|
||||
//if (!slen) slen = strlen(sho);
|
||||
//if (!llen) llen = strlen(lon);
|
||||
|
||||
if (slen == 0 || *sho != '_' ||
|
||||
llen == 0 || *lon != '_') {
|
||||
P1(("Please use long format keywords (compact ones would be faster, I know..)\n"))
|
||||
if (slen == 0 || *sho != '_' || llen == 0 || *lon != '_') {
|
||||
P1(("Please use long format keywords (compact ones would be faster, I know..)\n"));
|
||||
return -2;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
if (slen == llen) {
|
||||
if (!strncmp(sho, lon, slen)) {
|
||||
P1(("Identical arguments.\n"))
|
||||
P1(("Identical arguments.\n"));
|
||||
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;
|
||||
}
|
||||
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;
|
||||
le = lon+llen;
|
||||
sho++; lon++; slen--; llen--;
|
||||
while(*sho && sho < se) {
|
||||
P3(("# comparing short '%.*s' (%d)\n", (int)slen, sho, (int)slen))
|
||||
unless (s = memchr(sho, '_', slen)) s = se;
|
||||
P4(("# sho goes '%c' and lon goes '%c'\n", *sho, (int)*lon))
|
||||
while(*lon && lon < le) {
|
||||
P3(("# against long '%.*s' (%d)\n", (int)llen, lon, (int)llen))
|
||||
unless (l = memchr(lon, '_', llen)) l = le;
|
||||
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;
|
||||
se = sho + slen;
|
||||
le = lon + llen;
|
||||
sho++;
|
||||
lon++;
|
||||
slen--;
|
||||
llen--;
|
||||
while (*sho && sho < se) {
|
||||
P3(("# comparing short '%.*s' (%d)\n", (int) slen, sho, (int) slen));
|
||||
unless(s = memchr(sho, '_', slen)) s = se;
|
||||
P4(("# sho goes '%c' and lon goes '%c'\n", *sho, (int) *lon));
|
||||
while (*lon && lon < le) {
|
||||
P3(("# against long '%.*s' (%d)\n", (int) llen, lon, (int) llen));
|
||||
unless(l = memchr(lon, '_', llen)) l = le;
|
||||
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;
|
||||
}
|
||||
goto failed;
|
||||
foundone:
|
||||
P3(("# found %ld of short '%.*s' and long '%.*s'\n", s-sho, (int)(s-sho), sho, (int)(s-sho), lon))
|
||||
llen -= l-lon;
|
||||
slen -= s-sho;
|
||||
foundone:
|
||||
P3(("# found %ld of short '%.*s' and long '%.*s'\n", s - sho,
|
||||
(int) (s - sho), sho, (int) (s - sho), lon));
|
||||
llen -= l - lon;
|
||||
slen -= s - sho;
|
||||
sho = ++s;
|
||||
lon = ++l;
|
||||
}
|
||||
return 0;
|
||||
failed:
|
||||
P4(("No, they don't match.\n"))
|
||||
failed:
|
||||
P4(("No, they don't match.\n"));
|
||||
return 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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,
|
||||
PsycBool inherit, int8_t *matching)
|
||||
PsycBool inherit, int8_t * matching)
|
||||
{
|
||||
size_t cursor = 1;
|
||||
uint8_t i, m = 0;
|
||||
|
@ -114,10 +121,12 @@ void * psyc_dict_lookup (const PsycDict *dict, size_t size,
|
|||
return 0;
|
||||
|
||||
// first find the keywords with matching length
|
||||
for (i=0; i<size; i++)
|
||||
if (keylen == dict[i].key.length ||
|
||||
(inherit && keylen > dict[i].key.length && key[dict[i].key.length] == '_'))
|
||||
for (i = 0; i < size; i++) {
|
||||
if (keylen == dict[i].key.length
|
||||
|| (inherit && keylen > dict[i].key.length
|
||||
&& key[dict[i].key.length] == '_'))
|
||||
matching[m++] = i;
|
||||
}
|
||||
|
||||
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
|
||||
int main(int argc, char **argv) {
|
||||
int
|
||||
main(int argc, char **argv)
|
||||
{
|
||||
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;
|
||||
}
|
||||
if (psyc_matches(argv[1], 0, argv[2], 0) == 0)
|
||||
|
|
11
src/memmem.c
11
src/memmem.c
|
@ -31,13 +31,12 @@
|
|||
/*
|
||||
* Find the first occurrence of the byte string s in byte string l.
|
||||
*/
|
||||
|
||||
void *
|
||||
memmem(const void *l, size_t l_len, const void *s, size_t s_len)
|
||||
{
|
||||
register char *cur, *last;
|
||||
const char *cl = (const char *)l;
|
||||
const char *cs = (const char *)s;
|
||||
const char *cl = (const char *) l;
|
||||
const char *cs = (const char *) s;
|
||||
|
||||
/* we need something to compare */
|
||||
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 */
|
||||
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" */
|
||||
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)
|
||||
return cur;
|
||||
|
||||
|
|
73
src/packet.c
73
src/packet.c
|
@ -2,20 +2,18 @@
|
|||
#include <psyc/syntax.h>
|
||||
#include <psyc/packet.h>
|
||||
|
||||
inline
|
||||
PsycListFlag psyc_list_length_check (PsycList *list)
|
||||
inline PsycListFlag
|
||||
psyc_list_length_check (PsycList * list)
|
||||
{
|
||||
PsycListFlag flag = PSYC_LIST_NO_LENGTH;
|
||||
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];
|
||||
length += 1 + elem->length; // |elem
|
||||
if (length > PSYC_MODIFIER_SIZE_THRESHOLD ||
|
||||
memchr(elem->data, (int)'|', elem->length) ||
|
||||
memchr(elem->data, (int)'\n', elem->length))
|
||||
{
|
||||
memchr(elem->data, (int) '|', elem->length) ||
|
||||
memchr(elem->data, (int) '\n', elem->length)) {
|
||||
flag = PSYC_LIST_NEED_LENGTH;
|
||||
break;
|
||||
}
|
||||
|
@ -24,22 +22,19 @@ PsycListFlag psyc_list_length_check (PsycList *list)
|
|||
return flag;
|
||||
}
|
||||
|
||||
inline
|
||||
PsycListFlag psyc_list_length (PsycList *list)
|
||||
inline PsycListFlag
|
||||
psyc_list_length (PsycList * list)
|
||||
{
|
||||
size_t i, length = 0;
|
||||
|
||||
if (list->flag == PSYC_LIST_NEED_LENGTH)
|
||||
{
|
||||
for (i = 0; i < list->num_elems; i++)
|
||||
{
|
||||
if (list->flag == PSYC_LIST_NEED_LENGTH) {
|
||||
for (i = 0; i < list->num_elems; i++) {
|
||||
if (i > 0)
|
||||
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++)
|
||||
length += 1 + list->elems[i].length; // |elem
|
||||
}
|
||||
|
@ -47,11 +42,12 @@ PsycListFlag psyc_list_length (PsycList *list)
|
|||
return length;
|
||||
}
|
||||
|
||||
inline
|
||||
void psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
|
||||
inline void
|
||||
psyc_list_init (PsycList * list, PsycString * elems, size_t num_elems,
|
||||
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
|
||||
list->flag = psyc_list_length_check(list);
|
||||
|
@ -60,8 +56,8 @@ void psyc_list_init (PsycList *list, PsycString *elems, size_t num_elems,
|
|||
}
|
||||
|
||||
|
||||
inline
|
||||
size_t psyc_modifier_length (PsycModifier *m)
|
||||
inline size_t
|
||||
psyc_modifier_length (PsycModifier * m)
|
||||
{
|
||||
size_t length = 2; // oper\n
|
||||
if (m->name.length > 0)
|
||||
|
@ -73,8 +69,8 @@ size_t psyc_modifier_length (PsycModifier *m)
|
|||
return length;
|
||||
}
|
||||
|
||||
inline
|
||||
PsycPacketFlag psyc_packet_length_check (PsycPacket *p)
|
||||
inline PsycPacketFlag
|
||||
psyc_packet_length_check (PsycPacket * p)
|
||||
{
|
||||
if (p->data.length == 1 && p->data.data[0] == PSYC_PACKET_DELIMITER_CHAR)
|
||||
return PSYC_PACKET_NEED_LENGTH;
|
||||
|
@ -83,8 +79,8 @@ PsycPacketFlag psyc_packet_length_check (PsycPacket *p)
|
|||
return PSYC_PACKET_NEED_LENGTH;
|
||||
|
||||
int i;
|
||||
// if any entity modifiers need length it is possible they contain
|
||||
// a packet terminator, thus the content should have a length as well
|
||||
// If any entity modifiers need length, it is possible they contain
|
||||
// a packet terminator, thus the content should have a length as well.
|
||||
for (i = 0; i < p->entity.lines; i++)
|
||||
if (p->entity.modifiers[i].flag == PSYC_MODIFIER_NEED_LENGTH)
|
||||
return PSYC_PACKET_NEED_LENGTH;
|
||||
|
@ -95,8 +91,8 @@ PsycPacketFlag psyc_packet_length_check (PsycPacket *p)
|
|||
return PSYC_PACKET_NO_LENGTH;
|
||||
}
|
||||
|
||||
inline
|
||||
size_t psyc_packet_length_set (PsycPacket *p)
|
||||
inline size_t
|
||||
psyc_packet_length_set (PsycPacket * p)
|
||||
{
|
||||
size_t i;
|
||||
p->routingLength = 0;
|
||||
|
@ -108,8 +104,7 @@ size_t psyc_packet_length_set (PsycPacket *p)
|
|||
|
||||
if (p->content.length)
|
||||
p->contentLength = p->content.length;
|
||||
else
|
||||
{
|
||||
else {
|
||||
// add state operation
|
||||
if (p->stateop != PSYC_STATE_NOOP)
|
||||
p->contentLength += 2; // op\n
|
||||
|
@ -137,16 +132,16 @@ size_t psyc_packet_length_set (PsycPacket *p)
|
|||
return p->length;
|
||||
}
|
||||
|
||||
inline
|
||||
void psyc_packet_init (PsycPacket *p,
|
||||
PsycModifier *routing, size_t routinglen,
|
||||
PsycModifier *entity, size_t entitylen,
|
||||
inline void
|
||||
psyc_packet_init (PsycPacket * p,
|
||||
PsycModifier * routing, size_t routinglen,
|
||||
PsycModifier * entity, size_t entitylen,
|
||||
char *method, size_t methodlen,
|
||||
char *data, size_t datalen,
|
||||
char stateop, PsycPacketFlag flag)
|
||||
{
|
||||
*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
|
||||
p->flag = psyc_packet_length_check(p);
|
||||
|
@ -154,13 +149,13 @@ void psyc_packet_init (PsycPacket *p,
|
|||
psyc_packet_length_set(p);
|
||||
}
|
||||
|
||||
inline
|
||||
void psyc_packet_init_raw (PsycPacket *p,
|
||||
PsycModifier *routing, size_t routinglen,
|
||||
inline void
|
||||
psyc_packet_init_raw (PsycPacket * p,
|
||||
PsycModifier * routing, size_t routinglen,
|
||||
char *content, size_t contentlen,
|
||||
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};
|
||||
|
||||
if (flag == PSYC_PACKET_CHECK_LENGTH) // find out if it needs length
|
||||
|
|
252
src/parse.c
252
src/parse.c
|
@ -10,8 +10,7 @@
|
|||
#include <psyc/parse.h>
|
||||
|
||||
#define ADVANCE_CURSOR_OR_RETURN(ret) \
|
||||
if (++(state->cursor) >= state->buffer.length) \
|
||||
{ \
|
||||
if (++(state->cursor) >= state->buffer.length) { \
|
||||
state->cursor = state->startc; \
|
||||
return ret; \
|
||||
}
|
||||
|
@ -29,14 +28,13 @@ typedef enum {
|
|||
* It should contain one or more keyword characters.
|
||||
* @return PARSE_ERROR or PARSE_SUCCESS
|
||||
*/
|
||||
static inline
|
||||
ParseRC psyc_parse_keyword (PsycParseState *state, PsycString *name)
|
||||
static inline ParseRC
|
||||
psyc_parse_keyword (PsycParseState *state, PsycString *name)
|
||||
{
|
||||
name->data = state->buffer.data + state->cursor;
|
||||
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
|
||||
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
|
||||
*/
|
||||
static inline
|
||||
ParseRC psyc_parse_binary_value (PsycParseState *state, PsycString *value,
|
||||
static inline ParseRC
|
||||
psyc_parse_binary_value (PsycParseState *state, PsycString *value,
|
||||
size_t *length, size_t *parsed)
|
||||
{
|
||||
size_t remaining = *length - *parsed;
|
||||
value->data = state->buffer.data + state->cursor;
|
||||
|
||||
if (state->cursor + remaining > state->buffer.length)
|
||||
{ // value doesn't fit in the buffer completely
|
||||
if (state->cursor + remaining > state->buffer.length) {
|
||||
// value doesn't fit in the buffer completely
|
||||
value->length = state->buffer.length - state->cursor;
|
||||
state->cursor += value->length;
|
||||
*parsed += value->length;
|
||||
|
@ -81,8 +79,8 @@ ParseRC psyc_parse_binary_value (PsycParseState *state, PsycString *value,
|
|||
* Parse simple or binary variable.
|
||||
* @return PARSE_ERROR or PARSE_SUCCESS
|
||||
*/
|
||||
static inline
|
||||
ParseRC psyc_parse_modifier (PsycParseState *state, char *oper,
|
||||
static inline ParseRC
|
||||
psyc_parse_modifier (PsycParseState *state, char *oper,
|
||||
PsycString *name, PsycString *value)
|
||||
{
|
||||
*oper = *(state->buffer.data + state->cursor);
|
||||
|
@ -102,22 +100,20 @@ ParseRC psyc_parse_modifier (PsycParseState *state, char *oper,
|
|||
|
||||
// Parse the value.
|
||||
// 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
|
||||
{ // After SP the length follows.
|
||||
if (state->part == PSYC_PART_CONTENT && state->buffer.data[state->cursor] == ' ') {
|
||||
// binary arg
|
||||
// After SP the length follows.
|
||||
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;
|
||||
do
|
||||
{
|
||||
do {
|
||||
length = 10 * length + state->buffer.data[state->cursor] - '0';
|
||||
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
|
||||
}
|
||||
while (psyc_is_numeric(state->buffer.data[state->cursor]));
|
||||
state->valueLength = length;
|
||||
}
|
||||
else
|
||||
} else
|
||||
return PSYC_PARSE_ERROR_MOD_LEN;
|
||||
|
||||
// After the length a TAB follows.
|
||||
|
@ -127,26 +123,24 @@ ParseRC psyc_parse_modifier (PsycParseState *state, char *oper,
|
|||
if (++(state->cursor) >= state->buffer.length)
|
||||
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)
|
||||
return ret;
|
||||
|
||||
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);
|
||||
value->data = state->buffer.data + state->cursor;
|
||||
|
||||
while (state->buffer.data[state->cursor] != '\n')
|
||||
{
|
||||
while (state->buffer.data[state->cursor] != '\n') {
|
||||
value->length++;
|
||||
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
|
||||
}
|
||||
|
||||
return PARSE_SUCCESS;
|
||||
}
|
||||
else
|
||||
} else
|
||||
return PSYC_PARSE_ERROR_MOD_TAB;
|
||||
}
|
||||
|
||||
|
@ -154,15 +148,15 @@ ParseRC psyc_parse_modifier (PsycParseState *state, char *oper,
|
|||
#ifdef __INLINE_PSYC_PARSE
|
||||
static inline
|
||||
#endif
|
||||
PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
||||
PsycParseRC
|
||||
psyc_parse (PsycParseState *state, char *oper,
|
||||
PsycString *name, PsycString *value)
|
||||
{
|
||||
#ifdef DEBUG
|
||||
if (state->flags & PSYC_PARSE_ROUTING_ONLY &&
|
||||
state->flags & PSYC_PARSE_START_AT_CONTENT)
|
||||
PP(("Invalid flag combination"))
|
||||
PP(("Invalid flag combination"));
|
||||
#endif
|
||||
|
||||
ParseRC ret; // a return value
|
||||
size_t pos = state->cursor; // a cursor position
|
||||
|
||||
|
@ -171,11 +165,10 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
state->startc = state->cursor;
|
||||
|
||||
// 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;
|
||||
|
||||
switch (state->part)
|
||||
{
|
||||
switch (state->part) {
|
||||
case PSYC_PART_RESET: // New packet starts here, reset state.
|
||||
state->valueParsed = 0;
|
||||
state->valueLength = 0;
|
||||
|
@ -188,24 +181,20 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
// fall thru
|
||||
|
||||
case PSYC_PART_ROUTING:
|
||||
if (state->routingLength > 0)
|
||||
{
|
||||
if (state->routingLength > 0) {
|
||||
if (state->buffer.data[state->cursor] != '\n')
|
||||
return PSYC_PARSE_ERROR_MOD_NL;
|
||||
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
|
||||
}
|
||||
|
||||
// Each line of the header starts with a glyph,
|
||||
// i.e. :_name, -_name +_name etc,
|
||||
// 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?
|
||||
{ // it is a glyph, so a variable starts here
|
||||
if (psyc_is_glyph(state->buffer.data[state->cursor])) {
|
||||
// it is a glyph, so a variable starts here
|
||||
ret = psyc_parse_modifier(state, oper, name, value);
|
||||
state->routingLength += state->cursor - pos;
|
||||
return ret == PARSE_SUCCESS ? PSYC_PARSE_ROUTING : ret;
|
||||
}
|
||||
else // not a glyph
|
||||
{
|
||||
} else { // not a glyph
|
||||
state->part = PSYC_PART_LENGTH;
|
||||
state->startc = state->cursor;
|
||||
// fall thru
|
||||
|
@ -213,35 +202,29 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
|
||||
case PSYC_PART_LENGTH:
|
||||
// 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->contentLength = 0;
|
||||
|
||||
do
|
||||
{
|
||||
state->contentLength = 10 * state->contentLength + state->buffer.data[state->cursor] - '0';
|
||||
do {
|
||||
state->contentLength =
|
||||
10 * state->contentLength +
|
||||
state->buffer.data[state->cursor] - '0';
|
||||
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,
|
||||
// then skip content parsing.
|
||||
if (state->flags & PSYC_PARSE_ROUTING_ONLY)
|
||||
{
|
||||
if (state->flags & PSYC_PARSE_ROUTING_ONLY) {
|
||||
state->part = PSYC_PART_DATA;
|
||||
if (++(state->cursor) >= state->buffer.length)
|
||||
return PSYC_PARSE_INSUFFICIENT;
|
||||
goto PSYC_PART_DATA;
|
||||
}
|
||||
else
|
||||
} else
|
||||
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 (state->contentLengthFound)
|
||||
return PSYC_PARSE_ERROR_LENGTH;
|
||||
|
@ -256,9 +239,9 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
|
||||
case PSYC_PART_CONTENT:
|
||||
// In case of an incomplete binary variable resume parsing it.
|
||||
if (state->valueParsed < state->valueLength)
|
||||
{
|
||||
ret = psyc_parse_binary_value(state, value, &(state->valueLength), &(state->valueParsed));
|
||||
if (state->valueParsed < state->valueLength) {
|
||||
ret = psyc_parse_binary_value(state, value, &(state->valueLength),
|
||||
&(state->valueParsed));
|
||||
state->contentParsed += value->length;
|
||||
|
||||
if (ret == PARSE_INCOMPLETE)
|
||||
|
@ -269,26 +252,22 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
|
||||
pos = state->cursor;
|
||||
|
||||
if (state->contentParsed > 0)
|
||||
{
|
||||
if (state->contentParsed > 0) {
|
||||
if (state->buffer.data[state->cursor] != '\n')
|
||||
return PSYC_PARSE_ERROR_MOD_NL;
|
||||
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_INSUFFICIENT);
|
||||
}
|
||||
|
||||
// Each line of the header starts with a glyph,
|
||||
// i.e. :_name, -_name +_name etc.
|
||||
// So just test if the first char is a glyph.
|
||||
// In the body, the same applies, only that the
|
||||
// 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);
|
||||
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);
|
||||
switch (*oper)
|
||||
{
|
||||
switch (*oper) {
|
||||
case PSYC_STATE_RESYNC:
|
||||
state->contentParsed += 2;
|
||||
return PSYC_PARSE_STATE_RESYNC;
|
||||
|
@ -310,9 +289,7 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
return PSYC_PARSE_ENTITY;
|
||||
|
||||
return ret;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
state->contentParsed += state->cursor - pos;
|
||||
state->startc = state->cursor;
|
||||
state->part = PSYC_PART_METHOD;
|
||||
|
@ -325,8 +302,8 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
|
||||
if (ret == PARSE_INSUFFICIENT)
|
||||
return ret;
|
||||
else if (ret == PARSE_SUCCESS)
|
||||
{ // the method ends with a \n then the data follows
|
||||
else if (ret == PARSE_SUCCESS) {
|
||||
// The method ends with a \n then the data follows.
|
||||
if (state->buffer.data[state->cursor] != '\n')
|
||||
return PSYC_PARSE_ERROR_METHOD;
|
||||
|
||||
|
@ -334,20 +311,16 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
state->valueParsed = 0;
|
||||
state->valueLength = 0;
|
||||
|
||||
if (state->contentLengthFound)
|
||||
{ // if length was found set start position to the beginning of data
|
||||
if (state->contentLengthFound) {
|
||||
// Length found, set start position to the beginning of data.
|
||||
state->cursor++;
|
||||
state->startc = state->cursor;
|
||||
state->contentParsed += state->cursor - pos;
|
||||
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);
|
||||
}
|
||||
}
|
||||
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->startc = state->cursor;
|
||||
goto PSYC_PART_END;
|
||||
|
@ -359,48 +332,44 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
value->data = state->buffer.data + state->cursor;
|
||||
value->length = 0;
|
||||
|
||||
if (state->contentLengthFound) // We know the length of the packet.
|
||||
{
|
||||
if (!state->valueLengthFound) // start of data
|
||||
{
|
||||
if (state->contentLengthFound) { // We know the length of the packet.
|
||||
if (!state->valueLengthFound) { // start of data
|
||||
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))
|
||||
state->valueLength--; // \n at the end is not part of data
|
||||
}
|
||||
if (state->valueParsed < state->valueLength)
|
||||
{
|
||||
ret = psyc_parse_binary_value(state, value, &(state->valueLength), &(state->valueParsed));
|
||||
if (state->valueParsed < state->valueLength) {
|
||||
ret = psyc_parse_binary_value(state, value, &(state->valueLength),
|
||||
&(state->valueParsed));
|
||||
state->contentParsed += value->length;
|
||||
|
||||
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;
|
||||
return state->valueLength == value->length ? PSYC_PARSE_BODY : PSYC_PARSE_BODY_END;
|
||||
}
|
||||
else // Search for the terminator.
|
||||
{
|
||||
return state->valueLength == value->length ?
|
||||
PSYC_PARSE_BODY : PSYC_PARSE_BODY_END;
|
||||
} else { // Search for the terminator.
|
||||
size_t datac = state->cursor; // start of data
|
||||
if (state->flags & PSYC_PARSE_ROUTING_ONLY)
|
||||
state->startc = datac; // in routing-only mode restart from the start of data
|
||||
if (state->flags & PSYC_PARSE_ROUTING_ONLY) // in routing-only mode restart
|
||||
state->startc = datac; // from the start of data
|
||||
|
||||
while (1)
|
||||
{
|
||||
while (1) {
|
||||
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
|
||||
if (state->cursor == datac || nl)
|
||||
{
|
||||
if (state->cursor+1+nl >= state->buffer.length) // incremented cursor inside length?
|
||||
{
|
||||
if (state->cursor == datac || nl) {
|
||||
// incremented cursor inside length?
|
||||
if (state->cursor + 1 + nl >= state->buffer.length) {
|
||||
state->cursor = state->startc;
|
||||
return PSYC_PARSE_INSUFFICIENT;
|
||||
}
|
||||
|
||||
if (state->buffer.data[state->cursor+nl] == '|' &&
|
||||
state->buffer.data[state->cursor+1+nl] == '\n') // packet ends here
|
||||
{
|
||||
if (state->buffer.data[state->cursor + nl] == '|'
|
||||
&& state->buffer.data[state->cursor + 1 + nl] == '\n') {
|
||||
// packet ends here
|
||||
if (state->flags & PSYC_PARSE_ROUTING_ONLY)
|
||||
value->length++;
|
||||
|
||||
|
@ -417,9 +386,9 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
|
||||
case PSYC_PART_END:
|
||||
PSYC_PART_END:
|
||||
if (state->contentLengthFound && state->valueLengthFound && state->valueLength &&
|
||||
!(state->flags & PSYC_PARSE_ROUTING_ONLY))
|
||||
{ // if data was not empty next is the \n at the end of data
|
||||
// if data was not empty next is the \n at the end of data
|
||||
if (state->contentLengthFound && state->valueLengthFound
|
||||
&& state->valueLength && !(state->flags & PSYC_PARSE_ROUTING_ONLY)) {
|
||||
state->valueLength = 0;
|
||||
state->valueLengthFound = 0;
|
||||
|
||||
|
@ -429,21 +398,18 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
state->contentParsed++;
|
||||
state->cursor++;
|
||||
}
|
||||
|
||||
// End of packet, at this point we have already passed a \n
|
||||
// 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;
|
||||
|
||||
if (state->buffer.data[state->cursor] == '|' &&
|
||||
state->buffer.data[state->cursor+1] == '\n') // packet ends here
|
||||
{
|
||||
if (state->buffer.data[state->cursor] == '|'
|
||||
&& state->buffer.data[state->cursor + 1] == '\n') {
|
||||
// Packet ends here.
|
||||
state->cursor += 2;
|
||||
state->part = PSYC_PART_RESET;
|
||||
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;
|
||||
return PSYC_PARSE_ERROR_END;
|
||||
}
|
||||
|
@ -455,59 +421,50 @@ PsycParseRC psyc_parse (PsycParseState *state, char *oper,
|
|||
#ifdef __INLINE_PSYC_PARSE
|
||||
static inline
|
||||
#endif
|
||||
PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem)
|
||||
PsycParseListRC
|
||||
psyc_parse_list (PsycParseListState *state, PsycString *elem)
|
||||
{
|
||||
if (state->cursor >= state->buffer.length)
|
||||
return PSYC_PARSE_LIST_INCOMPLETE;
|
||||
|
||||
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
|
||||
if (state->buffer.data[state->cursor] == '|')
|
||||
{
|
||||
if (state->buffer.data[state->cursor] == '|') {
|
||||
state->type = PSYC_LIST_TEXT;
|
||||
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;
|
||||
else
|
||||
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->length = 0;
|
||||
|
||||
if (state->cursor >= state->buffer.length)
|
||||
return PSYC_PARSE_LIST_END;
|
||||
|
||||
while (state->buffer.data[state->cursor] != '|')
|
||||
{
|
||||
while (state->buffer.data[state->cursor] != '|') {
|
||||
elem->length++;
|
||||
if (++(state->cursor) >= state->buffer.length)
|
||||
return PSYC_PARSE_LIST_END;
|
||||
}
|
||||
state->cursor++;
|
||||
return PSYC_PARSE_LIST_ELEM;
|
||||
}
|
||||
else // binary list
|
||||
{
|
||||
if (!(state->elemParsed < state->elemLength))
|
||||
{
|
||||
} else { // binary list
|
||||
if (!(state->elemParsed < state->elemLength)) {
|
||||
// Element starts with a number.
|
||||
if (psyc_is_numeric(state->buffer.data[state->cursor]))
|
||||
{
|
||||
do
|
||||
{
|
||||
state->elemLength = 10 * state->elemLength + state->buffer.data[state->cursor] - '0';
|
||||
if (psyc_is_numeric(state->buffer.data[state->cursor])) {
|
||||
do {
|
||||
state->elemLength =
|
||||
10 * state->elemLength +
|
||||
state->buffer.data[state->cursor] - '0';
|
||||
ADVANCE_CURSOR_OR_RETURN(PSYC_PARSE_LIST_INCOMPLETE);
|
||||
}
|
||||
while (psyc_is_numeric(state->buffer.data[state->cursor]));
|
||||
}
|
||||
else
|
||||
} while (psyc_is_numeric(state->buffer.data[state->cursor]));
|
||||
} else
|
||||
return PSYC_PARSE_LIST_ERROR_LEN;
|
||||
|
||||
if (state->buffer.data[state->cursor] != ' ')
|
||||
|
@ -518,12 +475,11 @@ PsycParseListRC psyc_parse_list (PsycParseListState *state, PsycString *elem)
|
|||
elem->length = 0;
|
||||
state->elemParsed = 0;
|
||||
}
|
||||
|
||||
// Start or resume parsing the binary data
|
||||
if (state->elemParsed < state->elemLength)
|
||||
{
|
||||
if (psyc_parse_binary_value((PsycParseState*)state, elem,
|
||||
&(state->elemLength), &(state->elemParsed)) == PARSE_INCOMPLETE)
|
||||
if (state->elemParsed < state->elemLength) {
|
||||
if (PARSE_INCOMPLETE == psyc_parse_binary_value((PsycParseState*)state,
|
||||
elem, &state->elemLength,
|
||||
&state->elemParsed))
|
||||
return PSYC_PARSE_LIST_INCOMPLETE;
|
||||
|
||||
state->elemLength = 0;
|
||||
|
|
57
src/render.c
57
src/render.c
|
@ -6,7 +6,8 @@
|
|||
#ifdef __INLINE_PSYC_RENDER
|
||||
static inline
|
||||
#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;
|
||||
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
|
||||
return PSYC_RENDER_LIST_ERROR;
|
||||
|
||||
if (list->flag == PSYC_LIST_NEED_LENGTH)
|
||||
{
|
||||
for (i = 0; i < list->num_elems; i++)
|
||||
{
|
||||
if (list->flag == PSYC_LIST_NEED_LENGTH) {
|
||||
for (i = 0; i < list->num_elems; i++) {
|
||||
elem = &list->elems[i];
|
||||
if (i > 0)
|
||||
buffer[cur++] = '|';
|
||||
|
@ -26,11 +25,8 @@ PsycRenderListRC psyc_render_list (PsycList *list, char *buffer, size_t buflen)
|
|||
memcpy(buffer + cur, elem->data, elem->length);
|
||||
cur += elem->length;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
for (i = 0; i < list->num_elems; i++)
|
||||
{
|
||||
} else {
|
||||
for (i = 0; i < list->num_elems; i++) {
|
||||
elem = &list->elems[i];
|
||||
buffer[cur++] = '|';
|
||||
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);
|
||||
return PSYC_RENDER_LIST_SUCCESS;
|
||||
}
|
||||
|
||||
static inline
|
||||
size_t psyc_render_modifier (PsycModifier *mod, char *buffer)
|
||||
static inline size_t
|
||||
psyc_render_modifier (PsycModifier * mod, char *buffer)
|
||||
{
|
||||
size_t cur = 0;
|
||||
|
||||
|
@ -54,8 +50,7 @@ size_t psyc_render_modifier (PsycModifier *mod, char *buffer)
|
|||
if (cur == 1)
|
||||
return cur; // error, name can't be empty
|
||||
|
||||
if (mod->flag == PSYC_MODIFIER_NEED_LENGTH)
|
||||
{
|
||||
if (mod->flag == PSYC_MODIFIER_NEED_LENGTH) {
|
||||
buffer[cur++] = ' ';
|
||||
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
|
||||
static inline
|
||||
#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;
|
||||
|
||||
|
@ -79,8 +75,7 @@ PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen)
|
|||
return PSYC_RENDER_ERROR;
|
||||
|
||||
// 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);
|
||||
cur += len;
|
||||
if (len <= 1)
|
||||
|
@ -91,41 +86,35 @@ PsycRenderRC psyc_render (PsycPacket *packet, char *buffer, size_t buflen)
|
|||
if (packet->flag == PSYC_PACKET_NEED_LENGTH)
|
||||
cur += itoa(packet->contentLength, buffer + cur, 10);
|
||||
|
||||
if (packet->flag == PSYC_PACKET_NEED_LENGTH || packet->content.length ||
|
||||
packet->stateop || packet->entity.lines ||
|
||||
packet->method.length || packet->data.length)
|
||||
if (packet->flag == PSYC_PACKET_NEED_LENGTH || packet->content.length
|
||||
|| packet->stateop || packet->entity.lines
|
||||
|| packet->method.length || packet->data.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);
|
||||
cur += packet->content.length;
|
||||
}
|
||||
else
|
||||
{
|
||||
} else {
|
||||
if (packet->stateop) {
|
||||
buffer[cur++] = packet->stateop;
|
||||
buffer[cur++] = '\n';
|
||||
}
|
||||
|
||||
// render entity modifiers
|
||||
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);
|
||||
cur += packet->method.length;
|
||||
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);
|
||||
cur += packet->data.length;
|
||||
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;
|
||||
}
|
||||
|
||||
|
|
35
src/text.c
35
src/text.c
|
@ -1,7 +1,8 @@
|
|||
#include "lib.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 *prev = state->tmpl.data + state->cursor;
|
||||
|
@ -10,8 +11,7 @@ PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra)
|
|||
size_t len;
|
||||
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,
|
||||
state->tmpl.length - state->cursor,
|
||||
state->open.data, state->open.length);
|
||||
|
@ -29,40 +29,39 @@ PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra)
|
|||
|
||||
if (!end)
|
||||
break; // ] not found
|
||||
if (start + state->open.length == end)
|
||||
{
|
||||
if (start + state->open.length == end) {
|
||||
state->cursor += state->close.length;
|
||||
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)
|
||||
continue; // value not found, no substitution
|
||||
|
||||
// first copy the part in the input from the previous subst. to the current one
|
||||
// if there's enough buffer space for that
|
||||
// First copy the part in the input from the previous subst.
|
||||
// to the current one, if there's enough buffer space for that.
|
||||
len = start - prev;
|
||||
if (state->written + len > state->buffer.length)
|
||||
{
|
||||
if (state->written + len > state->buffer.length) {
|
||||
state->cursor = prev - state->tmpl.data;
|
||||
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;
|
||||
|
||||
// now substitute the value if there's enough buffer space
|
||||
if (state->written + value.length > state->buffer.length)
|
||||
{
|
||||
// Now substitute the value if there's enough buffer space.
|
||||
if (state->written + value.length > state->buffer.length) {
|
||||
state->cursor = start - state->tmpl.data;
|
||||
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;
|
||||
|
||||
// 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;
|
||||
no_subst = 0;
|
||||
}
|
||||
|
@ -70,12 +69,12 @@ PsycTextRC psyc_text (PsycTextState *state, PsycTextCB getValue, void* extra)
|
|||
if (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);
|
||||
if (state->written + len > state->buffer.length)
|
||||
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;
|
||||
|
||||
return PSYC_TEXT_COMPLETE;
|
||||
|
|
|
@ -3,7 +3,8 @@
|
|||
#include "psyc/uniform.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;
|
||||
PsycString *p;
|
||||
|
@ -25,12 +26,10 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
|
|||
}
|
||||
|
||||
p = &uni->scheme;
|
||||
if (p->length == 4 &&
|
||||
tolower(p->data[0]) == 'p' &&
|
||||
if (p->length == 4 && (tolower(p->data[0]) == 'p' &&
|
||||
tolower(p->data[1]) == 's' &&
|
||||
tolower(p->data[2]) == 'y' &&
|
||||
tolower(p->data[3]) == 'c') {
|
||||
|
||||
tolower(p->data[3]) == 'c')) {
|
||||
uni->type = PSYC_SCHEME_PSYC;
|
||||
part = PSYC_UNIFORM_SLASHES;
|
||||
uni->slashes.data = str + pos;
|
||||
|
@ -42,7 +41,8 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
|
|||
case PSYC_UNIFORM_SLASHES:
|
||||
if (c == '/')
|
||||
uni->slashes.length++;
|
||||
else return PSYC_PARSE_UNIFORM_INVALID_SLASHES;
|
||||
else
|
||||
return PSYC_PARSE_UNIFORM_INVALID_SLASHES;
|
||||
|
||||
if (uni->slashes.length == 2) {
|
||||
part = PSYC_UNIFORM_HOST;
|
||||
|
@ -69,8 +69,8 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
|
|||
|
||||
part = PSYC_UNIFORM_RESOURCE;
|
||||
p = &uni->resource;
|
||||
}
|
||||
else return PSYC_PARSE_UNIFORM_INVALID_HOST;
|
||||
} else
|
||||
return PSYC_PARSE_UNIFORM_INVALID_HOST;
|
||||
|
||||
p->data = str + pos + 1;
|
||||
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.length = 0;
|
||||
break;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
part = PSYC_UNIFORM_TRANSPORT;
|
||||
uni->transport.data = str + pos;
|
||||
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.length = 0;
|
||||
break;
|
||||
} else return PSYC_PARSE_UNIFORM_INVALID_RESOURCE;
|
||||
} else
|
||||
return PSYC_PARSE_UNIFORM_INVALID_RESOURCE;
|
||||
|
||||
case PSYC_UNIFORM_CHANNEL:
|
||||
if (psyc_is_name_char(c)) {
|
||||
uni->channel.length++;
|
||||
break;
|
||||
} else return PSYC_PARSE_UNIFORM_INVALID_CHANNEL;
|
||||
} else
|
||||
return PSYC_PARSE_UNIFORM_INVALID_CHANNEL;
|
||||
}
|
||||
pos++;
|
||||
}
|
||||
|
@ -150,16 +151,19 @@ int psyc_uniform_parse (PsycUniform *uni, char *str, size_t length)
|
|||
return PSYC_PARSE_UNIFORM_INVALID_HOST;
|
||||
|
||||
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)
|
||||
uni->host_port.length++;
|
||||
|
||||
uni->root.data = str;
|
||||
uni->root.length = uni->scheme.length + 1 + uni->slashes.length +
|
||||
uni->host_port.length;
|
||||
uni->root.length = uni->scheme.length + 1
|
||||
+ uni->slashes.length + uni->host_port.length;
|
||||
|
||||
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.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;
|
||||
}
|
||||
|
||||
} else return PSYC_PARSE_UNIFORM_INVALID_SCHEME;
|
||||
} else
|
||||
return PSYC_PARSE_UNIFORM_INVALID_SCHEME;
|
||||
|
||||
if (uni->host.length == 0)
|
||||
return PSYC_PARSE_UNIFORM_INVALID_HOST;
|
||||
|
|
|
@ -3,8 +3,7 @@
|
|||
|
||||
|
||||
/// Routing variables in alphabetical order.
|
||||
const PsycString psyc_routing_vars[] =
|
||||
{
|
||||
const PsycString psyc_routing_vars[] = {
|
||||
PSYC_C2STRI("_amount_fragments"),
|
||||
PSYC_C2STRI("_context"),
|
||||
//PSYC_C2STRI("_count"), // older PSYC
|
||||
|
@ -15,7 +14,8 @@ const PsycString psyc_routing_vars[] =
|
|||
//PSYC_C2STRI("_source_identification"), // older PSYC
|
||||
PSYC_C2STRI("_source_identity"),
|
||||
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_relay"),
|
||||
//PSYC_C2STRI("_tag_reply"), // older PSYC
|
||||
|
@ -27,8 +27,7 @@ const PsycString psyc_routing_vars[] =
|
|||
};
|
||||
|
||||
// Variable types in alphabetical order.
|
||||
const PsycDictInt psyc_var_types[] =
|
||||
{
|
||||
const PsycDictInt psyc_var_types[] = {
|
||||
{PSYC_C2STRI("_amount"), PSYC_TYPE_AMOUNT},
|
||||
{PSYC_C2STRI("_color"), PSYC_TYPE_COLOR},
|
||||
{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.
|
||||
*/
|
||||
inline
|
||||
PsycBool psyc_var_is_routing (const char *name, size_t len)
|
||||
inline PsycBool
|
||||
psyc_var_is_routing (const char *name, size_t len)
|
||||
{
|
||||
size_t cursor = 1;
|
||||
uint8_t i, m = 0;
|
||||
|
@ -60,16 +59,14 @@ PsycBool psyc_var_is_routing (const char *name, size_t len)
|
|||
return PSYC_FALSE;
|
||||
|
||||
// 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)
|
||||
matching[m++] = i;
|
||||
|
||||
matching[m] = -1; // mark the end of matching indexes
|
||||
|
||||
while (cursor < len && matching[0] >= 0)
|
||||
{
|
||||
for (i = m = 0; i < psyc_routing_vars_num; i++)
|
||||
{
|
||||
while (cursor < len && matching[0] >= 0) {
|
||||
for (i = m = 0; i < psyc_routing_vars_num; i++) {
|
||||
if (matching[i] < 0)
|
||||
break; // reached the end of possible matches
|
||||
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.
|
||||
*/
|
||||
inline
|
||||
PsycType psyc_var_type (const char *name, size_t len)
|
||||
inline PsycType
|
||||
psyc_var_type (const char *name, size_t len)
|
||||
{
|
||||
int8_t m[psyc_var_types_num];
|
||||
return (PsycType) psyc_dict_lookup((PsycDict*)psyc_var_types, psyc_var_types_num,
|
||||
name, len, PSYC_YES, (int8_t*)&m);
|
||||
return (PsycType) psyc_dict_lookup((PsycDict *) psyc_var_types,
|
||||
psyc_var_types_num, name, len, PSYC_YES,
|
||||
(int8_t *) &m);
|
||||
}
|
||||
|
|
16
test/test.c
16
test/test.c
|
@ -30,7 +30,9 @@
|
|||
// cmd line args
|
||||
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);
|
||||
if (n < 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:
|
||||
void *get_in_addr (struct sockaddr *sa) {
|
||||
void *
|
||||
get_in_addr (struct sockaddr *sa)
|
||||
{
|
||||
if (sa->sa_family == AF_INET)
|
||||
return &(((struct sockaddr_in*)sa)->sin_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]
|
||||
size_t i, nbytes, size;
|
||||
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 *recvbuf = buf + CONT_BUF_SIZE; // recv buf: ^^^^
|
||||
|
||||
|
|
18
test/test.h
18
test/test.h
|
@ -42,11 +42,19 @@
|
|||
#define HELP_P " -P\t\t\tShow progress\n"
|
||||
#define HELP_h " -h\t\t\tShow this help\n"
|
||||
|
||||
void test_init(int i);
|
||||
int test_input(int i, char *recvbuf, size_t nbytes);
|
||||
void
|
||||
test_init (int i);
|
||||
|
||||
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);
|
||||
int
|
||||
test_input (int i, char *recvbuf, size_t nbytes);
|
||||
|
||||
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
|
||||
|
|
|
@ -26,11 +26,15 @@ json_object *obj;
|
|||
json_tokener *tok;
|
||||
enum json_tokener_error error;
|
||||
|
||||
void test_init (int i) {
|
||||
void
|
||||
test_init (int i)
|
||||
{
|
||||
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;
|
||||
int r, ret = 0;
|
||||
|
||||
|
@ -82,7 +86,9 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
int c;
|
||||
|
||||
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_v CASE_S CASE_P
|
||||
case 'h':
|
||||
printf(
|
||||
HELP_FILE("test_json", "mnqSsvP")
|
||||
printf(HELP_FILE("test_json", "mnqSsvP")
|
||||
HELP_PORT("test_json", "nqsvP")
|
||||
HELP_f HELP_p HELP_b HELP_c
|
||||
HELP_m HELP_n HELP_q HELP_S
|
||||
|
|
|
@ -24,13 +24,17 @@ int exit_code;
|
|||
JsonParser *parser;
|
||||
JsonGenerator *generator;
|
||||
|
||||
void test_init (int i) {
|
||||
void
|
||||
test_init (int i)
|
||||
{
|
||||
g_type_init();
|
||||
parser = json_parser_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;
|
||||
GError *error = NULL;
|
||||
char *str;
|
||||
|
@ -77,8 +81,7 @@ int main (int argc, char **argv) {
|
|||
CASE_c CASE_n CASE_q
|
||||
CASE_s CASE_v CASE_P
|
||||
case 'h':
|
||||
printf(
|
||||
HELP_FILE("test_json_glib", "mnqSsvP")
|
||||
printf(HELP_FILE("test_json_glib", "mnqSsvP")
|
||||
HELP_PORT("test_json_glib", "nqsvP")
|
||||
HELP_f HELP_p HELP_b HELP_c
|
||||
HELP_m HELP_n HELP_q HELP_S
|
||||
|
|
|
@ -2,17 +2,33 @@
|
|||
#include <lib.h>
|
||||
|
||||
int main() {
|
||||
if (psyc_matches(PSYC_C2ARG("_failure_delivery"), PSYC_C2ARG("_failure_unsuccessful_delivery_death"))) return 1;
|
||||
if (psyc_matches(PSYC_C2ARG("_failure"), 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;
|
||||
if (psyc_matches(PSYC_C2ARG("_failure_delivery"),
|
||||
PSYC_C2ARG("_failure_unsuccessful_delivery_death")))
|
||||
return 1;
|
||||
if (psyc_matches(PSYC_C2ARG("_failure"),
|
||||
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.");
|
||||
|
||||
unless (psyc_inherits(PSYC_C2ARG("_failure_delivery"), PSYC_C2ARG("_failure_unsuccessful_delivery_death"))) 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;
|
||||
unless (psyc_inherits(PSYC_C2ARG("_failure_delivery"),
|
||||
PSYC_C2ARG("_failure_unsuccessful_delivery_death")))
|
||||
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.");
|
||||
|
||||
|
|
|
@ -5,7 +5,8 @@
|
|||
#include <psyc.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 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);
|
||||
|
||||
// try parsing that now
|
||||
do
|
||||
{
|
||||
do {
|
||||
oper = 0;
|
||||
name.length = 0;
|
||||
value.length = 0;
|
||||
|
@ -43,8 +43,7 @@ int main (int argc, char **argv)
|
|||
if (verbose)
|
||||
printf(">> ret = %d\n", ret);
|
||||
|
||||
switch (ret)
|
||||
{
|
||||
switch (ret) {
|
||||
case PSYC_PARSE_ROUTING:
|
||||
case PSYC_PARSE_ENTITY:
|
||||
if (verbose)
|
||||
|
@ -56,18 +55,15 @@ int main (int argc, char **argv)
|
|||
(int)name.length, name.data,
|
||||
(int)value.length, value.data);
|
||||
|
||||
if (psyc_var_is_list(PSYC_S2ARG(name)))
|
||||
{
|
||||
if (psyc_var_is_list(PSYC_S2ARG(name))) {
|
||||
if (verbose)
|
||||
printf(">> LIST START\n");
|
||||
|
||||
psyc_parse_list_state_init(&listState);
|
||||
psyc_parse_list_buffer_set(&listState, PSYC_S2ARG(value));
|
||||
|
||||
while ((ret = psyc_parse_list(&listState, &elem)))
|
||||
{
|
||||
switch (ret)
|
||||
{
|
||||
while ((ret = psyc_parse_list(&listState, &elem))) {
|
||||
switch (ret) {
|
||||
case PSYC_PARSE_LIST_END:
|
||||
case PSYC_PARSE_LIST_ELEM:
|
||||
if (verbose)
|
||||
|
@ -78,8 +74,7 @@ int main (int argc, char **argv)
|
|||
return 1;
|
||||
}
|
||||
|
||||
if (ret == PSYC_PARSE_LIST_END)
|
||||
{
|
||||
if (ret == PSYC_PARSE_LIST_END) {
|
||||
if (verbose)
|
||||
printf(">> LIST END\n");
|
||||
break;
|
||||
|
|
|
@ -31,14 +31,16 @@ PsycModifier entity[NUM_PARSERS][ENTITY_LINES];
|
|||
|
||||
int contbytes, exit_code;
|
||||
|
||||
static inline
|
||||
void resetString (PsycString *s, uint8_t freeptr);
|
||||
static inline void
|
||||
resetString (PsycString *s, uint8_t freeptr);
|
||||
|
||||
// initialize parser & packet variables
|
||||
void test_init (int i) {
|
||||
void
|
||||
test_init (int i)
|
||||
{
|
||||
// reset parser state & packet
|
||||
psyc_parse_state_init(&parsers[i], routing_only ?
|
||||
PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL);
|
||||
psyc_parse_state_init(&parsers[i],
|
||||
routing_only ? PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL);
|
||||
|
||||
memset(&packets[i], 0, sizeof(PsycPacket));
|
||||
memset(&routing[i], 0, sizeof(PsycModifier) * ROUTING_LINES);
|
||||
|
@ -48,7 +50,9 @@ void test_init (int i) {
|
|||
}
|
||||
|
||||
// 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;
|
||||
char sendbuf[SEND_BUF_SIZE];
|
||||
char *parsebuf = recvbuf - contbytes;
|
||||
|
@ -82,7 +86,8 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
|
||||
do {
|
||||
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)
|
||||
ret = exit_code = psyc_parse(parser, &oper, &name, &value);
|
||||
if (verbose >= 2)
|
||||
|
@ -146,12 +151,14 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
|
||||
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 (filename && write(1, sendbuf, packet->length) == -1) {
|
||||
perror("write");
|
||||
ret = -1;
|
||||
} else if (!filename && send(i, sendbuf, packet->length, 0) == -1) {
|
||||
} else if (!filename && -1 == send(i, sendbuf,
|
||||
packet->length, 0)) {
|
||||
perror("send");
|
||||
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 (verbose >= 3)
|
||||
printf("# remaining = [%.*s]\n", (int)contbytes, psyc_parse_remaining_buffer(parser));
|
||||
assert(contbytes <= CONT_BUF_SIZE); // make sure it's still in the buffer
|
||||
memmove(recvbuf - contbytes, psyc_parse_remaining_buffer(parser), contbytes);
|
||||
printf("# remaining = [%.*s]\n",
|
||||
(int)contbytes, psyc_parse_remaining_buffer(parser));
|
||||
assert(contbytes <= CONT_BUF_SIZE); // make sure it fits in the buffer
|
||||
memmove(recvbuf - contbytes,
|
||||
psyc_parse_remaining_buffer(parser), contbytes);
|
||||
}
|
||||
ret = 0;
|
||||
break;
|
||||
|
@ -312,8 +321,8 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
return ret;
|
||||
}
|
||||
|
||||
static inline
|
||||
void resetString (PsycString *s, uint8_t freeptr)
|
||||
static inline void
|
||||
resetString (PsycString *s, uint8_t freeptr)
|
||||
{
|
||||
if (freeptr && s->length)
|
||||
free((void*)s->data);
|
||||
|
@ -322,7 +331,9 @@ void resetString (PsycString *s, uint8_t freeptr)
|
|||
s->length = 0;
|
||||
}
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
int c;
|
||||
while ((c = getopt (argc, argv, "f:p:b:c:mnqrsvPSh")) != -1) {
|
||||
switch (c) {
|
||||
|
@ -330,8 +341,7 @@ int main (int argc, char **argv) {
|
|||
CASE_m CASE_n CASE_q CASE_r
|
||||
CASE_s CASE_v CASE_S CASE_P
|
||||
case 'h':
|
||||
printf(
|
||||
HELP_FILE("test_psyc", "mnqrSsvP")
|
||||
printf(HELP_FILE("test_psyc", "mnqrSsvP")
|
||||
HELP_PORT("test_psyc", "nqrsvP")
|
||||
HELP_f HELP_p HELP_b HELP_c
|
||||
HELP_m HELP_n HELP_r
|
||||
|
|
|
@ -25,12 +25,16 @@ size_t count = 1, recv_buf_size;
|
|||
|
||||
PsycParseState parser;
|
||||
|
||||
void test_init (int i) {
|
||||
psyc_parse_state_init(&parser, routing_only ?
|
||||
PSYC_PARSE_ROUTING_ONLY : PSYC_PARSE_ALL);
|
||||
void
|
||||
test_init (int i)
|
||||
{
|
||||
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;
|
||||
PsycString name, value;
|
||||
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;
|
||||
while ((c = getopt (argc, argv, "f:p:b:c:rsh")) != -1) {
|
||||
switch (c) {
|
||||
CASE_f CASE_p CASE_b CASE_c
|
||||
CASE_r CASE_s
|
||||
CASE_f CASE_p CASE_b CASE_c CASE_r CASE_s
|
||||
case 'h':
|
||||
printf(
|
||||
HELP_FILE("test_psyc_speed", "rs")
|
||||
printf(HELP_FILE("test_psyc_speed", "rs")
|
||||
HELP_PORT("test_psyc_speed", "rs")
|
||||
HELP_f HELP_p HELP_b HELP_c
|
||||
HELP_r HELP_s HELP_h,
|
||||
|
|
|
@ -7,7 +7,8 @@
|
|||
#define myUNI "psyc://10.100.1000/~ludwig"
|
||||
|
||||
/* 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 *rendered, uint8_t verbose)
|
||||
{
|
||||
|
@ -40,7 +41,8 @@ int testPresence (const char *avail, int availlen,
|
|||
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];
|
||||
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;
|
||||
psyc_list_init(&list_text, elems_text, 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);
|
||||
psyc_list_init(&list_text, elems_text,
|
||||
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];
|
||||
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);
|
||||
}
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
uint8_t verbose = argc > 1;
|
||||
|
||||
if (testPresence(PSYC_C2ARG("_here"), PSYC_C2ARG("I'm omnipresent right now"), "\
|
||||
|
|
|
@ -21,10 +21,15 @@ size_t count = 1, recv_buf_size;
|
|||
|
||||
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);
|
||||
|
||||
if (!len) {
|
||||
|
@ -35,16 +40,16 @@ int test_input (int i, char *recvbuf, size_t nbytes) {
|
|||
return 0;
|
||||
}
|
||||
|
||||
int main (int argc, char **argv) {
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
int c;
|
||||
|
||||
while ((c = getopt (argc, argv, "f:p:b:c:sh")) != -1) {
|
||||
switch (c) {
|
||||
CASE_f CASE_p CASE_b CASE_c
|
||||
CASE_s
|
||||
CASE_f CASE_p CASE_b CASE_c CASE_s
|
||||
case 'h':
|
||||
printf(
|
||||
HELP_FILE("test_strlen", "s")
|
||||
printf(HELP_FILE("test_strlen", "s")
|
||||
HELP_PORT("test_strlen", "s")
|
||||
HELP_f HELP_p HELP_b HELP_c
|
||||
HELP_s HELP_h,
|
||||
|
|
|
@ -7,7 +7,8 @@
|
|||
|
||||
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)
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
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;
|
||||
}
|
||||
|
||||
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)
|
||||
printf("> getValue: %.*s\n", (int)len, name);
|
||||
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;
|
||||
size_t length = 0;
|
||||
PsycTextRC ret;
|
||||
|
||||
psyc_text_state_init(&state, template, tmplen, buffer, buflen);
|
||||
do
|
||||
{
|
||||
do {
|
||||
ret = psyc_text(&state, getValue, NULL);
|
||||
length += psyc_text_bytes_written(&state);
|
||||
switch (ret)
|
||||
{
|
||||
switch (ret) {
|
||||
case PSYC_TEXT_INCOMPLETE:
|
||||
if (verbose)
|
||||
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
|
||||
}
|
||||
|
||||
int main(int argc, char **argv)
|
||||
int
|
||||
main (int argc, char **argv)
|
||||
{
|
||||
verbose = argc > 1;
|
||||
char buffer[BUFSIZE];
|
||||
|
@ -85,11 +89,11 @@ int main(int argc, char **argv)
|
|||
if (memcmp(result.data, PSYC_C2ARG("Hello & !")))
|
||||
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;
|
||||
|
||||
for (i = 1; i < 22; i++)
|
||||
{
|
||||
for (i = 1; i < 22; i++) {
|
||||
testText(str, len, buffer, i, &result, &getValueFooBar);
|
||||
if (memcmp(result.data, PSYC_C2ARG("Hello Foo Bar & Foo Bar!")))
|
||||
return 10 + i;
|
||||
|
|
|
@ -4,13 +4,15 @@
|
|||
#include <lib.h>
|
||||
|
||||
void
|
||||
testUniform (char *str, int ret) {
|
||||
testUniform (char *str, int ret)
|
||||
{
|
||||
PsycUniform *uni = malloc(sizeof(PsycUniform));
|
||||
memset(uni, 0, sizeof(PsycUniform));
|
||||
printf("%s\n", 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->slashes),
|
||||
(int)PSYC_S2ARG2(uni->host),
|
||||
|
@ -25,12 +27,15 @@ testUniform (char *str, int ret) {
|
|||
|
||||
free(uni);
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
int main () {
|
||||
int
|
||||
main ()
|
||||
{
|
||||
testUniform("psyc://foo.tld:4404d/@bar#baz", PSYC_SCHEME_PSYC);
|
||||
testUniform("psyc://foo:4405/~bar", 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://host:/~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");
|
||||
return 0;
|
||||
|
|
|
@ -6,8 +6,7 @@
|
|||
|
||||
int main() {
|
||||
#if 0
|
||||
const char* vars[] =
|
||||
{
|
||||
const char* vars[] = {
|
||||
"_source",
|
||||
"_source_relay",
|
||||
"_source_foo",
|
||||
|
@ -18,8 +17,7 @@ int main() {
|
|||
};
|
||||
|
||||
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\n", vars[i], psyc_var_is_routing(vars[i], strlen(vars[i])));
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue