put print_columns options into a helper struct, eases passing things around

This commit is contained in:
tg
2016-11-11 20:14:18 +00:00
parent e16b0b9d5b
commit 5ba6be5837
4 changed files with 40 additions and 28 deletions

7
edit.c
View File

@ -28,7 +28,7 @@
#ifndef MKSH_NO_CMDLINE_EDITING #ifndef MKSH_NO_CMDLINE_EDITING
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.308 2016/11/11 19:59:37 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/edit.c,v 1.309 2016/11/11 20:14:16 tg Exp $");
/* /*
* in later versions we might use libtermcap for this, but since external * in later versions we might use libtermcap for this, but since external
@ -238,6 +238,7 @@ x_print_expansions(int nwords, char * const *words, bool is_command)
bool use_copy = false; bool use_copy = false;
size_t prefix_len; size_t prefix_len;
XPtrV l = { NULL, 0, 0 }; XPtrV l = { NULL, 0, 0 };
struct columnise_opts co;
/* /*
* Check if all matches are in the same directory (in this * Check if all matches are in the same directory (in this
@ -271,7 +272,9 @@ x_print_expansions(int nwords, char * const *words, bool is_command)
*/ */
x_putc('\r'); x_putc('\r');
x_putc('\n'); x_putc('\n');
pr_list(shl_out, use_copy ? (char **)XPptrv(l) : words); co.shf = shl_out;
co.prefcol = false;
pr_list(&co, use_copy ? (char **)XPptrv(l) : words);
if (use_copy) if (use_copy)
/* not x_free_words() */ /* not x_free_words() */

16
exec.c
View File

