/*
CIFFOLD 0.3 Pre-Release
by Kostadin Mitev, Georgi Todorov and Herbert J. Bernstein

Copyright (C) Kostadin Mitev 2005

Work funded in part by the International Union of Crystallography under
a grant to Dowling College.

This software is covered by the GNU General Public License 
<see COPYING>.

This program is free software; you can redistribute it and/or modify it
under the terms of the GNU General Public License as published by the
Free Software Foundation; either version 2 of the License, or (at your
option) any later version.

This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
Public License for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

 */



#include <menu.h>
#include <stdio.h>
#include <string>
#include <curses.h>
#include <iostream>
#include "FUCIF.h"
#include "getOpt.h"
#define GLOBALS
#include "Globals.h"
#define MAXLEN 80

#define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
#define CTRLD 	4

using namespace std;

char *choices[] = 
{
	"1.0",
	"1.1",
	"Do not change"
};

char *yesno[] = 
{
	"Yes",
	"No"
};

void print_in_middle(WINDOW *, int, int, int, char *, chtype);
int print_menu(MENU *, int, int, int, char *, chtype);
char * ask_win(char *, chtype, char *);
int print_yesno_question(int, int, int, char *, chtype, char *);
void printError(int, int, int, char *, chtype, char *);
void printStdinToStdout();

