changed hcl_inttostr()

touched up log output and formatted output functions
This commit is contained in:
hyung-hwan 2019-04-16 15:46:00 +00:00
parent dc3c393247
commit 9045220ef8
6 changed files with 170 additions and 152 deletions

View File

@ -41,8 +41,11 @@
#define IS_POW2(ui) (((ui) > 0) && ((ui) & ((ui) - 1)) == 0) #define IS_POW2(ui) (((ui) > 0) && ((ui) & ((ui) - 1)) == 0)
/* digit character array */ /* digit character array */
static char _digitc_upper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; static char* _digitc_array[] =
static char _digitc_lower[] = "0123456789abcdefghijklmnopqrstuvwxyz"; {
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
"0123456789abcdefghijklmnopqrstuvwxyz"
};
/* exponent table for pow2 between 1 and 32 inclusive. */ /* exponent table for pow2 between 1 and 32 inclusive. */
static hcl_uint8_t _exp_tab[32] = static hcl_uint8_t _exp_tab[32] =
@ -4387,22 +4390,14 @@ oops_einval:
return HCL_NULL; return HCL_NULL;
} }
static hcl_oow_t oow_to_text (hcl_t* hcl, hcl_oow_t w, int radix, hcl_ooch_t* buf) static hcl_oow_t oow_to_text (hcl_t* hcl, hcl_oow_t w, int flagged_radix, hcl_ooch_t* buf)
{ {
hcl_ooch_t* ptr; hcl_ooch_t* ptr;
const char* _digitc; const char* _digitc;
int radix;
if (radix < 0) radix = flagged_radix & HCL_INTTOSTR_RADIXMASK;
{ _digitc = _digitc_array[!!(flagged_radix & HCL_INTTOSTR_LOWERCASE)];
_digitc = _digitc_lower;
radix = -radix;
}
else
{
_digitc = _digitc_upper;
}
HCL_ASSERT (hcl, radix >= 2 && radix <= 36); HCL_ASSERT (hcl, radix >= 2 && radix <= 36);
ptr = buf; ptr = buf;
@ -4712,7 +4707,7 @@ static HCL_INLINE hcl_liw_t get_last_digit (hcl_t* hcl, hcl_liw_t* x, hcl_oow_t*
return carry; return carry;
} }
hcl_oop_t hcl_inttostr (hcl_t* hcl, hcl_oop_t num, int radix, int ngc) hcl_oop_t hcl_inttostr (hcl_t* hcl, hcl_oop_t num, int flagged_radix)
{ {
hcl_ooi_t v = 0; hcl_ooi_t v = 0;
hcl_oow_t w; hcl_oow_t w;
@ -4721,18 +4716,11 @@ hcl_oop_t hcl_inttostr (hcl_t* hcl, hcl_oop_t num, int radix, int ngc)
hcl_ooch_t* xbuf = HCL_NULL; hcl_ooch_t* xbuf = HCL_NULL;
hcl_oow_t xlen = 0, reqcapa; hcl_oow_t xlen = 0, reqcapa;
int radix;
const char* _digitc; const char* _digitc;
int orgradix = radix;
if (radix < 0) radix = flagged_radix & HCL_INTTOSTR_RADIXMASK;
{ _digitc = _digitc_array[!!(flagged_radix & HCL_INTTOSTR_LOWERCASE)];
_digitc = _digitc_lower;
radix = -radix;
}
else
{
_digitc = _digitc_upper;
}
HCL_ASSERT (hcl, radix >= 2 && radix <= 36); HCL_ASSERT (hcl, radix >= 2 && radix <= 36);
if (!hcl_isint(hcl,num)) goto oops_einval; if (!hcl_isint(hcl,num)) goto oops_einval;
@ -4758,11 +4746,10 @@ hcl_oop_t hcl_inttostr (hcl_t* hcl, hcl_oop_t num, int radix, int ngc)
xbuf = hcl->inttostr.xbuf.ptr; xbuf = hcl->inttostr.xbuf.ptr;
} }
xlen = oow_to_text(hcl, w, orgradix, xbuf); xlen = oow_to_text(hcl, w, flagged_radix, xbuf);
if (v < 0) xbuf[xlen++] = '-'; if (v < 0) xbuf[xlen++] = '-';
reverse_string (xbuf, xlen); reverse_string (xbuf, xlen);
#if 0
if (flagged_radix & HCL_INTTOSTR_NONEWOBJ) if (flagged_radix & HCL_INTTOSTR_NONEWOBJ)
{ {
/* special case. don't create a new object. /* special case. don't create a new object.
@ -4770,7 +4757,6 @@ hcl_oop_t hcl_inttostr (hcl_t* hcl, hcl_oop_t num, int radix, int ngc)
hcl->inttostr.xbuf.len = xlen; hcl->inttostr.xbuf.len = xlen;
return hcl->_nil; return hcl->_nil;
} }
#endif
return hcl_makestring(hcl, xbuf, xlen, 0); return hcl_makestring(hcl, xbuf, xlen, 0);
} }
@ -4812,7 +4798,7 @@ hcl_oop_t hcl_inttostr (hcl_t* hcl, hcl_oop_t num, int radix, int ngc)
if (HCL_IS_NBIGINT(hcl, num)) xbuf[xlen++] = '-'; if (HCL_IS_NBIGINT(hcl, num)) xbuf[xlen++] = '-';
reverse_string (xbuf, xlen); reverse_string (xbuf, xlen);
if (ngc < 0) if (flagged_radix & HCL_INTTOSTR_NONEWOBJ)
{ {
/* special case. don't create a new object. /* special case. don't create a new object.
* the caller can use the data left in hcl->inttostr.xbuf */ * the caller can use the data left in hcl->inttostr.xbuf */

View File

@ -959,20 +959,21 @@ hcl_oop_t hcl_strtoint (
int radix int radix
); );
#define HCL_INTTOSTR_RADIXMASK (0xFF)
#define HCL_INTTOSTR_LOWERCASE (1 << 8)
#define HCL_INTTOSTR_NONEWOBJ (1 << 9)
/** /**
* The hcl_inttostr() function converts an integer object to a string object * The hcl_inttostr() function converts an integer object to a string object
* printed in the given radix. If ngc is 0, it creates a normal string object * printed in the given radix. If HCL_INTTOSTR_NONEWOBJ is set in flags_radix,
* managed by object memory manager. If ngc greater than 0, it creates a non-GC * it returns hcl->_nil but keeps the result in the buffer pointed to by
* string object that should be destroyed with hcl_freengcobj() later. If ngc * hcl->inttostr.xbuf.ptr with the length stored in hcl->inttostr.xbuf.len.
* is less than 0, it returns hcl->_nil but keeps the result in the buffer * If the function fails, it returns #HCL_NULL.
* pointed to by hcl->inttostr.xbuf.ptr with the length stored in
* hcl->inttostr.xbuf.len. If the function fails, it returns #HCL_NULL.
*/ */
hcl_oop_t hcl_inttostr ( hcl_oop_t hcl_inttostr (
hcl_t* hcl, hcl_t* hcl,
hcl_oop_t num, hcl_oop_t num,
int radix, int flagged_radix /* radix between 2 and 36 inclusive, optionally bitwise ORed of HCL_INTTOSTR_XXX bits */
int ngc
); );
/* ========================================================================= */ /* ========================================================================= */

View File

@ -778,7 +778,7 @@ static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t
const hcl_ooch_t* fmt, * fmtend; const hcl_ooch_t* fmt, * fmtend;
const hcl_ooch_t* checkpoint, * percent; const hcl_ooch_t* checkpoint, * percent;
int n, base, neg, sign; int n, radix, neg, sign, radix_flags;
hcl_ooi_t extra, width, precision; hcl_ooi_t extra, width, precision;
hcl_ooch_t padc, ooch; hcl_ooch_t padc, ooch;
hcl_ooci_t ch; hcl_ooci_t ch;
@ -840,6 +840,7 @@ static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t
neg = 0; sign = 0; neg = 0; sign = 0;
flagc = 0; flagc = 0;
radix_flags = HCL_INTTOSTR_NONEWOBJ;
reswitch: reswitch:
GET_NEXT_CHAR_TO (hcl, fmt, fmtend, ch); GET_NEXT_CHAR_TO (hcl, fmt, fmtend, ch);
@ -955,23 +956,24 @@ static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t
/* integer conversions */ /* integer conversions */
case 'd': case 'd':
case 'i': /* signed conversion */ case 'i': /* signed conversion */
base = 10; radix = 10;
sign = 1; sign = 1;
goto print_integer; goto print_integer;
case 'o': case 'o':
base = 8; radix = 8;
goto print_integer; goto print_integer;
case 'u': case 'u':
base = 10; radix = 10;
goto print_integer;
case 'X':
base = 16;
goto print_integer; goto print_integer;
case 'x': case 'x':
base = -16; radix_flags |= HCL_INTTOSTR_LOWERCASE;
case 'X':
radix= 16;
goto print_integer; goto print_integer;
case 'b': case 'b':
base = 2; radix = 2;
goto print_integer; goto print_integer;
/* end of integer conversions */ /* end of integer conversions */
@ -993,7 +995,7 @@ static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t
arg = fa->value; arg = fa->value;
} }
if (!hcl_inttostr(hcl, arg, 10, -1)) if (!hcl_inttostr(hcl, arg, 10 | HCL_INTTOSTR_NONEWOBJ))
{ {
HCL_LOG1 (hcl, HCL_LOG_WARN | HCL_LOG_UNTYPED, "unable to convert %O to string \n", arg); HCL_LOG1 (hcl, HCL_LOG_WARN | HCL_LOG_UNTYPED, "unable to convert %O to string \n", arg);
goto invalid_format; goto invalid_format;
@ -1183,7 +1185,7 @@ static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t
arg = nv; arg = nv;
} }
if (!hcl_inttostr(hcl, arg, base, -1)) if (!hcl_inttostr(hcl, arg, radix | radix_flags))
{ {
/*hcl_seterrbfmt (hcl, HCL_EINVAL, "not a valid number - %O", arg); /*hcl_seterrbfmt (hcl, HCL_EINVAL, "not a valid number - %O", arg);
goto oops;*/ goto oops;*/
@ -1209,7 +1211,7 @@ static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t
if ((flagc & FLAGC_SHARP) && arg != HCL_SMOOI_TO_OOP(0)) if ((flagc & FLAGC_SHARP) && arg != HCL_SMOOI_TO_OOP(0))
{ {
if (base == 2 || base == 8 || base == 16 || base == -16) extra += 2; if (radix == 2 || radix == 8 || radix == 16 || radix == -16) extra += 2;
} }
if (neg) extra++; if (neg) extra++;
else if (flagc & FLAGC_SIGN) extra++; else if (flagc & FLAGC_SIGN) extra++;
@ -1233,17 +1235,17 @@ static HCL_INLINE int print_formatted (hcl_t* hcl, hcl_ooi_t nargs, hcl_fmtout_t
if ((flagc & FLAGC_SHARP) && arg != HCL_SMOOI_TO_OOP(0)) if ((flagc & FLAGC_SHARP) && arg != HCL_SMOOI_TO_OOP(0))
{ {
if (base == 2) if (radix == 2)
{ {
PRINT_OOCH ('#', 1); PRINT_OOCH ('#', 1);
PRINT_OOCH ('b', 1); PRINT_OOCH ('b', 1);
} }
if (base == 8) if (radix == 8)
{ {
PRINT_OOCH ('#', 1); PRINT_OOCH ('#', 1);
PRINT_OOCH ('o', 1); PRINT_OOCH ('o', 1);
} }
else if (base == 16 || base == -16) else if (radix == 16 || radix == -16)
{ {
PRINT_OOCH ('#', 1); PRINT_OOCH ('#', 1);
PRINT_OOCH ('x', 1); PRINT_OOCH ('x', 1);

View File

@ -84,6 +84,12 @@ struct bb_t
hcl_bch_t* fn; hcl_bch_t* fn;
}; };
enum logfd_flag_t
{
LOGFD_TTY = (1 << 0),
LOGFD_OPENED_HERE = (1 << 1)
};
typedef struct xtn_t xtn_t; typedef struct xtn_t xtn_t;
struct xtn_t struct xtn_t
{ {
@ -95,18 +101,19 @@ struct xtn_t
int vm_running; int vm_running;
int logfd; struct
hcl_bitmask_t logmask;
int logfd_istty;
struct
{ {
hcl_bch_t buf[4096]; int fd;
hcl_oow_t len; int fd_flag; /* bitwise OR'ed fo logfd_flag_t bits */
} logbuf;
struct
{
hcl_bch_t buf[4096];
hcl_oow_t len;
} out;
} log;
int reader_istty; int reader_istty;
hcl_oop_t sym_errstr; hcl_oop_t sym_errstr;
}; };
@ -438,32 +445,29 @@ static int write_all (int fd, const hcl_bch_t* ptr, hcl_oow_t len)
return 0; return 0;
} }
static int write_log (hcl_t* hcl, int fd, const hcl_bch_t* ptr, hcl_oow_t len) static int write_log (hcl_t* hcl, int fd, const hcl_bch_t* ptr, hcl_oow_t len)
{ {
xtn_t* xtn; xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);
xtn = (xtn_t*)hcl_getxtn(hcl);
while (len > 0) while (len > 0)
{ {
if (xtn->logbuf.len > 0) if (xtn->log.out.len > 0)
{ {
hcl_oow_t rcapa, cplen; hcl_oow_t rcapa, cplen;
rcapa = HCL_COUNTOF(xtn->logbuf.buf) - xtn->logbuf.len; rcapa = HCL_COUNTOF(xtn->log.out.buf) - xtn->log.out.len;
cplen = (len >= rcapa)? rcapa: len; cplen = (len >= rcapa)? rcapa: len;
memcpy (&xtn->logbuf.buf[xtn->logbuf.len], ptr, cplen); HCL_MEMCPY (&xtn->log.out.buf[xtn->log.out.len], ptr, cplen);
xtn->logbuf.len += cplen; xtn->log.out.len += cplen;
ptr += cplen; ptr += cplen;
len -= cplen; len -= cplen;
if (xtn->logbuf.len >= HCL_COUNTOF(xtn->logbuf.buf)) if (xtn->log.out.len >= HCL_COUNTOF(xtn->log.out.buf))
{ {
int n; int n;
n = write_all(fd, xtn->logbuf.buf, xtn->logbuf.len); n = write_all(fd, xtn->log.out.buf, xtn->log.out.len);
xtn->logbuf.len = 0; xtn->log.out.len = 0;
if (n <= -1) return -1; if (n <= -1) return -1;
} }
} }
@ -471,7 +475,7 @@ static int write_log (hcl_t* hcl, int fd, const hcl_bch_t* ptr, hcl_oow_t len)
{ {
hcl_oow_t rcapa; hcl_oow_t rcapa;
rcapa = HCL_COUNTOF(xtn->logbuf.buf); rcapa = HCL_COUNTOF(xtn->log.out.buf);
if (len >= rcapa) if (len >= rcapa)
{ {
if (write_all(fd, ptr, rcapa) <= -1) return -1; if (write_all(fd, ptr, rcapa) <= -1) return -1;
@ -480,11 +484,10 @@ static int write_log (hcl_t* hcl, int fd, const hcl_bch_t* ptr, hcl_oow_t len)
} }
else else
{ {
memcpy (xtn->logbuf.buf, ptr, len); HCL_MEMCPY (xtn->log.out.buf, ptr, len);
xtn->logbuf.len += len; xtn->log.out.len += len;
ptr += len; ptr += len;
len -= len; len -= len;
} }
} }
} }
@ -494,15 +497,15 @@ static int write_log (hcl_t* hcl, int fd, const hcl_bch_t* ptr, hcl_oow_t len)
static void flush_log (hcl_t* hcl, int fd) static void flush_log (hcl_t* hcl, int fd)
{ {
xtn_t* xtn; xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);
xtn = (xtn_t*)hcl_getxtn(hcl); if (xtn->log.out.len > 0)
if (xtn->logbuf.len > 0)
{ {
write_all (fd, xtn->logbuf.buf, xtn->logbuf.len); write_all (fd, xtn->log.out.buf, xtn->log.out.len);
xtn->logbuf.len = 0; xtn->log.out.len = 0;
} }
} }
static void log_write (hcl_t* hcl, hcl_bitmask_t mask, const hcl_ooch_t* msg, hcl_oow_t len) static void log_write (hcl_t* hcl, hcl_bitmask_t mask, const hcl_ooch_t* msg, hcl_oow_t len)
{ {
hcl_bch_t buf[256]; hcl_bch_t buf[256];
@ -512,23 +515,12 @@ static void log_write (hcl_t* hcl, hcl_bitmask_t mask, const hcl_ooch_t* msg, hc
xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl); xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);
int logfd; int logfd;
if (mask & HCL_LOG_STDERR) if (mask & HCL_LOG_STDERR) logfd = 2;
{ else if (mask & HCL_LOG_STDOUT) logfd = 1;
/* the messages that go to STDERR don't get masked out */
logfd = 2;
}
else else
{ {
if (!(xtn->logmask & mask & ~HCL_LOG_ALL_LEVELS)) return; /* check log types */ logfd = xtn->log.fd;
if (!(xtn->logmask & mask & ~HCL_LOG_ALL_TYPES)) return; /* check log levels */ if (logfd <= -1) return;
if (mask & HCL_LOG_STDOUT) logfd = 1;
else
{
logfd = xtn->logfd;
if (logfd <= -1) return;
}
} }
/* TODO: beautify the log message. /* TODO: beautify the log message.
@ -540,35 +532,43 @@ static void log_write (hcl_t* hcl, hcl_bitmask_t mask, const hcl_ooch_t* msg, hc
size_t tslen; size_t tslen;
struct tm tm, *tmp; struct tm tm, *tmp;
now = time(NULL); now = time(HCL_NULL);
#if defined(_WIN32) #if defined(_WIN32)
tmp = localtime(&now); tmp = localtime(&now);
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp); tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp);
if (tslen == 0) if (tslen == 0)
{ {
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S ", tmp); tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d ", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
if (tslen == 0)
{
strcpy (ts, "0000-00-00 00:00:00 +0000 ");
tslen = 26;
}
} }
#elif defined(__DOS__) #elif defined(__OS2__)
#if defined(__WATCOMC__)
tmp = _localtime(&now, &tm);
#else
tmp = localtime(&now); tmp = localtime(&now);
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S ", tmp); /* no timezone info */ #endif
#if defined(__BORLANDC__)
/* the borland compiler doesn't handle %z properly - it showed 00 all the time */
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %Z ", tmp);
#else
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp);
#endif
if (tslen == 0) if (tslen == 0)
{ {
strcpy (ts, "0000-00-00 00:00:00 "); tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d ", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
tslen = 20;
} }
#elif defined(__DOS__)
tmp = localtime(&now);
/* since i know that %z/%Z is not available in strftime, i switch to sprintf immediately */
tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d ", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
#else #else
#if defined(__OS2__) #if defined(HAVE_LOCALTIME_R)
tmp = _localtime(&now, &tm);
#elif defined(HAVE_LOCALTIME_R)
tmp = localtime_r(&now, &tm); tmp = localtime_r(&now, &tm);
#else #else
tmp = localtime(&now); tmp = localtime(&now);
#endif #endif
#if defined(HAVE_STRFTIME_SMALL_Z) #if defined(HAVE_STRFTIME_SMALL_Z)
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp); tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp);
#else #else
@ -576,14 +576,13 @@ static void log_write (hcl_t* hcl, hcl_bitmask_t mask, const hcl_ooch_t* msg, hc
#endif #endif
if (tslen == 0) if (tslen == 0)
{ {
strcpy (ts, "0000-00-00 00:00:00 +0000 "); tslen = sprintf(ts, "%04d-%02d-%02d %02d:%02d:%02d ", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
tslen = 26;
} }
#endif #endif
write_log (hcl, logfd, ts, tslen); write_log (hcl, logfd, ts, tslen);
} }
if (logfd == xtn->logfd && xtn->logfd_istty) if (logfd == xtn->log.fd && (xtn->log.fd_flag & LOGFD_TTY))
{ {
if (mask & HCL_LOG_FATAL) write_log (hcl, logfd, "\x1B[1;31m", 7); if (mask & HCL_LOG_FATAL) write_log (hcl, logfd, "\x1B[1;31m", 7);
else if (mask & HCL_LOG_ERROR) write_log (hcl, logfd, "\x1B[1;32m", 7); else if (mask & HCL_LOG_ERROR) write_log (hcl, logfd, "\x1B[1;32m", 7);
@ -628,7 +627,7 @@ static void log_write (hcl_t* hcl, hcl_bitmask_t mask, const hcl_ooch_t* msg, hc
write_log (hcl, logfd, msg, len); write_log (hcl, logfd, msg, len);
#endif #endif
if (logfd == xtn->logfd && xtn->logfd_istty) if (logfd == xtn->log.fd && (xtn->log.fd_flag & LOGFD_TTY))
{ {
if (mask & (HCL_LOG_FATAL | HCL_LOG_ERROR | HCL_LOG_WARN)) write_log (hcl, logfd, "\x1B[0m", 4); if (mask & (HCL_LOG_FATAL | HCL_LOG_ERROR | HCL_LOG_WARN)) write_log (hcl, logfd, "\x1B[0m", 4);
} }
@ -677,22 +676,32 @@ static void gc_hcl (hcl_t* hcl)
if (xtn->sym_errstr) xtn->sym_errstr = hcl_moveoop(hcl, xtn->sym_errstr); if (xtn->sym_errstr) xtn->sym_errstr = hcl_moveoop(hcl, xtn->sym_errstr);
} }
static HCL_INLINE void reset_log_to_default (xtn_t* xtn)
{
#if defined(ENABLE_LOG_INITIALLY)
xtn->log.fd = 2;
xtn->log.fd_flag = 0;
#if defined(HAVE_ISATTY)
if (isatty(xtn->log.fd)) xtn->log.fd_flag |= LOGFD_TTY;
#endif
#else
xtn->log.fd = -1;
xtn->log.fd_flag = 0;
#endif
}
static void fini_hcl (hcl_t* hcl) static void fini_hcl (hcl_t* hcl)
{ {
xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl); xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);
if (xtn->logfd >= 0) if ((xtn->log.fd_flag & LOGFD_OPENED_HERE) && xtn->log.fd >= 0) close (xtn->log.fd);
{ reset_log_to_default (xtn);
close (xtn->logfd);
xtn->logfd = -1;
xtn->logfd_istty = 0;
}
} }
/* ========================================================================= */ /* ========================================================================= */
static int handle_logopt (hcl_t* hcl, const hcl_bch_t* str) static int handle_logopt (hcl_t* hcl, const hcl_bch_t* str)
{ {
xtn_t* xtn = (xtn_t*)hcl_getxtn (hcl); xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);
hcl_bch_t* xstr = (hcl_bch_t*)str; hcl_bch_t* xstr = (hcl_bch_t*)str;
hcl_bch_t* cm, * flt; hcl_bch_t* cm, * flt;
hcl_bitmask_t logmask; hcl_bitmask_t logmask;
@ -712,7 +721,7 @@ static int handle_logopt (hcl_t* hcl, const hcl_bch_t* str)
cm = hcl_find_bchar_in_bcstr(xstr, ','); cm = hcl_find_bchar_in_bcstr(xstr, ',');
*cm = '\0'; *cm = '\0';
logmask = xtn->logmask; logmask = 0;
do do
{ {
flt = cm + 1; flt = cm + 1;
@ -759,23 +768,24 @@ static int handle_logopt (hcl_t* hcl, const hcl_bch_t* str)
} }
#if defined(_WIN32) #if defined(_WIN32)
xtn->logfd = _open(xstr, _O_CREAT | _O_WRONLY | _O_APPEND | _O_BINARY , 0644); xtn->log.fd = _open(xstr, _O_CREAT | _O_WRONLY | _O_APPEND | _O_BINARY , 0644);
#else #else
xtn->logfd = open(xstr, O_CREAT | O_WRONLY | O_APPEND , 0644); xtn->log.fd = open(xstr, O_CREAT | O_WRONLY | O_APPEND , 0644);
#endif #endif
if (xtn->logfd == -1) if (xtn->log.fd == -1)
{ {
fprintf (stderr, "ERROR: cannot open a log file %s\n", xstr); fprintf (stderr, "ERROR: cannot open a log file %s\n", xstr);
if (str != xstr) hcl_freemem (hcl, xstr); if (str != xstr) hcl_freemem (hcl, xstr);
return -1; return -1;
} }
xtn->logmask = logmask; xtn->log.fd_flag |= LOGFD_OPENED_HERE;
#if defined(HAVE_ISATTY) #if defined(HAVE_ISATTY)
xtn->logfd_istty = isatty(xtn->logfd); if (isatty(xtn->log.fd)) xtn->log.fd_flag |= LOGFD_TTY;
#endif #endif
if (str != xstr) hcl_freemem (hcl, xstr); if (str != xstr) hcl_freemem (hcl, xstr);
hcl_setoption (hcl, HCL_LOG_MASK, &logmask);
return 0; return 0;
} }
@ -1063,9 +1073,8 @@ int main (int argc, char* argv[])
hcl_setoption (hcl, HCL_LOG_MASK, &trait);*/ hcl_setoption (hcl, HCL_LOG_MASK, &trait);*/
} }
xtn = (xtn_t*)hcl_getxtn (hcl); xtn = (xtn_t*)hcl_getxtn(hcl);
xtn->logfd = -1; reset_log_to_default (xtn);
xtn->logfd_istty = 0;
memset (&hclcb, 0, HCL_SIZEOF(hclcb)); memset (&hclcb, 0, HCL_SIZEOF(hclcb));
hclcb.fini = fini_hcl; hclcb.fini = fini_hcl;
@ -1077,12 +1086,7 @@ int main (int argc, char* argv[])
if (logopt) if (logopt)
{ {
if (handle_logopt (hcl, logopt) <= -1) goto oops; if (handle_logopt(hcl, logopt) <= -1) goto oops;
}
else
{
/* default logging mask when no logging option is set */
xtn->logmask = HCL_LOG_ALL_TYPES | HCL_LOG_ERROR | HCL_LOG_FATAL;
} }
#if defined(HCL_BUILD_DEBUG) #if defined(HCL_BUILD_DEBUG)
@ -1170,7 +1174,7 @@ count++;
code_offset = hcl_getbclen(hcl); code_offset = hcl_getbclen(hcl);
hcl_proutbfmt (hcl, 0, "\n"); /*hcl_proutbfmt (hcl, 0, "\n");*/
if (hcl_compile(hcl, obj) <= -1) if (hcl_compile(hcl, obj) <= -1)
{ {
if (hcl->errnum == HCL_ESYNERR) if (hcl->errnum == HCL_ESYNERR)
@ -1201,8 +1205,9 @@ count++;
} }
else else
{ {
hcl_logbfmt (hcl, HCL_LOG_STDERR, "OK: EXITED WITH %O\n", retv); /* print the result in the interactive mode regardless 'verbose' */
hcl_logbfmt (hcl, HCL_LOG_STDOUT, "%O\n", retv);
/* /*
* print the value of ERRSTR. * print the value of ERRSTR.
hcl_oop_cons_t cons = hcl_getatsysdic(hcl, xtn->sym_errstr); hcl_oop_cons_t cons = hcl_getatsysdic(hcl, xtn->sym_errstr);
@ -1234,9 +1239,9 @@ count++;
{ {
hcl_logbfmt (hcl, HCL_LOG_STDERR, "ERROR: cannot execute - [%d] %js\n", hcl_geterrnum(hcl), hcl_geterrmsg(hcl)); hcl_logbfmt (hcl, HCL_LOG_STDERR, "ERROR: cannot execute - [%d] %js\n", hcl_geterrnum(hcl), hcl_geterrmsg(hcl));
} }
else else if (verbose)
{ {
if (verbose) hcl_logbfmt (hcl, HCL_LOG_STDERR, "EXECUTION OK - EXITED WITH %O\n", retv); hcl_logbfmt (hcl, HCL_LOG_STDERR, "EXECUTION OK - EXITED WITH %O\n", retv);
} }
/*cancel_tick();*/ /*cancel_tick();*/

View File

@ -272,7 +272,7 @@ next:
/* -1 to drive hcl_inttostr() to not create a new string object. /* -1 to drive hcl_inttostr() to not create a new string object.
* not using the object memory. the result stays in the temporary * not using the object memory. the result stays in the temporary
* buffer */ * buffer */
tmp = hcl_inttostr(hcl, obj, 10, -1); tmp = hcl_inttostr(hcl, obj, 10 | HCL_INTTOSTR_NONEWOBJ);
if (!tmp) return -1; if (!tmp) return -1;
HCL_ASSERT (hcl, (hcl_oop_t)tmp == hcl->_nil); HCL_ASSERT (hcl, (hcl_oop_t)tmp == hcl->_nil);
@ -303,7 +303,7 @@ next:
hcl_oop_t tmp; hcl_oop_t tmp;
hcl_oow_t len, adj; hcl_oow_t len, adj;
tmp = hcl_inttostr(hcl, f->value, 10, -1); tmp = hcl_inttostr(hcl, f->value, 10 | HCL_INTTOSTR_NONEWOBJ);
if (!tmp) return -1; if (!tmp) return -1;
adj = (hcl->inttostr.xbuf.ptr[0] == '-'); adj = (hcl->inttostr.xbuf.ptr[0] == '-');

View File

@ -1,11 +1,35 @@
a=`openssl rand -hex 20 | tr '[a-z]' '[A-Z]'` count=0
b=`openssl rand -hex 19 | tr '[a-z]' '[A-Z]'` while true
do
/bin/echo -n -e "$count\r"
count=$(($count + 1))
q1=$(echo -e "ibase=16\nprint ($a / $b)" | bc -q) a=`openssl rand -hex 1 | tr '[a-z]' '[A-Z]'`
r1=$(echo -e "ibase=16\nprint ($a % $b)" | bc -q) a=$(echo -e "(printf \"%O\" #x$a)" | ~/xxx/bin/hcl --log /dev/null /dev/stdin)
[ "$a" = "0" ] && a=1
a=`openssl rand -hex $a | tr '[a-z]' '[A-Z]'`
b=`openssl rand -hex 1 | tr '[a-z]' '[A-Z]'`
b=$(echo -e "(printf \"%O\" #x$b)" | ~/xxx/bin/hcl --log /dev/null /dev/stdin)
[ "$b" = "0" ] && b=1
b=`openssl rand -hex $b | tr '[a-z]' '[A-Z]'`
a=$(echo -e "(printf \"%O\" #x$a)" | ~/xxx/bin/hcl --log /dev/null /dev/stdin)
b=$(echo -e "(printf \"%O\" #x$b)" | ~/xxx/bin/hcl --log /dev/null /dev/stdin)
[ "$b" = "0" ] && b=1
q2=$(echo -e "(printf \"%O\" (/ #x$a #x$b))" | ~/xxx/bin/hcl /dev/stdin 2> /dev/null) q=$(echo -e "(printf \"%O\" (/ $a $b))" | ~/xxx/bin/hcl --log /dev/null /dev/stdin)
r2=$(echo -e "(printf \"%O\" (rem #x$a #x$b))" | ~/xxx/bin/hcl /dev/stdin 2>/dev/null) r=$(echo -e "(printf \"%O\" (rem $a $b))" | ~/xxx/bin/hcl --log /dev/null /dev/stdin)
a1=$(echo -e "(printf \"%O\" (+ (* $q $b) $r))" | ~/xxx/bin/hcl --log /dev/null /dev/stdin)
echo "$q2"
echo "$r2" if [ "$a" != "$a1" ]
then
echo "a=>$a"
echo "b=>$b"
echo "q=>$q"
echo "r=>$r"
echo "a1=>$a1"
break
fi
done