improve calc error code system

Changed calc_errno a global int variable so that is may be directly
accessed by libcalc users.

Further improve help files for help/errno, help/error, help/newerror,
help/stoponerror and help/strerror by adding to documentation
of the calc error code system as well as libcalc interface
where applicable.
This commit is contained in:
Landon Curt Noll
2023-09-12 16:06:27 -07:00
parent 19819340ff
commit 229a60e4d5
8 changed files with 162 additions and 80 deletions

View File

@@ -221,6 +221,14 @@ The following are the changes from calc version 2.14.3.5 to date:
Improve help/error. Added text about error code ranges and Improve help/error. Added text about error code ranges and
range symbols. range symbols.
Changed calc_errno a global int variable so that is may be directly
accessed by libcalc users.
Further improve help files for help/errno, help/error, help/newerror,
help/stoponerror and help/strerror by adding to documentation
of the calc error code system as well as libcalc interface
where applicable.
The following are the changes from calc version 2.14.3.4 to 2.14.3.5: The following are the changes from calc version 2.14.3.4 to 2.14.3.5:

1
calc.h
View File

@@ -222,6 +222,7 @@ EXTERN int abortlevel; /* current level of aborts */
EXTERN bool inputwait; /* true if in a terminal input wait */ EXTERN bool inputwait; /* true if in a terminal input wait */
EXTERN VALUE *stack; /* execution stack */ EXTERN VALUE *stack; /* execution stack */
EXTERN int dumpnames; /* true => dump names rather than indices */ EXTERN int dumpnames; /* true => dump names rather than indices */
EXTERN int calc_errno; /* global calc_errno value */
EXTERN char *calcpath; /* $CALCPATH or default */ EXTERN char *calcpath; /* $CALCPATH or default */
EXTERN char *calcrc; /* $CALCRC or default */ EXTERN char *calcrc; /* $CALCRC or default */

View File

@@ -5,19 +5,35 @@ SYNOPSIS
errno([errnum]) errno([errnum])
TYPES TYPES
errnum integer, 0 <= errnum <= 32767 errnum integer
return integer return integer
DESCRIPTION DESCRIPTION
Whenever an operation or evaluation of function returns an error-value, Whenever an operation or evaluation of function returns an error-value,
the numerical code for that value is stored as calc_errno. the numerical code for that value is stored as "global calc_errno value".
errno() returns the current value of calc_errno. The term "global calc_errno value" is an internal calc state.
This is NOT a calc variable called calc_errno.
errno(errnum) sets calc_errno to the value errnum and returns its NOTE:
previous value.
The errno() builtin should not be confused with the errno
used by libc in C. The range of errnum extend beyond the
C's libc errno. See help error for information on the mean
of various errnum ranges.
Unlike error() calling errno() does NOT increment the global
calc error count (see help errcount).
errno() returns the current value of "global calc_errno value".
Unlike error() calling errno() does NOT increment the global
calc error count (see help errcount).
errno(errnum) sets "global calc_errno value" to the value errnum
and returns its previous value. Unlike error(errnum) calling
errno(errnum) does NOT increment the global calc error count
(see help errcount).
To detect whether an error occurs during some sequence of operations, To detect whether an error occurs during some sequence of operations,
one may immediately before that sequence set the stored error-number one may immediately before that sequence set the stored error-number
@@ -34,29 +50,29 @@ DESCRIPTION
EXAMPLE EXAMPLE
; ## Assuming there is no file with name "not_a_file" ; ## Assuming there is no file with name "not_a_file"
; errno(0) ; errcount(0),; errmax(20),;
0 ; print errcount(), errmax()
; errmax(errcount()+4) 0 20
20
; badfile = fopen("not_a_file", "r") ; badfile = fopen("not_a_file", "r")
; print errno(), error(), strerror() ; print errcount(), errno(), strerror()
2 System error 2 No such file or directory 1 2 No such file or directory
; a = 1/0 ; a = 1/0
; print errno(), error(), strerror() ; print errcount(), errno(), strerror()
10001 Error 10001 Division by zero 2 10001 Division by zero
LIMITS LIMITS
none 0 <= errnum <= 32767
LINK LIBRARY LINK LIBRARY
none int calc_errno; /* global calc_errno value */
SEE ALSO SEE ALSO
errmax, errcount, error, strerror, iserror, newerror, errorcodes, errmax, errcount, error, strerror, iserror, newerror, errorcodes,
stoponerror stoponerror
## Copyright (C) 1999-2006,2021 Landon Curt Noll ## Copyright (C) 1999-2006,2021,2023 Landon Curt Noll
## ##
## Calc is open software; you can redistribute it and/or modify it under ## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License ## the terms of the version 2.1 of the GNU Lesser General Public License

