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
|
/*
* BSD Zero Clause License
*
* Copyright (c) 2022 Thomas Voss
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
* PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/ioctl.h>
#include <sys/queue.h>
#include <ctype.h>
#include <err.h>
#include <errno.h>
#include <getopt.h>
#include <limits.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define ESC 033
#define die(...) err(EXIT_FAILURE, __VA_ARGS__)
#define diex(...) errx(EXIT_FAILURE, __VA_ARGS__)
struct line_item {
char *buffer;
STAILQ_ENTRY(line_item) list;
};
STAILQ_HEAD(lines_head, line_item);
static void center(FILE *);
static void center_by_longest(FILE *);
static void println(const char *, size_t);
static long polong(char *, const char *);
static int cols(void);
static int utf8len(const char *);
static int noesclen(const char *);
static int matchesc(const char *);
static int cnttabs(const char *);
extern int optind;
extern char *optarg;
int rval;
long width = -1;
long tabwidth = 8;
bool rflag;
int (*lenfunc)(const char *) = noesclen;
/* Return the length of the line `s' which contains `tabs' amount of tab
* characters.
*/
static inline int
calclen(const char *s, int tabs)
{
return lenfunc(s) - tabs + tabs * tabwidth;
}
int
main(int argc, char **argv)
{
int opt;
const char *optstr = "elrt:w:",
*progargs = "[-elr] [-t width] [-w width] [file ...]";
void (*centerfunc)(FILE *) = center;
static struct option longopts[] = {
{"ignore-ansi", no_argument, NULL, 'e'},
{"longest", no_argument, NULL, 'l'},
{"spaces", no_argument, NULL, 'r'},
{"tabsize", required_argument, NULL, 't'},
{"width", required_argument, NULL, 'w'},
{NULL, 0, NULL, 0 }
};
while ((opt = getopt_long(argc, argv, optstr, longopts, NULL)) != -1) {
switch (opt) {
case 'e':
lenfunc = utf8len;
break;
case 'l':
centerfunc = center_by_longest;
break;
case 'r':
rflag = true;
break;
case 't':
tabwidth = polong(optarg, "tab width");
break;
case 'w':
width = polong(optarg, "output width");
break;
default:
fprintf(stderr, "Usage: %s %s\n", argv[0], progargs);
exit(EXIT_FAILURE);
}
}
if (width == -1)
width = cols();
argc -= optind;
argv += optind;
if (argc == 0)
centerfunc(stdin);
else do {
if (strcmp(*argv, "-") == 0)
centerfunc(stdin);
else {
FILE *fp;
if ((fp = fopen(*argv, "r")) == NULL) {
warn("fopen");
rval = EXIT_FAILURE;
} else {
centerfunc(fp);
fclose(fp);
}
}
} while (*++argv);
return rval;
}
/* Write the contents of the file pointed to by `fp' center aligned to the
* standard output.
*/
void
center(FILE *fp)
{
static char *line = NULL;
static size_t bs = 0;
while (getline(&line, &bs, fp) != -1) {
int len, tabs;
tabs = cnttabs(line);
len = calclen(line, tabs);
println(line, len);
}
if (ferror(fp)) {
warn("getline");
rval = EXIT_FAILURE;
}
}
/* Same as center(), but aligns all lines according to the longest line. Great
* for centering code.
*/
void
center_by_longest(FILE *fp)
{
int tabs;
size_t curr_len, longest = 0;
struct lines_head list_head;
struct line_item *line, *tmp;
static char *buffer = NULL;
static size_t bs = 0;
STAILQ_INIT(&list_head);
/* Collect all input lines in a list and find the longest */
while (getline(&buffer, &bs, fp) != -1) {
line = malloc(sizeof(struct line_item));
if (!line) {
warn("malloc");
rval = EXIT_FAILURE;
return;
}
line->buffer = buffer;
STAILQ_INSERT_TAIL(&list_head, line, list);
tabs = cnttabs(buffer);
curr_len = calclen(buffer, tabs);
if (curr_len > longest)
longest = curr_len;
/* Reset buffer and bs so getline() can alloc a new buffer */
buffer = NULL;
bs = 0;
}
if (ferror(fp)) {
warn("getline");
rval = EXIT_FAILURE;
return;
}
/* Output lines aligned to the longest and free them */
line = STAILQ_FIRST(&list_head);
while (line != NULL) {
println(line->buffer, longest);
tmp = STAILQ_NEXT(line, list);
free(line->buffer);
free(line);
line = tmp;
}
}
/* Return the column width of the output device if it is a TTY. If it is not a
* TTY then return -1.
*/
int
cols(void)
{
char *s;
struct winsize w;
if ((s = getenv("COLUMNS")) != NULL)
return polong(s, "output width");
if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &w) == -1) {
if (errno == ENOTTY)
return 80;
else
die("ioctl");
}
return w.ws_col;
}
/* Return the length of the UTF-8 encoded string `s' in characters, not bytes */
int
utf8len(const char *s)
{
int l = 0;
for (; *s; s++)
l += *s == '\b' ? -1 : (*s & 0xC0) != 0x80;
if (l > 0 && s[-1] == '\n')
l--;
return l;
}
/* Return the length of the UTF-8 encoded string `s' in characters, not bytes.
* Additionally this function ignores ANSI color escape sequences when computing
* the length.
*/
int
noesclen(const char *s)
{
int off = 0;
const char *d = s;
while ((d = strchr(d, ESC)) != NULL)
off += matchesc(++d);
return utf8len(s) - off;
}
/* Return the length of the ANSI color escape sequence at the beginning of the
* string `s'. If no escape sequence is matched then 0 is returned. The local
* variable `c' is initialized to 3 in order to account for the leading ESC, the
* following `[', and the trailing `m'.
*/
int
matchesc(const char *s)
{
int c = 3;
if (*s++ != '[')
return 0;
while (isdigit(*s) || *s == ';') {
c++;
s++;
}
return *s == 'm' ? c : 0;
}
/* Return the amount of tab characters present in the string `s'. */
int
cnttabs(const char *s)
{
int cnt = 0;
const char *p = s - 1;
while ((p = strchr(p + 1, '\t')) != NULL)
cnt++;
return cnt;
}
/* Print the line `s' with a length of `len' centered to standard output. */
void
println(const char *s, size_t len)
{
for (int i = (width - len) / 2 - 1; i >= 0; i--)
putchar(' ');
if (rflag) {
for (int i = 0; s[i]; i++) {
if (s[i] == '\t') {
for (int j = 0; j < tabwidth; j++)
putchar(' ');
} else
putchar(s[i]);
}
} else
fputs(s, stdout);
}
/* Parse `optarg' as a long and store the result in `n'. If `optarg' is less
* than 0 then print a diagnostic message with the variable name `s' and exit.
*/
long
polong(char *s, const char *e)
{
long n;
char *endptr;
n = strtol(s, &endptr, 0);
if (*s == '\0' || *endptr != '\0')
diex("invalid integer '%s'", s);
if (n < 0)
diex("%s must be >= 0", e);
if (errno == ERANGE || n > INT_MAX) \
warnx("potential overflow of %s", e);
return n;
}
|