Skip to content

Commit

Permalink
Merge pull request #73 from RobSanders/libcli_1_10_7_staging
Browse files Browse the repository at this point in the history
Libcli 1 10 7 staging
  • Loading branch information
RobSanders authored Feb 25, 2021
2 parents 54c0fe1 + d487a2a commit 0f8d257
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 12 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ PREFIX = /usr/local

MAJOR = 1
MINOR = 10
REVISION = 6
REVISION = 7
LIB = libcli.so
LIB_STATIC = libcli.a

Expand Down
34 changes: 24 additions & 10 deletions libcli.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -962,7 +963,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);
}
Expand Down Expand Up @@ -3106,9 +3107,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;
Expand All @@ -3117,8 +3131,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;
/*
Expand All @@ -3128,13 +3140,15 @@ 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);
free_z(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;
Expand All @@ -3152,7 +3166,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);

Expand Down Expand Up @@ -3267,7 +3281,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);

Expand Down
7 changes: 6 additions & 1 deletion libcli.spec
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
Version: 1.10.6
Version: 1.10.7
Summary: Cisco-like telnet command-line library
Name: libcli
Release: 1
Expand Down Expand Up @@ -67,6 +67,11 @@ rm -rf $RPM_BUILD_ROOT
%defattr(-, root, root)

%changelog
* Wed Feb 24 2021 Rob Sanders <rsanders.forcepoint.com> 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 <rsanders.forcepoint.com> 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
Expand Down

0 comments on commit 0f8d257

Please sign in to comment.