View File

@@ -2,72 +2,82 @@ NAME
error - generate a value of specified error type error - generate a value of specified error type
SYNOPSIS SYNOPSIS
error([n]) error([errnum])
TYPES TYPES
n integer, defaults to errno() errnum integer, defaults to errno()
return null value or error value return null value or error value
DESCRIPTION DESCRIPTION
If n == 0: If errnum == 0:
error(n) returns the null value. error(errnum) returns the null value.
The "global calc_errno value" is set to 0. The "global calc_errno value" is set to 0.
The global calc error count is not changed. The global calc error count is not changed.
For 0 < n <= 32767: For 0 < errnum <= 32767:
error(n) returns a value that is of a special type called error(errnum) returns a value that is of a special type called
"error". The "error" return value, when given to iserror() "error". The "error" return value, when given to iserror()
will cause that function to return a true value that happens will cause that function to return a true value that happens
to match the "global calc_errno value". to match the "global calc_errno value".
The "global calc_errno value" is then set to n. The "global calc_errno value" is then set to errnum.
The global calc error count (see help errcount) is incremented. The global calc error count (see help errcount) is incremented.
If the global calc error count exceeds the maximum count If the global calc error count exceeds the maximum count
(see help errmax), any ongoing calc execution is aborted. (see help errmax), any ongoing calc execution is aborted.
All other values of n are reserved for future use and currently All other values of errnum are reserved for future use and currently
will generate an error. will generate an error.
NOTE:
Calling error() without an argument will return the current
"global calc_errno value" AND will also increment the
global calc error count.
Use errno() instead of error() to return the "global calc_errno
value" without incrementing the global calc error count.
The term "global calc_errno value" is an internal calc state. The term "global calc_errno value" is an internal calc state.
This is NOT a calc variable called calc_errno. This is NOT a calc variable called calc_errno. The value of the
"global calc_errno value" may be returned by errno() (see help errno).
The "global calc_errno value" be set to some other non-zero The "global calc_errno value" be set to some other non-zero
value by other builtin functions and calculations, especially value by other builtin functions and calculations, especially
if they result in an error. Thus, while calling errno(n) may if they result in an error. Thus, while calling errno(errnum) may
set calc_errno, some other error may later overwrite the set calc_errno, some other error may later overwrite the
"global calc_errno value". "global calc_errno value".
By convention, the "global calc_errno value" has the following ranges: By convention, the "global calc_errno value" has the following ranges:
n < 0 Reserved for future use errnum < 0 Reserved for future use
n == 0 calc_errno cleared: libc errno codes above here errnum == 0 calc_errno cleared: libc errno codes above here
1 <= n <= 9999 system error and libc errno codes 1 <= errnum <= 9999 system error and libc errno codes
n == 10000 Reserved for "No error" calc internal state errnum == 10000 Reserved for "No error" calc internal state
10001 <= n <= E__HIGHEST calc computation error codes 10001 <= errnum <= E__HIGHEST calc computation error codes
E__HIGHEST < n < 20000 Reserved for future calc error codes E__HIGHEST < errnum < 20000 Reserved for future calc error codes
20000 < n <= 32767 User defined error codes start here 20000 < errnum <= 32767 User defined error codes start here
n >= 32768 Reserved for future use errnum >= 32768 Reserved for future use
The following constants are part of the calc error code mechanism: The following constants are part of the calc error code mechanism:
E__BASE == 10000 E__BASE == 10000 # 1 less than the 1st calc computation error code
E_USERDEF == 20000 E_USERDEF == 20000 # User defined error codes start
E_USERMAX == 32767 E_USERMAX == 32767 # maximum user defined error code
E__BASE < E__HIGHEST < E_USERDEF E__BASE < E__HIGHEST < E_USERDEF # How E__HIGHEST is bounded
E__COUNT == E__HIGHEST - E__BASE E__COUNT == E__HIGHEST - E__BASE # Number of calc computation error codes
See help errorcodes for the actual values of the above constants. See help errorcodes for the actual values of the above constants.
@@ -84,8 +94,10 @@ EXAMPLE
Error 10009 Error 10009
; print iserror(a) ; print iserror(a)
10009 10009
; strerror(a) ; print strerror(a)
"Bad argument for inverse" "Bad argument for inverse"
; print errno()
10009
; b = error(0) ; b = error(0)
@@ -93,12 +105,14 @@ EXAMPLE
1 20 1 20
; print isnull(b) ; print isnull(b)
1 1
; print errno()
0
LIMITS LIMITS
0 <= n <= 32767 0 <= errnum <= 32767
LINK LIBRARY LINK LIBRARY
none int calc_errno; /* global calc_errno value */
SEE ALSO SEE ALSO
errcount, errmax, errorcodes, iserror, errno, strerror, newerror, errcount, errmax, errorcodes, iserror, errno, strerror, newerror,

