fold.c 4.9 KB

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