Annotation of src/usr.bin/vis/vis.c, Revision 1.8
1.8 ! millert 1: /* $OpenBSD: vis.c,v 1.7 2003/06/10 22:20:54 deraadt 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
1.8 ! millert 34: static const char copyright[] =
1.1 deraadt 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
1.8 ! millert 41: static const char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 6/6/93";
1.1 deraadt 42: #endif
1.8 ! millert 43: static const char rcsid[] = "$OpenBSD: vis.c,v 1.7 2003/06/10 22:20:54 deraadt 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.8 ! millert 57: __dead void usage(void);
1.1 deraadt 58:
59: int
1.7 deraadt 60: main(int argc, char *argv[])
1.1 deraadt 61: {
62: FILE *fp;
63: int ch;
64:
1.3 millert 65: while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != -1)
1.1 deraadt 66: switch((char)ch) {
67: case 'n':
68: none++;
69: break;
70: case 'w':
71: eflags |= VIS_WHITE;
72: break;
73: case 'c':
74: eflags |= VIS_CSTYLE;
75: break;
76: case 't':
77: eflags |= VIS_TAB;
78: break;
79: case 's':
80: eflags |= VIS_SAFE;
81: break;
82: case 'o':
83: eflags |= VIS_OCTAL;
84: break;
85: case 'b':
86: eflags |= VIS_NOSLASH;
87: break;
88: case 'F':
89: if ((foldwidth = atoi(optarg))<5) {
90: errx(1, "can't fold lines to less than 5 cols");
91: /* NOTREACHED */
92: }
93: /*FALLTHROUGH*/
94: case 'f':
95: fold++; /* fold output lines to 80 cols */
96: break; /* using hidden newline */
97: case 'l':
98: markeol++; /* mark end of line with \$ */
99: break;
100: #ifdef DEBUG
101: case 'd':
102: debug++;
103: break;
104: #endif
105: case '?':
106: default:
1.8 ! millert 107: usage();
1.1 deraadt 108: }
109: argc -= optind;
110: argv += optind;
111:
112: if (*argv)
113: while (*argv) {
114: if ((fp=fopen(*argv, "r")) != NULL)
115: process(fp, *argv);
116: else
117: warn("%s", *argv);
118: argv++;
119: }
120: else
121: process(stdin, "<stdin>");
122: exit(0);
123: }
124:
125: void
1.7 deraadt 126: process(FILE *fp, char *filename)
1.1 deraadt 127: {
128: static int col = 0;
1.4 mpech 129: char *cp = "\0"+1; /* so *(cp-1) starts out != '\n' */
130: int c, rachar;
1.1 deraadt 131: char buff[5];
132:
133: c = getc(fp);
134: while (c != EOF) {
135: rachar = getc(fp);
136: if (none) {
137: cp = buff;
138: *cp++ = c;
139: if (c == '\\')
140: *cp++ = '\\';
141: *cp = '\0';
142: } else if (markeol && c == '\n') {
143: cp = buff;
144: if ((eflags & VIS_NOSLASH) == 0)
145: *cp++ = '\\';
146: *cp++ = '$';
147: *cp++ = '\n';
148: *cp = '\0';
149: } else
150: (void) vis(buff, (char)c, eflags, (char)rachar);
151:
152: cp = buff;
153: if (fold) {
154: #ifdef DEBUG
155: if (debug)
156: printf("<%02d,", col);
157: #endif
158: col = foldit(cp, col, foldwidth);
159: #ifdef DEBUG
160: if (debug)
161: printf("%02d>", col);
162: #endif
163: }
164: do {
165: putchar(*cp);
166: } while (*++cp);
167: c = rachar;
168: }
169: /*
170: * terminate partial line with a hidden newline
171: */
172: if (fold && *(cp-1) != '\n')
173: printf("\\\n");
1.8 ! millert 174: }
! 175:
! 176: __dead void
! 177: usage(void)
! 178: {
! 179: extern char *__progname;
! 180:
! 181: fprintf(stderr, "usage: %s [-cbflnostw] [-F [foldwidth]] [file ...]\n",
! 182: __progname);
! 183: exit(1);
1.1 deraadt 184: }