Skip to content

Commit

Permalink
scanner: simplify scanopt_usage().
Browse files Browse the repository at this point in the history
In-function macro CALC_LEN removed and no longer needed.

Local variable renames:

    maxlen[0] => opt_col_width

    maxlen[1] => desc_col_width

    indent now explicitly const
  • Loading branch information
Explorer09 authored and westes committed Dec 8, 2017
1 parent 2388238 commit aa775c8
Showing 1 changed file with 19 additions and 44 deletions.
63 changes: 19 additions & 44 deletions src/scanopt.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,16 @@ typedef struct usg_elem usg_elem;
int scanopt_usage (scanopt_t *scanner, FILE *fp, const char *usage)
{
struct _scanopt_t *s;
int i, columns, indent = 2;
int i, columns;
const int indent = 2;
usg_elem *byr_val = NULL; /* option indices sorted by r_val */
usg_elem *store; /* array of preallocated elements. */
int store_idx = 0;
usg_elem *ue;
int maxlen[2];
int desccol = 0;
int opt_col_width = 0, desc_col_width = 0;
int desccol;
int print_run = 0;

maxlen[0] = 0;
maxlen[1] = 0;

s = (struct _scanopt_t *) scanner;

if (usage) {
Expand Down Expand Up @@ -315,56 +313,33 @@ int scanopt_usage (scanopt_t *scanner, FILE *fp, const char *usage)
/* first pass calculate how much room we need. */
for (ue = byr_val; ue; ue = ue->next) {
usg_elem *ap;
int len = 0;
int nshort = 0, nlong = 0;


#define CALC_LEN(i) do {\
if(FLAGS(s,i) & IS_LONG) \
len += (nlong++||nshort) ? 2+PRINTLEN(s,i) : PRINTLEN(s,i);\
else\
len += (nshort++||nlong)? 2+PRINTLEN(s,i) : PRINTLEN(s,i);\
}while(0)
int len;

if (!(FLAGS (s, ue->idx) & IS_LONG))
CALC_LEN (ue->idx);
len = PRINTLEN(s, ue->idx);

/* do short aliases first. */
for (ap = ue->alias; ap; ap = ap->next) {
if (FLAGS (s, ap->idx) & IS_LONG)
continue;
CALC_LEN (ap->idx);
}

if (FLAGS (s, ue->idx) & IS_LONG)
CALC_LEN (ue->idx);

/* repeat the above loop, this time for long aliases. */
for (ap = ue->alias; ap; ap = ap->next) {
if (!(FLAGS (s, ap->idx) & IS_LONG))
continue;
CALC_LEN (ap->idx);
len += PRINTLEN(s, ap->idx) + (int) strlen(", ");
}

if (len > maxlen[0])
maxlen[0] = len;
if (len > opt_col_width)
opt_col_width = len;

/* It's much easier to calculate length for description column! */
len = (int) strlen (DESC (s, ue->idx));
if (len > maxlen[1])
maxlen[1] = len;
if (len > desc_col_width)
desc_col_width = len;
}

/* Determine how much room we have, and how much we will allocate to each col.
* Do not address pathological cases. Output will just be ugly. */
columns = get_cols () - 1;
if (maxlen[0] + maxlen[1] + indent * 2 > columns) {
/* col 0 gets whatever it wants. we'll wrap the desc col. */
maxlen[1] = columns - (maxlen[0] + indent * 2);
if (maxlen[1] < 14) /* 14 is arbitrary lower limit on desc width. */
maxlen[1] = INT_MAX;
if (opt_col_width + desc_col_width + indent * 2 > columns) {
/* opt col gets whatever it wants. we'll wrap the desc col. */
desc_col_width = columns - (opt_col_width + indent * 2);
if (desc_col_width < 14) /* 14 is arbitrary lower limit on desc width. */
desc_col_width = INT_MAX;
}
desccol = maxlen[0] + indent * 2;
desccol = opt_col_width + indent * 2;

#define PRINT_SPACES(fp,n) \
fprintf((fp), "%*s", (n), "")
Expand Down Expand Up @@ -421,7 +396,7 @@ int scanopt_usage (scanopt_t *scanner, FILE *fp, const char *usage)
/* pad to desccol */
PRINT_SPACES (fp, desccol - nchars);

/* Print description, wrapped to maxlen[1] columns. */
/* Print description, wrapped to desc_col_width columns. */
if (1) {
const char *pstart;

Expand All @@ -432,7 +407,7 @@ int scanopt_usage (scanopt_t *scanner, FILE *fp, const char *usage)

p = pstart;

while (*p && n < maxlen[1]
while (*p && n < desc_col_width
&& *p != '\n') {
if (isspace ((unsigned char)(*p))
|| *p == '-') lastws =
Expand Down

0 comments on commit aa775c8

Please sign in to comment.