xref: /original-bsd/usr.bin/unvis/unvis.c (revision c829ecf6)
1 /*-
2  * Copyright (c) 1989 The Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 #ifndef lint
9 char copyright[] =
10 "@(#) Copyright (c) 1989 The Regents of the University of California.\n\
11  All rights reserved.\n";
12 #endif /* not lint */
13 
14 #ifndef lint
15 static char sccsid[] = "@(#)unvis.c	5.1 (Berkeley) 06/01/90";
16 #endif /* not lint */
17 
18 #include <stdio.h>
19 #include <vis.h>
20 
21 char	*Program;
22 #define usage()	fprintf(stderr, "usage: %s %s\n", Program, USAGE)
23 #define USAGE "[file...]"
24 
25 main(argc, argv)
26 	char *argv[];
27 {
28 	FILE *fp;
29 	extern char *optarg;
30 	extern int optind;
31 	int ch;
32 
33 	Program = argv[0];
34 	while ((ch = getopt(argc, argv, "")) != EOF)
35 		switch((char)ch) {
36 		case '?':
37 		default:
38 			usage();
39 			exit(1);
40 		}
41 	argc -= optind;
42 	argv += optind;
43 
44 	if (*argv)
45 		while (*argv) {
46 			if ((fp=fopen(*argv, "r")) != NULL)
47 				process(fp, *argv);
48 			else
49 				syserror("%s", *argv);
50 			argv++;
51 		}
52 	else
53 		process(stdin, "<stdin>");
54 	exit(0);
55 }
56 
57 process(fp, filename)
58 	FILE *fp;
59 	char *filename;
60 {
61 	register int offset = 0, c, ret;
62 	int state = 0;
63 	char outc;
64 
65 	while ((c = getc(fp)) != EOF) {
66 		offset++;
67 	again:
68 		switch(ret = unvis(&outc, (char)c, &state, 0)) {
69 		case UNVIS_VALID:
70 			putchar(outc);
71 			break;
72 		case UNVIS_VALIDPUSH:
73 			putchar(outc);
74 			goto again;
75 		case UNVIS_SYNBAD:
76 			error("%s: offset: %d: can't decode", filename, offset);
77 			state = 0;
78 			break;
79 		case 0:
80 		case UNVIS_NOCHAR:
81 			break;
82 		default:
83 			error("bad return value (%d), can't happen", ret);
84 			exit(1);
85 		}
86 	}
87 	if (unvis(&outc, (char)0, &state, UNVIS_END) == UNVIS_VALID)
88 		putchar(outc);
89 }
90 
91 #include <varargs.h>
92 
93 error(va_alist)
94 	va_dcl
95 {
96 	char *fmt;
97 	va_list ap;
98 	extern errno;
99 
100 	fprintf(stderr, "%s: ", Program);
101 	va_start(ap);
102 	fmt = va_arg(ap, char *);
103 	(void) vfprintf(stderr, fmt, ap);
104 	va_end(ap);
105 	fprintf(stderr, "\n");
106 }
107 
108 syserror(va_alist)
109 	va_dcl
110 {
111 	char *fmt;
112 	va_list ap;
113 	extern errno;
114 
115 	fprintf(stderr, "%s: ", Program);
116 	va_start(ap);
117 	fmt = va_arg(ap, char *);
118 	(void) vfprintf(stderr, fmt, ap);
119 	va_end(ap);
120 	fprintf(stderr, ": %s\n", strerror(errno));
121 }
122