dm-devel.redhat.com archive mirror
 help / color / mirror / Atom feed
From: mwilck@suse.com
To: Benjamin Marzinski <bmarzins@redhat.com>,
	Christophe Varoqui <christophe.varoqui@opensvc.com>
Cc: dm-devel@redhat.com, Martin Wilck <mwilck@suse.com>
Subject: [dm-devel] [PATCH v2 9/9] multipathd: use strbuf in cli.c
Date: Wed, 11 Aug 2021 17:41:50 +0200	[thread overview]
Message-ID: <20210811154150.24714-10-mwilck@suse.com> (raw)
In-Reply-To: <20210811154150.24714-1-mwilck@suse.com>

From: Martin Wilck <mwilck@suse.com>

Here, too, strbuf can be used to simplify code.

Reviewed-by: Benjamin Marzinski <bmarzins@redhat.com>
Signed-off-by: Martin Wilck <mwilck@suse.com>
---
 multipathd/cli.c | 94 ++++++++++++++++++------------------------------
 1 file changed, 34 insertions(+), 60 deletions(-)

diff --git a/multipathd/cli.c b/multipathd/cli.c
index bdc9fb1..4d6c37c 100644
--- a/multipathd/cli.c
+++ b/multipathd/cli.c
@@ -16,6 +16,7 @@
 #include "mpath_cmd.h"
 #include "cli.h"
 #include "debug.h"
+#include "strbuf.h"
 
 static vector keys;
 static vector handlers;
@@ -354,107 +355,80 @@ alloc_handlers (void)
 }
 
 static int