int main(int argc, char *argv[])
{
	ITEM **my_items;
	MENU *my_menu;
        int n_choices, i;
	ITEM *cur_item;
	WINDOW *my_menu_win;
	
	char * infile = NULL;
	char * pinfile;
	char * outfile = NULL;
	char * large_loop = new char[200];
	char * column_align = new char[50];
	char * filechunks = new char[4];
	char *tmpFile = NULL;  	//name of the temporary file generated after the first pass of through the inputfile
	int t =atoi(large_loop);
	int startGUI = 2;		//2- start the GUI menu any other value do not start
	
/*	OPTIONS EXPLAINED:
	OPTIONS[0];		//is the file version (0 - do not change;1 - 1.0;2 - 1.1)
	OPTIONS[1];		//is Folding/Unfolding (0 - unfolding; 1 - folding)
	OPTIONS[2];		//is Terse Folding/Unfolding (0 - no terse; 1 - terse)
	OPTIONS[3];		//is Terse on loops (0 - no terse; 1 - terse)
	OPTIONS[4];		//is Preserve Leading Blanks (0 - do not preserve; 1 - preserver)
	OPTIONS[5];		//is size of large loops (def is 70)
	OPTIONS[6];		//is Process the entire file(0 - not set; 1 - set)
	OPTIONS[7];		//is Format Only Comments (0 - not set; 1 - set)	
	OPTIONS[8];		//is Format everything except comments(0 - not set; 1 - set)
	OPTIONS[9];		//The column with respect to which the data should be aligned
	OPTIONS[10];		//0- not a dictionary file, 1- dictionary file
	OPTIONS[11]; 		//Output the warning messages as a special comment at the bottom of the file(0-no, 1-yes)
	OPTIONS[12];    	//Output the error messages as a spcial comment at the bottom of the file(0-no , 1- yes)
	OPTIONS[13];		//if folding generate a map to recover the file in unfolding, if unfolding recover the file using 
*/				//the generated map	
	
	for (i = 0; i < 14; i++) OPTIONS[i] = -1;
	
	if( argc > 1 ) {
	   startGUI =  parseOpt(argc, argv, &pinfile); 
	   if(startGUI){
		//if the options are passed correctly parseOpt returns 1(do not enter curses) or 2 (enter curses)
		infile = pinfile;
	   } else {
	   exit(1);
	   }
	}
	//there are no command line arguments stdin should be printed to stdout
	else if(argc == 1){
		printStdinToStdout();
	   exit(0);			
	}
	if (startGUI == 2){
		//Initialize curses 	
		initscr();
		
		start_color();
		init_pair(1, COLOR_BLACK, COLOR_GREEN);
		init_pair(2, COLOR_WHITE, COLOR_BLACK);
		init_pair(3, COLOR_MAGENTA, COLOR_GREEN);
		
		// Initialize items 
       		 n_choices = ARRAY_SIZE(choices);
       		 my_items = new ITEM* [n_choices+1];
        
		for(i = 0; i < n_choices; ++i)
       		         my_items[i] = new_item(choices[i],NULL);
	
		my_items[n_choices] = (ITEM *)NULL;

		/* Create menu */
		my_menu = new_menu((ITEM **)my_items);
	
		/*Start asking...*/
	
	
		infile = ask_win("ENTER INPUTFILE", COLOR_PAIR(0),"");
		while((fopen(infile, "r")) == NULL){
			printError(1, 0, 40, "ERROR! Cannot Read File", COLOR_PAIR(0),"");
			infile = ask_win("ENTER INPUTFILE", COLOR_PAIR(0),"");
		}
	
		while(TRUE){
			int over=-1;
			outfile = ask_win("ENTER OUTPUTFILE", COLOR_PAIR(0),"");
			if (!strcmp(infile,outfile)){
				printError(1, 0, 40, "ERROR! Input and Output files match!", COLOR_PAIR(0), "Please enter different file names for input and output files!");
				continue;
			}
			if((fopen(outfile, "r")) !=NULL){
				over=print_yesno_question(1, 0, 40, "ERROR! File Exists! Overwrite?", COLOR_PAIR(0),"");
				if (over==1)
					break;
				else
					continue;
			}
			else
				break;
		}
		
		OPTIONS[0] = print_menu(my_menu, 1, 0, 40,"File version",COLOR_PAIR(0));
		
		while(OPTIONS[1]== -1){
			OPTIONS[1]=print_yesno_question(1, 0, 40, "Folding(Yes) or Unfolding(No)", COLOR_PAIR(0),"");
		}
		if (OPTIONS[1]==1){
			while(OPTIONS[13]==-1){
				OPTIONS[13]=print_yesno_question(1, 0, 40, "Create a MAP?", COLOR_PAIR(0),"Some coments on MAPs");
			}
			int tmp = -1;
			while(tmp == -1){
				tmp =atoi(ask_win("Enter maximum line length", COLOR_PAIR(0),"Valid input is an integer in the range 60-2048"));
				if((tmp < 60) || (tmp > 2048) || (tmp < 0)){
					printError(1, 0, 40, "ERROR! Invalid input", COLOR_PAIR(0), "Input must be an integer in the range 60-2048");
					tmp = -1;
				}
			}
			MAXLENGTH = tmp;
		}else{
			while(OPTIONS[13]==-1)
			OPTIONS[13]=print_yesno_question(1, 0, 40, "Read from a MAP?", COLOR_PAIR(0),"Some coments on MAPs");
		}
		if (OPTIONS[1]==0&&OPTIONS[13]==1){
			while (OPTIONS[6]==-1){
				OPTIONS[6]=print_yesno_question(1, 0, 40, "Process the entire file?", COLOR_PAIR(0),"");
			}
			if (OPTIONS[6]==0){//Process the entire file is not set
				char * token = NULL;
				unsigned long start = 0;
				unsigned long end = 0;
				strcpy(filechunks, "n");
				while(true){
					filechunks=ask_win("Enter chunk pairs or END to continue", COLOR_PAIR(0),"Example: 1-4 or 234-345 or end or End or END");
					if (!strcasecmp(filechunks,"END")){
						break;
					}
				    if(strstr(filechunks, "-") == NULL){
					    printError(1, 0, 40, "ERROR! Input must be in pairs", COLOR_PAIR(0),"Example: 1-4 or 234-345 or end or End or END");
					    clear();
				    	continue;
				    }	
					token = strtok( filechunks, "-" );
					while( token != NULL ){	
						start = atoi(token);
						token = strtok( NULL,"-" );
						end = atoi(token);
						token = strtok( NULL,"-" );
						if (start < end){
							if(!FileChunks.empty() && start <= FileChunks.back()){
								printError(1, 0, 40, "ERROR! First value problem", COLOR_PAIR(0),"Your first value must be greater than the largest value you've entered so far!");
								break;
							}
						  FileChunks.push_back(start);
						  FileChunks.push_back(end);
						}else
						printError(1, 0, 40, "ERROR! Second value problem", COLOR_PAIR(0),"Your second value should be greater than the first one!");
					}
				}
			}
			while(OPTIONS[7]==-1){
				OPTIONS[7]=print_yesno_question(1, 0, 40, "Format only coments?", COLOR_PAIR(0),"");
			}
			if (OPTIONS[7]==0){//Format comments not set
				while (OPTIONS[8]==-1){
					OPTIONS[8]=print_yesno_question(1, 0, 40, "Format everything except comments?", COLOR_PAIR(0),"");
				}
			}
		}else{
		while(OPTIONS[2]==-1){
			OPTIONS[2]=print_yesno_question(1, 0, 40, "Terse Folding/Unfolding?", COLOR_PAIR(0),"");
		}
		if (OPTIONS[2]==0){//Terse  Folding/Unfolding is not set
			while (OPTIONS[3]==-1){
				OPTIONS[3]=print_yesno_question(1, 0, 40, "Terse formatting on loops?", COLOR_PAIR(0),"");
			}
			if (OPTIONS[3]==1){//Terse formatting on loops is set
		 		while ((atoi(large_loop)==1) || (atoi(large_loop)<5) ){
					large_loop = ask_win("How many items is a big loop?", COLOR_PAIR(0),"Default is 70 (tag + data)");
				OPTIONS[5]=atoi(large_loop);
				}
			}
			while (OPTIONS[4]==-1){
				OPTIONS[4]= print_yesno_question(1, 0, 40, "Preserve leading blanks?", COLOR_PAIR(0),"");
			}
			while (OPTIONS[6]==-1){
				OPTIONS[6]=print_yesno_question(1, 0, 40, "Process the entire file?", COLOR_PAIR(0),"");
			}
			if (OPTIONS[6]==0){//Process the entire file is not set
				char * token = NULL;
				unsigned long start = 0;
				unsigned long end = 0;
				strcpy(filechunks, "n");
				while(true){
					filechunks=ask_win("Enter chunk pairs or END to continue", COLOR_PAIR(0),"Example: 1-4 or 234-345 or end or End or END");
					if (!strcasecmp(filechunks,"END")){
						break;
					}
				    if(strstr(filechunks, "-") == NULL){
					    printError(1, 0, 40, "ERROR! Input must be in pairs", COLOR_PAIR(0),"Example: 1-4 or 234-345 or end or End or END");
				    clear();
					    	continue;
				    }	
					token = strtok( filechunks, "-" );
					while( token != NULL ){	
						start = atoi(token);
						token = strtok( NULL,"-" );
						end = atoi(token);
						token = strtok( NULL,"-" );
						if (start < end){
							if(!FileChunks.empty() && start <= FileChunks.back()){
								printError(1, 0, 40, "ERROR! First value problem", COLOR_PAIR(0),"Your first value must be greater than the largest value you've entered so far!");
								break;
							}
						  FileChunks.push_back(start);
						  FileChunks.push_back(end);
						}else
						printError(1, 0, 40, "ERROR! Second value problem", COLOR_PAIR(0),"Your second value should be greater than the first one!");
					}
				}
			}
			while(OPTIONS[7]==-1){
				OPTIONS[7]=print_yesno_question(1, 0, 40, "Format only coments?", COLOR_PAIR(0),"");
			}
			if (OPTIONS[7]==0){//Format comments not set
				while (OPTIONS[8]==-1){
					OPTIONS[8]=print_yesno_question(1, 0, 40, "Format everything except comments?", COLOR_PAIR(0),"");
				}
			}
			if (OPTIONS[1]==0){
				column_align = ask_win("The column with respect to which the data should be aligned", COLOR_PAIR(0),"Default is Not Specified(0) or enter a number between 5 and 2039");
				while (atoi(column_align)!=0 && (atoi(column_align)<5||atoi(column_align)>(LMAXLEN -10) )){
					printError(1, 0, 40, "ERROR! The number is invalid", COLOR_PAIR(0),"Enter a number between 5 and 2000 or leave blank for none");
					column_align = ask_win("The column with respect to which the data should be aligned", COLOR_PAIR(0),"Default is 0 or enter a number between 5 and 2000");
				}
				OPTIONS[9]=atoi(column_align);
			}
			
		}   //No terse formatting ends here
		}
		while (OPTIONS[10]==-1){
			OPTIONS[10]=print_yesno_question(1, 0, 40, "Is this a dictionary file?",COLOR_PAIR(0),"");
		}
		while (OPTIONS[11]==-1){
			OPTIONS[11]=print_yesno_question(1, 0, 40, "Output the warning messages?",COLOR_PAIR(0),"Warning messages will be as a special comment at the bottom of the file");
		}
		while (OPTIONS[12]==-1){
			OPTIONS[12]=print_yesno_question(1, 0, 40, "Output the error messages?",COLOR_PAIR(0),"Error messages will be as a special comment at the bottom of the file");
		}
		/* Free memory? */
		unpost_menu(my_menu);
		for(i = 0; i < n_choices; ++i)
			free_item(my_items[i]);
		free_menu(my_menu);
/*		if(!OPTIONS[1]){
			MAXLENGTH = LMAXLEN;
		}
		else{
			MAXLENGTH = MAXLEN;
		}
*/
		if (strcmp(outfile,"-")) {
		OUTF = fopen(outfile, "w");
		if(!OUTF){
			fprintf(stderr, "FATAL ERROR: Cannot open file %s \n", outfile);
			exit(1);
		}
		} else {
		  OUTF = stdout;
		}
	}//options gethered with ncurses
	//No option should be left unassigned (to have a value of -1)
	//initialize default options if needed
	if(OPTIONS[0] == -1){
		OPTIONS[0] = 0;
	}
	if(OPTIONS[2] == -1){
		OPTIONS[2] = 0;	 	//is Terse Folding/Unfolding (0 - no terse; 1 - terse)
	}	
	if(OPTIONS[3] == -1){
		OPTIONS[3] = 0;		//is Terse on loops (0 - no terse; 1 - terse)
	}
	if(OPTIONS[4] = -1){
		OPTIONS[4] = 1;		//is Preserve Leading Blanks (0 - do not preserve; 1 - preserve)
	}
	if(OPTIONS[5] == -1){
		OPTIONS[5] = 70;	//is size of large loops (def is 70)
	}
	if(OPTIONS[6] == -1){
		OPTIONS[6] = 1;         //is Process the entire file(0 - not set; 1 - set)
	}
	if(OPTIONS[7] == -1){
		OPTIONS[7] = 0;         //is Format Only Comments (0 - not set; 1 - set)
	}
	if(OPTIONS[8] == -1){
		 OPTIONS[8] = 0;        //is Format everything except comments(0 - not set; 1 - set)
	}
	if(OPTIONS[9] == -1){
		OPTIONS[9] = 0;         //The column with respect to which the data should be aligned
	}
	if(OPTIONS[10] == -1){
		 OPTIONS[10] = 0;       //0- not a dictionary file, 1- dictionary file
	}
	if(OPTIONS[11] == -1){
		 OPTIONS[11] = 0;       //Output the warning messages as a special comment at the bottom of the file(0-no, 1-yes)
	}
	if(OPTIONS[12] == -1){
		 OPTIONS[12] = 0;       //Output the error messages as a special comment at the bottom of the file(0-no , 1- yes)
	}
	if(OPTIONS[13] == -1){
		 OPTIONS[13] = 0;       //if folding do not generate a map to recover the file in unfolding, if unfolding do not recover the file using
		                        //the generated map
	}
/////////////////////////////FORCE MAXLENGTH TO BE 2048 if UNFOLDING//////////////////
	if(!OPTIONS[1]){
		MAXLENGTH = 2048;
	}
//////////////////////////////////////////////////////
	tmpFile = R.read(infile, OPTIONS[5],OPTIONS[3], OPTIONS[10]);	
	if(tmpFile != NULL){
		if((INF = fopen(tmpFile, "r")) == NULL){
			fprintf(stderr, "FATAL ERROR: Cannot open file %s \n", tmpFile);
		        exit(1);
		}
		reader();
	}
	if(outfile != NULL){
		printf("The file %s was written.\n", outfile);
	}
	
/////////////////////////////////////////////TESTING//////////////////////////////////	
/*	
	for (int a=0;a< 13;a++){
		fprintf(OUTF, "#_#OPTIONS[%d]: %d\n", a, OPTIONS[a]);
	}
*/
////////////////////////////////////////////////////////////////////////////////////////
	//close the opened files
	if(OUTF){
		fclose(OUTF);
	}
	if(INF){
		fclose(INF);
		remove(tmpFile);
		delete [] tmpFile;
	}
}
void print_in_middle(WINDOW *win, int starty, int startx, int width, char *string, chtype color)
{	int length, x, y;
	float temp;

	if(win == NULL)
		win = stdscr;
	getyx(win, y, x);
	if(startx != 0)
		x = startx;
	if(starty != 0)
		y = starty;
	if(width == 0)
		width = 80;

	length = strlen(string);
	temp = (width - length)/ 2;
	x = startx + (int)temp;
	wattron(win, color);
	mvwprintw(win, y, x, "%s", string);
	wattroff(win, color);
	refresh();
}


