xref: /original-bsd/usr.bin/vis/vis.c (revision f72a1a16)
1 /*
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms are permitted
6  * provided that the above copyright notice and this paragraph are
7  * duplicated in all such forms and that any documentation,
8  * advertising materials, and other materials related to such
9  * distribution and use acknowledge that the software was developed
10  * by the University of California, Berkeley.  The name of the
11  * University may not be used to endorse or promote products derived
12  * from this software without specific prior written permission.
13  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16  */
17 
18 #ifndef lint
19 char copyright[] =
20 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
21  All rights reserved.\n";
22 #endif /* not lint */
23 
24 #ifndef lint
25 static char sccsid[] = "@(#)vis.c	1.2 (Berkeley) 06/29/90";
26 #endif /* not lint */
27 
28 #include <stdio.h>
29 #include <vis.h>
30 
31 int eflags, fold, foldwidth=80, none, markeol, debug;
32 
33 main(argc, argv)
34 	char *argv[];
35 {
36 	extern char *optarg;
37 	extern int optind;
38 	extern int errno;
39 	FILE *fp;
40 	int ch;
41 
42 	while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != EOF)
43 		switch((char)ch) {
44 		case 'n':
45 			none++;
46 			break;
47 		case 'w':
48 			eflags |= VIS_WHITE;
49 			break;
50 		case 'c':
51 			eflags |= VIS_CSTYLE;
52 			break;
53 		case 't':
54 			eflags |= VIS_TAB;
55 			break;
56 		case 's':
57 			eflags |= VIS_SAFE;
58 			break;
59 		case 'o':
60 			eflags |= VIS_OCTAL;
61 			break;
62 		case 'b':
63 			eflags |= VIS_NOSLASH;
64 			break;
65 		case 'F':
66 			if ((foldwidth = atoi(optarg))<5) {
67 				fprintf(stderr,
68 				 "vis: can't fold lines to less than 5 cols\n");
69 				exit(1);
70 			}
71 			/*FALLTHROUGH*/
72 		case 'f':
73 			fold++;		/* fold output lines to 80 cols */
74 			break;		/* using hidden newline */
75 		case 'l':
76 			markeol++;	/* mark end of line with \$ */
77 			break;
78 #ifdef DEBUG
79 		case 'd':
80 			debug++;
81 			break;
82 #endif
83 		case '?':
84 		default:
85 			fprintf(stderr,
86 		"usage: vis [-nwctsobf] [-F foldwidth]\n");
87 			exit(1);
88 		}
89 	argc -= optind;
90 	argv += optind;
91 
92 	if (*argv)
93 		while (*argv) {
94 			if ((fp=fopen(*argv, "r")) != NULL)
95 				process(fp, *argv);
96 			else
97 				fprintf(stderr, "vis: %s: %s\n", *argv,
98 				    (char *)strerror(errno));
99 			argv++;
100 		}
101 	else
102 		process(stdin, "<stdin>");
103 	exit(0);
104 }
105 
106 process(fp, filename)
107 	FILE *fp;
108 	char *filename;
109 {
110 	static int col = 0;
111 	register char *cp = "\0"+1;	/* so *(cp-1) starts out != '\n' */
112 	register int c, rachar;
113 	register char nc;
114 	char buff[5];
115 
116 	c = getc(fp);
117 	while (c != EOF) {
118 		rachar = getc(fp);
119 		if (none) {
120 			cp = buff;
121 			*cp++ = c;
122 			if (c == '\\')
123 				*cp++ = '\\';
124 			*cp = '\0';
125 		} else if (markeol && c == '\n') {
126 			cp = buff;
127 			if ((eflags & VIS_NOSLASH) == 0)
128 				*cp++ = '\\';
129 			*cp++ = '$';
130 			*cp++ = '\n';
131 			*cp = '\0';
132 		} else
133 			(void) vis(buff, (char)c, eflags, (char)rachar);
134 
135 		cp = buff;
136 		if (fold) {
137 #ifdef DEBUG
138 			if (debug)
139 				printf("<%02d,", col);
140 #endif
141 			col = foldit(cp, col, foldwidth);
142 #ifdef DEBUG
143 			if (debug)
144 				printf("%02d>", col);
145 #endif
146 		}
147 		do {
148 			putchar(*cp);
149 		} while (*++cp);
150 		c = rachar;
151 	}
152 	/*
153 	 * terminate partial line with a hidden newline
154 	 */
155 	if (fold && *(cp-1) != '\n')
156 		printf("\\\n");
157 }
158