mirror of
https://github.com/lcn2/calc.git
synced 2025-08-19 01:13:27 +03:00
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
1151 lines
26 KiB
C
1151 lines
26 KiB
C
/*
|
|
* input - nested input source file reader
|
|
*
|
|
* Copyright (C) 1999-2007,2014,2018,2021-2023 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:16
|
|
* File existed as early as: before 1990
|
|
*
|
|
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
|
|
*/
|
|
|
|
/*
|
|
* Nested input source file reader.
|
|
* For terminal input, this also provides a simple command stack.
|
|
*/
|
|
|
|
|
|
#include <stdio.h>
|
|
#include <ctype.h>
|
|
#if !defined(_WIN32) && !defined(_WIN64)
|
|
# include <pwd.h>
|
|
#else
|
|
# include <stdlib.h>
|
|
#endif
|
|
#include <sys/types.h>
|
|
#include <sys/stat.h>
|
|
|
|
#include "have_unistd.h"
|
|
#if defined(HAVE_UNISTD_H)
|
|
#include <unistd.h>
|
|
#endif
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
#include <limits.h>
|
|
#define _fullpath(f,n,s) (_fixpath(n,f),f)
|
|
#endif
|
|
|
|
#include "calc.h"
|
|
#include "conf.h"
|
|
#include "hist.h"
|
|
#include "strl.h"
|
|
|
|
|
|
#include "errtbl.h"
|
|
#include "banned.h" /* include after system header <> includes */
|
|
|
|
|
|
EXTERN int stdin_tty; /* TRUE if stdin is a tty */
|
|
E_FUNC FILE *f_open(char *name, char *mode);
|
|
E_FUNC FILE *curstream(void);
|
|
|
|
|
|
#define TTYSIZE 8191 /* reallocation size for terminal buffers */
|
|
#define MAXDEPTH 255 /* maximum depth of input */
|
|
#define IS_READ 1 /* reading normally */
|
|
#define IS_REREAD 2 /* reread current character */
|
|
#define chartoint(ch) ((ch) & 0xff) /* make sure char is not negative */
|
|
#define READSET_ALLOC 8 /* readset to allocate chunk size */
|
|
|
|
|
|
typedef struct {
|
|
int i_state; /* state (read, reread) */
|
|
int i_char; /* currently read char */
|
|
long i_line; /* line number */
|
|
char *i_cp; /* pointer to string character to be read */
|
|
char *i_str; /* start of string copy to be read, or NULL */
|
|
long i_num; /* number of string characters remaining */
|
|
char *i_ttystr; /* current character of tty line (or NULL) */
|
|
FILE *i_fp; /* current file for input (if not NULL) */
|
|
char *i_name; /* file name if known */
|
|
} INPUT;
|
|
|
|
|
|
/* files that calc has read or included */
|
|
typedef struct {
|
|
int active; /* != 0 => active entry, 0 => unused entry */
|
|
char *name; /* name used to read file */
|
|
char *path; /* real path used to open file */
|
|
struct stat inode; /* inode information for file */
|
|
} READSET;
|
|
|
|
STATIC READSET *readset = NULL; /* array of files read */
|
|
STATIC int maxreadset = 0; /* length of readset */
|
|
|
|
STATIC int linesize; /* current max size of input line */
|
|
STATIC char *linebuf; /* current input line buffer */
|
|
STATIC char *prompt; /* current prompt for terminal */
|
|
STATIC BOOL noprompt; /* TRUE if should not print prompt */
|
|
|
|
STATIC int depth; /* current input depth */
|
|
STATIC INPUT *cip; /* current input source */
|
|
STATIC INPUT inputs[MAXDEPTH]; /* input sources */
|
|
|
|
|
|
S_FUNC int openfile(char *name);
|
|
S_FUNC int ttychar(void);
|
|
S_FUNC int isinoderead(struct stat *sbuf);
|
|
S_FUNC int findfreeread(void);
|
|
S_FUNC int addreadset(char *name, char *path, struct stat *sbuf);
|
|
S_FUNC char *homeexpand(char *name);
|
|
|
|
|
|
/*
|
|
* Open an input file by possibly searching through a path list
|
|
* and also possibly applying the specified extension. For example:
|
|
*
|
|
* opensearchfile("barf", ".:/tmp", ".c", rd_once)
|
|
*
|
|
* searches in order for the files:
|
|
*
|
|
* "./barf", "./barf.c", "/tmp/barf", and "/tmp/barf.c".
|
|
*
|
|
* Returns -1 if we could not open a file or error.
|
|
* Returns 1 if file was opened and added to/updated in the readset
|
|
* Returns 0 if file was already in the readset and reopen was 0.
|
|
*
|
|
* given:
|
|
* name file name to be read
|
|
* pathlist list of colon separated paths (or NULL)
|
|
* extension extra extension to try (or NULL)
|
|
* rd_once TRUE => do not reread a file
|
|
*/
|
|
int
|
|
opensearchfile(char *name, char *pathlist, char *extension, int rd_once)
|
|
{
|
|
int i;
|
|
char *cp;
|
|
char *path; /* name being searched for */
|
|
size_t path_alloc; /* length of malloced path */
|
|
struct stat statbuf; /* stat of the path */
|
|
size_t namelen; /* length of name */
|
|
size_t extlen; /* length of the extension if non-NULL or 0 */
|
|
size_t pathlen; /* length of the pathlist if non-NULL or 0 */
|
|
|
|
/* firewall */
|
|
if (name == NULL) {
|
|
math_error("NULL name given to opensearchfile");
|
|
not_reached();
|
|
}
|
|
|
|
/*
|
|
* We ignore the pathlist of the file is absolute, dot-relative
|
|
* or tilde-relative or if there is no search path.
|
|
*/
|
|
if (name[0] == PATHCHAR ||
|
|
name[0] == HOMECHAR ||
|
|
(name[0] == DOTCHAR && name[1] == '\0') ||
|
|
(name[0] == DOTCHAR && name[1] == DOTCHAR && name[2] == '\0') ||
|
|
(name[0] == DOTCHAR && name[1] == PATHCHAR) ||
|
|
(name[0] == DOTCHAR && name[1] == DOTCHAR && name[2] == PATHCHAR) ||
|
|
pathlist == NULL) {
|
|
pathlist = "";
|
|
}
|
|
|
|
/*
|
|
* allocate storage for the longest name being searched for
|
|
*
|
|
* We will allocate more than we could ever want/need.
|
|
* The longest we could ever need would be:
|
|
*
|
|
* pathlist (as a single long string)
|
|
* /
|
|
* name
|
|
* .
|
|
* extension
|
|
* \0
|
|
* guard byte
|
|
*/
|
|
namelen = strlen(name);
|
|
if (extension != NULL) {
|
|
extlen = strlen(extension);
|
|
} else {
|
|
extlen = 0;
|
|
}
|
|
pathlen = strlen(pathlist);
|
|
path_alloc = pathlen+1 + 1 + namelen+1 + extlen+1 + 1 + 1;
|
|
path = malloc(path_alloc+1);
|
|
if (path == NULL) {
|
|
math_error("Cannot allocate filename path buffer");
|
|
not_reached();
|
|
}
|
|
path[0] = '\0'; /* paranoia */
|
|
path[path_alloc] = '\0'; /* paranoia */
|
|
|
|
/*
|
|
* Don't try the extension if the filename already contains it.
|
|
*/
|
|
if (extension != NULL && namelen >= extlen &&
|
|
strcmp(&name[namelen-extlen], extension) == 0) {
|
|
extension = NULL;
|
|
}
|
|
|
|
/*
|
|
* Search through the path list for the file
|
|
*/
|
|
pathlist--;
|
|
do {
|
|
pathlist++;
|
|
cp = path;
|
|
while (*pathlist && (*pathlist != LISTCHAR))
|
|
*cp++ = *pathlist++;
|
|
if (cp != path)
|
|
*cp++ = PATHCHAR;
|
|
strlcpy(cp, name, namelen+1);
|
|
i = openfile(path);
|
|
if ((i < 0) &&
|
|
(extension != NULL && extension[0] != '\0')) {
|
|
strlcat(path, extension, path_alloc+1);
|
|
i = openfile(path);
|
|
}
|
|
} while ((i < 0) && *pathlist);
|
|
|
|
/* examine what our search produced */
|
|
if (i < 0) {
|
|
free(path);
|
|
return i;
|
|
}
|
|
if (cip->i_fp == NULL) {
|
|
/* cannot find a file to open */
|
|
free(path);
|
|
return -3;
|
|
}
|
|
if (fstat(fileno(cip->i_fp), &statbuf) < 0) {
|
|
/* unable to fstat the open file */
|
|
free(path);
|
|
return -4;
|
|
}
|
|
|
|
/* note if we will reopen a file and if that is allowed */
|
|
if (rd_once == TRUE && isinoderead(&statbuf) >= 0) {
|
|
/* file is in readset and reopen is false */
|
|
closeinput();
|
|
free(path);
|
|
return 1;
|
|
}
|
|
|
|
/* add this name to the readset if allowed */
|
|
if (addreadset(name, path, &statbuf) < 0) {
|
|
/* cannot add to readset */
|
|
closeinput();
|
|
free(path);
|
|
return -1;
|
|
}
|
|
|
|
/* file was added to/updated in readset */
|
|
free(path);
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
* f_pathopen - open an absolute or relative filename along a search path
|
|
*
|
|
* Open a file by possibly searching through a path list. For example:
|
|
*
|
|
* f_pathopen("curds", ".:/tmp:~/pub", "r", NULL)
|
|
*
|
|
* searches in order for a file that it can open for reading:
|
|
*
|
|
* "./curds", "/tmp/curds", "~/pub/curds"
|
|
*
|
|
* NOTE: ~ is expanded accordingly (see homeexpand() below).
|
|
*
|
|
* However is the file is /absolue/path/name or a ./dot/relative/name, or
|
|
* a ~/home/dir/name, or a ~user/home/name, then the pathlist is ignored
|
|
* we just attempt to open name.
|
|
*
|
|
* and opens the first one that exists and allows the mode.
|
|
*
|
|
* name file name to be read
|
|
* mode fopen() mode argument
|
|
* (one of "r", "w", "a", "r+", "w+", "a+")
|
|
* pathlist list of colon separated paths (or NULL)
|
|
* openpath if non-NULL, and file was opened, set to malloced
|
|
* path used to open
|
|
*
|
|
* returns:
|
|
* open file stream, NULL ==> file was not found or error
|
|
* If file was open and openpath was non-NULL, changed to point
|
|
* to path used to open
|
|
*/
|
|
FILE *
|
|
f_pathopen(char *name, char *mode, char *pathlist, char **openpath)
|
|
{
|
|
char *cp;
|
|
char *path; /* name being searched for */
|
|
size_t namelen; /* length of name */
|
|
size_t pathlen; /* length of the pathlist if non-NULL or 0 */
|
|
FILE *ret; /* return open stream or NULL */
|
|
|
|
/* firewall */
|
|
if (name == NULL) {
|
|
math_error("NULL name given to f_pathopen");
|
|
not_reached();
|
|
}
|
|
if (mode == NULL) {
|
|
math_error("NULL mode given to f_pathopen");
|
|
not_reached();
|
|
}
|
|
|
|
/*
|
|
* We ignore the pathlist of the file is absolute, dot-relative
|
|
* or tilde-relative or if there is no search path.
|
|
*/
|
|
if (name[0] == PATHCHAR ||
|
|
name[0] == HOMECHAR ||
|
|
(name[0] == DOTCHAR && name[1] == '\0') ||
|
|
(name[0] == DOTCHAR && name[1] == DOTCHAR && name[2] == '\0') ||
|
|
(name[0] == DOTCHAR && name[1] == PATHCHAR) ||
|
|
(name[0] == DOTCHAR && name[1] == DOTCHAR && name[2] == PATHCHAR) ||
|
|
pathlist == NULL) {
|
|
pathlist = "";
|
|
}
|
|
|
|
/*
|
|
* allocate storage for the longest name being searched for
|
|
*
|
|
* We will allocate more than we could ever want/need.
|
|
* The longest we could ever need would be:
|
|
*
|
|
* pathlist (as a single long string)
|
|
* /
|
|
* name
|
|
* \0
|
|
* guard byte
|
|
*/
|
|
namelen = strlen(name);
|
|
pathlen = strlen(pathlist);
|
|
path = malloc(pathlen+1 + 1 + namelen+1 + 1 + 1);
|
|
if (path == NULL) {
|
|
math_error("Cannot allocate f_pathopen buffer");
|
|
not_reached();
|
|
}
|
|
|
|
/*
|
|
* Search through the path list for the file
|
|
*/
|
|
pathlist--;
|
|
do {
|
|
pathlist++;
|
|
cp = path;
|
|
while (*pathlist && (*pathlist != LISTCHAR))
|
|
*cp++ = *pathlist++;
|
|
if (cp != path)
|
|
*cp++ = PATHCHAR;
|
|
strlcpy(cp, name, namelen+1);
|
|
ret = f_open(path, mode);
|
|
} while ((ret == NULL) && *pathlist);
|
|
|
|
/* if caller wants to know the path, malloc it and return it */
|
|
if (openpath != NULL && ret != NULL) {
|
|
if (path[0] == HOMECHAR) {
|
|
*openpath = homeexpand(path);
|
|
} else {
|
|
*openpath = strdup(path);
|
|
}
|
|
if (*openpath == NULL) {
|
|
free(path);
|
|
if ((conf->calc_debug & CALCDBG_TTY) && ret == stdin) {
|
|
printf("f_pathopen: closing stdin "
|
|
"on malloc return error\n");
|
|
}
|
|
fclose(ret);
|
|
math_error("cannot malloc return openpath buffer");
|
|
not_reached();
|
|
}
|
|
}
|
|
free(path);
|
|
|
|
/* return open file or NULL */
|
|
return ret;
|
|
}
|
|
|
|
|
|
/*
|
|
* Given a filename with a leading ~, expand it into a home directory for
|
|
* that user. This function will malloc the space for the expanded path.
|
|
*
|
|
* If the path is just ~, or begins with ~/, expand it to the home
|
|
* directory of the current user. If the environment variable $HOME
|
|
* is known, it will be used, otherwise the password file will be
|
|
* consulted.
|
|
*
|
|
* If the path is just ~username, or ~username/, expand it to the home
|
|
* directory of that user by looking it up in the password file.
|
|
*
|
|
* If the password file must be consulted and the username is not found
|
|
* a NULL pointer is returned.
|
|
*
|
|
* given:
|
|
* name a filename with a leading ~
|
|
*/
|
|
S_FUNC char *
|
|
homeexpand(char *name)
|
|
{
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
return NULL;
|
|
|
|
#else /* Windows free systems */
|
|
|
|
struct passwd *ent; /* password entry */
|
|
char *home2; /* fullpath of the home directory */
|
|
char *fullpath; /* the malloced expanded path */
|
|
char *after; /* after the ~user or ~ */
|
|
char *username; /* extracted username */
|
|
size_t fullpath_len; /* length of fullpath */
|
|
size_t snprintf_len; /* malloced snprintf buffer length */
|
|
|
|
/* firewall */
|
|
if (name[0] != HOMECHAR)
|
|
return NULL;
|
|
|
|
/*
|
|
* obtain the home directory component
|
|
*/
|
|
switch (name[1]) {
|
|
case PATHCHAR: /* ~/... */
|
|
case '\0': /* ~ */
|
|
home2 = home;
|
|
after = name+1;
|
|
break;
|
|
default: /* ~username or ~username/... */
|
|
|
|
/* extract the username after the ~ */
|
|
after = (char *)strchr(name+2, PATHCHAR);
|
|
if (after == NULL) {
|
|
/* path is just ~username */
|
|
ent = (struct passwd *)getpwnam(name+1);
|
|
if (ent == NULL) {
|
|
/* unknown user */
|
|
return NULL;
|
|
}
|
|
/* just malloc the home directory and return it */
|
|
fullpath_len = strlen(ent->pw_dir);
|
|
fullpath = (char *)malloc(fullpath_len+1);
|
|
if (fullpath == NULL) {
|
|
return NULL;
|
|
}
|
|
strlcpy(fullpath, ent->pw_dir, fullpath_len+1);
|
|
return fullpath;
|
|
}
|
|
username = (char *) malloc(after-name + 1 + 1);
|
|
if (username == NULL) {
|
|
/* failed to malloc username */
|
|
return NULL;
|
|
}
|
|
strlcpy(username, name+1, after-name+1+1);
|
|
|
|
/* get that user's home directory */
|
|
ent = (struct passwd *)getpwnam(username);
|
|
free(username);
|
|
if (ent == NULL) {
|
|
/* unknown user */
|
|
return NULL;
|
|
}
|
|
home2 = ent->pw_dir;
|
|
break;
|
|
}
|
|
|
|
/*
|
|
* build the fullpath given the home directory
|
|
*/
|
|
snprintf_len = strlen(home2)+strlen(after) + 1;
|
|
fullpath = (char *)malloc(snprintf_len+1);
|
|
if (fullpath == NULL) {
|
|
return NULL;
|
|
}
|
|
snprintf(fullpath, snprintf_len, "%s%s", home2, after);
|
|
fullpath[snprintf_len] = '\0'; /* paranoia */
|
|
return fullpath;
|
|
#endif /* Windows free systems */
|
|
}
|
|
|
|
|
|
/*
|
|
* f_open - ~-expand a filename and fopen() it
|
|
*
|
|
* given:
|
|
* name the filename to open
|
|
* mode fopen() mode argument
|
|
* (one of "r", "w", "a", "r+", "w+", "a+")
|
|
*/
|
|
FILE *
|
|
f_open(char *name, char *mode)
|
|
{
|
|
FILE *fp; /* open file descriptor */
|
|
char *fullname; /* file name with HOMECHAR expansion */
|
|
|
|
/*
|
|
* be sore we are allowed to open a file in this mode
|
|
*/
|
|
if (!allow_read && !allow_write) {
|
|
/* no reads and no writes means no opens! */
|
|
if (run_state > RUN_BEGIN) {
|
|
fprintf(stderr,
|
|
"open of %s mode %s - %s\n", name, mode,
|
|
"open for read or write disallowed by -m\n");
|
|
}
|
|
return NULL;
|
|
} else if (!allow_read && strchr(mode, 'r') != NULL) {
|
|
/* reading new files disallowed */
|
|
if (run_state > RUN_BEGIN) {
|
|
fprintf(stderr,
|
|
"open of %s mode %s - %s\n", name, mode,
|
|
"open for read disallowed by -m\n");
|
|
}
|
|
return NULL;
|
|
} else if (!allow_write &&
|
|
(strchr(mode, 'w') != NULL ||
|
|
strchr(mode, 'a') != NULL ||
|
|
strchr(mode, '+') != NULL)) {
|
|
/* writing new files disallowed */
|
|
if (run_state > RUN_BEGIN) {
|
|
fprintf(stderr,
|
|
"open of %s mode %s - %s\n", name, mode,
|
|
"open for write disallowed by -m\n");
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
/*
|
|
* expand ~ if needed
|
|
*/
|
|
if (name[0] == HOMECHAR) {
|
|
fullname = homeexpand(name);
|
|
if (fullname == NULL)
|
|
return NULL;
|
|
fp = fopen(fullname, mode);
|
|
free(fullname);
|
|
} else {
|
|
fp = fopen(name, mode);
|
|
}
|
|
return fp;
|
|
}
|
|
|
|
|
|
/*
|
|
* Setup for reading from a input file.
|
|
* Returns -1 if file could not be opened.
|
|
*
|
|
* given:
|
|
* name file name to be read
|
|
*/
|
|
S_FUNC int
|
|
openfile(char *name)
|
|
{
|
|
FILE *fp; /* open file descriptor */
|
|
size_t namelen;
|
|
|
|
if (depth >= MAXDEPTH)
|
|
return -2;
|
|
fp = f_open(name, "r");
|
|
if (fp == NULL)
|
|
return -1;
|
|
cip = inputs + depth++;
|
|
cip->i_state = IS_READ;
|
|
cip->i_char = '\0';
|
|
cip->i_str = NULL;
|
|
cip->i_ttystr = NULL;
|
|
cip->i_fp = fp;
|
|
cip->i_line = 1;
|
|
namelen = strlen(name);
|
|
cip->i_name = (char *)malloc(namelen+1);
|
|
if (cip->i_name == NULL) {
|
|
return -1;
|
|
}
|
|
strlcpy(cip->i_name, name, namelen+1);
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
* Return the current input file stream, or NULL if none.
|
|
*/
|
|
FILE *
|
|
curstream(void)
|
|
{
|
|
if (depth <= 0 || depth > MAXDEPTH)
|
|
return NULL;
|
|
return cip->i_fp;
|
|
}
|
|
|
|
|
|
/*
|
|
* Open a string for scanning, num characters to be read.
|
|
* String is copied into local memory so it can be trashed afterwards.
|
|
* Returns -1 if cannot open string.
|
|
*
|
|
* given:
|
|
* str string to be opened
|
|
* num length of string to open
|
|
*/
|
|
int
|
|
openstring(char *str, size_t num)
|
|
{
|
|
char *cp; /* copied string */
|
|
|
|
if ((depth >= MAXDEPTH) || (str == NULL))
|
|
return -2;
|
|
cp = (char *) malloc(num + 1);
|
|
if (cp == NULL)
|
|
return -1;
|
|
strlcpy(cp, str, num+1);
|
|
cip = inputs + depth++;
|
|
cip->i_state = IS_READ;
|
|
cip->i_char = '\0';
|
|
cip->i_cp = cp;
|
|
cip->i_str = cp;
|
|
cip->i_num = num;
|
|
cip->i_fp = NULL;
|
|
cip->i_name = NULL;
|
|
cip->i_ttystr = NULL;
|
|
cip->i_line = 1;
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
* Set to read input from the terminal.
|
|
* Returns -1 if there is no more depth for input.
|
|
*/
|
|
int
|
|
openterminal(void)
|
|
{
|
|
if (depth >= MAXDEPTH)
|
|
return -2;
|
|
cip = inputs + depth++;
|
|
cip->i_state = IS_READ;
|
|
cip->i_char = '\0';
|
|
cip->i_str = NULL;
|
|
cip->i_ttystr = NULL;
|
|
cip->i_fp = NULL;
|
|
cip->i_name = NULL;
|
|
cip->i_line = 1;
|
|
return 0;
|
|
}
|
|
|
|
|
|
/*
|
|
* Close the current input source.
|
|
*/
|
|
void
|
|
closeinput(void)
|
|
{
|
|
if (depth <= 0)
|
|
return;
|
|
if (cip->i_str)
|
|
free(cip->i_str);
|
|
if (cip->i_fp) {
|
|
if ((conf->calc_debug & CALCDBG_TTY) && cip->i_fp == stdin) {
|
|
printf("closeinput: closing stdin "
|
|
"at depth: %d\n", depth);
|
|
}
|
|
fclose(cip->i_fp);
|
|
}
|
|
if (cip->i_name)
|
|
free(cip->i_name);
|
|
depth--;
|
|
cip = depth ? &inputs[depth - 1] : NULL;
|
|
}
|
|
|
|
|
|
/*
|
|
* Reset the input sources back to the initial state.
|
|
*/
|
|
void
|
|
resetinput(void)
|
|
{
|
|
while (depth > 0)
|
|
closeinput();
|
|
noprompt = FALSE;
|
|
}
|
|
|
|
|
|
/*
|
|
* Set the prompt for terminal input.
|
|
*/
|
|
void
|
|
setprompt(char *str)
|
|
{
|
|
prompt = str;
|
|
noprompt = FALSE;
|
|
}
|
|
|
|
|
|
/*
|
|
* Read the next character from the current input source.
|
|
* End of file closes current input source, and returns EOF character.
|
|
*/
|
|
int
|
|
nextchar(void)
|
|
{
|
|
int ch; /* current input character */
|
|
|
|
if (depth == 0) /* input finished */
|
|
return EOF;
|
|
if (cip->i_state == IS_REREAD) { /* rereading current char */
|
|
ch = cip->i_char;
|
|
cip->i_state = IS_READ;
|
|
if (ch == '\n')
|
|
cip->i_line++;
|
|
return ch;
|
|
}
|
|
if (cip->i_str) { /* from string */
|
|
if (cip->i_num) {
|
|
ch = chartoint(*cip->i_cp++);
|
|
cip->i_num--;
|
|
} else {
|
|
ch = EOF;
|
|
}
|
|
} else if (cip->i_fp) { /* from file */
|
|
ch = fgetc(cip->i_fp);
|
|
} else if (!stdin_tty) { /* from file */
|
|
ch = fgetc(stdin);
|
|
} else { /* from terminal */
|
|
ch = ttychar();
|
|
}
|
|
if (depth > 0)
|
|
cip->i_char = ch; /* save for rereads */
|
|
if (ch == '\n')
|
|
cip->i_line++;
|
|
return ch;
|
|
}
|
|
|
|
|
|
/*
|
|
* Read in the next line of input from the current input source.
|
|
* The line is terminated with a null character, and does not contain
|
|
* the final newline character. The returned string is only valid
|
|
* until the next such call, and so must be copied if necessary.
|
|
* Returns NULL on end of file.
|
|
*/
|
|
char *
|
|
nextline(void)
|
|
{
|
|
char *cp;
|
|
int ch;
|
|
int len;
|
|
|
|
cp = linebuf;
|
|
if (linesize == 0) {
|
|
cp = (char *)malloc(TTYSIZE + 1);
|
|
if (cp == NULL) {
|
|
math_error("Cannot allocate line buffer");
|
|
not_reached();
|
|
}
|
|
linebuf = cp;
|
|
linesize = TTYSIZE;
|
|
}
|
|
len = 0;
|
|
for (;;) {
|
|
noprompt = TRUE;
|
|
ch = nextchar();
|
|
noprompt = FALSE;
|
|
if (ch == EOF)
|
|
return NULL;
|
|
if (ch == '\0')
|
|
continue;
|
|
if (ch == '\n')
|
|
break;
|
|
if (len >= linesize) {
|
|
cp = (char *)realloc(cp, linesize + TTYSIZE + 1);
|
|
if (cp == NULL) {
|
|
math_error("Cannot realloc line buffer");
|
|
not_reached();
|
|
}
|
|
linebuf = cp;
|
|
linesize += TTYSIZE;
|
|
}
|
|
cp[len++] = (char)ch;
|
|
}
|
|
cp[len] = '\0';
|
|
return linebuf;
|
|
}
|
|
|
|
|
|
/*
|
|
* Read the next character from the terminal.
|
|
* The routines in the history module are called so that the user
|
|
* can use a command history and emacs-like editing of the line.
|
|
*/
|
|
S_FUNC int
|
|
ttychar(void)
|
|
{
|
|
int ch; /* current char */
|
|
int len; /* length of current command */
|
|
STATIC char charbuf[256*1024];
|
|
|
|
/*
|
|
* If we have more to read from the saved command line, then do that.
|
|
* When we see a newline character, then clear the pointer so we will
|
|
* read a new line on the next call.
|
|
*/
|
|
if (cip->i_ttystr) {
|
|
ch = chartoint(*cip->i_ttystr++);
|
|
if (ch == '\n')
|
|
cip->i_ttystr = NULL;
|
|
return ch;
|
|
}
|
|
|
|
/*
|
|
* We need another complete line.
|
|
*/
|
|
abortlevel = 0;
|
|
inputwait = TRUE;
|
|
len = hist_getline(noprompt ? "" : prompt, charbuf, sizeof(charbuf));
|
|
if (len == 0) {
|
|
inputwait = FALSE;
|
|
return EOF;
|
|
}
|
|
inputwait = FALSE;
|
|
|
|
/*
|
|
* Handle shell escape if present
|
|
*/
|
|
if (charbuf[0] == '!') { /* do a shell command */
|
|
char *cmd;
|
|
int ret;
|
|
|
|
cmd = charbuf + 1;
|
|
if (*cmd == '\0' || *cmd == '\n')
|
|
cmd = shell;
|
|
if (allow_exec) {
|
|
if (conf->calc_debug & CALCDBG_SYSTEM) {
|
|
printf("%s\n", cmd);
|
|
}
|
|
ret = system(cmd);
|
|
if (ret < 0) {
|
|
fprintf(stderr, "error in cmd: %s\n", cmd);
|
|
}
|
|
} else {
|
|
fprintf(stderr, "execution disallowed by -m flag\n");
|
|
}
|
|
return '\n';
|
|
}
|
|
hist_saveline(charbuf, len);
|
|
|
|
/*
|
|
* Return the first character of the line, and set up to
|
|
* return the rest of it with later calls.
|
|
*/
|
|
ch = chartoint(charbuf[0]);
|
|
if (ch != '\n')
|
|
cip->i_ttystr = charbuf + 1;
|
|
return ch;
|
|
}
|
|
|
|
|
|
/*
|
|
* Return whether or not the input source is the terminal.
|
|
*/
|
|
BOOL
|
|
inputisterminal(void)
|
|
{
|
|
return ((depth <= 0) || ((cip->i_str == NULL) && (cip->i_fp == NULL)));
|
|
}
|
|
|
|
|
|
/*
|
|
* Return depth of current input source
|
|
*/
|
|
int
|
|
inputlevel(void)
|
|
{
|
|
return depth - 1;
|
|
}
|
|
|
|
|
|
/*
|
|
* Return the name of the current input file.
|
|
* Returns NULL for terminal or strings.
|
|
*/
|
|
char *
|
|
inputname(void)
|
|
{
|
|
if (depth <= 0)
|
|
return NULL;
|
|
return cip->i_name;
|
|
}
|
|
|
|
|
|
/*
|
|
* Return the current line number.
|
|
*/
|
|
long
|
|
linenumber(void)
|
|
{
|
|
if (depth > 0)
|
|
return cip->i_line;
|
|
return 1;
|
|
}
|
|
|
|
|
|
/*
|
|
* Restore the next character to be read again on the next nextchar call.
|
|
*/
|
|
void
|
|
reread(void)
|
|
{
|
|
if ((depth <= 0) || (cip->i_state == IS_REREAD))
|
|
return;
|
|
cip->i_state = IS_REREAD;
|
|
if (cip->i_char == '\n')
|
|
cip->i_line--;
|
|
}
|
|
|
|
|
|
/*
|
|
* Process all startup files found in the $CALCRC path.
|
|
*/
|
|
void
|
|
runrcfiles(void)
|
|
{
|
|
char path[MAX_CALCRC+1+1]; /* name being searched for */
|
|
char *cp;
|
|
char *p;
|
|
|
|
/* execute each file in the list */
|
|
while (calcrc != NULL && *calcrc) {
|
|
cp = calcrc;
|
|
calcrc = (char *) strchr(calcrc + 1, LISTCHAR);
|
|
|
|
/* load file name into the path */
|
|
if (calcrc == NULL) {
|
|
strlcpy(path, cp, MAX_CALCRC+1);
|
|
} else {
|
|
strlcpy(path, cp, calcrc - cp + 1);
|
|
}
|
|
|
|
/* find the start of the path */
|
|
p = (path[0] == ':') ? path + 1 : path;
|
|
if (p[0] == '\0') {
|
|
continue;
|
|
}
|
|
|
|
/* process the current file in the list */
|
|
if (openfile(p) < 0) {
|
|
/* Unable to open rcfile */
|
|
if (c_flag && !d_flag)
|
|
fprintf(stderr,
|
|
"Unable to open rcfile \"%s\"\n", p);
|
|
continue;
|
|
}
|
|
getcommands(FALSE);
|
|
closeinput();
|
|
}
|
|
}
|
|
|
|
|
|
/*
|
|
* isinoderead - determine if we have read a given dev/inode
|
|
*
|
|
* This function returns the index of the readset element that matches
|
|
* a given device/inode, -1 otherwise.
|
|
*
|
|
*
|
|
* _WIN32 and _WIN64 NOTE:
|
|
*
|
|
* This function likely will not work under _WIN32 or _WIN64.
|
|
*
|
|
* The sbuf->st_ino is always zero because the FAT and NTFS filesystems
|
|
* do not support inodes. Those filesystems don't support links, which
|
|
* is why you need this function under UNIX. For _WIN32 or _WIN64, use
|
|
* _fullpath() to determine if you have already opened a file.
|
|
*
|
|
* given:
|
|
* sbuf stat of the inode in question
|
|
*/
|
|
|
|
S_FUNC int
|
|
isinoderead(struct stat *sbuf)
|
|
{
|
|
int i;
|
|
|
|
/* deal with the empty case */
|
|
if (readset == NULL || maxreadset <= 0) {
|
|
/* readset is empty */
|
|
return -1;
|
|
}
|
|
|
|
/* scan the entire readset */
|
|
for (i=0; i < maxreadset; ++i) {
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
char tmp[MAX_PATH+1];
|
|
tmp[MAX_PATH] = '\0';
|
|
if (_fullpath(tmp, cip->i_name, MAX_PATH) &&
|
|
readset[i].active &&
|
|
strcasecmp(readset[i].path, tmp) == 0) {
|
|
/* found a match */
|
|
return i;
|
|
}
|
|
#else /* Windows free systems */
|
|
if (readset[i].active &&
|
|
sbuf->st_dev == readset[i].inode.st_dev &&
|
|
sbuf->st_ino == readset[i].inode.st_ino) {
|
|
/* found a match */
|
|
return i;
|
|
}
|
|
#endif /* Windows free systems */
|
|
}
|
|
|
|
/* no match found */
|
|
return -1;
|
|
}
|
|
|
|
|
|
/*
|
|
* findfreeread - find the next free readset element
|
|
*
|
|
* This function will return the index of the next free readset element.
|
|
* If needed, this function will allocate new readset elements.
|
|
*
|
|
* This function returns the index of the next free element, or -1.
|
|
*/
|
|
S_FUNC int
|
|
findfreeread(void)
|
|
{
|
|
int i;
|
|
|
|
/* deal with an empty readset case */
|
|
if (readset == NULL || maxreadset <= 0) {
|
|
|
|
/* malloc a new readset */
|
|
readset = (READSET *)malloc((READSET_ALLOC+1)*sizeof(READSET));
|
|
if (readset == NULL) {
|
|
return -1;
|
|
}
|
|
maxreadset = READSET_ALLOC;
|
|
for (i=0; i < READSET_ALLOC; ++i) {
|
|
readset[i].active = 0;
|
|
}
|
|
|
|
/* return first entry */
|
|
return 0;
|
|
}
|
|
|
|
/* try to find a free readset entry */
|
|
for (i=0; i < maxreadset; ++i) {
|
|
if (readset[i].active == 0) {
|
|
/* found a free readset entry */
|
|
return i;
|
|
}
|
|
}
|
|
|
|
/* all readset entries are in use, allocate more */
|
|
readset = (READSET *)realloc(readset,
|
|
(maxreadset+READSET_ALLOC) * sizeof(READSET));
|
|
if (readset == NULL) {
|
|
return -1;
|
|
}
|
|
for (i=0; i < READSET_ALLOC; ++i) {
|
|
readset[i+maxreadset].active = 0;
|
|
}
|
|
maxreadset += READSET_ALLOC;
|
|
|
|
/* return the first newly allocated free entry */
|
|
return maxreadset-READSET_ALLOC;
|
|
}
|
|
|
|
|
|
/*
|
|
* addreadset - add a entry to the readset array if it is not already there
|
|
*
|
|
* This function attempts to add a file into the readset. If the readset
|
|
* has an entry with a matching dev/inode, then that entry is updated with
|
|
* the new name and path. If no such readset entry is found, a new entry
|
|
* is added.
|
|
*
|
|
* This function returns the index of the readset entry, or -1 if error.
|
|
*
|
|
* given:
|
|
* name name given to read or include
|
|
* path full pathname of file
|
|
* sbuf stat of the path
|
|
*/
|
|
S_FUNC int
|
|
addreadset(char *name, char *path, struct stat *sbuf)
|
|
{
|
|
int ret; /* index to return */
|
|
size_t name_len; /* length of read set name */
|
|
size_t path_len; /* length of path to read set name */
|
|
|
|
/* find the inode */
|
|
ret = isinoderead(sbuf);
|
|
if (ret < 0) {
|
|
/* not in readset, find a free node */
|
|
ret = findfreeread();
|
|
if (ret < 0) {
|
|
/* cannot find/form a free readset entry */
|
|
return -1;
|
|
}
|
|
} else {
|
|
/* found an readset entry, free old readset data */
|
|
if (readset[ret].name != NULL) {
|
|
free(readset[ret].name);
|
|
}
|
|
if (readset[ret].path != NULL) {
|
|
free(readset[ret].path);
|
|
}
|
|
}
|
|
|
|
/* load our information into the readset entry */
|
|
name_len = strlen(name);
|
|
readset[ret].name = (char *)malloc(name_len+1);
|
|
if (readset[ret].name == NULL) {
|
|
return -1;
|
|
}
|
|
strlcpy(readset[ret].name, name, name_len+1);
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
/*
|
|
* For _WIN32 or _WIN64, _fullpath expands the path to a fully qualified
|
|
* path name, which under _WIN32 or _WIN64 FAT and NTFS is unique, just
|
|
* like UNIX inodes. _fullpath also allocated the memory for
|
|
* this new longer path name.
|
|
*/
|
|
{
|
|
readset[ret].path = _fullpath(NULL, path, MAX_PATH);
|
|
if (readset[ret].path == NULL) {
|
|
return -1;
|
|
}
|
|
}
|
|
#else /* Windows free systems */
|
|
path_len = strlen(path);
|
|
readset[ret].path = (char *)malloc(path_len+1);
|
|
if (readset[ret].path == NULL) {
|
|
return -1;
|
|
}
|
|
strlcpy(readset[ret].path, path, path_len+1);
|
|
#endif /* Windows free systems */
|
|
readset[ret].inode = *sbuf;
|
|
readset[ret].active = 1;
|
|
|
|
/* return index of the newly added entry */
|
|
return ret;
|
|
}
|