File: bwx_ncu.c

package info (click to toggle)
bwbasic 2.20pl2-11
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch, wheezy
  • size: 1,236 kB
  • sloc: ansic: 21,001; makefile: 80
file content (815 lines) | stat: -rw-r--r-- 18,893 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
/***************************************************************

        bwx_ncu.c       Environment-dependent implementation
                        of Bywater BASIC Interpreter
                        for Linux (and others?) using Ncurses
                        library, 
                        
                        This BWBASIC file hacked together by L.C. Benschop,
                        Eindhoven, The Netherlands. 1997/01/14 and
                        1997/01/15  derived from the iqc version.
                        (benschop@eb.ele.tue.nl)

			All the shell commands (like FILES) interact badly 
			with curses. I should replace them with popen/addch
                        
                        Copyright (c) 1993, Ted A. Campbell
			Bywater Software

                        email: tcamp@delphi.com

        Copyright and Permissions Information:

        All U.S. and international rights are claimed by the author,
        Ted A. Campbell.

	This software is released under the terms of the GNU General
	Public License (GPL), which is distributed with this software
	in the file "COPYING".  The GPL specifies the terms under
	which users may copy and use the software in this distribution.

	A separate license is available for commercial distribution,
	for information on which you should contact the author.

***************************************************************/

/*---------------------------------------------------------------*/
/* NOTE: Modifications marked "JBV" were made by Jon B. Volkoff, */
/* 11/1995 (eidetics@cerf.net).                                  */
/*                                                               */
/* Those additionally marked with "DD" were at the suggestion of */
/* Dale DePriest (daled@cadence.com).                            */
/*---------------------------------------------------------------*/

#include <stdio.h>
#include <stdlib.h>
#include <curses.h> /* Should be the ncurses version */
#include <setjmp.h>
#include <signal.h>
#undef TRUE
#undef FALSE

/* So curses TRUE and FALSE conflict with the ones defined by bwbasic.
   Doesn't this suck big time?
   */

#include "bwbasic.h"
#include "bwb_mes.h"

extern int prn_col;
extern jmp_buf mark;
short oldfgd;
long oldbgd;
int reset_mode = FALSE;

static int ncu_setpos( void );

/***************************************************************

        FUNCTION:       main()

        DESCRIPTION:    As in any C program, main() is the basic
                        function from which the rest of the
                        program is called. Some environments,
			however, provide their own main() functions
			(Microsoft Windows (tm) is an example).
			In these cases, the following code will
			have to be included in the initialization
			function that is called by the environment.

***************************************************************/


static int col_arr[]={COLOR_BLACK,COLOR_RED,COLOR_GREEN,COLOR_YELLOW,
               COLOR_BLUE,COLOR_MAGENTA,COLOR_CYAN,COLOR_WHITE};
	       

int /* Nobody shall declare main() as void!!!!! (L.C. Benschop)*/
main( int argc, char **argv )
   {
   int i,j;
   initscr();
   start_color();
   if(has_colors()) {
     /* so there are 63 color pairs, from 1 to 63. Just 1 too few for
        all the foreground/background combinations. */
    for(i=0;i<8;i++)
      for(j=0;j<8;j++)
       if(i||j) init_pair(i*8+j,col_arr[i],col_arr[j]);
   }
   cbreak();
   nonl();
   noecho();
   scrollok(stdscr,1);
   bwb_init( argc, argv );

#if INTERACTIVE
   setjmp( mark );
#endif

   while( !feof( stdin ) )		/* condition !feof( stdin ) added in v1.11 */
      {
      bwb_mainloop();
      }
   }

/***************************************************************

        FUNCTION:       bwx_signon()

        DESCRIPTION:

***************************************************************/