View File

@@ -15,6 +15,11 @@ DESCRIPTION
that any future use of newerror(str) with the same str will return that any future use of newerror(str) with the same str will return
the same error-value. the same error-value.
When this function is called, the global calc error count (see
help errcount) is incremented. If the global calc error count
exceeds the maximum count (see help errmax), any ongoing calc
execution is aborted.
If x = newerror(str), both strerror(x) and strerror(iserro(x)) will If x = newerror(str), both strerror(x) and strerror(iserro(x)) will
return str and iserror(x) will return the error code value of the return str and iserror(x) will return the error code value of the
new error. new error.
@@ -61,7 +66,8 @@ EXAMPLE
triangle side length <= 0: 20000 triangle side length <= 0: 20000
LIMITS LIMITS
The number of new described error-values is not to exceed 12767. The number of new described error-values is not to exceed E_USERMAX-E_USERDEF
(which is usually 12767 calls to the newerror function).
LINK LIBRARY LINK LIBRARY
none none
@@ -70,7 +76,7 @@ SEE ALSO
errmax, errcount, error, strerror, iserror, errno, errorcodes, errmax, errcount, error, strerror, iserror, errno, errorcodes,
stoponerror stoponerror
## Copyright (C) 1999-2006 Landon Curt Noll ## Copyright (C) 1999-2006,2023 Landon Curt Noll
## ##
## Calc is open software; you can redistribute it and/or modify it under ## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License ## the terms of the version 2.1 of the GNU Lesser General Public License

View File

@@ -22,6 +22,9 @@ DESCRIPTION
stoponerror value. When 1 argument is given, stoponerror() returns stoponerror value. When 1 argument is given, stoponerror() returns
the previous stoponerror value. the previous stoponerror value.
See help errcount for information on the current error count.
See help errmax for the maximum error-count before execution stops.
EXAMPLE EXAMPLE
; stoponerror() ; stoponerror()
0 0
@@ -35,7 +38,7 @@ LINK LIBRARY
SEE ALSO SEE ALSO
errcount, errmax, errorcodes, iserror, errno, strerror, newerror errcount, errmax, errorcodes, iserror, errno, strerror, newerror
## Copyright (C) 2006,2021 Landon Curt Noll ## Copyright (C) 2006,2021,2023 Landon Curt Noll
## ##
## Calc is open software; you can redistribute it and/or modify it under ## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License ## the terms of the version 2.1 of the GNU Lesser General Public License

View File

