123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186 |
- #include "libbb.h"
- #include "unicode.h"
- #define FLAG_COUNT_BYTES 1
- #define FLAG_BREAK_SPACES 2
- #define FLAG_WIDTH 4
- static int adjust_column(unsigned column, char c)
- {
- if (option_mask32 & FLAG_COUNT_BYTES)
- return ++column;
- if (c == '\t')
- return column + 8 - column % 8;
- if (c == '\b') {
- if ((int)--column < 0)
- column = 0;
- }
- else if (c == '\r')
- column = 0;
- else {
- if (unicode_status != UNICODE_ON
- || (c & 0xc0) != 0x80
- ) {
- column++;
- }
- }
- return column;
- }
- static void write2stdout(const void *buf, unsigned size)
- {
- fwrite(buf, 1, size, stdout);
- }
- int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int fold_main(int argc UNUSED_PARAM, char **argv)
- {
- char *line_out = NULL;
- const char *w_opt = "80";
- unsigned width;
- exitcode_t exitcode = EXIT_SUCCESS;
- init_unicode();
- if (ENABLE_INCLUDE_SUSv2) {
-
- int i;
- for (i = 1; argv[i]; i++) {
- const char *a = argv[i];
- if (*a == '-') {
- a++;
- if (*a == '-' && !a[1])
- break;
- if (isdigit(*a))
- argv[i] = xasprintf("-w%s", a);
- }
- }
- }
- getopt32(argv, "bsw:", &w_opt);
- width = xatou_range(w_opt, 1, 10000);
- argv += optind;
- if (!*argv)
- *--argv = (char*)"-";
- do {
- FILE *istream = fopen_or_warn_stdin(*argv);
- int c;
- unsigned column = 0;
- unsigned offset_out = 0;
- if (istream == NULL) {
- exitcode = EXIT_FAILURE;
- continue;
- }
- while ((c = getc(istream)) != EOF) {
-
- if ((offset_out & 0xfff) == 0) {
- line_out = xrealloc(line_out, offset_out + 0x1000);
- }
- rescan:
- line_out[offset_out] = c;
- if (c == '\n') {
- write2stdout(line_out, offset_out + 1);
- column = offset_out = 0;
- continue;
- }
- column = adjust_column(column, c);
- if (column <= width || offset_out == 0) {
-
- offset_out++;
- continue;
- }
-
- if (option_mask32 & FLAG_BREAK_SPACES) {
- unsigned i;
- unsigned logical_end;
-
- for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) {
- if (!isblank(line_out[logical_end]))
- continue;
-
- logical_end++;
-
- write2stdout(line_out, logical_end);
- putchar('\n');
-
- memmove(line_out, line_out + logical_end, offset_out - logical_end);
- offset_out -= logical_end;
- for (column = i = 0; i < offset_out; i++) {
- column = adjust_column(column, line_out[i]);
- }
- goto rescan;
- }
-
- }
-
- line_out[offset_out] = '\n';
- write2stdout(line_out, offset_out + 1);
- column = offset_out = 0;
- goto rescan;
- }
- if (offset_out) {
- write2stdout(line_out, offset_out);
- }
- if (fclose_if_not_stdin(istream)) {
- bb_simple_perror_msg(*argv);
- exitcode = EXIT_FAILURE;
- }
- } while (*++argv);
- fflush_stdout_and_exit(exitcode);
- }
|