diff --git a/configure.ac b/configure.ac index bd5fb46..1727292 100644 --- a/configure.ac +++ b/configure.ac @@ -1,6 +1,6 @@ dnl Process this file with autoconf to produce a configure script. -AC_INIT(libsexpr, 1.2) +AC_INIT(libsexpr, 1.3) AC_CONFIG_HEADERS([config.h]) diff --git a/include/sexpr/cstring.h b/include/sexpr/cstring.h index 30ca805..79e7458 100644 --- a/include/sexpr/cstring.h +++ b/include/sexpr/cstring.h @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -42,10 +42,6 @@ LA-CC-04-094 * * -matt sottile */ -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ #ifndef __CSTRING_H__ #define __CSTRING_H__ diff --git a/include/sexpr/faststack.h b/include/sexpr/faststack.h index 120beb2..195895d 100644 --- a/include/sexpr/faststack.h +++ b/include/sexpr/faststack.h @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -39,11 +39,6 @@ LA-CC-04-094 * * \brief Implementation of a fast stack with smart memory management. */ - -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ #ifndef __FASTSTACK_H__ #define __FASTSTACK_H__ @@ -108,7 +103,7 @@ extern "C" { * Return a pointer to an empty stack structure. If the return value is * NULL, one should check sexp_errno to determine why. */ - faststack_t *make_stack(void); + faststack_t *make_stack(); /** * Given a stack structure, destroy it and free all of the stack levels. diff --git a/include/sexpr/sexp.h b/include/sexpr/sexp.h index 46b8701..e54987d 100644 --- a/include/sexpr/sexp.h +++ b/include/sexpr/sexp.h @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -34,22 +34,16 @@ LA-CC-04-094 @endcond **/ -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #ifndef __SEXP_H__ #define __SEXP_H__ #include #include /* for BUFSIZ only */ +#include "faststack.h" +#include "cstring.h" +#include "sexp_memory.h" +#include "sexp_errors.h" -/* local includes */ -#include -#include -#include -#include /* doxygen documentation groups defined here */ /** @@ -94,7 +88,7 @@ LA-CC-04-094 * \section credits Credits * * SFSEXP: Small, Fast S-Expression Library version 1.2, October 2007 \n - * Written by Matthew Sottile (matt@cs.uoregon.edu) + * Written by Matthew Sottile (mjsottile@gmail.com) * * \section license License Information * @@ -346,13 +340,13 @@ typedef struct parser_event_handlers { * The start_sexpr function pointer is called when an open parenthesis * is encountered starting an expression. */ - void (* start_sexpr)(void); + void (* start_sexpr)(); /** * The end_sexpr function pointer is called when an close parenthesis * is encountered ending an expression. */ - void (* end_sexpr)(void); + void (* end_sexpr)(); /** * The characters function pointer is called when an atom is completely @@ -563,7 +557,7 @@ typedef struct sexp_iowrap { * Byte count for last read. If it is -1, there was an error. Otherwise, * it will be a value from 0 to BUFSIZ. */ - int cnt; + size_t cnt; } sexp_iowrap_t; /*========*/ @@ -669,6 +663,16 @@ extern "C" { * Allocate a new sexp_t element representing a list. */ sexp_t *new_sexp_list(sexp_t *l); + + /** + * Allocate a new sexp_t element representing a raw binary atom. + * This element will contain a pointer to the raw binary data + * provided, as well as the binary data length. The character + * atom fields will be NULL and the corresponding val + * length and allocation size will be set to zero since this + * element is carrying a binary pointer only. + */ + sexp_t *new_sexp_binary_atom(char *data, size_t binlength); /** * Allocate a new sexp_t element representing a value. The user must @@ -758,7 +762,17 @@ extern "C" { /** * reset the value of sexp_errno to SEXP_ERR_OK. */ - void reset_sexp_errno(void); + void reset_sexp_errno(); + + /** + * print the contents of the parser continuation stack to a buffer. + * this is useful if an expression is partially parsed and the caller + * realizes that something is wrong with it. with this routine, + * the caller can reconstruct the expression parsed so far and use it + * for error reporting. this works with fixed size buffers allocated + * by the caller. there is not a CSTRING-based version currently. + */ + void print_pcont(pcont_t * pc, char * buf, size_t buflen); /* this is for C++ users */ #ifdef __cplusplus diff --git a/include/sexpr/sexp_errors.h b/include/sexpr/sexp_errors.h index b4bf11a..87c63f0 100644 --- a/include/sexpr/sexp_errors.h +++ b/include/sexpr/sexp_errors.h @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -34,11 +34,6 @@ LA-CC-04-094 @endcond **/ -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #ifndef __SEXP_ERRORS_H__ #define __SEXP_ERRORS_H__ @@ -132,7 +127,21 @@ typedef enum { /** * unknown parser state */ - SEXP_ERR_UNKNOWN_STATE + SEXP_ERR_UNKNOWN_STATE, + + /** + * parsing is incomplete and need more data to complete it. + */ + SEXP_ERR_INCOMPLETE, + + /** + * this error code indicates that an atom was created with + * the incorrect constructor. For example, attempting to + * create a binary mode atom with the new_sexp_atom + * constructor intended for text atoms will cause this to + * be set. + */ + SEXP_ERR_BAD_CONSTRUCTOR } sexp_errcode_t; diff --git a/include/sexpr/sexp_memory.h b/include/sexpr/sexp_memory.h index 18bb856..5d902bf 100644 --- a/include/sexpr/sexp_memory.h +++ b/include/sexpr/sexp_memory.h @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -34,11 +34,6 @@ LA-CC-04-094 @endcond **/ -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #ifndef __SEXP_MEMORY_H__ #define __SEXP_MEMORY_H__ diff --git a/include/sexpr/sexp_ops.h b/include/sexpr/sexp_ops.h index e5049f0..18ef492 100644 --- a/include/sexpr/sexp_ops.h +++ b/include/sexpr/sexp_ops.h @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -34,11 +34,6 @@ LA-CC-04-094 @endcond **/ -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #ifndef __SEXP_OPS_H__ #define __SEXP_OPS_H__ @@ -50,7 +45,7 @@ LA-CC-04-094 * A set of routines for operating on s-expressions. */ -#include +#include "sexp.h" #ifdef __cplusplus extern "C" { diff --git a/include/sexpr/sexp_vis.h b/include/sexpr/sexp_vis.h index b068360..8ef642f 100644 --- a/include/sexpr/sexp_vis.h +++ b/include/sexpr/sexp_vis.h @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -37,10 +37,6 @@ LA-CC-04-094 /** * \defgroup viz Visualization and debugging routines */ -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ /** * \file sexp_vis.h diff --git a/lib/Makefile.am b/lib/Makefile.am index 6a4067a..ec8397d 100644 --- a/lib/Makefile.am +++ b/lib/Makefile.am @@ -4,6 +4,7 @@ AM_CPPFLAGS = \ -DPACKAGE_LOCALE_DIR=\""$(localedir)"\" \ -DPACKAGE_SRC_DIR=\""$(srcdir)"\" \ -DPACKAGE_DATA_DIR=\""$(pkgdatadir)"\" \ + -D_NO_MEMORY_MANAGEMENT_ \ $(LIBTDATA_CFLAGS) -I../include AM_CFLAGS =\ diff --git a/lib/cstring.c b/lib/cstring.c index c91a194..62a72be 100644 --- a/lib/cstring.c +++ b/lib/cstring.c @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -37,11 +37,6 @@ LA-CC-04-094 /** * Implementation of stuff in cstring.h to make ron happy */ -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #include #include #include @@ -92,7 +87,7 @@ CSTRING *snew(size_t s) { } CSTRING *sadd(CSTRING *s, char *a) { - int alen; + size_t alen; char *newbase; /* no string, so bail */ diff --git a/lib/event_temp.c b/lib/event_temp.c index e9734d1..0ac27b7 100644 --- a/lib/event_temp.c +++ b/lib/event_temp.c @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -34,11 +34,6 @@ LA-CC-04-094 @endcond **/ -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #include #include @@ -773,7 +768,7 @@ eparse_sexp (char *str, size_t len, pcont_t *lc) state = 15; vcur[0] = '\0'; - binexpected = atoi(val); + binexpected = (size_t) atoi(val); assert(binexpected > 0); binread = 0; #ifdef __cplusplus diff --git a/lib/faststack.c b/lib/faststack.c index 5f00c2b..9d4ccd5 100644 --- a/lib/faststack.c +++ b/lib/faststack.c @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -39,12 +39,6 @@ LA-CC-04-094 * * matt sottile / matt@lanl.gov */ - -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #include #include #include @@ -123,7 +117,7 @@ destroy_stack (faststack_t * s) faststack_t * push (faststack_t * cur_stack, void *data) { - stack_lvl_t *top; + stack_lvl_t *top, *tmp; if (cur_stack == NULL) { sexp_errno = SEXP_ERR_BAD_STACK; @@ -131,7 +125,6 @@ push (faststack_t * cur_stack, void *data) } top = cur_stack->top; - stack_lvl_t *tmp; /* if top isn't null, try to push above it. */ if (top != NULL) diff --git a/lib/io.c b/lib/io.c index 7309466..8cb9524 100644 --- a/lib/io.c +++ b/lib/io.c @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -34,15 +34,14 @@ LA-CC-04-094 @endcond **/ - -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - -#include #include -#include +#ifndef WIN32 +# include +#else +# define ssize_t int +# include +# include +#endif #include #include #include @@ -55,9 +54,9 @@ sexp_iowrap_t *init_iowrap(int fd) { sexp_iowrap_t *iow; #ifdef __cplusplus - iow = (sexp_iowrap_t *)sexp_malloc(sizeof(sexp_iowrap_t)); + iow = (sexp_iowrap_t *)sexp_calloc(1,sizeof(sexp_iowrap_t)); #else - iow = sexp_malloc(sizeof(sexp_iowrap_t)); + iow = sexp_calloc(1,sizeof(sexp_iowrap_t)); #endif if (iow == NULL) { @@ -106,7 +105,7 @@ sexp_t *read_one_sexp(sexp_iowrap_t *iow) { } if (iow->cnt == 0) { - iow->cnt = read(iow->fd,iow->buf,BUFSIZ); + iow->cnt = (size_t) read(iow->fd,iow->buf,BUFSIZ); if (iow->cnt == 0) { sexp_errno = SEXP_ERR_IO_EMPTY; @@ -122,7 +121,7 @@ sexp_t *read_one_sexp(sexp_iowrap_t *iow) { return NULL; } - iow->cnt = read(iow->fd,iow->buf,BUFSIZ); + iow->cnt = (size_t) read(iow->fd,iow->buf,BUFSIZ); if (iow->cnt == 0) { sexp_errno = SEXP_ERR_IO_EMPTY; diff --git a/lib/parser.c b/lib/parser.c index 452e279..ce07034 100644 --- a/lib/parser.c +++ b/lib/parser.c @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -34,16 +34,9 @@ LA-CC-04-094 @endcond **/ - -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #include #include #include - #include #include @@ -84,7 +77,9 @@ parse_data_t; /** * parse_data_t stack - similar malloc prevention to sexp_t_cache. */ +#ifndef _NO_MEMORY_MANAGEMENT_ faststack_t *pd_cache; +#endif /** * The global sexp_t_cache is a faststack implementing a cache of @@ -94,7 +89,9 @@ faststack_t *pd_cache; * This should be left alone and manipulated only by the sexp_t_allocate and * sexp_t_deallocate functions. Touching the stack is bad. */ +#ifndef _NO_MEMORY_MANAGEMENT_ faststack_t *sexp_t_cache; +#endif /** * sexp_t allocation @@ -104,7 +101,7 @@ sexp_t * sexp_t_allocate(void) { sexp_t *sx = sexp_calloc(1, sizeof(sexp_t)); if (sx == NULL) { - sexp_errno = SEXP_MEMORY; + sexp_errno = SEXP_ERR_MEMORY; return NULL; } @@ -237,6 +234,14 @@ void sexp_cleanup(void) { /** * allocation */ +#ifdef _NO_MEMORY_MANAGEMENT_ +parse_data_t * +pd_allocate(void) { + parse_data_t *p = NULL; + p = sexp_malloc(sizeof(parse_data_t)); + return p; +} +#else parse_data_t * pd_allocate(void) { parse_data_t *p; @@ -281,10 +286,17 @@ pd_allocate(void) { return p; } +#endif /* _NO_MEMORY_MANAGEMENT_ */ /** * de-allocation */ +#ifdef _NO_MEMORY_MANAGEMENT_ +void +pd_deallocate(parse_data_t *p) { + sexp_free(p, sizeof(parse_data_t)); +} +#else void pd_deallocate(parse_data_t *p) { if (pd_cache == NULL) { @@ -298,6 +310,101 @@ pd_deallocate(parse_data_t *p) { pd_cache = push(pd_cache, p); } +#endif /* _NO_MEMORY_MANAGEMENT_ */ + +/** + * print the current parsing state based on the contents of the parser + * continuation. Useful for error reporting if an error is detected + * while the current expression being parsed is incomplete. + */ +void print_pcont(pcont_t * pc, char * buf, size_t buflen) { + char *cur = buf; + int loc = 0; + int n; + stack_lvl_t *lvl; + parse_data_t *pdata; + sexp_t *sx; + + /* return if either the buffer or continuation are null */ + if (buf == NULL) return; + if (pc == NULL) return; + + /* if continuation has no stack, return */ + if (pc->stack == NULL) return; + + /* start at the bottom of the stack */ + lvl = pc->stack->bottom; + + /* go until we either run out of buffer space or we hit the + top of the stack */ + while (loc < buflen-1 && lvl != NULL) { + /* get the data at the current stack level */ + pdata = (parse_data_t *)lvl->data; + + /* if this is null, we're at a level with nothing added yet */ + if (pdata == NULL) break; + + /* get first fully parsed sexpr for this level. this could be + any sub-expression, like an atom or a full s-expression */ + sx = pdata->fst; + + /* spin through all of the s-expressions at this level */ + while (sx != NULL) { + + /* if we have a list that has no contents, just add the open + paren. this means we haven't finished this expression and the + stack contains it's partial contents. Just print the open paren + and break out so we can pop up the stack. */ + if (sx->ty == SEXP_LIST && sx->list == NULL) { + cur[0] = '('; + cur++; + loc++; + break; + } else { + /* print the fully parsed sub-expression */ + n = print_sexp(cur,buflen-loc,sx); + + /* add a space between this and the next expression. note that + this may induce spaces that were not part of the original + expression. */ + cur[n] = ' '; + + /* increment n to compensate for the space we added */ + n++; + + /* push the pointer into the output buffer forward by n */ + cur += n; + + /* increment counter for location in buffer by n */ + loc += n; + } + + /* go to next s-expr */ + sx = sx->next; + } + + /* go up to next level in stack */ + lvl = lvl->above; + } + + /* at this point, all that may remain is a partially parsed string + that hasn't been turned into a sexpr yet. attach it to the + output string. */ + if (pc->val_used < (buflen-loc)-1) { + strncpy(cur, pc->val, pc->val_used); + cur += pc->val_used; + } else { + /* don't bother if we're so close to the end of the buffer that + we can't attach our null terminator. */ + if (buflen-loc > 2) { + strncpy(cur, pc->val, (buflen-loc)-2); + cur += (buflen-loc)-2; + } + } + + /* add null terminator */ + cur[0] = '\0'; +} /** * Destroy a continuation by freeing all of its fields that it is responsible @@ -378,7 +485,6 @@ parse_sexp (char *s, size_t len) pc = cparse_sexp (s, len, pc); if (pc == NULL) return NULL; /* assume that cparse_sexp set sexp_errno */ sx = pc->last_sexp; - sexp_errno = pc->error; destroy_continuation(pc); @@ -476,7 +582,6 @@ iparse_sexp (char *s, size_t len, pcont_t *cc) { if (pc == NULL) return NULL; /* assume cparse_sexp set sexp_errno */ if (cc->last_sexp != NULL) { - sexp_errno = cc->error; sx = cc->last_sexp; cc->last_sexp = NULL; } @@ -526,7 +631,13 @@ cparse_sexp (char *str, size_t len, pcont_t *lc) parser_event_handlers_t *event_handlers = NULL; /*** define a macro used for stashing continuation state away ***/ - /** NOTE: sbuffer is set manually as appropriate. **/ + /** NOTE1: sbuffer is set manually as appropriate. **/ + /** NOTE2: this also sets sexp_errno to the same value as the + error field in the continuation. This used to be + done in iparse_sexp and parse_sexp, but that meant that + direct callers of cparse_sexp would see inconsistent errors. + sexp_errno could say one thing, but cc would say the other. + This has been fixed. **/ #define SAVE_CONT_STATE(err,ls) { \ cc->bindata = bindata; \ cc->binread = binread; \ @@ -546,6 +657,7 @@ cparse_sexp (char *str, size_t len, pcont_t *lc) cc->last_sexp = (ls); \ cc->error = (err); \ cc->event_handlers = event_handlers; \ + sexp_errno = (err); \ } /*** end continuation state saving macro ***/ @@ -1424,12 +1536,7 @@ cparse_sexp (char *str, size_t len, pcont_t *lc) state = 15; vcur[0] = '\0'; - binexpected = atoi(val); - - if (binexpected < 0) { - SAVE_CONT_STATE(SEXP_ERR_BADCONTENT, NULL); - return cc; - } + binexpected = (size_t) atoi(val); binread = 0; if (binexpected > 0) { @@ -1562,7 +1669,7 @@ cparse_sexp (char *str, size_t len, pcont_t *lc) state = 1; SAVE_CONT_STATE(SEXP_ERR_OK, sx); } else { - SAVE_CONT_STATE(SEXP_ERR_OK, NULL); + SAVE_CONT_STATE(SEXP_ERR_INCOMPLETE, NULL); if (t[0] == '\0' || t == bufEnd) cc->lastPos = NULL; else diff --git a/lib/sexp.c b/lib/sexp.c index 52d7c2c..63759ac 100644 --- a/lib/sexp.c +++ b/lib/sexp.c @@ -1,9 +1,9 @@ /** -@cond IGNORE +@Cond IGNORE ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -34,16 +34,9 @@ LA-CC-04-094 @endcond **/ - -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #include #include #include - #include #include @@ -94,7 +87,7 @@ int print_sexp (char *buf, size_t size, const sexp_t * sx) { int retval; - int sz; + size_t sz; char *b = buf, *tc; size_t left = size; int depth = 0; @@ -251,8 +244,6 @@ print_sexp (char *buf, size_t size, const sexp_t * sx) left--; } - if (left < 0) - left = 0; if (left == 0) { sexp_errno = SEXP_ERR_BUFFER_FULL; @@ -311,7 +302,7 @@ print_sexp (char *buf, size_t size, const sexp_t * sx) if (left != 0) { b[0] = 0; - retval = (size-left); + retval = (int) (size-left); } else { b--; b[0] = 0; @@ -480,7 +471,7 @@ print_sexp_cstr (CSTRING **s, const sexp_t *sx, size_t ss) if (_s == NULL) retval = 0; else - retval = _s->curlen; + retval = (int) _s->curlen; destroy_stack (stack); sexp_t_deallocate(fakehead); @@ -510,11 +501,40 @@ sexp_t *new_sexp_list(sexp_t *l) { return sx; } +/** + * allocate a new sexp_t element representing a raw binary value + */ +sexp_t *new_sexp_binary_atom(char *data, size_t binlength) { + sexp_t *sx = sexp_t_allocate(); + + if (sx == NULL) { + sexp_errno = SEXP_ERR_MEMORY; + return NULL; + } + + sx->ty = SEXP_VALUE; + sx->next = sx->list = NULL; + sx->aty = SEXP_BINARY; + sx->bindata = data; + sx->binlength = binlength; + sx->val = NULL; + sx->val_used = sx->val_allocated = 0; + + return sx; +} + /** * allocate a new sexp_t element representing a value */ sexp_t *new_sexp_atom(const char *buf, size_t bs, atom_t aty) { - sexp_t *sx = sexp_t_allocate(); + sexp_t *sx = NULL; + + if (aty == SEXP_BINARY) { + sexp_errno = SEXP_ERR_BAD_CONSTRUCTOR; + return NULL; + } + + sx = sexp_t_allocate(); if (sx == NULL) { sexp_errno = SEXP_ERR_MEMORY; diff --git a/lib/sexp_memory.c b/lib/sexp_memory.c index c297ac1..2961e64 100644 --- a/lib/sexp_memory.c +++ b/lib/sexp_memory.c @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -34,15 +34,8 @@ LA-CC-04-094 @endcond **/ - -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #include #include - #include #include #include diff --git a/lib/sexp_ops.c b/lib/sexp_ops.c index 57e4548..ca15436 100644 --- a/lib/sexp_ops.c +++ b/lib/sexp_ops.c @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -34,16 +34,9 @@ LA-CC-04-094 @endcond **/ - -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #include #include #include - #include /** @@ -137,94 +130,95 @@ int sexp_list_length(const sexp_t *sx) { * Copy an s-expression. */ sexp_t *copy_sexp(const sexp_t *s) { - sexp_t *snew; + sexp_t *s_new; if (s == NULL) return NULL; - snew = sexp_t_allocate(); - if (snew == NULL) { + s_new = sexp_t_allocate(); + if (s_new == NULL) { sexp_errno = SEXP_ERR_MEMORY; return NULL; } /* initialize fields to null and zero, and fill in only those necessary. */ - snew->val_allocated = snew->val_used = 0; - snew->val = NULL; - snew->list = snew->next = NULL; - snew->bindata = NULL; - snew->binlength = 0; + s_new->val_allocated = s_new->val_used = 0; + s_new->val = NULL; + s_new->list = s_new->next = NULL; + s_new->bindata = NULL; + s_new->binlength = 0; /* now start copying in data and setting appropriate fields. */ - snew->ty = s->ty; + s_new->ty = s->ty; /* values */ - if (snew->ty == SEXP_VALUE) { - snew->aty = s->aty; + if (s_new->ty == SEXP_VALUE) { + s_new->aty = s->aty; /* binary */ - if (snew->aty == SEXP_BINARY) { + if (s_new->aty == SEXP_BINARY) { if (s->bindata == NULL && s->binlength > 0) { sexp_errno = SEXP_ERR_BADCONTENT; - sexp_t_deallocate(snew); + sexp_t_deallocate(s_new); return NULL; } - snew->binlength = s->binlength; + s_new->binlength = s->binlength; if (s->bindata == NULL) { - snew->bindata = NULL; + s_new->bindata = NULL; } else { /** allocate space **/ #ifdef __cplusplus - snew->bindata = (char *)sexp_malloc(sizeof(char)*s->binlength); + s_new->bindata = (char *)sexp_malloc(sizeof(char)*s->binlength); #else - snew->bindata = sexp_malloc(sizeof(char)*s->binlength); + s_new->bindata = sexp_malloc(sizeof(char)*s->binlength); #endif } - if (snew->bindata == NULL) { + if (s_new->bindata == NULL) { sexp_errno = SEXP_ERR_MEMORY; - sexp_t_deallocate(snew); + sexp_t_deallocate(s_new); return NULL; } - memcpy(snew->bindata,s->bindata,s->binlength*sizeof(char)); + memcpy(s_new->bindata,s->bindata,s->binlength*sizeof(char)); /* non-binary */ } else { if (s->val == NULL && (s->val_used > 0 || s->val_allocated > 0)) { sexp_errno = SEXP_ERR_BADCONTENT; - sexp_t_deallocate(snew); + sexp_t_deallocate(s_new); return NULL; } - snew->val_used = snew->val_allocated = s->val_used; + s_new->val_used = s->val_used; + s_new->val_allocated = s->val_allocated; if (s->val == NULL) { - snew->val = NULL; + s_new->val = NULL; } else { /** allocate space **/ #ifdef __cplusplus - snew->val = (char *)sexp_malloc(sizeof(char)*s->val_used); + s_new->val = (char *)sexp_calloc(1,sizeof(char)*s->val_allocated); #else - snew->val = sexp_malloc(sizeof(char)*s->val_used); + s_new->val = sexp_calloc(1,sizeof(char)*s->val_allocated); #endif - if (snew->val == NULL) { + if (s_new->val == NULL) { sexp_errno = SEXP_ERR_MEMORY; - sexp_t_deallocate(snew); + sexp_t_deallocate(s_new); return NULL; } - strcpy(snew->val,s->val); + memcpy(s_new->val, s->val, sizeof(char)*s->val_used); } } } else { - snew->list = copy_sexp(s->list); + s_new->list = copy_sexp(s->list); } - snew->next = copy_sexp(s->next); + s_new->next = copy_sexp(s->next); - return snew; + return s_new; } diff --git a/lib/sexp_vis.c b/lib/sexp_vis.c index 1408f80..c8ca07a 100644 --- a/lib/sexp_vis.c +++ b/lib/sexp_vis.c @@ -3,7 +3,7 @@ ====================================================== SFSEXP: Small, Fast S-Expression Library version 1.2 - Written by Matthew Sottile (matt@cs.uoregon.edu) + Written by Matthew Sottile (mjsottile@gmail.com) ====================================================== Copyright (2003-2006). The Regents of the University of California. This @@ -34,12 +34,6 @@ LA-CC-04-094 @endcond **/ - -/** - * Ported to Jari OS by Alfeiks Kaanoken - * (c) Jari OS Core dev team 2005-2009 - */ - #include #include