196625Stjr/*	$NetBSD: asa.c,v 1.11 1997/09/20 14:55:00 lukem Exp $	*/
296625Stjr
396625Stjr/*
496625Stjr * Copyright (c) 1993,94 Winning Strategies, Inc.
596625Stjr * All rights reserved.
696625Stjr *
796625Stjr * Redistribution and use in source and binary forms, with or without
896625Stjr * modification, are permitted provided that the following conditions
996625Stjr * are met:
1096625Stjr * 1. Redistributions of source code must retain the above copyright
1196625Stjr *    notice, this list of conditions and the following disclaimer.
1296625Stjr * 2. Redistributions in binary form must reproduce the above copyright
1396625Stjr *    notice, this list of conditions and the following disclaimer in the
1496625Stjr *    documentation and/or other materials provided with the distribution.
1596625Stjr * 3. All advertising materials mentioning features or use of this software
1696625Stjr *    must display the following acknowledgement:
1796625Stjr *      This product includes software developed by Winning Strategies, Inc.
1896625Stjr * 4. The name of the author may not be used to endorse or promote products
1996625Stjr *    derived from this software without specific prior written permission
2096625Stjr *
2196625Stjr * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
2296625Stjr * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
2396625Stjr * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
2496625Stjr * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
2596625Stjr * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
2696625Stjr * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
2796625Stjr * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
2896625Stjr * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
2996625Stjr * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
3096625Stjr * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3196625Stjr */
3296625Stjr
3396625Stjr#include <sys/cdefs.h>
3496625Stjr#if 0
3596625Stjr#ifndef lint
3696625Stjr__RCSID("$NetBSD: asa.c,v 1.11 1997/09/20 14:55:00 lukem Exp $");
3796625Stjr#endif
3896625Stjr#endif
3996625Stjr__FBSDID("$FreeBSD$");
4096625Stjr
4196626Stjr#include <err.h>
4296625Stjr#include <stdio.h>
4396625Stjr#include <stdlib.h>
4496629Stjr#include <unistd.h>
4596625Stjr
4696627Stjrstatic void asa(FILE *);
4796628Stjrstatic void usage(void);
4896625Stjr
4996625Stjrint
5096627Stjrmain(int argc, char *argv[])
5196625Stjr{
5296628Stjr	int ch, exval;
5396625Stjr	FILE *fp;
5496628Stjr	const char *fn;
5596625Stjr
5696628Stjr	while ((ch = getopt(argc, argv, "")) != -1) {
5796628Stjr		switch (ch) {
5896628Stjr		case '?':
5996628Stjr		default:
6096628Stjr			usage();
6196628Stjr			/*NOTREACHED*/
6296628Stjr		}
6396628Stjr	}
6496628Stjr	argc -= optind;
6596628Stjr	argv += optind;
6696625Stjr
6796628Stjr	exval = 0;
6896628Stjr	if (argc == 0)
6996628Stjr		asa(stdin);
7096628Stjr	else {
7196628Stjr		while ((fn = *argv++) != NULL) {
7296628Stjr                        if ((fp = fopen(fn, "r")) == NULL) {
7396628Stjr				warn("%s", fn);
7496628Stjr				exval = 1;
7596625Stjr				continue;
7696625Stjr                        }
7796628Stjr			asa(fp);
7896628Stjr			fclose(fp);
7996628Stjr		}
8096628Stjr	}
8196625Stjr
8296628Stjr	exit(exval);
8396625Stjr}
8496625Stjr
8596625Stjrstatic void
8696628Stjrusage(void)
8796628Stjr{
8896628Stjr
89146466Sru	fprintf(stderr, "usage: asa [file ...]\n");
9096628Stjr	exit(1);
9196628Stjr}
9296628Stjr
9396628Stjrstatic void
9496627Stjrasa(FILE *f)
9596625Stjr{
9696626Stjr	size_t len;
9796625Stjr	char *buf;
9896625Stjr
9996626Stjr	if ((buf = fgetln(f, &len)) != NULL) {
10096625Stjr		if (buf[len - 1] == '\n')
10196625Stjr			buf[--len] = '\0';
10296626Stjr		/* special case the first line */
10396625Stjr		switch (buf[0]) {
10496625Stjr		case '0':
10596626Stjr			putchar('\n');
10696625Stjr			break;
10796625Stjr		case '1':
10896626Stjr			putchar('\f');
10996625Stjr			break;
11096625Stjr		}
11196625Stjr
11296626Stjr		if (len > 1 && buf[0] && buf[1])
11396625Stjr			printf("%.*s", (int)(len - 1), buf + 1);
11496625Stjr
11596625Stjr		while ((buf = fgetln(f, &len)) != NULL) {
11696625Stjr			if (buf[len - 1] == '\n')
11796625Stjr				buf[--len] = '\0';
11896625Stjr			switch (buf[0]) {
11996625Stjr			default:
12096625Stjr			case ' ':
12196626Stjr				putchar('\n');
12296625Stjr				break;
12396625Stjr			case '0':
12496626Stjr				putchar('\n');
12596626Stjr				putchar('\n');
12696625Stjr				break;
12796625Stjr			case '1':
12896626Stjr				putchar('\f');
12996625Stjr				break;
13096625Stjr			case '+':
13196626Stjr				putchar('\r');
13296625Stjr				break;
13396625Stjr			}
13496625Stjr
13596626Stjr			if (len > 1 && buf[0] && buf[1])
13696625Stjr				printf("%.*s", (int)(len - 1), buf + 1);
13796625Stjr		}
13896625Stjr
13996626Stjr		putchar('\n');
14096625Stjr	}
14196625Stjr}
142