fold.c 4.3 KB

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