aboutsummaryrefslogtreecommitdiff
path: root/toys/pending/fold.c
blob: 2291308f2b84647568442d1e4c70ec206eabd3e3 (plain)
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
/* fold.c - fold text
 *
 * Copyright 2014 Samuel Holland <samuel@sholland.net>
 *
 * See http://pubs.opengroup.org/onlinepubs/9699919799/utilities/fold.html

USE_FOLD(NEWTOY(fold, "bsuw#", TOYFLAG_USR|TOYFLAG_BIN))

config FOLD
  bool "fold"
  default n
  help
    usage: fold [-bsu] [-w WIDTH] [FILE...]

    Folds (wraps) or unfolds FILE or stdin by adding or removing newlines.
    Default line width is 80 columns for folding and infinite for unfolding.

    -b  Fold based on bytes instead of columns
    -s  Fold/unfold at whitespace boundaries if possible
    -u  Unfold text (and refold if -w is given)
    -w  Set lines to WIDTH columns or bytes
*/

#define FOR_fold
#include "toys.h"

GLOBALS(
  int width;
)

void do_fold(int fd, char *name)
{
  char *buf;
  int bufsz, pos, len = 0, maxlen, split;

  if (toys.optflags & FLAG_w)
    maxlen = TT.width;
  else if (toys.optflags & FLAG_u)
    maxlen = 0;
  else
    maxlen = 80;

  while ((bufsz = read(fd, toybuf, sizeof(toybuf))) > 0) {
    buf = toybuf;
    pos = 0;
    split = -1;

    while (pos < bufsz) {
      switch (buf[pos]) {
        case '\n':
          //print everything but the \n, then move on to the next buffer
          if ((toys.optflags & FLAG_u) && buf[pos-1] != '\n'
                                       && buf[pos+1] != '\n') {
              xwrite(1, buf, pos);
              bufsz -= pos + 1;
              buf += pos + 1;
              pos = 0;
              split = -1;
          }
          //reset len, FLAG_b or not; just print multiple lines at once
          else len = 0;
          break;
        case '\b':
          //len cannot be negative; not allowed to wrap after backspace
          if (toys.optflags & FLAG_b) len++;
          else if (len > 0) len--;
          break;
        case '\r':
          //not allowed to wrap after carriage return
          if (toys.optflags & FLAG_b) len++;
          else len = 0;
          break;
        case '\t':
          //round to 8, but we add one after falling through
          //(because of whitespace, but it also takes care of FLAG_b)
          if (!(toys.optflags & FLAG_b)) len = (len & -8) + 7;
        case ' ':
          split = pos;
        default:
          len++;
      }

      //we don't want to double up \n; not allowed to wrap before \b
      if (maxlen > 0 && len >= maxlen && buf[pos+1] != '\n' && buf[pos+1] != '\b') {
        if (!(toys.optflags & FLAG_s) || split < 0) split = pos;
        xwrite(1, buf, split + 1);
        xputc('\n');
        bufsz -= split + 1;
        buf += split + 1;
        len = pos = 0;
        split = -1;
      } else {
        pos++;
      }
    }
    xwrite(1, buf, bufsz);
  }
  xputc('\n');
}

void fold_main(void)
{
  loopfiles(toys.optargs, do_fold);
}