added putc to Stdio.

renamed/refactored some encoding conversion functions.
added stix_errnumtoerrstr().
added generr.st to generate error string code
This commit is contained in:
hyunghwan.chung 2016-12-27 18:15:35 +00:00
parent a9c48b75e6
commit d03b97f19d
17 changed files with 528 additions and 214 deletions

View File

@ -332,10 +332,39 @@
} }
} }
(* --------------------
#pooldic Error
{
#NONE := error(0).
#GENERIC := error(1).
#
}
------------------- *)
#class Error(Apex) #class Error(Apex)
{ {
(* ----------------------------
TODO: support constant declaration...
#const
{
#NONE := error(0).
#GENERIC := error(1).
}
-------------------------------- *)
#method isError #method isError
{ {
^true ^true
} }
#method asInteger
{
<primitive: #_error_as_integer>
}
#method asString
{
<primitive: #_error_as_string>
}
} }

85
stix/kernel/generr.st Normal file
View File

@ -0,0 +1,85 @@
#include 'Stix.st'.
#class MyObject(Object)
{
#method(#class) main
{
| s c f |
s := #(
'no error'
'generic error'
'not implemented'
'subsystem error'
'internal error that should never have happened'
'insufficient system memory'
'insufficient object memory'
'invalid parameter or argument'
'data not found'
'existing/duplicate data'
'busy'
'access denied'
'operation not permitted'
'not a directory'
'interrupted'
'pipe error'
'resource temporarily unavailable'
'data too large'
'message sending error'
'range error'
'byte-code full'
'dictionary full'
'processor full'
'semaphore heap full'
'semaphore list full'
'divide by zero'
'I/O error'
'encoding conversion error'
).
f := Stdio open: 'generr.out' for: 'w'.
[ f isError ] ifTrue: [ System logNl: 'Cannot open generr.out'. thisProcess terminate. ].
c := s size - 1.
0 to: c do: [:i |
self printString: (s at: i) index: i on: f.
].
f puts: S'static stix_ooch_t* errstr[] =\n{\n'.
0 to: c do: [:i |
((i rem: 8) = 0) ifTrue: [ f putc: C'\t' ].
f puts: S'e'.
f puts: (i asString).
(i = c) ifFalse: [f puts: S',' ].
(((i + 1) rem: 8) = 0) ifTrue: [ f putc: C'\n' ] ifFalse: [ f putc: C' ' ].
].
(((c + 1) rem: 8) = 0) ifFalse: [ f putc: C'\n' ].
f puts: S'};\n'.
f close.
}
#method(#class) printString: s index: index on: f
{
| c |
c := s size - 1.
f puts: 'static stix_ooch_t e'.
f puts: index asString.
f puts: '[] = {'.
0 to: c do: [:i |
f putc: $'.
f putc: (s at: i).
f putc: $'.
(i = c) ifFalse: [f putc: $, ].
].
f puts: S',\'\\0\'};\n'.
}
}

View File

@ -125,17 +125,16 @@
##v1 := Stdio2 open: '/tmp/1.txt' for: 'w+'. ##v1 := Stdio2 open: '/tmp/1.txt' for: 'w+'.
v1 := Stdio2 new open: '/tmp/1.txt' for: 'w+'. v1 := Stdio2 new open: '/tmp/x/1.txt' for: 'w+'.
(v1 isError) (v1 isError)
ifTrue: [ ifTrue: [
System logNl: ('Error in opening a file....'). System logNl: ('Error in opening a file....' & v1 asString).
] ]
ifFalse: [ ifFalse: [
## v1 puts: 'hello'. ## v1 puts: 'hello'.
v1 puts ('hello', 'world', 'good', C'\n', C'\t', 'under my umbrella.', C'\n'). v1 puts ('hello', 'world', 'good', C'\n', C'\t', 'under my umbrella.', C'\n').
v1 close. v1 close.
(*v1 format(10, 20) isNil ifFalse: [ (*v1 format(10, 20) isNil ifFalse: [
'Beautiful life' dump. 'Beautiful life' dump.
].*) ].*)

View File

@ -42,6 +42,7 @@ libstix_la_SOURCES = \
debug.c \ debug.c \
decode.c \ decode.c \
dic.c \ dic.c \
err.c \
exec.c \ exec.c \
logfmtv.h \ logfmtv.h \
logfmt.c \ logfmt.c \

View File

@ -136,10 +136,10 @@ am__DEPENDENCIES_2 = $(am__DEPENDENCIES_1) $(am__DEPENDENCIES_1)
libstix_la_DEPENDENCIES = $(am__DEPENDENCIES_2) libstix_la_DEPENDENCIES = $(am__DEPENDENCIES_2)
am_libstix_la_OBJECTS = libstix_la-bigint.lo libstix_la-comp.lo \ am_libstix_la_OBJECTS = libstix_la-bigint.lo libstix_la-comp.lo \
libstix_la-debug.lo libstix_la-decode.lo libstix_la-dic.lo \ libstix_la-debug.lo libstix_la-decode.lo libstix_la-dic.lo \
libstix_la-exec.lo libstix_la-logfmt.lo libstix_la-gc.lo \ libstix_la-err.lo libstix_la-exec.lo libstix_la-logfmt.lo \
libstix_la-heap.lo libstix_la-obj.lo libstix_la-proc.lo \ libstix_la-gc.lo libstix_la-heap.lo libstix_la-obj.lo \
libstix_la-rbt.lo libstix_la-stix.lo libstix_la-sym.lo \ libstix_la-proc.lo libstix_la-rbt.lo libstix_la-stix.lo \
libstix_la-utf8.lo libstix_la-utl.lo libstix_la-sym.lo libstix_la-utf8.lo libstix_la-utl.lo
libstix_la_OBJECTS = $(am_libstix_la_OBJECTS) libstix_la_OBJECTS = $(am_libstix_la_OBJECTS)
AM_V_lt = $(am__v_lt_@AM_V@) AM_V_lt = $(am__v_lt_@AM_V@)
am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@) am__v_lt_ = $(am__v_lt_@AM_DEFAULT_V@)
@ -409,6 +409,7 @@ libstix_la_SOURCES = \
debug.c \ debug.c \
decode.c \ decode.c \
dic.c \ dic.c \
err.c \
exec.c \ exec.c \
logfmtv.h \ logfmtv.h \
logfmt.c \ logfmt.c \
@ -582,6 +583,7 @@ distclean-compile:
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-debug.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-debug.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-decode.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-decode.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-dic.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-dic.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-err.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-exec.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-exec.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-gc.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-gc.Plo@am__quote@
@AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-heap.Plo@am__quote@ @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/libstix_la-heap.Plo@am__quote@
@ -654,6 +656,13 @@ libstix_la-dic.lo: dic.c
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@ @AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libstix_la-dic.lo `test -f 'dic.c' || echo '$(srcdir)/'`dic.c @am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libstix_la-dic.lo `test -f 'dic.c' || echo '$(srcdir)/'`dic.c
libstix_la-err.lo: err.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libstix_la-err.lo -MD -MP -MF $(DEPDIR)/libstix_la-err.Tpo -c -o libstix_la-err.lo `test -f 'err.c' || echo '$(srcdir)/'`err.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libstix_la-err.Tpo $(DEPDIR)/libstix_la-err.Plo
@AMDEP_TRUE@@am__fastdepCC_FALSE@ $(AM_V_CC)source='err.c' object='libstix_la-err.lo' libtool=yes @AMDEPBACKSLASH@
@AMDEP_TRUE@@am__fastdepCC_FALSE@ DEPDIR=$(DEPDIR) $(CCDEPMODE) $(depcomp) @AMDEPBACKSLASH@
@am__fastdepCC_FALSE@ $(AM_V_CC@am__nodep@)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -c -o libstix_la-err.lo `test -f 'err.c' || echo '$(srcdir)/'`err.c
libstix_la-exec.lo: exec.c libstix_la-exec.lo: exec.c
@am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libstix_la-exec.lo -MD -MP -MF $(DEPDIR)/libstix_la-exec.Tpo -c -o libstix_la-exec.lo `test -f 'exec.c' || echo '$(srcdir)/'`exec.c @am__fastdepCC_TRUE@ $(AM_V_CC)$(LIBTOOL) $(AM_V_lt) --tag=CC $(AM_LIBTOOLFLAGS) $(LIBTOOLFLAGS) --mode=compile $(CC) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) $(libstix_la_CPPFLAGS) $(CPPFLAGS) $(AM_CFLAGS) $(CFLAGS) -MT libstix_la-exec.lo -MD -MP -MF $(DEPDIR)/libstix_la-exec.Tpo -c -o libstix_la-exec.lo `test -f 'exec.c' || echo '$(srcdir)/'`exec.c
@am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libstix_la-exec.Tpo $(DEPDIR)/libstix_la-exec.Plo @am__fastdepCC_TRUE@ $(AM_V_at)$(am__mv) $(DEPDIR)/libstix_la-exec.Tpo $(DEPDIR)/libstix_la-exec.Plo

