diff --git a/src/include/ngspice/cmproto.h b/src/include/ngspice/cmproto.h index 75e474241..5692b5a21 100644 --- a/src/include/ngspice/cmproto.h +++ b/src/include/ngspice/cmproto.h @@ -85,6 +85,13 @@ int cm_event_queue(double time); char *cm_message_get_errmsg(void); int cm_message_send(char *msg); +#ifdef __GNUC__ +int cm_message_printf(const char *fmt, ...) __attribute__ ((format (__printf__, 1, 2))); +#else +int cm_message_printf(const char *fmt, ...); +#endif + + double cm_netlist_get_c(void); double cm_netlist_get_l(void); diff --git a/src/xspice/icm/analog/file_source/cfunc.mod b/src/xspice/icm/analog/file_source/cfunc.mod index 4493f64dd..7ec3d03c4 100644 --- a/src/xspice/icm/analog/file_source/cfunc.mod +++ b/src/xspice/icm/analog/file_source/cfunc.mod @@ -179,9 +179,7 @@ void cm_filesource(ARGS) /* structure holding parms, inputs, outputs, etc. free(p); } if (!loc->state->fp) { - char msg[512]; - snprintf(msg, sizeof(msg), "cannot open file %s", PARAM(file)); - cm_message_send(msg); + cm_message_printf("cannot open file %s", PARAM(file)); loc->state->atend = 1; } } diff --git a/src/xspice/icm/digital/d_source/cfunc.mod b/src/xspice/icm/digital/d_source/cfunc.mod index 7540ff5d7..768b82585 100644 --- a/src/xspice/icm/digital/d_source/cfunc.mod +++ b/src/xspice/icm/digital/d_source/cfunc.mod @@ -936,11 +936,8 @@ void cm_d_source(ARGS) source = fopen(p, "r"); free(p); } - if (!source) { - char msg[512]; - snprintf(msg, sizeof(msg), "cannot open file %s", PARAM(input_file)); - cm_message_send(msg); - } + if (!source) + cm_message_printf("cannot open file %s", PARAM(input_file)); } /* increment counter if not a comment until EOF reached... */ diff --git a/src/xspice/icm/dlmain.c b/src/xspice/icm/dlmain.c index 47e2d1fd2..ab228ca0b 100644 --- a/src/xspice/icm/dlmain.c +++ b/src/xspice/icm/dlmain.c @@ -17,6 +17,8 @@ #include #include +#include + ////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////// @@ -452,3 +454,41 @@ FILE *fopen_with_path(const char *path, const char *mode) fp = fopen(path, mode); return fp; } + + +int +cm_message_printf(const char *fmt, ...) +{ + char buf[1024]; + char *p = buf; + int size = sizeof(buf); + int rv; + + for (;;) { + + int nchars; + va_list ap; + + va_start(ap, fmt); + nchars = vsnprintf(p, (size_t) size, fmt, ap); + va_end(ap); + + if (nchars == -1) { // compatibility to old implementations + size *= 2; + } else if (size < nchars + 1) { + size = nchars + 1; + } else { + break; + } + + if (p == buf) + p = tmalloc((size_t) size * sizeof(char)); + else + p = trealloc(p, (size_t) size * sizeof(char)); + } + + rv = cm_message_send(p); + if (p != buf) + txfree(p); + return rv; +}