-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcommands.c
1561 lines (1499 loc) · 64.4 KB
/
commands.c
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
/*
** This file is part of the Matrix Brandy Basic VI Interpreter.
** Copyright (C) 2000-2014 David Daniels
** Copyright (C) 2018-2024 Michael McConnell and contributors
**
** Brandy is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2, or (at your option)
** any later version.
**
** Brandy is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Brandy; see the file COPYING. If not, write to
** the Free Software Foundation, 59 Temple Place - Suite 330,
** Boston, MA 02111-1307, USA.
**
**
** This file contains all the 'immediate' Basic commands
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include "common.h"
#include "target.h"
#include "basicdefs.h"
#include "miscprocs.h"
#include "tokens.h"
#include "statement.h"
#include "variables.h"
#include "editor.h"
#include "errors.h"
#include "heap.h"
#include "stack.h"
#include "strings.h"
#include "evaluate.h"
#include "screen.h"
#include "keyboard.h"
#ifdef TARGET_RISCOS
#include "kernel.h"
#include "swis.h"
#endif
#define PAGESIZE 20 /* Number of lines listed before pausing */
static int32 editnameLen = 80;
static char editname[80]; /* Default Name of editor invoked by 'EDIT' command */
#ifndef NOINLINEHELP
static void detailed_help(char *);
#endif
/*
** 'get_number' is called to evaluate an expression that returns an
** integer value. It is used by the functions handling the various
** Basic commands in this file
*/
static int64 get_number(void) {
DEBUGFUNCMSGIN;
factor();
DEBUGFUNCMSGOUT;
return pop_anynum64();
}
/*
** 'get_pair' is called to return a pair of values. It assumes that
** 'basicvars.current' points at the first item after the token for the
** command for which it is being used
*/
static void get_pair(size_t *first, size_t *second, size_t firstdef, size_t secondef) {
size_t low, high = 0;
DEBUGFUNCMSGIN;
*first = firstdef;
*second = secondef;
if (isateol(basicvars.current)) { /* Return if there is nothing to do */
DEBUGFUNCMSGOUT;
return;
}
if (*basicvars.current == ',' || *basicvars.current == '-')
low = firstdef;
else {
low = get_number();
}
if (isateol(basicvars.current))
high = low;
else if (*basicvars.current == ',' || *basicvars.current == '-') {
basicvars.current++;
if (isateol(basicvars.current))
high = secondef;
else {
high = get_number();
check_ateol();
}
}
else {
error(ERR_SYNTAX);
return;
}
*first = low;
*second = high;
DEBUGFUNCMSGOUT;
}
/*
** 'get_name' evaluates an expression that returns a string (normally
** a file name). It returns a pointer to that string as a null-terminated
** C string. If this string has to be kept, a copy of has to be made
*/
static char *get_name(void) {
stackitem topitem;
basicstring descriptor;
char *cp;
DEBUGFUNCMSGIN;
expression();
topitem = GET_TOPITEM;
if (topitem != STACK_STRING && topitem != STACK_STRTEMP) {
DEBUGFUNCMSGOUT;
error(ERR_TYPESTR);
return NULL;
}
descriptor = pop_string();
cp = tocstring(descriptor.stringaddr, descriptor.stringlen);
if (topitem == STACK_STRTEMP) free_string(descriptor);
DEBUGFUNCMSGOUT;
return cp;
}
/*
** 'exec_new' clears away the program currently in memory. It can also be
** used to alter the amount of memory used by the interpreter in which
** to store and run programs
*/
static void exec_new(void) {
DEBUGFUNCMSGIN;
if (basicvars.runflags.running) { /* Cannot edit a running program */
DEBUGFUNCMSGOUT;
error(ERR_COMMAND);
return;
}
basicvars.current++;
if (!isateol(basicvars.current)) { /* New workspace size supplied */
int32 oldsize, newsize;
boolean ok;
newsize = get_number();
check_ateol();
oldsize = basicvars.worksize;
release_workspace(); /* Discard horrible, rusty old Basic workspace */
ok = init_workspace(ALIGN(newsize)); /* Obtain nice, shiny new one */
if (!ok) { /* Allocation failed - Should still be a block of the old size available */
(void) init_workspace(oldsize);
error(ERR_NOMEMORY);
return;
}
emulate_printf("\r\nMemory available for Basic programs is now %u bytes\r\n", basicvars.worksize);
}
clear_program();
init_expressions();
DEBUGFUNCMSGOUT;
}
/*
** 'exec_old' used tp check to see if there is a program still in memory
** and attempt to recover it. Unfortunately, it didn't work.
*/
static void exec_old(void) {
DEBUGFUNCMSGIN;
DEBUGFUNCMSGOUT;
error(ERR_UNSUPPORTED);
}
/*
** 'list_vars' lists the variables, procedures and functions in the
** symbol table and their values or parameter types. If followed by
** a character, then only the names of variables starting with that
** letter are listed. If followed by a name in double quotes, the
** name is taken to be that of a library and the variables in that
** library are listed.
*/
static void list_vars(void) {
char *p, ch;
library *lp;
DEBUGFUNCMSGIN;
p = CAST(GET_SRCADDR(basicvars.current), char *); /* Get the argument of the LVAR command if one is supplied */
basicvars.current+=1+OFFSIZE;
check_ateol();
ch = *p;
if (ch == '"') { /* List variables in library */
int len;
char *start;
boolean found;
p++;
start = p;
while (*p != '"') p++;
len = p-start;
if (len == 0) return; /* Quick way out if length of name is zero */
memcpy(basicvars.stringwork, start, len);
basicvars.stringwork[len] = asc_NUL;
lp = basicvars.liblist;
while (lp != NIL && strncmp(lp->libname, basicvars.stringwork, FNAMESIZE) != 0) lp = lp->libflink;
found = lp != NIL;
if (lp != NIL) detail_library(lp);
lp = basicvars.installist;
while (lp != NIL && strncmp(lp->libname, basicvars.stringwork, FNAMESIZE) != 0) lp = lp->libflink;
found = found || lp != NIL;
if (lp != NIL) detail_library(lp);
if (!found) {
DEBUGFUNCMSGOUT;
error(ERR_NOLIB, basicvars.stringwork);
}
}
else {
if (isalpha(ch))
emulate_printf("Variables in program starting with '%c':\r\n", ch);
else {
ch = ' ';
emulate_printf("Variables in program:\r\n");
}
list_variables(ch);
list_libraries(ch);
}
DEBUGFUNCMSGOUT;
}
/*
** 'list_if' deals with the 'LISTIF' command. It lists each line
** where there is at least one occurence of the string following
** the 'LISTIF' command.
*/
static void list_if(void) {
byte *p, *tp;
int32 targetlen, statelen;
char first, *sp;
DEBUGFUNCMSGIN;
p = tp = GET_SRCADDR(basicvars.current); /* Get address of string to search for */
basicvars.current+=1+OFFSIZE;
check_ateol();
while (*p != asc_NUL) p++; /* Find the end of the string */
targetlen = p-tp; /* Number of characters in search string */
if (targetlen == 0) return; /* End if search string is empty */
p = basicvars.start;
first = *tp;
while (!AT_PROGEND(p)) {
reset_indent();
expand(p, basicvars.stringwork);
sp = basicvars.stringwork;
statelen = strlen(basicvars.stringwork);
do {
sp++;
while (statelen>=targetlen && *sp != first) {
statelen--;
sp++;
}
} while(statelen>=targetlen && memcmp(sp, tp, targetlen) != 0);
if (statelen>=targetlen) { /* Can only be true if the string was found */
#ifdef DEBUG
if (basicvars.debug_flags.tokens)
emulate_printf("%08p %s\r\n", p, basicvars.stringwork);
else
#endif
emulate_printf("%s\r\n", basicvars.stringwork);
}
p+=GET_LINELEN(p);
}
DEBUGFUNCMSGOUT;
}
/*
** 'set_listopt' sets the options for the LIST command. It is also
** used to set the level of internal debugging information produced
** by the interpreter.
*/
void set_listopt(void) {
int32 listopts;
DEBUGFUNCMSGIN;
basicvars.current++;
listopts = get_number();
check_ateol();
set_listoption(listopts);
DEBUGFUNCMSGOUT;
}
/*
** 'delete' is called to delete a range of lines from the program
*/
static void delete(void) {
size_t low, high;
DEBUGFUNCMSGIN;
if (basicvars.misc_flags.badprogram) {
DEBUGFUNCMSGOUT;
error(ERR_BADPROG);
return;
}
if (basicvars.runflags.running) { /* Cannot modify a running program */
DEBUGFUNCMSGOUT;
error(ERR_COMMAND);
return;
}
basicvars.current++;
if (isateol(basicvars.current)) {
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
get_pair(&low, &high, 0, MAXLINENO);
check_ateol();
if (low>MAXLINENO || high>MAXLINENO) {
DEBUGFUNCMSGOUT;
error(ERR_LINENO);
return;
}
delete_range(low, high);
DEBUGFUNCMSGOUT;
}
/*
** 'renumber' renumbers a Basic program
*/
static void renumber(void) {
size_t start, step;
DEBUGFUNCMSGIN;
if (basicvars.misc_flags.badprogram) {
DEBUGFUNCMSGOUT;
error(ERR_BADPROG);
return;
}
if (basicvars.runflags.running) { /* Cannot modify a running program */
DEBUGFUNCMSGOUT;
error(ERR_COMMAND);
return;
}
basicvars.current++;
get_pair(&start, &step, 10, 10);
check_ateol();
if (start>MAXLINENO) {
DEBUGFUNCMSGOUT;
error(ERR_LINENO);
return;
}
if (step==0 || step>=MAXLINENO) { /* An increment of zero is silly */
DEBUGFUNCMSGOUT;
error(ERR_SILLY);
return;
}
renumber_program(basicvars.start, start, step);
DEBUGFUNCMSGOUT;
}
/*
** 'show_memory' prints out chunks of memory in hex and character form.
** 'LISTB' displays it as byte data and 'LISTW' as word.
*/
static void show_memory(void) {
byte which;
size_t lowaddr, highaddr;
DEBUGFUNCMSGIN;
which = *basicvars.current;
basicvars.current++;
get_pair(&lowaddr, &highaddr, basicvars.memdump_lastaddr, basicvars.memdump_lastaddr+0x40);
check_ateol();
if (highaddr == lowaddr) highaddr = lowaddr+0x40;
if (which == BASTOKEN_LISTB)
show_byte(lowaddr, highaddr);
else {
show_word(lowaddr, highaddr);
}
basicvars.memdump_lastaddr = highaddr;
DEBUGFUNCMSGOUT;
}
/*
** 'list_program' lists the source of a Basic program
*/
static void list_program(void) {
size_t lowline, highline;
int32 count;
boolean more, paused;
byte *p;
DEBUGFUNCMSGIN;
if (basicvars.misc_flags.badprogram) {
DEBUGFUNCMSGOUT;
error(ERR_BADPROG);
return;
}
basicvars.current++;
get_pair(&lowline, &highline, 0, MAXLINENO);
check_ateol();
if (lowline>MAXLINENO || highline>MAXLINENO) {
DEBUGFUNCMSGOUT;
error(ERR_LINENO);
return;
}
if (lowline == 0)
p = basicvars.start;
else {
p = find_line(lowline);
}
reset_indent();
basicvars.printcount = 0;
count = 0;
more = TRUE;
while (more && !AT_PROGEND(p) && GET_LINENO(p)<=highline) {
expand(p, basicvars.stringwork);
#ifdef DEBUG
if (basicvars.debug_flags.tokens)
emulate_printf("%p %s\r\n", p, basicvars.stringwork);
else
#endif
emulate_printf("%s\r\n", basicvars.stringwork);
p+=GET_LINELEN(p);
if (basicvars.list_flags.showpage) {
count++;
if (count == PAGESIZE) {
paused = TRUE;
emulate_printf("-- More --");
do {
if (kbd_escpoll()) {
DEBUGFUNCMSGOUT;
error(ERR_ESCAPE);
return;
}
count = kbd_get();
switch (count) {
case ' ':
count = 0;
paused = FALSE;
break;
case asc_CR: case asc_LF:
count = PAGESIZE-1; /* A hack, but it does the job */
paused = FALSE;
break;
case asc_ESC:
paused = more = FALSE;
}
} while (paused);
emulate_printf("\r \r"); /* Overwrite 'more' */
}
}
#ifdef USE_SDL
if (kbd_escpoll()) {
DEBUGFUNCMSGOUT;
error(ERR_ESCAPE);
return;
}
#endif
if (basicvars.escape) {
DEBUGFUNCMSGOUT;
error(ERR_ESCAPE);
}
}
DEBUGFUNCMSGOUT;
}
/*
** 'list_hexline' lists a line as a hex dump
*/
static void list_hexline(void) {
int32 length;
size_t theline;
byte *where;
DEBUGFUNCMSGIN;
basicvars.current++;
get_pair(&theline, &theline, 0, 0);
check_ateol();
if (theline>MAXLINENO) {
DEBUGFUNCMSGOUT;
error(ERR_LINENO);
return;
}
if (theline == 0)
where = basicvars.start;
else {
where = find_line(theline);
}
if (theline != GET_LINENO(where)) { /* Line not found */
DEBUGFUNCMSGOUT;
error(ERR_LINEMISS, theline);
return;
}
length = GET_LINELEN(where);
emulate_printf("Line %d at %p, length=%d", GET_LINENO(where), where, length);
if (length<MINSTATELEN || length>MAXSTATELEN) {
emulate_printf(" ** Statement length is bad **\r\n");
length = 96; /* Print six lines of sixteen bytes */
}
else {
emulate_printf("\r\n");
}
show_byte((size_t)where, (size_t)where+length);
DEBUGFUNCMSGOUT;
}
/*
** 'check_incore' looks for a so-called 'incore' filename, that is, the
** name of the file to save as given on the first line of the program after
** a '>'. It returns a pointer to the file name or NIL if it cannot find
(( one
*/
static char *check_incore(void) {
byte *p;
DEBUGFUNCMSGIN;
if (AT_PROGEND(basicvars.start)) return NIL; /* There is nothing to search */
p = basicvars.start+OFFSOURCE;
while (*p != asc_NUL && *p != '>') p++; /* Look for a '>' */
if (*p == asc_NUL) return NIL; /* Did not find one so give up */
p = skip(p+1);
if (*p == asc_NUL) return NIL; /* There is nothing after the '>' */
DEBUGFUNCMSGOUT;
return TOSTRING(p);
}
/*
** 'get_savefile' returns a pointer to the name to be used when
** saving a file. This can be a name given on the command line,
** an 'in core' name or one saved from a previous load or save
** command. The 'in core' name takes precedence over the one
** saved in basicvars.program. (Should this be reversed?)
*/
static char *get_savefile(void) {
char *np;
DEBUGFUNCMSGIN;
if (isateol(basicvars.current)) {
np = check_incore(); /* Check for an 'incore' file name */
if (np == NIL) { /* Did not find one */
if (basicvars.program[0] == asc_NUL) {
DEBUGFUNCMSGOUT;
error(ERR_FILENAME);
return NULL;
} else {
np = basicvars.program;
}
}
}
else {
np = get_name();
check_ateol();
}
DEBUGFUNCMSGOUT;
return np;
}
/*
** 'save_program' is called to save a program. Programs are
** always saved in text form
*/
static void save_program(void) {
char *np;
int32 listovalue;
DEBUGFUNCMSGIN;
if (basicvars.misc_flags.badprogram) {
DEBUGFUNCMSGOUT;
error(ERR_BADPROG);
return;
}
basicvars.current++;
np = get_savefile();
reset_indent();
listovalue = get_listo();
set_listoption(0);
write_text(np, NULL);
set_listoption(listovalue);
STRLCPY(basicvars.program, np, FNAMESIZE); /* Preserve name used when saving program for later */
DEBUGFUNCMSGOUT;
}
/*
** 'saveo_program' implements TEXTSAVEO and SAVEO
*/
static void saveo_program(void) {
char *np;
int32 saveopts;
DEBUGFUNCMSGIN;
if (basicvars.misc_flags.badprogram) {
DEBUGFUNCMSGOUT;
error(ERR_BADPROG);
return;
}
basicvars.current++;
if (isateol(basicvars.current)) {
DEBUGFUNCMSGOUT;
error(ERR_SYNTAX);
return;
}
saveopts = get_number();
if (*basicvars.current == ',') basicvars.current++; /* Skip comma between options and name */
np = get_savefile();
basicvars.listo_copy = basicvars.list_flags;
set_listoption(saveopts); /* Change list options to save options */
basicvars.list_flags.lower = FALSE; /* Lose pointless options */
basicvars.list_flags.showpage = FALSE;
basicvars.list_flags.expand = FALSE;
reset_indent();
write_text(np, NULL);
STRLCPY(basicvars.program, np, FNAMESIZE); /* Preserve name used for program for later */
basicvars.list_flags = basicvars.listo_copy; /* Restore LISTO flags to original values */
DEBUGFUNCMSGOUT;
}
/*
** 'load_program' attempts to load a Basic program into memory.
** Note that the real name of the file loaded will be stored in
** basicvars.filename when the file is opened.
*/
static void load_program(void) {
char *np;
DEBUGFUNCMSGIN;
if (basicvars.runflags.running) { /* Cannot modify a running program */
DEBUGFUNCMSGOUT;
error(ERR_COMMAND);
return;
}
basicvars.current++;
if (isateol(basicvars.current)) {
DEBUGFUNCMSGOUT;
error(ERR_FILENAME);
return;
}
np = get_name();
check_ateol();
clear_varptrs();
clear_varlists();
clear_strings();
clear_heap();
clear_stack();
read_basic(np);
init_expressions();
STRLCPY(basicvars.program, basicvars.filename, FNAMESIZE);
DEBUGFUNCMSGOUT;
}
/*
** 'install_library' loads the named library into permanent memory (as
** opposed to saving it on the Basic heap)
*/
static void install_library(void) {
DEBUGFUNCMSGIN;
basicvars.current++;
if (isateol(basicvars.current)) { /* Filename missing */
DEBUGFUNCMSGOUT;
error(ERR_FILENAME);
return;
} else {
do {
char *name = get_name();
if (strlen(name)>0) read_library(name, INSTALL_LIBRARY); /* Permanently install the library */
if (*basicvars.current != ',') break;
basicvars.current++;
} while (TRUE);
check_ateol();
}
DEBUGFUNCMSGOUT;
}
static void print_help(void) {
DEBUGFUNCMSGIN;
#ifndef NOINLINEHELP
char *parm;
#endif
basicvars.current++;
#ifndef NOINLINEHELP
if (isateol(basicvars.current)) {
show_options(1);
emulate_printf("HELP can show help on a keyword, for example HELP \"MODE\". Note that the\r\nkeyword must be given in quotes. HELP \".\" will list the keywords help is\r\navailable on.\r\n");
} else {
parm=get_name();
detailed_help(parm);
}
#else
if (!isateol(basicvars.current)) {
emulate_printf("Detailed help not available (compiled with -DNOINLINEHELP)\r\n");
while (*basicvars.current != '\0') basicvars.current = skip_token(basicvars.current);
} else {
show_options(1);
}
#endif
check_ateol();
DEBUGFUNCMSGOUT;
}
#ifdef TARGET_RISCOS
/*
** 'invoke_editor' invokes an editor. The Basic program current in memory
** is written to a temporary file and then a text editor invoked to edit
** the file. On returning from the editor, the Basic program is reloaded.
** There is no error checking here. If the editor flagged an error then
** the results are unpredictable
*/
static void invoke_editor(void) {
char tempname[FNAMESIZE], savedname[FNAMESIZE];
int32 retcode;
char *p;
FILE *fhandle;
_kernel_osfile_block now, then;
DEBUGFUNCMSGIN;
if (basicvars.runflags.running) { /* Cannot edit a running program */
DEBUGFUNCMSGOUT;
error(ERR_COMMAND);
return;
}
basicvars.listo_copy = basicvars.list_flags;
if (basicvars.misc_flags.validedit) basicvars.list_flags = basicvars.edit_flags;
basicvars.list_flags.lower = FALSE; /* Changing keyword to lower case is useless here */
basicvars.list_flags.expand = FALSE; /* So is adding extra blanks */
fhandle=secure_tmpnam(tempname);
if (!fhandle) {
DEBUGFUNCMSGOUT;
error(ERR_EDITFAIL, strerror (errno));
return;
}
reset_indent();
write_text(tempname, fhandle);
basicvars.list_flags = basicvars.listo_copy; /* Restore LISTO flags to original values */
p = getenv("Wimp$State"); /* Is interpreter running under the RISC OS desktop? */
if (p == NIL || strncmp(p, "desktop", 8) != 0) { /* Running at F12 command line or outside desktop */
/*
** Interpreter is running at the F12 command line or outside the
** desktop. The editor called is twin in this case, but it is
** fairly pointless to do this as twin does not return to the
** program that invoked it in the way this code wants
*/
STRLCPY(basicvars.stringwork, "twin", MAXSTRING);
STRLCAT(basicvars.stringwork, " ", MAXSTRING);
STRLCAT(basicvars.stringwork, tempname, MAXSTRING);
retcode = system(basicvars.stringwork);
if (retcode == 0) { /* Invocation of editor worked */
STRLCPY(savedname, basicvars.program, FNAMESIZE); /* Otherwise 'clear' erases it */
clear_program();
read_basic(tempname);
STRLCPY(basicvars.program, savedname, FNAMESIZE);
} else {
DEBUGFUNCMSGOUT;
error(ERR_EDITFAIL, strerror (errno));
return;
}
} else {
/*
** Program is running in the desktop, probably in a task window.
** The program can be passed to an editor easily enough by just
** writing it to a file and the filer_run'ing that file. Getting
** it back is more of a problem. The editor runs in parallel
** with the interpreter and the filer_run call returns immediately.
** There is no easy way to wait until the file has been saved so
** a hack involving the 'inkey' OSBYTE call is used. Once per
** second the code uses an OSFILE call to check if the timestamp
** on the file has changed and if it has, it ends the loop and
** reloads it
*/
STRLCPY(basicvars.stringwork, editname, MAXSTRING);
STRLCAT(basicvars.stringwork, " ", MAXSTRING);
STRLCAT(basicvars.stringwork, tempname, MAXSTRING);
/* Extract details of file as written to disk */
retcode = _kernel_osfile(17, tempname, &now);
if (retcode != 1) {
DEBUGFUNCMSGOUT;
error(ERR_BROKEN, __LINE__, "commands");
return;
}
retcode = system(basicvars.stringwork);
if (retcode == 0) { /* Invocation of editor worked */
/* Now sit in a loop waiting for the file to change */
do {
int32 r2byte;
retcode = _kernel_osbyte(129, 100, 0); /* Wait for one second */
r2byte = (retcode>>BYTESHIFT) & BYTEMASK; /* Only interested in what R2 contains after call */
if (r2byte == ESC || basicvars.escape) break; /* Escape was pressed - Escape from loop */
retcode = _kernel_osfile(17, tempname, &then);
} while (retcode == 1 && now.load == then.load && now.exec == then.exec);
if (retcode == 1) { /* Everything is okay and file has been updated */
STRLCPY(savedname, basicvars.program, FNAMESIZE); /* Otherwise 'clear' erases it */
clear_program();
read_basic(tempname);
STRLCPY(basicvars.program, savedname, FNAMESIZE);
}
} else {
DEBUGFUNCMSGOUT;
error(ERR_EDITFAIL, strerror (errno));
return;
}
}
remove(tempname);
DEBUGFUNCMSGOUT;
}
/*
** 'exec_editor' either calls a text editor to edit the whole
** program or allows a single line to be edited. This latter
** feature is not supported under RISCOS (as it has cursor
** editing, which is far more flexible)
*/
static void exec_editor(void) {
DEBUGFUNCMSGIN;
if (basicvars.misc_flags.badprogram) {
DEBUGFUNCMSGOUT;
error(ERR_BADPROG);
return;
}
basicvars.current++;
if (isateol(basicvars.current)) /* Nothing on line - start an editor */
invoke_editor();
else { /* 'edit <line no>' is not supported under RISC OS */
DEBUGFUNCMSGOUT;
error(ERR_UNSUPPORTED);
}
DEBUGFUNCMSGOUT;
}
#else
/*
** 'invoke_editor' invokes an editor. The Basic program current in memory
** is written to a temporary file and then a text editor invoked to edit
** the file. On returning from the editor, the Basic program is reloaded.
** There is no error checking here. If the editor flagged an error then
** the results are unpredictable
*/
static void invoke_editor(void) {
char tempname[FNAMESIZE], savedname[FNAMESIZE];
int32 retcode;
FILE *fhandle;
#ifdef TARGET_DJGPP
char *p;
#endif
DEBUGFUNCMSGIN;
if (basicvars.runflags.running) { /* Cannot edit a running program */
DEBUGFUNCMSGOUT;
error(ERR_COMMAND);
return;
}
fhandle=secure_tmpnam(tempname);
if (!fhandle) {
DEBUGFUNCMSGOUT;
error(ERR_EDITFAIL, strerror (errno));
return;
}
#ifdef TARGET_DJGPP
p = tempname; /* Replace '/' in filename with '\' */
while (*p != NUL) {
if (*p == '/') *p = '\\';
p++;
}
#endif
basicvars.listo_copy = basicvars.list_flags;
if (basicvars.misc_flags.validedit) basicvars.list_flags = basicvars.edit_flags;
basicvars.list_flags.lower = FALSE; /* Changing keyword to lower case is useless here */
basicvars.list_flags.expand = FALSE;
reset_indent();
write_text(tempname, fhandle); /* This function will close fhandle */
basicvars.list_flags = basicvars.listo_copy; /* Restore LISTO flags to original values */
STRLCPY(basicvars.stringwork, editname, MAXSTRING);
STRLCAT(basicvars.stringwork, " ", MAXSTRING);
STRLCAT(basicvars.stringwork, tempname, MAXSTRING);
retcode = system(basicvars.stringwork);
if (retcode == 0) { /* Editor call worked */
STRLCPY(savedname, basicvars.program, FNAMESIZE); /* Otherwise 'clear' erases it */
clear_program();
read_basic(tempname);
STRLCPY(basicvars.program, savedname, FNAMESIZE);
} else {
DEBUGFUNCMSGOUT;
error(ERR_EDITFAIL, strerror (errno));
return;
}
remove(tempname);
DEBUGFUNCMSGOUT;
}
/*
** 'alter_line' is called to alter one line in a program by copying
** it to the input buffer so that it can be edited there
** One point to watch out for here is that this code overwrites the
** existing contents of 'thisline' (this contains the 'EDIT <line>'
** command). Since we are going to return to the command line loop
** anyway, the code avoids the problems this will cause in
** exec_statements() by branching directly back there instead of
** returning via the proper route
*/
static void alter_line(void) {
int32 lineno;
byte *p;
boolean ok;
DEBUGFUNCMSGIN;
lineno = get_number();
check_ateol();
if (basicvars.runflags.running) { /* Cannot edit a running program */
DEBUGFUNCMSGOUT;
error(ERR_COMMAND);
return;
}
if (basicvars.misc_flags.badprogram) {
DEBUGFUNCMSGOUT;
error(ERR_BADPROG);
return;
}
if (lineno<0 || lineno>MAXLINENO) {
DEBUGFUNCMSGOUT;
error(ERR_LINENO);
return;
}
p = find_line(lineno);
if (GET_LINENO(p) != lineno) {
DEBUGFUNCMSGOUT;
error(ERR_LINEMISS, lineno);
return;
}
basicvars.listo_copy = basicvars.list_flags;
basicvars.list_flags.space = FALSE; /* Reset listing options */
basicvars.list_flags.indent = FALSE;
basicvars.list_flags.split = FALSE;
basicvars.list_flags.noline = FALSE;
basicvars.list_flags.lower = FALSE;
basicvars.list_flags.expand = FALSE;
expand(p, basicvars.stringwork);
basicvars.list_flags = basicvars.listo_copy; /* Restore LISTOF flags to original values */
ok = amend_line(basicvars.stringwork, MAXSTATELEN);
if (!ok) {
DEBUGFUNCMSGOUT;
error(ERR_ESCAPE);
return;
}
tokenize(basicvars.stringwork, thisline, HASLINE, FALSE);
// tokenize(basicvars.stringwork, thisline, HASLINE);
if (GET_LINENO(thisline) == NOLINENO) /* If line number has been removed, execute line */
exec_thisline();
else {
edit_line();
}
/*
** At this point the contents of 'thisline' are effectively undefined
** but this will cause problems in exec_statements() when we return
** to it. To avoid this, the code just branches back into the command
** loop via 'longjump()'. This is a kludge.
*/
DEBUGFUNCMSGOUT;
siglongjmp(basicvars.restart, 1);
}
/*
** 'exec_editor' either calls a text editor to edit the whole
** program or allows a single line to be edited
*/
static void exec_editor(void) {
DEBUGFUNCMSGIN;
if (basicvars.misc_flags.badprogram) {
DEBUGFUNCMSGOUT;
error(ERR_BADPROG);
return;
}
basicvars.current++;
if (isateol(basicvars.current)) /* Nothing on line - start an editor */
invoke_editor();
else { /* EDIT <line> command */
alter_line();
}
DEBUGFUNCMSGOUT;
}
#endif
/*
** 'exec_edito' deals with the EDITO command. This invokes the editor
** using the supplied listo options to format the program
*/
static void exec_edito(void) {
int32 editopts;
DEBUGFUNCMSGIN;
if (basicvars.misc_flags.badprogram) {
DEBUGFUNCMSGOUT;
error(ERR_BADPROG);
return;
}
basicvars.current++;
if (isateol(basicvars.current)) {