column: code readability improvements
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
This commit is contained in:
parent
bf90b8a6af
commit
1ae90932bd
|
@ -64,6 +64,11 @@ static char *mtsafe_strtok(char *, const char *, char **);
|
|||
#define wcstok mtsafe_strtok
|
||||
#endif
|
||||
|
||||
#define DEFCOLS 25
|
||||
#define TAB 8
|
||||
#define DEFNUM 1000
|
||||
#define MAXLINELEN (LINE_MAX + 1)
|
||||
|
||||
static void c_columnate __P((void));
|
||||
static void *emalloc __P((int));
|
||||
static void input __P((FILE *));
|
||||
|
@ -71,6 +76,11 @@ static void maketbl __P((void));
|
|||
static void print __P((void));
|
||||
static void r_columnate __P((void));
|
||||
|
||||
typedef struct _tbl {
|
||||
wchar_t **list;
|
||||
int cols, *len;
|
||||
} TBL;
|
||||
|
||||
int termwidth = 80; /* default terminal width */
|
||||
|
||||
int entries; /* number of records */
|
||||
|
@ -131,8 +141,8 @@ main(int argc, char **argv)
|
|||
switch(ch) {
|
||||
case 'h':
|
||||
case '?':
|
||||
usage(EXIT_SUCCESS);
|
||||
break;
|
||||
usage(EXIT_SUCCESS);
|
||||
break;
|
||||
case 'c':
|
||||
termwidth = atoi(optarg);
|
||||
break;
|
||||
|
@ -147,20 +157,23 @@ main(int argc, char **argv)
|
|||
break;
|
||||
default:
|
||||
usage(EXIT_FAILURE);
|
||||
}
|
||||
}
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
|
||||
if (!*argv)
|
||||
input(stdin);
|
||||
else for (; *argv; ++argv)
|
||||
if ((fp = fopen(*argv, "r")) != NULL) {
|
||||
input(fp);
|
||||
(void)fclose(fp);
|
||||
} else {
|
||||
warn("%s", *argv);
|
||||
eval = EXIT_FAILURE;
|
||||
else {
|
||||
for (; *argv; ++argv) {
|
||||
if ((fp = fopen(*argv, "r")) != NULL) {
|
||||
input(fp);
|
||||
(void)fclose(fp);
|
||||
} else {
|
||||
warn("%s", *argv);
|
||||
eval = EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!entries)
|
||||
exit(eval);
|
||||
|
@ -178,7 +191,6 @@ main(int argc, char **argv)
|
|||
exit(eval);
|
||||
}
|
||||
|
||||
#define TAB 8
|
||||
static void
|
||||
c_columnate()
|
||||
{
|
||||
|
@ -217,7 +229,7 @@ r_columnate()
|
|||
maxlength = (maxlength + TAB) & ~(TAB - 1);
|
||||
numcols = termwidth / maxlength;
|
||||
if (!numcols)
|
||||
numcols = 1;
|
||||
numcols = 1;
|
||||
numrows = entries / numcols;
|
||||
if (entries % numcols)
|
||||
++numrows;
|
||||
|
@ -251,12 +263,6 @@ print()
|
|||
}
|
||||
}
|
||||
|
||||
typedef struct _tbl {
|
||||
wchar_t **list;
|
||||
int cols, *len;
|
||||
} TBL;
|
||||
#define DEFCOLS 25
|
||||
|
||||
static void
|
||||
maketbl()
|
||||
{
|
||||
|
@ -272,42 +278,41 @@ maketbl()
|
|||
cols = emalloc((maxcols = DEFCOLS) * sizeof(wchar_t *));
|
||||
lens = emalloc(maxcols * sizeof(int));
|
||||
for (cnt = 0, lp = list; cnt < entries; ++cnt, ++lp, ++t) {
|
||||
for (coloff = 0, p = *lp;
|
||||
(cols[coloff] = wcstok(p, separator, &wcstok_state)) != NULL;
|
||||
p = NULL)
|
||||
if (++coloff == maxcols) {
|
||||
if (!(cols = realloc(cols, ((u_int)maxcols + DEFCOLS)
|
||||
* sizeof(wchar_t *))) ||
|
||||
!(lens = realloc(lens, ((u_int)maxcols + DEFCOLS)
|
||||
* sizeof(int))))
|
||||
err(EXIT_FAILURE, _("out of memory?"));
|
||||
memset((char *)lens + maxcols * sizeof(int),
|
||||
0, DEFCOLS * sizeof(int));
|
||||
maxcols += DEFCOLS;
|
||||
for (coloff = 0, p = *lp;
|
||||
(cols[coloff] = wcstok(p, separator, &wcstok_state)) != NULL;
|
||||
p = NULL) {
|
||||
if (++coloff == maxcols) {
|
||||
cols = realloc(cols, ((u_int)maxcols + DEFCOLS)
|
||||
* sizeof(wchar_t *));
|
||||
lens = realloc(lens, ((u_int)maxcols + DEFCOLS)
|
||||
* sizeof(int));
|
||||
if (!cols || !lens)
|
||||
err(EXIT_FAILURE, _("out of memory?"));
|
||||
memset((char *)lens + maxcols * sizeof(int),
|
||||
0, DEFCOLS * sizeof(int));
|
||||
maxcols += DEFCOLS;
|
||||
}
|
||||
}
|
||||
t->list = emalloc(coloff * sizeof(wchar_t *));
|
||||
t->len = emalloc(coloff * sizeof(int));
|
||||
for (t->cols = coloff; --coloff >= 0;) {
|
||||
t->list[coloff] = cols[coloff];
|
||||
t->len[coloff] = wcs_width(cols[coloff]);
|
||||
if (t->len[coloff] > lens[coloff])
|
||||
lens[coloff] = t->len[coloff];
|
||||
}
|
||||
t->list = emalloc(coloff * sizeof(wchar_t *));
|
||||
t->len = emalloc(coloff * sizeof(int));
|
||||
for (t->cols = coloff; --coloff >= 0;) {
|
||||
t->list[coloff] = cols[coloff];
|
||||
t->len[coloff] = wcs_width(cols[coloff]);
|
||||
if (t->len[coloff] > lens[coloff])
|
||||
lens[coloff] = t->len[coloff];
|
||||
}
|
||||
}
|
||||
for (cnt = 0, t = tbl; cnt < entries; ++cnt, ++t) {
|
||||
for (coloff = 0; coloff < t->cols - 1; ++coloff) {
|
||||
for (coloff = 0; coloff < t->cols - 1; ++coloff) {
|
||||
fputws(t->list[coloff], stdout);
|
||||
for (i = lens[coloff] - t->len[coloff] + 2; i > 0; i--)
|
||||
putwchar(' ');
|
||||
}
|
||||
fputws(t->list[coloff], stdout);
|
||||
for (i = lens[coloff] - t->len[coloff] + 2; i > 0; i--)
|
||||
putwchar(' ');
|
||||
}
|
||||
fputws(t->list[coloff], stdout);
|
||||
putwchar('\n');
|
||||
putwchar('\n');
|
||||
}
|
||||
}
|
||||
|
||||
#define DEFNUM 1000
|
||||
#define MAXLINELEN (LINE_MAX + 1)
|
||||
|
||||
static void
|
||||
input(fp)
|
||||
FILE *fp;
|
||||
|
@ -324,7 +329,8 @@ input(fp)
|
|||
continue;
|
||||
if (!(p = wcschr(p, '\n')) && !feof(fp)) {
|
||||
if (reportedline < lineno) {
|
||||
warnx(_("line %d is too long, output will be truncated"), lineno);
|
||||
warnx(_("line %d is too long, output will be truncated"),
|
||||
lineno);
|
||||
reportedline = lineno;
|
||||
}
|
||||
eval = EXIT_FAILURE;
|
||||
|
@ -338,8 +344,8 @@ input(fp)
|
|||
maxlength = len;
|
||||
if (entries == maxentry) {
|
||||
maxentry += DEFNUM;
|
||||
if (!(list = realloc(list,
|
||||
(u_int)maxentry * sizeof(wchar_t *))))
|
||||
list = realloc(list, (u_int)maxentry * sizeof(wchar_t *));
|
||||
if (!list)
|
||||
err(EXIT_FAILURE, _("out of memory?"));
|
||||
}
|
||||
list[entries++] = wcsdup(buf);
|
||||
|
@ -390,7 +396,7 @@ static char *mtsafe_strtok(char *str, const char *delim, char **ptr)
|
|||
|
||||
static void *
|
||||
emalloc(size)
|
||||
int size;
|
||||
int size;
|
||||
{
|
||||
char *p;
|
||||
|
||||
|
@ -399,5 +405,3 @@ emalloc(size)
|
|||
memset(p, 0, size);
|
||||
return (p);
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in New Issue