// saved from url
// http://www-cs.canisius.edu/PL_TUTORIALS/C++/EXAMPLES/MRD/SRC/shell.c -->

//-------------------------------- shell.c -------------------------------

//  This is the basic shell to MRD.  It provides for interactive prompting
//  in case you forget parameters.


#include <iostream.h>
#include <stdio.h>
#include <stdlib.h>
#include <string>
using namespace std;
#include <sys/types.h>
#include <sys/stat.h>
#include "relation.h"

extern bool validfilename(char * filename);
extern void mrdcommand (char *);

Parseline parms;

static int exists (char *filename)
{
     struct stat buf;
     return stat(filename, &buf) == 0;
}



void showall ()
{
printf ("\
batch filename\n\
info filename\n\
select oldrelation newrelation field1 > value1 or field2 = field2 ...\n\
cross relation1 relation2 newrelation\n\
join relation1 field1 relation2 field2 newrelation\n\
diff relation1 field1 relation2 field2 newrelation\n\
project relation1 newrelation field1 field2 field3 ...\n\
append relation1 relation2 newrelation\n\
print relation1\n\
sort oldrelation newrelation aord field1\n\
stats oldrelation newrelation field1\n\
number oldrelation newrelation\n\
unique oldrelation newrelation\n\
addrecord relation\n\
changevalue relation recnum fieldname newvalue\n\
renamefield relation fieldname newname\n\
changewidth relation fieldname newwidth\n\
deleterecord relation recnum\n\
addfield relation fieldname datatype printwidth defaultvalue\n\
retype relation field1 field2\n\
calc relation fieldname \"arithmetic expression\"\n\
!<system command> ...\n\
mrd \"@<command>\"\n\
quit\n");
}

void show1 () {
	if (strcmp (parms.word(1), "info") == 0) {
		printf ("info relation\n");
		printf ("For example: info colleges\n");
	}

	else if (strcmp (parms.word(1), "select") == 0) {
		printf ("select oldrelation newrelation field1 > value1 or field2 = field2 ...\n");
		printf ("For example: select colleges temp size > 5000\n");
	}

	else if (strcmp (parms.word(1), "cross") == 0) {
		printf ("cross relation1 relation2 newrelation\n");
		printf ("For example: cross family currentyear temp\n");
	}
	else if (strcmp (parms.word(1), "join") == 0) {
		printf ("join relation1 field1 relation2 field2 newrelation\n");
		printf ("For example: join family city states city temp\n");
	}
	else if (strcmp (parms.word(1), "project") == 0) {
		printf ("project relation1 newrelation field1 field2 field3 ...\n");
		printf ("For example: project colleges temp college city \n");
	}
	else if (strcmp (parms.word(1), "append") == 0) {
		printf ("append relation1 relation2 newrelation\n");
	}
	else if (strcmp (parms.word(1), "print") == 0) {
		printf ("print relation1\n");
		printf ("For example: print colleges\n");
	}
	else if (strcmp (parms.word(1), "sort") == 0) {
		printf ("sort oldrelation newrelation aord field1\n");
		printf ("For example: sort family temp a iq\n");
	}
	else if (strcmp (parms.word(1), "stats") == 0) {
		printf ("stats oldrelation newrelation field1\n");
		printf ("For example: stats family temp age\n");
	}
	else if (strcmp (parms.word(1), "number") == 0) {
		printf ("number oldrelation newrelation\n");
		printf ("For example: number colleges temp\n");
	}
	else if (strcmp (parms.word(1), "unique") == 0) {
		printf ("unique oldrelation newrelation\n");
		printf ("For example: unique colleges temp\n");
	}
	else if (strcmp (parms.word(1), "addrecord") == 0) {
		printf ("addrecord relation\n");
	}
	else if (strcmp (parms.word(1), "changevalue") == 0) {
		printf ("changevalue relation recnum fieldname newvalue\n");
	}
	else if (strcmp (parms.word(1), "renamefield") == 0) {
		printf ("renamefield relation fieldname newname\n");
	}
	else if (strcmp (parms.word(1), "changewidth") == 0) {
		printf ("changewidth relation fieldname newwidth\n");
	}
	else if (strcmp (parms.word(1), "deleterecord") == 0) {
		printf ("deleterecord relation recnum\n");
		printf ("For example: deleterecord colleges 0\n");
	}
	else if (strcmp (parms.word(1), "addfield") == 0) {
		printf ("addfield relation fieldname datatype printwidth defaultvalue\n");
	}
	else if (strcmp (parms.word(1), "calc") == 0) {
		printf ("calc oldrelation fieldname \"arithmetic expression\"\n");
	}
	else if (strcmp (parms.word(1), "retype") == 0) {
		printf ("retype relation field1 field2\n");
	}
	else if (strcmp (parms.word(1), "@") == 0) {
		printf ("mrd \"@<command>\"\n");
		printf ("For example: mrd \"@help\"\n");
	}
	else printf ("Unknown command\n");
}

