root/src/dps8/dps8_console.c

/* [previous][next][first][last][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. ta_flush
  2. ta_push
  3. ta_peek
  4. ta_get
  5. opc_reset
  6. check_attn_key
  7. console_init
  8. console_exit
  9. opc_autoinput_set
  10. clear_opc_autoinput
  11. add_opc_autoinput
  12. opc_autoinput_show
  13. console_attn
  14. console_attn_idx
  15. newlineOff
  16. newlineOn
  17. handleRCP
  18. sendConsole
  19. consoleProcessIdx
  20. consoleProcess
  21. opc_iom_cmd
  22. opc_svc
  23. opc_show_nunits
  24. opc_set_nunits
  25. opc_set_config
  26. opc_show_config
  27. opc_show_device_name
  28. opc_set_device_name
  29. opc_set_console_port
  30. opc_show_console_port
  31. opc_set_console_address
  32. opc_show_console_address
  33. opc_set_console_pw
  34. opc_show_console_pw
  35. console_putstr
  36. consolePutchar0
  37. console_putchar
  38. consoleConnectPrompt
  39. startRemoteConsole

   1 /*
   2  * vim: filetype=c:tabstop=4:ai:expandtab
   3  * SPDX-License-Identifier: ICU
   4  * SPDX-License-Identifier: Multics
   5  * scspell-id: 6421764a-f62d-11ec-b542-80ee73e9b8e7
   6  *
   7  * ---------------------------------------------------------------------------
   8  *
   9  * Copyright (c) 2007-2013 Michael Mondy
  10  * Copyright (c) 2012-2016 Harry Reed
  11  * Copyright (c) 2013-2016 Charles Anthony
  12  * Copyright (c) 2021-2025 The DPS8M Development Team
  13  *
  14  * This software is made available under the terms of the ICU License.
  15  * See the LICENSE.md file at the top-level directory of this distribution.
  16  *
  17  * ---------------------------------------------------------------------------
  18  *
  19  * This source file may contain code comments that adapt, include, and/or
  20  * incorporate Multics program code and/or documentation distributed under
  21  * the Multics License.  In the event of any discrepancy between code
  22  * comments herein and the original Multics materials, the original Multics
  23  * materials should be considered authoritative unless otherwise noted.
  24  * For more details and historical background, see the LICENSE.md file at
  25  * the top-level directory of this distribution.
  26  *
  27  * ---------------------------------------------------------------------------
  28  */
  29 
  30 #include <stdio.h>
  31 #include <unistd.h>
  32 #include <signal.h>
  33 #if !defined(__MINGW64__) && !defined(__MINGW32__)
  34 # include <termios.h>
  35 #endif /* if !defined(__MINGW64__) && !defined(__MINGW32__) */
  36 #include <ctype.h>
  37 
  38 #include "dps8.h"
  39 #include "dps8_iom.h"
  40 #include "dps8_sys.h"
  41 #include "dps8_console.h"
  42 #include "dps8_cable.h"
  43 #include "dps8_cpu.h"
  44 #include "dps8_faults.h"
  45 #include "dps8_scu.h"
  46 #include "dps8_mt.h"  // attachTape
  47 #include "dps8_disk.h"  // attachDisk
  48 #include "dps8_utils.h"
  49 #if defined(LOCKLESS)
  50 # include "threadz.h"
  51 #endif /* if defined(LOCKLESS) */
  52 
  53 #include "libtelnet.h"
  54 #if defined(CONSOLE_FIX)
  55 # include "threadz.h"
  56 #endif /* if defined(CONSOLE_FIX) */
  57 
  58 #if defined(SIM_NAME)
  59 # undef SIM_NAME
  60 #endif /* if defined(SIM_NAME) */
  61 #define SIM_NAME "DPS8M"
  62 
  63 #define DBG_CTR 1
  64 #define ASSUME0 0
  65 
  66 #if defined(FREE)
  67 # undef FREE
  68 #endif /* if defined(FREE) */
  69 #define FREE(p) do  \
  70   {                 \
  71     free((p));      \
  72     (p) = NULL;     \
  73   } while(0)
  74 
  75 // config switch -- The bootload console has a 30-second timer mechanism. When
  76 // reading from the console, if no character is typed within 30 seconds, the
  77 // read operation is terminated. The timer is controlled by an enable switch,
  78 // must be set to enabled during Multics and BCE
  79 
  80 static t_stat opc_reset (DEVICE * dptr);
  81 static t_stat opc_show_nunits (FILE *st, UNIT *uptr, int val,
  82                                const void *desc);
  83 static t_stat opc_set_nunits (UNIT * uptr, int32 value, const char * cptr,
  84                               void * desc);
  85 static t_stat opc_autoinput_set (UNIT *uptr, int32 val, const char *cptr,
  86                                  void *desc);
  87 static t_stat opc_autoinput_show (FILE *st, UNIT *uptr, int val,
  88                                   const void *desc);
  89 static t_stat opc_set_config (UNUSED UNIT *  uptr, UNUSED int32 value,
  90                               const char * cptr, UNUSED void * desc);
  91 static t_stat opc_show_config (UNUSED FILE * st, UNUSED UNIT * uptr,
  92                                UNUSED int  val, UNUSED const void * desc);
  93 static t_stat opc_set_console_port (UNIT * uptr, UNUSED int32 value,
  94                                     const char * cptr, UNUSED void * desc);
  95 static t_stat opc_show_console_port (UNUSED FILE * st, UNIT * uptr,
  96                                        UNUSED int val, UNUSED const void * desc);
  97 static t_stat opc_set_console_address (UNIT * uptr, UNUSED int32 value,
  98                                     const char * cptr, UNUSED void * desc);
  99 static t_stat opc_show_console_address (UNUSED FILE * st, UNIT * uptr,
 100                                        UNUSED int val, UNUSED const void * desc);
 101 static t_stat opc_set_console_pw (UNIT * uptr, UNUSED int32 value,
 102                                     const char * cptr, UNUSED void * desc);
 103 static t_stat opc_show_console_pw (UNUSED FILE * st, UNIT * uptr,
 104                                        UNUSED int val, UNUSED const void * desc);
 105 static t_stat opc_set_device_name (UNIT * uptr, UNUSED int32 value,
 106                                    const char * cptr, UNUSED void * desc);
 107 static t_stat opc_show_device_name (UNUSED FILE * st, UNIT * uptr,
 108                                     UNUSED int val, UNUSED const void * desc);
 109 
 110 static MTAB opc_mtab[] =
 111   {
 112     {
 113        MTAB_unit_nouc,     /* Mask          */
 114        0,                  /* Match         */
 115        "AUTOINPUT",        /* Print string  */
 116        "AUTOINPUT",        /* Match pstring */
 117        opc_autoinput_set,
 118        opc_autoinput_show,
 119        NULL,
 120        NULL
 121     },
 122 
 123     {
 124       MTAB_dev_valr,                        /* Mask               */
 125       0,                                    /* Match              */
 126       "NUNITS",                             /* Print string       */
 127       "NUNITS",                             /* Match string       */
 128       opc_set_nunits,                       /* Validation routine */
 129       opc_show_nunits,                      /* Display routine    */
 130       "Number of OPC units in the system",  /* Value descriptor   */
 131       NULL                                  /* Help               */
 132     },
 133 
 134     {
 135       MTAB_unit_uc,                   /* Mask               */
 136       0,                              /* Match              */
 137       (char *) "CONFIG",              /* Print string       */
 138       (char *) "CONFIG",              /* Match string       */
 139       opc_set_config,                 /* Validation routine */
 140       opc_show_config,                /* Display routine    */
 141       NULL,                           /* Value descriptor   */
 142       NULL,                           /* Help               */
 143     },
 144     {
 145       MTAB_XTD | MTAB_VUN | \
 146       MTAB_VALR | MTAB_NC,            /* Mask               */
 147       0,                              /* Match              */
 148       "NAME",                         /* Print string       */
 149       "NAME",                         /* Match string       */
 150       opc_set_device_name,            /* Validation routine */
 151       opc_show_device_name,           /* Display routine    */
 152       "Set the device name",          /* Value descriptor   */
 153       NULL                            /* Help               */
 154     },
 155 
 156     {
 157       MTAB_unit_valr_nouc,            /* Mask               */
 158       0,                              /* Match              */
 159       "PORT",                         /* Print string       */
 160       "PORT",                         /* Match string       */
 161       opc_set_console_port,           /* validation routine */
 162       opc_show_console_port,          /* Display routine    */
 163       "Set the console port number",  /* Value descriptor   */
 164       NULL                            /* Help               */
 165     },
 166 
 167     {
 168       MTAB_unit_valr_nouc,            /* Mask               */
 169       0,                              /* Match              */
 170       "ADDRESS",                      /* Print string       */
 171       "ADDRESS",                      /* Match string       */
 172       opc_set_console_address,        /* Validation routine */
 173       opc_show_console_address,       /* Display routine    */
 174       "Set the console IP Address",   /* Value descriptor   */
 175       NULL                            /* Help               */
 176     },
 177 
 178     {
 179       MTAB_unit_valr_nouc,            /* Mask               */
 180       0,                              /* Match              */
 181       "PW",                           /* Print string       */
 182       "PW",                           /* Match string       */
 183       opc_set_console_pw,             /* Validation routine */
 184       opc_show_console_pw,            /* Display routine    */
 185       "Set the console password",     /* Value descriptor   */
 186       NULL                            /* Help               */
 187     },
 188 
 189     MTAB_eol
 190 };
 191 
 192 static DEBTAB opc_dt[] =
 193   {
 194     { "NOTIFY", DBG_NOTIFY, NULL },
 195     { "INFO",   DBG_INFO,   NULL },
 196     { "ERR",    DBG_ERR,    NULL },
 197     { "WARN",   DBG_WARN,   NULL },
 198     { "DEBUG",  DBG_DEBUG,  NULL },
 199     { "ALL",    DBG_ALL,    NULL }, // Don't move as it messes up DBG message
 200     { NULL,     0,          NULL }
 201   };
 202 
 203 // Multics only supports a single operator console; but
 204 // it is possible to run multiple Multics instances in a
 205 // cluster. It is also possible to route message output to
 206 // alternate console(s) as I/O devices.
 207 
 208 #define N_OPC_UNITS 1 // default
 209 #define OPC_UNIT_IDX(uptr) ((uptr) - opc_unit)
 210 
 211 // sim_activate counts in instructions, is dependent on the execution
 212 // model
 213 #if defined(LOCKLESS)
 214 // The sim_activate calls are done by the controller thread, which
 215 // has a 1000Hz cycle rate.
 216 // 1K ~= 1 sec
 217 # define ACTIVATE_1SEC 1000
 218 #else
 219 // The sim_activate calls are done by the only thread, with a 4 MHz
 220 // cycle rate.
 221 // 4M ~= 1 sec
 222 # define ACTIVATE_1SEC 4000000
 223 #endif
 224 
 225 static t_stat opc_svc (UNIT * unitp);
 226 
 227 UNIT opc_unit[N_OPC_UNITS_MAX] = {
 228 #if defined(NO_C_ELLIPSIS)
 229   { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 230   { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 231   { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 232   { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 233   { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 234   { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 235   { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 236   { UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL }
 237 #else
 238   [0 ... N_OPC_UNITS_MAX - 1] = {
 239     UDATA (& opc_svc, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL
 240   }
 241 #endif
 242 };
 243 
 244 DEVICE opc_dev = {
 245     "OPC",         /* Name                */
 246     opc_unit,      /* Units               */
 247     NULL,          /* Registers           */
 248     opc_mtab,      /* Modifiers           */
 249     N_OPC_UNITS,   /* #units              */
 250     10,            /* Address radix       */
 251     8,             /* Address width       */
 252     1,             /* Address increment   */
 253     8,             /* Address width       */
 254     8,             /* Data width          */
 255     NULL,          /* Examine routine     */
 256     NULL,          /* Deposit routine     */
 257     opc_reset,     /* Reset routine       */
 258     NULL,          /* Boot routine        */
 259     NULL,          /* Attach routine      */
 260     NULL,          /* Detach routine      */
 261     NULL,          /* Context             */
 262     DEV_DEBUG,     /* Flags               */
 263     0,             /* Debug control flags */
 264     opc_dt,        /* Debug flag names    */
 265     NULL,          /* Memory size change  */
 266     NULL,          /* Logical name        */
 267     NULL,          /* Help                */
 268     NULL,          /* Attach help         */
 269     NULL,          /* Help context        */
 270     NULL,          /* Description         */
 271     NULL           /* End                 */
 272 };
 273 
 274 enum console_model { m6001 = 0, m6004 = 1, m6601 = 2 };
 275 
 276 // Hangs off the device structure
 277 typedef struct opc_state_t
 278   {
 279     // Track progress of reads through the autoinput buffer
 280     unsigned char *tailp;
 281     unsigned char *readp;
 282 
 283     // Autoinput buffer pointers
 284     unsigned char *auto_input;
 285     unsigned char *autop;
 286 
 287     // stuff saved from the Read ASCII command
 288     time_t startTime;
 289     UNIT * unitp;
 290 
 291     // telnet connection to console
 292     uv_access console_access;
 293 
 294     enum console_model model;
 295     enum console_mode { opc_no_mode, opc_read_mode, opc_write_mode } io_mode;
 296 
 297     // stuff saved from the Read ASCII command
 298     uint tally;
 299     uint daddr;
 300     int chan;
 301 
 302     // ^T
 303     // Generate "accept" command when dial_ctl announces dialin console
 304     int autoaccept;
 305     // Disable the 30 second read timeout (T&D waits for operator answers)
 306     int noto;
 307     // Replace empty console input with "@"
 308     int noempty;
 309     // ATTN flushes typeahead buffer
 310     int attn_flush;
 311 
 312     int simh_buffer_cnt;
 313 
 314     // Track the carrier position to allow tab expansion
 315     // (If the left margin is 1, then the tab stops are 11, 21, 31, 41, ...)
 316     int carrierPosition;
 317 
 318     bool echo;
 319 
 320     bool attn_pressed;
 321     bool simh_attn_pressed;
 322 
 323     bool bcd;
 324 
 325     // Handle escape sequence
 326     bool escapeSequence;
 327 
 328     char device_name [MAX_DEV_NAME_LEN];
 329 
 330 // Multics does console reads with a tally of 64 words; so 256 characters + NUL.
 331 // If the tally is smaller than the contents of the buffer, sendConsole will
 332 // issue a warning and discard the excess.
 333 #define bufsize 257
 334     unsigned char keyboardLineBuffer[bufsize];
 335     bool tabStops [bufsize];
 336 
 337 #define simh_buffer_sz 4096
 338     char simh_buffer[simh_buffer_sz];
 339  } opc_state_t;
 340 
 341 static opc_state_t console_state[N_OPC_UNITS_MAX];
 342 
 343 static char * bcd_code_page =
 344   "01234567"
 345   "89[#@;>?"
 346   " ABCDEFG"
 347   "HI&.](<\\"
 348   "^JKLMNOP"
 349   "QR-$*);'"
 350   "+/STUVWX"
 351   "YZ_,%=\"!";
 352 
 353 //
 354 // Typeahead buffer
 355 //
 356 
 357 #if !defined(TA_BUFFER_SIZE)
 358 # define TA_BUFFER_SIZE 65536
 359 #endif
 360 
 361 static int ta_buffer[TA_BUFFER_SIZE];
 362 static uint ta_cnt  = 0;
 363 static uint ta_next = 0;
 364 static bool ta_ovf  = false;
 365 
 366 static void ta_flush (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 367   {
 368     ta_cnt = ta_next = 0;
 369     ta_ovf = false;
 370   }
 371 
 372 static void ta_push (int c)
     /* [previous][next][first][last][top][bottom][index][help] */
 373   {
 374     // discard overflow
 375     if (ta_cnt >= TA_BUFFER_SIZE)
 376       {
 377         if (! ta_ovf)
 378           sim_print ("typeahead buffer overflow");
 379         ta_ovf = true;
 380         return;
 381       }
 382     ta_buffer [ta_cnt ++] = c;
 383   }
 384 
 385 static int ta_peek (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 386   {
 387     if (ta_next >= ta_cnt)
 388       return SCPE_OK;
 389     int c = ta_buffer[ta_next];
 390     return c;
 391   }
 392 
 393 static int ta_get (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 394   {
 395     if (ta_next >= ta_cnt)
 396       return SCPE_OK;
 397     int c = ta_buffer[ta_next ++];
 398     if (ta_next >= ta_cnt)
 399       ta_flush ();
 400     return c;
 401   }
 402 
 403 static t_stat opc_reset (UNUSED DEVICE * dptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 404   {
 405     for (uint i = 0; i < N_OPC_UNITS_MAX; i ++)
 406       {
 407         console_state[i].io_mode         = opc_no_mode;
 408         console_state[i].tailp           = console_state[i].keyboardLineBuffer;
 409         console_state[i].readp           = console_state[i].keyboardLineBuffer;
 410         console_state[i].carrierPosition = 1;
 411         (void)memset (console_state[i].tabStops, 0, sizeof (console_state[i].tabStops));
 412         console_state[i].escapeSequence  = false;
 413       }
 414     return SCPE_OK;
 415   }
 416 
 417 int check_attn_key (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 418   {
 419     for (uint i = 0; i < opc_dev.numunits; i ++)
 420       {
 421         opc_state_t * csp = console_state + i;
 422         if (csp->attn_pressed)
 423           {
 424              csp->attn_pressed = false;
 425              return (int) i;
 426           }
 427       }
 428     return -1;
 429   }
 430 
 431 // Once-only initialization
 432 
 433 void console_init (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 434   {
 435     opc_reset (& opc_dev);
 436     for (uint i = 0; i < N_OPC_UNITS_MAX; i ++)
 437       {
 438         opc_state_t * csp      = console_state + i;
 439         csp->model             = m6001;
 440         csp->auto_input        = NULL;
 441         csp->autop             = NULL;
 442         csp->attn_pressed      = false;
 443         csp->simh_attn_pressed = false;
 444         csp->simh_buffer_cnt   = 0;
 445         strcpy (csp->console_access.pw, "MulticsRulez");
 446 
 447         csp->autoaccept      = 0;
 448         csp->noempty         = 0;
 449         csp->attn_flush      = 1;
 450         csp->carrierPosition = 1;
 451         csp->escapeSequence  = 1;
 452         (void)memset (csp->tabStops, 0, sizeof (csp->tabStops));
 453       }
 454   }
 455 
 456 // Once-only shutdown
 457 
 458 void console_exit (void) {
     /* [previous][next][first][last][top][bottom][index][help] */
 459   for (uint i = 0; i < N_OPC_UNITS_MAX; i ++) {
 460     opc_state_t * csp = console_state + i;
 461     if (csp->auto_input) {
 462       FREE (csp->auto_input);
 463       csp->auto_input = NULL;
 464     }
 465     // The Telnet parser belongs to the accepted socket's session and is
 466     // freed by its libuv close callback, not by the console state.
 467     accessCloseAll (&csp->console_access);
 468     if (csp->console_access.open &&
 469         !uv_is_closing ((uv_handle_t *)&csp->console_access.server))
 470       uv_close ((uv_handle_t *)&csp->console_access.server, NULL);
 471     csp->console_access.open = false;
 472     FREE (csp->console_access.inBuffer);
 473     csp->console_access.inSize = csp->console_access.inUsed = 0;
 474   }
 475 }
 476 
 477 static int opc_autoinput_set (UNIT * uptr, UNUSED int32 val,
     /* [previous][next][first][last][top][bottom][index][help] */
 478                                 const char *  cptr, UNUSED void * desc)
 479   {
 480     int devUnitIdx = (int) OPC_UNIT_IDX (uptr);
 481     opc_state_t * csp = console_state + devUnitIdx;
 482 
 483     if (cptr)
 484       {
 485         unsigned char * new = (unsigned char *) strdupesc (cptr);
 486         if (csp-> auto_input)
 487           {
 488             size_t nl = strlen ((char *) new);
 489             size_t ol = strlen ((char *) csp->auto_input);
 490 
 491             unsigned char * old = realloc (csp->auto_input, nl + ol + 1);
 492             if (!old)
 493               {
 494                 (void)fprintf(stderr, "\rFATAL: Out of memory! Aborting at %s[%s:%d]\r\n",
 495                               __func__, __FILE__, __LINE__);
 496 #if defined(USE_BACKTRACE)
 497 # if defined(SIGUSR2)
 498                 (void)raise(SIGUSR2);
 499                 /*NOTREACHED*/ /* unreachable */
 500 # endif /* if defined(SIGUSR2) */
 501 #endif /* if defined(USE_BACKTRACE) */
 502                 abort();
 503               }
 504             strcpy ((char *) old + ol, (char *) new);
 505             csp->auto_input = old;
 506             FREE (new);
 507           }
 508         else
 509           csp->auto_input = new;
 510       }
 511     else
 512       {
 513         if (csp->auto_input)
 514           FREE (csp->auto_input);
 515         csp->auto_input = NULL;
 516       }
 517     csp->autop = csp->auto_input;
 518     return SCPE_OK;
 519   }
 520 
 521 int clear_opc_autoinput (int32 flag, UNUSED const char * cptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 522   {
 523     opc_state_t * csp = console_state + flag;
 524     if (csp->auto_input)
 525       FREE (csp->auto_input);
 526     csp->auto_input = NULL;
 527     csp->autop = csp->auto_input;
 528     return SCPE_OK;
 529   }
 530 
 531 int add_opc_autoinput (int32 flag, const char * cptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 532   {
 533     opc_state_t * csp = console_state + flag;
 534     unsigned char * new = (unsigned char *) strdupesc (cptr);
 535     if (csp->auto_input)
 536       {
 537         size_t nl = strlen ((char *) new);
 538         size_t ol = strlen ((char *) csp->auto_input);
 539 
 540         unsigned char * old = realloc (csp->auto_input, nl + ol + 1);
 541         if (!old)
 542           {
 543             (void)fprintf(stderr, "\rFATAL: Out of memory! Aborting at %s[%s:%d]\r\n",
 544                           __func__, __FILE__, __LINE__);
 545 #if defined(USE_BACKTRACE)
 546 # if defined(SIGUSR2)
 547             (void)raise(SIGUSR2);
 548             /*NOTREACHED*/ /* unreachable */
 549 # endif /* if defined(SIGUSR2) */
 550 #endif /* if defined(USE_BACKTRACE) */
 551             abort();
 552           }
 553         strcpy ((char *) old + ol, (char *) new);
 554         csp->auto_input = old;
 555         FREE (new);
 556       }
 557     else
 558       csp->auto_input = new;
 559     csp->autop = csp->auto_input;
 560     return SCPE_OK;
 561   }
 562 
 563 static int opc_autoinput_show (UNUSED FILE * st, UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
 564                                  UNUSED int val, UNUSED const void * desc)
 565   {
 566     int conUnitIdx = (int) OPC_UNIT_IDX (uptr);
 567     opc_state_t * csp = console_state + conUnitIdx;
 568     if (csp->auto_input)
 569       sim_print ("autoinput: '%s'", csp->auto_input);
 570     else
 571       sim_print ("autoinput: empty");
 572     return SCPE_OK;
 573   }
 574 
 575 static t_stat console_attn (UNUSED UNIT * uptr);
 576 
 577 static UNIT attn_unit[N_OPC_UNITS_MAX] = {
 578 #if defined(NO_C_ELLIPSIS)
 579   { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 580   { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 581   { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 582   { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 583   { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 584   { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 585   { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL },
 586   { UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL }
 587 #else
 588   [0 ... N_OPC_UNITS_MAX - 1] = {
 589     UDATA (& console_attn, 0, 0), 0, 0, 0, 0, 0, NULL, NULL, NULL, NULL
 590   }
 591 #endif
 592 };
 593 
 594 static t_stat console_attn (UNUSED UNIT * uptr)
     /* [previous][next][first][last][top][bottom][index][help] */
 595   {
 596     uint con_unit_idx  = (uint) (uptr - attn_unit);
 597     uint ctlr_port_num = 0; // Consoles are single ported
 598     uint iom_unit_idx  = cables->opc_to_iom[con_unit_idx][ctlr_port_num].iom_unit_idx;
 599     uint chan_num      = cables->opc_to_iom[con_unit_idx][ctlr_port_num].chan_num;
 600     uint dev_code      = 0; // Only a single console on the controller
 601 
 602     send_special_interrupt (iom_unit_idx, chan_num, dev_code, 0, 0);
 603     return SCPE_OK;
 604   }
 605 
 606 void console_attn_idx (int conUnitIdx)
     /* [previous][next][first][last][top][bottom][index][help] */
 607   {
 608     console_attn (attn_unit + conUnitIdx);
 609   }
 610 
 611 #if !defined(__MINGW64__) && !defined(__MINGW32__)
 612 static struct termios ttyTermios;
 613 static bool ttyTermiosOk = false;
 614 
 615 static void newlineOff (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 616   {
 617     if (! isatty (0))
 618       return;
 619     if (! ttyTermiosOk)
 620       {
 621         int rc = tcgetattr (0, & ttyTermios); /* get old flags */
 622         if (rc)
 623            return;
 624         ttyTermiosOk = true;
 625       }
 626     struct termios runtty;
 627     runtty = ttyTermios;
 628     runtty.c_oflag &= (unsigned int) ~OPOST; /* no output edit */
 629 # if defined(__ANDROID__)
 630 #  define TCSA_TYPE TCSANOW
 631     (void)fflush(stdout);
 632     (void)fflush(stderr);
 633 # else
 634 #  define TCSA_TYPE TCSAFLUSH
 635 # endif
 636     tcsetattr (0, TCSA_TYPE, & runtty);
 637   }
 638 
 639 static void newlineOn (void)
     /* [previous][next][first][last][top][bottom][index][help] */
 640   {
 641     if (! isatty (0))
 642       return;
 643     if (! ttyTermiosOk)
 644       return;
 645 # if defined(__ANDROID__)
 646     (void)fflush(stdout);
 647     (void)fflush(stderr);
 648 # endif
 649     tcsetattr (0, TCSA_TYPE, & ttyTermios);
 650   }
 651 #endif /* if !defined(__MINGW64__) && !defined(__MINGW32__) */
 652 
 653 static void handleRCP (uint con_unit_idx, char * text)
     /* [previous][next][first][last][top][bottom][index][help] */
 654   {
 655 // It appears that Cygwin doesn't grok "%ms"
 656 
 657 
 658 
 659 
 660 
 661 
 662 
 663 
 664     size_t len = strlen (text);
 665     char   label [len + 1];
 666     char   with  [len + 1];
 667     char   drive [len + 1];
 668     int rc = sscanf (text, "%*d.%*d RCP: Mount Reel %s %s ring on %s",
 669                 label, with, drive);
 670     if (rc == 3)
 671       {
 672         bool withring = (strcmp (with, "with") == 0);
 673         char labelDotTap[strlen (label) + strlen (".tap") + 1];
 674         strcpy (labelDotTap, label);
 675         strcat (labelDotTap, ".tap");
 676         attachTape (labelDotTap, withring, drive);
 677         return;
 678       }
 679 
 680     rc = sscanf (text, "%*d.%*d RCP: Remount Reel %s %s ring on %s",
 681                 label, with, drive);
 682     if (rc == 3)
 683       {
 684         bool withring = (strcmp (with, "with") == 0);
 685         char labelDotTap [strlen (label) + strlen (".tap") + 1];
 686         strcpy (labelDotTap, label);
 687         strcat (labelDotTap, ".tap");
 688         attachTape (labelDotTap, withring, drive);
 689         return;
 690       }
 691 
 692 //  1629  as   dial_ctl_: Channel d.h000 dialed to Initializer
 693 
 694     if (console_state[con_unit_idx].autoaccept)
 695       {
 696         rc = sscanf (text, "%*d  as   dial_ctl_: Channel %s dialed to Initializer",
 697                      label);
 698         if (rc == 1)
 699           {
 700             //sim_printf (" dial system <%s>\r\n", label);
 701             opc_autoinput_set (opc_unit + con_unit_idx, 0, "accept ", NULL);
 702             opc_autoinput_set (opc_unit + con_unit_idx, 0, label, NULL);
 703             opc_autoinput_set (opc_unit + con_unit_idx, 0, "\r", NULL);
 704 // XXX This is subject to race conditions
 705             if (console_state[con_unit_idx].io_mode != opc_read_mode)
 706               console_state[con_unit_idx].attn_pressed = true;
 707             return;
 708           }
 709       }
 710   }
 711 
 712 // Send entered text to the IOM.
 713 static void sendConsole (int conUnitIdx, word12 stati)
     /* [previous][next][first][last][top][bottom][index][help] */
 714   {
 715     opc_state_t * csp   = console_state + conUnitIdx;
 716     uint tally          = csp->tally;
 717     uint ctlr_port_num  = 0; // Consoles are single ported
 718     uint iomUnitIdx     = cables->opc_to_iom[conUnitIdx][ctlr_port_num].iom_unit_idx;
 719     uint chan_num       = cables->opc_to_iom[conUnitIdx][ctlr_port_num].chan_num;
 720     iom_chan_data_t * p = & iom_chan_data[iomUnitIdx][chan_num];
 721 
 722     //ASSURE (csp->io_mode == opc_read_mode);
 723     if (csp->io_mode != opc_read_mode)
 724       {
 725         sim_warn ("%s called with io_mode != opc_read_mode (%d)\r\n",
 726                   __func__, csp->io_mode);
 727         return;
 728       }
 729 
 730     uint n_chars = (uint) (csp->tailp - csp->readp);
 731     uint n_words;
 732     if (csp->bcd)
 733         // + 2 for the !1 newline
 734         n_words = ((n_chars+2) + 5) / 6;
 735       else
 736         n_words = (n_chars + 3) / 4;
 737     // The "+1" is for them empty line case below
 738     word36 buf[n_words + 1];
 739     // Make Oracle lint happy
 740     (void)memset (buf, 0, sizeof (word36) * (n_words + 1));
 741     word36 * bufp = buf;
 742 
 743     // Multics doesn't seem to like empty lines; it the line buffer
 744     // is empty and there is room in the I/O buffer, send a line kill.
 745     if ((!csp->bcd) && csp->noempty && n_chars == 0 && tally)
 746       {
 747         n_chars = 1;
 748         n_words = 1;
 749         putbits36_9 (bufp, 0, '@');
 750         tally --;
 751       }
 752     else
 753       {
 754         int bcd_nl_state = 0;
 755         // BCD: six 6-bit characters per word (it packed only four, so the
 756         // T&D executive read "ALTER 071247" as "ALTE00R 0700..."). The
 757         // "!1" end of line marker isn't sent in T&D mode (as dps8m4).
 758         bool bcd_marker = ! tandd_io_mode ();
 759         uint bcd_charno = 0;
 760         if (csp->bcd)
 761           * bufp = 0;
 762         while (tally && (csp->bcd ? (csp->readp < csp->tailp ||
 763                                      (bcd_marker && bcd_nl_state < 2))
 764                                   : csp->readp < csp->tailp))
 765           {
 766             if (csp->bcd)
 767               {
 768                 unsigned char c;
 769                 if (csp->readp >= csp->tailp)
 770                   {
 771                     if (bcd_nl_state == 0)
 772                       c = '!';
 773                     else
 774                       c = '1';
 775                     bcd_nl_state ++;
 776                   }
 777                 else
 778                   c = (unsigned char) (* csp->readp ++);
 779                 c = (unsigned char) toupper (c);
 780                 int i;
 781                 for (i = 0; i < 64; i ++)
 782                   if (bcd_code_page[i] == c)
 783                     break;
 784                 if (i >= 64)
 785                   {
 786                     sim_warn ("Character %o does not map to BCD; replacing with '?'\r\n", c);
 787                     i = 017; //-V536
 788                   }
 789                 putbits36_6 (bufp, bcd_charno * 6, (word6) i);
 790                 if (++ bcd_charno > 5)
 791                   {
 792                     bcd_charno = 0;
 793                     bufp ++;
 794                     tally --;
 795                     if (tally)
 796                       * bufp = 0;
 797                   }
 798                 continue;
 799               }
 800             else
 801               {
 802                 * bufp = 0ul;
 803                 for (uint charno = 0; charno < 4; ++ charno)
 804                   {
 805                     if (csp->readp >= csp->tailp)
 806                       break;
 807                     unsigned char c = (unsigned char) (* csp->readp ++);
 808                     c &= 0177;  // Multics get consternated about this
 809                     putbits36_9 (bufp, charno * 9, c);
 810                   }
 811               }
 812             bufp ++;
 813             tally --;
 814           }
 815         if (csp->readp < csp->tailp)
 816           {
 817             sim_warn ("opc_iom_io: discarding %d characters from end of line\r\n",
 818                       (int) (csp->tailp - csp->readp));
 819           }
 820       }
 821 
 822     iom_indirect_data_service (iomUnitIdx, chan_num, buf, & n_words, true);
 823 
 824     p->charPos = n_chars % 4;
 825     p->stati   = (word12) stati;
 826 
 827     csp->readp   = csp->keyboardLineBuffer;
 828     csp->tailp   = csp->keyboardLineBuffer;
 829     csp->io_mode = opc_no_mode;
 830 
 831     send_terminate_interrupt (iomUnitIdx, chan_num);
 832   }
 833 
 834 static void console_putchar (int conUnitIdx, char ch);
 835 static void console_putstr  (int conUnitIdx, char * str);
 836 
 837 // Process characters entered on keyboard or autoinput
 838 static void consoleProcessIdx (int conUnitIdx)
     /* [previous][next][first][last][top][bottom][index][help] */
 839   {
 840     opc_state_t * csp = console_state + conUnitIdx;
 841     int c;
 842 
 843 //// Move data from keyboard buffers into type-ahead buffer
 844 
 845     for (;;)
 846       {
 847         c = sim_poll_kbd ();
 848         if (c == SCPE_OK)
 849           c = accessGetChar (& csp->console_access);
 850 
 851         // Check for stop signaled by scp
 852 
 853         if (breakEnable && stop_cpu)
 854           {
 855             console_putstr (conUnitIdx,  " Got <sim stop> \r\n");
 856             return;
 857           }
 858 
 859         // Check for ^E
 860         //   (Windows doesn't handle ^E as a signal; need to explicitly test
 861         //   for it.)
 862 
 863         if (breakEnable && c == SCPE_STOP)
 864           {
 865             console_putstr (conUnitIdx,  " Got <sim stop> \r\n");
 866             stop_cpu = 1;
 867             return; // User typed ^E to stop simulation
 868           }
 869 
 870         // Check for scp break
 871 
 872         if (breakEnable && c == SCPE_BREAK)
 873           {
 874             console_putstr (conUnitIdx,  " Got <sim stop> \r\n");
 875             stop_cpu = 1;
 876             return; // User typed ^E to stop simulation
 877           }
 878 
 879         // End of available input
 880 
 881         if (c == SCPE_OK)
 882           break;
 883 
 884         // sanity test
 885 
 886         if (c < SCPE_KFLAG)
 887           {
 888             sim_printf ("impossible %d %o\r\n", c, c);
 889             continue; // Should be impossible
 890           }
 891 
 892         // translate to ascii
 893 
 894         int ch = c - SCPE_KFLAG;
 895 
 896         // XXX This is subject to race conditions
 897         // If the console is not in read mode and ESC or ^A
 898         // is pressed, signal Multics for ATTN.
 899         if (csp->io_mode != opc_read_mode)
 900           {
 901             if (ch == '\033' || ch == '\001') // escape or ^A
 902               {
 903                 if (csp->attn_flush)
 904                   ta_flush ();
 905                 csp->attn_pressed = true;
 906                 continue;
 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         // ^S
 932 
 933         if (ch == 023) // ^S scp command
 934           {
 935             if (! csp->simh_attn_pressed)
 936               {
 937                 ta_flush ();
 938                 csp->simh_attn_pressed = true;
 939                 csp->simh_buffer_cnt = 0;
 940                 console_putstr (conUnitIdx, "^S\r\n" SIM_NAME "> ");
 941               }
 942             continue;
 943           }
 944 
 945 //// ^P  Prompt
 946 
 947         // ATTN is ambiguous; in read mode, it cancels the read.
 948         // In write mode, is discards output and asks for a prompt.
 949 
 950         // ^P will check the mode, and if it is not input, will do an
 951         // ATTN signal. There is still a small race window; if Multics
 952         // is in the process of starting read mode, it may end up canceling
 953         // the read.
 954 
 955         if (ch == 020) { // ^P
 956           if (csp->io_mode != opc_read_mode) {
 957             if (csp->attn_flush)
 958               ta_flush ();
 959             csp->attn_pressed = true;
 960           }
 961           continue;
 962         }
 963 
 964 //// ^T
 965 
 966         if (ch == 024) // ^T
 967           {
 968             char buf[256];
 969             char cms[3] = "?RW";
 970             // XXX Assumes console 0
 971             (void)sprintf (buf, "^T attn %c %c cnt %d next %d\r\n",
 972                            console_state[0].attn_pressed+'0',
 973                            cms[console_state[0].io_mode],
 974                            ta_cnt, ta_next);
 975             console_putstr (conUnitIdx, buf);
 976 #if defined(THREADZ) || defined(LOCKLESS)
 977             (void) sprintf (buf, "syncClockMode %c syncClockModeMasterIdx %u\r\n",
 978                             syncClockMode ? 'T' : 'F',
 979                             syncClockModeMasterIdx);
 980             console_putstr (conUnitIdx, buf);
 981             for (int i = 0; i < N_CPU_UNITS_MAX; i ++) {
 982               (void) sprintf (buf, "CPU %c %s %05o:%06o\r\n"
 983                                    "      executing %c syncClockModeMaster %c workAllocation %4ld\r\n"
 984                                    "      inMultics %c syncClockModeCache %c isSlave %c\r\n",
 985                               "ABCDEFGH"[i],
 986                               cycle_str (cpus[i].cycle),
 987                               cpus[i].PPR.PSR,
 988                               cpus[i].PPR.IC,
 989                               cpus[i].executing ? 'T' : 'F',
 990                               cpus[i].syncClockModeMaster ? 'T' : 'F',
 991                               cpus[i].workAllocation,
 992                               cpus[i].inMultics ? 'T' : 'F',
 993                               cpus[i].syncClockModeCache ? 'T' : 'F',
 994                               cpus[i].isSlave ? 'T' : 'F');
 995               console_putstr (conUnitIdx, buf);
 996             }
 997 #endif /* if defined(THREADZ) || defined(LOCKLESS) */
 998 
 999             continue;
1000           }
1001 
1002 //// In ^S mode (accumulating a scp command)?
1003 
1004         if (csp->simh_attn_pressed)
1005           {
1006             ta_get ();
1007             if (ch == '\177' || ch == '\010')  // backspace/del
1008               {
1009                 if (csp->simh_buffer_cnt > 0)
1010                   {
1011                     -- csp->simh_buffer_cnt;
1012                     csp->simh_buffer[csp->simh_buffer_cnt] = 0;
1013                     console_putstr (conUnitIdx,  "\b \b");
1014                   }
1015                 return;
1016               }
1017 
1018             //// scp ^R
1019 
1020             if (ch == '\022')  // ^R
1021               {
1022                 console_putstr (conUnitIdx, "^R\r\n" SIM_NAME "> ");
1023                 for (int i = 0; i < csp->simh_buffer_cnt; i ++)
1024                   console_putchar (conUnitIdx, (char) (csp->simh_buffer[i]));
1025                 return;
1026               }
1027 
1028             //// scp ^U
1029 
1030             if (ch == '\025')  // ^U
1031               {
1032                 console_putstr (conUnitIdx, "^U\r\n" SIM_NAME "> ");
1033                 csp->simh_buffer_cnt = 0;
1034                 return;
1035               }
1036 
1037             //// scp CR/LF
1038 
1039             if (ch == '\012' || ch == '\015')  // CR/LF
1040               {
1041                 console_putstr (conUnitIdx,  "\r\n");
1042                 csp->simh_buffer[csp->simh_buffer_cnt] = 0;
1043 
1044                 char * cptr = csp->simh_buffer;
1045                 char gbuf[simh_buffer_sz];
1046                 cptr = (char *) get_glyph (cptr, gbuf, 0); /* get command glyph */
1047                 if (strlen (gbuf))
1048                   {
1049                     CTAB *cmdp;
1050                     if ((cmdp = find_cmd (gbuf))) /* lookup command */
1051                       {
1052                         t_stat stat = cmdp->action (cmdp->arg, cptr);
1053                            /* if found, exec */
1054                         if (stat != SCPE_OK)
1055                           {
1056                             char buf[4096];
1057                             (void)sprintf (buf, "\r%s returned %d '%s'\r\n",
1058                                            SIM_NAME, stat, sim_error_text (stat));
1059                             console_putstr (conUnitIdx,  buf);
1060                           }
1061                       }
1062                     else
1063                        console_putstr (conUnitIdx,
1064                          "\rUnrecognized " SIM_NAME " command.\r\n");
1065                   }
1066                 csp->simh_buffer_cnt   = 0;
1067                 csp->simh_buffer[0]    = 0;
1068                 csp->simh_attn_pressed = false;
1069                 return;
1070               }
1071 
1072             //// scp ESC/^D/^Z
1073 
1074             if (ch == '\033' || ch == '\004' || ch == '\032')  // ESC/^D/^Z
1075               {
1076                 console_putstr (conUnitIdx,  "\r\n" SIM_NAME " cancel\r\n");
1077                 // Empty input buffer
1078                 csp->simh_buffer_cnt   = 0;
1079                 csp->simh_buffer[0]    = 0;
1080                 csp->simh_attn_pressed = false;
1081                 return;
1082               }
1083 
1084             //// scp isprint?
1085 
1086             if (isprint (ch))
1087               {
1088                 // silently drop buffer overrun
1089                 if (csp->simh_buffer_cnt + 1 >= simh_buffer_sz)
1090                   return;
1091                 csp->simh_buffer[csp->simh_buffer_cnt ++] = (char) ch;
1092                 console_putchar (conUnitIdx, (char) ch);
1093                 return;
1094               }
1095             return;
1096           } // if (simh_attn_pressed)
1097 
1098         // Save the character
1099 
1100         ta_push (c);
1101       }
1102 
1103 //// Check for stop signaled by scp
1104 
1105     if (breakEnable && stop_cpu)
1106       {
1107         console_putstr (conUnitIdx,  " Got <sim stop> \r\n");
1108         return;
1109       }
1110 
1111 //// Console is reading and autoinput is ready
1112 ////   Move line of text from autoinput buffer to console buffer
1113 
1114     if (csp->io_mode == opc_read_mode &&
1115         csp->autop != NULL)
1116       {
1117         int announce = 1;
1118         for (;;)
1119           {
1120             if (csp->tailp >= csp->keyboardLineBuffer + sizeof (csp->keyboardLineBuffer))
1121              {
1122                 sim_warn ("getConsoleInput: Buffer full; flushing autoinput.\r\n");
1123                 sendConsole (conUnitIdx, 04000); // Normal status
1124                 return;
1125               }
1126             unsigned char c = * (csp->autop);
1127             if (c == 4) // eot
1128               {
1129                 FREE (csp->auto_input);
1130                 csp->auto_input = NULL;
1131                 csp->autop      = NULL;
1132                 // Empty input buffer
1133                 csp->readp      = csp->keyboardLineBuffer;
1134                 csp->tailp      = csp->keyboardLineBuffer;
1135                 sendConsole (conUnitIdx, 04310); // Null line, status operator
1136                                                  // distracted
1137                 console_putstr (conUnitIdx,  "CONSOLE: RELEASED\r\n");
1138                 return;
1139               }
1140             if (c == 030 || c == 031) // ^X ^Y
1141               {
1142                 // an expect string is in the autoinput buffer; wait for it
1143                 // to be processed
1144                 return;
1145               }
1146             if (c == 0)
1147               {
1148                 FREE (csp->auto_input);
1149                 csp->auto_input = NULL;
1150                 csp->autop      = NULL;
1151                 goto eol;
1152               }
1153             if (announce)
1154               {
1155                 console_putstr (conUnitIdx,  "[auto-input] ");
1156                 announce = 0;
1157               }
1158             csp->autop ++;
1159 
1160             if (c == '\012' || c == '\015')
1161               {
1162 eol:
1163                 if (csp->echo)
1164                   console_putstr (conUnitIdx,  "\r\n");
1165                 sendConsole (conUnitIdx, 04000); // Normal status
1166                 return;
1167               }
1168             else
1169               {
1170                 * csp->tailp ++ = c;
1171                 if (csp->echo)
1172                   console_putchar (conUnitIdx, (char) c);
1173               }
1174           } // for (;;)
1175       } // if (autop)
1176 
1177 //// Read mode and nothing in console buffer
1178 ////   Check for timeout
1179 
1180     if (csp->io_mode == opc_read_mode &&
1181         csp->tailp == csp->keyboardLineBuffer)
1182       {
1183         if ((! csp->noto) && csp->startTime + 30 <= time (NULL))
1184           {
1185             console_putstr (conUnitIdx,  "CONSOLE: TIMEOUT\r\n");
1186             csp->readp = csp->keyboardLineBuffer;
1187             csp->tailp = csp->keyboardLineBuffer;
1188             sendConsole (conUnitIdx, 04310); // Null line, status operator
1189                                              // distracted
1190           }
1191       }
1192 
1193 //// Peek at the character in the typeahead buffer
1194 
1195     c = ta_peek ();
1196 
1197     // No data
1198     if (c == SCPE_OK)
1199         return;
1200 
1201     // Convert from scp encoding to ASCII
1202     if (c < SCPE_KFLAG)
1203       {
1204         sim_printf ("impossible %d %o\r\n", c, c);
1205         return; // Should be impossible
1206       }
1207 
1208     // translate to ascii
1209 
1210     int ch = c - SCPE_KFLAG;
1211 
1212 // XXX This is subject to race conditions
1213     if (csp->io_mode != opc_read_mode)
1214       {
1215         if (ch == '\033' || ch == '\001') // escape or ^A
1216           {
1217             ta_get ();
1218             csp->attn_pressed = true;
1219           }
1220         return;
1221       }
1222 
1223     if (ch == '\177' || ch == '\010')  // backspace/del
1224       {
1225         ta_get ();
1226         if (csp->tailp > csp->keyboardLineBuffer)
1227           {
1228             * csp->tailp = 0;
1229             -- csp->tailp;
1230             if (csp->echo)
1231               console_putstr (conUnitIdx,  "\b \b");
1232           }
1233         return;
1234       }
1235 
1236     if (ch == '\022')  // ^R
1237       {
1238         ta_get ();
1239         if (csp->echo)
1240           {
1241             console_putstr (conUnitIdx,  "^R\r\n");
1242             for (unsigned char * p = csp->keyboardLineBuffer; p < csp->tailp; p ++)
1243               console_putchar (conUnitIdx, (char) (*p));
1244             return;
1245           }
1246       }
1247 
1248     if (ch == '\025')  // ^U
1249       {
1250         ta_get ();
1251         console_putstr (conUnitIdx,  "^U\r\n");
1252         csp->tailp = csp->keyboardLineBuffer;
1253         return;
1254       }
1255 
1256     if (ch == '\030')  // ^X
1257       {
1258         ta_get ();
1259         console_putstr (conUnitIdx,  "^X\r\n");
1260         csp->tailp = csp->keyboardLineBuffer;
1261         return;
1262       }
1263 
1264     if (ch == '\012' || ch == '\015')  // CR/LF
1265       {
1266         ta_get ();
1267         if (csp->echo)
1268           console_putstr (conUnitIdx,  "\r\n");
1269         sendConsole (conUnitIdx, 04000); // Normal status
1270         return;
1271       }
1272 
1273     if (ch == '\033' || ch == '\004' || ch == '\032')  // ESC/^D/^Z
1274       {
1275         ta_get ();
1276         console_putstr (conUnitIdx,  "\r\n");
1277         // Empty input buffer
1278         csp->readp = csp->keyboardLineBuffer;
1279         csp->tailp = csp->keyboardLineBuffer;
1280         sendConsole (conUnitIdx, 04310); // Null line, status operator
1281                                          // distracted
1282         console_putstr (conUnitIdx,  "CONSOLE: RELEASED\r\n");
1283         return;
1284       }
1285 
1286     if (isprint (ch))
1287       {
1288         // silently drop buffer overrun
1289         ta_get ();
1290         if (csp->tailp >= csp->keyboardLineBuffer + sizeof (csp->keyboardLineBuffer))
1291           return;
1292 
1293         * csp->tailp ++ = (unsigned char) ch;
1294         if (csp->echo)
1295           console_putchar (conUnitIdx, (char) ch);
1296         return;
1297       }
1298     // ignore other chars...
1299     ta_get ();
1300     return;
1301   }
1302 
1303 void consoleProcess (void)
     /* [previous][next][first][last][top][bottom][index][help] */
1304   {
1305     for (int conUnitIdx = 0; conUnitIdx < (int) opc_dev.numunits; conUnitIdx ++)
1306       consoleProcessIdx (conUnitIdx);
1307   }
1308 
1309 /*
1310  * opc_iom_cmd ()
1311  *
1312  * Handle a device command.  Invoked by the IOM while processing a PCW
1313  * or IDCW.
1314  */
1315 
1316 iom_cmd_rc_t opc_iom_cmd (uint iomUnitIdx, uint chan) {
     /* [previous][next][first][last][top][bottom][index][help] */
1317   iom_cmd_rc_t rc = IOM_CMD_PROCEED;
1318 #if defined(TESTING)
1319   cpu_state_t * cpup = _cpup;
1320 #endif
1321 
1322 #if defined(LOCKLESS)
1323   lock_libuv ();
1324 #endif
1325 
1326   iom_chan_data_t * p = & iom_chan_data[iomUnitIdx][chan];
1327   uint con_unit_idx   = get_ctlr_idx (iomUnitIdx, chan);
1328   UNIT * unitp        = & opc_unit[con_unit_idx];
1329   opc_state_t * csp   = console_state + con_unit_idx;
1330 
1331   p->dev_code = p->IDCW_DEV_CODE;
1332   p->stati = 0;
1333   //int conUnitIdx = (int) d->devUnitIdx;
1334 
1335   // The 6001 only executes the PCW DCW command; the 6601 executes
1336   // the PCW DCW and (at least) the first DCW list item.
1337   // When Multics uses the 6601, the PCW DCW is always 040 RESET.
1338   // The 040 RESET will trigger the DCW list read.
1339   // will change this.
1340 
1341   // IDCW?
1342   if (IS_IDCW (p)) {
1343     // IDCW
1344 
1345     switch (p->IDCW_DEV_CMD) {
1346       case 000: // CMD 00 Request status
1347         sim_debug (DBG_DEBUG, & opc_dev, "%s: Status request\r\n", __func__);
1348         csp->io_mode = opc_no_mode;
1349         p->stati     = 04000;
1350         break;
1351 
1352       case 003:               // Read BCD
1353         sim_debug (DBG_DEBUG, & tape_dev, "%s: Read BCD echoed\r\n", __func__);
1354         csp->io_mode = opc_read_mode;
1355         p->recordResidue --;
1356         csp->echo    = true;
1357         csp->bcd     = true;
1358         p->stati     = 04000;
1359         break;
1360 
1361       case 013:               // Write BCD
1362         sim_debug (DBG_DEBUG, & opc_dev, "%s: Write BCD\r\n", __func__);
1363         p->isRead    = false;
1364         csp->bcd     = true;
1365         csp->io_mode = opc_write_mode;
1366         p->recordResidue --;
1367         p->stati     = 04000;
1368         break;
1369 
1370       case 023:               // Read ASCII
1371         sim_debug (DBG_DEBUG, & tape_dev, "%s: Read ASCII echoed\r\n", __func__);
1372         csp->io_mode = opc_read_mode;
1373         p->recordResidue --;
1374         csp->echo    = true;
1375         csp->bcd     = false;
1376         p->stati     = 04000;
1377         break;
1378 
1379       case 033:               // Write ASCII
1380         sim_debug (DBG_DEBUG, & opc_dev, "%s: Write ASCII\r\n", __func__);
1381         p->isRead    = false;
1382         csp->bcd     = false;
1383         csp->io_mode = opc_write_mode;
1384         p->recordResidue --;
1385         p->stati     = 04000;
1386         break;
1387 
1388 // Model 6001 vs. 6601.
1389 //
1390 // When Multics switches to 6601 mode, the PCW DCW is always 040 RESET; the
1391 // bootload and 6001 code never does that. Therefore, we use the 040
1392 // as an indication that the DCW list should be processed.
1393 // All of the other device commands will return IOM_CMD_DISCONNECT, stopping
1394 // parsing of the channel command program. This one will return IOM_CMD_PROCEED,
1395 // causing the parser to move to the DCW list.
1396 
1397       case 040:               // Reset
1398         sim_debug (DBG_DEBUG, & opc_dev, "%s: Reset\r\n", __func__);
1399         p->stati = 04000;
1400         // T&D probing
1401         //if (p->IDCW_DEV_CODE == 077) {
1402           // T&D uses dev code 77 to test for the console device;
1403           // it ignores dev code, and so returns OK here.
1404           //p->stati = 04502; // invalid device code
1405           // if (p->IDCW_CHAN_CTRL == 0) { sim_warn ("%s: TERMINATE_BUG\r\n", __func__); return IOM_CMD_DISCONNECT; }
1406         //}
1407         break;
1408 
1409       case 043:               // Read ASCII unechoed
1410         sim_debug (DBG_DEBUG, & tape_dev, "%s: Read ASCII unechoed\r\n", __func__);
1411         csp->io_mode = opc_read_mode;
1412         p->recordResidue --;
1413         csp->echo    = false;
1414         csp->bcd     = false;
1415         p->stati     = 04000;
1416         break;
1417 
1418       case 051:               // Write Alert -- Ring Bell
1419         sim_debug (DBG_DEBUG, & opc_dev, "%s: Alert\r\n", __func__);
1420         p->isRead = false;
1421         console_putstr ((int) con_unit_idx,  "CONSOLE: ALERT\r\n");
1422         console_putchar ((int) con_unit_idx, '\a');
1423         p->stati  = 04000;
1424         if (csp->model == m6001 && p->isPCW) {
1425           rc = IOM_CMD_DISCONNECT;
1426           goto done;
1427         }
1428         break;
1429 
1430       case 057:               // Read ID (according to AN70-1)
1431         // FIXME: No support for Read ID; appropriate values are not known
1432         //[CAC] Looking at the bootload console code, it seems more
1433         // concerned about the device responding, rather than the actual
1434         // returned value. Make some thing up.
1435         sim_debug (DBG_DEBUG, & opc_dev, "%s: Read ID\r\n", __func__);
1436         p->stati = 04500;
1437         if (csp->model == m6001 && p->isPCW) {
1438           rc = IOM_CMD_DISCONNECT;
1439           goto done;
1440         }
1441         break;
1442 
1443       case 060:               // LOCK MCA
1444         sim_debug (DBG_DEBUG, & opc_dev, "%s: Lock\r\n", __func__);
1445         console_putstr ((int) con_unit_idx,  "CONSOLE: LOCK\r\n");
1446         p->stati = 04000;
1447         break;
1448 
1449       case 063:               // UNLOCK MCA
1450         sim_debug (DBG_DEBUG, & opc_dev, "%s: Unlock\r\n", __func__);
1451         console_putstr ((int) con_unit_idx,  "CONSOLE: UNLOCK\r\n");
1452         p->stati = 04000;
1453         break;
1454 
1455       default:
1456         sim_debug (DBG_DEBUG, & opc_dev, "%s: Unknown command 0%o\r\n", __func__, p->IDCW_DEV_CMD);
1457         p->stati = 04501; // command reject, invalid instruction code
1458         rc = IOM_CMD_ERROR;
1459         goto done;
1460     } // switch IDCW_DEV_CMD
1461     goto done;
1462   } // IDCW
1463 
1464   // Not IDCW; TDCW are captured in IOM, so must be IOTD or IOTP
1465   switch (csp->io_mode) {
1466     case opc_no_mode:
1467       // T&D: the executive's channel survey follows its commands with data
1468       // DCWs whatever the device answered; not worth a warning there
1469       if (! tandd_io_mode ())
1470         sim_warn ("%s: Unexpected IOTx\r\n", __func__);
1471       rc = IOM_CMD_ERROR;
1472       goto done;
1473 
1474     case opc_read_mode: {
1475         if (csp->tailp != csp->keyboardLineBuffer) {
1476           sim_warn ("%s: Discarding previously buffered input.\r\n", __func__);
1477         }
1478         uint tally = p->DDCW_TALLY;
1479         uint daddr = p->DDCW_ADDR;
1480 
1481         if (tally == 0) {
1482           tally = 4096;
1483         }
1484 
1485         csp->tailp     = csp->keyboardLineBuffer;
1486         csp->readp     = csp->keyboardLineBuffer;
1487         csp->startTime = time (NULL);
1488         csp->tally     = tally;
1489         csp->daddr     = daddr;
1490         csp->unitp     = unitp;
1491         csp->chan      = (int) chan;
1492 
1493         // If Multics has gone seriously awry (eg crash
1494         // to BCE during boot), the autoinput will become
1495         // wedged waiting for the expect string 'Ready'.
1496         // We just went to read mode; if we are waiting
1497         // on an expect string, it is never coming because
1498         // console access is blocked by the expect code.
1499         // Throw out the script if this happens....
1500 
1501         // If there is autoinput and it is at ^X or ^Y
1502         if (csp->autop && (*csp->autop == 030 || *csp->autop == 031)) { // ^X ^Y
1503           // We are wedged.
1504           // Clear the autoinput buffer; this will cancel the
1505           // expect wait and any remaining script, returning
1506           // control of the console to the user.
1507           // Assuming opc0.
1508           clear_opc_autoinput (ASSUME0, NULL);
1509           ta_flush ();
1510           sim_printf \
1511               ("\r\nScript wedged and abandoned; autoinput and typeahead buffers flushed\r\n");
1512         }
1513         rc = IOM_CMD_PENDING; // command in progress; do not send terminate interrupt
1514         goto done;
1515       } // case opc_read_mode
1516 
1517     case opc_write_mode: {
1518         uint tally = p->DDCW_TALLY;
1519 
1520 // We would hope that number of valid characters in the last word
1521 // would be in DCW_18_20_CP, but it seems to reliably be zero.
1522 
1523         if (tally == 0) {
1524           tally = 4096;
1525         }
1526 
1527         word36 buf[tally];
1528         iom_indirect_data_service (iomUnitIdx, chan, buf, & tally, false);
1529         p->initiate = false;
1530 
1531 
1532 
1533 
1534 
1535 
1536 
1537 
1538 
1539 
1540 
1541 
1542 
1543 
1544 
1545 
1546 
1547 
1548 
1549 
1550 
1551 
1552 
1553 
1554 
1555 
1556 
1557 
1558 
1559 
1560 
1561         // Tally is in words, not chars.
1562         char text[tally * 4 + 1];
1563         char * textp = text;
1564         word36 * bufp = buf;
1565         * textp = 0;
1566 #if !defined(__MINGW64__)
1567         newlineOff ();
1568 #endif
1569         // 0 == no escape character seen
1570         // 1 ==  ! seen
1571         // 2 == !! seen
1572         int escape_cnt = 0;
1573 
1574         while (tally) {
1575           word36 datum = * bufp ++;
1576           tally --;
1577           if (csp->bcd) {
1578             // Lifted from tolts_util_.pl1:bci_to_ascii
1579             for (int i = 0; i < 6; i ++) {
1580               word36 narrow_char = datum >> 30; // slide leftmost char
1581                                                 //  into low byte
1582               datum = datum << 6; // lose the leftmost char
1583               narrow_char &= 077;
1584               char ch = bcd_code_page [narrow_char];
1585               if (escape_cnt == 2) {
1586                 console_putchar ((int) con_unit_idx, ch);
1587                 * textp ++ = ch;
1588                 escape_cnt = 0;
1589               } else if (ch == '!') {
1590                 escape_cnt ++;
1591               } else if (escape_cnt == 1) {
1592                 uint lp = (uint)narrow_char;
1593                 // !0 is mapped to !1
1594                 // !1 to !9, ![, !#, !@, !;, !>, !?    1 to 15 newlines
1595                 if (lp == 060 /* + */ || lp == 075 /* = */) { // POLTS
1596                   p->stati = 04320;
1597                   goto done;
1598                 }
1599                 if (lp == 0)
1600                   lp = 1;
1601                 if (lp >= 16) {
1602                   console_putstr ((int) con_unit_idx, "\f");
1603                   //* textp ++ = '\f';
1604                 } else {
1605                   for (uint i = 0; i < lp; i ++) {
1606                     console_putstr ((int) con_unit_idx, "\r\n");
1607                     //* textp ++ = '\r';
1608                     //* textp ++ = '\n';
1609                   }
1610                 }
1611                 escape_cnt = 0;
1612               } else if (ch == '?') {
1613                 escape_cnt = 0;
1614               } else {
1615                 console_putchar ((int) con_unit_idx, ch);
1616                 * textp ++ = ch;
1617                 escape_cnt = 0;
1618               }
1619             }
1620           } else {
1621             for (int i = 0; i < 4; i ++) {
1622               word36 wide_char = datum >> 27; // slide leftmost char
1623                                               //  into low byte
1624               datum = datum << 9; // lose the leftmost char
1625               char ch = wide_char & 0x7f;
1626               if (ch != 0177 && ch != 0) {
1627                 console_putchar ((int) con_unit_idx, ch);
1628                 * textp ++ = ch;
1629               }
1630             }
1631           }
1632         }
1633         * textp ++ = 0;
1634 
1635         // autoinput expect
1636         if (csp->autop && * csp->autop == 030) {
1637           //   ^xstring\0
1638           //size_t expl = strlen ((char *) (csp->autop + 1));
1639           //   ^xstring^x
1640           size_t expl = strcspn ((char *) (csp->autop + 1), "\030");
1641 
1642           if (strncmp (text, (char *) (csp->autop + 1), expl) == 0) {
1643             csp->autop += expl + 2;
1644             sim_activate (& attn_unit[con_unit_idx], ACTIVATE_1SEC);
1645           }
1646         }
1647         // autoinput expect
1648         if (csp->autop && * csp->autop == 031) {
1649           //   ^ystring\0
1650           //size_t expl = strlen ((char *) (csp->autop + 1));
1651           //   ^ystring^y
1652           size_t expl = strcspn ((char *) (csp->autop + 1), "\031");
1653 
1654           char needle [expl + 1];
1655           strncpy (needle, (char *) csp->autop + 1, expl);
1656           needle [expl] = 0;
1657           if (strstr (text, needle)) {
1658             csp->autop += expl + 2;
1659             sim_activate (& attn_unit[con_unit_idx], ACTIVATE_1SEC);
1660           }
1661         }
1662         handleRCP (con_unit_idx, text);
1663 #if !defined(__MINGW64__)
1664         newlineOn ();
1665 #endif
1666         p->stati = 04000;
1667         goto done;
1668       } // case opc_write_mode
1669   } // switch io_mode
1670 
1671 done:
1672 #if defined(LOCKLESS)
1673   unlock_libuv ();
1674 #endif
1675   return rc;
1676 } // opc_iom_cmd
1677 
1678 static t_stat opc_svc (UNIT * unitp)
     /* [previous][next][first][last][top][bottom][index][help] */
1679   {
1680     int con_unit_idx   = (int) OPC_UNIT_IDX (unitp);
1681     uint ctlr_port_num = 0; // Consoles are single ported
1682     uint iom_unit_idx  = cables->opc_to_iom[con_unit_idx][ctlr_port_num].iom_unit_idx;
1683     uint chan_num      = cables->opc_to_iom[con_unit_idx][ctlr_port_num].chan_num;
1684 
1685     opc_iom_cmd (iom_unit_idx, chan_num);
1686     return SCPE_OK;
1687   }
1688 
1689 static t_stat opc_show_nunits (UNUSED FILE * st, UNUSED UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
1690                                  UNUSED int val, UNUSED const void * desc)
1691   {
1692     sim_print ("%d units\r\n", opc_dev.numunits);
1693     return SCPE_OK;
1694   }
1695 
1696 static t_stat opc_set_nunits (UNUSED UNIT * uptr, int32 UNUSED value,
     /* [previous][next][first][last][top][bottom][index][help] */
1697                                 const char * cptr, UNUSED void * desc)
1698   {
1699     if (! cptr)
1700       return SCPE_ARG;
1701     int n = atoi (cptr);
1702     if (n < 1 || n > N_OPC_UNITS_MAX)
1703       return SCPE_ARG;
1704     opc_dev.numunits = (uint32) n;
1705     return SCPE_OK;
1706   }
1707 
1708 static config_value_list_t cfg_on_off[] =
1709   {
1710     { "off",     0 },
1711     { "on",      1 },
1712     { "disable", 0 },
1713     { "enable",  1 },
1714     { NULL,      0 }
1715   };
1716 
1717 static config_value_list_t cfg_model[] =
1718   {
1719     { "m6001", m6001 },
1720     { "m6004", m6004 },
1721     { "m6601", m6601 },
1722     { NULL,    0 }
1723   };
1724 
1725 static config_list_t opc_config_list[] =
1726   {
1727    { "autoaccept", 0, 1, cfg_on_off },
1728    { "noto",       0, 1, cfg_on_off },
1729    { "noempty",    0, 1, cfg_on_off },
1730    { "attn_flush", 0, 1, cfg_on_off },
1731    { "model",      1, 0, cfg_model },
1732    { NULL,         0, 0, NULL }
1733   };
1734 
1735 static t_stat opc_set_config (UNUSED UNIT *  uptr, UNUSED int32 value,
     /* [previous][next][first][last][top][bottom][index][help] */
1736                               const char * cptr, UNUSED void * desc)
1737   {
1738     int devUnitIdx           = (int) OPC_UNIT_IDX (uptr);
1739     opc_state_t * csp        = console_state + devUnitIdx;
1740 // XXX Minor bug; this code doesn't check for trailing garbage
1741     config_state_t cfg_state = { NULL, NULL };
1742 
1743     for (;;)
1744       {
1745         int64_t v;
1746         int rc = cfg_parse (__func__, cptr, opc_config_list,
1747                            & cfg_state, & v);
1748         if (rc == -1) // done
1749           break;
1750 
1751         if (rc == -2) // error
1752           {
1753             cfg_parse_done (& cfg_state);
1754             return SCPE_ARG;
1755           }
1756         const char * p = opc_config_list[rc].name;
1757 
1758         if (strcmp (p, "autoaccept") == 0)
1759           {
1760             csp->autoaccept = (int) v;
1761             continue;
1762           }
1763 
1764         if (strcmp (p, "noto") == 0)
1765           {
1766             csp->noto = (int) v;
1767             continue;
1768           }
1769 
1770         if (strcmp (p, "noempty") == 0)
1771           {
1772             csp->noempty = (int) v;
1773             continue;
1774           }
1775 
1776         if (strcmp (p, "attn_flush") == 0)
1777           {
1778             csp->attn_flush = (int) v;
1779             continue;
1780           }
1781 
1782         if (strcmp (p, "model") == 0)
1783           {
1784             csp->model = (enum console_model) v;
1785             continue;
1786           }
1787 
1788         sim_warn ("error: opc_set_config: Invalid cfg_parse rc <%d>\r\n",
1789                   rc);
1790         cfg_parse_done (& cfg_state);
1791         return SCPE_ARG;
1792       } // process statements
1793     cfg_parse_done (& cfg_state);
1794     return SCPE_OK;
1795   }
1796 
1797 static t_stat opc_show_config (UNUSED FILE * st, UNUSED UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
1798                                UNUSED int  val, UNUSED const void * desc)
1799   {
1800     int devUnitIdx    = (int) OPC_UNIT_IDX (uptr);
1801     opc_state_t * csp = console_state + devUnitIdx;
1802     sim_msg ("flags    : ");
1803     sim_msg ("autoaccept=%d, ", csp->autoaccept);
1804     sim_msg ("noto=%d, ", csp->noto);
1805     sim_msg ("noempty=%d, ",    csp->noempty);
1806     sim_msg ("attn_flush=%d",   csp->attn_flush);
1807     return SCPE_OK;
1808   }
1809 
1810 static t_stat opc_show_device_name (UNUSED FILE * st, UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
1811                                     UNUSED int val, UNUSED const void * desc)
1812   {
1813     int n = (int) OPC_UNIT_IDX (uptr);
1814     if (n < 0 || n >= N_OPC_UNITS_MAX)
1815       return SCPE_ARG;
1816     sim_printf("name     : OPC%d", n);
1817     return SCPE_OK;
1818   }
1819 
1820 static t_stat opc_set_device_name (UNIT * uptr, UNUSED int32 value,
     /* [previous][next][first][last][top][bottom][index][help] */
1821                                    const char * cptr, UNUSED void * desc)
1822   {
1823     int n = (int) OPC_UNIT_IDX (uptr);
1824     if (n < 0 || n >= N_OPC_UNITS_MAX)
1825       return SCPE_ARG;
1826     if (cptr)
1827       {
1828         strncpy (console_state[n].device_name, cptr, MAX_DEV_NAME_LEN-1);
1829         console_state[n].device_name[MAX_DEV_NAME_LEN-1] = 0;
1830       }
1831     else
1832       console_state[n].device_name[0] = 0;
1833     return SCPE_OK;
1834   }
1835 
1836 static t_stat opc_set_console_port (UNIT * uptr, UNUSED int32 value,
     /* [previous][next][first][last][top][bottom][index][help] */
1837                                     const char * cptr, UNUSED void * desc)
1838   {
1839     int dev_idx = (int) OPC_UNIT_IDX (uptr);
1840     if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1841       return SCPE_ARG;
1842 
1843     if (cptr)
1844       {
1845         int port = atoi (cptr);
1846         if (port < 0 || port > 65535) // 0 is 'disable'
1847           return SCPE_ARG;
1848         console_state[dev_idx].console_access.port = port;
1849         if (port != 0)
1850           sim_msg ("[OPC emulation: TELNET server port set to %d]\r\n", (int)port);
1851       }
1852     else
1853       console_state[dev_idx].console_access.port = 0;
1854     return SCPE_OK;
1855   }
1856 
1857 static t_stat opc_show_console_port (UNUSED FILE * st, UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
1858                                        UNUSED int val, UNUSED const void * desc)
1859   {
1860     int dev_idx = (int) OPC_UNIT_IDX (uptr);
1861     if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1862       return SCPE_ARG;
1863     if (console_state[dev_idx].console_access.port)
1864       sim_printf("port     : %d", console_state[dev_idx].console_access.port);
1865     else
1866       sim_printf("port     : disabled");
1867     return SCPE_OK;
1868   }
1869 
1870 static t_stat opc_set_console_address (UNIT * uptr, UNUSED int32 value,
     /* [previous][next][first][last][top][bottom][index][help] */
1871                                     const char * cptr, UNUSED void * desc)
1872   {
1873     int dev_idx = (int) OPC_UNIT_IDX (uptr);
1874     if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1875       return SCPE_ARG;
1876 
1877     if (console_state[dev_idx].console_access.address)
1878       {
1879         FREE (console_state[dev_idx].console_access.address);
1880         console_state[dev_idx].console_access.address = NULL;
1881       }
1882 
1883     if (cptr)
1884       {
1885         console_state[dev_idx].console_access.address = strdup (cptr);
1886         if (!console_state[dev_idx].console_access.address)
1887           {
1888             (void)fprintf (stderr, "\rFATAL: Out of memory! Aborting at %s[%s:%d]\r\n",
1889                            __func__, __FILE__, __LINE__);
1890 #if defined(USE_BACKTRACE)
1891 # if defined(SIGUSR2)
1892             (void)raise(SIGUSR2);
1893             /*NOTREACHED*/ /* unreachable */
1894 # endif /* if defined(SIGUSR2) */
1895 #endif /* if defined(USE_BACKTRACE) */
1896             abort();
1897           }
1898         sim_msg ("\r[OPC emulation: OPC%d server address set to %s]\r\n",
1899                 dev_idx, console_state[dev_idx].console_access.address);
1900       }
1901 
1902     return SCPE_OK;
1903   }
1904 
1905 static t_stat opc_show_console_address (UNUSED FILE * st, UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
1906                                        UNUSED int val, UNUSED const void * desc)
1907   {
1908     int dev_idx = (int) OPC_UNIT_IDX (uptr);
1909     if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1910       return SCPE_ARG;
1911     if (console_state[dev_idx].console_access.address)
1912       sim_printf("address  : %s", console_state[dev_idx].console_access.address);
1913     else
1914       sim_printf("address  : any");
1915     return SCPE_OK;
1916   }
1917 
1918 static t_stat opc_set_console_pw (UNIT * uptr, UNUSED int32 value,
     /* [previous][next][first][last][top][bottom][index][help] */
1919                                     const char * cptr, UNUSED void * desc)
1920   {
1921     long dev_idx = (int) OPC_UNIT_IDX (uptr);
1922     if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1923       return SCPE_ARG;
1924 
1925     if (cptr && (strlen(cptr) > 0))
1926       {
1927         char token[strlen (cptr)+1];
1928         int rc = sscanf (cptr, "%s", token);
1929         if (rc != 1)
1930           return SCPE_ARG;
1931         if (strlen (token) > PW_SIZE)
1932           return SCPE_ARG;
1933         strcpy (console_state[dev_idx].console_access.pw, token);
1934       }
1935     else
1936       {
1937         sim_msg ("no password\r\n");
1938         console_state[dev_idx].console_access.pw[0] = 0;
1939       }
1940 
1941     return SCPE_OK;
1942   }
1943 
1944 static t_stat opc_show_console_pw (UNUSED FILE * st, UNIT * uptr,
     /* [previous][next][first][last][top][bottom][index][help] */
1945                                        UNUSED int val, UNUSED const void * desc)
1946   {
1947     int dev_idx = (int) OPC_UNIT_IDX (uptr);
1948     if (dev_idx < 0 || dev_idx >= N_OPC_UNITS_MAX)
1949       return SCPE_ARG;
1950     sim_printf("password : %s", console_state[dev_idx].console_access.pw);
1951     return SCPE_OK;
1952   }
1953 
1954 static void console_putstr (int conUnitIdx, char * str)
     /* [previous][next][first][last][top][bottom][index][help] */
1955   {
1956     size_t l = strlen (str);
1957     for (size_t i = 0; i < l; i ++)
1958       sim_putchar (str[i]);
1959     opc_state_t * csp = console_state + conUnitIdx;
1960     if (csp->console_access.loggedOn)
1961       accessStartWrite (csp->console_access.client, str,
1962                            (ssize_t) l);
1963   }
1964 
1965 static void consolePutchar0 (int conUnitIdx, char ch) {
     /* [previous][next][first][last][top][bottom][index][help] */
1966   opc_state_t * csp = console_state + conUnitIdx;
1967   sim_putchar (ch);
1968   if (csp->console_access.loggedOn)
1969     accessStartWrite (csp->console_access.client, & ch, 1);
1970 }
1971 
1972 static void console_putchar (int conUnitIdx, char ch) {
     /* [previous][next][first][last][top][bottom][index][help] */
1973   opc_state_t * csp = console_state + conUnitIdx;
1974   if (csp->escapeSequence) { // Prior character was an escape
1975     csp->escapeSequence = false;
1976     if (ch == '1') { // Set tab
1977       if (csp->carrierPosition >= 1 && csp->carrierPosition <= 256) {
1978         csp->tabStops[csp->carrierPosition] = true;
1979       }
1980     } else if (ch == '2') { // Clear all tabs
1981       (void)memset (csp->tabStops, 0, sizeof (csp->tabStops));
1982     } else { // Unrecognized
1983       sim_warn ("Unrecognized escape sequence \\033\\%03o\r\n", ch);
1984     }
1985   } else if (isprint (ch)) {
1986     consolePutchar0 (conUnitIdx, ch);
1987     csp->carrierPosition ++;
1988   } else if (ch == '\t') {  // Tab
1989     while (csp->carrierPosition < bufsize - 1) {
1990       consolePutchar0 (conUnitIdx, ' ');
1991       csp->carrierPosition ++;
1992       if (csp->tabStops[csp->carrierPosition])
1993         break;
1994     }
1995   } else if (ch == '\b') { // Backspace
1996       consolePutchar0 (conUnitIdx, ch);
1997       csp->carrierPosition --;
1998   } else if (ch == '\f' || ch == '\r') { // Formfeed, Carriage return
1999       consolePutchar0 (conUnitIdx, ch);
2000       csp->carrierPosition = 1;
2001   } else if (ch == '\033') { //-V536  // Escape
2002       csp->escapeSequence = true;
2003   } else { // Non-printing and we don't recognize a carriage motion character, so just print it...
2004       consolePutchar0 (conUnitIdx, ch);
2005   }
2006 }
2007 
2008 static void consoleConnectPrompt (uv_tcp_t * client)
     /* [previous][next][first][last][top][bottom][index][help] */
2009   {
2010     accessStartWriteStr (client, "password: \r\n");
2011   }
2012 
2013 void startRemoteConsole (void)
     /* [previous][next][first][last][top][bottom][index][help] */
2014   {
2015     for (int conUnitIdx = 0; conUnitIdx < N_OPC_UNITS_MAX; conUnitIdx ++)
2016       {
2017         console_state[conUnitIdx].console_access.connectPrompt = consoleConnectPrompt;
2018         console_state[conUnitIdx].console_access.connected     = NULL;
2019         console_state[conUnitIdx].console_access.useTelnet     = true;
2020 #if defined(CONSOLE_FIX)
2021 # if defined(LOCKLESS)
2022         lock_libuv ();
2023 # endif
2024 #endif
2025         uv_open_access (& console_state[conUnitIdx].console_access);
2026 #if defined(CONSOLE_FIX)
2027 # if defined(LOCKLESS)
2028         unlock_libuv ();
2029 # endif
2030 #endif
2031       }
2032   }

/* [previous][next][first][last][top][bottom][index][help] */