/*
 *  main.c -- コマンドラインパラメータの処理
 *
 *  revision history:
 *	0.0: Jun   8, 1996 by Dai ISHIJIMA
 *	0.1: Jul. 27, 1996
 *	0.2: Aug. 10, 1996
 *	0.3: Sep.  3, 1996
 */

#include <stdio.h>
#include <unistd.h>

#include "bsdfiler.h"


char *prog;
#define shift --argc; ++argv


usage()
{
    fprintf(stderr, "Usage: %s [option]... tty\n", prog);
    fputs("    options:\n", stderr);
    fputs("\t-speed number, -number: set terminal speed to the number given\n",
	  stderr);
    fputs("\t-root dir: change top directory\n", stderr);
}


main(int argc, char *argv[])
{
    int fd;
    int bps;
    int n;

    prog = *argv;
    shift;
    bps = BPS;
    while ((argc > 0) && (argv[0][0] == '-')) {
	if (sscanf(*argv, "%d", &n) == 1) {
	    bps = -n;
	}
	else if (argv[0][1] == 's') {
	    shift;
	    bps = atoi(*argv);
	}
	else if (argv[0][1] == 'r') {
	    shift;
	    if (chdir(*argv) != 0) {
		fprintf(stderr, "%s: no such directory: %s\n", prog, *argv);
		exit(1);
	    }
	}
	else {
	    usage();
	    exit(1);
	}
	shift;
    }
    if (argc > 0) {
	fd = opentty(*argv);
    }
    else {
	usage();
	exit(1);
    }
    tblini(); /* CRC計算用テーブルの初期化 */
    ttyinit(fd, bps);
    while (emulserver(fd) != YES) {	
	; /* loop */
    }
    ttyrestore(fd);
    exit(0);
}
