fold.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * fold -- wrap each input line to fit in specified width.
  4. *
  5. * Written by David MacKenzie, djm@gnu.ai.mit.edu.
  6. * Copyright (C) 91, 1995-2002 Free Software Foundation, Inc.
  7. *
  8. * Modified for busybox based on coreutils v 5.0
  9. * Copyright (C) 2003 Glenn McGrath
  10. *
  11. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  12. */
  13. //config:config FOLD
  14. //config: bool "fold (4.6 kb)"
  15. //config: default y
  16. //config: help
  17. //config: Wrap text to fit a specific width.
  18. //applet:IF_FOLD(APPLET_NOEXEC(fold, fold, BB_DIR_USR_BIN, BB_SUID_DROP, fold))
  19. //kbuild:lib-$(CONFIG_FOLD) += fold.o
  20. //usage:#define fold_trivial_usage
  21. //usage: "[-bs] [-w WIDTH] [FILE]..."
  22. //usage:#define fold_full_usage "\n\n"
  23. //usage: "Wrap input lines in FILEs (or stdin), writing to stdout\n"
  24. //usage: "\n -b Count bytes rather than columns"
  25. //usage: "\n -s Break at spaces"
  26. //usage: "\n -w Use WIDTH columns instead of 80"
  27. #include "libbb.h"
  28. #include "unicode.h"
  29. /* This is a NOEXEC applet. Be very careful! */
  30. /* Must match getopt32 call */
  31. #define FLAG_COUNT_BYTES 1
  32. #define FLAG_BREAK_SPACES 2
  33. #define FLAG_WIDTH 4
  34. /* Assuming the current column is COLUMN, return the column that
  35. printing C will move the cursor to.
  36. The first column is 0. */
  37. static int adjust_column(unsigned column, char c)
  38. {
  39. if (option_mask32 & FLAG_COUNT_BYTES)
  40. return ++column;
  41. if (c == '\t')
  42. return column + 8 - column % 8;
  43. if (c == '\b') {
  44. if ((int)--column < 0)
  45. column = 0;
  46. }
  47. else if (c == '\r')
  48. column = 0;
  49. else { /* just a printable char */
  50. if (unicode_status != UNICODE_ON /* every byte is a new char */
  51. || (c & 0xc0) != 0x80 /* it isn't a 2nd+ byte of a Unicode char */
  52. ) {
  53. column++;
  54. }
  55. }
  56. return column;
  57. }
  58. /* Note that this function can write NULs, unlike fputs etc. */
  59. static void write2stdout(const void *buf, unsigned size)
  60. {
  61. fwrite(buf, 1, size, stdout);
  62. }
  63. int fold_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  64. int fold_main(int argc UNUSED_PARAM, char **argv)
  65. {
  66. char *line_out = NULL;
  67. const char *w_opt = "80";
  68. unsigned width;
  69. smallint exitcode = EXIT_SUCCESS;
  70. init_unicode();
  71. if (ENABLE_INCLUDE_SUSv2) {
  72. /* Turn any numeric options into -w options. */
  73. int i;
  74. for (i = 1; argv[i]; i++) {
  75. const char *a = argv[i];
  76. if (*a == '-') {
  77. a++;
  78. if (*a == '-' && !a[1]) /* "--" */
  79. break;
  80. if (isdigit(*a))
  81. argv[i] = xasprintf("-w%s", a);
  82. }
  83. }
  84. }
  85. getopt32(argv, "bsw:", &w_opt);
  86. width = xatou_range(w_opt, 1, 10000);
  87. argv += optind;
  88. if (!*argv)
  89. *--argv = (char*)"-";
  90. do {
  91. FILE *istream = fopen_or_warn_stdin(*argv);
  92. int c;
  93. unsigned column = 0; /* Screen column where next char will go */
  94. unsigned offset_out = 0; /* Index in 'line_out' for next char */
  95. if (istream == NULL) {
  96. exitcode = EXIT_FAILURE;
  97. continue;
  98. }
  99. while ((c = getc(istream)) != EOF) {
  100. /* We grow line_out in chunks of 0x1000 bytes */
  101. if ((offset_out & 0xfff) == 0) {
  102. line_out = xrealloc(line_out, offset_out + 0x1000);
  103. }
  104. rescan:
  105. line_out[offset_out] = c;
  106. if (c == '\n') {
  107. write2stdout(line_out, offset_out + 1);
  108. column = offset_out = 0;
  109. continue;
  110. }
  111. column = adjust_column(column, c);
  112. if (column <= width || offset_out == 0) {
  113. /* offset_out == 0 case happens
  114. * with small width (say, 1) and tabs.
  115. * The very first tab already goes to column 8,
  116. * but we must not wrap it */
  117. offset_out++;
  118. continue;
  119. }
  120. /* This character would make the line too long.
  121. * Print the line plus a newline, and make this character
  122. * start the next line */
  123. if (option_mask32 & FLAG_BREAK_SPACES) {
  124. unsigned i;
  125. unsigned logical_end;
  126. /* Look for the last blank. */
  127. for (logical_end = offset_out - 1; (int)logical_end >= 0; logical_end--) {
  128. if (!isblank(line_out[logical_end]))
  129. continue;
  130. /* Found a space or tab.
  131. * Output up to and including it, and start a new line */
  132. logical_end++;
  133. /*line_out[logical_end] = '\n'; - NO! this nukes one buffered character */
  134. write2stdout(line_out, logical_end);
  135. putchar('\n');
  136. /* Move the remainder to the beginning of the next line.
  137. * The areas being copied here might overlap. */
  138. memmove(line_out, line_out + logical_end, offset_out - logical_end);
  139. offset_out -= logical_end;
  140. for (column = i = 0; i < offset_out; i++) {
  141. column = adjust_column(column, line_out[i]);
  142. }
  143. goto rescan;
  144. }
  145. /* No blank found, wrap will split the overlong word */
  146. }
  147. /* Output what we accumulated up to now, and start a new line */
  148. line_out[offset_out] = '\n';
  149. write2stdout(line_out, offset_out + 1);
  150. column = offset_out = 0;
  151. goto rescan;
  152. } /* while (not EOF) */
  153. if (offset_out) {
  154. write2stdout(line_out, offset_out);
  155. }
  156. if (fclose_if_not_stdin(istream)) {
  157. bb_simple_perror_msg(*argv);
  158. exitcode = EXIT_FAILURE;
  159. }
  160. } while (*++argv);
  161. fflush_stdout_and_exit(exitcode);
  162. }