All of lore.kernel.org
 help / color / mirror / Atom feed
From: Benjamin Marzinski <bmarzins@redhat.com>
To: mwilck@suse.com
Cc: dm-devel@redhat.com
Subject: Re: [dm-devel] [PATCH 9/9] multipathd: use strbuf in cli.c
Date: Thu, 29 Jul 2021 10:46:02 -0500	[thread overview]
Message-ID: <20210729154602.GQ3087@octiron.msp.redhat.com> (raw)
In-Reply-To: <20210715105223.30463-10-mwilck@suse.com>

On Thu, Jul 15, 2021 at 12:52:23PM +0200, mwilck@suse.com wrote:
> 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


      reply	other threads:[~2021-07-29 15:47 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-15 10:52 [dm-devel] [PATCH 0/9] multipath-tools: use variable-size string buffers mwilck
2021-07-15 10:52 ` [dm-devel] [PATCH 1/9] libmultipath: variable-size parameters in dm_get_map() mwilck
2021-07-26 22:17   ` Benjamin Marzinski
2021-08-11 14:18     ` Martin Wilck
2021-07-15 10:52 ` [dm-devel] [PATCH 2/9] libmultipath: strbuf: simple api for growing string buffers mwilck
2021-07-27  4:54   ` Benjamin Marzinski
2021-08-11 15:03     ` Martin Wilck
2021-07-15 10:52 ` [dm-devel] [PATCH 3/9] libmultipath: variable-size parameters in assemble_map() mwilck
2021-07-28 15:54   ` Benjamin Marzinski
2021-08-11 15:15     ` Martin Wilck
2021-07-15 10:52 ` [dm-devel] [PATCH 4/9] libmultipath: use strbuf in dict.c mwilck
2021-07-28 19:03   ` Benjamin Marzinski
2021-08-11 15:27     ` Martin Wilck
2021-08-30 18:23       ` Benjamin Marzinski
2021-07-15 10:52 ` [dm-devel] [PATCH 5/9] libmultipath: use strbuf in print.c mwilck
2021-07-29  0:00   ` Benjamin Marzinski
2021-07-15 10:52 ` [dm-devel] [PATCH 6/9] libmultipath: print.c: fail hard if keywords are not found mwilck
2021-07-29  2:36   ` Benjamin Marzinski
2021-07-15 10:52 ` [dm-devel] [PATCH 7/9] libmultipath: print.h: move macros to print.c mwilck
2021-07-29  2:36   ` Benjamin Marzinski
2021-07-15 10:52 ` [dm-devel] [PATCH 8/9] libmultipath: use strbuf in alias.c mwilck
2021-07-29 15:22   ` Benjamin Marzinski
2021-07-15 10:52 ` [dm-devel] [PATCH 9/9] multipathd: use strbuf in cli.c mwilck
2021-07-29 15:46   ` Benjamin Marzinski [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=20210729154602.GQ3087@octiron.msp.redhat.com \
    --to=bmarzins@redhat.com \
    --cc=dm-devel@redhat.com \
    --cc=mwilck@suse.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.