View File

@ -81,6 +81,7 @@ static struct voca_t
{ 4, { '#','d','c','l' } }, { 4, { '#','d','c','l' } },
{ 8, { '#','d','e','c','l','a','r','e' } }, { 8, { '#','d','e','c','l','a','r','e' } },
{ 6, { 'e','n','s','u','r','e', } }, { 6, { 'e','n','s','u','r','e', } },
{ 5, { 'e','r','r','o','r' } },
{ 9, { 'e','x','c','e','p','t','i','o','n' } }, { 9, { 'e','x','c','e','p','t','i','o','n' } },
{ 7, { '#','e','x','t','e','n','d' } }, { 7, { '#','e','x','t','e','n','d' } },
{ 5, { 'f','a','l','s','e' } }, { 5, { 'f','a','l','s','e' } },
@ -118,6 +119,7 @@ enum voca_id_t
VOCA_DCL, VOCA_DCL,
VOCA_DECLARE, VOCA_DECLARE,
VOCA_ENSURE, VOCA_ENSURE,
VOCA_ERROR,
VOCA_EXCEPTION, VOCA_EXCEPTION,
VOCA_EXTEND, VOCA_EXTEND,
VOCA_FALSE, VOCA_FALSE,
@ -264,8 +266,9 @@ static int is_reserved_word (const stix_oocs_t* ucs)
VOCA_NIL, VOCA_NIL,
VOCA_TRUE, VOCA_TRUE,
VOCA_FALSE, VOCA_FALSE,
VOCA_ERROR,
VOCA_THIS_CONTEXT, VOCA_THIS_CONTEXT,
VOCA_THIS_PROCESS VOCA_THIS_PROCESS,
}; };
int i; int i;

165
stix/lib/err.c Normal file
View File

@ -0,0 +1,165 @@
/*
* $Id$
*
Copyright (c) 2014-2016 Chung, Hyung-Hwan. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "stix-prv.h"
/* BEGIN: GENERATED WITH generr.st */
static stix_ooch_t e0[] = {'n','o',' ','e','r','r','o','r','\0'};
static stix_ooch_t e1[] = {'g','e','n','e','r','i','c',' ','e','r','r','o','r','\0'};
static stix_ooch_t e2[] = {'n','o','t',' ','i','m','p','l','e','m','e','n','t','e','d','\0'};
static stix_ooch_t e3[] = {'s','u','b','s','y','s','t','e','m',' ','e','r','r','o','r','\0'};
static stix_ooch_t e4[] = {'i','n','t','e','r','n','a','l',' ','e','r','r','o','r',' ','t','h','a','t',' ','s','h','o','u','l','d',' ','n','e','v','e','r',' ','h','a','v','e',' ','h','a','p','p','e','n','e','d','\0'};
static stix_ooch_t e5[] = {'i','n','s','u','f','f','i','c','i','e','n','t',' ','s','y','s','t','e','m',' ','m','e','m','o','r','y','\0'};
static stix_ooch_t e6[] = {'i','n','s','u','f','f','i','c','i','e','n','t',' ','o','b','j','e','c','t',' ','m','e','m','o','r','y','\0'};
static stix_ooch_t e7[] = {'i','n','v','a','l','i','d',' ','p','a','r','a','m','e','t','e','r',' ','o','r',' ','a','r','g','u','m','e','n','t','\0'};
static stix_ooch_t e8[] = {'d','a','t','a',' ','n','o','t',' ','f','o','u','n','d','\0'};
static stix_ooch_t e9[] = {'e','x','i','s','t','i','n','g','/','d','u','p','l','i','c','a','t','e',' ','d','a','t','a','\0'};
static stix_ooch_t e10[] = {'b','u','s','y','\0'};
static stix_ooch_t e11[] = {'a','c','c','e','s','s',' ','d','e','n','i','e','d','\0'};
static stix_ooch_t e12[] = {'o','p','e','r','a','t','i','o','n',' ','n','o','t',' ','p','e','r','m','i','t','t','e','d','\0'};
static stix_ooch_t e13[] = {'n','o','t',' ','a',' ','d','i','r','e','c','t','o','r','y','\0'};
static stix_ooch_t e14[] = {'i','n','t','e','r','r','u','p','t','e','d','\0'};
static stix_ooch_t e15[] = {'p','i','p','e',' ','e','r','r','o','r','\0'};
static stix_ooch_t e16[] = {'r','e','s','o','u','r','c','e',' ','t','e','m','p','o','r','a','r','i','l','y',' ','u','n','a','v','a','i','l','a','b','l','e','\0'};
static stix_ooch_t e17[] = {'d','a','t','a',' ','t','o','o',' ','l','a','r','g','e','\0'};
static stix_ooch_t e18[] = {'m','e','s','s','a','g','e',' ','s','e','n','d','i','n','g',' ','e','r','r','o','r','\0'};
static stix_ooch_t e19[] = {'r','a','n','g','e',' ','e','r','r','o','r','\0'};
static stix_ooch_t e20[] = {'b','y','t','e','-','c','o','d','e',' ','f','u','l','l','\0'};
static stix_ooch_t e21[] = {'d','i','c','t','i','o','n','a','r','y',' ','f','u','l','l','\0'};
static stix_ooch_t e22[] = {'p','r','o','c','e','s','s','o','r',' ','f','u','l','l','\0'};
static stix_ooch_t e23[] = {'s','e','m','a','p','h','o','r','e',' ','h','e','a','p',' ','f','u','l','l','\0'};
static stix_ooch_t e24[] = {'s','e','m','a','p','h','o','r','e',' ','l','i','s','t',' ','f','u','l','l','\0'};
static stix_ooch_t e25[] = {'d','i','v','i','d','e',' ','b','y',' ','z','e','r','o','\0'};
static stix_ooch_t e26[] = {'I','/','O',' ','e','r','r','o','r','\0'};
static stix_ooch_t e27[] = {'e','n','c','o','d','i','n','g',' ','c','o','n','v','e','r','s','i','o','n',' ','e','r','r','o','r','\0'};
static stix_ooch_t* errstr[] =
{
e0, e1, e2, e3, e4, e5, e6, e7,
e8, e9, e10, e11, e12, e13, e14, e15,
e16, e17, e18, e19, e20, e21, e22, e23,
e24, e25, e26, e27
};
/* END: GENERATED WITH generr.st */
/* --------------------------------------------------------------------------
* ERROR NUMBER TO STRING CONVERSION
* -------------------------------------------------------------------------- */
const stix_ooch_t* stix_errnumtoerrstr (stix_errnum_t errnum)
{
static stix_ooch_t e_unknown[] = {'u','n','k','n','o','w','n',' ','e','r','r','o','r','\0'};
return (errnum >= 0 && errnum < STIX_COUNTOF(errstr))? errstr[errnum]: e_unknown;
}
/* --------------------------------------------------------------------------
* SYSTEM DEPENDENT FUNCTIONS
* -------------------------------------------------------------------------- */
#if defined(_WIN32)
# include <windows.h>
#elif defined(__OS2__)
# define INCL_DOSPROCESS
# define INCL_DOSFILEMGR
# include <os2.h>
#elif defined(__DOS__)
# include <dos.h>
# include <dosfunc.h>
#elif defined(vms) || defined(__vms)
# define __NEW_STARLET 1
# include <starlet.h> /* (SYS$...) */
# include <ssdef.h> /* (SS$...) */
# include <lib$routines.h> /* (lib$...) */
#elif defined(macintosh)
# include <Process.h>
# include <Dialogs.h>
# include <TextUtils.h>
#else
# include <sys/types.h>
# include <unistd.h>
# include <signal.h>
# include <errno.h>
#endif
stix_errnum_t stix_syserrtoerrnum (int e)
{
switch (e)
{
case ENOMEM: return STIX_ESYSMEM;
case EINVAL: return STIX_EINVAL;
case EBUSY: return STIX_EBUSY;
case EACCES: return STIX_EACCES;
case EPERM: return STIX_EPERM;
case ENOTDIR: return STIX_ENOTDIR;
case ENOENT: return STIX_ENOENT;
case EEXIST: return STIX_EEXIST;
case EINTR: return STIX_EINTR;
case EPIPE: return STIX_EPIPE;
#if defined(EAGAIN) && defined(EWOULDBLOCK) && (EAGAIN != EWOULDBLOCK)
case EAGAIN:
case EWOULDBLOCK: return STIX_EAGAIN;
#elif defined(EAGAIN)
case EAGAIN: return STIX_EAGAIN;
#elif defined(EWOULDBLOCK)
case EWOULDBLOCK: return STIX_EAGAIN;
#endif
default: return STIX_ESYSERR;
}
}
/* --------------------------------------------------------------------------
* ASSERTION FAILURE HANDLER
* -------------------------------------------------------------------------- */
void stix_assertfailed (stix_t* stix, const stix_bch_t* expr, const stix_bch_t* file, stix_oow_t line)
{
stix_logbfmt (stix, STIX_LOG_DEBUG, "ASSERTION FAILURE: %s at %s:%zu\n", expr, file, line);
#if defined(_WIN32)
ExitProcess (249);
#elif defined(__OS2__)
DosExit (EXIT_PROCESS, 249);
#elif defined(__DOS__)
{
union REGS regs;
regs.h.ah = DOS_EXIT;
regs.h.al = 249;
intdos (&regs, &regs);
}
#elif defined(vms) || defined(__vms)
lib$stop (SS$_ABORT); /* use SS$_OPCCUS instead? */
/* this won't be reached since lib$stop() terminates the process */
sys$exit (SS$_ABORT); /* this condition code can be shown with
* 'show symbol $status' from the command-line. */
#elif defined(macintosh)
ExitToShell ();
#else
kill (getpid(), SIGABRT);
_exit (1);
#endif
}

