first implementation of “print -c” for columnising, currently hardcoded newlines

This commit is contained in:
tg 2016-11-11 19:59:39 +00:00
parent 6a9dae2144
commit e16b0b9d5b
3 changed files with 44 additions and 8 deletions

4
edit.c
View File

@ -28,7 +28,7 @@
#ifndef MKSH_NO_CMDLINE_EDITING
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.307 2016/09/01 12:59:08 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.308 2016/11/11 19:59:37 tg Exp $");
/*
* in later versions we might use libtermcap for this, but since external
@ -271,7 +271,7 @@ x_print_expansions(int nwords, char * const *words, bool is_command)
*/
x_putc('\r');
x_putc('\n');
pr_list(use_copy ? (char **)XPptrv(l) : words);
pr_list(shl_out, use_copy ? (char **)XPptrv(l) : words);
if (use_copy)
/* not x_free_words() */

6
exec.c
View File

@ -23,7 +23,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.182 2016/10/02 22:21:46 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.183 2016/11/11 19:59:39 tg Exp $");
#ifndef MKSH_DEFAULT_EXECSHELL
#define MKSH_DEFAULT_EXECSHELL MKSH_UNIXROOT "/bin/sh"
@ -1696,7 +1696,7 @@ plain_fmt_entry(char *buf, size_t buflen, unsigned int i, const void *arg)
}
void
pr_list(char * const *ap)
pr_list(struct shf *shf, char * const *ap)
{
size_t acols = 0, aocts = 0, i;
unsigned int n;
@ -1711,7 +1711,7 @@ pr_list(char * const *ap)
acols = i;
}
print_columns(shl_out, n, plain_fmt_entry, (const void *)ap,
print_columns(shf, n, plain_fmt_entry, (const void *)ap,
aocts, acols, false);
}

42
funcs.c
View File

@ -38,7 +38,7 @@
#endif
#endif
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.312 2016/11/11 19:18:40 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.313 2016/11/11 19:59:39 tg Exp $");
#if HAVE_KILLPG
/*
@ -284,6 +284,7 @@ c_print(const char **wp)
const char *s;
char *xp;
XString xs;
XPtrV words;
struct {
/* temporary storage for a wide character */
mksh_ari_t wc;
@ -297,6 +298,8 @@ c_print(const char **wp)
char nl;
/* expand backslash sequences? */
bool exp;
/* columnise output? */
bool col;
/* print to history instead of file descriptor / stdout? */
bool hist;
/* print words as wide characters? */
@ -312,6 +315,7 @@ c_print(const char **wp)
po.ws = ' ';
po.nl = '\n';
po.exp = true;
po.col = false;
po.hist = false;
po.chars = false;
@ -379,7 +383,7 @@ c_print(const char **wp)
}
} else {
/* "print" builtin */
const char *opts = "AlNnpRrsu,";
const char *opts = "AclNnpRrsu,";
const char *emsg;
po.pminusminus = false;
@ -389,6 +393,9 @@ c_print(const char **wp)
case 'A':
po.chars = true;
break;
case 'c':
po.col = true;
break;
case 'e':
po.exp = true;
break;
@ -446,6 +453,8 @@ c_print(const char **wp)
if (*wp == NULL)
goto print_no_arg;
if (po.col)
XPinit(words, 16);
print_read_arg:
if (po.chars) {
while (*wp != NULL) {
@ -499,10 +508,37 @@ c_print(const char **wp)
Xput(xs, xp, c);
}
}
if (po.col) {
Xput(xs, xp, '\0');
XPput(words, Xclose(xs, xp));
Xinit(xs, xp, 128, ATEMP);
}
if (*wp != NULL) {
Xput(xs, xp, po.ws);
if (!po.col)
Xput(xs, xp, po.ws);
goto print_read_arg;
}
if (po.col) {
size_t w = XPsize(words);
char *cp;
struct shf shf;
shf_sopen(NULL, 128, SHF_WR | SHF_DYNAMIC, &shf);
XPput(words, NULL);
pr_list(&shf, (char **)XPptrv(words));
while (w--)
afree(XPptrv(words)[w], ATEMP);
XPfree(words);
cp = shf_sclose(&shf);
w = strlen(cp);
XcheckN(xs, xp, w);
memcpy(xp, cp, w);
xp += w;
afree(cp, ATEMP);
po.nl = '!'; /* for now */
}
print_no_arg:
if (po.nl != '!')
Xput(xs, xp, po.nl);