xref: /netbsd-src/usr.bin/unvis/unvis.c (revision ae1bfcddc410612bc8c58b807e1830becb69a24c)
1 /*-
2  * Copyright (c) 1989 The Regents of the University of California.
3  * 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 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
37  All rights reserved.\n";
38 #endif /* not lint */
39 
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)unvis.c	5.1 (Berkeley) 6/1/90";*/
42 static char rcsid[] = "$Id: unvis.c,v 1.2 1993/08/01 18:03:50 mycroft Exp $";
43 #endif /* not lint */
44 
45 #include <stdio.h>
46 #include <vis.h>
47 
48 char	*Program;
49 #define usage()	fprintf(stderr, "usage: %s %s\n", Program, USAGE)
50 #define USAGE "[file...]"
51 
52 main(argc, argv)
53 	char *argv[];
54 {
55 	FILE *fp;
56 	extern char *optarg;
57 	extern int optind;
58 	int ch;
59 
60 	Program = argv[0];
61 	while ((ch = getopt(argc, argv, "")) != EOF)
62 		switch((char)ch) {
63 		case '?':
64 		default:
65 			usage();
66 			exit(1);
67 		}
68 	argc -= optind;
69 	argv += optind;
70 
71 	if (*argv)
72 		while (*argv) {
73 			if ((fp=fopen(*argv, "r")) != NULL)
74 				process(fp, *argv);
75 			else
76 				syserror("%s", *argv);
77 			argv++;
78 		}
79 	else
80 		process(stdin, "<stdin>");
81 	exit(0);
82 }
83 
84 process(fp, filename)
85 	FILE *fp;
86 	char *filename;
87 {
88 	register int offset = 0, c, ret;
89 	int state = 0;
90 	char outc;
91 
92 	while ((c = getc(fp)) != EOF) {
93 		offset++;
94 	again:
95 		switch(ret = unvis(&outc, (char)c, &state, 0)) {
96 		case UNVIS_VALID:
97 			putchar(outc);
98 			break;
99 		case UNVIS_VALIDPUSH:
100 			putchar(outc);
101 			goto again;
102 		case UNVIS_SYNBAD:
103 			error("%s: offset: %d: can't decode", filename, offset);
104 			state = 0;
105 			break;
106 		case 0:
107 		case UNVIS_NOCHAR:
108 			break;
109 		default:
110 			error("bad return value (%d), can't happen", ret);
111 			exit(1);
112 		}
113 	}
114 	if (unvis(&outc, (char)0, &state, UNVIS_END) == UNVIS_VALID)
115 		putchar(outc);
116 }
117 
118 #include <varargs.h>
119 
120 error(va_alist)
121 	va_dcl
122 {
123 	char *fmt;
124 	va_list ap;
125 	extern errno;
126 
127 	fprintf(stderr, "%s: ", Program);
128 	va_start(ap);
129 	fmt = va_arg(ap, char *);
130 	(void) vfprintf(stderr, fmt, ap);
131 	va_end(ap);
132 	fprintf(stderr, "\n");
133 }
134 
135 syserror(va_alist)
136 	va_dcl
137 {
138 	char *fmt;
139 	va_list ap;
140 	extern errno;
141 
142 	fprintf(stderr, "%s: ", Program);
143 	va_start(ap);
144 	fmt = va_arg(ap, char *);
145 	(void) vfprintf(stderr, fmt, ap);
146 	va_end(ap);
147 	fprintf(stderr, ": %s\n", strerror(errno));
148 }
149