/usr.bin/expand/expand.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 196 lines · 147 code · 16 blank · 33 comment · 41 complexity · d94c99d381c2594a2802977ea260309e MD5 · raw file

  1. /*
  2. * Copyright (c) 1980, 1993
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 4. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. #ifndef lint
  30. static const char copyright[] =
  31. "@(#) Copyright (c) 1980, 1993\n\
  32. The Regents of the University of California. All rights reserved.\n";
  33. #endif /* not lint */
  34. #ifndef lint
  35. #if 0
  36. static char sccsid[] = "@(#)expand.c 8.1 (Berkeley) 6/9/93";
  37. #endif
  38. #endif /* not lint */
  39. #include <sys/cdefs.h>
  40. __FBSDID("$FreeBSD$");
  41. #include <ctype.h>
  42. #include <err.h>
  43. #include <locale.h>
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <unistd.h>
  47. #include <wchar.h>
  48. #include <wctype.h>
  49. /*
  50. * expand - expand tabs to equivalent spaces
  51. */
  52. static int nstops;
  53. static int tabstops[100];
  54. static void getstops(char *);
  55. static void usage(void);
  56. int
  57. main(int argc, char *argv[])
  58. {
  59. const char *curfile;
  60. wint_t wc;
  61. int c, column;
  62. int n;
  63. int rval;
  64. int width;
  65. setlocale(LC_CTYPE, "");
  66. /* handle obsolete syntax */
  67. while (argc > 1 && argv[1][0] == '-' &&
  68. isdigit((unsigned char)argv[1][1])) {
  69. getstops(&argv[1][1]);
  70. argc--; argv++;
  71. }
  72. while ((c = getopt (argc, argv, "t:")) != -1) {
  73. switch (c) {
  74. case 't':
  75. getstops(optarg);
  76. break;
  77. case '?':
  78. default:
  79. usage();
  80. /* NOTREACHED */
  81. }
  82. }
  83. argc -= optind;
  84. argv += optind;
  85. rval = 0;
  86. do {
  87. if (argc > 0) {
  88. if (freopen(argv[0], "r", stdin) == NULL) {
  89. warn("%s", argv[0]);
  90. rval = 1;
  91. argc--, argv++;
  92. continue;
  93. }
  94. curfile = argv[0];
  95. argc--, argv++;
  96. } else
  97. curfile = "stdin";
  98. column = 0;
  99. while ((wc = getwchar()) != WEOF) {
  100. switch (wc) {
  101. case '\t':
  102. if (nstops == 0) {
  103. do {
  104. putwchar(' ');
  105. column++;
  106. } while (column & 07);
  107. continue;
  108. }
  109. if (nstops == 1) {
  110. do {
  111. putwchar(' ');
  112. column++;
  113. } while (((column - 1) % tabstops[0]) != (tabstops[0] - 1));
  114. continue;
  115. }
  116. for (n = 0; n < nstops; n++)
  117. if (tabstops[n] > column)
  118. break;
  119. if (n == nstops) {
  120. putwchar(' ');
  121. column++;
  122. continue;
  123. }
  124. while (column < tabstops[n]) {
  125. putwchar(' ');
  126. column++;
  127. }
  128. continue;
  129. case '\b':
  130. if (column)
  131. column--;
  132. putwchar('\b');
  133. continue;
  134. default:
  135. putwchar(wc);
  136. if ((width = wcwidth(wc)) > 0)
  137. column += width;
  138. continue;
  139. case '\n':
  140. putwchar(wc);
  141. column = 0;
  142. continue;
  143. }
  144. }
  145. if (ferror(stdin)) {
  146. warn("%s", curfile);
  147. rval = 1;
  148. }
  149. } while (argc > 0);
  150. exit(rval);
  151. }
  152. static void
  153. getstops(char *cp)
  154. {
  155. int i;
  156. nstops = 0;
  157. for (;;) {
  158. i = 0;
  159. while (*cp >= '0' && *cp <= '9')
  160. i = i * 10 + *cp++ - '0';
  161. if (i <= 0)
  162. errx(1, "bad tab stop spec");
  163. if (nstops > 0 && i <= tabstops[nstops-1])
  164. errx(1, "bad tab stop spec");
  165. if (nstops == sizeof(tabstops) / sizeof(*tabstops))
  166. errx(1, "too many tab stops");
  167. tabstops[nstops++] = i;
  168. if (*cp == 0)
  169. break;
  170. if (*cp != ',' && !isblank((unsigned char)*cp))
  171. errx(1, "bad tab stop spec");
  172. cp++;
  173. }
  174. }
  175. static void
  176. usage(void)
  177. {
  178. (void)fprintf (stderr, "usage: expand [-t tablist] [file ...]\n");
  179. exit(1);
  180. }