int
bwx_signon( void )
   {

   sprintf( bwb_ebuf, "\r%s %s\n", MES_SIGNON, VERSION );
   prn_xprintf( stdout, bwb_ebuf );
   sprintf( bwb_ebuf, "\r%s\n", MES_COPYRIGHT );
   prn_xprintf( stdout, bwb_ebuf );
   sprintf( bwb_ebuf, "\r%s\n", MES_COPYRIGHT_2 ); /* JBV 1/97 */
   prn_xprintf( stdout, bwb_ebuf );
#if PERMANENT_DEBUG
   sprintf( bwb_ebuf, "\r%s\n", "Debugging Mode" );
   prn_xprintf( stdout, bwb_ebuf );
#else
   sprintf( bwb_ebuf, "\r%s\n", MES_LANGUAGE );
   prn_xprintf( stdout, bwb_ebuf );
#endif

   return TRUE;

   }

/***************************************************************

        FUNCTION:       bwx_message()

        DESCRIPTION:

***************************************************************/

int
bwx_message( char *m )
   {

#if DEBUG
   addstr( "<MES>" );
#endif

   addstr( m );
   refresh();
   return TRUE;

   }

/***************************************************************

	FUNCTION:       bwx_putc()

	DESCRIPTION:

***************************************************************/

extern int
bwx_putc( char c )
   {
   addch(c);
   refresh();
   }

/***************************************************************

        FUNCTION:       bwx_error()

        DESCRIPTION:

***************************************************************/

int
bwx_errmes( char *m )
   {
   static char tbuf[ MAXSTRINGSIZE + 1 ];	/* this memory should be
						   permanent in case of memory
						   overrun errors */

   if (( prn_col != 1 ) && ( errfdevice == stderr ))
      {
      prn_xprintf( errfdevice, "\n" );
      }
   if ( CURTASK number == 0 )
      {
      sprintf( tbuf, "\n%s: %s\n", ERRD_HEADER, m );
      }
   else
      {
      sprintf( tbuf, "\n%s %d: %s\n", ERROR_HEADER, CURTASK number, m );
      }

#if INTENSIVE_DEBUG
   prn_xprintf( stderr, "<ERR>" );
#endif

   prn_xprintf( errfdevice, tbuf );

   return TRUE;

   }

/***************************************************************

        FUNCTION:       bwx_input()

	DESCRIPTION:    (w)get(n)str seems to interact badly with last line
                        on screen condition and scrolling.


***************************************************************/

int
bwx_input( char *prompt, char *buffer )
   {
    int y,x,my,mx; 
#if INTENSIVE_DEBUG
   prn_xprintf( stdout, "<INP>" );
#endif
   prn_xprintf( stdout, prompt );
   getyx(stdscr,y,x);
   echo();
   wgetnstr(stdscr, buffer, MAXREADLINESIZE);
   noecho();
   getmaxyx(stdscr,my,mx);
   /*  printf("%d %d",my,mx);*/
   if(y+1==my)scroll(stdscr);
   /* So this is an extreeeeemely ugly kludge to work around some
      bug/feature/wart in ncurses FIXME
      I should replace it with getch/addch in a loop  */   

   /* prn_xprintf( stdout, "\n" );*/        /* let _outtext catch up */

   * prn_getcol( stdout ) = 1;			/* reset column */
   
   return TRUE;

   }

/***************************************************************

        FUNCTION:       bwx_terminate()

        DESCRIPTION:

***************************************************************/

void
bwx_terminate( void )
   {
    nodelay(stdscr,FALSE);
    echo();
    nl();
    nocbreak();
    endwin();
    exit( 0 );
   }

/***************************************************************

	FUNCTION:       bwx_shell()

	DESCRIPTION:

***************************************************************/

