Files
calc/lib_calc.c
Landon Curt Noll ff90bc0e3a add E_STRING to error, errno, strerror, change multiple E_STRING's
While help/errstr has been added, the errstr builtin function is
not yet written.  In anticipation of the new errstr builtin the
rest of the calc error system has been updated to associated errsym
E_STRING's with errnum error codes and errmsg error messages.

Minor improvements to help/rand.

The verify_error_table() function that does a verification
the error_table[] array and setup private_error_alias[] array
is now called by libcalc_call_me_first().

Fix comment about wrong include file in have_sys_mount.h.

Removed unused booltostr() and strtobool() macros from bool.h.

Moved define of math_error(char *, ...) from zmath.h to errtbl.h.
The errtbl.h include file, unless ERRCODE_SRC is defined
also includes attribute.h and errsym.h.

Group calc error related builtin support functions together in func.c.

Make switch indenting in func.c consistent.

Passing an invalid argument to error(), errno() or strerror() will
set errno AND throw a math error.  Before errno would be set and
an error value was returned.  Before there was no way to tell if
the error value was a result of the arg or if an error detected.

Added E_STRING to error([errnum | "E_STRING"]) builtin function.
Added E_STRING to errno([errnum | "E_STRING"]) builtin function.
Added E_STRING to strerror([errnum | "E_STRING"]) builtin function.
Calling these functions with an E_STRING errsym is the same as calling
them with the matching errnum code.

Standardized on calc computation error related E_STRING strings
where there are a set of related codes.  Changed "E_...digits" into
"E_..._digits".  For example, E_FPUTC1 became E_FPUTC_1, E_FPUTC2
became E_FPUTC_2, and E_FPUTC3 became E_FPUTC_3.  In a few cases
such as E_APPR became E_APPR_1, because there was a E_APPR2 (which
became E_APPR_2) and E_APPR3 (which became E_APPR_3).  To other
special cases, E_ILOG10 became E_IBASE10_LOG and E_ILOG2 became
E_IBASE2_LOG because E_ILOG10 and E_ILOG2 are both independent calc
computation error related E_STRING strings.  Now related sets of
E_STRING strings end in _ (underscore) followed by digits.