@@ -2,51 +2,85 @@ NAME
strerror - returns a string describing an error value strerror - returns a string describing an error value
SYNOPSIS SYNOPSIS
strerror([x]) strerror([errnum])
TYPES TYPES
x error-value or integer in [0, 32767], defaults to errno() errnum integer
return string return string
DESCRIPTION DESCRIPTION
If x is the error-value with index n, strerror(x) and strerror(n) If 0 <= errnum <= 32767 then strerror(errnum) will return one of:
return one of:
a system-generated message, * system error and libc errno value related string
a calc-generated description, * calc computation error code string
a user-defined description created by newerror(str), * user-defined description string created by newerror(str)
the string "Error n", * "Unknown error errnum" (where errnum is in decimal)
where, in the last form, n is represented decimally. Without a argument, strerror() returns as if it were called with:
strerror(errno())
That is, the string associated with the "global calc_errno value"
is returned.
The term "global calc_errno value" is an internal calc state.
This is NOT a calc variable called calc_errno.
The system error and libc errno value related string may be
different for different systems.
For users of libcalc:
The libcalc array error_table[] contains the hard coded
calc-generated error array of E__COUNT+2 pointers to strings
starting with E__BASE, ending with E__HIGHEST and terminated
by a NULL pointer.
The error_table[0] is for errnum of E__BASE, and is the
string "No error".
The error_table[E__COUNT] is string associated with the
highest calc-generated error message.
The error_table[E__COUNT+1] is a NULL pointer.
See help error for more information on the ranges of errnum and
the meaning of the above mentioned constants.
EXAMPLE EXAMPLE
System error messages may be different for different systems. ; errcount(0),; errmax(20),;
; errmax(errcount()+3) ; print errcount(), errmax()
0 0 20
; strerror(2)
"No such file or directory"
; x = 3 * ("a" + "b") ; print strerror(2)
; print strerror(x) No such file or directory
Bad arguments for + ; print errcount(), errmax()
0 20
; a = newerror("alpha") ; x = 3 / 0;
; print strerror(a) ; print errcount(), errmax()
alpha 1 20
; print strerror(999)
Error 999
; a = 1/0
; print strerror() ; print strerror()
Division by zero Division by zero
; a = newerror("alpha")
; print errcount(), errmax()
2 20
; print strerror()
alpha
; print strerror(999)
Unknown error: 999
; print errcount(), errmax()
2 20
LIMITS LIMITS
none 0 <= errnum <= 32767
LINK LIBRARY LINK LIBRARY
none int calc_errno; /* global calc_errno value */
CONST char *error_table[E__COUNT+2]; /* calc coded error messages */
SEE ALSO SEE ALSO
strcat, strcpy, strlen, strncmp, strncpy, strpos, strcat, strcpy, strlen, strncmp, strncpy, strpos,
@@ -55,7 +89,7 @@ SEE ALSO
errcount, errmax, error, iserror, errno, newerror, errorcodes, errcount, errmax, error, iserror, errno, newerror, errorcodes,
stoponerror stoponerror
## Copyright (C) 1999-2006 Landon Curt Noll ## Copyright (C) 1999-2006,2023 Landon Curt Noll
## ##
## Calc is open software; you can redistribute it and/or modify it under ## Calc is open software; you can redistribute it and/or modify it under
## the terms of the version 2.1 of the GNU Lesser General Public License ## the terms of the version 2.1 of the GNU Lesser General Public License

View File

@@ -57,7 +57,6 @@
STATIC VALUE stackarray[MAXSTACK]; /* storage for stack */ STATIC VALUE stackarray[MAXSTACK]; /* storage for stack */
STATIC VALUE oldvalue; /* previous calculation value */ STATIC VALUE oldvalue; /* previous calculation value */
STATIC bool saveval = true; /* to enable or disable saving */ STATIC bool saveval = true; /* to enable or disable saving */
STATIC int calc_errno; /* most recent error-number */
STATIC int errcount; /* counts calls to error_value */ STATIC int errcount; /* counts calls to error_value */
STATIC bool go; STATIC bool go;
STATIC long calc_depth; STATIC long calc_depth;
@@ -65,10 +64,11 @@ STATIC long calc_depth;
/* /*
* global symbols * global symbols
*/ */
VALUE *stack; /* current location of top of stack */ VALUE *stack = NULL; /* current location of top of stack */
int dumpnames; /* names if true, otherwise indices */ int dumpnames = false; /* names if true, otherwise indices */
char *funcname; /* function being executed */ char *funcname = NULL; /* function being executed */
long funcline; /* function line being executed */ long funcline = 0; /* function line being executed */
int calc_errno = 0; /* global calc_errno value */
/* /*