-genhelp_sprint_aliases (char * reply, int maxlen, vector keys,
+genhelp_sprint_aliases (struct strbuf *reply, vector keys,
 			struct key * refkw)
 {
-	int i, len = 0;
+	int i;
 	struct key * kw;
+	size_t initial_len = get_strbuf_len(reply);
 
 	vector_foreach_slot (keys, kw, i) {
-		if (kw->code == refkw->code && kw != refkw) {
-			len += snprintf(reply + len, maxlen - len,
-					"|%s", kw->str);
-			if (len >= maxlen)
-				return len;
-		}
+		if (kw->code == refkw->code && kw != refkw &&
+		    print_strbuf(reply, "|%s", kw->str) < 0)
+			return -1;
 	}
 
-	return len;
+	return get_strbuf_len(reply) - initial_len;
 }
 
 static int
-do_genhelp(char *reply, int maxlen, const char *cmd, int error) {
-	int len = 0;
+do_genhelp(struct strbuf *reply, const char *cmd, int error) {
 	int i, j;
 	uint64_t fp;
 	struct handler * h;
 	struct key * kw;
+	int rc = 0;
+	size_t initial_len = get_strbuf_len(reply);
 
 	switch(error) {
 	case ENOMEM:
-		len += snprintf(reply + len, maxlen - len,
-				"%s: Not enough memory\n", cmd);
+		rc = print_strbuf(reply, "%s: Not enough memory\n", cmd);
 		break;
 	case EAGAIN:
-		len += snprintf(reply + len, maxlen - len,
-				"%s: not found\n", cmd);
+		rc = print_strbuf(reply, "%s: not found\n", cmd);
 		break;
 	case EINVAL:
-		len += snprintf(reply + len, maxlen - len,
-				"%s: Missing argument\n", cmd);
+		rc = print_strbuf(reply, "%s: Missing argument\n", cmd);
 		break;
 	}
-	if (len >= maxlen)
-		goto out;
-	len += snprintf(reply + len, maxlen - len, VERSION_STRING);
-	if (len >= maxlen)
-		goto out;
-	len += snprintf(reply + len, maxlen - len, "CLI commands reference:\n");
-	if (len >= maxlen)
-		goto out;
+	if (rc < 0)
+		return -1;
+
+	if (print_strbuf(reply, VERSION_STRING) < 0 ||
+	    append_strbuf_str(reply, "CLI commands reference:\n") < 0)
+		return -1;
 
 	vector_foreach_slot (handlers, h, i) {
 		fp = h->fingerprint;
 		vector_foreach_slot (keys, kw, j) {
 			if ((kw->code & fp)) {
 				fp -= kw->code;
-				len += snprintf(reply + len , maxlen - len,
-						" %s", kw->str);
-				if (len >= maxlen)
-					goto out;
-				len += genhelp_sprint_aliases(reply + len,
-							      maxlen - len,
-							      keys, kw);
-				if (len >= maxlen)
-					goto out;
+				if (print_strbuf(reply, " %s", kw->str) < 0 ||
+				    genhelp_sprint_aliases(reply, keys, kw) < 0)
+					return -1;
 
 				if (kw->has_param) {
-					len += snprintf(reply + len,
-							maxlen - len,
-							" $%s", kw->str);
-					if (len >= maxlen)
-						goto out;
+					if (print_strbuf(reply, " $%s",
+							 kw->str) < 0)
+						return -1;
 				}
 			}
 		}
-		len += snprintf(reply + len, maxlen - len, "\n");
-		if (len >= maxlen)
-			goto out;
+		if (append_strbuf_str(reply, "\n") < 0)
+			return -1;
 	}
-out:
-	return len;
+	return get_strbuf_len(reply) - initial_len;
 }
 
 
 static char *
 genhelp_handler (const char *cmd, int error)
 {
-	char * reply;
-	char * p = NULL;
-	int maxlen = INITIAL_REPLY_LEN;
-	int again = 1;
-
-	reply = MALLOC(maxlen);
-
-	while (again) {
-		if (!reply)
-			return NULL;
-		p = reply;
-		p += do_genhelp(reply, maxlen, cmd, error);
-		again = ((p - reply) >= maxlen);
-		REALLOC_REPLY(reply, again, maxlen);
-	}
-	return reply;
+	STRBUF_ON_STACK(reply);
+
+	if (do_genhelp(&reply, cmd, error) == -1)
+		condlog(0, "genhelp_handler: out of memory");
+	return steal_strbuf_str(&reply);
 }
 
 int
-- 
2.32.0


--
dm-devel mailing list
dm-devel@redhat.com
https://listman.redhat.com/mailman/listinfo/dm-devel


      parent reply	other threads:[~2021-08-11 15:42 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-11 15:41 [dm-devel] [PATCH v2 0/9] multipath-tools: use variable-size string buffers mwilck
2021-08-11 15:41 ` [dm-devel] [PATCH v2 1/9] libmultipath: variable-size parameters in dm_get_map() mwilck
2021-08-30 20:44   ` Benjamin Marzinski
2021-08-11 15:41 ` [dm-devel] [PATCH v2 2/9] libmultipath: strbuf: simple api for growing string buffers mwilck
2021-08-11 16:08   ` Bart Van Assche
2021-08-12  7:19     ` Martin Wilck
2021-08-30 20:45   ` Benjamin Marzinski
2021-08-11 15:41 ` [dm-devel] [PATCH v2 3/9] libmultipath: variable-size parameters in assemble_map() mwilck
2021-08-30 20:45   ` Benjamin Marzinski
2021-08-11 15:41 ` [dm-devel] [PATCH v2 4/9] libmultipath: use strbuf in dict.c mwilck
2021-08-30 20:46   ` Benjamin Marzinski
2021-08-11 15:41 ` [dm-devel] [PATCH v2 5/9] libmultipath: use strbuf in print.c mwilck
2021-08-30 20:47   ` Benjamin Marzinski
2021-08-11 15:41 ` [dm-devel] [PATCH v2 6/9] libmultipath: print.c: fail hard if keywords are not found mwilck
2021-08-11 15:41 ` [dm-devel] [PATCH v2 7/9] libmultipath: print.h: move macros to print.c mwilck
2021-08-11 15:41 ` [dm-devel] [PATCH v2 8/9] libmultipath: use strbuf in alias.c mwilck
2021-08-11 15:41 ` mwilck [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210811154150.24714-10-mwilck@suse.com \
    --to=mwilck@suse.com \
    --cc=bmarzins@redhat.com \
    --cc=christophe.varoqui@opensvc.com \
    --cc=dm-devel@redhat.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).