1 /* $NetBSD: vis.c,v 1.6 2000/07/05 00:35:28 itohy 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 #include <sys/cdefs.h> 37 #ifndef lint 38 __COPYRIGHT("@(#) 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 __RCSID("$NetBSD: vis.c,v 1.6 2000/07/05 00:35:28 itohy 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 int main __P((int, char **)); 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 int rval; 70 71 while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != -1) 72 switch((char)ch) { 73 case 'n': 74 none++; 75 break; 76 case 'w': 77 eflags |= VIS_WHITE; 78 break; 79 case 'c': 80 eflags |= VIS_CSTYLE; 81 break; 82 case 't': 83 eflags |= VIS_TAB; 84 break; 85 case 's': 86 eflags |= VIS_SAFE; 87 break; 88 case 'o': 89 eflags |= VIS_OCTAL; 90 break; 91 case 'b': 92 eflags |= VIS_NOSLASH; 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] [-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 165 (void) vis(buff, (char)c, eflags, (char)rachar); 166 167 cp = buff; 168 if (fold) { 169 #ifdef DEBUG 170 if (debug) 171 printf("<%02d,", col); 172 #endif 173 col = foldit(cp, col, foldwidth); 174 #ifdef DEBUG 175 if (debug) 176 printf("%02d>", col); 177 #endif 178 } 179 do { 180 putchar(*cp); 181 } while (*++cp); 182 c = rachar; 183 } 184 /* 185 * terminate partial line with a hidden newline 186 */ 187 if (fold && *(cp-1) != '\n') 188 printf("\\\n"); 189 } 190