Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[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.2 2003/06/17 04:29:33 dillon 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 __P((FILE *, char *filename));
49 static void usage __P((void));
50 extern int foldit __P((char *, int, int));
51
52 int
53 main(argc, argv)
54         char *argv[];
55 {
56         FILE *fp;
57         int ch;
58
59         (void) setlocale(LC_CTYPE, "");
60
61         while ((ch = getopt(argc, argv, "nwctsobfF:ld")) != -1)
62                 switch((char)ch) {
63                 case 'n':
64                         none++;
65                         break;
66                 case 'w':
67                         eflags |= VIS_WHITE;
68                         break;
69                 case 'c':
70                         eflags |= VIS_CSTYLE;
71                         break;
72                 case 't':
73                         eflags |= VIS_TAB;
74                         break;
75                 case 's':
76                         eflags |= VIS_SAFE;
77                         break;
78                 case 'o':
79                         eflags |= VIS_OCTAL;
80                         break;
81                 case 'b':
82                         eflags |= VIS_NOSLASH;
83                         break;
84                 case 'F':
85                         if ((foldwidth = atoi(optarg))<5)
86                                 errx(1, "can't fold lines to less than 5 cols");
87                         /*FALLTHROUGH*/
88                 case 'f':
89                         fold++;         /* fold output lines to 80 cols */
90                         break;          /* using hidden newline */
91                 case 'l':
92                         markeol++;      /* mark end of line with \$ */
93                         break;
94 #ifdef DEBUG
95                 case 'd':
96                         debug++;
97                         break;
98 #endif
99                 case '?':
100                 default:
101                         usage();
102                 }
103         argc -= optind;
104         argv += optind;
105
106         if (*argv)
107                 while (*argv) {
108                         if ((fp=fopen(*argv, "r")) != NULL)
109                                 process(fp, *argv);
110                         else
111                                 warn("%s", *argv);
112                         argv++;
113                 }
114         else
115                 process(stdin, "<stdin>");
116         exit(0);
117 }
118
119
120 static void
121 usage()
122 {
123 #ifdef DEBUG
124         fprintf(stderr, "usage: vis [-cbflnostwd] [-F foldwidth] [file ...]\n");
125 #else
126         fprintf(stderr, "usage: vis [-cbflnostw] [-F foldwidth] [file ...]\n");
127 #endif
128         exit(1);
129 }
130
131 void
132 process(fp, filename)
133         FILE *fp;
134         char *filename;
135 {
136         static int col = 0;
137         register char *cp = "\0"+1;     /* so *(cp-1) starts out != '\n' */
138         register int c, rachar;
139         char buff[5];
140
141         c = getc(fp);
142         while (c != EOF) {
143                 rachar = getc(fp);
144                 if (none) {
145                         cp = buff;
146                         *cp++ = c;
147                         if (c == '\\')
148                                 *cp++ = '\\';
149                         *cp = '\0';
150                 } else if (markeol && c == '\n') {
151                         cp = buff;
152                         if ((eflags & VIS_NOSLASH) == 0)
153                                 *cp++ = '\\';
154                         *cp++ = '$';
155                         *cp++ = '\n';
156                         *cp = '\0';
157                 } else
158                         (void) vis(buff, (char)c, eflags, (char)rachar);
159
160                 cp = buff;
161                 if (fold) {
162 #ifdef DEBUG
163                         if (debug)
164                                 printf("<%02d,", col);
165 #endif
166                         col = foldit(cp, col, foldwidth);
167 #ifdef DEBUG
168                         if (debug)
169                                 printf("%02d>", col);
170 #endif
171                 }
172                 do {
173                         putchar(*cp);
174                 } while (*++cp);
175                 c = rachar;
176         }
177         /*
178          * terminate partial line with a hidden newline
179          */
180         if (fold && *(cp-1) != '\n')
181                 printf("\\\n");
182 }