The following is the list of E_STRING strings changes:

    E_APPR ==> E_APPR_1
    E_ROUND ==> E_ROUND_1
    E_SQRT ==> E_SQRT_1
    E_ROOT ==> E_ROOT_1
    E_SHIFT ==> E_SHIFT_1
    E_SCALE ==> E_SCALE_1
    E_POWI ==> E_POWI_1
    E_POWER ==> E_POWER_1
    E_QUO ==> E_QUO_1
    E_MOD ==> E_MOD_1
    E_ABS ==> E_ABS_1
    E_APPR2 ==> E_APPR_2
    E_APPR3 ==> E_APPR_3
    E_ROUND2 ==> E_ROUND_2
    E_ROUND3 ==> E_ROUND_3
    E_BROUND2 ==> E_BROUND_2
    E_BROUND3 ==> E_BROUND_3
    E_SQRT2 ==> E_SQRT_2
    E_SQRT3 ==> E_SQRT_3
    E_ROOT2 ==> E_ROOT_2
    E_ROOT3 ==> E_ROOT_3
    E_SHIFT2 ==> E_SHIFT_2
    E_SCALE2 ==> E_SCALE_2
    E_POWI2 ==> E_POWI_2
    E_POWER2 ==> E_POWER_2
    E_POWER3 ==> E_POWER_3
    E_QUO2 ==> E_QUO_2
    E_QUO3 ==> E_QUO_3
    E_MOD2 ==> E_MOD_2
    E_MOD3 ==> E_MOD_3
    E_ABS2 ==> E_ABS_2
    E_EXP1 ==> E_EXP_1
    E_EXP2 ==> E_EXP_2
    E_FPUTC1 ==> E_FPUTC_1
    E_FPUTC2 ==> E_FPUTC_2
    E_FPUTC3 ==> E_FPUTC_3
    E_FGETC1 ==> E_FGETC_1
    E_FGETC2 ==> E_FGETC_2
    E_FOPEN1 ==> E_FOPEN_1
    E_FOPEN2 ==> E_FOPEN_2
    E_FREOPEN1 ==> E_FREOPEN_1
    E_FREOPEN2 ==> E_FREOPEN_2
    E_FREOPEN3 ==> E_FREOPEN_3
    E_FCLOSE1 ==> E_FCLOSE_1
    E_FPUTS1 ==> E_FPUTS_1
    E_FPUTS2 ==> E_FPUTS_2
    E_FPUTS3 ==> E_FPUTS_3
    E_FGETS1 ==> E_FGETS_1
    E_FGETS2 ==> E_FGETS_2
    E_FPUTSTR1 ==> E_FPUTSTR_1
    E_FPUTSTR2 ==> E_FPUTSTR_2
    E_FPUTSTR3 ==> E_FPUTSTR_3
    E_FGETSTR1 ==> E_FGETSTR_1
    E_FGETSTR2 ==> E_FGETSTR_2
    E_FGETLINE1 ==> E_FGETLINE_1
    E_FGETLINE2 ==> E_FGETLINE_2
    E_FGETFIELD1 ==> E_FGETFIELD_1
    E_FGETFIELD2 ==> E_FGETFIELD_2
    E_REWIND1 ==> E_REWIND_1
    E_PRINTF1 ==> E_PRINTF_1
    E_PRINTF2 ==> E_PRINTF_2
    E_FPRINTF1 ==> E_FPRINTF_1
    E_FPRINTF2 ==> E_FPRINTF_2
    E_FPRINTF3 ==> E_FPRINTF_3
    E_STRPRINTF1 ==> E_STRPRINTF_1
    E_STRPRINTF2 ==> E_STRPRINTF_2
    E_FSCAN1 ==> E_FSCAN_1
    E_FSCAN2 ==> E_FSCAN_2
    E_FSCANF1 ==> E_FSCANF_1
    E_FSCANF2 ==> E_FSCANF_2
    E_FSCANF3 ==> E_FSCANF_3
    E_FSCANF4 ==> E_FSCANF_4
    E_STRSCANF1 ==> E_STRSCANF_1
    E_STRSCANF2 ==> E_STRSCANF_2
    E_STRSCANF3 ==> E_STRSCANF_3
    E_STRSCANF4 ==> E_STRSCANF_4
    E_SCANF1 ==> E_SCANF_1
    E_SCANF2 ==> E_SCANF_2
    E_SCANF3 ==> E_SCANF_3
    E_FTELL1 ==> E_FTELL_1
    E_FTELL2 ==> E_FTELL_2
    E_FSEEK1 ==> E_FSEEK_1
    E_FSEEK2 ==> E_FSEEK_2
    E_FSEEK3 ==> E_FSEEK_3
    E_FSIZE1 ==> E_FSIZE_1
    E_FSIZE2 ==> E_FSIZE_2
    E_FEOF1 ==> E_FEOF_1
    E_FEOF2 ==> E_FEOF_2
    E_FERROR1 ==> E_FERROR_1
    E_FERROR2 ==> E_FERROR_2
    E_UNGETC1 ==> E_UNGETC_1
    E_UNGETC2 ==> E_UNGETC_2
    E_UNGETC3 ==> E_UNGETC_3
    E_ISATTY1 ==> E_ISATTY_1
    E_ISATTY2 ==> E_ISATTY_2
    E_ACCESS1 ==> E_ACCESS_1
    E_ACCESS2 ==> E_ACCESS_2
    E_SEARCH1 ==> E_SEARCH_1
    E_SEARCH2 ==> E_SEARCH_2
    E_SEARCH3 ==> E_SEARCH_3
    E_SEARCH4 ==> E_SEARCH_4
    E_SEARCH5 ==> E_SEARCH_5
    E_SEARCH6 ==> E_SEARCH_6
    E_RSEARCH1 ==> E_RSEARCH_1
    E_RSEARCH2 ==> E_RSEARCH_2
    E_RSEARCH3 ==> E_RSEARCH_3
    E_RSEARCH4 ==> E_RSEARCH_4
    E_RSEARCH5 ==> E_RSEARCH_5
    E_RSEARCH6 ==> E_RSEARCH_6
    E_REWIND2 ==> E_REWIND_2
    E_STRERROR1 ==> E_STRERROR_1
    E_STRERROR2 ==> E_STRERROR_2
    E_COS1 ==> E_COS_1
    E_COS2 ==> E_COS_2
    E_SIN1 ==> E_SIN_1
    E_SIN2 ==> E_SIN_2
    E_EVAL2 ==> E_EVAL_2
    E_ARG1 ==> E_ARG_1
    E_ARG2 ==> E_ARG_2
    E_POLAR1 ==> E_POLAR_1
    E_POLAR2 ==> E_POLAR_2
    E_MATFILL1 ==> E_MATFILL_1
    E_MATFILL2 ==> E_MATFILL_2
    E_MATTRANS1 ==> E_MATTRANS_1
    E_MATTRANS2 ==> E_MATTRANS_2
    E_DET1 ==> E_DET_1
    E_DET2 ==> E_DET_2
    E_DET3 ==> E_DET_3
    E_MATMIN1 ==> E_MATMIN_1
    E_MATMIN2 ==> E_MATMIN_2
    E_MATMIN3 ==> E_MATMIN_3
    E_MATMAX1 ==> E_MATMAX_1
    E_MATMAX2 ==> E_MATMAX_2
    E_MATMAX3 ==> E_MATMAX_3
    E_CP1 ==> E_CP_1
    E_CP2 ==> E_CP_2
    E_CP3 ==> E_CP_3
    E_DP1 ==> E_DP_1
    E_DP2 ==> E_DP_2
    E_DP3 ==> E_DP_3
    E_SUBSTR1 ==> E_SUBSTR_1
    E_SUBSTR2 ==> E_SUBSTR_2
    E_INSERT1 ==> E_INSERT_1
    E_INSERT2 ==> E_INSERT_2
    E_DELETE1 ==> E_DELETE_1
    E_DELETE2 ==> E_DELETE_2
    E_LN1 ==> E_LN_1
    E_LN2 ==> E_LN_2
    E_ERROR1 ==> E_ERROR_1
    E_ERROR2 ==> E_ERROR_2
    E_EVAL3 ==> E_EVAL_3
    E_EVAL4 ==> E_EVAL_4
    E_RM1 ==> E_RM_1
    E_RM2 ==> E_RM_2
    E_BLK1 ==> E_BLK_1
    E_BLK2 ==> E_BLK_2
    E_BLK3 ==> E_BLK_3
    E_BLK4 ==> E_BLK_4
    E_BLKFREE1 ==> E_BLKFREE_1
    E_BLKFREE2 ==> E_BLKFREE_2
    E_BLKFREE3 ==> E_BLKFREE_3
    E_BLKFREE4 ==> E_BLKFREE_4
    E_BLKFREE5 ==> E_BLKFREE_5
    E_BLOCKS1 ==> E_BLOCKS_1
    E_BLOCKS2 ==> E_BLOCKS_2
    E_COPY1 ==> E_COPY_01
    E_COPY2 ==> E_COPY_02
    E_COPY3 ==> E_COPY_03
    E_COPY4 ==> E_COPY_04
    E_COPY5 ==> E_COPY_05
    E_COPY6 ==> E_COPY_06
    E_COPY7 ==> E_COPY_07
    E_COPY8 ==> E_COPY_08
    E_COPY9 ==> E_COPY_09
    E_COPY10 ==> E_COPY_10
    E_COPY11 ==> E_COPY_11
    E_COPY12 ==> E_COPY_12
    E_COPY13 ==> E_COPY_13
    E_COPY14 ==> E_COPY_14
    E_COPY15 ==> E_COPY_15
    E_COPY16 ==> E_COPY_16
    E_COPY17 ==> E_COPY_17
    E_COPYF1 ==> E_COPYF_1
    E_COPYF2 ==> E_COPYF_2
    E_COPYF3 ==> E_COPYF_3
    E_COPYF4 ==> E_COPYF_4
    E_PROTECT1 ==> E_PROTECT_1
    E_PROTECT2 ==> E_PROTECT_2
    E_PROTECT3 ==> E_PROTECT_3
    E_MATFILL3 ==> E_MATFILL_3
    E_MATFILL4 ==> E_MATFILL_4
    E_MATTRACE1 ==> E_MATTRACE_1
    E_MATTRACE2 ==> E_MATTRACE_2
    E_MATTRACE3 ==> E_MATTRACE_3
    E_TAN1 ==> E_TAN_1
    E_TAN2 ==> E_TAN_2
    E_COT1 ==> E_COT_1
    E_COT2 ==> E_COT_2
    E_SEC1 ==> E_SEC_1
    E_SEC2 ==> E_SEC_2
    E_CSC1 ==> E_CSC_1
    E_CSC2 ==> E_CSC_2
    E_SINH1 ==> E_SINH_1
    E_SINH2 ==> E_SINH_2
    E_COSH1 ==> E_COSH_1
    E_COSH2 ==> E_COSH_2
    E_TANH1 ==> E_TANH_1
    E_TANH2 ==> E_TANH_2
    E_COTH1 ==> E_COTH_1
    E_COTH2 ==> E_COTH_2
    E_SECH1 ==> E_SECH_1
    E_SECH2 ==> E_SECH_2
    E_CSCH1 ==> E_CSCH_1
    E_CSCH2 ==> E_CSCH_2
    E_ASIN1 ==> E_ASIN_1
    E_ASIN2 ==> E_ASIN_2
    E_ACOS1 ==> E_ACOS_1
    E_ACOS2 ==> E_ACOS_2
    E_ATAN1 ==> E_ATAN_1
    E_ATAN2 ==> E_ATAN_2
    E_ACOT1 ==> E_ACOT_1
    E_ACOT2 ==> E_ACOT_2
    E_ASEC1 ==> E_ASEC_1
    E_ASEC2 ==> E_ASEC_2
    E_ACSC1 ==> E_ACSC_1
    E_ACSC2 ==> E_ACSC_2
    E_ASINH1 ==> E_ASINH_1
    E_ASINH2 ==> E_ASINH_2
    E_ACOSH1 ==> E_ACOSH_1
    E_ACOSH2 ==> E_ACOSH_2
    E_ATANH1 ==> E_ATANH_1
    E_ATANH2 ==> E_ATANH_2
    E_ACOTH1 ==> E_ACOTH_1
    E_ACOTH2 ==> E_ACOTH_2
    E_ASECH1 ==> E_ASECH_1
    E_ASECH2 ==> E_ASECH_2
    E_ACSCH1 ==> E_ACSCH_1
    E_ACSCH2 ==> E_ACSCH_2
    E_GD1 ==> E_GD_1
    E_GD2 ==> E_GD_2
    E_AGD1 ==> E_AGD_1
    E_AGD2 ==> E_AGD_2
    E_BIT1 ==> E_BIT_1
    E_BIT2 ==> E_BIT_2
    E_SETBIT1 ==> E_SETBIT_1
    E_SETBIT2 ==> E_SETBIT_2
    E_SETBIT3 ==> E_SETBIT_3
    E_SEG1 ==> E_SEG_1
    E_SEG2 ==> E_SEG_2
    E_SEG3 ==> E_SEG_3
    E_HIGHBIT1 ==> E_HIGHBIT_1
    E_HIGHBIT2 ==> E_HIGHBIT_2
    E_LOWBIT1 ==> E_LOWBIT_1
    E_LOWBIT2 ==> E_LOWBIT_2
    E_HEAD1 ==> E_HEAD_1
    E_HEAD2 ==> E_HEAD_2
    E_TAIL1 ==> E_TAIL_1
    E_TAIL2 ==> E_TAIL_2
    E_XOR1 ==> E_XOR_1
    E_XOR2 ==> E_XOR_2
    E_INDICES1 ==> E_INDICES_1
    E_INDICES2 ==> E_INDICES_2
    E_EXP3 ==> E_EXP_3
    E_SINH3 ==> E_SINH_3
    E_COSH3 ==> E_COSH_3
    E_SIN3 ==> E_SIN_3
    E_COS3 ==> E_COS_3
    E_GD3 ==> E_GD_3
    E_AGD3 ==> E_AGD_3
    E_POWER4 ==> E_POWER_4
    E_ROOT4 ==> E_ROOT_4
    E_DGT1 ==> E_DGT_1
    E_DGT2 ==> E_DGT_2
    E_DGT3 ==> E_DGT_3
    E_PLCS1 ==> E_PLCS_1
    E_PLCS2 ==> E_PLCS_2
    E_DGTS1 ==> E_DGTS_1
    E_DGTS2 ==> E_DGTS_2
    E_ILOG10 ==> E_IBASE10_LOG
    E_ILOG2 ==> E_IBASE2_LOG
    E_COMB1 ==> E_COMB_1
    E_COMB2 ==> E_COMB_2
    E_ASSIGN1 ==> E_ASSIGN_1
    E_ASSIGN2 ==> E_ASSIGN_2
    E_ASSIGN3 ==> E_ASSIGN_3
    E_ASSIGN4 ==> E_ASSIGN_4
    E_ASSIGN5 ==> E_ASSIGN_5
    E_ASSIGN6 ==> E_ASSIGN_6
    E_ASSIGN7 ==> E_ASSIGN_7
    E_ASSIGN8 ==> E_ASSIGN_8
    E_ASSIGN9 ==> E_ASSIGN_9
    E_SWAP1 ==> E_SWAP_1
    E_SWAP2 ==> E_SWAP_2
    E_SWAP3 ==> E_SWAP_3
    E_QUOMOD1 ==> E_QUOMOD_1
    E_QUOMOD2 ==> E_QUOMOD_2
    E_QUOMOD3 ==> E_QUOMOD_3
    E_PREINC1 ==> E_PREINC_1
    E_PREINC2 ==> E_PREINC_2
    E_PREINC3 ==> E_PREINC_3
    E_PREDEC1 ==> E_PREDEC_1
    E_PREDEC2 ==> E_PREDEC_2
    E_PREDEC3 ==> E_PREDEC_3
    E_POSTINC1 ==> E_POSTINC_1
    E_POSTINC2 ==> E_POSTINC_2
    E_POSTINC3 ==> E_POSTINC_3
    E_POSTDEC1 ==> E_POSTDEC_1
    E_POSTDEC2 ==> E_POSTDEC_2
    E_POSTDEC3 ==> E_POSTDEC_3
    E_INIT1 ==> E_INIT_01
    E_INIT2 ==> E_INIT_02
    E_INIT3 ==> E_INIT_03
    E_INIT4 ==> E_INIT_04
    E_INIT5 ==> E_INIT_05
    E_INIT6 ==> E_INIT_06
    E_INIT7 ==> E_INIT_07
    E_INIT8 ==> E_INIT_08
    E_INIT9 ==> E_INIT_09
    E_INIT10 ==> E_INIT_10
    E_LIST1 ==> E_LIST_1
    E_LIST2 ==> E_LIST_2
    E_LIST3 ==> E_LIST_3
    E_LIST4 ==> E_LIST_4
    E_LIST5 ==> E_LIST_5
    E_LIST6 ==> E_LIST_6
    E_MODIFY1 ==> E_MODIFY_1
    E_MODIFY2 ==> E_MODIFY_2
    E_MODIFY3 ==> E_MODIFY_3
    E_MODIFY4 ==> E_MODIFY_4
    E_MODIFY5 ==> E_MODIFY_5
    E_FPATHOPEN1 ==> E_FPATHOPEN_1
    E_FPATHOPEN2 ==> E_FPATHOPEN_2
    E_LOG1 ==> E_LOG_1
    E_LOG2 ==> E_LOG_2
    E_LOG3 ==> E_LOG_3
    E_FGETFILE1 ==> E_FGETFILE_1
    E_FGETFILE2 ==> E_FGETFILE_2
    E_FGETFILE3 ==> E_FGETFILE_3
    E_TAN3 ==> E_TAN_3
    E_TAN4 ==> E_TAN_4
    E_COT3 ==> E_COT_3
    E_COT4 ==> E_COT_4
    E_SEC3 ==> E_SEC_3
    E_CSC3 ==> E_CSC_3
    E_TANH3 ==> E_TANH_3
    E_TANH4 ==> E_TANH_4
    E_COTH3 ==> E_COTH_3
    E_COTH4 ==> E_COTH_4
    E_SECH3 ==> E_SECH_3
    E_CSCH3 ==> E_CSCH_3
    E_ASIN3 ==> E_ASIN_3
    E_ACOS3 ==> E_ACOS_3
    E_ASINH3 ==> E_ASINH_3
    E_ACOSH3 ==> E_ACOSH_3
    E_ATAN3 ==> E_ATAN_3
    E_ACOT3 ==> E_ACOT_3
    E_ASEC3 ==> E_ASEC_3
    E_ACSC3 ==> E_ACSC_3
    E_ATANH3 ==> E_ATANH_3
    E_ACOTH3 ==> E_ACOTH_3
    E_ASECH3 ==> E_ASECH_3
    E_ACSCH3 ==> E_ACSCH_3
    E_D2R1 ==> E_D2R_1
    E_D2R2 ==> E_D2R_2
    E_R2D1 ==> E_R2D_1
    E_R2D2 ==> E_R2D_2
    E_G2R1 ==> E_G2R_1
    E_G2R2 ==> E_G2R_2
    E_R2G1 ==> E_R2G_1
    E_R2G2 ==> E_R2G_2
    E_D2G1 ==> E_D2G_1
    E_G2D1 ==> E_G2D_1
    E_D2DMS1 ==> E_D2DMS_1
    E_D2DMS2 ==> E_D2DMS_2
    E_D2DMS3 ==> E_D2DMS_3
    E_D2DMS4 ==> E_D2DMS_4
    E_D2DM1 ==> E_D2DM_1
    E_D2DM2 ==> E_D2DM_2
    E_D2DM3 ==> E_D2DM_3
    E_D2DM4 ==> E_D2DM_4
    E_G2GMS1 ==> E_G2GMS_1
    E_G2GMS2 ==> E_G2GMS_2
    E_G2GMS3 ==> E_G2GMS_3
    E_G2GMS4 ==> E_G2GMS_4
    E_G2GM1 ==> E_G2GM_1
    E_G2GM2 ==> E_G2GM_2
    E_G2GM3 ==> E_G2GM_3
    E_G2GM4 ==> E_G2GM_4
    E_H2HMS1 ==> E_H2HMS_1
    E_H2HMS2 ==> E_H2HMS_2
    E_H2HMS3 ==> E_H2HMS_3
    E_H2HMS4 ==> E_H2HMS_4
    E_H2HM1 ==> E_H2HM_1
    E_H2HM2 ==> E_H2HM_2
    E_H2HM3 ==> E_H2HM_3
    E_H2HM4 ==> E_H2HM_4
    E_DMS2D1 ==> E_DMS2D_1
    E_DMS2D2 ==> E_DMS2D_2
    E_DM2D1 ==> E_DM2D_1
    E_DM2D2 ==> E_DM2D_2
    E_GMS2G1 ==> E_GMS2G_1
    E_GMS2G2 ==> E_GMS2G_2
    E_GM2G1 ==> E_GM2G_1
    E_GM2G2 ==> E_GM2G_2
    E_HMS2H1 ==> E_HMS2H_1
    E_HMS2H2 ==> E_HMS2H_2
    E_HM2H1 ==> E_HM2H_1
    E_HM2H2 ==> E_HM2H_2
    E_VERSIN1 ==> E_VERSIN_1
    E_VERSIN2 ==> E_VERSIN_2
    E_VERSIN3 ==> E_VERSIN_3
    E_AVERSIN1 ==> E_AVERSIN_1
    E_AVERSIN2 ==> E_AVERSIN_2
    E_AVERSIN3 ==> E_AVERSIN_3
    E_COVERSIN1 ==> E_COVERSIN_1
    E_COVERSIN2 ==> E_COVERSIN_2
    E_COVERSIN3 ==> E_COVERSIN_3
    E_ACOVERSIN1 ==> E_ACOVERSIN_1
    E_ACOVERSIN2 ==> E_ACOVERSIN_2
    E_ACOVERSIN3 ==> E_ACOVERSIN_3
    E_VERCOS1 ==> E_VERCOS_1
    E_VERCOS2 ==> E_VERCOS_2
    E_VERCOS3 ==> E_VERCOS_3
    E_AVERCOS1 ==> E_AVERCOS_1
    E_AVERCOS2 ==> E_AVERCOS_2
    E_AVERCOS3 ==> E_AVERCOS_3
    E_COVERCOS1 ==> E_COVERCOS_1
    E_COVERCOS2 ==> E_COVERCOS_2
    E_COVERCOS3 ==> E_COVERCOS_3
    E_ACOVERCOS1 ==> E_ACOVERCOS_1
    E_ACOVERCOS2 ==> E_ACOVERCOS_2
    E_ACOVERCOS3 ==> E_ACOVERCOS_3
    E_TAN5 ==> E_TAN_5
    E_COT5 ==> E_COT_5
    E_COT6 ==> E_COT_6
    E_SEC5 ==> E_SEC_5
    E_CSC5 ==> E_CSC_5
    E_CSC6 ==> E_CSC_6
