expand.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  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_UNICODE_SUPPORT
  48. {
  49. uni_stat_t uni_stat;
  50. printable_string(&uni_stat, ptr_strbeg);
  51. len = uni_stat.unicode_width;
  52. }
  53. # else
  54. len = ptr - ptr_strbeg;
  55. # endif
  56. len = tab_size - (len % tab_size);
  57. /*while (ptr[1] == '\t') { ptr++; len += tab_size; } - can handle many tabs at once */
  58. printf("%s%*s", ptr_strbeg, len, "");
  59. ptr_strbeg = ptr + 1;
  60. }
  61. ptr++;
  62. }
  63. fputs(ptr_strbeg, stdout);
  64. free(line);
  65. }
  66. }
  67. #endif
  68. #if ENABLE_UNEXPAND
  69. static void unexpand(FILE *file, unsigned tab_size, unsigned opt)
  70. {
  71. char *line;
  72. while ((line = xmalloc_fgets(file)) != NULL) {
  73. char *ptr = line;
  74. unsigned column = 0;
  75. while (*ptr) {
  76. unsigned n;
  77. unsigned len = 0;
  78. while (*ptr == ' ') {
  79. ptr++;
  80. len++;
  81. }
  82. column += len;
  83. if (*ptr == '\t') {
  84. column += tab_size - (column % tab_size);
  85. ptr++;
  86. continue;
  87. }
  88. n = column / tab_size;
  89. if (n) {
  90. len = column = column % tab_size;
  91. while (n--)
  92. putchar('\t');
  93. }
  94. if ((opt & OPT_INITIAL) && ptr != line) {
  95. printf("%*s%s", len, "", ptr);
  96. break;
  97. }
  98. n = strcspn(ptr, "\t ");
  99. printf("%*s%.*s", len, "", n, ptr);
  100. # if ENABLE_UNICODE_SUPPORT
  101. {
  102. char c;
  103. uni_stat_t uni_stat;
  104. c = ptr[n];
  105. ptr[n] = '\0';
  106. printable_string(&uni_stat, ptr);
  107. len = uni_stat.unicode_width;
  108. ptr[n] = c;
  109. }
  110. # else
  111. len = n;
  112. # endif
  113. ptr += n;
  114. column = (column + len) % tab_size;
  115. }
  116. free(line);
  117. }
  118. }
  119. #endif
  120. int expand_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  121. int expand_main(int argc UNUSED_PARAM, char **argv)
  122. {
  123. /* Default 8 spaces for 1 tab */
  124. const char *opt_t = "8";
  125. FILE *file;
  126. unsigned tab_size;
  127. unsigned opt;
  128. int exit_status = EXIT_SUCCESS;
  129. #if ENABLE_FEATURE_EXPAND_LONG_OPTIONS
  130. static const char expand_longopts[] ALIGN1 =
  131. /* name, has_arg, val */
  132. "initial\0" No_argument "i"
  133. "tabs\0" Required_argument "t"
  134. ;
  135. #endif
  136. #if ENABLE_FEATURE_UNEXPAND_LONG_OPTIONS
  137. static const char unexpand_longopts[] ALIGN1 =
  138. /* name, has_arg, val */
  139. "first-only\0" No_argument "i"
  140. "tabs\0" Required_argument "t"
  141. "all\0" No_argument "a"
  142. ;
  143. #endif
  144. init_unicode();
  145. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
  146. IF_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);
  147. opt = getopt32(argv, "it:", &opt_t);
  148. } else {
  149. IF_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts);
  150. /* -t NUM sets also -a */
  151. opt_complementary = "ta";
  152. opt = getopt32(argv, "ft:a", &opt_t);
  153. /* -f --first-only is the default */
  154. if (!(opt & OPT_ALL)) opt |= OPT_INITIAL;
  155. }
  156. tab_size = xatou_range(opt_t, 1, UINT_MAX);
  157. argv += optind;
  158. if (!*argv) {
  159. *--argv = (char*)bb_msg_standard_input;
  160. }
  161. do {
  162. file = fopen_or_warn_stdin(*argv);
  163. if (!file) {
  164. exit_status = EXIT_FAILURE;
  165. continue;
  166. }
  167. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e'))
  168. IF_EXPAND(expand(file, tab_size, opt));
  169. else
  170. IF_UNEXPAND(unexpand(file, tab_size, opt));
  171. /* Check and close the file */
  172. if (fclose_if_not_stdin(file)) {
  173. bb_simple_perror_msg(*argv);
  174. exit_status = EXIT_FAILURE;
  175. }
  176. /* If stdin also clear EOF */
  177. if (file == stdin)
  178. clearerr(file);
  179. } while (*++argv);
  180. /* Now close stdin also */
  181. /* (if we didn't read from it, it's a no-op) */
  182. if (fclose(stdin))
  183. bb_perror_msg_and_die(bb_msg_standard_input);
  184. fflush_stdout_and_exit(exit_status);
  185. }