1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
|
#define _XOPEN_SOURCE
#include "tabular.h"
#include "util.h"
#include <sys/ioctl.h>
#include <wchar.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#include <stddef.h>
#define BIT(i) (1U << (i))
struct col_state {
size_t width;
size_t written;
size_t maxlen;
size_t lpad, rpad;
bool hidden;
};
struct fmt_state {
struct col_state *columns;
size_t column_count;
size_t hseplen;
size_t vseplen;
size_t xseplen;
size_t mseplen;
size_t line_limit;
size_t row_limit;
bool header_line;
};
static void calc_word_aware(struct tabular_entry *entry, size_t maxwidth,
size_t *offset, size_t *width, size_t *lines);
static size_t recalc_col_word_aware(const struct tabular_cfg *cfg,
struct tabular_row *rows, size_t limit,
size_t col, size_t maxwidth);
static size_t calc_row_width(struct fmt_state *fmt);
static size_t calc_output_lines(const struct tabular_cfg *cfg,
struct fmt_state *fmt, struct tabular_row *rows, size_t limit);
static bool recalc_cols(const struct tabular_cfg *cfg,
struct tabular_stats *stats, struct fmt_state *fmt,
struct tabular_row *rows, size_t limit);
static int calc_params_row(const struct tabular_cfg *cfg,
struct tabular_stats *stats, struct tabular_row *rows,
struct tabular_row *row, struct fmt_state *fmt, size_t limit);
static int calc_params(const struct tabular_cfg *cfg,
struct tabular_stats *stat, struct tabular_row **rows,
struct fmt_state *fmt);
static void output_header(FILE *file, const struct tabular_cfg *cfg,
struct tabular_stats *stat, const struct fmt_state *fmt);
static void output_row(FILE *file, const struct tabular_cfg *cfg,
struct tabular_stats *stat, const struct tabular_row *row,
struct fmt_state *fmt);
static int output_rows(FILE *file, const struct tabular_cfg *cfg,
struct tabular_stats *stats, struct tabular_row *rows,
struct fmt_state *fmt);
const char *
calc_word_aware_line(const char *str, size_t maxwidth,
size_t *out_offset, size_t *out_width)
{
const char *sep, *nstr;
size_t width, nwidth;
size_t offset, wlen;
if (!str) {
if (out_offset) *out_offset = 0;
if (out_width) *out_width = 0;
return NULL;
}
offset = strspn(str, " \v\t\r\n");
str += offset;
nstr = str;
nwidth = width = 0;
while (nwidth <= maxwidth) {
width = nwidth;
str = nstr;
if (!str) break;
sep = strchr(str, ' ');
if (!sep) {
wlen = u8strlen(str);
nstr = NULL;
} else {
wlen = u8strnlen(str, (size_t) (sep - str));
nstr = sep + 1;
}
nwidth = width + (width > 0) + wlen;
}
if (out_offset) *out_offset = offset;
if (out_width) {
/* single word > maxwidth */
if (!width && nwidth) {
*out_width = maxwidth;
str = nstr; /* skip single word */
} else {
*out_width = width;
}
}
return str;
}
void
calc_word_aware(struct tabular_entry *entry, size_t maxwidth,
size_t *out_offset, size_t *out_width, size_t *out_lines)
{
const char *str;
size_t width, mwidth;
size_t lines;
if (out_offset) *out_offset = 0;
lines = 0;
mwidth = 0;
str = entry->str;
while (str) {
str = calc_word_aware_line(str, maxwidth,
str == entry->str ? out_offset : NULL, &width);
lines++;
mwidth = MAX(mwidth, width);
}
if (out_width) *out_width = mwidth;
if (out_lines) *out_lines = lines;
}
size_t
recalc_col_word_aware(const struct tabular_cfg *cfg,
struct tabular_row *rows, size_t limit, size_t col,
size_t maxwidth)
{
size_t off, wwidth, max_wwidth, rowcnt;
struct tabular_row *row;
max_wwidth = cfg->columns[col].minwidth;
rowcnt = 0;
for (row = rows; row && rowcnt < limit; row = row->next) {
calc_word_aware(&row->entries[col],
maxwidth, &off, &wwidth, NULL);
max_wwidth = MAX(max_wwidth, wwidth);
rowcnt += 1;
}
return max_wwidth;
}
size_t
calc_row_width(struct fmt_state *fmt)
{
size_t sum, i;
sum = 0;
for (i = 0; i < fmt->column_count; i++) {
if (fmt->columns[i].hidden) continue;
sum += fmt->columns[i].width + (sum ? fmt->hseplen : 0);
}
return sum;
}
size_t
calc_output_lines(const struct tabular_cfg *cfg, struct fmt_state *fmt,
struct tabular_row *rows, size_t limit)
{
struct tabular_row *row;
size_t row_index, row_lines;
size_t entry_lines;
size_t lines, i;
size_t width;
lines = 0;
row_index = 0;
for (row = rows; row; row = row->next) {
if (row_index == limit) break;
row_lines = 0;
for (i = 0; i < cfg->column_count; i++) {
if (fmt->columns[i].hidden) continue;
width = fmt->columns[i].width
- fmt->columns[i].lpad - fmt->columns[i].rpad;
switch (cfg->columns[i].strategy) {
case TABULAR_TRUNC:
entry_lines = 1;
break;
case TABULAR_WRAP:
entry_lines = CEILDIV(row->entries[i].ulen, width);
break;
case TABULAR_WRAP_WORDAWARE:
calc_word_aware(&row->entries[i],
width, NULL, NULL, &entry_lines);
break;
}
row_lines = MAX(row_lines, entry_lines);
}
lines += row_lines;
row_index += 1;
}
return lines;
}
bool
recalc_cols(const struct tabular_cfg *cfg, struct tabular_stats *stats,
struct fmt_state *fmt, struct tabular_row *rows, size_t limit)
{
size_t width, fullwidth, remaining;
ssize_t i;
fullwidth = cfg->outw - cfg->lpad - cfg->rpad;
/* reset widths to minimum requirement */
for (i = 0; i < cfg->column_count; i++) {
if (fmt->columns[i].hidden) continue;
if (cfg->columns[i].squashable) {
fmt->columns[i].width = cfg->columns[i].minwidth;
} else {
width = MIN(fmt->columns[i].maxlen,
cfg->columns[i].maxwidth - cfg->columns[i].lpad
- cfg->columns[i].rpad);
if (cfg->columns[i].strategy == TABULAR_WRAP_WORDAWARE)
width = recalc_col_word_aware(cfg, rows,
limit, (size_t) i, width);
fmt->columns[i].width = width + cfg->columns[i].lpad
+ cfg->columns[i].rpad;
}
}
/* could not fit all necessary columns at minimum width */
width = calc_row_width(fmt);
while (width > fullwidth) {
/* remove non-essential columns */
for (i = ((ssize_t) cfg->column_count) - 1; i >= 0; i--) {
if (!cfg->columns[i].essential
&& !fmt->columns[i].hidden) {
fmt->columns[i].hidden = true;
break;
}
}
/* failed to remove any more */
if (i < 0) return false;
stats->cols_truncated = true;
width = calc_row_width(fmt);
}
/* redistribute excess width to columns, left to right */
remaining = fullwidth - width;
for (i = 0; remaining > 0 && i < cfg->column_count; i++) {
if (fmt->columns[i].hidden) continue;
if (!cfg->columns[i].squashable) continue;
width = MIN(fmt->columns[i].maxlen, cfg->columns[i].maxwidth
- fmt->columns[i].lpad - fmt->columns[i].rpad);
width = MIN(width, fmt->columns[i].width + remaining
- fmt->columns[i].lpad - fmt->columns[i].rpad);
if (cfg->columns[i].strategy == TABULAR_WRAP_WORDAWARE)
width = recalc_col_word_aware(cfg, rows,
limit, (size_t) i, width);
width = MAX(width + fmt->columns[i].lpad + fmt->columns[i].rpad,
fmt->columns[i].width);
remaining -= width - fmt->columns[i].width;
fmt->columns[i].width = width;
}
return true;
}
int
calc_params_row(const struct tabular_cfg *cfg, struct tabular_stats *stats,
struct tabular_row *rows, struct tabular_row *row,
struct fmt_state *fmt, size_t limit)
{
struct colinfo *copy;
size_t i, height;
int rc;
/* make copy of current width in case the next row does not fit */
copy = cfg->allocator->alloc(cfg->allocator,
sizeof(struct col_state) * cfg->column_count, &rc);
if (!copy) return -rc;
memcpy(copy, fmt->columns, sizeof(struct col_state) * cfg->column_count);
/* unhide cols */
for (i = 0; i < cfg->column_count; i++) {
tabular_load_row_entry_hidden(cfg, row, i);
if (fmt->columns[i].hidden) {
fmt->columns[i].hidden =
(row->entries[i].flags & BIT(TABULAR_ENTRY_HIDDEN));
}
if (fmt->columns[i].hidden) continue;
}
/* update maxlen for visible cols */
for (i = 0; i < cfg->column_count; i++) {
tabular_load_row_entry_str(cfg, row, i);
fmt->columns[i].maxlen = MAX(fmt->columns[i].maxlen,
row->entries[i].ulen);
}
/* recalc column sizes to fit new column */
if (!recalc_cols(cfg, stats, fmt, rows, limit))
goto undo;
height = calc_output_lines(cfg, fmt, rows, limit);
/* check the line limit if applicable */
if (fmt->line_limit > 0 && height > fmt->line_limit)
goto undo;
cfg->allocator->free(cfg->allocator, copy);
return 0;
undo:
memcpy(fmt->columns, copy, sizeof(struct col_state) * fmt->column_count);
cfg->allocator->free(cfg->allocator, copy);
return 1;
}
int
calc_params(const struct tabular_cfg *cfg, struct tabular_stats *stats,
struct tabular_row **rows, struct fmt_state *fmt)
{
struct tabular_row **row;
ssize_t first, last;
size_t i;
int rc;
fmt->header_line = ((cfg->vsep != NULL && *cfg->vsep)
&& (cfg->xsep != NULL && *cfg->xsep));
fmt->line_limit = 0;
if (cfg->fit_rows) {
fmt->line_limit = MAX(0, cfg->outh
- cfg->skip_lines - 1 - fmt->header_line);
}
fmt->hseplen = u8strlen(cfg->hsep);
fmt->vseplen = u8strlen(cfg->vsep);
fmt->xseplen = u8strlen(cfg->xsep);
fmt->mseplen = MAX(fmt->hseplen, fmt->xseplen);
fmt->column_count = cfg->column_count;
fmt->columns = cfg->allocator->alloc(cfg->allocator,
fmt->column_count * sizeof(struct col_state), &rc);
if (!fmt->columns) return -rc;
for (i = 0; i < cfg->column_count; i++) {
fmt->columns[i].lpad = cfg->columns[i].lpad;
fmt->columns[i].rpad = cfg->columns[i].rpad;
fmt->columns[i].hidden = true;
fmt->columns[i].width = cfg->columns[i].minwidth;
fmt->columns[i].maxlen = u8strlen(cfg->columns[i].name)
+ fmt->columns[i].lpad + fmt->columns[i].rpad;
if (fmt->columns[i].maxlen > cfg->columns[i].minwidth)
return 1;
fmt->columns[i].written = 0;
}
fmt->row_limit = 0;
if (!*rows && cfg->row_gen) *rows = cfg->row_gen(&cfg->user);
if (!*rows) return 0;
if (!recalc_cols(cfg, stats, fmt, *rows, 0)) {
stats->cols_truncated = true;
stats->rows_truncated = true;
return 0;
}
for (row = rows; ; row = &(*row)->next) {
if (!*row && cfg->row_gen) *row = cfg->row_gen(&cfg->user);
if (!*row) break;
rc = calc_params_row(cfg, stats, *rows,
*row, fmt, fmt->row_limit + 1);
if (rc < 0) return rc;
if (rc > 0) {
stats->rows_truncated = true;
break;
}
fmt->row_limit++;
}
first = last = -1;
for (i = 0; i < cfg->column_count; i++) {
if (fmt->columns[i].hidden) continue;
if (first < 0) first = (ssize_t) i;
last = (ssize_t) i;
}
if (first >= 0 && last >= 0) {
fmt->columns[first].lpad += cfg->lpad;
fmt->columns[first].width += cfg->lpad;
fmt->columns[last].rpad += cfg->rpad;
fmt->columns[last].width += cfg->rpad;
}
return 0;
}
void
output_header(FILE *file, const struct tabular_cfg *cfg,
struct tabular_stats *stats, const struct fmt_state *fmt)
{
size_t i, k, width;
bool dirty, first;
first = true;
for (i = 0; i < cfg->column_count; i++) {
if (fmt->columns[i].hidden) continue;
if (!first) {
dirty = cfg->print_style(file, cfg, NULL, NULL);
fprintf(file, "%s%*.s", cfg->hsep,
(int) (fmt->mseplen - fmt->hseplen), "");
if (dirty) fprintf(file, "\x1b[0m");
}
first = false;
width = fmt->columns[i].width
- fmt->columns[i].lpad - fmt->columns[i].rpad;
dirty = cfg->print_style(file, cfg,
NULL, &cfg->columns[i]);
print_pad(file, fmt->columns[i].lpad);
print_left(file, cfg->columns[i].name, width, width);
print_pad(file, fmt->columns[i].rpad);
if (dirty) fprintf(file, "\x1b[0m");
}
fprintf(file, "\n");
stats->lines_used += 1;
if (fmt->header_line) {
first = true;
dirty = cfg->print_style(file, cfg, NULL, NULL);
for (i = 0; i < cfg->column_count; i++) {
if (fmt->columns[i].hidden) continue;
if (!first) {
fprintf(file, "%s%*.s", cfg->xsep,
(int) (fmt->mseplen - fmt->xseplen), "");
}
for (k = 0; k < fmt->columns[i].width; k++)
fprintf(file, "%s", cfg->vsep);
first = false;
}
if (dirty) fprintf(file, "\x1b[0m");
fprintf(file, "\n");
stats->lines_used += 1;
}
}
void
output_row(FILE *file, const struct tabular_cfg *cfg,
struct tabular_stats *stat, const struct tabular_row *row,
struct fmt_state *fmt)
{
size_t wwidth, padwidth, outlen;
size_t i, off, width;
bool first, done, dirty;
char *entry;
for (i = 0; i < cfg->column_count; i++) {
fmt->columns[i].written = 0;
if (cfg->columns[i].strategy == TABULAR_TRUNC) {
width = fmt->columns[i].width
- fmt->columns[i].lpad - fmt->columns[i].rpad;
if (row->entries[i].ulen > width) {
width = u8rawlen(row->entries[i].str, width - 2);
row->entries[i].str[width] = '.';
row->entries[i].str[width+1] = '.';
row->entries[i].str[width+2] = '\0';
}
}
}
do {
done = true;
first = true;
for (i = 0; i < cfg->column_count; i++) {
if (fmt->columns[i].hidden) continue;
if (!first) {
dirty = cfg->print_style(file, cfg, NULL, NULL);
fprintf(file, "%s", cfg->hsep);
if (dirty) fprintf(file, "\x1b[0m");
}
first = false;
if (!row->entries[i].str) {
print_pad(file, fmt->columns[i].width);
continue;
}
entry = row->entries[i].str + fmt->columns[i].written;
dirty = cfg->print_style(file, cfg, row, &cfg->columns[i]);
off = 0;
width = fmt->columns[i].width
- fmt->columns[i].lpad - fmt->columns[i].rpad;
padwidth = width;
if (cfg->columns[i].strategy == TABULAR_WRAP_WORDAWARE) {
calc_word_aware_line(entry, width, &off, &wwidth);
entry += off;
width = wwidth;
}
print_pad(file, fmt->columns[i].lpad);
if (cfg->columns[i].align == TABULAR_ALIGN_LEFT) {
print_left(file, entry, width, padwidth);
} else if (cfg->columns[i].align == TABULAR_ALIGN_RIGHT) {
print_right(file, entry, width, padwidth);
} else {
print_center(file, entry, width, padwidth);
}
print_pad(file, fmt->columns[i].rpad);
if (dirty) fprintf(file, "\x1b[0m");
outlen = MIN(u8strlen(entry), width + off);
fmt->columns[i].written += u8rawlen(entry, outlen);
/* check if anything other than whitespace left */
entry += u8rawlen(entry, outlen);
if (strspn(entry, " \t\v\r\n") != u8strlen(entry))
done = false;
}
fprintf(file, "\n");
stat->lines_used += 1;
} while (!done);
stat->rows_displayed += 1;
}
int
output_rows(FILE *file, const struct tabular_cfg *cfg,
struct tabular_stats *stats, struct tabular_row *rows,
struct fmt_state *fmt)
{
struct tabular_row *row;
size_t count;
if (fmt->row_limit == 0) return 0;
output_header(file, cfg, stats, fmt);
count = 0;
for (row = rows; row; row = row->next) {
if (count == fmt->row_limit)
break;
output_row(file, cfg, stats, row, fmt);
count += 1;
}
return 0;
}
int
tabular_format(FILE *file, const struct tabular_cfg *cfg,
struct tabular_stats *stats, struct tabular_row **rows)
{
struct fmt_state fmt;
size_t i;
int rc;
stats->lines_used = 0;
stats->rows_displayed = 0;
stats->rows_truncated = false;
stats->cols_truncated = false;
if (!rows) return 1;
if (!cfg->column_count) return 0;
for (i = 0; i < cfg->column_count; i++) {
if (cfg->columns[i].minwidth > cfg->columns[i].maxwidth)
return 1;
if (cfg->columns[i].lpad + cfg->columns[i].rpad
>= cfg->columns[i].minwidth)
return 1;
if (cfg->columns[i].strategy != TABULAR_TRUNC
&& cfg->columns[i].strategy != TABULAR_WRAP
&& cfg->columns[i].strategy != TABULAR_WRAP_WORDAWARE)
return 1;
}
rc = calc_params(cfg, stats, rows, &fmt);
if (rc) return rc;
rc = output_rows(file, cfg, stats, *rows, &fmt);
if (rc) return rc;
rc = cfg->allocator->free(cfg->allocator, fmt.columns);
if (rc) return -rc;
return 0;
}
struct tabular_row *
tabular_alloc_row(const struct tabular_cfg *cfg,
int *rc, struct tabular_user user)
{
struct tabular_row *row;
row = cfg->allocator->alloc(cfg->allocator,
sizeof(struct tabular_row), rc);
if (!row && rc) *rc = -*rc;
if (!row) return NULL;
row->next = NULL;
row->user = user;
row->entries = cfg->allocator->alloc(cfg->allocator,
sizeof(struct tabular_entry) * cfg->column_count, rc);
if (!row->entries && rc) *rc = -*rc;
if (!row->entries) return NULL;
memset(row->entries, 0, sizeof(struct tabular_entry) * cfg->column_count);
return row;
}
int
tabular_free_rows(const struct tabular_cfg *cfg, struct tabular_row *rows)
{
struct tabular_row *iter, *next;
size_t i;
int rc;
for (iter = rows; iter; iter = next) {
next = iter->next;
for (i = 0; i < cfg->column_count; i++) {
rc = cfg->allocator->free(cfg->allocator,
iter->entries[i].str);
if (rc) return -rc;
}
rc = cfg->allocator->free(cfg->allocator, iter->entries);
if (rc) return -rc;
rc = cfg->allocator->free(cfg->allocator, iter);
if (rc) return -rc;
}
return 0;
}
void
tabular_load_row_entry_hidden(const struct tabular_cfg *cfg,
struct tabular_row *row, size_t col)
{
bool hidden;
if (row->entries[col].flags & BIT(TABULAR_ENTRY_HIDDEN_SET)) return;
if (cfg->columns[col].is_hidden) {
hidden = cfg->columns[col].is_hidden(
&row->user, &cfg->columns[col].user);
} else {
hidden = false;
}
if (hidden) {
row->entries[col].flags |= BIT(TABULAR_ENTRY_HIDDEN);
} else {
row->entries[col].flags &= ~BIT(TABULAR_ENTRY_HIDDEN);
}
row->entries[col].flags |= BIT(TABULAR_ENTRY_HIDDEN_SET);
}
void
tabular_load_row_entry_str(const struct tabular_cfg *cfg,
struct tabular_row *row, size_t col)
{
if (row->entries[col].flags & BIT(TABULAR_ENTRY_STR_SET)) return;
row->entries[col].str = cfg->columns[col].to_str(
&row->user, &cfg->columns[col].user);
row->entries[col].ulen = (uint32_t) u8strlen(row->entries[col].str);
row->entries[col].flags |= BIT(TABULAR_ENTRY_STR_SET);
}
|