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