
#include <conio.h>
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include "defines.h"
#include "window.h"

PLAYER player[MAXFL][MAXSZ];       /* basic data array: names, scores, etc */
short outcome[MAXSZ][MAXSZ];       /* results of matches in a given flight */

/* location of score on hole 0..17 in formatted score string */
int ndx[] = {0,1,2,4,5,6,8,9,10,13,14,15,17,18,19,21,22,23};

/* par values of holes on course being played */
int par[18];
/* handicap order of holes 0..17 for course being played */
int order[18];

/* flags for recalculation of flight */
int recalc[] = {0,0,0,0,0,0,0,0,0,0};

int NP[MAXFL];        /* number of players in each flight */
int numflights;       /* number of flights */

/* attributes for various screens */
int norm_attr=16*3+8, high_attr=16*7+8, old_attr;
int mono = 0;                 /* flag */
char input_file[13];          /* name of entry list or olddata file */
char datafile[13];            /* name of results file for current tournament */
char month[3], day[3];        /* for default datafile = MPmmdd */
int desc_flag;                /* flag for display of menu descriptions */
char mark[] = {176,'\0'};     /* single shaded block */
char namefield[] =            /* 19 marks */
    {176,176,176,176,176,176,176,176,176,176,176,176,176,176,
     176,176,176,176,176,'\0'};
char filefield[] =            /* 13 marks */
    {176,176,176,176,176,176,176,176,176,176,176,176,176,'\0'};
char hcpfield[] = {176,176,176,'\0'};   /* negative hcps are temporary */
char scorefield[] = {176,176,176,45,176,176,176,45,176,176,176,45,45,
                         176,176,176,45,176,176,176,45,176,176,176,'\0'};

int no_space = 0;             /* flag for get_string() */
int left = 28, top = 1, right = 80, bottom = 25;   /* for data window */
char data_title[81];

/* function prototypes */
void
enter(),
check(void),
display(void),
print(void),
call_kb_entry(int),
call_list_results(void),
high(int,int,int,int,int, char *),
norm(int,int,int,int,int, char *),
call_print_matches(void),
call_print_results(void),
main_menu(int),
call_list_data(),
normal_exit(int),
rect(int,int,int,int,int,int),
std_screen(void);

int
  get_entries(FILE *pf),
  get_old_data(FILE *fp),
  get_filename(int, char *),
  get_flight(int),
  get_type(void),
  pick_one(int,int,int *,int *),
  menu_select(int,int,int,int,int,char *,int,int,int,int,int,int,
                int,int,int,int *,char **);

extern void
  calc_results(int),
  chk(int),
  compare_two(void),
  hide_cursor(void),
  list_gross(int),
  list_matches(void),
  list_net(int),
  list_results(int),
  print_gross(int),
  print_net(int),
  print_results(int),
  print_matches(int),
  showcursor(void);
extern int
  error_msg(char *, int, int),
  get_string(char *, char *, char *, int, int),
  find(char *, int *),
  findn(char *, int *, int *),
  Getint(int, char *,int *);

/*===========================================================================*/

main()
{
  FILE *fp;
  int i;
  struct text_info oldtext;

  clrscr();
  gettextinfo(&oldtext);           /* original text settings */
  old_attr = oldtext.normattr;
  if (oldtext.currmode == '\7') {
    mono = 1;
    norm_attr = 7;
    high_attr = 16*7;
  }
  initialize();

  main_menu(0);
}

/*===========================================================================*/

static void main_menu(int current)
{
  int scol=8,srow=5,ecol=21,erow=12,border = 2;
  int tag_norm = 16*3+4;
  int tag_high = 16*7+4;
  char *item[] = {
         "  Enter   ",
         "  Display ",
         "  Print   ",
         "  Check   ",
         "  Quit    " };
  int numitems = 5; int tag_indx=2;
  int selection;
  int ltr[] = {'e','d','p','c','q'};

  if (mono) tag_norm = tag_high = norm_attr;
  std_screen();
  desc_flag = 1;
  selection = menu_select(scol,srow,ecol,erow,border,"",norm_attr,high_attr,
                tag_norm,tag_high,tag_indx,numitems-1,current,2,2,ltr,item);
  desc_flag = 0;
  if (selection == 0) {
    enter();
    main_menu(0);
  }
  else if (selection == 1)
    display();
  else if (selection == 2)
    print();
  else if (selection == 3)
    check();
  else if (selection == 4 || selection == 27)
    normal_exit(0);
}

