xref: /freebsd/usr.bin/asa/asa.c (revision 031beb4e239bfce798af17f5fe8dba8bcaf13d99)
1 /*	$NetBSD: asa.c,v 1.11 1997/09/20 14:55:00 lukem Exp $	*/
2 
3 /*-
4  * SPDX-License-Identifier: BSD-4-Clause
5  *
6  * Copyright (c) 1993,94 Winning Strategies, Inc.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  * 3. All advertising materials mentioning features or use of this software
18  *    must display the following acknowledgement:
19  *      This product includes software developed by Winning Strategies, Inc.
20  * 4. The name of the author may not be used to endorse or promote products
21  *    derived from this software without specific prior written permission
22  *
23  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
24  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
25  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
28  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
32  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 #if 0
37 #ifndef lint
38 __RCSID("$NetBSD: asa.c,v 1.11 1997/09/20 14:55:00 lukem Exp $");
39 #endif
40 #endif
41 #include <err.h>
42 #include <stdio.h>
43 #include <stdlib.h>
44 #include <string.h>
45 #include <unistd.h>
46 
47 static void asa(FILE *);
48 static void usage(void) __dead2;
49 
50 int
51 main(int argc, char *argv[])
52 {
53 	int ch, exval;
54 	FILE *fp;
55 	const char *fn;
56 
57 	while ((ch = getopt(argc, argv, "")) != -1) {
58 		switch (ch) {
59 		case '?':
60 		default:
61 			usage();
62 			/*NOTREACHED*/
63 		}
64 	}
65 	argc -= optind;
66 	argv += optind;
67 
68 	exval = 0;
69 	if (argc == 0)
70 		asa(stdin);
71 	else {
72 		while ((fn = *argv++) != NULL) {
73 			if (strcmp(fn, "-") == 0) {
74 				asa(stdin);
75 			} else {
76 				if ((fp = fopen(fn, "r")) == NULL) {
77 					warn("%s", fn);
78 					exval = 1;
79 					continue;
80 				}
81 				asa(fp);
82 				fclose(fp);
83 			}
84 		}
85 	}
86 
87 	if (fflush(stdout) != 0)
88 		err(1, "stdout");
89 
90 	exit(exval);
91 }
92 
93 static void
94 usage(void)
95 {
96 
97 	fprintf(stderr, "usage: asa [file ...]\n");
98 	exit(1);
99 }
100 
101 static void
102 asa(FILE *f)
103 {
104 	size_t len;
105 	char *buf;
106 
107 	if ((buf = fgetln(f, &len)) != NULL) {
108 		if (buf[len - 1] == '\n')
109 			buf[--len] = '\0';
110 		/* special case the first line */
111 		switch (buf[0]) {
112 		case '0':
113 			putchar('\n');
114 			break;
115 		case '1':
116 			putchar('\f');
117 			break;
118 		}
119 
120 		if (len > 1 && buf[0] && buf[1])
121 			printf("%.*s", (int)(len - 1), buf + 1);
122 
123 		while ((buf = fgetln(f, &len)) != NULL) {
124 			if (buf[len - 1] == '\n')
125 				buf[--len] = '\0';
126 			switch (buf[0]) {
127 			default:
128 			case ' ':
129 				putchar('\n');
130 				break;
131 			case '0':
132 				putchar('\n');
133 				putchar('\n');
134 				break;
135 			case '1':
136 				putchar('\f');
137 				break;
138 			case '+':
139 				putchar('\r');
140 				break;
141 			}
142 
143 			if (len > 1 && buf[0] && buf[1])
144 				printf("%.*s", (int)(len - 1), buf + 1);
145 		}
146 
147 		putchar('\n');
148 	}
149 
150 	if (ferror(stdout) != 0)
151 		err(1, "stdout");
152 }
153