toolbox

Check-in [85b159a208]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:retab: initial import
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 85b159a208b028c16ad92007f42f6006e5af4b952fd93db6987cfdf1030e33bb
User & Date: jef 2020-04-29 11:07:29
Context
2020-04-29
11:12
retab: fix man page date field check-in: 7cd5438c52 user: jef tags: trunk
11:07
retab: initial import check-in: 85b159a208 user: jef tags: trunk
11:07
lib: new str_ methods and single linked list library check-in: 2a94a61c30 user: jef tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to Makefile.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.POSIX:
.PHONY: doc

PROJS = chrono cook flooze http-code radio \
	renew taillast soundmachine thread calc radios-check \
	i3notify rss i3status-notif i3radio i3codemusic \
	townsign phone ipmi-lcd-set natospell fwiki \
	rrdreel

all: progs

doc:
	# generate text documentation from man pages
	# (section title is erased using a perl one liner)
	@for proj in ${PROJS}; do \







|







1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
.POSIX:
.PHONY: doc

PROJS = chrono cook flooze http-code radio \
	renew taillast soundmachine thread calc radios-check \
	i3notify rss i3status-notif i3radio i3codemusic \
	townsign phone ipmi-lcd-set natospell fwiki \
	rrdreel retab

all: progs

doc:
	# generate text documentation from man pages
	# (section title is erased using a perl one liner)
	@for proj in ${PROJS}; do \

Changes to doc/README.md.

14
15
16
17
18
19
20

21
22
23
24
25
26
27
28
* [i3status-notif](/doc/tip/doc/i3status-notif.txt): custom i3status with notification area
* [ipmi-lcd-set](/doc/tip/doc/ipmi-lcd-set.txt): set LCD display on Dell servers
* [natospell](/doc/tip/doc/natospell.txt): spell words using NATO phonetic alphabet
* [phone](/doc/tip/doc/phone.txt): list contacts in a phonebook
* [radio](/doc/tip/doc/radio.txt): command line radio player
* [radios-check](/doc/tip/doc/radios-check.txt): check foutaise.org radios stream availability
* [renew](/doc/tip/doc/renew.txt): monitor expiration of domains, certificates and discrete events

* [rrdreel](/doc/tip/doc/rrdreel.txt): Live data visualisation framework
* [rss](/doc/tip/doc/rss.txt): RSS reader for the command line
* [soundmachine](/doc/tip/doc/soundmachine.txt): command line soundmachine
* [taillast](/doc/tip/doc/taillast.txt): tail file from last saved offset
* [thread](/doc/tip/doc/thread.txt): execute commands in parallel on a collection of hosts
* [townsign](/doc/tip/doc/townsign.txt): french town sign generator

## Installation







>
|







14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
* [i3status-notif](/doc/tip/doc/i3status-notif.txt): custom i3status with notification area
* [ipmi-lcd-set](/doc/tip/doc/ipmi-lcd-set.txt): set LCD display on Dell servers
* [natospell](/doc/tip/doc/natospell.txt): spell words using NATO phonetic alphabet
* [phone](/doc/tip/doc/phone.txt): list contacts in a phonebook
* [radio](/doc/tip/doc/radio.txt): command line radio player
* [radios-check](/doc/tip/doc/radios-check.txt): check foutaise.org radios stream availability
* [renew](/doc/tip/doc/renew.txt): monitor expiration of domains, certificates and discrete events
* [retab](/doc/tip/doc/retab.txt): retabulate a markdown table
* [rrdreel](/doc/tip/doc/rrdreel.txt): live data visualisation framework
* [rss](/doc/tip/doc/rss.txt): RSS reader for the command line
* [soundmachine](/doc/tip/doc/soundmachine.txt): command line soundmachine
* [taillast](/doc/tip/doc/taillast.txt): tail file from last saved offset
* [thread](/doc/tip/doc/thread.txt): execute commands in parallel on a collection of hosts
* [townsign](/doc/tip/doc/townsign.txt): french town sign generator

## Installation

Added doc/retab.txt.



























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
RETAB(1)                                                              RETAB(1)

NAME
     retab – Retabulate a markdown table