void
std_screen()            /* set up standard screens */
{
  window(28,1,80,25);
  textattr(norm_attr);
  clrscr();
  window(1,1,80,25);
  textattr(7);
  rect(1,1,27,25,7,1);
  window(2,2,26,24);
  textattr(high_attr);
  clrscr();
}

/* ------------------------- show menu, make selection -------------------- */
menu_select(scol,srow,ecol,erow,border,title,norm_attr,high_attr,tag_norm,
            tag_high,tag_indx,max,current,col0,row0,ltr,item)
int scol,srow,ecol,erow,border,norm_attr,high_attr,tag_norm,tag_high,
            tag_indx,max,current,col0,row0,ltr[];
char *title, *item[];
{
  int i, c, numrows=max;
  int col;

  hide_cursor();
  window(scol,srow,ecol,erow);
  textattr(norm_attr);
  clrscr();
  if (border)
    rect(scol,srow,ecol,erow,norm_attr,border);
  if (title[0] && strlen(title) < ecol-scol-2) {
    col = (scol+ecol-strlen(title))/2 - scol + 2;
    gotoxy(col,1);
    cputs(title);
  }
  for (i=0; i<=numrows; i++) {
    norm(1+col0,i+row0,norm_attr,tag_norm,tag_indx,item[i]);
  }
  high(1+col0,current+row0,high_attr,tag_high, tag_indx, item[current]);

  while (kbhit())
    getch();                              /* clear kb buffer */
  while ((c=getkey()) != ESC)             /* until Esc key pressed */
    {
     switch(c)
      {
       case UP:
         norm(1+col0,current+row0,norm_attr,tag_norm,tag_indx,item[current]);
         if (current == 0)
           current = max;
         else
           current--;
         high(1+col0,current+row0,high_attr,tag_high,tag_indx,item[current]);
         break;
       case DN:
         norm(1+col0,current+row0,norm_attr,tag_norm,tag_indx,item[current]);
         if (current == max)
           current = 0;
         else
           current++;
         high(1+col0,current+row0,high_attr,tag_high,tag_indx,item[current]);
         break;
        case 13:                   /* make selection when Enter key pressed */
          return current;
        default:      /* if letter command, change highlight and select */
          for (i=0; i<=max; i++)
            if (ltr[i] == c || ltr[i]-32 == c)
              {
               norm(1+col0,current+row0,norm_attr,tag_norm,tag_indx,item[current]);
               current = i;
               high(1+col0,current+row0,high_attr,tag_high,tag_indx,item[current]);
               return current;
              }
        }
     }
}

/* -------------------------------------- */
void
norm(int col, int row, int attr, int tag_attr, int tag_indx, char *s)
{
  textattr(attr);
  gotoxy(col,row);
  cputs(s);
  textattr(tag_attr);
  gotoxy(col+tag_indx,row);
  cprintf("%c",s[tag_indx]);
  if (desc_flag) {
    textattr(norm_attr);
    window (1,1,80,25);
    if (desc_flag == 1)
      gotoxy(28,4+row);
    else
      gotoxy(28,7+row);
    clreol();                       /* erase main_menu description */
    if (desc_flag == 1)
      window(8,5,21,12);
    else
      window(8,8,21,14);
  }
}

/* -------------------------------------- */
void
high(int col,int row, int attr, int tag_attr, int tag_indx, char *s)
{
  static char *description1[] = {  /* for main menu */
         "  Enter scores ",
         "  On screen display of current data ",
         "  Printout of current results ",
         "  Check and/or correct current data ",
         "" };
  char *description2[21] = {        /* for 'display()' menu */
         "  Current entrants, hcps, scores",
         "  Current standings",
         "  Matches won/lost by specified player",
         "  Match between two specified players"};
  struct text_info text;
  gettextinfo(&text);

  textattr(attr);
  gotoxy(col,row);
  cputs(s);
  textattr(tag_attr);
  gotoxy(col+tag_indx,row);
  cprintf("%c",s[tag_indx]);
  if (desc_flag) {
    window (1,1,80,25);
    textattr(norm_attr);
    if (desc_flag == 1) {
      gotoxy(28,4+row);
      cputs(description1[row-2]);      /* 'current' = row - 2 */
      window(8,5,21,12);
    }
    else {
      gotoxy(28,7+row);
      cputs(description2[row-2]);
      window(8,8,21,14);
    }
  }
}
/* ------------------------- main_menu functions ------------------------- */
void
enter()
{
  int flight;

  if (numflights == 0)
    error_msg("No data",9,9);
  else
    score_entry();
  main_menu(0);
}

