1 /*- 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 3. All advertising materials mentioning features or use of this software 14 * must display the following acknowledgement: 15 * This product includes software developed by the University of 16 * California, Berkeley and its contributors. 17 * 4. Neither the name of the University nor the names of its contributors 18 * may be used to endorse or promote products derived from this software 19 * without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 31 * SUCH DAMAGE. 32 */ 33 34 #ifndef lint 35 static const char copyright[] = 36 "@(#) Copyright (c) 1989, 1993\n\ 37 The Regents of the University of California. All rights reserved.\n"; 38 #endif /* not lint */ 39 40 #ifndef lint 41 #if 0 42 static char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 6/6/93"; 43 #endif 44 static const char rcsid[] = 45 "$FreeBSD: src/usr.bin/vis/vis.c,v 1.6 1999/08/28 01:07:25 peter Exp $"; 46 #endif /* not lint */ 47 48 #include <err.h> 49 #include <locale.h> 50 #include <stdio.h> 51 #include <stdlib.h> 52 #include <unistd.h> 53 #include <vis.h> 54 55 int eflags, fold, foldwidth=80, none, markeol, debug; 56 57 void process __P((FILE *, char *filename)); 58 static void usage __P((void)); 59 extern int foldit __P((char *, int, int)); 60 61 int 62 main(argc, argv) 63 char *argv[]; 64 { 65 FILE *fp; 66 int ch; 67 68 (void) setlocale(LC_CTYPE, ""); 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 /*FALLTHROUGH*/ 97 case 'f': 98 fold++; /* fold output lines to 80 cols */ 99 break; /* using hidden newline */ 100 case 'l': 101 markeol++; /* mark end of line with \$ */ 102 break; 103 #ifdef DEBUG 104 case 'd': 105 debug++; 106 break; 107 #endif 108 case '?': 109 default: 110 usage(); 111 } 112 argc -= optind; 113 argv += optind; 114 115 if (*argv) 116 while (*argv) { 117 if ((fp=fopen(*argv, "r")) != NULL) 118 process(fp, *argv); 119 else 120 warn("%s", *argv); 121 argv++; 122 } 123 else 124 process(stdin, "<stdin>"); 125 exit(0); 126 } 127 128 129 static void 130 usage() 131 { 132 #ifdef DEBUG 133 fprintf(stderr, "usage: vis [-cbflnostwd] [-F foldwidth] [file ...]\n"); 134 #else 135 fprintf(stderr, "usage: vis [-cbflnostw] [-F foldwidth] [file ...]\n"); 136 #endif 137 exit(1); 138 } 139 140 void 141 process(fp, filename) 142 FILE *fp; 143 char *filename; 144 { 145 static int col = 0; 146 register char *cp = "\0"+1; /* so *(cp-1) starts out != '\n' */ 147 register int c, rachar; 148 char buff[5]; 149 150 c = getc(fp); 151 while (c != EOF) { 152 rachar = getc(fp); 153 if (none) { 154 cp = buff; 155 *cp++ = c; 156 if (c == '\\') 157 *cp++ = '\\'; 158 *cp = '\0'; 159 } else if (markeol && c == '\n') { 160 cp = buff; 161 if ((eflags & VIS_NOSLASH) == 0) 162 *cp++ = '\\'; 163 *cp++ = '$'; 164 *cp++ = '\n'; 165 *cp = '\0'; 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