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.
 
 
 
 
 
 

816 lines
18 KiB

  1. /***************************************************************
  2. bwx_ncu.c Environment-dependent implementation
  3. of Bywater BASIC Interpreter
  4. for Linux (and others?) using Ncurses
  5. library,
  6. This BWBASIC file hacked together by L.C. Benschop,
  7. Eindhoven, The Netherlands. 1997/01/14 and
  8. 1997/01/15 derived from the iqc version.
  9. (benschop@eb.ele.tue.nl)
  10. All the shell commands (like FILES) interact badly
  11. with curses. I should replace them with popen/addch
  12. Copyright (c) 1993, Ted A. Campbell
  13. Bywater Software
  14. email: tcamp@delphi.com
  15. Copyright and Permissions Information:
  16. All U.S. and international rights are claimed by the author,
  17. Ted A. Campbell.
  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. A separate license is available for commercial distribution,
  23. for information on which you should contact the author.
  24. ***************************************************************/
  25. /*---------------------------------------------------------------*/
  26. /* NOTE: Modifications marked "JBV" were made by Jon B. Volkoff, */
  27. /* 11/1995 (eidetics@cerf.net). */
  28. /* */
  29. /* Those additionally marked with "DD" were at the suggestion of */
  30. /* Dale DePriest (daled@cadence.com). */
  31. /*---------------------------------------------------------------*/
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #include <curses.h> /* Should be the ncurses version */
  35. #include <setjmp.h>
  36. #include <signal.h>
  37. #undef TRUE
  38. #undef FALSE
  39. /* So curses TRUE and FALSE conflict with the ones defined by bwbasic.
  40. Doesn't this suck big time?
  41. */
  42. #include "bwbasic.h"
  43. #include "bwb_mes.h"
  44. extern int prn_col;
  45. extern jmp_buf mark;
  46. short oldfgd;
  47. long oldbgd;
  48. int reset_mode = FALSE;
  49. static int ncu_setpos( void );
  50. /***************************************************************
  51. FUNCTION: main()
  52. DESCRIPTION: As in any C program, main() is the basic
  53. function from which the rest of the
  54. program is called. Some environments,
  55. however, provide their own main() functions
  56. (Microsoft Windows (tm) is an example).
  57. In these cases, the following code will
  58. have to be included in the initialization
  59. function that is called by the environment.
  60. ***************************************************************/
  61. static int col_arr[]={COLOR_BLACK,COLOR_RED,COLOR_GREEN,COLOR_YELLOW,
  62. COLOR_BLUE,COLOR_MAGENTA,COLOR_CYAN,COLOR_WHITE};
  63. int /* Nobody shall declare main() as void!!!!! (L.C. Benschop)*/
  64. main( int argc, char **argv )
  65. {
  66. int i,j;
  67. initscr();
  68. start_color();
  69. if(has_colors()) {
  70. /* so there are 63 color pairs, from 1 to 63. Just 1 too few for
  71. all the foreground/background combinations. */
  72. for(i=0;i<8;i++)
  73. for(j=0;j<8;j++)
  74. if(i||j) init_pair(i*8+j,col_arr[i],col_arr[j]);
  75. }
  76. cbreak();
  77. nonl();
  78. noecho();
  79. scrollok(stdscr,1);
  80. bwb_init( argc, argv );
  81. #if INTERACTIVE
  82. setjmp( mark );
  83. #endif
  84. while( !feof( stdin ) ) /* condition !feof( stdin ) added in v1.11 */
  85. {
  86. bwb_mainloop();
  87. }
  88. }
  89. /***************************************************************
  90. FUNCTION: bwx_signon()
  91. DESCRIPTION:
  92. ***************************************************************/
  93. int
  94. bwx_signon( void )
  95. {
  96. sprintf( bwb_ebuf, "\r%s %s\n", MES_SIGNON, VERSION );
  97. prn_xprintf( stdout, bwb_ebuf );
  98. sprintf( bwb_ebuf, "\r%s\n", MES_COPYRIGHT );
  99. prn_xprintf( stdout, bwb_ebuf );
  100. sprintf( bwb_ebuf, "\r%s\n", MES_COPYRIGHT_2 ); /* JBV 1/97 */
  101. prn_xprintf( stdout, bwb_ebuf );
  102. #if PERMANENT_DEBUG
  103. sprintf( bwb_ebuf, "\r%s\n", "Debugging Mode" );
  104. prn_xprintf( stdout, bwb_ebuf );
  105. #else
  106. sprintf( bwb_ebuf, "\r%s\n", MES_LANGUAGE );
  107. prn_xprintf( stdout, bwb_ebuf );
  108. #endif
  109. return TRUE;
  110. }
  111. /***************************************************************
  112. FUNCTION: bwx_message()
  113. DESCRIPTION:
  114. ***************************************************************/
  115. int
  116. bwx_message( char *m )
  117. {
  118. #if DEBUG
  119. addstr( "<MES>" );
  120. #endif
  121. addstr( m );
  122. refresh();
  123. return TRUE;
  124. }
  125. /***************************************************************
  126. FUNCTION: bwx_putc()
  127. DESCRIPTION:
  128. ***************************************************************/
  129. extern int
  130. bwx_putc( char c )
  131. {
  132. addch(c);
  133. refresh();
  134. }
  135. /***************************************************************
  136. FUNCTION: bwx_error()
  137. DESCRIPTION:
  138. ***************************************************************/
  139. int
  140. bwx_errmes( char *m )
  141. {
  142. static char tbuf[ MAXSTRINGSIZE + 1 ]; /* this memory should be
  143. permanent in case of memory
  144. overrun errors */
  145. if (( prn_col != 1 ) && ( errfdevice == stderr ))
  146. {
  147. prn_xprintf( errfdevice, "\n" );
  148. }
  149. if ( CURTASK number == 0 )
  150. {
  151. sprintf( tbuf, "\n%s: %s\n", ERRD_HEADER, m );
  152. }
  153. else
  154. {
  155. sprintf( tbuf, "\n%s %d: %s\n", ERROR_HEADER, CURTASK number, m );
  156. }
  157. #if INTENSIVE_DEBUG
  158. prn_xprintf( stderr, "<ERR>" );
  159. #endif
  160. prn_xprintf( errfdevice, tbuf );
  161. return TRUE;
  162. }
  163. /***************************************************************
  164. FUNCTION: bwx_input()
  165. DESCRIPTION: (w)get(n)str seems to interact badly with last line
  166. on screen condition and scrolling.
  167. ***************************************************************/
  168. int
  169. bwx_input( char *prompt, char *buffer )
  170. {
  171. int y,x,my,mx;
  172. #if INTENSIVE_DEBUG
  173. prn_xprintf( stdout, "<INP>" );
  174. #endif
  175. prn_xprintf( stdout, prompt );
  176. getyx(stdscr,y,x);
  177. echo();
  178. wgetnstr(stdscr, buffer, MAXREADLINESIZE);
  179. noecho();
  180. getmaxyx(stdscr,my,mx);
  181. /* printf("%d %d",my,mx);*/
  182. if(y+1==my)scroll(stdscr);
  183. /* So this is an extreeeeemely ugly kludge to work around some
  184. bug/feature/wart in ncurses FIXME
  185. I should replace it with getch/addch in a loop */
  186. /* prn_xprintf( stdout, "\n" );*/ /* let _outtext catch up */
  187. * prn_getcol( stdout ) = 1; /* reset column */
  188. return TRUE;
  189. }
  190. /***************************************************************
  191. FUNCTION: bwx_terminate()
  192. DESCRIPTION:
  193. ***************************************************************/
  194. void
  195. bwx_terminate( void )
  196. {
  197. nodelay(stdscr,FALSE);
  198. echo();
  199. nl();
  200. nocbreak();
  201. endwin();
  202. exit( 0 );
  203. }
  204. /***************************************************************
  205. FUNCTION: bwx_shell()
  206. DESCRIPTION:
  207. ***************************************************************/
  208. #if COMMAND_SHELL
  209. extern int
  210. bwx_shell( struct bwb_line *l )
  211. {
  212. static char *s_buffer;
  213. static int init = FALSE;
  214. static int position;
  215. /* get memory for temporary buffer if necessary */
  216. if ( init == FALSE )
  217. {
  218. init = TRUE;
  219. /* Revised to CALLOC pass-thru call by JBV */
  220. if ( ( s_buffer = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ),"bwx_shell" )) == NULL )
  221. {
  222. bwb_error( err_getmem );
  223. return FALSE;
  224. }
  225. }
  226. /* get the first element and check for a line number */
  227. #if INTENSIVE_DEBUG
  228. sprintf( bwb_ebuf, "in bwx_shell(): line buffer is <%s>.", l->buffer );
  229. bwb_debug( bwb_ebuf );
  230. #endif
  231. position = 0;
  232. adv_element( l->buffer, &position, s_buffer );
  233. if ( is_numconst( s_buffer ) != TRUE ) /* not a line number */
  234. {
  235. #if INTENSIVE_DEBUG
  236. sprintf( bwb_ebuf, "in bwx_shell(): no line number, command <%s>.",
  237. l->buffer );
  238. bwb_debug( bwb_ebuf );
  239. #endif
  240. nl();
  241. endwin(); /* Added by JBV 10/11/97 */
  242. if ( system( l->buffer ) == 0 )
  243. {
  244. refresh(); /* Added by JBV 10/11/97 */
  245. nonl();
  246. ncu_setpos();
  247. return TRUE;
  248. }
  249. else
  250. {
  251. refresh(); /* Added by JBV 10/11/97 */
  252. nonl();
  253. ncu_setpos();
  254. return FALSE;
  255. }
  256. }
  257. else /* advance past line number */
  258. {
  259. adv_ws( l->buffer, &position ); /* advance past whitespace */
  260. #if INTENSIVE_DEBUG
  261. sprintf( bwb_ebuf, "in bwx_shell(): line number, command <%s>.",
  262. l->buffer );
  263. bwb_debug( bwb_ebuf );
  264. #endif
  265. nl();
  266. endwin(); /* Added by JBV 10/11/97 */
  267. if ( system( &( l->buffer[ position ] ) ) == 0 )
  268. {
  269. refresh(); /* Added by JBV 10/11/97 */
  270. nonl();
  271. ncu_setpos();
  272. return TRUE;
  273. }
  274. else
  275. {
  276. refresh(); /* Added by JBV 10/11/97 */
  277. nonl();
  278. ncu_setpos();
  279. return FALSE;
  280. }
  281. }
  282. }
  283. #endif
  284. /***************************************************************
  285. FUNCTION: ncu_setpos()
  286. DESCRIPTION:
  287. ***************************************************************/
  288. static int
  289. ncu_setpos( void )
  290. {
  291. int x,y;
  292. getyx(stdscr,y,x);
  293. move(y,x);
  294. /* and move down one position */
  295. prn_xprintf( stdout, "\n" );
  296. return TRUE;
  297. }
  298. #if COMMON_CMDS
  299. /***************************************************************
  300. FUNCTION: bwb_edit()
  301. DESCRIPTION:
  302. ***************************************************************/
  303. struct bwb_line *
  304. bwb_edit( struct bwb_line *l )
  305. {
  306. char tbuf[ MAXSTRINGSIZE + 1 ];
  307. char edname[ MAXSTRINGSIZE + 1 ];
  308. struct bwb_variable *ed;
  309. FILE *loadfile;
  310. ed = var_find( DEFVNAME_EDITOR );
  311. str_btoc( edname, var_getsval( ed ));
  312. sprintf( tbuf, "%s %s", edname, CURTASK progfile );
  313. #if INTENSIVE_DEBUG
  314. sprintf( bwb_ebuf, "in bwb_edit(): command line <%s>", tbuf );
  315. bwb_debug( bwb_ebuf );
  316. #else
  317. nl();
  318. endwin(); /* Added by JBV 10/11/97 */
  319. system( tbuf );
  320. /*-----------------------*/
  321. /* Added by JBV 10/11/97 */
  322. /*-----------------------*/
  323. fprintf( stderr, "Press RETURN when ready..." );
  324. fgets( tbuf, MAXREADLINESIZE, stdin );
  325. refresh();
  326. nonl();
  327. #endif
  328. /* open edited file for read */
  329. if ( ( loadfile = fopen( CURTASK progfile, "r" )) == NULL )
  330. {
  331. sprintf( bwb_ebuf, err_openfile, CURTASK progfile );
  332. bwb_error( bwb_ebuf );
  333. ncu_setpos();
  334. return bwb_zline( l );
  335. }
  336. /* clear current contents */
  337. bwb_new( l ); /* Relocated by JBV (bug found by DD) */
  338. /* and (re)load the file into memory */
  339. bwb_fload( loadfile );
  340. ncu_setpos();
  341. return bwb_zline( l );
  342. }
  343. /***************************************************************
  344. FUNCTION: bwb_renum()
  345. DESCRIPTION: This function implements the BASIC RENUM
  346. command by shelling out to a default
  347. renumbering program called "renum".
  348. Added by JBV 10/95
  349. SYNTAX: RENUM
  350. ***************************************************************/
  351. #if ANSI_C
  352. struct bwb_line *
  353. bwb_renum( struct bwb_line *l )
  354. #else
  355. struct bwb_line *
  356. bwb_renum( l )
  357. struct bwb_line *l;
  358. #endif
  359. {
  360. char tbuf[ MAXSTRINGSIZE + 1 ];
  361. FILE *loadfile;
  362. sprintf( tbuf, "renum %s\0", CURTASK progfile );
  363. #if INTENSIVE_DEBUG
  364. sprintf( bwb_ebuf, "in bwb_renum(): command line <%s>", tbuf );
  365. bwb_debug( bwb_ebuf );
  366. #else
  367. nl();
  368. endwin(); /* Added by JBV 10/11/97 */
  369. system( tbuf );
  370. /*-----------------------*/
  371. /* Added by JBV 10/11/97 */
  372. /*-----------------------*/
  373. fprintf( stderr, "Press RETURN when ready..." );
  374. fgets( tbuf, MAXREADLINESIZE, stdin );
  375. refresh();
  376. nonl();
  377. #endif
  378. /* open edited file for read */
  379. if ( ( loadfile = fopen( CURTASK progfile, "r" )) == NULL )
  380. {
  381. sprintf( bwb_ebuf, err_openfile, CURTASK progfile );
  382. bwb_error( bwb_ebuf );
  383. ncu_setpos();
  384. return bwb_zline( l );
  385. }
  386. /* clear current contents */
  387. bwb_new( l ); /* Relocated by JBV (bug found by DD) */
  388. /* and (re)load the file into memory */
  389. bwb_fload( loadfile );
  390. ncu_setpos();
  391. return bwb_zline( l );
  392. }
  393. /***************************************************************
  394. FUNCTION: bwb_files()
  395. DESCRIPTION:
  396. ***************************************************************/
  397. struct bwb_line *
  398. bwb_files( struct bwb_line *l )
  399. {
  400. char tbuf[ MAXVARNAMESIZE + 1 ];
  401. char finame[ MAXVARNAMESIZE + 1 ];
  402. char argument[ MAXVARNAMESIZE + 1 ];
  403. struct bwb_variable *fi;
  404. struct exp_ese *e;
  405. fi = var_find( DEFVNAME_FILES );
  406. str_btoc( finame, var_getsval( fi ));
  407. /* get argument */
  408. adv_ws( l->buffer, &( l->position ));
  409. switch( l->buffer[ l->position ] )
  410. {
  411. case '\0':
  412. case '\r':
  413. case '\n':
  414. argument[ 0 ] = '\0';
  415. break;
  416. default:
  417. e = bwb_exp( l->buffer, FALSE, &( l->position ) );
  418. if ( e->type != STRING )
  419. {
  420. bwb_error( err_mismatch );
  421. return bwb_zline( l );
  422. }
  423. str_btoc( argument, exp_getsval( e ) );
  424. break;
  425. }
  426. sprintf( tbuf, "%s %s", finame, argument );
  427. #if INTENSIVE_DEBUG
  428. sprintf( bwb_ebuf, "in bwb_files(): command line <%s>", tbuf );
  429. bwb_debug( bwb_ebuf );
  430. #else
  431. nl();
  432. endwin(); /* Added by JBV 10/11/97 */
  433. system( tbuf );
  434. /*-----------------------*/
  435. /* Added by JBV 10/11/97 */
  436. /*-----------------------*/
  437. fprintf( stderr, "Press RETURN when ready..." );
  438. fgets( tbuf, MAXREADLINESIZE, stdin );
  439. refresh();
  440. nonl();
  441. #endif
  442. ncu_setpos();
  443. return bwb_zline( l );
  444. }
  445. #endif /* COMMON_CMDS */
  446. #if INTERACTIVE
  447. /***************************************************************
  448. FUNCTION: fnc_inkey()
  449. DESCRIPTION: This C function implements the BASIC INKEY$
  450. function. It is implementation-specific.
  451. ***************************************************************/
  452. extern struct bwb_variable *
  453. fnc_inkey( int argc, struct bwb_variable *argv,int unique)
  454. {
  455. static struct bwb_variable nvar;
  456. char tbuf[ MAXSTRINGSIZE + 1 ];
  457. static int init = FALSE;
  458. int c;
  459. /* initialize the variable if necessary */
  460. if ( init == FALSE )
  461. {
  462. init = TRUE;
  463. var_make( &nvar, STRING);
  464. }
  465. /* check arguments */
  466. #if PROG_ERRORS
  467. if ( argc > 0 )
  468. {
  469. sprintf( bwb_ebuf, "Two many arguments to function INKEY$()" );
  470. bwb_error( bwb_ebuf );
  471. return &nvar;
  472. }
  473. #else
  474. if ( fnc_checkargs( argc, argv, 0, 0 ) == FALSE )
  475. {
  476. return NULL;
  477. }
  478. #endif
  479. /* body of the INKEY$ function */
  480. nodelay(stdscr,1);
  481. if ( (c=getch())==EOF )
  482. {
  483. tbuf[ 0 ] = '\0';
  484. }
  485. else
  486. {
  487. tbuf[ 0 ] = (char) c;
  488. tbuf[ 1 ] = '\0';
  489. }
  490. nodelay(stdscr,0);
  491. /* assign value to nvar variable */
  492. str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );
  493. /* return value contained in nvar */
  494. return &nvar;
  495. }
  496. #endif /* INTERACTIVE */
  497. #if MS_CMDS
  498. /***************************************************************
  499. FUNCTION: bwb_cls()
  500. DESCRIPTION: This C function implements the BASIC CLS
  501. command. It is implementation-specific.
  502. ***************************************************************/
  503. extern struct bwb_line *
  504. bwb_cls( struct bwb_line *l )
  505. {
  506. clear();
  507. refresh();
  508. return bwb_zline( l );
  509. }
  510. /***************************************************************
  511. FUNCTION: bwb_locate()
  512. DESCRIPTION: This C function implements the BASIC LOCATE
  513. command. It is implementation-specific.
  514. ***************************************************************/
  515. extern struct bwb_line *
  516. bwb_locate( struct bwb_line *l )
  517. {
  518. struct exp_ese *e;
  519. int row, column;
  520. /* get first argument */
  521. e = bwb_exp( l->buffer, FALSE, &( l->position ));
  522. row = (int) exp_getnval( e );
  523. /* advance past comma */
  524. adv_ws( l->buffer, &( l->position ));
  525. if ( l->buffer[ l->position ] != ',' )
  526. {
  527. bwb_error( err_syntax );
  528. return bwb_zline( l );
  529. }
  530. ++( l->position );
  531. /* get second argument */
  532. e = bwb_exp( l->buffer, FALSE, &( l->position ));
  533. column = (int) exp_getnval( e );
  534. /* position the cursor */
  535. move( row-1, column-1 );
  536. return bwb_zline( l );
  537. }
  538. /***************************************************************
  539. FUNCTION: bwb_color()
  540. DESCRIPTION: This C function implements the BASIC COLOR
  541. command. It is implementation-specific.
  542. ***************************************************************/
  543. extern struct bwb_line *
  544. bwb_color( struct bwb_line *l )
  545. {
  546. struct exp_ese *e;
  547. int fgcolor,bgcolor;
  548. /* get first argument */
  549. e = bwb_exp( l->buffer, FALSE, &( l->position ));
  550. fgcolor = (int) exp_getnval( e );
  551. #if INTENSIVE_DEBUG
  552. sprintf( bwb_ebuf, "Setting text color to %d", fgcolor );
  553. bwb_debug( bwb_ebuf );
  554. #endif
  555. #if INTENSIVE_DEBUG
  556. sprintf( bwb_ebuf, "Set text color to %d", fgcolor );
  557. bwb_debug( bwb_ebuf );
  558. #endif
  559. /* advance past comma */
  560. adv_ws( l->buffer, &( l->position ));
  561. if ( l->buffer[ l->position ] == ',' )
  562. {
  563. ++( l->position );
  564. /* get second argument */
  565. e = bwb_exp( l->buffer, FALSE, &( l->position ));
  566. bgcolor = (int) exp_getnval( e );
  567. #if INTENSIVE_DEBUG
  568. sprintf( bwb_ebuf, "Setting background color to %d", bgcolor );
  569. bwb_debug( bwb_ebuf );
  570. #endif
  571. /* set the foreground and background color */
  572. if (has_colors()) {
  573. attrset(A_NORMAL);
  574. bkgdset(COLOR_PAIR((bgcolor&7))|' ');
  575. if((fgcolor&7)==0 && (bgcolor&7)==0){
  576. /* we didn't reserve a color pair for fg and bg both black.
  577. Bright black(color 8)==dark gray as foreground color A_DIM
  578. A_INVIS doesn't seem to work. wait for next version of
  579. ncurses, don't bother for now.*/
  580. if(fgcolor<8) attrset(A_INVIS); else attrset(A_DIM);
  581. } else
  582. attrset(COLOR_PAIR((8*(fgcolor&7)+(bgcolor&7))) |
  583. ((fgcolor>7)*A_BOLD));
  584. /* fg colors 8--15 == extra brightness */
  585. } else { /* no colors, have a go at it with reverse/bold/dim */
  586. attrset(A_NORMAL);
  587. bkgdset(A_REVERSE*((fgcolor&7)<(bgcolor&7))|' ');
  588. attrset(A_BOLD*(fgcolor>8)|
  589. A_REVERSE*((fgcolor&7)<(bgcolor&7))|A_INVIS*(fgcolor==bgcolor));
  590. }
  591. #if INTENSIVE_DEBUG
  592. sprintf( bwb_ebuf, "Setting background color to %d\n", bgcolor );
  593. bwb_debug( bwb_ebuf );
  594. #endif
  595. }
  596. return bwb_zline( l );
  597. }
  598. #endif /* MS_CMDS */