expand.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. /* expand - convert tabs to spaces
  2. * unexpand - convert spaces to tabs
  3. *
  4. * Copyright (C) 89, 91, 1995-2006 Free Software Foundation, Inc.
  5. *
  6. * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  7. *
  8. * David MacKenzie <djm@gnu.ai.mit.edu>
  9. *
  10. * Options for expand:
  11. * -t num --tabs=NUM Convert tabs to num spaces (default 8 spaces).
  12. * -i --initial Only convert initial tabs on each line to spaces.
  13. *
  14. * Options for unexpand:
  15. * -a --all Convert all blanks, instead of just initial blanks.
  16. * -f --first-only Convert only leading sequences of blanks (default).
  17. * -t num --tabs=NUM Have tabs num characters apart instead of 8.
  18. *
  19. * Busybox version (C) 2007 by Tito Ragusa <farmatito@tiscali.it>
  20. *
  21. * Caveat: this versions of expand and unexpand don't accept tab lists.
  22. */
  23. #include "libbb.h"
  24. #include "unicode.h"
  25. enum {
  26. OPT_INITIAL = 1 << 0,
  27. OPT_TABS = 1 << 1,
  28. OPT_ALL = 1 << 2,
  29. };
  30. #if ENABLE_EXPAND
  31. static void expand(FILE *file, unsigned tab_size, unsigned opt)
  32. {
  33. char *line;
  34. while ((line = xmalloc_fgets(file)) != NULL) {
  35. unsigned char c;
  36. char *ptr;
  37. char *ptr_strbeg;
  38. ptr = ptr_strbeg = line;
  39. while ((c = *ptr) != '\0') {
  40. if ((opt & OPT_INITIAL) && !isblank(c)) {
  41. /* not space or tab */
  42. break;
  43. }
  44. if (c == '\t') {
  45. unsigned len;
  46. *ptr = '\0';
  47. # if ENABLE_FEATURE_ASSUME_UNICODE
  48. len = unicode_strlen(ptr_strbeg);
  49. # else
  50. len = ptr - ptr_strbeg;
  51. # endif
  52. len = tab_size - (len % tab_size);
  53. /*while (ptr[1] == '\t') { ptr++; len += tab_size; } - can handle many tabs at once */
  54. printf("%s%*s", ptr_strbeg, len, "");
  55. ptr_strbeg = ptr + 1;
  56. }
  57. ptr++;
  58. }
  59. fputs(ptr_strbeg, stdout);
  60. free(line);
  61. }
  62. }
  63. #endif
  64. #if ENABLE_UNEXPAND
  65. static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
  66. {
  67. char *line;
  68. while ((line = xmalloc_fgets(file)) != NULL) {
  69. char *ptr = line;
  70. unsigned column = 0;
  71. while (*ptr) {
  72. unsigned n;
  73. unsigned len;
  74. while (*ptr == ' ') {
  75. column++;
  76. ptr++;
  77. }
  78. if (*ptr == '\t') {
  79. column += tab_size - (column % tab_size);
  80. ptr++;
  81. continue;
  82. }
  83. n = column / tab_size;
  84. column = column % tab_size;
  85. while (n--)
  86. putchar('\t');
  87. if ((opt & OPT_INITIAL) && ptr != line) {
  88. printf("%*s%s", column, "", ptr);
  89. break;
  90. }
  91. n = strcspn(ptr, "\t ");
  92. printf("%*s%.*s", column, "", n, ptr);
  93. # if ENABLE_FEATURE_ASSUME_UNICODE
  94. {
  95. char c;
  96. c = ptr[n];
  97. ptr[n] = '\0';
  98. len = unicode_strlen(ptr);
  99. ptr[n] = c;
  100. }
  101. # else
  102. len = n;
  103. # endif
  104. ptr += n;
  105. column = (column + len) % tab_size;
  106. }
  107. free(line);
  108. }
  109. }
  110. #endif
  111. int expand_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  112. int expand_main(int argc UNUSED_PARAM, char **argv)
  113. {
  114. /* Default 8 spaces for 1 tab */
  115. const char *opt_t = "8";
  116. FILE *file;
  117. unsigned tab_size;
  118. unsigned opt;
  119. int exit_status = EXIT_SUCCESS;
  120. #if ENABLE_FEATURE_EXPAND_LONG_OPTIONS
  121. static const char expand_longopts[] ALIGN1 =
  122. /* name, has_arg, val */
  123. "initial\0" No_argument "i"
  124. "tabs\0" Required_argument "t"
  125. ;
  126. #endif
  127. #if ENABLE_FEATURE_UNEXPAND_LONG_OPTIONS
  128. static const char unexpand_longopts[] ALIGN1 =
  129. /* name, has_arg, val */
  130. "first-only\0" No_argument "i"
  131. "tabs\0" Required_argument "t"
  132. "all\0" No_argument "a"
  133. ;
  134. #endif
  135. init_unicode();
  136. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
  137. IF_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);
  138. opt = getopt32(argv, "it:", &opt_t);
  139. } else {
  140. IF_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts);
  141. /* -t NUM sets also -a */
  142. opt_complementary = "ta";
  143. opt = getopt32(argv, "ft:a", &opt_t);
  144. /* -f --first-only is the default */
  145. if (!(opt & OPT_ALL)) opt |= OPT_INITIAL;
  146. }
  147. tab_size = xatou_range(opt_t, 1, UINT_MAX);
  148. argv += optind;
  149. if (!*argv) {
  150. *--argv = (char*)bb_msg_standard_input;
  151. }
  152. do {
  153. file = fopen_or_warn_stdin(*argv);
  154. if (!file) {
  155. exit_status = EXIT_FAILURE;
  156. continue;
  157. }
  158. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e'))
  159. IF_EXPAND(expand(file, tab_size, opt));
  160. else
  161. IF_UNEXPAND(unexpand(file, tab_size, opt));
  162. /* Check and close the file */
  163. if (fclose_if_not_stdin(file)) {
  164. bb_simple_perror_msg(*argv);
  165. exit_status = EXIT_FAILURE;
  166. }
  167. /* If stdin also clear EOF */
  168. if (file == stdin)
  169. clearerr(file);
  170. } while (*++argv);
  171. /* Now close stdin also */
  172. /* (if we didn't read from it, it's a no-op) */
  173. if (fclose(stdin))
  174. bb_perror_msg_and_die(bb_msg_standard_input);
  175. fflush_stdout_and_exit(exit_status);
  176. }