summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorExplorer09 <explorer09@gmail.com>2017-11-12 21:58:42 +0800
committerWill Estes <westes575@gmail.com>2017-12-08 09:55:19 -0500
commitaa775c871137c570ded2bbd6ebbe7ca4ee39efe7 (patch)
tree1fe731d7d8dcde4656a91dd9eb0807a4c4ea8d5f
parent23882383d45dcd37b5177835c873f4e1d9582db1 (diff)
scanner: simplify scanopt_usage().
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
-rw-r--r--src/scanopt.c63
1 files changed, 19 insertions, 44 deletions
diff --git a/src/scanopt.c b/src/scanopt.c
index 0b6d7d1..76d15af 100644
--- a/src/scanopt.c
+++ b/src/scanopt.c
@@ -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) {
@@ -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), "")
@@ -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;
@@ -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 =