[net.sources] echo.c for APPLE AZTEC, new & improved

jed@mb2c.UUCP (John E. Duncan III) (11/16/83)

/* echo.c  for APPLE AZTEC 'C' system */

/* This command echoes its arguments to STDOUT.  Any special characters
 * can be produced by using the backslash to quote the character or
 * octal string (ex: \012 is same as \n) or hex string (ex. \x0C is
 * same as \n).  Stdio is avoided like the plague to keep it small, fast
 * and reusable via the 'run' command.
 */

#define STDOUT 1
#define BUFSIZ 256

static char *buf, *op;
static char hex[] = "0123456789ABCDEF";

main(argc, argv)
	int argc;
	char **argv;
{
	register char *ap, *endbuf, *hp, ch, cx;
	register int i, j;
	char space[BUFSIZ];

	op = buf = space;
	endbuf = space + BUFSIZ - 1;

	if( --argc ) {
		for( i = 1; i <= argc; i++ ) {
			for( ap = *++argv; *ap; ++ap ) {
				if(*ap == '\\') {
					switch(*++ap) {
					case 'b':
						ch = '\b';
						break;
					case 'c':
						goto done;
					case 'f':
						ch = '\f';
						break;
					case 'n':
						ch = '\n';
						break;
					case 'r':
						ch = '\r';
						break;
					case 't':
						ch = '\t';
						break;
					case 'x':
						ch = 0;
						if( hp = index( hex, *++ap )) ch = hp - hex;
						if( hp = index( hex, *++ap )) ch = (ch<<4) + hp - hex;
						break;
					case '\\':
						ch = '\\';
						break;
					case '0':
						j = ch = 0;
						hp = ap + 4;
						while (((cx = *++ap) >= '0' && cx <= '7') && ap < hp)
							ch = (ch<<3) + cx - '0';
					default:
						--ap;
					}
					*op = ch;
				}
				else
					*op = *ap;
				if( ++op == endbuf ) flush();
			}
			*op = (i == argc ? '\n' : ' ');
			if( ++op == endbuf ) flush();
		}
	}
	else {		/* no arguments, just emit new-line and exit */
		*space = '\n';
		++op;
	}
done:
	flush();
	exit(0);
}

flush()
{
	register int i;

	i = op - buf;
	if( i ) write( STDOUT, buf, i );
	op = buf;
}