void
display()
{
  int scol=8,srow=8,ecol=21,erow=13;
  int norm_attr1=WHITE+16*BROWN;
  int high_attr1=WHITE+16*BLACK;
  int tag_attr1=YELLOW+16*BROWN;
  int tag_high1=YELLOW+16*BLACK;
  char *item[] = {" Entries  ",
                  " Standings",
                  " Matches  ",
                  " Two X Two"};
  int numitems = 4, tag_indx = 1;
  static int current = 0;
  int selection;
  int ltr[] = {'e','s','m','t'};

  if (mono) {
    norm_attr1 = tag_attr1 = norm_attr;
    high_attr1 = tag_high1 = high_attr;
  }
  desc_flag = 2;
  selection = menu_select(scol,srow,ecol,erow,1,"",norm_attr1,high_attr1,
           tag_attr1,tag_high1,tag_indx,numitems-1,current,2,2,ltr,item);
  desc_flag = 0;
  if (selection == 27)
    main_menu(1);
  current = selection;
  switch(selection) {
    case 0:
      call_list_data();
      break;
    case 1:
      call_list_results();
      break;
    case 2:
      list_matches();
      break;
    case 3:
      compare_two();
      break;
  }
  main_menu(1);
}

/*---------------------------------------------------------------------------*/
void
print()
{
  int scol=8,srow=9,ecol=20,erow=13;
  int norm_attr1=WHITE+16*BROWN;
  int high_attr1=WHITE+16*BLACK;
  int tag_attr1=YELLOW+16*BROWN;
  int tag_high1=YELLOW+16*BLACK;
  char *item[] = {" Standings",
                  " Matches  "};
  int numitems = 2, tag_indx = 1, current = 0;
  int selection;
  int ltr[] = {'s','m'};
  int printer_status;

  if (mono) {
    norm_attr1 = tag_attr1 = norm_attr;
    high_attr1 = tag_high1 = high_attr;
  }
  selection = menu_select(scol,srow,ecol,erow,1,"",norm_attr1,high_attr1,
              tag_attr1,tag_high1,tag_indx,numitems-1,current,2,2,ltr,item);

  if (selection == 27)
    main_menu(2);               /* Esc pressed */

  if (biosprint(2,0,0) & 8) {
    error_msg("Printer not ready",scol,erow+1);
    main_menu(2);
  }

  switch(selection) {
    case 0:
      call_print_results();
      break;
    case 1:
      call_print_matches();
      break;
    }
}

/*---------------------------------------------------------------------------*/
void
check(void)
{
  int i,k,m,flight;
  char pname[21], h[3], s[10], scores[25];
  FILE *fp;
  int norm_attr1=norm_attr;  /* WHITE+16*BROWN; */
  int flt[10],index[10];
  int left1=40,top1=3,right1=62,bottom1=4;
  char *msg[] = {
         "       CHECK        ",
         "",
         "Enter names, correct",
         "data as necessary.  ",
         "",
         "To return to menu,  ",
         "   press Esc or     ",
         "enter a blank name. "};
  int num_msgs = 8;

  if (mono) norm_attr1 = norm_attr;

  window(2,2,26,24);
  textattr(high_attr);
  clrscr();
  for (i=0; i<num_msgs; i++) {
    gotoxy(3,i+4);
    cputs(msg[i]);
  }
  while(1) {
    window(left,top,right,bottom);
    textattr(norm_attr);
    clrscr();
    window(1,1,80,25);
    rect(left1-1,top1-1,right1+1,bottom1+1,norm_attr1,1);
    window(left1,top1,right1,bottom1);
    clrscr();
    gotoxy(2,1);
    cprintf("Enter name:");
    gotoxy(2,2);
    showcursor();
    i = get_string("",pname,"",2,2);
    if (!i)
      break;
    m = findn(pname,flt,index);
    if (m == -2) {
      error_msg("More than ten",left1+2,top1+3);
      continue;
    }
    if (m == -1) {
      error_msg("Not found",left1+2,top1+3);
      continue;
    }
    if (m > 0)
      m = pick_one(bottom1+2, m, flt, index);
    if (m == -1 || m == 27)
      continue;
    flight = flt[m];
    correct(flt[m], index[m]);
  }

  i = 0;   /* used as flag */
  for (flight=0; flight<numflights; flight++)
    if (recalc[flight]) {
             /* if any changes were made, recalculate and store data */
      i = 1;
      calc_results(flight);
      recalc[flight] = 0;
    }
    if (i) {
      fp = fopen(datafile,"w");
      write_data(fp);
      fclose(fp);
    }
  main_menu(1);           /* assume 'display' next */
}

