/usr.bin/vis/vis.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 189 lines · 140 code · 17 blank · 32 comment · 26 complexity · f600b22027a42b211c29ecb7de63f183 MD5 · raw file

  1. /*-
  2. * Copyright (c) 1989, 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. #include <sys/cdefs.h>
  30. __FBSDID("$FreeBSD$");
  31. #ifndef lint
  32. static const char copyright[] =
  33. "@(#) Copyright (c) 1989, 1993\n\
  34. The Regents of the University of California. All rights reserved.\n";
  35. #endif
  36. #ifndef lint
  37. static const char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 6/6/93";
  38. #endif
  39. #include <err.h>
  40. #include <locale.h>
  41. #include <stdio.h>
  42. #include <stdlib.h>
  43. #include <unistd.h>
  44. #include <vis.h>
  45. #include "extern.h"
  46. static int eflags, fold, foldwidth = 80, none, markeol;
  47. #ifdef DEBUG
  48. static int debug;
  49. #endif
  50. static void process(FILE *);
  51. static void usage(void);
  52. int
  53. main(int argc, char *argv[])
  54. {
  55. FILE *fp;
  56. int ch;
  57. (void) setlocale(LC_CTYPE, "");
  58. while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != -1)
  59. switch((char)ch) {
  60. case 'n':
  61. none++;
  62. break;
  63. case 'w':
  64. eflags |= VIS_WHITE;
  65. break;
  66. case 'c':
  67. eflags |= VIS_CSTYLE;
  68. break;
  69. case 't':
  70. eflags |= VIS_TAB;
  71. break;
  72. case 's':
  73. eflags |= VIS_SAFE;
  74. break;
  75. case 'o':
  76. eflags |= VIS_OCTAL;
  77. break;
  78. case 'b':
  79. eflags |= VIS_NOSLASH;
  80. break;
  81. case 'F':
  82. if ((foldwidth = atoi(optarg))<5)
  83. errx(1, "can't fold lines to less than 5 cols");
  84. /*FALLTHROUGH*/
  85. case 'f':
  86. fold++; /* fold output lines to 80 cols */
  87. break; /* using hidden newline */
  88. case 'l':
  89. markeol++; /* mark end of line with \$ */
  90. break;
  91. #ifdef DEBUG
  92. case 'd':
  93. debug++;
  94. break;
  95. #endif
  96. case '?':
  97. default:
  98. usage();
  99. }
  100. argc -= optind;
  101. argv += optind;
  102. if (*argv)
  103. while (*argv) {
  104. if ((fp=fopen(*argv, "r")) != NULL)
  105. process(fp);
  106. else
  107. warn("%s", *argv);
  108. argv++;
  109. }
  110. else
  111. process(stdin);
  112. exit(0);
  113. }
  114. static void
  115. usage(void)
  116. {
  117. #ifdef DEBUG
  118. fprintf(stderr, "usage: vis [-cbflnostwd] [-F foldwidth] [file ...]\n");
  119. #else
  120. fprintf(stderr, "usage: vis [-cbflnostw] [-F foldwidth] [file ...]\n");
  121. #endif
  122. exit(1);
  123. }
  124. static void
  125. process(FILE *fp)
  126. {
  127. static int col = 0;
  128. static char dummy[] = "\0";
  129. char *cp = dummy+1; /* so *(cp-1) starts out != '\n' */
  130. int c, rachar;
  131. char buff[5];
  132. c = getc(fp);
  133. while (c != EOF) {
  134. rachar = getc(fp);
  135. if (none) {
  136. cp = buff;
  137. *cp++ = c;
  138. if (c == '\\')
  139. *cp++ = '\\';
  140. *cp = '\0';
  141. } else if (markeol && c == '\n') {
  142. cp = buff;
  143. if ((eflags & VIS_NOSLASH) == 0)
  144. *cp++ = '\\';
  145. *cp++ = '$';
  146. *cp++ = '\n';
  147. *cp = '\0';
  148. } else
  149. (void) vis(buff, (char)c, eflags, (char)rachar);
  150. cp = buff;
  151. if (fold) {
  152. #ifdef DEBUG
  153. if (debug)
  154. printf("<%02d,", col);
  155. #endif
  156. col = foldit(cp, col, foldwidth);
  157. #ifdef DEBUG
  158. if (debug)
  159. printf("%02d>", col);
  160. #endif
  161. }
  162. do {
  163. putchar(*cp);
  164. } while (*++cp);
  165. c = rachar;
  166. }
  167. /*
  168. * terminate partial line with a hidden newline
  169. */
  170. if (fold && *(cp-1) != '\n')
  171. printf("\\\n");
  172. }