Files
calc/str.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

1584 lines
31 KiB
C

/*
* str - string list routines
*
* Copyright (C) 1999-2007,2021-2023 David I. Bell and Ernest Bowen
*
* Primary author: David I. Bell
*
* 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: 1990/02/15 01:48:10
* File existed as early as: before 1990
*
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
#include <stdio.h>
#include <ctype.h>
#include "have_string.h"
#ifdef HAVE_STRING_H
# include <string.h>
#endif
#include "calc.h"
#include "alloc.h"
#include "str.h"
#include "strl.h"
#include "errtbl.h"
#include "banned.h" /* include after system header <> includes */
#define STR_TABLECHUNK (1<<10) /* how often to reallocate string table */
#define STR_CHUNK (1<<16) /* size of string storage allocation */
#define OCTET_VALUES 256 /* number of different values in a OCTET */
#define STR_UNIQUE (1<<7) /* size of string to allocate separately */
STRING _nullstring_ = {"", 0, 1, NULL};
STATIC char *chartable; /* single character string table */
STATIC struct {
long l_count; /* count of strings in table */
long l_maxcount; /* maximum strings storable in table */
size_t l_avail; /* characters available in current string */
char *l_alloc; /* next available string storage */
char **l_table; /* current string table */
} literals;
/*
* Initialize or reinitialize a string header for use.
*
* given:
* hp structure to be inited
*/
void
initstr(STRINGHEAD *hp)
{
if (hp->h_list == NULL) {
/* alloc + 1 guard paranoia */
hp->h_list = (char *)malloc(STR_CHUNK + 1);
if (hp->h_list == NULL) {
math_error("Cannot allocate string header");
not_reached();
}
hp->h_list[STR_CHUNK] = '\0'; /* guard paranoia */
hp->h_avail = STR_CHUNK;
hp->h_used = 0;
}
hp->h_avail += hp->h_used;
hp->h_used = 0;
hp->h_count = 0;
hp->h_list[0] = '\0';
hp->h_list[1] = '\0';
}
/*
* Copy a string to the end of a list of strings, and return the address
* of the copied string. Returns NULL if the string could not be copied.
* No checks are made to see if the string is already in the list.
* The string cannot be null or have embedded nulls.
*
* given:
* hp header of string storage
* str string to be added
*/
char *
addstr(STRINGHEAD *hp, char *str)
{
char *retstr; /* returned string pointer */
char *list; /* string list */
long newsize; /* new size of string list */
size_t len; /* length of current string */
if ((str == NULL) || (*str == '\0'))
return NULL;
len = strlen(str) + 1;
if (len >= hp->h_avail) {
/* alloc + 1 guard paranoia */
newsize = len + STR_CHUNK + hp->h_used + hp->h_avail + 1;
/* alloc + 1 guard paranoia */
list = (char *)realloc(hp->h_list, newsize + 1);
if (list == NULL)
return NULL;
list[newsize] = '\0'; /* guard paranoia */
hp->h_list = list;
hp->h_avail = newsize - hp->h_used;
}
retstr = hp->h_list + hp->h_used;
hp->h_used += len;
hp->h_avail -= len;
hp->h_count++;
strlcpy(retstr, str, len);
retstr[len] = '\0';
return retstr;
}
/*
* Return a null-terminated string which consists of a single character.
* The table is initialized on the first call.
*/
char *
charstr(int ch)
{
char *cp;
int i;
if (chartable == NULL) {
/* alloc + 1 guard paranoia */
cp = (char *)malloc((OCTET_VALUES + 1)*2);
if (cp == NULL) {
math_error("Cannot allocate character table");
not_reached();
}
for (i = 0; i < OCTET_VALUES; i++) {
*cp++ = (char)i;
*cp++ = '\0';
}
chartable = cp - (OCTET_VALUES*2);
*cp++ = '\0'; /* guard paranoia */
*cp++ = '\0'; /* guard paranoia */
}
return &chartable[(ch & 0xff) * 2];
}
/*
* Find a string with the specified name and return its number in the
* string list. The first string is numbered zero. Minus one is returned
* if the string is not found.
*
* given:
* hp header of string storage
* str string to be added
*/
int
findstr(STRINGHEAD *hp, char *str)
{
register char *test; /* string being tested */
size_t len; /* length of string being found */
size_t testlen; /* length of test string */
int index; /* index of string */
if ((hp->h_count <= 0) || (str == NULL))
return -1;
len = strlen(str);
test = hp->h_list;
index = 0;
while (*test) {
testlen = strlen(test);
if ((testlen == len) && (*test == *str) &&
(strcmp(test, str) == 0))
return index;
test += (testlen + 1);
index++;
}
return -1;
}
/*
* Return the name of a string with the given index.
* If the index is illegal, a pointer to an empty string is returned.
*
* given:
* hp header of string storage
* n string index
*/
char *
namestr(STRINGHEAD *hp, long n)
{
register char *str; /* current string */
if (n >= hp->h_count)
return "";
str = hp->h_list;
while (*str) {
if (--n < 0)
return str;
str += (strlen(str) + 1);
}
return "";
}
/*
* Useful routine to return the index of one string within another one
* which has the format: "str1\000str2\000str3\000...strn\0\0". Index starts
* at one for the first string. Returns zero if the string being checked
* is not contained in the formatted string.
*
* Be sure to use \000 instead of \0. ANSI-C compilers interpret "foo\0foo..."
* as "foo\017oo...".
*
* given:
* format string formatted into substrings
* test string to be found in formatted string
*/
long
stringindex(char *format, char *test)
{
long index; /* found index */
size_t len; /* length of current piece of string */
size_t testlen; /* length of test string */
testlen = strlen(test);
index = 1;
while (*format) {
len = strlen(format);
if ((len == testlen) && (*format == *test) &&
(strcmp(format, test) == 0))
return index;
format += (len + 1);
index++;
}
return 0;
}
/*
* Add a possibly new literal string to the literal string pool.
* Returns the new string address which is guaranteed to be always valid.
* Duplicate strings will repeatedly return the same address.
*/
char *
addliteral(char *str)
{
register char **table; /* table of strings */
char *newstr; /* newly allocated string */
long count; /* number of strings */
size_t len; /* length of string to allocate */
len = strlen(str);
if (len <= 1)
return charstr(*str);
/*
* See if the string is already in the table.
*/
table = literals.l_table;
count = literals.l_count;
while (count-- > 0) {
if ((str[0] == table[0][0]) && (str[1] == table[0][1]) &&
(strcmp(str, table[0]) == 0))
return table[0];
table++;
}
/*
* Make the table of string pointers larger if necessary.
*/
if (literals.l_count >= literals.l_maxcount) {
count = literals.l_maxcount + STR_TABLECHUNK;
if (literals.l_maxcount) {
/* alloc + 1 guard paranoia */
table = (char **) realloc(literals.l_table,
(count + 1) * sizeof(char *));
table[count] = NULL; /* guard paranoia */
} else {
/* alloc + 1 guard paranoia */
table = (char **) malloc((count + 1) * sizeof(char *));
table[count] = NULL; /* guard paranoia */
}
if (table == NULL) {
math_error("Cannot allocate string literal table");
not_reached();
}
literals.l_table = table;
literals.l_maxcount = count;
}
table = literals.l_table;
/*
* If the new string is very long, allocate it manually.
*
* Add room for trailing NUL and then round up to a
* memory chunk (in this case we pick the size of a FULL
* just because that is a nice size) for extra padded room.
*/
len = ROUNDUP(len+1, FULL_LEN);
if (len >= STR_UNIQUE) {
/* alloc + 1 guard paranoia */
newstr = (char *)malloc(len + 1);
if (newstr == NULL) {
math_error("Cannot allocate large literal string");
not_reached();
}
newstr[len] = '\0'; /* guard paranoia */
strlcpy(newstr, str, len);
table[literals.l_count++] = newstr;
return newstr;
}
/*
* If the remaining space in the allocate string is too small,
* then allocate a new one.
*/
if (literals.l_avail < len) {
/* alloc + 1 guard paranoia */
newstr = (char *)malloc(STR_CHUNK + 1);
if (newstr == NULL) {
math_error("Cannot allocate new literal string");
not_reached();
}
newstr[STR_CHUNK] = '\0'; /* guard paranoia */
literals.l_alloc = newstr;
literals.l_avail = STR_CHUNK;
}
/*
* Allocate the new string from the allocate string.
*/
newstr = literals.l_alloc;
literals.l_avail -= len;
literals.l_alloc += len;
table[literals.l_count++] = newstr;
strlcpy(newstr, str, len);
return newstr;
}
STRING *
stringadd(STRING *s1, STRING *s2)
{
STRING *s;
char *cfrom, *c;
long len;
len = s1->s_len + s2->s_len;
s = stralloc();
s->s_len = len;
s->s_str = (char *) malloc(len + 1);
if (s->s_str == NULL)
return NULL;
len = s1->s_len;
cfrom = s1->s_str;
c = s->s_str;
while (len-- > 0)
*c++ = *cfrom++;
len = s2->s_len;
cfrom = s2->s_str;
while (len-- > 0)
*c++ = *cfrom++;
*c = '\0';
return s;
}
/*
* stringneg reverses the characters in a string, returns null if malloc fails
*/
STRING *
stringneg(STRING *str)
{
long len;
STRING *s;
char *c, *cfrom;
len = str->s_len;
if (len <= 1)
return slink(str);
c = (char *) malloc(len + 1);
if (c == NULL)
return NULL;
s = stralloc();
s->s_len = len;
s->s_str = c;
cfrom = str->s_str + len;
while (len-- > 0)
*c++ = *--cfrom;
*c = '\0';
return s;
}
STRING *
stringsub(STRING *s1, STRING *s2)
{
STRING *tmp, *s;
tmp = stringneg(s2);
if (tmp == NULL)
return NULL;
s = stringadd(s1, tmp);
if (s != NULL)
sfree(tmp);
return s;
}
/*
* stringmul: repeated concatenation, reverse if negative multiplier
* returns null if malloc fails
*/
STRING *
stringmul(NUMBER *q, STRING *str)
{
long len;
size_t j;
NUMBER *tmp1, *tmp2;
char *c, *c1;
STRING *s;
bool neg;
if (str->s_len == 0)
return slink(str);
neg = qisneg(q);
q = neg ? qneg(q): qlink(q);
tmp1 = itoq(str->s_len);
tmp2 = qmul(q, tmp1);
qfree(tmp1);
tmp1 = qint(tmp2);
qfree(tmp2);
if (zge31b(tmp1->num)) {
qfree(q);
qfree(tmp1);
return NULL;
}
len = qtoi(tmp1);
qfree(tmp1);
qfree(q);
if (len == 0)
return slink(&_nullstring_);
c = (char *) malloc(len + 1);
if (c == NULL)
return NULL;
str = neg ? stringneg(str) : slink(str);
s = stralloc();
s->s_str = c;
s->s_len = len;
j = 0;
c1 = str->s_str;
while (len-- > 0) {
*c++ = *c1++;
if (++j == str->s_len) {
j = 0;
c1 = str->s_str;
}
}
*c = '\0';
sfree(str);
return s;
}
STRING *
stringand(STRING *s1, STRING *s2)
{
STRING *s;
size_t len;
char *c1, *c2, *c;
if (s1->s_len == 0 || s2->s_len == 0)
return slink(&_nullstring_);
len = s1->s_len;
if (s2->s_len < len)
len = s2->s_len;
s = stralloc();
s->s_len = len;
c = malloc(len + 1);
if (c == NULL)
return NULL;
s->s_str = c;
c1 = s1->s_str;
c2 = s2->s_str;
while (len-- > 0)
*c++ = *c1++ & *c2++;
return s;
}
STRING *
stringor(STRING *s1, STRING *s2)
{
STRING *s;
long len, i, j;
char *c1, *c2, *c;
if (s1->s_len < s2->s_len) {
s = s1;
s1 = s2;
s2 = s;
} /* Now len(s1) >= len(s2) */
if (s2->s_len == 0)
return slink(s1);
i = s1->s_len;
if (i == 0)
return slink(&_nullstring_);
len = s1->s_len;
s = stralloc();
s->s_len = len;
c = malloc(len + 1);
if (c == NULL)
return NULL;
s->s_str = c;
c1 = s1->s_str;
c2 = s2->s_str;
i = s2->s_len;
j = s1->s_len - i;
while (i-- > 0)
*c++ = *c1++ | *c2++;
while (j-- > 0)
*c++ = *c1++;
return s;
}
STRING *
stringxor(STRING *s1, STRING *s2)
{
STRING *s;
long len, i, j;
char *c1, *c2, *c;
if (s1->s_len < s2->s_len) {
s = s1;
s1 = s2;
s2 = s;
} /* Now len(s1) >= len(s2) */
if (s2->s_len == 0)
return slink(s1);
i = s1->s_len;
if (i == 0)
return slink(&_nullstring_);
len = s1->s_len;
s = stralloc();
s->s_len = len;
c = malloc(len + 1);
if (c == NULL)
return NULL;
s->s_str = c;
c1 = s1->s_str;
c2 = s2->s_str;
i = s2->s_len;
j = s1->s_len - i;
while (i-- > 0)
*c++ = *c1++ ^ *c2++;
while (j-- > 0)
*c++ = *c1++;
return s;
}
STRING *
stringdiff(STRING *s1, STRING *s2)
{
STRING *s;
size_t i;
char *c2, *c;
i = s1->s_len;
if (i == 0)
return slink(s1);
s = stringcopy(s1);
if (i > s2->s_len)
i = s2->s_len;
c = s->s_str;
c2 = s2->s_str;
while (i-- > 0)
*c++ &= ~*c2++;
return s;
}
STRING *
stringcomp(STRING *s1)
{
long len;
STRING *s;
char *c1, *c;
len = s1->s_len;
if (len == 0)
return slink(&_nullstring_);
c = malloc(len + 1);
if (c == NULL)
return NULL;
s = stralloc();
s->s_len = len;
s->s_str = c;
c1 = s1->s_str;
while (len-- > 0)
*c++ = ~*c1++;
*c = '\0';
return s;
}
STRING *
stringsegment(STRING *s1, long n1, long n2)
{
STRING *s;
char *c, *c1;
long len;
if ((n1 < 0 && n2 < 0) ||
((size_t)n1 >= s1->s_len && (size_t)n2 >= s1->s_len))
return slink(&_nullstring_);
if (n1 < 0)
n1 = 0;
if (n2 < 0)
n2 = 0;
if ((size_t)n1 >= s1->s_len)
n1 = s1->s_len - 1;
if ((size_t)n2 >= s1->s_len)
n2 = s1->s_len - 1;
len = (n1 >= n2) ? n1 - n2 + 1 : n2 - n1 + 1;
s = stralloc();
c = malloc(len + 1);
if (c == NULL)
return NULL;
s->s_len = len;
s->s_str = c;
c1 = s1->s_str + n1;
if (n1 >= n2) {
/*
* We prevent the c1 pointer from walking behind s1_s_str
* by stopping one short of the end and running the loop one
* more time.
*
* We could stop the loop with just len-- > 0, but stopping
* short and running the loop one last time manually helps make
* code checkers such as insure happy.
*/
while (len-- > 1) {
*c++ = *c1--;
}
/* run the loop manually one last time */
*c++ = *c1;
} else {
while (len-- > 0)
*c++ = *c1++;
}
*c = '\0';
return s;
}
/*
* stringshift shifts s1 n bits to left if n > 0, -n to the right if n < 0;
* octets in string considered to be in decreasing order of index, as in
* ... a_3 a_2 a_1 a_0. Returned string has same length as s1.
* Vacated bits are filled with '\0'; bits shifted off end are lost
*/
STRING *
stringshift(STRING *s1, long n)
{
char *c1, *c;
STRING *s;
long len, i, j, k;
bool right;
char ch;
len = s1->s_len;
if (len == 0 || n == 0)
return slink(s1);
right = (n < 0);
if (right) n = -n;
j = n & 7;
k = 8 - j;
n >>= 3;
c = malloc(len + 1);
if (c == NULL)
return NULL;
s = stralloc();
s->s_len = len;
s->s_str = c;
c[len] = '\0';
if (n > len)
n = len;
ch = '\0';
c1 = s1->s_str;
i = n;
if (right) {
c += len;
c1 += len;
while (i-- > 0)
*--c = '\0';
i = len - n;
while (i-- > 0) {
*--c = ((unsigned char) *--c1 >> j) | ch;
ch = (unsigned char) *c1 << k;
}
} else {
while (i-- > 0)
*c++ = '\0';
i = len - n;
while (i-- > 0) {
*c++ = ((unsigned char) *c1 << j) | ch;
ch = (unsigned char) *c1++ >> k;
}
}
return s;
}
/*
* stringtoupper makes st upper case
*/
STRING *
stringtoupper(STRING *st)
{
char *c1, *c2;
size_t num;
if (st->s_len > 0) {
c1 = st->s_str;
num = st->s_len;
c2 = c1;
while (num-- > 0)
*c1++ = (char)toupper((int)*c2++);
*c1 = '\0';
}
return slink(st);
}
/*
* stringtolower makes st lower case
*/
STRING *
stringtolower(STRING *st)
{
char *c1, *c2;
size_t num;
if (st->s_len > 0) {
c1 = st->s_str;
num = st->s_len;
c2 = c1;
while (num-- > 0)
*c1++ = (char)tolower((int)*c2++);
*c1 = '\0';
}
return slink(st);
}
/*
* stringcpy copies as many characters as possible
* from s2 to s1 and returns s1
*/
STRING *
stringcpy(STRING *s1, STRING *s2)
{
char *c1, *c2;
size_t num;
if (s1->s_len > 0) {
c1 = s1->s_str;
c2 = s2->s_str;
num = s1->s_len;
if (num > s2->s_len)
num = s2->s_len;
while (num-- > 0)
*c1++ = *c2++;
*c1 = '\0';
}
return slink(s1);
}
/*
* stringncpy copies up to num characters from s2 to s1 and returns s1
* If num > size(s2) null characters are copied until s1 is full or
* a total of num characters have been copied
*/
STRING *
stringncpy(STRING *s1, STRING *s2, size_t num)
{
char *c1, *c2;
size_t i;
if (num > s1->s_len)
num = s1->s_len;
i = num;
if (i > s2->s_len)
i = s2->s_len;
c1 = s1->s_str;
c2 = s2->s_str;
while (i-- > 0)
*c1++ = *c2++;
if (num > s2->s_len) {
memset(c1, 0, num - s2->s_len);
}
return slink(s1);
}
/*
* stringcontent counts the number of set bits in s
*/
long
stringcontent(STRING *s)
{
char *c;
unsigned char ch;
long count;
long len;
len = s->s_len;
count = 0;
c = s->s_str;
while (len-- > 0) {
ch = *c++;
while (ch) {
count += (ch & 1);
ch >>= 1;
}
}
return count;
}
long
stringhighbit(STRING *s)
{
char *c;
unsigned char ch;
long i;
i = s->s_len;
c = s->s_str + i;
while (--i >= 0 && *--c == '\0');
if (i < 0)
return -1;
i <<= 3;
for (ch = *c; ch >>= 1; i++);
return i;
}
long
stringlowbit(STRING *s)
{
char *c;
unsigned char ch;
long i;
for (i = s->s_len, c = s->s_str; i > 0 && *c == '\0'; i--, c++);
if (i == 0)
return -1;
i = (s->s_len - i) << 3;
for (ch = *c; !(ch & 1); ch >>= 1, i++);
return i;
}
/*
* stringcompare compares first len characters of strings starting at c1, c2
* Returns true if and only if a difference is encountered.
* Essentially a local version of memcmp.
*/
S_FUNC bool
stringcompare(char *c1, char *c2, long len)
{
while (len-- > 0) {
if (*c1++ != *c2++)
return true;
}
return false;
}
/*
* stringcmp returns true if strings differ, false if strings equal
*/
bool
stringcmp(STRING *s1, STRING *s2)
{
if (s1->s_len != s2->s_len)
return true;
return stringcompare(s1->s_str, s2->s_str, s1->s_len);
}
/*
* stringrel returns 0 if strings are equal; otherwise 1 or -1 according
* as the greater of the first unequal characters are in the first or
* second string, or in the case of unequal-length strings when the compared
* characters are all equal, 1 or -1 according as the first or second string
* is longer.
*/
FLAG
stringrel(STRING *s1, STRING *s2)
{
char *c1, *c2;
long i1, i2;
if (s1 == s2)
return 0;
i1 = s1->s_len;
i2 = s2->s_len;
if (i2 == 0)
return (i1 > 0);
if (i1 == 0)
return -1;
c1 = s1->s_str;
c2 = s2->s_str;
while (i1 > 1 && i2 > 1 && *c1 == *c2) {
i1--;
i2--;
c1++;
c2++;
}
if ((unsigned char) *c1 > (unsigned char) *c2) return 1;
if ((unsigned char) *c1 < (unsigned char) *c2) return -1;
if (i1 < i2) return -1;
return (i1 > i2);
}
/* Case independent stringrel(STRING *s1, STRING *s2)
* stringcaserel returns 0 if strings are equal; otherwise 1 or -1 according
* as the greater of the first unequal characters are in the first or
* second string, or in the case of unequal-length strings when the compared
* characters are all equal, 1 or -1 according as the first or second string
* is longer.
*/
FLAG
stringcaserel(STRING *s1, STRING *s2)
{
char *c1, *c2;
long i1, i2;
if (s1 == s2)
return 0;
i1 = s1->s_len;
i2 = s2->s_len;
if (i2 == 0)
return (i1 > 0);
if (i1 == 0)
return -1;
c1 = s1->s_str;
c2 = s2->s_str;
while (i1 > 1 && i2 > 1 && tolower(*c1) == tolower(*c2)) {
i1--;
i2--;
c1++;
c2++;
}
if ( (tolower(*c1)) > (tolower(*c2)))
return 1;
if ( (tolower(*c1)) < (tolower(*c2)))
return -1;
if (i1 < i2) return -1;
return (i1 > i2);
}
/*
* str with characters c0, c1, ... is considered as a bitstream, 8 bits
* per character; within a character the bits ordered from low order to
* high order. For 0 <= i < 8 * length of str, stringbit returns 1 or 0
* according as the bit with index i is set or not set; other values of i
* return -1.
*/
int
stringbit(STRING *s, long index)
{
unsigned int ch;
int res;
if (index < 0)
return -1;
res = index & 7;
index >>= 3;
if ((size_t)index >= s->s_len)
return -1;
ch = s->s_str[index];
return (ch >> res) & 1;
}
bool
stringtest(STRING *s)
{
long i;
char *c;
i = s->s_len;
c = s->s_str;
while (i-- > 0) {
if (*c++)
return true;
}
return false;
}
/*
* If index is in acceptable range, stringsetbit sets or resets specified
* bit in string s according as val is true or false, and returns 0.
* Returns 1 if index < 0; 2 if index too large.
*/
int
stringsetbit(STRING *s, long index, bool val)
{
char *c;
int bit;
if (index < 0)
return 1;
bit = 1 << (index & 7);
index >>= 3;
if ((size_t)index >= s->s_len)
return 2;
c = &s->s_str[index];
*c &= ~bit;
if (val)
*c |= bit;
return 0;
}
/*
* stringsearch returns 0 and sets index = i if the first occurrence
* of s2 in s1 for start <= i < end is at index i. If no such occurrence
* is found, -1 is returned.
*/
int
stringsearch(STRING *s1, STRING *s2, long start, long end, ZVALUE *index)
{
long len2, i, j;
char *c1, *c2, *c;
char ch;
len2 = s2->s_len;
if (start < 0)
start = 0;
if (end < start + len2)
return -1;
if (len2 == 0) {
itoz(start, index);
return 0;
}
i = end - start - len2;
c1 = s1->s_str + start;
ch = *s2->s_str;
while (i-- >= 0) {
if (*c1++ == ch) {
c = c1;
c2 = s2->s_str;
j = len2;
while (--j > 0 && *c++ == *++c2);
if (j == 0) {
itoz(end - len2 - i - 1, index);
return 0;
}
}
}
return -1;
}
int
stringrsearch(STRING *s1, STRING *s2, long start, long end, ZVALUE *index)
{
long len1, len2, i, j;
char *c1, *c2, *c, *c2top;
char ch;
len1 = s1->s_len;
len2 = s2->s_len;
if (start < 0)
start = 0;
if (end > len1)
end = len1;
if (end < start + len2)
return -1;
if (len2 == 0) {
itoz(start, index);
return 0;
}
i = end - start - len2 + 1;
c1 = s1->s_str + end - 1;
c2top = s2->s_str + len2 - 1;
ch = *c2top;
while (--i >= 0) {
if (*c1-- == ch) {
c = c1;
j = len2;
c2 = c2top;
while (--j > 0 && *c-- == *--c2);
if (j == 0) {
itoz(start + i, index);
return 0;
}
}
}
return -1;
}
/*
* String allocation routines
*/
#define STRALLOC 100
STATIC STRING *freeStr = NULL;
STATIC STRING **firstStrs = NULL;
STATIC long blockcount = 0;
STRING *
stralloc(void)
{
STRING *temp;
STRING **newfn;
if (freeStr == NULL) {
/* alloc + 1 guard paranoia */
freeStr = (STRING *) malloc(sizeof (STRING) * (STRALLOC + 1));
if (freeStr == NULL) {
math_error("Unable to allocate memory for stralloc");
not_reached();
}
/* guard paranoia */
memset(freeStr+STRALLOC, 0, sizeof(STRING));
freeStr[STRALLOC - 1].s_next = NULL;
freeStr[STRALLOC - 1].s_links = 0;
/*
* We prevent the temp pointer from walking behind freeStr
* by stopping one short of the end and running the loop one
* more time.
*
* We would stop the loop with just temp >= freeStr, but
* doing this helps make code checkers such as insure happy.
*/
for (temp = freeStr + STRALLOC - 2; temp > freeStr; --temp) {
temp->s_next = temp + 1;
temp->s_links = 0;
}
/* run the loop manually one last time */
temp->s_next = temp + 1;
temp->s_links = 0;
blockcount++;
if (firstStrs == NULL) {
/* alloc + 1 guard paranoia */
newfn = (STRING **)
malloc((blockcount + 1) * sizeof(STRING *));
newfn[blockcount] = NULL; /* guard paranoia */
} else {
/* alloc + 1 guard paranoia */
newfn = (STRING **)
realloc(firstStrs,
(blockcount + 1) * sizeof(STRING *));
newfn[blockcount] = NULL; /* guard paranoia */
}
if (newfn == NULL) {
math_error("Cannot allocate new string block");
not_reached();
}
firstStrs = newfn;
firstStrs[blockcount - 1] = freeStr;
}
temp = freeStr;
freeStr = temp->s_next;
temp->s_links = 1;
temp->s_str = NULL;
return temp;
}
/*
* makestring to be called only when str is the result of a malloc
*/
STRING *
makestring(char *str)
{
STRING *s;
size_t len;
len = strlen(str);
s = stralloc();
s->s_str = str;
s->s_len = len;
return s;
}
STRING *
charstring(int ch)
{
STRING *s;
char *c;
c = (char *) malloc(2);
if (c == NULL) {
math_error("Allocation failure for charstring");
not_reached();
}
s = stralloc();
s->s_len = 1;
s->s_str = c;
*c++ = (char) ch;
*c = '\0';
return s;
}
/*
* makenewstring creates a new string by copying null-terminated str;
* str is not freed
*/
STRING *
makenewstring(char *str)
{
STRING *s;
char *c;
size_t len;
len = strlen(str);
if (len == 0)
return slink(&_nullstring_);
c = (char *) malloc(len + 1);
if (c == NULL) {
math_error("malloc for makenewstring failed");
not_reached();
}
s = stralloc();
s->s_str = c;
s->s_len = len;
memcpy(c, str, len);
c[len] = '\0';
return s;
}
STRING *
stringcopy (STRING *s1)
{
STRING *s;
char *c;
long len;
len = s1->s_len;
if (len == 0)
return slink(s1);
c = malloc(len + 1);
if (c == NULL) {
math_error("Malloc failed for stringcopy");
not_reached();
}
s = stralloc();
s->s_len = len;
s->s_str = c;
memcpy(c, s1->s_str, len);
c[len] = '\0';
return s;
}
STRING *
slink(STRING *s)
{
if (s->s_links <= 0) {
math_error("Argument for slink has non-positive links!!!");
not_reached();
}
++s->s_links;
return s;
}
void
sfree(STRING *s)
{
if (s->s_links <= 0) {
math_error("Argument for sfree has non-positive links!!!");
not_reached();
}
if (--s->s_links > 0 || s->s_len == 0)
return;
free(s->s_str);
s->s_next = freeStr;
freeStr = s;
}
STATIC long stringconstcount = 0;
STATIC long stringconstavail = 0;
STATIC STRING **stringconsttable;
#define STRCONSTALLOC 100
void
initstrings(void)
{
/* alloc + 1 guard paranoia */
stringconsttable = (STRING **)
malloc(sizeof(STRING *) * (STRCONSTALLOC + 1));
if (stringconsttable == NULL) {
math_error("Unable to allocate constant table");
not_reached();
}
stringconsttable[STRCONSTALLOC] = NULL; /* guard paranoia */
stringconsttable[0] = &_nullstring_;
stringconstcount = 1;
stringconstavail = STRCONSTALLOC - 1;
}
/*
* addstring is called only from token.c
* When called, len is length of string including '\0'
*/
long
addstring(char *str, size_t len)
{
STRING **sp;
STRING *s;
char *c;
long index; /* index into constant table */
long first; /* first non-null position found */
bool havefirst;
if (len < 1) {
math_error("addstring length including trailing NUL < 1");
}
if (stringconstavail <= 0) {
if (stringconsttable == NULL) {
initstrings();
} else {
/* alloc + 1 guard paranoia */
sp = (STRING **)
realloc((char *) stringconsttable,
sizeof(STRING *) *
(stringconstcount + STRCONSTALLOC + 1));
if (sp == NULL) {
math_error("Unable to reallocate string "
"const table");
not_reached();
}
/* guard paranoia */
sp[stringconstcount + STRCONSTALLOC] = NULL;
stringconsttable = sp;
stringconstavail = STRCONSTALLOC;
}
}
len--;
first = 0;
havefirst = false;
sp = stringconsttable;
for (index = 0; index < stringconstcount; index++, sp++) {
s = *sp;
if (s->s_links == 0) {
if (!havefirst) {
havefirst = true;
first = index;
}
continue;
}
if (s->s_len == len && stringcompare(s->s_str, str, len) == 0) {
s->s_links++;
return index;
}
}
s = stralloc();
c = (char *) malloc(len + 1);
if (c == NULL) {
math_error("Unable to allocate string constant memory");
not_reached();
}
s->s_str = c;
s->s_len = len;
memcpy(s->s_str, str, len+1);
if (havefirst) {
stringconsttable[first] = s;
return first;
}
stringconstavail--;
stringconsttable[stringconstcount++] = s;
return index;
}
STRING *
findstring(long index)
{
if (index < 0 || index >= stringconstcount) {
math_error("Bad index for findstring");
not_reached();
}
return stringconsttable[index];
}
void
freestringconstant(long index)
{
STRING *s;
STRING **sp;
if (index >= 0) {
s = findstring(index);
sfree(s);
if (index == stringconstcount - 1) {
sp = &stringconsttable[index];
while (stringconstcount > 0 && (*sp)->s_links == 0) {
stringconstcount--;
stringconstavail++;
sp--;
}
}
}
}
long
printechar(char *c)
{
unsigned char ch, cc;
unsigned char ech; /* for escape sequence */
ch = *c;
if (ch >= ' ' && ch < 127 && ch != '\\' && ch != '\"' && ch != '\'') {
math_chr(ch);
return 1;
}
math_chr('\\');
ech = 0;
switch (ch) {
case '\a': ech = 'a'; break;
case '\b': ech = 'b'; break;
case '\f': ech = 'f'; break;
case '\n': ech = 'n'; break;
case '\r': ech = 'r'; break;
case '\t': ech = 't'; break;
case '\v': ech = 'v'; break;
case '\\': ech = '\\'; break;
case '\"': ech = '\"'; break;
case '\'': ech = '\''; break;
case 0: ech = '0'; break;
case 27: ech = 'e'; break;
}
if (ech == '0') {
cc = *(c + 1);
if (cc >= '0' && cc < '8') {
math_str("000");
return 4;
}
}
if (ech) {
math_chr(ech);
return 2;
}
math_chr('x');
cc = ch / 16;
math_chr((cc < 10) ? '0' + cc : 87 + cc);
cc = ch % 16;
math_chr((cc < 10) ? '0' + cc : 87 + cc);
return 4;
}
void
fitstring(char *str, long len, long width)
{
long i, j, n, max;
char *c;
unsigned char ch, nch;
max = (width - 3)/2;
if (len == 0)
return;
c = str;
for (i = 0, n = 0; i < len && n < max; i++) {
n += printechar(c++);
}
if (i >= len)
return;
c = str + len;
nch = '\0';
for (n = 0, j = len ; j > i && n < max ; --j, nch = ch) {
ch = *--c;
n++;
if (ch >= ' ' && ch <= 127 && ch != '\\' && ch != '\"')
continue;
n++;
switch (ch) {
case '\a':
case '\b':
case '\f':
case '\n':
case '\r':
case '\t':
case '\v':
case '\\':
case '\"':
case '\'':
case 27:
continue;
}
if (ch >= 64 || (nch >= '0' && nch <= '7')) {
n += 2;
continue;
}
if (ch >= 8)
n++;
}
if (j > i)
math_str("...");
while (j++ < len)
(void) printechar(c++);
}
void
strprint(STRING *str) {
long n;
char *c;
c = str->s_str;
n = str->s_len;
while (n-- > 0)
(void) printechar(c++);
}
void
showstrings(void)
{
STRING *sp;
long i, j, k;
long count;
printf("Index Links Length String\n");
printf("----- ----- ------ ------\n");
sp = &_nullstring_;
printf(" 0 %5ld 0 \"\"\n", sp->s_links);
for (i = 0, k = 1, count = 1; i < blockcount; i++) {
sp = firstStrs[i];
for (j = 0; j < STRALLOC; j++, k++, sp++) {
if (sp->s_links > 0) {
++count;
printf("%5ld %5ld %6ld \"",
k, sp->s_links, (long int)sp->s_len);
fitstring(sp->s_str, sp->s_len, 50);
printf("\"\n");
}
}
}
printf("\nNumber: %ld\n", count);
}
void
showliterals(void)
{
STRING *sp;
long i;
long count = 0;
printf("Index Links Length String\n");
printf("----- ----- ------ ------\n");
for (i = 0; i < stringconstcount; i++) {
sp = stringconsttable[i];
if (sp->s_links > 0) {
++count;
printf("%5ld %5ld %6ld \"",
i, sp->s_links, (long int)sp->s_len);
fitstring(sp->s_str, sp->s_len, 50);
printf("\"\n");
}
}
printf("\nNumber: %ld\n", count);
}