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