#if COMMAND_SHELL
extern int
bwx_shell( struct bwb_line *l )
   {
   static char *s_buffer;
   static int init = FALSE;
   static int position;

   /* get memory for temporary buffer if necessary */

   if ( init == FALSE )
      {
      init = TRUE;

      /* Revised to CALLOC pass-thru call by JBV */
      if ( ( s_buffer = CALLOC( MAXSTRINGSIZE + 1, sizeof( char ),"bwx_shell" )) == NULL )
	 {
	 bwb_error( err_getmem );
	 return FALSE;
	 }
      }

   /* get the first element and check for a line number */

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwx_shell(): line buffer is <%s>.", l->buffer );
   bwb_debug( bwb_ebuf );
#endif

   position = 0;
   adv_element( l->buffer, &position, s_buffer );
   if ( is_numconst( s_buffer ) != TRUE )                  /* not a line number */
      {

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in bwx_shell(): no line number, command <%s>.",
	 l->buffer );
      bwb_debug( bwb_ebuf );
#endif
      nl();
      endwin(); /* Added by JBV 10/11/97 */
      if ( system( l->buffer ) == 0 )
	 {
         refresh(); /* Added by JBV 10/11/97 */
         nonl();
	 ncu_setpos();
	 return TRUE;
	 }
      else
	 {
         refresh(); /* Added by JBV 10/11/97 */
         nonl();
	 ncu_setpos();
	 return FALSE;
	 }
      }

   else                                         /* advance past line number */
      {
      adv_ws( l->buffer, &position );           /* advance past whitespace */

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "in bwx_shell(): line number, command <%s>.",
	 l->buffer );
      bwb_debug( bwb_ebuf );
#endif
      nl();
      endwin(); /* Added by JBV 10/11/97 */
      if ( system( &( l->buffer[ position ] ) ) == 0 )
	 {
         refresh(); /* Added by JBV 10/11/97 */
         nonl();
	 ncu_setpos();
	 return TRUE;
	 }
      else
	 {
         refresh(); /* Added by JBV 10/11/97 */
         nonl();
	 ncu_setpos();
	 return FALSE;
	 }
      }
   }
#endif

/***************************************************************

	FUNCTION:      ncu_setpos()

	DESCRIPTION:

***************************************************************/

static int
ncu_setpos( void )
   {
    int x,y;
    getyx(stdscr,y,x);
    move(y,x);
   /* and move down one position */

   prn_xprintf( stdout, "\n" );

   return TRUE;
   }


#if COMMON_CMDS

/***************************************************************

        FUNCTION:       bwb_edit()

        DESCRIPTION:

***************************************************************/

struct bwb_line *
bwb_edit( struct bwb_line *l )
   {
   char tbuf[ MAXSTRINGSIZE + 1 ];
   char edname[ MAXSTRINGSIZE + 1 ];
   struct bwb_variable *ed;
   FILE *loadfile;

   ed = var_find( DEFVNAME_EDITOR );
   str_btoc( edname, var_getsval( ed ));

   sprintf( tbuf, "%s %s", edname, CURTASK progfile );

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_edit(): command line <%s>", tbuf );
   bwb_debug( bwb_ebuf );
#else
   nl();
   endwin(); /* Added by JBV 10/11/97 */
   system( tbuf );

   /*-----------------------*/
   /* Added by JBV 10/11/97 */
   /*-----------------------*/
   fprintf( stderr, "Press RETURN when ready..." );
   fgets( tbuf, MAXREADLINESIZE, stdin );
   refresh();

   nonl();
#endif

   /* open edited file for read */

   if ( ( loadfile = fopen( CURTASK progfile, "r" )) == NULL )
      {
      sprintf( bwb_ebuf, err_openfile, CURTASK progfile );
      bwb_error( bwb_ebuf );

      ncu_setpos();
      return bwb_zline( l );
      }

   /* clear current contents */

   bwb_new( l ); /* Relocated by JBV (bug found by DD) */

   /* and (re)load the file into memory */

   bwb_fload( loadfile );


   ncu_setpos();
   return bwb_zline( l );
   }

/***************************************************************

        FUNCTION:       bwb_renum()

	DESCRIPTION:    This function implements the BASIC RENUM
			command by shelling out to a default
			renumbering program called "renum".
			Added by JBV 10/95

	SYNTAX:		RENUM

***************************************************************/

#if ANSI_C
struct bwb_line *
bwb_renum( struct bwb_line *l )
#else
struct bwb_line *
bwb_renum( l )
   struct bwb_line *l;
