diff options
author | Markus Armbruster <armbru@redhat.com> | 2019-04-17 21:06:36 +0200 |
---|---|---|
committer | Markus Armbruster <armbru@redhat.com> | 2019-04-18 22:18:59 +0200 |
commit | 679cb8e1a1e5d10f44ac9e40fe1458cbfd720ebb (patch) | |
tree | 388f5556a444d8037c3b48fe05ccc41cddbe2591 /monitor.c | |
parent | 26eaf2cd0dd31d7bdb2a5ac53f32a051754b0c30 (diff) |
monitor error: Make printf()-like functions return a value
printf() & friends return the number of characters written on success,
negative value on error.
monitor_printf(), monitor_vfprintf(), monitor_vprintf(),
error_printf(), error_printf_unless_qmp(), error_vprintf(), and
error_vprintf_unless_qmp() return void. Some of them carry a TODO
comment asking for int instead.
Improve them to return int like printf() does.
This makes our use of monitor_printf() as fprintf_function slightly
less dirty: the function cast no longer adds a return value that isn't
there. It still changes a parameter's pointer type. That will be
addressed in a future commit.
monitor_vfprintf() always returns zero. Improve it to return the
proper value.
Cc: Dr. David Alan Gilbert <dgilbert@redhat.com>
Signed-off-by: Markus Armbruster <armbru@redhat.com>
Reviewed-by: Dr. David Alan Gilbert <dgilbert@redhat.com>
Message-Id: <20190417190641.26814-11-armbru@redhat.com>
Diffstat (limited to 'monitor.c')
-rw-r--r-- | monitor.c | 61 |
1 files changed, 33 insertions, 28 deletions
@@ -430,15 +430,14 @@ void monitor_flush(Monitor *mon) } /* flush at every end of line */ -static void monitor_puts(Monitor *mon, const char *str) +static int monitor_puts(Monitor *mon, const char *str) { + int i; char c; qemu_mutex_lock(&mon->mon_lock); - for(;;) { - c = *str++; - if (c == '\0') - break; + for (i = 0; str[i]; i++) { + c = str[i]; if (c == '\n') { qstring_append_chr(mon->outbuf, '\r'); } @@ -448,39 +447,48 @@ static void monitor_puts(Monitor *mon, const char *str) } } qemu_mutex_unlock(&mon->mon_lock); + + return i; } -void monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) +int monitor_vprintf(Monitor *mon, const char *fmt, va_list ap) { char *buf; + int n; if (!mon) - return; + return -1; if (monitor_is_qmp(mon)) { - return; + return -1; } buf = g_strdup_vprintf(fmt, ap); - monitor_puts(mon, buf); + n = monitor_puts(mon, buf); g_free(buf); + return n; } -void monitor_printf(Monitor *mon, const char *fmt, ...) +int monitor_printf(Monitor *mon, const char *fmt, ...) { + int ret; + va_list ap; va_start(ap, fmt); - monitor_vprintf(mon, fmt, ap); + ret = monitor_vprintf(mon, fmt, ap); va_end(ap); + return ret; } int monitor_fprintf(FILE *stream, const char *fmt, ...) { + int ret; + va_list ap; va_start(ap, fmt); - monitor_vprintf((Monitor *)stream, fmt, ap); + ret = monitor_vprintf((Monitor *)stream, fmt, ap); va_end(ap); - return 0; + return ret; } static void qmp_send_response(Monitor *mon, const QDict *rsp) @@ -4535,35 +4543,32 @@ static void monitor_readline_flush(void *opaque) /* * Print to current monitor if we have one, else to stream. - * TODO should return int, so callers can calculate width, but that - * requires surgery to monitor_vprintf(). Left for another day. */ -void monitor_vfprintf(FILE *stream, const char *fmt, va_list ap) +int monitor_vfprintf(FILE *stream, const char *fmt, va_list ap) { if (cur_mon && !monitor_cur_is_qmp()) { - monitor_vprintf(cur_mon, fmt, ap); - } else { - vfprintf(stream, fmt, ap); + return monitor_vprintf(cur_mon, fmt, ap); } + return vfprintf(stream, fmt, ap); } /* * Print to current monitor if we have one, else to stderr. - * TODO should return int, so callers can calculate width, but that - * requires surgery to monitor_vprintf(). Left for another day. */ -void error_vprintf(const char *fmt, va_list ap) +int error_vprintf(const char *fmt, va_list ap) { - monitor_vfprintf(stderr, fmt, ap); + return monitor_vfprintf(stderr, fmt, ap); } -void error_vprintf_unless_qmp(const char *fmt, va_list ap) +int error_vprintf_unless_qmp(const char *fmt, va_list ap) { - if (cur_mon && !monitor_cur_is_qmp()) { - monitor_vprintf(cur_mon, fmt, ap); - } else if (!cur_mon) { - vfprintf(stderr, fmt, ap); + if (!cur_mon) { + return vfprintf(stderr, fmt, ap); } + if (!monitor_cur_is_qmp()) { + return monitor_vprintf(cur_mon, fmt, ap); + } + return -1; } static void monitor_list_append(Monitor *mon) |