/* -------------------Calls to MP functions ------------------------------ */

void
call_list_data()
{
  int flight = 0;
  if (NP[flight] == 0)
    error_msg("No Data Entered",7+1,7+5);
  else {
    chk(flight);
  }
  main_menu(1);
}

/*---------------------------------------------------------------------------*/
void
call_list_results()
{
  int flight=0,type;

  type = get_type();
  if (type == 0)
    list_results(flight);
  else if (type == 1)
    list_points(flight);
  else if (type == 2)
    list_gross(flight);
  else if (type == 3)
    list_net(flight);
  main_menu(1);
}

/*---------------------------------------------------------------------------*/
void
call_print_results()
{
  int flight,type;

  if (numflights > 1)
    flight = get_flight(11);
  else flight = 0;
  if (NP[flight] == 0) {
    error_msg("Empty flight",7+2,7+12);
    main_menu(2);
  }
  type = get_type();
  if (type == 0)
    print_results(flight);
  else if (type == 1)
    print_points(flight);
  else if (type == 2)
    print_gross(flight);
  else if (type == 3)
    print_net(flight);
  main_menu(2);
}

/*---------------------------------------------------------------------------*/
void
call_print_matches()
{
  int flight;

  if (numflights > 1)
    flight = get_flight(12);
  else flight = 0;
  if (NP[flight] == 0)
    error_msg("Empty flight",7+2,7+13);
  else
    print_matches(flight);
  main_menu(2);
}


/* ----------------------------- */
int
get_flight(int row)
{
  char s[3], mask[] = {176,'\0'};
  int r, ret_val;
  struct text_info text;

  gettextinfo(&text);
  window(9,row,19,row);
  textattr(BLACK+16*LIGHTGRAY);
  clrscr();
  r = get_string("Flight",s,mask,10,1);
  if (!r)
    ret_val = 0;
  else
    ret_val = atoi(s);
  window(text.winleft, text.wintop, text.winright, text.winbottom);
  textattr(text.attribute);
  return ret_val;
}

/* ----------------------------- */
int
get_type()
{
  char *item[] = {" Match Play   ",
                  " Point System ",
                  " Gross Scores ",
                  " Net Scores    " };
  int numitems = 4, tag_indx = 1, current = 0;
  int selection;
  int ltr[] = {'m','p','g','n'};
  int tag_attr1=YELLOW+16*BLUE;
  int norm_attr1 = WHITE+16*BLUE;
  int high_attr1 = BLUE+16*LIGHTGRAY;
  int tag_high1 = YELLOW+16*LIGHTGRAY;
  struct text_info text;

  gettextinfo(&text);
  if (mono) {
    norm_attr1 = tag_attr1 = norm_attr;
    high_attr1 = tag_high1 = high_attr;
  }
  textattr(norm_attr1);
  selection = menu_select(8,11,24,16,1,"",norm_attr1,high_attr1,tag_attr1,
                tag_high1,tag_indx,numitems-1,current,1,2,ltr,item);
  if (selection == 27)
    main_menu(1);
  window(text.winleft, text.wintop, text.winright, text.winbottom);
  return selection;
}

/* ----------------------------- */
int
get_filename(int row, char *filename)
{
  char filemask[] = {176,176,176,176,176,176,176,176,176,176,176,176,176,0};
  int r;
  char s[13];

  textattr(norm_attr);
  window(7,row,24,row+4);
  clrscr();
  rect(8,row,24,row+4,norm_attr,1);
  gotoxy(2,2);
  if (strcmp(filename,"entrants") == 0)
    cputs("Entry File:");
  else
    cputs("Data file:");
  r = get_string("",s,filemask,2,3);
  if (r)
    strcpy(filename,s);
  return r;
}