int verify_field (char *fieldname, char *relation_name)
{
     Relation rel;
     rel.load (relation_name);
     if (rel.findfield (fieldname) == -1) return 0; 
     return 1;
}

void select_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This allows you to select certain rows out of a relation\n");
     printf ("thereby forming a new relation.\n");

     strcpy (com, "select ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp,1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }

     strcat (com, temp);
     strcat (com, " ");

     printf ("Give a name to the new, resulting relation: ");
     cin.getline(temp,1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("Now you will enter 1 or more conditions that are used to\n");
     printf ("select certain rows from the existing relation.\n");
     printf ("These all have the form\n");
     printf ("\n        fieldname  comparison  value\n");
     printf ("\nFor example:\n");
     printf ("\n        age  >  50\n");
     printf ("If the value is a character string AND contains blanks,\n");
     printf ("surround the value with double quotes.\n");
     printf ("The valid operators are =, !=, <, >, <= and >=\n");

     printf ("Please enter the first comparison, making sure that there are\n");
     printf ("blanks around the comparison operator!!!\n");
     printf ("    -->  ");

	 cin.getline(temp, 1000);

     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("Now you can enter more comparisons.  These can be connected\n");
     printf ("by \"and\" and \"or\".  There is no precedence among these.\n");

     while (1) {
          printf ("Add another comparison? (y/n) ");
          cin.getline(temp, 1000);
          if (strlen(temp) == 0) return;
          if (temp[0] != 'y') break;
          printf ("Enter \"and\" or \"or\": ");
          cin .getline(temp, 1000);
          if (strlen(temp) == 0) return;
          strcat (com, temp);
          strcat (com, " ");
          printf ("Enter comparison: ");
          cin.getline(temp, 1000);
          if (strlen(temp) == 0) return;
          strcat (com, temp);
          strcat (com, " ");
     }

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void project_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This allows you to select certain columns out of a relation\n");
     printf ("thereby forming a new relation.\n");

     strcpy (com, "project ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Give a name to the new, resulting relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("Now you will enter 1 or more field names, thereby identifying\n");
     printf ("which columns you wish to keep in the new relation.  Type each\n");
     printf ("field name on a line by itself and press return.  When done,\n");
     printf ("just press return on an empty line.\n");
	 
     while (1) {
          printf ("field name? ");
	      cin.getline(temp, 1000);
          if (strlen(temp) == 0) break;
          strcat (com, temp);
          strcat (com, " ");
     }

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void cross_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This allows you to splice together rows of two relations,\n");
     printf ("thereby forming a new relation.  It is like \"join\" but\n");
     printf ("there are no conditions.  All rows are spliced together.\n");

     strcpy (com, "cross ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of first existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Enter name of second existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Give a name to the new, resulting relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void join_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This allows you to splice together rows of two relations,\n");
     printf ("thereby forming a new relation.  It is sometimes called\n");
     printf ("equijoin because only rows that have equal values in a given\n");
     printf ("field are included in the output relation.\n");

     strcpy (com, "join ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of first existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Now give the name a field in this relation to be used in the\n");
     printf ("comparison: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("Enter name of second existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Now give the name a field in this relation to be used in the\n");
     printf ("comparison: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("Give a name to the new, resulting relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void append_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This allows you to append two relations to form a new one.\n");
     printf ("BEWARE!!!  It does not check to make sure the dictionaries\n");
     printf ("are the same!\n");

     strcpy (com, "append ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of first existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Enter name of second existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Give a name to the new, resulting relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void print_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This allows you to print a relation.\n");

     strcpy (com, "print ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void info_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This prints out the dictionary and current size of a relation.\n");

     strcpy (com, "info ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void sort_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This allows you to sort an existing relation and put the results\n");
     printf ("into a new relation.\n");

     strcpy (com, "sort ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Give a name to the new, resulting relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("Should the values be arranged in ascending or descending order?\n");
     printf ("Please enter \"a\" or \"d\" (without the quotes): ");
     cin >> temp;
     strcat (com, temp);
     strcat (com, " ");

     printf ("Which field do you wish to sort on? ");
     cin >> temp;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void stats_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This allows you to calculate statistics for an existing \n");
     printf ("relation and put the results into a new 1-record relation.\n");

     strcpy (com, "stats ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Give a name to the new, resulting relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("Which field do you wish to sort on? ");
     cin >> temp;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void number_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This inserts record numbers into the relation as a new field.\n");

     strcpy (com, "number ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Give a name to the new, resulting relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void unique_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This removes duplicate rows from a relation.\n");

     strcpy (com, "unique ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Give a name to the new, resulting relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void add_prompt ()
{
     char com[2000];
	 char com0[2000];
     char temp[1000];

     printf ("This adds a new record to an existing relation.\n");
     printf ("It merely copies the last record.  Later you can change the\n");
     printf ("values in this record as you like.\n");

     strcpy (com, "addrecord ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");
	 strcpy (com0, "printfields ");
	 strcat (com0, temp);
	
	 printf ("Enter the new record according to the following fields:\n");
	 mrdcommand(com0);

	 
	 cin.getline(temp, 1000);
	 if (strlen(temp) == 0) return;
	 strcat (com, temp);
	 strcat (com, " ");
	 

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void change_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This changes the value in a given record for a given field.\n");

     strcpy (com, "changevalue ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Which record do you wish to change?  The numbers start at 0: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");
     
     printf ("Which field do you wish to change?  Give its name: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");
     
     printf ("What is the new value?  If it is a string and contains a blank\n");
     printf ("Then surround it with double quotes: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");
     
     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void rename_field_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This renames a field.\n");

     strcpy (com, "renamefield ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("What is the old name of the field? ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("What is the new name of the field? ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void change_printwidth_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This changes the printing width of a field.\n");

     strcpy (com, "changewidth ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("What is the name of the field? ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("Enter the new printing width: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void del_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This deletes a record (row) from a relation.\n");

     strcpy (com, "deleterecord ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("Which record do you want to delete? The numbers start at 0: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void addfield_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This adds a field to a relation.\n");

     strcpy (com, "addfield ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("What is the name of the new field? ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("What is the type of the data?  Please enter a single letter:\n");
     printf ("  c=character string,  d=double (floating point),   i=integer\n");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("What is the printing width of the field? ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("What is the default value of the field? ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void calcfield_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This changes the value of a given field for every record.\n");
     printf ("The new value depends on an arithmetic formula you enter.\n");
     printf ("This arithmetic formula can contain parentheses, binary operators,\n");
     printf ("and the names of existing fields.\n");

     strcpy (com, "calcfield ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("What is the name of the field to change? ");
     cin >> temp;
     strcat (com, temp);
     strcat (com, " ");

     printf ("Now enter the arithmetic expression.  Surround it with double quotes.\n");
     cin >> temp;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void diff_prompt()
{
     printf ("Not yet implemented\n");
}

void retype_field_prompt ()
{
     char com[2000];
     char temp[1000];

     printf ("This changes the value of a given field for every record\n");
     printf ("by retyping the value of another field.\n");

     strcpy (com, "retype ");

     printf ("At any time while responding to prompts, if you change your \n");
     printf ("mind about doing this command, press RETURN on a blank.\n");

     printf ("Enter name of existing relation: ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
	 if (!validfilename(temp))
		  strcat(temp, ".txt");
     if (!exists(temp)) {
          printf ("This relation does not exist.\n");
          return;
     }
     strcat (com, temp);
     strcat (com, " ");

     printf ("What is the name of the source field from which to get values? ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("What is the name of the field to change? ");
     cin.getline(temp, 1000);
     if (strlen(temp) == 0) return;
     strcat (com, temp);
     strcat (com, " ");

     printf ("%s\n", com);
     printf ("GO AHEAD?  (y/n)==> ");
     cin >> temp;
     if (temp[0] != 'y') return;
     mrdcommand (com);
}

void batchcommands (char *filename)
{
     char line[1000];
     FILE *fp;
     Parseline parms;

     string fname(filename);
     fname += ".sh";

     if ((fp = fopen (filename, "r")) == NULL) {
         if ((fp = fopen (fname.data(), "r")) == NULL) {
             printf ("Cannot open file %s\n", filename);
             printf ("Cannot open file %s\n", fname.data());
             return;
         }
     }

     while (fgets (line, sizeof(line), fp) != NULL) {
          line[strlen(line)-1] = 0;
          parms.set(line);

          if (strcmp (parms.word(0), "batch") == 0)
               batchcommands (parms.word(1));
          else if (line[0] == '!')
               system (&line[1]);
          else if (line[0] == '#') {

			   printf("%s\n", line);
			   continue;
		  }
		  else 
               mrdcommand (line);
     }

     fclose (fp);
}

void interactive_mrdcommand (char *line)
{
     if (line[0] == '!') {
          system (line);
          return;
     }

     parms.set(line);

     if (strcmp (parms.word(0), "help") == 0) {
          if (parms.size() == 1) showall();
          else show1();
          return;
     }

     if (strcmp (parms.word(0), "version") == 0) {
          printf ("Mark's Relational Database V 2.0\n");
          return;
     }

     if (strcmp (parms.word(0), "batch") == 0) {
          batchcommands (parms.word(1));
          return;
     }

     if (parms.size() > 1) {
           mrdcommand (line);
           return;
     }

     else if (strcmp (parms.word(0), "select") == 0)
           select_prompt ();

     else if (strcmp (parms.word(0), "project") == 0)
           project_prompt ();

     else if (strcmp (parms.word(0), "cross") == 0)
           cross_prompt ();

     else if (strcmp (parms.word(0), "join") == 0)
           join_prompt ();

     else if (strcmp (parms.word(0), "diff") == 0)
           diff_prompt ();

     else if (strcmp (parms.word(0), "append") == 0)
           append_prompt ();

     else if (strcmp (parms.word(0), "print") == 0)
           print_prompt ();

     else if (strcmp (parms.word(0), "sort") == 0)
           sort_prompt ();

     else if (strcmp (parms.word(0), "stats") == 0)
           stats_prompt ();

     else if (strcmp (parms.word(0), "number") == 0)
           number_prompt ();

     else if (strcmp (parms.word(0), "unique") == 0)
           unique_prompt ();

     else if (strcmp (parms.word(0), "addrecord") == 0)
           add_prompt ();

     else if (strcmp (parms.word(0), "changevalue") == 0)
           change_prompt ();

     else if (strcmp (parms.word(0), "renamefield") == 0)
           rename_field_prompt ();

     else if (strcmp (parms.word(0), "changewidth") == 0)
           change_printwidth_prompt ();

     else if (strcmp (parms.word(0), "deleterecord") == 0)
           del_prompt ();

     else if (strcmp (parms.word(0), "addfield") == 0)
           addfield_prompt ();

     else if (strcmp (parms.word(0), "calcfield") == 0)
           calcfield_prompt ();

     else if (strcmp (parms.word(0), "retype") == 0)
           retype_field_prompt ();

     else if (strcmp (parms.word(0), "info") == 0)
           info_prompt ();

     else
           printf ("Unknown MRD command.\n");

}


int main (int argc, char *argv[])
{
     char line[1000];

     if (argc > 1) {
		 try {
          if (argv[1][0] == '@')
               mrdcommand (&argv[1][1]);
          else
               batchcommands (argv[1]);
		 }
		 catch (char * s)
		 {		
			printf("%s\n", s );
		 }
          exit(1);
     }

     while (1) {
          printf ("MRD> ");
          cin.getline(line, 1000);
		  if (strlen(line) == 0)
			   cin.getline(line, 1000);
          if (strcmp (line, "quit") == 0) break;
		try {
          if (line[0] == '!') {
               system (&line[1]);
               continue;
          }

          interactive_mrdcommand (line);
		}
		catch (char * s)
		{		
			printf("%s\n", s );
		}
     }
	 return 0;
}
