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