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.
 
 
 
 
 
 

773 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. #if PERMANENT_DEBUG
  102. sprintf( bwb_ebuf, "\r%s\n", "Debugging Mode" );
  103. prn_xprintf( stdout, bwb_ebuf );
  104. #else
  105. sprintf( bwb_ebuf, "\r%s\n", MES_LANGUAGE );
  106. prn_xprintf( stdout, bwb_ebuf );
  107. #endif
  108. return TRUE;
  109. }
  110. /***************************************************************
  111. FUNCTION: bwx_message()
  112. DESCRIPTION:
  113. ***************************************************************/
  114. int
  115. bwx_message( char *m )
  116. {
  117. #if DEBUG
  118. _outtext( "<MES>" );
  119. #endif
  120. _outtext( m );
  121. return TRUE;
  122. }
  123. /***************************************************************
  124. FUNCTION: bwx_putc()
  125. DESCRIPTION:
  126. ***************************************************************/
  127. extern int
  128. bwx_putc( char c )
  129. {
  130. static char tbuf[ 2 ];
  131. tbuf[ 0 ] = c;
  132. tbuf[ 1 ] = '\0';
  133. _outtext( tbuf );
  134. return TRUE;
  135. }
  136. /***************************************************************
  137. FUNCTION: bwx_error()
  138. DESCRIPTION:
  139. ***************************************************************/
  140. int
  141. bwx_errmes( char *m )
  142. {
  143. static char tbuf[ MAXSTRINGSIZE + 1 ]; /* this memory should be
  144. permanent in case of memory
  145. overrun errors */
  146. if (( prn_col != 1 ) && ( errfdevice == stderr ))
  147. {
  148. prn_xprintf( errfdevice, "\n" );
  149. }
  150. if ( CURTASK number == 0 )
  151. {
  152. sprintf( tbuf, "\n%s: %s\n", ERRD_HEADER, m );
  153. }
  154. else
  155. {
  156. sprintf( tbuf, "\n%s %d: %s\n", ERROR_HEADER, CURTASK number, m );
  157. }
  158. #if INTENSIVE_DEBUG
  159. prn_xprintf( stderr, "<ERR>" );
  160. #endif
  161. prn_xprintf( errfdevice, tbuf );
  162. return TRUE;
  163. }
  164. /***************************************************************
  165. FUNCTION: bwx_input()
  166. DESCRIPTION: As implemented here, the input facility
  167. is a hybrid of _outtext output (which allows
  168. the color to be set) and standard output
  169. (which does not). The reason is that I've
  170. found it helpful to use the DOS facility
  171. for text entry, with its backspace-delete
  172. and recognition of the SIGINT, depite the
  173. fact that its output goes to stdout.
  174. ***************************************************************/
  175. int
  176. bwx_input( char *prompt, char *buffer )
  177. {
  178. #if INTENSIVE_DEBUG
  179. prn_xprintf( stdout, "<INP>" );
  180. #endif
  181. prn_xprintf( stdout, prompt );
  182. fgets( buffer, MAXREADLINESIZE, stdin );
  183. prn_xprintf( stdout, "\n" ); /* let _outtext catch up */
  184. * prn_getcol( stdout ) = 1; /* reset column */
  185. return TRUE;
  186. }
  187. /***************************************************************
  188. FUNCTION: bwx_terminate()
  189. DESCRIPTION:
  190. ***************************************************************/
  191. void
  192. bwx_terminate( void )
  193. {
  194. #if MS_CMDS
  195. if ( reset_mode == TRUE )
  196. {
  197. _setvideomode( _DEFAULTMODE );
  198. /* Restore original foreground and background. */
  199. _settextcolor( oldfgd );
  200. _setbkcolor( oldbgd );
  201. }
  202. #endif
  203. exit( 0 );
  204. }
  205. /***************************************************************
  206. FUNCTION: bwx_shell()
  207. DESCRIPTION:
  208. ***************************************************************/
  209. #if COMMAND_SHELL
  210. extern int
  211. bwx_shell( struct bwb_line *l )
  212. {
  213. static char *s_buffer;
  214. static int init = FALSE;
  215. static int position;
  216. /* get memory for temporary buffer if necessary */
  217. if ( init == FALSE )
  218. {
  219. init = TRUE;
  220. /* Revised to CALLOC pass-thru call by JBV */
  221. if ( ( s_buffer = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ) )) == NULL )
  222. {
  223. bwb_error( err_getmem );
  224. return FALSE;
  225. }
  226. }
  227. /* get the first element and check for a line number */
  228. #if INTENSIVE_DEBUG
  229. sprintf( bwb_ebuf, "in bwx_shell(): line buffer is <%s>.", l->buffer );
  230. bwb_debug( bwb_ebuf );
  231. #endif
  232. position = 0;
  233. adv_element( l->buffer, &position, s_buffer );
  234. if ( is_numconst( s_buffer ) != TRUE ) /* not a line number */
  235. {
  236. #if INTENSIVE_DEBUG
  237. sprintf( bwb_ebuf, "in bwx_shell(): no line number, command <%s>.",
  238. l->buffer );
  239. bwb_debug( bwb_ebuf );
  240. #endif
  241. if ( system( l->buffer ) == 0 )
  242. {
  243. iqc_setpos();
  244. return TRUE;
  245. }
  246. else
  247. {
  248. iqc_setpos();
  249. return FALSE;
  250. }
  251. }
  252. else /* advance past line number */
  253. {
  254. adv_ws( l->buffer, &position ); /* advance past whitespace */
  255. #if INTENSIVE_DEBUG
  256. sprintf( bwb_ebuf, "in bwx_shell(): line number, command <%s>.",
  257. l->buffer );
  258. bwb_debug( bwb_ebuf );
  259. #endif
  260. if ( system( &( l->buffer[ position ] ) ) == 0 )
  261. {
  262. iqc_setpos();
  263. return TRUE;
  264. }
  265. else
  266. {
  267. iqc_setpos();
  268. return FALSE;
  269. }
  270. }
  271. }
  272. #endif
  273. /***************************************************************
  274. FUNCTION: iqc_setpos()
  275. DESCRIPTION:
  276. ***************************************************************/
  277. static int
  278. iqc_setpos( void )
  279. {
  280. union REGS ibm_registers;
  281. /* call the BDOS function 0x10 to read the current cursor position */
  282. ibm_registers.h.ah = 3;
  283. ibm_registers.h.bh = (unsigned char) _getvisualpage();
  284. int86( 0x10, &ibm_registers, &ibm_registers );
  285. /* set text to this position */
  286. _settextposition( ibm_registers.h.dh, ibm_registers.h.dl );
  287. /* and move down one position */
  288. prn_xprintf( stdout, "\n" );
  289. return TRUE;
  290. }
  291. #if COMMON_CMDS
  292. /***************************************************************
  293. FUNCTION: bwb_edit()
  294. DESCRIPTION:
  295. ***************************************************************/
  296. struct bwb_line *
  297. bwb_edit( struct bwb_line *l )
  298. {
  299. char tbuf[ MAXSTRINGSIZE + 1 ];
  300. char edname[ MAXSTRINGSIZE + 1 ];
  301. struct bwb_variable *ed;
  302. FILE *loadfile;
  303. ed = var_find( DEFVNAME_EDITOR );
  304. str_btoc( edname, var_getsval( ed ));
  305. sprintf( tbuf, "%s %s", edname, CURTASK progfile );
  306. #if INTENSIVE_DEBUG
  307. sprintf( bwb_ebuf, "in bwb_edit(): command line <%s>", tbuf );
  308. bwb_debug( bwb_ebuf );
  309. #else
  310. system( tbuf );
  311. #endif
  312. /* open edited file for read */
  313. if ( ( loadfile = fopen( CURTASK progfile, "r" )) == NULL )
  314. {
  315. sprintf( bwb_ebuf, err_openfile, CURTASK progfile );
  316. bwb_error( bwb_ebuf );
  317. iqc_setpos();
  318. return bwb_zline( l );
  319. }
  320. /* clear current contents */
  321. bwb_new( l ); /* Relocated by JBV (bug found by DD) */
  322. /* and (re)load the file into memory */
  323. bwb_fload( loadfile );
  324. iqc_setpos();
  325. return bwb_zline( l );
  326. }
  327. /***************************************************************
  328. FUNCTION: bwb_renum()
  329. DESCRIPTION: This function implements the BASIC RENUM
  330. command by shelling out to a default
  331. renumbering program called "renum".
  332. Added by JBV 10/95
  333. SYNTAX: RENUM
  334. ***************************************************************/
  335. #if ANSI_C
  336. struct bwb_line *
  337. bwb_renum( struct bwb_line *l )
  338. #else
  339. struct bwb_line *
  340. bwb_renum( l )
  341. struct bwb_line *l;
  342. #endif
  343. {
  344. char tbuf[ MAXSTRINGSIZE + 1 ];
  345. FILE *loadfile;
  346. sprintf( tbuf, "renum %s\0", CURTASK progfile );
  347. #if INTENSIVE_DEBUG
  348. sprintf( bwb_ebuf, "in bwb_renum(): command line <%s>", tbuf );
  349. bwb_debug( bwb_ebuf );
  350. #else
  351. system( tbuf );
  352. #endif
  353. /* open edited file for read */
  354. if ( ( loadfile = fopen( CURTASK progfile, "r" )) == NULL )
  355. {
  356. sprintf( bwb_ebuf, err_openfile, CURTASK progfile );
  357. bwb_error( bwb_ebuf );
  358. iqc_setpos();
  359. return bwb_zline( l );
  360. }
  361. /* clear current contents */
  362. bwb_new( l ); /* Relocated by JBV (bug found by DD) */
  363. /* and (re)load the file into memory */
  364. bwb_fload( loadfile );
  365. iqc_setpos();
  366. return bwb_zline( l );
  367. }
  368. /***************************************************************
  369. FUNCTION: bwb_files()
  370. DESCRIPTION:
  371. ***************************************************************/
  372. struct bwb_line *
  373. bwb_files( struct bwb_line *l )
  374. {
  375. char tbuf[ MAXVARNAMESIZE + 1 ];
  376. char finame[ MAXVARNAMESIZE + 1 ];
  377. char argument[ MAXVARNAMESIZE + 1 ];
  378. struct bwb_variable *fi;
  379. struct exp_ese *e;
  380. fi = var_find( DEFVNAME_FILES );
  381. str_btoc( finame, var_getsval( fi ));
  382. /* get argument */
  383. adv_ws( l->buffer, &( l->position ));
  384. switch( l->buffer[ l->position ] )
  385. {
  386. case '\0':
  387. case '\r':
  388. case '\n':
  389. argument[ 0 ] = '\0';
  390. break;
  391. default:
  392. e = bwb_exp( l->buffer, FALSE, &( l->position ) );
  393. if ( e->type != STRING )
  394. {
  395. bwb_error( err_mismatch );
  396. return bwb_zline( l );
  397. }
  398. str_btoc( argument, exp_getsval( e ) );
  399. break;
  400. }
  401. sprintf( tbuf, "%s %s", finame, argument );
  402. #if INTENSIVE_DEBUG
  403. sprintf( bwb_ebuf, "in bwb_files(): command line <%s>", tbuf );
  404. bwb_debug( bwb_ebuf );
  405. #else
  406. system( tbuf );
  407. #endif
  408. iqc_setpos();
  409. return bwb_zline( l );
  410. }
  411. #endif /* COMMON_CMDS */
  412. #if INTERACTIVE
  413. /***************************************************************
  414. FUNCTION: fnc_inkey()
  415. DESCRIPTION: This C function implements the BASIC INKEY$
  416. function. It is implementation-specific.
  417. ***************************************************************/
  418. extern struct bwb_variable *
  419. fnc_inkey( int argc, struct bwb_variable *argv )
  420. {
  421. static struct bwb_variable nvar;
  422. char tbuf[ MAXSTRINGSIZE + 1 ];
  423. static int init = FALSE;
  424. /* initialize the variable if necessary */
  425. if ( init == FALSE )
  426. {
  427. init = TRUE;
  428. var_make( &nvar, STRING, "fnc_inkey" );
  429. }
  430. /* check arguments */
  431. #if PROG_ERRORS
  432. if ( argc > 0 )
  433. {
  434. sprintf( bwb_ebuf, "Two many arguments to function INKEY$()" );
  435. bwb_error( bwb_ebuf );
  436. return &nvar;
  437. }
  438. #else
  439. if ( fnc_checkargs( argc, argv, 0, 0 ) == FALSE )
  440. {
  441. return NULL;
  442. }
  443. #endif
  444. /* body of the INKEY$ function */
  445. if ( _bios_keybrd( _KEYBRD_READY ) == 0 )
  446. {
  447. tbuf[ 0 ] = '\0';
  448. }
  449. else
  450. {
  451. tbuf[ 0 ] = (char) _bios_keybrd( _KEYBRD_READ );
  452. tbuf[ 1 ] = '\0';
  453. }
  454. /* assign value to nvar variable */
  455. str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );
  456. /* return value contained in nvar */
  457. return &nvar;
  458. }
  459. #endif /* INTERACTIVE */
  460. #if MS_CMDS
  461. /***************************************************************
  462. FUNCTION: bwb_cls()
  463. DESCRIPTION: This C function implements the BASIC CLS
  464. command. It is implementation-specific.
  465. ***************************************************************/
  466. extern struct bwb_line *
  467. bwb_cls( struct bwb_line *l )
  468. {
  469. _clearscreen( _GCLEARSCREEN );
  470. return bwb_zline( l );
  471. }
  472. /***************************************************************
  473. FUNCTION: bwb_locate()
  474. DESCRIPTION: This C function implements the BASIC LOCATE
  475. command. It is implementation-specific.
  476. ***************************************************************/
  477. extern struct bwb_line *
  478. bwb_locate( struct bwb_line *l )
  479. {
  480. struct exp_ese *e;
  481. int row, column;
  482. /* get first argument */
  483. e = bwb_exp( l->buffer, FALSE, &( l->position ));
  484. row = (int) exp_getnval( e );
  485. /* advance past comma */
  486. adv_ws( l->buffer, &( l->position ));
  487. if ( l->buffer[ l->position ] != ',' )
  488. {
  489. bwb_error( err_syntax );
  490. return bwb_zline( l );
  491. }
  492. ++( l->position );
  493. /* get second argument */
  494. e = bwb_exp( l->buffer, FALSE, &( l->position ));
  495. column = (int) exp_getnval( e );
  496. /* position the cursor */
  497. _settextposition( row, column );
  498. return bwb_zline( l );
  499. }
  500. /***************************************************************
  501. FUNCTION: bwb_color()
  502. DESCRIPTION: This C function implements the BASIC COLOR
  503. command. It is implementation-specific.
  504. ***************************************************************/
  505. extern struct bwb_line *
  506. bwb_color( struct bwb_line *l )
  507. {
  508. struct exp_ese *e;
  509. int color;
  510. /* get first argument */
  511. e = bwb_exp( l->buffer, FALSE, &( l->position ));
  512. color = (int) exp_getnval( e );
  513. #if INTENSIVE_DEBUG
  514. sprintf( bwb_ebuf, "Setting text color to %d", color );
  515. bwb_debug( bwb_ebuf );
  516. #endif
  517. _settextcolor( (short) color );
  518. #if INTENSIVE_DEBUG
  519. sprintf( bwb_ebuf, "Set text color to %d", color );
  520. bwb_debug( bwb_ebuf );
  521. #endif
  522. /* advance past comma */
  523. adv_ws( l->buffer, &( l->position ));
  524. if ( l->buffer[ l->position ] == ',' )
  525. {
  526. ++( l->position );
  527. /* get second argument */
  528. e = bwb_exp( l->buffer, FALSE, &( l->position ));
  529. color = (int) exp_getnval( e );
  530. #if INTENSIVE_DEBUG
  531. sprintf( bwb_ebuf, "Setting background color to %d", color );
  532. bwb_debug( bwb_ebuf );
  533. #endif
  534. /* set the background color */
  535. _setbkcolor( (long) color );
  536. #if INTENSIVE_DEBUG
  537. sprintf( bwb_ebuf, "Setting background color to %d\n", color );
  538. bwb_debug( bwb_ebuf );
  539. #endif
  540. }
  541. return bwb_zline( l );
  542. }
  543. #endif /* MS_CMDS */