#include <curses.h>
#include <signal.h>

main()
{
	void die();

/*           if (startup() != ERR)  startup unwritten  */

/*                   init stdscr, curscr and terminal  */

	initscr();

/*                        call die() if get interrupt  */

	signal(SIGINT, die);	

/*                 spreadsheet function defined later  */

	spreadsheet();
	die();
}

void die()
{
/*                                  ignore interrupts  */

	signal(SIGINT, SIG_IGN);

/*                          move cursor to lower left  */

	mvcur(0, COLS - 1, LINES - 1, 0);

/*                       make terminal the way it was  */

	endwin();
	exit(0);                    /*  exit normally  */
}

#define VERSION 12

spreadsheet()
{
	char filename[20];

	intro(VERSION);        /*  print first screen  */
/*                             initialize input modes  */
	initinput();
/*                       read in file, check password  */

	getfileandpw(filename);

/*                  create the fields for spreadsheet  */

	makefields();
/*                       makefields also calls driver  */
}

/*  intro() prints an initial screen  */

intro(version)
int version;
{
	int x1, x2, y;
/*                         make box around the screen  */

	box(stdscr, '*', '-');
	move(1,2);
	addstr("SHOWME Spreadsheet Program, version: ");
	getyx(stdscr, y, x1);
	printw("%d", version);
	getyx(stdscr, y, x2);
	while (x2++ - x1 < 6)
		addch(' ');
	addstr("1/1/86");
/*                            send stdscr to terminal  */
	refresh();
}

initinput()                  /*  set up initial modes  */
{
	raw();
	echo();
	nonl();
}

getfileandpw(filename)
char *filename;
{
	char passwd[6];
	int i = 0;
	mvaddstr(4,2, "What file do you want? ");
	refresh();
	while ((filename[i] = getch()) != '\r')
		i++;
	filename[i] = '\0';
	mvaddstr(3,2, "Six character Password? ");
	noecho();
	refresh();
	i = 0;
	while (i < 6)
		passwd[i++] = getch();
/*                                     check password  */
	if (passwdok(passwd)) {
		mvaddstr(4,26, "OK");
/*         readinfile(filename);  readinfile unwritten */
		refresh();
		}
	else {
		mvaddstr(4,26, "NO");
		refresh();
		die();
		}
}
/*            Dummy function so that example will run  */
passwdok(passwd)
char *passwd;
{
	return(1);
}

int _top, _left;

makefields()
{
	WINDOW *worksp, *field[10][10];
	int i, j;
	extern int _left, _top;

	worksp = subwin(stdscr, LINES - 2, COLS - 2, 1, 1);

/*                     create a sub-window to work in  */

	werase(worksp);
	wmove(worksp, 1, 0);
	waddstr(worksp, "\t0\t1\t2\t3\t4\t5\t6\t7");
	wmove(worksp, 3, 0);
	waddch(worksp, 'a');
	mvwaddch(worksp, 5, 0, 'b');
	mvwaddch(worksp, 7, 0, 'c');
	mvwaddch(worksp, 9, 0, 'd');
	mvwaddch(worksp, 11, 0, 'e');
	mvwaddch(worksp, 13, 0, 'f');
	mvwaddch(worksp, 15, 0, 'g');
	mvwaddch(worksp, 17, 0, 'h');
	mvwaddch(worksp, 19, 0, 'i');
	mvwaddch(worksp, 21, 0, 'j');
	wrefresh(worksp);

/*                         display border of workspace */

	for (i = 0; i < 10; i++)
		for (j = 0; j < 10; j++) {
			field[i][j] = newwin(1, 7, 4+2*i, 7+8*j);
			wprintw(field[i][j], "%d %d", i, j);
		}

/*                           create fields to work in  */

	_left = 0; _top = 0;
	reffields(field);
/*     iodriver(worksp, field);    iodriver unwritten  */
}

/*           reffields refreshes the fields currently
                          positioned in the workspace  */

reffields(field)
WINDOW *field[10][10];
{
	int i, j;
	extern int _top, _left;
	for (i = _top; i <= _top + 7; i++)
		for (j = _left; j <= _left + 7; j++) {
			touchwin(field[i][j]);
			wrefresh(field[i][j]);
		}
}

