1 /* $NetBSD: vis.c,v 1.9 2004/01/05 23:23:37 jmmv 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\n\ 35 The Regents of the University of California. All rights reserved.\n"); 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.9 2004/01/05 23:23:37 jmmv 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, "nwctsobe:fF:ld")) != -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 'b': 89 eflags |= VIS_NOSLASH; 90 break; 91 case 'e': 92 extra = optarg; 93 break; 94 case 'F': 95 if ((foldwidth = atoi(optarg))<5) { 96 errx(1, "can't fold lines to less than 5 cols"); 97 /* NOTREACHED */ 98 } 99 /*FALLTHROUGH*/ 100 case 'f': 101 fold++; /* fold output lines to 80 cols */ 102 break; /* using hidden newline */ 103 case 'l': 104 markeol++; /* mark end of line with \$ */ 105 break; 106 #ifdef DEBUG 107 case 'd': 108 debug++; 109 break; 110 #endif 111 case '?': 112 default: 113 fprintf(stderr, 114 "usage: vis [-nwctsobf] [-e extra] [-F foldwidth]\n"); 115 exit(1); 116 } 117 argc -= optind; 118 argv += optind; 119 120 rval = 0; 121 122 if (*argv) 123 while (*argv) { 124 if ((fp=fopen(*argv, "r")) != NULL) { 125 process(fp, *argv); 126 fclose(fp); 127 } else { 128 warn("%s", *argv); 129 rval = 1; 130 } 131 argv++; 132 } 133 else 134 process(stdin, "<stdin>"); 135 exit(rval); 136 } 137 138 void 139 process(fp, filename) 140 FILE *fp; 141 char *filename; 142 { 143 static int col = 0; 144 char *cp = "\0"+1; /* so *(cp-1) starts out != '\n' */ 145 int c, rachar; 146 char buff[5]; 147 148 c = getc(fp); 149 while (c != EOF) { 150 rachar = getc(fp); 151 if (none) { 152 cp = buff; 153 *cp++ = c; 154 if (c == '\\') 155 *cp++ = '\\'; 156 *cp = '\0'; 157 } else if (markeol && c == '\n') { 158 cp = buff; 159 if ((eflags & VIS_NOSLASH) == 0) 160 *cp++ = '\\'; 161 *cp++ = '$'; 162 *cp++ = '\n'; 163 *cp = '\0'; 164 } else if (extra) 165 (void) svis(buff, (char)c, eflags, (char)rachar, extra); 166 else 167 (void) vis(buff, (char)c, eflags, (char)rachar); 168 169 cp = buff; 170 if (fold) { 171 #ifdef DEBUG 172 if (debug) 173 printf("<%02d,", col); 174 #endif 175 col = foldit(cp, col, foldwidth); 176 #ifdef DEBUG 177 if (debug) 178 printf("%02d>", col); 179 #endif 180 } 181 do { 182 putchar(*cp); 183 } while (*++cp); 184 c = rachar; 185 } 186 /* 187 * terminate partial line with a hidden newline 188 */ 189 if (fold && *(cp-1) != '\n') 190 printf("\\\n"); 191 } 192