changed hcl_inttostr()
touched up log output and formatted output functions
This commit is contained in:
parent
dc3c393247
commit
9045220ef8
44
lib/bigint.c
44
lib/bigint.c
@ -41,8 +41,11 @@
|
||||
#define IS_POW2(ui) (((ui) > 0) && ((ui) & ((ui) - 1)) == 0)
|
||||
|
||||
/* digit character array */
|
||||
static char _digitc_upper[] = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
||||
static char _digitc_lower[] = "0123456789abcdefghijklmnopqrstuvwxyz";
|
||||
static char* _digitc_array[] =
|
||||
{
|
||||
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ",
|
||||
"0123456789abcdefghijklmnopqrstuvwxyz"
|
||||
};
|
||||
|
||||
/* exponent table for pow2 between 1 and 32 inclusive. */
|
||||
static hcl_uint8_t _exp_tab[32] =
|
||||
@ -4387,22 +4390,14 @@ oops_einval:
|
||||
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;
|
||||
const char* _digitc;
|
||||
int radix;
|
||||
|
||||
if (radix < 0)
|
||||
{
|
||||
|
||||
_digitc = _digitc_lower;
|
||||
radix = -radix;
|
||||
}
|
||||
else
|
||||
{
|
||||
_digitc = _digitc_upper;
|
||||
}
|
||||
|
||||
radix = flagged_radix & HCL_INTTOSTR_RADIXMASK;
|
||||
_digitc = _digitc_array[!!(flagged_radix & HCL_INTTOSTR_LOWERCASE)];
|
||||
HCL_ASSERT (hcl, radix >= 2 && radix <= 36);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
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_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_oow_t xlen = 0, reqcapa;
|
||||
|
||||
int radix;
|
||||
const char* _digitc;
|
||||
int orgradix = radix;
|
||||
|
||||
if (radix < 0)
|
||||
{
|
||||
_digitc = _digitc_lower;
|
||||
radix = -radix;
|
||||
}
|
||||
else
|
||||
{
|
||||
_digitc = _digitc_upper;
|
||||
}
|
||||
radix = flagged_radix & HCL_INTTOSTR_RADIXMASK;
|
||||
_digitc = _digitc_array[!!(flagged_radix & HCL_INTTOSTR_LOWERCASE)];
|
||||
HCL_ASSERT (hcl, radix >= 2 && radix <= 36);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
xlen = oow_to_text(hcl, w, orgradix, xbuf);
|
||||
xlen = oow_to_text(hcl, w, flagged_radix, xbuf);
|
||||
if (v < 0) xbuf[xlen++] = '-';
|
||||
|
||||
reverse_string (xbuf, xlen);
|
||||
#if 0
|
||||
if (flagged_radix & HCL_INTTOSTR_NONEWOBJ)
|
||||
{
|
||||
/* 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;
|
||||
return hcl->_nil;
|
||||
}
|
||||
#endif
|
||||
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++] = '-';
|
||||
reverse_string (xbuf, xlen);
|
||||
if (ngc < 0)
|
||||
if (flagged_radix & HCL_INTTOSTR_NONEWOBJ)
|
||||
{
|
||||
/* special case. don't create a new object.
|
||||
* the caller can use the data left in hcl->inttostr.xbuf */
|
||||
|
@ -959,20 +959,21 @@ hcl_oop_t hcl_strtoint (
|
||||
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
|
||||
* printed in the given radix. If ngc is 0, it creates a normal string object
|
||||
* managed by object memory manager. If ngc greater than 0, it creates a non-GC
|
||||
* string object that should be destroyed with hcl_freengcobj() later. If ngc
|
||||
* is less than 0, it returns hcl->_nil but keeps the result in the buffer
|
||||
* pointed to by hcl->inttostr.xbuf.ptr with the length stored in
|
||||
* hcl->inttostr.xbuf.len. If the function fails, it returns #HCL_NULL.
|
||||
* printed in the given radix. If HCL_INTTOSTR_NONEWOBJ is set in flags_radix,
|
||||
* it returns hcl->_nil but keeps the result in the buffer 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_t* hcl,
|
||||
hcl_oop_t num,
|
||||
int radix,
|
||||
int ngc
|
||||
int flagged_radix /* radix between 2 and 36 inclusive, optionally bitwise ORed of HCL_INTTOSTR_XXX bits */
|
||||
);
|
||||
|
||||
/* ========================================================================= */
|
||||
|
32
lib/logfmt.c
32
lib/logfmt.c
@ -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* checkpoint, * percent;
|
||||
|
||||
int n, base, neg, sign;
|
||||
int n, radix, neg, sign, radix_flags;
|
||||
hcl_ooi_t extra, width, precision;
|
||||
hcl_ooch_t padc, ooch;
|
||||
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;
|
||||
|
||||
flagc = 0;
|
||||
radix_flags = HCL_INTTOSTR_NONEWOBJ;
|
||||
|
||||
reswitch:
|
||||
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 */
|
||||
case 'd':
|
||||
case 'i': /* signed conversion */
|
||||
base = 10;
|
||||
radix = 10;
|
||||
sign = 1;
|
||||
goto print_integer;
|
||||
case 'o':
|
||||
base = 8;
|
||||
radix = 8;
|
||||
goto print_integer;
|
||||
case 'u':
|
||||
base = 10;
|
||||
goto print_integer;
|
||||
case 'X':
|
||||
base = 16;
|
||||
radix = 10;
|
||||
goto print_integer;
|
||||
|
||||
case 'x':
|
||||
base = -16;
|
||||
radix_flags |= HCL_INTTOSTR_LOWERCASE;
|
||||
case 'X':
|
||||
radix= 16;
|
||||
goto print_integer;
|
||||
|
||||
case 'b':
|
||||
base = 2;
|
||||
radix = 2;
|
||||
goto print_integer;
|
||||
/* 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;
|
||||
}
|
||||
|
||||
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);
|
||||
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;
|
||||
}
|
||||
|
||||
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);
|
||||
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 (base == 2 || base == 8 || base == 16 || base == -16) extra += 2;
|
||||
if (radix == 2 || radix == 8 || radix == 16 || radix == -16) extra += 2;
|
||||
}
|
||||
if (neg) 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 (base == 2)
|
||||
if (radix == 2)
|
||||
{
|
||||
PRINT_OOCH ('#', 1);
|
||||
PRINT_OOCH ('b', 1);
|
||||
}
|
||||
if (base == 8)
|
||||
if (radix == 8)
|
||||
{
|
||||
PRINT_OOCH ('#', 1);
|
||||
PRINT_OOCH ('o', 1);
|
||||
}
|
||||
else if (base == 16 || base == -16)
|
||||
else if (radix == 16 || radix == -16)
|
||||
{
|
||||
PRINT_OOCH ('#', 1);
|
||||
PRINT_OOCH ('x', 1);
|
||||
|
183
lib/main.c
183
lib/main.c
@ -84,6 +84,12 @@ struct bb_t
|
||||
hcl_bch_t* fn;
|
||||
};
|
||||
|
||||
enum logfd_flag_t
|
||||
{
|
||||
LOGFD_TTY = (1 << 0),
|
||||
LOGFD_OPENED_HERE = (1 << 1)
|
||||
};
|
||||
|
||||
typedef struct xtn_t xtn_t;
|
||||
struct xtn_t
|
||||
{
|
||||
@ -95,18 +101,19 @@ struct xtn_t
|
||||
|
||||
int vm_running;
|
||||
|
||||
int logfd;
|
||||
hcl_bitmask_t logmask;
|
||||
int logfd_istty;
|
||||
|
||||
struct
|
||||
struct
|
||||
{
|
||||
hcl_bch_t buf[4096];
|
||||
hcl_oow_t len;
|
||||
} logbuf;
|
||||
int fd;
|
||||
int fd_flag; /* bitwise OR'ed fo logfd_flag_t bits */
|
||||
|
||||
struct
|
||||
{
|
||||
hcl_bch_t buf[4096];
|
||||
hcl_oow_t len;
|
||||
} out;
|
||||
} log;
|
||||
|
||||
int reader_istty;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
static int write_log (hcl_t* hcl, int fd, const hcl_bch_t* ptr, hcl_oow_t len)
|
||||
{
|
||||
xtn_t* xtn;
|
||||
|
||||
xtn = (xtn_t*)hcl_getxtn(hcl);
|
||||
xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);
|
||||
|
||||
while (len > 0)
|
||||
{
|
||||
if (xtn->logbuf.len > 0)
|
||||
if (xtn->log.out.len > 0)
|
||||
{
|
||||
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;
|
||||
|
||||
memcpy (&xtn->logbuf.buf[xtn->logbuf.len], ptr, cplen);
|
||||
xtn->logbuf.len += cplen;
|
||||
HCL_MEMCPY (&xtn->log.out.buf[xtn->log.out.len], ptr, cplen);
|
||||
xtn->log.out.len += cplen;
|
||||
ptr += 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;
|
||||
n = write_all(fd, xtn->logbuf.buf, xtn->logbuf.len);
|
||||
xtn->logbuf.len = 0;
|
||||
n = write_all(fd, xtn->log.out.buf, xtn->log.out.len);
|
||||
xtn->log.out.len = 0;
|
||||
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;
|
||||
|
||||
rcapa = HCL_COUNTOF(xtn->logbuf.buf);
|
||||
rcapa = HCL_COUNTOF(xtn->log.out.buf);
|
||||
if (len >= rcapa)
|
||||
{
|
||||
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
|
||||
{
|
||||
memcpy (xtn->logbuf.buf, ptr, len);
|
||||
xtn->logbuf.len += len;
|
||||
HCL_MEMCPY (xtn->log.out.buf, ptr, len);
|
||||
xtn->log.out.len += len;
|
||||
ptr += 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)
|
||||
{
|
||||
xtn_t* xtn;
|
||||
xtn = (xtn_t*)hcl_getxtn(hcl);
|
||||
if (xtn->logbuf.len > 0)
|
||||
xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);
|
||||
if (xtn->log.out.len > 0)
|
||||
{
|
||||
write_all (fd, xtn->logbuf.buf, xtn->logbuf.len);
|
||||
xtn->logbuf.len = 0;
|
||||
write_all (fd, xtn->log.out.buf, xtn->log.out.len);
|
||||
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)
|
||||
{
|
||||
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);
|
||||
int logfd;
|
||||
|
||||
if (mask & HCL_LOG_STDERR)
|
||||
{
|
||||
/* the messages that go to STDERR don't get masked out */
|
||||
logfd = 2;
|
||||
}
|
||||
if (mask & HCL_LOG_STDERR) logfd = 2;
|
||||
else if (mask & HCL_LOG_STDOUT) logfd = 1;
|
||||
else
|
||||
{
|
||||
if (!(xtn->logmask & mask & ~HCL_LOG_ALL_LEVELS)) return; /* check log types */
|
||||
if (!(xtn->logmask & mask & ~HCL_LOG_ALL_TYPES)) return; /* check log levels */
|
||||
|
||||
if (mask & HCL_LOG_STDOUT) logfd = 1;
|
||||
else
|
||||
{
|
||||
|
||||
logfd = xtn->logfd;
|
||||
if (logfd <= -1) return;
|
||||
}
|
||||
logfd = xtn->log.fd;
|
||||
if (logfd <= -1) return;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
struct tm tm, *tmp;
|
||||
|
||||
now = time(NULL);
|
||||
now = time(HCL_NULL);
|
||||
#if defined(_WIN32)
|
||||
tmp = localtime(&now);
|
||||
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp);
|
||||
if (tslen == 0)
|
||||
{
|
||||
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S ", tmp);
|
||||
if (tslen == 0)
|
||||
{
|
||||
strcpy (ts, "0000-00-00 00:00:00 +0000 ");
|
||||
tslen = 26;
|
||||
}
|
||||
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);
|
||||
}
|
||||
#elif defined(__DOS__)
|
||||
#elif defined(__OS2__)
|
||||
#if defined(__WATCOMC__)
|
||||
tmp = _localtime(&now, &tm);
|
||||
#else
|
||||
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)
|
||||
{
|
||||
strcpy (ts, "0000-00-00 00:00:00 ");
|
||||
tslen = 20;
|
||||
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);
|
||||
}
|
||||
|
||||
#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
|
||||
#if defined(__OS2__)
|
||||
tmp = _localtime(&now, &tm);
|
||||
#elif defined(HAVE_LOCALTIME_R)
|
||||
#if defined(HAVE_LOCALTIME_R)
|
||||
tmp = localtime_r(&now, &tm);
|
||||
#else
|
||||
tmp = localtime(&now);
|
||||
#endif
|
||||
|
||||
#if defined(HAVE_STRFTIME_SMALL_Z)
|
||||
tslen = strftime(ts, sizeof(ts), "%Y-%m-%d %H:%M:%S %z ", tmp);
|
||||
#else
|
||||
@ -576,14 +576,13 @@ static void log_write (hcl_t* hcl, hcl_bitmask_t mask, const hcl_ooch_t* msg, hc
|
||||
#endif
|
||||
if (tslen == 0)
|
||||
{
|
||||
strcpy (ts, "0000-00-00 00:00:00 +0000 ");
|
||||
tslen = 26;
|
||||
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);
|
||||
}
|
||||
#endif
|
||||
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);
|
||||
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);
|
||||
#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);
|
||||
}
|
||||
@ -677,22 +676,32 @@ static void gc_hcl (hcl_t* hcl)
|
||||
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)
|
||||
{
|
||||
xtn_t* xtn = (xtn_t*)hcl_getxtn(hcl);
|
||||
if (xtn->logfd >= 0)
|
||||
{
|
||||
close (xtn->logfd);
|
||||
xtn->logfd = -1;
|
||||
xtn->logfd_istty = 0;
|
||||
}
|
||||
if ((xtn->log.fd_flag & LOGFD_OPENED_HERE) && xtn->log.fd >= 0) close (xtn->log.fd);
|
||||
reset_log_to_default (xtn);
|
||||
}
|
||||
|
||||
/* ========================================================================= */
|
||||
|
||||
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* cm, * flt;
|
||||
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 = '\0';
|
||||
|
||||
logmask = xtn->logmask;
|
||||
logmask = 0;
|
||||
do
|
||||
{
|
||||
flt = cm + 1;
|
||||
@ -759,23 +768,24 @@ static int handle_logopt (hcl_t* hcl, const hcl_bch_t* str)
|
||||
}
|
||||
|
||||
#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
|
||||
xtn->logfd = open(xstr, O_CREAT | O_WRONLY | O_APPEND , 0644);
|
||||
xtn->log.fd = open(xstr, O_CREAT | O_WRONLY | O_APPEND , 0644);
|
||||
#endif
|
||||
if (xtn->logfd == -1)
|
||||
if (xtn->log.fd == -1)
|
||||
{
|
||||
fprintf (stderr, "ERROR: cannot open a log file %s\n", xstr);
|
||||
if (str != xstr) hcl_freemem (hcl, xstr);
|
||||
return -1;
|
||||
}
|
||||
|
||||
xtn->logmask = logmask;
|
||||
xtn->log.fd_flag |= LOGFD_OPENED_HERE;
|
||||
#if defined(HAVE_ISATTY)
|
||||
xtn->logfd_istty = isatty(xtn->logfd);
|
||||
if (isatty(xtn->log.fd)) xtn->log.fd_flag |= LOGFD_TTY;
|
||||
#endif
|
||||
|
||||
if (str != xstr) hcl_freemem (hcl, xstr);
|
||||
hcl_setoption (hcl, HCL_LOG_MASK, &logmask);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@ -1063,9 +1073,8 @@ int main (int argc, char* argv[])
|
||||
hcl_setoption (hcl, HCL_LOG_MASK, &trait);*/
|
||||
}
|
||||
|
||||
xtn = (xtn_t*)hcl_getxtn (hcl);
|
||||
xtn->logfd = -1;
|
||||
xtn->logfd_istty = 0;
|
||||
xtn = (xtn_t*)hcl_getxtn(hcl);
|
||||
reset_log_to_default (xtn);
|
||||
|
||||
memset (&hclcb, 0, HCL_SIZEOF(hclcb));
|
||||
hclcb.fini = fini_hcl;
|
||||
@ -1077,12 +1086,7 @@ int main (int argc, char* argv[])
|
||||
|
||||
if (logopt)
|
||||
{
|
||||
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 (handle_logopt(hcl, logopt) <= -1) goto oops;
|
||||
}
|
||||
|
||||
#if defined(HCL_BUILD_DEBUG)
|
||||
@ -1170,7 +1174,7 @@ count++;
|
||||
|
||||
code_offset = hcl_getbclen(hcl);
|
||||
|
||||
hcl_proutbfmt (hcl, 0, "\n");
|
||||
/*hcl_proutbfmt (hcl, 0, "\n");*/
|
||||
if (hcl_compile(hcl, obj) <= -1)
|
||||
{
|
||||
if (hcl->errnum == HCL_ESYNERR)
|
||||
@ -1201,8 +1205,9 @@ count++;
|
||||
}
|
||||
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.
|
||||
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));
|
||||
}
|
||||
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();*/
|
||||
|
@ -272,7 +272,7 @@ next:
|
||||
/* -1 to drive hcl_inttostr() to not create a new string object.
|
||||
* not using the object memory. the result stays in the temporary
|
||||
* buffer */
|
||||
tmp = hcl_inttostr(hcl, obj, 10, -1);
|
||||
tmp = hcl_inttostr(hcl, obj, 10 | HCL_INTTOSTR_NONEWOBJ);
|
||||
if (!tmp) return -1;
|
||||
|
||||
HCL_ASSERT (hcl, (hcl_oop_t)tmp == hcl->_nil);
|
||||
@ -303,7 +303,7 @@ next:
|
||||
hcl_oop_t tmp;
|
||||
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;
|
||||
|
||||
adj = (hcl->inttostr.xbuf.ptr[0] == '-');
|
||||
|
42
t/test-bi.sh
42
t/test-bi.sh
@ -1,11 +1,35 @@
|
||||
a=`openssl rand -hex 20 | tr '[a-z]' '[A-Z]'`
|
||||
b=`openssl rand -hex 19 | tr '[a-z]' '[A-Z]'`
|
||||
count=0
|
||||
while true
|
||||
do
|
||||
/bin/echo -n -e "$count\r"
|
||||
count=$(($count + 1))
|
||||
|
||||
q1=$(echo -e "ibase=16\nprint ($a / $b)" | bc -q)
|
||||
r1=$(echo -e "ibase=16\nprint ($a % $b)" | bc -q)
|
||||
a=`openssl rand -hex 1 | tr '[a-z]' '[A-Z]'`
|
||||
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)
|
||||
r2=$(echo -e "(printf \"%O\" (rem #x$a #x$b))" | ~/xxx/bin/hcl /dev/stdin 2>/dev/null)
|
||||
|
||||
echo "$q2"
|
||||
echo "$r2"
|
||||
q=$(echo -e "(printf \"%O\" (/ $a $b))" | ~/xxx/bin/hcl --log /dev/null /dev/stdin)
|
||||
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)
|
||||
|
||||
if [ "$a" != "$a1" ]
|
||||
then
|
||||
echo "a=>$a"
|
||||
echo "b=>$b"
|
||||
echo "q=>$q"
|
||||
echo "r=>$r"
|
||||
echo "a1=>$a1"
|
||||
break
|
||||
fi
|
||||
done
|
||||
|
Loading…
Reference in New Issue
Block a user