/* ------------------------- choose player from list ------------------ */
/* m = number of choices <= 10; arrays flt[], index[] must be filled to m-1 */
int
pick_one(int row, int m, int flt[], int index[])
{
  struct text_info text;
  char *buf;
  char *full_name[10] = {"                      ",
                         "                      ",
                         "                      ",
                         "                      ",
                         "                      ",
                         "                      ",
                         "                      ",
                         "                      ",
                         "                      ",
                         "                      " };
  int i,k;
  int ltr[] = {0,0,0,0,0,0,0,0,0,0};
  extern int high_attr, norm_attr;

  buf = (char *)malloc(2*(m+2)*22);
  if (buf == NULL) {
    error_msg("Not enough memory",30,5);
    return -1;
  }

  gettextinfo(&text);
  for (i=0; i<=m; i++)
    strcpy(full_name[i],player[flt[i]][index[i]].name);
  window(1,1,80,25);
  gettext(30,row,52,row+m+2,buf);
  k = menu_select(30,row,52,row+m+2,             /* left,top,right,bottom */
                  1,                             /* single border */
                  " Pick One ",                  /* title */
                  high_attr,norm_attr,           /* norm,high */
                  high_attr,norm_attr,           /* tag,tag high */
                  1,                             /* no tag */
                  m,                             /* number of items */
                  0,2,2,                  /* current, start col, start row */
                  ltr,                           /* letters to select */
                  full_name);                    /* items to select */
  puttext(30,row,52,row+m+2,buf);
  free(buf);
  window(text.winleft,text.wintop,text.winright,text.winbottom);
  textattr(text.attribute);
  return k;
}

/*---------------------------------------------------------------------------*/
/* rectangle drawing routine for borders */
/* box characters */
int tlc[3]={0,218,201};
int trc[3]={0,191,187};
int blc[3]={0,192,200};
int brc[3]={0,217,188};  /* single,double corners */
int hl[3]={0,196,205};
int vl[3]={0,179,186};   /* single,double horizontal, vertical lines */

void
rect(int left, int top, int right, int bottom, int attr, int border)
{
  int i;
  int width, height;

  width = right-left;
  height = bottom-top;

  window(left,top,right,bottom);
  gotoxy(1,1);
  putch(tlc[border]);
  for (i=1; i<width; i++)
    putch(hl[border]);
  putch(trc[border]);
  for (i=0; i<height-1; i++) {
    gotoxy(1,2+i);
    putch(vl[border]);
    gotoxy(1+width,2+i);
    putch(vl[border]);
  }
  gotoxy(1,height+1);
  putch(blc[border]);
  for (i=1; i<width; i++)
    putch(hl[border]);
  _AH = 9;
  _AL = brc[border];
  _BH = 0;
  _BL = attr;
  _CX = 1;
  geninterrupt(0x10);
  window(left,top,right,bottom);
}

