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