summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
authorPaul Smith <psmith@gnu.org>2013-04-28 19:09:20 -0400
committerPaul Smith <psmith@gnu.org>2013-04-28 19:09:20 -0400
commitf88eb23b02088e7549c46231d0dfa4ee8d35a365 (patch)
treef4aa139eaa7cd62e90cf918e96e77402b7355f8f /misc.c
parent441b64335590ab91854b997bec07a9578086d895 (diff)
downloadgunmake-f88eb23b02088e7549c46231d0dfa4ee8d35a365.tar.gz
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.
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c73
1 files changed, 73 insertions, 0 deletions
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