SYNOPSIS
     retab < table.txt

DESCRIPTION
     retab retabulate a markdown table to fix malformed columns

EXAMPLE
           $ retab <<EOF
           | header 1 | header 2|
           ------------------
           | row 1 cell 1| xx|
           | yy|row2 cell2|
           | brol|stuff|
           EOF
           | header 1     | header 2   |
           -----------------------------
           | row 1 cell 1 | xx         |
           | yy           | row2 cell2 |
           | brol         | stuff      |

AUTHOR
     Gerome Fournier <jef@foutaise.org>.

                                 January 2019

Added src/retab/Makefile.













































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
.POSIX:

PREFIX = /usr/local
MANPREFIX = $(PREFIX)/share/man

CC = cc
CFLAGS = -O3 -Wall -Wextra -Werror -pedantic -I/usr/local/include -I ../include
LDFLAGS = -L/usr/local/lib -L../lib -ljt
PROG = retab
OBJS = retab.o

all: $(PROG)

lib:
	make -C ../lib

$(PROG): lib $(OBJS)
	$(CC) $(CFLAGS) $(OBJS) -o $(PROG) $(LDFLAGS)

install: $(PROG) $(PROG).1
	mkdir -p $(PREFIX)/bin
	cp -f $(PROG) $(PREFIX)/bin
	chmod 0755 $(PREFIX)/bin/$(PROG)
	mkdir -p $(MANPREFIX)/man1
	cp -f $(PROG).1 $(MANPREFIX)/man1
	gzip -f $(MANPREFIX)/man1/$(PROG).1
	chmod 0644 $(MANPREFIX)/man1/$(PROG).1.gz

uninstall:
	rm -f $(PREFIX)/bin/$(PROG)
	rm -f $(MANPREFIX)/man1/$(PROG).1.gz

clean:
	rm -f $(PROG) $(OBJS) || true

.SUFFIXES: .c .o
.c.o:
	$(CC) $(CFLAGS) -c $<

Added src/retab/retab.1.





























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
.Dd January 2019
.Dt RETAB 1
.Os
.Sh NAME
.Nm retab
.Nd Retabulate a markdown table
.Sh SYNOPSIS
.Nm
<
.Ar table.txt
.Sh DESCRIPTION
.Nm
retabulate a markdown table to fix malformed columns
.Sh EXAMPLE
.Bd -literal -offset indent
$ retab <<EOF
| header 1 | header 2|
------------------
| row 1 cell 1| xx|
| yy|row2 cell2|
| brol|stuff|
EOF
| header 1     | header 2   |
-----------------------------
| row 1 cell 1 | xx         |
| yy           | row2 cell2 |
| brol         | stuff      |
.Ed
.Sh AUTHOR
.An Gerome Fournier Aq Mt jef@foutaise.org .

Added src/retab/retab.c.























































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <getopt.h>
#include <sys/queue.h>
#include "jt.h"
#include "utf8.h"

#define PROGNAME "retab"

void
usage()
{
	fprintf(stderr,
		"Retabulate a markdown table\n"
		"Usage:"
		"	" PROGNAME " < table.txt\n"
		"Options:\n"
		"	-h : display this help and exit\n");
	exit(1);
}

size_t max(size_t a, size_t b)
{
	return a > b ? a : b;
}

int
is_line(const char *str)
{
	if (str[0] == '\0')
		return 0;
	for (; *str && *str == '-'; str++)
		;
	return *str == '\0';
}

int
is_row(const char *str)
{
	if (strlen(str) < 2)
		return 0;
	if (str[0] != '|')
		return 0;
	if (str[strlen(str) - 1] != '|')
		return 0;

	return 1;
}

int
read_line(char **line)
{
	char *data = NULL;
	size_t linecap = 0;
	ssize_t res;

	res = getline(&data, &linecap, stdin);
	if (res == -1)
		return 0;

	str_trim(data);
	if (is_line(data) || is_row(data)) {
		*line = data;
		return 1;
	}

	die("invalid line '%s'", data);
	return 0;
}