View File

@ -2409,26 +2409,46 @@ static int pf_integer_inttostr (stix_t* stix, stix_ooi_t nargs)
return 1; return 1;
} }
static int pf_error_as_string (stix_t* stix, stix_ooi_t nargs) static int pf_error_as_integer (stix_t* stix, stix_ooi_t nargs)
{ {
stix_oop_t rcv, str; stix_oop_t rcv;
stix_ooi_t ec;
STIX_ASSERT (stix, nargs == 0); STIX_ASSERT (stix, nargs == 0);
rcv = STIX_STACK_GETRCV(stix, nargs); rcv = STIX_STACK_GETRCV(stix, nargs);
if (!STIX_OOP_IS_ERROR(rcv)) return 0; if (!STIX_OOP_IS_ERROR(rcv)) return 0;
//str = stix_makestring (stix, xxx, xxx); ec = STIX_OOP_TO_ERROR(rcv);
STIX_STACK_SETRET (stix, nargs, str); STIX_ASSERT (stix, STIX_IN_SMOOI_RANGE(ec));
STIX_STACK_SETRET (stix, nargs, STIX_SMOOI_TO_OOP(ec));
return 1; return 1;
} }
static int pf_error_text (stix_t* stix, stix_ooi_t nargs) static int pf_error_as_string (stix_t* stix, stix_ooi_t nargs)
{ {
stix_oop_t rcv, ss;
stix_ooi_t ec;
const stix_ooch_t* s;
return -1; STIX_ASSERT (stix, nargs == 0);
rcv = STIX_STACK_GETRCV(stix, nargs);
if (!STIX_OOP_IS_ERROR(rcv)) return 0;
ec = STIX_OOP_TO_ERROR(rcv);
STIX_ASSERT (stix, STIX_IN_SMOOI_RANGE(ec));
/* TODO: error string will be mostly the same.. do i really have to call makestring every time? */
s = stix_errnumtoerrstr (ec);
ss = stix_makestring (stix, s, stix_countoocstr(s));
if (!ss) return -1;
STIX_STACK_SETRET (stix, nargs, ss);
return 1;
} }
static int pf_ffi_open (stix_t* stix, stix_ooi_t nargs) static int pf_ffi_open (stix_t* stix, stix_ooi_t nargs)
{ {
stix_oop_t rcv, arg; stix_oop_t rcv, arg;
@ -2586,7 +2606,7 @@ STIX_DEBUG2 (stix, "CALL MODE 222 ERROR %d %d\n", dcGetError (dc), DC_ERROR_UNSU
stix_bch_t bcs[1024]; stix_bch_t bcs[1024];
ucslen = STIX_OBJ_GET_SIZE(arr->slot[i - 2]); ucslen = STIX_OBJ_GET_SIZE(arr->slot[i - 2]);
stix_oocstobcs (stix, ((stix_oop_char_t)arr->slot[i - 2])->slot, &ucslen, bcs, &bcslen); /* proper string conversion */ stix_convootobchars (stix, ((stix_oop_char_t)arr->slot[i - 2])->slot, &ucslen, bcs, &bcslen); /* proper string conversion */
bcs[bcslen] = '\0'; bcs[bcslen] = '\0';
dcArgPointer (dc, bcs); dcArgPointer (dc, bcs);
@ -2644,7 +2664,7 @@ STIX_DEBUG2 (stix, "CALL ERROR %d %d\n", dcGetError (dc), DC_ERROR_UNSUPPORTED_M
char* r = dcCallPointer (dc, f); char* r = dcCallPointer (dc, f);
bcslen = strlen(r); bcslen = strlen(r);
stix_bcstooocs (stix, r, &bcslen, ucs, &ucslen); /* proper string conversion */ stix_convbtooochars (stix, r, &bcslen, ucs, &ucslen); /* proper string conversion */
s = stix_makestring(stix, ucs, ucslen) s = stix_makestring(stix, ucs, ucslen)
if (!s) if (!s)
@ -2780,8 +2800,9 @@ static pf_t pftab[] =
{ 1, 1, pf_integer_ge, "_integer_ge" }, { 1, 1, pf_integer_ge, "_integer_ge" },
{ 1, 1, pf_integer_inttostr, "_integer_inttostr" }, { 1, 1, pf_integer_inttostr, "_integer_inttostr" },
{ 0, 0, pf_error_as_integer, "_error_as_integer" },
{ 0, 0, pf_error_as_string, "_error_as_string" }, { 0, 0, pf_error_as_string, "_error_as_string" },
{ 0, 0, pf_error_text, "_error_text" },
{ 1, 1, pf_ffi_open, "_ffi_open" }, { 1, 1, pf_ffi_open, "_ffi_open" },
{ 1, 1, pf_ffi_close, "_ffi_close" }, { 1, 1, pf_ffi_close, "_ffi_close" },

View File

@ -425,7 +425,7 @@ reswitch:
/* get the length */ /* get the length */
for (bslen = 0; bsp[bslen]; bslen++); for (bslen = 0; bsp[bslen]; bslen++);
if (stix_bcstooocs (stix, bsp, &bslen, STIX_NULL, &slen) <= -1) if (stix_convbtooochars (stix, bsp, &bslen, STIX_NULL, &slen) <= -1)
{ {
/* conversion error */ /* conversion error */
stix->errnum = STIX_EECERR; stix->errnum = STIX_EECERR;
@ -450,7 +450,7 @@ reswitch:
conv_len = STIX_COUNTOF(conv_buf); conv_len = STIX_COUNTOF(conv_buf);
/* this must not fail since the dry-run above was successful */ /* this must not fail since the dry-run above was successful */
stix_bcstooocs (stix, &bsp[tot_len], &src_len, conv_buf, &conv_len); stix_convbtooochars (stix, &bsp[tot_len], &src_len, conv_buf, &conv_len);
tot_len += src_len; tot_len += src_len;
if (conv_len > n) conv_len = n; if (conv_len > n) conv_len = n;

View File

@ -142,9 +142,9 @@ static STIX_INLINE stix_ooi_t open_input (stix_t* stix, stix_ioarg_t* arg)
/* includee */ /* includee */
stix_bch_t bcs[1024]; /* TODO: right buffer size */ stix_bch_t bcs[1024]; /* TODO: right buffer size */
stix_oow_t bcslen = STIX_COUNTOF(bcs); stix_oow_t bcslen = STIX_COUNTOF(bcs);
stix_oow_t ucslen = ~(stix_oow_t)0; stix_oow_t ucslen;
if (stix_oocstobcs (stix, arg->name, &ucslen, bcs, &bcslen) <= -1) if (stix_convootobcstr (stix, arg->name, &ucslen, bcs, &bcslen) <= -1)
{ {
stix_seterrnum (stix, STIX_EECERR); stix_seterrnum (stix, STIX_EECERR);
return -1; return -1;
@ -231,8 +231,8 @@ static STIX_INLINE stix_ooi_t read_input (stix_t* stix, stix_ioarg_t* arg)
bcslen = bb->len; bcslen = bb->len;
ucslen = STIX_COUNTOF(arg->buf); ucslen = STIX_COUNTOF(arg->buf);
x = stix_bcstooocs (stix, bb->buf, &bcslen, arg->buf, &ucslen); x = stix_convbtooochars (stix, bb->buf, &bcslen, arg->buf, &ucslen);
x = stix_bcstooocs (stix, bb->buf, &bcslen, arg->buf, &ucslen); x = stix_convbtooochars (stix, bb->buf, &bcslen, arg->buf, &ucslen);
if (x <= -1 && ucslen <= 0) if (x <= -1 && ucslen <= 0)
{ {
stix_seterrnum (stix, STIX_EECERR); stix_seterrnum (stix, STIX_EECERR);
@ -287,9 +287,8 @@ static void* dl_open (stix_t* stix, const stix_ooch_t* name)
len = stix_copybcstr (buf, STIX_COUNTOF(buf), STIX_DEFAULT_MODPREFIX); len = stix_copybcstr (buf, STIX_COUNTOF(buf), STIX_DEFAULT_MODPREFIX);
/* TODO: proper error checking and overflow checking */ /* TODO: proper error checking and overflow checking */
ucslen = ~(stix_oow_t)0;
bcslen = STIX_COUNTOF(buf) - len; bcslen = STIX_COUNTOF(buf) - len;
stix_oocstobcs (stix, name, &ucslen, &buf[len], &bcslen); stix_convootobcstr (stix, name, &ucslen, &buf[len], &bcslen);
stix_copybcstr (&buf[bcslen + len], STIX_COUNTOF(buf) - bcslen - len, STIX_DEFAULT_MODPOSTFIX); stix_copybcstr (&buf[bcslen + len], STIX_COUNTOF(buf) - bcslen - len, STIX_DEFAULT_MODPOSTFIX);
@ -339,9 +338,8 @@ static void* dl_getsym (stix_t* stix, void* handle, const stix_ooch_t* name)
buf[0] = '_'; buf[0] = '_';
ucslen = ~(stix_oow_t)0;
bcslen = STIX_COUNTOF(buf) - 2; bcslen = STIX_COUNTOF(buf) - 2;
stix_oocstobcs (stix, name, &ucslen, &buf[1], &bcslen); /* TODO: error check */ stix_convootobcstr (stix, name, &ucslen, &buf[1], &bcslen); /* TODO: error check */
symname = &buf[1]; symname = &buf[1];
sym = lt_dlsym (handle, symname); sym = lt_dlsym (handle, symname);
if (!sym) if (!sym)
@ -436,7 +434,7 @@ if (mask & STIX_LOG_GC) return; /* don't show gc logs */
ucslen = len; ucslen = len;
bcslen = STIX_COUNTOF(buf); bcslen = STIX_COUNTOF(buf);
n = stix_oocstobcs (stix, &msg[msgidx], &ucslen, buf, &bcslen); n = stix_convootobchars (stix, &msg[msgidx], &ucslen, buf, &bcslen);
if (n == 0 || n == -2) if (n == 0 || n == -2)
{ {
/* n = 0: /* n = 0:
@ -731,8 +729,7 @@ int main (int argc, char* argv[])
if (synerr.loc.file) if (synerr.loc.file)
{ {
bcslen = STIX_COUNTOF(bcs); bcslen = STIX_COUNTOF(bcs);
ucslen = ~(stix_oow_t)0; if (stix_convootobcstr (stix, synerr.loc.file, &ucslen, bcs, &bcslen) >= 0)
if (stix_oocstobcs (stix, synerr.loc.file, &ucslen, bcs, &bcslen) >= 0)
{ {
printf ("%.*s ", (int)bcslen, bcs); printf ("%.*s ", (int)bcslen, bcs);
} }
@ -751,7 +748,7 @@ int main (int argc, char* argv[])
bcslen = STIX_COUNTOF(bcs); bcslen = STIX_COUNTOF(bcs);
ucslen = synerr.tgt.len; ucslen = synerr.tgt.len;
if (stix_oocstobcs (stix, synerr.tgt.ptr, &ucslen, bcs, &bcslen) >= 0) if (stix_convootobchars (stix, synerr.tgt.ptr, &ucslen, bcs, &bcslen) >= 0)
{ {
printf (" [%.*s]", (int)bcslen, bcs); printf (" [%.*s]", (int)bcslen, bcs);
} }

View File

@ -513,7 +513,7 @@ STIX_EXPORT stix_rbt_pair_t* stix_rbt_update (
STIX_EXPORT stix_rbt_pair_t* stix_rbt_cbsert ( STIX_EXPORT stix_rbt_pair_t* stix_rbt_cbsert (
stix_rbt_t* rbt, /**< red-black tree */ stix_rbt_t* rbt, /**< red-black tree */
void* kptr, /**< key pointer */ void* kptr, /**< key pointer */
stix_oow_t klen, /**< key length */ stix_oow_t klen, /**< key length */
stix_rbt_cbserter_t cbserter, /**< callback function */ stix_rbt_cbserter_t cbserter, /**< callback function */
void* ctx /**< callback context */ void* ctx /**< callback context */
); );
@ -525,7 +525,7 @@ STIX_EXPORT stix_rbt_pair_t* stix_rbt_cbsert (
STIX_EXPORT int stix_rbt_delete ( STIX_EXPORT int stix_rbt_delete (
stix_rbt_t* rbt, /**< red-black tree */ stix_rbt_t* rbt, /**< red-black tree */
const void* kptr, /**< key pointer */ const void* kptr, /**< key pointer */
stix_oow_t klen /**< key size */ stix_oow_t klen /**< key size */
); );
/** /**

View File

@ -39,7 +39,7 @@ extern "C" {
# define stix_compoocbcstr(str1,str2) stix_compucbcstr(str1,str2) # define stix_compoocbcstr(str1,str2) stix_compucbcstr(str1,str2)
# define stix_compoocstr(str1,str2) stix_compucstr(str1,str2) # define stix_compoocstr(str1,str2) stix_compucstr(str1,str2)
# define stix_copyoochars(dst,src,len) stix_copyuchars(dst,src,len) # define stix_copyoochars(dst,src,len) stix_copyuchars(dst,src,len)
# define stix_copybctooochars(dst,src,len) stix_copybctouchars(dst,src,len) # define stix_copybctooochars(dst,src,len) stix_copybtouchars(dst,src,len)
# define stix_copyoocstr(dst,len,src) stix_copyucstr(dst,len,src) # define stix_copyoocstr(dst,len,src) stix_copyucstr(dst,len,src)
# define stix_findoochar(ptr,len,c) stix_finduchar(ptr,len,c) # define stix_findoochar(ptr,len,c) stix_finduchar(ptr,len,c)
# define stix_rfindoochar(ptr,len,c) stix_rfinduchar(ptr,len,c) # define stix_rfindoochar(ptr,len,c) stix_rfinduchar(ptr,len,c)
@ -122,7 +122,7 @@ STIX_EXPORT void stix_copybchars (
stix_oow_t len stix_oow_t len
); );
STIX_EXPORT void stix_copybctouchars ( STIX_EXPORT void stix_copybtouchars (
stix_uch_t* dst, stix_uch_t* dst,
const stix_bch_t* src, const stix_bch_t* src,
stix_oow_t len stix_oow_t len
@ -190,7 +190,7 @@ STIX_EXPORT stix_cmgr_t* stix_getutf8cmgr (
); );
/** /**
* The stix_ucstoutf8() function converts a unicode character string \a ucs * The stix_convutoutf8chars() function converts a unicode character string \a ucs
* to a UTF8 string and writes it into the buffer pointed to by \a bcs, but * to a UTF8 string and writes it into the buffer pointed to by \a bcs, but
* not more than \a bcslen bytes including the terminating null. * not more than \a bcslen bytes including the terminating null.
* *
@ -211,14 +211,14 @@ STIX_EXPORT stix_cmgr_t* stix_getutf8cmgr (
* stix_bch_t bcs[10]; * stix_bch_t bcs[10];
* stix_oow_t ucslen = 5; * stix_oow_t ucslen = 5;
* stix_oow_t bcslen = STIX_COUNTOF(bcs); * stix_oow_t bcslen = STIX_COUNTOF(bcs);
* n = stix_ucstoutf8 (ucs, &ucslen, bcs, &bcslen); * n = stix_convutoutf8chars (ucs, &ucslen, bcs, &bcslen);
* if (n <= -1) * if (n <= -1)
* { * {
* // conversion error * // conversion error
* } * }
* \endcode * \endcode
*/ */
STIX_EXPORT int stix_ucstoutf8 ( STIX_EXPORT int stix_convutoutf8chars (
const stix_uch_t* ucs, const stix_uch_t* ucs,
stix_oow_t* ucslen, stix_oow_t* ucslen,
stix_bch_t* bcs, stix_bch_t* bcs,
@ -226,7 +226,7 @@ STIX_EXPORT int stix_ucstoutf8 (
); );
/** /**
* The stix_utf8toucs() function converts a UTF8 string to a uncide string. * The stix_convutf8touchars() function converts a UTF8 string to a uncide string.
* *
* It never returns -2 if \a ucs is #STIX_NULL. * It never returns -2 if \a ucs is #STIX_NULL.
* *
@ -236,13 +236,9 @@ STIX_EXPORT int stix_ucstoutf8 (
* stix_oow_t ucslen = STIX_COUNTOF(buf), n; * stix_oow_t ucslen = STIX_COUNTOF(buf), n;
* stix_oow_t bcslen = 11; * stix_oow_t bcslen = 11;
* int n; * int n;
* n = stix_utf8toucs (bcs, &bcslen, ucs, &ucslen); * n = stix_convutf8touchars (bcs, &bcslen, ucs, &ucslen);
* if (n <= -1) { invalid/incomplenete sequence or buffer to small } * if (n <= -1) { invalid/incomplenete sequence or buffer to small }
* \endcode * \endcode
*
* For a null-terminated string, you can specify ~(stix_oow_t)0 in
* \a bcslen. The destination buffer \a ucs also must be large enough to
* store a terminating null. Otherwise, -2 is returned.
* *
* The resulting \a ucslen can still be greater than 0 even if the return * The resulting \a ucslen can still be greater than 0 even if the return
* value is negative. The value indiates the number of characters converted * value is negative. The value indiates the number of characters converted
@ -253,7 +249,7 @@ STIX_EXPORT int stix_ucstoutf8 (
* -2 if the wide-character string buffer is too small. * -2 if the wide-character string buffer is too small.
* -3 if \a bcs is not a complete sequence. * -3 if \a bcs is not a complete sequence.
*/ */
STIX_EXPORT int stix_utf8toucs ( STIX_EXPORT int stix_convutf8touchars (
const stix_bch_t* bcs, const stix_bch_t* bcs,
stix_oow_t* bcslen, stix_oow_t* bcslen,
stix_uch_t* ucs, stix_uch_t* ucs,
@ -261,6 +257,20 @@ STIX_EXPORT int stix_utf8toucs (
); );
STIX_EXPORT int stix_convutoutf8cstr (
const stix_uch_t* ucs,
stix_oow_t* ucslen,
stix_bch_t* bcs,
stix_oow_t* bcslen
);
STIX_EXPORT int stix_convutf8toucstr (
const stix_bch_t* bcs,
stix_oow_t* bcslen,
stix_uch_t* ucs,
stix_oow_t* ucslen
);
/* ========================================================================= */ /* ========================================================================= */
/* utf8.c */ /* utf8.c */
/* ========================================================================= */ /* ========================================================================= */

View File

@ -226,6 +226,11 @@ void stix_seterrnum (stix_t* stix, stix_errnum_t errnum)
stix->errnum = errnum; stix->errnum = errnum;
} }
const stix_ooch_t* stix_geterrstr (stix_t* stix)
{
return stix_errnumtoerrstr (stix->errnum);
}
int stix_setoption (stix_t* stix, stix_option_t id, const void* value) int stix_setoption (stix_t* stix, stix_option_t id, const void* value)
{ {
switch (id) switch (id)
@ -390,7 +395,9 @@ stix_mod_data_t* stix_openmod (stix_t* stix, const stix_ooch_t* name, stix_oow_t
*/ */
stix_ooch_t buf[MOD_PREFIX_LEN + STIX_MOD_NAME_LEN_MAX + 1 + 1]; stix_ooch_t buf[MOD_PREFIX_LEN + STIX_MOD_NAME_LEN_MAX + 1 + 1];
/* the terminating null isn't needed in buf here */ /* copy instead of encoding conversion. MOD_PREFIX must not
* include a character that requires encoding conversion.
* note the terminating null isn't needed in buf here. */
stix_copybctooochars (buf, MOD_PREFIX, MOD_PREFIX_LEN); stix_copybctooochars (buf, MOD_PREFIX, MOD_PREFIX_LEN);
if (namelen > STIX_COUNTOF(buf) - (MOD_PREFIX_LEN + 1 + 1)) if (namelen > STIX_COUNTOF(buf) - (MOD_PREFIX_LEN + 1 + 1))
@ -744,67 +751,3 @@ oops:
stix_poptmps (stix, tmp_count); stix_poptmps (stix, tmp_count);
return -1; return -1;
} }
/* --------------------------------------------------------------------------
* SYSTEM DEPENDENT FUNCTIONS - TODO: MOVE THESE ELSE WHERE..........
* -------------------------------------------------------------------------- */
#include <errno.h>
#include <unistd.h>
#include <signal.h>
stix_errnum_t stix_syserrtoerrnum (int e)
{
switch (e)
{
case ENOMEM: return STIX_ESYSMEM;
case EINVAL: return STIX_EINVAL;
case EBUSY: return STIX_EBUSY;
case EACCES: return STIX_EACCES;
case EPERM: return STIX_EPERM;
case ENOTDIR: return STIX_ENOTDIR;
case ENOENT: return STIX_ENOENT;
case EEXIST: return STIX_EEXIST;
case EINTR: return STIX_EINTR;
case EPIPE: return STIX_EPIPE;
#if defined(EAGAIN) && defined(EWOULDBLOCK) && (EAGAIN != EWOULDBLOCK)
case EAGAIN:
case EWOULDBLOCK: return STIX_EAGAIN;
#elif defined(EAGAIN)
case EAGAIN: return STIX_EAGAIN;
#elif defined(EWOULDBLOCK)
case EWOULDBLOCK: return STIX_EAGAIN;
#endif
default: return STIX_ESYSERR;
}
}
void stix_assertfailed (stix_t* stix, const stix_bch_t* expr, const stix_bch_t* file, stix_oow_t line)
{
stix_logbfmt (stix, STIX_LOG_DEBUG, "ASSERTION FAILURE: %s at %s:%zu\n", expr, file, line);
#if defined(_WIN32)
ExitProcess (249);
#elif defined(__OS2__)
DosExit (EXIT_PROCESS, 249);
#elif defined(__DOS__)
{
union REGS regs;
regs.h.ah = DOS_EXIT;
regs.h.al = 249;
intdos (&regs, &regs);
}
#elif defined(vms) || defined(__vms)
lib$stop (SS$_ABORT); /* use SS$_OPCCUS instead? */
/* this won't be reached since lib$stop() terminates the process */
sys$exit (SS$_ABORT); /* this condition code can be shown with
* 'show symbol $status' from the command-line. */
#elif defined(macintosh)
ExitToShell ();
#else
kill (getpid(), SIGABRT);
_exit (1);
#endif
}

View File

@ -44,16 +44,18 @@
*/ */
enum stix_errnum_t enum stix_errnum_t
{ {
STIX_ENOERR, /**< no error */ STIX_ENOERR, /**< no error */
STIX_EOTHER, /**< other error */ STIX_EGENERIC, /**< generic error */
STIX_ENOIMPL, /**< not implemented */
STIX_ESYSERR, /**< subsystem error */
STIX_EINTERN, /**< internal error */
STIX_ESYSMEM, /**< insufficient system memory */
STIX_EOOMEM, /**< insufficient object memory */
STIX_EINVAL, /**< invalid parameter or data */ STIX_ENOIMPL, /**< not implemented */
STIX_EEXIST, /**< existing/duplicate data */ STIX_ESYSERR, /**< subsystem error */
STIX_EINTERN, /**< internal error */
STIX_ESYSMEM, /**< insufficient system memory */
STIX_EOOMEM, /**< insufficient object memory */
STIX_EINVAL, /**< invalid parameter or data */
STIX_ENOENT, /**< data not found */
STIX_EEXIST, /**< existing/duplicate data */
STIX_EBUSY, STIX_EBUSY,
STIX_EACCES, STIX_EACCES,
STIX_EPERM, STIX_EPERM,
@ -62,21 +64,20 @@ enum stix_errnum_t
STIX_EPIPE, STIX_EPIPE,
STIX_EAGAIN, STIX_EAGAIN,
STIX_ETOOBIG, /**< data too large */ STIX_ETOOBIG, /**< data too large */
STIX_EMSGSND, /**< message sending error. even doesNotUnderstand: is not found */ STIX_EMSGSND, /**< message sending error. even doesNotUnderstand: is not found */
STIX_ERANGE, /**< range error. overflow and underflow */ STIX_ERANGE, /**< range error. overflow and underflow */
STIX_ENOENT, /**< no matching entry */ STIX_EBCFULL, /**< byte-code full */
STIX_EBCFULL, /**< byte-code full */ STIX_EDFULL, /**< dictionary full */
STIX_EDFULL, /**< dictionary full */ STIX_EPFULL, /**< processor full */
STIX_EPFULL, /**< processor full */ STIX_ESHFULL, /**< semaphore heap full */
STIX_ESHFULL, /**< semaphore heap full */ STIX_ESLFULL, /**< semaphore list full */
STIX_ESLFULL, /**< semaphore list full */ STIX_EDIVBY0, /**< divide by zero */
STIX_EDIVBY0, /**< divide by zero */ STIX_EIOERR, /**< I/O error */
STIX_EIOERR, /**< I/O error */ STIX_EECERR, /**< encoding conversion error */
STIX_EECERR, /**< encoding conversion error */
#if defined(STIX_INCLUDE_COMPILER) #if defined(STIX_INCLUDE_COMPILER)
STIX_ESYNTAX /** < syntax error */ STIX_ESYNTAX /** < syntax error */
#endif #endif
}; };
typedef enum stix_errnum_t stix_errnum_t; typedef enum stix_errnum_t stix_errnum_t;
@ -962,9 +963,11 @@ struct stix_t
/* you can't access arguments and receiver after this macro. /* you can't access arguments and receiver after this macro.
* also you must not call this macro more than once */ * also you must not call this macro more than once */
#define STIX_STACK_SETRET(stix,nargs,retv) (STIX_STACK_POPS(stix, nargs), STIX_STACK_SETTOP(stix, retv)) #define STIX_STACK_SETRET(stix,nargs,retv) (STIX_STACK_POPS(stix, nargs), STIX_STACK_SETTOP(stix, (retv)))
#define STIX_STACK_SETRETTORCV(stix,nargs) (STIX_STACK_POPS(stix, nargs)) #define STIX_STACK_SETRETTORCV(stix,nargs) (STIX_STACK_POPS(stix, nargs))
#define STIX_STACK_SETRETTOERROR(stix,nargs,ec) (STIX_STACK_POPS(stix, nargs), STIX_STACK_SETTOP(stix, STIX_ERROR_TO_OOP(ec))) #define STIX_STACK_SETRETTOERROR(stix,nargs) STIX_STACK_SETRET(stix, nargs, STIX_ERROR_TO_OOP(stix->errnum))
/*#define STIX_STACK_SETRETTOERROR(stix,nargs,ec) STIX_STACK_SETRET(stix, nargs, STIX_ERROR_TO_OOP(ec))*/
/* ========================================================================= /* =========================================================================
* STIX VM LOGGING * STIX VM LOGGING
@ -1081,6 +1084,10 @@ STIX_EXPORT void stix_seterrnum (
stix_errnum_t errnum stix_errnum_t errnum
); );
STIX_EXPORT const stix_ooch_t* stix_geterrstr (
stix_t* stix
);
/** /**
* The stix_getoption() function gets the value of an option * The stix_getoption() function gets the value of an option
* specified by \a id into the buffer pointed to by \a value. * specified by \a id into the buffer pointed to by \a value.
@ -1228,15 +1235,19 @@ STIX_EXPORT int stix_genpfmethod (
* ========================================================================= */ * ========================================================================= */
#if defined(STIX_OOCH_IS_UCH) #if defined(STIX_OOCH_IS_UCH)
# define stix_oocstobcs(stix,oocs,oocslen,bcs,bcslen) stix_ucstobcs(stix,oocs,oocslen,bcs,bcslen) # define stix_convootobchars(stix,oocs,oocslen,bcs,bcslen) stix_convutobchars(stix,oocs,oocslen,bcs,bcslen)
# define stix_bcstooocs(stix,bcs,bcslen,oocs,oocslen) stix_bcstoucs(stix,bcs,bcslen,oocs,oocslen) # define stix_convbtooochars(stix,bcs,bcslen,oocs,oocslen) stix_convbtouchars(stix,bcs,bcslen,oocs,oocslen)
# define stix_convootobcstr(stix,oocs,oocslen,bcs,bcslen) stix_convutobcstr(stix,oocs,oocslen,bcs,bcslen)
# define stix_convbtooocstr(stix,bcs,bcslen,oocs,oocslen) stix_convbtoucstr(stix,bcs,bcslen,oocs,oocslen)
#else #else
#error TODO #error TODO
# define stix_oocstobcs(stix,oocs,oocslen,bcs,bcslen) stix_ucstobcs(stix,oocs,oocslen,bcs,bcslen) # define stix_convootobchars(stix,oocs,oocslen,bcs,bcslen) stix_convutobchars(stix,oocs,oocslen,bcs,bcslen)
# define stix_bcstooocs(stix,bcs,bcslen,oocs,oocslen) stix_bcstoucs(stix,bcs,bcslen,oocs,oocslen) # define stix_convbtooochars(stix,bcs,bcslen,oocs,oocslen) stix_convbtouchars(stix,bcs,bcslen,oocs,oocslen)
# define stix_convootobcstr(stix,oocs,oocslen,bcs,bcslen) stix_convutobcstr(stix,oocs,oocslen,bcs,bcslen)
# define stix_convbtooocstr(stix,bcs,bcslen,oocs,oocslen) stix_convbtoucstr(stix,bcs,bcslen,oocs,oocslen)
#endif #endif
STIX_EXPORT int stix_bcstoucs ( STIX_EXPORT int stix_convbtouchars (
stix_t* stix, stix_t* stix,
const stix_bch_t* bcs, const stix_bch_t* bcs,
stix_oow_t* bcslen, stix_oow_t* bcslen,
@ -1244,7 +1255,7 @@ STIX_EXPORT int stix_bcstoucs (
stix_oow_t* ucslen stix_oow_t* ucslen
); );
STIX_EXPORT int stix_ucstobcs ( STIX_EXPORT int stix_convutobchars (
stix_t* stix, stix_t* stix,
const stix_uch_t* ucs, const stix_uch_t* ucs,
stix_oow_t* ucslen, stix_oow_t* ucslen,
@ -1253,6 +1264,31 @@ STIX_EXPORT int stix_ucstobcs (
); );
/**
* The stix_convbtoucstr() function converts a null-terminated byte string
* to a wide string.
*/
STIX_EXPORT int stix_convbtoucstr (
stix_t* stix,
const stix_bch_t* bcs,
stix_oow_t* bcslen,
stix_uch_t* ucs,
stix_oow_t* ucslen
);
/**
* The stix_convutobcstr() function converts a null-terminated wide string
* to a byte string.
*/
int stix_convutobcstr (
stix_t* stix,
const stix_uch_t* ucs,
stix_oow_t* ucslen,
stix_bch_t* bcs,
stix_oow_t* bcslen
);
/* ========================================================================= /* =========================================================================
* STIX VM LOGGING * STIX VM LOGGING
* ========================================================================= */ * ========================================================================= */
@ -1289,9 +1325,12 @@ STIX_EXPORT void stix_assertfailed (
); );
STIX_EXPORT stix_errnum_t stix_syserrtoerrnum ( STIX_EXPORT stix_errnum_t stix_syserrtoerrnum (
int e int syserr
); );
STIX_EXPORT const stix_ooch_t* stix_errnumtoerrstr (
stix_errnum_t errnum
);
#if defined(__cplusplus) #if defined(__cplusplus)
} }

View File

@ -31,7 +31,10 @@
/* some naming conventions /* some naming conventions
* bchars, uchars -> pointer and length * bchars, uchars -> pointer and length
* bcstr, ucstr -> null-terminated string pointer * bcstr, ucstr -> null-terminated string pointer
* bctouchars -> bchars to uchars * btouchars -> bchars to uchars
* utobchars -> uchars to bchars
* btoucstr -> bcstr to ucstr
* utobcstr -> ucstr to bcstr
*/ */
stix_oow_t stix_hashbytes (const stix_oob_t* ptr, stix_oow_t len) stix_oow_t stix_hashbytes (const stix_oob_t* ptr, stix_oow_t len)
@ -128,8 +131,10 @@ void stix_copybchars (stix_bch_t* dst, const stix_bch_t* src, stix_oow_t len)
for (i = 0; i < len; i++) dst[i] = src[i]; for (i = 0; i < len; i++) dst[i] = src[i];
} }
void stix_copybctouchars (stix_uch_t* dst, const stix_bch_t* src, stix_oow_t len) void stix_copybtouchars (stix_uch_t* dst, const stix_bch_t* src, stix_oow_t len)
{ {
/* copy without conversions.
* use stix_bctouchars() for conversion encoding */
stix_oow_t i; stix_oow_t i;
for (i = 0; i < len; i++) dst[i] = src[i]; for (i = 0; i < len; i++) dst[i] = src[i];
} }
@ -572,6 +577,8 @@ static int ucs_to_bcs_with_cmgr (
return ret; return ret;
} }
/* ----------------------------------------------------------------------- */
static stix_cmgr_t utf8_cmgr = static stix_cmgr_t utf8_cmgr =
{ {
stix_utf8touc, stix_utf8touc,
@ -583,58 +590,52 @@ stix_cmgr_t* stix_getutf8cmgr (void)
return &utf8_cmgr; return &utf8_cmgr;
} }
int stix_utf8toucs (const stix_bch_t* bcs, stix_oow_t* bcslen, stix_uch_t* ucs, stix_oow_t* ucslen) int stix_convutf8touchars (const stix_bch_t* bcs, stix_oow_t* bcslen, stix_uch_t* ucs, stix_oow_t* ucslen)
{ {
if (*bcslen == ~(stix_oow_t)0) /* the source is length bound */
{ return bcsn_to_ucsn_with_cmgr (bcs, bcslen, ucs, ucslen, &utf8_cmgr, 0);
/* the source is null-terminated. */
return bcs_to_ucs_with_cmgr (bcs, bcslen, ucs, ucslen, &utf8_cmgr, 0);
}
else
{
/* the source is length bound */
return bcsn_to_ucsn_with_cmgr (bcs, bcslen, ucs, ucslen, &utf8_cmgr, 0);
}
} }
int stix_ucstoutf8 (const stix_uch_t* ucs, stix_oow_t* ucslen, stix_bch_t* bcs, stix_oow_t* bcslen) int stix_convutoutf8chars (const stix_uch_t* ucs, stix_oow_t* ucslen, stix_bch_t* bcs, stix_oow_t* bcslen)
{ {
if (*ucslen == ~(stix_oow_t)0) /* length bound */
{ return ucsn_to_bcsn_with_cmgr (ucs, ucslen, bcs, bcslen, &utf8_cmgr);
/* null-terminated */
return ucs_to_bcs_with_cmgr (ucs, ucslen, bcs, bcslen, &utf8_cmgr);
}
else
{
/* length bound */
return ucsn_to_bcsn_with_cmgr (ucs, ucslen, bcs, bcslen, &utf8_cmgr);
}
} }
int stix_bcstoucs (stix_t* stix, const stix_bch_t* bcs, stix_oow_t* bcslen, stix_uch_t* ucs, stix_oow_t* ucslen) int stix_convutf8toucstr (const stix_bch_t* bcs, stix_oow_t* bcslen, stix_uch_t* ucs, stix_oow_t* ucslen)
{ {
if (*bcslen == ~(stix_oow_t)0) /* null-terminated. */
{ return bcs_to_ucs_with_cmgr (bcs, bcslen, ucs, ucslen, &utf8_cmgr, 0);
/* the source is null-terminated. */
return bcs_to_ucs_with_cmgr (bcs, bcslen, ucs, ucslen, stix->cmgr, 0);
}
else
{
/* the source is length bound */
return bcsn_to_ucsn_with_cmgr (bcs, bcslen, ucs, ucslen, stix->cmgr, 0);
}
} }
int stix_ucstobcs (stix_t* stix, const stix_uch_t* ucs, stix_oow_t* ucslen, stix_bch_t* bcs, stix_oow_t* bcslen) int stix_convutoutf8cstr (const stix_uch_t* ucs, stix_oow_t* ucslen, stix_bch_t* bcs, stix_oow_t* bcslen)
{ {
if (*ucslen == ~(stix_oow_t)0) /* null-terminated */
{ return ucs_to_bcs_with_cmgr (ucs, ucslen, bcs, bcslen, &utf8_cmgr);
/* null-terminated */ }
return ucs_to_bcs_with_cmgr (ucs, ucslen, bcs, bcslen, stix->cmgr);
} /* ----------------------------------------------------------------------- */
else
{ int stix_convbtouchars (stix_t* stix, const stix_bch_t* bcs, stix_oow_t* bcslen, stix_uch_t* ucs, stix_oow_t* ucslen)
/* length bound */ {
return ucsn_to_bcsn_with_cmgr (ucs, ucslen, bcs, bcslen, stix->cmgr); /* length bound */
} return bcsn_to_ucsn_with_cmgr (bcs, bcslen, ucs, ucslen, stix->cmgr, 0);
}
int stix_convutobchars (stix_t* stix, const stix_uch_t* ucs, stix_oow_t* ucslen, stix_bch_t* bcs, stix_oow_t* bcslen)
{
/* length bound */
return ucsn_to_bcsn_with_cmgr (ucs, ucslen, bcs, bcslen, stix->cmgr);
}
int stix_convbtoucstr (stix_t* stix, const stix_bch_t* bcs, stix_oow_t* bcslen, stix_uch_t* ucs, stix_oow_t* ucslen)
{
/* null-terminated. */
return bcs_to_ucs_with_cmgr (bcs, bcslen, ucs, ucslen, stix->cmgr, 0);
}
int stix_convutobcstr (stix_t* stix, const stix_uch_t* ucs, stix_oow_t* ucslen, stix_bch_t* bcs, stix_oow_t* bcslen)
{
/* null-terminated */
return ucs_to_bcs_with_cmgr (ucs, ucslen, bcs, bcslen, stix->cmgr);
} }

View File

@ -166,7 +166,7 @@ static int pf_write (stix_t* stix, stix_ooi_t nargs)
{ {
ucslen = ucsrem; ucslen = ucsrem;
bcslen = STIX_COUNTOF(bcs); bcslen = STIX_COUNTOF(bcs);
if ((n = stix_oocstobcs (stix, &oomsg->slot[ucspos], &ucslen, bcs, &bcslen)) <= -1) if ((n = stix_convootobchars (stix, &oomsg->slot[ucspos], &ucslen, bcs, &bcslen)) <= -1)
{ {
if (n != -2 || ucslen <= 0) if (n != -2 || ucslen <= 0)
{ {

View File

@ -63,15 +63,14 @@ static int pf_open (stix_t* stix, stix_ooi_t nargs)
mode = (stix_oop_char_t)STIX_STACK_GETARG(stix, nargs, 1); mode = (stix_oop_char_t)STIX_STACK_GETARG(stix, nargs, 1);
#if defined(STIX_OOCH_IS_UCH) #if defined(STIX_OOCH_IS_UCH)
/* TODO: error check on string conversion */
ucslen = STIX_OBJ_GET_SIZE(name); ucslen = STIX_OBJ_GET_SIZE(name);
bcslen = STIX_COUNTOF(namebuf) - 1; bcslen = STIX_COUNTOF(namebuf) - 1;
stix_oocstobcs (stix, name->slot, &ucslen, namebuf, &bcslen); /* TODO: error check */ if (stix_convootobchars (stix, name->slot, &ucslen, namebuf, &bcslen) <= -1) goto reterr;
namebuf[bcslen] = '\0'; namebuf[bcslen] = '\0';
ucslen = STIX_OBJ_GET_SIZE(mode); ucslen = STIX_OBJ_GET_SIZE(mode);
bcslen = STIX_COUNTOF(modebuf) - 1; bcslen = STIX_COUNTOF(modebuf) - 1;
stix_oocstobcs (stix, mode->slot, &ucslen, modebuf, &bcslen); /* TODO: error check */ if (stix_convootobchars (stix, mode->slot, &ucslen, modebuf, &bcslen) <= -1) goto reterr;
modebuf[bcslen] = '\0'; modebuf[bcslen] = '\0';
rcv->fp = fopen (namebuf, modebuf); rcv->fp = fopen (namebuf, modebuf);
@ -81,13 +80,17 @@ static int pf_open (stix_t* stix, stix_ooi_t nargs)
if (!rcv->fp) if (!rcv->fp)
{ {
STIX_DEBUG2 (stix, "cannot open %s for %s\n", namebuf, modebuf); STIX_DEBUG2 (stix, "cannot open %s for %s\n", namebuf, modebuf);
STIX_STACK_SETRETTOERROR (stix, nargs, stix_syserrtoerrnum(errno)); stix_seterrnum (stix, stix_syserrtoerrnum(errno));
return 1; goto reterr;
} }
STIX_DEBUG3 (stix, "opened %s for %s - %p\n", namebuf, modebuf, rcv->fp); STIX_DEBUG3 (stix, "opened %s for %s - %p\n", namebuf, modebuf, rcv->fp);
STIX_STACK_SETRETTORCV (stix, nargs); STIX_STACK_SETRETTORCV (stix, nargs);
return 1; return 1;
reterr:
STIX_STACK_SETRETTOERROR (stix, nargs);
return 1;
} }
static int pf_close (stix_t* stix, stix_ooi_t nargs) static int pf_close (stix_t* stix, stix_ooi_t nargs)
@ -113,7 +116,7 @@ static int pf_gets (stix_t* stix, stix_ooi_t nargs)
return 1; return 1;
} }
static int pf_puts (stix_t* stix, stix_ooi_t nargs) static int __pf_puts (stix_t* stix, stix_ooi_t nargs, stix_oow_t limit)
{ {
stdio_t* rcv; stdio_t* rcv;
stix_ooi_t i; stix_ooi_t i;
@ -145,31 +148,29 @@ static int pf_puts (stix_t* stix, stix_ooi_t nargs)
puts_string: puts_string:
ucspos = 0; ucspos = 0;
ucsrem = STIX_OBJ_GET_SIZE(x); ucsrem = STIX_OBJ_GET_SIZE(x);
if (ucsrem > limit) ucsrem = limit;
while (ucsrem > 0) while (ucsrem > 0)
{ {
ucslen = ucsrem; ucslen = ucsrem;
bcslen = STIX_COUNTOF(bcs); bcslen = STIX_COUNTOF(bcs);
/* TODO: implement character conversion into stdio and use it */
if ((n = stix_oocstobcs (stix, &x->slot[ucspos], &ucslen, bcs, &bcslen)) <= -1) /* TODO: implement character conversion into stdio and use it instead of vm's conversion facility. */
if ((n = stix_convootobchars (stix, &x->slot[ucspos], &ucslen, bcs, &bcslen)) <= -1)
{ {
if (n != -2 || ucslen <= 0) if (n != -2 || ucslen <= 0)
{ {
/* TODO:
*
STIX_STACK_SETRET (stix, nargs, stix->_error);
return 1;
*
*/
stix_seterrnum (stix, STIX_EECERR); stix_seterrnum (stix, STIX_EECERR);
return -1; goto reterr;
} }
} }
fwrite (bcs, bcslen, 1, rcv->fp); if (fwrite (bcs, 1, bcslen, rcv->fp) < bcslen)
/* TODO; error handling... {
STIX_STACK_SETRET (stix, nargs, stix->_error); stix_seterrnum (stix, stix_syserrtoerrnum(errno));
return 1; goto reterr;
*/ }
/* TODO: abort looping for async processing???? */ /* TODO: abort looping for async processing???? */
ucspos += ucslen; ucspos += ucslen;
ucsrem -= ucslen; ucsrem -= ucslen;
@ -177,21 +178,30 @@ return 1;
} }
else else
{ {
/* TODO;
STIX_STACK_SETRET (stix, nargs, stix->_error);
return 1;
*/
STIX_DEBUG1 (stix, "ARGUMETN ISN INVALID...[%O]\n", x);
stix_seterrnum (stix, STIX_EINVAL); stix_seterrnum (stix, STIX_EINVAL);
return -1; goto reterr;
} }
} }
STIX_STACK_SETRETTORCV (stix, nargs); STIX_STACK_SETRETTORCV (stix, nargs);
return 1; return 1;
reterr:
STIX_STACK_SETRETTOERROR (stix, nargs);
return 1;
} }
static int pf_putc (stix_t* stix, stix_ooi_t nargs)
{
return __pf_puts (stix, nargs, 1);
}
static int pf_puts (stix_t* stix, stix_ooi_t nargs)
{
return __pf_puts (stix, nargs, STIX_TYPE_MAX(stix_oow_t));
}
/* ------------------------------------------------------------------------ */ /* ------------------------------------------------------------------------ */
typedef struct fnctab_t fnctab_t; typedef struct fnctab_t fnctab_t;
@ -212,6 +222,8 @@ static fnctab_t fnctab[] =
{ I, { 'c','l','o','s','e','\0' }, 0, pf_close }, { I, { 'c','l','o','s','e','\0' }, 0, pf_close },
{ I, { 'g','e','t','s','\0' }, 0, pf_gets }, { I, { 'g','e','t','s','\0' }, 0, pf_gets },
{ I, { 'o','p','e','n',':','f','o','r',':','\0' }, 0, pf_open }, { I, { 'o','p','e','n',':','f','o','r',':','\0' }, 0, pf_open },
{ I, { 'p','u','t','c','\0' }, 1, pf_putc },
{ I, { 'p','u','t','c',':','\0' }, 0, pf_putc },
{ I, { 'p','u','t','s','\0' }, 1, pf_puts }, { I, { 'p','u','t','s','\0' }, 1, pf_puts },
{ I, { 'p','u','t','s',':','\0' }, 0, pf_puts } { I, { 'p','u','t','s',':','\0' }, 0, pf_puts }
}; };