/*---------------------------------------------------------------------------*/
initialize()
{
  FILE *fp;
  char filename[32], s[80];
  char *opening[] = {
             "              MATCHPLAY                     ",
             "                                            ",
             "        Golf Tournament Organizer           ",
             "                                            ",
             " A program for scoring a tournament in      ",
             " which each player competes at match play   ",
             " with with each other player in his flight. ",
             "                                            ",
             "               B. J. Ball                   ",
             "           3304 Glen Rose Drive             ",
             "             Austin, TX 78731               ",
             NULL
  };

  char *description[] = {
    " This program will score a golf tournament under several formats.  ",
    " Gross and Net scores are always calculated, as are the 'points'   ",
    " obtained under the point system described in the documentation.   ",
    " The unusual feature, however, is that the tournament may be scored",
    " in match play format, with each player playing at match play with ",
    " each other player in his flight, receiving one match play point   ",
    " for each match won and one-half point for each match halved. In   ",
    " each match, strokes are awarded the weaker player, with these     ",
    " strokes taken on the on the appropriate holes, based on handicap. ",
    NULL
  };

  char *description1[] = {
    " The program requires an entry list file, in the format described  ",
    " in the documentation. This file may be created with any standard  ",
    " word processor. As scores are entered, they will be stored in a   ",
    " 'results' file; if the program should be interrupted for any      ",
    " reason, it may be restarted by loading this file, rather than the ",
    " entry list file. You will be asked for the name for this file at  ",
    " the start of the program. If there is an existing file with the   ",
    " same name, it will be overwritten with the new results file.      ",
    NULL
  };

  int left = 17, top = 3, right = 62, bottom = 15;
  int left1= 6,  top1= 3, right1= 74, bottom1= 13;

/* opening display */
  textattr(7);
  clrscr();
  establish_window(left,top,right,bottom,15,1,0);
  text_window(opening,1);
  window(1,1,80,25);
  textattr(7);
  gotoxy(left1,25);
  cputs("Press any key (Esc to exit) ...");
  if (getch()==27) normal_exit(1);
  delete_window();
  clrscr();
  establish_window(left1,top1,right1,bottom1,15,1,0);
  window_title(" MTCHPLAY ");
  text_window(description,1);
  window(1,1,80,25);
  textattr(7);
  gotoxy(left1,25);
  cputs("Press any key (Esc to exit) ...");
  if (getch()==27) normal_exit(1);
  textattr(31);
  clear_window();
  window_title(" MTCHPLAY ");
  text_window(description1,1);
  window(1,1,80,25);
  textattr(7);
  gotoxy(left1,25);
  cputs("Press any key (Esc to exit) ...");
  if (getch()==27) normal_exit(1);
  delete_window();
  gotoxy(left1,25);
  cputs("                                 ");
a:
  get_start_data();
  if (!numflights) {
    gotoxy(19,20);
    cprintf("Error in input file; please reenter.");
    if (getch() == 27)
      normal_exit(1);
   goto a;
  }
}

get_start_data()
{
  int c, olddata;
  FILE *fp;
  struct date dt;
  char dfault[13];

/* set up default file name */
  getdate(&dt);
  sprintf(month, "%.2d", dt.da_mon);
  sprintf(day, "%.2d", dt.da_day);
  strcpy(dfault,"MP");
  strcat(dfault,month);
  strcat(dfault,day);

  textattr(31);
  establish_window(16,15,60,21,8,3,0);
  delete_window();
  window(1,1,80,25);
  textattr(16*3+8);
  gotoxy(17,16);
  showcursor();
  cputs("Reload previous data (Y/N)? ");
  if ((c = getch()) == 27)
    normal_exit(1);
  if (toupper((char) c) == 'Y')
    olddata=1;
  else olddata = 0;
a:
  if(olddata) {
    gotoxy(17,17);
    cprintf("Default: %s",dfault);
  }
  get_string("Name of input file",input_file,filefield,37,17);
  if (!input_file[0]) {
    if (olddata)
      strcpy (input_file,dfault);
    else {
      gotoxy(17,18);
      cputs("This program requires an input file");
      gotoxy(17,19);
      cputs("Press Esc to exit, any other key to reenter");
      if (getch() == 27)
        normal_exit(1);
      gotoxy(17,18); cputs("                                   ");
      gotoxy(17,19); cputs("                                            ");
      goto a;
    }
  }
  if ((fp = fopen(input_file,"r")) == NULL) {
    gotoxy(17,18);
    cputs("Not found; please reenter. (Esc to exit)");
    if (getch() == 27)
      normal_exit(1);
    gotoxy(17,18);
    cputs("                                        ");
    goto a;
  }
b:
  gotoxy(17,19);
  cprintf("Default: %s",dfault);
  get_string("Name of output file",datafile,filefield,38,18);
  if (!datafile[0])
    strcpy (datafile,dfault);
  if ((fp = fopen(datafile,"r")) == NULL) {
    gotoxy(17,20);
    cputs("Not found; please reenter. (Esc to exit)");
    if (getch() == 27)
      normal_exit(1);
    gotoxy(17,20);
    cputs("                                       ");
    goto b;
  }
  numflights = 0;
  if (olddata)
    get_old_data(fp);
  else
    get_entrants(fp);
  fclose(fp);
  if (numflights == 0) {
    gotoxy(17,19);
    cputs("Error in input file; please reenter.");
    if (getch() == 27)
      normal_exit(1);
    gotoxy(17,19);
    cputs("                                    ");
    gotoxy(17,18);
    cputs("                                   ");
    goto a;
  }
}

