|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274 |
- /***************************************************************
-
- bwb_cmd.c Miscellaneous Commands
- for Bywater BASIC Interpreter
-
- Commands: RUN
- LET
- LOAD
- MERGE
- CHAIN
- NEW
- RENUM
- SAVE
- LIST
- GOTO
- GOSUB
- RETURN
- ON
- STOP
- END
- SYSTEM
- TRON
- TROFF
- DELETE
- RANDOMIZE
- ENVIRON
- CMDS (*debugging)
-
- 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 <math.h>
- #include <ctype.h>
-
- #include "bwbasic.h"
- #include "bwb_mes.h"
-
- #if HAVE_SIGNAL
- #include <signal.h>
- #endif
-
- char err_gosubl[ MAXVARNAMESIZE + 1 ] = { '\0' }; /* line for error GOSUB */
-
- #if ANSI_C
- extern struct bwb_line *bwb_xnew( struct bwb_line *l );
- extern struct bwb_line *bwb_onerror( struct bwb_line *l );
- struct bwb_line *bwb_donum( struct bwb_line *l );
- struct bwb_line *bwb_dounnum( struct bwb_line *l );
- static int xl_line( FILE *file, struct bwb_line *l );
- #else
- extern struct bwb_line *bwb_xnew();
- extern struct bwb_line *bwb_onerror();
- struct bwb_line *bwb_donum();
- struct bwb_line *bwb_dounnum();
- static int xl_line();
- #endif
-
- /***************************************************************
-
- FUNCTION: bwb_null()
-
- DESCRIPTION: This is a null command function body, and
- can be used as the basis for developing
- new BASIC commands.
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_null( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_null( l )
- struct bwb_line *l;
- #endif
- {
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_null(): NULL command" );
- bwb_debug( bwb_ebuf );
- #endif
-
- #if MULTISEG_LINES
- adv_eos( l->buffer, &( l->position ));
- #endif
-
- return bwb_zline( l );
- }
-
- /***************************************************************
-
- FUNCTION: bwb_rem()
-
- DESCRIPTION: This C function implements the BASIC rem
- (REMark) command, ignoring the remainder
- of the line.
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_rem( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_rem( l )
- struct bwb_line *l;
- #endif
- {
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_rem(): REM command" );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* do not use bwb_zline() here; blank out remainder of line */
-
- l->next->position = 0;
- return l->next;
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_let()
-
- DESCRIPTION: This C function implements the BASIC
- LET assignment command, even if LET
- is implied and not explicit.
-
- SYNTAX: LET variable = expression
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_let( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_let( l )
- struct bwb_line *l;
- #endif
- {
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_let(): pos <%d> line <%s>",
- l->position, l->buffer );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* Call the expression interpreter to evaluate the assignment */
-
- bwb_exp( l->buffer, TRUE, &( l->position ) );
-
-
- return bwb_zline( l );
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_go
-
- DESCRIPTION: This C function implements the BASIC
- GO command, branching appropriately to
- GOTO or GOSUB.
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_go( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_go( l )
- struct bwb_line *l;
- #endif
- {
- char tbuf[ MAXSTRINGSIZE + 1 ];
-
- adv_element( l->buffer, &( l->position ), tbuf );
- bwb_strtoupper( tbuf );
-
- if ( strcmp( tbuf, CMD_XSUB ) == 0 )
- {
- return bwb_gosub( l );
- }
-
- if ( strcmp( tbuf, CMD_XTO ) == 0 )
- {
- return bwb_goto( l );
- }
-
- #if PROG_ERRORS
- sprintf( bwb_ebuf, "in bwb_go(): Nonsense following GO" );
- bwb_error( bwb_ebuf );
- #else
- bwb_error( err_syntax );
- #endif
-
- return bwb_zline( l );
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_goto
-
- DESCRIPTION: This C function implements the BASIC
- GOTO command.
-
- SYNTAX: GOTO line | label
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_goto( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_goto( l )
- struct bwb_line *l;
- #endif
- {
- struct bwb_line *x;
- char tbuf[ MAXSTRINGSIZE + 1 ];
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_goto(): entered function" );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* Check for argument */
-
- adv_ws( l->buffer, &( l->position ) );
- switch( l->buffer[ l->position ] )
- {
- case '\0':
- case '\n':
- case '\r':
- case ':':
- bwb_error( err_noln );
-
- return bwb_zline( l );
- default:
- break;
- }
-
- adv_element( l->buffer, &( l->position ), tbuf );
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_goto(): buffer has <%s>", tbuf );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* check for target label */
-
- #if STRUCT_CMDS
-
- if ( isalpha( tbuf[ 0 ] ))
- {
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_goto(): found LABEL, <%s>", tbuf );
- bwb_debug( bwb_ebuf );
- #endif
-
- x = find_label( tbuf );
- x->position = 0;
- return x;
- }
-
- else
- {
- for ( x = &CURTASK bwb_start; x != &CURTASK bwb_end; x = x->next )
- {
- if ( x->number == atoi( tbuf ) )
- {
-
- /* found the requested number */
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_goto(): returning line <%d>", x->number );
- bwb_debug( bwb_ebuf );
- #endif
-
- x->position = 0;
- return x;
- }
- }
- }
-
- #else
-
- for ( x = &CURTASK bwb_start; x != &CURTASK bwb_end; x = x->next )
- {
- if ( x->number == atoi( tbuf ) )
- {
-
- /* found the requested number */
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_goto(): returning line <%d>", x->number );
- bwb_debug( bwb_ebuf );
- #endif
-
- x->position = 0;
- return x;
- }
- }
-
- #endif
-
- sprintf( bwb_ebuf, err_lnnotfound, atoi( tbuf ) );
- bwb_error( bwb_ebuf );
-
- return bwb_zline( l );
- }
-
- /***************************************************************
-
- FUNCTION: bwb_gosub()
-
- DESCRIPTION: This function implements the BASIC GOSUB
- command.
-
- SYNTAX: GOSUB line | label
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_gosub( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_gosub( l )
- struct bwb_line *l;
- #endif
- {
- struct bwb_line *x;
- char atbuf[ MAXSTRINGSIZE + 1 ];
-
- /* Check for argument */
-
- adv_ws( l->buffer, &( l->position ) );
- switch( l->buffer[ l->position ] )
- {
- case '\0':
- case '\n':
- case '\r':
- case ':':
- sprintf( bwb_ebuf, err_noln );
- bwb_error( bwb_ebuf );
-
- return bwb_zline( l );
- default:
- break;
- }
-
- /* get the target line number in tbuf */
-
- adv_element( l->buffer, &( l->position ), atbuf );
-
- #if MULTISEG_LINES
- adv_eos( l->buffer, &( l->position ));
- #endif
-
- /* check for a label rather than line number */
-
- #if STRUCT_CMDS
-
- if ( isalpha( atbuf[ 0 ] ))
- {
- x = find_label( atbuf );
-
- #if MULTISEG_LINES
- CURTASK excs[ CURTASK exsc ].position = l->position;
- #endif
-
- bwb_incexec();
-
- /* set the new position to x and return x */
-
- x->cmdnum = -1;
- x->marked = FALSE;
- x->position = 0;
- bwb_setexec( x, 0, EXEC_GOSUB );
-
- return x;
-
- }
-
- #endif
-
- for ( x = &CURTASK bwb_start; x != &CURTASK bwb_end; x = x->next )
- {
-
- if ( x->number == atoi( atbuf ))
- {
-
- /* this is the line we are looking for */
-
- #if MULTISEG_LINES
- CURTASK excs[ CURTASK exsc ].position = l->position;
- #endif
-
- /* increment the EXEC stack */
-
- bwb_incexec();
-
- /* set the new position to x and return x */
-
- x->cmdnum = -1;
- x->marked = FALSE;
- x->position = 0;
- bwb_setexec( x, 0, EXEC_GOSUB );
-
- return x;
- }
- }
-
- /* the requested line was not found */
-
- sprintf( bwb_ebuf, err_lnnotfound, atoi( atbuf ) );
- bwb_error( bwb_ebuf );
-
- return bwb_zline( l );
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_return()
-
- DESCRIPTION: This function implements the BASIC RETURN
- command.
-
- SYNTAX: RETURN
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_return( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_return( l )
- struct bwb_line *l;
- #endif
- {
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_return() at line <%d> cmdnum <%d>",
- l->number, l->cmdnum );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* see if old position was "GOSUB" */
-
- if ( CURTASK excs[ CURTASK exsc ].code != EXEC_GOSUB )
- {
- bwb_error( err_retnogosub );
- }
-
- /* decrement the EXEC stack counter */
-
- bwb_decexec();
-
- /* restore position and return old line */
-
- #if MULTISEG_LINES
- CURTASK excs[ CURTASK exsc ].line->position
- = CURTASK excs[ CURTASK exsc ].position;
- return CURTASK excs[ CURTASK exsc ].line;
- #else
- CURTASK excs[ CURTASK exsc ].line->next->position = 0;
- return CURTASK excs[ CURTASK exsc ].line->next;
- #endif
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_on
-
- DESCRIPTION: This function implements the BASIC ON...
- GOTO or ON...GOSUB statements.
-
- It will also detect the ON ERROR... statement
- and pass execution to bwb_onerror().
-
- SYNTAX: ON variable GOTO|GOSUB line[,line,line,...]
-
- LIMITATION: As implemented here, the ON...GOSUB|GOTO
- command recognizes line numbers only
- (not labels).
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_on( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_on( l )
- struct bwb_line *l;
- #endif
- {
- struct bwb_line *oline, *x;
- char varname[ MAXVARNAMESIZE + 1 ];
- char tbuf[ MAXSTRINGSIZE + 1 ];
- static int p;
- struct exp_ese *rvar;
- int v;
- int loop;
- int num_lines;
- int command;
- int lines[ MAX_GOLINES ];
- char sbuf[ 7 ];
-
- /* Check for argument */
-
- adv_ws( l->buffer, &( l->position ) );
-
- switch( l->buffer[ l->position ] )
- {
- case '\0':
- case '\n':
- case '\r':
- case ':':
- sprintf( bwb_ebuf, err_incomplete );
- bwb_error( bwb_ebuf );
-
- return bwb_zline( l );
- default:
- break;
- }
-
- /* get the variable name or numerical constant */
-
- adv_element( l->buffer, &( l->position ), varname );
-
- /* check for ON ERROR statement */
-
- #if COMMON_CMDS
- strncpy( sbuf, varname, 6 );
- bwb_strtoupper( sbuf );
- if ( strcmp( sbuf, CMD_XERROR ) == 0 )
- {
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_on(): detected ON ERROR" );
- bwb_debug( bwb_ebuf );
- #endif
- return bwb_onerror( l );
- }
- #endif /* COMMON_CMDS */
-
- /* evaluate the variable name or constant */
-
- p = 0;
- rvar = bwb_exp( varname, FALSE, &p );
- v = (int) exp_getnval( rvar );
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_on(): value is <%d>", v );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* Get GOTO or GOSUB statements */
-
- adv_element( l->buffer, &( l->position ), tbuf );
- bwb_strtoupper( tbuf );
- if ( strncmp( tbuf, CMD_GOTO, (size_t) strlen( CMD_GOTO ) ) == 0 )
- {
- command = getcmdnum( CMD_GOTO );
- }
- else if ( strncmp( tbuf, CMD_GOSUB, (size_t) strlen( CMD_GOSUB ) ) == 0 )
- {
- command = getcmdnum( CMD_GOSUB );
- }
- else
- {
- sprintf( bwb_ebuf, ERR_ONNOGOTO );
- bwb_error( bwb_ebuf );
-
- return bwb_zline( l );
- }
-
- num_lines = 0;
-
- loop = TRUE;
- while( loop == TRUE )
- {
-
- /* read a line number */
-
- inp_adv( l->buffer, &( l->position ) );
- adv_element( l->buffer, &( l->position ), tbuf );
-
- lines[ num_lines ] = atoi( tbuf );
-
- ++num_lines;
-
- if ( num_lines >= MAX_GOLINES )
- {
- loop = FALSE;
- }
-
- /* check for end of line */
-
- adv_ws( l->buffer, &( l->position ) );
- switch( l->buffer[ l->position ] )
- {
- case '\0':
- case '\n':
- case '\r':
- case ':':
- loop = FALSE;
- break;
- }
-
- }
-
- /* advance to end of segment */
-
- #if MULTISEG_LINES
- adv_eos( l->buffer, &( l->position ) );
- #endif
-
- /* Be sure value is in range */
-
- if ( ( v < 1 ) || ( v > num_lines ))
- {
- sprintf( bwb_ebuf, err_valoorange );
- bwb_error( bwb_ebuf );
-
- return bwb_zline( l );
- }
-
- if ( command == getcmdnum( CMD_GOTO ))
- {
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_on(): executing ON...GOTO" );
- bwb_debug( bwb_ebuf );
- #endif
-
- oline = NULL;
- for ( x = &CURTASK bwb_start; x != &CURTASK bwb_end; x = x->next )
- {
- if ( x->number == lines[ v - 1 ] )
- {
-
- /* found the requested number */
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_on(): returning line <%d>", x->number );
- bwb_debug( bwb_ebuf );
- #endif
-
- oline = x;
- }
- }
-
- if ( oline == NULL )
- {
- bwb_error( err_lnnotfound );
- return bwb_zline( l );
- }
-
- oline->position = 0;
- bwb_setexec( oline, 0, CURTASK excs[ CURTASK exsc ].code );
-
- return oline;
-
- }
-
- else if ( command == getcmdnum( CMD_GOSUB ))
- {
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_on(): executing ON...GOSUB" );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* save current stack level */
-
- bwb_setexec( l, l->position, CURTASK excs[ CURTASK exsc ].code );
-
- /* increment exec stack */
-
- bwb_incexec();
-
- /* get memory for line and buffer */
-
- /* Revised to CALLOC pass-thru call by JBV */
- if ( ( oline = CALLOC( 1, sizeof( struct bwb_line ), "bwb_on") ) == NULL )
- {
- #if PROG_ERRORS
- bwb_error( "in bwb_on(): failed to find memory for oline" );
- #else
- bwb_error( err_getmem );
- #endif
- }
- /* Revised to CALLOC pass-thru call by JBV */
- if ( ( oline->buffer = CALLOC( 1, MAXSTRINGSIZE + 1, "bwb_on") ) == NULL )
- {
- #if PROG_ERRORS
- bwb_error( "in bwb_on(): failed to find memory for oline buffer" );
- #else
- bwb_error( err_getmem );
- #endif
- }
-
- CURTASK excs[ CURTASK exsc ].while_line = oline;
-
- sprintf( oline->buffer, "%s %d", CMD_GOSUB, lines[ v - 1 ] );
- oline->marked = FALSE;
- oline->position = 0;
- oline->next = l->next;
- bwb_setexec( oline, 0, EXEC_ON );
- return oline;
- }
-
- else
- {
- #if PROG_ERRORS
- sprintf( bwb_ebuf, "in bwb_on(): invalid value for command." );
- bwb_error( bwb_ebuf );
- #else
- bwb_error( err_syntax );
- #endif
-
- return bwb_zline( l );
- }
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_stop()
-
- DESCRIPTION: This C function implements the BASIC
- STOP command, interrupting program flow
- at a specific point.
-
- SYNTAX: STOP
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_stop( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_stop( l )
- struct bwb_line *l;
- #endif
- {
-
- #if HAVE_SIGNAL
- #if HAVE_RAISE
- raise( SIGINT );
- #else
- kill( getpid(), SIGINT );
- #endif
- #endif
-
- return bwb_xend( l );
- }
-
- /***************************************************************
-
- FUNCTION: bwb_xend()
-
- DESCRIPTION: This C function implements the BASIC
- END command, checking for END SUB
- or END FUNCTION, else stopping program
- execution for a simple END command.
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_xend( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_xend( l )
- struct bwb_line *l;
- #endif
- {
- #if STRUCT_CMDS
- char tbuf[ MAXSTRINGSIZE + 1 ];
- #endif
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_xend(): entered funtion" );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* Detect END SUB or END FUNCTION here */
-
- #if STRUCT_CMDS
- adv_element( l->buffer, &( l->position ), tbuf );
- bwb_strtoupper( tbuf );
-
- if ( strcmp( tbuf, CMD_XSUB ) == 0 )
- {
- return bwb_endsub( l );
- }
-
- if ( strcmp( tbuf, CMD_XFUNCTION ) == 0 )
- {
- return bwb_endfnc( l );
- }
-
- if ( strcmp( tbuf, CMD_XIF ) == 0 )
- {
- return bwb_endif( l );
- }
-
- if ( strcmp( tbuf, CMD_XSELECT ) == 0 )
- {
- return bwb_endselect( l );
- }
-
- #endif /* STRUCT_CMDS */
-
- /* else a simple END statement */
-
- break_handler();
-
- return &CURTASK bwb_end;
- }
-
- /***************************************************************
-
- FUNCTION: bwb_do()
-
- DESCRIPTION: This C function implements the BASIC DO
- command, also checking for the DO NUM
- and DO UNNUM commands for interactive
- programming environment.
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_do( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_do( l )
- struct bwb_line *l;
- #endif
- {
- char tbuf[ MAXSTRINGSIZE + 1 ];
-
- adv_element( l->buffer, &( l->position ), tbuf );
- bwb_strtoupper( tbuf );
-
- /* if there is no argument (with STRUCT_CMDS) then we have a
- DO-LOOP structure: pass on to bwb_doloop() in bwb_stc.c */
-
- #if STRUCT_CMDS
-
- if ( strlen( tbuf ) == 0 )
- {
- return bwb_doloop( l );
- }
-
- if ( strcmp( tbuf, CMD_WHILE ) == 0 )
- {
- return bwb_while( l );
- }
- #endif
-
- #if INTERACTIVE
- if ( strcmp( tbuf, CMD_XNUM ) == 0 )
- {
- return bwb_donum( l );
- }
-
- if ( strcmp( tbuf, CMD_XUNNUM ) == 0 )
- {
- return bwb_dounnum( l );
- }
- #endif /* INTERACTIVE */
-
- /* if none of these occurred, then presume an error */
-
- bwb_error( err_syntax );
- return bwb_zline( l );
- }
-
- /***************************************************************
-
- FUNCTION: bwb_run()
-
- DESCRIPTION: This C function implements the BASIC
- RUN command.
-
- Even though RUN is not a core statement,
- the function bwb_run() is called from
- core, so it must be present for a minimal
- implementation.
-
- SYNTAX: RUN [line]|[file-name]
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_run( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_run( l )
- struct bwb_line *l;
- #endif
- {
- struct bwb_line *current, *x;
- int go_lnumber; /* line number to go to */
- char tbuf[ MAXSTRINGSIZE + 1 ];
- struct exp_ese *e;
- FILE *input;
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_run(): entered function. buffer <%s> pos <%d>",
- l->buffer, l->position );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* see if there is an element */
-
- current = NULL;
- adv_ws( l->buffer, &( l->position ) );
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_run(): check buffer <%s> pos <%d> char <0x%x>",
- l->buffer, l->position, l->buffer[ l->position ] );
- bwb_debug( bwb_ebuf );
- #endif
- switch ( l->buffer[ l->position ] )
- {
- case '\0':
- case '\n':
- case '\r':
- case ':':
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_run(): no argument; begin at start.next" );
- bwb_debug( bwb_ebuf );
- #endif
- current = CURTASK bwb_start.next;
- e = NULL;
- break;
- default:
- e = bwb_exp( l->buffer, FALSE, &( l->position ) );
- break;
- }
-
- /* check its type: if it is a string, open the file and execute it */
-
- if (( e != NULL ) && ( e->type == STRING ))
- {
- bwb_new( l ); /* clear memory */
- str_btoc( tbuf, exp_getsval( e ) ); /* get string in tbuf */
- if ( ( input = fopen( tbuf, "r" )) == NULL ) /* open file */
- {
- sprintf( bwb_ebuf, err_openfile, tbuf );
- bwb_error( bwb_ebuf );
- }
- bwb_fload( input ); /* load program */
-
- /* Next line removed by JBV (unnecessary recursion asks for trouble) */
- /* bwb_run( &CURTASK bwb_start ); */ /* and call bwb_run() recursively */
- current = &CURTASK bwb_start; /* JBV */
- }
-
- /* else if it is a line number, execute the program in memory
- at that line number */
-
- /* Removed by JBV */
- /* else
- { */
-
- /* Removed by JBV */
- /* if ( current == NULL )
- { */
-
- /* Added expression type check and changed loop boundaries (JBV) */
- if (( e != NULL ) && ( e->type != STRING ))
- {
- go_lnumber = (int) exp_getnval( e );
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_run(): element detected <%s>, lnumber <%d>",
- tbuf, go_lnumber );
- bwb_debug( bwb_ebuf );
- #endif
-
- for ( x = CURTASK bwb_start.next; x != &CURTASK bwb_end; x = x->next )
- {
- if ( x->number == go_lnumber )
- {
- current = x;
- }
- }
- }
-
- /* } */ /* Removed by JBV */
-
- if ( current == NULL )
- {
- sprintf( bwb_ebuf, err_lnnotfound, go_lnumber );
- bwb_error( bwb_ebuf );
- return &CURTASK bwb_end;
- }
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_run(): ready to run starting at line %d",
- current->number );
- bwb_debug( bwb_ebuf );
- #endif
-
- if ( CURTASK rescan == TRUE )
- {
- bwb_scan();
- }
-
- current->position = 0;
- CURTASK exsc = 0;
- bwb_setexec( current, 0, EXEC_NORM );
-
- /* } */ /* Removed by JBV */
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_run(): function complete." );
- bwb_debug( bwb_ebuf );
- #endif
-
- return current;
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_new()
-
- DESCRIPTION: This C function implements the BASIC
- NEW command.
-
- Even though NEW is not a core statement,
- the function bwb_run() is called from
- core, so it must be present for a minimal
- implementation.
-
- SYNTAX: NEW
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_new( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_new( l )
- struct bwb_line *l;
- #endif
- {
-
- /* clear program in memory */
-
- bwb_xnew( l );
-
- /* clear all variables */
-
- bwb_clear( l );
-
-
- return bwb_zline( l );
-
- }
-
- /* End of Core Functions Section */
-
- #if INTERACTIVE
-
- /***************************************************************
-
- FUNCTION: bwb_system()
-
- DESCRIPTION: This C function implements the BASIC
- SYSTEM command, exiting to the operating
- system (or calling program). It is also
- called by the QUIT command, a functional
- equivalent for SYSTEM in Bywater BASIC.
-
- SYNTAX: SYSTEM
- QUIT
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_system( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_system( l )
- struct bwb_line *l;
- #endif
- {
- prn_xprintf( stdout, "\n" );
-
- #if INTENSIVE_DEBUG
- bwb_debug( "in bwb_system(): ready to exit" );
- #endif
-
- bwx_terminate();
- return &CURTASK bwb_end; /* to make LINT happy */
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_load()
-
- DESCRIPTION: This C function implements the BASIC
- LOAD command.
-
- SYNTAX: LOAD file-name
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_load( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_load( l )
- struct bwb_line *l;
- #endif
- {
-
- /* clear current contents */
-
- bwb_new( l );
-
- /* call xload function to load program in memory */
-
- bwb_xload( l );
-
- return bwb_zline( l );
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_xload()
-
- DESCRIPTION: This C function loads a BASIC program
- into memory.
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_xload( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_xload( l )
- struct bwb_line *l;
- #endif
- {
- FILE *loadfile;
- struct exp_ese *e; /* JBV */
-
- /* Get an argument for filename */
-
- adv_ws( l->buffer, &( l->position ) );
- switch( l->buffer[ l->position ] )
- {
- case '\0':
- case '\n':
- case '\r':
- case ':':
- bwb_error( err_nofn ); /* Added by JBV (bug found by DD) */
-
- return bwb_zline( l );
- default:
- break;
- }
-
- /* Section added by JBV (bug found by DD) */
- e = bwb_exp( l->buffer, FALSE, &( l->position ) );
- if ( e->type != STRING )
- {
- #if PROG_ERRORS
- sprintf( bwb_ebuf, "in bwb_xload(): Missing filespec" );
- bwb_error( bwb_ebuf );
- #else
- bwb_error( err_syntax );
- #endif
-
- return bwb_zline( l );
- }
-
- /* This line removed by JBV (no longer required) */
- /* bwb_const( l->buffer, CURTASK progfile, &( l->position ) ); */
- str_btoc( CURTASK progfile, exp_getsval( e ) ); /* JBV */
-
- if ( ( loadfile = fopen( CURTASK progfile, "r" )) == NULL )
- {
- sprintf( bwb_ebuf, err_openfile, CURTASK progfile );
- bwb_error( bwb_ebuf );
-
- return bwb_zline( l );
- }
-
- bwb_fload( loadfile );
-
-
- return bwb_zline( l );
- }
-
- /***************************************************************
-
- FUNCTION: bwb_save()
-
- DESCRIPTION: This C function implements the BASIC
- SAVE command.
-
- SYNTAX: SAVE file-name
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_save( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_save( l )
- struct bwb_line *l;
- #endif
- {
- FILE *outfile;
- static char filename[ MAXARGSIZE ];
- struct exp_ese *e; /* JBV */
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_save(): entered function." );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* Get an argument for filename */
-
- adv_ws( l->buffer, &( l->position ) );
- switch( l->buffer[ l->position ] )
- {
- case '\0':
- case '\n':
- case '\r':
- case ':':
- bwb_error( err_nofn );
-
- return bwb_zline( l );
- default:
- break;
- }
-
- /* Section added by JBV (bug found by DD) */
- e = bwb_exp( l->buffer, FALSE, &( l->position ) );
- if ( e->type != STRING )
- {
- #if PROG_ERRORS
- sprintf( bwb_ebuf, "in bwb_save(): Missing filespec" );
- bwb_error( bwb_ebuf );
- #else
- bwb_error( err_syntax );
- #endif
-
- return bwb_zline( l );
- }
-
- /* This line removed by JBV (no longer required) */
- /* bwb_const( l->buffer, filename, &( l->position ) ); */
- str_btoc( filename, exp_getsval( e ) ); /* JBV */
-
- if ( ( outfile = fopen( filename, "w" )) == NULL )
- {
- sprintf( bwb_ebuf, err_openfile, filename );
- bwb_error( bwb_ebuf );
-
- return bwb_zline( l );
- }
-
- bwb_xlist( l, outfile );
- fclose( outfile );
-
-
- return bwb_zline( l );
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_list()
-
- DESCRIPTION: This C function implements the BASIC
- LIST command.
-
- SYNTAX: LIST line[-line]
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_list( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_list( l )
- struct bwb_line *l;
- #endif
- {
- bwb_xlist( l, stdout );
-
- return bwb_zline( l );
- }
-
- /***************************************************************
-
- FUNCTION: bwb_xlist()
-
- DESCRIPTION: This C function lists the program in
- memory to a specified output device.
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_xlist( struct bwb_line *l, FILE *file )
- #else
- struct bwb_line *
- bwb_xlist( l, file )
- struct bwb_line *l;
- FILE *file;
- #endif
- {
- struct bwb_line *start, *end, *current;
- int s, e;
- int f, r;
-
- start = CURTASK bwb_start.next;
- end = &CURTASK bwb_end;
-
- r = bwb_numseq( &( l->buffer[ l->position ] ), &s, &e );
-
- /* advance to the end of the segment */
-
- #if MULTISEG_LINES
- adv_eos( l->buffer, &( l->position ));
- #endif
-
- if (( r == FALSE ) || ( s == 0 ))
- {
- s = CURTASK bwb_start.next->number;
- }
-
- if ( e == 0 )
- {
- e = s;
- }
-
- if ( r == FALSE )
- {
- for ( current = CURTASK bwb_start.next; current != &CURTASK bwb_end; current = current->next )
- {
- if ( current->next == &CURTASK bwb_end )
- {
- e = current->number;
- }
- }
- }
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_xlist(): LBUFFER sequence is %d-%d", s, e );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* abort if either number == (MAXLINENO + 1) which denotes CURTASK bwb_end */
-
- if ( ( s == (MAXLINENO + 1)) || ( e == (MAXLINENO + 1 ) ) )
- {
-
- return bwb_zline( l );
- }
-
- /* Now try to find the actual lines in memory */
-
- f = FALSE;
-
- for ( current = CURTASK bwb_start.next; current != &CURTASK bwb_end; current = current->next )
- {
- if ( current != l )
- {
- if (( current->number == s ) && ( f == FALSE ))
- {
- f = TRUE;
- start = current;
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_xlist(): start line number is <%d>",
- s );
- bwb_debug( bwb_ebuf );
- #endif
-
- }
- }
- }
-
- /* check and see if a line number was found */
-
- if ( f == FALSE )
- {
- sprintf( bwb_ebuf, err_lnnotfound, s );
- bwb_error( bwb_ebuf );
-
- return bwb_zline( l );
- }
-
- if ( e >= s )
- {
- for ( current = CURTASK bwb_start.next; current != &CURTASK bwb_end; current = current->next )
- {
- if ( current != l )
- {
- if ( current->number == e )
- {
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_xlist(): end line number is <%d>",
- current->next->number );
- bwb_debug( bwb_ebuf );
- #endif
-
- end = current->next;
- }
- }
- }
- }
- else
- {
- end = start;
- }
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_xlist(): line sequence is <%d-%d>",
- start->number, end->number );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* previous should now be set to the line previous to the
- first in the omission list */
-
- /* now go through and list appropriate lines */
-
- if ( start == end )
- {
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_xlist(): start == end" );
- bwb_debug( bwb_ebuf );
- #endif
- xl_line( file, start );
- }
- else
- {
- for ( current = start; current != end; current = current->next )
- {
- xl_line( file, current );
- }
- }
-
- return bwb_zline( l );
-
- }
-
- /***************************************************************
-
- FUNCTION: xl_line()
-
- DESCRIPTION: This function lists a single program
- line to a specified device of file.
- It is called by bwb_xlist();
-
- ***************************************************************/
-
- #if ANSI_C
- static int
- xl_line( FILE *file, struct bwb_line *l )
- #else
- static int
- xl_line( file, l )
- FILE *file;
- struct bwb_line *l;
- #endif
- {
- char tbuf[ MAXSTRINGSIZE + 1 ];
-
- if (( file == stdout ) || ( file == stderr ))
- {
-
- if ( l->xnum == (char) TRUE ) /* Better recast this one (JBV) */
- {
- sprintf( tbuf, "%7d: %s\n", l->number, l->buffer );
- }
- else
- {
- sprintf( tbuf, " : %s\n", l->buffer );
- }
-
- prn_xprintf( file, tbuf );
- }
- else
- {
-
- if ( l->xnum == (char) TRUE ) /* Better recast this one (JBV) */
- {
- fprintf( file, "%d %s\n", l->number, l->buffer );
- }
- else
- {
- fprintf( file, "%s\n", l->buffer );
- }
-
- }
-
- return TRUE;
- }
-
- /***************************************************************
-
- FUNCTION: bwb_delete()
-
- DESCRIPTION: This C function implements the BASIC
- DELETE command for interactive programming,
- deleting a specified program line (or lines)
- from memory.
-
- SYNTAX: DELETE line[-line]
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_delete( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_delete( l )
- struct bwb_line *l;
- #endif
- {
- struct bwb_line *start, *end, *current, *previous, *p, *next;
- static int s, e;
- int f;
-
- previous = &CURTASK bwb_start;
- start = CURTASK bwb_start.next;
- end = &CURTASK bwb_end;
-
- bwb_numseq( &( l->buffer[ l->position ] ), &s, &e );
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_delete(): LBUFFER sequence is %d-%d", s, e );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* advance to the end of the segment */
-
- #if MULTISEG_LINES
- adv_eos( l->buffer, &( l->position ));
- #endif
-
- /* Now try to find the actual lines in memory */
-
- previous = p = &CURTASK bwb_start;
- f = FALSE;
-
- for ( current = CURTASK bwb_start.next; current != &CURTASK bwb_end; current = current->next )
- {
- if ( current != l )
- {
- /* Following line revised by JBV */
- if (( current->xnum == (char) TRUE ) && ( current->number == s ))
- {
- f = TRUE;
- previous = p;
- start = current;
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_delete(): start line number is <%d>",
- s );
- bwb_debug( bwb_ebuf );
- #endif
-
- }
- }
- p = current;
- }
-
- /* check and see if a line number was found */
-
- if ( f == FALSE )
- {
- sprintf( bwb_ebuf, err_lnnotfound, s );
- bwb_error( bwb_ebuf );
-
- return bwb_zline( l );
- }
-
- if ( e > s )
- {
- for ( current = CURTASK bwb_start.next; current != &CURTASK bwb_end; current = current->next )
- {
- if ( current != l )
- {
- /* Following line revised by JBV */
- if (( current->xnum == (char) TRUE) && ( current->number == e ))
- {
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_delete(): end line number is <%d>",
- e );
- bwb_debug( bwb_ebuf );
- #endif
-
- end = current->next;
- }
- }
- }
- }
- else
- {
- end = start->next;
- }
-
- /* previous should now be set to the line previous to the
- first in the omission list */
-
- /* now go through and delete appropriate lines */
-
- current = start;
- while (( current != end ) && ( current != &CURTASK bwb_end ))
- {
- next = current->next;
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_delete(): deleting line %d",
- current->number );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* free line memory */
-
- bwb_freeline( current );
-
- /* recycle */
-
- current = next;
- }
-
- /* reset link */
-
- previous->next = current;
-
-
- return bwb_zline( l );
- }
-
- /***************************************************************
-
- FUNCTION: bwb_donum()
-
- DESCRIPTION: This function implements the BASIC DO
- NUM command, numbering all program lines
- in memory in increments of 10 beginning
- at 10.
-
- SYNTAX: DO NUM
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_donum( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_donum( l )
- struct bwb_line *l;
- #endif
- {
- struct bwb_line *current;
- register int lnumber;
-
- lnumber = 10;
- for ( current = bwb_start.next; current != &bwb_end; current = current->next )
- {
- current->number = lnumber;
- current->xnum = TRUE;
-
- lnumber += 10;
- if ( lnumber >= MAXLINENO )
- {
- return bwb_zline( l );
- }
- }
-
- return bwb_zline( l );
- }
-
- /***************************************************************
-
- FUNCTION: bwb_dounnum()
-
- DESCRIPTION: This function implements the BASIC DO
- UNNUM command, removing all line numbers
- from the program in memory.
-
- SYNTAX: DO UNNUM
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_dounnum( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_dounnum( l )
- struct bwb_line *l;
- #endif
- {
- struct bwb_line *current;
-
- for ( current = bwb_start.next; current != &bwb_end; current = current->next )
- {
- current->number = 0;
- current->xnum = FALSE;
- }
-
- return bwb_zline( l );
- }
-
- #endif /* INTERACTIVE */
-
- #if COMMON_CMDS
-
- /***************************************************************
-
- FUNCTION: bwb_chain()
-
- DESCRIPTION: This C function implements the BASIC
- CHAIN command.
-
- SYNTAX: CHAIN file-name
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_chain( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_chain( l )
- struct bwb_line *l;
- #endif
- {
-
- /* deallocate all variables except common ones */
-
- var_delcvars();
-
- /* remove old program from memory */
-
- bwb_xnew( l );
-
- /* call xload function to load new program in memory */
-
- bwb_xload( l );
-
- /* reset all stack counters */
-
- CURTASK exsc = -1;
- CURTASK expsc = 0;
- CURTASK xtxtsc = 0;
-
- /* run the program */
-
- return bwb_run( &CURTASK bwb_start );
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_merge()
-
- DESCRIPTION: This C function implements the BASIC
- MERGE command, merging command lines from
- a specified file into the program in memory
- without deleting the lines already in memory.
-
- SYNTAX: MERGE file-name
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_merge( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_merge( l )
- struct bwb_line *l;
- #endif
- {
-
- /* call xload function to merge program in memory */
-
- bwb_xload( l );
-
-
- return bwb_zline( l );
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_onerror()
-
- DESCRIPTION: This C function implements the BASIC
- ON ERROR GOSUB command.
-
- SYNTAX: ON ERROR GOSUB line | label
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_onerror( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_onerror( l )
- struct bwb_line *l;
- #endif
- {
- char tbuf[ MAXSTRINGSIZE + 1 ];
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_onerror(): entered function" );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* get the GOSUB STATEMENT */
-
- adv_element( l->buffer, &( l->position ), tbuf );
-
- /* check for GOSUB statement */
-
- bwb_strtoupper( tbuf );
- if ( strcmp( tbuf, CMD_GOSUB ) != 0 )
- {
- #if PROG_ERRORS
- sprintf( bwb_ebuf, "in bwb_onerror(): GOSUB statement missing" );
- bwb_error( bwb_ebuf );
- #else
- bwb_error( err_syntax );
- #endif
-
- return bwb_zline( l );
- }
-
- /* get the GOSUB line */
-
- adv_element( l->buffer, &( l->position ), err_gosubl );
-
- return bwb_zline( l );
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_tron()
-
- DESCRIPTION: This function implements the BASIC TRON
- command, turning the trace mechanism on.
-
- SYNTAX: TRON
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_tron( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_tron( l )
- struct bwb_line *l;
- #endif
- {
- bwb_trace = TRUE;
- prn_xprintf( stdout, "Trace is ON\n" );
-
- return bwb_zline( l );
- }
-
- /***************************************************************
-
- FUNCTION: bwb_troff()
-
- DESCRIPTION: This function implements the BASIC TROFF
- command, turning the trace mechanism off.
-
- SYNTAX: TROFF
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_troff( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_troff( l )
- struct bwb_line *l;
- #endif
- {
- bwb_trace = FALSE;
- prn_xprintf( stdout, "Trace is OFF\n" );
-
- return bwb_zline( l );
- }
-
- #endif /* COMMON_CMDS */
-
- /***************************************************************
-
- FUNCTION: bwb_randomize()
-
- DESCRIPTION: This function implements the BASIC
- RANDOMIZE command, seeding the pseudo-
- random number generator.
-
- SYNTAX: RANDOMIZE number
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_randomize( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_randomize( l )
- struct bwb_line *l;
- #endif
- {
- register unsigned n;
- struct exp_ese *e;
-
- /* Check for argument */
-
- adv_ws( l->buffer, &( l->position ) );
- switch( l->buffer[ l->position ] )
- {
- case '\0':
- case '\n':
- case '\r':
- #if MULTISEG_LINES
- case ':':
- #endif
- n = (unsigned) 1;
- break;
- default:
- n = (unsigned) 0;
- break;
- }
-
- /* get the argument in tbuf */
-
- if ( n == (unsigned) 0 )
- {
- e = bwb_exp( l->buffer, FALSE, &( l->position ) );
- n = (unsigned) exp_getnval( e );
- }
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_randomize(): argument is <%d>", n );
- bwb_debug( bwb_ebuf );
- #endif
-
- srand( n );
-
- return bwb_zline( l );
- }
-
-
- /***************************************************************
-
- FUNCTION: bwb_xnew()
-
- DESCRIPTION: Clears the program in memory, but does not
- deallocate all variables.
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_xnew( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_xnew( l )
- struct bwb_line *l;
- #endif
- {
- struct bwb_line *current, *previous;
- int wait;
-
- wait = TRUE;
- for ( current = CURTASK bwb_start.next; current != &CURTASK bwb_end; current = current->next )
- {
- if ( wait != TRUE )
- {
- /* Revised to FREE pass-thru call by JBV */
- FREE( previous, "bwb_xnew" );
- previous = NULL; /* JBV */
- }
- wait = FALSE;
- previous = current;
- }
-
- CURTASK bwb_start.next = &CURTASK bwb_end;
-
- return bwb_zline( l );
- }
-
- #if UNIX_CMDS
-
- /***************************************************************
-
- FUNCTION: bwb_environ()
-
- DESCRIPTION: This C function implements the BASIC
- ENVIRON command, assigning a string
- value to an environment variable.
-
- SYNTAX: ENVIRON variable-string$ = string$
-
- ***************************************************************/
-
- #if ANSI_C
- struct bwb_line *
- bwb_environ( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_environ( l )
- struct bwb_line *l;
- #endif
- {
- static char tbuf[ MAXSTRINGSIZE + 1 ];
- char tmp[ MAXSTRINGSIZE + 1 ];
- register int i;
- int pos;
- struct exp_ese *e;
-
- /* find the equals sign */
-
- for ( i = 0; ( l->buffer[ l->position ] != '=' ) && ( l->buffer[ l->position ] != '\0' ); ++i )
- {
- tbuf[ i ] = l->buffer[ l->position ];
- tbuf[ i + 1 ] = '\0';
- ++( l->position );
- }
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_environ(): variable string is <%s>", tbuf );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* get the value string to be assigned */
-
- pos = 0;
- e = bwb_exp( tbuf, FALSE, &pos );
- str_btoc( tbuf, exp_getsval( e ) );
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_environ(): variable string resolves to <%s>", tbuf );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* find the equals sign */
-
- adv_ws( l->buffer, &( l->position ) );
- if ( l->buffer[ l->position ] != '=' )
- {
- #if PROG_ERRORS
- sprintf( bwb_ebuf, "in bwb_environ(): failed to find equal sign" );
- bwb_error( bwb_ebuf );
- #else
- bwb_error( err_syntax );
- #endif
-
- return bwb_zline( l );
- }
- ++( l->position );
-
- /* get the value string to be assigned */
-
- e = bwb_exp( l->buffer, FALSE, &( l->position ));
- str_btoc( tmp, exp_getsval( e ) );
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_environ(): value string resolves to <%s>", tmp );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* construct string */
-
- strcat( tbuf, "=" );
- strcat( tbuf, tmp );
-
- #if INTENSIVE_DEBUG
- sprintf( bwb_ebuf, "in bwb_environ(): assignment string is <%s>", tbuf );
- bwb_debug( bwb_ebuf );
- #endif
-
- /* now assign value to variable */
-
- if ( putenv( tbuf ) == -1 )
- {
- bwb_error( err_opsys );
-
- return bwb_zline( l );
- }
-
- /* return */
-
-
- return bwb_zline( l );
-
- }
-
- #endif /* UNIX_CMDS */
-
- /***************************************************************
-
- FUNCTION: bwb_cmds()
-
- DESCRIPTION: This function implements a CMD command,
- which lists all commands implemented.
- It is not part of a BASIC specification,
- but is used for debugging bwBASIC.
-
- SYNTAX: CMDS
-
- ***************************************************************/
-
- #if PERMANENT_DEBUG
-
- #if ANSI_C
- struct bwb_line *
- bwb_cmds( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_cmds( l )
- struct bwb_line *l;
- #endif
- {
- register int n;
- char tbuf[ MAXSTRINGSIZE + 1 ];
-
- prn_xprintf( stdout, "BWBASIC COMMANDS AVAILABLE: \n" );
-
- /* run through the command table and print comand names */
-
- for ( n = 0; n < COMMANDS; ++n )
- {
- sprintf( tbuf, "%s \n", bwb_cmdtable[ n ].name );
- prn_xprintf( stdout, tbuf );
- }
-
- return bwb_zline( l );
- }
- #endif
-
- /***************************************************************
-
- FUNCTION: getcmdnum()
-
- DESCRIPTION: This function returns the number associated
- with a specified command (cmdstr) in the
- command table.
-
- ***************************************************************/
-
- #if ANSI_C
- int
- getcmdnum( char *cmdstr )
- #else
- int
- getcmdnum( cmdstr )
- char *cmdstr;
- #endif
- {
- register int c;
-
- for ( c = 0; c < COMMANDS; ++c )
- {
- if ( strcmp( bwb_cmdtable[ c ].name, cmdstr ) == 0 )
- {
- return c;
- }
- }
-
- return -1;
-
- }
-
- /***************************************************************
-
- FUNCTION: bwb_zline()
-
- DESCRIPTION: This function is called at the exit from
- Bywater BASIC command functions. If
- MULTISEG_LINES is TRUE, then it returns
- a pointer to the current line; otherwise it
- sets the position in the next line to zero
- and returns a pointer to the next line.
-
- ***************************************************************/
-
- #if ANSI_C
- extern struct bwb_line *
- bwb_zline( struct bwb_line *l )
- #else
- struct bwb_line *
- bwb_zline( l )
- struct bwb_line *l;
- #endif
- {
- #if MULTISEG_LINES
- /* l->marked = FALSE; */
- return l;
- #else
- l->next->position = 0;
- return l->next;
- #endif
- }
-
-
|