]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - usr.bin/vis/vis.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / usr.bin / vis / vis.c
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  * 4. Neither the name of the University nor the names of its contributors
14  *    may be used to endorse or promote products derived from this software
15  *    without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29
30 #include <sys/cdefs.h>
31
32 __FBSDID("$FreeBSD$");
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
39
40 #ifndef lint
41 static const char sccsid[] = "@(#)vis.c 8.1 (Berkeley) 6/6/93";
42 #endif
43
44 #include <err.h>
45 #include <locale.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <unistd.h>
49 #include <vis.h>
50
51 #include "extern.h"
52
53 int eflags, fold, foldwidth=80, none, markeol, debug;
54
55 void process(FILE *);
56 static void usage(void);
57
58 int
59 main(int argc, char *argv[])
60 {
61         FILE *fp;
62         int ch;
63
64         (void) setlocale(LC_CTYPE, "");
65
66         while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != -1)
67                 switch((char)ch) {
68                 case 'n':
69                         none++;
70                         break;
71                 case 'w':
72                         eflags |= VIS_WHITE;
73                         break;
74                 case 'c':
75                         eflags |= VIS_CSTYLE;
76                         break;
77                 case 't':
78                         eflags |= VIS_TAB;
79                         break;
80                 case 's':
81                         eflags |= VIS_SAFE;
82                         break;
83                 case 'o':
84                         eflags |= VIS_OCTAL;
85                         break;
86                 case 'b':
87                         eflags |= VIS_NOSLASH;
88                         break;
89                 case 'F':
90                         if ((foldwidth = atoi(optarg))<5)
91                                 errx(1, "can't fold lines to less than 5 cols");
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                         usage();
107                 }
108         argc -= optind;
109         argv += optind;
110
111         if (*argv)
112                 while (*argv) {
113                         if ((fp=fopen(*argv, "r")) != NULL)
114                                 process(fp);
115                         else
116                                 warn("%s", *argv);
117                         argv++;
118                 }
119         else
120                 process(stdin);
121         exit(0);
122 }
123
124
125 static void
126 usage(void)
127 {
128 #ifdef DEBUG
129         fprintf(stderr, "usage: vis [-cbflnostwd] [-F foldwidth] [file ...]\n");
130 #else
131         fprintf(stderr, "usage: vis [-cbflnostw] [-F foldwidth] [file ...]\n");
132 #endif
133         exit(1);
134 }
135
136 void
137 process(FILE *fp)
138 {
139         static int col = 0;
140         static char dummy[] = "\0";
141         char *cp = dummy+1; /* so *(cp-1) starts out != '\n' */
142         int c, rachar;
143         char buff[5];
144
145         c = getc(fp);
146         while (c != EOF) {
147                 rachar = getc(fp);
148                 if (none) {
149                         cp = buff;
150                         *cp++ = c;
151                         if (c == '\\')
152                                 *cp++ = '\\';
153                         *cp = '\0';
154                 } else if (markeol && c == '\n') {
155                         cp = buff;
156                         if ((eflags & VIS_NOSLASH) == 0)
157                                 *cp++ = '\\';
158                         *cp++ = '$';
159                         *cp++ = '\n';
160                         *cp = '\0';
161                 } else
162                         (void) vis(buff, (char)c, eflags, (char)rachar);
163
164                 cp = buff;
165                 if (fold) {
166 #ifdef DEBUG
167                         if (debug)
168                                 printf("<%02d,", col);
169 #endif
170                         col = foldit(cp, col, foldwidth);
171 #ifdef DEBUG
172                         if (debug)
173                                 printf("%02d>", col);
174 #endif
175                 }
176                 do {
177                         putchar(*cp);
178                 } while (*++cp);
179                 c = rachar;
180         }
181         /*
182          * terminate partial line with a hidden newline
183          */
184         if (fold && *(cp-1) != '\n')
185                 printf("\\\n");
186 }