#endif
   {
   char tbuf[ MAXSTRINGSIZE + 1 ];
   FILE *loadfile;

   sprintf( tbuf, "renum %s\0", CURTASK progfile );

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_renum(): command line <%s>", tbuf );
   bwb_debug( bwb_ebuf );
#else
   nl();
   endwin(); /* Added by JBV 10/11/97 */
   system( tbuf );

   /*-----------------------*/
   /* Added by JBV 10/11/97 */
   /*-----------------------*/
   fprintf( stderr, "Press RETURN when ready..." );
   fgets( tbuf, MAXREADLINESIZE, stdin );
   refresh();

   nonl();
#endif

   /* open edited file for read */

   if ( ( loadfile = fopen( CURTASK progfile, "r" )) == NULL )
      {
      sprintf( bwb_ebuf, err_openfile, CURTASK progfile );
      bwb_error( bwb_ebuf );

      ncu_setpos();
      return bwb_zline( l );
      }

   /* clear current contents */

   bwb_new( l ); /* Relocated by JBV (bug found by DD) */

   /* and (re)load the file into memory */

   bwb_fload( loadfile );


   ncu_setpos();
   return bwb_zline( l );
   }

/***************************************************************

        FUNCTION:       bwb_files()

        DESCRIPTION:

***************************************************************/

struct bwb_line *
bwb_files( struct bwb_line *l )
   {
   char tbuf[ MAXVARNAMESIZE + 1 ];
   char finame[ MAXVARNAMESIZE + 1 ];
   char argument[ MAXVARNAMESIZE + 1 ];
   struct bwb_variable *fi;
   struct exp_ese *e;

   fi = var_find( DEFVNAME_FILES );
   str_btoc( finame, var_getsval( fi ));

   /* get argument */

   adv_ws( l->buffer, &( l->position ));
   switch( l->buffer[ l->position ] )
      {
      case '\0':
      case '\r':
      case '\n':
         argument[ 0 ] = '\0';
         break;
      default:
         e = bwb_exp( l->buffer, FALSE, &( l->position ) );
         if ( e->type != STRING )
            {
            bwb_error( err_mismatch );
            return bwb_zline( l );
            }
         str_btoc( argument, exp_getsval( e ) );
         break;
      }


   sprintf( tbuf, "%s %s", finame, argument );

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "in bwb_files(): command line <%s>", tbuf );
   bwb_debug( bwb_ebuf );
#else
   nl();
   endwin(); /* Added by JBV 10/11/97 */
   system( tbuf );

   /*-----------------------*/
   /* Added by JBV 10/11/97 */
   /*-----------------------*/
   fprintf( stderr, "Press RETURN when ready..." );
   fgets( tbuf, MAXREADLINESIZE, stdin );
   refresh();

   nonl();
#endif

   ncu_setpos();
   return bwb_zline( l );

   }

#endif					/* COMMON_CMDS */

#if INTERACTIVE

/***************************************************************

        FUNCTION:       fnc_inkey()

        DESCRIPTION:    This C function implements the BASIC INKEY$
        		function.  It is implementation-specific.

***************************************************************/

extern struct bwb_variable *
fnc_inkey( int argc, struct bwb_variable *argv,int unique)
   {
   static struct bwb_variable nvar;
   char tbuf[ MAXSTRINGSIZE + 1 ];
   static int init = FALSE;
   int c;

   /* initialize the variable if necessary */

   if ( init == FALSE )
      {
      init = TRUE;
      var_make( &nvar, STRING);
      }

   /* check arguments */

#if PROG_ERRORS
   if ( argc > 0 )
      {
      sprintf( bwb_ebuf, "Two many arguments to function INKEY$()" );
      bwb_error( bwb_ebuf );
      return &nvar;
      }

#else
   if ( fnc_checkargs( argc, argv, 0, 0 ) == FALSE )
      {
      return NULL;
      }
#endif

   /* body of the INKEY$ function */
   
   nodelay(stdscr,1);
   if ( (c=getch())==EOF )
      {
      tbuf[ 0 ] = '\0';
      }
   else
      {
      tbuf[ 0 ] = (char) c;
      tbuf[ 1 ] = '\0';
      }
   nodelay(stdscr,0);
   /* assign value to nvar variable */

   str_ctob( var_findsval( &nvar, nvar.array_pos ), tbuf );

   /* return value contained in nvar */

   return &nvar;

   }