int print_yesno_question(int starty, int startx, int width, char *string, chtype color,char*err)
{	
	clear();
	WINDOW *my_menu_win;
	int row, col, c, w=0;
	char * str = new char[10];
	my_menu_win = newwin(10,60,4,4);
	ITEM **my_items;				
	MENU *my_menu;
        int n_choices, i;
	noecho();
		/* Initialize items */
        n_choices = ARRAY_SIZE(yesno);
        my_items = new ITEM* [n_choices+1];
	
	for(i = 0; i < n_choices; ++i)
                my_items[i] = new_item(yesno[i],NULL);
	
	my_items[n_choices] = (ITEM *)NULL;

	/* Create menu */
	my_menu = new_menu((ITEM **)my_items);

	
	keypad(my_menu_win, TRUE);
	set_menu_win(my_menu, my_menu_win);
        set_menu_sub(my_menu, derwin(my_menu_win, 6, 58, 3, 1));


	/* Set fore ground and back ground of the menu */
	set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE);
	set_menu_back(my_menu, COLOR_PAIR(2));
	set_menu_grey(my_menu, COLOR_PAIR(3));

	
	
	/* Post the menu */
        box(my_menu_win, 0, 0);
	
	
	print_in_middle(my_menu_win, starty, startx, width, string, color);
	mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
	mvwhline(my_menu_win, 2, 1, ACS_HLINE, 58);
	mvwaddch(my_menu_win, 2, 59, ACS_RTEE);
	mvprintw(LINES - 2, 0, "Up and Down arrow keys to navigate (F1 to Exit)");
	mvprintw(LINES - 7, 0, "CIFFOLD 0.3 Copyright (C) K. Mitev 2005");
	mvprintw(LINES - 6, 0, "CIFFOLD comes with ABSOLUTELY NO WARRANTY"); 
	mvprintw(LINES - 5, 0, "This is free software, and you are welcome to redistribute it");
	mvprintw(LINES - 4, 0, "under certain conditions; see the GPL in file COPYING.");
	mvprintw(LINES - 3, 0, err);
 
	refresh();
        
	/* Post the menu */
	post_menu(my_menu);
	wrefresh(my_menu_win);

	while((c = wgetch(my_menu_win)) != KEY_F(1))
	{       switch(c)
	        {	case KEY_DOWN:
			{
				menu_driver(my_menu, REQ_DOWN_ITEM);
				break;
			}
			case KEY_UP:
			{
				menu_driver(my_menu, REQ_UP_ITEM);
				break;
			}
			case 10: /* Enter */
			{
			strcpy(str, item_name(current_item(my_menu)));
				pos_menu_cursor(my_menu);
				mvwprintw(my_menu_win, 7, 5, str);
				w = 1;
				break;
			}
			
		}
		if(w==1)
			break;
		wrefresh(my_menu_win);
	}	
	
	/* Free memory? */
	unpost_menu(my_menu);
	for(i = 0; i < n_choices; ++i)
		free_item(my_items[i]);
	free_menu(my_menu);
	endwin();
	if (strcmp(str,"Yes")==0){
		return 1;
	}else if (strcmp(str,"No")==0){
		return 0;
	}else{
		return -1;
	}
}

