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

2853 lines
54 KiB
C

/*
* file - file I/O routines callable by users
*
* Copyright (C) 1999-2007,2018,2021-2023 David I. Bell and Landon Curt Noll
*
* 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: 1991/07/20 00:21:56
* File existed as early as: 1991
*
* chongo <was here> /\oo/\ http://www.isthe.com/chongo/
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "have_unistd.h"
#if defined(HAVE_UNISTD_H)
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <ctype.h>
#include "calc.h"
#include "alloc.h"
#include "longbits.h"
#include "have_fgetsetpos.h"
#include "have_fpos_pos.h"
#include "fposval.h"
#include "file.h"
#include "strl.h"
#if defined(_WIN32) || defined(_WIN64)
# include <io.h>
#endif
#include "errtbl.h"
#include "banned.h" /* include after system header <> includes */
#define READSIZE 1024 /* buffer size for reading */
#define MIN(a,b) (((a) <= (b)) ? (a) : (b))
/*
* external STDIO functions
*/
E_FUNC void math_setfp(FILE *fp);
E_FUNC FILE *f_open(char *name, char *mode);
/*
* Table of opened files.
* The first three entries always correspond to stdin, stdout, and stderr,
* and cannot be closed. Their file ids are always 0, 1, and 2.
*/
STATIC FILEIO files[MAXFILES] = {
{FILEID_STDIN, NULL, (dev_t)0, (ino_t)0,
"(stdin)", true, false, false, false, 'r', "r"},
{FILEID_STDOUT, NULL, (dev_t)0, (ino_t)0,
"(stdout)", false, true, false, false, 'w', "w"},
{FILEID_STDERR, NULL, (dev_t)0, (ino_t)0,
"(stderr)", false, true, false, false, 'w', "w"}
};
STATIC int ioindex[MAXFILES] = {0,1,2}; /* Indices for FILEIO table */
STATIC FILEID lastid = FILEID_STDERR; /* Last allocated file id */
STATIC int idnum = 3; /* Number of allocated file ids */
/* forward static declarations */
S_FUNC ZVALUE filepos2z(FILEPOS pos);
S_FUNC FILEPOS z2filepos(ZVALUE pos);
S_FUNC int set_open_pos(FILE *fp, ZVALUE zpos);
S_FUNC int get_open_pos(FILE *fp, ZVALUE *res);
S_FUNC ZVALUE off_t2z(off_t siz);
S_FUNC ZVALUE dev2z(dev_t dev);
S_FUNC ZVALUE inode2z(ino_t inode);
S_FUNC void getscanfield(FILE *fp, bool skip, unsigned int width,
int scannum, char *scanptr, char **strptr);
S_FUNC void getscanwhite(FILE *fp, bool skip, unsigned int width,
int scannum, char **strptr);
S_FUNC int fscanfile(FILE *fp, char *fmt, int count, VALUE **vals);
S_FUNC void freadnum(FILE *fp, VALUE *valptr);
S_FUNC void freadsum(FILE *fp, VALUE *valptr);
S_FUNC void freadprod(FILE *fp, VALUE *valptr);
S_FUNC void fskipnum(FILE *fp);
/*
* file_init - perform needed initialization work
*
* On some systems, one cannot initialize a pointer to a FILE *.
* This routine, called once at startup is a work-a-round for
* systems with such bogons.
*
* We will also probe for any open files beyond stderr and set them up.
*/
void
file_init(void)
{
STATIC int done = 0; /* 1 => routine already called */
struct stat sbuf; /* file status */
FILEIO *fiop;
FILE *fp;
int i;
if (!done) {
/*
* setup the default set
*/
files[0].fp = stdin;
files[1].fp = stdout;
files[2].fp = stderr;
for (i = 0; i < 3; ++i) {
if (fstat(i, &sbuf) >= 0) {
files[i].dev = sbuf.st_dev;
files[i].inode = sbuf.st_ino;
}
}
/*
* note any other files that we can find
*/
fiop = &files[3];
for (i = 3; i < MAXFILES; fiop++, ++i) {
char *tname;
fiop->name = NULL;
files[idnum].reading = true;
files[idnum].writing = true;
files[idnum].action = 0;
memset(files[idnum].mode, 0, MODE_LEN+1);
/*
* stat the descriptor to see what we have
*/
if (fstat(i, &sbuf) >= 0) {
size_t snprintf_len; /* malloced snprintf length */
fp = (FILE *) fdopen(i,"r+"); /*guess mode*/
if (fp) {
strlcpy(files[idnum].mode, "r+",
sizeof(files[idnum].mode));
} else {
fp = (FILE *) fdopen(i, "r");
if (fp) {
strlcpy(files[idnum].mode, "r",
sizeof(files[idnum].mode));
files[idnum].writing = false;
} else {
fp = (FILE *) fdopen(i, "w");
if (fp) {
strlcpy(files[idnum].mode, "w",
sizeof(files[idnum].mode));
files[idnum].reading = false;
}
else
continue;
}
}
snprintf_len =
sizeof("descriptor[12345678901234567890]") + 1;
tname = (char *)malloc(snprintf_len+1);
if (tname == NULL) {
math_error("Out of memory for init_file");
not_reached();
}
snprintf(tname, snprintf_len, "descriptor[%d]", i);
tname[snprintf_len] = '\0'; /* paranoia */
files[idnum].name = tname;
files[idnum].id = idnum;
files[idnum].fp = fp;
files[idnum].dev = sbuf.st_dev;
files[idnum].inode = sbuf.st_ino;
ioindex[idnum] = idnum;
idnum++;
lastid++;
}
}
done = 1;
}
}
/*
* init_fileio - initialize a FILEIO structure
*
* This function initializes a calc FILEIO structure. It will optionally
* malloc the filename string if given an non-NULL associated filename.
* It will canonicalize the file open mode string.
*
* given:
* fiop pointer to FILEIO structure to initialize
* name associated filename (NULL => caller will setup filename)
* mode open mode (one of {r,w,a}{,b}{,+})
* sbufp pointer to stat of open file
* id calc file ID
* fp open file stream
*/
S_FUNC void
init_fileio(FILEIO *fiop, char *name, char *mode,
struct stat *sbufp, FILEID id, FILE *fp)
{
char modestr[MODE_LEN+1]; /* mode [rwa]b?\+? */
size_t namelen; /* length of name */
/* clear modestr */
memset(modestr, 0, sizeof(modestr));
/* allocate filename if requested */
namelen = 0;
if (name != NULL) {
namelen = strlen(name);
fiop->name = (char *)malloc(namelen + 1);
if (fiop->name == NULL) {
math_error("No memory for filename");
not_reached();
}
}
/* initialize FILEIO structure */
if (name != NULL) {
strlcpy(fiop->name, name, namelen+1);
}
fiop->id = id;
fiop->fp = fp;
fiop->dev = sbufp->st_dev;
fiop->inode = sbufp->st_ino;
fiop->reading = false;
fiop->writing = false;
fiop->appending = false;
fiop->binary = false;
fiop->action = 0;
memset(fiop->mode, 0, sizeof(fiop->mode));
/*
* determine file open mode
*
* While a leading 'r' is for reading and a leading 'w' is
* for writing, the presence of a '+' in the string means
* both reading and writing. A leading 'a' means append
* which is writing.
*/
/* canonicalize read modes */
if (mode[0] == 'r') {
/* note read mode */
strlcpy(modestr, "r", sizeof(modestr));
fiop->reading = true;
/* note binary mode even though mode is not used / ignored */
if (strchr(mode, 'b') != NULL) {
strlcat(modestr, "b", sizeof(modestr));
}
/* note if reading and writing */
if (strchr(mode, '+') != NULL) {
fiop->writing = true;
strlcat(modestr, "+", sizeof(modestr));
}
/* canonicalize write modes */
} else if (mode[0] == 'w') {
/* note write mode */
strlcpy(modestr, "w", sizeof(modestr));
fiop->writing = true;
/* note binary mode even though mode is not used / ignored */
if (strchr(mode, 'b') != NULL) {
strlcat(modestr, "b", sizeof(modestr));
}
/* note if reading and writing */
if (strchr(mode, '+') != NULL) {
fiop->reading = true;
strlcat(modestr, "+", sizeof(modestr));
}
/* canonicalize append modes */
} else if (mode[0] == 'a') {
/* note append mode */
strlcpy(modestr, "a", sizeof(modestr));
fiop->writing = true;
fiop->appending = true;
/* note binary mode even though mode is not used / ignored */
if (strchr(mode, 'b') != NULL) {
strlcat(modestr, "b", sizeof(modestr));
}
/* note if reading and writing */
if (strchr(mode, '+') != NULL) {
fiop->reading = true;
strlcat(modestr, "+", sizeof(modestr));
}
/* canonicalize no I/O modes */
} else {
modestr[0] = '\0';
}
modestr[MODE_LEN] = '\0'; /* firewall */
/* record canonical open mode string */
strlcpy(fiop->mode, modestr, sizeof(fiop->mode));
}
/*
* openid - open the specified file name for reading or writing
*
* given:
* name file name
* mode open mode (one of {r,w,a}{,b}{,+})
*
* returns:
* >=3 FILEID which can be used to do I/O to the file
* <0 if the open failed
*
* NOTE: This function will not return 0, 1 or 2 since they are
* reserved for stdin, stdout, stderr. In fact, it must not
* return 0, 1, or 2 because it will confuse those who call
* the opensearchfile() function
*/
FILEID
openid(char *name, char *mode)
{
FILEIO *fiop; /* file structure */
FILEID id; /* new file id */
FILE *fp;
struct stat sbuf; /* file status */
int i;
/* find the next open slot in the files array */
if (idnum >= MAXFILES)
return -E_MANYOPEN;
fiop = &files[3];
for (i = 3; i < MAXFILES; fiop++,i++) {
if (fiop->name == NULL)
break;
}
if (i == MAXFILES)
math_error("This should not happen in openid()!!!");
/* open the file */
fp = f_open(name, mode);
if (fp == NULL) {
return FILEID_NONE;
}
if (fstat(fileno(fp), &sbuf) < 0) {
math_error("bad fstat");
not_reached();
}
/* get a new FILEID */
id = ++lastid;
ioindex[idnum++] = i;
/* initialize FILEIO structure */
init_fileio(fiop, name, mode, &sbuf, id, fp);
/* return calc open file ID */
return id;
}
/*
* openpathid - open the specified base filename, or
* relative filename along a search path
*
* given:
* name file name
* mode open mode (one of {r,w,a}{,b}{,+})
* pathlist list of colon separated paths (or NULL)
*
* returns:
* >=3 FILEID which can be used to do I/O to the file
* <0 if the open failed
*
* NOTE: This function will not return 0, 1 or 2 since they are
* reserved for stdin, stdout, stderr. In fact, it must not
* return 0, 1, or 2 because it will confuse those who call
* the opensearchfile() function
*/
FILEID
openpathid(char *name, char *mode, char *pathlist)
{
FILEIO *fiop; /* file structure */
FILEID id; /* new file id */
FILE *fp;
struct stat sbuf; /* file status */
char *openpath; /* malloc copy of path that was opened */
int i;
/* find the next open slot in the files array */
if (idnum >= MAXFILES)
return -E_MANYOPEN;
fiop = &files[3];
for (i = 3; i < MAXFILES; fiop++,i++) {
if (fiop->name == NULL)
break;
}
if (i == MAXFILES)
math_error("This should not happen in openpathid()!!!");
/* open a file - searching along a path */
openpath = NULL;
fp = f_pathopen(name, mode, pathlist, &openpath);
if (fp == NULL) {
if (openpath != NULL) {
/* should not happen, but just in case */
free(openpath);
}
return FILEID_NONE;
}
if (fstat(fileno(fp), &sbuf) < 0) {
if (openpath != NULL) {
free(openpath);
}
math_error("bad fstat");
not_reached();
}
if (openpath == NULL) {
fclose(fp);
math_error("bad openpath");
not_reached();
}
/* get a new FILEID */
id = ++lastid;
ioindex[idnum++] = i;
/* initialize FILEIO structure */
init_fileio(fiop, NULL, mode, &sbuf, id, fp);
fiop->name = openpath; /* already malloced by f_pathopen */
/* return calc open file ID */
return id;
}
/*
* reopenid - reopen a FILEID
*
* given:
* id FILEID to reopen
* mode new mode to open as
* mode new mode to open as (one of "r", "w", "a", "r+", "w+", "a+")
* name name of new file
*
* returns:
* FILEID which can be used to do I/O to the file
* <0 if the open failed
*/
FILEID
reopenid(FILEID id, char *mode, char *name)
{
FILEIO *fiop; /* file structure */
FILE *fp;
struct stat sbuf;
int i;
/* firewall */
if ((id == FILEID_STDIN) || (id == FILEID_STDOUT) ||
(id == FILEID_STDERR)) {
math_error("Cannot freopen stdin, stdout, or stderr");
not_reached();
}
/* reopen the file */
fiop = NULL;
for (i = 3; i < idnum; i++) {
fiop = &files[ioindex[i]];
if (fiop->id == id)
break;
}
if (i == idnum) {
if (name == NULL) {
fprintf(stderr, "File not open, need file name\n");
return FILEID_NONE;
}
if (idnum >= MAXFILES) {
fprintf(stderr, "Too many open files\n");
return FILEID_NONE;
}
for (fiop = &files[3], i = 3; i < MAXFILES; fiop++, i++) {
if (fiop->name == NULL)
break;
}
if (i >= MAXFILES) {
math_error("This should not happen in reopenid");
not_reached();
}
fp = f_open(name, mode);
if (fp == NULL) {
fprintf(stderr, "Cannot open file\n");
return FILEID_NONE;
}
ioindex[idnum++] = i;
fiop->id = id;
} else {
if (name == NULL)
fp = freopen(fiop->name, mode, fiop->fp);
else
fp = freopen(name, mode, fiop->fp);
if (fp == NULL) {
free(fiop->name);
fiop->name = NULL;
idnum--;
for (; i < idnum; i++)
ioindex[i] = ioindex[i + 1];
return FILEID_NONE;
}
}
if (fstat(fileno(fp), &sbuf) < 0) {
math_error("bad fstat");
not_reached();
}
/* initialize FILEIO structure */
if (name == NULL) {
if (fiop->name == NULL) {
math_error("old and new reopen filenames are NULL");
}
} else if (fiop->name != NULL) {
free(fiop->name);
fiop->name = NULL;
}
init_fileio(fiop, name, mode, &sbuf, id, fp);
/* return calc open file ID */
return id;
}
/*
* Find the file I/O structure for the specified file id, and verifies that
* it is opened in the required manner (0 for reading or 1 for writing).
* If writable is -1, then no open checks are made at all and NULL is then
* returned if the id represents a closed file.
*/
FILEIO *
findid(FILEID id, int writable)
{
FILEIO *fiop; /* file structure */
int i;
fiop = NULL;
if ((id < 0) || (id > lastid))
return NULL;
for (i = 0; i < idnum; i++) {
fiop = &files[ioindex[i]];
if (fiop->id == id)
break;
}
if (i == idnum)
return NULL;
if (writable >= 0) {
if ((writable && !fiop->writing) ||
(!writable && !fiop->reading)) {
return NULL;
}
}
return fiop;
}
/*
* Return whether or not a file id is valid. This is used for if tests.
*/
bool
validid(FILEID id)
{
return (findid(id, -1) != NULL);
}
/*
* Return the file with id = index if this is the id of a file that has been
* opened (it may have since been closed). Otherwise returns FILEID_NONE.
*/
FILEID
indexid(long index)
{
FILEID id;
id = (FILEID) index;
if ((index < 0) || (id > lastid))
return FILEID_NONE;
return id;
}
/*
* Close the specified file id. Returns true if there was an error.
* Closing of stdin, stdout, or stderr is illegal, but closing of already
* closed files is allowed.
*/
int
closeid(FILEID id)
{
FILEIO *fiop; /* file structure */
int i;
int err;
fiop = NULL;
/* firewall */
if ((id == FILEID_STDIN) || (id == FILEID_STDOUT) ||
(id == FILEID_STDERR)) {
math_error("Cannot close stdin, stdout, or stderr");
not_reached();
}
/* get file structure */
for (i = 3; i < idnum; i++) {
fiop = &files[ioindex[i]];
if (fiop->id == id)
break;
}
if (i == idnum)
return 1; /* File not open */
idnum--;
for (; i < idnum; i++)
ioindex[i] = ioindex[i + 1];
free(fiop->name);
fiop->name = NULL;
/* close file and note error state */
err = ferror(fiop->fp);
err |= fclose(fiop->fp);
fiop->fp = NULL;
/* return success or failure */
return (err ? EOF : 0);
}
int
closeall(void)
{
FILEIO *fiop;
int i;
int err;
err = 0;
for (i = 3; i < idnum; i++) {
fiop = &files[ioindex[i]];
if (fiop->fp) {
free(fiop->name);
fiop->name = NULL;
err |= fclose(fiop->fp);
}
}
idnum = 3;
return err;
}
/*
* Return whether or not an error occurred to a file.
*/
bool
errorid(FILEID id)
{
FILEIO *fiop; /* file structure */
fiop = findid(id, -1);
if (fiop == NULL)
return EOF;
return (ferror(fiop->fp) != 0);
}
/*
* Return whether or not end of file occurred to a file.
*/
bool
eofid(FILEID id)
{
FILEIO *fiop; /* file structure */
fiop = findid(id, -1);
if (fiop == NULL)
return EOF;
return (feof(fiop->fp) != 0);
}
/*
* Flush output to an opened file.
*/
int
flushid(FILEID id)
{
FILEIO *fiop; /* file structure */
fiop = findid(id, -1);
if (fiop == NULL)
return 0;
if (!fiop->writing || fiop->action == 'r')
return 0;
return fflush(fiop->fp);
}
#if !defined(_WIN32) && !defined(_WIN64)
int
flushall(void)
{
FILEIO *fiop;
int i;
int err;
err = 0;
for (i = 3; i < idnum; i++) {
fiop = &files[ioindex[i]];
if (fiop->writing && fiop->action != 'r')
err |= fflush(fiop->fp);
}
return err;
}
#endif /* Windows free systems */
/*
* Read the next line, string or word from an opened file.
* Returns a pointer to an allocated string holding a null-terminated
* or newline terminated string. Where reading stops is controlled by
* flags:
*
* bit 0: at newline
* bit 1: at null character
* bit 2: at white space (also skips leading white space)
*
* If neither '\n' nor '\0' is encountered reading continues until EOF.
* If bit 3 is set the stop character is removed.
*
* given:
* id file to read from
* flags read flags (see above)
* retstr returned pointer to string
*/
int
readid(FILEID id, int flags, STRING **retstr)
{
FILEIO *fiop; /* file structure */
FILE *fp;
char *str; /* current string */
unsigned long n; /* current number characters read into buf */
unsigned long totlen; /* total length of string copied from buf */
char buf[READSIZE]; /* temporary buffer */
char *b;
int c;
bool nlstop, nullstop, wsstop, rmstop, done;
FILEPOS fpos;
STRING *newstr;
totlen = 0;
str = NULL;
fiop = findid(id, false);
if (fiop == NULL)
return 1;
nlstop = (flags & 1);
nullstop = (flags & 2);
wsstop = (flags & 4);
rmstop = (flags & 8);
fp = fiop->fp;
if (fiop->action == 'w') {
f_tell(fp, &fpos);
fflush(fp);
if (f_seek_set(fp, &fpos) < 0)
return 3;
}
fiop->action = 'r';
if (wsstop) {
while (isspace(c = fgetc(fp)));
ungetc(c, fp);
}
for (;;) {
b = buf;
n = 0;
do {
c = fgetc(fp);
if (c == EOF)
break;
n++;
if (nlstop && c == '\n')
break;
if (nullstop && c == '\0')
break;
if (wsstop && isspace(c))
break;
*b++ = c;
} while (n < READSIZE);
done = ((nlstop && c == '\n') || (nullstop && c == '\0') ||
(wsstop && isspace(c)) || c == EOF);
if (done && rmstop && c != EOF)
n--;
if (totlen)
str = (char *)realloc(str, totlen + n + 1);
else
str = (char *)malloc(n + 1);
if (str == NULL) {
math_error("Out of memory for readid");
not_reached();
}
if (n > 0)
memcpy(&str[totlen], buf, n);
totlen += n;
if (done)
break;
}
if (totlen == 0 && c == EOF) {
free(str);
return EOF;
}
if ((nlstop && c == '\n') && !rmstop)
str[totlen - 1] = '\n';
if ((nullstop && c == '\0') && !rmstop)
str[totlen - 1] = '\0';
str[totlen] = '\0';
newstr = stralloc();
newstr->s_len = totlen;
newstr->s_str = str;
*retstr = newstr;
return 0;
}
/*
* Return the next character from an opened file.
* Returns EOF if there was an error or end of file.
*/
int
getcharid(FILEID id)
{
FILEIO *fiop;
FILEPOS fpos;
fiop = findid(id, false);
if (fiop == NULL)
return -2;
if (fiop->action == 'w') {
f_tell(fiop->fp, &fpos);
fflush(fiop->fp);
if (f_seek_set(fiop->fp, &fpos) < 0)
return -3;
}
fiop->action = 'r';
return fgetc(fiop->fp);
}
/*
* Print out the name of an opened file.
* If the file has been closed, a null name is printed.
* If flags contain PRINT_UNAMBIG then extra information is printed
* identifying the output as a file and some data about it.
*/
int
printid(FILEID id, int flags)
{
FILEIO *fiop; /* file structure */
FILE *fp;
ZVALUE pos; /* file position */
/*
* filewall - file is closed
*/
fiop = findid(id, -1);
if (fiop == NULL) {
if (flags & PRINT_UNAMBIG)
math_fmt("FILE %ld closed", id);
else
math_str("\"\"");
return 1;
}
/*
* print quoted filename and mode
*/
if ((flags & PRINT_UNAMBIG) == 0) {
math_chr('"');
math_str(fiop->name);
math_chr('"');
return 0;
}
math_fmt("FILE %ld \"%s\" (%s", id, fiop->name, fiop->mode);
/*
* print file position
*/
fp = fiop->fp;
if (get_open_pos(fp, &pos) < 0) {
if (fileno(fp) > 2)
math_str("Error while determining file position!");
math_chr(')');
return 0;
}
math_str(", pos ");
zprintval(pos, 0, 0);
zfree(pos);
/*
* report special status
*/
if (ferror(fp))
math_str(", error");
if (feof(fp))
math_str(", eof");
math_chr(')');
printf(" fileno: %d ", fileno(fp));
return 0;
}
/*
* Print a formatted string similar to printf. Various formats of output
* are possible, depending on the format string AND the actual types of the
* values. Mismatches do not cause errors, instead something reasonable is
* printed instead. The output goes to the file with the specified id.
*
* given:
* id file id to print to
* count print count
* fmt standard format string
* vals table of values to print
*/
int
idprintf(FILEID id, char *fmt, int count, VALUE **vals)
{
FILEIO *fiop;
VALUE *vp;
char *str;
int ch;
size_t len;
int oldmode, newmode;
long olddigits, newdigits;
long width, precision;
bool didneg, didprecision;
FILEPOS fpos;
bool printstring;
bool printchar;
fiop = findid(id, true);
if (fiop == NULL)
return 1;
if (fiop->action == 'r') {
f_tell(fiop->fp, &fpos);
if (f_seek_set(fiop->fp, &fpos) < 0)
return 3;
}
fiop->action = 'w';
printstring = false;
printchar = false;
math_setfp(fiop->fp);
while ((ch = *fmt++) != '\0') {
if (ch != '%') {
math_chr(ch);
continue;
}
/*
* Here to handle formats.
*/
didneg = false;
didprecision = false;
width = 0;
precision = 0;
ch = *fmt++;
if (ch == '-') {
didneg = true;
ch = *fmt++;
}
while ((ch >= '0') && (ch <= '9')) {
width = width * 10 + (ch - '0');
ch = *fmt++;
}
if (ch == '.') {
didprecision = true;
ch = *fmt++;
while ((ch >= '0') && (ch <= '9')) {
precision = precision * 10 + (ch - '0');
ch = *fmt++;
}
}
if (ch == 'l')
ch = *fmt++;
oldmode = conf->outmode;
newmode = oldmode;
olddigits = conf->outdigits;
newdigits = olddigits;
if (didprecision)
newdigits = precision;
switch (ch) {
case 's':
printstring = true;
/*FALLTHRU*/
case 'c':
printchar = true;
case 'd':
break;
case 'f':
newmode = MODE_REAL;
break;
case 'e':
newmode = MODE_EXP;
break;
case 'n':
newmode = MODE_ENG;
break;
case 'g':
newmode = MODE_REAL_AUTO;
break;
case 'r':
newmode = MODE_FRAC;
break;
case 'o':
newmode = MODE_OCTAL;
break;
case 'x':
newmode = MODE_HEX;
break;
case 'b':
newmode = MODE_BINARY;
break;
case 0:
math_setfp(stdout);
return 0;
default:
math_chr(ch);
continue;
}
if (--count < 0) {
while (width-- > 0)
math_chr(' ');
continue;
}
vp = *vals++;
math_setdigits(newdigits);
math_setmode(newmode);
/*
* If there is no width specification, or if the type of
* value requires multiple lines, then just output the
* value directly.
*/
if ((width == 0) ||
(vp->v_type == V_MAT) || (vp->v_type == V_LIST)) {
switch(vp->v_type) {
case V_OCTET:
if (printstring)
math_str((char *)vp->v_octet);
else if (printchar)
math_chr(*vp->v_octet);
else
printvalue(vp, PRINT_NORMAL);
break;
case V_BLOCK:
if (printstring)
math_str((char *)
vp->v_block->data);
else if (printchar)
math_chr(*vp->v_block->data);
else
printvalue(vp, PRINT_NORMAL);
break;
case V_NBLOCK:
if (printstring) {
if (vp->v_nblock->blk->data !=
NULL)
math_str((char *)
vp->v_nblock
->blk->data);
} else if (printchar) {
if (vp->v_nblock->blk->data !=
NULL)
math_chr(*vp->v_nblock->
blk->data);
} else {
printvalue(vp, PRINT_NORMAL);
}
break;
default:
printvalue(vp, PRINT_NORMAL);
}
math_setmode(oldmode);
math_setdigits(olddigits);
continue;
}
/*
* There is a field width. Collect the output in a string,
* print it padded appropriately with spaces, and free it.
* However, if the output contains a newline, then ignore
* the field width.
*/
math_divertio();
switch(vp->v_type) {
case V_OCTET:
if (printstring)
math_str((char *)vp->v_octet);
else if (printchar)
math_chr(*vp->v_octet);
else
printvalue(vp, PRINT_NORMAL);
break;
case V_BLOCK:
if (printstring)
math_str((char *)vp->v_block->data);
else if (printchar)
math_chr(*vp->v_block->data);
else
printvalue(vp, PRINT_NORMAL);
break;
case V_NBLOCK:
if (printstring) {
if (vp->v_nblock->blk->data != NULL)
math_str((char *)
vp->v_nblock->blk->data);
}
else if (printchar) {
if (vp->v_nblock->blk->data != NULL)
math_chr(*vp->v_nblock->blk->data);
}
else
printvalue(vp, PRINT_NORMAL);
break;
default:
printvalue(vp, PRINT_NORMAL);
}
str = math_getdivertedio();
if (strchr(str, '\n'))
width = 0;
len = strlen(str);
while (!didneg && ((size_t)width > len)) {
width--;
math_chr(' ');
}
math_str(str);
free(str);
while (didneg && ((size_t)width > len)) {
width--;
math_chr(' ');
}
math_setmode(oldmode);
math_setdigits(olddigits);
}
math_setfp(stdout);
return 0;
}
/*
* Write a character to a file.
*
* given:
* id file id to print to
* ch character to write
*/
int
idfputc(FILEID id, int ch)
{
FILEIO *fiop;
FILEPOS fpos;
/* get the file info pointer */
fiop = findid(id, true);
if (fiop == NULL)
return 1;
if (fiop->action == 'r') {
f_tell(fiop->fp, &fpos);
if (f_seek_set(fiop->fp, &fpos) < 0)
return 2;
}
fiop->action = 'w';
/* set output to file */
math_setfp(fiop->fp);
/* write char */
math_chr(ch);
/* restore output to stdout */
math_setfp(stdout);
return 0;
}
/*
* Unget a character read from a file.
*
* given:
* id file id to print to
* ch character to write
*/
int
idungetc(FILEID id, int ch)
{
FILEIO *fiop;
fiop = findid(id, false);
if (fiop == NULL)
return -2;
if (fiop->action != 'r')
return -2;
return ungetc(ch, fiop->fp);
}
/*
* Write a string to a file.
*
* given:
* id file id to print to
* str string to write
*/
int
idfputs(FILEID id, STRING *str)
{
FILEIO *fiop;
FILEPOS fpos;
FILE *fp;
char *c;
long len;
/* get the file info pointer */
fiop = findid(id, true);
if (fiop == NULL)
return 1;
if (fiop->action == 'r') {
f_tell(fiop->fp, &fpos);
if (f_seek_set(fiop->fp, &fpos) < 0)
return 2;
}
fiop->action = 'w';
fp = fiop->fp;
len = str->s_len;
c = str->s_str;
while (len-- > 0)
fputc(*c++, fp);
return 0;
}
/*
* Same as idfputs but writes a terminating null character
*
* given:
* id file id to print to
* str string to write
*/
int
idfputstr(FILEID id, char *str)
{
FILEIO *fiop;
FILEPOS fpos;
/* get the file info pointer */
fiop = findid(id, true);
if (fiop == NULL)
return 1;
if (fiop->action == 'r') {
f_tell(fiop->fp, &fpos);
if (f_seek_set(fiop->fp, &fpos) < 0)
return 2;
}
fiop->action = 'w';
/* set output to file */
math_setfp(fiop->fp);
/* write the string */
math_str(str);
math_chr('\0');
/* restore output to stdout */
math_setfp(stdout);
return 0;
}
int
rewindid(FILEID id)
{
FILEIO *fiop;
fiop = findid(id, -1);
if (fiop == NULL)
return 1;
rewind(fiop->fp);
fiop->action = 0;
return 0;
}
void
rewindall(void)
{
FILEIO *fiop;
int i;
for (i = 3; i < idnum; i++) {
fiop = &files[ioindex[i]];
if (fiop != NULL) {
(void) rewind(fiop->fp);
fiop->action = 0;
}
}
}
/*
* filepos2z - convert a positive file position into a ZVALUE
*
* given:
* pos file position
*
* returns:
* file position as a ZVALUE
*
* NOTE: Does not support negative file positions.
*/
/*ARGSUSED*/
S_FUNC ZVALUE
filepos2z(FILEPOS pos)
{
ZVALUE ret; /* ZVALUE file position to return */
/*
* store FILEPOS in a ZVALUE as a positive value
*/
ret.len = FILEPOS_BITS/BASEB;
ret.v = alloc(ret.len);
zclearval(ret);
SWAP_HALF_IN_FILEPOS(ret.v, &pos);
ret.sign = 0;
ztrim(&ret);
/*
* return our result
*/
return ret;
}
/*
* z2filepos - convert a positive ZVALUE file position to a FILEPOS
*
* given:
* zpos file position as a ZVALUE
*
* returns:
* file position as a FILEPOS
*
* NOTE: Does not support negative file positions.
*/
S_FUNC FILEPOS
z2filepos(ZVALUE zpos)
{
#if FILEPOS_BITS > FULL_BITS
FILEPOS tmp; /* temp file position as a FILEPOS */
#endif
FILEPOS ret; /* file position as a FILEPOS */
#if FILEPOS_BITS < FULL_BITS
long pos; /* zpos as a long */
#else
FULL pos; /* zpos as a FULL */
#endif
/*
* firewall
*/
zpos.sign = 0; /* deal only with the absolute value */
/*
* quick return if the position can fit into a long
*/
#if FILEPOS_BITS == FULL_BITS
/* ztofull puts the value into native byte order */
pos = ztofull(zpos);
memset(&ret, 0, sizeof(ret)); /* FILEPOS could be non-scalar */
memcpy((void *)&ret, (void *)&pos, MIN(sizeof(ret), sizeof(pos)));
return ret;
#elif FILEPOS_BITS < FULL_BITS
/* ztofull puts the value into native byte order */
pos = ztolong(zpos);
memset(&ret, 0, sizeof(ret)); /* FILEPOS could be non-scalar */
memcpy((void *)&ret, (void *)&pos, MIN(sizeof(ret), sizeof(pos)));
return ret;
#else /* FILEPOS_BITS > FULL_BITS */
if (!zgtmaxfull(zpos)) {
/* ztofull puts the value into native byte order */
pos = ztofull(zpos);
memset(&ret, 0, sizeof(ret)); /* FILEPOS could be non-scalar */
memcpy((void *)&ret, (void *)&pos,
MIN(sizeof(ret), sizeof(pos)));
return ret;
}
/*
* copy (and swap if needed) lower part of the ZVALUE as needed
*/
if (zpos.len >= FILEPOS_BITS/BASEB) {
/* copy the lower FILEPOS_BITS of the ZVALUE */
memset(&tmp, 0, sizeof(tmp)); /* FILEPOS could be non-scalar */
memcpy(&tmp, zpos.v, MIN(sizeof(tmp), FILEPOS_LEN));
} else {
/* copy what bits we can into the temp value */
memset(&tmp, 0, sizeof(tmp)); /* FILEPOS could be non-scalar */
memcpy(&tmp, zpos.v, MIN(sizeof(tmp),
MIN(zpos.len*BASEB/8, FILEPOS_LEN)));
}
/* swap into native byte order */
SWAP_HALF_IN_FILEPOS(&ret, &tmp);
/*
* return our result
*/
return ret;
#endif /* FILEPOS_BITS <= FULL_BITS */
}
/*
* get_open_pos - get a an open file position
*
* given:
* fp open file stream
* res where to place the file position (ZVALUE)
*
* returns:
* 0 res points to the file position
* -1 error
*/
S_FUNC int
get_open_pos(FILE *fp, ZVALUE *res)
{
FILEPOS pos; /* current file position */
/*
* get the file position
*/
if (f_tell(fp, &pos) < 0) {
/* cannot get file position, return -1 */
return -1;
}
/*
* update file position and return success
*/
*res = filepos2z(pos);
return 0;
}
/*
* getloc - get the current position of the file
*
* given:
* id file id of the file
* loc pointer to result
*
* returns:
* 0 able to get file position
* -1 unable to get file position
*/
int
getloc(FILEID id, ZVALUE *res)
{
FILEIO *fiop; /* file structure */
FILE *fp;
/*
* convert id to stream
*/
fiop = findid(id, -1);
if (fiop == NULL) {
/* file not open */
return -1;
}
fp = fiop->fp;
if (fp == NULL) {
math_error("Bogus internal file pointer!");
not_reached();
}
/*
* return result
*/
return get_open_pos(fp, res);
}
int
ftellid(FILEID id, ZVALUE *res)
{
FILEIO *fiop;
FILEPOS fpos; /* current file position */
/* get FILEIO */
fiop = findid(id, -1);
if (fiop == NULL)
return -2;
/* get the file position */
if (f_tell(fiop->fp, &fpos) < 0)
return -3;
/* convert file position to ZVALUE */
*res = filepos2z(fpos);
return 0;
}
int
fseekid(FILEID id, ZVALUE offset, int whence)
{
FILEIO *fiop; /* FILEIO of file */
FILEPOS off; /* offset as a FILEPOS */
ZVALUE cur, tmp; /* current or end of file location */
int ret = 0; /* return code */
/* setup */
fiop = findid(id, -1);
if (fiop == NULL)
return -2;
/* seek depending on whence */
switch (whence) {
case 0:
/* construct seek position, off = offset */
if (zisneg(offset))
return -3;
off = z2filepos(offset);
/* seek there */
ret = f_seek_set(fiop->fp, &off);
break;
case 1:
/* construct seek position, off = cur+offset */
f_tell(fiop->fp, &off);
cur = filepos2z(off);
zadd(cur, offset, &tmp);
zfree(cur);
if (zisneg(tmp)) {
zfree(tmp);
return -3;
}
off = z2filepos(tmp);
zfree(tmp);
/* seek there */
ret = f_seek_set(fiop->fp, &off);
break;
case 2:
/* construct seek position, off = len+offset */
if (get_open_siz(fiop->fp, &cur) < 0)
return -4;
zadd(cur, offset, &tmp);
zfree(cur);
if (zisneg(tmp)) {
zfree(tmp);
return -3;
}
off = z2filepos(tmp);
zfree(tmp);
/* seek there */
ret = f_seek_set(fiop->fp, &off);
break;
default:
return -5;
}
return ret;
}
/*
* set_open_pos - set a an open file position
*
* given:
* fp open file stream
* zpos file position (ZVALUE) to set
*
* returns:
* 0 res points to the file position
* -1 error
*
* NOTE: Due to fsetpos limitation, position is set relative to only
* the beginning of the file.
*/
S_FUNC int
set_open_pos(FILE *fp, ZVALUE zpos)
{
FILEPOS pos; /* current file position */
/*
* convert ZVALUE to file position
*/
pos = z2filepos(zpos);
/*
* set the file position
*/
if (f_seek_set(fp, &pos) < 0) {
/* cannot set file position, return -1 */
return -1;
}
/*
* return success
*/
return 0;
}
/*
* setloc - set the current position of the file
*
* given:
* id file id of the file
* zpos file position (ZVALUE) to set
*
* returns:
* 0 able to set file position
* -1 unable to set file position
*/
int
setloc(FILEID id, ZVALUE zpos)
{
FILEIO *fiop; /* file structure */
FILE *fp;
/*
* firewall
*/
if ((id == FILEID_STDIN) || (id == FILEID_STDOUT) ||
(id == FILEID_STDERR)) {
math_error("Cannot fseek stdin, stdout, or stderr");
not_reached();
}
/*
* convert id to stream
*/
fiop = findid(id, -1);
if (fiop == NULL) {
/* file not open */
return -1;
}
fp = fiop->fp;
if (fp == NULL) {
math_error("Bogus internal file pointer!");
not_reached();
}
fiop->action = 0;
/*
* return result
*/
return set_open_pos(fp, zpos);
}
/*
* off_t2z - convert an off_t into a ZVALUE
*
* given:
* siz file size
*
* returns:
* file size as a ZVALUE
*/
/*ARGSUSED*/
S_FUNC ZVALUE
off_t2z(off_t siz)
{
ZVALUE ret; /* ZVALUE file size to return */
/*
* store off_t in a ZVALUE as a positive value
*/
ret.len = OFF_T_BITS/BASEB;
ret.v = alloc(ret.len);
zclearval(ret);
SWAP_HALF_IN_OFF_T(ret.v, &siz);
ret.sign = 0;
ztrim(&ret);
/*
* return our result
*/
return ret;
}
/*
* dev2z - convert a stat.st_dev into a ZVALUE
*
* given:
* dev device
*
* returns:
* file size as a ZVALUE
*/
S_FUNC ZVALUE
dev2z(dev_t dev)
{
ZVALUE ret; /* ZVALUE file size to return */
/*
* store off_t in a ZVALUE as a positive value
*/
ret.len = DEV_BITS/BASEB;
ret.v = alloc(ret.len);
zclearval(ret);
SWAP_HALF_IN_DEV(ret.v, &dev);
ret.sign = 0;
ztrim(&ret);
/*
* return our result
*/
return ret;
}
/*
* inode2z - convert a stat.st_ino into a ZVALUE
*
* given:
* inode file size
*
* returns:
* file size as a ZVALUE
*/
/*ARGSUSED*/
S_FUNC ZVALUE
inode2z(ino_t inode)
{
ZVALUE ret; /* ZVALUE file size to return */
/*
* store off_t in a ZVALUE as a positive value
*/
ret.len = INODE_BITS/BASEB;
ret.v = alloc(ret.len);
zclearval(ret);
SWAP_HALF_IN_INODE(ret.v, &inode);
ret.sign = 0;
ztrim(&ret);
/*
* return our result
*/
return ret;
}
/*
* get_open_siz - get a an open file size
*
* given:
* fp open file stream
* res where to place the file size (ZVALUE)
*
* returns:
* 0 res points to the file size
* -1 error
*/
int
get_open_siz(FILE *fp, ZVALUE *res)
{
struct stat buf; /* file status */
/*
* get the file size
*/
if (fstat(fileno(fp), &buf) < 0) {
/* stat error */
return -1;
}
/*
* update file size and return success
*/
*res = off_t2z(buf.st_size);
return 0;
}
/*
* getsize - get the current size of the file
*
* given:
* id file id of the file
* res pointer to result
*
* returns:
* 0 able to get file size
* EOF system error
* other nonzero file not open or other problem
*/
int
getsize(FILEID id, ZVALUE *res)
{
FILEIO *fiop; /* file structure */
FILE *fp;
/*
* convert id to stream
*/
fiop = findid(id, -1);
if (fiop == NULL) {
/* file not open */
return 1;
}
fp = fiop->fp;
if (fp == NULL) {
return 2;
}
/*
* return result
*/
return get_open_siz(fp, res);
}
/*
* getdevice - get the device of the file
*
* given:
* id file id of the file
* dev pointer to the result
*
* returns:
* 0 able to get device
* -1 unable to get device
*/
int
get_device(FILEID id, ZVALUE *dev)
{
FILEIO *fiop; /* file structure */
/*
* convert id to stream
*/
fiop = findid(id, -1);
if (fiop == NULL) {
/* file not open */
return -1;
}
/*
* return result
*/
*dev = dev2z(fiop->dev);
return 0;
}
/*
* getinode - get the inode of the file
*
* given:
* id file id of the file
* inode pointer to the result
*
* returns:
* 0 able to get inode
* -1 unable to get inode
*/
int
get_inode(FILEID id, ZVALUE *inode)
{
FILEIO *fiop; /* file structure */
/*
* convert id to stream
*/
fiop = findid(id, -1);
if (fiop == NULL) {
/* file not open */
return -1;
}
/*
* return result
*/
*inode = inode2z(fiop->inode);
return 0;
}
S_FUNC off_t
filesize(FILEIO *fiop)
{
struct stat sbuf;
/* return length */
if (fstat(fileno(fiop->fp), &sbuf) < 0) {
math_error("bad fstat");
not_reached();
}
return sbuf.st_size;
}
ZVALUE
zfilesize(FILEID id)
{
FILEIO *fiop;
off_t len; /* file length */
ZVALUE ret; /* file size as a ZVALUE return value */
/* file FILEIO */
fiop = findid(id, -1);
if (fiop == NULL) {
/* return neg value for non-file error */
itoz(-1, &ret);
return ret;
}
/* get length */
len = filesize(fiop);
ret = off_t2z(len);
return ret;
}
void
showfiles(void)
{
bool listed[MAXFILES];
FILEIO *fiop;
FILE *fp;
struct stat sbuf;
ino_t inodes[MAXFILES];
off_t sizes[MAXFILES];
int i, j;
for (i = 0; i < idnum; i++) {
listed[i] = false;
fiop = &files[ioindex[i]];
fp = fiop->fp;
if (fstat(fileno(fp), &sbuf) < 0) {
printf("Bad fstat for file %d\n", (int) fiop->id);
sizes[i] = -1;
} else {
inodes[i] = sbuf.st_ino;
sizes[i] = sbuf.st_size;
}
}
for (i = 0; i < idnum; i++) {
if (listed[i])
continue;
fiop = &files[ioindex[i]];
printf("\t");
printid(fiop->id, PRINT_UNAMBIG);
if (sizes[i] == -1) {
math_chr('\n');
continue;
}
printf(" size = %ld\n", (long int)sizes[i]);
for (j = i + 1; j < idnum; j++) {
if (listed[j] || sizes[j] == -1)
continue;
if (inodes[j] == inodes[i]) {
listed[j] = true;
fiop = &files[ioindex[j]];
printf("\t = ");
printid(fiop->id, PRINT_UNAMBIG);
printf("\n");
}
}
}
printf("\tNumber open = %d\n", idnum);
printf("\tLastid = %d\n", (int) lastid);
}
/*
* getscanfield - scan a field separated by some characters
*
* given:
* fp FILEID to scan
* skip
* width max field width
* scannum Number of characters in scanset
* scanptr string of characters considered separators
* strptr pointer to where the new field pointer may be found
*/
S_FUNC void
getscanfield(FILE *fp, bool skip, unsigned int width, int scannum,
char *scanptr, char **strptr)
{
char *str; /* current string */
unsigned long len; /* current length of string */
unsigned long totlen; /* total length of string */
char buf[READSIZE]; /* temporary buffer */
int c;
char *b;
bool comp; /* Use complement of scanset */
unsigned int chnum;
totlen = 0;
str = NULL;
comp = (scannum < 0);
if (comp)
scannum = -scannum;
chnum = 0;
for (;;) {
len = 0;
b = buf;
for(;;) {
c = fgetc(fp);
if (c == EOF || c == '\0')
break;
chnum++;
if(scannum &&
((memchr(scanptr,c,scannum)==NULL) ^ comp))
break;
if (!skip) {
*b++ = c;
len++;
if (len >= READSIZE)
break;
}
if (chnum == width)
break;
}
if (!skip) {
if (totlen)
str = (char *) realloc(str, totlen + len + 1);
else
str = (char *) malloc(len + 1);
if (str == NULL) {
math_error("Out of memory for scanning");
not_reached();
}
if (len)
memcpy(&str[totlen], buf, len);
totlen += len;
}
if (len < READSIZE)
break;
}
if (!(width && chnum == width) && c != '\0')
ungetc(c, fp);
if (!skip) {
str[totlen] = '\0';
*strptr = str;
}
}
/*
* getscanwhite - scan a field separated by whitespace
*
* given:
* fp FILEID to scan
* skip
* width max field width
* scannum Number of characters in scanset
* strptr pointer to where the new field pointer may be found
*/
S_FUNC void
getscanwhite(FILE *fp, bool skip, unsigned int width, int scannum,
char **strptr)
{
char *str; /* current string */
unsigned long len; /* current length of string */
unsigned long totlen; /* total length of string */
char buf[READSIZE]; /* temporary buffer */
int c;
char *b;
bool comp; /* Use complement of scanset */
unsigned int chnum;
totlen = 0;
str = NULL;
comp = (scannum < 0);
if (comp)
scannum = -scannum;
chnum = 0;
for (;;) {
len = 0;
b = buf;
for(;;) {
c = fgetc(fp);
if (c == EOF || c == '\0')
break;
chnum++;
if(scannum && (!isspace(c) ^ comp))
break;
if (!skip) {
*b++ = c;
len++;
if (len >= READSIZE)
break;
}
if (chnum == width)
break;
}
if (!skip) {
if (totlen)
str = (char *) realloc(str, totlen + len + 1);
else
str = (char *) malloc(len + 1);
if (str == NULL) {
math_error("Out of memory for scanning");
not_reached();
}
if (len)
memcpy(&str[totlen], buf, len);
totlen += len;
}
if (len < READSIZE)
break;
}
if (!(width && chnum == width) && c != '\0')
ungetc(c, fp);
if (!skip) {
str[totlen] = '\0';
*strptr = str;
}
}
S_FUNC int
fscanfile(FILE *fp, char *fmt, int count, VALUE **vals)
{
int assnum; /* Number of assignments made */
int c; /* Character read from file */
char f; /* Character read from format string */
int scannum; /* Number of characters in scanlist */
char *scanptr; /* Start of scanlist */
char *str;
bool comp; /* True scanset is complementary */
bool skip; /* True if string to be skipped rather than read */
int width;
VALUE *var; /* lvalue to be assigned to */
unsigned short subtype; /* for var->v_subtype */
FILEPOS cur; /* current location */
if (feof(fp))
return EOF;
assnum = 0;
for (;;) {
for (;;) {
f = *fmt++;
if (isspace((int)f)) {
getscanwhite(fp,1,0,6,NULL);
do {
f = *fmt++;
} while (isspace((int)f));
}
c = fgetc(fp);
if (c == EOF)
return assnum;
if (f == '%') {
f = *fmt++;
if (f != '%' && f != '\0')
break;
}
if (f != c || f == '\0') {
ungetc(c, fp);
return assnum;
}
}
ungetc(c, fp);
skip = (f == '*');
if (!skip && count == 0) {
return assnum;
}
if (skip)
f = *fmt++;
width = 0;
while (f >= '0' && f <= '9') {
width = 10 * width + f - '0';
f = *fmt++;
}
switch (f) {
case 'c':
if (width == 0)
width = 1;
getscanfield(fp,skip,width,0,NULL,&str);
break;
case 's':
getscanwhite(fp,1,0,6,NULL);
if (feof(fp))
return assnum;
getscanwhite(fp,skip,width,-6,&str);
break;
case '[':
f = *fmt;
comp = (f == '^');
if (comp)
f = *++fmt;
scanptr = fmt;
if (f == '\0')
return assnum;
fmt = strchr((f == ']' ? fmt + 1 : fmt), ']');
if (fmt == NULL)
return assnum;
scannum = fmt - scanptr;
if (comp)
scannum = -scannum;
fmt++;
getscanfield(fp,skip,
width,scannum,scanptr,&str);
break;
case 'f':
case 'e':
case 'r':
case 'i':
getscanwhite(fp,1,0,6, NULL);
if (feof(fp))
return assnum;
if (skip) {
fskipnum(fp);
continue;
}
assnum++;
var = *vals++;
if (var->v_type != V_ADDR)
math_error("This should not happen!!");
var = var->v_addr;
subtype = var->v_subtype;
freevalue(var);
count--;
freadsum(fp, var);
var->v_subtype = subtype;
continue;
case 'n':
assnum++;
var = *vals++;
count--;
if (var->v_type != V_ADDR)
math_error("This should not happen!!");
var = var->v_addr;
subtype = var->v_subtype;
freevalue(var);
var->v_type = V_NUM;
var->v_num = qalloc();
f_tell(fp, &cur);
var->v_num->num = filepos2z(cur);
var->v_subtype = subtype;
continue;
default:
fprintf(stderr, "Unsupported scan specifier");
return assnum;
}
if (!skip) {
assnum++;
var = *vals++;
count--;
if (var->v_type != V_ADDR)
math_error("Assigning to non-variable");
var = var->v_addr;
subtype = var->v_subtype;
freevalue(var);
var->v_type = V_STR;
var->v_str = makestring(str);
}
}
}
int
fscanfid(FILEID id, char *fmt, int count, VALUE **vals)
{
FILEIO *fiop;
FILE *fp;
FILEPOS fpos;
fiop = findid(id, false);
if (fiop == NULL)
return -2;
fp = fiop->fp;
if (fiop->action == 'w') {
f_tell(fp, &fpos);
fflush(fp);
if (f_seek_set(fp, &fpos) < 0)
return -4;
}
fiop->action = 'r';
return fscanfile(fp, fmt, count, vals);
}
int
scanfstr(char *str, char *fmt, int count, VALUE **vals)
{
FILE *fp;
int i;
fp = tmpfile();
if (fp == NULL)
return EOF;
fputs(str, fp);
rewind(fp);
i = fscanfile(fp, fmt, count, vals);
fclose(fp);
return i;
}
/*
* Read a number in floating-point format from a file. The first dot,
* if any, is considered as the decimal point; later dots are ignored.
* For example, -23.45..67. is interpreted as -23.4567
* An optional 'e' or 'E' indicates multiplication by a power or 10,
* e.g. -23.45e-6 has the effect of -23.45 * 10^-6. The reading
* ceases when a character other than a digit, a leading sign,
* a sign immediately following 'e' or 'E', or a dot is encountered.
* Absence of digits is interpreted as zero.
*/
S_FUNC void
freadnum(FILE *fp, VALUE *valptr)
{
ZVALUE num, zden, newnum, newden, div, tmp;
NUMBER *q;
COMPLEX *c;
VALUE val;
char ch;
LEN i;
HALF *a;
FULL f;
long decimals, exp;
bool sign, negexp, havedp, imag, exptoobig;
decimals = 0;
exp = 0;
sign = false;
negexp = false;
havedp = false;
imag = false;
exptoobig = false;
ch = fgetc(fp);
if (ch == '+' || ch == '-') {
if (ch == '-')
sign = true;
ch = fgetc(fp);
}
num.v = alloc(1);
*num.v = 0;
num.len = 1;
num.sign = sign;
for (;;) {
if (ch >= '0' && ch <= '9') {
f = (FULL) (ch - '0');
a = num.v;
i = num.len;
while (i-- > 0) {
f = 10 * (FULL) *a + f;
*a++ = (HALF) f;
f >>= BASEB;
}
if (f) {
a = alloc(num.len + 1);
memcpy(a, num.v, num.len * sizeof(HALF));
a[num.len] = (HALF) f;
num.len++;
freeh(num.v);
num.v = a;
}
if (havedp)
decimals++;
}
else if (ch == '.')
havedp = true;
else
break;
ch = fgetc(fp);
}
if (ch == 'e' || ch == 'E') {
ch = fgetc(fp);
if (ch == '+' || ch == '-') {
if (ch == '-')
negexp = true;
ch = fgetc(fp);
}
while (ch >= '0' && ch <= '9') {
if (!exptoobig) {
exp = (exp * 10) + ch - '0';
if (exp > 1000000)
exptoobig = true;
}
ch = fgetc(fp);
}
}
if (ch == 'i' || ch == 'I') {
imag = true;
} else {
ungetc(ch, fp);
}
if (ziszero(num)) {
zfree(num);
val.v_type = V_NUM;
val.v_subtype = V_NOSUBTYPE;
val.v_num = qlink(&_qzero_);
*valptr = val;
return;
}
if (exptoobig) {
zfree(num);
*valptr = error_value(E_BIGEXP);
return;
}
ztenpow(decimals, &zden);
if (exp) {
ztenpow(exp, &tmp);
if (negexp) {
zmul(zden, tmp, &newden);
zfree(zden);
zden = newden;
} else {
zmul(num, tmp, &newnum);
zfree(num);
num = newnum;
}
zfree(tmp);
}
if (!zisunit(num) && !zisunit(zden)) {
zgcd(num, zden, &div);
if (!zisunit(div)) {
zequo(num, div, &newnum);
zfree(num);
zequo(zden, div, &newden);
zfree(zden);
num = newnum;
zden = newden;
}
zfree(div);
}
q = qalloc();
q->num = num;
q->den = zden;
if (imag) {
c = comalloc();
qfree(c->imag);
c->imag = q;
val.v_type = V_COM;
val.v_com = c;
} else {
val.v_type = V_NUM;
val.v_num = q;
}
val.v_subtype = V_NOSUBTYPE;
*valptr = val;
}
S_FUNC void
freadsum(FILE *fp, VALUE *valptr)
{
VALUE v1, v2, v3;
char ch;
freadprod(fp, &v1);
ch = fgetc(fp);
while (ch == '+' || ch == '-') {
freadprod(fp, &v2);
if (ch == '+')
addvalue(&v1, &v2, &v3);
else
subvalue(&v1, &v2, &v3);
freevalue(&v1);
freevalue(&v2);
v1 = v3;
ch = fgetc(fp);
}
ungetc(ch, fp);
*valptr = v1;
}
S_FUNC void
freadprod(FILE *fp, VALUE *valptr)
{
VALUE v1, v2, v3;
char ch;
freadnum(fp, &v1);
ch = fgetc(fp);
while (ch == '*' || ch == '/') {
freadnum(fp, &v2);
if (ch == '*')
mulvalue(&v1, &v2, &v3);
else
divvalue(&v1, &v2, &v3);
freevalue(&v1);
freevalue(&v2);
v1 = v3;
ch = fgetc(fp);
}
ungetc(ch, fp);
*valptr = v1;
}
S_FUNC void
fskipnum(FILE *fp)
{
char ch;
do {
ch = fgetc(fp);
if (ch == '+' || ch == '-')
ch = fgetc(fp);
while ((ch >= '0' && ch <= '9') || ch == '.')
ch = fgetc(fp);
if (ch == 'e' || ch == 'E') {
ch = fgetc(fp);
if (ch == '+' || ch == '-')
ch = fgetc(fp);
while (ch >= '0' && ch <= '9')
ch = fgetc(fp);
}
if (ch == 'i' || ch == 'I')
ch = fgetc(fp);
} while (ch == '/' || ch == '*' || ch == '+' || ch == '-');
ungetc(ch, fp);
}
int
isattyid(FILEID id)
{
FILEIO *fiop;
fiop = findid(id, -1);
if (fiop == NULL)
return -2;
return isatty(fileno(fiop->fp));
}
/*
* fsearch - search for a string in a file
*
* given:
* id FILEID to search
* str string to look for
* pos file position to start at (NULL => current position)
*
* returns:
* EOF if system error
* other negative integer if file not open, etc.
* positive integer if string not found
* zero if string found, position stored at res
*
* XXX - This search is a translation of the original search that did not
* work with large files. The search algorithm used is slow and
* should be speed up much more.
*/
int
fsearch(FILEID id, char *str, ZVALUE start, ZVALUE end, ZVALUE *res)
{
FILEIO *fiop; /* FILEIO of file id */
FILEPOS cur; /* current file position */
ZVALUE tmp, tmp2; /* temporary ZVALUEs */
char c; /* str comparison character */
int r; /* character read from file */
char *s; /* str comparison pointer */
long k = 0;
/* get FILEIO */
fiop = findid(id, false);
if (fiop == NULL)
return -2;
/*
* file setup
*/
if (fiop->action == 'w')
fflush(fiop->fp);
zsub(end, start, &tmp2);
if (zisneg(tmp2)) {
zfree(tmp2);
return 1;
}
tmp.sign = 0;
tmp.len = tmp2.len;
tmp.v = alloc(tmp.len);
zcopyval(tmp2, tmp);
zfree(tmp2);
cur = z2filepos(start);
if (f_seek_set(fiop->fp, &cur) < 0) {
zfree(tmp);
return EOF;
}
/*
* search setup
*/
/* note the first str search character */
c = *str++;
if (c == '\0') {
zfree(tmp);
return 2;
}
clearerr(fiop->fp);
while ((r = fgetc(fiop->fp)) != EOF) {
if ((char)r == c) {
(void) f_tell(fiop->fp, &cur);
s = str;
while (*s) {
r = fgetc(fiop->fp);
if ((char)r != *s)
break;
s++;
}
if (r == EOF)
break;
if (*s == '\0') {
zfree(tmp);
tmp = filepos2z(cur);
zsub(tmp, _one_, res);
zfree(tmp);
return 0;
}
(void) f_seek_set(fiop->fp, &cur);
}
if (*tmp.v) {
(*tmp.v)--;
} else {
if (tmp.len == 1)
break;
k = 0;
do {
tmp.v[k++] = BASE1;
}
while (k < tmp.len && tmp.v[k] == 0);
if (k == tmp.len) {
math_error("This should not happen");
not_reached();
}
tmp.v[k]--;
if (tmp.v[tmp.len - 1] == 0)
tmp.len--;
}
}
zfree(tmp);
if (ferror(fiop->fp))
return EOF;
return 1;
}
/*
* frsearch - reverse search for a string in a file
*
* given:
* id FILEID to search
* str string to look for
* search starts at pos = first and continues for decreasing
* pos >= last
*
* returns:
* EOF if system error
* other negative integer if file not open, etc.
* positive integer if string not found
* zero if string found, position stored at res
*
* XXX - This search is a translation of the original search that did not
* work with large files. The search algorithm used is so slow
* as to be painful to the user and needs to be sped up much more.
*/
int
frsearch(FILEID id, char *str, ZVALUE first, ZVALUE last, ZVALUE *res)
{
FILEIO *fiop; /* FILEIO of file id */
FILEPOS cur; /* current file position */
ZVALUE pos; /* current file position as ZVALUE */
ZVALUE tmp; /* temporary ZVALUEs */
char c; /* str comparison character */
int r; /* character read from file */
char *s; /* str comparison pointer */
/* get FILEIO */
fiop = findid(id, false);
if (fiop == NULL)
return -2;
/*
* file setup
*/
if (fiop->action == 'w')
fflush(fiop->fp);
zcopy(first, &pos);
/*
* search setup
*/
/* note the first str search character */
c = *str++;
if (c == '\0') {
cur = z2filepos(pos);
if (f_seek_set(fiop->fp, &cur) < 0) {
zfree(pos);
return EOF;
}
*res = pos;
return 0;
}
clearerr(fiop->fp);
while(zrel(pos, last) >= 0) {
cur = z2filepos(pos);
if (f_seek_set(fiop->fp, &cur) < 0) {
zfree(pos);
return EOF;
}
r = fgetc(fiop->fp);
if (r == EOF) {
zfree(pos);
return EOF;
}
if ((char) r == c) {
s = str;
while (*s) {
r = fgetc(fiop->fp);
if ((char)r != *s)
break;
s++;
}
if (r == EOF) {
zfree(pos);
return EOF;
}
if (*s == '\0') {
*res = pos;
ungetc(r, fiop->fp);
return 0;
}
}
zsub(pos, _one_, &tmp);
zfree(pos);
pos = tmp;
}
cur = z2filepos(last);
f_seek_set(fiop->fp, &cur);
zfree(pos);
if (ferror(fiop->fp))
return EOF;
return 1;
}
char *
findfname(FILEID id)
{
FILEIO *fiop;
fiop = findid(id, -1);
if (fiop == NULL)
return NULL;
return fiop->name;
}