Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / usr.bin / vis / vis.c
blob47f7d356d4669b786248108a194213da313505e2
1 /* $NetBSD: vis.c,v 1.14 2009/02/11 03:04:52 christos Exp $ */
3 /*-
4 * Copyright (c) 1989, 1993
5 * The Regents of the University of California. All rights reserved.
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. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 #ifndef lint
34 __COPYRIGHT("@(#) Copyright (c) 1989, 1993\
35 The Regents of the University of California. All rights reserved.");
36 #endif /* not lint */
38 #ifndef lint
39 #if 0
40 static char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 6/6/93";
41 #endif
42 __RCSID("$NetBSD: vis.c,v 1.14 2009/02/11 03:04:52 christos Exp $");
43 #endif /* not lint */
45 #include <stdio.h>
46 #include <string.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <err.h>
50 #include <vis.h>
52 #include "extern.h"
54 static int eflags, fold, foldwidth = 80, none, markeol;
55 #ifdef DEBUG
56 int debug;
57 #endif
58 static char *extra;
60 static void process(FILE *);
62 int
63 main(int argc, char *argv[])
65 FILE *fp;
66 int ch;
67 int rval;
69 while ((ch = getopt(argc, argv, "bcde:F:fhlmnostw")) != -1)
70 switch((char)ch) {
71 case 'b':
72 eflags |= VIS_NOSLASH;
73 break;
74 case 'c':
75 eflags |= VIS_CSTYLE;
76 break;
77 #ifdef DEBUG
78 case 'd':
79 debug++;
80 break;
81 #endif
82 case 'e':
83 extra = optarg;
84 break;
85 case 'F':
86 if ((foldwidth = atoi(optarg)) < 5) {
87 errx(1, "can't fold lines to less than 5 cols");
88 /* NOTREACHED */
90 markeol++;
91 break;
92 case 'f':
93 fold++; /* fold output lines to 80 cols */
94 break; /* using hidden newline */
95 case 'h':
96 eflags |= VIS_HTTPSTYLE;
97 break;
98 case 'l':
99 markeol++; /* mark end of line with \$ */
100 break;
101 case 'm':
102 eflags |= VIS_MIMESTYLE;
103 if (foldwidth == 80)
104 foldwidth = 76;
105 break;
106 case 'n':
107 none++;
108 break;
109 case 'o':
110 eflags |= VIS_OCTAL;
111 break;
112 case 's':
113 eflags |= VIS_SAFE;
114 break;
115 case 't':
116 eflags |= VIS_TAB;
117 break;
118 case 'w':
119 eflags |= VIS_WHITE;
120 break;
121 case '?':
122 default:
123 (void)fprintf(stderr,
124 "Usage: %s [-bcfhlmnostw] [-e extra]"
125 " [-F foldwidth] [file ...]\n", getprogname());
126 return 1;
129 if ((eflags & (VIS_HTTPSTYLE|VIS_MIMESTYLE)) ==
130 (VIS_HTTPSTYLE|VIS_MIMESTYLE))
131 errx(1, "Can't specify -m and -h at the same time");
133 argc -= optind;
134 argv += optind;
136 rval = 0;
138 if (*argv)
139 while (*argv) {
140 if ((fp = fopen(*argv, "r")) != NULL) {
141 process(fp);
142 (void)fclose(fp);
143 } else {
144 warn("%s", *argv);
145 rval = 1;
147 argv++;
149 else
150 process(stdin);
151 return rval;
154 static void
155 process(FILE *fp)
157 static int col = 0;
158 static char nul[] = "\0";
159 char *cp = nul + 1; /* so *(cp-1) starts out != '\n' */
160 int c, rachar;
161 char buff[5];
163 c = getc(fp);
164 while (c != EOF) {
165 rachar = getc(fp);
166 if (none) {
167 cp = buff;
168 *cp++ = c;
169 if (c == '\\')
170 *cp++ = '\\';
171 *cp = '\0';
172 } else if (markeol && c == '\n') {
173 cp = buff;
174 if ((eflags & VIS_NOSLASH) == 0)
175 *cp++ = '\\';
176 *cp++ = '$';
177 *cp++ = '\n';
178 *cp = '\0';
179 } else if (extra)
180 (void)svis(buff, (char)c, eflags, (char)rachar, extra);
181 else
182 (void)vis(buff, (char)c, eflags, (char)rachar);
184 cp = buff;
185 if (fold) {
186 #ifdef DEBUG
187 if (debug)
188 (void)printf("<%02d,", col);
189 #endif
190 col = foldit(cp, col, foldwidth, eflags);
191 #ifdef DEBUG
192 if (debug)
193 (void)printf("%02d>", col);
194 #endif
196 do {
197 (void)putchar(*cp);
198 } while (*++cp);
199 c = rachar;
202 * terminate partial line with a hidden newline
204 if (fold && *(cp - 1) != '\n')
205 (void)printf(eflags & VIS_MIMESTYLE ? "=\n" : "\\\n");