xref: /original-bsd/usr.bin/vis/vis.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1989, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 static char copyright[] =
10 "@(#) Copyright (c) 1989, 1993\n\
11 	The Regents of the University of California.  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)vis.c	8.1 (Berkeley) 06/06/93";
16 #endif /* not lint */
17 
18 #include <stdio.h>
19 #include <vis.h>
20 
21 int eflags, fold, foldwidth=80, none, markeol, debug;
22 
23 main(argc, argv)
24 	char *argv[];
25 {
26 	extern char *optarg;
27 	extern int optind;
28 	extern int errno;
29 	FILE *fp;
30 	int ch;
31 
32 	while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != EOF)
33 		switch((char)ch) {
34 		case 'n':
35 			none++;
36 			break;
37 		case 'w':
38 			eflags |= VIS_WHITE;
39 			break;
40 		case 'c':
41 			eflags |= VIS_CSTYLE;
42 			break;
43 		case 't':
44 			eflags |= VIS_TAB;
45 			break;
46 		case 's':
47 			eflags |= VIS_SAFE;
48 			break;
49 		case 'o':
50 			eflags |= VIS_OCTAL;
51 			break;
52 		case 'b':
53 			eflags |= VIS_NOSLASH;
54 			break;
55 		case 'F':
56 			if ((foldwidth = atoi(optarg))<5) {
57 				fprintf(stderr,
58 				 "vis: can't fold lines to less than 5 cols\n");
59 				exit(1);
60 			}
61 			/*FALLTHROUGH*/
62 		case 'f':
63 			fold++;		/* fold output lines to 80 cols */
64 			break;		/* using hidden newline */
65 		case 'l':
66 			markeol++;	/* mark end of line with \$ */
67 			break;
68 #ifdef DEBUG
69 		case 'd':
70 			debug++;
71 			break;
72 #endif
73 		case '?':
74 		default:
75 			fprintf(stderr,
76 		"usage: vis [-nwctsobf] [-F foldwidth]\n");
77 			exit(1);
78 		}
79 	argc -= optind;
80 	argv += optind;
81 
82 	if (*argv)
83 		while (*argv) {
84 			if ((fp=fopen(*argv, "r")) != NULL)
85 				process(fp, *argv);
86 			else
87 				fprintf(stderr, "vis: %s: %s\n", *argv,
88 				    (char *)strerror(errno));
89 			argv++;
90 		}
91 	else
92 		process(stdin, "<stdin>");
93 	exit(0);
94 }
95 
96 process(fp, filename)
97 	FILE *fp;
98 	char *filename;
99 {
100 	static int col = 0;
101 	register char *cp = "\0"+1;	/* so *(cp-1) starts out != '\n' */
102 	register int c, rachar;
103 	register char nc;
104 	char buff[5];
105 
106 	c = getc(fp);
107 	while (c != EOF) {
108 		rachar = getc(fp);
109 		if (none) {
110 			cp = buff;
111 			*cp++ = c;
112 			if (c == '\\')
113 				*cp++ = '\\';
114 			*cp = '\0';
115 		} else if (markeol && c == '\n') {
116 			cp = buff;
117 			if ((eflags & VIS_NOSLASH) == 0)
118 				*cp++ = '\\';
119 			*cp++ = '$';
120 			*cp++ = '\n';
121 			*cp = '\0';
122 		} else
123 			(void) vis(buff, (char)c, eflags, (char)rachar);
124 
125 		cp = buff;
126 		if (fold) {
127 #ifdef DEBUG
128 			if (debug)
129 				printf("<%02d,", col);
130 #endif
131 			col = foldit(cp, col, foldwidth);
132 #ifdef DEBUG
133 			if (debug)
134 				printf("%02d>", col);
135 #endif
136 		}
137 		do {
138 			putchar(*cp);
139 		} while (*++cp);
140 		c = rachar;
141 	}
142 	/*
143 	 * terminate partial line with a hidden newline
144 	 */
145 	if (fold && *(cp-1) != '\n')
146 		printf("\\\n");
147 }
148