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.
 
 
 
 
 
 

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