Skip to content

Commit 48de4cb

Browse files
author
Rob Sanders
committed
Fix issue #71 (blank lines in opt/arg help display)
1 parent c349322 commit 48de4cb

3 files changed

Lines changed: 28 additions & 12 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ PREFIX = /usr/local
1111

1212
MAJOR = 1
1313
MINOR = 10
14-
REVISION = 6
14+
REVISION = 7
1515
LIB = libcli.so
1616
LIB_STATIC = libcli.a
1717

libcli.c

Lines changed: 22 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -962,7 +962,7 @@ void cli_get_completions(struct cli_def *cli, const char *command, char lastchar
962962
}
963963
}
964964
if (asprintf(&nameptr, "%s%s%s", delim_start, c->command, delim_end) != -1) {
965-
if (asprintf(&strptr, " %-20s", nameptr) != -1) {
965+
if (asprintf(&strptr, " %s", nameptr) != -1) {
966966
cli_int_wrap_help_line(strptr, c->help, comphelp);
967967
free_z(strptr);
968968
}
@@ -3106,9 +3106,22 @@ int cli_int_execute_pipeline(struct cli_def *cli, struct cli_pipeline *pipeline)
31063106
}
31073107

31083108
/*
3109-
* Attemp quick dirty wrapping of helptext taking into account the offset from name, embedded
3110-
* cr/lf in helptext, and trying to split on last white-text before the margin
3109+
* Attempt quick dirty wrapping of helptext taking into account the offset from name, embedded
3110+
* cr/lf in helptext, and trying to split on last white-text before the right margin. If there is
3111+
* no identifiable whitespace to split on, then the split will be done on the last character to fit
3112+
* that line (currently max line with is 80 characters).
3113+
* The firstcolumn width will be a greater of 22 characters or the width of nameptr, which ever is
3114+
* greater, and will be offset from the rest of the line by one space. However, if nameptr is
3115+
* greater than 22 characters it will be put on a line by itself. The first column will be formatted
3116+
* as spaces (22 of em) for all subsequent lines.
3117+
.
3118+
* This routine assumes any 'indenting' of the nameptr field has already been done, and is solely
3119+
* concerned about wrapping the combination of nameptr and helpptr to look 'nice'.
31113120
*/
3121+
3122+
#define MAX(a,b) ((a) >(b) ? (a) : (b))
3123+
#define MAXWIDTHCOL1 22
3124+
31123125
void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *comphelp) {
31133126
int maxwidth = 80; // temporary assumption, to be fixed later when libcli 'understands' screen dimensions
31143127
int availwidth;
@@ -3117,8 +3130,6 @@ void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *c
31173130
char *crlf;
31183131
char *line;
31193132
char emptystring[] = "";
3120-
namewidth = strlen(nameptr);
3121-
availwidth = maxwidth - namewidth;
31223133

31233134
if (!helpptr) helpptr = emptystring;
31243135
/*
@@ -3128,13 +3139,14 @@ void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *c
31283139
*/
31293140

31303141
do {
3131-
// note - 22 is used because name is always indented 2 spaces
3132-
if ((nameptr != emptystring) && (namewidth > 22)) {
3142+
if ((nameptr != emptystring) && (strlen(nameptr) > MAXWIDTHCOL1)) {
31333143
if (asprintf(&line, "%s", nameptr) < 0) break;
31343144
cli_add_comphelp_entry(comphelp, line);
31353145
nameptr = emptystring;
3136-
namewidth = 22;
3146+
namewidth = MAXWIDTHCOL1;
31373147
}
3148+
namewidth = MAX(MAXWIDTHCOL1,strlen(nameptr));
3149+
availwidth = maxwidth - namewidth -1; // subtract 1 for space separating col1 from rest of line
31383150
toprint = strlen(helpptr);
31393151
if (toprint > availwidth) {
31403152
toprint = availwidth;
@@ -3152,7 +3164,7 @@ void cli_int_wrap_help_line(char *nameptr, char *helpptr, struct cli_comphelp *c
31523164
}
31533165
}
31543166

3155-
if (asprintf(&line, "%*.*s%.*s", namewidth, namewidth, nameptr, toprint, helpptr) < 0) break;
3167+
if (asprintf(&line, "%-*.*s %.*s", namewidth, namewidth, nameptr, toprint, helpptr) < 0) break;
31563168
cli_add_comphelp_entry(comphelp, line);
31573169
free_z(line);
31583170

@@ -3267,7 +3279,7 @@ static void cli_get_optarg_comphelp(struct cli_def *cli, struct cli_optarg *opta
32673279
do {
32683280
char *leftcolumn;
32693281
if (asprintf(&tname, "%s%s%s", delim_start, nameptr, delim_end) == -1) break;
3270-
if (asprintf(&leftcolumn, "%*.*s%-20s ", indent, indent, "", tname) == -1) break;
3282+
if (asprintf(&leftcolumn, "%*.*s%s", indent, indent, "", tname) == -1) break;
32713283

32723284
cli_int_wrap_help_line(leftcolumn, helpptr, comphelp);
32733285

libcli.spec

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
Version: 1.10.6
1+
Version: 1.10.7
22
Summary: Cisco-like telnet command-line library
33
Name: libcli
44
Release: 1
@@ -67,6 +67,10 @@ rm -rf $RPM_BUILD_ROOT
6767
%defattr(-, root, root)
6868

6969
%changelog
70+
* Wed Feb 24 2021 Rob Sanders <rsanders.forcepoint.com> 1.10.7
71+
- Fix bug were an extra newline was being inserted on every line
72+
when help was being requested for options and arguments
73+
7074
* Mon Feb 22 2021 Rob Sanders <rsanders.forcepoint.com> 1.10.6
7175
- Fix bug when a command not found in the current mode, but is found
7276
in the CONFIG_MODE, which would resultin an an infinate loop. Bug

0 commit comments

Comments
 (0)