@ -23,7 +23,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.183 2016/11/11 19:59:39 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/exec.c,v 1.184 2016/11/11 20:14:17 tg Exp $");
#ifndef MKSH_DEFAULT_EXECSHELL #ifndef MKSH_DEFAULT_EXECSHELL
#define MKSH_DEFAULT_EXECSHELL MKSH_UNIXROOT "/bin/sh" #define MKSH_DEFAULT_EXECSHELL MKSH_UNIXROOT "/bin/sh"
@ -1656,6 +1656,7 @@ pr_menu(const char * const *ap)
const char * const *pp; const char * const *pp;
size_t acols = 0, aocts = 0, i; size_t acols = 0, aocts = 0, i;
unsigned int n; unsigned int n;
struct columnise_opts co;
/* /*
* width/column calculations were done once and saved, but this * width/column calculations were done once and saved, but this
@ -1684,9 +1685,10 @@ pr_menu(const char * const *ap)
smi.num_width++; smi.num_width++;
smi.args = ap; smi.args = ap;
print_columns(shl_out, n, select_fmt_entry, (void *)&smi, co.shf = shl_out;
smi.num_width + 2 + aocts, smi.num_width + 2 + acols, co.prefcol = true;
true); print_columns(&co, n, select_fmt_entry, (void *)&smi,
smi.num_width + 2 + aocts, smi.num_width + 2 + acols);
} }
static void static void
@ -1696,7 +1698,7 @@ plain_fmt_entry(char *buf, size_t buflen, unsigned int i, const void *arg)
} }
void void
pr_list(struct shf *shf, char * const *ap) pr_list(struct columnise_opts *cop, char * const *ap)
{ {
size_t acols = 0, aocts = 0, i; size_t acols = 0, aocts = 0, i;
unsigned int n; unsigned int n;
@ -1711,8 +1713,8 @@ pr_list(struct shf *shf, char * const *ap)
acols = i; acols = i;
} }
print_columns(shf, n, plain_fmt_entry, (const void *)ap, print_columns(cop, n, plain_fmt_entry, (const void *)ap,
aocts, acols, false); aocts, acols);
} }
/* /*

20
funcs.c
View File

@ -38,7 +38,7 @@
#endif #endif
#endif #endif
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.313 2016/11/11 19:59:39 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.314 2016/11/11 20:14:17 tg Exp $");
#if HAVE_KILLPG #if HAVE_KILLPG
/* /*
@ -521,15 +521,16 @@ c_print(const char **wp)
if (po.col) { if (po.col) {
size_t w = XPsize(words); size_t w = XPsize(words);
char *cp; char *cp;
struct shf shf; struct columnise_opts co;
shf_sopen(NULL, 128, SHF_WR | SHF_DYNAMIC, &shf);
XPput(words, NULL); XPput(words, NULL);
pr_list(&shf, (char **)XPptrv(words)); co.shf = shf_sopen(NULL, 128, SHF_WR | SHF_DYNAMIC, NULL);
co.prefcol = false;
pr_list(&co, (char **)XPptrv(words));
while (w--) while (w--)
afree(XPptrv(words)[w], ATEMP); afree(XPptrv(words)[w], ATEMP);
XPfree(words); XPfree(words);
cp = shf_sclose(&shf); cp = shf_sclose(co.shf);
w = strlen(cp); w = strlen(cp);
XcheckN(xs, xp, w); XcheckN(xs, xp, w);
@ -1495,6 +1496,7 @@ c_kill(const char **wp)
ssize_t w, mess_cols = 0, mess_octs = 0; ssize_t w, mess_cols = 0, mess_octs = 0;
int j = ksh_NSIG - 1; int j = ksh_NSIG - 1;
struct kill_info ki = { 0, 0 }; struct kill_info ki = { 0, 0 };
struct columnise_opts co;
do { do {
ki.num_width++; ki.num_width++;
@ -1512,11 +1514,13 @@ c_kill(const char **wp)
mess_cols = w; mess_cols = w;
} }
print_columns(shl_stdout, (unsigned int)(ksh_NSIG - 1), co.shf = shl_stdout;
co.prefcol = true;
print_columns(&co, (unsigned int)(ksh_NSIG - 1),
kill_fmt_entry, (void *)&ki, kill_fmt_entry, (void *)&ki,
ki.num_width + 1 + ki.name_width + 1 + mess_octs, ki.num_width + 1 + ki.name_width + 1 + mess_octs,
ki.num_width + 1 + ki.name_width + 1 + mess_cols, ki.num_width + 1 + ki.name_width + 1 + mess_cols);
true);
} }
return (0); return (0);
} }

25
misc.c
View File

@ -30,7 +30,7 @@
#include <grp.h> #include <grp.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.245 2016/08/01 18:42:42 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/misc.c,v 1.246 2016/11/11 20:14:18 tg Exp $");
#define KSH_CHVT_FLAG #define KSH_CHVT_FLAG
#ifdef MKSH_SMALL #ifdef MKSH_SMALL
@ -195,6 +195,7 @@ printoptions(bool verbose)
if (verbose) { if (verbose) {
size_t n = 0, len, octs = 0; size_t n = 0, len, octs = 0;
struct options_info oi; struct options_info oi;
struct columnise_opts co;
/* verbose version */ /* verbose version */
shf_puts("Current option settings\n", shl_stdout); shf_puts("Current option settings\n", shl_stdout);
@ -211,8 +212,10 @@ printoptions(bool verbose)
} }
++i; ++i;
} }
print_columns(shl_stdout, n, options_fmt_entry, &oi, co.shf = shl_stdout;
octs + 4, oi.opt_width + 4, true); co.prefcol = true;
print_columns(&co, n, options_fmt_entry, &oi,
octs + 4, oi.opt_width + 4);
} else { } else {
/* short version like AT&T ksh93 */ /* short version like AT&T ksh93 */
shf_puts(Tset, shl_stdout); shf_puts(Tset, shl_stdout);
@ -1226,9 +1229,9 @@ print_value_quoted(struct shf *shf, const char *s)
* the i-th element * the i-th element
*/ */
void void
print_columns(struct shf *shf, unsigned int n, print_columns(struct columnise_opts *opts, unsigned int n,
void (*func)(char *, size_t, unsigned int, const void *), void (*func)(char *, size_t, unsigned int, const void *),
const void *arg, size_t max_oct, size_t max_colz, bool prefcol) const void *arg, size_t max_oct, size_t max_colz)
{ {
unsigned int i, r, c, rows, cols, nspace, max_col; unsigned int i, r, c, rows, cols, nspace, max_col;
char *str; char *str;
@ -1267,14 +1270,14 @@ print_columns(struct shf *shf, unsigned int n,
if (cols < 2) { if (cols < 2) {
for (i = 0; i < n; ++i) { for (i = 0; i < n; ++i) {
(*func)(str, max_oct, i, arg); (*func)(str, max_oct, i, arg);
shf_puts(str, shf); shf_puts(str, opts->shf);
shf_putc('\n', shf); shf_putc('\n', opts->shf);
} }
goto out; goto out;
} }
rows = (n + cols - 1) / cols; rows = (n + cols - 1) / cols;
if (prefcol && cols > rows) { if (opts->prefcol && cols > rows) {
cols = rows; cols = rows;
rows = (n + cols - 1) / cols; rows = (n + cols - 1) / cols;
} }
@ -1289,12 +1292,12 @@ print_columns(struct shf *shf, unsigned int n,
break; break;
(*func)(str, max_oct, i, arg); (*func)(str, max_oct, i, arg);
if (i + rows >= n) if (i + rows >= n)
shf_puts(str, shf); shf_puts(str, opts->shf);
else else
shf_fprintf(shf, "%*s%*s", shf_fprintf(opts->shf, "%*s%*s",
(int)max_col, str, (int)nspace, null); (int)max_col, str, (int)nspace, null);
} }
shf_putchar('\n', shf); shf_putchar('\n', opts->shf);
} }
out: out:
afree(str, ATEMP); afree(str, ATEMP);