From d364571c2c42eb696037343c3972b0521ab193c1 Mon Sep 17 00:00:00 2001 From: Mark Pizzolato Date: Wed, 24 Apr 2013 09:00:36 -0700 Subject: [PATCH] Added tmxr_linemsgf api to send formatted messages to a multiplexer line. --- sim_tmxr.c | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++--- sim_tmxr.h | 1 + 2 files changed, 70 insertions(+), 3 deletions(-) diff --git a/sim_tmxr.c b/sim_tmxr.c index 50830049..4d34b9b2 100644 --- a/sim_tmxr.c +++ b/sim_tmxr.c @@ -85,6 +85,7 @@ tmxr_dep - (null) deposit tmxr_msg - send message to socket tmxr_linemsg - send message to line + tmxr_linemsgf - send formatted message to line tmxr_fconns - output connection status tmxr_fstats - output connection statistics tmxr_set_log - enable logging for line @@ -490,9 +491,7 @@ static void tmxr_report_disconnection (TMLN *lp) { if (lp->notelnet) return; -tmxr_linemsg (lp, "\r\nDisconnected from the "); /* report disconnection */ -tmxr_linemsg (lp, sim_name); -tmxr_linemsg (lp, " simulator\r\n\n"); +tmxr_linemsgf (lp, "\nDisconnected from the %s simulator\n\n", sim_name);/* report disconnection */ return; } @@ -2988,6 +2987,73 @@ return; } +/* Write a formatted message to a line */ + +void tmxr_linemsgf (TMLN *lp, const char *fmt, ...) +{ +char stackbuf[STACKBUFSIZE]; +int32 bufsize = sizeof(stackbuf); +char *buf = stackbuf; +va_list arglist; +int32 i, len; + +buf[bufsize-1] = '\0'; +while (1) { /* format passed string, args */ + va_start (arglist, fmt); +#if defined(NO_vsnprintf) +#if defined(HAS_vsprintf_void) + +/* Note, this could blow beyond the buffer, and we couldn't tell */ +/* That is a limitation of the C runtime library available on this platform */ + + vsprintf (buf, fmt, arglist); + for (len = 0; len < bufsize-1; len++) + if (buf[len] == 0) break; +#else + len = vsprintf (buf, fmt, arglist); +#endif /* HAS_vsprintf_void */ +#else /* NO_vsnprintf */ +#if defined(HAS_vsnprintf_void) + vsnprintf (buf, bufsize-1, fmt, arglist); + for (len = 0; len < bufsize-1; len++) + if (buf[len] == 0) break; +#else + len = vsnprintf (buf, bufsize-1, fmt, arglist); +#endif /* HAS_vsnprintf_void */ +#endif /* NO_vsnprintf */ + va_end (arglist); + +/* If the formatted result didn't fit into the buffer, then grow the buffer and try again */ + + if ((len < 0) || (len >= bufsize-1)) { + if (buf != stackbuf) + free (buf); + bufsize = bufsize * 2; + buf = (char *) malloc (bufsize); + if (buf == NULL) /* out of memory */ + return; + buf[bufsize-1] = '\0'; + continue; + } + break; + } + +/* Output the formatted data expanding newlines where they exist */ + +for (i = 0; i < len; ++i) { + if ('\n' == buf[i]) { + tmxr_putc_ln (lp, '\r'); + tmxr_putc_ln (lp, buf[i]); + } + else + tmxr_putc_ln (lp, buf[i]); + } +if (buf != stackbuf) + free (buf); +return; +} + + /* Print connections - used only in named SHOW command */ void tmxr_fconns (FILE *st, TMLN *lp, int32 ln) diff --git a/sim_tmxr.h b/sim_tmxr.h index 6901ba68..147234fc 100644 --- a/sim_tmxr.h +++ b/sim_tmxr.h @@ -174,6 +174,7 @@ t_stat tmxr_ex (t_value *vptr, t_addr addr, UNIT *uptr, int32 sw); t_stat tmxr_dep (t_value val, t_addr addr, UNIT *uptr, int32 sw); void tmxr_msg (SOCKET sock, char *msg); void tmxr_linemsg (TMLN *lp, char *msg); +void tmxr_linemsgf (TMLN *lp, const char *fmt, ...); void tmxr_fconns (FILE *st, TMLN *lp, int32 ln); void tmxr_fstats (FILE *st, TMLN *lp, int32 ln); t_stat tmxr_set_log (UNIT *uptr, int32 val, char *cptr, void *desc);