2011-06-08 20:30:33 +00:00
|
|
|
/* See LICENSE file for copyright and license details. */
|
|
|
|
#include <ctype.h>
|
2015-02-01 00:24:03 +00:00
|
|
|
#include <stdint.h>
|
2011-06-08 20:30:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2015-03-16 18:26:42 +00:00
|
|
|
#include <string.h>
|
2014-11-13 17:29:30 +00:00
|
|
|
|
2016-03-09 11:38:44 +00:00
|
|
|
#include "text.h"
|
2011-06-08 20:30:33 +00:00
|
|
|
#include "util.h"
|
|
|
|
|
2015-03-13 22:47:41 +00:00
|
|
|
static int bflag = 0;
|
|
|
|
static int sflag = 0;
|
|
|
|
static size_t width = 80;
|
2011-06-08 20:30:33 +00:00
|
|
|
|
2015-01-25 20:22:17 +00:00
|
|
|
static void
|
2016-03-09 11:38:44 +00:00
|
|
|
foldline(struct line *l) {
|
|
|
|
size_t i, col, last, spacesect, len;
|
2015-01-25 20:22:17 +00:00
|
|
|
|
2016-03-09 11:38:44 +00:00
|
|
|
for (i = 0, last = 0, col = 0, spacesect = 0; i < l->len; i++) {
|
|
|
|
if (!UTF8_POINT(l->data[i]) && !bflag)
|
2015-03-16 18:26:42 +00:00
|
|
|
continue;
|
|
|
|
if (col >= width) {
|
2016-03-09 11:38:44 +00:00
|
|
|
len = ((sflag && spacesect) ? spacesect : i) - last;
|
|
|
|
if (fwrite(l->data + last, 1, len, stdout) != len)
|
2015-03-16 18:26:42 +00:00
|
|
|
eprintf("fwrite <stdout>:");
|
|
|
|
putchar('\n');
|
2016-03-09 11:38:44 +00:00
|
|
|
last = (sflag && spacesect) ? spacesect : i;
|
2015-03-16 18:26:42 +00:00
|
|
|
col = 0;
|
2016-03-09 11:38:44 +00:00
|
|
|
spacesect = 0;
|
2015-03-16 18:26:42 +00:00
|
|
|
}
|
2016-03-09 11:38:44 +00:00
|
|
|
if (sflag && isspace(l->data[i]))
|
|
|
|
spacesect = i + 1;
|
|
|
|
if (!bflag && iscntrl(l->data[i])) {
|
|
|
|
switch(l->data[i]) {
|
2015-03-16 18:26:42 +00:00
|
|
|
case '\b':
|
|
|
|
col -= (col > 0);
|
|
|
|
break;
|
|
|
|
case '\r':
|
|
|
|
col = 0;
|
|
|
|
break;
|
|
|
|
case '\t':
|
|
|
|
col += (col + 1) % 8;
|
|
|
|
break;
|
2015-03-13 22:47:41 +00:00
|
|
|
}
|
2015-03-16 18:26:42 +00:00
|
|
|
} else {
|
|
|
|
col++;
|
2015-01-25 20:22:17 +00:00
|
|
|
}
|
2015-03-16 18:26:42 +00:00
|
|
|
}
|
2016-03-09 11:38:44 +00:00
|
|
|
if (l->len - last)
|
|
|
|
fwrite(l->data + last, 1, l->len - last, stdout);
|
2015-01-25 20:22:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2015-03-13 22:47:41 +00:00
|
|
|
fold(FILE *fp, const char *fname)
|
2015-01-25 20:22:17 +00:00
|
|
|
{
|
2016-03-09 11:38:44 +00:00
|
|
|
static struct line line;
|
|
|
|
static size_t size = 0;
|
|
|
|
ssize_t len;
|
2015-01-25 20:22:17 +00:00
|
|
|
|
2016-03-09 11:38:44 +00:00
|
|
|
while ((len = getline(&line.data, &size, fp)) > 0) {
|
|
|
|
line.len = len;
|
|
|
|
foldline(&line);
|
|
|
|
}
|
2015-03-13 22:47:41 +00:00
|
|
|
if (ferror(fp))
|
|
|
|
eprintf("getline %s:", fname);
|
2015-01-25 20:22:17 +00:00
|
|
|
}
|
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2015-03-16 18:26:42 +00:00
|
|
|
eprintf("usage: %s [-bs] [-w num | -num] [FILE ...]\n", argv0);
|
2013-06-14 18:20:47 +00:00
|
|
|
}
|
|
|
|
|
2011-06-08 20:30:33 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
FILE *fp;
|
2015-03-13 22:47:41 +00:00
|
|
|
int ret = 0;
|
2011-06-08 20:30:33 +00:00
|
|
|
|
2013-06-14 18:20:47 +00:00
|
|
|
ARGBEGIN {
|
|
|
|
case 'b':
|
2014-11-13 20:24:47 +00:00
|
|
|
bflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
|
|
|
case 's':
|
2014-11-13 20:24:47 +00:00
|
|
|
sflag = 1;
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
|
|
|
case 'w':
|
2015-02-01 00:24:03 +00:00
|
|
|
width = estrtonum(EARGF(usage()), 1, MIN(LLONG_MAX, SIZE_MAX));
|
2013-06-14 18:20:47 +00:00
|
|
|
break;
|
2013-11-11 19:53:01 +00:00
|
|
|
ARGNUM:
|
2015-05-19 21:43:58 +00:00
|
|
|
if (!(width = ARGNUMF()))
|
|
|
|
eprintf("illegal width value, too small\n");
|
2013-11-11 19:53:01 +00:00
|
|
|
break;
|
2013-06-14 18:20:47 +00:00
|
|
|
default:
|
|
|
|
usage();
|
2015-11-01 10:16:49 +00:00
|
|
|
} ARGEND
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2015-03-13 22:47:41 +00:00
|
|
|
if (!argc) {
|
|
|
|
fold(stdin, "<stdin>");
|
2015-01-25 20:26:30 +00:00
|
|
|
} else {
|
2015-03-13 22:47:41 +00:00
|
|
|
for (; *argv; argc--, argv++) {
|
2015-05-19 15:44:15 +00:00
|
|
|
if (!strcmp(*argv, "-")) {
|
2015-05-15 11:28:39 +00:00
|
|
|
*argv = "<stdin>";
|
|
|
|
fp = stdin;
|
|
|
|
} else if (!(fp = fopen(*argv, "r"))) {
|
2015-03-13 22:47:41 +00:00
|
|
|
weprintf("fopen %s:", *argv);
|
|
|
|
ret = 1;
|
2015-05-15 11:28:39 +00:00
|
|
|
continue;
|
2014-04-22 10:43:01 +00:00
|
|
|
}
|
2015-05-15 11:28:39 +00:00
|
|
|
fold(fp, *argv);
|
|
|
|
if (fp != stdin && fshut(fp, *argv))
|
|
|
|
ret = 1;
|
2013-11-12 10:44:37 +00:00
|
|
|
}
|
2011-06-08 20:30:33 +00:00
|
|
|
}
|
2013-06-14 18:20:47 +00:00
|
|
|
|
2015-05-24 23:33:19 +00:00
|
|
|
ret |= fshut(stdin, "<stdin>") | fshut(stdout, "<stdout>");
|
|
|
|
|
|
|
|
return ret;
|
2011-06-08 20:30:33 +00:00
|
|
|
}
|