From 48de4cb2c6492d2573ff379390fc87d3902461ed Mon Sep 17 00:00:00 2001 From: Rob Sanders Date: Thu, 25 Feb 2021 09:14:02 -0500 Subject: [PATCH 1/2] Fix issue #71 (blank lines in opt/arg help display) --- Makefile | 2 +- libcli.c | 32 ++++++++++++++++++++++---------- libcli.spec | 6 +++++- 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index fbfba05..38d8b2a 100644 --- a/Makefile +++ b/Makefile @@ -11,7 +11,7 @@ PREFIX = /usr/local MAJOR = 1 MINOR = 10 -REVISION = 6 +REVISION = 7 LIB = libcli.so LIB_STATIC = libcli.a diff --git a/libcli.c b/libcli.c index 3ff35ef..82965f6 100644 --- a/libcli.c +++ b/libcli.c @@ -962,7 +962,7 @@ void cli_get_completions(struct cli_def *cli, const char *command, char lastchar } } if (asprintf(&nameptr, "%s%s%s", delim_start, c->command, delim_end) != -1) { - if (asprintf(&strptr, " %-20s", nameptr) != -1) { + if (asprintf(&strptr, " %s", nameptr) != -1) { cli_int_wrap_help_line(strptr, c->help, comphelp); free_z(strptr); } @@ -3106,9 +3106,22 @@ int cli_int_execute_pipeline(struct cli_def *cli, struct cli_pipeline *pipeline) } /* - * Attemp quick dirty wrapping of helptext taking into account the offset from name, embedded - * cr/lf in helptext, and trying to split on last white-text before the margin + * Attempt quick dirty wrapping of helptext taking into account the offset from name, embedded + * cr/lf in helptext, and trying to split on last white-text before the right margin. If there is + * no identifiable whitespace to split on, then the split will be done on the last character to fit + * that line (currently max line with is 80 characters). + * The firstcolumn width will be a greater of 22 characters or the width of nameptr, which ever is + * greater, and will be offset from the rest of the line by one space. However, if nameptr is + * greater than 22 characters it will be put on a line by itself. The first column will be formatted + * as spaces (22 of em) for all subsequent lines. +. + * This routine assumes any 'indenting' of the nameptr field has already been done, and is solely + * concerned about wrapping the combination of nameptr and helpptr to look 'nice'. */ + +#define MAX(a,b) ((a) >(b) ? (a) : (b)) +#define MAXWIDTHCOL1 22 + void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *comphelp) { int maxwidth = 80; // temporary assumption, to be fixed later when libcli 'understands' screen dimensions int availwidth; @@ -3117,8 +3130,6 @@ void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *c char *crlf; char *line; char emptystring[] = ""; - namewidth = strlen(nameptr); - availwidth = maxwidth - namewidth; if (!helpptr) helpptr = emptystring; /* @@ -3128,13 +3139,14 @@ void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *c */ do { - // note - 22 is used because name is always indented 2 spaces - if ((nameptr != emptystring) && (namewidth > 22)) { + if ((nameptr != emptystring) && (strlen(nameptr) > MAXWIDTHCOL1)) { if (asprintf(&line, "%s", nameptr) < 0) break; cli_add_comphelp_entry(comphelp, line); nameptr = emptystring; - namewidth = 22; + namewidth = MAXWIDTHCOL1; } + namewidth = MAX(MAXWIDTHCOL1,strlen(nameptr)); + availwidth = maxwidth - namewidth -1; // subtract 1 for space separating col1 from rest of line toprint = strlen(helpptr); if (toprint > availwidth) { toprint = availwidth; @@ -3152,7 +3164,7 @@ void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *c } } - if (asprintf(&line, "%*.*s%.*s", namewidth, namewidth, nameptr, toprint, helpptr) < 0) break; + if (asprintf(&line, "%-*.*s %.*s", namewidth, namewidth, nameptr, toprint, helpptr) < 0) break; cli_add_comphelp_entry(comphelp, line); free_z(line); @@ -3267,7 +3279,7 @@ static void cli_get_optarg_comphelp(struct cli_def *cli, struct cli_optarg *opta do { char *leftcolumn; if (asprintf(&tname, "%s%s%s", delim_start, nameptr, delim_end) == -1) break; - if (asprintf(&leftcolumn, "%*.*s%-20s ", indent, indent, "", tname) == -1) break; + if (asprintf(&leftcolumn, "%*.*s%s", indent, indent, "", tname) == -1) break; cli_int_wrap_help_line(leftcolumn, helpptr, comphelp); diff --git a/libcli.spec b/libcli.spec index 0d21c11..e38d9d0 100644 --- a/libcli.spec +++ b/libcli.spec @@ -1,4 +1,4 @@ -Version: 1.10.6 +Version: 1.10.7 Summary: Cisco-like telnet command-line library Name: libcli Release: 1 @@ -67,6 +67,10 @@ rm -rf $RPM_BUILD_ROOT %defattr(-, root, root) %changelog +* Wed Feb 24 2021 Rob Sanders 1.10.7 +- Fix bug were an extra newline was being inserted on every line + when help was being requested for options and arguments + * Mon Feb 22 2021 Rob Sanders 1.10.6 - Fix bug when a command not found in the current mode, but is found in the CONFIG_MODE, which would resultin an an infinate loop. Bug From d487a2a922ed77b11c437acce7fc711ff9236568 Mon Sep 17 00:00:00 2001 From: Rob Sanders Date: Thu, 25 Feb 2021 09:30:20 -0500 Subject: [PATCH 2/2] Fix memory leak in linewrap code for help items --- libcli.c | 2 ++ libcli.spec | 1 + 2 files changed, 3 insertions(+) diff --git a/libcli.c b/libcli.c index 82965f6..67c779e 100644 --- a/libcli.c +++ b/libcli.c @@ -737,6 +737,7 @@ int cli_done(struct cli_def *cli) { if (cli->buildmode) cli_int_free_buildmode(cli); cli_unregister_tree(cli, cli->commands, CLI_ANY_COMMAND); + free_z(cli->promptchar); free_z(cli->modestring); free_z(cli->banner); free_z(cli->promptchar); @@ -3142,6 +3143,7 @@ void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *c if ((nameptr != emptystring) && (strlen(nameptr) > MAXWIDTHCOL1)) { if (asprintf(&line, "%s", nameptr) < 0) break; cli_add_comphelp_entry(comphelp, line); + free_z(line); nameptr = emptystring; namewidth = MAXWIDTHCOL1; } diff --git a/libcli.spec b/libcli.spec index e38d9d0..fc2725c 100644 --- a/libcli.spec +++ b/libcli.spec @@ -70,6 +70,7 @@ rm -rf $RPM_BUILD_ROOT * Wed Feb 24 2021 Rob Sanders 1.10.7 - Fix bug were an extra newline was being inserted on every line when help was being requested for options and arguments +- Fix memory leak in linewrapping code for help items * Mon Feb 22 2021 Rob Sanders 1.10.6 - Fix bug when a command not found in the current mode, but is found