int print_menu(MENU *my_menu, int starty, int startx, int width, char *string, chtype color)
{	WINDOW *my_menu_win;
	noecho();
	int row, col, c, w=0;
	char * str = new char[10];
	my_menu_win = newwin(10,60,4,4);
	
	
	keypad(my_menu_win, TRUE);
	set_menu_win(my_menu, my_menu_win);
        set_menu_sub(my_menu, derwin(my_menu_win, 6, 58, 3, 1));


	/* Set fore ground and back ground of the menu */
	set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE);
	set_menu_back(my_menu, COLOR_PAIR(2));
	set_menu_grey(my_menu, COLOR_PAIR(3));

	
	
	/* Post the menu */
        box(my_menu_win, 0, 0);
	print_in_middle(my_menu_win, starty, startx, width, string, color);
	mvwaddch(my_menu_win, 2, 0, ACS_LTEE);
	mvwhline(my_menu_win, 2, 1, ACS_HLINE, 58);
	mvwaddch(my_menu_win, 2, 59, ACS_RTEE);
	mvprintw(LINES - 2, 0, "Up and Down arrow keys to navigate (F1 to Exit)");
 	mvprintw(LINES - 7, 0, "CIFFOLD 0.3 Copyright (C) K. Mitev 2005");
	mvprintw(LINES - 6, 0, "CIFFOLD comes with ABSOLUTELY NO WARRANTY"); 
	mvprintw(LINES - 5, 0, "This is free software, and you are welcome to redistribute it");
	mvprintw(LINES - 4, 0, "under certain conditions; see the GPL in file COPYING.");
	refresh();
        
	/* Post the menu */
	post_menu(my_menu);
	wrefresh(my_menu_win);

	while((c = wgetch(my_menu_win)) != KEY_F(1))
	{       switch(c)
	        {	case KEY_DOWN:
			{
				menu_driver(my_menu, REQ_DOWN_ITEM);
				break;
			}
			case KEY_UP:
			{
				menu_driver(my_menu, REQ_UP_ITEM);
				break;
			}
			case 10: /* Enter */
			{
			strcpy(str, item_name(current_item(my_menu)));
				pos_menu_cursor(my_menu);
				mvwprintw(my_menu_win, 7, 5, str);
				w = 1;
				break;
			}
		}
		if(w==1)
			break;
		wrefresh(my_menu_win);
	}	
	endwin();
	if (strcmp(str,"Do not change")==0){
		return 0;
	}else if (strcmp(str,"1.0")==0){
		return 1;
	}else if (strcmp(str,"1.1")==0){
		return 2;
	}	else{
		return -1;
	}
}
char * ask_win(char*string, chtype color, char*err)
{
	clear();
	WINDOW *win;
	echo();
	char mesg[]="Enter: ";
	int row, col;
	char * str = new char [255];
	win = newwin(10,60,4,4);
	box(win, 0, 0);
	print_in_middle(win, 1, 0, 60, string, color);
        mvwaddch(win, 2, 0, ACS_LTEE);
	mvwhline(win, 2, 1, ACS_HLINE, 58);
	mvwaddch(win, 2, 59, ACS_RTEE);
	wrefresh(win);
	mvprintw(LINES - 7, 0, "CIFFOLD 0.3 Copyright (C) K. Mitev 2005");
	mvprintw(LINES - 6, 0, "CIFFOLD comes with ABSOLUTELY NO WARRANTY"); 
	mvprintw(LINES - 5, 0, "This is free software, and you are welcome to redistribute it");
	mvprintw(LINES - 4, 0, "under certain conditions; see the GPL in file COPYING.");
	mvprintw(LINES - 2, 0, err);
	getmaxyx(win,row,col);
	mvprintw(row,(col-strlen(mesg))/2,"%s",mesg);
        getstr(str);
	endwin();
	return str;
}

