vis.c revision 1.1.1.2       1      1.1  cgd /*-
      2  1.1.1.2  jtc  * Copyright (c) 1989, 1993
      3  1.1.1.2  jtc  *	The Regents of the University of California.  All rights reserved.
      4      1.1  cgd  *
      5      1.1  cgd  * Redistribution and use in source and binary forms, with or without
      6      1.1  cgd  * modification, are permitted provided that the following conditions
      7      1.1  cgd  * are met:
      8      1.1  cgd  * 1. Redistributions of source code must retain the above copyright
      9      1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     10      1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     11      1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     12      1.1  cgd  *    documentation and/or other materials provided with the distribution.
     13      1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     14      1.1  cgd  *    must display the following acknowledgement:
     15      1.1  cgd  *	This product includes software developed by the University of
     16      1.1  cgd  *	California, Berkeley and its contributors.
     17      1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     18      1.1  cgd  *    may be used to endorse or promote products derived from this software
     19      1.1  cgd  *    without specific prior written permission.
     20      1.1  cgd  *
     21      1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     22      1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     23      1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     24      1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     25      1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     26      1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     27      1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     28      1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     29      1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     30      1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     31      1.1  cgd  * SUCH DAMAGE.
     32      1.1  cgd  */
     33      1.1  cgd 
     34      1.1  cgd #ifndef lint
     35  1.1.1.2  jtc static char copyright[] =
     36  1.1.1.2  jtc "@(#) Copyright (c) 1989, 1993\n\
     37  1.1.1.2  jtc 	The Regents of the University of California.  All rights reserved.\n";
     38      1.1  cgd #endif /* not lint */
     39      1.1  cgd 
     40      1.1  cgd #ifndef lint
     41  1.1.1.2  jtc static char sccsid[] = "@(#)vis.c	8.1 (Berkeley) 6/6/93";
     42      1.1  cgd #endif /* not lint */
     43      1.1  cgd 
     44      1.1  cgd #include <stdio.h>
     45      1.1  cgd #include <vis.h>
     46      1.1  cgd 
     47      1.1  cgd int eflags, fold, foldwidth=80, none, markeol, debug;
     48      1.1  cgd 
     49      1.1  cgd main(argc, argv)
     50      1.1  cgd 	char *argv[];
     51      1.1  cgd {
     52      1.1  cgd 	extern char *optarg;
     53      1.1  cgd 	extern int optind;
     54      1.1  cgd 	extern int errno;
     55      1.1  cgd 	FILE *fp;
     56      1.1  cgd 	int ch;
     57      1.1  cgd 
     58      1.1  cgd 	while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != EOF)
     59      1.1  cgd 		switch((char)ch) {
     60      1.1  cgd 		case 'n':
     61      1.1  cgd 			none++;
     62      1.1  cgd 			break;
     63      1.1  cgd 		case 'w':
     64      1.1  cgd 			eflags |= VIS_WHITE;
     65      1.1  cgd 			break;
     66      1.1  cgd 		case 'c':
     67      1.1  cgd 			eflags |= VIS_CSTYLE;
     68      1.1  cgd 			break;
     69      1.1  cgd 		case 't':
     70      1.1  cgd 			eflags |= VIS_TAB;
     71      1.1  cgd 			break;
     72      1.1  cgd 		case 's':
     73      1.1  cgd 			eflags |= VIS_SAFE;
     74      1.1  cgd 			break;
     75      1.1  cgd 		case 'o':
     76      1.1  cgd 			eflags |= VIS_OCTAL;
     77      1.1  cgd 			break;
     78      1.1  cgd 		case 'b':
     79      1.1  cgd 			eflags |= VIS_NOSLASH;
     80      1.1  cgd 			break;
     81      1.1  cgd 		case 'F':
     82      1.1  cgd 			if ((foldwidth = atoi(optarg))<5) {
     83      1.1  cgd 				fprintf(stderr,
     84      1.1  cgd 				 "vis: can't fold lines to less than 5 cols\n");
     85      1.1  cgd 				exit(1);
     86      1.1  cgd 			}
     87      1.1  cgd 			/*FALLTHROUGH*/
     88      1.1  cgd 		case 'f':
     89      1.1  cgd 			fold++;		/* fold output lines to 80 cols */
     90      1.1  cgd 			break;		/* using hidden newline */
     91      1.1  cgd 		case 'l':
     92      1.1  cgd 			markeol++;	/* mark end of line with \$ */
     93      1.1  cgd 			break;
     94      1.1  cgd #ifdef DEBUG
     95      1.1  cgd 		case 'd':
     96      1.1  cgd 			debug++;
     97      1.1  cgd 			break;
     98      1.1  cgd #endif
     99      1.1  cgd 		case '?':
    100      1.1  cgd 		default:
    101      1.1  cgd 			fprintf(stderr,
    102      1.1  cgd 		"usage: vis [-nwctsobf] [-F foldwidth]\n");
    103      1.1  cgd 			exit(1);
    104      1.1  cgd 		}
    105      1.1  cgd 	argc -= optind;
    106      1.1  cgd 	argv += optind;
    107      1.1  cgd 
    108      1.1  cgd 	if (*argv)
    109      1.1  cgd 		while (*argv) {
    110      1.1  cgd 			if ((fp=fopen(*argv, "r")) != NULL)
    111      1.1  cgd 				process(fp, *argv);
    112      1.1  cgd 			else
    113      1.1  cgd 				fprintf(stderr, "vis: %s: %s\n", *argv,
    114      1.1  cgd 				    (char *)strerror(errno));
    115      1.1  cgd 			argv++;
    116      1.1  cgd 		}
    117      1.1  cgd 	else
    118      1.1  cgd 		process(stdin, "<stdin>");
    119      1.1  cgd 	exit(0);
    120      1.1  cgd }
    121      1.1  cgd 
    122      1.1  cgd process(fp, filename)
    123      1.1  cgd 	FILE *fp;
    124      1.1  cgd 	char *filename;
    125      1.1  cgd {
    126      1.1  cgd 	static int col = 0;
    127      1.1  cgd 	register char *cp = "\0"+1;	/* so *(cp-1) starts out != '\n' */
    128      1.1  cgd 	register int c, rachar;
    129      1.1  cgd 	register char nc;
    130      1.1  cgd 	char buff[5];
    131      1.1  cgd 
    132      1.1  cgd 	c = getc(fp);
    133      1.1  cgd 	while (c != EOF) {
    134      1.1  cgd 		rachar = getc(fp);
    135      1.1  cgd 		if (none) {
    136      1.1  cgd 			cp = buff;
    137      1.1  cgd 			*cp++ = c;
    138      1.1  cgd 			if (c == '\\')
    139      1.1  cgd 				*cp++ = '\\';
    140      1.1  cgd 			*cp = '\0';
    141      1.1  cgd 		} else if (markeol && c == '\n') {
    142      1.1  cgd 			cp = buff;
    143      1.1  cgd 			if ((eflags & VIS_NOSLASH) == 0)
    144      1.1  cgd 				*cp++ = '\\';
    145      1.1  cgd 			*cp++ = '$';
    146      1.1  cgd 			*cp++ = '\n';
    147      1.1  cgd 			*cp = '\0';
    148      1.1  cgd 		} else
    149      1.1  cgd 			(void) vis(buff, (char)c, eflags, (char)rachar);
    150      1.1  cgd 
    151      1.1  cgd 		cp = buff;
    152      1.1  cgd 		if (fold) {
    153      1.1  cgd #ifdef DEBUG
    154      1.1  cgd 			if (debug)
    155      1.1  cgd 				printf("<%02d,", col);
    156      1.1  cgd #endif
    157      1.1  cgd 			col = foldit(cp, col, foldwidth);
    158      1.1  cgd #ifdef DEBUG
    159      1.1  cgd 			if (debug)
    160      1.1  cgd 				printf("%02d>", col);
    161      1.1  cgd #endif
    162      1.1  cgd 		}
    163      1.1  cgd 		do {
    164      1.1  cgd 			putchar(*cp);
    165      1.1  cgd 		} while (*++cp);
    166      1.1  cgd 		c = rachar;
    167      1.1  cgd 	}
    168      1.1  cgd 	/*
    169      1.1  cgd 	 * terminate partial line with a hidden newline
    170      1.1  cgd 	 */
    171      1.1  cgd 	if (fold && *(cp-1) != '\n')
    172      1.1  cgd 		printf("\\\n");
    173      1.1  cgd }
    174