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