expand.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. enum {
  25. OPT_INITIAL = 1 << 0,
  26. OPT_TABS = 1 << 1,
  27. OPT_ALL = 1 << 2,
  28. };
  29. static void xputchar(char c)
  30. {
  31. if (putchar(c) < 0)
  32. bb_error_msg_and_die(bb_msg_write_error);
  33. }
  34. #if ENABLE_EXPAND
  35. static void expand(FILE *file, unsigned tab_size, unsigned opt)
  36. {
  37. char *line;
  38. char *ptr;
  39. int convert;
  40. unsigned pos;
  41. /* Increment tab_size by 1 locally.*/
  42. tab_size++;
  43. while ((line = xmalloc_fgets(file)) != NULL) {
  44. convert = 1;
  45. pos = 0;
  46. ptr = line;
  47. while (*line) {
  48. pos++;
  49. if (*line == '\t' && convert) {
  50. for (; pos < tab_size; pos++) {
  51. xputchar(' ');
  52. }
  53. } else {
  54. if ((opt & OPT_INITIAL) && !isblank(*line)) {
  55. convert = 0;
  56. }
  57. xputchar(*line);
  58. }
  59. if (pos == tab_size) {
  60. pos = 0;
  61. }
  62. line++;
  63. }
  64. free(ptr);
  65. }
  66. }
  67. #endif
  68. #if ENABLE_UNEXPAND
  69. static void unexpand(FILE *file, unsigned int tab_size, unsigned opt)
  70. {
  71. char *line;
  72. char *ptr;
  73. int convert;
  74. int pos;
  75. int i = 0;
  76. unsigned column = 0;
  77. while ((line = xmalloc_fgets(file)) != NULL) {
  78. convert = 1;
  79. pos = 0;
  80. ptr = line;
  81. while (*line) {
  82. while ((*line == ' ' || *line == '\t') && convert) {
  83. pos += (*line == ' ') ? 1 : tab_size;
  84. line++;
  85. column++;
  86. if ((opt & OPT_ALL) && column == tab_size) {
  87. column = 0;
  88. goto put_tab;
  89. }
  90. }
  91. if (pos) {
  92. i = pos / tab_size;
  93. if (i) {
  94. for (; i > 0; i--) {
  95. put_tab:
  96. xputchar('\t');
  97. }
  98. } else {
  99. for (i = pos % tab_size; i > 0; i--) {
  100. xputchar(' ');
  101. }
  102. }
  103. pos = 0;
  104. } else {
  105. if (opt & OPT_INITIAL) {
  106. convert = 0;
  107. }
  108. if (opt & OPT_ALL) {
  109. column++;
  110. }
  111. xputchar(*line);
  112. line++;
  113. }
  114. }
  115. free(ptr);
  116. }
  117. }
  118. #endif
  119. int expand_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  120. int expand_main(int argc ATTRIBUTE_UNUSED, char **argv)
  121. {
  122. /* Default 8 spaces for 1 tab */
  123. const char *opt_t = "8";
  124. FILE *file;
  125. unsigned tab_size;
  126. unsigned opt;
  127. int exit_status = EXIT_SUCCESS;
  128. #if ENABLE_FEATURE_EXPAND_LONG_OPTIONS
  129. static const char expand_longopts[] ALIGN1 =
  130. /* name, has_arg, val */
  131. "initial\0" No_argument "i"
  132. "tabs\0" Required_argument "t"
  133. ;
  134. #endif
  135. #if ENABLE_FEATURE_UNEXPAND_LONG_OPTIONS
  136. static const char unexpand_longopts[] ALIGN1 =
  137. /* name, has_arg, val */
  138. "first-only\0" No_argument "i"
  139. "tabs\0" Required_argument "t"
  140. "all\0" No_argument "a"
  141. ;
  142. #endif
  143. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e')) {
  144. USE_FEATURE_EXPAND_LONG_OPTIONS(applet_long_options = expand_longopts);
  145. opt = getopt32(argv, "it:", &opt_t);
  146. } else {
  147. USE_FEATURE_UNEXPAND_LONG_OPTIONS(applet_long_options = unexpand_longopts);
  148. /* -t NUM sets also -a */
  149. opt_complementary = "ta";
  150. opt = getopt32(argv, "ft:a", &opt_t);
  151. /* -f --first-only is the default */
  152. if (!(opt & OPT_ALL)) opt |= OPT_INITIAL;
  153. }
  154. tab_size = xatou_range(opt_t, 1, UINT_MAX);
  155. argv += optind;
  156. if (!*argv) {
  157. *--argv = (char*)bb_msg_standard_input;
  158. }
  159. do {
  160. file = fopen_or_warn_stdin(*argv);
  161. if (!file) {
  162. exit_status = EXIT_FAILURE;
  163. continue;
  164. }
  165. if (ENABLE_EXPAND && (!ENABLE_UNEXPAND || applet_name[0] == 'e'))
  166. USE_EXPAND(expand(file, tab_size, opt));
  167. else
  168. USE_UNEXPAND(unexpand(file, tab_size, opt));
  169. /* Check and close the file */
  170. if (fclose_if_not_stdin(file)) {
  171. bb_simple_perror_msg(*argv);
  172. exit_status = EXIT_FAILURE;
  173. }
  174. /* If stdin also clear EOF */
  175. if (file == stdin)
  176. clearerr(file);
  177. } while (*++argv);
  178. /* Now close stdin also */
  179. /* (if we didn't read from it, it's a no-op) */
  180. if (fclose(stdin))
  181. bb_perror_msg_and_die(bb_msg_standard_input);
  182. fflush_stdout_and_exit(exit_status);
  183. }