From f88eb23b02088e7549c46231d0dfa4ee8d35a365 Mon Sep 17 00:00:00 2001 From: Paul Smith Date: Sun, 28 Apr 2013 19:09:20 -0400 Subject: Ensure error messages are printed with sync'd output. Enhance the child_error() function so that it will write error output to the child's sync output buffer, if it exists. If it doesn't the output goes to stdout/stderr. --- misc.c | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) (limited to 'misc.c') diff --git a/misc.c b/misc.c index 366e856..12b9d3d 100644 --- a/misc.c +++ b/misc.c @@ -179,6 +179,79 @@ concat (unsigned int num, ...) return result; } +/* If we had a standard-compliant vsnprintf() this would be a lot simpler. + Maybe in the future we'll include gnulib's version. */ + +/* Return a formatted string buffer. */ + +const char * +message_s (unsigned int length, int prefix, const char *fmt, ...) +{ + static char *buffer = NULL; + static unsigned int bsize = 0; + char *bp; + va_list args; + + /* Compute the maximum buffer size we'll need, and make sure we have it. */ + length += strlen (fmt) + strlen (program) + 4 + INTEGER_LENGTH + 1; + if (length > bsize) + { + bsize = length * 2; + buffer = xrealloc (buffer, bsize); + } + + bp = buffer; + if (prefix) + { + if (makelevel == 0) + sprintf (bp, "%s: ", program); + else + sprintf (bp, "%s[%u]: ", program, makelevel); + bp += strlen (buffer); + } + + va_start (args, fmt); + vsprintf (bp, fmt, args); + va_end (args); + + return buffer; +} + +/* Return a formatted error message in a buffer. */ + +const char * +error_s (unsigned int length, const gmk_floc *flocp, const char *fmt, ...) +{ + static char *buffer = NULL; + static unsigned int bsize = 0; + char *bp; + va_list args; + + /* Compute the maximum buffer size we'll need, and make sure we have it. */ + length += (strlen (fmt) + strlen (program) + 4 + INTEGER_LENGTH + 1 + + (flocp && flocp->filenm ? strlen (flocp->filenm) : 0)); + if (length > bsize) + { + bsize = length * 2; + buffer = xrealloc (buffer, bsize); + } + + bp = buffer; + if (flocp && flocp->filenm) + sprintf (bp, "%s:%lu: ", flocp->filenm, flocp->lineno); + else if (makelevel == 0) + sprintf (bp, "%s: ", program); + else + sprintf (bp, "%s[%u]: ", program, makelevel); + bp += strlen (bp); + + va_start (args, fmt); + vsprintf (bp, fmt, args); + va_end (args); + + return buffer; +} + /* Print a message on stdout. */ void -- cgit v1.2.3