home *** CD-ROM | disk | FTP | other *** search
/ HyperLib 1997 Winter - Disc 1 / HYPERLIB-1997-Winter-CD1.ISO.7z / HYPERLIB-1997-Winter-CD1.ISO / オンラインウェア / PRG / bwbasic-2.10.sit / bwbasic-2.10 / bwx_iqc.c < prev    next >
C/C++ Source or Header  |  1996-10-09  |  15KB  |  707 lines

  1. /***************************************************************
  2.  
  3.         bwx_iqc.c       Environment-dependent implementation
  4.                         of Bywater BASIC Interpreter
  5.             for IBM PC and Compatibles
  6.             using the Microsoft QuickC (tm) Compiler
  7.  
  8.                         Copyright (c) 1993, Ted A. Campbell
  9.             Bywater Software
  10.  
  11.                         email: tcamp@delphi.com
  12.  
  13.         Copyright and Permissions Information:
  14.  
  15.         All U.S. and international rights are claimed by the author,
  16.         Ted A. Campbell.
  17.  
  18.     This software is released under the terms of the GNU General
  19.     Public License (GPL), which is distributed with this software
  20.     in the file "COPYING".  The GPL specifies the terms under
  21.     which users may copy and use the software in this distribution.
  22.  
  23.     A separate license is available for commercial distribution,
  24.     for information on which you should contact the author.
  25.  
  26. ***************************************************************/
  27.  
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include <setjmp.h>
  31. //#include <bios.h>
  32. //#include <graph.h>
  33. #include <signal.h>
  34.  
  35. #include "bwbasic.h"
  36. #include "bwb_mes.h"
  37.  
  38. extern int prn_col;
  39. extern jmp_buf mark;
  40. short oldfgd;
  41. long oldbgd;
  42. int reset_mode = FALSE;
  43.  
  44. static int iqc_setpos( void );
  45.  
  46. /***************************************************************
  47.  
  48.         FUNCTION:       main()
  49.  
  50.         DESCRIPTION:    As in any C program, main() is the basic
  51.                         function from which the rest of the
  52.                         program is called. Some environments,
  53.             however, provide their own main() functions
  54.             (Microsoft Windows (tm) is an example).
  55.             In these cases, the following code will
  56.             have to be included in the initialization
  57.             function that is called by the environment.
  58.  
  59. ***************************************************************/
  60.  
  61. void
  62. main( int argc, char **argv )
  63.    {
  64. #if MS_CMDS
  65.    struct videoconfig vc;
  66.    short videomode;
  67.  
  68.    /* Save original foreground, background, and text position. */
  69.  
  70.    _getvideoconfig( &vc );
  71.    oldfgd = _gettextcolor();
  72.    oldbgd = _getbkcolor();
  73.  
  74.    if ( vc.mode != _TEXTC80 )
  75.       {
  76.       if ( _setvideomode( _TEXTC80 ) == 0 )
  77.          {
  78.      _getvideoconfig( &vc );
  79.      prn_xprintf( stderr, "Failed to set color video mode¥n" );
  80.          }
  81.       else
  82.      {
  83.      reset_mode = FALSE;
  84.      }
  85.       }
  86.    else
  87.       {
  88.       reset_mode = FALSE;
  89.       }
  90.  
  91. #endif       /* MS_CMDS */
  92.  
  93.    bwb_init( argc, argv );
  94.  
  95. #if INTERACTIVE
  96.    setjmp( mark );
  97. #endif
  98.  
  99.    /* now set the number of colors available */
  100.  
  101.  //  * var_findnval( co, co->array_pos ) = (bnumber) vc.numcolors;
  102.  
  103.    /* main program loop */
  104.  
  105.    while( !feof( stdin ) )        /* condition !feof( stdin ) added in v1.11 */
  106.       {
  107.       bwb_mainloop();
  108.       }
  109.  
  110.    }
  111.  
  112. /***************************************************************
  113.  
  114.         FUNCTION:       bwx_signon()
  115.  
  116.         DESCRIPTION:
  117.  
  118. ***************************************************************/
  119.  
  120. int
  121. bwx_signon( void )
  122.    {
  123.  
  124.    sprintf( bwb_ebuf, "¥r%s %s¥n", MES_SIGNON, VERSION );
  125.    prn_xprintf( stdout, bwb_ebuf );
  126.    sprintf( bwb_ebuf, "¥r%s¥n", MES_COPYRIGHT );
  127.    prn_xprintf( stdout, bwb_ebuf );
  128. #if PERMANENT_DEBUG
  129.    sprintf( bwb_ebuf, "¥r%s¥n", "Debugging Mode" );
  130.    prn_xprintf( stdout, bwb_ebuf );
  131. #else
  132.    sprintf( bwb_ebuf, "¥r%s¥n", MES_LANGUAGE );
  133.    prn_xprintf( stdout, bwb_ebuf );
  134. #endif
  135.  
  136.    return TRUE;
  137.  
  138.    }
  139.  
  140. /***************************************************************
  141.  
  142.         FUNCTION:       bwx_message()
  143.  
  144.         DESCRIPTION:
  145.  
  146. ***************************************************************/
  147.  
  148. int
  149. bwx_message( char *m )
  150.    {
  151.  
  152. #if DEBUG
  153.    _outtext( "<MES>" );
  154. #endif
  155.  
  156.   // _outtext( m );
  157.     printf("%s",m);
  158.    return TRUE;
  159.  
  160.    }
  161.  
  162. /***************************************************************
  163.  
  164.     FUNCTION:       bwx_putc()
  165.  
  166.     DESCRIPTION:
  167.  
  168. ***************************************************************/
  169.  
  170. extern int
  171. bwx_putc( char c )
  172.    {
  173.    static char tbuf[ 2 ];
  174.  
  175.    tbuf[ 0 ] = c;
  176.    tbuf[ 1 ] = '¥0';
  177.    //_outtext( tbuf );
  178.     printf("%s",c);
  179.    return TRUE;
  180.  
  181.    }
  182.  
  183. /***************************************************************
  184.  
  185.         FUNCTION:       bwx_error()
  186.  
  187.         DESCRIPTION:
  188.  
  189. ***************************************************************/
  190.  
  191. int
  192. bwx_errmes( char *m )
  193.    {
  194.    static char tbuf[ MAXSTRINGSIZE + 1 ];    /* this memory should be
  195.                            permanent in case of memory
  196.                            overrun errors */
  197.  
  198.    if (( prn_col != 1 ) && ( errfdevice == stderr ))
  199.       {
  200.       prn_xprintf( errfdevice, "¥n" );
  201.       }
  202.    if ( CURTASK number == 0 )
  203.       {
  204.       sprintf( tbuf, "¥n%s: %s¥n", ERRD_HEADER, m );
  205.       }
  206.    else
  207.       {
  208.       sprintf( tbuf, "¥n%s %d: %s¥n", ERROR_HEADER, CURTASK number, m );
  209.       }
  210.  
  211. #if INTENSIVE_DEBUG
  212.    prn_xprintf( stderr, "<ERR>" );
  213. #endif
  214.  
  215.    prn_xprintf( errfdevice, tbuf );
  216.  
  217.    return TRUE;
  218.  
  219.    }
  220.  
  221. /***************************************************************
  222.  
  223.         FUNCTION:       bwx_input()
  224.  
  225.     DESCRIPTION:    As implemented here, the input facility
  226.             is a hybrid of _outtext output (which allows
  227.             the color to be set) and standard output
  228.             (which does not).  The reason is that I've
  229.             found it helpful to use the DOS facility
  230.             for text entry, with its backspace-delete
  231.             and recognition of the SIGINT, depite the
  232.             fact that its output goes to stdout.
  233.  
  234. ***************************************************************/
  235.  
  236. int
  237. bwx_input( char *prompt, char *buffer )
  238.    {
  239.  
  240. #if INTENSIVE_DEBUG
  241.    prn_xprintf( stdout, "<INP>" );
  242. #endif
  243.  
  244.    prn_xprintf( stdout, prompt );
  245.  
  246.    fgets( buffer, MAXREADLINESIZE, stdin );
  247.    prn_xprintf( stdout, "¥n" );               /* let _outtext catch up */
  248.  
  249.    * prn_getcol( stdout ) = 1;            /* reset column */
  250.  
  251.    return TRUE;
  252.  
  253.    }
  254.  
  255. /***************************************************************
  256.  
  257.         FUNCTION:       bwx_terminate()
  258.  
  259.         DESCRIPTION:
  260.  
  261. ***************************************************************/
  262.  
  263. void
  264. bwx_terminate( void )
  265.    {
  266. #if MS_CMDS
  267.  
  268.    if ( reset_mode == TRUE )
  269.       {
  270.  
  271.       _setvideomode( _DEFAULTMODE );
  272.  
  273.       /* Restore original foreground and background. */
  274.  
  275.       _settextcolor( oldfgd );
  276.       _setbkcolor( oldbgd );
  277.  
  278.       }
  279.  
  280. #endif
  281.  
  282.    exit( 0 );
  283.    }
  284.  
  285. /***************************************************************
  286.  
  287.     FUNCTION:       bwx_shell()
  288.  
  289.     DESCRIPTION:
  290.  
  291. ***************************************************************/
  292.  
  293. #if COMMAND_SHELL
  294. extern int
  295. bwx_shell( struct bwb_line *l )
  296.    {
  297.    static char *s_buffer;
  298.    static int init = FALSE;
  299.    static int position;
  300.  
  301.    /* get memory for temporary buffer if necessary */
  302.  
  303.    if ( init == FALSE )
  304.       {
  305.       init = TRUE;
  306.       if ( ( s_buffer = calloc( MAXSTRINGSIZE + 1, sizeof( char ) )) == NULL )
  307.      {
  308.      bwb_error( err_getmem );
  309.      return FALSE;
  310.      }
  311.       }
  312.  
  313.    /* get the first element and check for a line number */
  314.  
  315. #if INTENSIVE_DEBUG
  316.    sprintf( bwb_ebuf, "in bwx_shell(): line buffer is <%s>.", l->buffer );
  317.    bwb_debug( bwb_ebuf );
  318. #endif
  319.  
  320.    position = 0;
  321.    adv_element( l->buffer, &position, s_buffer );
  322.    if ( is_numconst( s_buffer ) != TRUE )                  /* not a line number */
  323.       {
  324.  
  325. #if INTENSIVE_DEBUG
  326.       sprintf( bwb_ebuf, "in bwx_shell(): no line number, command <%s>.",
  327.      l->buffer );
  328.       bwb_debug( bwb_ebuf );
  329. #endif
  330.  
  331.       if ( system( l->buffer ) == 0 )
  332.      {
  333.      iqc_setpos();
  334.      return TRUE;
  335.      }
  336.       else
  337.      {
  338.      iqc_setpos();
  339.      return FALSE;
  340.      }
  341.       }
  342.  
  343.    else                                         /* advance past line number */
  344.       {
  345.       adv_ws( l->buffer, &position );           /* advance past whitespace */
  346.  
  347. #if INTENSIVE_DEBUG
  348.       sprintf( bwb_ebuf, "in bwx_shell(): line number, command <%s>.",
  349.      l->buffer );
  350.       bwb_debug( bwb_ebuf );
  351. #endif
  352.  
  353.       if ( system( &( l->buffer[ position ] ) ) == 0 )
  354.      {
  355.      iqc_setpos();
  356.      return TRUE;
  357.      }
  358.       else
  359.      {
  360.      iqc_setpos();
  361.      return FALSE;
  362.      }
  363.       }
  364.    }
  365. #endif
  366.  
  367. /***************************************************************
  368.  
  369.     FUNCTION:      iqc_setpos()
  370.  
  371.     DESCRIPTION:
  372.  
  373. ***************************************************************/
  374.  
  375. static int
  376. iqc_setpos( void )
  377.    {
  378.  //  union REGS ibm_registers;
  379.  
  380.    /* call the BDOS function 0x10 to read the current cursor position */
  381.  
  382.   // ibm_registers.h.ah = 3;
  383.  //  ibm_registers.h.bh = (unsigned char) _getvisualpage();
  384.  //  int86( 0x10, &ibm_registers, &ibm_registers );
  385.  
  386.    /* set text to this position */
  387.  
  388.   // _settextposition( ibm_registers.h.dh, ibm_registers.h.dl );
  389.  
  390.    /* and move down one position */
  391.  
  392.    prn_xprintf( stdout, "¥n" );
  393.  
  394.    return TRUE;
  395.    }
  396.  
  397.  
  398. #if COMMON_CMDS
  399.  
  400. /***************************************************************
  401.  
  402.         FUNCTION:       bwb_edit()
  403.  
  404.         DESCRIPTION:
  405.  
  406. ***************************************************************/
  407.  
  408. struct bwb_line *
  409. bwb_edit( struct bwb_line *l )
  410.    {
  411.    char tbuf[ MAXSTRINGSIZE + 1 ];
  412.    char edname[ MAXSTRINGSIZE + 1 ];
  413.    struct bwb_variable *ed;
  414.    FILE *loadfile;
  415.  
  416.    ed = var_find( DEFVNAME_EDITOR );
  417.    str_btoc( edname, var_getsval( ed ));
  418.  
  419.    sprintf( tbuf, "%s %s", edname, CURTASK progfile );
  420.  
  421. #if INTENSIVE_DEBUG
  422.    sprintf( bwb_ebuf, "in bwb_edit(): command line <%s>", tbuf );
  423.    bwb_debug( bwb_ebuf );
  424. #else
  425.    system( tbuf );
  426. #endif
  427.  
  428.    /* clear current contents */
  429.  
  430.    bwb_new( l );
  431.  
  432.    /* open edited file for read */
  433.  
  434.    if ( ( loadfile = fopen( CURTASK progfile, "r" )) == NULL )
  435.       {
  436.       sprintf( bwb_ebuf, err_openfile, CURTASK progfile );
  437.       bwb_error( bwb_ebuf );
  438.  
  439.       iqc_setpos();
  440.       return bwb_zline( l );
  441.       }
  442.  
  443.    /* and (re)load the file into memory */
  444.  
  445.    bwb_fload( loadfile );
  446.  
  447.  
  448.    iqc_setpos();
  449.    return bwb_zline( l );
  450.    }
  451.  
  452. /***************************************************************
  453.  
  454.         FUNCTION:       bwb_files()
  455.  
  456.         DESCRIPTION:
  457.  
  458. ***************************************************************/
  459.  
  460. struct bwb_line *
  461. bwb_files( struct bwb_line *l )
  462.    {
  463.    char tbuf[ MAXVARNAMESIZE + 1 ];
  464.    char finame[ MAXVARNAMESIZE + 1 ];
  465.    char argument[ MAXVARNAMESIZE + 1 ];
  466.    struct bwb_variable *fi;
  467.    struct exp_ese *e;
  468.  
  469.    fi = var_find( DEFVNAME_FILES );
  470.    str_btoc( finame, var_getsval( fi ));
  471.  
  472.    /* get argument */
  473.  
  474.    adv_ws( l->buffer, &( l->position ));
  475.    switch( l->buffer[ l->position ] )
  476.       {
  477.       case '¥0':
  478.       case '¥r':
  479.       case '¥n':
  480.          argument[ 0 ] = '¥0';
  481.          break;
  482.       default:
  483.          e = bwb_exp( l->buffer, FALSE, &( l->position ) );
  484.          if ( e->type != STRING )
  485.             {
  486.             bwb_error( err_mismatch );
  487.             return bwb_zline( l );
  488.             }
  489.          str_btoc( argument, exp_getsval( e ) );
  490.          break;
  491.       }
  492.  
  493.  
  494.    sprintf( tbuf, "%s %s", finame, argument );
  495.  
  496. #if INTENSIVE_DEBUG
  497.    sprintf( bwb_ebuf, "in bwb_files(): command line <%s>", tbuf );
  498.    bwb_debug( bwb_ebuf );
  499. #else
  500.    system( tbuf );
  501. #endif
  502.  
  503.    iqc_setpos();
  504.    return bwb_zline( l );
  505.  
  506.    }
  507.  
  508. #endif                    /* COMMON_CMDS */
  509.  
  510. #if INTERACTIVE
  511.  
  512. /***************************************************************
  513.  
  514.         FUNCTION:       fnc_inkey()
  515.  
  516.         DESCRIPTION:    This C function implements the BASIC INKEY$
  517.                 function.  It is implementation-specific.
  518.  
  519. ***************************************************************/
  520.  
  521. extern struct bwb_variable *
  522. fnc_inkey( int argc, struct bwb_variable *argv ,int x)
  523.    {
  524.    static struct bwb_variable nvar;
  525.    char tbuf[ MAXSTRINGSIZE + 1 ];
  526.    static int init = FALSE;
  527.  
  528.    /* initialize the variable if necessary */
  529.  
  530.    if ( init == FALSE )
  531.       {
  532.       init = TRUE;
  533.       var_make( &nvar, STRING );
  534.       }
  535.  
  536.    /* check arguments */
  537.  
  538. #if PROG_ERRORS
  539.    if ( argc > 0 )
  540.       {
  541.       sprintf( bwb_ebuf, "Two many arguments to function INKEY$()" );
  542.       bwb_error( bwb_ebuf );
  543.       return &nvar;
  544.       }
  545.  
  546. #else
  547.    if ( fnc_checkargs( argc, argv, 0, 0 ) == FALSE )
  548.       {
  549.       return NULL;
  550.       }
  551. #endif
  552.  
  553.    /* body of the INKEY$ function */
  554. /*
  555.    if ( _bios_keybrd( _KEYBRD_READY ) == 0 )
  556.       {
  557.       tbuf[ 0 ] = '¥0';
  558.       }
  559.    else
  560.       {
  561.       tbuf[ 0 ] = (char) _bios_keybrd( _KEYBRD_READ );
  562.       tbuf[ 1 ] = '¥0';
  563.       }
  564. */
  565.  
  566.     gets(tbuf);    
  567.    /* assign value to nvar variable */
  568.  
  569.    str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );
  570.  
  571.    /* return value contained in nvar */
  572.  
  573.    return &nvar;
  574.  
  575.    }
  576.  
  577. #endif                /* INTERACTIVE */
  578.  
  579. #if MS_CMDS
  580.  
  581. /***************************************************************
  582.  
  583.         FUNCTION:       bwb_cls()
  584.  
  585.         DESCRIPTION:    This C function implements the BASIC CLS
  586.                 command.  It is implementation-specific.
  587.  
  588. ***************************************************************/
  589.  
  590. extern struct bwb_line *
  591. bwb_cls( struct bwb_line *l )
  592.    {
  593.  
  594.    _clearscreen( _GCLEARSCREEN );
  595.  
  596.    return bwb_zline( l );
  597.    }
  598.  
  599. /***************************************************************
  600.  
  601.         FUNCTION:       bwb_locate()
  602.  
  603.         DESCRIPTION:    This C function implements the BASIC LOCATE
  604.                 command.  It is implementation-specific.
  605.  
  606. ***************************************************************/
  607.  
  608. extern struct bwb_line *
  609. bwb_locate( struct bwb_line *l )
  610.    {
  611.    struct exp_ese *e;
  612.    int row, column;
  613.  
  614.    /* get first argument */
  615.  
  616.    e = bwb_exp( l->buffer, FALSE, &( l->position ));
  617.    row = (int) exp_getnval( e );
  618.  
  619.    /* advance past comma */
  620.  
  621.    adv_ws( l->buffer, &( l->position ));
  622.    if ( l->buffer[ l->position ] != ',' )
  623.       {
  624.       bwb_error( err_syntax );
  625.       return bwb_zline( l );
  626.       }
  627.    ++( l->position );
  628.  
  629.    /* get second argument */
  630.  
  631.    e = bwb_exp( l->buffer, FALSE, &( l->position ));
  632.    column = (int) exp_getnval( e );
  633.  
  634.    /* position the cursor */
  635.  
  636.    _settextposition( row, column );
  637.  
  638.    return bwb_zline( l );
  639.    }
  640.  
  641. /***************************************************************
  642.  
  643.     FUNCTION:       bwb_color()
  644.  
  645.     DESCRIPTION:    This C function implements the BASIC COLOR
  646.             command.  It is implementation-specific.
  647.  
  648. ***************************************************************/
  649.  
  650. extern struct bwb_line *
  651. bwb_color( struct bwb_line *l )
  652.    {
  653.    struct exp_ese *e;
  654.    int color;
  655.  
  656.    /* get first argument */
  657.  
  658.    e = bwb_exp( l->buffer, FALSE, &( l->position ));
  659.    color = (int) exp_getnval( e );
  660.  
  661. #if INTENSIVE_DEBUG
  662.    sprintf( bwb_ebuf, "Setting text color to %d", color );
  663.    bwb_debug( bwb_ebuf );
  664. #endif
  665.  
  666.    _settextcolor( (short) color );
  667.  
  668. #if INTENSIVE_DEBUG
  669.    sprintf( bwb_ebuf, "Set text color to %d", color );
  670.    bwb_debug( bwb_ebuf );
  671. #endif
  672.  
  673.    /* advance past comma */
  674.  
  675.    adv_ws( l->buffer, &( l->position ));
  676.    if ( l->buffer[ l->position ] == ',' )
  677.       {
  678.  
  679.       ++( l->position );
  680.  
  681.       /* get second argument */
  682.  
  683.       e = bwb_exp( l->buffer, FALSE, &( l->position ));
  684.       color = (int) exp_getnval( e );
  685.  
  686. #if INTENSIVE_DEBUG
  687.       sprintf( bwb_ebuf, "Setting background color to %d", color );
  688.       bwb_debug( bwb_ebuf );
  689. #endif
  690.  
  691.       /* set the background color */
  692.  
  693.       _setbkcolor( (long) color );
  694.  
  695. #if INTENSIVE_DEBUG
  696.       sprintf( bwb_ebuf, "Setting background color to %d¥n", color );
  697.       bwb_debug( bwb_ebuf );
  698. #endif
  699.  
  700.       }
  701.  
  702.    return bwb_zline( l );
  703.    }
  704.  
  705. #endif                /* MS_CMDS */
  706.  
  707.