fold.c 4.6 KB

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