/*---------------------------------------------------------------------------*/
/* input old (possibly incomplete) tournament data from file */
get_old_data(FILE *fp)
{
   int i,k,n,blank=0;
   char s[81];
   int np=0,flight=0;
   extern par[], order[];

   for (k=0; k<MAXFL; k++)
     NP[k]= 0;
   numflights = 0;

   /* 1st non-blank line should be the title, if any, or else the par values */
   while (getline(fp,s) == blank)
     ;
   for (i=0; i<strlen(s); i++)
     if (s[i] != ' ') break;
   if (isalpha(s[i]))
     strcpy (data_title,s);
   else
     data_title[0] = '\0';
   if (isdigit(s[0])) goto b;

   /* next non-blank line should give the par values of the holes */
   while (getline(fp,s) == blank)
     ;
b:
   if (Getint(18,s,par) != 18)
     return;  /* since numflights = 0; error message will be printed */

   /* next non_blank line should give course handicap values of the holes */
   while (getline(fp,s) == blank)
     ;
   if (Getint(18,s,order) != 18)
     return;  /* since numflights = 0; error message will be printed */

   /* the line immediately after the handicap values MUST be blank */
   /* hereafter, blank lines are used only to separate flights */
   if (getline(fp,s) != blank)
     return;  /* since numflights = 0; error message will be printed */

  /* start of data entry */
  while (!feof(fp))
  {
    getline(fp,s);
    if (s[0] == '\0') {            /* blank line */
      if (np != 0) {
        NP[flight++] = np;           /* end of flight */
        np = 0;
        continue;
      }
      else
        goto a;
    }
      strcpy(player[flight][np].name,s);
      getline(fp,s);
      player[flight][np].hcp = atoi(s);
      getline(fp,s);
      if (isdigit(s[0]))
        strcpy(player[flight][np++].scores,s);
      else
        player[flight][np++].scores[0] = 0;
  }
a:
  numflights = flight;
  for (flight=0; flight<numflights; flight++)
    set_gnp(flight);                /* calculate gross, net, points */
  calc_all();                       /* calculate match play info */
}

/*---------------------------------------------------------------------------*/
/* input entry list (names and handicaps only */
get_entrants(FILE *fp)
{
   int i,k,n,blank=0;
   char s[81];
   int np=0,flight=0;
   extern par[], order[];

   for (k=0; k<MAXFL; k++) NP[k]=0;        /* clear existing data */
   numflights = 0;

   /* 1st non-blank line should be the title, if any, or else the par values */
   while (getline(fp,s) == blank)
     ;
   for (i=0; i<strlen(s); i++)
     if (s[i] != ' ') break;
   if (isalpha(s[i]))
     strcpy (data_title,s);
   else
     data_title[0] = '\0';
   if (isdigit(s[0])) goto b;

   /* next non-blank line should give the par values of the holes */
   while (getline(fp,s) == blank)
     ;
b:
   if (Getint(18,s,par) != 18)
     return;  /* since numflights = 0; error message will be printed */

   /* next non_blank line should give course handicap values of the holes */
   while (getline(fp,s) == blank)
     ;
   if (Getint(18,s,order) != 18)
     return;  /* since numflights = 0; error message will be printed */

   /* the line immediately after the handicap values MUST be blank */
   /* hereafter, blank lines are used only to separate flights */
   if (getline(fp,s) != blank)
     return;

  /* start of data entry */
  while (!feof(fp))
  {
    getline(fp,s);
    if (s[0] == '\0') {              /* blank line */
      if (np != 0) {
        NP[flight++] = np;           /* end of flight */
        np = 0;
        continue;
      }
      else
        goto a;
    }
    if (isalpha(s[0])) {
      strcpy(player[flight][np].name,s);
      getline(fp,s);
      player[flight][np].hcp = atoi(s);
      np++;
    }
  }
a:
  numflights = flight;
}

/*---------------------------------------------------------------------------*/
void normal_exit(int m)
{
  int c;

  window(1,1,80,25);
  if (m == 0) {
    c = error_msg("Quit program (Y/N)? ",30,3);
    if (toupper(c) != 'Y') {
      main_menu(0);
    }
  }
  textattr(old_attr);
  clrscr();
  showcursor();
  exit(0);
}

