Fixed an error I made a long while back which caused us to link against
[unix-history] / usr.bin / unvis / unvis.c
CommitLineData
15637ed4
RG
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
35char 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
41static char sccsid[] = "@(#)unvis.c 5.1 (Berkeley) 6/1/90";
42#endif /* not lint */
43
44#include <stdio.h>
45#include <vis.h>
46
47char *Program;
48#define usage() fprintf(stderr, "usage: %s %s\n", Program, USAGE)
49#define USAGE "[file...]"
50
51main(argc, argv)
52 char *argv[];
53{
54 FILE *fp;
55 extern char *optarg;
56 extern int optind;
57 int ch;
58
59 Program = argv[0];
60 while ((ch = getopt(argc, argv, "")) != EOF)
61 switch((char)ch) {
62 case '?':
63 default:
64 usage();
65 exit(1);
66 }
67 argc -= optind;
68 argv += optind;
69
70 if (*argv)
71 while (*argv) {
72 if ((fp=fopen(*argv, "r")) != NULL)
73 process(fp, *argv);
74 else
75 syserror("%s", *argv);
76 argv++;
77 }
78 else
79 process(stdin, "<stdin>");
80 exit(0);
81}
82
83process(fp, filename)
84 FILE *fp;
85 char *filename;
86{
87 register int offset = 0, c, ret;
88 int state = 0;
89 char outc;
90
91 while ((c = getc(fp)) != EOF) {
92 offset++;
93 again:
94 switch(ret = unvis(&outc, (char)c, &state, 0)) {
95 case UNVIS_VALID:
96 putchar(outc);
97 break;
98 case UNVIS_VALIDPUSH:
99 putchar(outc);
100 goto again;
101 case UNVIS_SYNBAD:
102 error("%s: offset: %d: can't decode", filename, offset);
103 state = 0;
104 break;
105 case 0:
106 case UNVIS_NOCHAR:
107 break;
108 default:
109 error("bad return value (%d), can't happen", ret);
110 exit(1);
111 }
112 }
113 if (unvis(&outc, (char)0, &state, UNVIS_END) == UNVIS_VALID)
114 putchar(outc);
115}
116
117#include <varargs.h>
118
119error(va_alist)
120 va_dcl
121{
122 char *fmt;
123 va_list ap;
124 extern errno;
125
126 fprintf(stderr, "%s: ", Program);
127 va_start(ap);
128 fmt = va_arg(ap, char *);
129 (void) vfprintf(stderr, fmt, ap);
130 va_end(ap);
131 fprintf(stderr, "\n");
132}
133
134syserror(va_alist)
135 va_dcl
136{
137 char *fmt;
138 va_list ap;
139 extern errno;
140
141 fprintf(stderr, "%s: ", Program);
142 va_start(ap);
143 fmt = va_arg(ap, char *);
144 (void) vfprintf(stderr, fmt, ap);
145 va_end(ap);
146 fprintf(stderr, ": %s\n", strerror(errno));
147}