This is the mail archive of the gdb-patches@sources.redhat.com mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[patch/rfc] xstrvprintf to match xstrprintf


Hello,

This is a fairly logical follow-on from xstrprintf.  It introduces:
	char *xstrvprintf (const char *fmt, va_list ap);
to match:
	char *xstrprintf (const char *fmt, ...);
I'll look to commit in a few days,
Andrew

2004-06-26  Andrew Cagney  <cagney@gnu.org>

	* defs.h (xstrvprintf): Declare.
	* utils.c (xstrvprintf): New function.
	(internal_vproblem, xstrprintf, xasprintf) 
	(vfprintf_maybe_filtered, vfprintf_unfiltered): Use xstrvprintf.
	* serial.c (serial_printf): Ditto.
	* complaints.c (vcomplaint): Ditto.

Index: complaints.c
===================================================================
RCS file: /cvs/src/src/gdb/complaints.c,v
retrieving revision 1.13
diff -p -u -r1.13 complaints.c
--- complaints.c	21 Apr 2004 23:52:20 -0000	1.13
+++ complaints.c	26 Jun 2004 19:16:36 -0000
@@ -197,7 +197,7 @@ vcomplaint (struct complaints **c, const
 	{
 	  char *msg;
 	  struct cleanup *cleanups;
-	  xvasprintf (&msg, complaint->fmt, args);
+	  msg = xstrvprintf (complaint->fmt, args);
 	  cleanups = make_cleanup (xfree, msg);
 	  wrap_here ("");
 	  if (series != SUBSEQUENT_MESSAGE)
Index: defs.h
===================================================================
RCS file: /cvs/src/src/gdb/defs.h,v
retrieving revision 1.150
diff -p -u -r1.150 defs.h
--- defs.h	25 Jun 2004 19:46:07 -0000	1.150
+++ defs.h	26 Jun 2004 19:16:37 -0000
@@ -892,8 +892,10 @@ extern void xfree (void *);
 extern void xasprintf (char **ret, const char *format, ...) ATTR_FORMAT (printf, 2, 3);
 extern void xvasprintf (char **ret, const char *format, va_list ap);
 
-/* Like asprintf, but return the string, throw an error if no memory.  */
+/* Like asprintf and vasprintf, but return the string, throw an error
+   if no memory.  */
 extern char *xstrprintf (const char *format, ...) ATTR_FORMAT (printf, 1, 2);
+extern char *xstrvprintf (const char *format, va_list ap);
 
 extern int parse_escape (char **);
 
Index: serial.c
===================================================================
RCS file: /cvs/src/src/gdb/serial.c,v
retrieving revision 1.15
diff -p -u -r1.15 serial.c
--- serial.c	19 Mar 2002 02:51:07 -0000	1.15
+++ serial.c	26 Jun 2004 19:16:37 -0000
@@ -394,7 +394,7 @@ serial_printf (struct serial *desc, cons
   char *buf;
   va_start (args, format);
 
-  xvasprintf (&buf, format, args);
+  buf = xstrvprintf (format, args);
   serial_write (desc, buf, strlen (buf));
 
   xfree (buf);
Index: utils.c
===================================================================
RCS file: /cvs/src/src/gdb/utils.c,v
retrieving revision 1.121
diff -p -u -r1.121 utils.c
--- utils.c	11 May 2004 19:19:34 -0000	1.121
+++ utils.c	26 Jun 2004 19:16:38 -0000
@@ -752,7 +752,7 @@ internal_vproblem (struct internal_probl
      so that the user knows that they are living on the edge.  */
   {
     char *msg;
-    xvasprintf (&msg, fmt, ap);
+    msg = xstrvprintf (fmt, ap);
     xasprintf (&reason, "\
 %s:%d: %s: %s\n\
 A problem internal to GDB has been detected,\n\
@@ -1156,7 +1156,7 @@ xstrprintf (const char *format, ...)
   char *ret;
   va_list args;
   va_start (args, format);
-  xvasprintf (&ret, format, args);
+  ret = xstrvprintf (format, args);
   va_end (args);
   return ret;
 }
@@ -1166,7 +1166,7 @@ xasprintf (char **ret, const char *forma
 {
   va_list args;
   va_start (args, format);
-  xvasprintf (ret, format, args);
+  (*ret) = xstrvprintf (format, args);
   va_end (args);
 }
 
@@ -1186,6 +1186,21 @@ xvasprintf (char **ret, const char *form
 		    "vasprintf call failed (errno %d)", errno);
 }
 
+char *
+xstrvprintf (const char *format, va_list ap)
+{
+  char *ret = NULL;
+  int status = vasprintf (&ret, format, ap);
+  /* NULL is returned when there was a memory allocation problem.  */
+  if (ret == NULL)
+    nomem (0);
+  /* A negative status (the printed length) with a non-NULL buffer
+     should never happen, but just to be sure.  */
+  if (status < 0)
+    internal_error (__FILE__, __LINE__,
+		    "vasprintf call failed (errno %d)", errno);
+  return ret;
+}
 
 /* My replacement for the read system call.
    Used like `read' but keeps going if `read' returns too soon.  */
@@ -2260,7 +2275,7 @@ vfprintf_maybe_filtered (struct ui_file 
   char *linebuffer;
   struct cleanup *old_cleanups;
 
-  xvasprintf (&linebuffer, format, args);
+  linebuffer = xstrvprintf (format, args);
   old_cleanups = make_cleanup (xfree, linebuffer);
   fputs_maybe_filtered (linebuffer, stream, filter);
   do_cleanups (old_cleanups);
@@ -2279,7 +2294,7 @@ vfprintf_unfiltered (struct ui_file *str
   char *linebuffer;
   struct cleanup *old_cleanups;
 
-  xvasprintf (&linebuffer, format, args);
+  linebuffer = xstrvprintf (format, args);
   old_cleanups = make_cleanup (xfree, linebuffer);
   fputs_unfiltered (linebuffer, stream);
   do_cleanups (old_cleanups);

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]