-
Notifications
You must be signed in to change notification settings - Fork 0
/
sim_console.c
4420 lines (3940 loc) · 162 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
sim_set_noconsole_port Enable automatic WRU console polling
sim_set_stable_registers_state Declare that all registers are always stable
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
#ifndef MIN
#define MIN(a,b) (((a) <= (b)) ? (a) : (b))
#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_fd_isatty (int fd);
static t_stat sim_set_rem_telnet (int32 flag, CONST char *cptr);
static t_stat sim_set_rem_bufsize (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_DBGINT 3
#define KMAP_MASK 0377
#define KMAP_NZ 0400
int32 sim_int_char = 005; /* interrupt character */
int32 sim_dbg_int_char = 0; /* SIGINT char under debugger */
static t_bool sigint_message_issued = FALSE;
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
t_bool sim_signaled_int_char /* WRU character detected by signal while running */
#if defined (_WIN32) || defined (_VMS) || defined (__CYGWIN__) || (defined(USE_SIM_VIDEO) && defined(HAVE_LIBSDL))
= FALSE;
#else
= TRUE;
#endif
uint32 sim_last_poll_kbd_time; /* time when sim_poll_kbd was called */
extern TMLN *sim_oline; /* global output socket */
static uint32 sim_con_pos; /* console character output count */
static t_stat sim_con_poll_svc (UNIT *uptr); /* console connection poll routine */
static t_stat sim_con_reset (DEVICE *dptr); /* console reset routine */
static t_stat sim_con_attach (UNIT *uptr, CONST char *ptr); /* console attach routine (save,restore) */
static t_stat sim_con_detach (UNIT *uptr); /* console detach routine (save,restore) */
UNIT sim_con_units[2] = {{ UDATA (&sim_con_poll_svc, UNIT_ATTABLE, 0)}}; /* console connection unit */
#define sim_con_unit sim_con_units[0]
/* 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_CON TMXR_DBG_CON /* display connection activity */
#define DBG_EXP 0x00000001 /* Expect match activity */
#define DBG_SND 0x00000002 /* Send (Inject) data activity */
static DEBTAB sim_con_debug[] = {
{"TRC", DBG_TRC, "routine calls"},
{"XMT", DBG_XMT, "Transmitted Data"},
{"RCV", DBG_RCV, "Received Data"},
{"RET", DBG_RET, "Returned Received Data"},
{"ASY", DBG_ASY, "asynchronous activity"},
{"CON", DBG_CON, "connection activity"},
{"EXP", DBG_EXP, "Expect match activity"},
{"SND", DBG_SND, "Send (Inject) data activity"},
{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") },
{ DRDATAD (CONSOLE_POS, sim_con_pos, 32, "character output count") },
{ 0 },
};
static MTAB sim_con_mod[] = {
{ 0 },
};
static const char *sim_con_telnet_description (DEVICE *dptr)
{
return "Console telnet support";
}
DEVICE sim_con_telnet = {
"CON-TELNET", sim_con_units, sim_con_reg, sim_con_mod,
2, 0, 0, 0, 0, 0,
NULL, NULL, sim_con_reset, NULL, sim_con_attach, sim_con_detach,
NULL, DEV_DEBUG | DEV_NOSAVE, 0, sim_con_debug,
NULL, NULL, NULL, NULL, NULL, sim_con_telnet_description};
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 = {0, &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;
}
static t_bool sim_con_stable_registers = FALSE;
/* Enable automatic WRU console polling */
t_stat sim_set_stable_registers_state (void)
{
sim_con_stable_registers = TRUE;
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)
{
dptr->units[1].flags = UNIT_DIS;
return sim_con_poll_svc (&dptr->units[0]); /* establish polling as needed */
}
/* Console Attach/Detach - only used indirectly in restore */
static t_stat sim_con_attach (UNIT *uptr, CONST char *ptr)
{
return tmxr_attach (&sim_con_tmxr, &sim_con_unit, ptr);
}
static t_stat sim_con_detach (UNIT *uptr)
{
return sim_set_notelnet (0, NULL);
}
/* 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 },
{ "DBGINT", &sim_set_kmap, KMAP_DBGINT | 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 },
{ "BUFFERSIZE", &sim_set_rem_bufsize, 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 },
#if (defined(__GNUC__) && !defined(__OPTIMIZE__) && !defined(_WIN32)) /* Debug build? */
{ "DBGINT", &sim_show_kmap, KMAP_DBGINT },
#endif
{ "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, -1 },
{ "INPUT", &sim_show_cons_send_input, 0 },
{ "RESPONSE", &sim_show_cons_send_input, -1 },
{ "DELAY", &sim_show_cons_expect, -1 },
{ 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,
&sim_dbg_int_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++)
if (show_con_tab[i].arg != -1)
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, NULL);
else return SCPE_NOPARAM;
}
return SCPE_OK;
}
#define MAX_REMOTE_SESSIONS 40 /* Arbitrary Session Limit */
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_repeat_svc (UNIT *uptr); /* remote auto repeat command console timing routine */
t_stat sim_rem_con_smp_collect_svc (UNIT *uptr); /* remote remote register data sampling routine */
t_stat sim_rem_con_reset (DEVICE *dptr); /* remote console reset routine */
#define rem_con_poll_unit (&sim_remote_console.units[0])
#define rem_con_data_unit (&sim_remote_console.units[1])
#define REM_CON_BASE_UNITS 2
#define rem_con_repeat_units (&sim_remote_console.units[REM_CON_BASE_UNITS])
#define rem_con_smp_smpl_units (&sim_remote_console.units[REM_CON_BASE_UNITS+sim_rem_con_tmxr.lines])
#define DBG_MOD 0x00000004 /* Remote Console Mode activities */
#define DBG_REP 0x00000008 /* Remote Console Repeat activities */
#define DBG_SAM 0x00000010 /* Remote Console Sample activities */
#define DBG_CMD 0x00000020 /* Remote Console Command activities */
DEBTAB sim_rem_con_debug[] = {
{"TRC", DBG_TRC, "routine calls"},
{"XMT", DBG_XMT, "Transmitted Data"},
{"RCV", DBG_RCV, "Received Data"},
{"CON", DBG_CON, "connection activity"},
{"CMD", DBG_CMD, "Remote Console Command activity"},
{"MODE", DBG_MOD, "Remote Console Mode activity"},
{"REPEAT", DBG_REP, "Remote Console Repeat activity"},
{"SAMPLE", DBG_SAM, "Remote Console Sample activity"},
{0}
};
MTAB sim_rem_con_mod[] = {
{ 0 },
};
static const char *sim_rem_con_description (DEVICE *dptr)
{
return "Remote Console Facility";
}
DEVICE sim_remote_console = {
"REM-CON", NULL, NULL, sim_rem_con_mod,
0, 0, 0, 0, 0, 0,
NULL, NULL, sim_rem_con_reset, NULL, NULL, NULL,
NULL, DEV_DEBUG | DEV_NOSAVE, 0, sim_rem_con_debug,
NULL, NULL, NULL, NULL, NULL, sim_rem_con_description};
typedef struct BITSAMPLE BITSAMPLE;
struct BITSAMPLE {
int tot; /* total of all values */
int ptr; /* pointer to next value cell */
int depth; /* number of values */
int *vals; /* values */
};
typedef struct BITSAMPLE_REG BITSAMPLE_REG;
struct BITSAMPLE_REG {
REG *reg; /* Register to be sampled */
uint32 idx; /* Register index */
t_bool indirect; /* Register value points at memory */
DEVICE *dptr; /* Device register is part of */
UNIT *uptr; /* Unit Register is related to */
uint32 width; /* number of bits to sample */
BITSAMPLE *bits;
};
typedef struct REMOTE REMOTE;
struct REMOTE {
int32 buf_size;
int32 buf_ptr;
char *buf;
char *act_buf;
size_t act_buf_size;
char *act;
t_bool single_mode;
uint32 read_timeout;
int line; /* remote console line number */
TMLN *lp; /* mux line/socket for remote session */
UNIT *uptr; /* remote console unit */
uint32 repeat_interval; /* usecs between repeat execution */
t_bool repeat_pending; /* repeat delivery pending */
char *repeat_action; /* command(s) to repeatedly execute */
int smp_sample_interval; /* cycles between samples */
int smp_sample_dither_pct; /* dithering of cycles interval */
uint32 smp_reg_count; /* sample register count */
BITSAMPLE_REG *smp_regs; /* registers being sampled */
};
REMOTE *sim_rem_consoles = NULL;
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 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 */
static t_stat sim_rem_sample_output (FILE *st, int32 line)
{
REMOTE *rem = &sim_rem_consoles[line];
uint32 reg;
if (rem->smp_reg_count == 0) {
fprintf (st, "Samples are not being collected\n");
return SCPE_OK;
}
for (reg = 0; reg < rem->smp_reg_count; reg++) {
uint32 bit;
if (rem->smp_regs[reg].reg->depth > 1)
fprintf (st, "}%s %s[%d] %s %d:", rem->smp_regs[reg].dptr->name, rem->smp_regs[reg].reg->name, rem->smp_regs[reg].idx, rem->smp_regs[reg].indirect ? " -I" : "", rem->smp_regs[reg].bits[0].depth);
else
fprintf (st, "}%s %s%s %d:", rem->smp_regs[reg].dptr->name, rem->smp_regs[reg].reg->name, rem->smp_regs[reg].indirect ? " -I" : "", rem->smp_regs[reg].bits[0].depth);
for (bit = 0; bit < rem->smp_regs[reg].width; bit++)
fprintf (st, "%s%d", (bit != 0) ? "," : "", rem->smp_regs[reg].bits[bit].tot);
fprintf (st, "\n");
}
return SCPE_OK;
}
/* 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;
REMOTE *rem;
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", rem_con_poll_unit->filename);
fprintf (st, "Remote Console Per Command Output buffer size: %d bytes\n", sim_rem_con_tmxr.buffered);
}
for (i=connections=0; i<sim_rem_con_tmxr.lines; i++) {
rem = &sim_rem_consoles[i];
if (!rem->lp->conn)
continue;
++connections;
if (connections == 1)
fprintf (st, "Remote Console Connections:\n");
tmxr_fconns (st, rem->lp, i);
if (rem->read_timeout != sim_rem_read_timeout) {
if (rem->read_timeout)
fprintf (st, "Remote Console Input on connection %d automatically continues after %d seconds\n", i, rem->read_timeout);
else
fprintf (st, "Remote Console Input on connection %d does not continue automatically\n", i);
}
if (rem->repeat_action) {
fprintf (st, "The Command: %s\n", rem->repeat_action);
fprintf (st, " is repeated every %s\n", sim_fmt_secs (rem->repeat_interval / 1000000.0));
}
if (rem->smp_reg_count) {
uint32 reg;
DEVICE *dptr = NULL;
if (rem->smp_sample_dither_pct)
fprintf (st, "Register Bit Sampling is occurring every %d %s (dithered %d percent)\n", rem->smp_sample_interval, sim_vm_interval_units, rem->smp_sample_dither_pct);
else
fprintf (st, "Register Bit Sampling is occurring every %d %s\n", rem->smp_sample_interval, sim_vm_interval_units);
fprintf (st, " Registers being sampled are: ");
for (reg = 0; reg < rem->smp_reg_count; reg++) {
if (rem->smp_regs[reg].indirect)
fprintf (st, " indirect ");
if (dptr != rem->smp_regs[reg].dptr)
fprintf (st, "%s ", rem->smp_regs[reg].dptr->name);
if (rem->smp_regs[reg].reg->depth > 1)
fprintf (st, "%s[%d]%s", rem->smp_regs[reg].reg->name, rem->smp_regs[reg].idx, ((reg + 1) < rem->smp_reg_count) ? ", " : "");
else
fprintf (st, "%s%s", rem->smp_regs[reg].reg->name, ((reg + 1) < rem->smp_reg_count) ? ", " : "");
dptr = rem->smp_regs[reg].dptr;
}
fprintf (st, "\n");
if (sim_switches & SWMASK ('D'))
sim_rem_sample_output (st, rem->line);
}
}
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 */
REMOTE *rem = &sim_rem_consoles[c];
TMLN *lp = rem->lp;
char wru_name[8];
sim_activate_after(rem_con_data_unit, 1000000); /* start data poll after 1 second */
lp->rcve = 1; /* rcv enabled */
rem->buf_ptr = 0; /* start with empty command buffer */
rem->single_mode = TRUE; /* start in single command mode */
rem->read_timeout = 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? */
rem->single_mode = 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 1+SCPE_IERR; /* This routine should never be called */
}
static t_stat x_repeat_cmd (int32 flag, CONST char *cptr)
{
return 2+SCPE_IERR; /* This routine should never be called */
}
static t_stat x_collect_cmd (int32 flag, CONST char *cptr)
{
return 3+SCPE_IERR; /* This routine should never be called */
}
static t_stat x_sampleout_cmd (int32 flag, CONST char *cptr)
{
return 4+SCPE_IERR; /* This routine should never be called */
}
static t_stat x_execute_cmd (int32 flag, CONST char *cptr)
{
return 5+SCPE_IERR; /* This routine should never be called */
}
static t_stat x_step_cmd (int32 flag, CONST char *cptr)
{
return 6+SCPE_IERR; /* This routine should never be called */
}
static t_stat x_run_cmd (int32 flag, CONST char *cptr)
{
return 7+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 },
{ "REPEAT", &x_repeat_cmd, 0 },
{ "COLLECT", &x_collect_cmd, 0 },
{ "SAMPLEOUT",&x_sampleout_cmd, 0 },
{ "PWD", &pwd_cmd, 0 },
{ "SAVE", &save_cmd, 0 },
{ "DIR", &dir_cmd, 0 },
{ "LS", &dir_cmd, 0 },
{ "ECHO", &echo_cmd, 0 },
{ "ECHOF", &echof_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 },
{ "REPEAT", &x_repeat_cmd, 0 },
{ "COLLECT", &x_collect_cmd, 0 },
{ "SAMPLEOUT",&x_sampleout_cmd, 0 },
{ "EXECUTE", &x_execute_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 },
{ "ECHOF", &echof_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 },
{ "EXPECT", &expect_cmd, 1 },
{ "NOEXPECT", &expect_cmd, 0 },
{ "DEBUG", &debug_cmd, 1 },
{ "NODEBUG", &debug_cmd, 0 },
{ "SEND", &send_cmd, 0 },
{ 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 },
{ "REPEAT", &x_repeat_cmd, 0 },
{ "COLLECT", &x_collect_cmd, 0 },
{ "SAMPLEOUT",&x_sampleout_cmd, 0 },
{ "EXECUTE", &x_execute_cmd, 0 },
{ "PWD", &pwd_cmd, 0 },
{ "DIR", &dir_cmd, 0 },
{ "LS", &dir_cmd, 0 },
{ "ECHO", &echo_cmd, 0 },
{ "ECHOF", &echof_cmd, 0 },
{ "SHOW", &show_cmd, 0 },
{ "DEBUG", &debug_cmd, 1 },
{ "NODEBUG", &debug_cmd, 0 },
{ "HELP", &x_help_cmd, 0 },
{ NULL, NULL }
};
static CTAB remote_only_cmds[] = {
{ "REPEAT", &x_repeat_cmd, 0 },
{ "COLLECT", &x_collect_cmd, 0 },
{ "SAMPLEOUT",&x_sampleout_cmd, 0 },
{ "EXECUTE", &x_execute_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];
REMOTE *rem = &sim_rem_consoles[(int)(lp - sim_rem_con_tmxr.ldsc)];
if ((!sim_oline) && (sim_log)) {
fflush (sim_log);
(void)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);
}
sim_oline = NULL;
if ((rem->act == NULL) &&
(!tmxr_input_pending_ln (lp))) {
int32 unwritten;
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;
strlcpy (cbuf, sim_rem_command_buf, sizeof (cbuf));
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 */
if (!sim_processing_event)
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);
if (sim_vm_post != NULL) /* optionally let the simulator know */
(*sim_vm_post) (TRUE); /* something might have changed */
if (!sim_processing_event) {
sim_ttrun (); /* set console mode */
sim_cancel (rem_con_data_unit); /* force immediate activation of sim_rem_con_data_svc */
sim_activate (rem_con_data_unit, -1);
}
sim_switches = saved_switches; /* restore original switches */
}
/* Clear pending actions */
static char *sim_rem_clract (int32 line)
{
REMOTE *rem = &sim_rem_consoles[line];
tmxr_send_buffered_data (rem->lp); /* flush any buffered data */
return rem->act = NULL;
}
/* Set up pending actions */
static void sim_rem_setact (int32 line, const char *action)
{
if (action) {
size_t act_size = strlen (action) + 1;
REMOTE *rem = &sim_rem_consoles[line];
if (act_size > rem->act_buf_size) { /* expand buffer if necessary */
rem->act_buf = (char *)realloc (rem->act_buf, act_size);
rem->act_buf_size = act_size;
}
strcpy (rem->act_buf, action); /* populate buffer */
rem->act = rem->act_buf; /* start at beginning of buffer */
}
else
sim_rem_clract (line);
}
/* Get next pending action, if any */
static char *sim_rem_getact (int32 line, char *buf, int32 size)
{
char *ep;
size_t lnt;
REMOTE *rem = &sim_rem_consoles[line];
if (rem->act == NULL) /* any action? */
return NULL;
while (sim_isspace (*rem->act)) /* skip spaces */
rem->act++;
if (*rem->act == 0) /* now empty? */
return sim_rem_clract (line);
ep = strpbrk (rem->act, ";\"'"); /* search for a semicolon or single or double quote */
if ((ep != NULL) && (*ep != ';')) { /* if a quoted string is present */
char quote = *ep++; /* then save the opening quotation mark */
while (ep [0] != '\0' && ep [0] != quote) /* while characters remain within the quotes */
if (ep [0] == '\\' && ep [1] == quote) /* if an escaped quote sequence follows */
ep = ep + 2; /* then skip over the pair */
else /* otherwise */
ep = ep + 1; /* skip the non-quote character */
ep = strchr (ep, ';'); /* the next semicolon is outside the quotes if it exists */
}
if (ep != NULL) { /* if a semicolon is present */
lnt = ep - rem->act; /* cmd length */
memcpy (buf, rem->act, lnt + 1); /* copy with ; */
buf[lnt] = 0; /* erase ; */
rem->act += lnt + 1; /* adv ptr */
}
else {
strlcpy (buf, rem->act, size); /* copy action */
rem->act += strlen (rem->act); /* adv ptr to end */
sim_rem_clract (line);
}
return buf;
}