forked from rcornwell/sims
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_console.c
3260 lines (2888 loc) · 114 KB
/
sim_console.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
/* sim_console.c: simulator console I/O library
Copyright (c) 1993-2014, Robert M Supnik
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
ROBERT M SUPNIK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Except as contained in this notice, the name of Robert M Supnik shall not be
used in advertising or otherwise to promote the sale, use or other dealings
in this Software without prior written authorization from Robert M Supnik.
02-Jan-14 RMS Added tab stop routines
18-Mar-12 RMS Removed unused reference to sim_switches (Dave Bryan)
07-Dec-11 MP Added sim_ttisatty to support reasonable behaviour (i.e.
avoid in infinite loop) in the main command input
loop when EOF is detected and input is coming from
a file (or a null device: /dev/null or NUL:) This may
happen when a simulator is running in a background
process.
17-Apr-11 MP Cleaned up to support running in a background/detached
process
20-Jan-11 MP Fixed support for BREAK key on Windows to account
for/ignore other keyboard Meta characters.
18-Jan-11 MP Added log file reference count support
17-Jan-11 MP Added support for a "Buffered" behaviors which include:
- If Buffering is enabled and Telnet is enabled, a
telnet connection is not required for simulator
operation (instruction execution).
- If Buffering is enabled, all console output is
written to the buffer at all times (deleting the
oldest buffer contents on overflow).
- when a connection is established on the console
telnet port, the whole contents of the Buffer is
presented on the telnet session and connection
will then proceed as if the connection had always
been there.
This concept allows a simulator to run in the background
and when needed a console session to be established.
The "when needed" case usually will be interested in
what already happened before looking to address what
to do, hence the buffer contents being presented.
28-Dec-10 MP Added support for BREAK key on Windows
30-Sep-06 RMS Fixed non-printable characters in KSR mode
22-Jun-06 RMS Implemented SET/SHOW PCHAR
31-May-06 JDB Fixed bug if SET CONSOLE DEBUG with no argument
22-Nov-05 RMS Added central input/output conversion support
05-Nov-04 RMS Moved SET/SHOW DEBUG under CONSOLE hierarchy
28-Oct-04 JDB Fixed SET CONSOLE to allow comma-separated parameters
20-Aug-04 RMS Added OS/2 EMX fixes (Holger Veit)
14-Jul-04 RMS Revised Windows console code (Dave Bryan)
28-May-04 RMS Added SET/SHOW CONSOLE
RMS Added break, delete character maps
02-Jan-04 RMS Removed timer routines, added Telnet console routines
RMS Moved console logging to OS-independent code
25-Apr-03 RMS Added long seek support (Mark Pizzolato)
Added Unix priority control (Mark Pizzolato)
24-Sep-02 RMS Removed VT support, added Telnet console support
Added CGI support (Brian Knittel)
Added MacOS sleep (Peter Schorn)
14-Jul-02 RMS Added Windows priority control (Mark Pizzolato)
20-May-02 RMS Added Windows VT support (Fischer Franz)
01-Feb-02 RMS Added VAX fix (Robert Alan Byer)
19-Sep-01 RMS More MacOS changes
31-Aug-01 RMS Changed int64 to t_int64 for Windoze
20-Jul-01 RMS Added MacOS support (Louis Chretien, Peter Schorn, Ben Supnik)
15-May-01 RMS Added logging support
05-Mar-01 RMS Added clock calibration support
08-Dec-00 BKR Added OS/2 support (Bruce Ray)
18-Aug-98 RMS Added BeOS support
13-Oct-97 RMS Added NetBSD terminal support
25-Jan-97 RMS Added POSIX terminal I/O support
02-Jan-97 RMS Fixed bug in sim_poll_kbd
This module implements the following routines to support terminal and
Remote Console I/O:
sim_poll_kbd poll for keyboard input
sim_putchar output character to console
sim_putchar_s output character to console, stall if congested
sim_set_console set console parameters
sim_show_console show console parameters
sim_set_remote_console set remote console parameters
sim_show_remote_console show remote console parameters
sim_set_cons_buff set console buffered
sim_set_cons_unbuff set console unbuffered
sim_set_cons_log set console log
sim_set_cons_nolog set console nolog
sim_show_cons_buff show console buffered
sim_show_cons_log show console log
sim_tt_inpcvt convert input character per mode
sim_tt_outcvt convert output character per mode
sim_cons_get_send get console send structure address
sim_cons_get_expect get console expect structure address
sim_show_cons_send_input show pending input data
sim_show_cons_expect show expect rules and state
sim_ttinit called once to get initial terminal state
sim_ttrun called to put terminal into run state
sim_ttcmd called to return terminal to command state
sim_ttclose called once before the simulator exits
sim_ttisatty called to determine if running interactively
sim_os_poll_kbd poll for keyboard input
sim_os_putchar output character to console
The first group is OS-independent; the second group is OS-dependent.
The following routines are exposed but deprecated:
sim_set_telnet set console to Telnet port
sim_set_notelnet close console Telnet port
sim_show_telnet show console status
*/
#include "sim_defs.h"
#include "sim_tmxr.h"
#include "sim_serial.h"
#include "sim_timer.h"
#include <ctype.h>
#include <math.h>
#ifdef __HAIKU__
#define nice(n) ({})
#endif
/* Forward Declaraations of Platform specific routines */
static t_stat sim_os_poll_kbd (void);
static t_bool sim_os_poll_kbd_ready (int ms_timeout);
static t_stat sim_os_putchar (int32 out);
static t_stat sim_os_ttinit (void);
static t_stat sim_os_ttrun (void);
static t_stat sim_os_ttcmd (void);
static t_stat sim_os_ttclose (void);
static t_bool sim_os_ttisatty (void);
static t_stat sim_set_rem_telnet (int32 flag, CONST char *cptr);
static t_stat sim_set_rem_connections (int32 flag, CONST char *cptr);
static t_stat sim_set_rem_timeout (int32 flag, CONST char *cptr);
static t_stat sim_set_rem_master (int32 flag, CONST char *cptr);
/* Deprecated CONSOLE HALT, CONSOLE RESPONSE and CONSOLE DELAY support */
static t_stat sim_set_halt (int32 flag, CONST char *cptr);
static t_stat sim_set_response (int32 flag, CONST char *cptr);
static t_stat sim_set_delay (int32 flag, CONST char *cptr);
#define KMAP_WRU 0
#define KMAP_BRK 1
#define KMAP_DEL 2
#define KMAP_MASK 0377
#define KMAP_NZ 0400
int32 sim_int_char = 005; /* interrupt character */
int32 sim_brk_char = 000; /* break character */
int32 sim_tt_pchar = 0x00002780;
#if defined (_WIN32) || defined (__OS2__) || (defined (__MWERKS__) && defined (macintosh))
int32 sim_del_char = '\b'; /* delete character */
#else
int32 sim_del_char = 0177;
#endif
static t_stat sim_con_poll_svc (UNIT *uptr); /* console connection poll routine */
static t_stat sim_con_reset (DEVICE *dptr); /* console reset routine */
UNIT sim_con_unit = { UDATA (&sim_con_poll_svc, 0, 0) }; /* console connection unit */
/* debugging bitmaps */
#define DBG_TRC TMXR_DBG_TRC /* trace routine calls */
#define DBG_XMT TMXR_DBG_XMT /* display Transmitted Data */
#define DBG_RCV TMXR_DBG_RCV /* display Received Data */
#define DBG_RET TMXR_DBG_RET /* display Returned Received Data */
#define DBG_ASY TMXR_DBG_ASY /* asynchronous thread activity */
#define DBG_EXP 0x00000001 /* Expect match activity */
#define DBG_SND 0x00000002 /* Send (Inject) data activity */
static DEBTAB sim_con_debug[] = {
{"TRC", DBG_TRC},
{"XMT", DBG_XMT},
{"RCV", DBG_RCV},
{"RET", DBG_RET},
{"ASY", DBG_ASY},
{"EXP", DBG_EXP},
{"SND", DBG_SND},
{0}
};
static REG sim_con_reg[] = {
{ ORDATAD (WRU, sim_int_char, 8, "interrupt character") },
{ ORDATAD (BRK, sim_brk_char, 8, "break character") },
{ ORDATAD (DEL, sim_del_char, 8, "delete character ") },
{ ORDATAD (PCHAR, sim_tt_pchar, 32, "printable character mask") },
{ 0 },
};
static MTAB sim_con_mod[] = {
{ 0 },
};
DEVICE sim_con_telnet = {
"CON-TEL", &sim_con_unit, sim_con_reg, sim_con_mod,
1, 0, 0, 0, 0, 0,
NULL, NULL, sim_con_reset, NULL, NULL, NULL,
NULL, DEV_DEBUG, 0, sim_con_debug};
TMLN sim_con_ldsc = { 0 }; /* console line descr */
TMXR sim_con_tmxr = { 1, 0, 0, &sim_con_ldsc, NULL, &sim_con_telnet };/* console line mux */
SEND sim_con_send = {SEND_DEFAULT_DELAY, &sim_con_telnet, DBG_SND};
EXPECT sim_con_expect = {&sim_con_telnet, DBG_EXP};
static t_bool sim_con_console_port = TRUE;
/* Enable automatic WRU console polling */
t_stat sim_set_noconsole_port (void)
{
sim_con_console_port = FALSE;
return SCPE_OK;
}
/* Unit service for console connection polling */
static t_stat sim_con_poll_svc (UNIT *uptr)
{
if ((sim_con_tmxr.master == 0) && /* not Telnet and not serial and not WRU polling? */
(sim_con_ldsc.serport == 0) &&
(sim_con_console_port))
return SCPE_OK; /* done */
if (tmxr_poll_conn (&sim_con_tmxr) >= 0) /* poll connect */
sim_con_ldsc.rcve = 1; /* rcv enabled */
sim_activate_after(uptr, 1000000); /* check again in 1 second */
if (!sim_con_console_port) /* WRU poll needed */
sim_poll_kbd(); /* sets global stop_cpu when WRU received */
if (sim_con_ldsc.conn)
tmxr_send_buffered_data (&sim_con_ldsc); /* try to flush any buffered data */
return SCPE_OK;
}
static t_stat sim_con_reset (DEVICE *dptr)
{
return sim_con_poll_svc (&dptr->units[0]); /* establish polling as needed */
}
/* Set/show data structures */
static CTAB set_con_tab[] = {
{ "WRU", &sim_set_kmap, KMAP_WRU | KMAP_NZ },
{ "BRK", &sim_set_kmap, KMAP_BRK },
{ "DEL", &sim_set_kmap, KMAP_DEL |KMAP_NZ },
{ "PCHAR", &sim_set_pchar, 0 },
{ "SPEED", &sim_set_cons_speed, 0 },
{ "TELNET", &sim_set_telnet, 0 },
{ "NOTELNET", &sim_set_notelnet, 0 },
{ "SERIAL", &sim_set_serial, 0 },
{ "NOSERIAL", &sim_set_noserial, 0 },
{ "LOG", &sim_set_logon, 0 },
{ "NOLOG", &sim_set_logoff, 0 },
{ "DEBUG", &sim_set_debon, 0 },
{ "NODEBUG", &sim_set_deboff, 0 },
#define CMD_WANTSTR 0100000
{ "HALT", &sim_set_halt, 1 | CMD_WANTSTR },
{ "NOHALT", &sim_set_halt, 0 },
{ "DELAY", &sim_set_delay, 0 },
{ "RESPONSE", &sim_set_response, 1 | CMD_WANTSTR },
{ "NORESPONSE", &sim_set_response, 0 },
{ NULL, NULL, 0 }
};
static CTAB set_rem_con_tab[] = {
{ "CONNECTIONS", &sim_set_rem_connections, 0 },
{ "TELNET", &sim_set_rem_telnet, 1 },
{ "NOTELNET", &sim_set_rem_telnet, 0 },
{ "TIMEOUT", &sim_set_rem_timeout, 0 },
{ "MASTER", &sim_set_rem_master, 1 },
{ "NOMASTER", &sim_set_rem_master, 0 },
{ NULL, NULL, 0 }
};
static SHTAB show_con_tab[] = {
{ "WRU", &sim_show_kmap, KMAP_WRU },
{ "BRK", &sim_show_kmap, KMAP_BRK },
{ "DEL", &sim_show_kmap, KMAP_DEL },
{ "PCHAR", &sim_show_pchar, 0 },
{ "SPEED", &sim_show_cons_speed, 0 },
{ "LOG", &sim_show_cons_log, 0 },
{ "TELNET", &sim_show_telnet, 0 },
{ "DEBUG", &sim_show_cons_debug, 0 },
{ "BUFFERED", &sim_show_cons_buff, 0 },
{ "EXPECT", &sim_show_cons_expect, 0 },
{ "HALT", &sim_show_cons_expect, 0 },
{ "INPUT", &sim_show_cons_send_input, 0 },
{ "RESPONSE", &sim_show_cons_send_input, 0 },
{ "DELAY", &sim_show_cons_expect, 0 },
{ NULL, NULL, 0 }
};
static CTAB set_con_telnet_tab[] = {
{ "LOG", &sim_set_cons_log, 0 },
{ "NOLOG", &sim_set_cons_nolog, 0 },
{ "BUFFERED", &sim_set_cons_buff, 0 },
{ "NOBUFFERED", &sim_set_cons_unbuff, 0 },
{ "UNBUFFERED", &sim_set_cons_unbuff, 0 },
{ NULL, NULL, 0 }
};
static CTAB set_con_serial_tab[] = {
{ "LOG", &sim_set_cons_log, 0 },
{ "NOLOG", &sim_set_cons_nolog, 0 },
{ NULL, NULL, 0 }
};
static int32 *cons_kmap[] = {
&sim_int_char,
&sim_brk_char,
&sim_del_char
};
/* Console I/O package.
The console terminal can be attached to the controlling window
or to a Telnet connection. If attached to a Telnet connection,
the console is described by internal terminal multiplexor
sim_con_tmxr and internal terminal line description sim_con_ldsc.
*/
/* SET CONSOLE command */
t_stat sim_set_console (int32 flag, CONST char *cptr)
{
char *cvptr, gbuf[CBUFSIZE];
CTAB *ctptr;
t_stat r;
if ((cptr == NULL) || (*cptr == 0))
return SCPE_2FARG;
while (*cptr != 0) { /* do all mods */
cptr = get_glyph_nc (cptr, gbuf, ','); /* get modifier */
if ((cvptr = strchr (gbuf, '='))) /* = value? */
*cvptr++ = 0;
get_glyph (gbuf, gbuf, 0); /* modifier to UC */
if ((ctptr = find_ctab (set_con_tab, gbuf))) { /* match? */
r = ctptr->action (ctptr->arg, cvptr); /* do the rest */
if (r != SCPE_OK)
return r;
}
else return SCPE_NOPARAM;
}
return SCPE_OK;
}
/* SHOW CONSOLE command */
t_stat sim_show_console (FILE *st, DEVICE *dptr, UNIT *uptr, int32 flag, CONST char *cptr)
{
char gbuf[CBUFSIZE];
SHTAB *shptr;
int32 i;
if (*cptr == 0) { /* show all */
for (i = 0; show_con_tab[i].name; i++)
show_con_tab[i].action (st, dptr, uptr, show_con_tab[i].arg, cptr);
return SCPE_OK;
}
while (*cptr != 0) {
cptr = get_glyph (cptr, gbuf, ','); /* get modifier */
if ((shptr = find_shtab (show_con_tab, gbuf)))
shptr->action (st, dptr, uptr, shptr->arg, cptr);
else return SCPE_NOPARAM;
}
return SCPE_OK;
}
t_stat sim_rem_con_poll_svc (UNIT *uptr); /* remote console connection poll routine */
t_stat sim_rem_con_data_svc (UNIT *uptr); /* remote console connection data routine */
t_stat sim_rem_con_reset (DEVICE *dptr); /* remote console reset routine */
UNIT sim_rem_con_unit[2] = {
{ UDATA (&sim_rem_con_poll_svc, 0, 0) }, /* remote console connection polling unit */
{ UDATA (&sim_rem_con_data_svc, 0, 0) }}; /* console data handling unit */
DEBTAB sim_rem_con_debug[] = {
{"TRC", DBG_TRC},
{"XMT", DBG_XMT},
{"RCV", DBG_RCV},
{0}
};
MTAB sim_rem_con_mod[] = {
{ 0 },
};
DEVICE sim_remote_console = {
"REM-CON", sim_rem_con_unit, NULL, sim_rem_con_mod,
2, 0, 0, 0, 0, 0,
NULL, NULL, sim_rem_con_reset, NULL, NULL, NULL,
NULL, DEV_DEBUG | DEV_NOSAVE, 0, sim_rem_con_debug};
#define MAX_REMOTE_SESSIONS 40 /* Arbitrary Session Limit */
static int32 *sim_rem_buf_size = NULL;
static int32 *sim_rem_buf_ptr = NULL;
static char **sim_rem_buf = NULL;
static t_bool *sim_rem_single_mode = NULL; /* per line command mode (single command or must continue) */
static TMXR sim_rem_con_tmxr = { 0, 0, 0, NULL, NULL, &sim_remote_console };/* remote console line mux */
static uint32 sim_rem_read_timeout = 30; /* seconds before automatic continue */
static uint32 *sim_rem_read_timeouts = NULL;/* per line read timeout (default from sim_rem_read_timeout) */
static int32 sim_rem_active_number = -1; /* -1 - not active, >= 0 is index of active console */
int32 sim_rem_cmd_active_line = -1; /* step in progress on line # */
static CTAB *sim_rem_active_command = NULL; /* active command */
static char *sim_rem_command_buf; /* active command buffer */
static t_bool sim_log_temp = FALSE; /* temporary log file active */
static char sim_rem_con_temp_name[PATH_MAX+1];
static t_bool sim_rem_master_mode = FALSE; /* Master Mode Enabled Flag */
static t_bool sim_rem_master_was_enabled = FALSE; /* Master was Enabled */
static t_bool sim_rem_master_was_connected = FALSE; /* Master Mode has been connected */
static t_offset sim_rem_cmd_log_start = 0; /* Log File saved position */
/* SET REMOTE CONSOLE command */
t_stat sim_set_remote_console (int32 flag, CONST char *cptr)
{
char *cvptr, gbuf[CBUFSIZE];
CTAB *ctptr;
t_stat r;
if ((cptr == NULL) || (*cptr == 0))
return SCPE_2FARG;
while (*cptr != 0) { /* do all mods */
cptr = get_glyph_nc (cptr, gbuf, ','); /* get modifier */
if ((cvptr = strchr (gbuf, '='))) /* = value? */
*cvptr++ = 0;
get_glyph (gbuf, gbuf, 0); /* modifier to UC */
if ((ctptr = find_ctab (set_rem_con_tab, gbuf))) { /* match? */
r = ctptr->action (ctptr->arg, cvptr); /* do the rest */
if (r != SCPE_OK)
return r;
}
else return SCPE_NOPARAM;
}
return SCPE_OK;
}
/* SHOW REMOTE CONSOLE command */
t_stat sim_show_remote_console (FILE *st, DEVICE *dptr, UNIT *uptr, int32 flag, CONST char *cptr)
{
int32 i, connections;
TMLN *lp;
if (*cptr != 0)
return SCPE_NOPARAM;
if (sim_rem_active_number >= 0) {
if (sim_rem_master_mode && (sim_rem_active_number == 0))
fprintf (st, "Running from Master Mode Remote Console Connection\n");
else
fprintf (st, "Running from Remote Console Connection %d\n", sim_rem_active_number);
}
if (sim_rem_con_tmxr.lines > 1)
fprintf (st, "Remote Console Input Connections from %d sources are supported concurrently\n", sim_rem_con_tmxr.lines);
if (sim_rem_read_timeout)
fprintf (st, "Remote Console Input automatically continues after %d seconds\n", sim_rem_read_timeout);
if (!sim_rem_con_tmxr.master)
fprintf (st, "Remote Console Command input is disabled\n");
else
fprintf (st, "Remote Console Command Input listening on TCP port: %s\n", sim_rem_con_unit[0].filename);
for (i=connections=0; i<sim_rem_con_tmxr.lines; i++) {
lp = &sim_rem_con_tmxr.ldsc[i];
if (!lp->conn)
continue;
++connections;
if (connections == 1)
fprintf (st, "Remote Console Connections:\n");
tmxr_fconns (st, lp, i);
if (sim_rem_read_timeouts[i] != sim_rem_read_timeout) {
if (sim_rem_read_timeouts[i])
fprintf (st, "Remote Console Input on connection %d automatically continues after %d seconds\n", i, sim_rem_read_timeouts[i]);
else
fprintf (st, "Remote Console Input on connection %d does not continue automatically\n", i);
}
}
return SCPE_OK;
}
/* Unit service for remote console connection polling */
t_stat sim_rem_con_poll_svc (UNIT *uptr)
{
int32 c;
c = tmxr_poll_conn (&sim_rem_con_tmxr);
if (c >= 0) { /* poll connect */
TMLN *lp = &sim_rem_con_tmxr.ldsc[c];
char wru_name[8];
sim_activate_after(uptr+1, 1000000); /* start data poll after 1 second */
lp->rcve = 1; /* rcv enabled */
sim_rem_buf_ptr[c] = 0; /* start with empty command buffer */
sim_rem_single_mode[c] = TRUE; /* start in single command mode */
sim_rem_read_timeouts[c] = sim_rem_read_timeout; /* Start with default timeout */
if (isprint(sim_int_char&0xFF))
sprintf(wru_name, "'%c'", sim_int_char&0xFF);
else
if (sim_int_char <= 26)
sprintf(wru_name, "^%c", '@' + (sim_int_char&0xFF));
else
sprintf(wru_name, "'\\%03o'", sim_int_char&0xFF);
tmxr_linemsgf (lp, "%s Remote Console\r\n"
"Enter single commands or to enter multiple command mode enter the %s character\r"
"%s",
sim_name, wru_name,
((sim_rem_master_mode && (c == 0)) ? "" : "\nSimulator Running..."));
if (sim_rem_master_mode && (c == 0)) /* Master Mode session? */
sim_rem_single_mode[c] = FALSE; /* start in multi-command mode */
tmxr_send_buffered_data (lp); /* flush buffered data */
}
sim_activate_after(uptr, 1000000); /* check again in 1 second */
if (sim_con_ldsc.conn)
tmxr_send_buffered_data (&sim_con_ldsc); /* try to flush any buffered data */
return SCPE_OK;
}
static t_stat x_continue_cmd (int32 flag, CONST char *cptr)
{
return SCPE_IERR; /* This routine should never be called */
}
static t_stat x_step_cmd (int32 flag, CONST char *cptr)
{
return SCPE_IERR; /* This routine should never be called */
}
static t_stat x_run_cmd (int32 flag, CONST char *cptr)
{
return SCPE_IERR; /* This routine should never be called */
}
static t_stat x_help_cmd (int32 flag, CONST char *cptr);
static CTAB allowed_remote_cmds[] = {
{ "EXAMINE", &exdep_cmd, EX_E },
{ "DEPOSIT", &exdep_cmd, EX_D },
{ "EVALUATE", &eval_cmd, 0 },
{ "ATTACH", &attach_cmd, 0 },
{ "DETACH", &detach_cmd, 0 },
{ "ASSIGN", &assign_cmd, 0 },
{ "DEASSIGN", &deassign_cmd, 0 },
{ "CONTINUE", &x_continue_cmd, 0 },
{ "STEP", &x_step_cmd, 0 },
{ "PWD", &pwd_cmd, 0 },
{ "SAVE", &save_cmd, 0 },
{ "DIR", &dir_cmd, 0 },
{ "LS", &dir_cmd, 0 },
{ "ECHO", &echo_cmd, 0 },
{ "SET", &set_cmd, 0 },
{ "SHOW", &show_cmd, 0 },
{ "HELP", &x_help_cmd, 0 },
{ NULL, NULL }
};
static CTAB allowed_master_remote_cmds[] = {
{ "EXAMINE", &exdep_cmd, EX_E },
{ "DEPOSIT", &exdep_cmd, EX_D },
{ "EVALUATE", &eval_cmd, 0 },
{ "ATTACH", &attach_cmd, 0 },
{ "DETACH", &detach_cmd, 0 },
{ "ASSIGN", &assign_cmd, 0 },
{ "DEASSIGN", &deassign_cmd, 0 },
{ "CONTINUE", &x_continue_cmd, 0 },
{ "STEP", &x_step_cmd, 0 },
{ "PWD", &pwd_cmd, 0 },
{ "SAVE", &save_cmd, 0 },
{ "CD", &set_default_cmd, 0 },
{ "DIR", &dir_cmd, 0 },
{ "LS", &dir_cmd, 0 },
{ "ECHO", &echo_cmd, 0 },
{ "SET", &set_cmd, 0 },
{ "SHOW", &show_cmd, 0 },
{ "HELP", &x_help_cmd, 0 },
{ "EXIT", &exit_cmd, 0 },
{ "QUIT", &exit_cmd, 0 },
{ "RUN", &x_run_cmd, RU_RUN },
{ "GO", &x_run_cmd, RU_GO },
{ "BOOT", &x_run_cmd, RU_BOOT },
{ "BREAK", &brk_cmd, SSH_ST },
{ "NOBREAK", &brk_cmd, SSH_CL },
{ NULL, NULL }
};
static CTAB allowed_single_remote_cmds[] = {
{ "ATTACH", &attach_cmd, 0 },
{ "DETACH", &detach_cmd, 0 },
{ "EXAMINE", &exdep_cmd, EX_E },
{ "EVALUATE", &eval_cmd, 0 },
{ "PWD", &pwd_cmd, 0 },
{ "DIR", &dir_cmd, 0 },
{ "LS", &dir_cmd, 0 },
{ "ECHO", &echo_cmd, 0 },
{ "SHOW", &show_cmd, 0 },
{ "HELP", &x_help_cmd, 0 },
{ NULL, NULL }
};
static t_stat x_help_cmd (int32 flag, CONST char *cptr)
{
CTAB *cmdp, *cmdph;
if (*cptr) {
int32 saved_switches = sim_switches;
t_stat r;
sim_switches |= SWMASK ('F');
r = help_cmd (flag, cptr);
sim_switches = saved_switches;
return r;
}
sim_printf ("Help is available for the following Remote Console commands:\r\n");
for (cmdp=allowed_remote_cmds; cmdp->name != NULL; ++cmdp) {
cmdph = find_cmd (cmdp->name);
if (cmdph && cmdph->help)
sim_printf (" %s\r\n", cmdp->name);
}
sim_printf ("Enter \"HELP cmd\" for detailed help on a command\r\n");
return SCPE_OK;
}
static t_stat _sim_rem_message (const char *cmd, t_stat stat)
{
CTAB *cmdp = NULL;
t_stat stat_nomessage = stat & SCPE_NOMESSAGE; /* extract possible message supression flag */
cmdp = find_cmd (cmd);
stat = SCPE_BARE_STATUS(stat); /* remove possible flag */
if (!stat_nomessage) {
if (cmdp && (cmdp->message)) /* special message handler? */
cmdp->message (NULL, stat); /* let it deal with display */
else {
if (stat >= SCPE_BASE) /* error? */
sim_printf ("%s\r\n", sim_error_text (stat));
}
}
return stat;
}
static void _sim_rem_log_out (TMLN *lp)
{
char cbuf[4*CBUFSIZE];
if (sim_log) {
int32 unwritten;
fflush (sim_log);
sim_fseeko (sim_log, sim_rem_cmd_log_start, SEEK_SET);
cbuf[sizeof(cbuf)-1] = '\0';
while (fgets (cbuf, sizeof(cbuf)-1, sim_log))
tmxr_linemsgf (lp, "%s", cbuf);
if (!tmxr_input_pending_ln (lp)) {
do {
unwritten = tmxr_send_buffered_data (lp);
if (unwritten == lp->txbsz)
sim_os_ms_sleep (100);
} while (unwritten == lp->txbsz);
}
}
}
void sim_remote_process_command (void)
{
char cbuf[4*CBUFSIZE], gbuf[CBUFSIZE], *argv[1] = {NULL};
CONST char *cptr;
int32 saved_switches = sim_switches;
t_stat stat;
strcpy (cbuf, sim_rem_command_buf);
while (isspace(cbuf[0]))
memmove (cbuf, cbuf+1, strlen(cbuf+1)+1); /* skip leading whitespace */
sim_sub_args (cbuf, sizeof(cbuf), argv);
cptr = cbuf;
cptr = get_glyph (cptr, gbuf, 0); /* get command glyph */
sim_rem_active_command = find_cmd (gbuf); /* find command */
sim_ttcmd (); /* restore console */
stat = sim_rem_active_command->action (sim_rem_active_command->arg, cptr);/* execute command */
if (stat != SCPE_OK)
stat = _sim_rem_message (gbuf, stat); /* display results */
sim_last_cmd_stat = SCPE_BARE_STATUS(stat);
sim_ttrun (); /* set console mode */
sim_cancel (&sim_rem_con_unit[1]); /* force immediate activation of sim_rem_con_data_svc */
sim_activate (&sim_rem_con_unit[1], -1);
sim_switches = saved_switches; /* restore original switches */
}
/* Unit service for remote console data polling */
t_stat sim_rem_con_data_svc (UNIT *uptr)
{
int32 i, j, c = 0;
t_stat stat = SCPE_OK;
t_bool active_command = FALSE;
int32 steps = 0;
t_bool was_active_command = (sim_rem_cmd_active_line != -1);
t_bool got_command;
t_bool close_session = FALSE;
TMLN *lp;
char cbuf[4*CBUFSIZE], gbuf[CBUFSIZE], *argv[1] = {NULL};
CONST char *cptr;
CTAB *cmdp = NULL;
CTAB *basecmdp = NULL;
uint32 read_start_time = 0;
tmxr_poll_rx (&sim_rem_con_tmxr); /* poll input */
for (i=(was_active_command ? sim_rem_cmd_active_line : 0);
(i < sim_rem_con_tmxr.lines) && (!active_command);
i++) {
t_bool master_session = (sim_rem_master_mode && (i == 0));
lp = &sim_rem_con_tmxr.ldsc[i];
if (!lp->conn)
continue;
if (master_session && !sim_rem_master_was_connected) {
tmxr_linemsgf (lp, "\nMaster Mode Session\r\n");
tmxr_send_buffered_data (lp); /* flush any buffered data */
}
sim_rem_master_was_connected |= master_session; /* Remember if master ever connected */
stat = SCPE_OK;
if ((was_active_command) ||
(master_session && !sim_rem_single_mode[i])) {
if (was_active_command) {
sim_rem_cmd_active_line = -1; /* Done with active command */
if (!sim_rem_active_command) { /* STEP command? */
stat = SCPE_STEP;
_sim_rem_message ("STEP", stat); /* produce a STEP complete message */
}
_sim_rem_log_out (lp);
sim_rem_active_command = NULL; /* Restart loop to process available input */
was_active_command = FALSE;
i = -1;
continue;
}
else {
sim_is_running = 0;
sim_stop_timer_services ();
for (j=0; j < sim_rem_con_tmxr.lines; j++) {
TMLN *lpj = &sim_rem_con_tmxr.ldsc[j];
if ((i == j) || (!lpj->conn))
continue;
tmxr_linemsgf (lpj, "\nRemote Master Console(%s) Entering Commands\n", lp->ipad);
tmxr_send_buffered_data (lpj); /* flush any buffered data */
}
lp = &sim_rem_con_tmxr.ldsc[i];
}
}
else {
c = tmxr_getc_ln (lp);
if (!(TMXR_VALID & c))
continue;
c = c & ~TMXR_VALID;
if (sim_rem_single_mode[i]) {
if (c == sim_int_char) { /* ^E (the interrupt character) must start continue mode console interaction */
sim_rem_single_mode[i] = FALSE; /* enter multi command mode */
sim_is_running = 0;
sim_stop_timer_services ();
stat = SCPE_STOP;
_sim_rem_message ("RUN", stat);
_sim_rem_log_out (lp);
for (j=0; j < sim_rem_con_tmxr.lines; j++) {
TMLN *lpj = &sim_rem_con_tmxr.ldsc[j];
if ((i == j) || (!lpj->conn))
continue;
tmxr_linemsgf (lpj, "\nRemote Console %d(%s) Entering Commands\n", i, lp->ipad);
tmxr_send_buffered_data (lpj); /* flush any buffered data */
}
lp = &sim_rem_con_tmxr.ldsc[i];
if (!master_session)
tmxr_linemsg (lp, "\r\nSimulator paused.\r\n");
if (!master_session && sim_rem_read_timeouts[i]) {
tmxr_linemsgf (lp, "Simulation will resume automatically if input is not received in %d seconds\n", sim_rem_read_timeouts[i]);
tmxr_linemsgf (lp, "\r\n");
tmxr_send_buffered_data (lp); /* flush any buffered data */
}
}
else {
if ((sim_rem_buf_ptr[i] == 0) && /* At beginning of input line */
((c == '\n') || /* Ignore bare LF between commands (Microsoft Telnet bug) */
(c == '\r'))) /* Ignore empty commands */
continue;
if ((c == '\004') || (c == '\032')) { /* EOF character (^D or ^Z) ? */
tmxr_linemsgf (lp, "\r\nGoodbye\r\n");
tmxr_send_buffered_data (lp); /* flush any buffered data */
tmxr_reset_ln (lp);
continue;
}
if (sim_rem_buf_ptr[i] == 0) {
/* we just picked up the first character on a command line */
if (!master_session)
tmxr_linemsgf (lp, "\r\n%s", sim_prompt);
else
tmxr_linemsgf (lp, "\r\n%s", sim_is_running ? "SIM> " : "sim> ");
sim_debug (DBG_XMT, &sim_remote_console, "Prompt Written: %s\n", sim_is_running ? "SIM> " : "sim> ");
if (!tmxr_input_pending_ln (lp))
tmxr_send_buffered_data (lp); /* flush any buffered data */
}
}
}
}
got_command = FALSE;
while (1) {
if (stat == SCPE_EXIT)
return stat|SCPE_NOMESSAGE;
if (!sim_rem_single_mode[i]) {
read_start_time = sim_os_msec();
if (master_session)
tmxr_linemsg (lp, "sim> ");
else
tmxr_linemsg (lp, sim_prompt);
tmxr_send_buffered_data (lp); /* flush any buffered data */
}
do {
if (!sim_rem_single_mode[i]) {
c = tmxr_getc_ln (lp);
if (!(TMXR_VALID & c)) {
tmxr_send_buffered_data (lp); /* flush any buffered data */
if (!master_session &&
sim_rem_read_timeouts[i] &&
((sim_os_msec() - read_start_time)/1000 >= sim_rem_read_timeouts[i])) {
while (sim_rem_buf_ptr[i] > 0) {/* Erase current input line */
tmxr_linemsg (lp, "\b \b");
--sim_rem_buf_ptr[i];
}
if (sim_rem_buf_ptr[i]+80 >= sim_rem_buf_size[i]) {
sim_rem_buf_size[i] += 1024;
sim_rem_buf[i] = (char *)realloc (sim_rem_buf[i], sim_rem_buf_size[i]);
}
strcpy (sim_rem_buf[i], "CONTINUE ! Automatic continue due to timeout");
tmxr_linemsgf (lp, "%s\n", sim_rem_buf[i]);
got_command = TRUE;
break;
}
sim_os_ms_sleep (50);
tmxr_poll_rx (&sim_rem_con_tmxr); /* poll input */
if (!lp->conn) { /* if connection lost? */
sim_rem_single_mode[i] = TRUE; /* No longer multi-command more */
break; /* done waiting */
}
continue;
}
read_start_time = sim_os_msec();
c = c & ~TMXR_VALID;
}
switch (c) {
case 0: /* no data */
break;
case '\b': /* Backspace */
case 127: /* Rubout */
if (sim_rem_buf_ptr[i] > 0) {
tmxr_linemsg (lp, "\b \b");
--sim_rem_buf_ptr[i];
}
break;
case 27: /* escape */
case 21: /* ^U */
while (sim_rem_buf_ptr[i] > 0) {
tmxr_linemsg (lp, "\b \b");
--sim_rem_buf_ptr[i];
}
break;
case '\n':
if (sim_rem_buf_ptr[i] == 0)
break;
case '\r':
tmxr_linemsg (lp, "\r\n");
if (sim_rem_buf_ptr[i]+1 >= sim_rem_buf_size[i]) {
sim_rem_buf_size[i] += 1024;
sim_rem_buf[i] = (char *)realloc (sim_rem_buf[i], sim_rem_buf_size[i]);
}
sim_rem_buf[i][sim_rem_buf_ptr[i]++] = '\0';
sim_debug (DBG_RCV, &sim_remote_console, "Got Command (%d bytes still in buffer): %s\n", tmxr_input_pending_ln (lp), sim_rem_buf[i]);
got_command = TRUE;
break;
case '\004': /* EOF (^D) */
case '\032': /* EOF (^Z) */
while (sim_rem_buf_ptr[i] > 0) { /* Erase current input line */
tmxr_linemsg (lp, "\b \b");
--sim_rem_buf_ptr[i];
}
if (!sim_rem_single_mode[i]) {
if (sim_rem_buf_ptr[i]+80 >= sim_rem_buf_size[i]) {
sim_rem_buf_size[i] += 1024;
sim_rem_buf[i] = (char *)realloc (sim_rem_buf[i], sim_rem_buf_size[i]);
}
strcpy (sim_rem_buf[i], "CONTINUE ! Automatic continue before close");
tmxr_linemsgf (lp, "%s\n", sim_rem_buf[i]);
got_command = TRUE;
}
close_session = TRUE;
break;
default:
tmxr_putc_ln (lp, c);
if (sim_rem_buf_ptr[i]+2 >= sim_rem_buf_size[i]) {
sim_rem_buf_size[i] += 1024;
sim_rem_buf[i] = (char *)realloc (sim_rem_buf[i], sim_rem_buf_size[i]);
}
sim_rem_buf[i][sim_rem_buf_ptr[i]++] = (char)c;
sim_rem_buf[i][sim_rem_buf_ptr[i]] = '\0';
if (((size_t)sim_rem_buf_ptr[i]) >= sizeof(cbuf))
got_command = TRUE; /* command too long */
break;
}
c = 0;
if ((!got_command) && (sim_rem_single_mode[i]) && (tmxr_input_pending_ln (lp))) {
c = tmxr_getc_ln (lp);
c = c & ~TMXR_VALID;
}
} while ((!got_command) && ((!sim_rem_single_mode[i]) || c));
if (!tmxr_input_pending_ln (lp))
tmxr_send_buffered_data (lp); /* flush any buffered data */
if ((sim_rem_single_mode[i]) && !got_command) {
break;
}
sim_printf ("Remote Console Command from %s> %s\r\n", lp->ipad, sim_rem_buf[i]);
got_command = FALSE;
if (strlen(sim_rem_buf[i]) >= sizeof(cbuf)) {
sim_printf ("\r\nLine too long. Ignored. Continuing Simulator execution\r\n");
tmxr_linemsgf (lp, "\nLine too long. Ignored. Continuing Simulator execution\n");
tmxr_send_buffered_data (lp); /* try to flush any buffered data */
break;
}
strcpy (cbuf, sim_rem_buf[i]);
sim_rem_buf_ptr[i] = 0;
sim_rem_buf[i][sim_rem_buf_ptr[i]] = '\0';
while (isspace(cbuf[0]))
memmove (cbuf, cbuf+1, strlen(cbuf+1)+1); /* skip leading whitespace */
if (cbuf[0] == '\0') {
if (sim_rem_single_mode[i]) {
sim_rem_single_mode[i] = FALSE;
break;
}
else
continue;
}
strcpy (sim_rem_command_buf, cbuf);
sim_sub_args (cbuf, sizeof(cbuf), argv);
cptr = cbuf;
cptr = get_glyph (cptr, gbuf, 0); /* get command glyph */
sim_switches = 0; /* init switches */
sim_rem_active_number = i;
if (!sim_log) { /* Not currently logging? */
int32 save_quiet = sim_quiet;
sim_quiet = 1;
sprintf (sim_rem_con_temp_name, "sim_remote_console_%d.temporary_log", (int)getpid());
sim_set_logon (0, sim_rem_con_temp_name);
sim_quiet = save_quiet;
sim_log_temp = TRUE;
}
sim_rem_cmd_log_start = sim_ftell (sim_log);
basecmdp = find_cmd (gbuf); /* validate basic command */
if (basecmdp == NULL) {
if ((gbuf[0] == ';') || (gbuf[0] == '#')) { /* ignore comment */
sim_rem_cmd_active_line = i;
was_active_command = TRUE;
sim_rem_active_command = &allowed_single_remote_cmds[0];/* Dummy */
i = i - 1;
break;
}
else
stat = SCPE_UNK;
}
else {
if ((cmdp = find_ctab (sim_rem_single_mode[i] ? allowed_single_remote_cmds : (master_session ? allowed_master_remote_cmds : allowed_remote_cmds), gbuf))) {/* lookup command */
if (cmdp->action == &x_continue_cmd)
stat = SCPE_OK;
else {
if (cmdp->action == &exit_cmd)
return SCPE_EXIT;
if (cmdp->action == &x_step_cmd) {
steps = 1; /* default of 1 instruction */
stat = SCPE_OK;
if (*cptr != 0) { /* argument? */
cptr = get_glyph (cptr, gbuf, 0);/* get next glyph */
if (*cptr != 0) /* should be end */
stat = SCPE_2MARG;
else {
steps = (int32) get_uint (gbuf, 10, INT_MAX, &stat);
if ((stat != SCPE_OK) || (steps <= 0)) /* error? */
stat = SCPE_ARG;
}
}
if (stat != SCPE_OK)
cmdp = NULL;
}