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