56ce9599666fdfd5ca353c514aef0ec7c5957380
[dragonfly.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  * @(#) Copyright (c) 1989, 1993 The Regents of the University of California.  All rights reserved.
34  * @(#)vis.c    8.1 (Berkeley) 6/6/93
35  * $FreeBSD: src/usr.bin/vis/vis.c,v 1.6 1999/08/28 01:07:25 peter Exp $
36  * $DragonFly: src/usr.bin/vis/vis.c,v 1.3 2003/10/04 20:36:54 hmp Exp $
37  */
38
39 #include <err.h>
40 #include <locale.h>
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <vis.h>
45
46 int eflags, fold, foldwidth=80, none, markeol, debug;
47
48 void process(FILE *, char *filename);
49 static void usage(void);
50 extern int foldit(char *, int, int);
51
52 int
53 main(int argc, char **argv)
54 {
55         FILE *fp;
56         int ch;
57
58         (void) setlocale(LC_CTYPE, "");
59
60         while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != -1)
61                 switch((char)ch) {
62                 case 'n':
63                         none++;
64                         break;
65                 case 'w':
66                         eflags |= VIS_WHITE;
67                         break;
68                 case 'c':
69                         eflags |= VIS_CSTYLE;
70                         break;
71                 case 't':
72                         eflags |= VIS_TAB;
73                         break;
74                 case 's':
75                         eflags |= VIS_SAFE;
76                         break;
77                 case 'o':
78                         eflags |= VIS_OCTAL;
79                         break;
80                 case 'b':
81                         eflags |= VIS_NOSLASH;
82                         break;
83                 case 'F':
84                         if ((foldwidth = atoi(optarg))<5)
85                                 errx(1, "can't fold lines to less than 5 cols");
86                         /*FALLTHROUGH*/
87                 case 'f':
88                         fold++;         /* fold output lines to 80 cols */
89                         break;          /* using hidden newline */
90                 case 'l':
91                         markeol++;      /* mark end of line with \$ */
92                         break;
93 #ifdef DEBUG
94                 case 'd':
95                         debug++;
96                         break;
97 #endif
98                 case '?':
99                 default:
100                         usage();
101                 }
102         argc -= optind;
103         argv += optind;
104
105         if (*argv)
106                 while (*argv) {
107                         if ((fp=fopen(*argv, "r")) != NULL)
108                                 process(fp, *argv);
109                         else
110                                 warn("%s", *argv);
111                         argv++;
112                 }
113         else
114                 process(stdin, "<stdin>");
115         exit(0);
116 }
117
118
119 static void
120 usage(void)
121 {
122 #ifdef DEBUG
123         fprintf(stderr, "usage: vis [-cbflnostwd] [-F foldwidth] [file ...]\n");
124 #else
125         fprintf(stderr, "usage: vis [-cbflnostw] [-F foldwidth] [file ...]\n");
126 #endif
127         exit(1);
128 }
129
130 void
131 process(FILE *fp, char *filename)
132 {
133         static int col = 0;
134         register char *cp = "\0"+1;     /* so *(cp-1) starts out != '\n' */
135         register int c, rachar;
136         char buff[5];
137
138         c = getc(fp);
139         while (c != EOF) {
140                 rachar = getc(fp);
141                 if (none) {
142                         cp = buff;
143                         *cp++ = c;
144                         if (c == '\\')
145                                 *cp++ = '\\';
146                         *cp = '\0';
147                 } else if (markeol && c == '\n') {
148                         cp = buff;
149                         if ((eflags & VIS_NOSLASH) == 0)
150                                 *cp++ = '\\';
151                         *cp++ = '$';
152                         *cp++ = '\n';
153                         *cp = '\0';
154                 } else
155                         (void) vis(buff, (char)c, eflags, (char)rachar);
156
157                 cp = buff;
158                 if (fold) {
159 #ifdef DEBUG
160                         if (debug)
161                                 printf("<%02d,", col);
162 #endif
163                         col = foldit(cp, col, foldwidth);
164 #ifdef DEBUG
165                         if (debug)
166                                 printf("%02d>", col);
167 #endif
168                 }
169                 do {
170                         putchar(*cp);
171                 } while (*++cp);
172                 c = rachar;
173         }
174         /*
175          * terminate partial line with a hidden newline
176          */
177         if (fold && *(cp-1) != '\n')
178                 printf("\\\n");
179 }