Home | History | Annotate | Line # | Download | only in vis
vis.c revision 1.14
      1 /*	$NetBSD: vis.c,v 1.14 2009/02/11 03:04:52 christos 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.14 2009/02/11 03:04:52 christos 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 #include "extern.h"
     53 
     54 static int eflags, fold, foldwidth = 80, none, markeol;
     55 #ifdef DEBUG
     56 int debug;
     57 #endif
     58 static char *extra;
     59 
     60 static void process(FILE *);
     61 
     62 int
     63 main(int argc, char *argv[])
     64 {
     65 	FILE *fp;
     66 	int ch;
     67 	int rval;
     68 
     69 	while ((ch = getopt(argc, argv, "bcde:F:fhlmnostw")) != -1)
     70 		switch((char)ch) {
     71 		case 'b':
     72 			eflags |= VIS_NOSLASH;
     73 			break;
     74 		case 'c':
     75 			eflags |= VIS_CSTYLE;
     76 			break;
     77 #ifdef DEBUG
     78 		case 'd':
     79 			debug++;
     80 			break;
     81 #endif
     82 		case 'e':
     83 			extra = optarg;
     84 			break;
     85 		case 'F':
     86 			if ((foldwidth = atoi(optarg)) < 5) {
     87 				errx(1, "can't fold lines to less than 5 cols");
     88 				/* NOTREACHED */
     89 			}
     90 			markeol++;
     91 			break;
     92 		case 'f':
     93 			fold++;		/* fold output lines to 80 cols */
     94 			break;		/* using hidden newline */
     95 		case 'h':
     96 			eflags |= VIS_HTTPSTYLE;
     97 			break;
     98 		case 'l':
     99 			markeol++;	/* mark end of line with \$ */
    100 			break;
    101 		case 'm':
    102 			eflags |= VIS_MIMESTYLE;
    103 			if (foldwidth == 80)
    104 				foldwidth = 76;
    105 			break;
    106 		case 'n':
    107 			none++;
    108 			break;
    109 		case 'o':
    110 			eflags |= VIS_OCTAL;
    111 			break;
    112 		case 's':
    113 			eflags |= VIS_SAFE;
    114 			break;
    115 		case 't':
    116 			eflags |= VIS_TAB;
    117 			break;
    118 		case 'w':
    119 			eflags |= VIS_WHITE;
    120 			break;
    121 		case '?':
    122 		default:
    123 			(void)fprintf(stderr,
    124 			    "Usage: %s [-bcfhlmmnostw] [-e extra]"
    125 			    " [-F foldwidth] [file ...]\n", getprogname());
    126 			return 1;
    127 		}
    128 
    129 	if ((eflags & (VIS_HTTPSTYLE|VIS_MIMESTYLE)) ==
    130 	    (VIS_HTTPSTYLE|VIS_MIMESTYLE))
    131 		errx(1, "Can't specify -m and -h at the same time");
    132 
    133 	argc -= optind;
    134 	argv += optind;
    135 
    136 	rval = 0;
    137 
    138 	if (*argv)
    139 		while (*argv) {
    140 			if ((fp = fopen(*argv, "r")) != NULL) {
    141 				process(fp);
    142 				(void)fclose(fp);
    143 			} else {
    144 				warn("%s", *argv);
    145 				rval = 1;
    146 			}
    147 			argv++;
    148 		}
    149 	else
    150 		process(stdin);
    151 	return rval;
    152 }
    153 
    154 static void
    155 process(FILE *fp)
    156 {
    157 	static int col = 0;
    158 	static char nul[] = "\0";
    159 	char *cp = nul + 1;	/* so *(cp-1) starts out != '\n' */
    160 	int c, rachar;
    161 	char buff[5];
    162 
    163 	c = getc(fp);
    164 	while (c != EOF) {
    165 		rachar = getc(fp);
    166 		if (none) {
    167 			cp = buff;
    168 			*cp++ = c;
    169 			if (c == '\\')
    170 				*cp++ = '\\';
    171 			*cp = '\0';
    172 		} else if (markeol && c == '\n') {
    173 			cp = buff;
    174 			if ((eflags & VIS_NOSLASH) == 0)
    175 				*cp++ = '\\';
    176 			*cp++ = '$';
    177 			*cp++ = '\n';
    178 			*cp = '\0';
    179 		} else if (extra)
    180 			(void)svis(buff, (char)c, eflags, (char)rachar, extra);
    181 		else
    182 			(void)vis(buff, (char)c, eflags, (char)rachar);
    183 
    184 		cp = buff;
    185 		if (fold) {
    186 #ifdef DEBUG
    187 			if (debug)
    188 				(void)printf("<%02d,", col);
    189 #endif
    190 			col = foldit(cp, col, foldwidth, eflags);
    191 #ifdef DEBUG
    192 			if (debug)
    193 				(void)printf("%02d>", col);
    194 #endif
    195 		}
    196 		do {
    197 			(void)putchar(*cp);
    198 		} while (*++cp);
    199 		c = rachar;
    200 	}
    201 	/*
    202 	 * terminate partial line with a hidden newline
    203 	 */
    204 	if (fold && *(cp - 1) != '\n')
    205 		(void)printf(eflags & VIS_MIMESTYLE ? "=\n" : "\\\n");
    206 }
    207