LISTING FOUR

#include    "channel.hpp"
	
#define MAX_CHANNELS	36		// # of lines

// Global Variables
int Hw_int;		   		// hardware interrupt
int Int_level;	   		// software interrupt
CHANNEL Channel[MAX_CHANNELS];

// Local Prototypes
static int wait_for_event(EVTBLK *evtp);
static void process_lines();
static int init_card();

main()
{
	if(!init_card()) {
		puts("Couldn't initialize card!");
		exit(0);
	}
	for (int line=0; line<MAX_CHANNELS; line++)
		Channel[line].prep(line);
	process_lines();
	stopsys();
}


void process_lines()
{
	// event data block for speech card "events"
    EVTBLK evtblk;   	

	for(;;) {
		// wait for a speech card to generate event
       	if (wait_for_event(&evtblk)!=0)
        	break;

		// get a pointer to channel for this event
		CHANNEL *ch = &Channel[evtblk.devchan];
		ch->cmplt_state(evtblk.evtcode);

		// begin new state and check for error
		int errcode;
		if ((errcode = ch->begin_state()) != 0)
        	printf("Error %d\n", errcode);
	}
}


int init_card()
{
	unsigned int channels = 0; 	// phone lines found
	Int_level = getvctr();	// get software interrupt


	// if card found, start the dialogic system with
	// correct interrupt level, with event queuing 
	// enabled, no extra buffers allocated, and
	// store the number of lines in 'channels'
	if(Int_level) {
		stopsys();
		startsys(Hw_int, SM_EVENT, 0, 0, &channels);
	}
	return channels;	
}


// Return 0 if card generates event or -1 
// if ESC key was pressed.
int wait_for_event(EVTBLK *evtp)
{
	for(;;) {
		// check to see if a key has been pressed
       	while (bioskey(1))  {
        	// exit if ESC was pressed
          	if (bioskey(0) == 0x11b) 
            	return -1;
		}
       	if (gtevtblk(evtp) == -1)
        	break;
	}
    return 0;
}
