fold.c 4.3 KB

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