]> CyberLeo.Net >> Repos - FreeBSD/releng/9.0.git/blob - usr.bin/unvis/unvis.c
Copy stable/9 to releng/9.0 as part of the FreeBSD 9.0-RELEASE release
[FreeBSD/releng/9.0.git] / usr.bin / unvis / unvis.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 #ifndef lint
31 static const char copyright[] =
32 "@(#) Copyright (c) 1989, 1993\n\
33         The Regents of the University of California.  All rights reserved.\n";
34 #endif /* not lint */
35
36 #ifndef lint
37 #if 0
38 static char sccsid[] = "@(#)unvis.c     8.1 (Berkeley) 6/6/93";
39 #endif
40 static const char rcsid[] =
41   "$FreeBSD$";
42 #endif /* not lint */
43
44 #include <err.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <unistd.h>
48 #include <vis.h>
49
50 void process(FILE *, const char *);
51 static void usage(void);
52
53 int
54 main(int argc, char *argv[])
55 {
56         FILE *fp;
57         int ch;
58
59         while ((ch = getopt(argc, argv, "")) != -1)
60                 switch((char)ch) {
61                 case '?':
62                 default:
63                         usage();
64                 }
65         argc -= optind;
66         argv += optind;
67
68         if (*argv)
69                 while (*argv) {
70                         if ((fp=fopen(*argv, "r")) != NULL)
71                                 process(fp, *argv);
72                         else
73                                 warn("%s", *argv);
74                         argv++;
75                 }
76         else
77                 process(stdin, "<stdin>");
78         exit(0);
79 }
80
81 static void
82 usage(void)
83 {
84         fprintf(stderr, "usage: unvis [file ...]\n");
85         exit(1);
86 }
87
88 void
89 process(FILE *fp, const char *filename)
90 {
91         int offset = 0, c, ret;
92         int state = 0;
93         char outc;
94
95         while ((c = getc(fp)) != EOF) {
96                 offset++;
97         again:
98                 switch(ret = unvis(&outc, (char)c, &state, 0)) {
99                 case UNVIS_VALID:
100                         putchar(outc);
101                         break;
102                 case UNVIS_VALIDPUSH:
103                         putchar(outc);
104                         goto again;
105                 case UNVIS_SYNBAD:
106                         warnx("%s: offset: %d: can't decode", filename, offset);
107                         state = 0;
108                         break;
109                 case 0:
110                 case UNVIS_NOCHAR:
111                         break;
112                 default:
113                         errx(1, "bad return value (%d), can't happen", ret);
114                 }
115         }
116         if (unvis(&outc, (char)0, &state, UNVIS_END) == UNVIS_VALID)
117                 putchar(outc);
118 }