void printError(int starty, int startx, int width, char *string, chtype color,char*err)
{	WINDOW *my_err_win;
	int row, col, c, w=0;
	char * str = new char[10];
	my_err_win = newwin(10,60,5,5);
	ITEM **my_items;				
	MENU *my_menu;
        int n_choices, i;
	char * error[] = 
{
	"Continue",
	"Exit Program"
};
	noecho();
		/* Initialize items */
        n_choices = ARRAY_SIZE(error);
        my_items = new ITEM* [n_choices+1];
	
	for(i = 0; i < n_choices; ++i)
                my_items[i] = new_item(error[i],NULL);
	
	my_items[n_choices] = (ITEM *)NULL;

	/* Create menu */
	my_menu = new_menu((ITEM **)my_items);

	
	keypad(my_err_win, TRUE);
	set_menu_win(my_menu, my_err_win);
        set_menu_sub(my_menu, derwin(my_err_win, 6, 58, 3, 1));


	/* Set fore ground and back ground of the menu */
	set_menu_fore(my_menu, COLOR_PAIR(1) | A_REVERSE);
	set_menu_back(my_menu, COLOR_PAIR(2));
	set_menu_grey(my_menu, COLOR_PAIR(3));

	
	
	/* Post the menu */
        box(my_err_win, 0, 0);
	
	
	print_in_middle(my_err_win, starty, startx, width, string, color);
	mvwaddch(my_err_win, 2, 0, ACS_LTEE);
	mvwhline(my_err_win, 2, 1, ACS_HLINE, 58);
	mvwaddch(my_err_win, 2, 59, ACS_RTEE);
	mvprintw(LINES - 2, 0, "Up and Down arrow keys to navigate (F1 to Exit)");
	mvprintw(LINES - 7, 0, "CIFFOLD 0.3 Copyright (C) K. Mitev 2005");
	mvprintw(LINES - 6, 0, "CIFFOLD comes with ABSOLUTELY NO WARRANTY"); 
	mvprintw(LINES - 5, 0, "This is free software, and you are welcome to redistribute it");
	mvprintw(LINES - 4, 0, "under certain conditions; see the GPL in file COPYING.");
	mvprintw(LINES - 3, 0, err);
 
	refresh();
        
	/* Post the menu */
	post_menu(my_menu);
	wrefresh(my_err_win);

	while((c = wgetch(my_err_win)) != KEY_F(1))
	{       switch(c)
	        {	case KEY_DOWN:
			{
				menu_driver(my_menu, REQ_DOWN_ITEM);
				break;
			}
			case KEY_UP:
			{
				menu_driver(my_menu, REQ_UP_ITEM);
				break;
			}
			case 10: /* Enter */
			{
			strcpy(str, item_name(current_item(my_menu)));
				pos_menu_cursor(my_menu);
				mvwprintw(my_err_win, 7, 5, str);
				w = 1;
				break;
			}
			
		}
		if(w==1)
			break;
		wrefresh(my_err_win);
	}	
	
	/* Free memory? */
	unpost_menu(my_menu);
	for(i = 0; i < n_choices; ++i)
		free_item(my_items[i]);
	free_menu(my_menu);
	endwin();
	if (strcmp(str,"Continue")){
		exit(-1);
	}
}
//prints stdin to stdout unchanged
void printStdinToStdout()
{
//	FILE *inputfile = fopen(stdin);
	int maxbuff = 3000;
	char *oneline = new char[maxbuff];
	
	while(fgets(oneline, maxbuff, stdin))
	{
		printf(oneline);
	}
}
