From d5a56e0ab423c4d247381c5ae45cc45f4de06d5b Mon Sep 17 00:00:00 2001 From: Mark Pizzolato Date: Mon, 16 Jan 2017 14:25:38 -0800 Subject: [PATCH] SCP: Take care to only write to a socket a single time and when appropriate --- scp.c | 143 ++++++++++++++++++++++++++++------------------------- sim_fio.c | 6 +++ sim_tmxr.c | 10 +++- 3 files changed, 90 insertions(+), 69 deletions(-) diff --git a/scp.c b/scp.c index 84e17597..64aa71b3 100644 --- a/scp.c +++ b/scp.c @@ -3920,14 +3920,14 @@ if (1) { } } if (!sim_quiet) - printf ("Asynchronous I/O %sabled\n", sim_asynch_enabled ? "en" : "dis"); -if (sim_log) + fprintf (stdout, "Asynchronous I/O %sabled\n", sim_asynch_enabled ? "en" : "dis"); +if ((!sim_oline) && sim_log) fprintf (sim_log, "Asynchronous I/O %sabled\n", sim_asynch_enabled ? "en" : "dis"); return SCPE_OK; #else if (!sim_quiet) - printf ("Asynchronous I/O is not available in this simulator\n"); -if (sim_log) + fprintf (stdout, "Asynchronous I/O is not available in this simulator\n"); +if ((!sim_oline) && sim_log) fprintf (sim_log, "Asynchronous I/O is not available in this simulator\n"); return SCPE_NOFNC; #endif @@ -4228,9 +4228,9 @@ if (sim_ofile) { /* output file? */ } else { r = show_cmd_fi (stdout, flag, cptr); /* no, stdout, log */ - if (sim_log && (sim_log != stdout)) + if ((!sim_oline) && (sim_log && (sim_log != stdout))) show_cmd_fi (sim_log, flag, cptr); - if (sim_deb && (sim_deb != stdout) && (sim_deb != sim_log)) + if ((!sim_oline) && (sim_deb && (sim_deb != stdout) && (sim_deb != sim_log))) show_cmd_fi (sim_deb, flag, cptr); } return r; @@ -6750,6 +6750,9 @@ for (gptr = gbuf, reason = SCPE_OK; sim_switches = sim_switches | SIM_SW_HIDE; reason = exdep_reg_loop (ofile, sim_schrptr, flag, cptr, lowr, --highr, 0, 0); + if ((!sim_oline) && (sim_log && (ofile == stdout))) + exdep_reg_loop (sim_log, sim_schrptr, EX_E, cptr, + lowr, --highr, 0, 0); continue; } @@ -6777,6 +6780,9 @@ for (gptr = gbuf, reason = SCPE_OK; return SCPE_ARG; reason = exdep_reg_loop (ofile, sim_schrptr, flag, cptr, lowr, highr, (uint32) low, (uint32) high); + if ((!sim_oline) && (sim_log && (ofile == stdout))) + exdep_reg_loop (sim_log, sim_schrptr, EX_E, cptr, + lowr, highr, (uint32) low, (uint32) high); continue; } @@ -6789,6 +6795,9 @@ for (gptr = gbuf, reason = SCPE_OK; return SCPE_ARG; reason = exdep_addr_loop (ofile, sim_schaptr, flag, cptr, low, high, sim_dfdev, sim_dfunit); + if ((!sim_oline) && (sim_log && (ofile == stdout))) + exdep_addr_loop (sim_log, sim_schaptr, EX_E, cptr, low, high, + sim_dfdev, sim_dfunit); } /* end for */ if (sim_ofile) /* close output file */ fclose (sim_ofile); @@ -6808,6 +6817,7 @@ t_stat reason; uint32 idx, val_start=lows; t_value val, last_val; REG *rptr; +int32 saved_switches = sim_switches; if ((lowr == NULL) || (highr == NULL)) return SCPE_IERR; @@ -6822,6 +6832,7 @@ for (rptr = lowr; rptr <= highr; rptr++) { if (idx >= rptr->depth) return SCPE_SUB; val = get_rval (rptr, idx); + sim_switches = saved_switches; if (schptr && !test_search (&val, schptr)) continue; if (flag == EX_E) { @@ -6830,34 +6841,27 @@ for (rptr = lowr; rptr <= highr; rptr++) { if (idx > val_start+1) { if (idx-1 == val_start+1) { reason = ex_reg (ofile, val, flag, rptr, idx-1); + sim_switches = saved_switches; if (reason != SCPE_OK) return reason; - if (sim_log && (ofile == stdout)) - ex_reg (sim_log, val, flag, rptr, idx-1); } else { - if (val_start+1 != idx-1) { - Fprintf (ofile, "%s[%d]-%s[%d]: same as above\n", rptr->name, val_start+1, rptr->name, idx-1); - if (sim_log && (ofile == stdout)) - Fprintf (sim_log, "%s[%d]-%s[%d]: same as above\n", rptr->name, val_start+1, rptr->name, idx-1); - } - else { - Fprintf (ofile, "%s[%d]: same as above\n", rptr->name, val_start+1); - if (sim_log && (ofile == stdout)) - Fprintf (sim_log, "%s[%d]: same as above\n", rptr->name, val_start+1); - } + if (val_start+1 != idx-1) + fprintf (ofile, "%s[%d]-%s[%d]: same as above\n", rptr->name, val_start+1, rptr->name, idx-1); + else + fprintf (ofile, "%s[%d]: same as above\n", rptr->name, val_start+1); } } sim_last_val = last_val = val; val_start = idx; reason = ex_reg (ofile, val, flag, rptr, idx); + sim_switches = saved_switches; if (reason != SCPE_OK) return reason; - if (sim_log && (ofile == stdout)) - ex_reg (sim_log, val, flag, rptr, idx); } if (flag != EX_E) { reason = dep_reg (flag, cptr, rptr, idx); + sim_switches = saved_switches; if (reason != SCPE_OK) return reason; } @@ -6865,22 +6869,15 @@ for (rptr = lowr; rptr <= highr; rptr++) { if ((flag == EX_E) && (val_start != highs)) { if (highs == val_start+1) { reason = ex_reg (ofile, val, flag, rptr, highs); + sim_switches = saved_switches; if (reason != SCPE_OK) return reason; - if (sim_log && (ofile == stdout)) - ex_reg (sim_log, val, flag, rptr, highs); } else { - if (val_start+1 != highs) { - Fprintf (ofile, "%s[%d]-%s[%d]: same as above\n", rptr->name, val_start+1, rptr->name, highs); - if (sim_log && (ofile == stdout)) - Fprintf (sim_log, "%s[%d]-%s[%d]: same as above\n", rptr->name, val_start+1, rptr->name, highs); - } - else { - Fprintf (ofile, "%s[%d]: same as above\n", rptr->name, val_start+1); - if (sim_log && (ofile == stdout)) - Fprintf (sim_log, "%s[%d]: same as above\n", rptr->name, val_start+1); - } + if (val_start+1 != highs) + fprintf (ofile, "%s[%d]-%s[%d]: same as above\n", rptr->name, val_start+1, rptr->name, highs); + else + fprintf (ofile, "%s[%d]: same as above\n", rptr->name, val_start+1); } } } @@ -6892,6 +6889,7 @@ t_stat exdep_addr_loop (FILE *ofile, SCHTAB *schptr, int32 flag, const char *cpt { t_addr i, mask; t_stat reason; +int32 saved_switches = sim_switches; if (uptr->flags & UNIT_DIS) /* disabled? */ return SCPE_UDIS; @@ -6900,6 +6898,7 @@ if ((low > mask) || (high > mask) || (low > high)) return SCPE_ARG; for (i = low; i <= high; ) { /* all paths must incr!! */ reason = get_aval (i, dptr, uptr); /* get data */ + sim_switches = saved_switches; if (reason != SCPE_OK) /* return if error */ return reason; if (schptr && !test_search (sim_eval, schptr)) @@ -6907,14 +6906,15 @@ for (i = low; i <= high; ) { /* all paths must incr!! else { /* no sch or success */ if (flag != EX_D) { /* ex, ie, or id? */ reason = ex_addr (ofile, flag, i, dptr, uptr); + sim_switches = saved_switches; if (reason > SCPE_OK) return reason; - if (sim_log && (ofile == stdout)) - ex_addr (sim_log, flag, i, dptr, uptr); } - else reason = 1 - dptr->aincr; /* no, dflt incr */ + else + reason = 1 - dptr->aincr; /* no, dflt incr */ if (flag != EX_E) { /* ie, id, or d? */ reason = dep_addr (flag, cptr, i, dptr, uptr, reason); + sim_switches = saved_switches; if (reason > SCPE_OK) return reason; } @@ -6943,8 +6943,9 @@ int32 rdx; if (rptr == NULL) return SCPE_IERR; if (rptr->depth > 1) - Fprintf (ofile, "%s[%d]:\t", rptr->name, idx); -else Fprintf (ofile, "%s:\t", rptr->name); + fprintf (ofile, "%s[%d]:\t", rptr->name, idx); +else + fprintf (ofile, "%s:\t", rptr->name); if (!(flag & EX_E)) return SCPE_OK; GET_RADIX (rdx, rptr->radix); @@ -6955,13 +6956,14 @@ else if (!(rptr->flags & REG_VMFLAGS) || NULL, sim_switches | SIM_SW_REG) > 0)) { fprint_val (ofile, val, rdx, rptr->width, rptr->flags & REG_FMT); if (rptr->fields) { - Fprintf (ofile, "\t"); + fprintf (ofile, "\t"); fprint_fields (ofile, val, val, rptr->fields); } } if (flag & EX_I) - Fprintf (ofile, "\t"); -else Fprintf (ofile, "\n"); + fprintf (ofile, "\t"); +else + fprintf (ofile, "\n"); return SCPE_OK; } @@ -7173,7 +7175,7 @@ int32 rdx; if (sim_vm_fprint_addr) sim_vm_fprint_addr (ofile, dptr, addr); else fprint_val (ofile, addr, dptr->aradix, dptr->awidth, PV_LEFT); -Fprintf (ofile, ":\t"); +fprintf (ofile, ":\t"); if (!(flag & EX_E)) return (1 - dptr->aincr); @@ -7183,8 +7185,9 @@ if ((reason = fprint_sym (ofile, addr, sim_eval, uptr, sim_switches)) > 0) { reason = 1 - dptr->aincr; } if (flag & EX_I) - Fprintf (ofile, "\t"); -else Fprintf (ofile, "\n"); + fprintf (ofile, "\t"); +else + fprintf (ofile, "\n"); return reason; } @@ -8762,7 +8765,7 @@ if (!stream) if (width > MAX_WIDTH) width = MAX_WIDTH; sprint_val (dbuf, val, radix, width, format); -if (Fprintf (stream, "%s", dbuf) < 0) +if (fprintf (stream, "%s", dbuf) < 0) return SCPE_IOERR; return SCPE_OK; } @@ -10489,7 +10492,7 @@ for (i = fields-1; i >= 0; i--) { /* print xlation, transition continue; if ((bitdefs[i].width == 1) && (bitdefs[i].valuenames == NULL)) { int off = ((after >> bitdefs[i].offset) & 1) + (((before ^ after) >> bitdefs[i].offset) & 1) * 2; - Fprintf(stream, "%s%c ", bitdefs[i].name, debug_bstates[off]); + fprintf(stream, "%s%c ", bitdefs[i].name, debug_bstates[off]); } else { const char *delta = ""; @@ -10502,15 +10505,15 @@ for (i = fields-1; i >= 0; i--) { /* print xlation, transition if (value > beforevalue) delta = "^"; if (bitdefs[i].valuenames) - Fprintf(stream, "%s=%s%s ", bitdefs[i].name, delta, bitdefs[i].valuenames[value]); + fprintf(stream, "%s=%s%s ", bitdefs[i].name, delta, bitdefs[i].valuenames[value]); else if (bitdefs[i].format) { - Fprintf(stream, "%s=%s", bitdefs[i].name, delta); - Fprintf(stream, bitdefs[i].format, value); - Fprintf(stream, " "); + fprintf(stream, "%s=%s", bitdefs[i].name, delta); + fprintf(stream, bitdefs[i].format, value); + fprintf(stream, " "); } else - Fprintf(stream, "%s=%s0x%X ", bitdefs[i].name, delta, value); + fprintf(stream, "%s=%s0x%X ", bitdefs[i].name, delta, value); } } } @@ -10523,14 +10526,18 @@ void sim_debug_bits_hdr(uint32 dbits, DEVICE* dptr, const char *header, BITFIELD* bitdefs, uint32 before, uint32 after, int terminate) { if (sim_deb && dptr && (dptr->dctrl & dbits)) { + TMLN *saved_oline = sim_oline; + + sim_oline = NULL; /* avoid potential debug to active socket */ if (!debug_unterm) - fprintf(sim_deb, "%s", sim_debug_prefix(dbits, dptr)); /* print prefix if required */ + fprintf(sim_deb, "%s", sim_debug_prefix(dbits, dptr)); /* print prefix if required */ if (header) fprintf(sim_deb, "%s: ", header); - fprint_fields (sim_deb, (t_value)before, (t_value)after, bitdefs); /* print xlation, transition */ + fprint_fields (sim_deb, (t_value)before, (t_value)after, bitdefs); /* print xlation, transition */ if (terminate) fprintf(sim_deb, "\r\n"); - debug_unterm = terminate ? 0 : 1; /* set unterm for next */ + debug_unterm = terminate ? 0 : 1; /* set unterm for next */ + sim_oline = saved_oline; /* restore original socket */ } } void sim_debug_bits(uint32 dbits, DEVICE* dptr, BITFIELD* bitdefs, @@ -10577,20 +10584,20 @@ while (1) { /* format passed string, arg if (sim_is_running) { char *c, *remnant = buf; - while ((c = strchr(remnant, '\n'))) { + while ((c = strchr (remnant, '\n'))) { if ((c != buf) && (*(c - 1) != '\r')) - printf("%.*s\r\n", (int)(c-remnant), remnant); + fprintf (stdout, "%.*s\r\n", (int)(c-remnant), remnant); else - printf("%.*s\n", (int)(c-remnant), remnant); + fprintf (stdout, "%.*s\n", (int)(c-remnant), remnant); remnant = c + 1; } - printf("%s", remnant); + fprintf (stdout, "%s", remnant); } else - printf("%s", buf); -if (sim_log && (sim_log != stdout)) + fprintf (stdout, "%s", buf); +if ((!sim_oline) && (sim_log && (sim_log != stdout))) fprintf (sim_log, "%s", buf); -if (sim_deb && (sim_deb != stdout) && (sim_deb != sim_log)) +if ((!sim_oline) && (sim_deb && (sim_deb != stdout) && (sim_deb != sim_log))) fprintf (sim_deb, "%s", buf); if (buf != stackbuf) @@ -10654,20 +10661,20 @@ if (sim_is_running && !inhibit_message) { while ((c = strchr(remnant, '\n'))) { if ((c != buf) && (*(c - 1) != '\r')) - printf("%.*s\r\n", (int)(c-remnant), remnant); + fprintf (stdout, "%.*s\r\n", (int)(c-remnant), remnant); else - printf("%.*s\n", (int)(c-remnant), remnant); + fprintf (stdout, "%.*s\n", (int)(c-remnant), remnant); remnant = c + 1; } - printf("%s", remnant); + fprintf (stdout, "%s", remnant); } else { if (!inhibit_message) - printf("%s", buf); + fprintf (stdout, "%s", buf); } -if (sim_log && (sim_log != stdout) && !inhibit_message) +if ((!sim_oline) && (sim_log && (sim_log != stdout) && !inhibit_message)) fprintf (sim_log, "%s", buf); -if (sim_deb && (((sim_deb != stdout) && (sim_deb != sim_log)) || inhibit_message))/* Always display messages in debug output */ +if ((!sim_oline) && (sim_deb && (((sim_deb != stdout) && (sim_deb != sim_log)) || inhibit_message)))/* Always display messages in debug output */ fprintf (sim_deb, "%s", buf); if (buf != stackbuf) @@ -10691,7 +10698,7 @@ void _sim_debug (uint32 dbits, DEVICE* vdptr, const char* fmt, ...) { DEVICE *dptr = (DEVICE *)vdptr; if (sim_deb && dptr && (dptr->dctrl & dbits)) { - + TMLN *saved_oline = sim_oline; char stackbuf[STACKBUFSIZE]; int32 bufsize = sizeof(stackbuf); char *buf = stackbuf; @@ -10699,6 +10706,7 @@ if (sim_deb && dptr && (dptr->dctrl & dbits)) { int32 i, j, len; const char* debug_prefix = sim_debug_prefix(dbits, dptr); /* prefix to print if required */ + sim_oline = NULL; /* avoid potential debug to active socket */ buf[bufsize-1] = '\0'; while (1) { /* format passed string, args */ @@ -10755,6 +10763,7 @@ if (sim_deb && dptr && (dptr->dctrl & dbits)) { debug_unterm = len ? (((buf[len-1]=='\n')) ? 0 : 1) : debug_unterm; if (buf != stackbuf) free (buf); + sim_oline = saved_oline; /* restore original socket */ } return; } diff --git a/sim_fio.c b/sim_fio.c index 008db86d..258178de 100644 --- a/sim_fio.c +++ b/sim_fio.c @@ -65,6 +65,12 @@ t_bool sim_end; /* TRUE = little endian, FALSE = big endian t_bool sim_taddr_64; /* t_addr is > 32b and Large File Support available */ t_bool sim_toffset_64; /* Large File (>2GB) file I/O Support available */ +#if defined(fprintf) /* Make sure to only use the C rtl stream I/O routines */ +#undef fprintf +#undef fputs +#undef fputc +#endif + /* OS-independent, endian independent binary I/O package For consistency, all binary data read and written by the simulator diff --git a/sim_tmxr.c b/sim_tmxr.c index 7dec384a..79ae2b53 100644 --- a/sim_tmxr.c +++ b/sim_tmxr.c @@ -2047,8 +2047,14 @@ if ((lp->txbfd && !lp->notelnet) || (TXBUF_AVAIL(lp) > 1)) {/* room for char (+ TXBUF_CHAR (lp, chr); /* buffer char & adv pointer */ if ((!lp->txbfd) && (TXBUF_AVAIL (lp) <= TMXR_GUARD))/* near full? */ lp->xmte = 0; /* disable line */ - if (lp->txlog) /* log if available */ - fputc (chr, lp->txlog); + if (lp->txlog) { /* log if available */ + extern TMLN *sim_oline; /* Make sure to avoid recursion */ + TMLN *save_oline = sim_oline; /* when logging to a socket */ + + sim_oline = NULL; /* save output socket */ + fputc (chr, lp->txlog); /* log to actual file */ + sim_oline = save_oline; /* resture output socket */ + } sim_exp_check (&lp->expect, chr); /* process expect rules as needed */ return SCPE_OK; /* char sent */ }