-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathbwbasic.c
More file actions
3639 lines (3361 loc) · 89.4 KB
/
bwbasic.c
File metadata and controls
3639 lines (3361 loc) · 89.4 KB
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
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/***************************************************************
bwbasic.c Main Program File
for Bywater BASIC Interpreter
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). */
/* */
/* Version 3.00 by Howard Wulf, AF5NE */
/* */
/* Version 3.10 by Howard Wulf, AF5NE */
/* */
/* Version 3.20 by Howard Wulf, AF5NE */
/* */
/*---------------------------------------------------------------*/
#include "bwbasic.h"
static void break_handler (void);
static void break_mes (int x);
static int bwb_init (void);
static void bwb_initialize_warnings (void);
static void bwb_interact (void);
static int bwb_ladd (char *buffer, LineType * p, int IsUser);
static void bwb_single_step (char *buffer);
static void bwb_xtxtline (char *buffer);
static int bwx_signon (void);
static void execute_profile (char *FileName);
static void execute_program (char *FileName);
static char *FindClassicStatementEnd (char *C);
static int FixQuotes (char *tbuf);
static void ImportClassicIfThenElse (char *InBuffer);
static int is_ln (char *buffer);
static int is_numconst (char *buffer);
static void mark_preset_variables (void);
static FILE *nice_open (char *BaseName);
static void process_basic_line (char *buffer);
GlobalType *My = NULL;
static char *Banner[] = {
"######## ## ## ## ## ### ######## ######## ######## ",
"## ## ## ## ## ## ## ## ## ## ## ## ## ",
"## ## #### ## ## ## ## ## ## ## ## ## ",
"######## ## ## ## ## ## ## ## ###### ######## ",
"## ## ## ## ## ## ######### ## ## ## ## ",
"## ## ## ## ## ## ## ## ## ## ## ## ",
"######## ## ### ### ## ## ## ######## ## ## ",
" ",
" ",
" ######## ### ###### #### ###### ",
" ## ## ## ## ## ## ## ## ##",
" ## ## ## ## ## ## ## ",
" ######## ## ## ###### ## ## ",
" ## ## ######### ## ## ## ",
" ## ## ## ## ## ## ## ## ##",
" ######## ## ## ###### #### ###### ",
" ",
"Bywater BASIC Interpreter, version 3.20 ",
"Copyright (c) 1993, Ted A. Campbell ",
"Copyright (c) 1995-1997, Jon B. Volkoff ",
"Copyright (c) 2014-2017, Howard Wulf, AF5NE ",
" ",
NULL
};
#define NUM_WARNINGS 80
static char *ERROR4[NUM_WARNINGS];
static void
bwb_initialize_warnings (void)
{
int i;
for (i = 0; i < NUM_WARNINGS; i++)
{
ERROR4[i] = NULL;
}
ERROR4[1] = "NEXT without FOR";
ERROR4[2] = "Syntax error";
ERROR4[3] = "RETURN without GOSUB";
ERROR4[4] = "Out of DATA";
ERROR4[5] = "Illegal function call";
ERROR4[6] = "Overflow";
ERROR4[7] = "Out of memory";
ERROR4[8] = "Undefined line";
ERROR4[9] = "Subscript out of range";
ERROR4[10] = "Redimensioned array";
ERROR4[11] = "Division by zero";
ERROR4[12] = "Illegal direct";
ERROR4[13] = "Type mismatch";
ERROR4[14] = "Out of string space";
ERROR4[15] = "String too long";
ERROR4[16] = "String formula too complex";
ERROR4[17] = "Can't continue";
ERROR4[18] = "Undefined user function";
ERROR4[19] = "No RESUME";
ERROR4[20] = "RESUME without error";
ERROR4[21] = "Unprintable error";
ERROR4[22] = "Missing operand";
ERROR4[23] = "Line buffer overflow";
ERROR4[26] = "FOR without NEXT";
ERROR4[27] = "Bad DATA";
ERROR4[29] = "WHILE without WEND";
ERROR4[30] = "WEND without WHILE";
ERROR4[31] = "EXIT FUNCTION without FUNCTION";
ERROR4[32] = "END FUNCTION without FUNCTION";
ERROR4[33] = "EXIT SUB without SUB";
ERROR4[34] = "END SUB without SUB";
ERROR4[35] = "EXIT FOR without FOR";
ERROR4[50] = "Field overflow";
ERROR4[51] = "Internal error";
ERROR4[52] = "Bad file number";
ERROR4[53] = "File not found";
ERROR4[54] = "Bad file mode";
ERROR4[55] = "File already open";
ERROR4[57] = "Disk I/O error";
ERROR4[58] = "File already exists";
ERROR4[61] = "Disk full";
ERROR4[62] = "Input past end";
ERROR4[63] = "Bad record number";
ERROR4[64] = "Bad file name";
ERROR4[66] = "Direct statement in file";
ERROR4[67] = "Too many files";
ERROR4[70] = "Variable Not Declared";
ERROR4[73] = "Advanced Feature";
}
/***************************************************************
FUNCTION: bwx_terminate()
DESCRIPTION: This function terminates program execution.
***************************************************************/
void
bwx_terminate (void)
{
exit (0);
}
/***************************************************************
FUNCTION: break_handler()
DESCRIPTION: This function is called by break_mes()
and handles program interruption by break
(or by the STOP command).
***************************************************************/
static void
break_handler (void)
{
/*
**
**
*/
assert( My != NULL );
My->AutomaticLineNumber = 0;
My->AutomaticLineIncrement = 0;
if (My->IsInteractive)
{
/* INTERACTIVE: terminate program */
/* reset all stack counters */
bwb_clrexec ();
SetOnError (0);
My->ERR = -1; /* in break_handler() */
/* reset the break handler */
signal (SIGINT, break_mes);
longjmp (My->mark, -1);
return;
}
/* NOT INTERACTIVE: terminate immediately */
bwx_terminate ();
}
/***************************************************************
FUNCTION: break_mes()
DESCRIPTION: This function is called (a) by a SIGINT
signal or (b) by bwb_STOP via bwx_STOP.
It prints an error message then calls
break_handler() to terminate the program.
***************************************************************/
static void
break_mes (int x /* Parameter 'x' is never used */ )
{
/*
**
** break_mes is FATAL.
** x == SIGINT for control-C
** x == 0 for bwx_STOP
**
*/
assert( My != NULL );
assert( My->SYSOUT != NULL );
assert( My->SYSOUT->cfp != NULL );
assert( My->CurrentVersion != NULL );
if (My->ERR < 0) /* in break_mes(), do not make a bad situation worse */
{
/* an error has already ben reported */
}
else
{
fputc ('\n', My->SYSOUT->cfp);
ResetConsoleColumn ();
if (My->CurrentVersion->OptionVersionValue & (C77))
{
if (is_empty_string (My->ProgramFilename) == FALSE)
{
fprintf (My->SYSOUT->cfp, "FILE:%s, ", My->ProgramFilename);
}
}
fprintf (My->SYSOUT->cfp, "Program interrupted");
if (My->ThisLine) /* break_mes */
{
if (My->ThisLine->LineFlags & (LINE_USER)) /* break_mes */
{
/* don't print the line number */
}
else
{
fprintf (My->SYSOUT->cfp, " at line %d", My->ThisLine->number); /* break_mes */
}
}
fputc ('\n', My->SYSOUT->cfp);
ResetConsoleColumn ();
fflush (My->SYSOUT->cfp);
}
break_handler ();
}
extern void
bwx_STOP (int IsShowMessage)
{
if (IsShowMessage)
{
break_mes (0);
}
else
{
break_handler ();
}
}
static int
bwx_signon (void)
{
/*
**
** Display a sign-on banner.
** NOT called if a file is provided on the command line.
**
*/
int i;
assert( My != NULL );
assert( My->SYSOUT != NULL );
assert( My->SYSOUT->cfp != NULL );
for (i = 0; Banner[i] != NULL; i++)
{
fprintf (My->SYSOUT->cfp, "%s\n", Banner[i]);
}
ResetConsoleColumn ();
return TRUE;
}
DoubleType
bwx_TIMER (DoubleType Seconds)
{
/*
**
** Return a number representing Seconds in the future. Seconds >= 0.
** Seconds may be non-integer, such as 0.001 or 86399.999. The
** maximum allowed Seconds is MAXDBL. This is used two ways:
**
** 1) in bwb_TIMER(), the ON TIMER count (>0) is used to determine
** the expiration time. In this case, simply return what the value
** will be in the future. Note that ON TIMER enforces
** Seconds > (1 / CLOCKS_PER_SEC), and
**
** 2) in bwb_execline(), zero (=0) is used to determine the current
** time and compare it to #1. In this case, simply return what the
** value is now.
**
** Both the resolution of the timer, and the frequency of update,
** are implementation defined.
**
*/
if (Seconds < 0)
{
WARN_INTERNAL_ERROR;
return 0;
}
else
{
DoubleType Result;
Result = clock ();
assert (CLOCKS_PER_SEC > 0);
Result /= CLOCKS_PER_SEC;
if (Seconds > 0)
{
Result += Seconds;
}
return Result;
}
}
void
CleanLine (char *buffer)
{
/*
**
** cleanup the line, so it is easier to parse
**
*/
char *newbuffer;
if (is_empty_string (buffer))
{
/* do nothing */
return;
}
/* remove CR/LF */
newbuffer = bwb_strchr (buffer, '\r');
if (newbuffer != NULL)
{
*newbuffer = NulChar;
}
newbuffer = bwb_strchr (buffer, '\n');
if (newbuffer != NULL)
{
*newbuffer = NulChar;
}
/* remove ALL embedded control characters */
/* if you want a control character, then use CHR$ */
newbuffer = buffer;
while (*newbuffer != NulChar)
{
if (bwb_isprint (*newbuffer))
{
/* OK */
}
else
{
*newbuffer = ' ';
}
newbuffer++;
}
/* LTRIM$ */
newbuffer = buffer;
if (*newbuffer != NulChar)
{
/* not an empty line, so remove one (or more) leading spaces */
while (*newbuffer == ' ')
{
newbuffer++;
}
if (newbuffer > buffer)
{
bwb_strcpy (buffer, newbuffer);
}
}
/* RTRIM$ */
newbuffer = buffer;
if (*newbuffer != NulChar)
{
/* not an empty line, so remove one (or more) trailing spaces */
char *E;
E = bwb_strchr (newbuffer, NulChar);
E--;
while (E >= newbuffer && *E == ' ')
{
*E = NulChar;
E--;
}
}
}
static int
bwb_init (void)
{
/*
**
** initialize Bywater BASIC
**
*/
int n;
static char start_buf[] = "";
static char end_buf[] = "";
/* Memory allocation */
if ((My = (GlobalType *) calloc (1, sizeof (GlobalType))) == NULL)
{
return FALSE;
}
if ((My->MaxLenBuffer =
(char *) calloc (MAXLEN + 1 /* NulChar */ , sizeof (char))) == NULL)
{
return FALSE;
}
if ((My->NumLenBuffer =
(char *) calloc (NUMLEN + 1 /* NulChar */ , sizeof (char))) == NULL)
{
return FALSE;
}
if ((My->ConsoleOutput =
(char *) calloc (MAX_LINE_LENGTH + 1 /* NulChar */ ,
sizeof (char))) == NULL)
{
return FALSE;
}
if ((My->ConsoleInput =
(char *) calloc (MAX_LINE_LENGTH + 1 /* NulChar */ ,
sizeof (char))) == NULL)
{
return FALSE;
}
if ((My->SYSIN = (FileType *) calloc (1, sizeof (FileType))) == NULL)
{
return FALSE;
}
if ((My->SYSOUT = (FileType *) calloc (1, sizeof (FileType))) == NULL)
{
return FALSE;
}
if ((My->SYSPRN = (FileType *) calloc (1, sizeof (FileType))) == NULL)
{
return FALSE;
}
if ((My->StartMarker = (LineType *) calloc (1, sizeof (LineType))) == NULL)
{
return FALSE;
}
if ((My->UserMarker = (LineType *) calloc (1, sizeof (LineType))) == NULL)
{
return FALSE;
}
if ((My->EndMarker = (LineType *) calloc (1, sizeof (LineType))) == NULL)
{
return FALSE;
}
if ((My->EndMarker = (LineType *) calloc (1, sizeof (LineType))) == NULL)
{
return FALSE;
}
if ((My->ERROR4 =
(char *) calloc (MAX_ERR_LENGTH + 1 /* NulChar */ ,
sizeof (char))) == NULL)
{
return FALSE;
}
My->CurrentVersion = &bwb_vertable[0];
My->IsInteractive = TRUE;
My->OptionSleepDouble = 1;
My->OptionIndentInteger = 2;
My->OptionTerminalType = C_OPTION_TERMINAL_NONE;
My->OptionRoundType = C_OPTION_ROUND_BANK;
My->NextValidLineNumber = MINLIN;
My->IncludeLevel = 0; /* %INCLUDE */
My->IsCommandLineFile = FALSE;
My->ExternalInputFile = NULL; /* for automated testing, --TAPE command line parameter */
My->IsPrinter = FALSE; /* CBASIC-II: CONSOLE and LPRINTER commands */
My->OptionPromptString = DEF_PROMPT;
My->OptionEditString = DEF_EDITOR;
My->OptionFilesString = DEF_FILES;
My->OptionRenumString = DEF_RENUM;
My->OptionExtensionString = DEF_EXTENSION;
My->OptionScaleInteger = 0;
My->OptionDigitsInteger = SIGNIFICANT_DIGITS;
My->OptionZoneInteger = ZONE_WIDTH;
My->UseParameterString = NULL;
My->ProgramFilename = NULL;
My->StartMarker->number = MINLIN - 1;
My->StartMarker->next = My->EndMarker;
My->StartMarker->position = 0;
My->StartMarker->buffer = start_buf;
My->EndMarker->number = MAXLIN + 1;
My->EndMarker->next = My->EndMarker;
My->EndMarker->position = 0;
My->EndMarker->buffer = end_buf;
My->UserMarker->number = MINLIN - 1;
My->UserMarker->next = My->EndMarker;
My->UserMarker->position = 0;
My->UserMarker->buffer = NULL;
My->DataLine = My->EndMarker;
My->DataPosition = 0;
My->StackHead = NULL;
My->StackDepthInteger = 0;
My->FieldHead = NULL;
My->VirtualHead = NULL;
My->FileHead = NULL;
My->ThisLine = My->StartMarker; /* bwb_init */
My->SYSIN->DevMode = DEVMODE_INPUT;
My->SYSIN->width = 80;
My->SYSIN->col = 1;
My->SYSIN->row = 1;
My->SYSIN->delimit = ',';
My->SYSIN->cfp = stdin;
My->SYSOUT->DevMode = DEVMODE_OUTPUT;
My->SYSOUT->width = 80;
My->SYSOUT->col = 1;
My->SYSOUT->row = 1;
My->SYSOUT->delimit = ',';
My->SYSOUT->cfp = stdout;
My->SYSPRN->DevMode = DEVMODE_OUTPUT;
My->SYSPRN->width = 80;
My->SYSPRN->col = 1;
My->SYSPRN->row = 1;
My->SYSPRN->delimit = ',';
My->SYSPRN->cfp = stderr;
My->LPRINT_NULLS = 0;
My->SCREEN_ROWS = 24;
/* OPEN #0 is an ERROR. */
/* CLOSE #0 is an ERROR. */
/* WIDTH #0, 80 is the same as WIDTH 80. */
/* LPRINT and LLIST are sent to bwx_LPRINT() */
/* default variable type */
for (n = 0; n < 26; n++)
{
My->DefaultVariableType[n] = DoubleTypeCode;
}
/* default COMMAND$(0-9) */
for (n = 0; n < 10; n++)
{
My->COMMAND4[n] = NULL;
}
/* initialize tables of variables, functions */
bwb_initialize_warnings ();
SortAllCommands ();
SortAllFunctions ();
SortAllOperators ();
var_init ();
IntrinsicFunction_init ();
UserFunction_init ();
OptionVersionSet (0);
/* OK */
return TRUE;
}
/***************************************************************
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 void
process_basic_line (char *buffer)
{
CleanLine (buffer);
if (is_empty_string (buffer))
{
/* empty -- do nothing */
}
else if (is_ln (buffer) == FALSE)
{
/* If there is no line number, then execute the line as received */
/* RUN */
WARN_CLEAR; /* process_basic_line */
SetOnError (0);
bwb_xtxtline (buffer); /* process_basic_line */
}
else if (is_numconst (buffer) == TRUE)
{
/*-----------------------------------------------------------------*/
/* Another possibility: if buffer is a numeric constant, */
/* then delete the indicated line number (JBV) */
/*-----------------------------------------------------------------*/
/* 100 */
int LineNumber;
LineNumber = atoi (buffer);
WARN_CLEAR; /* process_basic_line */
SetOnError (0);
sprintf (buffer, "delete %d", LineNumber);
bwb_xtxtline (buffer); /* process_basic_line */
}
else
{
/* If there is a line number, then add it to the BASIC program */
/* 100 REM */
bwb_ladd (buffer, My->StartMarker, FALSE);
}
}
static void
bwb_single_step (char *buffer)
{
assert( My != NULL );
assert (buffer != NULL);
process_basic_line (buffer);
while (My->StackHead != NULL)
{
bwb_execline ();
}
}
static void
mark_preset_variables (void)
{
/* mark all existing variables as preset */
/* this includes all variables declared in any PROFILE */
VariableType *v;
assert( My != NULL );
for (v = My->VariableHead; v != NULL; v = v->next)
{
v->VariableFlags |= VARIABLE_PRESET;
v->VariableFlags |= VARIABLE_COMMON;
}
}
static void
execute_profile (char *FileName)
{
FILE *profile;
assert( My != NULL );
assert (FileName != NULL);
My->NextValidLineNumber = MINLIN;
profile = fopen (FileName, "r");
if (profile == NULL)
{
/* NOT FOUND */
/* OPTIONAL */
return;
}
/* FOUND */
if (My->IsInteractive)
{
/*
**
** set a buffer for jump: program execution returns to this point in
** case of a jump (error, interrupt, or finish program)
**
*/
My->program_run = 0;
signal (SIGINT, break_mes);
setjmp (My->mark);
if (My->program_run > 0)
{
/* error in PROFILE */
exit (1);
}
My->program_run++;
}
/*
The profile only exists to allow executing OPTION ... commands. No other use is supported.
*/
{
char *tbuf;
int tlen;
tbuf = My->ConsoleInput;
tlen = MAX_LINE_LENGTH;
while (fgets (tbuf, tlen, profile)) /* execute_profile */
{
tbuf[tlen] = NulChar;
bwb_single_step (tbuf); /* in execute_profile() */
}
bwb_fclose (profile);
mark_preset_variables ();
}
}
static void
execute_program (char *FileName)
{
assert( My != NULL );
assert( My->SYSOUT != NULL );
assert( My->SYSOUT->cfp != NULL );
assert (FileName != NULL);
My->NextValidLineNumber = MINLIN;
My->IsCommandLineFile = TRUE;
if (bwb_fload (FileName) == FALSE)
{
fprintf (My->SYSOUT->cfp, "Failed to open file %s\n", FileName);
/* the file load has failed, so do NOT run the program */
exit (1);
}
if (My->ERR < 0 /* in execute_program(), file load failed */ )
{
/* the file load has failed, so do NOT run the program */
exit (1);
}
bwb_RUN (My->StartMarker);
}
extern int
main (int argc, char **argv)
{
int i;
assert (argc >= 0);
assert (argv != NULL);
if (bwb_init () == FALSE)
{
/* FATAL */
puts ("Out of memory");
return 1;
}
assert( My != NULL );
assert( My->SYSOUT != NULL );
assert( My->SYSOUT->cfp != NULL );
assert( My->SYSIN != NULL );
assert( My->SYSIN->cfp != NULL );
/* Signon message banner */
if (argc < 2)
{
/* no parameters */
if (My->IsInteractive)
{
bwx_signon ();
}
else
{
/* if INTERACTIVE is FALSE, then we must have a program file */
fputs ("Program file not specified\n", My->SYSOUT->cfp);
return 1;
}
}
if (My->IsInteractive)
{
/*
**
** set a buffer for jump: program execution returns to this point in
** case of a jump (error, interrupt, or finish program)
**
*/
My->program_run = 0;
signal (SIGINT, break_mes);
setjmp (My->mark);
if (My->program_run > 0)
{
/* error in profile */
return 1;
}
My->program_run++;
}
#if PROFILE
execute_profile (PROFILENAME);
#endif
/* check to see if there is a program file: but do this only the first time around! */
for (i = 1; i < argc; i++)
{
/*
SYNTAX: bwbasic [ --profile profile.bas ] [ --tape tapefile.inp ] [ program.bas ]
*/
if (bwb_stricmp (argv[i], "--profile") == 0
|| bwb_stricmp (argv[i], "-p") == 0
|| bwb_stricmp (argv[i], "/profile") == 0
|| bwb_stricmp (argv[i], "/p") == 0)
{
i++;
if (i < argc)
{
/* --profile profile.bas */
execute_profile (argv[i]);
}
}
else
if (bwb_stricmp (argv[i], "--tape") == 0
|| bwb_stricmp (argv[i], "-t") == 0
|| bwb_stricmp (argv[i], "/tape") == 0
|| bwb_stricmp (argv[i], "/t") == 0)
{
i++;
if (i < argc)
{
/* --tape tapefile.inp */
My->ExternalInputFile = fopen (argv[i], "r");
}
}
else
{
/* program.bas */
{
int j;
int n;
j = i;
for (n = 0; n < 10 && j < argc; n++, j++)
{
My->COMMAND4[n] = argv[j];
}
}
execute_program (argv[i]);
break;
}
}
if (My->IsInteractive)
{
/*
**
** set a buffer for jump: program execution returns to this point in
** case of a jump (error, interrupt, or finish program)
**
*/
My->program_run = 0;
signal (SIGINT, break_mes);
setjmp (My->mark);
if (My->program_run > 0)
{
/* error in console mode */
}
My->program_run++;
}
/* main program loop */
My->NextValidLineNumber = MINLIN;
while (!feof (My->SYSIN->cfp)) /* condition !feof( My->SYSIN->cfp ) added in v1.11 */
{
bwb_mainloop ();
}
bwx_terminate (); /* allow ^D (Unix) exit with grace */
return 0;
}
/***************************************************************
FUNCTION: bwb_interact()
DESCRIPTION: This function gets a line from the user
and processes it.
***************************************************************/
static void
bwb_interact (void)
{
char *tbuf;
int tlen;
assert( My != NULL );
tbuf = My->ConsoleInput;
tlen = MAX_LINE_LENGTH;
My->NextValidLineNumber = MINLIN;
/* take input from keyboard */
if (My->AutomaticLineNumber > 0 && My->AutomaticLineIncrement > 0)
{
/* AUTO 100, 10 */
char *zbuf; /* end of the prompt, start of the response */
int zlen; /* length of the prompt */
char LineExists;
LineType *l;
LineExists = ' ';
for (l = My->StartMarker->next; l != My->EndMarker; l = l->next)
{
if (l->number == My->AutomaticLineNumber)
{
/* FOUND */
LineExists = '*';
break;
}
else if (l->number > My->AutomaticLineNumber)
{
/* NOT FOUND */
LineExists = ' ';
break;
}
}
sprintf (tbuf, "%d%c", My->AutomaticLineNumber, LineExists);
zbuf = bwb_strchr (tbuf, NulChar);
zlen = bwb_strlen (tbuf);
bwx_input (tbuf, FALSE, zbuf, tlen - zlen);
zbuf[-1] = ' '; /* remove LineExists indicator */
CleanLine (zbuf); /* JBV */
if (is_empty_string (zbuf))
{
/* empty response */
if (LineExists == '*')
{
/*
An empty response with an existing line,
causes AUTO to continue with the next line,
leaving the current line intact.
*/
My->AutomaticLineNumber += My->AutomaticLineIncrement;
}
else
{
/*
An empty response with a non-existing line,
causes AUTO to terminate.
*/
My->AutomaticLineNumber = 0;
My->AutomaticLineIncrement = 0;
}
}
else
{
/* non-empty response */
if (bwb_stricmp (zbuf, "MAN") == 0)
{
/* MAN terminates AUTO mode */
My->AutomaticLineNumber = 0;
My->AutomaticLineIncrement = 0;
}
else
{
/* overwrite any existing line */
bwb_ladd (tbuf, My->StartMarker, FALSE);
My->AutomaticLineNumber += My->AutomaticLineIncrement;
}
}