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