xref: /csrg-svn/usr.bin/vis/vis.c (revision 44588)
141959Smarc /*
241959Smarc  * Copyright (c) 1989 The Regents of the University of California.
341959Smarc  * All rights reserved.
441959Smarc  *
541959Smarc  * Redistribution and use in source and binary forms are permitted
641959Smarc  * provided that the above copyright notice and this paragraph are
741959Smarc  * duplicated in all such forms and that any documentation,
841959Smarc  * advertising materials, and other materials related to such
941959Smarc  * distribution and use acknowledge that the software was developed
1041959Smarc  * by the University of California, Berkeley.  The name of the
1141959Smarc  * University may not be used to endorse or promote products derived
1241959Smarc  * from this software without specific prior written permission.
1341959Smarc  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1441959Smarc  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1541959Smarc  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1641959Smarc  */
1741959Smarc 
1841959Smarc #ifndef lint
1941959Smarc char copyright[] =
2041959Smarc "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
2141959Smarc  All rights reserved.\n";
2241959Smarc #endif /* not lint */
2341959Smarc 
2441959Smarc #ifndef lint
25*44588Smarc static char sccsid[] = "@(#)vis.c	1.2 (Berkeley) 06/29/90";
2641959Smarc #endif /* not lint */
2741959Smarc 
2841959Smarc #include <stdio.h>
29*44588Smarc #include <vis.h>
3041959Smarc 
31*44588Smarc int eflags, fold, foldwidth=80, none, markeol, debug;
3241959Smarc 
3341959Smarc main(argc, argv)
3441959Smarc 	char *argv[];
3541959Smarc {
3641959Smarc 	extern char *optarg;
3741959Smarc 	extern int optind;
3841959Smarc 	extern int errno;
3941959Smarc 	FILE *fp;
4041959Smarc 	int ch;
4141959Smarc 
42*44588Smarc 	while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != EOF)
4341959Smarc 		switch((char)ch) {
4441959Smarc 		case 'n':
4541959Smarc 			none++;
4641959Smarc 			break;
4741959Smarc 		case 'w':
48*44588Smarc 			eflags |= VIS_WHITE;
4941959Smarc 			break;
5041959Smarc 		case 'c':
51*44588Smarc 			eflags |= VIS_CSTYLE;
5241959Smarc 			break;
53*44588Smarc 		case 't':
54*44588Smarc 			eflags |= VIS_TAB;
5541959Smarc 			break;
56*44588Smarc 		case 's':
57*44588Smarc 			eflags |= VIS_SAFE;
58*44588Smarc 			break;
5941959Smarc 		case 'o':
60*44588Smarc 			eflags |= VIS_OCTAL;
6141959Smarc 			break;
62*44588Smarc 		case 'b':
63*44588Smarc 			eflags |= VIS_NOSLASH;
6441959Smarc 			break;
6541959Smarc 		case 'F':
6641959Smarc 			if ((foldwidth = atoi(optarg))<5) {
6741959Smarc 				fprintf(stderr,
68*44588Smarc 				 "vis: can't fold lines to less than 5 cols\n");
6941959Smarc 				exit(1);
7041959Smarc 			}
7141959Smarc 			/*FALLTHROUGH*/
7241959Smarc 		case 'f':
7341959Smarc 			fold++;		/* fold output lines to 80 cols */
7441959Smarc 			break;		/* using hidden newline */
75*44588Smarc 		case 'l':
76*44588Smarc 			markeol++;	/* mark end of line with \$ */
7741959Smarc 			break;
78*44588Smarc #ifdef DEBUG
79*44588Smarc 		case 'd':
80*44588Smarc 			debug++;
8141959Smarc 			break;
82*44588Smarc #endif
8341959Smarc 		case '?':
8441959Smarc 		default:
8541959Smarc 			fprintf(stderr,
86*44588Smarc 		"usage: vis [-nwctsobf] [-F foldwidth]\n");
8741959Smarc 			exit(1);
8841959Smarc 		}
8941959Smarc 	argc -= optind;
9041959Smarc 	argv += optind;
9141959Smarc 
9241959Smarc 	if (*argv)
9341959Smarc 		while (*argv) {
9441959Smarc 			if ((fp=fopen(*argv, "r")) != NULL)
9541959Smarc 				process(fp, *argv);
9641959Smarc 			else
9741959Smarc 				fprintf(stderr, "vis: %s: %s\n", *argv,
9841959Smarc 				    (char *)strerror(errno));
9941959Smarc 			argv++;
10041959Smarc 		}
10141959Smarc 	else
10241959Smarc 		process(stdin, "<stdin>");
10341959Smarc 	exit(0);
10441959Smarc }
10541959Smarc 
10641959Smarc process(fp, filename)
10741959Smarc 	FILE *fp;
10841959Smarc 	char *filename;
10941959Smarc {
11041959Smarc 	static int col = 0;
111*44588Smarc 	register char *cp = "\0"+1;	/* so *(cp-1) starts out != '\n' */
11241959Smarc 	register int c, rachar;
11341959Smarc 	register char nc;
114*44588Smarc 	char buff[5];
11541959Smarc 
116*44588Smarc 	c = getc(fp);
117*44588Smarc 	while (c != EOF) {
118*44588Smarc 		rachar = getc(fp);
119*44588Smarc 		if (none) {
120*44588Smarc 			cp = buff;
121*44588Smarc 			*cp++ = c;
122*44588Smarc 			if (c == '\\')
123*44588Smarc 				*cp++ = '\\';
124*44588Smarc 			*cp = '\0';
125*44588Smarc 		} else if (markeol && c == '\n') {
126*44588Smarc 			cp = buff;
127*44588Smarc 			if ((eflags & VIS_NOSLASH) == 0)
128*44588Smarc 				*cp++ = '\\';
129*44588Smarc 			*cp++ = '$';
130*44588Smarc 			*cp++ = '\n';
131*44588Smarc 			*cp = '\0';
132*44588Smarc 		} else
133*44588Smarc 			(void) vis(buff, (char)c, eflags, (char)rachar);
134*44588Smarc 
135*44588Smarc 		cp = buff;
136*44588Smarc 		if (fold) {
137*44588Smarc #ifdef DEBUG
138*44588Smarc 			if (debug)
139*44588Smarc 				printf("<%02d,", col);
140*44588Smarc #endif
141*44588Smarc 			col = foldit(cp, col, foldwidth);
142*44588Smarc #ifdef DEBUG
143*44588Smarc 			if (debug)
144*44588Smarc 				printf("%02d>", col);
145*44588Smarc #endif
14641959Smarc 		}
147*44588Smarc 		do {
148*44588Smarc 			putchar(*cp);
149*44588Smarc 		} while (*++cp);
150*44588Smarc 		c = rachar;
151*44588Smarc 	}
15241959Smarc 	/*
153*44588Smarc 	 * terminate partial line with a hidden newline
15441959Smarc 	 */
155*44588Smarc 	if (fold && *(cp-1) != '\n')
156*44588Smarc 		printf("\\\n");
15741959Smarc }
158