jtlst *
line_row(char *line)
{
	jtlst *row;
	char *field;

	str_del_prefix(line, "|");
	str_del_suffix(line, "|");
	row = str_split(line, "|");
	JTLST_FOREACH(field, row)
		str_trim(field);

	return row;
}

jtlst *
row_widths(jtlst *row)
{
	jtlst *widths;
	char *field;

	widths = jtlst_new();
	JTLST_FOREACH(field, row)
		jtlst_append(widths, (void *)utf8len(field));

	return widths;
}

jtlst *
max_widths(jtlst *r1, jtlst *r2)
{
	jtlst *res;
	size_t *v1, *v2;

	if (jtlst_size(r1) != jtlst_size(r2))
		die("rows with different number of fields");

	res = jtlst_new();
	jtlstnode *iter1 = jtlst_iter(r1);
	jtlstnode *iter2 = jtlst_iter(r2);
	while (jtlst_next(&iter1, (void **) &v1) && jtlst_next(&iter2, (void **) &v2))
		jtlst_append(res, (size_t *) max((size_t) v1, (size_t) v2));

	return res;
}

jtlst *
table_widths(jtlst *lines)
{
	jtlst *widths, *width, *row, *res, *tmp;
	char *line;

	widths = jtlst_new();
	JTLST_FOREACH(line, lines) {
		if (is_line(line))
			continue;
		row = line_row(line);
		jtlst_append(widths, row_widths(row));
		jtlst_free_all(row);
	}

	if (jtlst_size(widths) == 0) {
		jtlst_free(widths);
		return NULL;
	}

	jtlst_pop(widths, (void **) &res);
	while (jtlst_pop(widths, (void **) &width)) {
		tmp = max_widths(res, width);
		jtlst_free(res);
		jtlst_free(width);
		res = tmp;
	}

	jtlst_free(widths);

	return res;
}

jtlst *
table_rows(jtlst *lines)
{
	jtlst *rows;
	char *line;

	rows = jtlst_new();
	JTLST_FOREACH(line, lines)
		jtlst_append(rows, line_row(line));

	return rows;
}

void
line_render(char *line, jtlst *widths)
{
	int first = 1;
	size_t *width, nchars, wsum = 0;
	char *field, *space;
	jtlst *fields;
	jtbuf buf;

	JTLST_FOREACH(width, widths)
		wsum += (size_t) width;

	if (is_line(line)) {
		nchars = wsum + (jtlst_size(widths) -1) * 3 + 4;
		line = str_repeat("-", nchars);
		puts(line);
		xfree(line);
	} else {
		buf = jtbuf_new();
		fields = line_row(line);
		jtlstnode *f = jtlst_iter(fields);
		jtlstnode *w = jtlst_iter(widths);
		while (jtlst_next(&f, (void **) &field) && jtlst_next(&w, (void **) &width)) {
			if (first) {
				buf = jtbuf_append_str(buf, "| ");
				first = 0;
			} else {
				buf = jtbuf_append_str(buf, " | ");
			}
			buf = jtbuf_append_str(buf, field);
			space = str_repeat(" ", (size_t) width - utf8len(field));
			buf = jtbuf_append_str(buf, space);
			xfree(space);
		}
		buf = jtbuf_append_str(buf, " |");
		puts(buf);
		jtlst_free_all(fields);
		jtbuf_free(buf);
	}
}

void
retab()
{
	char *line = NULL;
	jtlst *lines, *widths;

	// get lines
	lines = jtlst_new();
	while (read_line(&line))
		jtlst_append(lines, line);

	if (jtlst_size(lines) == 0)
		return;

	// compute cols widths
	widths = table_widths(lines);
	if (widths == NULL)
		die("abort, table without rows");

	// render
	JTLST_FOREACH(line, lines)
		line_render(line, widths);

	jtlst_free_all(lines);
	jtlst_free(widths);
}

int
main(int argc, char *argv[])
{
	int ch;

	while ((ch = getopt(argc, argv, "h")) != -1) {
		switch (ch) {
		case 'h':
			usage();
			break;
		}
	}
	argc -= optind;
	argv += optind;

	retab();
	
	return EXIT_SUCCESS;
}