#endif				/* INTERACTIVE */

#if MS_CMDS

/***************************************************************

        FUNCTION:       bwb_cls()

        DESCRIPTION:    This C function implements the BASIC CLS
        		command.  It is implementation-specific.

***************************************************************/

extern struct bwb_line *
bwb_cls( struct bwb_line *l )
   {

   clear();
   refresh();

   return bwb_zline( l );
   }

/***************************************************************

        FUNCTION:       bwb_locate()

        DESCRIPTION:    This C function implements the BASIC LOCATE
        		command.  It is implementation-specific.

***************************************************************/

extern struct bwb_line *
bwb_locate( struct bwb_line *l )
   {
   struct exp_ese *e;
   int row, column;

   /* get first argument */

   e = bwb_exp( l->buffer, FALSE, &( l->position ));
   row = (int) exp_getnval( e );

   /* advance past comma */

   adv_ws( l->buffer, &( l->position ));
   if ( l->buffer[ l->position ] != ',' )
      {
      bwb_error( err_syntax );
      return bwb_zline( l );
      }
   ++( l->position );

   /* get second argument */

   e = bwb_exp( l->buffer, FALSE, &( l->position ));
   column = (int) exp_getnval( e );

   /* position the cursor */

   move( row-1, column-1 );

   return bwb_zline( l );
   }

/***************************************************************

	FUNCTION:       bwb_color()

	DESCRIPTION:    This C function implements the BASIC COLOR
			command.  It is implementation-specific.

***************************************************************/


extern struct bwb_line *
bwb_color( struct bwb_line *l )
   {
   struct exp_ese *e;
   int fgcolor,bgcolor;

   /* get first argument */

   e = bwb_exp( l->buffer, FALSE, &( l->position ));
   fgcolor = (int) exp_getnval( e );

#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "Setting text color to %d", fgcolor );
   bwb_debug( bwb_ebuf );
#endif


#if INTENSIVE_DEBUG
   sprintf( bwb_ebuf, "Set text color to %d", fgcolor );
   bwb_debug( bwb_ebuf );
#endif

   /* advance past comma */

   adv_ws( l->buffer, &( l->position ));
   if ( l->buffer[ l->position ] == ',' )
      {

      ++( l->position );

      /* get second argument */

      e = bwb_exp( l->buffer, FALSE, &( l->position ));
      bgcolor = (int) exp_getnval( e );

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "Setting background color to %d", bgcolor );
      bwb_debug( bwb_ebuf );
#endif

      /* set the foreground and background color */
      if (has_colors()) {
	attrset(A_NORMAL);
        bkgdset(COLOR_PAIR((bgcolor&7))|' ');
        if((fgcolor&7)==0 && (bgcolor&7)==0){
	 /* we didn't reserve a color pair for fg and bg both black. 
            Bright black(color 8)==dark gray as foreground color A_DIM
            A_INVIS doesn't seem to work. wait for next version of
            ncurses, don't bother for now.*/
         if(fgcolor<8) attrset(A_INVIS); else attrset(A_DIM);
        } else
        attrset(COLOR_PAIR((8*(fgcolor&7)+(bgcolor&7))) | 
                      ((fgcolor>7)*A_BOLD));
        /* fg colors 8--15 == extra brightness */
      } else { /* no colors, have a go at it with reverse/bold/dim */
        attrset(A_NORMAL);
        bkgdset(A_REVERSE*((fgcolor&7)<(bgcolor&7))|' ');
        attrset(A_BOLD*(fgcolor>8)|
          A_REVERSE*((fgcolor&7)<(bgcolor&7))|A_INVIS*(fgcolor==bgcolor));
      }
      

#if INTENSIVE_DEBUG
      sprintf( bwb_ebuf, "Setting background color to %d\n", bgcolor );
      bwb_debug( bwb_ebuf );
#endif

      }

   return bwb_zline( l );
   }
#endif /* MS_CMDS */