2023-09-19 18:34:21 -07:00

1041 lines
24 KiB
C

/*
* lib_calc - calc link library initialization and shutdown routines
*
* Copyright (C) 1999-2007,2018,2021-2023 Landon Curt Noll
*
* 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
* as published by the Free Software Foundation.
*
* Calc is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Under source code control: 1996/06/17 18:06:19
* File existed as early as: 1996
*
* chongo <was here> /\oo/\ http://www.isthe.com/chongo/
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
#include <stdio.h>
#include <setjmp.h>
#include <signal.h>
#if !defined(_WIN32) && !defined(_WIN64)
# include <pwd.h>
#endif
#include "lib_calc.h"
#include "calc.h"
#include "zmath.h"
#include "zrandom.h"
#include "conf.h"
#include "token.h"
#include "symbol.h"
#include "func.h"
#include "strl.h"
#if defined(CUSTOM)
#include "custom.h"
#endif /* CUSTOM */
#include "have_strdup.h"
#if !defined(HAVE_STRDUP)
# define strdup(x) calc_strdup((CONST char *)(x))
#endif /* HAVE_STRDUP */
#include "have_unistd.h"
#if defined(HAVE_UNISTD_H)
#include <unistd.h>
#endif
#include "have_stdlib.h"
#if defined(HAVE_STDLIB_H)
#include <stdlib.h>
#endif
#include "terminal.h"
#if defined(USE_TERMIOS)
# include <termios.h>
typedef struct termios ttystruct;
#elif defined(USE_TERMIO)
# include <termio.h>
typedef struct termio ttystruct;
#elif defined(USE_SGTTY)
# include <sys/ioctl.h>
typedef struct sgttyb ttystruct;
#else
typedef struct {int fd;} ttystruct;
#endif
#if !defined(_WIN32) && !defined(_WIN64)
# if !defined(USE_SGTTY) && !defined (USE_TERMIOS) && !defined(USE_TERMIO)
-=*#*=- A Windows free system without termio, termios or sgtty!!! -=*#*=-
-=*#*=- We do not know how to compile for such a host, sorry!!!! -=*#*=-
# endif
#endif /* Windows */
#include "errtbl.h"
#include "banned.h" /* include after system header <> includes */
/*
* in case we do not have certain .h files
*/
#if !defined(HAVE_STDLIB_H) && !defined(HAVE_UNISTD_H)
#if !defined(HAVE_UID_T) && !defined(_UID_T)
typedef unsigned short uid_t;
#endif
E_FUNC char *getenv();
E_FUNC uid_t geteuid();
#endif
/*
* Common definitions
*/
int use_old_std = false; /* true => use old classic configuration */
int abortlevel; /* current level of aborts */
bool inputwait; /* true if in a terminal input wait */
char *program = "calc"; /* our name */
char *base_name = "calc"; /* basename of our name */
char cmdbuf[MAXCMD+1+1+1]; /* command line expression + "\n\0" + guard */
run run_state = RUN_ZERO; /* calc startup run state */
char *script_name = NULL; /* program name or -f filename arg or NULL */
/*
* global permissions
*/
int allow_read = true; /* false => may not open any files for reading */
int allow_write = true; /* false => may not open any files for writing */
int allow_exec = true; /* false => may not execute any commands */
/*
* global flags
*/
int p_flag = false; /* true => pipe mode */
int q_flag = false; /* true => don't execute rc files */
int u_flag = false; /* true => unbuffer stdin and stdout */
int d_flag = false; /* true => disable heading, resource_debug == 0 */
int c_flag = false; /* true => continue on error if permitted */
int i_flag = false; /* true => go interactive if permitted */
int s_flag = false; /* true => keep args as strings for argv() */
/*
* global values
*/
char *calcpath = NULL; /* $CALCPATH or default */
char *calcrc = NULL; /* $CALCRC or default */
char *calcbindings = NULL; /* $CALCBINDINGS or default */
char *home = NULL; /* $HOME or default */
char *pager = NULL; /* $PAGER or default */
char *shell = NULL; /* $SHELL or default */
char *calc_history = NULL; /* $CALCHISTFILE or ~/.calc_history */
char *calc_helpdir = NULL; /* $CALCHELP or /usr/local/share/calc/help */
/* $CALCCUSTOMHELP or /usr/local/share/calc/custhelp */
char *calc_customhelpdir = NULL;
int stdin_tty = false; /* true if stdin is a tty */
int havecommands = false; /* true if have one or more cmd args */
long stoponerror = 0; /* >0 => stop, <0 => continue, ==0 => use -c */
bool abort_now = false; /* true => go interactive now, if permitted */
/* non-zero => calc_scanerr_jmpbuf ready */
int calc_use_scanerr_jmpbuf = 0;
/* for scan and parse errors */
jmp_buf calc_scanerr_jmpbuf;
/* non-zero => use calc_use_matherr_jmpbuf */
int calc_use_matherr_jmpbuf = 0;
/* math_error() control jump point when calc_use_matherr_jmpbuf != 0 */
jmp_buf calc_matherr_jmpbuf;
/* last calc error message, also parse/scan errors use this buffer */
char calc_err_msg[MAXERROR+1];
/* 0 ==> do not print parse/scan errors */
int calc_print_scanerr_msg = 1;
/* last parse/scan warning message */
char calc_warn_msg[MAXERROR+1];
/* 0 ==> do not print parse/scan warnings */
int calc_print_scanwarn_msg = 1;
/* number of parse/scan warnings found */
unsigned long calc_warn_cnt = 0;
int argc_value = 0; /* count of argv[] strings for argv() builtin */
char **argv_value = NULL; /* argv[] strings for argv() builtin */
int no_env = false; /* true (-e) => ignore env vars on startup */
long errmax = ERRMAX; /* if >= 0, maximum value for errcount */
NUMBER *epsilon_default; /* default allowed error for float calcs */
char *calc_debug = NULL; /* !=NULL => value of config("calc_debug") */
char *resource_debug = NULL; /* !=NULL => config("resource_debug") value */
char *user_debug = NULL; /* !=NULL => value of config("user_debug") */
/*
* initialization functions
*/
E_FUNC void math_setfp(FILE *fp);
E_FUNC void file_init(void);
E_FUNC void zio_init(void);
E_FUNC void initialize(void);
E_FUNC void reinitialize(void);
/*
* static declarations
*/
STATIC int init_done = 0; /* 1 => we already initialized */
STATIC int *fd_setup = NULL; /* fd's setup for interaction or -1 */
STATIC int fd_setup_len = 0; /* number of fd's in fd_setup */
STATIC ttystruct *fd_orig = NULL; /* fd original state */
STATIC ttystruct *fd_cur = NULL; /* fd current state */
S_FUNC void initenv(void); /* setup calc environment */
S_FUNC int find_tty_state(int fd); /* find slot for saved tty state */
STATIC bool initialized = false; /* true => initialize() has been run */
/*
* libcalc_call_me_first - users of libcalc.a must call this function first!
*
* Anything that uses libcalc.a MUST call this function first before doing
* any other libcalc.a processing.
*/
void
libcalc_call_me_first(void)
{
char *p;
/*
* do nothing if we are initialized already
*/
if (init_done) {
return;
}
/*
* Disable SIGPIPE so that the pipe to the help file pager will
* not stop calc.
*/
#if !defined(_WIN32) && !defined(_WIN64)
(void) signal(SIGPIPE, SIG_IGN);
#endif
/*
* determine the basename
*/
if (program != NULL) {
p = strrchr(program, '/');
#if defined(_WIN32) || defined(_WIN64)
if (p == NULL) {
p = strrchr(program, '\\');
}
#endif
if (p == NULL) {
base_name = program;
} else {
base_name = p+1;
}
}
/*
* initialize old and new configuration values
*/
newstd.epsilon = &_qonesqbase_; /* magic to fake early str2q() */
if (program == NULL) {
fprintf(stderr, "libcalc_call_me_first: FATAL: "
"program is NULL\n");
exit(80);
}
newstd.program = strdup(program);
if (newstd.program == NULL) {
fprintf(stderr, "libcalc_call_me_first: FATAL: "
"cannot strdup program string\n");
exit(81);
}
if (base_name == NULL) {
fprintf(stderr, "libcalc_call_me_first: FATAL: "
"base_name is NULL\n");
exit(82);
}
newstd.base_name = strdup(base_name);
if (newstd.base_name == NULL) {
fprintf(stderr, "libcalc_call_me_first: FATAL: "
"cannot strdup base_name string\n");
exit(83);
}
p = version();
if (p == NULL) {
fprintf(stderr, "libcalc_call_me_first: FATAL: "
"version() returned NULL\n");
exit(84);
}
newstd.version = strdup(p);
if (newstd.version == NULL) {
fprintf(stderr, "libcalc_call_me_first: FATAL: "
"cannot strdup return from version()\n");
exit(85);
}
conf = config_copy(&newstd); /* more magic to fake early str2q() */
conf->tab_ok = false;
newstd.epsilon = str2q(EPSILON_DEFAULT);
oldstd.epsilon = str2q(EPSILON_DEFAULT);
/*
* make newstd our default config, unless -O
*/
config_free(conf);
if (use_old_std) {
conf = config_copy(&oldstd);
} else {
conf = config_copy(&newstd);
}
/*
* -d turns off resource_debug and tilde
*/
if (d_flag) {
conf->resource_debug = 0;
conf->tilde_ok = 0;
conf->tilde_space = 0;
}
/*
* -p turns off tab
*/
if (p_flag) {
conf->tab_ok = 0;
}
/*
* -D flags can change calc_debug, resource_debug of user_debug
*/
if (calc_debug) {
conf->calc_debug = strtol(calc_debug, NULL, 0);
}
if (resource_debug) {
conf->resource_debug = strtol(resource_debug, NULL, 0);
}
if (user_debug) {
conf->user_debug = strtol(user_debug, NULL, 0);
}
/*
* initialize
*/
initialize();
/*
* verify error_table[] array and setup private_error_alias[] array
*/
verify_error_table();
/*
* ready to rock & roll ..
*/
if (conf->calc_debug & CALCDBG_RUNSTATE) {
printf("libcalc_call_me_first: run_state from %s to %s\n",
run_state_name(run_state),
run_state_name(RUN_BEGIN));
}
run_state = RUN_BEGIN;
init_done = 1;
return;
}
/*
* initialize - perform the required calc initializations
*/
void
initialize(void)
{
/*
* firewall
*/
if (initialized) {
return;
}
/*
* ZVALUE io initialization
*/
zio_init();
/*
* process the environment
*/
initenv();
/*
* initialize I/O
*/
file_init();
/*
* initialize file I/O
*/
resetinput();
/*
* initialize calc internal data structures
*/
inittokens();
initglobals();
initfunctions();
initstack();
/*
* initialize calc math
*/
math_cleardiversions();
math_setfp(stdout);
math_setmode(MODE_INITIAL);
math_setdigits(DISPLAY_DEFAULT);
conf->maxprint = MAXPRINT_DEFAULT;
#if defined(CUSTOM)
/*
* initialize custom registers
*/
if (allow_custom) {
init_custreg();
}
#endif /* CUSTOM */
/*
* note that we are done
*/
initialized = true;
}
/*
* reinitialize - reinitialize after a longjmp
*/
void
reinitialize(void)
{
/*
* process commands (from stdin, not the command line)
*/
abortlevel = 0;
_math_abort_ = false;
inputwait = false;
math_cleardiversions();
math_setfp(stdout);
resetscopes();
resetinput();
inittokens();
(void) openterminal();
}
/*
* cvmalloc_error - for users of the SGI Workshop Debugger
*
* usage:
* message - error message passed along via libmalloc_cv.a
*/
void
cvmalloc_error(char *message)
{
/* firewall */
if (message == NULL) {
fprintf(stderr, "cvmalloc_error message is NULL\n");
return;
}
/* print message and return */
fprintf(stderr, "cvmalloc_error: %s\n", message);
return;
}
/*
* initenv - obtain $CALCPATH, $CALCRC, $CALCBINDINGS, $HOME, $PAGER
* and $SHELL values
*
* If $CALCPATH, $CALCRC, $CALCBINDINGS, $PAGER or $SHELL do not exist,
* use the default values. If $PAGER or $SHELL is an empty string, also
* use a default value. If $HOME does not exist, or is empty, use the home
* directory information from the password file.
*/
S_FUNC void
initenv(void)
{
#if !defined(_WIN32) && !defined(_WIN64)
struct passwd *ent; /* our password entry */
#endif
char *c;
/* determine the $CALCPATH value */
c = (no_env ? NULL : getenv(CALCPATH));
calcpath = (c ? strdup(c) : NULL);
if (calcpath == NULL)
calcpath = DEFAULTCALCPATH;
/* determine the $CALCRC value */
c = (no_env ? NULL : getenv(CALCRC));
calcrc = (c ? strdup(c) : NULL);
if (calcrc == NULL)
calcrc = DEFAULTCALCRC;
if (strlen(calcrc) > MAX_CALCRC) {
math_error("The $CALCRC variable is longer than %d chars",
MAX_CALCRC);
not_reached();
}
/* determine the $CALCBINDINGS value */
c = (no_env ? NULL : getenv(CALCBINDINGS));
calcbindings = (c ? strdup(c) : NULL);
if (calcbindings == NULL)
calcbindings = DEFAULTCALCBINDINGS;
/* determine the $HOME value */
c = (no_env ? NULL : getenv(HOME));
home = (c ? strdup(c) : NULL);
#if defined(_WIN32) || defined(_WIN64)
if (home == NULL || home[0] == '\0') {
/* free home if it was previously allocated, but empty */
if (home != NULL) {
free(home);
}
/* just assume . is home if all else fails */
home = strdup(".");
}
#else /* Windows free systems */
if (home == NULL || home[0] == '\0') {
/* free home if it was previously allocated, but empty */
if (home != NULL) {
free(home);
}
/* try using the home directory of current effective UID */
ent = (struct passwd *)getpwuid(geteuid());
if (ent == NULL || ent->pw_dir == NULL ||
ent->pw_dir[0] == '\0') {
/* just assume . is home if all else fails */
home = strdup(".");
} else {
/* use home directory of current effective UID */
home = strdup(ent->pw_dir);
}
}
#endif /* Windows free systems */
/* paranoia */
if (home == NULL) {
math_error("Unable to allocate string for $HOME");
not_reached();
}
/* determine the $PAGER value */
c = (no_env ? NULL : getenv(PAGER));
pager = (c ? strdup(c) : NULL);
if (pager == NULL || *pager == '\0')
pager = DEFAULTCALCPAGER;
/* determine the $SHELL value */
c = (no_env ? NULL : getenv(SHELL));
shell = (c ? strdup(c) : NULL);
if (shell == NULL || *shell == '\0')
shell = DEFAULTSHELL;
/* determine the $CALCHISTFILE value */
c = (no_env ? NULL : getenv(CALCHISTFILE));
calc_history = (c ? strdup(c) : NULL);
if (calc_history == NULL || *calc_history == '\0')
calc_history = NULL; /* will use ~/.calc_history */
/* determine the $CALCHELP value */
c = (no_env ? NULL : getenv(CALCHELP));
calc_helpdir = (c ? strdup(c) : NULL);
if (calc_helpdir == NULL || *calc_helpdir == '\0') {
/* will use /usr/local/share/calc/help */
calc_helpdir = HELPDIR;
}
#if defined(CUSTOM)
/* determine the $CALCCUSTOMHELP value */
c = (no_env ? NULL : getenv(CALCCUSTOMHELP));
calc_customhelpdir = (c ? strdup(c) : NULL);
if (calc_customhelpdir == NULL || *calc_customhelpdir == '\0') {
/* will use /usr/local/share/calc/custhelp */
calc_customhelpdir = CUSTOMHELPDIR;
}
#endif /* CUSTOM */
}
/*
* libcalc_call_me_last - users of libcalc.a can call this function when done
*
* Anything that uses libcalc.a can call this function after they are
* completely finished with libcalc.a processing. The only effect of
* this function is to free storage that might otherwise go unused.
*
* NOTE: If, for any reason, you need to do more libcalc.a processing,
* then you will need to call libcalc_call_me_first() again.
*/
void
libcalc_call_me_last(void)
{
int i;
/*
* firewall
*/
if (init_done == 0) {
return;
}
/*
* free the configuration
*/
config_free(conf);
/*
* free Blum generator state
*/
random_libcalc_cleanup();
/*
* free script_name
*/
if (script_name != NULL) {
free(script_name);
script_name = NULL;
}
/*
* restore all changed descriptor states
*/
for (i=0; i < fd_setup_len; ++i) {
if (fd_setup[i] >= 0) {
if (conf->calc_debug & CALCDBG_TTY)
printf("libcalc_call_me_last: fd %d "
"not in original state, "
"restoring it", fd_setup[i]);
orig_tty(fd_setup[i]);
}
}
/*
* free all globals
*/
freeglobals();
/*
* all done
*/
init_done = 0;
return;
}
/*
* run_state_name - return a constant string given a run_state
*/
char *
run_state_name(run state)
{
switch (state) {
case RUN_ZERO:
return "ZERO";
case RUN_BEGIN:
return "BEGIN";
case RUN_RCFILES:
return "RCFILES";
case RUN_PRE_CMD_ARGS:
return "PRE_CMD_ARGS";
case RUN_CMD_ARGS:
return "CMD_ARGS";
case RUN_PRE_TOP_LEVEL:
return "PRE_TOP_LEVEL";
case RUN_TOP_LEVEL:
return "TOP_LEVEL";
case RUN_EXIT:
return "EXIT";
case RUN_EXIT_WITH_ERROR:
return "EXIT_WITH_ERROR";
}
return "RUN_invalid";
}
/*
* calc_strdup - calc interface to provide or simulate strdup()
*/
char *
calc_strdup(CONST char *s1)
{
#if defined(HAVE_STRDUP)
return strdup(s1);
#else /* HAVE_STRDUP */
char *ret; /* return string */
size_t s1_len; /* length of string to duplicate */
/*
* firewall
*/
if (s1 == NULL) {
return NULL;
}
/*
* allocate duplicate storage
*/
s1_len = strlen(s1);
ret = (char *)malloc(s1_len+1);
/*
* if we have storage, duplicate the string
*/
if (ret != NULL) {
strlcpy(ret, s1, s1_len+1);
}
/*
* return the new string, or NULL if malloc failed
*/
return ret;
#endif /* HAVE_STRDUP */
}
/*
* find_tty_state - establish a new tty state
*
* Given:
* fd file descriptor to establish a new tty state for
*
* Returns:
* indx The index into fd_setup[], fd_orig[] and fd_cur[] to use or -1
*/
S_FUNC int
find_tty_state(int fd)
{
int *new_fd_setup; /* new fd_setup array */
ttystruct *new_fd_orig; /* new fd_orig array */
ttystruct *new_fd_cur; /* new fd_cur array */
int i;
/*
* firewall: must be > 0
*/
if (fd < 0) {
/* bad descriptor */
return -1;
}
/*
* case: need to initially malloc some state
*/
if (fd_setup_len <= 0 || fd_setup == NULL || fd_orig == NULL) {
/* setup for a single descriptor */
fd_setup = (int *)malloc(sizeof(fd_setup[0]));
if (fd_setup == NULL) {
return -1;
}
fd_setup[0] = -1;
fd_orig = (ttystruct *)malloc(sizeof(fd_orig[0]));
if (fd_orig == NULL) {
return -1;
}
fd_cur = (ttystruct *)malloc(sizeof(fd_cur[0]));
if (fd_cur == NULL) {
return -1;
}
fd_setup_len = 1;
}
/*
* look for an existing tty state for the descriptor
*/
for (i=0; i < fd_setup_len; ++i) {
/* case: found existing tty state, return index */
if (fd_setup[i] == fd) {
return i;
}
}
/*
* no tty state exists for the descriptor, look for empty slot
*/
for (i=0; i < fd_setup_len; ++i) {
/* case: found an empty slot, so return it */
if (fd_setup[i] < 0) {
return i;
}
}
/*
* no empty slots exist, realloc another slot
*/
/* expand fd_orig as an original pre-modified copy of fd_setup */
new_fd_orig = (ttystruct *)realloc(fd_orig, sizeof(fd_orig[0]) *
(fd_setup_len+1));
if (new_fd_orig == NULL) {
return -1;
}
fd_orig = new_fd_orig;
memcpy(fd_orig, fd_setup, sizeof(fd_orig[0]) * (fd_setup_len+1));
/* expand fd_setup */
new_fd_setup = (int *)realloc(fd_setup, sizeof(fd_setup[0]) *
(fd_setup_len+1));
if (new_fd_setup == NULL) {
return -1;
}
fd_setup = new_fd_setup;
new_fd_setup[fd_setup_len] = -1;
/* expand fd_cur */
new_fd_cur = (ttystruct *)realloc(fd_cur, sizeof(fd_cur[0]) *
(fd_setup_len+1));
if (new_fd_cur == NULL) {
return -1;
}
fd_cur = new_fd_cur;
/* expand fd setup length */
++fd_setup_len;
/* return the new slot */
return fd_setup_len-1;
}
/*
* calc_tty - setup a file descriptor for calc's interactive use
*
* Calc wants, in effect, cbreak on and echo off.
*
* Given:
* fd the descriptor for calc's interactive use
*
* Returns:
* true state change was successful
* false unable to change state of descriptor for interactive use
*/
bool
calc_tty(int fd)
{
int slot; /* the saved descriptor slot or -1 */
/*
* grab the saved slot for this descriptor
*/
slot = find_tty_state(fd);
if (slot < 0) {
if (conf->calc_debug & CALCDBG_TTY)
printf("calc_tty: Cannot get saved descriptor slot\n");
return false;
}
#if defined(USE_SGTTY)
/*
* USE_SGTTY tty state method
*/
/* save original state if needed */
if (fd_setup[slot] < 0 && ioctl(fd, TIOCGETP, fd_orig+slot) < 0) {
if (conf->calc_debug & CALCDBG_TTY)
printf("calc_tty: Cannot TIOCGETP fd %d\n", fd);
return false;
}
/* setup for new state */
fd_cur[slot] = fd_orig[slot];
fd_cur[slot].sg_flags &= ~ECHO;
fd_cur[slot].sg_flags |= CBREAK;
/* enable new state */
if (ioctl(fd, TIOCSETP, fd_cur+slot) < 0) {
if (conf->calc_debug & CALCDBG_TTY)
printf("calc_tty: Cannot TIOCSETP fd %d\n", fd);
return false;
}
if (conf->calc_debug & CALCDBG_TTY)
printf("calc_tty: stty -ECHO +CBREAK: fd %d\n", fd);
#elif defined(USE_TERMIO)
/*
* USE_TERMIO tty state method
*/
if (fd_setup[slot] < 0 && ioctl(fd, TCGETA, fd_orig+slot) < 0) {
if (conf->calc_debug & CALCDBG_TTY)
printf("calc_tty: Cannot TCGETA fd %d\n", fd);
return false;
}
/* setup for new state */
fd_cur[slot] = fd_orig[slot];
fd_cur[slot].c_lflag &= ~(ECHO | ECHOE | ECHOK);
fd_cur[slot].c_iflag |= ISTRIP;
fd_cur[slot].c_lflag &= ~ICANON;
fd_cur[slot].c_cc[VMIN] = 1;
fd_cur[slot].c_cc[VTIME] = 0;
/* enable new state */
if (ioctl(fd, TCSETAW, fd_cur+slot) < 0) {
if (conf->calc_debug & CALCDBG_TTY)
printf("calc_tty: Cannot TCSETAW fd %d\n", fd);
return false;
}
if (conf->calc_debug & CALCDBG_TTY)
printf("calc_tty: stty -ECHO -ECHOE -ECHOK -ICANON +ISTRIP "
"VMIN=1 VTIME=0: fd %d\n", fd);
#elif defined (USE_TERMIOS)
/*
* USE_TERMIOS tty state method
*/
if (fd_setup[slot] < 0 && tcgetattr(fd, fd_orig+slot) < 0) {
if (conf->calc_debug & CALCDBG_TTY)
printf("calc_tty: Cannot tcgetattr fd %d\n", fd);
return false;
}
/* setup for new state */
fd_cur[slot] = fd_orig[slot];
fd_cur[slot].c_lflag &= ~(ECHO | ECHOE | ECHOK);
fd_cur[slot].c_iflag |= ISTRIP;
fd_cur[slot].c_lflag &= ~ICANON;
fd_cur[slot].c_cc[VMIN] = 1;
fd_cur[slot].c_cc[VTIME] = 0;
/* enable new state */
if (tcsetattr(fd, TCSANOW, fd_cur+slot) < 0) {
if (conf->calc_debug & CALCDBG_TTY)
printf("calc_tty: Cannot tcsetattr fd %d\n", fd);
return false;
}
if (conf->calc_debug & CALCDBG_TTY)
printf("calc_tty: stty -ECHO -ECHOE -ECHOK -ICANON +ISTRIP "
"VMIN=1 VTIME=0: fd %d\n", fd);
#else /* Using none of the above */
fd_cur[slot] = fd_orig[slot];
#endif
/*
* note that the tty slot is on use
*/
fd_setup[slot] = fd;
return true;
}
/*
* orig_tty - restore the original state of a file descriptor
*
* This routine will restore the state of a descriptor to its calc
* startup value if it was set for interactive use by calc_tty().
*
* Given:
* fd the descriptor to restore
*
* Returns:
* true restore was successful
* false unable to restore descriptor to original state
*/
bool
orig_tty(int fd)
{
int slot; /* the saved descriptor slot or -1 */
/*
* find the saved slot for this descriptor
*/
slot = find_tty_state(fd);
if (slot < 0) {
if (conf->calc_debug & CALCDBG_TTY)
printf("orig_tty: Cannot get saved descriptor slot\n");
return false;
}
/*
* do nothing if no state was saved for this descriptor
*/
if (fd_setup[slot] < 0) {
if (conf->calc_debug & CALCDBG_TTY)
printf("orig_tty: no state saved for fd %d\n", fd);
return false;
}
#if defined(USE_SGTTY)
/*
* USE_SGTTY tty state method
*/
(void) ioctl(fd, TIOCSETP, fd_orig+slot);
if (conf->calc_debug & CALCDBG_TTY)
printf("orig_tty: TIOCSETP restored fd %d\n", fd);
#elif defined(USE_TERMIO)
/*
* USE_TERMIO tty state method
*/
(void) ioctl(fd, TCSETAW, fd_orig+slot);
if (conf->calc_debug & CALCDBG_TTY)
printf("orig_tty: TCSETAW restored fd %d\n", fd);
#elif defined (USE_TERMIOS)
/*
* assume USE_SGTTY tty state method
*/
(void) tcsetattr(fd, TCSANOW, fd_orig+slot);
if (conf->calc_debug & CALCDBG_TTY)
printf("orig_tty: TCSANOW restored fd %d\n", fd);
#else /* nothing assigned */
if (conf->calc_debug & CALCDBG_TTY)
printf ("orig_tty: nothing restored to fd %d\n", fd);
#endif
/*
* note new current state
*/
fd_cur[slot] = fd_orig[slot];
/*
* Since current state is the original state, we can free up
* this slot. This also prevents functions such as the
* libcalc_call_me_last() function from re-restoring it.
*/
fd_setup[slot] = -1;
return true;
}