ChipMaster's bwBASIC This also includes history going back to v2.10. *WARN* some binary files might have been corrupted by CRLF.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

775 lines
16 KiB

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