summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/bar.c28
-rw-r--r--src/main.c48
-rw-r--r--src/ratpoison.h3
-rw-r--r--src/sbuf.c30
-rw-r--r--src/sbuf.h2
5 files changed, 84 insertions, 27 deletions
diff --git a/src/bar.c b/src/bar.c
index 8e34d25..f2a6940 100644
--- a/src/bar.c
+++ b/src/bar.c
@@ -29,7 +29,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <stdarg.h>
#include "ratpoison.h"
@@ -121,36 +120,11 @@ update_window_names (screen_info *s)
void
marked_message_printf (int mark_start, int mark_end, char *fmt, ...)
{
- int size, nchars;
char *buffer;
va_list ap;
va_start (ap, fmt);
-
- /* A resonable starting value. */
- size = strlen (fmt) + 1;
- buffer = (char *)xmalloc (size);
-
- nchars = vsnprintf (buffer, size, fmt, ap);
-
- /* From the GNU Libc manual: In versions of the GNU C library prior
- to 2.1 the return value is the number of characters stored, not
- including the terminating null; unless there was not enough space
- in S to store the result in which case `-1' is returned. */
- if (nchars == -1)
- {
- do
- {
- size *= 2;
- buffer = (char *)xrealloc (buffer, size);
- } while (vsnprintf (buffer, size, fmt, ap) == -1);
- }
- else if (nchars >= size)
- {
- buffer = (char *)xrealloc (buffer, nchars + 1);
- vsnprintf (buffer, nchars + 1, fmt, ap);
- }
-
+ buffer = xvsprintf (fmt, ap);
va_end (ap);
marked_message (buffer, mark_start, mark_end);
diff --git a/src/main.c b/src/main.c
index b25a163..3a41edc 100644
--- a/src/main.c
+++ b/src/main.c
@@ -124,6 +124,54 @@ xstrdup (char *s)
return value;
}
+/* Return a new string based on fmt. */
+char *
+xvsprintf (char *fmt, va_list ap)
+{
+ int size, nchars;
+ char *buffer;
+
+ /* A resonable starting value. */
+ size = strlen (fmt) + 1;
+ buffer = (char *)xmalloc (size);
+
+ nchars = vsnprintf (buffer, size, fmt, ap);
+
+ /* From the GNU Libc manual: In versions of the GNU C library prior
+ to 2.1 the return value is the number of characters stored, not
+ including the terminating null; unless there was not enough space
+ in S to store the result in which case `-1' is returned. */
+ if (nchars == -1)
+ {
+ do
+ {
+ size *= 2;
+ buffer = (char *)xrealloc (buffer, size);
+ } while (vsnprintf (buffer, size, fmt, ap) == -1);
+ }
+ else if (nchars >= size)
+ {
+ buffer = (char *)xrealloc (buffer, nchars + 1);
+ vsnprintf (buffer, nchars + 1, fmt, ap);
+ }
+
+ return buffer;
+}
+
+/* Return a new string based on fmt. */
+char *
+xsprintf (char *fmt, ...)
+{
+ char *buffer;
+ va_list ap;
+
+ va_start (ap, fmt);
+ buffer = xvsprintf (fmt, ap);
+ va_end (ap);
+
+ return buffer;
+}
+
void
sighandler (int signum)
{
diff --git a/src/ratpoison.h b/src/ratpoison.h
index f8c6c53..293e868 100644
--- a/src/ratpoison.h
+++ b/src/ratpoison.h
@@ -28,6 +28,7 @@
#include <stdlib.h>
#include <stdio.h>
+#include <stdarg.h>
#include <X11/Xlib.h>
/* Some error reporting macros */
@@ -68,5 +69,7 @@ void fatal (const char *msg);
void *xmalloc (size_t size);
void *xrealloc (void *ptr, size_t size);
char *xstrdup (char *s);
+char *xsprintf (char *fmt, ...);
+char *xvsprintf (char *fmt, va_list ap);
#endif /* ! _RATPOISON_H */
diff --git a/src/sbuf.c b/src/sbuf.c
index bd77d7c..f6313b2 100644
--- a/src/sbuf.c
+++ b/src/sbuf.c
@@ -89,3 +89,33 @@ sbuf_get (struct sbuf *b)
{
return b->data;
}
+
+char *
+sbuf_printf (struct sbuf *b, char *fmt, ...)
+{
+ va_list ap;
+
+ free (b->data);
+
+ va_start (ap, fmt);
+ b->data = xvsprintf (fmt, ap);
+ va_end (ap);
+
+ return b->data;
+}
+
+char *
+sbuf_printf_concat (struct sbuf *b, char *fmt, ...)
+{
+ char *buffer;
+ va_list ap;
+
+ va_start (ap, fmt);
+ buffer = xvsprintf (fmt, ap);
+ va_end (ap);
+
+ sbuf_concat (b, buffer);
+ free (buffer);
+
+ return b->data;
+}
diff --git a/src/sbuf.h b/src/sbuf.h
index d25ee90..32cb4fe 100644
--- a/src/sbuf.h
+++ b/src/sbuf.h
@@ -38,5 +38,7 @@ char *sbuf_concat (struct sbuf *b, const char *str);
char *sbuf_copy (struct sbuf *b, const char *str);
char *sbuf_clear (struct sbuf *b);
char *sbuf_get (struct sbuf *b);
+char *sbuf_printf (struct sbuf *b, char *fmt, ...);
+char *sbuf_printf_concat (struct sbuf *b, char *fmt, ...);
#endif /* ! _RATPOISON_SBUF_H */