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